





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Final Examination CS 565 May 4, 2005
Name:
Instructions: Answer all questions in the space provided. Extra blank pages are provided in the back. Partial credit will be given where appropriate. Reference material are given as attachments.
Maximum score: 200
Attained total score:
Question 1. (50 points)
A closure is a data structure consisting of a piece of code and a representation of an environment used to hold values of the code’s free variables. For example, the program given on the left below has a closure-converted representation shown on the right:
let val x = 1 val y = 2 val z = 3 val f = λ w. x + y + w in f 100 end
let val x = 1 val y = 2 val z = 3 val code = λ env. #x(env) + #y(env) + w val env = x = x, y = y val fclos = (code,env) in #1(fclos) #2(fclos) 100 end
A closure thus eliminates free variables in the body of an abstraction without resorting to substitution.
Question 2. (35 points)
Consider the ML datatype:
datatype tree = Leaf of int | Unary of tree | Binary of tree * tree
Question 3. (40 points)
Many programming languages such as Scheme or Python support dynamic values, values whose types are not known statically at compile-time.
Values of type Dynamic are built using the dynamic construct. The result of evaluating expression dynamic e:T is a pair of a value v and type-tag T where v is the result of evaluating e.
The typecase construct is used to examine the tag of a Dynamic value. Thus, the expression:
λ x: Dynamic. typecase x of (i: Nat) => i+ else 0 end
applied to dynamic 1:Nat evaluates to 2.
Consider a language whose terms and types are generated by the following grammar:
e ::= x |wrong |λ x : τ.e |e(e) | dynamic e : τ | typecase e of... (x : τ ) => e... else e τ ::= c |τ 1 → τ 2 |Dynamic
where c ranges over base types (e.g., numbers, Booleans, etc.), and wrong corresponds to a runtime type error.
Question 4 (25 points)
σ τ (a) { a: { }, b: {x: Nat}} (1) (Bot → Top) → Bot (b) (Nat → Nat) → Bool (2) Top → Nat (c) { a: Top } → { a: Top } (3) {b : { }} (d) ({a:Top} → { }) → {b:Top} (4) {b: Nat} (e) {b: Top → Nat} (5) ({a:Nat} → {a:Top}) → { } (6) {b: { } → Top} (7) ({a:Top} → {a:Nat}) → { } (8) ({ } → {a : Top}) → { } (9) (Top → Bot) → Top (10) { a: Top, b: { }} → { }
Question 5. (25 points)
(a) fn x => x + let fun f (x) = x in f(2) end
(b) let fun twice(f,x) = f(f(x)) in twice(fn y => y + 1,2)
(c) let val f = fn x => x in f [f(0)] end
(a) λx : ∀t.t → ∀u.t.x[int]3)
(b) (λf : ∀t.t → t list.f [intlist]f [int]0) (Λt.λx : t.cons(x, nil))
(a) Λt.λx : t.Λt.x
(b) Λt.λx : t.cons(x, nil)[int][intlist]