[mercury-users] the closure constructor

Ralph Becket rafe at cs.mu.OZ.AU
Fri Jan 4 10:45:50 AEDT 2002


Dave Slutzkin, Thursday,  3 January 2002:
> Hi guys.
> 
> Just a quick question about 'closure'.  I presume this
> is the type constructor for a closure - but I can't
> find it anywhere in the docs.
> 
> For instance, in some of Fergus' lazy evaluation code:
> 
>         :- inst lazy == lazy(ground). 
>         :- inst lazy(I) ---> value(I) ; closure((func) = out(I) is det). 
> 
>         :- implementation. 
>         :- type lazy(T) ---> value(T) ; closure((func) = T). 
> 
>         val(X) = value(X). 
>         delay(X) = closure(X). 
>         force(value(X)) = X. 
>         force(closure(F)) = apply(F). 
> 
> Is it just a simple type constructor?

Notionally, a closure is just a pair consisting of a function pointer
and an environment.  Whenever you pass around a lambda or partial
application in Mercury, you are passing around a closure.

The type of a closure that takes no arguments and returns a value of
type T is just (func) = T - the parentheses are only necessary because
of the precedence of `func'.

The lazy(T) type is implemented using two constructors: value(X) means
that X has already been evaluated and closure(C) means that you have to
evaluate the closure C to obtain the value of the delayed computation
(see the second clause of force/1).

So closure/1 is indeed just an ordinary type constructor.

- Ralph
--------------------------------------------------------------------------
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