0% found this document useful (0 votes)
16 views15 pages

17 2 Final

The document is a final exam for CS 320 from Fall 2017, consisting of various questions related to programming language concepts, including memory management, interpreter implementation, evaluation of expressions, and operational semantics. It requires students to summarize course learnings, analyze code snippets, and demonstrate understanding of language semantics and type systems. The exam is closed-book and has a strict time limit of 180 minutes.

Uploaded by

전성진
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

17 2 Final

The document is a final exam for CS 320 from Fall 2017, consisting of various questions related to programming language concepts, including memory management, interpreter implementation, evaluation of expressions, and operational semantics. It requires students to summarize course learnings, analyze code snippets, and demonstrate understanding of language semantics and type systems. The exam is closed-book and has a strict time limit of 180 minutes.

Uploaded by

전성진
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Final Exam

CS 320, Fall 2017


Student id: Name:

Instructions: You have 180 minutes to complete this closed-book, closed-note, closed-computer exam. Please
write all answers in the provided space. Korean students should write your answers in Korean.

1) (5pts) Summarize what you learned from this course in one sentence.

2) (5pts) Write what you learned from the “Growing a Language” video lecture in one sentence.

3) (5pts) Suppose a garbage-collected interepreter uses the following five kinds of records:

– Tag 1: a record containing two pointers


– Tag 2: a record containing one pointer and one integer
– Tag 3: a record containing one integer
– Tag 4: a record containing one integer and one pointer
– Tag 99: forwarding pointer (to to-space)

The interpreter has one register, which always contains a pointer, and a memory pool of size 26. The
allocator/collector is a two-space copying collector, so each space is of size 13. Records are allocated
consecutively in to-space, starting from the first memory location, 0.
The following is a snapshot of memory just before a collection where all memory has been allocated:

– Register: 8
– From space: 1 3 8 3 0 4 7 3 2 0 8 3 42

What are the values in the register, the from-space, and the to-space after collection? Assume that
unallocated memory in to-space contains 0.

– Register:

– From space:

– To space:

1
4) (5pts) The following code is an excerpt from the implementation of the interpreter for BFAE:

(define (store-lookup addr st)


(type-case Store st
[mtSto () (error ’store-lookup "unallocated")]
[aSto (sto-addr val rest-st)
(if (= addr sto-addr)
val
(store-lookup addr rest-st))]))

(define (interp expr ds st)


...
[setbox (bx-expr val-expr)
(interp-two bx-expr val-expr ds st
(lambda (bx-val val st1)
(v*s val
(aSto (boxV-address bx-val) val st1))))]
[openbox (bx-expr)
(type-case Value*Store (interp bx-expr ds st)
[v*s (bx-val st1)
(v*s (store-lookup (boxV-address bx-val) st1)
st1)])]
...)

Change the implementation of interp for the setbox case so that the old value of the box is dropped
(i.e., replaced with the new value) instead of merely hidden by the outside-in search order of store-lookup.
Define your helper function with its purpose and contract.

2
5) (10pts) For each of the following LFAE expressions, show how it is evaluated and its result.

a) {{fun {x} {x 8}} {+ 42 {fun {y} {- y x}}}}

b) {{fun {y} {{fun {x} {+ 3 x}} y}} 5}

3
6) (10pts) The following code is an excerpt from the implementation of the interpreter for BMFAE:

(define (interp expr ds st)


...
[app (f a)
(if (id? a)
(type-case Value*Store (interp f ds st)
[v*s (fv st1)
(local [(define b (lookup (id-name a) ds))]
(interp (closureV-body fv)
(aSub (closureV-param fv) b (closureV-ds fv))
st1))])
(interp-two f a ds st
(lambda (fv av st1)
(local [(define a (malloc st1))]
(interp (closureV-body fv)
(aSub (closureV-param fv) a (closureV-ds fv))
(aSto a av st1))))))]
...)

a) What is the calling convention of this semantics?

Consider the following expression in BMFAE:

{with {n 42}
{with {f {fun {g} {g n}}}
{f {fun {x} {+ x 8}}}}}
b) Show the environment and store just before evaluating addition in the call-by-reference semantics.

