[mercury-users] Partial evaluation of closures

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Mar 17 01:26:26 AEDT 2000

On 16-Mar-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> Say I write
> foo(X) = ( func(Y) = bar(X) + Y )
> will the compiler transform this into (what one would expect from)
> foo(X) = ( func(Y) = Z + Y ) :-
> 	Z = bar(X).
> or not?

In other words, evaluate `bar(X)' at the time the closure
was created, rather than when the closure was invoked?
No, the compiler will not perform that transformation.

There's several reasons for this.

One is that this transformation is not necessarily a win.  If the closure is
called many times, it may be a win, but if the closure is never called, then
it will probably be a loss.

A second reason is that it is not unconditionally valid to perform the
transformation.  If `bar(X)' does not terminate, but the closure is never
called, then this transformation would transform a terminating program into a
non-terminating one, and so by the rules defined in the semantics chapter of
the Mercury language reference manual it is forbidden.  It would only be safe
to do this transformation if the compiler could be sure that `bar(X)' would

And lastly of course there's the old favourite: we just haven't gotten
around to it yet ;-)

Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at        |     -- the last words of T. S. Garp.
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list