new higher-order predicates for map/tree234.
Thomas Charles CONWAY
conway at cs.mu.OZ.AU
Mon Mar 30 11:55:14 AEST 1998
DJ, can you please review this?
thanks, Thomas
--
Thomas Conway || conway at cs.mu.oz.au
AD DEUM ET VINUM || Nail here [] for new monitor.
library/map.m:
added map__foldl/4 and map__map_values/3 which forward to the
corresponding predicates in tree234.
library/tree234.m:
implement tree234__foldl and tree234__map_values.
cvs diff: Diffing .
Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.97
diff -u -r1.97 NEWS
--- NEWS 1998/03/12 21:11:17 1.97
+++ NEWS 1998/03/24 01:00:20
@@ -93,6 +93,10 @@
* We have added the predicate queue__delete_all/3.
+* Map (and tree234) have 2 new predicates: map__foldl which is
+ analogous to list__foldl, and map__map_values which is analogous
+ to list__map.
+
New library packages in the `extras' distribution:
**************************************************
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing bytecode
cvs diff: Diffing bytecode/test
cvs diff: Diffing compiler
cvs diff: Diffing compiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/Togl-1.2
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/references
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing library
Index: library/array.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/array.m,v
retrieving revision 1.43
diff -u -r1.43 array.m
--- array.m 1998/01/30 05:59:40 1.43
+++ array.m 1998/03/09 00:53:50
@@ -287,7 +287,7 @@
Declare_entry(mercury__array__array_equal_2_0);
Declare_entry(mercury__array__array_compare_3_0);
-BEGIN_MODULE(array_module)
+BEGIN_MODULE(array_moduleX)
init_entry(mercury____Unify___array__array_1_0);
init_entry(mercury____Index___array__array_1_0);
init_entry(mercury____Compare___array__array_1_0);
@@ -311,12 +311,12 @@
/* Ensure that the initialization code for the above module gets run. */
/*
-INIT sys_init_array_module
+INIT sys_init_array_moduleX
*/
-void sys_init_array_module(void); /* suppress gcc -Wmissing-decl warning */
-void sys_init_array_module(void) {
+void sys_init_array_moduleX(void); /* suppress gcc -Wmissing-decl warning */
+void sys_init_array_moduleX(void) {
extern ModuleFunc array_module;
- array_module();
+ array_moduleX();
}
").
Index: library/map.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/map.m,v
retrieving revision 1.61
diff -u -r1.61 map.m
--- map.m 1998/01/23 12:33:23 1.61
+++ map.m 1998/03/24 00:45:19
@@ -175,6 +175,19 @@
:- pred map__remove_smallest(map(K, V), K, V, map(K, V)).
:- mode map__remove_smallest(in, out, out, out) is semidet.
+ % Perform an inorder tranversal of the map, applying
+ % an accumulator predicate for each key-value pair.
+:- pred map__foldl(pred(K, V, T, T), map(K, V), T, T).
+:- 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.
+
+ % Apply a transformation predicate to all the values
+ % in a map.
+:- pred map__map_values(pred(K, V, W), map(K, V), map(K, W)).
+:- mode map__map_values(pred(in, in, out) is det, in, out) is det.
+:- mode map__map_values(pred(in, in, out) is semidet, in, out) is semidet.
+
%-----------------------------------------------------------------------------%
:- import_module tree234.
@@ -385,6 +398,16 @@
map__remove_smallest(Map0, K, V, Map) :-
tree234__remove_smallest(Map0, K, V, Map).
+
+%-----------------------------------------------------------------------------%
+
+map__foldl(Pred, Map, Acc0, Acc) :-
+ tree234__foldl(Pred, Map, Acc0, Acc).
+
+%-----------------------------------------------------------------------------%
+
+map__map_values(Pred, Map0, Map) :-
+ tree234__map_values(Pred, Map0, Map).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
Index: library/tree234.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/tree234.m,v
retrieving revision 1.24
diff -u -r1.24 tree234.m
--- tree234.m 1998/01/23 12:33:43 1.24
+++ tree234.m 1998/03/24 00:45:45
@@ -79,6 +79,16 @@
:- pred tree234__tree234_to_assoc_list(tree234(K, V), assoc_list(K, V)).
:- mode tree234__tree234_to_assoc_list(in, out) is det.
+:- pred tree234__foldl(pred(K, V, T, T), tree234(K, V), T, T).
+:- mode tree234__foldl(pred(in, in, in, out) is det, in, in, out) is det.
+:- mode tree234__foldl(pred(in, in, in, out) is semidet, in, in, out)
+ is semidet.
+:- mode tree234__foldl(pred(in, in, di, uo) is det, in, di, uo) is det.
+
+:- pred tree234__map_values(pred(K, V, W), tree234(K, V), tree234(K, W)).
+:- mode tree234__map_values(pred(in, in, out) is det, in, out) is det.
+:- mode tree234__map_values(pred(in, in, out) is semidet, in, out) is semidet.
+
%------------------------------------------------------------------------------%
%------------------------------------------------------------------------------%
@@ -2059,6 +2069,56 @@
tree234__tree234_to_assoc_list_2(T2, [K2 - V2 | L1], L2),
tree234__tree234_to_assoc_list_2(T1, [K1 - V1 | L2], L3),
tree234__tree234_to_assoc_list_2(T0, [K0 - V0 | L3], L).
+
+%------------------------------------------------------------------------------%
+
+tree234__foldl(_Pred, empty, Acc, Acc).
+tree234__foldl(Pred, two(K, V, T0, T1), Acc0, Acc) :-
+ tree234__foldl(Pred, T0, Acc0, Acc1),
+ call(Pred, K, V, Acc1, Acc2),
+ tree234__foldl(Pred, T1, Acc2, Acc).
+tree234__foldl(Pred, three(K0, V0, K1, V1, T0, T1, T2), Acc0, Acc) :-
+ tree234__foldl(Pred, T0, Acc0, Acc1),
+ call(Pred, K0, V0, Acc1, Acc2),
+ tree234__foldl(Pred, T1, Acc2, Acc3),
+ call(Pred, K1, V1, Acc3, Acc4),
+ tree234__foldl(Pred, T2, Acc4, Acc).
+tree234__foldl(Pred, four(K0, V0, K1, V1, K2, V2, T0, T1, T2, T3), Acc0, Acc) :-
+ tree234__foldl(Pred, T0, Acc0, Acc1),
+ call(Pred, K0, V0, Acc1, Acc2),
+ tree234__foldl(Pred, T1, Acc2, Acc3),
+ call(Pred, K1, V1, Acc3, Acc4),
+ tree234__foldl(Pred, T2, Acc4, Acc5),
+ call(Pred, K2, V2, Acc5, Acc6),
+ tree234__foldl(Pred, T3, Acc6, Acc).
+
+%------------------------------------------------------------------------------%
+
+tree234__map_values(_Pred, empty, empty).
+tree234__map_values(Pred, Tree0, Tree) :-
+ Tree0 = two(K0, V0, Left0, Right0),
+ Tree = two(K0, W0, Left, Right),
+ call(Pred, K0, V0, W0),
+ tree234__map_values(Pred, Left0, Left),
+ tree234__map_values(Pred, Right0, Right).
+tree234__map_values(Pred, Tree0, Tree) :-
+ Tree0 = three(K0, V0, K1, V1, Left0, Middle0, Right0),
+ Tree = three(K0, W0, K1, W1, Left, Middle, Right),
+ call(Pred, K0, V0, W0),
+ call(Pred, K1, V1, W1),
+ tree234__map_values(Pred, Left0, Left),
+ tree234__map_values(Pred, Middle0, Middle),
+ tree234__map_values(Pred, Right0, Right).
+tree234__map_values(Pred, Tree0, Tree) :-
+ Tree0 = four(K0, V0, K1, V1, K2, V2, Left0, LMid0, RMid0, Right0),
+ Tree = four(K0, W0, K1, W1, K2, W2, Left, LMid, RMid, Right),
+ call(Pred, K0, V0, W0),
+ call(Pred, K1, V1, W1),
+ call(Pred, K2, V2, W2),
+ tree234__map_values(Pred, Left0, Left),
+ tree234__map_values(Pred, LMid0, LMid),
+ tree234__map_values(Pred, RMid0, RMid),
+ tree234__map_values(Pred, Right0, Right).
%------------------------------------------------------------------------------%
cvs diff: Diffing lp_solve
cvs diff: Diffing lp_solve/lp_examples
cvs diff: Diffing profiler
cvs diff: Diffing runtime
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing scripts
cvs diff: Diffing tools
cvs diff: Diffing trial
cvs diff: Diffing util
More information about the developers
mailing list