









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 TRUE ii. may require run-time checks to guarantee type errors do not occur TRUE iii. always has a strong type system FALSE iv. always requires explicit type declarations FALSE
(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 TRUE ii. the use of type inference to determine types of expressions is required FALSE iii. the language must be statically typed FALSE iv. the language must be dynamically typed FALSE
(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;; 45
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 =
(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). 1 # let sum_rpair p = 2 fold_pair (fun x y -> y) (fun x y -> x + y) 0 p;; 3 val sum_rpair : int_pair -> int =
(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. 1 # let flip_lpair p = 2 fold_pair (fun x y -> RPair (x,y)) (fun x y -> RPair (x,y)) Empty p;; 3 val flip_lpair : int_pair -> int_pair =
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
#1 {g : int → bool}
#2 int → bool
#3 {g : int → bool; f : int → bool}
#4 bool
#5 {g : int → bool; f : int → bool} #6 f
#7 int → bool
#8 {g : int → bool; f : int → bool}
#9 10
#10 int
#11 {g : int → bool, x : int}
#12 g x
#13 bool
#14 {g : int → bool, x : int}
#15 g
#16 int → bool
#17 {g : int → bool, x : int} #18 x
#19 int
(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 c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) →β (λc d.(λx y.x) c d)(λs.s)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λf.f f)(λt.t)) →β (λx y.x) (λs.s) ((λf.f f)(λt.t)) →β (λy.(λs.s)) ((λf.f f)(λt.t)) →β λs.s
(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)) (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) →β (λc d.(λx y.x) c d)(λs.s)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λt.t)(λt.t)) →β (λd.(λx y.x) (λs.s) d)(λt.t) →β (λx y.x) (λs.s) (λt.t) →β (λy.(λs.s)) (λt.t) →β λs.s
(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) (λa b.b b a)((λx y.y)(λw.a w)(λv.b v))(λt.t) →β (λa b.b b a)((λy.y)(λv.b v))(λt.t) →β (λa b.b b a)(λv.b v)(λt.t) →α (λa c.c c a)(λv.b v)(λt.t) →β (λc.c c(λv.b v))(λt.t) →β (λt.t)(λt.t)(λv.b v) →β (λt.t)(λv.b v) →β (λv.b v)
(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.
For the solution, just having the following would be sufficient: λp t.(λa b x.x a b)(p(λx y.x)) t This is a function that takes a pair and a term and then has three parts: the first is the pair constructor, (λa b x.x a b); the second is the π 1 or left projection function for the pair, taking out the first element, (p(λx y.x)); the third is the term to put in, t. Remember, a “loaded” pair with data in it is of the form λx.x a b where a and b are the left and right members of the pair. If you substitute the function (λx y.x) for x here, you will see that this will then return the first element of the pair. You could also have reduced this a bit, to λp t x.x(p(λx y.x)) t but any valid solution was accepted.
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. 1 let bound_vars lterm = 2 let rec bound_aux lterm binders = 3 match lterm with 4 | Var x -> if (List.mem x binders) then [x] else [] 5 | Abs (x,t) -> bound_aux t (x::binders) 6 | App (t,t’) -> (bound_aux t binders) @ (bound_aux t’ binders) 7 in bound_aux lterm [];;
(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). 1 let gen_church_numeral n = 2 let rec church_aux n = 3 match n with 4 | 0 -> Var "x" 5 | _ -> App (Var "f", church_aux (n-1)) 6 in Abs ("f", Abs("x", church_aux n));; 7 8 # gen_church_numeral 3;; 9 - : lterm = 10 Abs ("f", Abs ("x", App (Var "f", App (Var "f", App (Var "f", Var "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 : τ ′