[mercury-users] Parametrizing modes and determinism

Ondrej Bojar obo at cuni.cz
Tue Nov 7 01:33:21 AEDT 2006

```Hi.

Is there a short way of defining a higher order predicate to cover many
mode and determinism combinations?

Let's say I'm writing a fold predicate for traversing trees:

:- type t(T) ---> t(T, list(t(T))).

:- pred foldt((pred(t(T), A, A)), t(T), A, A).

And I want many modes:

:- mode foldt((pred(in, in, out) is det), in, in, out) is det.
:- mode foldt((pred(in, di, uo) is det), in, di, uo) is det.
:- mode foldt((pred(in, di, uo) is cc_multi), in, di, uo) is cc_multi.

But the foldt implementation calls another folding predicate, e.g.
list.foldl:

foldt(Pred, T at t(_Label, Sons), InAku, OutAku) :-
Pred(T, InAku, TAku),
list__foldl(
(pred(S::in, I::WHICH_INPUT_MODE, O::WHICH_OUTPUT_MODE)
is WHICH_DETERMINISM:-
foldt(Pred, S, I, O)
), Sons, TAku, OutAku).

I have to specify the modes and the determinism for the inner closure
explicitly.
The only solution I was able to come with, was to create many different
implementations (copying the routine) and join them under the common
name foldt/4 by explicitly stating the mode (and promising purity):

foldt(Pred::in(pred(in, in, out)is det), T::in, InAku::in, OutAku::out) :-
foldt_in(Pred, T, InAku, OutAku).
foldt(Pred::in(pred(in, di, uo)is det), T::in, InAku::di, OutAku::uo) :-
foldt_di(Pred, T, InAku, OutAku).
foldt(Pred::in(pred(in, di, uo)is cc_multi), T::in, InAku::di,
OutAku::uo) :-
foldt_di_cc(Pred, T, InAku, OutAku).

Is there a better way? Especially if I also want to use specific modes
for hash_tables or similar -- which would again require special foldt/4
modes.

Does anybody have a handy preprocessor for similar situations?

Thanks, Ondrej.

--
Ondrej Bojar (mailto:obo at cuni.cz)
http://www.cuni.cz/~obo
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au