Difference between revisions of "DDC/EvaluationOrder"

From HaskellWiki
< DDC
Jump to navigation Jump to search
Line 21: Line 21:
 
Disciple <hask>do</hask> 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.
 
Disciple <hask>do</hask> 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 <hask>do</hask> expression does not perform monadic desugaring, though there is a plan [http://code.google.com/p/disciple/issues/detail?id=123 (ticket)] to overload the syntax in the future.
+
''Differences to Haskell'': The <hask>do</hask> expression does not perform monadic desugaring, though there is a plan [http://code.google.com/p/disciple/issues/detail?id=123 (ticket)] to overload the syntax in the future.
   
 
== 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|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 <hask>f @ x</hask> (read as: f suspend x) creates a thunk containing the function <hask>f</hask> and its argument <hask>x</hask>. 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.
+
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.
+
The suspension operator <hask>@</hask> accepts a variable number of arguments. Uses of <hask>@</hask> map onto a set of primitive <hask>suspend</hask> functions, which can also be used if desired.
   
To suspend the applications of `f1` and `f3` in the above example we can write:
+
To suspend the applications of <hask>f1</hask> and <hask>f3</hask> in the above example we can write:
   
  +
<haskell>
{{{
 
f a b = f1 @ (f2 a) (f3 @ b)
+
f a b = f1 @ (f2 a) (f3 @ b)
  +
</haskell>
}}}
 
   
 
which is equivalent to:
 
which is equivalent to:
   
  +
<haskell>
{{{
 
f a b
+
f a b
= do x1 = f2 a
+
= do x1 = f2 a
x2 = suspend1 f3 b
+
x2 = suspend1 f3 b
suspend2 f1 x1 x2
+
suspend2 f1 x1 x2
  +
</haskell>
}}}
 

Revision as of 11:55, 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