17 2 Final
17 2 Final
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:
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:
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.
3
6) (10pts) The following code is an excerpt from the implementation of the interpreter for 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 (m)
(apply format "my ~a saw a ~a rock" (web-read-each ‘("noun" "adjective"))))
(m)
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:
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)
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:
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
σ ` 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:
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 ]
Γ[α] ` τ
[. . . α . . .] ` α
Γ ` (∀α τ )
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
| (∀α σ)
FTV (num) = ∅
FTV ((τ1 → τ2 )) = FTV (τ1 ) ∪ FTV (τ2 )
FTV (α) = {α}
S
FTV (Γ) = x:σ∈Γ FTV (σ)
FTV ((∀α σ)) = FTV (σ) \ {α}
14
b) Draw the type derivation of the following expression:
{{fun {x} {seqn {x 42} {x true}}} {fun {z} z}}
15