0% found this document useful (0 votes)
4 views11 pages

14-1-final

The document is a final exam for CS320, Spring 2014, consisting of various programming and type theory questions related to the Swift programming language, continuations, compilation, and type unification. It includes multiple sections requiring explanations, implementations, and derivations, with specific points allocated for each question. Students are instructed to complete the exam in a closed environment without any external resources.

Uploaded by

vedapac571
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)
4 views11 pages

14-1-final

The document is a final exam for CS320, Spring 2014, consisting of various programming and type theory questions related to the Swift programming language, continuations, compilation, and type unification. It includes multiple sections requiring explanations, implementations, and derivations, with specific points allocated for each question. Students are instructed to complete the exam in a closed environment without any external resources.

Uploaded by

vedapac571
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/ 11

Final Exam

CS320, Spring 2014


Student id: Name:

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.

c) (3pts) It supports type inference. Explain what type inference is.

d) (3pts) It has static typing. Explain what static typing is.

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 - ’-))

;; interp : KCFAE DefrdSub (KCFAE-Value -> alpha) -> alpha


(define (interp kcfae ds k)
(type-case KCFAE kcfae
[num (n) (k (numV n))]
...
[add (l r) (_____________________________________
_____________________________________
_____________________________________)]
...
[fun (x b) (_____________________________________)]
[app (f a) (_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________)]
[withcc (x b) (_____________________________________
_____________________________________
_____________________________________)]))

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 -))

; interp : CFAE DefrdSub CFAE-Cont -> CFAE-Value


(define (interp cfae ds k)
(type-case CFAE cfae
[cnum (n) (continue k (numV n))]
[cadd (l r) (interp l ds (addSecondK r ds k))]
[csub (l r) (interp l ds (subSecondK r ds k))]
[cid (pos) (continue k (list-ref ds pos))]
[cfun (body) (continue k (closureV body ds))]
[capp (f a) (interp f ds (appArgK a ds k))]))

; continue : CFAE-Cont CFAE-Value -> CFAE-Value


(define (continue k v)
(type-case CFAE-Cont k
[mtK () v]
[addSecondK (r ds k’) (interp _______________________________)]
[doAddK (v1 k’) (continue ___________________________________)]
[subSecondK (r ds k’) (interp _______________________________)]
[doSubK (v1 k’) (continue ___________________________________)]
[appArgK (a ds k’) (interp __________________________________)]
[doAppK (fv k’)
(interp (closureV-body fv)
(cons v (closureV-ds fv)) k’)]))

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:

Unify a type variable T with a type τ2:


– If T is set to τ1, _____________________________________.
– If τ2 is already equivalent to T, _________________________.
– If τ2 contains T, ____________________________________.
– Otherwise, ________________________________________.
Unify a type τ1 to type τ2:
– If τ2 is a type variable T, ______________________________.
– If τ1 and τ2 are both num or bool, succeed.
– If τ1 is (τ3 → τ4) and τ2 is (τ5 → τ6), then
∗ unify τ3 with τ5
∗ unify τ4 with τ6

6
6) (10pts) Consider the following TRCFAE expression:

{rec {f : (num -> num) f}


{f 10}}
a) (5pts) Draw the type derivation of the above expression.

b) (3pts) What happens if we interpret it?

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 Γ ` (∀α τ )
| (τ -> τ )
| (∀α τ ) [. . . α . . .] ` α
| α

Draw the type derivation of the following expression:

{{fun {f : (forall alpha (alpha -> alpha))}


{[@ f num] 10}}
[tyfun [alpha] {fun {x : alpha} x}]}

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:

{withtype {fruit {apple num} {banana (bool -> num)}}


{cases fruit {banana {fun {x:num} 3}}
{apple {n} n}
{banana {f} {f true}}}}

11

You might also like