[m-rev.] diff: fixes for set types as type class instances
Julien Fischer
juliensf at csse.unimelb.edu.au
Wed Feb 16 02:53:13 AEDT 2011
This second part of this diff addresses one of the issues raised in bug
#185.
Branches: main, 11.01
Fix a couple of issues with some of the set types in the standard library
being used in type class instances.
library/set_unordlist.m:
Define set_unordlists using a notag type rather than an equivalence.
This allows them to be safely used in type class instances.
library/set.m:
Export the definition of the type set/1 in an undocumented interface
section. Not exporting it leads to problems when it used used in
type class instances (since it is defined using an abstract
equivalence). We don't want to use a notag wrapper here since that
would lead to some operations, e.g. power_union/2, being less efficient.
This should be ok; the abstraction barrier is still in place since set/1
is defined to be set_ordlist/1 (which is itself abstract).
tests/invalid/actual_expected.err_exp:
tests/invalid/overloading.err_exp:
Conform to the above changes.
Julien.
Index: library/set.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/set.m,v
retrieving revision 1.89
diff -u -r1.89 set.m
--- library/set.m 13 Nov 2010 16:49:27 -0000 1.89
+++ library/set.m 15 Feb 2011 15:27:55 -0000
@@ -381,8 +381,11 @@
:- interface.
+:- import_module set_ordlist.
:- import_module term. % for var/1.
+:- type set(T) == set_ordlist(T).
+
:- pragma type_spec(set.list_to_set/2, T = var(_)).
:- pragma type_spec(set.list_to_set/1, T = var(_)).
@@ -409,10 +412,6 @@
:- implementation.
-:- import_module set_ordlist.
-
-:- type set(T) == set_ordlist(T).
-
set.list_to_set(List, Set) :-
set_ordlist.list_to_set(List, Set).
Index: library/set_unordlist.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/set_unordlist.m,v
retrieving revision 1.32
diff -u -r1.32 set_unordlist.m
--- library/set_unordlist.m 30 Nov 2010 02:41:53 -0000 1.32
+++ library/set_unordlist.m 15 Feb 2011 15:27:55 -0000
@@ -361,45 +361,46 @@
%--------------------------------------------------------------------------%
-:- type set_unordlist(T) == list(T).
+:- type set_unordlist(T)
+ ---> sul(list(T)).
-set_unordlist.list_to_set(List, List).
+set_unordlist.list_to_set(List, sul(List)).
-set_unordlist.from_list(List) = List.
+set_unordlist.from_list(List) = sul(List).
-set_unordlist.sorted_list_to_set(List, List).
+set_unordlist.sorted_list_to_set(List, sul(List)).
-set_unordlist.from_sorted_list(List) = List.
+set_unordlist.from_sorted_list(List) = sul(List).
-set_unordlist.to_sorted_list(Set, List) :-
+set_unordlist.to_sorted_list(sul(Set), List) :-
list.sort_and_remove_dups(Set, List).
-set_unordlist.insert_list(Set0, List, Set) :-
+set_unordlist.insert_list(sul(Set0), List, sul(Set)) :-
list.append(List, Set0, Set).
-set_unordlist.insert(S0, E, [E | S0]).
+set_unordlist.insert(sul(S0), E, sul([E | S0])).
-set_unordlist.init([]).
+set_unordlist.init(sul([])).
-set_unordlist.singleton_set([X], X).
+set_unordlist.singleton_set(sul([X]), X).
set_unordlist.equal(SetA, SetB) :-
set_unordlist.subset(SetA, SetB),
set_unordlist.subset(SetB, SetA).
-set_unordlist.empty([]).
+set_unordlist.empty(sul([])).
-set_unordlist.non_empty([_ | _]).
+set_unordlist.non_empty(sul([_ | _])).
-set_unordlist.subset([], _).
-set_unordlist.subset([E | S0], S1) :-
+set_unordlist.subset(sul([]), _).
+set_unordlist.subset(sul([E | S0]), S1) :-
set_unordlist.member(E, S1),
- set_unordlist.subset(S0, S1).
+ set_unordlist.subset(sul(S0), S1).
set_unordlist.superset(S0, S1) :-
set_unordlist.subset(S1, S0).
-set_unordlist.member(E, S) :-
+set_unordlist.member(E, sul(S)) :-
list.member(E, S).
set_unordlist.is_member(E, S, R) :-
@@ -417,7 +418,7 @@
set_unordlist.delete(S0, X, S1),
set_unordlist.delete_list(S1, Xs, S).
-set_unordlist.delete(S0, E, S) :-
+set_unordlist.delete(sul(S0), E, sul(S)) :-
list.delete_all(S0, E, S).
set_unordlist.remove_list(S, [], S).
@@ -425,23 +426,23 @@
set_unordlist.remove(S0, X, S1),
set_unordlist.remove_list(S1, Xs, S).
-set_unordlist.remove(S0, E, S) :-
+set_unordlist.remove(sul(S0), E, sul(S)) :-
list.member(E, S0),
- set_unordlist.delete(S0, E, S).
+ set_unordlist.delete(sul(S0), E, sul(S)).
-set_unordlist.remove_least(Set0, E, Set) :-
- Set0 = [_ | _], % fail early on an empty set
+set_unordlist.remove_least(Set0, E, sul(Set)) :-
+ Set0 = sul([_ | _]), % fail early on an empty set
set_unordlist.to_sorted_list(Set0, [E | Set]).
-set_unordlist.union(Set0, Set1, Set) :-
+set_unordlist.union(sul(Set0), sul(Set1), sul(Set)) :-
list.append(Set1, Set0, Set).
set_unordlist.union_list(LS) = S :-
- set_unordlist.power_union(LS, S).
+ set_unordlist.power_union(sul(LS), S).
-set_unordlist.power_union(PS, S) :-
+set_unordlist.power_union(sul(PS), sul(S)) :-
set_unordlist.init(S0),
- set_unordlist.power_union_2(PS, S0, S1),
+ set_unordlist.power_union_2(PS, S0, sul(S1)),
list.sort_and_remove_dups(S1, S).
:- pred set_unordlist.power_union_2(list(set_unordlist(T))::in,
@@ -452,11 +453,11 @@
set_unordlist.union(!.S, T, !:S),
set_unordlist.power_union_2(Ts, !S).
-set_unordlist.intersect(S0, S1, S) :-
+set_unordlist.intersect(sul(S0), sul(S1), sul(S)) :-
set_unordlist.intersect_2(S0, S1, [], S).
-:- pred set_unordlist.intersect_2(set_unordlist(T)::in, set_unordlist(T)::in,
- set_unordlist(T)::in, set_unordlist(T)::out) is det.
+:- pred set_unordlist.intersect_2(list(T)::in, list(T)::in,
+ list(T)::in, list(T)::out) is det.
set_unordlist.intersect_2([], _, S, S).
set_unordlist.intersect_2([E | S0], S1, S2, S) :-
@@ -467,19 +468,19 @@
),
set_unordlist.intersect_2(S0, S1, S3, S).
-set_unordlist.power_intersect([], []).
-set_unordlist.power_intersect([S0 | Ss], S) :-
+set_unordlist.power_intersect(sul([]), sul([])).
+set_unordlist.power_intersect(sul([S0 | Ss]), S) :-
(
Ss = [],
S = S0
;
Ss = [_ | _],
- set_unordlist.power_intersect(Ss, S1),
+ set_unordlist.power_intersect(sul(Ss), S1),
set_unordlist.intersect(S1, S0, S)
).
set_unordlist.intersect_list(Sets) =
- set_unordlist.power_intersect(Sets).
+ set_unordlist.power_intersect(sul(Sets)).
%--------------------------------------------------------------------------%
@@ -489,17 +490,17 @@
:- pred set_unordlist.difference_2(set_unordlist(T)::in, set_unordlist(T)::in,
set_unordlist(T)::out) is det.
-set_unordlist.difference_2([], C, C).
-set_unordlist.difference_2([E | Es], A, C) :-
+set_unordlist.difference_2(sul([]), C, C).
+set_unordlist.difference_2(sul([E | Es]), A, C) :-
set_unordlist.delete(A, E, B),
- set_unordlist.difference_2(Es, B, C).
+ set_unordlist.difference_2(sul(Es), B, C).
%-----------------------------------------------------------------------------%
set_unordlist.count(Set) = Count :-
set_unordlist.count(Set, Count).
-set_unordlist.count(Set, Count) :-
+set_unordlist.count(sul(Set), Count) :-
list.remove_dups(Set, Elems),
list.length(Elems, Count).
@@ -581,13 +582,13 @@
S2 = set_unordlist.list_to_set(list.filter_map(PF,
set_unordlist.to_sorted_list(S1))).
-set_unordlist.divide(Pred, Set, RevTruePart, RevFalsePart) :-
+set_unordlist.divide(Pred, sul(Set), sul(RevTruePart), sul(RevFalsePart)) :-
set_unordlist.divide_2(Pred, Set, [], RevTruePart, [], RevFalsePart).
:- pred set_unordlist.divide_2(pred(T1)::in(pred(in) is semidet),
- set_unordlist(T1)::in,
- set_unordlist(T1)::in, set_unordlist(T1)::out,
- set_unordlist(T1)::in, set_unordlist(T1)::out) is det.
+ list(T1)::in,
+ list(T1)::in, list(T1)::out,
+ list(T1)::in, list(T1)::out) is det.
set_unordlist.divide_2(_Pred, [], !RevTrue, !RevFalse).
set_unordlist.divide_2(Pred, [H | T], !RevTrue, !RevFalse) :-
Index: tests/invalid/actual_expected.err_exp
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/invalid/actual_expected.err_exp,v
retrieving revision 1.2
diff -u -r1.2 actual_expected.err_exp
--- tests/invalid/actual_expected.err_exp 16 Apr 2009 02:09:08 -0000 1.2
+++ tests/invalid/actual_expected.err_exp 15 Feb 2011 15:27:55 -0000
@@ -3,8 +3,8 @@
actual_expected.m:106: `find_compulsory_lvals'/5:
actual_expected.m:106: type error: variable `LiveMap' has type
actual_expected.m:106: `tree234.tree234(actual_expected.label,
-actual_expected.m:106: set.set(actual_expected.lval))',
+actual_expected.m:106: set_ordlist.set_ordlist(actual_expected.lval))',
actual_expected.m:106: expected type was
actual_expected.m:106: `maybe.maybe(tree234.tree234(actual_expected.label,
-actual_expected.m:106: set.set(actual_expected.lval)))'.
+actual_expected.m:106: set_ordlist.set_ordlist(actual_expected.lval)))'.
For more information, recompile with `-E'.
Index: tests/invalid/overloading.err_exp
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/invalid/overloading.err_exp,v
retrieving revision 1.9
diff -u -r1.9 overloading.err_exp
--- tests/invalid/overloading.err_exp 16 Apr 2009 02:09:08 -0000 1.9
+++ tests/invalid/overloading.err_exp 15 Feb 2011 15:27:55 -0000
@@ -5,8 +5,8 @@
overloading.m:050: type error in argument(s) of functor `opt_info/9'.
overloading.m:050: variable `OptInfo' has type `overloading.opt_info',
overloading.m:050: functor `opt_info/9' has type
-overloading.m:050: `opt_info(set.set(term.var(term.generic)),
-overloading.m:050: set.set(term.var(term.generic)),
+overloading.m:050: `opt_info(set_ordlist.set_ordlist(term.var(term.generic)),
+overloading.m:050: set_ordlist.set_ordlist(term.var(term.generic)),
overloading.m:050: tree234.tree234(term.var(term.generic),
overloading.m:050: tree234.tree234(overloading.goal_path, string)),
overloading.m:050: overloading.interval_id, counter.counter,
@@ -15,14 +15,14 @@
overloading.m:050: tree234.tree234(overloading.interval_id,
overloading.m:050: overloading.anchor),
overloading.m:050: tree234.tree234(overloading.interval_id,
-overloading.m:050: set.set(term.var(term.generic))),
+overloading.m:050: set_ordlist.set_ordlist(term.var(term.generic))),
overloading.m:050: tree234.tree234(overloading.interval_id,
overloading.m:050: list.list(overloading.interval_id))):
overloading.m:050: overloading.opt_info',
overloading.m:050: variable `MustHaveOwnSlot' has type
-overloading.m:050: `set.set(term.var(term.generic))',
+overloading.m:050: `set_ordlist.set_ordlist(term.var(term.generic))',
overloading.m:050: variable `EverOnStack' has type
-overloading.m:050: `set.set(term.var(term.generic))',
+overloading.m:050: `set_ordlist.set_ordlist(term.var(term.generic))',
overloading.m:050: variable `CurIntervalId' has type
overloading.m:050: `overloading.interval_id',
overloading.m:050: argument has type `'<any>'',
@@ -39,9 +39,9 @@
overloading.m:050: },
overloading.m:050: variable `VarsMap0' has overloaded type {
overloading.m:050: (pred tree234.tree234(overloading.interval_id,
-overloading.m:050: set.set(term.var(term.generic)))),
+overloading.m:050: set_ordlist.set_ordlist(term.var(term.generic)))),
overloading.m:050: tree234.tree234(overloading.interval_id,
-overloading.m:050: set.set(term.var(term.generic)))
+overloading.m:050: set_ordlist.set_ordlist(term.var(term.generic)))
overloading.m:050: },
overloading.m:050: variable `SuccMap0' has overloaded type {
overloading.m:050: (pred tree234.tree234(V_V_1, V_V_2)),
--------------------------------------------------------------------------
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