|Version 3 (modified by benl, 7 years ago)|
Disciple uses a strict/call-by-value evaluation order, where compound expressions are evaluated from left to right in data-dependency order.
f a b = f1 (f2 a) (f3 b)
is desugared to:
f a b = do x1 = f2 a x2 = f3 b f1 x1 x2
where x1 and x2 are fresh variables.
Disciple do expressions contain a sequence of bindings and statements, and must end with a statement. These elements are evaluated in turn, from top to bottom. Bindings may not be mutually recursive. The final statement is taken as the value of the entire expression.
Differences to Haskell: The do expression does not perform monadic desugaring, though there is a plan to overload the syntax in the future.
Introduction of laziness
Although strict evaluation is the default, any function application may be suspended with the @ operator, so long as the application does not cause visible side effects. The attempted suspension of an application with visible side effects will result in a compile-time type error.
The expression f @ x (read as: f suspend x) creates a thunk containing the function f and its argument x. When the value of this thunk is demanded at runtime, the function will be applied to its argument, yielding the result.
If a value is not suspended it is called direct. Both direct and lazy objects have the same type, are first class and interchangable. The forcing of thunks is transparent and does not require extra code in the source program.
The suspension operator @ accepts a variable number of arguments. Uses of @ map onto a set of primitive suspend functions, which can also be used if desired.
To suspend the applications of f1 and f3 in the above example we can write:
f a b = f1 @ (f2 a) (f3 @ b)
which is equivalent to:
f a b = do x1 = f2 a x2 = suspend1 f3 b suspend2 f1 x1 x2