lazy.dylan 0.1 -- add ML/Scheme-style lazy evaluation to Dylan INSTALLATION ------------ $ make $ su # make install USAGE ----- This is a little package to add lazy evaluation to Dylan. It exports three names: o A class representing delayed computations. Instances of should only be created and manipulated with delay/force; this name is only exported so you can use it as a specializer. o delay A statement macro to turn the body it contains into a . The value of the delay statement is the suspension. Its value should be returned or bound to a variable, unless you just want to create a suspension and ignore it. o force A method that will return the value of the suspension. The first time it is called, it will compute and save the value of the suspension. On subsequent calls it will just return the saved value. This behavior is often useful when writing amortized-time algorithms. EXAMPLE ------- define method nth-prime(n :: ) // find the nth prime ... end method nth-prime; begin let foo = delay nth-prime(500) end; // fast -- returns suspension format-out("%d\n", force(foo)); // slow -- computes prime format-out("%d\n", force(foo)); // fast -- uses memoized value end; NOTES ----- Note that computations are memoized, which means that if you try to reevaluate a suspension, it will return the original value. This gives the correct behavior for functional-style programs, and breaks horribly if you are mixing mutation and lazy evaluation. But if you were mixing lazy evaluation and mutation, you were screwed at the starting gate anyway, so no worries. :) Also, when your computation raises a signal, the signal is not memoized. Instead, the signal is just raised as usual, and when the suspension is ``force''-d again, the whole function is recalculated. This is because Dylan has recoverable exceptions but does not have first-class continuations, so it's not possible to save the execution state in a way that attempts to recover don't get wodged. (If this means nothing to you, don't worry about it.) BUGS ---- As of GD 2.3.2, there is a bug in d2c's code generation that causes the emitted C code to break on statements of the form ``apply(values, ...)''. This means the lazy module won't work with GD yet. It also means there are probably other bugs that I haven't found because I could not fully debug the module. Fun-Dev users are also encouraged to try this out and tell me if they find problems on their Dylan, so I can fix it. :)