[mercury-users] higher order preds and type variables

Ondrej Bojar bojar at csse.unimelb.edu.au
Thu Nov 22 22:19:57 AEDT 2007


Dear Barney,

The problem is not related to the higher-order call, it's just a plain type error.

By declaring

:- pred p2(pred(A)::in(pred(in) is det)) is det.

you say that the caller of p2 may choose an arbitrary type A (to be the type of 
the input argument of the higher order argument; but that's not important).

In the implementation of p2, you however uniquely decide that A can only be the 
aa type.

p2(P) :- P(aa).

Hence the error: you're breaking your promise to the caller of p2, the caller is 
not free to choose the type. The error message says the same thing assuming the 
converse: that the promise to the caller is valid and that by stating P(aa) you 
are overspecifying the type.

Cheers, Ondrej.

Barney Fisher wrote:
> Hi,
> 
> I have a program where a polymorphic pred is passed as input to
> another predicate and then called with values of a specific type, but
> I am getting compile errors which I don't understand.  The following
> program illustrates what i'm trying to do.
> 
> -----------------------------------------
> :- module ho.
> :- interface.
> :- import_module io.
> 
> :- pred main(io::di,io::uo) is det.
> 
> :- implementation.
> 
> :- type aa ---> aa.
> 
> :- pred p1(A::in) is det.
> p1(_).
> 
> :- pred p2(pred(A)::in(pred(in) is det)) is det.
> p2(P) :- P(aa).
> 
> main(!IO) :- p2(p1).
> ---------------------------------------------
> 
> When I try compiling this I get
> ho.m:017: In clause for predicate `p2'/1:
> ho.m:017:   in argument 2 (i.e. argument 1 of the called predicate) of
> ho.m:017:   higher-order predicate call:
> ho.m:017:   type error: argument has type `(ho.aa)',
> ho.m:017:   expected type was `(some [A] A)'.
> 
> Could someone please explain to me the problem (and a solution if possible!)?
> 
> Thanks
> --
> Barney
> --------------------------------------------------------------------------
> mercury-users mailing list
> Post messages to:       mercury-users at csse.unimelb.edu.au
> Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
> Subscriptions:          mercury-users-request at csse.unimelb.edu.au
> --------------------------------------------------------------------------
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list