for review: additions to bag.m

Andrew Bromage bromage at cs.mu.OZ.AU
Thu Mar 5 15:43:21 AEDT 1998


G'day all.

Tom, could you please review this change?  Thanks.

Cheers,
Andrew Bromage


Estimated hours taken: 0.5

library/bag.m:
	Add two predicates to bag.m, bag__remove_list/3 and
	bag__det_remove_list/3.

NEWS:
	Note the above change.


Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.95
diff -u -r1.95 NEWS
--- NEWS	1998/03/03 17:46:48	1.95
+++ NEWS	1998/03/05 04:36:46
@@ -288,8 +288,8 @@
   for details.
 
 * Miscellaneous new predicates: bag__least_upper_bound/3,
-  list__take_upto/3, set__count/2, set_ordlist__count/2,
-  and store__new_cyclic_mutvar/4.
+  bag__remove_list/3, bag__det_remove_list/3, list__take_upto/3,
+  set__count/2, set_ordlist__count/2, and store__new_cyclic_mutvar/4.
 
   See the Mercury Library Reference Manual for details.
 
Index: library/bag.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/bag.m,v
retrieving revision 1.15
diff -u -r1.15 bag.m
--- bag.m	1998/01/23 12:33:07	1.15
+++ bag.m	1998/03/05 04:29:30
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1994-1997 The University of Melbourne.
+% Copyright (C) 1994-1998 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -64,6 +64,25 @@
 :- pred bag__det_remove(bag(T), T, bag(T)).
 :- mode bag__det_remove(in, in, out) is det.
 
+	% Remove a list of values from a bag.  Duplicates are removed
+	% from the bag the appropriate number of times.  Fail if any of
+	% the items in the bag does not exist in the bag.
+	%
+	% This call is logically equivalent to:
+	%
+	%	bag__remove_list(Bag0, RemoveList, Bag) :-
+	%		bag__from_list(RemoveList, RemoveBag),
+	%		bag__is_subbag(RemoveBag, Bag0),
+	%		bag__subtract(Bag0, RemoveBag, Bag).
+:- pred bag__remove_list(bag(T), list(T), bag(T)).
+:- mode bag__remove_list(in, in, out) is semidet.
+
+	% Remove a list of values from a bag.  Duplicates are removed
+	% from the bag the appropriate number of times.  Abort if the
+	% item does not exist in the bag.
+:- pred bag__det_remove_list(bag(T), list(T), bag(T)).
+:- mode bag__det_remove_list(in, in, out) is det.
+
 	% Delete one occurrence of a particular value from a bag.
 	% If the key is not present, leave the bag unchanged.
 :- pred bag__delete(bag(T), T, bag(T)).
@@ -232,8 +251,21 @@
 	( bag__remove(Bag0, Item, Bag1) ->
 		Bag = Bag1
 	;
-		error("bag__det_remove: Missing item in bag."),
-		Bag = Bag0
+		error("bag__det_remove: Missing item in bag.")
+	).
+
+:- bag__remove_list(_, List, _) when List.
+
+bag__remove_list(Bag, [], Bag).
+bag__remove_list(Bag0, [X | Xs], Bag) :-
+	bag__remove(Bag0, X, Bag1),
+	bag__remove_list(Bag1, Xs, Bag).
+
+bag__det_remove_list(Bag0, List, Bag) :-
+	( bag__remove_list(Bag0, List, Bag1) ->
+		Bag = Bag1
+	;
+		error("bag__det_remove_list: Missing item in bag.")
 	).
 
 bag__remove_all(Bag0, Item, Bag) :- 	% semidet



More information about the developers mailing list