4 Problems on Programming Analysis and Understanding - Homework | CMSC 631, Assignments of Computer Science

Material Type: Assignment; Class: PROG ANLYS&UNDERSTANDING; Subject: Computer Science; University: University of Maryland; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-0jz
koofers-user-0jz 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 631, Spring 2009
Homework 5
Due Wednesday, April 8, in class
Problems 1–3 must be done individually.
Problem 4 may be done in pairs.
1. Write down a sequence of reduction steps reducing each of the following terms to normal form. For
this problem, reduction is allowed anywhere within a term, including under a λ.
(a) (λx.x(xy))(λu.u)
(b) (λxyz.z yx)aa(λpq.q)
(c) (λxyz.xz (yz))(λxy.x)(λxy.x)
Note: λxy.e is short for λx.λy.e. Remember also that the scope of λextends as far to the right as
possible, and that application associates to the left.
2. For each type, construct a simply-typed lambda calculus term (variables, functions, and function
application only) whose most general type is that type, or argue that no term has that type. (Hint:
You can double-check your answers in OCaml.)
(a) αββ
(b) (αβγ)βαγ
(c) αβ
(d) ααα
3. Does the simply-typed lambda calculus with integers have a subject expansion property, meaning if
A`e:tand e0e, does A`e0:t? Here is reduction under call-by-value semantics. Either prove
that subject expansion holds, or give a counterexample showing that it does not hold.
4. Consider the simply typed lambda calculus with references added:
e::= x|n|λx :t.e |e e |ref e|!e|e:= e
t::= int |tt|tref
Figure 1 gives an operational semantics for this language. In these rules, instead of just having an
expression reduce to another expression, we also track a store s, which is a (partial) mapping from
locations `to values. Locations are just our abstraction for pointers. In the S-Ref rule, we allocate
new memory by picking an `that is not already in the domain of sand then mapping `to vin the
store. Then we can apply S-Deref if we’re asked to dereference a value that is in the store. Similarly,
S-Assign handles assignment, which in these semantics not only updates the store, but also returns
the value of the right-hand side (so we don’t have a unit value, unlike OCaml).
1
pf3

Partial preview of the text

Download 4 Problems on Programming Analysis and Understanding - Homework | CMSC 631 and more Assignments Computer Science in PDF only on Docsity!

CMSC 631, Spring 2009

Homework 5

Due Wednesday, April 8, in class

Problems 1–3 must be done individually.

Problem 4 may be done in pairs.

  1. Write down a sequence of reduction steps reducing each of the following terms to normal form. For this problem, reduction is allowed anywhere within a term, including under a λ.

(a) (λx.x(xy))(λu.u) (b) (λxyz.zyx)aa(λpq.q) (c) (λxyz.xz(yz))(λxy.x)(λxy.x)

Note: λxy.e is short for λx.λy.e. Remember also that the scope of λ extends as far to the right as possible, and that application associates to the left.

  1. For each type, construct a simply-typed lambda calculus term (variables, functions, and function application only) whose most general type is that type, or argue that no term has that type. (Hint: You can double-check your answers in OCaml.)

(a) α → β → β (b) (α → β → γ) → β → α → γ (c) α → β (d) α → α → α

  1. Does the simply-typed lambda calculus with integers have a subject expansion property, meaning if A e : t and e′^ → e, does A e′^ : t? Here → is reduction under call-by-value semantics. Either prove that subject expansion holds, or give a counterexample showing that it does not hold.
  2. Consider the simply typed lambda calculus with references added:

e ::= x | n | λx : t.e | e e | ref e |!e | e := e t ::= int | t → t | t ref

Figure 1 gives an operational semantics for this language. In these rules, instead of just having an expression reduce to another expression, we also track a store s, which is a (partial) mapping from locations to values. Locations are just our abstraction for pointers. In the S-Ref rule, we allocate new memory by picking an that is not already in the domain of s and then mapping ` to v in the store. Then we can apply S-Deref if we’re asked to dereference a value that is in the store. Similarly, S-Assign handles assignment, which in these semantics not only updates the store, but also returns the value of the right-hand side (so we don’t have a unit value, unlike OCaml).

S-App 〈s, (λx.e) v〉 → 〈s, e[x 7 → v]〉

C-App-Left 〈s, e 1 〉 → 〈s′, e′ 1 〉 〈s, e 1 e 2 〉 → 〈s′, e′ 1 e 2 〉

C-App-Right 〈s, e 2 〉 → 〈s′, e′ 2 〉 〈s, v e 2 〉 → 〈s′, v e′ 2 〉

S-Ref 6 ∈ dom(s) 〈s, ref v〉 → 〈s[ 7 → v], `〉

C-Ref 〈s, e〉 → 〈s′, e′〉 〈s, ref e〉 → 〈s′, ref e′〉

S-Deref ∈ dom(s) 〈s, !〉 → 〈s, s(`)〉

C-Deref 〈s, e〉 → 〈s′, e′〉 〈s, !e〉 → 〈s′, !(e′)〉

S-Assign ∈ dom(s) 〈s, := v〉 → 〈s[` 7 → v], v〉

C-Assign-Left 〈s, e 1 〉 → 〈s′, e′ 1 〉 〈s, e 1 := e 2 〉 → 〈s′, e′ 1 := e 2 〉

C-Assign-Right 〈s, e 2 〉 → 〈s′, e′ 2 〉 〈s, := e 2 〉 → 〈s′, := e′ 2 〉

Figure 1: (Small-step) Operational semantics with stores

T-Var x ∈ dom(A) A ` x : A(x)

T-Int A ` n : int

T-Lam A, x : t e : t′ A λx : t.e : t → t′

T-App A e 1 : t → t′^ A e 2 : t A ` e 1 e 2 : t′

T-Ref A e : t A ref e : t ref

T-Deref A e : t ref A!e : t

T-Assign A e 1 : t ref A e 2 : t A ` e 1 := e 2 : t

Figure 2: Type rules with references