CS 3520 Homework 7 - Due October 25
Exercise 7.1, An Absurd Language
The interpreter similar to the one from the end of the October 18 lecture is in hw7.scm. It implements call-by-need procedures and well as lazy bindings (``let-by-need''), and it porvides call-by-reference arguments.Your task is to modify the interpreter's language so that it supports call-by-value and call-by-name procedures, and also provides let-by-value and let-by-name local bindings. In particular,- Change proc back to call-by-value, and change let back to eager evaluation.
- Introduce two new procedure forms:
- proc/name creates a call-by-name procedure
- proc/need creates a call-by-need procedure
The syntax for each form is the same as for proc, except that the keyword is different.
- Introduce two new local binding form:
- let/name binds a local variable with lazy evaluation; every use of the variable forces evaluation
- let/need binds a local variable with lazy evaluation; the first use of the variable forces evaluation, and future uses re-use the forced value
The syntax for each form is the same as for let, except that the keyword is different.
Hint: To implement both by-name and by-need forms, you will need to modify the implementation of environments to indicate whether a lazy binding in the environment should be updated with a forced value. The closure reprsentation is already modified to contain a kind field; you may also find that the provided closure-kind function is useful.Examples:
let f = proc(x) +(x, x)
in let y = 0
in (f {set y = +(y, 1); y})
-> 2
let f = proc/name(x) +(x, x)
in let y = 0
in (f {set y = +(y, 1); y})
-> 3
let f = proc/need(x) +(x, x)
in let y = 0
in (f {set y = +(y, 1); y})
-> 2
let f = proc/name(x) +(x, x)
in let y = 0
in let z = {set y = +(y, 1); y}
in (f z)
-> 2
let f = proc/name(x) +(x, x)
in let y = 0
in let/name z = {set y = +(y, 1); y}
in (f z)
-> 3
let f = proc(x, w) +(x, w)
in let y = 0
in let z = {set y = +(y, 1); y}
in (f z z)
-> 2
let f = proc(x, w) +(x, w)
in let y = 0
in let/name z = {set y = +(y, 1); y}
in (f z z)
-> 3
let f = proc(w, x) +(x, x)
in let y = 0
in (f set y = 8 {set y = +(y, 1); y})
-> 18
let f = proc/name(w, x) +(x, x)
in let y = 0
in (f set y = 8 {set y = +(y, 1); y})
-> 3
let f = proc/need(w, x) +(x, x)
in let y = 0
in (f set y = 8 {set y = +(y, 1); y})
-> 2
let f = proc(&x) set x = +(x, 1)
in let y = 10
in { (f y) ; y }
-> 11
let f = proc(y, &x) { set x = *(x, 3); +(x, +(y, y)) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 53
let f = proc(y, &x) { set x = *(x, 3); +(+(y, y), x) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 53
let f = proc/name(y, &x) { set x = *(x, 3); +(x, +(y, y)) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 44
let f = proc/name(y, &x) { set x = *(x, 3); +(+(y, y), x) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 50
let f = proc/need(y, &x) { set x = *(x, 3); +(x, +(y, y)) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 44
let f = proc/need(y, &x) { set x = *(x, 3); +(+(y, y), x) }
in let y = 10
in (f { set y = +(y, 3); 7 } y)
-> 47
Last update: Thursday, October 18th, 2001mflatt@cs.utah.edu |