[mercury-users] constructor class syntax
Ralph Becket
rbeck at microsoft.com
Tue Nov 2 23:16:36 AEDT 1999
> can't go making mistakes like that. But now I'm wondering, is
> it possible
> to "return" a predicate from another predicate? To have a higher order
> argument that has mode out, even.
Sure. For example,
:- func compose(func(T2) = T3, func(T1) = T2) = (func(T1) = T3).
:- mode compose(func(in) = out is det, func(in) = out is det) =
out(func(in) = out is det) is det.
compose(F, G) = ( func(X) = F(G(X)) ).
Of course, you'd probably write this in the more perspicuous style of
:- func compose(func(T2) = T3, func(T1) = T2, T1) = T3.
compose(F, G, X) = F(G(X)).
and let currying do all the hard work. Unfortunately the mode system can
be a right pain when it comes to higher order programming. One thing that
will make all the difference will be higher order modes, but there's a bit
of research left to do there!
You could write the first version in a slightly more readable style by
defining some new modes:
:- inst fn1 = (func(in) = out is det).
:- mode fn1_in :: in(fn1).
:- mode fn1_out :: out(fn1).
:- func compose(...) = ...
:- mode compose(fn1_in, fn1_in) = fn1_out is det.
Hasn't Mercury got a lot of equivalence operators (=, ==, ::)? Is there
any need for so many?
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