[mercury-users] Question

Juergen Stuber juergen at mpi-sb.mpg.de
Sun Aug 22 20:03:13 AEST 1999


Maria Garcia de la Banda <mbanda at floyd.cs.monash.edu.au> writes:
>
> I am trying to define a predicate which among other things receives a
> function as first argument, applies something to it, and returns the
> resulting function. For example:
> 
> :- mode out(T) :: new -> T.

out(T) is predefined, you don't need it.

> :- pred try(func(int,int)=int,int,func(int) = int).
> :- mode try(func(in,in) = out is det,in,out(func(in) = out is det)) is det.
> 
> try(X,Y,Z):-
>         Z = apply(X,Y).

What is apply?  I couldn't find it in the manual.

> But the compiler complains about the type of X not being func(A) = B.
> Am I doing anything wrong? 

I think automatic currying doesn't apply here, so you have to
do it explicitly.

> is there a better way to do it?

Here's my version (compose is a bonus):

:- module try.
:- interface.

:- implementation.

:- func apply( func(A) = B,  A) = B.
:- mode apply( func(in)=out is det, in) = out is det.

apply(F,A)=F(A).

:- pred try( func( A, B ) = C,            A,  func(B) = C ).
:- mode try( func( in, in ) = out is det, in, out(func(in) = out is det)) is det.

try( F, Y, FY ) :-
%    FY = apply(F,Y).
    FY = (func( Z::in ) = (FYZ::out) is det :- FYZ=F(Y,Z)).

:- func compose( func(A)=B, func(B)=C ) = (func(A)=C).
:- mode compose( func(in)=out is det, func(in)=out is det) = out(func(in)=out is det) is det.

compose(F,G) = (func( A::in ) = (B::out) is det :- B=G(F(A))).

:- end_module try.

Jürgen

-- 
Jürgen Stuber <juergen at mpi-sb.mpg.de>
http://www.mpi-sb.mpg.de/~juergen/
--------------------------------------------------------------------------
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