Next: , Previous: , Up: srfi lazy   [Index]


2.24.3 Specification

The following macros should be provided. The semantics, which is informally described here, should conform to that of the reference implementation.

Syntax: delay ?expression

Take an expression of arbitrary type a0 a ... and return a promise of type (Promise a0 a ...) which at some point in the future may be asked (by the force procedure) to evaluate the expression and deliver the resulting values.

Syntax: lazy ?expression

Take an expression of type (Promise a0 a ...) and return a promise of type (Promise a0 a ...) which at some point in the future may be asked (by the force procedure) to evaluate the expression and deliver the resulting promise.

The following procedures should be provided.

Function: force expression

Take an argument of type (Promise a0 a ...) and return a value of type a0 a ... as follows: if values of type a0 a ... have been computed for the promise, these values are returned. Otherwise, the promise is first evaluated, then overwritten by the obtained promise or value, and then force is again applied (iteratively) to the promise.

Function: eager expression0 expression ...

Take an argument of type a0 a ... and return a value of type (Promise a0 a ...). As opposed to delay, the argument is evaluated eagerly. Semantically, writing (eager expression) is equivalent to writing:

(let ((value expression)) (delay value))

However, the former is more efficient since it does not require unnecessary creation and evaluation of thunks. We also have the equivalence:

(delay expression) ≡ (lazy (eager expression))

The following reduction rules may be helpful for reasoning about these primitives. However, they do not express the memoization and memory usage semantics specified above:

(force (delay expression)) -> expression
(force (lazy  expression)) -> (force expression)
(force (eager value))      -> value

The typing can be succinctly expressed as follows:

type Promise a = lazy (Promise a) | eager a

       expression  : a
------------------------------
(eager expression) : Promise a

       expression  : Promise a
------------------------------
(lazy expression)  : Promise a

       expression  : a
------------------------------
(delay expression) : Promise a

       expression  : Promise a
------------------------------
(force expression) : a

Although this SRFI specifies an extension to the semantics of force, the extension is conservative in the sense that the semantics of the subset {delay, force} in isolation (i.e., as long as the program does not use lazy) agrees with that in R5RS.


Next: , Previous: , Up: srfi lazy   [Index]