[m-rev.] diff: creation of singleton bags

Julien Fischer jfischer at opturion.com
Sun May 7 03:02:38 AEST 2017


Creation of singleton bags.

library/bag.m:
     Add a function for creating singleton bags.

compiler/term_traversal.m:
     Use the new function in couple of spots.

NEWS:
     Announce the above addition.

Julien.

diff --git a/NEWS b/NEWS
index e1987374c..a72ff3f8f 100644
--- a/NEWS
+++ b/NEWS
@@ -446,8 +446,9 @@ Changes to the Mercury standard library:
    - return_sccs_in_from_to_order/1
    - return_sccs_in_to_from_order/1

-* We have added the following predicates and function to the bag module:
+* We have added the following predicates and functions to the bag module:

+  - singleton/1
    - insert_duplicates/4
    - det_insert_duplicates/4
    - det_insert_duplicates/3
diff --git a/compiler/term_traversal.m b/compiler/term_traversal.m
index 09fe53729..ebceedb3a 100644
--- a/compiler/term_traversal.m
+++ b/compiler/term_traversal.m
@@ -158,9 +158,8 @@ term_traverse_goal(ModuleInfo, Params, Goal, !Info) :-
              )
          ;
              Unification = assign(OutVar, InVar),
-            bag.init(Empty),
-            bag.insert(InVar, Empty, InVars),
-            bag.insert(OutVar, Empty, OutVars),
+            InVars = bag.singleton(InVar),
+            OutVars = bag.singleton(OutVar),
              record_change(InVars, OutVars, 0, [], !Info)
          ;
              Unification = simple_test(_InVar1, _InVar2)
diff --git a/library/bag.m b/library/bag.m
index 968725e26..1a22535dc 100644
--- a/library/bag.m
+++ b/library/bag.m
@@ -33,6 +33,10 @@
  :- func init = bag(T).
  :- pred init(bag(T)::out) is det.

+    % Create a bag containing the given item.
+    %
+:- func singleton(T) = bag(T).
+
      % Check whether a bag is empty.
      %
  :- pred is_empty(bag(T)::in) is semidet.
@@ -398,6 +402,9 @@ init = Bag :-
  init(bag(Map)) :-
      map.init(Map).

+singleton(Item) = bag(Map) :-
+    Map = map.singleton(Item, 1).
+
  is_empty(bag(Map)) :-
      map.is_empty(Map).



More information about the reviews mailing list