14-1-final
14-1-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.
1) (30pts) Apple recently announced a new programming language called Swift which has the following
features:
a) (3pts) One can distinguish declarations of mutable variables by using the keyword var and
immutable variables by using the keyword let. Explain what mutable and immutable variables
are.
b) (4pts) Swift has first-class functions. Explain what they are and what Int->Int means.
1
e) (4pts) It supports polymorphic types. Explain what they mean and how List and List<Int>
are different.
f) (3pts) It support option types. Compare using option types and the null type.
g) (4pts) It does not support tail call optimization and it does not guarantee type soundness yet.
Explain what they are.
h) (6pts) It supports garbage collection by reference counting. Explain and compare three garbage
collection algorithms discussed in this course.
2
2) (10pts) Finish the following partial implementation of continuations.
(define-type KCFAE
[num (n number?)]
[add (l KCFAE?) (r KCFAE?)]
[sub (l KCFAE?) (r KCFAE?)]
[id (name symbol?)]
[fun (x symbol?) (b KCFAE?)]
[app (f KCFAE?) (a KCFAE?)]
[if0 (test KCFAE?) (then KCFAE?) (else KCFAE?)]
[withcc (name symbol?) (body KCFAE?)])
(define-type KCFAE-Value
[numV (n number?)]
[closureV (param symbol?) (body KCFAE?) (sc DefrdSub?)]
[contV (proc procedure?)])
(define (num-op op op-name) (lambda (x y) (numV (op (numV-n x) (numV-n y)))))
(define num+ (num-op + ’+))
(define num- (num-op - ’-))
3
3) (5pts) Finish the following partial implementation of compilation (Step 2):
(define-type CFAE-Value
[numV (n number?)]
[closureV (body CFAE?) (ds DefrdSub?)])
;; num-op : (number number -> number) -> (CFAE-Value CFAE-Value -> CFAE-Value)
(define (num-op op) (lambda (x y) (numV (op (numV-n x) (numV-n y)))))
(define num+ (num-op +))
(define num- (num-op -))
4
4) (10pts) Consider the following language with lists:
e ::= n
| {+ e e }
| {- e e }
| x
| {fun {x:τ } e}
| {e e}
| {empty τ }
| {cons e e}
| {first e}
| {rest e}
τ ::= num
| (τ -> τ )
| list τ
Write the typing rules for 4 language constructs for lists and the type validity rule for the list type.
5
5) (5pts) Fill in the following “type unification” algorithm:
6
6) (10pts) Consider the following TRCFAE expression:
c) (2pts) How can we solve this type soundness problem without changing the soundness theorem?
7
7) (10pts) Given the following grammar and the partial typing rules:
e ::= n
| {+ e e } Γ[α] ` e : τ
| {- e e }
Γ ` [tyfun [α] e] : (∀α τ )
| x
| {fun {x:τ } e} Γ ` τ0 Γ ` e : (∀α τ1 )
| {e e}
| {if0 e e e} Γ ` [@ e τ0 ] : τ1 [α ← τ0 ]
| [tyfun [α] e]
Γ[α] ` τ
| [@ e τ ]
τ ::= num Γ ` (∀α τ )
| (τ -> τ )
| (∀α τ ) [. . . α . . .] ` α
| α
8
8) (20pts) Given the following grammar:
e ::= ...
| {throw}
| {try e catch e}
| {withtype {t {x τ } {x τ }} e}
| {cases t e {x {x} e} {x {x } e}}
τ ::= ...
| t
| anyT
a) (10pts) Write the operational semantics of the form σ ` e ⇒ v for the expressions.
9
b) (5pts) Write the typing rules for the expressions.
10
c) (5pts) Draw the type derivation of the following expression:
11