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

A homework assignment for cs 421, a computer science course, focusing on type checking and evaluation in ocaml. The assignment includes instructions, guidelines, and problems to be solved. Students are required to write answers on the provided pages and submit hard copies by a certain date.

Typology: Assignments

Pre 2010

Uploaded on 03/16/2009

koofers-user-74z
koofers-user-74z 🇺🇸

10 documents

1 / 7

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) (e1,e2) (v1,v2)
fst e,ρ vsnd e,ρ v
(Rew) (e1,e2)(v1,v2)
fst evsnd ev
(Env-σ)ρ,σ`(e1,e2)(v1,v2)0
ρ,σ`fst ev0ρ,σ`snd ev,σ0
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 ((Env-Problem 1. We gave three versions of the dynamic semantics of OCaml in class:σ) Environment, with state, 4/8, slides 4-5. (Note that we have added a rule for sequencing (semicolon) thatβ-value reduction), 4/3, slides 12- wasn’t in the original notes.) (e 1 , eFor each one, give a semantics for the pair operation and the 2 ) produces a value that is written (v 1 , v 2 ) (i.e. pairs are values). fst and snd operations. Assume an expression (Env) (^) (e 1 ,e 2 ),ρ ⇓ (v 1 ,v 2 ) fst e,ρ ⇓ v snd e,ρ ⇓ v (Rew) (^) (e 1 ,e 2 ) ⇓ (v 1 ,v 2 ) fst e ⇓ v snd e ⇓ v (Env-σ) (^) ρ,σ(e 1 ,e 2 ) ⇓ (v 1 ,v 2 ),σ′ ρ,σ fst e ⇓ v,σ′^1 ρ,σ` snd e ⇓ v,σ′

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) (^) let x=e and x′=e′ (^) in e′′,ρ ⇓ v (Rew) (^) let x=e and x′=e′ (^) in e′′ (^) ⇓ v (Env-σ) (^) ρ,σlet x=e and x′=e′ (^) in e′′ (^) ⇓ v,σ′ on this page.and Rew above. That is, using Env, prove: Problem 3. Prove that the expression^ For the problems below, use the templates starting on page 3. Do not write your answers e = ((∅, efun ⇓ 7 x (where -> fun ∅ yis the empty environment), and using Rew, prove -> x + y)3)4 returns 7, using semantic definitions Env e ⇓ 7. int.Problem 4.^ Use the type system presented on 4/8 (slides 11-14) to prove that expression^ e^ from problem 3 has type Problem 5. Consider these two top-level expressions:(1) let memcell = letin funcell n =-> let n’= !cell in (cell := n; n’);; ref 0 (a) Use the dynamic semantic definition Env-in the empty environment and state. That is, prove^ (2)^ memcell^ 5;; σ 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

∅ `^ ((fun

x^ -> fun

y^ ->^ x^

+^ y)3) : ∅ ` ((fun^ x^ -> fun

y^ ->^ x^

+^ y)3)4 :

int

Problem 5.a You may use the following abbreviations to save space in the proof tree. σ^ =^ {m

→^0 }

ρ^ =^ {cell

→^ m} ∅,^ ∅ `^ ref 0

⇓^ m, σ ∅,^ ∅ `^ let cell=ref 0 in

...^ ⇓^

Problem 6 You may use the following abbreviation to save space in the proof tree. ρ^ =^ {i^ →

∅ `^ fun

x^ ->^ (x, x

) :^ α^ →

α^ ∗^ α^

TREE-B

∅ `^ let

i^ =^ fun

x^ ->^ (x, x

)^ in^ (i^

3 , i^ “^3 ”) : (

int^ ∗^ int

)^ ∗^ (string

∗^ string

where TREE-B is:

ρ^ `^ 3 :

int^

ρ^ `^ “^3 ”

:^ string

ρ^ `^ (i^3

, i^ “^3 ”) : (

int^ ∗^ int

)^ ∗^ (string

∗^ string