Difference between revisions of "DDC/EvaluationOrder"
Line 24: | Line 24: | ||
== Introduction of Laziness == |
== Introduction of Laziness == |
||
− | Although strict evaluation is the default, any function application may be suspended with the <hask>@</hask> operator, so long as the application does not cause visible, impure [DDC/SideEffects]. The attempted suspension of an application with visible side effects will result in a compile-time type error. |
+ | Although strict evaluation is the default, any function application may be suspended with the <hask>@</hask> operator, so long as the application does not cause visible, impure [[DDC/SideEffects|SideEffects]]. 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. |
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. |
Revision as of 11:46, 16 March 2008
Evaluation Order
Disciple uses a strict/call-by-value evaluation order, where compound expressions are evaluated from left to right in data-dependency order.
This function:
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.
do Expressions
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 (ticket) 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, impure SideEffects. 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
}}}