c) Show the environment and store just before evaluating addition in the call-by-value semantics.

4
7) (5pts) What is the evaluation result of the following code?

(define table empty)


(define (remember v)
(local [(define n (length table))]
(begin (set! table (append table (list v)))
n)))
(define (lookup key) (list-ref table key))

(define (web-read prompt)


(let/cc cont
(local [(define key (remember cont))]
(error ’web-read "~a; to continue, call resume with ~a and value" prompt key))))

(define (web-read-each prompts)


(map web-read prompts))

(define (m)
(apply format "my ~a saw a ~a rock" (web-read-each ‘("noun" "adjective"))))

(m)

8) (5pts) Consider the following partial implementation of the compile function:

; compile : FAE CDefrdSub -> CFAE


(define (compile fae ds)
(type-case FAE fae
...
[id (name) (cid (locate name ds))]
[fun (param body)
(cfun (compile body (aCSub param ds)))]
...))

(define (locate name ds)


(type-case CDefrdSub ds
[mtCSub () (error ’locate "free variable")]
[aCSub (sub-name rest-ds)
(if (symbol=? sub-name name)
0
(+ 1 (locate name rest-ds)))]))

What is the result of calling the compile function with the following expression:
{{{{fun {x} {fun {y} {fun {z} {+ x {+ y z}}}}} 8} 42} 320}

5
9) (10pts) Consider the following language:

p ::= m∗ e program consists of 0 or more function declarations followed by an expression


m ::= fun f (x : τ ) = e function declaration
e ::= n number v ::= n number
| b boolean | b boolean
| e+e addition τ ::= num type of numbers
| not e negation | bool type of booleans
| x identifier
| f e function application
x∈ Id f ∈ FunctionName
n∈ Number b ∈ Boolean
v∈ Val σ ∈ Id → Val

The language supports multiple function declarations with the same name. The static and dynamic
semantics of the language are as follows:

∅ ` n : num ∅ ` b : bool

noDuplicate(m∗ ) m∗ , ∅ ` e ⇒ v

m∗ , σ ` n ⇒ n m∗ , σ ` b ⇒ b
∅`m e⇒v

m∗ , σ ` e1 ⇒ v1 m ∗ , σ ` e 2 ⇒ v2 m∗ , σ ` e ⇒ b x ∈ Dom(σ)
m∗ , σ ` e1 + e2 ⇒ v1 + v2 m∗ , σ ` not e ⇒ ¬b ∗
m , σ ` x ⇒ σ(x)

findDecl (m∗ , f ) = {fun f (x1 : τ1 ) = e1 , · · · , fun f (xn : τn ) = en }


m∗ , σ ` e ⇒ v ∅`v:τ τ = τi m∗ , [xi 7→ v] ` ei ⇒ v 0
m∗ , σ ` f e ⇒ v 0

where noDuplicate(m∗ ) checks that any two function declarations in m∗ have either different names or
different parameter types, and findDecl (m∗ , f ) returns a set of all the function declarations among m∗
of the name f .
Draw the semantics derivation of the following program:

fun f(x: num) = g x


fun g(y: bool) = not y
fun g(z: num) = z + 42
f 8

6
7
10) (5pts) Consider the following LFAE expression:
fin
e ::= n σ ∈ Env = Var −→ Val
| e+e x ∈ Var
| x hλx.e, σi ∈ Clo = Var × Expr × Env
| λx. e v ∈ Val = Z + Clo + ExprV
| ee (e, σ) ∈ ExprV = Expr × Env

with the following “strict” evaluation of the form v ⇓ v :

σ ` e ⇒ v1 v1 ⇓ v2
n⇓n hλx.e, σi ⇓ hλx.e, σi
(e, σ) ⇓ v2

Write the operational semantics of LFAE, where a value is strictly evaluted when it is used, that is,
strict evaluation is deferred as much as possible. Thus, the evaluation of “λx.x y” is not an error.

8
11) (10pts) Consider the following KCFAE expression:

e ::= n κ ::= [] (mkK) e∈ Expr


fin
| e+e | [ + (e, σ)] :: κ (addSecondK e ds k) σ∈ Env =Var −→ Val
| x | [v + ] :: κ (doAddK v k) x∈ Var
| λx. e | [ (e, σ)] :: κ (appArgK e ds k) hλx.e, σi ∈ Clo =Var × Expr × Env
| ee | [v ] :: κ (doAppK v k) v∈ Val =Z + Clo + Cont
| withcc x e κ∈ Cont

Write the operational semantics of KCFAE of the form σ, κ ` e ⇒ v using the continue semantics of
the form v1 7→ κ ⇓ v2 , which denotes that v2 is the result of continuation κ taking the value v1 .

