[m-rev.] diff: add pred version of set.map

Zoltan Somogyi zs at csse.unimelb.edu.au
Mon Jul 21 13:12:51 AEST 2008


library/set.m:
	Add a predicate version of set.map.

library/map.m:
	Fix some variable names, and some style issues.

Zoltan.

cvs diff: Diffing .
Index: map.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.110
diff -u -b -r1.110 map.m
--- map.m	30 Jul 2007 06:03:07 -0000	1.110
+++ map.m	19 Jul 2008 16:10:10 -0000
@@ -606,8 +606,8 @@
     tree234.search(Map, K, V).
 
 map.lookup(Map, K, V) :-
-    ( tree234.search(Map, K, V1) ->
-        V = V1
+    ( tree234.search(Map, K, VPrime) ->
+        V = VPrime
     ;
         report_lookup_error("map.lookup: key not found", K, V)
     ).
@@ -616,9 +616,9 @@
     tree234.lower_bound_search(Map, SearchK, K, V).
 
 map.lower_bound_lookup(Map, SearchK, K, V) :-
-    ( tree234.lower_bound_search(Map, SearchK, K1, V1) ->
-        K = K1,
-        V = V1
+    ( tree234.lower_bound_search(Map, SearchK, KPrime, VPrime) ->
+        K = KPrime,
+        V = VPrime
     ;
         report_lookup_error("map.lower_bound_lookup: key not found",
             SearchK, V)
@@ -628,9 +628,9 @@
     tree234.upper_bound_search(Map, SearchK, K, V).
 
 map.upper_bound_lookup(Map, SearchK, K, V) :-
-    ( tree234.upper_bound_search(Map, SearchK, K1, V1) ->
-        K = K1,
-        V = V1
+    ( tree234.upper_bound_search(Map, SearchK, KPrime, VPrime) ->
+        K = KPrime,
+        V = VPrime
     ;
         report_lookup_error("map.upper_bound_lookup: key not found",
             SearchK, V)
@@ -644,51 +644,34 @@
     tree234.insert(Map0, K, V, Map).
 
 map.det_insert(Map0, K, V, Map) :-
-    ( tree234.insert(Map0, K, V, Map1) ->
-        Map = Map1
+    ( tree234.insert(Map0, K, V, MapPrime) ->
+        Map = MapPrime
     ;
         report_lookup_error("map.det_insert: key already present", K, V)
     ).
 
-map.det_insert_from_corresponding_lists(Map0, Ks, Vs, Map) :-
-    (
-        Ks = [Key | Keys],
-        Vs = [Value | Values]
-    ->
-        map.det_insert(Map0, Key, Value, Map1),
-        map.det_insert_from_corresponding_lists(Map1, Keys, Values,
-            Map)
-    ;
-        Ks = [],
-        Vs = []
-    ->
-        Map = Map0
-    ;
-        error("map.det_insert_from_corresponding_lists - " ++
-            "lists do not correspond")
-    ).
+map.det_insert_from_corresponding_lists(Map, [], [], Map).
+map.det_insert_from_corresponding_lists(_, [], [_ | _], _) :-
+    error("map.det_insert_from_corresponding_lists - lists do not correspond").
+map.det_insert_from_corresponding_lists(_, [_ | _], [], _) :-
+    error("map.det_insert_from_corresponding_lists - lists do not correspond").
+map.det_insert_from_corresponding_lists(Map0, [K | Ks], [V | Vs], Map) :-
+    map.det_insert(Map0, K, V, Map1),
+    map.det_insert_from_corresponding_lists(Map1, Ks, Vs, Map).
 
 map.det_insert_from_assoc_list(Map, [], Map).
 map.det_insert_from_assoc_list(Map0, [K - V | KVs], Map) :-
     map.det_insert(Map0, K, V, Map1),
     map.det_insert_from_assoc_list(Map1, KVs, Map).
 
-map.set_from_corresponding_lists(Map0, Ks, Vs, Map) :-
-    (
-        Ks = [Key | Keys],
-        Vs = [Value | Values]
-    ->
-        map.set(Map0, Key, Value, Map1),
-        map.set_from_corresponding_lists(Map1, Keys, Values, Map)
-    ;
-        Ks = [],
-        Vs = []
-    ->
-        Map = Map0
-    ;
-        error("map.set_from_corresponding_lists - " ++
-            "lists do not correspond")
-    ).
+map.set_from_corresponding_lists(Map, [], [], Map).
+map.set_from_corresponding_lists(_, [], [_ | _], _) :-
+    error("map.set_from_corresponding_lists - lists do not correspond").
+map.set_from_corresponding_lists(_, [_ | _], [], _) :-
+    error("map.set_from_corresponding_lists - lists do not correspond").
+map.set_from_corresponding_lists(Map0, [K | Ks], [V | Vs], Map) :-
+    map.set(Map0, K, V, Map1),
+    map.set_from_corresponding_lists(Map1, Ks, Vs, Map).
 
 map.set_from_assoc_list(Map, [], Map).
 map.set_from_assoc_list(Map0, [K - V | KVs], Map) :-
@@ -699,8 +682,8 @@
     tree234.update(Map0, K, V, Map).
 
 map.det_update(Map0, K, V, Map) :-
-    ( tree234.update(Map0, K, V, Map1) ->
-        Map = Map1
+    ( tree234.update(Map0, K, V, MapPrime) ->
+        Map = MapPrime
     ;
         report_lookup_error("map.det_update: key not found", K, V)
     ).
@@ -709,13 +692,10 @@
     tree234.transform_value(P, K, !Map).
 
 map.det_transform_value(P, K, !Map) :-
-    (
-        map.transform_value(P, K, !.Map, NewMap)
-    ->
+    ( map.transform_value(P, K, !.Map, NewMap) ->
         !:Map = NewMap
     ;
-        report_lookup_error("map.det_transform_value: key not found",
-            K)
+        report_lookup_error("map.det_transform_value: key not found", K)
     ).
 
 map.det_transform_value(F, K, Map0) = Map :-
@@ -760,9 +740,9 @@
     tree234.remove(Map0, Key, Value, Map).
 
 map.det_remove(Map0, Key, Value, Map) :-
-    ( tree234.remove(Map0, Key, Value1, Map1) ->
-        Value = Value1,
-        Map = Map1
+    ( tree234.remove(Map0, Key, ValuePrime, MapPrime) ->
+        Value = ValuePrime,
+        Map = MapPrime
     ;
         report_lookup_error("map.det_remove: key not found", Key, Value)
     ).
@@ -840,8 +820,8 @@
     map.init(NewMap0),
     map.select_2(KeyList, Original, NewMap0, NewMap).
 
-:- pred map.select_2(list(K)::in, map(K, V)::in, map(K, V)::in,
-    map(K, V)::out) is det.
+:- pred map.select_2(list(K)::in, map(K, V)::in,
+    map(K, V)::in, map(K, V)::out) is det.
 :- pragma type_spec(map.select_2/4, K = var(_)).
 
 map.select_2([], _Original, New, New).
@@ -987,16 +967,13 @@
             ;
                 Common1 = Common0
             ),
-            Common = map.common_subset_2(AssocTail1, AssocTail2,
-                Common1)
+            Common = map.common_subset_2(AssocTail1, AssocTail2, Common1)
         ;
             R = (<),
-            Common = map.common_subset_2(AssocTail1, AssocList2,
-                Common0)
+            Common = map.common_subset_2(AssocTail1, AssocList2, Common0)
         ;
             R = (>),
-            Common = map.common_subset_2(AssocList1, AssocTail2,
-                Common0)
+            Common = map.common_subset_2(AssocList1, AssocTail2, Common0)
         )
     ).
 
@@ -1010,10 +987,8 @@
 
 :- pred map.union_2(assoc_list(K, V), assoc_list(K, V), pred(V, V, V),
     map(K, V), map(K, V)).
-:- mode map.union_2(in, in, pred(in, in, out) is semidet, in, out)
-    is semidet.
-:- mode map.union_2(in, in, pred(in, in, out) is det, in, out)
-    is det.
+:- mode map.union_2(in, in, pred(in, in, out) is semidet, in, out) is semidet.
+:- mode map.union_2(in, in, pred(in, in, out) is det, in, out) is det.
 
 map.union_2(AssocList1, AssocList2, CommonPred, !Common) :-
     (
Index: set.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/set.m,v
retrieving revision 1.83
diff -u -b -r1.83 set.m
--- set.m	6 Jun 2008 09:08:50 -0000	1.83
+++ set.m	20 Jul 2008 02:45:36 -0000
@@ -233,6 +233,8 @@
     % map(F, S) =
     %   list_to_set(list.map(F, to_sorted_list(S))).
     %
+:- pred set.map(pred(T1, T2), set(T1), set(T2)).
+:- mode set.map(in(pred(in, out) is det), in, out) is det.
 :- func set.map(func(T1) = T2, set(T1)) = set(T2).
 
     % set.map_fold(P, S0, S, A0, A) :-
@@ -488,6 +490,11 @@
 set.count(S) = N :-
     set.count(S, N).
 
+set.map(P, S1, S2) :-
+    set.to_sorted_list(S1, L1),
+    list.map(P, L1, L2),
+    set.list_to_set(L2, S2).
+
 set.map(F, S1) = S2 :-
     S2 = set.list_to_set(list.map(F, set.to_sorted_list(S1))).
 
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list