









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!










11:30am–12:45pm, Thursday, June 29, 2006
The total time for this exam is 75 minutes.
Print your name and netid below. Also write your name at the bottom of each subsequent page.
Name:
Netid:
Question Total points Score 1 10 2 9 3 16 4 19 5 12 6 20 7 8 8 6 SUBTOTAL 100 Extra Credit 12 TOTAL 112
(a) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): A statically-typed language: i. assigns types to all expressions at compile time ii. may require run-time checks to guarantee type errors do not occur iii. always has a strong type system iv. always requires explicit type declarations
(b) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): If a language is strongly typed: i. an operation that could produce a type error may be detected at compile-time or at run-time ii. the use of type inference to determine types of expressions is required iii. the language must be statically typed iv. the language must be dynamically typed
(c) (2 points) What value is produced by the following program? let x = 10 in let y = 5 + x in let f y = y + x in let x = 20 + y in f x;;
Given the following OCaml datatype:
type int_pair = Empty | LPair of int_pair * int | RPair of int * int_pair;;
(a) (6 points) Write a recursive function fold pair: val fold_pair : (int -> ’a -> ’a) -> (int -> ’a -> ’a) -> ’a -> int_pair -> ’a = that folds a function over an int pair. It should take a function that works on LPair (typed between the first set of parens in the type signature for fold pair), a function that works on RPair (typed between the second set of parens in the type signature for fold pair), and an identity for Empty, as well as the int pair, returning the value of the fold computation.
(b) (5 points) Write a function sum rpair, using fold pair, that will calculate the sum using only the integer value stored in RPair items – in other words, the int that is part of an LPair will be disregarded for the sum (but the int pair that is part of the LPair still needs to be processed, since it may contain RPair items that will contribute to the sum).
(c) (5 points) Write a function flip lpair, using fold pair, that will turn an LPair into an RPair, but will leave RPair and Empty items as they are.
let f = (fun x −> (g x)) in f 10
Please complete the derivation by giving the results to go in the numbered blanks. Put your answers next to the corresponding numbers below the type derivation outline.
(#14) ⊢ (#15) : (#16) (#17) ⊢ (#18) : (#19) (#11) ⊢ (#12) : (#13) (#1) ⊢ (fun x −> (g x)) : (#2)
(#5) ⊢ (#6) : (#7) (#8) ⊢ (#9) : (#10) (#3) ⊢ f 10 : (#4) {g : int → bool} ⊢ let f = (fun x −> (g x)) in f 10 : bool
(a) (5 points) Use β-reduction with a lazy evaluation order to reduce the following term as fully as possible: (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t))
(b) (5 points) Use β-reduction with an eager evaluation order to reduce the following term as fully as possible (note: this is the same term as above): (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t))
(c) (5 points) Use β-reduction to reduce this term as fully as possible. If you need α- conversion, show explicitly where it is needed. Use an eager evaluation order. (λa b.b b a)((λx y.y)(λw.a w)(λv.b v))(λt.t)
(d) (5 points) Recall our definition of pairs in the λ calculus – a pair can be represented as a term λx.x a b where x is the constructor tag (here we only have one constructor, so we only need one tag) and a and b are the two elements in the pair. The pair constructor abstraction is then λa b x.x a b, which will take the two elements of the pair and “plug them in” to the correct spots. Using this definition of pairs, provide a λ-abstraction that, given a pair and a term, will return a new pair where the first element is the same as that of the input pair and the second element is the input term. In other words, given pair (a, b) and term t, the pair (a, t) should be returned.
a ǫ (^) a
a
b (^) ǫ
(a) a (b) baab (c) aaaab (d) b (e) aaaaa (f) ba
type lterm = Var of string (* x ) | Abs of string * lterm ( \lambda x. t ) | App of lterm * lterm ( t t’ *);;
(a) (3 points) Write a function bound vars which, given a term of type lterm, returns a list of strings (string list) containing all the names of variables having a bound occurrence in the term. It is acceptable if a name appears in the list more than once.
(b) (4 points) Write a function gen church numeral which, given a non-negative integer (type int), returns an lterm representing the equivalent Church Numeral. Remember our definitions: 0 ≡ λf x.x, 1 ≡ λf x.f x, and in general Suc n ≡ λf x.f(n f x).
Rules for type derivations:
Constants
⊢ n : int (assuming n is an int)
⊢ true : bool
⊢ false : bool
Variables
Γ ⊢ x : τ if(x : τ ) ∈ Γ
Arithmetic Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ⊕ e 2 : int
(⊕ ∈ {+, −, ∗, /, · · ·})
Relational Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ∼ e 2 : bool
(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · ·})
Booleans Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 && e 2 : bool Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 || e 2 : bool Γ ⊢ e 1 : bool Γ ⊢! e 1 : bool If Γ ⊢ e 1 : bool Γ ⊢ e 2 : τ Γ ⊢ e 3 : τ Γ ⊢ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] ⊢ e : τ 2 Γ ⊢ fun x−>e : τ 1 → τ 2 Function Application Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 Γ ⊢ e 1 e 2 : τ 2 Let Γ ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let rec x = e 1 in e 2 : τ ′