14-2-final
14-2-final
Instructions: You have 150 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.
(define (remember v)
(local [(define n (length table))]
(begin (set! table (append table (list v)))
n)))
(define (a)
(web-read/k "First number"
(lambda (v1)
(web-read/k "Second number"
(lambda (v2) (+ v1 v2))))))
(define (b) (begin (a) (a)))
(b)
1
2) (5pts) What is the evaluation result of the following code?
(define (remember v)
(local [(define n (length table))]
(begin (set! table (append table (list v)))
n)))
(define (m)
(apply format "my ~a saw a ~a rock" (web-read-each ‘("noun" "adjective"))))
(m)
3) (5pts) In the KCFAE language, what is the value of the following expression:
{withcc result
{{withcc escape
{result {+ 2 {withcc k
{escape k}}}}}
8}}
2
4) (10pts) The Swift programming language uses the reference counting garbage collection algorithm.
c) Describe good and bad things about the reference counting algorithm.
3
5) (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:
c) What is a relation between the tail call optimization and continuation passing style?
4
7) (5pts) Compilation
a) Compare interpreters and compilers.
5
9) (5pts) Describe the following:
a) Type inference
b) Polymorphic types
c) Type soundness
10) (5pts) What are the evaluation results of the following? If it is an error, describe where the error
occurs:
a) {withtype {fruit {apple bool}
{banana num}}
{{withtype {animal {apple (num -> num)}
{banana bool}}
{fun {x: fruit}
{cases fruit x
{apple {b} b}
{banana {n} {- 8 n}}}}}
{banana true}}}
6
11) (5pts) Compare the following two versions of type checking function applications:
a) (define typecheck : (EXP TypeEnv -> Type)
(lambda (exp env)
(type-case EXP exp
...
[app (fn arg)
(local [(define result-type (varT (box (none))))]
(begin
(unify! (arrowT (typecheck arg env) result-type)
(typecheck fn env)
fn)
result-type))]
...)))
b) (define typecheck : (EXP TypeEnv -> Type)
(lambda (exp env)
(type-case EXP exp
...
[app (fn arg)
(type-case Type (typecheck fn env)
[arrowT (param-type result-type)
(if (equal? param-type (typecheck arg env))
result-type
(type-error arg (to-string param-type)))]
[else (type-error fn "function")])]
...)))
7
b) Given the partial typing rules:
Γ[α] ` e : τ
Γ ` [tyfun [α] e] : (∀α τ )
Γ ` τ0 Γ ` e : (∀α τ1 )
Γ ` [@ e τ0 ] : τ1 [α ← τ0 ]
Γ[α] ` τ
Γ ` (∀α τ )
[. . . α . . .] ` α
8
13) (10pts) Given the following grammar and the partial typing rules:
FTV (num) = ∅
FTV ((τ1 → τ2 )) = FTV (τ1 ) ∪ FTV (τ2 )
FTV (α) = {α}
S
FTV (Γ) = x:σ∈Γ FTV (σ)
FTV ((∀α σ)) = FTV (σ) \ {α}
9
14) (20pts) We’d like to allow the following expression:
10
b) Write the operational semantics of the form σ ` e ⇒ v for the expressions withtype and cases.
c) Write the typing rules of the form Γ ` e : τ for the expressions withtype and cases and the
well-formedness of the added types.
11
d) Draw the type derivation of the following expression:
{withtype {{alpha list} {empty num}
{cons (alpha * {alpha list})}}
{[@ [tyfun [alpha]
{fun {l : {alpha list}}
{cases {alpha list} l {empty {n} n} {cons {p} 1}}}] num]
{[@ empty num] 0}}}
12
13