Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rochester!pt!sei!sei.cmu.edu!firth
From: firth@sei.cmu.edu (Robert Firth)
Newsgroups: comp.lang.misc
Subject: lazy evaluation
Message-ID: <1848@aw.sei.cmu.edu>
Date: Fri, 10-Jul-87 10:32:40 EDT
Article-I.D.: aw.1848
Posted: Fri Jul 10 10:32:40 1987
Date-Received: Sun, 12-Jul-87 11:43:21 EDT
Sender: netnews@sei.cmu.edu
Lines: 42

First, to correct a misapprehension.  It is not true that
all imperative languages evaluate the arguments to a
function before the call.  The Algol-60 'call by name'
is an obvious counterexample:

	f(expr)

causes 'expr' to be passed as a thunk, ie a parameterless
procedure, and this thunk is invoked whenever the body of
f references the formal.

Secondly, different languages mean different things by
'evaluation'.  For example, consider the Algol-68 expression

	f(expr)

If f is defined as PROC(INT)INT then expr is evaluated to
yield an integer value, just as in Algol-60 call by value.
But if f is defined as PROC(PROC INT)INT then expr is evaluated
to yield an integer procedure, which is equivalent to call by
name.  In language terms, this is evaluation at the point of
call, but it mimics delayed evaluation.

However, neither of these is true lazy evaluation, because the
expression may be evaluated more than once.  In applicative
languages, repeated evaluations of an expression always yield
the same result, so we can replace delayed evaluation by true
lazy evaluation, ie evaluate once only when first referenced.

But the Algol-60 scheme does give you one benefit of lazy
evaluation: if the formal is not referenced, the actual is
not evaluated, and so the call f(expr) can still be valid even
when the evaluation of expr will fail or fail to terminate.

How easily this can be described depends on your descriptive
apparatus.  But if your compiler is going to implement routine
hoisting (pragma INLINE) then you had better be able to replace
the call with something that captures exactly the semantics of
parameter evaluation, and if the language in question can't do
that, you have to be careful, since you are transforming the
attributed tree into something from which legal source code
cannot be reconstructed.