

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages. Simply-typed lambda calculus; More types (Lectures 11-12).
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages
(a) Add appropriate type annotations to the following expressions, and state the type of the expression.
(i) λa. a + 4
(ii) λf. 3 + f ()
(iii) (λx. x) (λf. f (f 42))
(b) For each of the following expressions, give a derivation showing that the expression is well typed.
(i) (λf : int → int. f 38) (λa : int. a + 4)
(ii) λg : ( int → int ) → ( int → int ). g (λc : int. c + 1) 7
(iii) λf : int → int. λg : int → int. λx : int. g (f x)
(a) Recall the substitution lemma that we used in the proof of type soundness.
Lemma (Substitution). If x : τ ′^ e : τ and v : τ ′^ then ` e{v/x} : τ.
Using the definition of substitution given in Assignment 2, prove this lemma. You may assume that v does not have any free variables (i.e., FV (v) = ∅). Remember to state what set you are performing induction on and what the property is that you are proving for every element in that set. If you are not sure what cases you need to consider, or what you are able to assume in each case of the inductive proof, we strongly suggest that you write down the inductive reasoning principle for the inductively defined set.
(b) Recall the context lemma that we used in the proof of type soundness.
Lemma (Context). If E[e 0 ] : τ and e 0 : τ ′^ and e 1 : τ ′^ then E[e 1 ] : τ.
Prove this lemma. Remember to state what set you are performing induction on and what the property is that you are proving for every element in that set. If you are not sure what cases you need to consider, or what you are able to assume in each case of the inductive proof, we strongly suggest that you write down the inductive reasoning principle for the inductively defined set.
Simply-typed lambda calculus; More types (Lectures 11-12) Section and Practice Problems
For these questions, use the lambda calculus with products and sums (Lecture 13§1.1).
(a) Write a program that constructs two values of type int + ( int → int ), one using left injection, and one using right injection.
(b) Write a function that takes a value of type int + ( int → int ) and if the value is an integer, it adds 7 to it, and if the value is a function it applies the function to 42.
(c) Give a typing derivation for the following program.
λp : ( unit → int ) × ( int → int ). λx : unit + int. case x of #1 p | #2 p
(d) Write a program that uses the term in part (c) above to produce the value 42.
(a) Use the μx. e expression to write a function that takes a natural number n and returns the sum of all even natural numbers less than or equal to n. (You can assume you have appropriate integer comparison operators, and also a modulus operator.)
(b) Try executing your program by applying it to the number 5.
(c) Give a typing derivation for the following program. What happens if you execute the program?
μp : ( int → int ) × ( int → int ). (λn : int. n + 1, #1 p)
(a) Give a typing derivation for the following program.
let a : int ref = ref 4 in let b : ( int → int ) ref = ref λx : int. x + 38 in !b !a
(b) Execute the program above for 4 small steps, to get configuration 〈e, σ〉. What is an appropriate Σ such that ∅, Σ e : τ and Σ σ?
Page 2 of 2