[m-rev.] for review: add funtions for singleton maps, bimaps, etc

Julien Fischer juliensf at csse.unimelb.edu.au
Wed May 4 01:18:20 AEST 2011


For review anyone.

Branches: main

Add functions for creating singleton maps (of various sorts) to the standard
library.  The rationale for this to replace the following sequence of code,
which occurs a lot throughout the Mercury system:

     map.init(Map0),
     map.det_insert(SomeKey, SomeValue, Map0, Map)

library/bimap.m:
library/injection.m:
library/map.m:
library/rbtree.m:
library/tree234.m:
 	Add the new function singleton/2 that takes single key-value
 	pair as arguments and returns a new map.

compiler/disj_gen.m:
 	Avoid ambiguity in a spot.

NEWS:
 	Announce the additions.

Julien.

Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.562
diff -u -r1.562 NEWS
--- NEWS	3 May 2011 14:22:13 -0000	1.562
+++ NEWS	3 May 2011 15:10:26 -0000
@@ -49,6 +49,10 @@
    queue.get_from_back/3, queue.put_list_on_front/3,
    queue.get_from_back/3.

+* We have add the following new functions for creating singleton
+  maps: bimap.singleton/2, injection.singleton/2, map.singleton/2,
+  rbtree.singleton/2 and tree234.singleton/2 .
+

  NEWS for Mercury 11.01
  ----------------------
Index: compiler/disj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/disj_gen.m,v
retrieving revision 1.116
diff -u -r1.116 disj_gen.m
--- compiler/disj_gen.m	30 Dec 2010 11:17:53 -0000	1.116
+++ compiler/disj_gen.m	3 May 2011 06:33:52 -0000
@@ -215,12 +215,12 @@
      % generate_branch_end won't want to overwrite BaseReg.
      acquire_reg_not_in_storemap(StoreMap, BaseReg, !CI),

-    BaseRegInitCode = singleton(
+    BaseRegInitCode = cord.singleton(
          llds_instr(assign(BaseReg,
              mem_addr(heap_ref(SolnVectorAddrRval, 0, const(llconst_int(0))))),
              "Compute base address for this case")
      ),
-    SaveSlotCode = singleton(
+    SaveSlotCode = cord.singleton(
          llds_instr(assign(CurSlot, const(llconst_int(NumOutVars))),
              "Setup current slot in the solution array")
      ),
Index: library/bimap.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/bimap.m,v
retrieving revision 1.31
diff -u -r1.31 bimap.m
--- library/bimap.m	3 May 2011 04:35:00 -0000	1.31
+++ library/bimap.m	3 May 2011 06:22:26 -0000
@@ -37,6 +37,10 @@
  :- func bimap.init = bimap(K, V).
  :- pred bimap.init(bimap(K, V)::out) is det.

+    % Initialize a bimap with the given key-value pair.
+    %
+:- func bimap.singleton(K, V) = bimap(K, V).
+
      % Check whether a bimap is empty.
      %
  :- pred bimap.is_empty(bimap(K, V)::in) is semidet.
@@ -327,6 +331,11 @@
      map.init(Reverse),
      B = bimap(Forward, Reverse).

+bimap.singleton(K, V) = B:-
+    Forward = map.singleton(K, V),
+    Reverse = map.singleton(V, K),
+    B = bimap(Forward, Reverse).
+
  bimap.is_empty(bimap(Forward, _)) :-
      map.is_empty(Forward). % by inference == map.is_empty(Reverse).

Index: library/injection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/injection.m,v
retrieving revision 1.9
diff -u -r1.9 injection.m
--- library/injection.m	3 May 2011 04:35:01 -0000	1.9
+++ library/injection.m	3 May 2011 14:56:56 -0000
@@ -47,6 +47,10 @@
  :- func injection.init = injection(K, V).
  :- pred injection.init(injection(K, V)::out) is det.

+    % Intialise an injection wit the given key-value pair.
+    %
+:- func injection.singleton(K, V) = injection(K, V).
+
      % Check whether an injection is empty.
      %
  :- pred injection.is_empty(injection(K, V)::in) is semidet.
@@ -325,6 +329,10 @@

  injection.init(injection.init).

+injection.singleton(K, V) = injection(F, R) :-
+    F = map.singleton(K, V),
+    R = map.singleton(V, K).
+
  injection.is_empty(injection(F, _)) :-
      map.is_empty(F).

Index: library/map.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.123
diff -u -r1.123 map.m
--- library/map.m	3 May 2011 04:35:01 -0000	1.123
+++ library/map.m	3 May 2011 06:24:34 -0000
@@ -39,6 +39,10 @@
      %
  :- pred map.init(map(_, _)::uo) is det.
  :- func map.init = (map(K, V)::uo) is det.
+ 
+    % Initialize a map containing the given key-value pair.
+    %
+:- func map.singleton(K, V) = map(K, V).

      % Check whether a map is empty.
      %
@@ -714,6 +718,9 @@
  map.init(M) :-
      tree234.init(M).

+map.singleton(K, V) =
+    tree234.singleton(K, V).
+
  map.is_empty(M) :-
      tree234.is_empty(M).

Index: library/rbtree.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/rbtree.m,v
retrieving revision 1.30
diff -u -r1.30 rbtree.m
--- library/rbtree.m	23 Nov 2007 07:35:57 -0000	1.30
+++ library/rbtree.m	3 May 2011 06:27:49 -0000
@@ -55,6 +55,10 @@
  :- func rbtree.init = rbtree(K, V).
  :- pred rbtree.init(rbtree(K, V)::uo) is det.

+    % Initialise an rbtree containing the given key-value pair.
+    %
+:- func rbtree.singleton(K, V) = rbtree(K, V).
+
      % Check whether a tree is empty.
      %
  :- pred rbtree.is_empty(rbtree(K, V)::in) is semidet.
@@ -242,6 +246,8 @@
  rbtree.is_empty(Tree) :-
      Tree = empty.

+rbtree.singleton(K, V) = black(K, V, empty, empty).
+
  %-----------------------------------------------------------------------------%

  % Special conditions that must be satisfied by Red-Black trees.
Index: library/tree234.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/tree234.m,v
retrieving revision 1.70
diff -u -r1.70 tree234.m
--- library/tree234.m	22 Nov 2010 06:02:14 -0000	1.70
+++ library/tree234.m	3 May 2011 06:22:48 -0000
@@ -31,6 +31,8 @@
  :- func tree234.init = tree234(K, V).
  :- pred tree234.init(tree234(K, V)::uo) is det.

+:- func tree234.singleton(K, V) = tree234(K, V).
+
  :- pred tree234.is_empty(tree234(K, V)::in) is semidet.

  :- pred tree234.member(tree234(K, V)::in, K::out, V::out) is nondet.
@@ -470,6 +472,8 @@
  tree234.is_empty(Tree) :-
      Tree = empty.

+tree234.singleton(K, V) = two(K, V, empty, empty).
+
  %------------------------------------------------------------------------------%

  tree234.member(empty, _K, _V) :- fail.
--------------------------------------------------------------------------
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