a) v 7→ κ ⇓ v
∗ []
v 7→ [] ⇓ v
∗ [ + (e, σ)] :: κ
σ, [v1 + ] :: κ ` e ⇒ v
v1 7→ [ + (e, σ)] :: κ ⇓ v
∗ [v + ] :: κ

∗ [ (e, σ)] :: κ

∗ [v ] :: κ

9
b) σ, κ ` e ⇒ v
∗ n
n 7→ κ ⇓ v
σ, κ ` n ⇒ v
∗ e+e

∗ x

∗ λx. e

∗ ee

∗ withcc x e

10
12) (10pts) Given the following grammar:

e ::= n number
| x identifier
| e+e addition of two numbers
| ref e location construction with a given initial value
| !e dereferencing
| e := e assignment evaluating the right-hand side (RHS) first, producing the value of RHS
| e;e sequencing
| let x = e in e name binding
v ::= n number
| l location
τ ::= num type of numbers
| ref τ type of locations that contain values of type τ

x∈ Id
l∈ Loc
v∈ Val
σ∈ Id → Loc
M∈ Loc → Val

a) (5pts) Write the operational semantics of the form σ, M ` e ⇒ v, M for the expressions.

11
b) (5pts) Write the typing rules of the form Γ ` e : τ for the expressions. Assignments should not
change the types of the values at a given location.

12
13) (5pts) Rewrite the following code using explicit annotation of polymorphic types with tyfun and @ to
replace all the occurrences of ? with types and to make function calls to take explicit type arguments.

Γ[α] ` e : τ Γ ` τ0 Γ ` e : (∀α τ1 )
Γ ` [tyfun [α] e] : (∀α τ ) Γ ` [@ e τ0 ] : τ1 [α ← τ0 ]

Γ[α] ` τ
[. . . α . . .] ` α
Γ ` (∀α τ )

{with {f : ? {fun {g : ?} {fun {v : ?} {g v}}}}


{with {g : ? {fun {x : ?} x}}
{{f g} 10}}}

13
14) (10pts) Given the following grammar and the partial typing rules:

e ::= n
| b x:σ∈Γ στ
| {seqn e e} Γ`x:τ
| x
| {fun {x} e} Γ[x : τ ] ` e : τ 0
| {e e} Γ ` {fun {x} e} : (τ → τ 0 )
| {with {x e} e}
τ ::= num Γ ` e1 : (τ → τ 0 ) Γ ` e2 : τ
| bool Γ ` {e1 e2 } : τ 0
| (τ -> τ )
| α Γ ` e1 : τ Γ[x : Gen Γ (τ )] ` e2 : τ 0
σ ::= τ Γ ` {with {x e1 } e2 } : τ 0
| (∀α σ)

στ iff σ = (∀α1 (· · · (∀αn τ0 )) · · · ) ∧ τ = τ0 [αi ← τi ] where 1 ≤ i ≤ n

Gen Γ (τ ) = (∀α1 (· · · (∀αn τ )) · · · ) where {α1 , · · · , αn } = FTV (τ ) \ FTV (Γ)

FTV (num) = ∅
FTV ((τ1 → τ2 )) = FTV (τ1 ) ∪ FTV (τ2 )
FTV (α) = {α}
S
FTV (Γ) = x:σ∈Γ FTV (σ)
FTV ((∀α σ)) = FTV (σ) \ {α}

a) Draw the type derivation of the following expression:


{with {x {fun {z} z}} {seqn {x 42} {x true}}}

14
b) Draw the type derivation of the following expression:
{{fun {x} {seqn {x 42} {x true}}} {fun {z} z}}

15

You might also like