[mercury-users] map__foldl2

Dominique de Waleffe ddw at miscrit.be
Tue Dec 14 04:03:19 AEDT 1999


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 it :-)

mapx.m:030: In clause for `foldl2((pred(in, in, in, out, in, out) is det),
in, in, out, in, out)':
mapx.m:030:   in call to predicate `map:foldl/4':
mapx.m:030:   mode error: arguments `TypeInfo_for_K, TypeInfo_for_V,
TypeInfo_37, V_21, M, V_22, V_23'
mapx.m:030:   have insts `ground, ground,
unique(type_info(unique(<type_ctor_info for std_util:pair/2>), ground,
ground)), /* unique */(pred((ground -> ground), (ground -> ground),
((mapx:inst1) -> (mapx:inst2)), (free -> (mapx:inst4))) is det), ground,
unique(std_util:'-'(ground, ground)), free',
mapx.m:030:   which does not match any of the modes for predicate
`map:foldl/4'.
mapx.m:032: In clause for `foldl2((pred(in, in, in, out, di, uo) is det),
in, in, out, di, uo)':
mapx.m:032:   in argument 6 (i.e. argument 5 of the called predicate) of
higher-order predicate call:
mapx.m:032:   mode error: variable `ALL0' has instantiatedness `ground',
mapx.m:032:   expected instantiatedness was `unique'.

How can I achieve this without changing the library?


Of course, one can easily do fold<n> in the case of lists since the
traversal of the list is obvious and exposed. The same is not true for map's
or tree234's.....

Thanks for your advice.

D.


------------------------------------
Dominique de Waleffe
Mission Critical
Tel: + 32 2 757 10 15
Email: ddw at miscrit.be

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