Docsity

CS 565 Final Examination: Closures, Dynamic Types, and Algorithmic Subtyping - Prof. Sures, Exams of Programming Languages

Partial preview of the text

Download CS 565 Final Examination: Closures, Dynamic Types, and Algorithmic Subtyping - Prof. Sures and more Exams Programming Languages in PDF only on Docsity!

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.

  1. (5 points) What is the closure converted representation of the following program? let val y = 1 in if true then λ x. x + y else λ z.z end
  2. (10 points) Consider the language λC^ that extends the simply-typed λ-calculus (equipped with tuples and product types) with closure values. Present the grammar for this language, along with necessary typing and (small-step) evaluation rules.

Question 2. (35 points)

Consider the ML datatype:

datatype tree = Leaf of int | Unary of tree | Binary of tree * tree

  1. (5 points) Give a definition of tree as a recursive data type.
  2. (5 points) Using an iso-recursive formulation (i.e, assuming existence of fold and unfold operations), provide definitions for the constructors Leaf , Unary , and Binary.
  3. (10 points) On your representation, use an iso-recursive formulation (i.e., assume existence of fold and unfold operations) to write a function count: tree → int in ML-syntax that counts the number of leaves in a tree.
  4. (15 points) State progress and preservation theorems for the simply-typed λ-calculus equipped with iso-recursive types. How would you go about proving these theorems?

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.

  1. (5 points) Give a one sentence comparison between dynamic types and disjoint unions.
  2. (5 points) Write a program fragment whose evaluation yields wrong.

Question 4 (25 points)

  1. (5 points) Show that the type assigned to a term by the algorithmic subtyping rules can decrease during evaluation by finding two terms s and t with algorithmic types σ and τ such that s →∗^ t and τ <: σ but σ 6 <: τ.
  2. (5 points) In the absence of a Bot type, is there a type that is a subtype of every other type? Is there an arrow type that is a supertype of every other arrow type?
  3. (15 points) For each lettered type σ from the left-hand column below, indicate the number of every type τ on the right-hand column such that σ <: τ. If no matching term exists, write “None” for that letter.

σ τ (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)

  1. (9 points) For each of the following ML terms, write a corresponding term in System F. You may assume the availability of tuples and projections. You may further assume the availability of base types and lists.

(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

  1. (8 points) Give a one sentence explanation to describe each of the following terms. For each term, state whether it could be encoded in ML.

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

  1. (8 points) Give a one sentence explanation for why the following terms are not well-typed.

(a) Λt.λx : t.Λt.x

(b) Λt.λx : t.cons(x, nil)[int][intlist]