Simply-typed lambda calculus; More types (Lectures 11-12 ..., Exams of Programming Languages

Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages. Simply-typed lambda calculus; More types (Lectures 11-12).

Typology: Exams

2022/2023

Uploaded on 05/11/2023

seshadrinathan_hin
seshadrinathan_hin 🇺🇸

4.6

(17)

231 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages
Simply-typed lambda calculus; More types (Lectures 11-12)
Section and Practice Problems
Week 7: Tue Mar 12–Fri Mar 15, 2019
1 Simply-typed lambda calculus
(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 (f42))
(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)
2 Type soundness
(a) Recall the substitution lemma that we used in the proof of type soundness.
Lemma (Substitution).If x:τ0`e:τand `v:τ0then `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[e0]:τand `e0:τ0and `e1:τ0then `E[e1]:τ.
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.
pf2

Partial preview of the text

Download Simply-typed lambda calculus; More types (Lectures 11-12 ... and more Exams Programming Languages in PDF only on Docsity!

Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages

Simply-typed lambda calculus; More types (Lectures 11-12)

Section and Practice Problems

Week 7: Tue Mar 12–Fri Mar 15, 2019

1 Simply-typed lambda calculus

(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 : intint. f 38) (λa : int. a + 4)

(ii) λg : ( intint ) → ( intint ). g (λc : int. c + 1) 7

(iii) λf : intint. λg : intint. λx : int. g (f x)

2 Type soundness

(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

3 Products and Sums

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 + ( intint ), one using left injection, and one using right injection.

(b) Write a function that takes a value of type int + ( intint ) 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 : ( unitint ) × ( intint ). λ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.

4 Recursion

(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 : ( intint ) × ( intint ). (λn : int. n + 1, #1 p)

5 References

(a) Give a typing derivation for the following program.

let a : int ref = ref 4 in let b : ( intint ) 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