Type Checking and Evaluation Homework for CS 421, Spring 2008, Assignments of Computer Science

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

Pre 2010

Uploaded on 03/10/2009

koofers-user-vh6
koofers-user-vh6 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
HW 10 Type Checking and Evaluation
CS 421 Spring 2008
Revision 1.0
Assigned April 10, 2008
Due April 17, 2008, submit in class
Extension 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 the
templates provided starting on page 3.
If you’re submitting late, hand in your homework either to Prof. Kamin in room 4237 or to Baris Aktemur in
room 4307, by 5:00pm on April 17, Thursday. Note that 20% penalty applies.
Submit hard-copy only. No electronic submission.
Problem 1. We gave three versions of the dynamic semantics of OCaml in class:
(Env) Environment-based definition, 4/3, slides 10–11
(Rew) Rewriting version (β-value reduction), 4/3, slides 12-13
(Env-σ) Environment, with state, 4/8, slides 4-5. (Note that we have added a rule for sequencing (semicolon) that
wasn’t in the original notes.)
For each one, give a semantics for the pair operation and the fst and snd operations. Assume an expression
(e1, e2)produces a value that is written (v1, v2)(i.e. pairs are values).
(Env) e1v1e2v2
(e1,e2) (v1,v2)
e,ρ(v,v0)
fst e,ρ v
e,ρ(v0,v)
snd e,ρ v
(Rew) e1v1e2v2
(e1,e2)(v1,v2)
e(v,v0)
fst ev
e(v0,v)
snd ev
(Env-σ)ρ,σ`e1v100 ρ,σ00`e2v10
ρ,σ`(e1,e2)(v1,v2)0
ρ,σ`e(v,v0)0
ρ,σ`fst ev0
ρ,σ`e(v0,v)0
ρ,σ`snd ev0
1
pf3
pf4
pf5

Partial preview of the text

Download Type Checking and Evaluation Homework for CS 421, Spring 2008 and more Assignments Computer Science in PDF only on Docsity!

HW 10 – Type Checking and Evaluation CS 421 – Spring 2008 Revision 1. 0

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.

  • • If you’re submitting late, hand in your homework either to Prof. Kamin in room 4237 or to Baris Aktemur inroom 4307, by 5:00pm on April 17, Thursday. Note that 20% penalty applies.Submit hard-copy only. No electronic submission. (Env) Environment-based definition, 4/3, slides 10–11(Rew) Rewriting version (Problem 1. We gave three versions of the dynamic semantics of OCaml in class:β-value reduction), 4/3, slides 12- (Env-wasn’t in the original notes.)For each one, give a semantics for the pair operation and theσ) Environment, with state, 4/8, slides 4-5. (Note that we have added a rule for sequencing (semicolon) that fst and snd operations. Assume an expression (e 1 , e 2 ) produces a value that is written (v 1 , v 2 ) (i.e. pairs are values). (Env) e^1 (,ρe 1 ⇓,ev^12 ),ρ ⇓ (ve 12 ,v,ρ 2 ⇓)v^2 fst^ e,ρ⇓ e,ρ(v,v ⇓′ )v snde,ρ⇓ e,ρ(v′ ,v⇓) v (Rew) e^1 (e⇓ 1 v,e^1 2 ) ⇓ (v 1 e^2 ,v⇓ 2 v)^2 fst^ e⇓( v,ve ⇓′ )v snde⇓(v e′ ,v⇓ )v (Env-σ) ρ,σe^1 ρ,σ⇓^ v^1 (,σe 1 ′′,e^2 ) ⇓ (ρ,σv 1 ,v′′ 2 `)e,σ^2 ′⇓^ v^1 ,σ′ ρ,σ^ ρ,σefst^ ⇓^ ( v,ve ⇓′ )v,σ,σ′′ ρ,σ^ ρ,σ esnd^ ⇓^ ( ve ′⇓,v )v,σ,σ′′ 1

Problem 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

∅ `^ 3 :

int

∅ `^ ((

fun^ x

-> fun

y^ ->^ x^ +^ y

)3) :^

int^ →

int^

∅ `^ 4 :

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

→^0 }

ρ^ =^ { cell

→^ m

∅,^ ∅ `

0 ⇓^

0 ,^ ∅

∅,^ ∅ `

ref^

0 ⇓^

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^ →^

5]^ (

ρwas originally given as^1

ρ[n^0 →^ 5]. It was a typo.)

ρ=^ ρ^2

′^ [n→ 1

0]

σ=^ {^1

m^ →^

ρ, σ^ `^0

memcell

⇓ 〈n,

let n’=!cell in

..., ρ〉

, σ^

ρ, σ^ `^0

5 ⇓^

5 , σ

ρ, σ^ `^1

cell

⇓^ m, σ ρ, σ^ `^1

!cell

⇓^0 , σ

TREE-A

ρ, σ^ `^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

′^ ⇓^0

, σ^1