0

The SICP book describes how to implement a Scheme interpreter in Scheme. I have been playing with this on and off for months now and my code has evolved away from the book. I have now reached the stage where having implemented a strict-eval procedure, and I am attempting to implement a lazy-eval counterpart which always returns a 'thunk' (some object encapsulating an expression and environment and memoization info). I have a force-thunk procedure which evaluates the thunk with memoization. My quick and dirty implementation currently returns a thunk wrapper around a set! expression, and of course this leads to weird semantics (no side effect occurs until the thunk is forced). I am very tempted to change this and have my lazy-eval procedure being strict on set! expressions. Actually, to be more precise, I am thinking of delaying the evaluation of the expression being assigned (so create a thunk from it), but not delay the change in binding (i.e. assign the symbol to the new thunk in the environment immediately). Is there any reason why I should choose to delay the side effect?

Sven Williamson
  • 1,094
  • 1
  • 10
  • 19
  • 1
    Making `set!` (and a few other primitives) strict, but not forcing the value, seems to be the path [Lazy Racket](http://docs.racket-lang.org/lazy/index.html) chose. – molbdnilo Oct 05 '16 at 12:40

1 Answers1

1

set! mutates a binding and changes the interpeter from being referential transparent to having to handle a changing world. I think I agree on your description that the binding should get assosiated with a new thunk instead of the old, thus the binding is not set lazy but it's value is still lazy.

(define x expression) ; ==> x is a thunk
(set! x (+ x x)       ; ==> x is a new thunk that references the old `x`
(display x)           ; display would force the evaluation of the nested thunks referenced by `x`

If you delay the actual binding phase then the code above will never force the returned value from the set!-expression since it is never used and thus x would be the old thunk that is forced in display?

Sylwester
  • 47,942
  • 4
  • 47
  • 79