[mercury-users] map__foldl2

Warwick Harvey wharvey at cs.monash.edu.au
Tue Dec 14 12:12:04 AEDT 1999


ddw at miscrit.be (Dominique de Waleffe) wrote:
> I wanted to have a map__foldl2 with the 2nd accumulator being the io__state.
> I did not want to reimplement the whole thing down to the tree234 library.
> So I attempted to use the following code :
> 
> :- module mapx.
> :- interface.
> :-import_module map.
> :- pred mapx__foldl2(pred(K, V, T, T,TT,TT), map(K, V), T, T,TT,TT).
> :- mode mapx__foldl2(pred(in, in, in, out, in, out) is det,
> 		     in, in, out, in, out) is det.
> :- mode mapx__foldl2(pred(in, in, in, out, di, uo) is det,
> 		     in, in, out, di, uo) is det.
> 
> :- implementation.
> :- import_module std_util.
> 
> :- inst inst1 = bound(ground-unique ; ground-ground).
> :- inst inst2 = bound(ground-dead; ground-ground).
> :-mode indi :: inst1 ->inst2.
> :-inst inst4 = bound(ground-unique;ground-ground).
> :-mode outuo :: free -> inst4.
> 
> mapx__foldl2(P,M,L0,L,LL0,LL):-
> 	map__foldl((pred(K::in,V::in,A0::indi,A::outuo) is det:-
> 		    A0=AL0-ALL0,
> 		    P(K,V,AL0,AL,ALL0,ALL),
> 		    A=AL-ALL),
> 		   M,L0-LL0,L-LL).
> 
> :- end_module mapx.
> 
> Of course, the compiler does not [accept] it :-)

Note the available modes of `map__foldl':

:- mode map__foldl(pred(in, in, in, out) is det, in, in, out) is det.
:- mode map__foldl(pred(in, in, in, out) is semidet, in, in, out) is semidet.
:- mode map__foldl(pred(in, in, di, uo) is det, in, di, uo) is det.

This means the 3rd and 4th arguments of the predicate you pass must either 
have modes `di' and `uo', or `in' and `out'.  Unfortunately, your `indi' and 
`outuo' do not match either of these.

What's the other argument pair going to be in your application?  Is the 
initial input likely to be small, or big and complicated?  If it's going to 
be small (e.g. an empty list, an integer, etc.) then it's probably not going 
to hurt you much to make it unique (using `copy/2' if necessary).

I also thought this looked a bit strange:

> :- inst inst1 = bound(ground-unique ; ground-ground).

I don't think this means what you intend it to mean.  Do you really mean 
that either of these are allowed, or are you trying to (effectively) get the 
higher-order predicate to work in multiple modes?

Unfortunately, it seems one cannot do this kind of wrapping trick with 
higher-order predicates if you want the result to work in multiple modes.  
:-(

Warwick

--------------------------------------------------------------------------
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