4 In a lazy (or non-strict) language, the arguments to a function are
5 not evaluated before calling the function. Instead, the arguments are
6 suspended and only evaluated by the function if needed.
8 <:StandardML:Standard ML> is an eager (or strict) language, not a lazy
9 language. However, it is easy to delay evaluation of an expression in
10 SML by creating a _thunk_, which is a nullary function. In SML, a
11 thunk is written `fn () => e`. Another essential feature of laziness
12 is _memoization_, meaning that once a suspended argument is evaluated,
13 subsequent references look up the value. We can express this in SML
14 with a function that maps a thunk to a memoized thunk.
20 val lazy: (unit -> 'a) -> unit -> 'a
24 This is easy to implement in SML.
28 structure Lazy: LAZY =
30 fun lazy (th: unit -> 'a): unit -> 'a =
32 datatype 'a lazy_result = Unevaluated of (unit -> 'a)
36 val r = ref (Unevaluated th)
42 handle x => (r := Failed x; raise x)
43 val () = r := Evaluated a