



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
A homework assignment for cs 421, a computer science course at an unspecified university, focusing on type checking and evaluation in ocaml. The assignment includes several problems that require the application of dynamic and static semantics, as well as the use of let expressions and closures. Students are expected to submit their answers in class or to the professor's office by a certain date.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Assigned Due Extension April 17, 2008, submit in class April 10, 2008 due April 17, 5:00 pm, 20% penalty Guideline: • For problems 1 and 2, write your answers in the blanks right on page 1 and 2. For other problems, use thetemplates provided starting on page 3.
e^1 ρ,σ⇓^ v^1 (,σe 1 ′′,e^2 ) ⇓ (ρ,σv 1 ,v′′ 2 `)e,σ^2 ′⇓^ v^1 ,σ′ ρ,σ^ ρ,σefst^ ⇓^ ( v,ve ⇓′ )v,σ,σ′′ ρ,σ^ ρ,σ esnd^ ⇓^ ( ve ′⇓,v )v,σ,σ′′ 1Problem 2. For each of the semantic definitions, give a rule for this version of let: let x = e and x′^ = e′^ in e′′: (Env)^ e,ρ^ ⇓^ v^1 let ex′=,ρe^ ⇓and^ v^2 x′=e′e (^) in′′,ρ [ex′′→,ρv ⇓^1 ,x v′→v^2 ]^ ⇓^ v (Rew)^ e^ ⇓^ v^1 let xe=′^ e⇓ andv^2 x′=ee′ ′′in[v^1 /x,ve′′ (^) ⇓^2 /xv ′]^ ⇓^ v (Env-σ)^ ρ,σe^ ⇓^ v^1 ,σ^1 ρ,σ^ρ,σ let^1 ex′=^ ⇓e^ vand^2 ,σ^2 x′=ρe[x′ →inv 1 e,x′′ ′⇓→ v,σv^2 ],σ′^2e′′^ ⇓^ v,σ′ on this page. Problem 3. Prove that the expression^ For the problems below, use the templates starting on page 3. Do not write your answers e = ((fun x -> fun y -> x + y)3)4 returns 7, using semantic definitions Env and Rew above. That is, using Env, prove:Problem 4. Use the type system presented on 4/8 (slides 11-14) to prove that expression ∅, e ⇓ 7 (where ∅ is the empty environment), and using Rew, prove e from problem 3 has type e ⇓ 7. int.Problem 5. Consider these two top-level expressions: (1) (2) let memcellmemcell 5;; = letin funcell n =-> let ref 0 n’= !cell in (cell := n; n’);; (a) Use the dynamic semantic definition Env-in the empty environment and state. That is, proveσ to evaluate the expression let cell= ... from expression (1), (Hint: the value is a closure.)(Reminder: Use the template on page 5 to write your answer.)^ ∅,^ ∅ ^ let cell^ =^ ref^ 0 in^...^ cell:=n;n′^ ⇓^ , (b) Suppose the evaluation in step (a) returns a valueexpression (2) in the environment ρ 0 = {memcell → vv} and changes the state toand state σ. σ. Then give the evaluation of Problem 6. Use the polymorphic type system from 4/10 to infer: ∅ let i = fun x -> (x, x) in (i 3 , i “ 3 ”) : (int ∗ int) ∗ (string ∗ string)
2
Problem 4 {x^ →
int, y
→^ int
} `^ x^
:^ int^
{x^ → int, y
→^ int
} `^ y^
:^ int
{x^ → int, y
→^ int
} `^ x^
+^ y^ : int
{x^ →
int} `
fun^ y^ ->^ x
+^ y^ :
int^ →
int
∅ `^ fun
x^ -> fun
y^ ->^ x^ +^ y^
:^ int^
→^ int
→^ int
int
fun^ x
-> fun
y^ ->^ x^ +^ y
int^ →
int^
int
fun^ x
-> fun
y^ ->^ x^ +^ y
int
Problem 5.a You may use the following abbreviations to save space in the proof tree. σ^ =^ {m
ρ^ =^ { cell
→^ m
ref^
m, σ^
ρ, σ^ `
fun^ n^ ->^ ...
⇓ 〈n,
let n’=!cell in
..., ρ〉
, σ
let cell=ref
0 in
n,^ let n’=!cell in
..., ρ〉
, σ
Problem 5.b You may use the following abbreviations in addition to those in part (a) to save space in the proof tree. ρ=^ {^0
memcell
→ 〈n,
let n’=!cell in
..., ρ〉}
ρ=^ ρ^1
[n^ →^
ρwas originally given as^1
ρ[n^0 →^ 5]. It was a typo.)
ρ=^ ρ^2
′^ [n→ 1
σ=^ {^1
m^ →^
ρ, σ^ `^0
memcell
⇓ 〈n,
let n’=!cell in
..., ρ〉
, σ^
ρ, σ^ `^0
5 , σ
ρ, σ^ `^1
cell
⇓^ m, σ ρ, σ^ `^1
!cell
⇓^0 , σ
ρ, σ^ `^1
let n
′^ =^ !cell in
0 , σ^1
ρ, σ^ `^0
memcell
0 , σ^1
where TREE-A is: ρ, σ^ `^2
cell
⇓^ m, σ
ρ
, σ^ ` 2 n^ ⇓^
5 , σ
ρ, σ^ `^2
cell:=n
, σ^1
ρ, σ^21
′^ ` n⇓ 0 , σ^1
ρ, σ^ `^2
cell:=n;n
, σ^1