[mercury-users] Lists of predicates

Ralph Becket rbeck at microsoft.com
Thu Jun 7 22:36:23 AEST 2001


> Hi,
> 
> If I wish to create a list or map of predicates, the type ends up
being
> something like list(pred(int, string)) but I always end up with mode
> errors as I have not specified the modes of the predicates. Is it
possible
> to pass lists and maps of predicates or functions around? Is some kind
of
> inst cast required, or do you just have to define the insts correctly
on
> the predicates that receive them?
> 
> :- pred do_something(int, list(pred(int, string), string).
> :- mode do_something(in, in(does something go here?), out) is det.

Yes, because of the mode system you also have to declare
special modes for higher order types:

	% Declare a type and inst for convenience for the preds.
	%
:- type my_pred == pred(int, string).
:- inst my_pred == pred(in, out) is det.   % For example.

	% Type and insts for lists of these preds.
	%
:- type my_preds == list(my_pred).
:- inst my_preds == list_skel(my_pred).    % See list.m for list_skel/1.

	% My pred signature.
	%
:- pred do_something(int, my_preds, string).
:- mode do_something(in, in(my_preds), out) is det.

Unfortunately the current mode system does not support mode
polymorphism so you cannot pass these lists to, say, list__map
or list__foldl and expect them to work (those predicates have
`in' modes rather than `in(my_preds)', which causes the mode
info. for the preds to be lost).

Work is going on to provide mode polymorphism which would largely
solve this problem.  For instance, list__map might be given the
signature

:- func map(func(T1, T2) = T2, list(T1), T2) = T2.
:- mode map(func(in(I1), in(I2)) = out is det, in(list_skel(I1)),
in(I2))
                = out(I2).

That's pretty horrible, but c'est la vie.  I believe support for
this does exist in the current ROTD compiler, but still has some
bugs to be worked out.

- 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