17 1 Final
17 1 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) (4pts) Summarize what you learned from this course in one sentence.
2) (6pts) Write what you learned from the following video lectures in one sentence each.
– “Growing a Language”
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) (10pts) The Swift programming language uses the reference counting garbage collection algorithm.
c) Describe good and bad things about the reference counting algorithm.
2
5) (5pts) The following web servlet implementation (main handler plus helper function) uses web-read,
which takes only a prompt and uses let/cc internally to obtain a continuation. Note that course-handler
gets the course tree information from a user starting from "CS101" to recommended courses until un-
known courses, and it shows the course tree via the show-tree function.
(define (course-handler)
(show-tree (get-course "CS101")))
(define (get-course from)
(local [(define to (web-read from))]
(if (equal? to "unknown")
"???"
(list to
(get-course (format "first course after ~a" to))
(get-course (format "second course after ~a" to))))))
(define (show-tree t) t)
(course-handler)
b) (3pts) What is the final result of evaluating the following series of calls?
(resume 0 "CS109")
(resume 1 "CS206")
(resume 2 "unknown")
(resume 3 "unknown")
(resume 4 "CS220")
(resume 5 "unknown")
(resume 6 "unknown")
3
6) (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}
4
8) (5pts) Fill in the following “type unification” algorithm. Write clearly whether the case “succeeds,”
“fails,” or “unifies types.”
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) 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 ]
Γ[α] ` τ
[. . . α . . .] ` α
Γ ` (∀α τ )
8
11) (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.
9
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.
10
12) (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 (σ) \ {α}
11
b) Draw the type derivation of the following expression:
{{fun {x} {seqn {x 42} {x true}}} {fun {z} z}}
12
13) (20pts) We’d like to allow the following expression:
13
b) (4pts) Write the operational semantics of the form σ ` e ⇒ v for the expressions withtype,
cases, and applications.
c) (6pts) Write the typing rules of the form Γ ` e : τ for the expressions withtype and cases and
the well-formedness of the types.
14
d) (5pts) Consider the following expression:
15