[m-rev.] for post-commit review: using set_of_var for nonlocals

Zoltan Somogyi zs at csse.unimelb.edu.au
Tue Aug 16 13:25:33 AEST 2011


The diff is big but very boring. I would appreciate reviews, even partial
ones: since almost all the changes are extremely local, you do NOT need
any kind overview beyond the log message to be able to check the correctness
of each block in the diff.

I am committing this now because the size of the diff means that it has
a high chance of conflicting with other changes. I did test it in several
grades.

Zoltan.

Switch from using set(prog_var), which is represented using set_ordlist,
to set_of_progvar, which is represented using tree_bitset, for most sets
of variables in the compiler, including the nonlocals sets in goal_infos.

This diff yields about a 5% speedup when compiling the training_cars_full.m
stress test, but also about a 1% slowdown on tools/speedtest. Both of these
are with the current default state in which tree_bitset is compiled with
a whole bunch of sanity checks. If these are disabled, we get roughly a 1%
speedup on tools/speedtest. I intend to disable those sanity checks after
a shakedown period of a week or two in which the updated version of the
compiler is installed on our platforms.

compiler/hlds_goal.m:
	Replace almost all occurrences of set(prog_var) with set_of_progvar.
	The main exceptions are the types supporting rbmm.

compiler/set_of_var.m:
	Add some more predicates and functions that previous existed on sets
	but not yet on set_of_vars.

compiler/*.m:
	Conform to the change in hlds_goal.m, and make similar changes
	in set representations.

library/bag.m:
	Add a predicate and function for creating a bag from a sorted list.
	We already had them for creating a bag from a set, but a set_of_progvar
	shouldn't have to be converted to a set.

library/robdd.m:
	Fix deviations from our programming style.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/extra
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/extra
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/libatomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/doc
cvs diff: Diffing boehm_gc/libatomic_ops/src
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/armcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/gcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/hpc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/ibmc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/icc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/msftc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/sunc
cvs diff: Diffing boehm_gc/libatomic_ops/tests
cvs diff: Diffing boehm_gc/libatomic_ops-1.2
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/doc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/tests
cvs diff: Diffing boehm_gc/m4
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/accumulator.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/accumulator.m,v
retrieving revision 1.85
diff -u -b -r1.85 accumulator.m
--- compiler/accumulator.m	6 May 2011 05:03:18 -0000	1.85
+++ compiler/accumulator.m	15 Aug 2011 12:43:08 -0000
@@ -181,6 +181,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.goal_store.
 
 :- import_module assoc_list.
@@ -638,7 +639,7 @@
 
         Member = (pred(M::in) is semidet :-
             M = HeadVar - _,
-            set.member(HeadVar, ChangedVars)
+            set_of_var.member(ChangedVars, HeadVar)
         ),
         list.filter(Member, HeadArg0, HeadArg),
         list.map(fst, HeadArg, Out),
@@ -960,7 +961,7 @@
 
 :- type accu_assoc
     --->    accu_assoc(
-                set(prog_var),      % the associative input args
+                set_of_progvar,     % the associative input args
                 prog_var,           % the corresponding output arg
                 bool                % is the predicate commutative?
             ).
@@ -995,7 +996,7 @@
     % is descended from which.
     %
 :- pred associativity_assertion(module_info::in, list(assert_id)::in,
-    prog_vars::in, set(prog_var)::out, prog_var::out) is semidet.
+    prog_vars::in, set_of_progvar::out, prog_var::out) is semidet.
 
 associativity_assertion(ModuleInfo, [AssertId | AssertIds], Args0, VarAB,
         OutputVar) :-
@@ -1004,7 +1005,7 @@
             Args0, VarA - VarB, OutputVar0)
     ->
         \+ associativity_assertion(ModuleInfo, AssertIds, Args0, _, _),
-        VarAB = set.list_to_set([VarA, VarB]),
+        VarAB = set_of_var.list_to_set([VarA, VarB]),
         OutputVar = OutputVar0
     ;
         associativity_assertion(ModuleInfo, AssertIds, Args0, VarAB, OutputVar)
@@ -1018,7 +1019,7 @@
     % is descended from which.
     %
 :- pred commutativity_assertion(module_info::in,list(assert_id)::in,
-    prog_vars::in, set(prog_var)::out) is semidet.
+    prog_vars::in, set_of_progvar::out) is semidet.
 
 commutativity_assertion(ModuleInfo, [AssertId | AssertIds], Args0,
         PossibleStaticVars) :-
@@ -1027,7 +1028,7 @@
             Args0, StaticVarA - StaticVarB)
     ->
         \+ commutativity_assertion(ModuleInfo, AssertIds, Args0, _),
-        PossibleStaticVars = set.list_to_set([StaticVarA, StaticVarB])
+        PossibleStaticVars = set_of_var.list_to_set([StaticVarA, StaticVarB])
     ;
         commutativity_assertion(ModuleInfo, AssertIds, Args0,
             PossibleStaticVars)
@@ -1127,19 +1128,21 @@
         goal_store_lookup(GoalStore, Id, stored_goal(Goal, _InstMap)),
         Goal = hlds_goal(_GoalExpr, GoalInfo),
         NonLocals = goal_info_get_nonlocals(GoalInfo),
-        Set = NonLocals `union` Set0
+        set_of_var.union(NonLocals, Set0, Set)
     ),
-    list.foldl(P, set.to_sorted_list(Before), set.init, BeforeNonLocals),
-    list.foldl(P, set.to_sorted_list(After), set.init, AfterNonLocals),
-    InitAccs = BeforeNonLocals `intersect` AfterNonLocals,
+    list.foldl(P, set.to_sorted_list(Before),
+        set_of_var.init, BeforeNonLocals),
+    list.foldl(P, set.to_sorted_list(After),
+        set_of_var.init, AfterNonLocals),
+    InitAccs = set_of_var.intersect(BeforeNonLocals, AfterNonLocals),
 
     proc_info_get_varset(ProcInfo0, !:VarSet),
     proc_info_get_vartypes(ProcInfo0, !:VarTypes),
 
-    accu_substs_init(set.to_sorted_list(InitAccs), !VarSet, !VarTypes,
+    accu_substs_init(set_of_var.to_sorted_list(InitAccs), !VarSet, !VarTypes,
         !:Substs),
 
-    set.list_to_set(OutPrime, OutPrimeSet),
+    set_of_var.list_to_set(OutPrime, OutPrimeSet),
     accu_process_assoc_set(ModuleInfo, GoalStore, set.to_sorted_list(Assoc),
         OutPrimeSet, !Substs, !VarSet, !VarTypes, CS, Warnings),
 
@@ -1147,7 +1150,7 @@
         OutPrimeSet, !Substs, !VarSet, !VarTypes, UpdateOut, UpdateAccOut,
         BasePairs),
 
-    Accs = set.to_sorted_list(InitAccs) ++ UpdateAccOut,
+    Accs = set_of_var.to_sorted_list(InitAccs) ++ UpdateAccOut,
 
     accu_divide_base_case(ModuleInfo, !.VarTypes, GoalStore, UpdateOut, Out,
         UpdateBase, AssocBase, OtherBase),
@@ -1201,7 +1204,7 @@
     % by reordering the arguments to a call.
     %
 :- pred accu_process_assoc_set(module_info::in, accu_goal_store::in,
-    list(accu_goal_id)::in, set(prog_var)::in,
+    list(accu_goal_id)::in, set_of_progvar::in,
     accu_substs::in, accu_substs::out,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
     accu_goal_store::out, accu_warnings::out) is semidet.
@@ -1219,8 +1222,9 @@
     Goal = hlds_goal(plain_call(PredId, _, Args, _, _, _), GoalInfo),
     accu_is_associative(ModuleInfo, PredId, Args, AssocInfo),
     AssocInfo = accu_assoc(Vars, AssocOutput, IsCommutative),
-    set.singleton_set(Vars `intersect` OutPrime, DuringAssocVar),
-    set.singleton_set(Vars `difference` (Vars `intersect` OutPrime),
+    OutPrimeVars = set_of_var.intersect(Vars, OutPrime),
+    set_of_var.is_singleton(OutPrimeVars, DuringAssocVar),
+    set_of_var.is_singleton(set_of_var.difference(Vars, OutPrimeVars),
         BeforeAssocVar),
 
     map.lookup(AccVarSubst, BeforeAssocVar, AccVar),
@@ -1254,7 +1258,7 @@
             % of the predicate.
             accu_heuristic(ModuleName, PredName, Arity, Args,
                 PossibleDuringAssocVars),
-            set.member(DuringAssocVar, PossibleDuringAssocVars),
+            set_of_var.member(PossibleDuringAssocVars, DuringAssocVar), 
             CurWarnings = []
         ;
             ProgContext = goal_info_get_context(GoalInfo),
@@ -1262,7 +1266,7 @@
                 DuringAssocVar)]
         ),
         % Swap the arguments.
-        [A, B] = set.to_sorted_list(Vars),
+        [A, B] = set_of_var.to_sorted_list(Vars),
         map.from_assoc_list([A - B, B - A], Subst),
         rename_some_vars_in_goal(Subst, Goal, SwappedGoal),
         CSGoal = stored_goal(SwappedGoal, InstMap)
@@ -1281,11 +1285,11 @@
     % in the running time of the predicate.
     %
 :- pred accu_heuristic(module_name::in, string::in, arity::in, prog_vars::in,
-    set(prog_var)::out) is semidet.
+    set_of_progvar::out) is semidet.
 
 accu_heuristic(unqualified("list"), "append", 3, [_Typeinfo, A, _B, _C],
         Set) :-
-    set.list_to_set([A], Set).
+    set_of_var.make_singleton(A, Set).
 
 %-----------------------------------------------------------------------------%
 
@@ -1295,7 +1299,7 @@
     % to get the result from.
     %
 :- pred accu_process_update_set(module_info::in, accu_goal_store::in,
-    list(accu_goal_id)::in, set(prog_var)::in,
+    list(accu_goal_id)::in, set_of_progvar::in,
     accu_substs::in, accu_substs::out,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
     prog_vars::out, prog_vars::out, list(pair(prog_var))::out) is semidet.
@@ -1311,7 +1315,7 @@
     Goal = hlds_goal(plain_call(PredId, _, Args, _, _, _), _GoalInfo),
     accu_is_update(ModuleInfo, PredId, Args, StateVarA - StateVarB),
 
-    ( set.member(StateVarA, OutPrime) ->
+    ( set_of_var.member(OutPrime, StateVarA) ->
         StateInputVar = StateVarA,
         StateOutputVar = StateVarB
     ;
@@ -1399,7 +1403,7 @@
             apply_instmap_delta(InstMap0, InstMapDelta, InstMap),
             instmap_changed_vars(InstMap0, InstMap, VarTypes,
                 ModuleInfo, ChangedVars),
-            set.singleton_set(ChangedVars, Var)
+            set_of_var.is_singleton(ChangedVars, Var)
         ), Ids),
     (
         Ids = [],
@@ -1724,7 +1728,7 @@
     UniMode = LHSMode - RHSMode,
     Context = unify_context(umc_explicit, []),
     Expr = unify(Out, rhs_var(Acc), UniMode, assign(Out,Acc), Context),
-    set.list_to_set([Out, Acc], NonLocalVars),
+    set_of_var.list_to_set([Out, Acc], NonLocalVars),
     InstMapDelta = instmap_delta_bind_var(Out),
     goal_info_init(NonLocalVars, InstMapDelta, detism_det, purity_pure, Info),
     Goal = hlds_goal(Expr, Info).
Index: compiler/add_class.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_class.m,v
retrieving revision 1.38
diff -u -b -r1.38 add_class.m
--- compiler/add_class.m	11 Aug 2011 06:38:13 -0000	1.38
+++ compiler/add_class.m	15 Aug 2011 07:57:31 -0000
@@ -63,6 +63,7 @@
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module int.
@@ -606,7 +607,7 @@
         % First the goal info, ...
         goal_info_init(GoalInfo0),
         goal_info_set_context(Context, GoalInfo0, GoalInfo1),
-        set.list_to_set(HeadVars, NonLocals),
+        set_of_var.list_to_set(HeadVars, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo1, GoalInfo2),
         ( check_marker(Markers, marker_is_impure) ->
             goal_info_set_purity(purity_impure, GoalInfo2, GoalInfo)
Index: compiler/add_pragma.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pragma.m,v
retrieving revision 1.121
diff -u -b -r1.121 add_pragma.m
--- compiler/add_pragma.m	11 Aug 2011 06:38:13 -0000	1.121
+++ compiler/add_pragma.m	15 Aug 2011 07:58:12 -0000
@@ -159,6 +159,7 @@
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 :- import_module recompilation.
 :- import_module transform_hlds.term_constr_main.
 :- import_module transform_hlds.term_constr_util.
@@ -1484,7 +1485,7 @@
             % map.from_corresponding_lists(Args, Types, VarTypes0)
             map.init(VarTypes0),
             goal_info_init(GoalInfo0),
-            set.list_to_set(Args, NonLocals),
+            set_of_var.list_to_set(Args, NonLocals),
             goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
             goal_info_set_context(Context, GoalInfo1, GoalInfo),
 
Index: compiler/add_pred.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pred.m,v
retrieving revision 1.47
diff -u -b -r1.47 add_pred.m
--- compiler/add_pred.m	23 May 2011 05:08:00 -0000	1.47
+++ compiler/add_pred.m	15 Aug 2011 08:13:23 -0000
@@ -86,6 +86,7 @@
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module map.
 :- import_module set.
@@ -245,7 +246,7 @@
     clauses_info_get_headvar_list(ClausesInfo0, HeadVarList),
 
     goal_info_init(Context, GoalInfo0),
-    NonLocals = proc_arg_vector_to_set(HeadVars),
+    NonLocals = set_of_var.list_to_set(proc_arg_vector_to_list(HeadVars)),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
     (
         Module = mercury_private_builtin_module,
@@ -289,13 +290,14 @@
         UnifyMode = ((Free -> Ground) - (Ground -> Ground)),
         UnifyContext = unify_context(umc_explicit, []),
         AssignExpr = unify(LHS, RHS, UnifyMode, Unification, UnifyContext),
-        goal_info_set_nonlocals(set.make_singleton_set(ZeroVar),
+        goal_info_set_nonlocals(set_of_var.make_singleton(ZeroVar),
             GoalInfo0, GoalInfoWithZero),
         AssignGoal = hlds_goal(AssignExpr, GoalInfoWithZero),
 
         CastExpr = generic_call(cast(unsafe_type_inst_cast),
             [ZeroVar] ++ HeadVarList, [in_mode, uo_mode], detism_det),
-        goal_info_set_nonlocals(set.list_to_set([ZeroVar] ++ HeadVarList),
+        goal_info_set_nonlocals(
+            set_of_var.list_to_set([ZeroVar | HeadVarList]),
             GoalInfo0, GoalInfoWithZeroHeadVars),
         CastGoal = hlds_goal(CastExpr, GoalInfoWithZeroHeadVars),
 
Index: compiler/assertion.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/assertion.m,v
retrieving revision 1.71
diff -u -b -r1.71 assertion.m
--- compiler/assertion.m	6 May 2011 05:03:18 -0000	1.71
+++ compiler/assertion.m	15 Aug 2011 06:35:08 -0000
@@ -24,6 +24,7 @@
 :- import_module hlds.hlds_pred.
 :- import_module parse_tree.prog_data.
 
+:- import_module list.
 :- import_module pair.
 
 %-----------------------------------------------------------------------------%
@@ -55,7 +56,7 @@
     % identical locations on both sides of the equivalence).
     %
 :- pred is_commutativity_assertion(module_info::in, assert_id::in,
-    prog_vars::in, pair(prog_var)::out) is semidet.
+    list(prog_var)::in, pair(prog_var)::out) is semidet.
 
     % is_associativity_assertion(MI, Id, Vs, CVs, OV):
     %
@@ -84,7 +85,7 @@
     % identical locations on both sides of the equivalence).
     %
 :- pred is_associativity_assertion(module_info::in, assert_id::in,
-    prog_vars::in, pair(prog_var)::out, prog_var::out) is semidet.
+    list(prog_var)::in, pair(prog_var)::out, prog_var::out) is semidet.
 
     % is_update_assertion(MI, Id, PId, Ss):
     %
@@ -107,7 +108,7 @@
     % the pair of variables which are state variables, SPair.
     %
 :- pred is_update_assertion(module_info::in, assert_id::in,
-    pred_id::in, prog_vars::in, pair(prog_var)::out) is semidet.
+    pred_id::in, list(prog_var)::in, pair(prog_var)::out) is semidet.
 
     % is_construction_equivalence_assertion(MI, Id, C, P):
     %
@@ -136,9 +137,9 @@
 :- import_module hlds.hlds_clauses.
 :- import_module mdbcomp.
 :- import_module mdbcomp.prim_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
-:- import_module list.
 :- import_module map.
 :- import_module maybe.
 :- import_module require.
@@ -165,8 +166,8 @@
     % It also takes a list of variables, Vs, to a call and returns
     % the two variables in that list that can be swapped, ie [A,B].
     %
-:- pred commutative_var_ordering(prog_vars::in, prog_vars::in,
-    prog_vars::in, pair(prog_var)::out) is semidet.
+:- pred commutative_var_ordering(list(prog_var)::in, list(prog_var)::in,
+    list(prog_var)::in, pair(prog_var)::out) is semidet.
 
 commutative_var_ordering([P | Ps], [Q | Qs], [V | Vs], CommutativeVars) :-
     ( P = Q ->
@@ -176,8 +177,9 @@
         CommutativeVars = V - CallVarB
     ).
 
-:- pred commutative_var_ordering_2(prog_var::in, prog_var::in, prog_vars::in,
-    prog_vars::in, prog_vars::in, prog_var::out) is semidet.
+:- pred commutative_var_ordering_2(prog_var::in, prog_var::in,
+    list(prog_var)::in, list(prog_var)::in, list(prog_var)::in,
+    prog_var::out) is semidet.
 
 commutative_var_ordering_2(VarP, VarQ, [P | Ps], [Q | Qs], [V | Vs],
         CallVarB) :-
@@ -228,7 +230,7 @@
     %   compose(AB, C, ABC)     <=> compose(A, BC, ABC)
     %
 :- pred associative(hlds_goals::in, hlds_goals::in,
-    set(prog_var)::in, prog_vars::in,
+    set_of_progvar::in, list(prog_var)::in,
     pair(pair(prog_var), prog_var)::out) is cc_nondet.
 
 associative(PCalls, QCalls, UniversiallyQuantifiedVars, CallVars,
@@ -281,8 +283,8 @@
     % i.e. for app(TypeInfo, X, Y, XY), app(TypeInfo, XY, Z, XYZ)
     % L <= XY and Ps <= [X - XY, Y - Z, XY - XYZ]
     %
-:- pred process_one_side(hlds_goals::in, set(prog_var)::in, pred_id::out,
-    prog_var::out, assoc_list(prog_var)::out, prog_vars::out) is semidet.
+:- pred process_one_side(hlds_goals::in, set_of_progvar::in, pred_id::out,
+    prog_var::out, assoc_list(prog_var)::out, list(prog_var)::out) is semidet.
 
 process_one_side(Goals, UniversiallyQuantifiedVars, PredId,
         LinkingVar, Vars, VarsA) :-
@@ -325,8 +327,8 @@
     %   compose(S0, A, SA),     compose(SB, A, S),
     %   compose(SA, B, S)   <=> compose(S0, B, SB)
     %
-:- pred update(hlds_goals::in, hlds_goals::in, set(prog_var)::in,
-    prog_vars::in, pair(prog_var)::out) is nondet.
+:- pred update(hlds_goals::in, hlds_goals::in, set_of_progvar::in,
+    list(prog_var)::in, pair(prog_var)::out) is nondet.
 
 update(PCalls, QCalls, UniversiallyQuantifiedVars, CallVars,
         StateA - StateB) :-
@@ -364,9 +366,9 @@
     % corresponding variable in the second call, and VAs are the
     % variables of the first call.
     %
-:- pred process_two_linked_calls(hlds_goals::in, set(prog_var)::in,
-    pred_id::out, prog_var::out, assoc_list(prog_var)::out, prog_vars::out)
-    is semidet.
+:- pred process_two_linked_calls(hlds_goals::in, set_of_progvar::in,
+    pred_id::out, prog_var::out, assoc_list(prog_var)::out,
+    list(prog_var)::out) is semidet.
 
 process_two_linked_calls(Goals, UniversiallyQuantifiedVars, PredId,
         LinkingVar, Vars, VarsA) :-
@@ -376,7 +378,8 @@
     % Determine the linking variable, L. By definition it must be
     % existentially quantified and member of both variable lists.
     CommonVars = list_to_set(VarsA) `intersect` list_to_set(VarsB),
-    set.singleton_set(CommonVars `difference` UniversiallyQuantifiedVars,
+    set_of_var.is_singleton(
+        set_of_var.difference(CommonVars, UniversiallyQuantifiedVars),
         LinkingVar),
 
     % Set up mapping between the variables in the two calls.
@@ -591,8 +594,8 @@
         map.insert(VA, VB, !Subst)
     ).
 
-:- pred equal_vars(prog_vars::in, prog_vars::in, subst::in, subst::out)
-    is semidet.
+:- pred equal_vars(list(prog_var)::in, list(prog_var)::in,
+    subst::in, subst::out) is semidet.
 
 equal_vars([], [], !Subst).
 equal_vars([VA | VAs], [VB | VBs], !Subst) :-
Index: compiler/build_mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/build_mode_constraints.m,v
retrieving revision 1.44
diff -u -b -r1.44 build_mode_constraints.m
--- compiler/build_mode_constraints.m	23 May 2011 05:08:00 -0000	1.44
+++ compiler/build_mode_constraints.m	15 Aug 2011 09:03:29 -0000
@@ -28,10 +28,10 @@
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bimap.
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -71,7 +71,7 @@
 
     % Just a conveniently descriptive name.
     %
-:- type nonlocals == set(prog_var).
+:- type nonlocals == set_of_progvar.
 
     % In order to uniquely distinguish a prog_var that may not be
     % unique amongst predicates, this data structure is used to specify
@@ -279,7 +279,7 @@
     Nonlocals = goal_info_get_nonlocals(GoalInfo),
     GoalId = goal_info_get_goal_id(GoalInfo),
 
-    set.to_sorted_list(Nonlocals, NlsList),
+    set_of_var.to_sorted_list(Nonlocals, NlsList),
     prog_vars_at_path(ProgVarset, PredId, NlsList, GoalId, _, !VarInfo),
 
     % Switch on GoalExpr for recursion
@@ -360,7 +360,7 @@
         % Temporarily form the disjunction implied by the goal path
         % annotations.
         MainGoal = disj(Goals),
-        Nonlocals = proc_arg_vector_to_set(HeadVars),
+        Nonlocals = set_of_var.list_to_set(proc_arg_vector_to_list(HeadVars)),
         add_goal_expr_constraints(ModuleInfo, ProgVarset, PredId, MainGoal,
             Context, goal_id(0), Nonlocals, !VarInfo, !Constraints)
     ).
@@ -546,12 +546,10 @@
         Goal = hlds_goal(_, NegatedGoalInfo),
         NegatedGoalId = goal_info_get_goal_id(NegatedGoalInfo),
         VarMap = rep_var_map(!.VarInfo),
-        NonlocalsAtId = set.fold(
-            cons_prog_var_at_path(VarMap, PredId, GoalId),
-            Nonlocals, []),
-        NonlocalsConstraintVars = set.fold(
-            cons_prog_var_at_path(VarMap, PredId, NegatedGoalId),
-            Nonlocals, NonlocalsAtId),
+        set_of_var.fold(cons_prog_var_at_path(VarMap, PredId, GoalId),
+            Nonlocals, [], NonlocalsAtId),
+        set_of_var.fold(cons_prog_var_at_path(VarMap, PredId, NegatedGoalId),
+            Nonlocals, NonlocalsAtId, NonlocalsConstraintVars),
 
         add_goal_constraints(ModuleInfo, ProgVarset, PredId, Goal, !VarInfo,
             !Constraints),
@@ -570,7 +568,7 @@
         % statement, it is produced at the main goal as well
         % - here we pair up equivalent mode constraint vars and
         % then constrain them to reflect this.
-        NonlocalsList = set.to_sorted_list(Nonlocals),
+        NonlocalsList = set_of_var.to_sorted_list(Nonlocals),
         prog_vars_at_path(ProgVarset, PredId, NonlocalsList, GoalId,
             NonlocalsHere, !VarInfo),
         prog_vars_at_path(ProgVarset, PredId, NonlocalsList, SomeGoalId,
@@ -600,16 +598,17 @@
             NonlocalsAtThen, !VarInfo),
         prog_vars_at_path(ProgVarset, PredId, NonlocalsList, ElseId,
             NonlocalsAtElse, !VarInfo),
-        NonlocalsList = set.to_sorted_list(Nonlocals),
+        NonlocalsList = set_of_var.to_sorted_list(Nonlocals),
 
         % The existentially quantified variables shared between the condition
         % and the then-part have special constraints.
 
         CondNonlocals = goal_info_get_nonlocals(CondInfo),
         ThenNonlocals = goal_info_get_nonlocals(ThenInfo),
-        list.filter(set.contains(CondNonlocals), ExistVars, NonlocalToCond),
-        list.filter(set.contains(ThenNonlocals), NonlocalToCond,
-            LocalAndShared),
+        list.filter(set_of_var.contains(CondNonlocals),
+            ExistVars, NonlocalToCond),
+        list.filter(set_of_var.contains(ThenNonlocals),
+            NonlocalToCond, LocalAndShared),
         prog_vars_at_path(ProgVarset, PredId, LocalAndShared, CondId,
             LocalAndSharedAtCond, !VarInfo),
         prog_vars_at_path(ProgVarset, PredId, LocalAndShared, ThenId,
@@ -859,20 +858,22 @@
 
     % These are variables nonlocal to the conjunction that
     % appear in this particular conjunct.
-    Nonlocal = set.intersect(SubGoalNonlocals, Nonlocals),
+    Nonlocal = set_of_var.intersect(SubGoalNonlocals, Nonlocals),
 
     % These are variables local to the conjunction that
     % are non-local to this particular conjunct.
-    Local = set.difference(SubGoalNonlocals, Nonlocals),
+    Local = set_of_var.difference(SubGoalNonlocals, Nonlocals),
 
     some [!LocalsMap, !NonlocalsMap] (
         !:LocalsMap = !.ConjConstraintsInfo ^ locals_positions,
         !:NonlocalsMap = !.ConjConstraintsInfo ^ nonlocals_positions,
 
-        set.fold(add_variable_to_conjunct_production_map(VarMap, PredId,
-            SubGoalId), Local, !LocalsMap),
-        set.fold(add_variable_to_conjunct_production_map(VarMap, PredId,
-            SubGoalId), Nonlocal, !NonlocalsMap),
+        set_of_var.fold(
+            add_variable_to_conjunct_production_map(VarMap, PredId, SubGoalId),
+            Local, !LocalsMap),
+        set_of_var.fold(
+            add_variable_to_conjunct_production_map(VarMap, PredId, SubGoalId),
+            Nonlocal, !NonlocalsMap),
 
         !ConjConstraintsInfo ^ locals_positions := !.LocalsMap,
         !ConjConstraintsInfo ^ nonlocals_positions := !.NonlocalsMap
@@ -1048,11 +1049,12 @@
     % Retrieves the mode constraint var as per prog_var_at_path, but
     % attaches it to the list supplied rather than return it directly.
     %
-:- func cons_prog_var_at_path(mc_var_map, pred_id, goal_id, prog_var,
-    list(mc_var)) = list(mc_var).
+:- pred cons_prog_var_at_path(mc_var_map::in, pred_id::in, goal_id::in,
+    prog_var::in, list(mc_var)::in, list(mc_var)::out) is det.
 
-cons_prog_var_at_path(VarMap, PredId, GoalId, ProgVar, MCVars) =
-    [lookup_prog_var_at_path(VarMap, PredId, GoalId, ProgVar) | MCVars].
+cons_prog_var_at_path(VarMap, PredId, GoalId, ProgVar, MCVars0, MCVars) :-
+    MCVar = lookup_prog_var_at_path(VarMap, PredId, GoalId, ProgVar),
+    MCVars = [MCVar | MCVars0].
 
     % prog_var_at_paths(VarMap, GoalIds, ProgVar) = ConstraintVars
     % consults the map to form a list of the constraint variable
@@ -1095,7 +1097,7 @@
             prog_var_at_paths(ProgVarset, PredId, Nl, SubIds, NlAtSubIds,
                 !VInfo)
         ), NonlocalsList, NonlocalsAtSubIds, !VarInfo),
-    NonlocalsList = set.to_sorted_list(Nonlocals).
+    NonlocalsList = set_of_var.to_sorted_list(Nonlocals).
 
 %----------------------------------------------------------------------------%
 
Index: compiler/call_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/call_gen.m,v
retrieving revision 1.203
diff -u -b -r1.203 call_gen.m
--- compiler/call_gen.m	21 Jul 2011 06:58:24 -0000	1.203
+++ compiler/call_gen.m	15 Aug 2011 15:31:44 -0000
@@ -760,7 +760,7 @@
 
 %---------------------------------------------------------------------------%
 
-:- pred generate_call_vn_livevals(list(arg_loc)::in, set(prog_var)::in,
+:- pred generate_call_vn_livevals(list(arg_loc)::in, set_of_progvar::in,
     llds_code::out, code_info::in, code_info::out) is det.
 
 generate_call_vn_livevals(InputArgLocs, OutputArgs, Code, !CI) :-
Index: compiler/clause_to_proc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/clause_to_proc.m,v
retrieving revision 1.90
diff -u -b -r1.90 clause_to_proc.m
--- compiler/clause_to_proc.m	23 May 2011 05:08:00 -0000	1.90
+++ compiler/clause_to_proc.m	4 Aug 2011 02:22:59 -0000
@@ -79,13 +79,13 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type_subst.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module int.
 :- import_module map.
 :- import_module pair.
 :- import_module require.
-:- import_module set.
 :- import_module string.
 :- import_module term.
 :- import_module varset.
@@ -243,7 +243,8 @@
 
         % The non-local vars are just the head variables.
 
-        NonLocalVars = proc_arg_vector_to_set(HeadVars),
+        NonLocalVars =
+            set_of_var.list_to_set(proc_arg_vector_to_list(HeadVars)),
         goal_info_set_nonlocals(NonLocalVars, GoalInfo1, GoalInfo2),
 
         % The disjunction is impure/semipure if any of the disjuncts
@@ -409,7 +410,7 @@
     goal_to_conj_list(Body0, Goals0),
     Goals = Goals0 ++ ExistsCastHeadGoals ++ ExistsCastExtraGoals,
     HeadVars = ExtraHeadVars ++ OrigHeadVars,
-    set.list_to_set(HeadVars, NonLocals),
+    NonLocals = set_of_var.list_to_set(HeadVars),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
     Body = hlds_goal(conj(plain_conj, Goals), GoalInfo),
     proc_info_set_body(VarSet, VarTypes, HeadVars, Body, RttiVarMaps,
Index: compiler/closure_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/closure_analysis.m,v
retrieving revision 1.24
diff -u -b -r1.24 closure_analysis.m
--- compiler/closure_analysis.m	23 May 2011 05:08:00 -0000	1.24
+++ compiler/closure_analysis.m	15 Aug 2011 15:47:09 -0000
@@ -42,6 +42,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.dependency_graph.
 
 :- import_module assoc_list.
@@ -114,9 +115,9 @@
 
 closure_info_init(ModuleInfo, VarTypes, HeadVars, ArgModes) = ClosureInfo :-
     partition_arguments(ModuleInfo, VarTypes, HeadVars, ArgModes,
-        set.init, Inputs0, set.init, _Outputs),
-    Inputs = set.filter(var_has_ho_type(VarTypes), Inputs0),
-    set.fold(insert_unknown, Inputs, map.init, ClosureInfo).
+        set_of_var.init, Inputs0, set_of_var.init, _Outputs),
+    Inputs = set_of_var.filter(var_has_ho_type(VarTypes), Inputs0),
+    set_of_var.fold(insert_unknown, Inputs, map.init, ClosureInfo).
 
     % Succeeds iff the given variable has a higher-order type.
     %
@@ -195,7 +196,7 @@
         % in case there are duplicate arguments.
 
         partition_arguments(ModuleInfo, VarTypes, CallArgs, CallArgModes,
-            set.init, InputArgs, set.init, OutputArgs),
+            set_of_var.init, InputArgs, set_of_var.init, OutputArgs),
 
         % Update the goal_info to include any information about the
         % values of higher-order valued variables.
@@ -217,17 +218,17 @@
                 true
             )
         ),
-        set.fold(AddValues, InputArgs, map.init, Values),
+        set_of_var.fold(AddValues, InputArgs, map.init, Values),
         goal_info_set_ho_values(Values, GoalInfo0, GoalInfo),
 
         % Insert any information about higher-order outputs from this call
         % into the closure_info.
-        set.fold(insert_unknown, OutputArgs, !ClosureInfo),
+        set_of_var.fold(insert_unknown, OutputArgs, !ClosureInfo),
         Goal = hlds_goal(GoalExpr0, GoalInfo)
     ;
         GoalExpr0 = generic_call(Details, GCallArgs, GCallModes, _),
         partition_arguments(ModuleInfo, VarTypes, GCallArgs, GCallModes,
-            set.init, InputArgs0, set.init, OutputArgs),
+            set_of_var.init, InputArgs0, set_of_var.init, OutputArgs),
 
         % For higher-order calls we need to make sure that the actual
         % higher-order variable being called is also considered (it will
@@ -236,7 +237,7 @@
         % separately.
 
         ( Details = higher_order(CalledClosure0, _, _, _) ->
-            set.insert(CalledClosure0, InputArgs0, InputArgs)
+            set_of_var.insert(CalledClosure0, InputArgs0, InputArgs)
         ;
             InputArgs = InputArgs0
         ),
@@ -257,12 +258,12 @@
                 true
             )
         ),
-        set.fold(AddValues, InputArgs, map.init, Values),
+        set_of_var.fold(AddValues, InputArgs, map.init, Values),
         goal_info_set_ho_values(Values, GoalInfo0, GoalInfo),
 
         % Insert any information about higher-order outputs from this call
         % into the closure_info
-        set.fold(insert_unknown, OutputArgs, !ClosureInfo),
+        set_of_var.fold(insert_unknown, OutputArgs, !ClosureInfo),
         Goal = hlds_goal(GoalExpr0, GoalInfo)
     ;
         GoalExpr0 = switch(SwitchVar, SwitchCanFail, Cases0),
@@ -389,8 +390,8 @@
 
 :- pred partition_arguments(module_info::in, vartypes::in,
     prog_vars::in, list(mer_mode)::in,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 partition_arguments(_, _, [],    [], !Inputs, !Outputs).
 partition_arguments(_, _, [_|_], [], _, _, _, _) :-
@@ -401,9 +402,9 @@
         !Inputs, !Outputs) :-
     ( var_has_ho_type(VarTypes, Var) ->
         ( mode_is_input(ModuleInfo, Mode) ->
-            set.insert(Var, !Inputs)
+            set_of_var.insert(Var, !Inputs)
         ; mode_is_output(ModuleInfo, Mode) ->
-            set.insert(Var, !Outputs)
+            set_of_var.insert(Var, !Outputs)
         ;
             true
         )
Index: compiler/code_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/code_info.m,v
retrieving revision 1.392
diff -u -b -r1.392 code_info.m
--- compiler/code_info.m	21 Jul 2011 06:58:24 -0000	1.392
+++ compiler/code_info.m	15 Aug 2011 15:33:42 -0000
@@ -507,7 +507,7 @@
     ->
         trace_fail_vars(ModuleInfo, ProcInfo, FailVars),
         MaybeFailVars = yes(FailVars),
-        set.union(Liveness, FailVars, EffLiveness)
+        set_of_var.union(Liveness, FailVars, EffLiveness)
     ;
         MaybeFailVars = no,
         EffLiveness = Liveness
@@ -581,7 +581,7 @@
             MaybeContainingGoalMap
         ),
         code_info_loc_dep(
-            set_to_bitset(Liveness),
+            Liveness,
             InstMap,
             Zombies,
             VarLocnInfo,
@@ -1519,7 +1519,7 @@
     % being cut across. If the goal succeeds, the commit will cut
     % any choice points generated in the goal.
     %
-    % The set(prog_var) should be the set of variables live before
+    % The set_of_progvar should be the set of variables live before
     % the scope goal.
     %
 :- type semi_commit_info.
@@ -2902,7 +2902,7 @@
 
 %---------------------------------------------------------------------------%
 
-:- pred init_fail_info(code_model::in, maybe(set(prog_var))::in,
+:- pred init_fail_info(code_model::in, maybe(set_of_progvar)::in,
     resume_point_info::out, code_info::in, code_info::out) is det.
 
 init_fail_info(CodeModel, MaybeFailVars, ResumePoint, !CI) :-
@@ -2935,7 +2935,8 @@
     (
         MaybeFailVars = yes(FailVars),
         get_stack_slots(!.CI, StackSlots),
-        map.select(StackSlots, FailVars, AbsStackMap),
+        map.select_sorted_list(StackSlots, set_of_var.to_sorted_list(FailVars),
+            AbsStackMap),
         map.to_assoc_list(AbsStackMap, AbsStackList),
         StackList0 = assoc_list.map_values_only(stack_slot_to_lval,
             AbsStackList),
@@ -3798,7 +3799,7 @@
 
 :- pred clobber_regs(list(lval)::in, code_info::in, code_info::out) is det.
 
-:- pred save_variables(set(prog_var)::in, set(lval)::out, llds_code::out,
+:- pred save_variables(set_of_progvar::in, set(lval)::out, llds_code::out,
     code_info::in, code_info::out) is det.
 
 :- pred save_variables_on_stack(list(prog_var)::in, llds_code::out,
@@ -4188,7 +4189,7 @@
     set_var_locn_info(VarLocnInfo, !CI).
 
 save_variables(OutArgs, SavedLocs, Code, !CI) :-
-    compute_forward_live_var_saves(!.CI, set_to_bitset(OutArgs), VarLocs),
+    compute_forward_live_var_saves(!.CI, OutArgs, VarLocs),
     assoc_list.values(VarLocs, SavedLocList),
     set.list_to_set(SavedLocList, SavedLocs),
     place_vars(VarLocs, Code, !CI).
@@ -4241,7 +4242,7 @@
 :- interface.
 
 :- pred generate_call_vn_livevals(code_info::in, list(arg_loc)::in,
-    set(prog_var)::in, set(lval)::out) is det.
+    set_of_progvar::in, set(lval)::out) is det.
 
 :- pred generate_return_live_lvalues(code_info::in,
     assoc_list(prog_var, arg_loc)::in, instmap::in, bool::in,
@@ -4256,7 +4257,7 @@
     generate_input_var_vn(InputArgLocs, StackLiveVals, LiveVals).
 
 :- pred generate_call_stack_vn_livevals(code_info::in,
-    set(prog_var)::in, set(lval)::out) is det.
+    set_of_progvar::in, set(lval)::out) is det.
 
 generate_call_stack_vn_livevals(CI, OutputArgs, LiveVals) :-
     get_known_variables(CI, KnownVarList0),
@@ -4264,9 +4265,9 @@
     VarTypes = get_var_types(CI),
     list.filter(var_is_of_non_dummy_type(ModuleInfo, VarTypes),
         KnownVarList0, KnownVarList),
-    set.list_to_set(KnownVarList, KnownVars),
-    set.difference(KnownVars, OutputArgs, LiveVars),
-    set.to_sorted_list(LiveVars, LiveVarList),
+    set_of_var.list_to_set(KnownVarList, KnownVars),
+    set_of_var.difference(KnownVars, OutputArgs, LiveVars),
+    set_of_var.to_sorted_list(LiveVars, LiveVarList),
     generate_stack_var_vn(CI, LiveVarList, set.init, LiveVals1),
     get_active_temps_data(CI, Temps),
     generate_call_temp_vn(Temps, LiveVals1, LiveVals).
Index: compiler/common.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/common.m,v
retrieving revision 1.115
diff -u -b -r1.115 common.m
--- compiler/common.m	23 May 2011 05:08:01 -0000	1.115
+++ compiler/common.m	15 Aug 2011 08:55:00 -0000
@@ -101,6 +101,7 @@
 :- import_module libs.options.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.
 :- import_module transform_hlds.pd_cost.
 
@@ -776,7 +777,7 @@
     map.lookup(VarTypes, ToVar, ToVarType),
     map.lookup(VarTypes, FromVar, FromVarType),
 
-    set.list_to_set([ToVar, FromVar], NonLocals),
+    set_of_var.list_to_set([ToVar, FromVar], NonLocals),
     UniMode = ((_ - ToVarInst0) -> (_ - ToVarInst)),
     ( types_match_exactly(ToVarType, FromVarType) ->
         UnifyMode = (ToVarInst0 -> ToVarInst) - (ToVarInst -> ToVarInst),
Index: compiler/complexity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/complexity.m,v
retrieving revision 1.43
diff -u -b -r1.43 complexity.m
--- compiler/complexity.m	23 May 2011 05:08:01 -0000	1.43
+++ compiler/complexity.m	15 Aug 2011 11:07:25 -0000
@@ -74,6 +74,7 @@
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.term_norm.
 
 :- import_module assoc_list.
@@ -265,7 +266,7 @@
     % generated by the transformation does, so we need to compute the
     % nonlocals from the headvars rather than getting it from the
     % nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     goal_info_set_purity(purity_impure, OrigGoalInfo, ImpureOrigGoalInfo),
@@ -343,7 +344,7 @@
         RedoGoal = hlds_goal(RedoGoalExpr, RedoGoalInfo),
 
         instmap_delta_init_reachable(AfterInstMapDelta),
-        goal_info_init(list_to_set([SlotVar]), AfterInstMapDelta,
+        goal_info_init(set_of_var.make_singleton(SlotVar), AfterInstMapDelta,
             detism_multi, purity_impure, Context, AfterGoalInfo),
         AfterGoal = hlds_goal(disj([ExitGoal, RedoGoal]), AfterGoalInfo),
 
Index: compiler/constraint.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/constraint.m,v
retrieving revision 1.101
diff -u -b -r1.101 constraint.m
--- compiler/constraint.m	23 May 2011 05:08:01 -0000	1.101
+++ compiler/constraint.m	15 Aug 2011 11:11:50 -0000
@@ -54,13 +54,13 @@
 :- import_module hlds.goal_form.
 :- import_module libs.globals.
 :- import_module libs.options.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module list.
 :- import_module map.
 :- import_module pair.
 :- import_module require.
-:- import_module set.
 :- import_module term.
 :- import_module varset.
 
@@ -323,7 +323,7 @@
             \+ inst_matches_initial(InstAfter, InstBefore,
                 map.lookup(VarTypes, Var), ModuleInfo)
         ),
-    IncompatibleInstVars = set.list_to_set(
+    IncompatibleInstVars = set_of_var.list_to_set(
         list.filter(InCompatible, InstMapVars)),
 
     % This will consider variables with inst `any' to be bound by the goal,
@@ -337,12 +337,12 @@
             \+ inst_matches_binding(InstAfter, InstBefore,
                 map.lookup(VarTypes, Var), ModuleInfo)
         ),
-    BoundVars = set.list_to_set(list.filter(Bound, InstMapVars)),
+    BoundVars = set_of_var.list_to_set(list.filter(Bound, InstMapVars)),
 
     % Make sure that variables with inst `any' are placed in the changed vars
     % set. XXX This is too conservative, but avoids unexpected reorderings.
 
-    set.union(ChangedVars0, BoundVars, ChangedVars),
+    set_of_var.union(ChangedVars0, BoundVars, ChangedVars),
 
     AnnotatedConjunct = annotated_conjunct(Goal, ChangedVars, BoundVars,
         IncompatibleInstVars),
@@ -361,17 +361,17 @@
                 hlds_goal,
 
                 % All variables returned by instmap_changed_vars.
-                set(prog_var),
+                set_of_progvar,
 
                 % All variables returned by instmap_changed_vars for
                 % which inst_matches_binding(NewInst, OldInst) fails.
-                set(prog_var),
+                set_of_progvar,
 
                 % Variables returned by instmap_changed_vars
                 % for which the new inst cannot be substituted
                 % for the old as an input to a goal
                 % (inst_matches_initial(NewInst, OldInst) fails).
-                set(prog_var)
+                set_of_progvar
             ).
 
     % A constraint is a goal that may fail, has no outputs,
@@ -383,13 +383,13 @@
                 hlds_goal,
 
                 % All variables returned by instmap_changed_vars.
-                set(prog_var),
+                set_of_progvar,
 
                 % Variables returned by instmap_changed_vars
                 % for which the new inst cannot be substituted
                 % for the old as an input to a goal
                 % (inst_matches_initial(NewInst, OldInst) fails).
-                set(prog_var),
+                set_of_progvar,
 
                 % Goals to construct constants used by the constraint.
                 % (as in X = 2, Y < X). These need to be propagated
@@ -437,7 +437,7 @@
 
         % XXX This is probably a bit too conservative. For example,
         % `any->any' moded non-locals are considered to be outputs.
-        set.empty(OutputVars),
+        set_of_var.is_empty(OutputVars),
 
         % Don't propagate impure goals.
         goal_info_get_purity(GoalInfo) = purity_pure,
@@ -493,7 +493,7 @@
             ConstraintGoals),
         list.map(add_empty_constraints, [Goal | ConstraintGoals],
             GoalsAndConstraints),
-        list.append(GoalsAndConstraints, Goals0, Goals1)
+        Goals1 = GoalsAndConstraints ++ Goals0
     ;
         % Don't move goals which can fail before a goal which can loop
         % or throw an exception if `--fully-strict' is set.
@@ -558,7 +558,7 @@
     (
         ConstraintGoal0 = hlds_goal(_, ConstraintInfo),
         ConstraintNonLocals = goal_info_get_nonlocals(ConstraintInfo),
-        set.member(ConstructVar, ConstraintNonLocals)
+        set_of_var.member(ConstraintNonLocals, ConstructVar)
     ->
         VarSet0 = !.Info ^ constr_varset,
         VarTypes0 = !.Info ^ constr_vartypes,
@@ -592,7 +592,7 @@
     % current goal, if the purity and termination properties of the
     % current goal allow that.
     %
-:- pred filter_dependent_constraints(set(prog_var)::in, set(prog_var)::in,
+:- pred filter_dependent_constraints(set_of_progvar::in, set_of_progvar::in,
     list(constraint)::in, list(constraint)::out, list(constraint)::out) is det.
 
 filter_dependent_constraints(NonLocals, GoalOutputVars, Constraints,
@@ -602,7 +602,7 @@
     list.reverse(RevDependent, Dependent),
     list.reverse(RevIndependent, Independent).
 
-:- pred filter_dependent_constraints_2(set(prog_var)::in, set(prog_var)::in,
+:- pred filter_dependent_constraints_2(set_of_progvar::in, set_of_progvar::in,
     list(constraint)::in,
     list(constraint)::in, list(constraint)::out,
     list(constraint)::in, list(constraint)::out) is det.
@@ -619,17 +619,17 @@
         (
             % A constraint is not independent of a goal if it uses
             % any of the output variables of that goal.
-            set.intersect(ConstraintNonLocals, GoalOutputVars,
+            set_of_var.intersect(ConstraintNonLocals, GoalOutputVars,
                 OutputVarsUsedByConstraint),
-            \+ set.empty(OutputVarsUsedByConstraint)
+            \+ set_of_var.is_empty(OutputVarsUsedByConstraint)
         ;
             % A constraint is not independent of a goal if it changes
             % the inst of a non-local of the goal in such a way that
             % the new inst is incompatible with the old inst (e.g. by
             % losing uniqueness).
-            set.intersect(NonLocals, IncompatibleInstVars,
+            set_of_var.intersect(NonLocals, IncompatibleInstVars,
                 IncompatibleInstVarsUsedByGoal),
-            \+ set.empty(IncompatibleInstVarsUsedByGoal)
+            \+ set_of_var.is_empty(IncompatibleInstVarsUsedByGoal)
         ;
             % A constraint is not independent of a goal if it uses
             % any variables whose instantiatedness is changed
@@ -656,9 +656,9 @@
     Constraint = constraint(ConstraintGoal, _, _, _),
     ConstraintGoal = hlds_goal(_, ConstraintGoalInfo),
     ConstraintNonLocals = goal_info_get_nonlocals(ConstraintGoalInfo),
-    set.intersect(EarlierChangedVars, ConstraintNonLocals,
+    set_of_var.intersect(EarlierChangedVars, ConstraintNonLocals,
         EarlierConstraintIntersection),
-    set.empty(EarlierConstraintIntersection).
+    set_of_var.is_empty(EarlierConstraintIntersection).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/coverage_profiling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/coverage_profiling.m,v
retrieving revision 1.7
diff -u -b -r1.7 coverage_profiling.m
--- compiler/coverage_profiling.m	23 May 2011 05:08:01 -0000	1.7
+++ compiler/coverage_profiling.m	15 Aug 2011 11:05:02 -0000
@@ -51,6 +51,7 @@
 :- import_module libs.options.
 :- import_module ll_backend.deep_profiling.
 :- import_module parse_tree.builtin_lib_types.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -1160,7 +1161,7 @@
         coverage_point_ll_code(DataType, ForeignCallAttrs, ForeignCode),
         CallGoalExpr = call_foreign_proc(ForeignCallAttrs, PredId, ProcId,
             ForeignArgVars, [], no, ForeignCode),
-        NonLocals = list_to_set(ArgVars),
+        NonLocals = set_of_var.list_to_set(ArgVars),
         InstMapDelta = instmap_delta_from_assoc_list([]),
         CallGoalInfo = impure_init_goal_info(NonLocals, InstMapDelta,
             detism_det),
Index: compiler/cse_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/cse_detection.m,v
retrieving revision 1.132
diff -u -b -r1.132 cse_detection.m
--- compiler/cse_detection.m	23 May 2011 05:08:01 -0000	1.132
+++ compiler/cse_detection.m	4 Aug 2011 02:23:38 -0000
@@ -53,6 +53,7 @@
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type_subst.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -61,7 +62,6 @@
 :- import_module map.
 :- import_module pair.
 :- import_module require.
-:- import_module set.
 :- import_module string.
 :- import_module term.
 :- import_module varset.
@@ -337,20 +337,20 @@
         ;
             Goals0 = [_ | _],
             NonLocals = goal_info_get_nonlocals(GoalInfo),
-            set.to_sorted_list(NonLocals, NonLocalsList),
+            NonLocalsList = set_of_var.to_sorted_list(NonLocals),
             detect_cse_in_disj(NonLocalsList, Goals0, GoalInfo,
                 InstMap0, !CseInfo, Redo, GoalExpr)
         )
     ;
         GoalExpr0 = switch(Var, CanFail, Cases0),
         NonLocals = goal_info_get_nonlocals(GoalInfo),
-        set.to_sorted_list(NonLocals, NonLocalsList),
+        NonLocalsList = set_of_var.to_sorted_list(NonLocals),
         detect_cse_in_cases(NonLocalsList, Var, CanFail, Cases0, GoalInfo,
             InstMap0, !CseInfo, Redo, GoalExpr)
     ;
         GoalExpr0 = if_then_else(Vars, Cond0, Then0, Else0),
         NonLocals = goal_info_get_nonlocals(GoalInfo),
-        set.to_sorted_list(NonLocals, NonLocalsList),
+        NonLocalsList = set_of_var.to_sorted_list(NonLocals),
         detect_cse_in_ite(NonLocalsList, Vars, Cond0, Then0, Else0, GoalInfo,
             InstMap0, !CseInfo, Redo, GoalExpr)
     ;
Index: compiler/ctgc.livedata.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ctgc.livedata.m,v
retrieving revision 1.9
diff -u -b -r1.9 ctgc.livedata.m
--- compiler/ctgc.livedata.m	23 May 2011 05:08:01 -0000	1.9
+++ compiler/ctgc.livedata.m	15 Aug 2011 12:45:58 -0000
@@ -87,6 +87,7 @@
 
 :- implementation.
 
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.ctgc.datastruct.
 :- import_module transform_hlds.ctgc.structure_sharing.domain.
 
@@ -215,7 +216,7 @@
     % (XXX collect the what?)
     Lfu = goal_info_get_lfu(GoalInfo),
     Lbu = goal_info_get_lbu(GoalInfo), 
-    Lu = set.to_sorted_list(set.union(Lfu, Lbu)),
+    Lu = set_of_var.to_sorted_list(set_of_var.union(Lfu, Lbu)),
     (
         % When there are no data structure in forward nor backward use, 
         % then the livedata set is empty.
Index: compiler/deep_profiling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deep_profiling.m,v
retrieving revision 1.114
diff -u -b -r1.114 deep_profiling.m
--- compiler/deep_profiling.m	7 Jul 2011 00:47:29 -0000	1.114
+++ compiler/deep_profiling.m	15 Aug 2011 10:52:00 -0000
@@ -67,6 +67,7 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.
 :- import_module transform_hlds.dead_proc_elim.
 :- import_module transform_hlds.dependency_graph.
@@ -895,7 +896,8 @@
         generate_deep_call(ModuleInfo, "semi_fail_port_code_sr", 3,
             [TopCSD, MiddleCSD, ActivationPtr1], no, detism_failure,
             FailPortCode),
-        NewNonlocals = list_to_set([TopCSD, MiddleCSD, ActivationPtr1])
+        NewNonlocals =
+            set_of_var.list_to_set([TopCSD, MiddleCSD, ActivationPtr1])
     ;
         MaybeActivationPtr = no,
         generate_deep_det_call(ModuleInfo, "semi_call_port_code_ac", 3,
@@ -907,7 +909,7 @@
             [TopCSD, MiddleCSD], [], ExitPortCode),
         generate_deep_call(ModuleInfo, "semi_fail_port_code_ac", 2,
             [TopCSD, MiddleCSD], no, detism_failure, FailPortCode),
-        NewNonlocals = list_to_set([TopCSD, MiddleCSD])
+        NewNonlocals = set_of_var.list_to_set([TopCSD, MiddleCSD])
     ),
 
     ExitConjGoalInfo = goal_info_add_nonlocals_make_impure(GoalInfo0,
@@ -987,8 +989,7 @@
     determinism_components(Detism, CanFail, at_most_many),
     goal_info_set_determinism(Detism, GoalInfo0, GoalInfo1),
 
-    ExitRedoNonLocals = set.union(NewNonlocals,
-        list_to_set([NewOutermostProcDyn])),
+    set_of_var.insert(NewOutermostProcDyn, NewNonlocals, ExitRedoNonLocals),
     ExitRedoGoalInfo = impure_reachable_init_goal_info(ExitRedoNonLocals,
         detism_multi),
 
@@ -1376,7 +1377,7 @@
             ( CodeModel = model_semi
             ; CodeModel = model_non
             ),
-            ExtraVars = list_to_set([MiddleCSD | SaveRestoreVars]),
+            ExtraVars = set_of_var.list_to_set([MiddleCSD | SaveRestoreVars]),
             WrappedGoalGoalInfo0 =
                 goal_info_add_nonlocals_make_impure(MdprofInstGoalInfo,
                     ExtraVars),
@@ -1429,7 +1430,8 @@
             UseActivationCounts = yes,
 
             generate_var("SavedCounter", int_type, SavedCountVar, !VarInfo),
-            ExtraNonLocals = set.list_to_set([SavedCountVar, SavedPtrVar]),
+            ExtraNonLocals =
+                set_of_var.list_to_set([SavedCountVar, SavedPtrVar]),
 
             generate_deep_det_call(!.DeepInfo ^ deep_module_info,
                 "save_and_zero_activation_info_ac", 2,
@@ -1444,7 +1446,7 @@
         ;
             UseActivationCounts = no,
 
-            ExtraNonLocals = set.list_to_set([SavedPtrVar]),
+            ExtraNonLocals = set_of_var.make_singleton(SavedPtrVar),
 
             generate_deep_det_call(!.DeepInfo ^ deep_module_info,
                 "save_and_zero_activation_info_sr", 1,
@@ -1479,7 +1481,7 @@
     goal_info_set_mdprof_inst(goal_is_mdprof_inst,
         RestoreFailGoalInfo0, RestoreFailGoalInfo),
 
-    RezeroFailGoalInfo0 = impure_unreachable_init_goal_info(set.init,
+    RezeroFailGoalInfo0 = impure_unreachable_init_goal_info(set_of_var.init,
         detism_failure),
     goal_info_set_mdprof_inst(goal_is_mdprof_inst,
         RezeroFailGoalInfo0, RezeroFailGoalInfo),
@@ -1786,7 +1788,7 @@
 
 generate_deep_const_unify(ConsId, Var, Goal) :-
     Ground = ground(shared, none),
-    NonLocals = set.make_singleton_set(Var),
+    NonLocals = set_of_var.make_singleton(Var),
     InstMapDelta = instmap_delta_bind_var(Var),
     Determinism = detism_det,
     goal_info_init(NonLocals, InstMapDelta, Determinism, purity_pure,
@@ -1804,7 +1806,7 @@
 
 generate_deep_cell_unify(Length, ConsId, Args, Var, Goal) :-
     Ground = ground(shared, none),
-    NonLocals = set.list_to_set([Var | Args]),
+    NonLocals = set_of_var.list_to_set([Var | Args]),
     InstMapDelta = instmap_delta_bind_var(Var),
     Determinism = detism_det,
     goal_info_init(NonLocals, InstMapDelta, Determinism, purity_pure,
Index: compiler/deforest.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deforest.m,v
retrieving revision 1.103
diff -u -b -r1.103 deforest.m
--- compiler/deforest.m	11 Aug 2011 23:18:59 -0000	1.103
+++ compiler/deforest.m	15 Aug 2011 13:09:38 -0000
@@ -475,7 +475,7 @@
 %-----------------------------------------------------------------------------%
 
 :- pred propagate_conj_constraints(list(hlds_goal)::in,
-    set(prog_var)::in, list(hlds_goal)::in, list(hlds_goal)::out,
+    set_of_progvar::in, list(hlds_goal)::in, list(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
 propagate_conj_constraints([], _, RevGoals, Goals, !PDInfo) :-
@@ -531,7 +531,7 @@
 :- type annotated_conj ==
     assoc_list(hlds_goal, maybe(pd_branch_info(prog_var))).
 
-:- pred deforest_conj(annotated_conj::in, set(prog_var)::in,
+:- pred deforest_conj(annotated_conj::in, set_of_progvar::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -643,7 +643,7 @@
     % Take the part of a conjunction found to have potential
     % for deforestation and attempt the optimization.
     %
-:- pred handle_deforestation(set(prog_var)::in, deforest_info::in,
+:- pred handle_deforestation(set_of_progvar::in, deforest_info::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
     annotated_conj::in, annotated_conj::out, bool::out,
     pd_info::in, pd_info::out) is det.
@@ -837,8 +837,9 @@
         ),
         OpaqueGoal = hlds_goal(_, OpaqueGoalInfo),
         OpaqueNonLocals = goal_info_get_nonlocals(OpaqueGoalInfo),
-        set.intersect(OpaqueNonLocals, OpaqueVars, UsedOpaqueVars),
-        \+ set.empty(UsedOpaqueVars)
+        OpaqueVarsSet = set_of_var.set_to_bitset(OpaqueVars), 
+        set_of_var.intersect(OpaqueNonLocals, OpaqueVarsSet, UsedOpaqueVars),
+        set_of_var.is_non_empty(UsedOpaqueVars)
     ->
         trace [compile_time(flag("debug_deforest")), io(!IO)] (
             pd_debug_message(DebugPD,
@@ -1014,7 +1015,7 @@
 
     % Attempt deforestation on a pair of calls.
     %
-:- pred call_call(set(prog_var)::in, hlds_goal::in,
+:- pred call_call(set_of_progvar::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, maybe(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1033,7 +1034,7 @@
 
     % Attempt deforestation on a pair of calls.
     %
-:- pred call_call_2(set(prog_var)::in, hlds_goal::in,
+:- pred call_call_2(set_of_progvar::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, maybe(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1128,7 +1129,7 @@
     % recursively process that procedure.
     %
 :- pred create_deforest_goal(hlds_goal::in, hlds_goals::in,
-    maybe(hlds_goal)::in, hlds_goal::in, set(prog_var)::in, bool::in,
+    maybe(hlds_goal)::in, hlds_goal::in, set_of_progvar::in, bool::in,
     proc_pair::in, int::in, maybe(pred_proc_id)::in, maybe(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1180,7 +1181,7 @@
             DidUnfold, !PDInfo),
         create_conj(UnfoldedCall, BetweenGoals, MaybeLaterGoal, NonLocals,
             DeforestGoal0),
-        set.to_sorted_list(NonLocals, NonLocalsList),
+        set_of_var.to_sorted_list(NonLocals, NonLocalsList),
 
         (
             DidUnfold = yes,
@@ -1327,7 +1328,7 @@
     proc_info_get_argmodes(CalledProcInfo, ArgModes),
     instmap_delta_from_mode_list(Args, ArgModes, ModuleInfo, InstMapDelta),
     proc_info_interface_determinism(ProcInfo, Detism),
-    set.list_to_set(Args, NonLocals),
+    set_of_var.list_to_set(Args, NonLocals),
     pred_info_get_purity(CalledPredInfo, Purity),
     goal_info_init(NonLocals, InstMapDelta, Detism, Purity, GoalInfo),
 
@@ -1366,7 +1367,7 @@
     % into a conjunction.
     %
 :- pred create_conj(hlds_goal::in, list(hlds_goal)::in,
-    maybe(hlds_goal)::in, set(prog_var)::in, hlds_goal::out) is det.
+    maybe(hlds_goal)::in, set_of_progvar::in, hlds_goal::out) is det.
 
 create_conj(EarlierGoal, BetweenGoals, MaybeLaterGoal, NonLocals, FoldGoal) :-
     (
@@ -1396,7 +1397,7 @@
     % to achieve folding.
     %
 :- pred try_generalisation(hlds_goal::in, list(hlds_goal)::in,
-    maybe(hlds_goal)::in, hlds_goal::in, set(prog_var)::in,
+    maybe(hlds_goal)::in, hlds_goal::in, set_of_progvar::in,
     proc_pair::in, int::in, pred_proc_id::in, maybe(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1451,7 +1452,7 @@
 :- pred do_generalisation(list(prog_var)::in,
     map(prog_var, prog_var)::in, instmap::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, hlds_goal::in,
-    set(prog_var)::in, proc_pair::in, int::in,
+    set_of_progvar::in, proc_pair::in, int::in,
     pred_proc_id::in, maybe(hlds_goal)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1467,7 +1468,7 @@
     pd_info_get_instmap(!.PDInfo, InstMap0),
     instmap_lookup_vars(VersionInstMap, VersionArgs, VersionInsts),
     pd_util.inst_list_size(ModuleInfo, VersionInsts, VersionInstSizes),
-    set.to_sorted_list(ConjNonLocals, ConjNonLocalsList),
+    set_of_var.to_sorted_list(ConjNonLocals, ConjNonLocalsList),
     (
         % Check whether we can do a most specific generalisation of insts
         % of the non-locals.
@@ -1527,7 +1528,7 @@
 :- pred match_generalised_version(module_info::in,
     hlds_goal::in, list(prog_var)::in, list(mer_type)::in,
     hlds_goal::in, list(hlds_goal)::in, maybe(hlds_goal)::in,
-    set(prog_var)::in, prog_varset::in, vartypes::in,
+    set_of_progvar::in, prog_varset::in, vartypes::in,
     version_index::in, map(prog_var, prog_var)::out) is semidet.
 
 match_generalised_version(ModuleInfo, VersionGoal, VersionArgs,
@@ -1602,9 +1603,9 @@
     % This is needed to ensure that the temporary list in double_append is
     % found to be local to the conjunction and can be removed.
     %
-:- pred get_sub_conj_nonlocals(set(prog_var)::in, deforest_info::in,
+:- pred get_sub_conj_nonlocals(set_of_progvar::in, deforest_info::in,
     list(hlds_goal)::in, list(hlds_goal)::in, list(hlds_goal)::in,
-    annotated_conj::in, set(prog_var)::out) is det.
+    annotated_conj::in, set_of_progvar::out) is det.
 
 get_sub_conj_nonlocals(NonLocals0, DeforestInfo,
         RevBeforeGoals, BeforeIrrelevant, AfterIrrelevant,
@@ -1616,10 +1617,10 @@
         BeforeIrrelevant, EarlierGoal, BetweenGoals, yes(LaterGoal),
         AfterIrrelevant, AfterGoals, SubConjNonLocals).
 
-:- pred get_sub_conj_nonlocals(set(prog_var)::in,
+:- pred get_sub_conj_nonlocals(set_of_progvar::in,
     list(hlds_goal)::in, list(hlds_goal)::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, list(hlds_goal)::in,
-    list(hlds_goal)::in, set(prog_var)::out) is det.
+    list(hlds_goal)::in, set_of_progvar::out) is det.
 
 get_sub_conj_nonlocals(!.NonLocals, RevBeforeGoals, BeforeIrrelevant,
         EarlierGoal, BetweenGoals, MaybeLaterGoal,
@@ -1627,7 +1628,7 @@
     AddGoalNonLocals = (pred(Goal::in, Vars0::in, Vars::out) is det :-
         Goal = hlds_goal(_, GoalInfo),
         GoalNonLocals = goal_info_get_nonlocals(GoalInfo),
-        set.union(Vars0, GoalNonLocals, Vars)
+        set_of_var.union(GoalNonLocals, Vars0, Vars)
     ),
     list.foldl(AddGoalNonLocals, RevBeforeGoals, !NonLocals),
     list.foldl(AddGoalNonLocals, BeforeIrrelevant, !NonLocals),
@@ -1635,14 +1636,14 @@
     list.foldl(AddGoalNonLocals, AfterGoals, !NonLocals),
 
     list.foldl(AddGoalNonLocals, [EarlierGoal | BetweenGoals],
-        set.init, !:SubConjNonLocals),
+        set_of_var.init, !:SubConjNonLocals),
     (
         MaybeLaterGoal = yes(LaterGoal),
         call(AddGoalNonLocals, LaterGoal, !SubConjNonLocals)
     ;
         MaybeLaterGoal = no
     ),
-    set.intersect(!.NonLocals, !SubConjNonLocals).
+    set_of_var.intersect(!.NonLocals, !SubConjNonLocals).
 
 %-----------------------------------------------------------------------------%
 
@@ -1733,7 +1734,7 @@
     % of the first goal, unfolding the second goal in the branches which have
     % extra information about the arguments.
     %
-:- pred push_goal_into_goal(set(prog_var)::in, set(int)::in,
+:- pred push_goal_into_goal(set_of_progvar::in, set(int)::in,
     hlds_goal::in, hlds_goals::in, hlds_goal::in, hlds_goal::out,
     pd_info::in, pd_info::out) is det.
 
@@ -1743,7 +1744,7 @@
     EarlierGoal = hlds_goal(EarlierGoalExpr, _),
     (
         EarlierGoalExpr = switch(Var1, CanFail1, Cases1),
-        set.insert(Var1, NonLocals, CaseNonLocals),
+        set_of_var.insert(Var1, NonLocals, CaseNonLocals),
         append_goal_to_cases(Var1, BetweenGoals, LaterGoal,
             CaseNonLocals, 1, DeforestInfo, Cases1, Cases, !PDInfo),
         GoalExpr = switch(Var1, CanFail1, Cases)
@@ -1752,7 +1753,7 @@
         pd_info_update_goal(Cond, !PDInfo),
         Cond = hlds_goal(_, CondInfo),
         CondNonLocals = goal_info_get_nonlocals(CondInfo),
-        set.union(CondNonLocals, NonLocals, ThenNonLocals),
+        set_of_var.union(CondNonLocals, NonLocals, ThenNonLocals),
         append_goal(Then0, BetweenGoals, LaterGoal,
             ThenNonLocals, 1, DeforestInfo, Then, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo),
@@ -1807,7 +1808,7 @@
     pd_info_set_instmap(InstMap0, !PDInfo).
 
 :- pred append_goal_to_disjuncts(hlds_goals::in, hlds_goal::in,
-    set(prog_var)::in, int::in, set(int)::in, hlds_goals::in, hlds_goals::out,
+    set_of_progvar::in, int::in, set(int)::in, hlds_goals::in, hlds_goals::out,
     pd_info::in, pd_info::out) is det.
 
 append_goal_to_disjuncts(_, _, _, _, _, [], [], !PDInfo).
@@ -1822,7 +1823,7 @@
         NonLocals, NextBranch, Branches, Goals0, Goals, !PDInfo).
 
 :- pred append_goal_to_cases(prog_var::in, hlds_goals::in,
-    hlds_goal::in, set(prog_var)::in, int::in, set(int)::in,
+    hlds_goal::in, set_of_progvar::in, int::in, set(int)::in,
     list(case)::in,list(case)::out, pd_info::in, pd_info::out) is det.
 
 append_goal_to_cases(_, _, _, _, _, _, [], [], !PDInfo).
@@ -1840,7 +1841,7 @@
         NonLocals, NextCase, Branches, Cases0, Cases, !PDInfo).
 
 :- pred append_goal(hlds_goal::in, hlds_goals::in,
-    hlds_goal::in, set(prog_var)::in, int::in, set(int)::in,
+    hlds_goal::in, set_of_progvar::in, int::in, set(int)::in,
     hlds_goal::out, pd_info::in, pd_info::out) is det.
 
 append_goal(Goal0, BetweenGoals, GoalToAppend0, NonLocals0,
@@ -1859,7 +1860,7 @@
     list.condense([GoalList0, BetweenGoals, GoalListToAppend], Goals),
 
     goal_list_nonlocals(Goals, SubNonLocals),
-    set.intersect(NonLocals0, SubNonLocals, NonLocals),
+    set_of_var.intersect(NonLocals0, SubNonLocals, NonLocals),
     goal_list_instmap_delta(Goals, Delta0),
     instmap_delta_restrict(NonLocals, Delta0, Delta),
     goal_list_determinism(Goals, Detism),
@@ -2021,8 +2022,8 @@
         % Update the quantification if not all the output arguments are used.
         Goal1 = hlds_goal(_, GoalInfo1),
         NonLocals1 = goal_info_get_nonlocals(GoalInfo1),
-        set.list_to_set(Args, NonLocals),
-        ( set.equal(NonLocals1, NonLocals) ->
+        set_of_var.list_to_set(Args, NonLocals),
+        ( set_of_var.equal(NonLocals1, NonLocals) ->
             Goal2 = Goal1
         ;
             pd_requantify_goal(NonLocals, Goal1, Goal2, !PDInfo)
@@ -2034,8 +2035,8 @@
         ),
         proc_info_arglives(CalledProcInfo, ModuleInfo, ArgLives),
         get_live_vars(Args, ArgLives, LiveVars0),
-        set.list_to_set(LiveVars0, LiveVars1),
-        set.intersect(NonLocals, LiveVars1, LiveVars),
+        set_of_var.list_to_set(LiveVars0, LiveVars1),
+        set_of_var.intersect(NonLocals, LiveVars1, LiveVars),
         pd_util.unique_modecheck_goal_live_vars(LiveVars, Goal2, Goal3, Errors,
             !PDInfo),
 
Index: compiler/delay_construct.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/delay_construct.m,v
retrieving revision 1.35
diff -u -b -r1.35 delay_construct.m
--- compiler/delay_construct.m	21 Jul 2011 06:58:24 -0000	1.35
+++ compiler/delay_construct.m	15 Aug 2011 12:47:32 -0000
@@ -219,7 +219,7 @@
         Goal0 = hlds_goal(GoalExpr0, GoalInfo0),
         delay_construct_skippable(GoalExpr0, GoalInfo0),
         NonLocals = goal_info_get_nonlocals(GoalInfo0),
-        maybe_complete_with_typeinfo_vars(set_to_bitset(NonLocals),
+        maybe_complete_with_typeinfo_vars(NonLocals,
             DelayInfo ^ dci_body_typeinfo_liveness,
             DelayInfo ^ dci_vartypes,
             DelayInfo ^ dci_rtti_varmaps, CompletedNonLocals),
Index: compiler/delay_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/delay_info.m,v
retrieving revision 1.36
diff -u -b -r1.36 delay_info.m
--- compiler/delay_info.m	23 May 2011 05:08:01 -0000	1.36
+++ compiler/delay_info.m	15 Aug 2011 13:31:59 -0000
@@ -89,8 +89,8 @@
 
 :- implementation.
 
-:- import_module check_hlds.mode_errors.   % for the mode_error_info
-                                           % and delay_info types.
+:- import_module check_hlds.mode_errors.
+:- import_module parse_tree.set_of_var.
 
 :- import_module int.
 :- import_module map.
@@ -108,43 +108,35 @@
 
 :- type delay_info
     --->    delay_info(
+                % CurrentDepth: the current conjunction depth,
+                % i.e. the number of nested conjunctions which are
+                % currently active.
                 delay_depth     :: depth_num,
-                                % CurrentDepth:
-                                % the current conjunction depth,
-                                % i.e. the number of nested conjunctions
-                                % which are currently active
 
+                % DelayedGoalStack: for each nested conjunction,
+                % we store a collection of delayed goals associated with
+                % that conjunction, indexed by sequence number.
                 delay_goals     :: stack(map(seq_num, delayed_goal)),
-                                % DelayedGoalStack:
-                                % for each nested conjunction,
-                                % we store a collection of delayed goals
-                                % associated with that conjunction,
-                                % indexed by sequence number
 
+                % WaitingGoalsTable: for each variable, we keep track of
+                % all the goals which are waiting on that variable.
                 delay_waiting   :: waiting_goals_table,
-                                % WaitingGoalsTable:
-                                % for each variable, we keep track of
-                                % all the goals which are waiting on
-                                % that variable
 
+                % PendingGoalsTable: when a variable gets bound, we mark
+                % all the goals which are waiting on that variable as ready
+                % to be reawakened at the next opportunity.
                 delay_pending   :: pending_goals_table,
-                                % PendingGoalsTable:
-                                % when a variable gets bound, we
-                                % mark all the goals which are waiting
-                                % on that variable as ready to be
-                                % reawakened at the next opportunity
 
+                % SeqNumsStack: For each nested conjunction, the next
+                % available sequence number.
                 delay_seqs      :: stack(seq_num)
-                                % SeqNumsStack:
-                                % For each nested conjunction, the
-                                % next available sequence number.
             ).
 
-:- type waiting_goals_table == map(prog_var, waiting_goals).
     % Used to store the collection of goals waiting on a variable.
+:- type waiting_goals_table == map(prog_var, waiting_goals).
 
-:- type waiting_goals == map(delay_goal_num, list(prog_var)).
     % For each goal, we store all the variables that it is waiting on.
+:- type waiting_goals == map(delay_goal_num, list(prog_var)).
 
 :- type pending_goals_table == map(depth_num, list(seq_num)).
 
@@ -284,7 +276,7 @@
     map.lookup(DelayedGoalsTable, SeqNum, DelayedGoal),
     DelayedGoal = delayed_goal(Vars, _Error, _Goal),
     GoalNum = delay_goal_num(Depth, SeqNum),
-    set.to_sorted_list(Vars, VarList),
+    set_of_var.to_sorted_list(Vars, VarList),
     delete_waiting_vars(VarList, GoalNum, !WaitingGoalsTable),
     remove_delayed_goals(SeqNums, DelayedGoalsTable, Depth,
         !WaitingGoalsTable).
@@ -313,7 +305,7 @@
 
     % Store indexes to the goal in the waiting goals table
     GoalNum = delay_goal_num(CurrentDepth, SeqNum),
-    set.to_sorted_list(Vars, VarList),
+    set_of_var.to_sorted_list(Vars, VarList),
     add_waiting_vars(VarList, GoalNum, VarList,
         WaitingGoalsTable0, WaitingGoalsTable),
 
Index: compiler/dep_par_conj.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/dep_par_conj.m,v
retrieving revision 1.56
diff -u -b -r1.56 dep_par_conj.m
--- compiler/dep_par_conj.m	10 Jun 2011 11:46:12 -0000	1.56
+++ compiler/dep_par_conj.m	15 Aug 2011 10:58:48 -0000
@@ -113,10 +113,9 @@
 :- import_module hlds.hlds_goal.
 :- import_module hlds.hlds_module.
 :- import_module hlds.instmap.
-:- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -134,7 +133,7 @@
     % This function is exported for use by the implicit_parallelism pass.
     %
 :- func find_shared_variables(module_info, instmap, list(hlds_goal))
-    = set(prog_var).
+    = set_of_progvar.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -155,6 +154,7 @@
 :- import_module libs.options.
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.builtin_lib_types.
+:- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_util.
 :- import_module transform_hlds.dependency_graph.
@@ -222,7 +222,7 @@
                 % Variables which should not be replaced by futures in this
                 % pass because it has already been done. This field is
                 % read only.
-                sync_ignore_vars            :: set(prog_var),
+                sync_ignore_vars            :: set_of_progvar,
 
                 % The value of the --allow-some-paths-only-waits option.
                 % Read-only.
@@ -266,12 +266,12 @@
         HasParallelConj = no
     ;
         HasParallelConj = yes,
-        sync_dep_par_conjs_in_proc(PredId, ProcId, set.init,
+        sync_dep_par_conjs_in_proc(PredId, ProcId, set_of_var.init,
             !ModuleInfo, !ProcsToScan, !TSStringTable)
     ).
 
-:- pred sync_dep_par_conjs_in_proc(pred_id::in, proc_id::in, set(prog_var)::in,
-    module_info::in, module_info::out,
+:- pred sync_dep_par_conjs_in_proc(pred_id::in, proc_id::in,
+    set_of_progvar::in, module_info::in, module_info::out,
     list(pred_proc_id)::in, list(pred_proc_id)::out,
     ts_string_table::in, ts_string_table::out) is det.
 
@@ -469,9 +469,10 @@
     % Filter out all the variables which have already have associated futures,
     % i.e. they were head variables which were replaced by futures; signal and
     % wait calls will already have been inserted for them.
-    SharedVars = set.filter(isnt(set.contains(IgnoreVars)), SharedVars0),
+    SharedVars = set_of_var.filter(isnt(set_of_var.contains(IgnoreVars)),
+        SharedVars0),
 
-    ( set.empty(SharedVars) ->
+    ( set_of_var.is_empty(SharedVars) ->
         % Independant parallel conjunctions can somtimes be re-ordered to
         % generate faster code.
         reorder_indep_par_conj(PredProcId, VarTypes0, InstMap, Conjuncts,
@@ -511,14 +512,14 @@
     %           append(AB_10, A, ABA)
     %       ).
     %
-:- pred sync_dep_par_conj(module_info::in, bool::in, set(prog_var)::in,
+:- pred sync_dep_par_conj(module_info::in, bool::in, set_of_progvar::in,
     list(hlds_goal)::in, hlds_goal_info::in, hlds_goal::out, instmap::in,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
     ts_string_table::in, ts_string_table::out) is det.
 
 sync_dep_par_conj(ModuleInfo, AllowSomePathsOnly, SharedVars, Goals, GoalInfo,
         NewGoal, InstMap, !VarSet, !VarTypes, !TSStringTable) :-
-    SharedVarsList = set.to_sorted_list(SharedVars),
+    SharedVarsList = set_of_var.to_sorted_list(SharedVars),
     list.map_foldl4(allocate_future(ModuleInfo), SharedVarsList,
         AllocateFuturesGoals, !VarSet, !VarTypes, map.init, FutureMap,
         !TSStringTable),
@@ -560,22 +561,23 @@
     % analysis prevent this situation?
     %
 :- pred sync_dep_par_proc_body(module_info::in, bool::in,
-    set(prog_var)::in, future_map::in, instmap::in,
+    set_of_progvar::in, future_map::in, instmap::in,
     hlds_goal::in, hlds_goal::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out) is det.
 
 sync_dep_par_proc_body(ModuleInfo, AllowSomePathsOnly, SharedVars, FutureMap,
         InstMap, !Goal, !VarSet, !VarTypes) :-
     Nonlocals = goal_get_nonlocals(!.Goal),
-    set.intersect(Nonlocals, SharedVars, NonlocalSharedVars),
-    ( not set.empty(NonlocalSharedVars) ->
+    set_of_var.intersect(Nonlocals, SharedVars, NonlocalSharedVars),
+    ( not set_of_var.is_empty(NonlocalSharedVars) ->
         GoalInfo0 = !.Goal ^ hlds_goal_info,
         InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo0),
         consumed_and_produced_vars(ModuleInfo, InstMap, InstMapDelta0,
             NonlocalSharedVars, ConsumedVarsList, ProducedVarsList),
 
         % Insert waits into the conjunct.
-        list.foldl3(insert_wait_in_goal_for_proc(ModuleInfo,
+        list.foldl3(
+            insert_wait_in_goal_for_proc(ModuleInfo,
                 AllowSomePathsOnly, FutureMap),
             ConsumedVarsList, !Goal, !VarSet, !VarTypes),
 
@@ -586,27 +588,28 @@
         true
     ),
 
-    set.difference(SharedVars, Nonlocals, WaitAfterVars),
-    ( not set.empty(WaitAfterVars) ->
+    set_of_var.difference(SharedVars, Nonlocals, WaitAfterVars),
+    ( not set_of_var.is_empty(WaitAfterVars) ->
         % WaitAfterVars are pushed into this call but not consumed in the body.
         % Our caller expects them to be consumed by the time this call returns
         % so we must wait for them.
         list.foldl3(insert_wait_after_goal(ModuleInfo, FutureMap),
-            set.to_sorted_list(WaitAfterVars), !Goal, !VarSet, !VarTypes)
+            set_of_var.to_sorted_list(WaitAfterVars),
+            !Goal, !VarSet, !VarTypes)
     ;
         true
     ).
 
 :- pred sync_dep_par_conjunct(module_info::in, bool::in,
-    set(prog_var)::in, future_map::in, hlds_goal::in, hlds_goal::out,
+    set_of_progvar::in, future_map::in, hlds_goal::in, hlds_goal::out,
     instmap::in, instmap::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out) is det.
 
 sync_dep_par_conjunct(ModuleInfo, AllowSomePathsOnly, SharedVars, FutureMap,
         !Goal, !InstMap, !VarSet, !VarTypes) :-
     Nonlocals = goal_get_nonlocals(!.Goal),
-    set.intersect(Nonlocals, SharedVars, NonlocalSharedVars),
-    ( set.empty(NonlocalSharedVars) ->
+    set_of_var.intersect(Nonlocals, SharedVars, NonlocalSharedVars),
+    ( set_of_var.is_empty(NonlocalSharedVars) ->
         true
     ;
         GoalInfo0 = !.Goal ^ hlds_goal_info,
@@ -636,7 +639,7 @@
     % conjunct, and those that are produced by it.
     %
 :- pred consumed_and_produced_vars(module_info::in, instmap::in,
-    instmap_delta::in, set(prog_var)::in,
+    instmap_delta::in, set_of_progvar::in,
     list(prog_var)::out, list(prog_var)::out) is det.
 
 consumed_and_produced_vars(ModuleInfo, InstMap, InstMapDelta, Vars,
@@ -646,9 +649,9 @@
     % is nothing useful we can do if it isn't.
     IsProducedVar = var_is_bound_in_instmap_delta(ModuleInfo, InstMap,
         InstMapDelta),
-    set.divide(IsProducedVar, Vars, ProducedVars, ConsumedVars),
-    ConsumedVarsList = set.to_sorted_list(ConsumedVars),
-    ProducedVarsList = set.to_sorted_list(ProducedVars).
+    set_of_var.divide(IsProducedVar, Vars, ProducedVars, ConsumedVars),
+    ConsumedVarsList = set_of_var.to_sorted_list(ConsumedVars),
+    ProducedVarsList = set_of_var.to_sorted_list(ProducedVars).
 
 :- pred insert_wait_in_goal_for_proc(module_info::in, bool::in, future_map::in,
     prog_var::in, hlds_goal::in, hlds_goal::out,
@@ -1230,7 +1233,7 @@
         Goal0 = hlds_goal(_, GoalInfo0),
         InstMapDelta = goal_info_get_instmap_delta(GoalInfo0),
         instmap_delta_changed_vars(InstMapDelta, ChangedVars),
-        expect(set.contains(ChangedVars, ProducedVar), $module, $pred,
+        expect(set_of_var.contains(ChangedVars, ProducedVar), $module, $pred,
             "ProducedVar not in ChangedVars"),
         insert_signal_in_goal(ModuleInfo, FutureMap, ProducedVar,
             Goal0, Goal1, !VarSet, !VarTypes),
@@ -1260,7 +1263,7 @@
         Goal0 = hlds_goal(_, GoalInfo0),
         InstMapDelta = goal_info_get_instmap_delta(GoalInfo0),
         instmap_delta_changed_vars(InstMapDelta, ChangedVars),
-        expect(set.contains(ChangedVars, ProducedVar), $module, $pred,
+        expect(set_of_var.contains(ChangedVars, ProducedVar), $module, $pred,
             "ProducedVar not in ChangedVars"),
         insert_signal_in_goal(ModuleInfo, FutureMap, ProducedVar,
             Goal0, Goal, !VarSet, !VarTypes),
@@ -1643,7 +1646,7 @@
         module_info_get_globals(InitialModuleInfo, Globals),
         globals.lookup_bool_option(Globals, allow_some_paths_only_waits,
             AllowSomePathsOnly),
-        SharedVars = set.from_list(map.keys(FutureMap)),
+        SharedVars = set_of_var.sorted_list_to_set(map.keys(FutureMap)),
         sync_dep_par_proc_body(!.ModuleInfo, AllowSomePathsOnly, SharedVars,
             FutureMap, InstMap0, Goal0, Goal, !VarSet, !VarTypes),
 
@@ -1672,7 +1675,7 @@
         % Look for and process any dependent parallel conjunctions inside
         % the newly created (sort of; the previous version was only a
         % placeholder) specialized procedure.
-        IgnoreVars = set.from_list(map.keys(FutureMap)),
+        IgnoreVars = set_of_var.sorted_list_to_set(map.keys(FutureMap)),
         sync_dep_par_conjs_in_proc(NewPredId, NewProcId, IgnoreVars,
             !ModuleInfo, [], _ProcsToScan, !TSStringTable),
         find_specialization_requests_in_proc(DoneParProcs, InitialModuleInfo,
@@ -2419,7 +2422,7 @@
     % this entire code useless.
     (
         GoalExpr = unify(_, _, _, _, _),
-        ( set.member(Var, NonLocals) ->
+        ( set_of_var.member(NonLocals, Var) ->
             Wait = seen_wait_negligible_cost_before
         ;
             Wait = not_seen_wait_negligible_cost_so_far
@@ -2428,7 +2431,7 @@
         GoalExpr = plain_call(_, _, _, BuiltinStatus, _, _),
         (
             BuiltinStatus = inline_builtin,
-            ( set.member(Var, NonLocals) ->
+            ( set_of_var.member(NonLocals, Var) ->
                 Wait = seen_wait_negligible_cost_before
             ;
                 Wait = not_seen_wait_negligible_cost_so_far
@@ -2437,7 +2440,7 @@
             ( BuiltinStatus = not_builtin
             ; BuiltinStatus = out_of_line_builtin
             ),
-            ( set.member(Var, NonLocals) ->
+            ( set_of_var.member(NonLocals, Var) ->
                 Wait = seen_wait_non_negligible_cost_before
             ;
                 Wait = not_seen_wait_non_negligible_cost_so_far
@@ -2447,7 +2450,7 @@
         ( GoalExpr = generic_call(_, _, _, _)
         ; GoalExpr = call_foreign_proc(_, _, _, _, _, _, _)
         ),
-        ( set.member(Var, NonLocals) ->
+        ( set_of_var.member(NonLocals, Var) ->
             Wait = seen_wait_non_negligible_cost_before
         ;
             Wait = not_seen_wait_non_negligible_cost_so_far
@@ -2710,19 +2713,19 @@
         % signals, rendering this entire code useless.
         (
             GoalExpr = unify(_, _, _, _, _),
-            ( set.member(Var, NonLocals) ->
+            ( set_of_var.member(NonLocals, Var) ->
                 seen_produced_var(!Signal)
             ;
                 true
             )
         ;
             % With generic calls, the only safe assumption is that they produce
-            % Var just before return. With foreign code, the signal is done after
-            % the return to Mercury execution.
+            % Var just before return. With foreign code, the signal is done
+            % after the return to Mercury execution.
             ( GoalExpr = generic_call(_, _, _, _)
             ; GoalExpr = call_foreign_proc(_, _, _, _, _, _, _)
             ),
-            ( set.member(Var, NonLocals) ->
+            ( set_of_var.member(NonLocals, Var) ->
                 seen_produced_var(!Signal)
             ;
                 seen_nontrivial_cost(!Signal)
@@ -2732,7 +2735,7 @@
             % XXX We should invoke should_we_push recursively on the called
             % procedure, though that would require safeguards against infinite
             % recursion.
-            ( set.member(Var, NonLocals) ->
+            ( set_of_var.member(NonLocals, Var) ->
                 seen_produced_var(!Signal)
             ;
                 seen_nontrivial_cost(!Signal)
@@ -3123,8 +3126,8 @@
             construct_statically, cell_is_unique, no_construct_sub_info),
         unify_context(umc_implicit("dep_par_conj transformation"), [])),
     InstmapDelta = instmap_delta_from_assoc_list([FutureNameVar - Ground]),
-    goal_info_init(set([FutureNameVar]), InstmapDelta, detism_det, purity_pure,
-        GoalInfo),
+    goal_info_init(set_of_var.make_singleton(FutureNameVar), InstmapDelta,
+        detism_det, purity_pure, GoalInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
 :- pred make_wait_goal(module_info::in, vartypes::in,
@@ -3340,18 +3343,18 @@
 find_shared_variables(ModuleInfo, InstMap, Goals) = SharedVars :-
     list.map2(get_nonlocals_and_instmaps, Goals, Nonlocals, InstMapDeltas),
     find_shared_variables_2(ModuleInfo, 0, Nonlocals, InstMap, InstMapDeltas,
-        set.init, SharedVars).
+        set_of_var.init, SharedVars).
 
 :- pred get_nonlocals_and_instmaps(hlds_goal::in,
-    set(prog_var)::out, instmap_delta::out) is det.
+    set_of_progvar::out, instmap_delta::out) is det.
 
 get_nonlocals_and_instmaps(hlds_goal(_, GoalInfo), Nonlocals, InstMapDelta) :-
     Nonlocals = goal_info_get_nonlocals(GoalInfo),
     InstMapDelta = goal_info_get_instmap_delta(GoalInfo).
 
 :- pred find_shared_variables_2(module_info::in, int::in,
-    list(set(prog_var))::in, instmap::in, list(instmap_delta)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    list(set_of_progvar)::in, instmap::in, list(instmap_delta)::in,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 find_shared_variables_2(_ModuleInfo, _ConjunctIndex,
         [], _InstMap, _InstMapDeltas, !SharedVars).
@@ -3364,10 +3367,11 @@
         instmap_lookup_var(InstMap, Var, VarInst),
         not inst_is_bound(ModuleInfo, VarInst)
     ),
-    UnboundNonlocals = set.filter(Filter, Nonlocals),
+    UnboundNonlocals = set_of_var.filter(Filter, Nonlocals),
     Changed =
-        set.filter(changed_var(ModuleInfo, InstMapDeltasB), UnboundNonlocals),
-    set.union(Changed, !SharedVars),
+        set_of_var.filter(changed_var(ModuleInfo, InstMapDeltasB),
+            UnboundNonlocals),
+    set_of_var.union(Changed, !SharedVars),
     find_shared_variables_2(ModuleInfo, ConjunctIndex+1, MoreNonlocals,
         InstMap, InstMapDeltas, !SharedVars).
 
@@ -3402,7 +3406,7 @@
 :- pred var_in_nonlocals(prog_var::in, hlds_goal::in) is semidet.
 
 var_in_nonlocals(Var, Goal) :-
-    set.member(Var, goal_get_nonlocals(Goal)).
+    set_of_var.member(goal_get_nonlocals(Goal), Var).
 
 :- pred var_not_in_nonlocals(prog_var::in, hlds_goal::in) is semidet.
 
Index: compiler/det_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_analysis.m,v
retrieving revision 1.231
diff -u -b -r1.231 det_analysis.m
--- compiler/det_analysis.m	15 Jun 2011 03:35:40 -0000	1.231
+++ compiler/det_analysis.m	15 Aug 2011 08:51:06 -0000
@@ -126,6 +126,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -1583,9 +1584,10 @@
                             [always(NestedOuterPieces)])])
                     ]),
                 det_info_add_error_spec(NestedSpec, !DetInfo),
-                AllVars = set.union(list_to_set(OuterVars), list_to_set(Vars)),
+                AllVars = set_of_var.list_to_set(OuterVars ++ Vars),
                 MaybePromiseEqvSolutionSets =
-                    yes(pess_info(to_sorted_list(AllVars), OuterContext))
+                    yes(pess_info(set_of_var.to_sorted_list(AllVars),
+                        OuterContext))
             )
         ;
             Kind = equivalent_solution_sets_arbitrary,
@@ -1600,14 +1602,15 @@
             ;
                 MaybePromiseEqvSolutionSets0 = yes(pess_info(OldVars,
                     PromiseContext)),
-                OverlapVars = set.intersect(list_to_set(OldVars),
-                    list_to_set(Vars)),
-                ( set.empty(OverlapVars) ->
+                OverlapVars = set_of_var.intersect(
+                    set_of_var.list_to_set(OldVars),
+                    set_of_var.list_to_set(Vars)),
+                ( set_of_var.is_empty(OverlapVars) ->
                     true
                 ;
                     OverlapVarNames = list.map(
                         lookup_var_name_in_varset(VarSet),
-                        set.to_sorted_list(OverlapVars)),
+                        set_of_var.to_sorted_list(OverlapVars)),
                     (
                         OverlapVarNames = [],
                         unexpected($module, $pred,
@@ -1643,23 +1646,25 @@
         det_info_get_module_info(!.DetInfo, ModuleInfo),
         % BoundVars must include both vars whose inst has changed and vars
         % with inst any which may have been further constrained by the goal.
-        set.divide(var_is_ground_in_instmap(ModuleInfo, InstMap0),
+        set_of_var.divide(var_is_ground_in_instmap(ModuleInfo, InstMap0),
             ChangedVars, _GroundAtStartVars, GroundBoundVars),
         NonLocalVars = goal_info_get_nonlocals(GoalInfo),
-        AnyBoundVars = set.filter(var_is_any_in_instmap(ModuleInfo, InstMap0),
+        AnyBoundVars = set_of_var.filter(
+            var_is_any_in_instmap(ModuleInfo, InstMap0),
             NonLocalVars),
-        BoundVars0 = set.union(GroundBoundVars, AnyBoundVars),
+        BoundVars0 = set_of_var.union(GroundBoundVars, AnyBoundVars),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        BoundVars = remove_typeinfo_vars_from_set(VarTypes, BoundVars0),
+        BoundVars = remove_typeinfo_vars_from_set_of_var(VarTypes, BoundVars0),
 
         % Which vars were bound inside the scope but not listed
         % in the promise_equivalent_solution{s,_sets} or arbitrary scope?
-        set.difference(BoundVars, set.list_to_set(Vars), MissingVars),
-        ( set.empty(MissingVars) ->
+        set_of_var.difference(BoundVars, set_of_var.list_to_set(Vars),
+            MissingVars),
+        ( set_of_var.is_empty(MissingVars) ->
             true
         ;
             MissingVarNames = list.map(lookup_var_name_in_varset(VarSet),
-                set.to_sorted_list(MissingVars)),
+                set_of_var.to_sorted_list(MissingVars)),
             MissingKindStr = promise_solutions_kind_str(Kind),
             (
                 MissingVarNames = [],
@@ -1673,8 +1678,8 @@
                 MissingListStr = "some variables that are not listed:"
             ),
             (
-                set.member(MissingVar, MissingVars),
-                set.member(MissingVar, AnyBoundVars)
+                set_of_var.member(MissingVars, MissingVar),
+                set_of_var.member(AnyBoundVars, MissingVar)
             ->
                 BindsWords = "goal may constrain"
             ;
@@ -1689,17 +1694,18 @@
         ),
         % Which vars were listed in the promise_equivalent_solutions
         % but not bound inside the scope?
-        set.difference(set.list_to_set(Vars), BoundVars, ExtraVars),
+        set_of_var.difference(set_of_var.list_to_set(Vars),
+            BoundVars, ExtraVars),
         det_info_get_pess_extra_vars(!.DetInfo, IgnoreExtraVars),
         (
-            ( set.empty(ExtraVars)
+            ( set_of_var.is_empty(ExtraVars)
             ; IgnoreExtraVars = pess_extra_vars_ignore
             )
         ->
             true
         ;
             ExtraVarNames = list.map(lookup_var_name_in_varset(VarSet),
-                set.to_sorted_list(ExtraVars)),
+                set_of_var.to_sorted_list(ExtraVars)),
             ExtraKindStr = promise_solutions_kind_str(Kind),
             (
                 ExtraVarNames = [],
Index: compiler/det_report.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_report.m,v
retrieving revision 1.155
diff -u -b -r1.155 det_report.m
--- compiler/det_report.m	8 Apr 2011 07:25:44 -0000	1.155
+++ compiler/det_report.m	4 Aug 2011 02:27:26 -0000
@@ -154,6 +154,7 @@
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -163,7 +164,6 @@
 :- import_module maybe.
 :- import_module pair.
 :- import_module require.
-:- import_module set.
 :- import_module set_tree234.
 :- import_module solutions.
 :- import_module string.
@@ -908,10 +908,10 @@
         OtherConsIds = []
     ->
         NonLocals = goal_info_get_nonlocals(GoalInfo),
-        set.list_to_set(ArgVars, ArgVarsSet),
+        ArgVarsSet = set_of_var.list_to_set(ArgVars),
         (
-            set.intersect(NonLocals, ArgVarsSet, NonLocalArgVarsSet),
-            set.non_empty(NonLocalArgVarsSet)
+            set_of_var.intersect(NonLocals, ArgVarsSet, NonLocalArgVarsSet),
+            set_of_var.is_non_empty(NonLocalArgVarsSet)
         ->
             MaybeArgVars = yes(ArgVars)
         ;
@@ -975,10 +975,10 @@
         find_switch_var_submatches(Disjuncts, SwitchVarSynonyms,
             MaybeMainConsId, LeftOverConsIds, MaybeMainMatch, LeftOverMatches),
         NonLocals = goal_info_get_nonlocals(GoalInfo),
-        set.list_to_set(ArgVars, ArgVarsSet),
+        set_of_var.list_to_set(ArgVars, ArgVarsSet),
         (
-            set.intersect(NonLocals, ArgVarsSet, NonLocalArgVarsSet),
-            set.non_empty(NonLocalArgVarsSet)
+            set_of_var.intersect(NonLocals, ArgVarsSet, NonLocalArgVarsSet),
+            set_of_var.is_non_empty(NonLocalArgVarsSet)
         ->
             MaybeArgVars = yes(ArgVars)
         ;
Index: compiler/det_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_util.m,v
retrieving revision 1.54
diff -u -b -r1.54 det_util.m
--- compiler/det_util.m	2 Jan 2011 14:37:53 -0000	1.54
+++ compiler/det_util.m	4 Aug 2011 02:25:17 -0000
@@ -27,10 +27,10 @@
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -98,7 +98,7 @@
 :- pred det_lookup_var_type(module_info::in, proc_info::in, prog_var::in,
     hlds_type_defn::out) is semidet.
 
-:- pred det_no_output_vars(set(prog_var)::in, instmap::in, instmap_delta::in,
+:- pred det_no_output_vars(set_of_progvar::in, instmap::in, instmap_delta::in,
     det_info::in) is semidet.
 
 :- pred det_info_add_error_spec(error_spec::in, det_info::in, det_info::out)
Index: compiler/disj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/disj_gen.m,v
retrieving revision 1.119
diff -u -b -r1.119 disj_gen.m
--- compiler/disj_gen.m	21 Jul 2011 06:58:24 -0000	1.119
+++ compiler/disj_gen.m	15 Aug 2011 15:38:52 -0000
@@ -418,7 +418,8 @@
                 % We only need region support for backtracking if some disjunct
                 % performs some region operations (allocation or removal).
                 maybe_create_disj_region_frame_semi(AddRegionOps,
-                    DisjRemovedRegionVars, DisjAllocRegionVars,
+                    set_of_var.set_to_bitset(DisjRemovedRegionVars),
+                    set_of_var.set_to_bitset(DisjAllocRegionVars),
                     BeforeEnterRegionCode, LaterRegionCode, LastRegionCode,
                     RegionStackVars, RegionCommitDisjCleanup, !CI),
                 RegionStackVarsToRelease = RegionStackVars
@@ -769,7 +770,7 @@
     ).
 
 :- pred maybe_create_disj_region_frame_semi(add_region_ops::in,
-    set(prog_var)::in, set(prog_var)::in, llds_code::out, llds_code::out,
+    set_of_progvar::in, set_of_progvar::in, llds_code::out, llds_code::out,
     llds_code::out, list(lval)::out, commit_disj_region_cleanup::out,
     code_info::in, code_info::out) is det.
 
@@ -801,8 +802,8 @@
         %
         SnapshotRegionVars = DisjAllocRegionVars,
 
-        ProtectRegionVarList = set.to_sorted_list(ProtectRegionVars),
-        SnapshotRegionVarList = set.to_sorted_list(SnapshotRegionVars),
+        ProtectRegionVarList = set_of_var.to_sorted_list(ProtectRegionVars),
+        SnapshotRegionVarList = set_of_var.to_sorted_list(SnapshotRegionVars),
 
         list.length(ProtectRegionVarList, NumProtectRegionVars),
         list.length(SnapshotRegionVarList, NumSnapshotRegionVars),
Index: compiler/distance_granularity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/distance_granularity.m,v
retrieving revision 1.16
diff -u -b -r1.16 distance_granularity.m
--- compiler/distance_granularity.m	23 May 2011 05:08:01 -0000	1.16
+++ compiler/distance_granularity.m	15 Aug 2011 11:06:11 -0000
@@ -155,6 +155,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module int.
@@ -519,7 +520,7 @@
         % The non-locals of the hlds_goal_info of the if_then_else goal must
         % contain the variable controlling the granularity.
         NonLocals0 = goal_info_get_nonlocals(ConjInfo),
-        set.insert(GranularityVar, NonLocals0, NonLocals),
+        set_of_var.insert(GranularityVar, NonLocals0, NonLocals),
         goal_info_set_nonlocals(NonLocals, ConjInfo, IfThenElseInfo),
         IfThenElseGoal = hlds_goal(if_then_else([], Cond, Then, Else),
             IfThenElseInfo)
@@ -701,7 +702,7 @@
                     DecrementGoalExpr = plain_call(MinusPredId, MinusProcId,
                         MinusCallArgs, MinusCallBuiltin, MinusCallUnifyContext,
                         MinusCallSymName),
-                    set.list_to_set([GranularityVar, Var, VarResult],
+                    set_of_var.list_to_set([GranularityVar, Var, VarResult],
                         NonLocals),
                     VarResultDelta = VarResult - ground(unique, none),
                     VarDelta = Var - bound(shared, [bound_functor(int_const(1),
@@ -982,7 +983,7 @@
         % Update the nonlocals and the instmap_delta of the hlds_goal_info
         % of the recursive plain call for Var.
         NonLocals0 = goal_info_get_nonlocals(CallInfo0),
-        set.insert(Var, NonLocals0, NonLocals),
+        set_of_var.insert(Var, NonLocals0, NonLocals),
         goal_info_set_nonlocals(NonLocals, CallInfo0, CallInfo1),
         InstMapDelta0 = goal_info_get_instmap_delta(CallInfo1),
         MerInst = ground(shared, none),
Index: compiler/erl_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_gen.m,v
retrieving revision 1.40
diff -u -b -r1.40 erl_code_gen.m
--- compiler/erl_code_gen.m	23 May 2011 05:08:02 -0000	1.40
+++ compiler/erl_code_gen.m	15 Aug 2011 11:23:45 -0000
@@ -65,6 +65,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_foreign.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module int.
@@ -349,8 +350,8 @@
 
     Goal0 = hlds_goal(GoalExpr, GoalInfo0),
     NonLocals0 = goal_info_get_code_gen_nonlocals(GoalInfo0),
-    set.list_to_set(HeadVars, HeadVarsSet),
-    set.intersect(HeadVarsSet, NonLocals0, NonLocals),
+    set_of_var.list_to_set(HeadVars, HeadVarsSet),
+    set_of_var.intersect(HeadVarsSet, NonLocals0, NonLocals),
     goal_info_set_code_gen_nonlocals(NonLocals, GoalInfo0, GoalInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo),
 
@@ -586,7 +587,7 @@
         GoalStatement, PackedNonLocals, !Info) :-
     % Find the nonlocal variables bound by the goal.
     erl_bound_nonlocals_in_goal(!.Info, InstMap, Goal, NonLocalsSet),
-    NonLocals = set.to_sorted_list(NonLocalsSet),
+    NonLocals = set_of_var.to_sorted_list(NonLocalsSet),
 
     % Throw = ``throw({'MERCURY_COMMIT', {NonLocals, ...})''
     Throw = elds_throw(elds_term(ThrowValue)),
@@ -848,7 +849,7 @@
         maybe_join_exprs1(MaybeConvertToAtom, CaseExpr)).
 
 :- pred erl_gen_case(mer_type::in,
-    code_model::in, instmap::in, set(prog_var)::in,
+    code_model::in, instmap::in, set_of_progvar::in,
     maybe(elds_expr)::in, hlds_goal.case::in, elds_case::out,
     erl_gen_info::in, erl_gen_info::out) is det.
 
@@ -906,7 +907,7 @@
         Size = 0
     ).
 
-:- pred erl_gen_case_on_atom(code_model::in, instmap::in, set(prog_var)::in,
+:- pred erl_gen_case_on_atom(code_model::in, instmap::in, set_of_progvar::in,
     maybe(elds_expr)::in, hlds_goal.case::in, elds_case::out,
     erl_gen_info::in, erl_gen_info::out) is det.
 
@@ -939,19 +940,19 @@
 %
 
 :- pred union_bound_nonlocals_in_goals(erl_gen_info::in, instmap::in,
-    hlds_goals::in, set(prog_var)::out) is det.
+    hlds_goals::in, set_of_progvar::out) is det.
 
 union_bound_nonlocals_in_goals(Info, InstMap, Goals, NonLocalsUnion) :-
     IsBound = erl_bound_nonlocals_in_goal(Info, InstMap),
     list.map(IsBound, Goals, NonLocalsLists),
-    NonLocalsUnion = set.union_list(NonLocalsLists).
+    NonLocalsUnion = set_of_var.union_list(NonLocalsLists).
 
     % If a success expression is too large to duplicate but is required after
     % two or more goals Gs, we generate a closure C containing the success
     % expression which takes the nonlocal variables bound by Gs as arguments.
     % Then we generate the code for Gs such that they call C on success.
     %
-:- pred maybe_create_closure_for_success_expr(set(prog_var)::in,
+:- pred maybe_create_closure_for_success_expr(set_of_progvar::in,
     maybe(elds_expr)::in, maybe(elds_expr)::out, maybe(elds_expr)::out,
     instmap::in, instmap::out, erl_gen_info::in, erl_gen_info::out) is det.
 
@@ -964,7 +965,7 @@
         erl_gen_info_new_named_var("SuccessClosure", ClosureVar, !Info),
         ground_var_in_instmap(ClosureVar, InstMap0, InstMap),
         ClosureVarExpr = expr_from_var(ClosureVar),
-        ClosureArgs0 = set.to_sorted_list(NonLocals),
+        ClosureArgs0 = set_of_var.to_sorted_list(NonLocals),
 
         % Ignore dummy variables.
         erl_gen_info_get_module_info(!.Info, ModuleInfo),
@@ -1063,7 +1064,7 @@
         update_instmap(Cond, InstMap0, InstMap0PostCond),
         erl_bound_nonlocals_in_goal(!.Info, InstMap0PostCond, Then, ThenVars),
         erl_bound_nonlocals_in_goal(!.Info, InstMap0, Else, ElseVars),
-        CondVarsList = set.to_sorted_list(CondVars),
+        CondVarsList = set_of_var.to_sorted_list(CondVars),
 
         % Generate the condition goal, making it evaluate to a tuple of the
         % non-local variables that it binds on success.
@@ -1078,7 +1079,7 @@
         % Create a closure for the success expression if it is too large to
         % duplicate into the branches.
         % (InstMap1 = InstMap0 + optionally a variable bound to a closure)
-        BoundNonLocals = set.union(ThenVars, ElseVars),
+        BoundNonLocals = set_of_var.union(ThenVars, ElseVars),
         maybe_create_closure_for_success_expr(BoundNonLocals,
             MaybeSuccessExpr0, MaybeMakeClosure, MaybeSuccessExpr,
             InstMap0, InstMap1, !Info),
@@ -1336,7 +1337,7 @@
 
             % Find the variables bound by First.
             erl_bound_nonlocals_in_goal(!.Info, InstMap0, First, NonLocalsSet),
-            NonLocals = set.to_sorted_list(NonLocalsSet),
+            NonLocals = set_of_var.to_sorted_list(NonLocalsSet),
 
             % Make the success continuation.  Rename apart any variables bound
             % by First to avoid warnings about the closure shadowing variables.
@@ -1470,7 +1471,7 @@
             FirstCodeModel = model_semi,
 
             erl_bound_nonlocals_in_goal(!.Info, InstMap, First, FirstVarsSet),
-            FirstVars = set.to_sorted_list(FirstVarsSet),
+            FirstVars = set_of_var.to_sorted_list(FirstVarsSet),
             FirstVarsTerm = elds_tuple(exprs_from_vars(FirstVars)),
 
             % Generate code for the first goal, making it return a tuple of the
@@ -1540,7 +1541,7 @@
             FirstStatement0, !Info),
 
         erl_bound_nonlocals_in_goal(!.Info, InstMap, First, FirstVarsSet),
-        FirstVars = set.to_sorted_list(FirstVarsSet),
+        FirstVars = set_of_var.to_sorted_list(FirstVarsSet),
         erl_create_renaming(FirstVars, Subst, !Info),
         erl_rename_vars_in_expr(Subst, FirstStatement0, FirstStatement),
 
Index: compiler/erl_code_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_util.m,v
retrieving revision 1.25
diff -u -b -r1.25 erl_code_util.m
--- compiler/erl_code_util.m	23 May 2011 05:08:02 -0000	1.25
+++ compiler/erl_code_util.m	15 Aug 2011 11:21:44 -0000
@@ -22,6 +22,7 @@
 :- import_module hlds.hlds_pred.
 :- import_module hlds.instmap.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module list.
 :- import_module maybe.
@@ -131,7 +132,7 @@
     % by that goal.
     %
 :- pred erl_bound_nonlocals_in_goal(erl_gen_info::in, instmap::in,
-    hlds_goal::in, set(prog_var)::out) is det.
+    hlds_goal::in, set_of_progvar::out) is det.
 
     % erl_bind_unbound_vars(Info, VarsToBind, Goal, InstMap, !Statement)
     %
@@ -145,7 +146,7 @@
     %
     % VarsToBind must not include dummy variables.
     %
-:- pred erl_bind_unbound_vars(erl_gen_info::in, set(prog_var)::in,
+:- pred erl_bind_unbound_vars(erl_gen_info::in, set_of_progvar::in,
     hlds_goal::in, instmap::in, elds_expr::in, elds_expr::out) is det.
 
     % erl_var_or_dummy_replacement(ModuleInfo, VarTypes, DummyRepl, Var) = Expr
@@ -177,14 +178,14 @@
     % Rename all variables in Expr0 to fresh variables, except for the
     % variables in the set KeepVars.
     %
-:- pred erl_rename_vars_in_expr_except(set(prog_var)::in,
+:- pred erl_rename_vars_in_expr_except(set_of_progvar::in,
     elds_expr::in, elds_expr::out, erl_gen_info::in, erl_gen_info::out) is det.
 
     % erl_expr_vars(Expr, Vars)
     %
     % Vars is the set of variables appearing in Expr.
     %
-:- pred erl_expr_vars(elds_expr::in, set(prog_var)::out) is det.
+:- pred erl_expr_vars(elds_expr::in, set_of_progvar::out) is det.
 
     % Return a rough indication of the "size" of an expression, where each
     % simple constant has a value of 1.  This is used to decide if an
@@ -240,8 +241,8 @@
 
                 % input_vars and output_vars do not include variables of dummy
                 % types.
-                egi_input_vars          :: prog_vars,
-                egi_output_vars         :: prog_vars,
+                egi_input_vars          :: list(prog_var),
+                egi_output_vars         :: list(prog_var),
 
                 % Set of environment variables used by this procedure.
                 egi_env_var_names       :: set(string)
@@ -398,8 +399,9 @@
     Goal = hlds_goal(_, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
     InstmapDelta = goal_info_get_instmap_delta(GoalInfo),
-    BoundNonLocals = set.filter(is_bound_and_not_dummy(ModuleInfo, VarTypes,
-        InstMap, InstmapDelta), NonLocals).
+    BoundNonLocals = set_of_var.filter(
+        is_bound_and_not_dummy(ModuleInfo, VarTypes, InstMap, InstmapDelta),
+        NonLocals).
 
 :- pred is_bound_and_not_dummy(module_info::in, vartypes::in, instmap::in,
     instmap_delta::in, prog_var::in) is semidet.
@@ -412,12 +414,12 @@
 erl_bind_unbound_vars(Info, VarsToBind, Goal, InstMap,
         Statement0, Statement) :-
     erl_bound_nonlocals_in_goal(Info, InstMap, Goal, Bound),
-    NotBound = set.difference(VarsToBind, Bound),
-    (if set.empty(NotBound) then
+    NotBound = set_of_var.difference(VarsToBind, Bound),
+    ( if set_of_var.is_empty(NotBound) then
         Statement = Statement0
     else
         % We arbitrarily assign all the variables to the atom `false'.
-        NotBoundList = set.to_sorted_list(NotBound),
+        NotBoundList = set_of_var.to_sorted_list(NotBound),
         Assignments = list.map(var_eq_false, NotBoundList),
         Statement = join_exprs(elds_block(Assignments), Statement0)
     ).
@@ -717,23 +719,23 @@
 
 erl_rename_vars_in_expr_except(ExceptVars, Expr0, Expr, !Info) :-
     erl_expr_vars(Expr0, Vars0),
-    Vars = set.difference(Vars0, ExceptVars),
-    erl_create_renaming(set.to_sorted_list(Vars), Subn, !Info),
+    Vars = set_of_var.difference(Vars0, ExceptVars),
+    erl_create_renaming(set_of_var.to_sorted_list(Vars), Subn, !Info),
     erl_rename_vars_in_expr(Subn, Expr0, Expr).
 
 %-----------------------------------------------------------------------------%
 
 erl_expr_vars(Expr, Set) :-
-    erl_vars_in_expr(Expr, set.init, Set).
+    erl_vars_in_expr(Expr, set_of_var.init, Set).
 
 :- pred erl_vars_in_exprs(list(elds_expr)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_exprs(Exprs, !Set) :-
     list.foldl(erl_vars_in_expr, Exprs, !Set).
 
 :- pred erl_vars_in_expr(elds_expr::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_expr(Expr, !Set) :-
     (
@@ -797,13 +799,13 @@
     ).
 
 :- pred erl_vars_in_terms(list(elds_term)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_terms(Terms, !Set) :-
     list.foldl(erl_vars_in_term, Terms, !Set).
 
 :- pred erl_vars_in_term(elds_term::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_term(Term, !Set) :-
     (
@@ -822,11 +824,11 @@
         erl_vars_in_exprs(Exprs, !Set)
     ;
         Term = elds_var(Var),
-        set.insert(Var, !Set)
+        set_of_var.insert(Var, !Set)
     ).
 
 :- pred erl_vars_in_call_target(elds_call_target::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_call_target(Target, !Set) :-
     (
@@ -839,7 +841,7 @@
     ).
 
 :- pred erl_vars_in_clause(elds_clause::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_clause(Clause, !Set) :-
     Clause = elds_clause(Pattern, Expr),
@@ -847,13 +849,13 @@
     erl_vars_in_expr(Expr, !Set).
 
 :- pred erl_vars_in_cases(list(elds_case)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_cases(Cases, !Set) :-
     list.foldl(erl_vars_in_case, Cases, !Set).
 
 :- pred erl_vars_in_case(elds_case::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_case(Case, !Set) :-
     Case = elds_case(Pattern, Expr),
@@ -861,7 +863,7 @@
     erl_vars_in_expr(Expr, !Set).
 
 :- pred erl_vars_in_catch(elds_catch::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 erl_vars_in_catch(Catch, !Set) :-
     Catch = elds_catch(PatternA, PatternB, Expr),
Index: compiler/follow_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/follow_code.m,v
retrieving revision 1.104
diff -u -b -r1.104 follow_code.m
--- compiler/follow_code.m	23 May 2011 05:08:02 -0000	1.104
+++ compiler/follow_code.m	15 Aug 2011 13:24:10 -0000
@@ -52,6 +52,7 @@
 :- import_module hlds.instmap.
 :- import_module hlds.quantification.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
@@ -243,7 +244,7 @@
     Goal = hlds_goal(_, GoalInfo),
     InstMapDelta = goal_info_get_instmap_delta(GoalInfo),
     instmap_delta_changed_vars(InstMapDelta, ChangedVars),
-    set.empty(ChangedVars),
+    set_of_var.is_empty(ChangedVars),
     no_bind_vars(Goals).
 
 %-----------------------------------------------------------------------------%
Index: compiler/format_call.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/format_call.m,v
retrieving revision 1.27
diff -u -b -r1.27 format_call.m
--- compiler/format_call.m	23 May 2011 05:08:02 -0000	1.27
+++ compiler/format_call.m	15 Aug 2011 08:30:44 -0000
@@ -146,6 +146,7 @@
 :- import_module libs.options.
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.builtin_lib_types.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module char.
@@ -156,7 +157,6 @@
 :- import_module pair.
 :- import_module require.
 :- import_module set.
-:- import_module set_tree234.
 :- import_module string.
 :- import_module term.
 :- import_module univ.
@@ -309,7 +309,7 @@
     fill_goal_id_slots_in_proc_body(ModuleInfo, !.VarTypes, _, Goal0, Goal1),
     format_call_traverse_goal(ModuleInfo, Goal1, _, [], FormatCallSites,
         Counter0, _Counter, ConjMaps0, ConjMaps, map.init, PredMap,
-        set_tree234.init, _),
+        set_of_var.init, _),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, optimize_format_calls, OptFormatCalls),
     list.foldl4(
@@ -325,11 +325,9 @@
         Goal1 = hlds_goal(_, GoalInfo1),
         InstMapDelta = goal_info_get_instmap_delta(GoalInfo1),
         instmap_delta_changed_vars(InstMapDelta, NeededVars0),
-        ToDeleteVars0 = set_tree234.init,
-        NeededVarsSet = set_tree234.sorted_list_to_set(
-            set.to_sorted_list(NeededVars0)),
+        ToDeleteVars0 = set_of_var.init,
         opt_format_call_sites_in_goal(Goal1, Goal, GoalIdMap, _,
-            NeededVarsSet, _NeededVars, ToDeleteVars0, _ToDeleteVars),
+            NeededVars0, _NeededVars, ToDeleteVars0, _ToDeleteVars),
         MaybeGoal = yes(Goal)
     ).
 
@@ -469,7 +467,7 @@
         ->
             AllToDeleteVars = [StringVar | ValuesToDeleteVars],
             FCOptGoalInfo = fc_opt_goal_info(ReplacementGoal,
-                set_tree234.list_to_set(AllToDeleteVars)),
+                set_of_var.list_to_set(AllToDeleteVars)),
             map.det_insert(GoalId, FCOptGoalInfo, !GoalIdMap)
         ;
             % create_string_format_replacement does not (yet) recognize
@@ -492,7 +490,7 @@
         ->
             AllToDeleteVars = [StringVar | ValuesToDeleteVars],
             FCOptGoalInfo = fc_opt_goal_info(ReplacementGoal,
-                set_tree234.list_to_set(AllToDeleteVars)),
+                set_of_var.list_to_set(AllToDeleteVars)),
             map.det_insert(GoalId, FCOptGoalInfo, !GoalIdMap)
         ;
             % create_string_format_replacement does not (yet) recognize
@@ -599,7 +597,7 @@
     list(format_call_site)::in, list(format_call_site)::out,
     counter::in, counter::out, conj_maps::in, conj_maps::out,
     conj_pred_map::in, conj_pred_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 format_call_traverse_goal(ModuleInfo, Goal, CurId, !FormatCallSites, !Counter,
         !ConjMaps, !PredMap, !RelevantVars) :-
@@ -612,7 +610,7 @@
     conj_id::in, list(format_call_site)::in, list(format_call_site)::out,
     counter::in, counter::out, conj_maps::in, conj_maps::out,
     conj_pred_map::in, conj_pred_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 format_call_traverse_conj(_ModuleInfo, [], _CurId, !FormatCallSites, !Counter,
         !ConjMaps, !PredMap, !RelevantVars).
@@ -684,7 +682,7 @@
             FormatCallSite = format_call_site(GoalId, StringVar, ValuesVar,
                 Kind, ModuleName, Name, Arity, Context, CurId),
             !:FormatCallSites = [FormatCallSite | !.FormatCallSites],
-            set_tree234.insert_list([StringVar, ValuesVar], !RelevantVars)
+            set_of_var.insert_list([StringVar, ValuesVar], !RelevantVars)
         ;
             true
         )
@@ -727,15 +725,15 @@
 
 :- pred format_call_traverse_unify(unification::in, conj_id::in,
     conj_maps::in, conj_maps::out, conj_pred_map::in, conj_pred_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 format_call_traverse_unify(Unification, CurId, !ConjMaps, !PredMap,
         !RelevantVars) :-
     (
         Unification = assign(TargetVar, SourceVar),
-        ( set_tree234.contains(!.RelevantVars, TargetVar) ->
-            set_tree234.delete(TargetVar, !RelevantVars),
-            set_tree234.insert(SourceVar, !RelevantVars),
+        ( set_of_var.member(!.RelevantVars, TargetVar) ->
+            set_of_var.delete(TargetVar, !RelevantVars),
+            set_of_var.insert(SourceVar, !RelevantVars),
             ConjMap0 = get_conj_map(!.ConjMaps, CurId),
             ConjMap0 = conj_map(StringMap, ListMap, ElementMap, EqvMap0),
             map.det_insert(TargetVar, SourceVar, EqvMap0, EqvMap),
@@ -746,13 +744,13 @@
         )
     ;
         Unification = construct(CellVar, ConsId, ArgVars, _, _, _, _),
-        ( set_tree234.contains(!.RelevantVars, CellVar) ->
+        ( set_of_var.member(!.RelevantVars, CellVar) ->
             ConjMap0 = get_conj_map(!.ConjMaps, CurId),
             ConjMap0 = conj_map(StringMap0, ListMap0, ElementMap0, EqvMap0),
             (
                 ConsId = string_const(StringConst)
             ->
-                set_tree234.delete(CellVar, !RelevantVars),
+                set_of_var.delete(CellVar, !RelevantVars),
                 map.det_insert(CellVar, StringConst, StringMap0, StringMap),
                 ConjMap = conj_map(StringMap, ListMap0, ElementMap0, EqvMap0)
             ;
@@ -771,8 +769,8 @@
                     List = list_skeleton_nil
                 )
             ->
-                set_tree234.delete(CellVar, !RelevantVars),
-                set_tree234.insert_list(ArgVars, !RelevantVars),
+                set_of_var.delete(CellVar, !RelevantVars),
+                set_of_var.insert_list(ArgVars, !RelevantVars),
                 map.det_insert(CellVar, List, ListMap0, ListMap),
                 ConjMap = conj_map(StringMap0, ListMap, ElementMap0, EqvMap0)
             ;
@@ -794,7 +792,7 @@
                     Dummy = c('0')
                 )
             ->
-                set_tree234.delete(CellVar, !RelevantVars),
+                set_of_var.delete(CellVar, !RelevantVars),
                 ( ArgVars = [ArgVar] ->
                     WhatToPrint = what_to_print(ArgVar, Dummy)
                 ;
@@ -824,7 +822,7 @@
     conj_id::in, list(format_call_site)::in, list(format_call_site)::out,
     counter::in, counter::out, conj_maps::in, conj_maps::out,
     conj_pred_map::in, conj_pred_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 format_call_traverse_disj(ModuleInfo, Disjuncts, CurId, !FormatCallSites,
         !Counter, !ConjMaps, !PredMap, !RelevantVars) :-
@@ -833,14 +831,14 @@
         DisjRelevantVarSets),
     list.condense(DisjFormatCallSitesLists, DisjFormatCallSites),
     !:FormatCallSites = !.FormatCallSites ++ DisjFormatCallSites,
-    DisjRelevantVars = set_tree234.union_list(DisjRelevantVarSets),
-    set_tree234.union(DisjRelevantVars, !RelevantVars).
+    DisjRelevantVars = set_of_var.union_list(DisjRelevantVarSets),
+    set_of_var.union(DisjRelevantVars, !RelevantVars).
 
 :- pred format_call_traverse_disj_arms(module_info::in, list(hlds_goal)::in,
     conj_id::in, list(list(format_call_site))::out,
     counter::in, counter::out, conj_maps::in, conj_maps::out,
     conj_pred_map::in, conj_pred_map::out,
-    list(set_tree234(prog_var))::out) is det.
+    list(set_of_progvar)::out) is det.
 
 format_call_traverse_disj_arms(_, [], _, [], !Counter, !ConjMaps, !PredMap,
         []).
@@ -849,7 +847,7 @@
         !ConjMaps, !PredMap, [GoalRelevantVars | GoalsRelevantVars]) :-
     format_call_traverse_goal(ModuleInfo, Goal, DisjId, [],
         GoalFormatCallSites, !Counter, !ConjMaps, !PredMap,
-        set_tree234.init, GoalRelevantVars),
+        set_of_var.init, GoalRelevantVars),
     map.det_insert(DisjId, ContainingId, !PredMap),
     format_call_traverse_disj_arms(ModuleInfo, Goals, ContainingId,
         GoalsFormatCallSites, !Counter, !ConjMaps, !PredMap,
@@ -872,12 +870,10 @@
 
 %-----------------------------------------------------------------------------%
 
-% XXX Consider using set_tree234s instead of plain sets.
-
 :- type fc_opt_goal_info
     --->    fc_opt_goal_info(
                 fcogi_replacement_goal  :: hlds_goal,
-                fcogi_unneeded_vars     :: set_tree234(prog_var)
+                fcogi_unneeded_vars     :: set_of_progvar
             ).
 
 :- type fc_goal_id_map == map(goal_id, fc_opt_goal_info).
@@ -887,8 +883,8 @@
     %
 :- pred opt_format_call_sites_in_goal(hlds_goal::in, hlds_goal::out,
     fc_goal_id_map::in, fc_goal_id_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 opt_format_call_sites_in_goal(Goal0, Goal, !GoalIdMap,
         !NeededVars, !ToDeleteVars) :-
@@ -899,16 +895,13 @@
         ( map.remove(GoalId, OptGoalInfo, !GoalIdMap) ->
             OptGoalInfo = fc_opt_goal_info(ReplacementGoal, GoalToDeleteVars),
             Goal = ReplacementGoal,
-            set_tree234.union(!.ToDeleteVars, GoalToDeleteVars, !:ToDeleteVars)
+            set_of_var.union(!.ToDeleteVars, GoalToDeleteVars, !:ToDeleteVars)
         ;
             Goal = Goal0,
             NonLocals = goal_info_get_nonlocals(GoalInfo),
             % Assume that all nonlocals are needed.
-            NonLocalsSet = set_tree234.sorted_list_to_set(
-                set.to_sorted_list(NonLocals)),
-            set_tree234.union(!.NeededVars, NonLocalsSet, !:NeededVars),
-            set_tree234.difference(!.ToDeleteVars, NonLocalsSet,
-                !:ToDeleteVars)
+            set_of_var.union(!.NeededVars, NonLocals, !:NeededVars),
+            set_of_var.difference(!.ToDeleteVars, NonLocals, !:ToDeleteVars)
         )
     ;
         ( GoalExpr0 = generic_call(_, _, _, _)
@@ -917,19 +910,17 @@
         Goal = Goal0,
         NonLocals = goal_info_get_nonlocals(GoalInfo),
         % Assume that all nonlocals are needed.
-        NonLocalsSet = set_tree234.sorted_list_to_set(
-            set.to_sorted_list(NonLocals)),
-        set_tree234.union(!.NeededVars, NonLocalsSet, !:NeededVars),
-        set_tree234.difference(!.ToDeleteVars, NonLocalsSet, !:ToDeleteVars)
+        set_of_var.union(!.NeededVars, NonLocals, !:NeededVars),
+        set_of_var.difference(!.ToDeleteVars, NonLocals, !:ToDeleteVars)
     ;
         GoalExpr0 = unify(_LHS, _RHS, _UnifyModes, Unification, _UnifyContext),
         (
             Unification = construct(LHSVar, _ConsId, _RHSVars, _ArgModes, 
                 _How, _Unique, _SubInfo),
-            not set_tree234.contains(!.NeededVars, LHSVar),
+            not set_of_var.member(!.NeededVars, LHSVar),
             % If this succeeds, then the backward traversal cannot encounter
             % any more producers of LHSVar.
-            set_tree234.remove(LHSVar, !ToDeleteVars)
+            set_of_var.remove(LHSVar, !ToDeleteVars)
         ->
             % This effectively deletes the unification.
             Goal = true_goal
@@ -946,11 +937,8 @@
             Goal = Goal0,
             NonLocals = goal_info_get_nonlocals(GoalInfo),
             % Assume that all nonlocals are needed.
-            NonLocalsSet = set_tree234.sorted_list_to_set(
-                set.to_sorted_list(NonLocals)),
-            set_tree234.union(!.NeededVars, NonLocalsSet, !:NeededVars),
-            set_tree234.difference(!.ToDeleteVars, NonLocalsSet,
-                !:ToDeleteVars)
+            set_of_var.union(!.NeededVars, NonLocals, !:NeededVars),
+            set_of_var.difference(!.ToDeleteVars, NonLocals, !:ToDeleteVars)
         )
     ;
         % XXX Check that this works for parallel conjunctions.
@@ -964,8 +952,8 @@
         opt_format_call_sites_in_disj(Disjuncts0, Disjuncts, !GoalIdMap,
             !.NeededVars, [], NeededVarsSets,
             !.ToDeleteVars, [], ToDeleteVarsSets),
-        !:NeededVars = set_tree234.union_list(NeededVarsSets),
-        !:ToDeleteVars = set_tree234.intersect_list(ToDeleteVarsSets),
+        !:NeededVars = set_of_var.union_list(NeededVarsSets),
+        !:ToDeleteVars = set_of_var.intersect_list(ToDeleteVarsSets),
         GoalExpr = disj(Disjuncts),
         Goal = hlds_goal(GoalExpr, GoalInfo)
     ;
@@ -973,8 +961,8 @@
         opt_format_call_sites_in_switch(Cases0, Cases, !GoalIdMap,
             !.NeededVars, [], NeededVarsSets,
             !.ToDeleteVars, [], ToDeleteVarsSets),
-        !:NeededVars = set_tree234.union_list(NeededVarsSets),
-        !:ToDeleteVars = set_tree234.intersect_list(ToDeleteVarsSets),
+        !:NeededVars = set_of_var.union_list(NeededVarsSets),
+        !:ToDeleteVars = set_of_var.intersect_list(ToDeleteVarsSets),
         GoalExpr = switch(SwitchVar, CanFail, Cases),
         Goal = hlds_goal(GoalExpr, GoalInfo)
     ;
@@ -988,9 +976,9 @@
         opt_format_call_sites_in_goal(Cond0, Cond, !GoalIdMap,
             NeededVarsBeforeThen, NeededVarsBeforeCond,
             ToDeleteVarsBeforeThen, ToDeleteVarsBeforeCond),
-        set_tree234.union(NeededVarsBeforeCond, NeededVarsBeforeElse,
+        set_of_var.union(NeededVarsBeforeCond, NeededVarsBeforeElse,
             !:NeededVars),
-        set_tree234.intersect(ToDeleteVarsBeforeCond, ToDeleteVarsBeforeElse,
+        set_of_var.intersect(ToDeleteVarsBeforeCond, ToDeleteVarsBeforeElse,
             !:ToDeleteVars),
         GoalExpr = if_then_else(Vars, Cond, Then, Else),
         Goal = hlds_goal(GoalExpr, GoalInfo)
@@ -1030,9 +1018,9 @@
                 !GoalIdMap, !.NeededVars, [], NeededVarsSets,
                 !.ToDeleteVars, [], ToDeleteVarsSets),
             !:NeededVars =
-                set_tree234.union_list([NeededVarsMain | NeededVarsSets]),
+                set_of_var.union_list([NeededVarsMain | NeededVarsSets]),
             !:ToDeleteVars =
-                set_tree234.intersect_list(
+                set_of_var.intersect_list(
                     [ToDeleteVarsMain | ToDeleteVarsSets]),
             ShortHand = atomic_goal(AtomicType, OuterVars, InnerVars,
                 OutputVars, MainGoal, OrElseGoals, OrElseInners),
@@ -1054,8 +1042,8 @@
 :- pred opt_format_call_sites_in_conj(
     list(hlds_goal)::in, list(hlds_goal)::out,
     fc_goal_id_map::in, fc_goal_id_map::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out,
-    set_tree234(prog_var)::in, set_tree234(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 opt_format_call_sites_in_conj([], [], !GoalIdMap,
         !NeededVars, !ToDeleteVars).
@@ -1070,10 +1058,8 @@
 :- pred opt_format_call_sites_in_disj(
     list(hlds_goal)::in, list(hlds_goal)::out,
     fc_goal_id_map::in, fc_goal_id_map::out,
-    set_tree234(prog_var)::in,
-    list(set_tree234(prog_var))::in, list(set_tree234(prog_var))::out,
-    set_tree234(prog_var)::in,
-    list(set_tree234(prog_var))::in, list(set_tree234(prog_var))::out)
+    set_of_progvar::in, list(set_of_progvar)::in, list(set_of_progvar)::out,
+    set_of_progvar::in, list(set_of_progvar)::in, list(set_of_progvar)::out)
     is det.
 
 opt_format_call_sites_in_disj([], [], !GoalIdMap,
@@ -1091,10 +1077,8 @@
 
 :- pred opt_format_call_sites_in_switch(list(case)::in, list(case)::out,
     fc_goal_id_map::in, fc_goal_id_map::out,
-    set_tree234(prog_var)::in,
-    list(set_tree234(prog_var))::in, list(set_tree234(prog_var))::out,
-    set_tree234(prog_var)::in,
-    list(set_tree234(prog_var))::in, list(set_tree234(prog_var))::out)
+    set_of_progvar::in, list(set_of_progvar)::in, list(set_of_progvar)::out,
+    set_of_progvar::in, list(set_of_progvar)::in, list(set_of_progvar)::out)
     is det.
 
 opt_format_call_sites_in_switch([], [], !GoalIdMap,
@@ -1130,7 +1114,7 @@
             AssignGoal),
         AllGoals = Goals ++ [AssignGoal]
     ),
-    NonLocals = set.list_to_set([ResultVar | VarsToPrint]),
+    NonLocals = set_of_var.list_to_set([ResultVar | VarsToPrint]),
     InstMapDelta = instmap_delta_bind_var(ResultVar),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure,
         term.context_init, GoalInfo),
@@ -1189,7 +1173,7 @@
         InstMapDelta, ModuleInfo, term.context_init, CallGoal),
 
     AllGoals = Goals ++ [CallGoal],
-    NonLocals = set.list_to_set(ArgVars ++ VarsToPrint),
+    NonLocals = set_of_var.list_to_set(ArgVars ++ VarsToPrint),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure,
         term.context_init, GoalInfo),
     conj_list_to_goal(AllGoals, GoalInfo, Goal).
Index: compiler/goal_form.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_form.m,v
retrieving revision 1.53
diff -u -b -r1.53 goal_form.m
--- compiler/goal_form.m	23 May 2011 05:08:02 -0000	1.53
+++ compiler/goal_form.m	15 Aug 2011 14:55:05 -0000
@@ -21,11 +21,10 @@
 :- import_module hlds.hlds_goal.
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
-:- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -33,11 +32,11 @@
     % variable in the given set? A from_ground_term_construct scope counts
     % as a unification.
     %
-:- pred goal_is_conj_of_unify(set(prog_var)::in, hlds_goal::in) is semidet.
+:- pred goal_is_conj_of_unify(set_of_progvar::in, hlds_goal::in) is semidet.
 
     % Run goal_is_conj_of_unify on each goal in the list.
     %
-:- pred all_disjuncts_are_conj_of_unify(set(prog_var)::in,
+:- pred all_disjuncts_are_conj_of_unify(set_of_progvar::in,
     list(hlds_goal)::in) is semidet.
 
 %-----------------------------------------------------------------------------%
@@ -186,6 +185,7 @@
 
 :- import_module hlds.code_model.
 :- import_module hlds.hlds_goal.
+:- import_module parse_tree.prog_data.
 :- import_module transform_hlds.exception_analysis.
 :- import_module transform_hlds.term_constr_main.
 
@@ -203,7 +203,7 @@
     CodeModel = model_det,
     goal_to_conj_list(Goal, Conj),
     only_constant_goals(Conj, ToAssignVars0, ToAssignVars),
-    set.empty(ToAssignVars).
+    set_of_var.is_empty(ToAssignVars).
 
 all_disjuncts_are_conj_of_unify(_ToAssignVars, []).
 all_disjuncts_are_conj_of_unify(ToAssignVars, [Disjunct | Disjuncts]) :-
@@ -211,7 +211,7 @@
     all_disjuncts_are_conj_of_unify(ToAssignVars, Disjuncts).
 
 :- pred only_constant_goals(list(hlds_goal)::in,
-    set(prog_var)::in, set(prog_var)::out) is semidet.
+    set_of_progvar::in, set_of_progvar::out) is semidet.
 
 only_constant_goals([], !ToAssignVars).
 only_constant_goals([Goal | Goals], !ToAssignVars) :-
@@ -226,7 +226,7 @@
         GoalExpr = scope(Reason, _),
         Reason = from_ground_term(Var, from_ground_term_construct)
     ),
-    set.delete(Var, !ToAssignVars),
+    set_of_var.delete(Var, !ToAssignVars),
     only_constant_goals(Goals, !ToAssignVars).
 
 %-----------------------------------------------------------------------------%
Index: compiler/goal_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_util.m,v
retrieving revision 1.186
diff -u -b -r1.186 goal_util.m
--- compiler/goal_util.m	11 Aug 2011 23:18:59 -0000	1.186
+++ compiler/goal_util.m	15 Aug 2011 08:07:30 -0000
@@ -33,7 +33,6 @@
 :- import_module bool.
 :- import_module list.
 :- import_module maybe.
-:- import_module set.
 :- import_module term.
 
 %-----------------------------------------------------------------------------%
@@ -144,7 +143,7 @@
     % variable.
     %
 :- pred extra_nonlocal_typeinfos(rtti_varmaps::in, vartypes::in,
-    existq_tvars::in, set(prog_var)::in, set(prog_var)::out) is det.
+    existq_tvars::in, set_of_progvar::in, set_of_progvar::out) is det.
 
 :- type is_leaf
     --->    is_leaf
@@ -452,6 +451,7 @@
 :- import_module map.
 :- import_module pair.
 :- import_module require.
+:- import_module set.
 :- import_module solutions.
 :- import_module string.
 :- import_module varset.
@@ -493,7 +493,7 @@
     UnifyInfo = assign(OrigVar, NewVar),
     UnifyContext = unify_context(umc_explicit, []),
     GoalExpr = unify(OrigVar, rhs_var(NewVar), Mode, UnifyInfo, UnifyContext),
-    set.list_to_set([OrigVar, NewVar], NonLocals),
+    set_of_var.list_to_set([OrigVar, NewVar], NonLocals),
     UnifyInstMapDelta = instmap_delta_from_assoc_list([OrigVar - NewInst]),
     goal_info_init(NonLocals, UnifyInstMapDelta, detism_det, purity_pure,
         term.context_init, GoalInfo),
@@ -809,11 +809,11 @@
     % Find all non-local type vars.  That is, type vars that are existentially
     % quantified or type vars that appear in the type of a non-local prog_var.
 
-    set.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
     map.apply_to_list(NonLocalsList, VarTypes, NonLocalsTypes),
     type_vars_list(NonLocalsTypes, NonLocalTypeVarsList0),
-    list.append(ExistQVars, NonLocalTypeVarsList0, NonLocalTypeVarsList),
-    set.list_to_set(NonLocalTypeVarsList, NonLocalTypeVars),
+    NonLocalTypeVarsList = ExistQVars ++ NonLocalTypeVarsList0,
+    set_of_var.list_to_set(NonLocalTypeVarsList, NonLocalTypeVars),
 
     % Find all the type_infos that are non-local, that is, type_infos for
     % type vars that are non-local in the above sense.
@@ -822,13 +822,14 @@
         rtti_lookup_type_info_locn(RttiVarMaps, TypeVar, Locn),
         type_info_locn_var(Locn, ProgVar)
     ),
-    NonLocalTypeInfoVars = set.map(TypeVarToProgVar, NonLocalTypeVars),
+    NonLocalTypeInfoVars = set_of_var.list_to_set(
+        list.map(TypeVarToProgVar, NonLocalTypeVarsList)),
 
     % Find all the typeclass_infos that are non-local. These include
     % all typeclass_infos that constrain a type variable that is non-local
     % in the above sense.
 
-    solutions.solutions_set(
+    solutions.solutions(
         (pred(Var::out) is nondet :-
             % Search through all arguments of all constraints
             % that the goal could have used.
@@ -836,13 +837,15 @@
             list.member(Constraint, Constraints),
             Constraint = constraint(_Name, ArgTypes),
             type_list_contains_var(ArgTypes, TypeVar),
-            set.member(TypeVar, NonLocalTypeVars),
+            set_of_var.member(NonLocalTypeVars, TypeVar),
 
             % We found a constraint that is non-local. Include the variable
             % holding its typeclass_info.
             rtti_lookup_typeclass_info_var(RttiVarMaps, Constraint, Var)
-        ), NonLocalTypeClassInfoVars),
-    NonLocalTypeInfos = set.union(NonLocalTypeInfoVars,
+        ), NonLocalTypeClassInfoVarsList),
+    set_of_var.sorted_list_to_set(NonLocalTypeClassInfoVarsList,
+        NonLocalTypeClassInfoVars),
+    NonLocalTypeInfos = set_of_var.union(NonLocalTypeInfoVars,
         NonLocalTypeClassInfoVars).
 
 %-----------------------------------------------------------------------------%
@@ -1367,7 +1370,7 @@
         cannot_cgc),
     ExtraGoalExpr = unify(Var, rhs_functor(ConsId, no, ArgVars), UniMode,
         Unification, UnifyContext),
-    set.singleton_set(NonLocals, Var),
+    NonLocals = set_of_var.make_singleton(Var),
     instmap_delta_init_reachable(ExtraInstMapDelta0),
     instmap_delta_bind_var_to_functor(Var, VarType, ConsId, InstMap,
         ExtraInstMapDelta0, ExtraInstMapDelta, !ModuleInfo),
@@ -1382,15 +1385,15 @@
     % of the entire conjunction.
     CaseGoal = hlds_goal(_, CaseGoalInfo),
     CaseNonLocals0 = goal_info_get_nonlocals(CaseGoalInfo),
-    set.insert(Var, CaseNonLocals0, CaseNonLocals),
+    set_of_var.insert(Var, CaseNonLocals0, CaseNonLocals),
     CaseInstMapDelta = goal_info_get_instmap_delta(CaseGoalInfo),
     instmap_delta_apply_instmap_delta(ExtraInstMapDelta, CaseInstMapDelta,
         test_size, InstMapDelta),
     CaseDetism0 = goal_info_get_determinism(CaseGoalInfo),
     det_conjunction_detism(detism_semi, CaseDetism0, Detism),
     CasePurity = goal_info_get_purity(CaseGoalInfo),
-    goal_info_init(CaseNonLocals, InstMapDelta,
-        Detism, CasePurity, CombinedGoalInfo),
+    goal_info_init(CaseNonLocals, InstMapDelta, Detism, CasePurity,
+        CombinedGoalInfo),
     Disjunct = hlds_goal(conj(plain_conj, GoalList), CombinedGoalInfo).
 
 %-----------------------------------------------------------------------------%
@@ -1616,8 +1619,8 @@
         VarTypes, ModuleInfo, EarlierChangedVars),
 
     LaterGoalNonLocals = goal_info_get_nonlocals(LaterGoalInfo),
-    set.intersect(EarlierChangedVars, LaterGoalNonLocals, Intersection),
-    not set.empty(Intersection).
+    set_of_var.intersect(EarlierChangedVars, LaterGoalNonLocals, Intersection),
+    not set_of_var.is_empty(Intersection).
 
 %-----------------------------------------------------------------------------%
 
@@ -1635,7 +1638,7 @@
 
     GoalExpr = plain_call(PredId, ProcId, Args, BuiltinState, no,
         qualified(ModuleName, ProcName)),
-    set.list_to_set(Args, NonLocals),
+    set_of_var.list_to_set(Args, NonLocals),
     determinism_components(Detism, _CanFail, NumSolns),
     (
         NumSolns = at_most_zero,
@@ -1668,7 +1671,7 @@
     ArgVars = list.map(foreign_arg_var, Args),
     ExtraArgVars = list.map(foreign_arg_var, ExtraArgs),
     Vars = ArgVars ++ ExtraArgVars,
-    set.list_to_set(Vars, NonLocals),
+    set_of_var.list_to_set(Vars, NonLocals),
     determinism_components(Detism, _CanFail, NumSolns),
     (
         NumSolns = at_most_zero,
@@ -1696,7 +1699,7 @@
 
 generate_cast_with_insts(CastType, InArg, OutArg, InInst, OutInst, Context,
         Goal) :-
-    set.list_to_set([InArg, OutArg], NonLocals),
+    set_of_var.list_to_set([InArg, OutArg], NonLocals),
     InstMapDelta = instmap_delta_from_assoc_list([OutArg - OutInst]),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure, Context,
         GoalInfo),
Index: compiler/headvar_names.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/headvar_names.m,v
retrieving revision 1.13
diff -u -b -r1.13 headvar_names.m
--- compiler/headvar_names.m	23 May 2011 05:08:03 -0000	1.13
+++ compiler/headvar_names.m	15 Aug 2011 08:10:07 -0000
@@ -37,6 +37,7 @@
 :- import_module hlds.hlds_goal.
 :- import_module libs.options.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
@@ -75,7 +76,7 @@
                 VarSet0, VarSet, map.init, Subst, [], RevConj),
 
             NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-            rename_vars_in_var_set(need_not_rename, Subst,
+            rename_vars_in_set_of_var(need_not_rename, Subst,
                 NonLocals0, NonLocals),
             goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
             conj_list_to_goal(list.reverse(RevConj), GoalInfo, Goal),
@@ -152,7 +153,7 @@
                 ),
                 OtherGoal = hlds_goal(_, OtherGoalInfo),
                 OtherNonLocals = goal_info_get_nonlocals(OtherGoalInfo),
-                set.member(HeadVar, OtherNonLocals)
+                set_of_var.member(OtherNonLocals, HeadVar)
             ))
         ->
             SeenVars = [OtherVar | SeenVars0],
Index: compiler/hhf.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hhf.m,v
retrieving revision 1.45
diff -u -b -r1.45 hhf.m
--- compiler/hhf.m	5 May 2011 03:58:54 -0000	1.45
+++ compiler/hhf.m	15 Aug 2011 06:28:45 -0000
@@ -45,9 +45,10 @@
 :- import_module hlds.goal_util.
 :- import_module hlds.hlds_goal.
 :- import_module hlds.passes_aux.
+:- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
-:- import_module mdbcomp.prim_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module list.
 :- import_module map.
@@ -209,7 +210,7 @@
     % !HI ^ hhfi_varset := VarSet,
     % !HI ^ hhfi_vartypes := VarTypes.
 
-:- pred convert_goal_to_hhf(set(prog_var)::in, hlds_goal::in, hlds_goal::out,
+:- pred convert_goal_to_hhf(set_of_progvar::in, hlds_goal::in, hlds_goal::out,
     hhf_info::in, hhf_info::out) is det.
 
 convert_goal_to_hhf(NonLocals, Goal0, Goal, !HI) :-
@@ -226,7 +227,7 @@
     convert_goal_expr_to_hhf(NonLocals, GoalInfo, GoalExpr0, GoalExpr, !HI),
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
-:- pred convert_goal_expr_to_hhf(set(prog_var)::in, hlds_goal_info::in,
+:- pred convert_goal_expr_to_hhf(set_of_progvar::in, hlds_goal_info::in,
     hlds_goal_expr::in, hlds_goal_expr::out, hhf_info::in, hhf_info::out)
     is det.
 
@@ -289,7 +290,7 @@
         unexpected($module, $pred, "shorthand")
     ).
 
-:- pred convert_unify_to_hhf(unify_rhs::in, set(prog_var)::in,
+:- pred convert_unify_to_hhf(unify_rhs::in, set_of_progvar::in,
     hlds_goal_info::in, prog_var::in, unify_mode::in, unification::in,
     unify_context::in, hlds_goal_expr::out, hhf_info::in, hhf_info::out)
     is det.
@@ -324,7 +325,7 @@
             !HI ^ hhfi_inst_graph := InstGraph
         ),
         GINonlocals0 = goal_info_get_nonlocals(GoalInfo0),
-        GINonlocals = set.union(GINonlocals0, list_to_set(Args)),
+        GINonlocals = set_of_var.union(GINonlocals0, list_to_set(Args)),
         goal_info_set_nonlocals(GINonlocals, GoalInfo0, GoalInfo),
         RHS = rhs_functor(ConsId, IsExistConstruct, Args),
         UnifyGoalExpr = unify(X, RHS, Mode, Unif, Context),
@@ -344,11 +345,11 @@
 make_unifications([A | As], [B | Bs], GI0, M, U, C,
         [hlds_goal(unify(A, rhs_var(B), M, U, C), GI) | Us]) :-
     GINonlocals0 = goal_info_get_nonlocals(GI0),
-    GINonlocals = set.insert_list(GINonlocals0, [A, B]),
+    set_of_var.insert_list([A, B], GINonlocals0, GINonlocals),
     goal_info_set_nonlocals(GINonlocals, GI0, GI),
     make_unifications(As, Bs, GI0, M, U, C, Us).
 
-:- pred add_unifications(list(prog_var)::in, set(prog_var)::in,
+:- pred add_unifications(list(prog_var)::in, set_of_progvar::in,
     hlds_goal_info::in, unify_mode::in, unification::in, unify_context::in,
     list(prog_var)::out, hlds_goals::out, hhf_info::in, hhf_info::out) is det.
 
@@ -361,7 +362,7 @@
             map.lookup(InstGraph0, A, Node),
             Node = node(_, parent(_))
         ;
-            set.member(A, NonLocals)
+            set_of_var.member(NonLocals, A)
         )
     ->
         VarSet0 = !.HI ^ hhfi_varset,
@@ -375,7 +376,7 @@
         !HI ^ hhfi_vartypes := VarTypes,
         !HI ^ hhfi_inst_graph := InstGraph,
         GINonlocals0 = goal_info_get_nonlocals(GI0),
-        GINonlocals = set.insert(GINonlocals0, V),
+        set_of_var.insert(V, GINonlocals0, GINonlocals),
         goal_info_set_nonlocals(GINonlocals, GI0, GI),
         Goals = [hlds_goal(unify(A, rhs_var(V), M, U, C), GI) | Goals0]
     ;
Index: compiler/higher_order.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/higher_order.m,v
retrieving revision 1.195
diff -u -b -r1.195 higher_order.m
--- compiler/higher_order.m	23 May 2011 05:08:03 -0000	1.195
+++ compiler/higher_order.m	15 Aug 2011 11:00:43 -0000
@@ -68,6 +68,7 @@
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.inlining.
 
 :- import_module assoc_list.
@@ -1166,7 +1167,7 @@
         !ProcInfo),
     CallArgs = [TypeClassInfoVar, IndexVar, ResultVar],
 
-    set.list_to_set(CallArgs, NonLocals),
+    set_of_var.list_to_set(CallArgs, NonLocals),
     instmap_delta_init_reachable(InstMapDelta0),
     instmap_delta_insert_var(ResultVar, ground(shared, none),
         InstMapDelta0, InstMapDelta),
@@ -2254,7 +2255,7 @@
             NewCallArgs = [ComparisonResult, CastArg1, CastArg2],
             Call = plain_call(SpecialPredId, SpecialProcId, NewCallArgs,
                 not_builtin, MaybeContext, SymName),
-            set.list_to_set([ComparisonResult, Arg1, Arg2], NonLocals),
+            set_of_var.list_to_set([ComparisonResult, Arg1, Arg2], NonLocals),
             InstMapDelta = instmap_delta_bind_var(ComparisonResult),
             Detism = detism_det,
             goal_info_init(NonLocals, InstMapDelta, Detism, purity_pure,
@@ -2280,7 +2281,7 @@
         UnwrappedArg1, ExtractGoal1, ProcInfo0, ProcInfo1),
     unwrap_no_tag_arg(OuterType, WrappedType, Context, Constructor, Arg2,
         UnwrappedArg2, ExtractGoal2, ProcInfo1, ProcInfo2),
-    set.list_to_set([UnwrappedArg1, UnwrappedArg2], NonLocals0),
+    set_of_var.list_to_set([UnwrappedArg1, UnwrappedArg2], NonLocals0),
     (
         MaybeResult = no,
         in_mode(In),
@@ -2297,7 +2298,7 @@
         !Info ^ hoi_proc_info := ProcInfo2
     ;
         MaybeResult = yes(ComparisonResult),
-        set.insert(ComparisonResult, NonLocals0, NonLocals),
+        set_of_var.insert(ComparisonResult, NonLocals0, NonLocals),
         InstMapDelta = instmap_delta_bind_var(ComparisonResult),
         Detism = detism_det,
         % Build a new call with the unwrapped arguments.
@@ -2427,7 +2428,7 @@
     ConsId = cons(Constructor, 1, OuterTypeCtor),
     UniModes = [(ground(shared, none) - free) ->
         (ground(shared, none) - ground(shared, none))],
-    set.list_to_set([Arg, UnwrappedArg], NonLocals),
+    set_of_var.list_to_set([Arg, UnwrappedArg], NonLocals),
     % This will be recomputed later.
     InstMapDelta = instmap_delta_bind_var(UnwrappedArg),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure, Context,
@@ -3178,7 +3179,7 @@
         % other constants which include it will be recognized as constant.
         modes_to_uni_modes(ModuleInfo, CurriedArgModes1,
             CurriedArgModes1, UniModes),
-        set.list_to_set(CurriedHeadVars1, ConstNonLocals),
+        set_of_var.list_to_set(CurriedHeadVars1, ConstNonLocals),
         ConstInst = ground(shared, GroundInstInfo),
         ConstInstMapDelta = instmap_delta_from_assoc_list([LVar - ConstInst]),
         goal_info_init(ConstNonLocals, ConstInstMapDelta, detism_det,
Index: compiler/hlds_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_goal.m,v
retrieving revision 1.226
diff -u -b -r1.226 hlds_goal.m
--- compiler/hlds_goal.m	5 Jul 2011 03:34:32 -0000	1.226
+++ compiler/hlds_goal.m	15 Aug 2011 09:17:59 -0000
@@ -25,6 +25,7 @@
 :- import_module mdbcomp.goal_path.
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -1083,20 +1084,20 @@
     --->    mode_constr_goal_info(
                 % Inst_graph nodes that are reachable from variables
                 % that occur in the goal.
-                mci_occurring_vars          :: set(prog_var),
+                mci_occurring_vars          :: set_of_progvar,
 
                 % Inst_graph nodes produced by this goal.
-                mci_producing_vars          :: set(prog_var),
+                mci_producing_vars          :: set_of_progvar,
 
                 % Inst_graph nodes consumed by this goal.
-                mci_consuming_vars          :: set(prog_var),
+                mci_consuming_vars          :: set_of_progvar,
 
                 % The variables that this goal makes visible.
-                mci_make_visible_vars       :: set(prog_var),
+                mci_make_visible_vars       :: set_of_progvar,
 
                 % The variables that this goal needs to be visible
                 % before it is executed.
-                mci_need_visible_vars       :: set(prog_var)
+                mci_need_visible_vars       :: set_of_progvar
             ).
 
     % Information about compile-time garbage collection.
@@ -1108,13 +1109,13 @@
                 % + sum(pre_births), minus the set of dead vars
                 % (sum(post_deaths and pre_deaths).
                 % The information is needed for determining the direct reuses.
-                ctgc_lfu        :: set(prog_var),
+                ctgc_lfu                    :: set_of_progvar,
 
                 % The local backward use set. This set contains the
                 % instantiated variables that are needed upon backtracking
                 % (i.e. syntactically appearing in any nondet call preceding
                 % this goal).
-                ctgc_lbu        :: set(prog_var),
+                ctgc_lbu                    :: set_of_progvar,
 
                 % Any structure reuse information related to this call.
                 ctgc_reuse      :: reuse_description
@@ -1226,8 +1227,8 @@
                 port_counts_give_coverage_after
             ).
 
-    % A goal is trivial if it is a simple atomic goal (not a call), or it's a
-    % non-atomic goal and all it's descendants are trivial.
+    % A goal is trivial if it is a simple atomic goal (not a call),
+    % or it is a non-atomic goal and all its descendants are trivial.
     %
 :- type goal_trivial
     --->    goal_is_trivial
@@ -1242,19 +1243,19 @@
 
 :- pred goal_info_init(hlds_goal_info::out) is det.
 :- pred goal_info_init(prog_context::in, hlds_goal_info::out) is det.
-:- pred goal_info_init(set(prog_var)::in, instmap_delta::in, determinism::in,
+:- pred goal_info_init(set_of_progvar::in, instmap_delta::in, determinism::in,
     purity::in, hlds_goal_info::out) is det.
-:- pred goal_info_init(set(prog_var)::in, instmap_delta::in, determinism::in,
+:- pred goal_info_init(set_of_progvar::in, instmap_delta::in, determinism::in,
     purity::in, prog_context::in, hlds_goal_info::out) is det.
 
-:- func impure_init_goal_info(set(prog_var), instmap_delta, determinism)
+:- func impure_init_goal_info(set_of_progvar, instmap_delta, determinism)
     = hlds_goal_info.
-:- func impure_reachable_init_goal_info(set(prog_var), determinism)
+:- func impure_reachable_init_goal_info(set_of_progvar, determinism)
     = hlds_goal_info.
-:- func impure_unreachable_init_goal_info(set(prog_var), determinism)
+:- func impure_unreachable_init_goal_info(set_of_progvar, determinism)
     = hlds_goal_info.
 
-:- func goal_info_add_nonlocals_make_impure(hlds_goal_info, set(prog_var))
+:- func goal_info_add_nonlocals_make_impure(hlds_goal_info, set_of_progvar)
     = hlds_goal_info.
 :- pred make_impure(hlds_goal_info::in, hlds_goal_info::out) is det.
 :- pred add_impurity_if_needed(bool::in,
@@ -1272,8 +1273,8 @@
 :- func goal_info_get_determinism(hlds_goal_info) = determinism.
 :- func goal_info_get_instmap_delta(hlds_goal_info) = instmap_delta.
 :- func goal_info_get_context(hlds_goal_info) = prog_context.
-:- func goal_info_get_nonlocals(hlds_goal_info) = set(prog_var).
-:- func goal_info_get_code_gen_nonlocals(hlds_goal_info) = set(prog_var).
+:- func goal_info_get_nonlocals(hlds_goal_info) = set_of_progvar.
+:- func goal_info_get_code_gen_nonlocals(hlds_goal_info) = set_of_progvar.
 :- func goal_info_get_purity(hlds_goal_info) = purity.
 :- func goal_info_get_features(hlds_goal_info) = set(goal_feature).
 :- func goal_info_get_goal_id(hlds_goal_info) = goal_id.
@@ -1284,8 +1285,8 @@
 :- func goal_info_get_maybe_mode_constr(hlds_goal_info) =
     maybe(mode_constr_goal_info).
 :- func goal_info_get_maybe_ctgc(hlds_goal_info) = maybe(ctgc_goal_info).
-:- func goal_info_get_maybe_lfu(hlds_goal_info) = maybe(set(prog_var)).
-:- func goal_info_get_maybe_lbu(hlds_goal_info) = maybe(set(prog_var)).
+:- func goal_info_get_maybe_lfu(hlds_goal_info) = maybe(set_of_progvar).
+:- func goal_info_get_maybe_lbu(hlds_goal_info) = maybe(set_of_progvar).
 :- func goal_info_get_maybe_reuse(hlds_goal_info) = maybe(reuse_description).
 :- func goal_info_get_maybe_dp_info(hlds_goal_info) = maybe(dp_goal_info).
 
@@ -1297,9 +1298,9 @@
     hlds_goal_info::in, hlds_goal_info::out) is det.
 :- pred goal_info_set_purity(purity::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_nonlocals(set(prog_var)::in,
+:- pred goal_info_set_nonlocals(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_code_gen_nonlocals(set(prog_var)::in,
+:- pred goal_info_set_code_gen_nonlocals(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
 :- pred goal_info_set_features(set(goal_feature)::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
@@ -1317,9 +1318,9 @@
     hlds_goal_info::in, hlds_goal_info::out) is det.
 :- pred goal_info_set_maybe_ctgc(maybe(ctgc_goal_info)::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_lfu(set(prog_var)::in, hlds_goal_info::in,
+:- pred goal_info_set_lfu(set_of_progvar::in, hlds_goal_info::in,
     hlds_goal_info::out) is det.
-:- pred goal_info_set_lbu(set(prog_var)::in, hlds_goal_info::in,
+:- pred goal_info_set_lbu(set_of_progvar::in, hlds_goal_info::in,
     hlds_goal_info::out) is det.
 :- pred goal_info_set_reuse(reuse_description::in, hlds_goal_info::in,
     hlds_goal_info::out) is det.
@@ -1330,43 +1331,43 @@
     % requested values have not been set.
     %
 :- func goal_info_get_rbmm(hlds_goal_info) = rbmm_goal_info.
-:- func goal_info_get_lfu(hlds_goal_info) = set(prog_var).
-:- func goal_info_get_lbu(hlds_goal_info) = set(prog_var).
+:- func goal_info_get_lfu(hlds_goal_info) = set_of_progvar.
+:- func goal_info_get_lbu(hlds_goal_info) = set_of_progvar.
 :- func goal_info_get_reuse(hlds_goal_info) = reuse_description.
 
-:- pred goal_info_get_occurring_vars(hlds_goal_info::in, set(prog_var)::out)
+:- pred goal_info_get_occurring_vars(hlds_goal_info::in, set_of_progvar::out)
     is det.
-:- pred goal_info_get_producing_vars(hlds_goal_info::in, set(prog_var)::out)
+:- pred goal_info_get_producing_vars(hlds_goal_info::in, set_of_progvar::out)
     is det.
-:- pred goal_info_get_consuming_vars(hlds_goal_info::in, set(prog_var)::out)
+:- pred goal_info_get_consuming_vars(hlds_goal_info::in, set_of_progvar::out)
     is det.
-:- pred goal_info_get_make_visible_vars(hlds_goal_info::in, set(prog_var)::out)
+:- pred goal_info_get_make_visible_vars(hlds_goal_info::in, set_of_progvar::out)
     is det.
-:- pred goal_info_get_need_visible_vars(hlds_goal_info::in, set(prog_var)::out)
+:- pred goal_info_get_need_visible_vars(hlds_goal_info::in, set_of_progvar::out)
     is det.
 
-:- pred goal_info_set_occurring_vars(set(prog_var)::in,
+:- pred goal_info_set_occurring_vars(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_producing_vars(set(prog_var)::in,
+:- pred goal_info_set_producing_vars(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_consuming_vars(set(prog_var)::in,
+:- pred goal_info_set_consuming_vars(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_make_visible_vars(set(prog_var)::in,
+:- pred goal_info_set_make_visible_vars(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
-:- pred goal_info_set_need_visible_vars(set(prog_var)::in,
+:- pred goal_info_set_need_visible_vars(set_of_progvar::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
 
-:- func producing_vars(hlds_goal_info) = set(prog_var).
-:- func 'producing_vars :='(hlds_goal_info, set(prog_var)) = hlds_goal_info.
+:- func producing_vars(hlds_goal_info) = set_of_progvar.
+:- func 'producing_vars :='(hlds_goal_info, set_of_progvar) = hlds_goal_info.
 
-:- func consuming_vars(hlds_goal_info) = set(prog_var).
-:- func 'consuming_vars :='(hlds_goal_info, set(prog_var)) = hlds_goal_info.
+:- func consuming_vars(hlds_goal_info) = set_of_progvar.
+:- func 'consuming_vars :='(hlds_goal_info, set_of_progvar) = hlds_goal_info.
 
-:- func make_visible_vars(hlds_goal_info) = set(prog_var).
-:- func 'make_visible_vars :='(hlds_goal_info, set(prog_var)) = hlds_goal_info.
+:- func make_visible_vars(hlds_goal_info) = set_of_progvar.
+:- func 'make_visible_vars :='(hlds_goal_info, set_of_progvar) = hlds_goal_info.
 
-:- func need_visible_vars(hlds_goal_info) = set(prog_var).
-:- func 'need_visible_vars :='(hlds_goal_info, set(prog_var)) = hlds_goal_info.
+:- func need_visible_vars(hlds_goal_info) = set_of_progvar.
+:- func 'need_visible_vars :='(hlds_goal_info, set_of_progvar) = hlds_goal_info.
 
 :- type contains_trace_goal
     --->    contains_trace_goal
@@ -1375,7 +1376,7 @@
 :- func worst_contains_trace(contains_trace_goal, contains_trace_goal)
     = contains_trace_goal.
 
-:- func goal_get_nonlocals(hlds_goal) = set(prog_var).
+:- func goal_get_nonlocals(hlds_goal) = set_of_progvar.
 
 :- pred goal_set_goal_id(goal_id::in, hlds_goal::in, hlds_goal::out) is det.
 
@@ -1683,7 +1684,7 @@
 
     % Return the union of all the nonlocals of a list of goals.
     %
-:- pred goal_list_nonlocals(list(hlds_goal)::in, set(prog_var)::out) is det.
+:- pred goal_list_nonlocals(list(hlds_goal)::in, set_of_progvar::out) is det.
 
     % Compute the instmap_delta resulting from applying
     % all the instmap_deltas of the given goals.
@@ -1922,7 +1923,7 @@
                 % quantification.m). In some circumstances, this may be a
                 % conservative approximation: it may be a superset of the
                 % real non-locals.
-/*  3 */        gi_nonlocals        :: set(prog_var),
+/*  3 */        gi_nonlocals        :: set_of_progvar,
 
 /*  4 */        gi_purity           :: purity,
 
@@ -1964,7 +1965,7 @@
 goal_info_init(GoalInfo) :-
     Detism = detism_erroneous,
     instmap_delta_init_unreachable(InstMapDelta),
-    set.init(NonLocals),
+    NonLocals = set_of_var.init,
     term.context_init(Context),
     set.init(Features),
     GoalId = goal_id(-1),
@@ -1977,7 +1978,7 @@
 goal_info_init(Context, GoalInfo) :-
     Detism = detism_erroneous,
     instmap_delta_init_unreachable(InstMapDelta),
-    set.init(NonLocals),
+    NonLocals = set_of_var.init,
     set.init(Features),
     GoalId = goal_id(-1),
     GoalInfo = goal_info(Detism, InstMapDelta, NonLocals, purity_pure,
@@ -2019,13 +2020,13 @@
 
 goal_info_add_nonlocals_make_impure(!.GoalInfo, NewNonLocals) = !:GoalInfo :-
     NonLocals0 = goal_info_get_nonlocals(!.GoalInfo),
-    NonLocals = set.union(NonLocals0, NewNonLocals),
+    NonLocals = set_of_var.union(NonLocals0, NewNonLocals),
     goal_info_set_nonlocals(NonLocals, !GoalInfo),
     make_impure(!GoalInfo).
 
 fail_goal_info = GoalInfo :-
     instmap_delta_init_unreachable(InstMapDelta),
-    goal_info_init(set.init, InstMapDelta, detism_failure, purity_pure,
+    goal_info_init(set_of_var.init, InstMapDelta, detism_failure, purity_pure,
         GoalInfo).
 
 make_impure(!GoalInfo) :-
@@ -2054,7 +2055,8 @@
 
 :- func ctgc_goal_info_init = ctgc_goal_info.
 
-ctgc_goal_info_init = ctgc_goal_info(set.init, set.init, no_reuse_info).
+ctgc_goal_info_init =
+    ctgc_goal_info(set_of_var.init, set_of_var.init, no_reuse_info).
 
 rbmm_info_init = rbmm_goal_info(set.init, set.init, set.init, set.init,
     set.init).
@@ -2130,7 +2132,7 @@
         OccurringVars = MCI ^ mci_occurring_vars
     ;
         MMCI = no,
-        OccurringVars = set.init
+        OccurringVars = set_of_var.init
     ).
 
 goal_info_get_producing_vars(GoalInfo, ProducingVars) :-
@@ -2140,7 +2142,7 @@
         ProducingVars = MCI ^ mci_producing_vars
     ;
         MMCI = no,
-        ProducingVars = set.init
+        ProducingVars = set_of_var.init
     ).
 
 goal_info_get_consuming_vars(GoalInfo, ConsumingVars) :-
@@ -2150,7 +2152,7 @@
         ConsumingVars = MCI ^ mci_consuming_vars
     ;
         MMCI = no,
-        ConsumingVars = set.init
+        ConsumingVars = set_of_var.init
     ).
 
 goal_info_get_make_visible_vars(GoalInfo, MakeVisibleVars) :-
@@ -2160,7 +2162,7 @@
         MakeVisibleVars = MCI ^ mci_make_visible_vars
     ;
         MMCI = no,
-        MakeVisibleVars = set.init
+        MakeVisibleVars = set_of_var.init
     ).
 
 goal_info_get_need_visible_vars(GoalInfo, NeedVisibleVars) :-
@@ -2170,7 +2172,7 @@
         NeedVisibleVars = MCI ^ mci_need_visible_vars
     ;
         MMCI = no,
-        NeedVisibleVars = set.init
+        NeedVisibleVars = set_of_var.init
     ).
 
 goal_info_set_occurring_vars(OccurringVars, !GoalInfo) :-
@@ -2180,10 +2182,10 @@
         MCI = MCI0 ^ mci_occurring_vars := OccurringVars
     ;
         MMCI0 = no,
-        set.init(ProducingVars),
-        set.init(ConsumingVars),
-        set.init(MakeVisibleVars),
-        set.init(NeedVisibleVars),
+        set_of_var.init(ProducingVars),
+        set_of_var.init(ConsumingVars),
+        set_of_var.init(MakeVisibleVars),
+        set_of_var.init(NeedVisibleVars),
         MCI = mode_constr_goal_info(OccurringVars, ProducingVars,
             ConsumingVars, MakeVisibleVars, NeedVisibleVars)
     ),
@@ -2196,10 +2198,10 @@
         MCI = MCI0 ^ mci_producing_vars := ProducingVars
     ;
         MMCI0 = no,
-        set.init(OccurringVars),
-        set.init(ConsumingVars),
-        set.init(MakeVisibleVars),
-        set.init(NeedVisibleVars),
+        set_of_var.init(OccurringVars),
+        set_of_var.init(ConsumingVars),
+        set_of_var.init(MakeVisibleVars),
+        set_of_var.init(NeedVisibleVars),
         MCI = mode_constr_goal_info(OccurringVars, ProducingVars,
             ConsumingVars, MakeVisibleVars, NeedVisibleVars)
     ),
@@ -2212,10 +2214,10 @@
         MCI = MCI0 ^ mci_consuming_vars := ConsumingVars
     ;
         MMCI0 = no,
-        set.init(OccurringVars),
-        set.init(ProducingVars),
-        set.init(MakeVisibleVars),
-        set.init(NeedVisibleVars),
+        set_of_var.init(OccurringVars),
+        set_of_var.init(ProducingVars),
+        set_of_var.init(MakeVisibleVars),
+        set_of_var.init(NeedVisibleVars),
         MCI = mode_constr_goal_info(OccurringVars, ProducingVars,
             ConsumingVars, MakeVisibleVars, NeedVisibleVars)
     ),
@@ -2228,10 +2230,10 @@
         MCI = MCI0 ^ mci_make_visible_vars := MakeVisibleVars
     ;
         MMCI0 = no,
-        set.init(OccurringVars),
-        set.init(ProducingVars),
-        set.init(ConsumingVars),
-        set.init(NeedVisibleVars),
+        set_of_var.init(OccurringVars),
+        set_of_var.init(ProducingVars),
+        set_of_var.init(ConsumingVars),
+        set_of_var.init(NeedVisibleVars),
         MCI = mode_constr_goal_info(OccurringVars, ProducingVars,
             ConsumingVars, MakeVisibleVars, NeedVisibleVars)
     ),
@@ -2244,10 +2246,10 @@
         MCI = MCI0 ^ mci_need_visible_vars := NeedVisibleVars
     ;
         MMCI0 = no,
-        set.init(OccurringVars),
-        set.init(ProducingVars),
-        set.init(ConsumingVars),
-        set.init(MakeVisibleVars),
+        set_of_var.init(OccurringVars),
+        set_of_var.init(ProducingVars),
+        set_of_var.init(ConsumingVars),
+        set_of_var.init(MakeVisibleVars),
         MCI = mode_constr_goal_info(OccurringVars, ProducingVars,
             ConsumingVars, MakeVisibleVars, NeedVisibleVars)
     ),
@@ -3003,7 +3005,7 @@
     !.GoalInfo = goal_info(Detism, InstMapDelta0, NonLocals0, Purity,
         Features, GoalPath, CodeGenInfo0, ExtraInfo0),
 
-    rename_vars_in_var_set(Must, Subn, NonLocals0, NonLocals),
+    rename_vars_in_set_of_var(Must, Subn, NonLocals0, NonLocals),
     instmap_delta_apply_sub(Must, Subn, InstMapDelta0, InstMapDelta),
     (
         CodeGenInfo0 = no_code_gen_info,
@@ -3022,8 +3024,8 @@
     ;
         MaybeCTGC0 = yes(CTGC0),
         CTGC0 = ctgc_goal_info(ForwardUse0, BackwardUse0, ReuseDesc0),
-        rename_vars_in_var_set(Must, Subn, ForwardUse0, ForwardUse),
-        rename_vars_in_var_set(Must, Subn, BackwardUse0, BackwardUse),
+        rename_vars_in_set_of_var(Must, Subn, ForwardUse0, ForwardUse),
+        rename_vars_in_set_of_var(Must, Subn, BackwardUse0, BackwardUse),
         (
             ( ReuseDesc0 = no_reuse_info
             ; ReuseDesc0 = no_possible_reuse
@@ -3065,11 +3067,11 @@
         MaybeMCI0 = yes(MCI0),
         MCI0 = mode_constr_goal_info(Occurring0, Producing0, Consuming0,
             MakeVisible0, NeedVisible0),
-        rename_vars_in_var_set(Must, Subn, Occurring0, Occurring),
-        rename_vars_in_var_set(Must, Subn, Producing0, Producing),
-        rename_vars_in_var_set(Must, Subn, Consuming0, Consuming),
-        rename_vars_in_var_set(Must, Subn, MakeVisible0, MakeVisible),
-        rename_vars_in_var_set(Must, Subn, NeedVisible0, NeedVisible),
+        rename_vars_in_set_of_var(Must, Subn, Occurring0, Occurring),
+        rename_vars_in_set_of_var(Must, Subn, Producing0, Producing),
+        rename_vars_in_set_of_var(Must, Subn, Consuming0, Consuming),
+        rename_vars_in_set_of_var(Must, Subn, MakeVisible0, MakeVisible),
+        rename_vars_in_set_of_var(Must, Subn, NeedVisible0, NeedVisible),
         MCI = mode_constr_goal_info(Occurring, Producing, Consuming,
             MakeVisible, NeedVisible),
         MaybeMCI = yes(MCI)
@@ -3340,7 +3342,8 @@
 
 true_goal = hlds_goal(true_goal_expr, GoalInfo) :-
     instmap_delta_init_reachable(InstMapDelta),
-    goal_info_init(set.init, InstMapDelta, detism_det, purity_pure, GoalInfo).
+    goal_info_init(set_of_var.init, InstMapDelta, detism_det, purity_pure,
+        GoalInfo).
 
 true_goal_expr = conj(plain_conj, []).
 
@@ -3350,7 +3353,7 @@
 
 fail_goal = hlds_goal(fail_goal_expr, GoalInfo) :-
     instmap_delta_init_unreachable(InstMapDelta),
-    goal_info_init(set.init, InstMapDelta, detism_failure, purity_pure,
+    goal_info_init(set_of_var.init, InstMapDelta, detism_failure, purity_pure,
         GoalInfo).
 
 fail_goal_expr = disj([]).
@@ -3362,13 +3365,8 @@
 %-----------------------------------------------------------------------------%
 
 goal_list_nonlocals(Goals, NonLocals) :-
-    UnionNonLocals = (pred(Goal::in, Vars0::in, Vars::out) is det :-
-        Goal = hlds_goal(_, GoalInfo),
-        Vars1 = goal_info_get_nonlocals(GoalInfo),
-        set.union(Vars0, Vars1, Vars)
-    ),
-    set.init(NonLocals0),
-    list.foldl(UnionNonLocals, Goals, NonLocals0, NonLocals).
+    GoalNonLocals = list.map(goal_get_nonlocals, Goals),
+    set_of_var.union_list(GoalNonLocals, NonLocals).
 
 goal_list_instmap_delta(Goals, InstMapDelta) :-
     ApplyDelta = (pred(Goal::in, Delta0::in, Delta::out) is det :-
@@ -3488,7 +3486,7 @@
     Mode = ((free -> Ground) - (Ground -> Ground)),
     Unification = assign(X, Y),
     UnifyContext = unify_context(UnifyMainContext, UnifySubContext),
-    goal_info_init(set.list_to_set([X, Y]), instmap_delta_bind_var(X),
+    goal_info_init(set_of_var.list_to_set([X, Y]), instmap_delta_bind_var(X),
         detism_semi, purity_pure, GoalInfo),
     GoalExpr = unify(X, rhs_var(Y), Mode, Unification, UnifyContext),
     Goal = hlds_goal(GoalExpr, GoalInfo).
@@ -3498,7 +3496,7 @@
     Mode = ((Ground -> Ground) - (Ground -> Ground)),
     Unification = simple_test(X, Y),
     UnifyContext = unify_context(UnifyMainContext, UnifySubContext),
-    goal_info_init(set.list_to_set([X, Y]), instmap_delta_bind_no_var,
+    goal_info_init(set_of_var.list_to_set([X, Y]), instmap_delta_bind_no_var,
         detism_semi, purity_pure, GoalInfo),
     GoalExpr = unify(X, rhs_var(Y), Mode, Unification, UnifyContext),
     Goal = hlds_goal(GoalExpr, GoalInfo).
@@ -3580,7 +3578,7 @@
         construct_dynamically, cell_is_unique, no_construct_sub_info),
     Context = unify_context(umc_explicit, []),
     GoalExpr = unify(Var, RHS, Mode, Unification, Context),
-    set.singleton_set(NonLocals, Var),
+    NonLocals = set_of_var.make_singleton(Var),
     instmap_delta_init_reachable(InstMapDelta0),
     instmap_delta_insert_var(Var, Inst, InstMapDelta0, InstMapDelta),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure, GoalInfo).
@@ -3595,7 +3593,7 @@
         construct_dynamically, cell_is_unique, no_construct_sub_info),
     UnifyContext = unify_context(umc_explicit, []),
     Unify = unify(Var, Rhs, UnifyMode, Unification, UnifyContext),
-    set.list_to_set([Var | Args], NonLocals),
+    set_of_var.list_to_set([Var | Args], NonLocals),
     InstMapDelta = instmap_delta_bind_var(Var),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure, GoalInfo),
     Goal = hlds_goal(Unify, GoalInfo).
@@ -3610,7 +3608,7 @@
     Unification = deconstruct(Var, ConsId, Args, UniModes, cannot_fail,
         cannot_cgc),
     Unify = unify(Var, Rhs, UnifyMode, Unification, UnifyContext),
-    set.list_to_set([Var | Args], NonLocals),
+    set_of_var.list_to_set([Var | Args], NonLocals),
     InstMapDelta = instmap_delta_bind_vars(Args),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_pure, GoalInfo),
     Goal = hlds_goal(Unify, GoalInfo).
Index: compiler/hlds_llds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_llds.m,v
retrieving revision 1.27
diff -u -b -r1.27 hlds_llds.m
--- compiler/hlds_llds.m	21 Jul 2011 06:58:25 -0000	1.27
+++ compiler/hlds_llds.m	15 Aug 2011 06:30:18 -0000
@@ -727,15 +727,6 @@
         PreDeaths, PostDeaths, MaybeFollowVars, StoreMap,
         ResumePoint, MaybeNeed).
 
-:- pred rename_vars_in_set_of_var(must_rename::in,
-    map(prog_var, prog_var)::in,
-    set_of_progvar::in, set_of_progvar::out) is det.
-
-rename_vars_in_set_of_var(Must, Subn, Set0, Set) :-
-    List0 = set_of_var.to_sorted_list(Set0),
-    list.map(rename_var(Must, Subn), List0, List),
-    Set = set_of_var.list_to_set(List).
-
 :- pred rename_vars_in_var_locn_map(must_rename::in,
     map(prog_var, prog_var)::in,
     map(prog_var, abs_locn)::in, map(prog_var, abs_locn)::out) is det.
Index: compiler/hlds_out_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out_goal.m,v
retrieving revision 1.8
diff -u -b -r1.8 hlds_out_goal.m
--- compiler/hlds_out_goal.m	21 Jul 2011 06:58:25 -0000	1.8
+++ compiler/hlds_out_goal.m	15 Aug 2011 11:35:10 -0000
@@ -199,7 +199,7 @@
     ),
     ( string.contains_char(DumpOptions, 'n') ->
         NonLocalsSet = goal_info_get_nonlocals(GoalInfo),
-        set.to_sorted_list(NonLocalsSet, NonLocalsList),
+        set_of_var.to_sorted_list(NonLocalsSet, NonLocalsList),
         (
             NonLocalsList = [_ | _],
             write_indent(Indent, !IO),
@@ -242,8 +242,8 @@
     ),
     ( string.contains_char(DumpOptions, 'B') ->
         ProducingVars = GoalInfo ^ producing_vars,
-        ( set.non_empty(ProducingVars) ->
-            set.to_sorted_list(ProducingVars, ProducingVarsList),
+        ( set_of_var.is_non_empty(ProducingVars) ->
+            set_of_var.to_sorted_list(ProducingVars, ProducingVarsList),
             write_indent(Indent, !IO),
             io.write_string("% producing vars: ", !IO),
             mercury_output_vars(VarSet, AppendVarNums, ProducingVarsList, !IO),
@@ -253,8 +253,8 @@
         ),
 
         ConsumingVars = GoalInfo ^ consuming_vars,
-        ( set.non_empty(ConsumingVars) ->
-            set.to_sorted_list(ConsumingVars, ConsumingVarsList),
+        ( set_of_var.is_non_empty(ConsumingVars) ->
+            set_of_var.to_sorted_list(ConsumingVars, ConsumingVarsList),
             write_indent(Indent, !IO),
             io.write_string("% consuming vars: ", !IO),
             mercury_output_vars(VarSet, AppendVarNums, ConsumingVarsList, !IO),
@@ -264,8 +264,8 @@
         ),
 
         MakeVisibleVars = GoalInfo ^ make_visible_vars,
-        ( set.non_empty(MakeVisibleVars) ->
-            set.to_sorted_list(MakeVisibleVars, MakeVisibleVarsList),
+        ( set_of_var.is_non_empty(MakeVisibleVars) ->
+            set_of_var.to_sorted_list(MakeVisibleVars, MakeVisibleVarsList),
             write_indent(Indent, !IO),
             io.write_string("% make_visible vars: ", !IO),
             mercury_output_vars(VarSet, AppendVarNums, MakeVisibleVarsList,
@@ -276,8 +276,8 @@
         ),
 
         NeedVisibleVars = GoalInfo ^ need_visible_vars,
-        ( set.non_empty(NeedVisibleVars) ->
-            set.to_sorted_list(NeedVisibleVars, NeedVisibleVarsList),
+        ( set_of_var.is_non_empty(NeedVisibleVars) ->
+            set_of_var.to_sorted_list(NeedVisibleVars, NeedVisibleVarsList),
             write_indent(Indent, !IO),
             io.write_string("% need_visible vars: ", !IO),
             mercury_output_vars(VarSet, AppendVarNums, NeedVisibleVarsList,
@@ -395,7 +395,7 @@
         (
             instmap_delta_is_reachable(InstMapDelta),
             instmap_delta_changed_vars(InstMapDelta, Vars),
-            set.empty(Vars)
+            set_of_var.is_empty(Vars)
         ->
             true
         ;
@@ -448,8 +448,8 @@
             yes(LFU) = goal_info_get_maybe_lfu(GoalInfo),
             yes(LBU) = goal_info_get_maybe_lbu(GoalInfo),
             yes(ReuseDescription) = goal_info_get_maybe_reuse(GoalInfo),
-            set.to_sorted_list(LFU, ListLFU),
-            set.to_sorted_list(LBU, ListLBU)
+            set_of_var.to_sorted_list(LFU, ListLFU),
+            set_of_var.to_sorted_list(LBU, ListLBU)
         ->
             write_indent(Indent, !IO),
             io.write_string("% LFU: ", !IO),
Index: compiler/hlds_pred.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_pred.m,v
retrieving revision 1.274
diff -u -b -r1.274 hlds_pred.m
--- compiler/hlds_pred.m	11 Aug 2011 23:18:59 -0000	1.274
+++ compiler/hlds_pred.m	15 Aug 2011 15:31:11 -0000
@@ -34,8 +34,9 @@
 :- import_module mdbcomp.goal_path.
 :- import_module mdbcomp.prim_data.
 :- import_module mdbcomp.program_representation.
-:- import_module parse_tree.prog_data.
 :- import_module parse_tree.error_util.
+:- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.term_constr_main.
 :- import_module transform_hlds.term_util.
 
@@ -63,7 +64,6 @@
 :- import_module libs.options.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_util.
-:- import_module parse_tree.set_of_var.
 
 :- import_module int.
 :- import_module require.
@@ -168,7 +168,7 @@
     % generation. This is *not* the same thing as the notion of liveness
     % used by mode analysis!  See compiler/notes/glossary.html.
     %
-:- type liveness_info == set(prog_var).     % The live variables.
+:- type liveness_info == set_of_progvar.    % The live variables.
 
 :- type arg_info
     --->    arg_info(
@@ -1186,9 +1186,9 @@
         NonLocals = goal_info_get_nonlocals(GoalInfo),
         goal_util.extra_nonlocal_typeinfos(RttiVarMaps, VarTypes0,
             ExistQVars, NonLocals, ExtraTypeInfos0),
-        set.delete_list(ArgVars0, ExtraTypeInfos0, ExtraTypeInfos1),
-        set.to_sorted_list(ExtraTypeInfos1, ExtraTypeInfos),
-        list.append(ExtraTypeInfos, ArgVars0, ArgVars)
+        set_of_var.delete_list(ArgVars0, ExtraTypeInfos0, ExtraTypeInfos1),
+        set_of_var.to_sorted_list(ExtraTypeInfos1, ExtraTypeInfos),
+        ArgVars = ExtraTypeInfos ++ ArgVars0
     ;
         TypeInfoLiveness = no,
         ArgVars = ArgVars0,
@@ -2540,7 +2540,7 @@
     ModeErrors = [],
     InferredDet = detism_erroneous,
     map.init(StackSlots),
-    set.init(InitialLiveness),
+    set_of_var.init(InitialLiveness),
     ArgInfo = no,
     goal_info_init(GoalInfo),
     ClauseBody = hlds_goal(conj(plain_conj, []), GoalInfo),
@@ -2575,7 +2575,7 @@
         InstVarSet, HeadModes, DetismDecl, MaybeDeclaredDetism, Detism,
         Goal, RttiVarMaps, IsAddressTaken, VarNameRemap, ProcInfo) :-
     map.init(StackSlots),
-    set.init(Liveness),
+    set_of_var.init(Liveness),
     MaybeHeadLives = no,
     ModeErrors = [],
     Term2Info = term_constr_main.term2_info_init,
Index: compiler/inlining.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/inlining.m,v
retrieving revision 1.173
diff -u -b -r1.173 inlining.m
--- compiler/inlining.m	23 May 2011 05:08:03 -0000	1.173
+++ compiler/inlining.m	15 Aug 2011 12:46:53 -0000
@@ -160,6 +160,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.complexity.
 :- import_module transform_hlds.dead_proc_elim.
 :- import_module transform_hlds.dependency_graph.
@@ -659,7 +660,7 @@
         % If some of the output variables are not used in the calling
         % procedure, requantify the procedure.
         NonLocals = goal_info_get_nonlocals(GoalInfo0),
-        ( set.list_to_set(ArgVars) = NonLocals ->
+        ( set_of_var.list_to_set(ArgVars) = NonLocals ->
             Requantify = Requantify0
         ;
             Requantify = yes
Index: compiler/instmap.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/instmap.m,v
retrieving revision 1.72
diff -u -b -r1.72 instmap.m
--- compiler/instmap.m	6 May 2011 05:03:21 -0000	1.72
+++ compiler/instmap.m	15 Aug 2011 13:24:55 -0000
@@ -25,11 +25,11 @@
 :- import_module check_hlds.mode_info.
 :- import_module hlds.hlds_module.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module list.
 :- import_module map.
-:- import_module set.
 
 :- type instmap.
 :- type instmap_delta.
@@ -91,7 +91,7 @@
 
     % Return the set of variables in an instmap.
     %
-:- pred instmap_vars(instmap::in, set(prog_var)::out) is det.
+:- pred instmap_vars(instmap::in, set_of_progvar::out) is det.
 
     % Return the list of variables in an instmap.
     %
@@ -104,13 +104,13 @@
     % This predicate shouldn't be used if you want your code to
     % compile on the alias branch, use instmap_changed_vars instead.
     %
-:- pred instmap_delta_changed_vars(instmap_delta::in, set(prog_var)::out)
+:- pred instmap_delta_changed_vars(instmap_delta::in, set_of_progvar::out)
     is det.
 
     % Return the set of variables which has an instantiatedness for which
     % inst_is_bound succeeds.
     %
-:- pred instmap_bound_vars(instmap::in, module_info::in, set(prog_var)::out)
+:- pred instmap_bound_vars(instmap::in, module_info::in, set_of_progvar::out)
     is det.
 
     % instmap_changed_vars(IMA, IMB, MI, CV)
@@ -125,7 +125,7 @@
     % transform more easily to the alias branch.
     %
 :- pred instmap_changed_vars(instmap::in, instmap::in, vartypes::in,
-    module_info::in, set(prog_var)::out) is det.
+    module_info::in, set_of_progvar::out) is det.
 
 %-----------------------------------------------------------------------------%
 
@@ -213,7 +213,7 @@
     % Given two instmaps and a set of variables, compute an instmap delta
     % which records the change in the instantiation state of those variables.
     %
-:- pred compute_instmap_delta(instmap::in, instmap::in, set(prog_var)::in,
+:- pred compute_instmap_delta(instmap::in, instmap::in, set_of_progvar::in,
     instmap_delta::out) is det.
 
     % Given an instmap and an instmap_delta, overlay the entries in the
@@ -240,7 +240,7 @@
     % or if-then-else, and update the instantiatedness of all the nonlocal
     % variables, checking that it is the same for every branch.
     %
-:- pred instmap_merge(set(prog_var)::in, list(instmap)::in, merge_context::in,
+:- pred instmap_merge(set_of_progvar::in, list(instmap)::in, merge_context::in,
     mode_info::in, mode_info::out) is det.
 
     % instmap_unify(NonLocalVars, InstMapNonlocalvarPairs, !ModeInfo):
@@ -251,19 +251,19 @@
     % when modechecking the individual conjuncts ensures that variables
     % have at most one producer.
     %
-:- pred instmap_unify(set(prog_var)::in,
-    assoc_list(instmap, set(prog_var))::in,
+:- pred instmap_unify(set_of_progvar::in,
+    assoc_list(instmap, set_of_progvar)::in,
     mode_info::in, mode_info::out) is det.
 
     % instmap_restrict takes an instmap and a set of vars and returns
     % an instmap with its domain restricted to those vars.
     %
-:- pred instmap_restrict(set(prog_var)::in, instmap::in, instmap::out) is det.
+:- pred instmap_restrict(set_of_progvar::in, instmap::in, instmap::out) is det.
 
     % instmap_delta_restrict takes an instmap and a set of vars and returns
     % an instmap_delta with its domain restricted to those vars.
     %
-:- pred instmap_delta_restrict(set(prog_var)::in,
+:- pred instmap_delta_restrict(set_of_progvar::in,
     instmap_delta::in, instmap_delta::out) is det.
 
     % instmap_delta_delete_vars takes an instmap_delta and a list of vars
@@ -277,7 +277,7 @@
     % instantiated when InstmapDelta is applied to Instmap.
     %
 :- pred instmap_delta_no_output_vars(instmap::in, instmap_delta::in,
-    set(prog_var)::in, vartypes::in, module_info::in) is semidet.
+    set_of_progvar::in, vartypes::in, module_info::in) is semidet.
 
     % merge_instmap_delta(InitialInstMap, NonLocals,
     %   InstMapDeltaA, InstMapDeltaB, !ModuleInfo):
@@ -285,7 +285,7 @@
     % Merge the instmap_deltas of different branches of an if-then-else,
     % disj or switch.
     %
-:- pred merge_instmap_delta(instmap::in, set(prog_var)::in, vartypes::in,
+:- pred merge_instmap_delta(instmap::in, set_of_progvar::in, vartypes::in,
     instmap_delta::in, instmap_delta::in, instmap_delta::out,
     module_info::in, module_info::out) is det.
 
@@ -296,7 +296,7 @@
     % switch, or disj and merges them. This is used in situations
     % where the bindings are known to be compatible.
     %
-:- pred merge_instmap_deltas(instmap::in, set(prog_var)::in, vartypes::in,
+:- pred merge_instmap_deltas(instmap::in, set_of_progvar::in, vartypes::in,
     list(instmap_delta)::in, instmap_delta::out,
     module_info::in, module_info::out) is det.
 
@@ -306,7 +306,7 @@
     % Unify the instmap_deltas of different branches of a parallel
     % conjunction.
     %
-:- pred unify_instmap_delta(instmap::in, set(prog_var)::in, instmap_delta::in,
+:- pred unify_instmap_delta(instmap::in, set_of_progvar::in, instmap_delta::in,
     instmap_delta::in, instmap_delta::out,
     module_info::in, module_info::out) is det.
 
@@ -372,6 +372,7 @@
 :- import_module maybe.
 :- import_module pair.
 :- import_module require.
+:- import_module set.
 :- import_module std_util.
 :- import_module string.
 :- import_module term.
@@ -471,32 +472,32 @@
 
 instmap_vars(Instmap, Vars) :-
     instmap_vars_list(Instmap, VarsList),
-    set.list_to_set(VarsList, Vars).
+    set_of_var.list_to_set(VarsList, Vars).
 
 instmap_vars_list(unreachable, []).
 instmap_vars_list(reachable(InstMapping), VarsList) :-
     map.keys(InstMapping, VarsList).
 
-instmap_bound_vars(unreachable, _ModuleInfo, set.init).
+instmap_bound_vars(unreachable, _ModuleInfo, set_of_var.init).
 instmap_bound_vars(reachable(InstMapping), ModuleInfo, BoundVars) :-
     map.foldl(instmap_bound_vars_2(ModuleInfo), InstMapping,
-        set.init, BoundVars).
+        set_of_var.init, BoundVars).
 
 :- pred instmap_bound_vars_2(module_info::in, prog_var::in, mer_inst::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 instmap_bound_vars_2(ModuleInfo, Var, Inst, !BoundVars) :-
     ( inst_is_bound(ModuleInfo, Inst) ->
-        set.insert(Var, !BoundVars)
+        set_of_var.insert(Var, !BoundVars)
     ;
         true
     ).
 
 instmap_delta_changed_vars(unreachable, EmptySet) :-
-    set.init(EmptySet).
+    set_of_var.init(EmptySet).
 instmap_delta_changed_vars(reachable(InstMapping), ChangedVars) :-
     map.keys(InstMapping, ChangedVarsList),
-    set.sorted_list_to_set(ChangedVarsList, ChangedVars).
+    set_of_var.sorted_list_to_set(ChangedVarsList, ChangedVars).
 
 %-----------------------------------------------------------------------------%
 
@@ -506,11 +507,11 @@
         ChangedVars).
 
 :- pred instmap_changed_vars_2(prog_vars::in, instmap::in, instmap::in,
-    vartypes::in, module_info::in, set(prog_var)::out) is det.
+    vartypes::in, module_info::in, set_of_progvar::out) is det.
 
 instmap_changed_vars_2([], _InstMapA, _InstMapB, _Types,
         _ModuleInfo, ChangedVars) :-
-    set.init(ChangedVars).
+    set_of_var.init(ChangedVars).
 instmap_changed_vars_2([VarB | VarBs], InstMapA, InstMapB, VarTypes,
         ModuleInfo, ChangedVars) :-
     instmap_changed_vars_2(VarBs, InstMapA, InstMapB, VarTypes,
@@ -523,7 +524,7 @@
     ( inst_matches_final_typed(InitialInst, FinalInst, Type, ModuleInfo) ->
         ChangedVars = ChangedVars0
     ;
-        set.insert(VarB, ChangedVars0, ChangedVars)
+        set_of_var.insert(VarB, ChangedVars0, ChangedVars)
     ).
 
 %-----------------------------------------------------------------------------%
@@ -835,12 +836,14 @@
 
 instmap_restrict(_, unreachable, unreachable).
 instmap_restrict(Vars, reachable(InstMapping0), reachable(InstMapping)) :-
-    map.select(InstMapping0, Vars, InstMapping).
+    map.select_sorted_list(InstMapping0, set_of_var.to_sorted_list(Vars),
+        InstMapping).
 
 instmap_delta_restrict(_, unreachable, unreachable).
 instmap_delta_restrict(Vars,
         reachable(InstMapping0), reachable(InstMapping)) :-
-    map.select(InstMapping0, Vars, InstMapping).
+    map.select_sorted_list(InstMapping0, set_of_var.to_sorted_list(Vars),
+        InstMapping).
 
 instmap_delta_delete_vars(_, unreachable, unreachable).
 instmap_delta_delete_vars(Vars,
@@ -860,7 +863,7 @@
         InstMap0 = reachable(InstMapping0),
         InstMappingList = [_ | _]
     ->
-        set.to_sorted_list(NonLocals, NonLocalsList),
+        set_of_var.to_sorted_list(NonLocals, NonLocalsList),
         mode_info_get_var_types(!.ModeInfo, VarTypes),
         merge_insts_of_vars(NonLocalsList, InstMapList, VarTypes,
             InstMapping0, InstMapping, ModuleInfo0, ModuleInfo, ErrorList),
@@ -868,7 +871,7 @@
         (
             ErrorList = [FirstError | _],
             FirstError = merge_error(Var, _),
-            set.singleton_set(WaitingVars, Var),
+            WaitingVars = set_of_var.make_singleton(Var),
             mode_info_error(WaitingVars,
                 mode_error_disj(MergeContext, ErrorList), !ModeInfo)
         ;
@@ -1052,7 +1055,7 @@
             MergedDelta, !ModuleInfo)
     ).
 
-:- pred merge_instmap_deltas_2(instmap::in, set(prog_var)::in, vartypes::in,
+:- pred merge_instmap_deltas_2(instmap::in, set_of_progvar::in, vartypes::in,
     list(instmap_delta)::in, list(instmap_delta)::in, list(instmap_delta)::out,
     module_info::in, module_info::out) is det.
 
@@ -1110,7 +1113,7 @@
         % call unify_insts_of_vars which unifies each of the nonlocals from
         % each instmap with the corresponding inst in the accumulator.
         mode_info_get_module_info(!.ModeInfo, ModuleInfo0),
-        set.to_sorted_list(NonLocals, NonLocalsList),
+        set_of_var.to_sorted_list(NonLocals, NonLocalsList),
         unify_insts_of_vars(NonLocalsList, InstMap0, InstMapList1,
             ModuleInfo0, ModuleInfo, InstMapping0, InstMapping, ErrorList),
         mode_info_set_module_info(ModuleInfo, !ModeInfo),
@@ -1120,7 +1123,7 @@
         (
             ErrorList = [FirstError | _],
             FirstError = merge_error(Var, _),
-            set.singleton_set(WaitingVars, Var),
+            WaitingVars = set_of_var.make_singleton(Var),
             mode_info_error(WaitingVars,
                 mode_error_par_conj(ErrorList), !ModeInfo)
         ;
@@ -1141,7 +1144,7 @@
     % incompatible.
     %
 :- pred unify_insts_of_vars(list(prog_var)::in, instmap::in,
-    list(pair(instmap, set(prog_var)))::in, module_info::in, module_info::out,
+    list(pair(instmap, set_of_progvar))::in, module_info::in, module_info::out,
     map(prog_var, mer_inst)::in, map(prog_var, mer_inst)::out,
     merge_errors::out) is det.
 
@@ -1169,7 +1172,7 @@
     % corresponding `InstMaps'. Let `Error' be yes iff there are two
     % instmaps for which the inst of `Var' is incompatible.
     %
-:- pred unify_var_insts(list(pair(instmap, set(prog_var)))::in,
+:- pred unify_var_insts(list(pair(instmap, set_of_progvar))::in,
     prog_var::in, list(mer_inst)::in, list(mer_inst)::out,
     mer_inst::in, mer_inst::out, module_info::in, module_info::out,
     bool::in, bool::out) is det.
@@ -1177,7 +1180,7 @@
 unify_var_insts([], _, !Insts, !Inst, !ModuleInfo, !Error).
 unify_var_insts([InstMap - Nonlocals| Rest], Var, !InstList, !Inst,
         !ModuleInfo, !Error) :-
-    ( set.member(Var, Nonlocals) ->
+    ( set_of_var.member(Nonlocals, Var) ->
         instmap_lookup_var(InstMap, Var, VarInst),
         (
             % We can ignore the determinism of the unification:
@@ -1203,7 +1206,7 @@
 compute_instmap_delta(reachable(_), unreachable, _, unreachable).
 compute_instmap_delta(reachable(InstMapA), reachable(InstMapB), NonLocals,
         reachable(DeltaInstMap)) :-
-    set.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
     compute_instmap_delta_2(NonLocalsList, InstMapA, InstMapB, AssocList),
     map.from_sorted_assoc_list(AssocList, DeltaInstMap).
 
@@ -1226,7 +1229,7 @@
 
 instmap_delta_no_output_vars(_, unreachable, _, _, _).
 instmap_delta_no_output_vars(InstMap0, reachable(InstMapDelta), Vars, VT, M) :-
-    set.to_sorted_list(Vars, VarList),
+    set_of_var.to_sorted_list(Vars, VarList),
     instmap_delta_no_output_vars_2(VarList, InstMap0, InstMapDelta, VT, M).
 
 :- pred instmap_delta_no_output_vars_2(list(prog_var)::in, instmap::in,
@@ -1274,7 +1277,7 @@
     merge_instmapping_delta(InstMap, NonLocals, VarTypes,
         InstMappingA, InstMappingB, InstMapping, !ModuleInfo).
 
-:- pred merge_instmapping_delta(instmap::in, set(prog_var)::in, vartypes::in,
+:- pred merge_instmapping_delta(instmap::in, set_of_progvar::in, vartypes::in,
     instmapping::in, instmapping::in, instmapping::out,
     module_info::in, module_info::out) is det.
 
@@ -1282,10 +1285,10 @@
         InstMappingA, InstMappingB, InstMapping, !ModuleInfo) :-
     map.keys(InstMappingA, VarsInA),
     map.keys(InstMappingB, VarsInB),
-    set.sorted_list_to_set(VarsInA, SetofVarsInA),
-    set.insert_list(VarsInB, SetofVarsInA, SetofVars0),
-    set.intersect(SetofVars0, NonLocals, SetofVars),
-    set.to_sorted_list(SetofVars, ListofVars),
+    set_of_var.sorted_list_to_set(VarsInA, SetofVarsInA),
+    set_of_var.insert_list(VarsInB, SetofVarsInA, SetofVars0),
+    set_of_var.intersect(SetofVars0, NonLocals, SetofVars),
+    set_of_var.to_sorted_list(SetofVars, ListofVars),
     merge_instmapping_delta_2(ListofVars, InstMap, VarTypes,
         InstMappingA, InstMappingB, map.init, InstMapping, !ModuleInfo).
 
@@ -1352,7 +1355,7 @@
     unify_instmapping_delta(InstMap, NonLocals, InstMappingA, InstMappingB,
         InstMapping, !ModuleInfo).
 
-:- pred unify_instmapping_delta(instmap::in, set(prog_var)::in,
+:- pred unify_instmapping_delta(instmap::in, set_of_progvar::in,
     instmapping::in, instmapping::in, instmapping::out,
     module_info::in, module_info::out) is det.
 
@@ -1360,10 +1363,10 @@
         InstMapping, !ModuleInfo) :-
     map.keys(InstMappingA, VarsInA),
     map.keys(InstMappingB, VarsInB),
-    set.sorted_list_to_set(VarsInA, SetofVarsInA),
-    set.insert_list(VarsInB, SetofVarsInA, SetofVars0),
-    set.intersect(SetofVars0, NonLocals, SetofVars),
-    set.to_sorted_list(SetofVars, ListofVars),
+    set_of_var.sorted_list_to_set(VarsInA, SetofVarsInA),
+    set_of_var.insert_list(VarsInB, SetofVarsInA, SetofVars0),
+    set_of_var.intersect(SetofVars0, NonLocals, SetofVars),
+    set_of_var.to_sorted_list(SetofVars, ListofVars),
     unify_instmapping_delta_2(ListofVars, InstMap, InstMappingA, InstMappingB,
         map.init, InstMapping, !ModuleInfo).
 
Index: compiler/lambda.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lambda.m,v
retrieving revision 1.151
diff -u -b -r1.151 lambda.m
--- compiler/lambda.m	23 May 2011 05:08:04 -0000	1.151
+++ compiler/lambda.m	15 Aug 2011 11:08:29 -0000
@@ -99,6 +99,7 @@
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module array.
@@ -373,7 +374,7 @@
     ExistQVars = [],
     LambdaGoal = hlds_goal(_, LambdaGoalInfo),
     LambdaGoalNonLocals = goal_info_get_nonlocals(LambdaGoalInfo),
-    set.insert_list(Vars, LambdaGoalNonLocals, LambdaNonLocals),
+    set_of_var.insert_list(Vars, LambdaGoalNonLocals, LambdaNonLocals),
     goal_util.extra_nonlocal_typeinfos(RttiVarMaps, VarTypes, ExistQVars,
         LambdaNonLocals, ExtraTypeInfos),
     OrigVars = OrigNonLocals0,
@@ -389,14 +390,14 @@
         unexpected($module, $pred, "unexpected unification")
     ),
 
-    set.delete_list(Vars, LambdaGoalNonLocals, NonLocals1),
+    set_of_var.delete_list(Vars, LambdaGoalNonLocals, NonLocals1),
 
     % We need all the typeinfos, including the ones that are not used,
     % for the layout structure describing the closure.
-    set.difference(ExtraTypeInfos, NonLocals1, NewTypeInfos),
-    set.union(NonLocals1, NewTypeInfos, NonLocals),
+    set_of_var.difference(ExtraTypeInfos, NonLocals1, NewTypeInfos),
+    set_of_var.union(NonLocals1, NewTypeInfos, NonLocals),
 
-    ( set.empty(NewTypeInfos) ->
+    ( set_of_var.is_empty(NewTypeInfos) ->
         MustRecomputeNonLocals = MustRecomputeNonLocals0
     ;
         % If we added variables to the nonlocals of the lambda goal, then
@@ -404,7 +405,7 @@
         MustRecomputeNonLocals = yes
     ),
 
-    set.to_sorted_list(NonLocals, ArgVars1),
+    set_of_var.to_sorted_list(NonLocals, ArgVars1),
 
     (
         % Optimize a special case: replace
Index: compiler/live_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/live_vars.m,v
retrieving revision 1.147
diff -u -b -r1.147 live_vars.m
--- compiler/live_vars.m	22 Jul 2011 03:31:41 -0000	1.147
+++ compiler/live_vars.m	15 Aug 2011 13:22:30 -0000
@@ -276,8 +276,8 @@
             % must be available in a stackslot past the parallel conjunction
             % as well.
             NonLocals = goal_info_get_code_gen_nonlocals(GoalInfo0),
-            LiveSet = set_of_var.union_list([set_to_bitset(NonLocals),
-                !.Liveness, ResumeVars0]),
+            LiveSet =
+                set_of_var.union_list([NonLocals, !.Liveness, ResumeVars0]),
 
             InnerNonLocals = LiveSet `set_of_var.union` OuterNonLocals,
             InnerParStackVars0 =
Index: compiler/liveness.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/liveness.m,v
retrieving revision 1.182
diff -u -b -r1.182 liveness.m
--- compiler/liveness.m	25 Jul 2011 07:54:16 -0000	1.182
+++ compiler/liveness.m	15 Aug 2011 15:38:12 -0000
@@ -216,18 +216,6 @@
 :- import_module term.
 :- import_module varset.
 
-% We should switch to using tree_bitset(prog_var) instead. Unfortunately,
-% that is not easy, for two reasons.
-% 
-% - The job of this module is to fill in the pre_birth, post_birth, pre_death
-%   and post_death slots in goal_infos, and those fields are currently
-%   set(prog_var). Changing them to be set_of_var would be a nontrivial job.
-%
-% - The predicates that deal with RTTI information use set(prog_var). We would
-%   need to either change them to use set_of_var, or do conversions before and
-%   after all calls to them.
-%
-
 %-----------------------------------------------------------------------------%
 
 detect_liveness_preds_parallel(!HLDS) :-
@@ -347,19 +335,18 @@
         eff_trace_level_needs_fail_vars(ModuleInfo, PredInfo, !.ProcInfo,
             TraceLevel) = yes
     ->
-        trace_fail_vars(ModuleInfo, !.ProcInfo, ResumeVars0),
-        ResumeVars1 = set_to_bitset(ResumeVars0)
+        trace_fail_vars(ModuleInfo, !.ProcInfo, ResumeVars0)
     ;
-        ResumeVars1 = set_of_var.init
+        ResumeVars0 = set_of_var.init
     ),
     detect_resume_points_in_goal(Goal3, Goal, Liveness0, _,
-        LiveInfo, ResumeVars1),
+        LiveInfo, ResumeVars0),
     trace [io(!IO)] (
         maybe_debug_liveness(ModuleInfo, "\nafter resume point",
             DebugLiveness, PredIdInt, VarSet, Goal, !IO)
     ),
     proc_info_set_goal(Goal, !ProcInfo),
-    proc_info_set_liveness_info(bitset_to_set(Liveness0), !ProcInfo).
+    proc_info_set_liveness_info(Liveness0, !ProcInfo).
 
 :- pred maybe_debug_liveness(module_info::in, string::in, int::in, int::in,
     prog_varset::in, hlds_goal::in, io::di, io::uo) is det.
@@ -1754,7 +1741,7 @@
 
     module_info_get_globals(ModuleInfo, Globals),
     proc_info_get_goal(ProcInfo, hlds_goal(_Goal, GoalInfo)),
-    NonLocals0 = set_to_bitset(goal_info_get_code_gen_nonlocals(GoalInfo)),
+    NonLocals0 = goal_info_get_code_gen_nonlocals(GoalInfo),
     module_info_pred_info(ModuleInfo, PredId, PredInfo),
     proc_info_get_rtti_varmaps(ProcInfo, RttiVarMaps),
     body_should_use_typeinfo_liveness(PredInfo, Globals, TypeinfoLiveness),
@@ -1857,7 +1844,7 @@
 
 get_nonlocals_and_typeinfos(LiveInfo, GoalInfo,
         NonLocals, CompletedNonLocals) :-
-    NonLocals = set_to_bitset(goal_info_get_code_gen_nonlocals(GoalInfo)),
+    NonLocals = goal_info_get_code_gen_nonlocals(GoalInfo),
     maybe_complete_with_typeinfos(LiveInfo, NonLocals, CompletedNonLocals).
 
 :- pred maybe_complete_with_typeinfos(live_info::in,
Index: compiler/llds_out_file.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/llds_out_file.m,v
retrieving revision 1.8
diff -u -b -r1.8 llds_out_file.m
--- compiler/llds_out_file.m	23 May 2011 05:08:04 -0000	1.8
+++ compiler/llds_out_file.m	15 Aug 2011 04:29:32 -0000
@@ -124,12 +124,14 @@
 
 output_c_file_mercury_headers(Info, !IO) :-
     io.write_string("#define MR_ALLOW_RESET\n", !IO),
-    TraceLevel = Info ^ lout_trace_level,
-    ( given_trace_level_is_none(TraceLevel) = no ->
         io.write_string("#include ""mercury_imp.h""\n", !IO),
+    TraceLevel = Info ^ lout_trace_level,
+    TraceLevelIsNone = given_trace_level_is_none(TraceLevel),
+    (
+        TraceLevelIsNone = no,
         io.write_string("#include ""mercury_trace_base.h""\n", !IO)
     ;
-        io.write_string("#include ""mercury_imp.h""\n", !IO)
+        TraceLevelIsNone = yes
     ),
     DeepProfile = Info ^ lout_profile_deep,
     (
Index: compiler/lookup_switch.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lookup_switch.m,v
retrieving revision 1.94
diff -u -b -r1.94 lookup_switch.m
--- compiler/lookup_switch.m	9 Aug 2011 05:34:33 -0000	1.94
+++ compiler/lookup_switch.m	15 Aug 2011 13:21:56 -0000
@@ -184,7 +184,7 @@
     % end because we may have allocated some new static ground terms.
 
     figure_out_output_vars(!.CI, GoalInfo, OutVars),
-    set.list_to_set(OutVars, ArmNonLocals),
+    set_of_var.list_to_set(OutVars, ArmNonLocals),
     remember_position(!.CI, CurPos),
     generate_constants_for_lookup_switch(GetTag, TaggedCases,
         OutVars, ArmNonLocals, StoreMap, Liveness, map.init, CaseSolnMap,
@@ -214,7 +214,7 @@
 
 :- pred generate_constants_for_lookup_switch(
     pred(cons_tag, Key)::in(pred(in, out) is det),
-    list(tagged_case)::in, list(prog_var)::in, set(prog_var)::in,
+    list(tagged_case)::in, list(prog_var)::in, set_of_progvar::in,
     abs_store_map::in, set_of_progvar::out,
     map(Key, soln_consts(rval))::in, map(Key, soln_consts(rval))::out,
     branch_end::in, branch_end::out, set_of_progvar::in, set_of_progvar::out,
Index: compiler/lookup_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lookup_util.m,v
retrieving revision 1.17
diff -u -b -r1.17 lookup_util.m
--- compiler/lookup_util.m	21 Jul 2011 06:58:25 -0000	1.17
+++ compiler/lookup_util.m	15 Aug 2011 13:23:03 -0000
@@ -103,7 +103,7 @@
         instmap.apply_instmap_delta(CurrentInstMap, InstMapDelta,
             InstMapAfter),
         list.filter(is_output_var(ModuleInfo, CurrentInstMap, InstMapAfter),
-            set.to_sorted_list(ChangedVars), OutVars)
+            set_of_var.to_sorted_list(ChangedVars), OutVars)
     ).
 
 :- pred is_output_var(module_info::in, instmap::in, instmap::in, prog_var::in)
Index: compiler/loop_inv.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/loop_inv.m,v
retrieving revision 1.58
diff -u -b -r1.58 loop_inv.m
--- compiler/loop_inv.m	23 May 2011 05:08:05 -0000	1.58
+++ compiler/loop_inv.m	15 Aug 2011 12:48:20 -0000
@@ -128,6 +128,7 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module cord.
@@ -573,7 +574,7 @@
 has_uniquely_used_arg(UUVs, hlds_goal(_GoalExpr, GoalInfo)) :-
     NonLocals = goal_info_get_nonlocals(GoalInfo),
     list.member(UUV, UUVs),
-    set.member(UUV, NonLocals).
+    set_of_var.member(NonLocals, UUV).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/make_hlds_warn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/make_hlds_warn.m,v
retrieving revision 1.40
diff -u -b -r1.40 make_hlds_warn.m
--- compiler/make_hlds_warn.m	11 Aug 2011 06:38:13 -0000	1.40
+++ compiler/make_hlds_warn.m	15 Aug 2011 06:26:13 -0000
@@ -76,10 +76,10 @@
 :- import_module libs.options.
 :- import_module parse_tree.mercury_to_mercury.
 :- import_module parse_tree.prog_out.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module char.
-:- import_module set.
 :- import_module string.
 :- import_module varset.
 
@@ -140,14 +140,14 @@
     % be singletons, but aren't.
 
     Info0 = warn_info(ModuleInfo, PredCallId, VarSet,
-        [], set.init, set.init, context_init),
-    QuantVars = set.init,
+        [], set_of_var.init, set_of_var.init, context_init),
+    QuantVars = set_of_var.init,
     warn_singletons_in_goal(Body, QuantVars, Info0, Info),
     Info = warn_info(_ModuleInfo, _PredCallId, _VarSet,
         NewSpecs, SingletonHeadVarsSet, MultiHeadVarsSet, HeadContext),
     !:Specs = NewSpecs ++ !.Specs,
-    set.to_sorted_list(SingletonHeadVarsSet, SingletonHeadVars),
-    set.to_sorted_list(MultiHeadVarsSet, MultiHeadVars),
+    set_of_var.to_sorted_list(SingletonHeadVarsSet, SingletonHeadVars),
+    set_of_var.to_sorted_list(MultiHeadVarsSet, MultiHeadVars),
     (
         SingletonHeadVars = []
     ;
@@ -179,11 +179,11 @@
                 wi_specs                :: list(error_spec),
 
                 % The set of variables that occur singleton in the clause head.
-                wi_singleton_headvars   :: set(prog_var),
+                wi_singleton_headvars   :: set_of_progvar,
 
                 % The set of variables that occur more than once in the clause
                 % head, even though their names say they SHOULD be singletons.
-                wi_multi_headvars       :: set(prog_var),
+                wi_multi_headvars       :: set_of_progvar,
 
                 % The context of the clause head. Should be set to a meaningful
                 % value if either wi_singleton_headvars or wi_multi_headvars
@@ -196,7 +196,7 @@
                 wi_head_context         :: prog_context
             ).
 
-:- pred warn_singletons_in_goal(hlds_goal::in, set(prog_var)::in,
+:- pred warn_singletons_in_goal(hlds_goal::in, set_of_progvar::in,
     warn_info::in, warn_info::out) is det.
 
 warn_singletons_in_goal(Goal, QuantVars, !Info) :-
@@ -223,10 +223,10 @@
             (
                 Vars = [_ | _],
                 SubGoalVars = free_goal_vars(SubGoal),
-                set.init(EmptySet),
+                set_of_var.init(EmptySet),
                 warn_singletons_goal_vars(Vars, GoalInfo, EmptySet,
                     SubGoalVars, !Info),
-                set.insert_list(Vars, QuantVars, SubQuantVars)
+                set_of_var.insert_list(Vars, QuantVars, SubQuantVars)
             ;
                 Vars = [],
                 SubQuantVars = QuantVars
@@ -260,14 +260,14 @@
             Vars = [_ | _],
             CondVars = free_goal_vars(Cond),
             ThenVars = free_goal_vars(Then),
-            set.union(CondVars, ThenVars, CondThenVars),
-            set.init(EmptySet),
+            set_of_var.union(CondVars, ThenVars, CondThenVars),
+            set_of_var.init(EmptySet),
             warn_singletons_goal_vars(Vars, GoalInfo, EmptySet, CondThenVars,
                 !Info)
         ;
             Vars = []
         ),
-        set.insert_list(Vars, QuantVars, CondThenQuantVars),
+        set_of_var.insert_list(Vars, QuantVars, CondThenQuantVars),
         warn_singletons_in_goal(Cond, CondThenQuantVars, !Info),
         warn_singletons_in_goal(Then, CondThenQuantVars, !Info),
         warn_singletons_in_goal(Else, QuantVars, !Info)
@@ -302,7 +302,8 @@
             ShortHand = atomic_goal(_GoalType, _Outer, Inner,
                 _MaybeOutputVars, MainGoal, OrElseGoals, _OrElseInners),
             Inner = atomic_interface_vars(InnerDI, InnerUO),
-            set.insert_list([InnerDI, InnerUO], QuantVars, InsideQuantVars),
+            set_of_var.insert_list([InnerDI, InnerUO],
+                QuantVars, InsideQuantVars),
             warn_singletons_in_goal(MainGoal, InsideQuantVars, !Info),
             warn_singletons_in_goal_list(OrElseGoals, InsideQuantVars, !Info)
         ;
@@ -314,7 +315,7 @@
         )
     ).
 
-:- pred warn_singletons_in_goal_list(list(hlds_goal)::in, set(prog_var)::in,
+:- pred warn_singletons_in_goal_list(list(hlds_goal)::in, set_of_progvar::in,
     warn_info::in, warn_info::out) is det.
 
 warn_singletons_in_goal_list([], _, !Info).
@@ -322,7 +323,7 @@
     warn_singletons_in_goal(Goal, QuantVars, !Info),
     warn_singletons_in_goal_list(Goals, QuantVars, !Info).
 
-:- pred warn_singletons_in_cases(list(case)::in, set(prog_var)::in,
+:- pred warn_singletons_in_cases(list(case)::in, set_of_progvar::in,
     warn_info::in, warn_info::out) is det.
 
 warn_singletons_in_cases([], _, !Info).
@@ -332,7 +333,7 @@
     warn_singletons_in_cases(Cases, QuantVars, !Info).
 
 :- pred warn_singletons_in_unify(prog_var::in,
-    unify_rhs::in, hlds_goal_info::in, set(prog_var)::in,
+    unify_rhs::in, hlds_goal_info::in, set_of_progvar::in,
     warn_info::in, warn_info::out) is det.
 
 warn_singletons_in_unify(X, RHS, GoalInfo, QuantVars, !Info) :-
@@ -374,7 +375,7 @@
     % Omit the warning if GoalInfo says we should.
     %
 :- pred warn_singletons_goal_vars(list(prog_var)::in,
-    hlds_goal_info::in, set(prog_var)::in, set(prog_var)::in,
+    hlds_goal_info::in, set_of_progvar::in, set_of_progvar::in,
     warn_info::in, warn_info::out) is det.
 
 warn_singletons_goal_vars(GoalVars, GoalInfo, NonLocals, QuantVars, !Info) :-
@@ -400,7 +401,8 @@
     ;
         ( goal_info_has_feature(GoalInfo, feature_from_head) ->
             SingleHeadVars0 = !.Info ^ wi_singleton_headvars,
-            set.insert_list(SingleVars, SingleHeadVars0, SingleHeadVars),
+            set_of_var.insert_list(SingleVars,
+                SingleHeadVars0, SingleHeadVars),
             !Info ^ wi_singleton_headvars := SingleHeadVars,
             !Info ^ wi_head_context := goal_info_get_context(GoalInfo)
         ;
@@ -421,7 +423,7 @@
         MultiVars = [_ | _],
         ( goal_info_has_feature(GoalInfo, feature_from_head) ->
             MultiHeadVars0 = !.Info ^ wi_multi_headvars,
-            set.insert_list(MultiVars, MultiHeadVars0, MultiHeadVars),
+            set_of_var.insert_list(MultiVars, MultiHeadVars0, MultiHeadVars),
             !Info ^ wi_multi_headvars := MultiHeadVars,
             !Info ^ wi_head_context := goal_info_get_context(GoalInfo)
         ;
@@ -591,24 +593,24 @@
         TheRest = CodeChars
     ).
 
-:- pred is_singleton_var(set(prog_var)::in,
-    set(prog_var)::in, prog_varset::in, prog_var::in) is semidet.
+:- pred is_singleton_var(set_of_progvar::in,
+    set_of_progvar::in, prog_varset::in, prog_var::in) is semidet.
 
 is_singleton_var(NonLocals, QuantVars, VarSet, Var) :-
-    \+ set.member(Var, NonLocals),
+    \+ set_of_var.member(NonLocals, Var),
     varset.search_name(VarSet, Var, Name),
     \+ string.prefix(Name, "_"),
     \+ string.prefix(Name, "DCG_"),
     \+ (
-        set.member(QuantVar, QuantVars),
+        set_of_var.member(QuantVars, QuantVar),
         varset.search_name(VarSet, QuantVar, Name)
     ).
 
-:- pred is_multi_var(set(prog_var)::in, prog_varset::in, prog_var::in)
+:- pred is_multi_var(set_of_progvar::in, prog_varset::in, prog_var::in)
     is semidet.
 
 is_multi_var(NonLocals, VarSet, Var) :-
-    set.member(Var, NonLocals),
+    set_of_var.member(NonLocals, Var),
     varset.search_name(VarSet, Var, Name),
     string.prefix(Name, "_").
 
Index: compiler/ml_accurate_gc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_accurate_gc.m,v
retrieving revision 1.6
diff -u -b -r1.6 ml_accurate_gc.m
--- compiler/ml_accurate_gc.m	23 May 2011 05:08:05 -0000	1.6
+++ compiler/ml_accurate_gc.m	15 Aug 2011 11:33:01 -0000
@@ -91,6 +91,7 @@
 :- import_module ml_backend.ml_code_util.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module counter.
@@ -292,7 +293,7 @@
     NonLocalsList = list.map(
         (func(hlds_goal(_GX, GI)) = goal_info_get_nonlocals(GI)),
         HLDS_TypeInfoGoals),
-    NonLocals = set.union_list(NonLocalsList),
+    NonLocals = set_of_var.union_list(NonLocalsList),
     InstMapDelta = instmap_delta_bind_var(TypeInfoVar),
     goal_info_init(NonLocals, InstMapDelta, detism_det, purity_impure,
         GoalInfo),
@@ -335,7 +336,7 @@
                 mercury_type_to_mlds_type(ModuleInfo, LocalVarType),
                 gc_no_stmt, MLDS_Context)
         ),
-    set.to_sorted_list(NonLocals, NonLocalVarList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalVarList),
     MLDS_NonLocalVarDecls = list.map(GenLocalVarDecl, NonLocalVarList),
 
     % Combine the MLDS code fragments together.
Index: compiler/ml_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_code_gen.m,v
retrieving revision 1.231
diff -u -b -r1.231 ml_code_gen.m
--- compiler/ml_code_gen.m	23 May 2011 05:08:06 -0000	1.231
+++ compiler/ml_code_gen.m	15 Aug 2011 11:32:22 -0000
@@ -523,6 +523,7 @@
 :- import_module ml_backend.ml_switch_gen.
 :- import_module ml_backend.ml_unify_gen.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module map.
@@ -700,8 +701,8 @@
 find_vars_to_declare(VarTypes, GoalExpr, GoalInfo, VarsToDeclare) :-
     goal_expr_find_subgoal_nonlocals(GoalExpr, SubGoalNonLocals),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    set.difference(SubGoalNonLocals, NonLocals, VarsToDeclareSet),
-    set.to_sorted_list(VarsToDeclareSet, VarsToDeclare0),
+    set_of_var.difference(SubGoalNonLocals, NonLocals, VarsToDeclareSet),
+    set_of_var.to_sorted_list(VarsToDeclareSet, VarsToDeclare0),
     (
         ( VarsToDeclare0 = []
         ; VarsToDeclare0 = [_]
@@ -738,7 +739,7 @@
     % arguments from calls and unifications.
     %
 :- pred goal_expr_find_subgoal_nonlocals(hlds_goal_expr::in,
-    set(prog_var)::out) is det.
+    set_of_progvar::out) is det.
 
 goal_expr_find_subgoal_nonlocals(GoalExpr, SubGoalNonLocals) :-
     (
@@ -750,17 +751,17 @@
             % or a region to construct the term in, but both of those require
             % that variable to be nonlocal to GoalExpr, which means that they
             % would be subtracted from SubGoalNonLocals by our caller anyway.
-            SubGoalNonLocals = set.list_to_set([LHSVar | ArgVars])
+            SubGoalNonLocals = set_of_var.list_to_set([LHSVar | ArgVars])
         ;
             Unification = deconstruct(LHSVar, _ConsId, ArgVars, _ArgModes,
                 _CanFail, _CanCGC),
-            SubGoalNonLocals = set.list_to_set([LHSVar | ArgVars])
+            SubGoalNonLocals = set_of_var.list_to_set([LHSVar | ArgVars])
         ;
             Unification = assign(LHSVar, RHSVar),
-            SubGoalNonLocals = set.list_to_set([LHSVar, RHSVar])
+            SubGoalNonLocals = set_of_var.list_to_set([LHSVar, RHSVar])
         ;
             Unification = simple_test(LHSVar, RHSVar),
-            SubGoalNonLocals = set.list_to_set([LHSVar, RHSVar])
+            SubGoalNonLocals = set_of_var.list_to_set([LHSVar, RHSVar])
         ;
             Unification = complicated_unify(_, _, _),
             unexpected($module, $pred, "complicated_unify")
@@ -768,29 +769,29 @@
     ;
         GoalExpr = plain_call(_PredId, _ProcId, ArgVars, _Builtin,
             _Unify_context, _SymName),
-        SubGoalNonLocals = set.list_to_set(ArgVars)
+        SubGoalNonLocals = set_of_var.list_to_set(ArgVars)
     ;
         GoalExpr = generic_call(GenericCall, ArgVars, _Modes, _Detism),
         (
             GenericCall = higher_order(HOVar, _Purity, _Kind, _Arity),
-            SubGoalNonLocals = set.list_to_set([HOVar | ArgVars])
+            SubGoalNonLocals = set_of_var.list_to_set([HOVar | ArgVars])
         ;
             GenericCall = class_method(MethodVar, _MethodNum, _MethodClassId,
                 _Name),
-            SubGoalNonLocals = set.list_to_set([MethodVar | ArgVars])
+            SubGoalNonLocals = set_of_var.list_to_set([MethodVar | ArgVars])
         ;
             GenericCall = event_call(_Eventname),
-            SubGoalNonLocals = set.list_to_set(ArgVars)
+            SubGoalNonLocals = set_of_var.list_to_set(ArgVars)
         ;
             GenericCall = cast(_CastKind),
-            SubGoalNonLocals = set.list_to_set(ArgVars)
+            SubGoalNonLocals = set_of_var.list_to_set(ArgVars)
         )
     ;
         GoalExpr = call_foreign_proc(_Attr, _PredId, _ProcId, Args, ExtraArgs,
             _TraceCond, _Impl),
         ArgVars = list.map(foreign_arg_var, Args),
         ExtraVars = list.map(foreign_arg_var, ExtraArgs),
-        SubGoalNonLocals = set.list_to_set(ExtraVars ++ ArgVars)
+        SubGoalNonLocals = set_of_var.list_to_set(ExtraVars ++ ArgVars)
     ;
         ( GoalExpr = negation(SubGoal)
         ; GoalExpr = scope(_Reason, SubGoal)
@@ -805,40 +806,41 @@
         ( GoalExpr = conj(_, SubGoals)
         ; GoalExpr = disj(SubGoals)
         ),
-        goals_find_subgoal_nonlocals(SubGoals, set.init, SubGoalNonLocals)
+        goals_find_subgoal_nonlocals(SubGoals,
+            set_of_var.init, SubGoalNonLocals)
     ;
         GoalExpr = if_then_else(_Vars, Cond, Then, Else),
         % The value of _Vars is not guaranteed to contain the set of variables
         % shared between only Cond and Then.
         goals_find_subgoal_nonlocals([Cond, Then, Else],
-            set.init, SubGoalNonLocals)
+            set_of_var.init, SubGoalNonLocals)
     ;
         GoalExpr = switch(_Var, _CanFail, Cases),
         % _Var must be nonlocal; if it weren't, there would have been a mode
         % error (no producer for _Var before a consumer, namely this switch).
-        cases_find_subgoal_nonlocals(Cases, set.init, SubGoalNonLocals)
+        cases_find_subgoal_nonlocals(Cases, set_of_var.init, SubGoalNonLocals)
     ;
         GoalExpr = shorthand(_),
         unexpected($module, $pred, "shorthand")
     ).
 
 :- pred goals_find_subgoal_nonlocals(list(hlds_goal)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 goals_find_subgoal_nonlocals([], !SubGoalNonLocals).
 goals_find_subgoal_nonlocals([SubGoal | SubGoals], !SubGoalNonLocals) :-
     NonLocals = goal_get_nonlocals(SubGoal),
-    set.union(!.SubGoalNonLocals, NonLocals, !:SubGoalNonLocals),
+    set_of_var.union(NonLocals, !SubGoalNonLocals),
     goals_find_subgoal_nonlocals(SubGoals, !SubGoalNonLocals).
 
 :- pred cases_find_subgoal_nonlocals(list(case)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 cases_find_subgoal_nonlocals([], !SubGoalNonLocals).
 cases_find_subgoal_nonlocals([Case | Cases], !SubGoalNonLocals) :-
     Case = case(_, _, SubGoal),
     NonLocals = goal_get_nonlocals(SubGoal),
-    set.union(!.SubGoalNonLocals, NonLocals, !:SubGoalNonLocals),
+    set_of_var.union(NonLocals, !SubGoalNonLocals),
     cases_find_subgoal_nonlocals(Cases, !SubGoalNonLocals).
 
 %-----------------------------------------------------------------------------%
Index: compiler/ml_commit_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_commit_gen.m,v
retrieving revision 1.3
diff -u -b -r1.3 ml_commit_gen.m
--- compiler/ml_commit_gen.m	23 May 2011 05:08:06 -0000	1.3
+++ compiler/ml_commit_gen.m	15 Aug 2011 11:25:32 -0000
@@ -182,6 +182,7 @@
 :- import_module ml_backend.ml_accurate_gc.
 :- import_module ml_backend.ml_code_gen.
 :- import_module ml_backend.ml_code_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module map.
@@ -451,9 +452,10 @@
         Context = goal_info_get_context(GoalInfo),
         NonLocals = goal_info_get_nonlocals(GoalInfo),
         ml_gen_info_get_byref_output_vars(!.Info, ByRefOutputVars),
-        VarsToCopy = set.intersect(set.list_to_set(ByRefOutputVars),
-            NonLocals),
-        ml_gen_make_locals_for_output_args(set.to_sorted_list(VarsToCopy),
+        VarsToCopy = set_of_var.intersect(
+            set_of_var.list_to_set(ByRefOutputVars), NonLocals),
+        ml_gen_make_locals_for_output_args(
+            set_of_var.to_sorted_list(VarsToCopy),
             Context, LocalVarDecls, CopyLocalsToOutputArgs, !Info)
     ;
         NondetCopyOut = no,
Index: compiler/ml_disj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_disj_gen.m,v
retrieving revision 1.7
diff -u -b -r1.7 ml_disj_gen.m
--- compiler/ml_disj_gen.m	23 May 2011 05:08:06 -0000	1.7
+++ compiler/ml_disj_gen.m	15 Aug 2011 11:28:06 -0000
@@ -130,12 +130,12 @@
 :- import_module ml_backend.ml_gen_info.
 :- import_module ml_backend.ml_global_data.
 :- import_module ml_backend.ml_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module map.
 :- import_module maybe.
 :- import_module require.
-:- import_module set.
 
 ml_gen_disj(Disjuncts, GoalInfo, CodeModel, Context, Statements, !Info) :-
     % Note that we place each arm of the disjunction into a block. This is so
@@ -173,7 +173,7 @@
                     % not have to worry about resetting the trail at the
                     % starts of all non-first disjuncts.
                     NonLocals = goal_info_get_nonlocals(GoalInfo),
-                    OutVars = set.to_sorted_list(NonLocals),
+                    OutVars = set_of_var.to_sorted_list(NonLocals),
                     list.map_foldl(ml_generate_constants_for_arm(OutVars),
                         Disjuncts, Solns, !Info),
                     ml_gen_lookup_disj(OutVars, Solns, Context,
Index: compiler/ml_lookup_switch.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_lookup_switch.m,v
retrieving revision 1.11
diff -u -b -r1.11 ml_lookup_switch.m
--- compiler/ml_lookup_switch.m	9 Aug 2011 05:34:34 -0000	1.11
+++ compiler/ml_lookup_switch.m	15 Aug 2011 11:29:01 -0000
@@ -28,9 +28,9 @@
 :- import_module ml_backend.ml_gen_info.
 :- import_module ml_backend.mlds.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module list.
-:- import_module set.
 :- import_module unit.
 
 %-----------------------------------------------------------------------------%
@@ -52,7 +52,7 @@
     % Is the given list of cases implementable as a lookup switch?
     %
 :- pred ml_is_lookup_switch(pred(cons_tag, Key)::in(pred(in, out) is det),
-    prog_var::in, list(tagged_case)::in, set(prog_var)::in, code_model::in,
+    prog_var::in, list(tagged_case)::in, set_of_progvar::in, code_model::in,
     ml_lookup_switch_info(Key)::out,
     ml_gen_info::in, ml_gen_info::out) is semidet.
 
@@ -115,8 +115,8 @@
 
 ml_is_lookup_switch(GetTag, SwitchVar, TaggedCases, NonLocals, CodeModel,
         LookupSwitchInfo, !Info) :-
-    set.remove(SwitchVar, NonLocals, OtherNonLocals),
-    set.to_sorted_list(OtherNonLocals, OutVars),
+    set_of_var.remove(SwitchVar, NonLocals, OtherNonLocals),
+    set_of_var.to_sorted_list(OtherNonLocals, OutVars),
     % While the LLDS backend has to worry about about implementing trailing
     % for model_non lookup switches, we do not. The MLDS backend implements
     % trailing by a HLDS-to-HLDS transform (which is in add_trail_ops.m),
@@ -140,7 +140,7 @@
 
 :- pred ml_generate_constants_for_lookup_switch(
     pred(cons_tag, T)::in(pred(in, out) is det),
-    code_model::in, list(prog_var)::in, set(prog_var)::in,
+    code_model::in, list(prog_var)::in, set_of_progvar::in,
     list(tagged_case)::in,
     map(T, soln_consts(mlds_rval))::in,
     map(T, soln_consts(mlds_rval))::out,
Index: compiler/ml_unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_unify_gen.m,v
retrieving revision 1.160
diff -u -b -r1.160 ml_unify_gen.m
--- compiler/ml_unify_gen.m	15 Aug 2011 06:23:14 -0000	1.160
+++ compiler/ml_unify_gen.m	15 Aug 2011 11:24:30 -0000
@@ -141,6 +141,7 @@
 :- import_module ml_backend.ml_util.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module int.
@@ -2245,7 +2246,7 @@
 ml_gen_ground_term(TermVar, Goal, Statements, !Info) :-
     Goal = hlds_goal(GoalExpr, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    set.to_sorted_list(NonLocals, NonLocalList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalList),
     (
         NonLocalList = [],
         % The term being constructed by the scope is not needed, so there is
Index: compiler/mode_constraint_robdd.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_constraint_robdd.m,v
retrieving revision 1.21
diff -u -b -r1.21 mode_constraint_robdd.m
--- compiler/mode_constraint_robdd.m	10 May 2011 04:12:25 -0000	1.21
+++ compiler/mode_constraint_robdd.m	15 Aug 2011 10:43:44 -0000
@@ -23,13 +23,14 @@
 :- module check_hlds.mode_constraint_robdd.
 :- interface.
 
-:- import_module parse_tree.
-:- import_module parse_tree.prog_data.
 :- import_module hlds.
 :- import_module hlds.hlds_pred.
 :- import_module mdbcomp.
 :- import_module mdbcomp.goal_path.
 :- import_module mode_robdd.
+:- import_module parse_tree.
+:- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module io.
@@ -146,7 +147,7 @@
 % A prodvars_map maps each subgoal to the set of variables produced
 % by that subgoal.
 
-:- type prodvars_map == map(lambda_path, set(prog_var)).
+:- type prodvars_map == map(lambda_path, set_of_progvar).
 
 :- func atomic_prodvars_map(mode_constraint, mode_constraint_info) =
     prodvars_map.
@@ -181,7 +182,7 @@
                 mci_lambda_path         :: lambda_path,
                 mci_min_vars            :: map(pred_id, mode_constraint_var),
                 mci_max_vars            :: map(pred_id, mode_constraint_var),
-                mci_input_nodes         :: sparse_bitset(prog_var),
+                mci_input_nodes         :: set_of_progvar,
 
                 % A var that is always zero.
                 mci_zero_var            :: robdd_var,
@@ -202,7 +203,7 @@
     varset.new_var(ZeroVar, VarSet0, VarSet),
     PredId = hlds_pred.initial_pred_id,
     MCI = mode_constraint_info(VarSet, bimap.init, PredId, stack.init,
-        map.init, map.init, sparse_bitset.init, ZeroVar, Simple, map.init).
+        map.init, map.init, set_of_var.init, ZeroVar, Simple, map.init).
 
 :- type robdd_var == var(mc_type).
 
@@ -404,29 +405,28 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-atomic_prodvars_map(Constraint, MCI) =
-    (
-        some_vars(VarsEntailed) = vars_entailed(ensure_normalised(Constraint))
-    ->
+atomic_prodvars_map(Constraint, MCI) = ProdVarsMap :-
+    ( some_vars(VarsEntailed) = vars_entailed(ensure_normalised(Constraint)) ->
         list.foldl(
-            (func(MCVar, PVM) =
+            (pred(MCVar::in, PVM0::in, PVM::out) is det :-
                 (
                     bimap.reverse_lookup(MCI ^ mci_varmap, Key, MCVar),
                     Key = key(RepVar, PredId, LambdaId0),
                     PredId = MCI ^ mci_pred_id,
                     RepVar = ProgVar `at` GoalId,
-                    LambdaId = stack.push(LambdaId0, GoalId)
+                    stack.push(LambdaId0, GoalId, LambdaId)
                 ->
-                    ( Vs = map.search(PVM, LambdaId) ->
-                        map.det_update(PVM, LambdaId, Vs `insert` ProgVar)
+                    ( map.search(PVM0, LambdaId, Vs0) ->
+                        set_of_var.insert(ProgVar, Vs0, Vs),
+                        map.det_update(LambdaId, Vs, PVM0, PVM)
                     ;
-                        map.det_insert(PVM, LambdaId,
-                            make_singleton_set(ProgVar))
+                        Vs = set_of_var.make_singleton(ProgVar),
+                        map.det_insert(LambdaId, Vs, PVM0, PVM)
                     )
                 ;
-                    PVM
+                    PVM = PVM0
                 )
-            ), to_sorted_list(VarsEntailed), map.init)
+            ), to_sorted_list(VarsEntailed), map.init, ProdVarsMap)
     ;
-        func_error("atomic_prodvars_map: zero constraint")
+        unexpected($module, $pred, "zero constraint")
     ).
Index: compiler/mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_constraints.m,v
retrieving revision 1.64
diff -u -b -r1.64 mode_constraints.m
--- compiler/mode_constraints.m	23 May 2011 05:08:07 -0000	1.64
+++ compiler/mode_constraints.m	15 Aug 2011 09:48:09 -0000
@@ -74,6 +74,7 @@
 :- import_module parse_tree.file_names.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.dependency_graph.
 
 :- import_module assoc_list.
@@ -401,7 +402,7 @@
             (pred(Clause0::in, Clause::out, S0::in, S::out) is det :-
                 Goal0 = Clause0 ^ clause_body,
                 number_robdd_variables_in_goal(InstGraph,
-                    set.init, _, Goal0, Goal, S0, S),
+                    set_of_var.init, _, Goal0, Goal, S0, S),
                 Clause = Clause0 ^ clause_body := Goal
         ), Clauses2, Clauses, NRInfo0, NRInfo),
 
@@ -415,7 +416,7 @@
     save_max_var_for_pred(PredId, !MCI).
 
 :- pred number_robdd_variables_in_goal(inst_graph::in,
-    set(prog_var)::in, set(prog_var)::out, hlds_goal::in, hlds_goal::out,
+    set_of_progvar::in, set_of_progvar::out, hlds_goal::in, hlds_goal::out,
     number_robdd_info::in, number_robdd_info::out) is det.
 
 number_robdd_variables_in_goal(InstGraph, ParentNonLocals, Occurring,
@@ -429,7 +430,7 @@
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
 :- pred number_robdd_variables_in_goal_2(inst_graph::in, goal_id::in,
-    set(prog_var)::in, set(prog_var)::in, set(prog_var)::out,
+    set_of_progvar::in, set_of_progvar::in, set_of_progvar::out,
     hlds_goal_expr::in, hlds_goal_expr::out,
     number_robdd_info::in, number_robdd_info::out) is det.
 
@@ -462,7 +463,7 @@
         Then0, Then, !RInfo),
     number_robdd_variables_in_goal(InstGraph, NonLocals, OccElse,
         Else0, Else, !RInfo),
-    Occurring = OccCond `set.union` OccThen `set.union` OccElse.
+    Occurring = OccCond `set_of_var.union` OccThen `set_of_var.union` OccElse.
 number_robdd_variables_in_goal_2(_, _, _, _, _, shorthand(_), _, !RInfo) :-
     unexpected($module, $pred, "shorthand").
 % number_robdd_variables_in_goal_2(InstGraph, _, _, NonLocals, Occurring,
@@ -527,48 +528,49 @@
             ), InstGraph, LambdaHeadVars), !NRInfo),
 
     % Number variables within the lambda goal.
-    number_robdd_variables_in_goal(InstGraph, set.init, _Occurring,
+    number_robdd_variables_in_goal(InstGraph, set_of_var.init, _Occurring,
         LambdaGoal0, LambdaGoal, !NRInfo),
 
     update_mc_info(leave_lambda_goal, !NRInfo),
     !RHS ^ rhs_lambda_goal := LambdaGoal.
 
 :- pred number_robdd_variables_at_goal_path(inst_graph::in, goal_id::in,
-    set(prog_var)::in, list(prog_var)::in, set(prog_var)::out,
+    set_of_progvar::in, list(prog_var)::in, set_of_progvar::out,
     number_robdd_info::in, number_robdd_info::out) is det.
 
 number_robdd_variables_at_goal_path(InstGraph, GoalId, ParentNonLocals,
         Vars0, Occurring, !NRInfo) :-
-    solutions.solutions_set(inst_graph.reachable_from_list(InstGraph, Vars0),
-        Occurring),
-    Vars = set.to_sorted_list(ParentNonLocals `set.union`
-        set.list_to_set(Vars0)),
+    solutions.solutions(inst_graph.reachable_from_list(InstGraph, Vars0),
+        OccurringList),
+    set_of_var.list_to_set(OccurringList, Occurring),
+    Vars = set_of_var.to_sorted_list(ParentNonLocals `set_of_var.union`
+        set_of_var.list_to_set(Vars0)),
     % XXX We may be able to make this more efficient.
     inst_graph.foldl_reachable_from_list(
         (pred(V::in, S0::in, S::out) is det :-
             update_mc_info_t(mode_constraint_var(V `at` GoalId), _, S0, S)
         ), InstGraph, Vars, !NRInfo).
 
-:- pred number_robdd_variables_in_goals(inst_graph::in, set(prog_var)::in,
-    set(prog_var)::out, hlds_goals::in, hlds_goals::out,
+:- pred number_robdd_variables_in_goals(inst_graph::in, set_of_progvar::in,
+    set_of_progvar::out, hlds_goals::in, hlds_goals::out,
     number_robdd_info::in, number_robdd_info::out) is det.
 
 number_robdd_variables_in_goals(_, _, Occurring, [], [], !RInfo) :-
-    set.init(Occurring).
+    set_of_var.init(Occurring).
 number_robdd_variables_in_goals(InstGraph, NonLocals, Occurring,
         [Goal0 | Goals0], [Goal | Goals], !RInfo) :-
     number_robdd_variables_in_goal(InstGraph, NonLocals, Occurring0,
         Goal0, Goal, !RInfo),
     number_robdd_variables_in_goals(InstGraph, NonLocals, Occurring1,
         Goals0, Goals, !RInfo),
-    Occurring = Occurring0 `set.union` Occurring1.
+    Occurring = Occurring0 `set_of_var.union` Occurring1.
 
-:- pred number_robdd_variables_in_cases(inst_graph::in, set(prog_var)::in,
-    set(prog_var)::out, list(case)::in, list(case)::out,
+:- pred number_robdd_variables_in_cases(inst_graph::in, set_of_progvar::in,
+    set_of_progvar::out, list(case)::in, list(case)::out,
     number_robdd_info::in, number_robdd_info::out) is det.
 
 number_robdd_variables_in_cases(_, _, Occurring, [], [], !RInfo) :-
-    set.init(Occurring).
+    set_of_var.init(Occurring).
 number_robdd_variables_in_cases(InstGraph, NonLocals, Occurring,
         [Case0 | Cases0], [Case | Cases], !RInfo) :-
     Case0 = case(MainConsId, OtherConsIds, Goal0),
@@ -577,7 +579,7 @@
     Case = case(MainConsId, OtherConsIds, Goal),
     number_robdd_variables_in_cases(InstGraph, NonLocals, Occurring1,
         Cases0, Cases, !RInfo),
-    Occurring = Occurring0 `set.union` Occurring1.
+    Occurring = Occurring0 `set_of_var.union` Occurring1.
 
 %-----------------------------------------------------------------------------%
 
@@ -592,20 +594,17 @@
         !ModeConstraint, !MCI, !ModuleInfo) :-
     !:MCI = mci_set_pred_id(!.MCI, PredId),
     mc_process_pred(PredId, SCC, !ModeConstraint, !MCI, !ModuleInfo),
-    mc_process_scc_pass_1(PredIds, SCC, !ModeConstraint,
-        !MCI, !ModuleInfo).
+    mc_process_scc_pass_1(PredIds, SCC, !ModeConstraint, !MCI, !ModuleInfo).
 
 :- pred mc_process_scc_pass_2(list(pred_id)::in,
     mode_constraint::in, mode_constraint_info::in,
     module_info::in, module_info::out) is det.
 
 mc_process_scc_pass_2([], _, _, !ModuleInfo).
-mc_process_scc_pass_2([PredId | PredIds], ModeConstraint, MCI,
-        !ModuleInfo) :-
+mc_process_scc_pass_2([PredId | PredIds], ModeConstraint, MCI, !ModuleInfo) :-
     mc_process_pred_2(PredId, ModeConstraint,
         mci_set_pred_id(MCI, PredId), !ModuleInfo),
-    mc_process_scc_pass_2(PredIds, ModeConstraint,
-        MCI, !ModuleInfo).
+    mc_process_scc_pass_2(PredIds, ModeConstraint, MCI, !ModuleInfo).
 
 :- pred mc_process_pred(pred_id::in, list(pred_id)::in,
     mode_constraint::in, mode_constraint::out,
@@ -872,10 +871,10 @@
     mode_get_insts(ModuleInfo, Mode, InitialInst, FinalInst),
     process_inst(ModuleInfo, InstGraph,
         InitialFree, InitialBound, InitialHO, InitialInst,
-        set.init, Var, !Constraint, !MDI),
+        set_of_var.init, Var, !Constraint, !MDI),
     process_inst(ModuleInfo, InstGraph,
         FinalFree, FinalBound, FinalHO, FinalInst,
-        set.init, Var, !Constraint, !MDI).
+        set_of_var.init, Var, !Constraint, !MDI).
 
 :- func initial(prog_var) = rep_var.
 
@@ -944,13 +943,13 @@
 
 :- pred process_inst(module_info::in, inst_graph::in,
     constrain_var::in(constrain_var), constrain_var::in(constrain_var),
-    bool::in, mer_inst::in, set(prog_var)::in, prog_var::in,
+    bool::in, mer_inst::in, set_of_progvar::in, prog_var::in,
     mode_constraint::in, mode_constraint::out,
     mode_decl_info::in, mode_decl_info::out) is det.
 
 process_inst(ModuleInfo, InstGraph, Free, Bound, DoHO, Inst,
         Seen, Var, !Constraint, !MDI) :-
-    ( Var `set.member` Seen ->
+    ( set_of_var.member(Seen, Var) ->
         true
     ;
         ( Inst = defined_inst(InstName) ->
@@ -965,7 +964,7 @@
 
 :- pred do_process_inst(module_info::in, inst_graph::in,
     constrain_var::in(constrain_var), constrain_var::in(constrain_var),
-    bool::in, mer_inst::in, set(prog_var)::in, prog_var::in,
+    bool::in, mer_inst::in, set_of_progvar::in, prog_var::in,
     mode_constraint::in, mode_constraint::out,
     mode_decl_info::in, mode_decl_info::out) is det.
 
@@ -992,16 +991,15 @@
 
     map.lookup(InstGraph, Var, node(Functors, _)),
     map.foldl2(
-        (pred(ConsId::in, Vs::in, C0::in, C::out, S0::in, S::out)
-            is det :-
+        (pred(ConsId::in, Vs::in, C0::in, C::out, S0::in, S::out) is det :-
         ( Inst = bound(_, BIs) ->
             ( cons_id_in_bound_insts(ConsId, BIs, Insts) ->
-                assoc_list.from_corresponding_lists(Vs,
-                    Insts, VarInsts),
+                    assoc_list.from_corresponding_lists(Vs, Insts, VarInsts),
                 list.foldl2((pred((V - I)::in, C1::in, C2::out,
                         T0::in, T::out) is det :-
+                        set_of_var.insert(Var, Seen, SeenVar),
                     process_inst(ModuleInfo, InstGraph,
-                        Free, Bound, DoHO, I, Seen `set.insert` Var,
+                            Free, Bound, DoHO, I, SeenVar,
                         V, C1, C2, T0, T)
                     ), VarInsts, C0, C, S0, S)
             ;
@@ -1009,9 +1007,10 @@
                 S = S0
             )
         ;
+                set_of_var.insert(Var, Seen, SeenVar),
             list.foldl2(
                 process_inst(ModuleInfo, InstGraph,
-                    Free, Bound, DoHO, Inst, Seen `set.insert` Var),
+                        Free, Bound, DoHO, Inst, SeenVar),
                 Vs, C0, C, S0, S)
         )), Functors, !Constraint, !MDI),
     (
@@ -1056,8 +1055,8 @@
     AtomicGoals0 = set.init,
     GCInfo0 = goal_constraints_info(ModuleInfo, SCC, InstGraph, HeadVars,
         VarSet0, AtomicGoals0, !.MCI, HOModes0, map.init),
-    NonLocals = set.list_to_set(HeadVars),
-    GoalVars = set.sorted_list_to_set(map.sorted_keys(InstGraph)),
+    NonLocals = set_of_var.list_to_set(HeadVars),
+    GoalVars = set_of_var.sorted_list_to_set(map.sorted_keys(InstGraph)),
 
     goal_constraints_2(whole_body_goal_id, NonLocals, GoalVars, _CanSucceed,
         DisjGoal, _, !Constraint, GCInfo0, GCInfo1),
@@ -1127,7 +1126,7 @@
 
 :- type can_succeed == bool.
 
-:- pred goal_constraints(set(prog_var)::in, can_succeed::out, hlds_goal::in,
+:- pred goal_constraints(set_of_progvar::in, can_succeed::out, hlds_goal::in,
     hlds_goal::out, mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1149,15 +1148,16 @@
     % XXX
     list.foldl((pred(V::in, S0::in, S::out) is det :-
             get_var(V `at` GoalId, _, S0, S)
-        ), set.to_sorted_list(Vars), !GCInfo),
+        ), set_of_var.to_sorted_list(Vars), !GCInfo),
     save_threshold(!.GCInfo ^ mc_info, Threshold),
 
     NonLocals = goal_info_get_nonlocals(GoalInfo0),
 
     InstGraph = !.GCInfo ^ inst_graph,
-    NonLocalReachable = solutions.solutions_set(inst_graph.reachable_from_list(
-        InstGraph, to_sorted_list(NonLocals))),
-    LocalVars = Vars `difference` NonLocalReachable,
+    NonLocalReachableList = solutions.solutions(inst_graph.reachable_from_list(
+        InstGraph, set_of_var.to_sorted_list(NonLocals))),
+    set_of_var.sorted_list_to_set(NonLocalReachableList, NonLocalReachable),
+    LocalVars = Vars `set_of_var.difference` NonLocalReachable,
 
     ( using_simple_mode_constraints(!.GCInfo ^ g_mc_info) ->
         % With simple mode constraints, it is more efficient to do this
@@ -1211,8 +1211,8 @@
 
     Goal = hlds_goal(GoalExpr, GoalInfo0).
 
-:- pred goal_constraints_2(goal_id::in, set(prog_var)::in,
-    set(prog_var)::in, can_succeed::out, hlds_goal_expr::in,
+:- pred goal_constraints_2(goal_id::in, set_of_progvar::in,
+    set_of_progvar::in, can_succeed::out, hlds_goal_expr::in,
     hlds_goal_expr::out, mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1227,7 +1227,7 @@
             Usage = list.foldl(func(G, U0) =
                 list.foldl((func(V, U1) = U :-
                     multi_map.set(V, get_goal_id(G), U1, U)),
-                    set.to_sorted_list(vars(G)), U0),
+                    set_of_var.to_sorted_list(vars(G)), U0),
                 Goals0, Usage0),
 
             known_vars(ensure_normalised(!.Constraint), KnownTrue, KnownFalse),
@@ -1259,7 +1259,7 @@
                 get_var(V `at` I, VI),
                 { C = C0 ^ eq_vars(Vgp, VI) }
             ), DisjunctPaths, Cons0, Cons)
-        ), set.to_sorted_list(Vars), !Constraint, !GCInfo),
+        ), set_of_var.to_sorted_list(Vars), !Constraint, !GCInfo),
         GoalExpr = disj(Goals)
     ;
         GoalExpr0 = unify(Var, RHS0, _, _, _),
@@ -1375,7 +1375,7 @@
                 get_var(V `at` GoalId, Vgp),
                 get_var(V `at` get_goal_id(SubGoal), Vneg),
                 { C = C0 ^ eq_vars(Vgp, Vneg) }
-            ), set.to_sorted_list(Vars), !Constraint, !GCInfo),
+            ), set_of_var.to_sorted_list(Vars), !Constraint, !GCInfo),
 
         % Make sure the negation doesn't bind any nonlocal variables.
         negation_constraints(GoalId, NonLocals, !Constraint, !GCInfo),
@@ -1389,7 +1389,7 @@
                 get_var(V `at` GoalId, Vgp),
                 get_var(V `at` get_goal_id(SubGoal), Vexist),
                 { C = C0 ^ eq_vars(Vgp, Vexist) }
-            ), set.to_sorted_list(Vars), !Constraint, !GCInfo),
+            ), set_of_var.to_sorted_list(Vars), !Constraint, !GCInfo),
         GoalExpr = scope(Reason, SubGoal)
     ;
         GoalExpr0 = if_then_else(IteNonLocals, Cond0, Then0, Else0),
@@ -1427,7 +1427,8 @@
                 get_var(V `at` get_goal_id(Cond0), Vcond, S0, S1),
                 get_var(V `at` get_goal_id(Then0), Vthen, S1, S),
                 C = C0 ^ not_both(Vcond, Vthen)
-            ), set.to_sorted_list(vars(Cond0) `set.union` vars(Then0)),
+            ), set_of_var.to_sorted_list(
+                vars(Cond0) `set_of_var.union` vars(Then0)),
             !Constraint, !GCInfo),
 
         % Local variables bound in cond, then or else should be treated as
@@ -1550,7 +1551,7 @@
         )
     ).
 
-:- pred conj_subgoal_constraints(set(prog_var)::in, can_succeed::out,
+:- pred conj_subgoal_constraints(set_of_progvar::in, can_succeed::out,
     mode_constraint::in, mode_constraint::out,
     hlds_goals::in, hlds_goals::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
@@ -1564,7 +1565,7 @@
         Goals0, Goals, !GCInfo),
     CanSucceed = CanSucceed0 `bool.and` CanSucceed1.
 
-:- pred disj_constraints(set(prog_var)::in, can_succeed::out,
+:- pred disj_constraints(set_of_progvar::in, can_succeed::out,
     mode_constraint::in, mode_constraint::out,
     hlds_goals::in, hlds_goals::out,
     list(goal_id)::in, list(goal_id)::out,
@@ -1624,10 +1625,9 @@
                 ), Args, ArgsGi0, !GCInfo),
             ArgsGi = list_to_set(ArgsGi0),
             get_var(LHSVar `at` GoalId, LHSVargi, !GCInfo),
-            ( set.remove_least(Arg1gi, ArgsGi, ArgsGi1) ->
-                !:Constraint = !.Constraint
-                    ^ neq_vars(Arg1gi, LHSVargi)
-                    ^ fold(eq_vars(Arg1gi), ArgsGi1)
+            ( set_of_var.remove_least(Arg1gi, ArgsGi, ArgsGi1) ->
+                !:Constraint = neq_vars(Arg1gi, LHSVargi, !.Constraint),
+                set_of_var.fold_func(eq_vars(Arg1gi), ArgsGi1, !Constraint)
             ;
                 !:Constraint = !.Constraint
             )
@@ -1711,8 +1711,8 @@
         %   "lambda io_constraints Size: %d, Depth: %d\n",
         %   [i(NumNodes5), i(Depth5)])),
 
-        goal_constraints(set.init, _CanSucceed, Goal0, Goal, !Constraint,
-            !GCInfo),
+        goal_constraints(set_of_var.init, _CanSucceed, Goal0, Goal,
+            !Constraint, !GCInfo),
 
         % DEBUGGING CODE
         % size(Constraint, NumNodes, Depth),
@@ -1779,7 +1779,7 @@
             ), HoCalls, Constraint0, Constraint1)),
         Constraint, !GCInfo).
 
-:- pred negation_constraints(goal_id::in, set(prog_var)::in,
+:- pred negation_constraints(goal_id::in, set_of_progvar::in,
     mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1805,7 +1805,7 @@
             { C = C0 ^ var(Vout) ^ not_var(Vgp) }
         ), InstGraph, Var, !Constraint, !GCInfo).
 
-:- pred constrict_to_vars(list(prog_var)::in, set(prog_var)::in,
+:- pred constrict_to_vars(list(prog_var)::in, set_of_progvar::in,
     goal_id::in, mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1817,7 +1817,7 @@
         !.GCInfo ^ mc_info, !.Constraint).
 
 :- pred keep_var(goal_forward_path_map::in, list(prog_var)::in,
-    set(prog_var)::in, goal_id::in, set(goal_id)::in, inst_graph::in,
+    set_of_progvar::in, goal_id::in, set(goal_id)::in, inst_graph::in,
     rep_var::in) is semidet.
 
 keep_var(ForwardGoalPathMap, NonLocals, GoalVars, GoalId, AtomicGoals,
@@ -1831,7 +1831,7 @@
             ; RepVar = out(V)
             ; RepVar = V `at` _
             ),
-            set.member(V, GoalVars)
+            set_of_var.member(GoalVars, V)
         )
         =>
         (
@@ -1930,7 +1930,7 @@
 
 % For local variables, V_ must be equivalent to Vgp.
 
-:- pred constrain_local_vars(set(prog_var)::in, goal_id::in,
+:- pred constrain_local_vars(set_of_progvar::in, goal_id::in,
     mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1953,8 +1953,8 @@
         !:Constraint = !.Constraint ^ eq_vars(Vgp, Vout)
     ).
 
-:- pred constrain_non_occurring_vars(can_succeed::in, set(prog_var)::in,
-    set(prog_var)::in, goal_id::in,
+:- pred constrain_non_occurring_vars(can_succeed::in, set_of_progvar::in,
+    set_of_progvar::in, goal_id::in,
     mode_constraint::in, mode_constraint::out,
     goal_constraints_info::in, goal_constraints_info::out) is det.
 
@@ -1964,9 +1964,9 @@
     InstGraph = !.GCInfo ^ inst_graph,
     Generator =
         (pred(V::out) is nondet :-
-            set.member(U, ParentNonLocals),
+            set_of_var.member(ParentNonLocals, U),
             inst_graph.reachable(InstGraph, U, V),
-            \+ set.member(V, OccurringVars)
+            \+ set_of_var.member(OccurringVars, V)
         ),
     Accumulator =
         (pred(V::in, Vs0::in, Vs::out, in, out) is det -->
@@ -2116,7 +2116,7 @@
 get_goal_id(hlds_goal(_, GoalInfo)) =
     goal_info_get_goal_id(GoalInfo).
 
-:- func vars(hlds_goal) = set(prog_var).
+:- func vars(hlds_goal) = set_of_progvar.
 
 vars(hlds_goal(_, GoalInfo)) = OccurringVars :-
     goal_info_get_occurring_vars(GoalInfo, OccurringVars).
Index: compiler/mode_errors.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_errors.m,v
retrieving revision 1.134
diff -u -b -r1.134 mode_errors.m
--- compiler/mode_errors.m	23 May 2011 05:08:07 -0000	1.134
+++ compiler/mode_errors.m	15 Aug 2011 13:29:04 -0000
@@ -26,10 +26,10 @@
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -45,7 +45,7 @@
 
 :- type delayed_goal
     --->    delayed_goal(
-                set(prog_var),      % The vars it's waiting on.
+                set_of_progvar,     % The vars it's waiting on.
                 mode_error_info,    % The reason it can't be scheduled.
                 hlds_goal           % The goal itself.
             ).
@@ -174,7 +174,7 @@
 
 :- type mode_error_info
     --->    mode_error_info(
-                set(prog_var),      % The variables which caused the error
+                set_of_progvar,     % The variables which caused the error
                                     % (we will attempt to reschedule the goal
                                     % if one of these variables becomes
                                     % more instantiated).
@@ -491,7 +491,7 @@
 
 mode_error_conjunct_to_msgs(Context, !.ModeInfo, DelayedGoal) = Msgs :-
     DelayedGoal = delayed_goal(Vars, Error, Goal),
-    set.to_sorted_list(Vars, VarList),
+    set_of_var.to_sorted_list(Vars, VarList),
     mode_info_get_varset(!.ModeInfo, VarSet),
     Pieces1 = [words("Floundered goal, waiting on {"),
         words(mercury_vars_to_string(VarSet, no, VarList)),
Index: compiler/mode_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_info.m,v
retrieving revision 1.104
diff -u -b -r1.104 mode_info.m
--- compiler/mode_info.m	23 May 2011 05:08:07 -0000	1.104
+++ compiler/mode_info.m	15 Aug 2011 13:28:26 -0000
@@ -30,13 +30,13 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bag.
 :- import_module bool.
 :- import_module list.
 :- import_module maybe.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -88,24 +88,26 @@
     --->    may_change_called_proc
     ;       may_not_change_called_proc.
 
-:- type locked_vars == assoc_list(var_lock_reason, set(prog_var)).
+:- type locked_vars == assoc_list(var_lock_reason, set_of_progvar).
 
 :- type mode_info.
 
 :- type debug_flags
     --->    debug_flags(
+                % The value --debug-modes-verbose.
                 verbose     :: bool,
-                            % The value --debug-modes-verbose
+
+                % The value --debug-modes-minimal.
                 minimal     :: bool,
-                            % The value --debug-modes-minimal
+
+                % The value --debug-modes-statistics.
                 statistics  :: bool
-                            % The value --debug-modes-statistics
             ).
 
     % Initialize the mode_info.
     %
 :- pred mode_info_init(module_info::in, pred_id::in,
-    proc_id::in, prog_context::in, set(prog_var)::in, instmap::in,
+    proc_id::in, prog_context::in, set_of_progvar::in, instmap::in,
     how_to_check_goal::in, may_change_called_proc::in, mode_info::out) is det.
 
     % The mode_info contains a flag indicating whether initialisation calls,
@@ -144,8 +146,8 @@
 :- type par_conj_mode_check_stack == list(par_conj_mode_check).
 :- type par_conj_mode_check
     --->    par_conj_mode_check(
-                par_conj_nonlocals  :: set(prog_var),
-                par_conj_bound      :: set(prog_var)
+                par_conj_nonlocals  :: set_of_progvar,
+                par_conj_bound      :: set_of_progvar
             ).
 
 %-----------------------------------------------------------------------------%
@@ -170,7 +172,7 @@
 :- pred mode_info_get_in_promise_purity_scope(mode_info::in,
     in_promise_purity_scope::out) is det.
 :- pred mode_info_get_num_errors(mode_info::in, int::out) is det.
-:- pred mode_info_get_liveness(mode_info::in, set(prog_var)::out) is det.
+:- pred mode_info_get_liveness(mode_info::in, set_of_progvar::out) is det.
 :- pred mode_info_get_varset(mode_info::in, prog_varset::out) is det.
 :- pred mode_info_get_instvarset(mode_info::in, inst_varset::out) is det.
 :- pred mode_info_get_var_types(mode_info::in, vartypes::out) is det.
@@ -230,9 +232,9 @@
     mode_info::in, mode_info::out) is det.
 :- pred mode_info_set_in_promise_purity_scope(in_promise_purity_scope::in,
     mode_info::in, mode_info::out) is det.
-:- pred mode_info_add_live_vars(set(prog_var)::in,
+:- pred mode_info_add_live_vars(set_of_progvar::in,
     mode_info::in, mode_info::out) is det.
-:- pred mode_info_remove_live_vars(set(prog_var)::in,
+:- pred mode_info_remove_live_vars(set_of_progvar::in,
     mode_info::in, mode_info::out) is det.
 :- pred mode_info_set_varset(prog_varset::in,
     mode_info::in, mode_info::out) is det.
@@ -272,10 +274,10 @@
 :- pred mode_info_get_types_of_vars(mode_info::in,
     list(prog_var)::in, list(mer_type)::out) is det.
 
-:- pred mode_info_lock_vars(var_lock_reason::in, set(prog_var)::in,
+:- pred mode_info_lock_vars(var_lock_reason::in, set_of_progvar::in,
     mode_info::in, mode_info::out) is det.
 
-:- pred mode_info_unlock_vars(var_lock_reason::in, set(prog_var)::in,
+:- pred mode_info_unlock_vars(var_lock_reason::in, set_of_progvar::in,
     mode_info::in, mode_info::out) is det.
 
 :- pred mode_info_var_is_locked(mode_info::in, prog_var::in,
@@ -303,7 +305,7 @@
 
     % Record a mode error (and associated context info) in the mode_info.
     %
-:- pred mode_info_error(set(prog_var)::in, mode_error::in,
+:- pred mode_info_error(set_of_progvar::in, mode_error::in,
     mode_info::in, mode_info::out) is det.
 
 :- pred mode_info_add_error(mode_error_info::in,
@@ -515,7 +517,7 @@
     proc_info_get_vartypes(ProcInfo, VarTypes),
     proc_info_get_inst_varset(ProcInfo, InstVarSet),
 
-    bag.from_set(LiveVars, LiveVarsBag),
+    bag.from_sorted_list(set_of_var.to_sorted_list(LiveVars), LiveVarsBag),
     instmap.init_unreachable(LastCheckpointInstMap),
     LockedVars = [],
     ParallelVars = [],
@@ -541,7 +543,8 @@
     mode_context_init(ModeContext),
     delay_info_init(DelayInfo),
     ErrorList = [],
-    bag.from_set(LiveVars, NondetLiveVarsBag),
+    bag.from_sorted_list(set_of_var.to_sorted_list(LiveVars),
+        NondetLiveVarsBag),
 
     ModeInfo = mode_info(ModuleInfo, InstMap0, DelayInfo, ErrorList,
         ModeContext, Context, NondetLiveVarsBag, ModeSubInfo).
@@ -699,16 +702,18 @@
     % We keep track of the live variables and the nondet-live variables
     % a bag, represented as a list of sets of vars.
     % This allows us to easily add and remove sets of variables.
-    % It's probably not maximally efficient.
+    % It is probably not maximally efficient.
 
     % Add a set of vars to the bag of live vars and the bag of
     % nondet-live vars.
 
 mode_info_add_live_vars(NewLiveVars, !MI) :-
+    set_of_var.to_sorted_list(NewLiveVars, NewLiveVarsList),
+
     mode_info_get_live_vars(!.MI, LiveVars0),
     mode_info_get_nondet_live_vars(!.MI, NondetLiveVars0),
-    bag.insert_set(NewLiveVars, LiveVars0, LiveVars),
-    bag.insert_set(NewLiveVars, NondetLiveVars0, NondetLiveVars),
+    bag.insert_list(NewLiveVarsList, LiveVars0, LiveVars),
+    bag.insert_list(NewLiveVarsList, NondetLiveVars0, NondetLiveVars),
     mode_info_set_live_vars(LiveVars, !MI),
     mode_info_set_nondet_live_vars(NondetLiveVars, !MI).
 
@@ -716,18 +721,19 @@
     % the bag of nondet-live vars.
 
 mode_info_remove_live_vars(OldLiveVars, !MI) :-
+    set_of_var.to_sorted_list(OldLiveVars, OldLiveVarsList),
+
     mode_info_get_live_vars(!.MI, LiveVars0),
     mode_info_get_nondet_live_vars(!.MI, NondetLiveVars0),
-    bag.det_remove_set(OldLiveVars, LiveVars0, LiveVars),
-    bag.det_remove_set(OldLiveVars, NondetLiveVars0, NondetLiveVars),
+    bag.det_remove_list(OldLiveVarsList, LiveVars0, LiveVars),
+    bag.det_remove_list(OldLiveVarsList, NondetLiveVars0, NondetLiveVars),
     mode_info_set_live_vars(LiveVars, !MI),
     mode_info_set_nondet_live_vars(NondetLiveVars, !MI),
 
     % When a variable becomes dead, we may be able to wake up a goal
     % which is waiting on that variable.
-    set.to_sorted_list(OldLiveVars, VarList),
     mode_info_get_delay_info(!.MI, DelayInfo0),
-    delay_info_bind_var_list(VarList, DelayInfo0, DelayInfo),
+    delay_info_bind_var_list(OldLiveVarsList, DelayInfo0, DelayInfo),
     mode_info_set_delay_info(DelayInfo, !MI).
 
 mode_info_var_list_is_live(_, [], []).
@@ -756,7 +762,7 @@
 mode_info_get_liveness(ModeInfo, LiveVars) :-
     mode_info_get_live_vars(ModeInfo, LiveVarsBag),
     bag.to_list_without_duplicates(LiveVarsBag, SortedList),
-    set.sorted_list_to_set(SortedList, LiveVars).
+    set_of_var.sorted_list_to_set(SortedList, LiveVars).
 
 %-----------------------------------------------------------------------------%
 
@@ -781,7 +787,7 @@
     mode_info_get_locked_vars(!.ModeInfo, LockedVars0),
     (
         LockedVars0 = [Reason - TheseVars | LockedVars1],
-        set.equal(TheseVars, Vars)
+        set_of_var.equal(TheseVars, Vars)
     ->
         LockedVars = LockedVars1
     ;
@@ -797,7 +803,7 @@
     var_lock_reason::out) is semidet.
 
 mode_info_var_is_locked_2([ThisReason - Set | Sets], Var, Reason) :-
-    ( set.member(Var, Set) ->
+    ( set_of_var.member(Set, Var) ->
         Reason = ThisReason
     ;
         mode_info_var_is_locked_2(Sets, Var, Reason)
Index: compiler/mode_ordering.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_ordering.m,v
retrieving revision 1.37
diff -u -b -r1.37 mode_ordering.m
--- compiler/mode_ordering.m	23 May 2011 05:08:08 -0000	1.37
+++ compiler/mode_ordering.m	15 Aug 2011 10:20:15 -0000
@@ -55,6 +55,7 @@
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module digraph.
@@ -198,10 +199,10 @@
             union_mode_vars_sets(Goals, !GoalInfo),
             ConsVars = !.GoalInfo ^ consuming_vars,
             !GoalInfo ^ consuming_vars :=
-                ConsVars `difference` !.GoalInfo ^ producing_vars,
+                ConsVars `set_of_var.difference` !.GoalInfo ^ producing_vars,
             NeedVars = !.GoalInfo ^ need_visible_vars,
             !GoalInfo ^ need_visible_vars :=
-                NeedVars `difference` !.GoalInfo ^ make_visible_vars
+                NeedVars `set_of_var.difference` !.GoalInfo ^ make_visible_vars
         ;
             ConjType = parallel_conj,
             list.map_foldl(mode_order_goal, Goals0, Goals, !MOI),
@@ -211,11 +212,13 @@
     ;
         GoalExpr0 = plain_call(PredId, _, Args, _, _, _),
         set_atomic_prod_vars(ProdVars, !GoalInfo, !MOI),
-        MakeVisibleVars = list_to_set(Args) `intersect` ProdVars,
+        MakeVisibleVars =
+            set_of_var.list_to_set(Args) `set_of_var.intersect` ProdVars,
 
         find_matching_proc(PredId, Args, ProdVars, ProcId, ConsumingVars,
             !MOI),
-        NeedVisibleVars = list_to_set(Args) `intersect` ConsumingVars,
+        NeedVisibleVars =
+            set_of_var.list_to_set(Args) `set_of_var.intersect` ConsumingVars,
 
         goal_info_set_consuming_vars(ConsumingVars, !GoalInfo),
         goal_info_set_make_visible_vars(MakeVisibleVars, !GoalInfo),
@@ -234,47 +237,48 @@
         (
             RHS0 = rhs_var(VarB),
             RHS = RHS0,
-            ( ProdVars `contains` VarA ->
+            ( set_of_var.contains(ProdVars, VarA) ->
                 Unification = assign(VarA, VarB),
-                MakeVisibleVars = make_singleton_set(VarA),
-                NeedVisibleVars = make_singleton_set(VarB)
-            ; ProdVars `contains` VarB ->
+                MakeVisibleVars = set_of_var.make_singleton(VarA),
+                NeedVisibleVars = set_of_var.make_singleton(VarB)
+            ; set_of_var.contains(ProdVars, VarB) ->
                 Unification = assign(VarB, VarA),
-                MakeVisibleVars = make_singleton_set(VarB),
-                NeedVisibleVars = make_singleton_set(VarA)
+                MakeVisibleVars = set_of_var.make_singleton(VarB),
+                NeedVisibleVars = set_of_var.make_singleton(VarA)
             ;
                 Unification = simple_test(VarA, VarB),
                 % XXX may be complicated unify -- need to check.
-                MakeVisibleVars = set.init,
-                NeedVisibleVars = list_to_set([VarA, VarB])
+                MakeVisibleVars = set_of_var.init,
+                NeedVisibleVars = set_of_var.list_to_set([VarA, VarB])
             ),
-            ConsumingVars = solutions.solutions_set((pred(Var::out) is nondet :-
+            ConsumingVarsList = solutions.solutions((pred(Var::out) is nondet :-
                 inst_graph.same_graph_corresponding_nodes(InstGraph,
                     VarA, VarB, VarC, VarD),
-                ( ProdVars `contains` VarC ->
+                ( set_of_var.contains(ProdVars, VarC) ->
                     Var = VarD
-                ; ProdVars `contains` VarD ->
+                ; set_of_var.contains(ProdVars, VarD) ->
                     Var = VarC
                 ;
                     fail
-                )))
+                ))),
+            ConsumingVars = set_of_var.sorted_list_to_set(ConsumingVarsList)
         ;
             RHS0 = rhs_functor(_ConsId, _IsExistConstruct, ArgVars),
             RHS = RHS0,
-            ( ProdVars `contains` VarA ->
+            ( set_of_var.contains(ProdVars, VarA) ->
                 % Unification = construct(VarA, ConsId, ArgVars,
                 %   _UniModes, _HowTo, _CellUniq, _MaybeRLExprId),
                 Unification = Unification0, % XXX
-                ConsumingVars = set.init,
-                MakeVisibleVars = list_to_set([VarA | ArgVars]),
-                NeedVisibleVars = set.init
+                ConsumingVars = set_of_var.init,
+                MakeVisibleVars = set_of_var.list_to_set([VarA | ArgVars]),
+                NeedVisibleVars = set_of_var.init
             ;
                 % Unification = deconstruct(VarA, ConsId, ArgVars,
                 %   _UniModes, _CanFail, _CanCGC),
                 Unification = Unification0, % XXX
-                ConsumingVars = make_singleton_set(VarA),
-                MakeVisibleVars = list_to_set(ArgVars),
-                NeedVisibleVars = make_singleton_set(VarA)
+                ConsumingVars = set_of_var.make_singleton(VarA),
+                MakeVisibleVars = set_of_var.list_to_set(ArgVars),
+                NeedVisibleVars = set_of_var.make_singleton(VarA)
             )
         ;
             % Unification = construct(VarA, _ConsId, _ArgVars,
@@ -291,10 +295,11 @@
             mode_order_goal(SubGoal0, SubGoal, !MOI),
             leave_lambda_goal(!MOI),
 
-            ConsumingVars = solutions.solutions_set(
+            ConsumingVarsList = solutions.solutions(
                 inst_graph.reachable_from_list(InstGraph, NonLocals)),
-            MakeVisibleVars = make_singleton_set(VarA),
-            NeedVisibleVars = list_to_set(NonLocals)
+            ConsumingVars = set_of_var.sorted_list_to_set(ConsumingVarsList),
+            MakeVisibleVars = set_of_var.make_singleton(VarA),
+            NeedVisibleVars = set_of_var.list_to_set(NonLocals)
         ),
         goal_info_set_consuming_vars(ConsumingVars, !GoalInfo),
         goal_info_set_make_visible_vars(MakeVisibleVars, !GoalInfo),
@@ -328,10 +333,10 @@
         union_mode_vars_sets([Cond, Then], !GoalInfo),
         ConsVars = !.GoalInfo ^ consuming_vars,
         !GoalInfo ^ consuming_vars :=
-            ConsVars `difference` !.GoalInfo ^ producing_vars,
+            ConsVars `set_of_var.difference` !.GoalInfo ^ producing_vars,
         NeedVars = !.GoalInfo ^ need_visible_vars,
         !GoalInfo ^ need_visible_vars :=
-            NeedVars `difference` !.GoalInfo ^ make_visible_vars,
+            NeedVars `set_of_var.difference` !.GoalInfo ^ make_visible_vars,
 
         combine_mode_vars_sets(Else ^ hlds_goal_info, !GoalInfo),
         GoalExpr = if_then_else(Locals, Cond, Then, Else)
@@ -374,13 +379,13 @@
     NeedVisibleVars0 = !.GoalInfo ^ need_visible_vars,
 
     !GoalInfo ^ producing_vars
-        := ProdVars0 `intersect` GI ^ producing_vars,
+        := ProdVars0 `set_of_var.intersect` GI ^ producing_vars,
     !GoalInfo ^ consuming_vars
-        := ConsumVars0 `union` GI ^ consuming_vars,
+        := ConsumVars0 `set_of_var.union` GI ^ consuming_vars,
     !GoalInfo ^ make_visible_vars
-        := MakeVisibleVars0 `intersect` GI ^ make_visible_vars,
+        := MakeVisibleVars0 `set_of_var.intersect` GI ^ make_visible_vars,
     !GoalInfo ^ need_visible_vars
-        := NeedVisibleVars0 `union` GI ^ need_visible_vars.
+        := NeedVisibleVars0 `set_of_var.union` GI ^ need_visible_vars.
 
 :- pred union_mode_vars_sets(hlds_goals::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
@@ -399,13 +404,13 @@
     Goal = hlds_goal(_, GI),
 
     !GoalInfo ^ producing_vars
-        := ProdVars0 `union` GI ^ producing_vars,
+        := ProdVars0 `set_of_var.union` GI ^ producing_vars,
     !GoalInfo ^ consuming_vars
         := ConsumVars0 `union` GI ^ consuming_vars,
     !GoalInfo ^ make_visible_vars
-        := MakeVisibleVars0 `union` GI ^ make_visible_vars,
+        := MakeVisibleVars0 `set_of_var.union` GI ^ make_visible_vars,
     !GoalInfo ^ need_visible_vars
-        := NeedVisibleVars0 `union` GI ^ need_visible_vars.
+        := NeedVisibleVars0 `set_of_var.union` GI ^ need_visible_vars.
 
 :- pred goal_info_copy_mode_var_sets(hlds_goal_info::in,
     hlds_goal_info::in, hlds_goal_info::out) is det.
@@ -437,14 +442,17 @@
     ProdMap =
         map.foldl((func(I, G, PM0) =
             list.foldl((func(V, PM1) = map.det_insert(PM1, V, I)),
-            set.to_sorted_list(G ^ hlds_goal_info ^ producing_vars), PM0)
+                set_of_var.to_sorted_list(G ^ hlds_goal_info ^ producing_vars),
+                PM0)
         ), GoalMap, map.init),
 
     MakeVisMap =
         map.foldl((func(I, G, MVM0) =
             list.foldl((func(V, MVM1) = map.set(MVM1, V, I)),
             % XXX disjunction required!
-            set.to_sorted_list(G ^ hlds_goal_info ^ make_visible_vars), MVM0)
+                set_of_var.to_sorted_list(
+                    G ^ hlds_goal_info ^ make_visible_vars),
+                MVM0)
         ), GoalMap, map.init),
 
     Graph = map.foldl((func(I, G, !.R) = !:R :-
@@ -457,7 +465,7 @@
                 ;
                     true
                 )
-            ), set.to_sorted_list(GI ^ consuming_vars), !.R),
+            ), set_of_var.to_sorted_list(GI ^ consuming_vars), !.R),
         !:R = list.foldl((func(V, !.R2) = !:R2 :-
                 ( Index2 = map.search(MakeVisMap, V) ->
                     digraph.add_vertex(Index2, Key2, !R2),
@@ -465,7 +473,7 @@
                 ;
                     true
                 )
-            ), set.to_sorted_list(GI ^ need_visible_vars), !.R)
+            ), set_of_var.to_sorted_list(GI ^ need_visible_vars), !.R)
         ), GoalMap, digraph.init),
 
     ( digraph.tsort(Graph, TSort) ->
@@ -475,7 +483,7 @@
         unexpected($module, $pred, "tsort failed")
     ).
 
-:- pred set_atomic_prod_vars(set(prog_var)::out,
+:- pred set_atomic_prod_vars(set_of_progvar::out,
     hlds_goal_info::in, hlds_goal_info::out,
     mode_ordering_info::in, mode_ordering_info::out) is det.
 
@@ -489,7 +497,7 @@
     ->
         ProdVars = ProdVars0
     ;
-        ProdVars = set.init
+        ProdVars = set_of_var.init
     ),
     goal_info_set_producing_vars(ProdVars, !GoalInfo).
 
@@ -506,8 +514,8 @@
         ProcId = initial_proc_id
     ).
 
-:- pred find_matching_proc(pred_id::in, list(prog_var)::in, set(prog_var)::in,
-    proc_id::out, set(prog_var)::out,
+:- pred find_matching_proc(pred_id::in, list(prog_var)::in, set_of_progvar::in,
+    proc_id::out, set_of_progvar::out,
     mode_ordering_info::in, mode_ordering_info::out) is det.
 
 find_matching_proc(PredId, Args, ProdVars, ProcId, ConsumingVars, !MOI) :-
@@ -540,8 +548,8 @@
     ).
 
 :- pred find_matching_proc_2(assoc_list(proc_id, proc_info)::in,
-    set(prog_var)::in, list(prog_var)::in, inst_graph::in, inst_graph::in,
-    mode_constraint_info::in, proc_id::out, set(prog_var)::out) is semidet.
+    set_of_progvar::in, list(prog_var)::in, inst_graph::in, inst_graph::in,
+    mode_constraint_info::in, proc_id::out, set_of_progvar::out) is semidet.
 
 find_matching_proc_2([ProcId0 - ProcInfo | ProcList], ProdVars, Args,
         CallerInstGraph, CalleeInstGraph, MCInfo, ProcId, ConsumingVars) :-
@@ -554,7 +562,7 @@
                 CallerInstGraph, CalleeInstGraph, Args, HeadVars, X, Y)
         =>
             (
-                ProdVars `contains` X
+                set_of_var.contains(ProdVars, X)
             <=>
                 (
                     var_entailed(Constraint,
@@ -566,13 +574,14 @@
         )
     ->
         ProcId = ProcId0,
-        ConsumingVars = solutions.solutions_set(pred(X::out) is nondet :-
+        ConsumingVarsList = solutions.solutions(pred(X::out) is nondet :-
             some [Y] (
                 inst_graph.corresponding_nodes_from_lists(CallerInstGraph,
                 CalleeInstGraph, Args, HeadVars, X, Y),
                 var_entailed(Constraint, mode_constraint_var(MCInfo, in(Y)))
             )
-        )
+        ),
+        set_of_var.sorted_list_to_set(ConsumingVarsList, ConsumingVars)
     ;
         find_matching_proc_2(ProcList, ProdVars, Args, CallerInstGraph,
         CalleeInstGraph, MCInfo, ProcId, ConsumingVars)
Index: compiler/mode_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_util.m,v
retrieving revision 1.218
diff -u -b -r1.218 mode_util.m
--- compiler/mode_util.m	23 May 2011 05:08:08 -0000	1.218
+++ compiler/mode_util.m	15 Aug 2011 13:27:12 -0000
@@ -206,6 +206,7 @@
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
+:- import_module parse_tree.set_of_var.
 
 :- import_module int.
 :- import_module maybe.
@@ -1251,7 +1252,7 @@
             ExtraArgs = [_ | _],
             OldInstMapDelta = goal_info_get_instmap_delta(GoalInfo),
             ExtraArgVars = list.map(foreign_arg_var, ExtraArgs),
-            instmap_delta_restrict(set.list_to_set(ExtraArgVars),
+            instmap_delta_restrict(set_of_var.list_to_set(ExtraArgVars),
                 OldInstMapDelta, ExtraArgsInstMapDelta),
             instmap_delta_apply_instmap_delta(InstMapDelta0,
                 ExtraArgsInstMapDelta, large_base, InstMapDelta)
@@ -1309,7 +1310,7 @@
 
 :- pred recompute_instmap_delta_disj(recompute_atomic_instmap_deltas::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
-    vartypes::in, instmap::in, set(prog_var)::in, instmap_delta::out,
+    vartypes::in, instmap::in, set_of_progvar::in, instmap_delta::out,
     recompute_info::in, recompute_info::out) is det.
 
 recompute_instmap_delta_disj(RecomputeAtomic, Goals0, Goals,
@@ -1328,7 +1329,7 @@
 
 :- pred recompute_instmap_delta_disj_2(recompute_atomic_instmap_deltas::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
-    vartypes::in, instmap::in, set(prog_var)::in, list(instmap_delta)::out,
+    vartypes::in, instmap::in, set_of_progvar::in, list(instmap_delta)::out,
     recompute_info::in, recompute_info::out) is det.
 
 recompute_instmap_delta_disj_2(_RecomputeAtomic, [], [],
@@ -1345,7 +1346,7 @@
 
 :- pred recompute_instmap_delta_cases(recompute_atomic_instmap_deltas::in,
     prog_var::in, list(case)::in, list(case)::out,
-    vartypes::in, instmap::in, set(prog_var)::in, instmap_delta::out,
+    vartypes::in, instmap::in, set_of_progvar::in, instmap_delta::out,
     recompute_info::in, recompute_info::out) is det.
 
 recompute_instmap_delta_cases(RecomputeAtomic, Var, Cases0, Cases,
@@ -1364,7 +1365,7 @@
 
 :- pred recompute_instmap_delta_cases_2(recompute_atomic_instmap_deltas::in,
     prog_var::in, list(case)::in,
-    list(case)::out, vartypes::in, instmap::in, set(prog_var)::in,
+    list(case)::out, vartypes::in, instmap::in, set_of_progvar::in,
     list(instmap_delta)::out, recompute_info::in, recompute_info::out) is det.
 
 recompute_instmap_delta_cases_2(_RecomputeAtomic, _Var, [], [],
@@ -1551,7 +1552,7 @@
         Uni = construct(Var, ConsId, Args, _, _, _, _),
         (
             NonLocals = goal_info_get_nonlocals(GoalInfo),
-            set.member(Var, NonLocals),
+            set_of_var.member(NonLocals, Var),
             OldInstMapDelta = goal_info_get_instmap_delta(GoalInfo),
             \+ instmap_delta_search_var(OldInstMapDelta, Var, _),
             MaybeInst = cons_id_to_shared_inst(ModuleInfo0, ConsId,
Index: compiler/modecheck_call.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_call.m,v
retrieving revision 1.89
diff -u -b -r1.89 modecheck_call.m
--- compiler/modecheck_call.m	23 May 2011 05:08:08 -0000	1.89
+++ compiler/modecheck_call.m	15 Aug 2011 13:48:55 -0000
@@ -90,11 +90,11 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module map.
 :- import_module require.
-:- import_module set.
 :- import_module term.
 
 %-----------------------------------------------------------------------------%
@@ -130,7 +130,7 @@
         ProcIds = [],
         \+ check_marker(Markers, marker_infer_modes)
     ->
-        set.init(WaitingVars),
+        set_of_var.init(WaitingVars),
         mode_info_error(WaitingVars, mode_error_no_mode_decl, !ModeInfo),
         TheProcId = invalid_proc_id,
         ArgVars = ArgVars0,
@@ -173,7 +173,7 @@
         mode_info_get_errors(!.ModeInfo, OldErrors),
         mode_info_set_errors([], !ModeInfo),
 
-        set.init(WaitingVars0),
+        set_of_var.init(WaitingVars0),
         modecheck_find_matching_modes(ProcIds, PredId, Procs, ArgVars0,
             [], RevMatchingProcIds, WaitingVars0, WaitingVars1, !ModeInfo),
 
@@ -193,7 +193,7 @@
                 CalleeModeErrors = [_ | _],
                 % mode error in callee for this mode
                 ArgVars = ArgVars0,
-                WaitingVars = set.list_to_set(ArgVars),
+                WaitingVars = set_of_var.list_to_set(ArgVars),
                 ExtraGoals = no_extra_goals,
                 instmap_lookup_vars(InstMap, ArgVars, ArgInsts),
                 mode_info_set_call_arg_context(0, !ModeInfo),
@@ -251,7 +251,7 @@
             mode_info_var_is_locked(!.ModeInfo, PredVar, Reason)
         ->
             BetterPredVarInst = ground(A, B),
-            set.singleton_set(WaitingVars, PredVar),
+            WaitingVars = set_of_var.make_singleton(PredVar),
             mode_info_error(WaitingVars, mode_error_bind_var(Reason, PredVar,
                     PredVarInst, BetterPredVarInst), !ModeInfo),
             Modes = [],
@@ -275,7 +275,7 @@
     ;
         % The error occurred in argument 1, i.e. the pred term.
         mode_info_set_call_arg_context(1, !ModeInfo),
-        set.singleton_set(WaitingVars, PredVar),
+        WaitingVars = set_of_var.make_singleton(PredVar),
         mode_info_error(WaitingVars,
             mode_error_higher_order_pred_var(PredOrFunc, PredVar, PredVarInst,
                 Arity),
@@ -325,14 +325,13 @@
 %--------------------------------------------------------------------------%
 
 :- pred no_matching_modes(pred_id::in, list(prog_var)::in,
-    maybe(determinism)::in, set(prog_var)::in, proc_id::out,
+    maybe(determinism)::in, set_of_progvar::in, proc_id::out,
     mode_info::in, mode_info::out) is det.
 
 no_matching_modes(PredId, ArgVars, DeterminismKnown, WaitingVars, TheProcId,
         !ModeInfo) :-
-    %
     % There were no matching modes.
-    % If we're inferring modes for this called predicate, then
+    % If we are inferring modes for this called predicate, then
     % just insert a new mode declaration which will match.
     % Otherwise, report an error.
     %
@@ -362,7 +361,7 @@
 
 :- pred modecheck_find_matching_modes(list(proc_id)::in, pred_id::in,
     proc_table::in, list(prog_var)::in, list(proc_mode)::in,
-    list(proc_mode)::out, set(prog_var)::in, set(prog_var)::out,
+    list(proc_mode)::out, set_of_progvar::in, set_of_progvar::out,
     mode_info::in, mode_info::out) is det.
 
 modecheck_find_matching_modes([], _PredId, _Procs, _ArgVars,
@@ -413,7 +412,7 @@
         Errors = [FirstError | _],
         mode_info_set_errors([], !ModeInfo),
         FirstError = mode_error_info(ErrorWaitingVars, _, _, _),
-        set.union(!.WaitingVars, ErrorWaitingVars, !:WaitingVars)
+        set_of_var.union(ErrorWaitingVars, !WaitingVars)
     ;
         Errors = [],
         NewMatch = proc_mode(ProcId, InstVarSub, ProcArgModes),
Index: compiler/modecheck_conj.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_conj.m,v
retrieving revision 1.4
diff -u -b -r1.4 modecheck_conj.m
--- compiler/modecheck_conj.m	23 May 2011 05:08:08 -0000	1.4
+++ compiler/modecheck_conj.m	15 Aug 2011 13:58:56 -0000
@@ -42,6 +42,7 @@
 :- import_module hlds.instmap.
 :- import_module parse_tree.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -337,7 +338,7 @@
             % (i.e. have non-free insts).
             mode_info_get_instmap(!.ModeInfo, InstMap),
             instmap_to_assoc_list(InstMap, VarInsts),
-            NonFreeVars0 = set.list_to_set(
+            NonFreeVars0 = set_of_var.list_to_set(
                 non_free_vars_in_assoc_list(VarInsts)),
 
             % Find the set of vars whose instantiation should lead to
@@ -352,7 +353,7 @@
             mode_info_get_module_info(!.ModeInfo, ModuleInfo),
             mode_info_get_var_types(!.ModeInfo, VarTypes),
             all [Var] (
-                set.member(Var, CandidateInitVars)
+                set_of_var.member(CandidateInitVars, Var)
             =>
                 (
                     map.lookup(VarTypes, Var, VarType),
@@ -363,7 +364,8 @@
         ->
             % Construct the inferred initialisation goals
             % and try scheduling again.
-            CandidateInitVarList = set.to_sorted_list(CandidateInitVars),
+            CandidateInitVarList =
+                set_of_var.to_sorted_list(CandidateInitVars),
             construct_initialisation_calls(CandidateInitVarList,
                 InitGoals, !ModeInfo),
             Goals1 = InitGoals ++ Goals0,
@@ -416,25 +418,25 @@
     % goals, foreign_code, or var/lambda unifications.
     %
 :- pred candidate_init_vars(mode_info::in, list(hlds_goal)::in,
-    set(prog_var)::in, set(prog_var)::out) is cc_nondet.
+    set_of_progvar::in, set_of_progvar::out) is cc_nondet.
 
 candidate_init_vars(ModeInfo, Goals, NonFreeVars0, CandidateVars) :-
-    CandidateVars0 = set.init,
+    CandidateVars0 = set_of_var.init,
     candidate_init_vars_2(ModeInfo, Goals, NonFreeVars0, NonFreeVars1,
         CandidateVars0, CandidateVars1),
-    CandidateVars = set.difference(CandidateVars1, NonFreeVars1).
+    CandidateVars = set_of_var.difference(CandidateVars1, NonFreeVars1).
 
 :- pred candidate_init_vars_2(mode_info::in, list(hlds_goal)::in,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is nondet.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is nondet.
 
 candidate_init_vars_2(ModeInfo, Goals, !NonFree, !CandidateVars) :-
     list.foldl2(candidate_init_vars_3(ModeInfo), Goals,
         !NonFree, !CandidateVars).
 
 :- pred candidate_init_vars_3(mode_info::in, hlds_goal::in,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is nondet.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is nondet.
 
 candidate_init_vars_3(ModeInfo, Goal, !NonFree, !CandidateVars) :-
     Goal = hlds_goal(GoalExpr, _GoalInfo),
@@ -442,21 +444,21 @@
         % A var/var unification.
         GoalExpr = unify(X, RHS, _, _, _),
         RHS  = rhs_var(Y),
-        ( set.member(X, !.NonFree) ->
-            not set.member(Y, !.NonFree),
+        ( set_of_var.member(!.NonFree, X) ->
+            not set_of_var.member(!.NonFree, Y),
             % It is an assignment from X to Y.
-            !:NonFree = set.insert(!.NonFree, Y)
-        ; set.member(Y, !.NonFree) ->
+            set_of_var.insert(Y, !NonFree)
+        ; set_of_var.member(!.NonFree, Y) ->
             % It is an assignment from Y to X.
-            !:NonFree = set.insert(!.NonFree, X)
+            set_of_var.insert(X, !NonFree)
         ;
             % It is an assignment one way or the other.
             (
-                !:NonFree       = set.insert(!.NonFree, X),
-                !:CandidateVars = set.insert(!.CandidateVars, Y)
+                set_of_var.insert(X, !NonFree),
+                set_of_var.insert(Y, !CandidateVars)
             ;
-                !:NonFree       = set.insert(!.NonFree, Y),
-                !:CandidateVars = set.insert(!.CandidateVars, X)
+                set_of_var.insert(Y, !NonFree),
+                set_of_var.insert(X, !CandidateVars)
             )
         )
     ;
@@ -466,13 +468,13 @@
         RHS  = rhs_functor(_, _, Args),
 
         % If this is a construction then X must be free.
-        not set.member(X, !.NonFree),
+        not set_of_var.member(!.NonFree, X),
 
         % But X becomes instantiated.
-        !:NonFree = set.insert(!.NonFree, X),
+        set_of_var.insert(X, !NonFree),
 
         % And the Args are potential candidates for initialisation.
-        !:CandidateVars = set.insert_list(!.CandidateVars, Args)
+        set_of_var.insert_list(Args, !CandidateVars)
     ;
         % A var/lambda unification, which can only be deterministic if it is
         % a construction.
@@ -480,10 +482,10 @@
         RHS  = rhs_lambda_goal(_, _, _, _, _, _, _, _, _),
 
         % If this is a construction then X must be free.
-        not set.member(X, !.NonFree),
+        not set_of_var.member(!.NonFree, X),
 
         % But X becomes instantiated.
-        !:NonFree = set.insert(!.NonFree, X)
+        set_of_var.insert(X, !NonFree)
     ;
         % Disjunctions are tricky, because we don't perform switch analysis
         % until after mode analysis. So here we assume that the disjunction
@@ -503,14 +505,14 @@
         mode_info_get_module_info(ModeInfo, ModuleInfo),
         mode_info_get_var_types(ModeInfo, VarTypes),
         NonSolverNonLocals =
-            set.filter(non_solver_var(ModuleInfo, VarTypes), NonLocals),
-        !:NonFree = set.union(NonSolverNonLocals, !.NonFree),
+            set_of_var.filter(non_solver_var(ModuleInfo, VarTypes), NonLocals),
+        set_of_var.union(NonSolverNonLocals, !NonFree),
 
         candidate_init_vars_3(ModeInfo, ThenGoal, !.NonFree, NonFreeThen,
             !CandidateVars),
         candidate_init_vars_3(ModeInfo, ElseGoal, !.NonFree, NonFreeElse,
             !CandidateVars),
-        !:NonFree = set.union(NonFreeThen, NonFreeElse)
+        set_of_var.union(NonFreeThen, NonFreeElse, !:NonFree)
     ;
         % XXX We should special-case the handling of from_ground_term_construct
         % scopes.
@@ -566,8 +568,8 @@
     %
 :- pred candidate_init_vars_call(mode_info::in,
     list(prog_var)::in, list(mer_mode)::in,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is semidet.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is semidet.
 
 candidate_init_vars_call(_ModeInfo, [], [], !NonFree, !CandidateVars).
 candidate_init_vars_call(ModeInfo, [Arg | Args], [Mode | Modes],
@@ -579,19 +581,19 @@
         InitialInst \= free(_)
     ->
         % This arg is an input that needs instantiation.
-        !:CandidateVars = set.insert(!.CandidateVars, Arg)
+        set_of_var.insert(Arg, !CandidateVars)
     ;
         % Otherwise this arg could be an output...
         FinalInst \= free,
         FinalInst \= free(_)
     ->
         % And it is.
-        ( set.contains(!.NonFree, Arg) ->
+        ( set_of_var.contains(!.NonFree, Arg) ->
             % This arg appears in an implied mode.
             fail
         ;
             % This arg is instantiated on output.
-            !:NonFree = set.insert(!.NonFree, Arg)
+            set_of_var.insert(Arg, !NonFree)
         )
     ;
         % This arg is unused.
@@ -729,18 +731,18 @@
     % Given an association list of Vars - Goals,
     % combine all the Vars together into a single set.
     %
-:- pred get_all_waiting_vars(list(delayed_goal)::in, set(prog_var)::out)
+:- pred get_all_waiting_vars(list(delayed_goal)::in, set_of_progvar::out)
     is det.
 
 get_all_waiting_vars(DelayedGoals, Vars) :-
-    get_all_waiting_vars_2(DelayedGoals, set.init, Vars).
+    get_all_waiting_vars_2(DelayedGoals, set_of_var.init, Vars).
 
 :- pred get_all_waiting_vars_2(list(delayed_goal)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 get_all_waiting_vars_2([], Vars, Vars).
 get_all_waiting_vars_2([delayed_goal(Vars1, _, _) | Rest], Vars0, Vars) :-
-    set.union(Vars0, Vars1, Vars2),
+    set_of_var.union(Vars0, Vars1, Vars2),
     get_all_waiting_vars_2(Rest, Vars2, Vars).
 
 :- pred redelay_goals(list(delayed_goal)::in, delay_info::in, delay_info::out)
Index: compiler/modecheck_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_goal.m,v
retrieving revision 1.8
diff -u -b -r1.8 modecheck_goal.m
--- compiler/modecheck_goal.m	23 May 2011 05:08:08 -0000	1.8
+++ compiler/modecheck_goal.m	15 Aug 2011 13:50:16 -0000
@@ -140,6 +140,7 @@
 :- import_module parse_tree.prog_event.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bag.
 :- import_module bool.
@@ -297,7 +298,7 @@
             NonLocals, LargeFlatConstructs, !ModeInfo),
         ( mode_info_solver_init_is_supported(!.ModeInfo) ->
             mode_info_get_var_types(!.ModeInfo, VarTypes),
-            handle_solver_vars_in_disjs(set.to_sorted_list(NonLocals),
+            handle_solver_vars_in_disjs(set_of_var.to_sorted_list(NonLocals),
                 VarTypes, Disjuncts1, Disjuncts2, InstMaps0, InstMaps,
                 !ModeInfo)
         ;
@@ -312,7 +313,7 @@
     mode_checkpoint(exit, "disj", !ModeInfo).
 
 :- pred modecheck_disj_list(list(hlds_goal)::in, list(hlds_goal)::out,
-    list(instmap)::out, set(prog_var)::in, set(prog_var)::out,
+    list(instmap)::out, set_of_progvar::in, set_of_progvar::out,
     mode_info::in, mode_info::out) is det.
 
 modecheck_disj_list([], [], [], !LargeFlatConstructs, !ModeInfo).
@@ -326,13 +327,13 @@
     modecheck_disj_list(Goals0, Goals, InstMaps, !LargeFlatConstructs,
         !ModeInfo).
 
-:- pred merge_disj_branches(set(prog_var)::in, set(prog_var)::in,
+:- pred merge_disj_branches(set_of_progvar::in, set_of_progvar::in,
     list(hlds_goal)::in, list(hlds_goal)::out, list(instmap)::in,
     mode_info::in, mode_info::out) is det.
 
 merge_disj_branches(NonLocals, LargeFlatConstructs, Disjuncts0, Disjuncts,
         InstMaps0, !ModeInfo) :-
-    ( set.empty(LargeFlatConstructs) ->
+    ( set_of_var.is_empty(LargeFlatConstructs) ->
         Disjuncts = Disjuncts0,
         InstMaps = InstMaps0
     ;
@@ -356,7 +357,8 @@
         list.map(
             set_large_flat_constructs_to_ground_in_goal(LargeFlatConstructs),
             Disjuncts0, Disjuncts),
-        LargeFlatConstructList = set.to_sorted_list(LargeFlatConstructs),
+        LargeFlatConstructList =
+            set_of_var.to_sorted_list(LargeFlatConstructs),
         list.map(
             instmap_set_vars_same(ground(shared, none),
                 LargeFlatConstructList),
@@ -410,7 +412,7 @@
     mode_checkpoint(exit, "switch", !ModeInfo).
 
 :- pred modecheck_case_list(list(case)::in, prog_var::in, list(case)::out,
-    list(instmap)::out, set(prog_var)::in, set(prog_var)::out,
+    list(instmap)::out, set_of_progvar::in, set_of_progvar::out,
     mode_info::in, mode_info::out) is det.
 
 modecheck_case_list([], _Var, [], [], !LargeFlatConstructs, !ModeInfo).
@@ -445,13 +447,13 @@
     modecheck_case_list(Cases0, Var, Cases, InstMaps, !LargeFlatConstructs,
         !ModeInfo).
 
-:- pred merge_switch_branches(set(prog_var)::in, set(prog_var)::in,
+:- pred merge_switch_branches(set_of_progvar::in, set_of_progvar::in,
     list(case)::in, list(case)::out, list(instmap)::in,
     mode_info::in, mode_info::out) is det.
 
 merge_switch_branches(NonLocals, LargeFlatConstructs, Cases0, Cases,
         InstMaps0, !ModeInfo) :-
-    ( set.empty(LargeFlatConstructs) ->
+    ( set_of_var.is_empty(LargeFlatConstructs) ->
         Cases = Cases0,
         InstMaps = InstMaps0
     ;
@@ -459,7 +461,8 @@
         list.map(
             set_large_flat_constructs_to_ground_in_case(LargeFlatConstructs),
             Cases0, Cases),
-        LargeFlatConstructList = set.to_sorted_list(LargeFlatConstructs),
+        LargeFlatConstructList =
+            set_of_var.to_sorted_list(LargeFlatConstructs),
         list.map(
             instmap_set_vars_same(ground(shared, none),
                 LargeFlatConstructList),
@@ -474,19 +477,19 @@
 %
 
 :- pred accumulate_large_flat_constructs(hlds_goal::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 accumulate_large_flat_constructs(Goal, !LargeFlatConstructs) :-
-    ( set.empty(!.LargeFlatConstructs) ->
+    ( set_of_var.is_empty(!.LargeFlatConstructs) ->
         % Calling goal_large_flat_constructs and then set.intersect
         % would be waste of time; !:LargeFlatConstructs will still be empty.
         true
     ;
         GoalLargeFlatConstructs = goal_large_flat_constructs(Goal),
-        set.intersect(GoalLargeFlatConstructs, !LargeFlatConstructs)
+        set_of_var.intersect(GoalLargeFlatConstructs, !LargeFlatConstructs)
     ).
 
-:- func goal_large_flat_constructs(hlds_goal) = set(prog_var).
+:- func goal_large_flat_constructs(hlds_goal) = set_of_progvar.
 
 goal_large_flat_constructs(Goal) = LargeFlatConstructs :-
     Goal = hlds_goal(GoalExpr, _),
@@ -494,13 +497,13 @@
         GoalExpr = unify(_, _, _, _, _),
         % Unifications not wrapped in from_ground_term_construct scopes
         % are never marked by the modechecker as being constructed statically.
-        LargeFlatConstructs = set.init
+        LargeFlatConstructs = set_of_var.init
     ;
         ( GoalExpr = plain_call(_, _, _, _, _, _)
         ; GoalExpr = generic_call(_, _, _, _)
         ; GoalExpr = call_foreign_proc(_, _, _, _, _, _, _)
         ),
-        LargeFlatConstructs = set.init
+        LargeFlatConstructs = set_of_var.init
     ;
         ( GoalExpr = disj(_)
         ; GoalExpr = switch(_, _, _)
@@ -509,12 +512,12 @@
         ; GoalExpr = shorthand(_)
         ; GoalExpr = conj(parallel_conj, _)
         ),
-        LargeFlatConstructs = set.init
+        LargeFlatConstructs = set_of_var.init
     ;
         GoalExpr = scope(Reason, _),
         (
             Reason = from_ground_term(TermVar, from_ground_term_construct),
-            LargeFlatConstructs = set.make_singleton_set(TermVar)
+            LargeFlatConstructs = set_of_var.make_singleton(TermVar)
         ;
             ( Reason = from_ground_term(_, from_ground_term_deconstruct)
             ; Reason = from_ground_term(_, from_ground_term_other)
@@ -527,23 +530,24 @@
             ; Reason = barrier(_)
             ; Reason = trace_goal(_, _, _, _, _)
             ),
-            LargeFlatConstructs = set.init
+            LargeFlatConstructs = set_of_var.init
         )
     ;
         GoalExpr = conj(plain_conj, Conjuncts),
-        goals_large_flat_constructs(Conjuncts, set.init, LargeFlatConstructs)
+        goals_large_flat_constructs(Conjuncts,
+            set_of_var.init, LargeFlatConstructs)
     ).
 
 :- pred goals_large_flat_constructs(list(hlds_goal)::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 goals_large_flat_constructs([], !LargeFlatConstructs).
 goals_large_flat_constructs([Goal | Goals], !LargeFlatConstructs) :-
     GoalLargeFlatConstructs = goal_large_flat_constructs(Goal),
-    set.union(GoalLargeFlatConstructs, !LargeFlatConstructs),
+    set_of_var.union(GoalLargeFlatConstructs, !LargeFlatConstructs),
     goals_large_flat_constructs(Goals, !LargeFlatConstructs).
 
-:- pred set_large_flat_constructs_to_ground_in_goal(set(prog_var)::in,
+:- pred set_large_flat_constructs_to_ground_in_goal(set_of_progvar::in,
     hlds_goal::in, hlds_goal::out) is det.
 
 set_large_flat_constructs_to_ground_in_goal(LargeFlatConstructs,
@@ -571,7 +575,7 @@
         GoalExpr0 = scope(Reason, SubGoal0),
         (
             Reason = from_ground_term(TermVar, from_ground_term_construct),
-            ( set.member(TermVar, LargeFlatConstructs) ->
+            ( set_of_var.member(LargeFlatConstructs, TermVar) ->
                 InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo0),
                 instmap_delta_set_var(TermVar, ground(shared, none),
                     InstMapDelta0, InstMapDelta),
@@ -611,14 +615,15 @@
 
         InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo0),
         instmap_delta_changed_vars(InstMapDelta0, ChangedVars),
-        set.intersect(ChangedVars, LargeFlatConstructs, GroundVars),
+        set_of_var.intersect(ChangedVars, LargeFlatConstructs, GroundVars),
         instmap_delta_set_vars_same(ground(shared, none),
-            set.to_sorted_list(GroundVars), InstMapDelta0, InstMapDelta),
+            set_of_var.to_sorted_list(GroundVars),
+            InstMapDelta0, InstMapDelta),
         goal_info_set_instmap_delta(InstMapDelta, GoalInfo0, GoalInfo),
         Goal = hlds_goal(GoalExpr, GoalInfo)
     ).
 
-:- pred set_large_flat_constructs_to_ground_in_goals(set(prog_var)::in,
+:- pred set_large_flat_constructs_to_ground_in_goals(set_of_progvar::in,
     list(hlds_goal)::in, list(hlds_goal)::out) is det.
 
 set_large_flat_constructs_to_ground_in_goals(_, [], []).
@@ -629,7 +634,7 @@
     set_large_flat_constructs_to_ground_in_goals(LargeFlatConstructs,
         Goals0, Goals).
 
-:- pred set_large_flat_constructs_to_ground_in_case(set(prog_var)::in,
+:- pred set_large_flat_constructs_to_ground_in_case(set_of_progvar::in,
     case::in, case::out) is det.
 
 set_large_flat_constructs_to_ground_in_case(LargeFlatConstructs,
@@ -679,7 +684,7 @@
     modecheck_goal(Else0, Else1, !ModeInfo),
     mode_info_get_instmap(!.ModeInfo, InstMapElse1),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    handle_solver_vars_in_ite(set.to_sorted_list(NonLocals), VarTypes,
+    handle_solver_vars_in_ite(set_of_var.to_sorted_list(NonLocals), VarTypes,
         Then1, Then, Else1, Else,
         InstMapThen1, InstMapThen, InstMapElse1, InstMapElse, !ModeInfo),
     mode_info_set_instmap(InstMap0, !ModeInfo),
@@ -692,7 +697,8 @@
         InPromisePurityScope = not_in_promise_purity_scope,
         CondNonLocals0 = goal_get_nonlocals(Cond),
         CondNonLocals =
-            set.to_sorted_list(CondNonLocals0 `intersect` NonLocals),
+            set_of_var.to_sorted_list(
+                set_of_var.intersect(CondNonLocals0, NonLocals)),
         check_no_inst_any_vars(if_then_else, CondNonLocals,
             InstMap0, InstMap, !ModeInfo)
     ;
@@ -759,7 +765,8 @@
         InPromisePurityScope = not_in_promise_purity_scope,
         NegNonLocals = goal_info_get_nonlocals(GoalInfo0),
         instmap.init_unreachable(Unreachable),
-        check_no_inst_any_vars(negation, set.to_sorted_list(NegNonLocals),
+        check_no_inst_any_vars(negation,
+            set_of_var.to_sorted_list(NegNonLocals),
             InstMap0, Unreachable, !ModeInfo)
     ;
         InPromisePurityScope = in_promise_purity_scope
@@ -1066,7 +1073,7 @@
     SubGoal = hlds_goal(SubGoalExpr, SubGoalInfo),
     (
         NonLocals = goal_info_get_nonlocals(SubGoalInfo),
-        set.singleton_set(NonLocals, TermVar),
+        set_of_var.is_singleton(NonLocals, TermVar),
         goal_info_get_purity(SubGoalInfo) = purity_pure,
         SubGoalExpr = conj(plain_conj, [UnifyTermGoal | UnifyArgGoals]),
         % If TermVar is created by an impure unification, which is
@@ -1422,7 +1429,7 @@
         % mode_info_unlock_vars(var_lock_atomic_goal, OuterVars, !ModeInfo),
 
         % XXX STM: Handling of solver vars
-        handle_solver_vars_in_disjs(set.to_sorted_list(NonLocals),
+        handle_solver_vars_in_disjs(set_of_var.to_sorted_list(NonLocals),
             VarTypes, AtomicGoalList1, AtomicGoalList, InstMapList0,
             InstMapList, !ModeInfo),
         MainGoal = list.det_head(AtomicGoalList),
@@ -1518,9 +1525,10 @@
         mode_info_get_module_info(!.ModeInfo, ModuleInfo),
         inst_contains_any(ModuleInfo, Inst)
     ->
+        WaitingVars = set_of_var.make_singleton(NonLocal),
         ModeError = purity_error_should_be_in_promise_purity_scope(NegCtxtDesc,
             NonLocal),
-        mode_info_error(make_singleton_set(NonLocal), ModeError, !ModeInfo)
+        mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ;
         check_no_inst_any_vars(NegCtxtDesc, NonLocals, InstMap0, InstMap,
             !ModeInfo)
@@ -1565,13 +1573,13 @@
     add_necessary_disj_init_calls(Goals0, Goals, InstMaps0, InstMaps,
         EnsureInitialised, !ModeInfo).
 
-:- func append_init_calls_to_goal(set(prog_var), list(hlds_goal), hlds_goal) =
+:- func append_init_calls_to_goal(set_of_progvar, list(hlds_goal), hlds_goal) =
         hlds_goal.
 
 append_init_calls_to_goal(InitedVars, InitCalls, Goal0) = Goal :-
     Goal0 = hlds_goal(GoalExpr0, GoalInfo0),
     NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-    NonLocals = set.union(InitedVars, NonLocals0),
+    set_of_var.union(InitedVars, NonLocals0, NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
     ( GoalExpr0 = disj(Disjs0) ->
         Disjs = list.map(append_init_calls_to_goal(InitedVars, InitCalls),
Index: compiler/modecheck_unify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_unify.m,v
retrieving revision 1.141
diff -u -b -r1.141 modecheck_unify.m
--- compiler/modecheck_unify.m	23 May 2011 05:08:08 -0000	1.141
+++ compiler/modecheck_unify.m	15 Aug 2011 13:46:47 -0000
@@ -70,6 +70,7 @@
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -124,7 +125,7 @@
                 LambdaNonLocals),
             AnyVars = [_ | _]
         ->
-            set.init(WaitingVars),
+            set_of_var.init(WaitingVars),
             mode_info_error(WaitingVars,
                 purity_error_lambda_should_be_any(AnyVars), !ModeInfo),
             Goal = conj(plain_conj, [])
@@ -223,7 +224,7 @@
             UnifyGoalExpr = conj(plain_conj, [InitGoal, UnifySubGoal])
         )
     ;
-        set.list_to_set([X, Y], WaitingVars),
+        set_of_var.list_to_set([X, Y], WaitingVars),
         ModeError = mode_error_unify_var_var(X, Y, InstOfX, InstOfY),
         mode_info_error(WaitingVars, ModeError, !ModeInfo),
         % If we get an error, set the inst to not_reached to suppress
@@ -363,7 +364,7 @@
     % Mark the non-clobbered lambda variables as live.
     get_arg_lives(ModuleInfo0, Modes, ArgLives),
     get_live_vars(Vars, ArgLives, LiveVarsList),
-    set.list_to_set(LiveVarsList, LiveVars),
+    set_of_var.list_to_set(LiveVarsList, LiveVars),
     mode_info_add_live_vars(LiveVars, !ModeInfo),
 
     % Lock the non-locals.  A ground lambda goal is not allowed to bind any
@@ -376,21 +377,21 @@
     % is safe to bind the lambda goal itself.
     Goal0 = hlds_goal(_, GoalInfo0),
     NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-    set.delete_list(Vars, NonLocals0, NonLocals1),
+    set_of_var.delete_list(Vars, NonLocals0, NonLocals1),
     (
         Groundness = ho_ground,
         NonLocals = NonLocals1
     ;
         Groundness = ho_any,
         mode_info_get_var_types(!.ModeInfo, NonLocalTypes),
-        NonLocals = set.filter((pred(NonLocal::in) is semidet :-
+        NonLocals = set_of_var.filter((pred(NonLocal::in) is semidet :-
                 map.lookup(NonLocalTypes, NonLocal, NonLocalType),
                 instmap_lookup_var(InstMap1, NonLocal, NonLocalInst),
                 \+ inst_matches_initial(NonLocalInst, any(shared, none),
                     NonLocalType, ModuleInfo0)
             ), NonLocals1)
     ),
-    set.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
     instmap_lookup_vars(InstMap1, NonLocalsList, NonLocalInsts),
     mode_info_get_module_info(!.ModeInfo, ModuleInfo2),
     (
@@ -466,7 +467,7 @@
         (
             NonGroundNonLocals = [BadVar | _],
             instmap_lookup_var(InstMap1, BadVar, BadInst),
-            set.singleton_set(WaitingVars, BadVar),
+            WaitingVars = set_of_var.make_singleton(BadVar),
             ModeError = mode_error_non_local_lambda_var(BadVar, BadInst),
             mode_info_error(WaitingVars, ModeError, !ModeInfo)
         ;
@@ -509,7 +510,7 @@
             RHS0, RHS, Unification0, Unification, !ModeInfo),
         modecheck_set_var_inst(X, Inst, no, !ModeInfo)
     ;
-        set.list_to_set([X], WaitingVars),
+        set_of_var.list_to_set([X], WaitingVars),
         ModeError = mode_error_unify_var_lambda(X, InstOfX, InstOfY),
         mode_info_error(WaitingVars, ModeError, !ModeInfo),
         % If we get an error, set the inst to not_reached to avoid cascading
@@ -546,8 +547,9 @@
             ( MatchResult = possible_modes([])
             ; MatchResult = ho_arg_not_ground
             ),
+            WaitingVars = set_of_var.make_singleton(X),
             ModeError = mode_error_unify_var_multimode_pred(X, PredId),
-            mode_info_error(set.make_singleton_set(X), ModeError, !ModeInfo),
+            mode_info_error(WaitingVars, ModeError, !ModeInfo),
             % Return any old garbage.
             Goal = true_goal_expr
         ;
@@ -560,9 +562,10 @@
                 GoalInfo, Goal, !ModeInfo)
         ;
             MatchResult = possible_modes([_, _ | _]),
+            WaitingVars = set_of_var.make_singleton(X),
             ModeError =
                 mode_error_unify_var_multimode_pred_undetermined(X, PredId),
-            mode_info_error(set.make_singleton_set(X), ModeError, !ModeInfo),
+            mode_info_error(WaitingVars, ModeError, !ModeInfo),
             % Return any old garbage.
             Goal = true_goal_expr
         )
@@ -652,7 +655,7 @@
         list.member(X, ArgVars0),
         \+ inst_is_ground(ModuleInfo0, InstOfX)
     ->
-        set.list_to_set([X], WaitingVars),
+        set_of_var.list_to_set([X], WaitingVars),
         ModeError = mode_error_unify_var_functor(X, InstConsId, ArgVars0,
             InstOfX, InstArgs),
         mode_info_error(WaitingVars, ModeError, !ModeInfo),
@@ -739,7 +742,7 @@
             bind_args(Inst, ArgVars, UnifyArgInsts, !ModeInfo)
         )
     ;
-        set.list_to_set([X | ArgVars0], WaitingVars), % conservative
+        set_of_var.list_to_set([X | ArgVars0], WaitingVars), % conservative
         ModeError = mode_error_unify_var_functor(X, InstConsId, ArgVars0,
             InstOfX, InstArgs),
         mode_info_error(WaitingVars, ModeError, !ModeInfo),
@@ -953,7 +956,7 @@
     % N.B. This may overestimate the set of non-locals,
     % but that shouldn't cause any problems.
 
-    set.list_to_set([Var0, Var], NonLocals),
+    set_of_var.list_to_set([Var0, Var], NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
     goal_info_set_context(Context, GoalInfo1, GoalInfo2),
 
@@ -1124,8 +1127,8 @@
 record_optimize_away(GoalInfo, Var1, Var2, !ModeInfo) :-
     NonLocals = goal_info_get_nonlocals(GoalInfo),
     (
-        set.member(Var1, NonLocals),
-        set.member(Var2, NonLocals)
+        set_of_var.member(NonLocals, Var1),
+        set_of_var.member(NonLocals, Var2)
     ->
         true
     ;
@@ -1187,14 +1190,14 @@
         Type = type_variable(_, _),
         \+ inst_is_ground_or_any(ModuleInfo3, InitialInstX)
     ->
-        set.singleton_set(WaitingVars, X),
+        WaitingVars = set_of_var.make_singleton(X),
         ModeError = mode_error_poly_unify(X, InitialInstX),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ;
         Type = type_variable(_, _),
         \+ inst_is_ground_or_any(ModuleInfo3, InitialInstY)
     ->
-        set.singleton_set(WaitingVars, Y),
+        WaitingVars = set_of_var.make_singleton(Y),
         ModeError = mode_error_poly_unify(Y, InitialInstY),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ;
@@ -1216,7 +1219,7 @@
         ->
             true
         ;
-            set.init(WaitingVars),
+            set_of_var.init(WaitingVars),
             ModeError =
                 mode_error_unify_pred(X, error_at_var(Y), Type, PredOrFunc),
             mode_info_error(WaitingVars, ModeError, !ModeInfo)
@@ -1324,7 +1327,7 @@
         % If it is a deconstruction, it is a mode error.
         % The error message would be incorrect in unreachable code,
         % since not_reached is considered bound.
-        set.init(WaitingVars),
+        set_of_var.init(WaitingVars),
         mode_info_get_var_types(!.ModeInfo, VarTypes0),
         map.lookup(VarTypes0, X, Type),
         ModeError = mode_error_unify_pred(X,
@@ -1421,7 +1424,7 @@
                 type_is_higher_order_details(TypeOfX, _, PredOrFunc, _, _),
                 instmap_is_reachable(InstMap0)
             ->
-                set.init(WaitingVars),
+                set_of_var.init(WaitingVars),
                 ModeError = mode_error_unify_pred(X,
                     error_at_functor(ConsId, ArgVars), TypeOfX, PredOrFunc),
                 mode_info_error(WaitingVars, ModeError, !ModeInfo)
Index: compiler/modecheck_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_util.m,v
retrieving revision 1.7
diff -u -b -r1.7 modecheck_util.m
--- compiler/modecheck_util.m	23 May 2011 05:08:08 -0000	1.7
+++ compiler/modecheck_util.m	15 Aug 2011 13:36:58 -0000
@@ -29,19 +29,21 @@
 :- type extra_goals
     --->    no_extra_goals
     ;       extra_goals(
+                % Goals to insert before the main goal.
                 extra_before_main   :: list(hlds_goal),
-                                    % goals to insert before the main goal
+
+                % Goals to append after the main goal.
                 extra_after_main    :: list(hlds_goal)
-                                    % goals to append after the main goal
             ).
 
 :- type after_goals
     --->    no_after_goals
     ;       after_goals(
+                % Instmap at end of main goal.
                 after_instmap       :: instmap,
-                                    % instmap at end of main goal
+
+                % Goals to append after the main goal.
                 after_goals         :: list(hlds_goal)
-                                    % goals to append after the main goal
             ).
 
     % Append_extra_goals inserts adds some goals to the
@@ -193,6 +195,7 @@
 :- import_module mdbcomp.
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module int.
@@ -243,11 +246,11 @@
 
         % Recompute the new set of non-local variables for the main goal.
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        set.list_to_set(Args0, OldArgVars),
-        set.list_to_set(Args, NewArgVars),
-        set.difference(NewArgVars, OldArgVars, IntroducedVars),
-        set.union(NonLocals0, IntroducedVars, OutsideVars),
-        set.intersect(OutsideVars, NewArgVars, NonLocals),
+        set_of_var.list_to_set(Args0, OldArgVars),
+        set_of_var.list_to_set(Args, NewArgVars),
+        set_of_var.difference(NewArgVars, OldArgVars, IntroducedVars),
+        set_of_var.union(NonLocals0, IntroducedVars, OutsideVars),
+        set_of_var.intersect(OutsideVars, NewArgVars, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
 
         % Combine the main goal and the extra goals into a conjunction.
@@ -433,7 +436,7 @@
         ExpectedIsLive = is_dead,
         VarIsLive = is_live
     ->
-        set.singleton_set(WaitingVars, VarId),
+        WaitingVars = set_of_var.make_singleton(VarId),
         ModeError = mode_error_var_is_live(VarId),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ;
@@ -450,7 +453,7 @@
     ( VarIsLive = ExpectedIsLive ->
         true
     ;
-        set.singleton_set(WaitingVars, VarId),
+        WaitingVars = set_of_var.make_singleton(VarId),
         ModeError = mode_error_var_is_live(VarId),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ).
@@ -523,7 +526,7 @@
     ->
         mode_info_set_module_info(ModuleInfo, !ModeInfo)
     ;
-        set.singleton_set(WaitingVars, Var),
+        WaitingVars = set_of_var.make_singleton(Var),
         ModeError = mode_error_var_has_inst(Var, VarInst, Inst),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ).
@@ -544,7 +547,7 @@
     ->
         mode_info_set_module_info(ModuleInfo, !ModeInfo)
     ;
-        set.singleton_set(WaitingVars, Var),
+        WaitingVars = set_of_var.make_singleton(Var),
         ModeError = mode_error_var_has_inst(Var, VarInst, Inst),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ).
@@ -560,7 +563,7 @@
     ->
         mode_info_set_module_info(ModuleInfo, !ModeInfo)
     ;
-        set.singleton_set(WaitingVars, Var),
+        WaitingVars = set_of_var.make_singleton(Var),
         ModeError = mode_error_var_has_inst(Var, VarInst, Inst),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ).
@@ -682,7 +685,7 @@
                     ModuleInfo)
             )
         ->
-            set.singleton_set(WaitingVars, Var0),
+            WaitingVars = set_of_var.make_singleton(Var0),
             ModeError = mode_error_bind_var(Reason0, Var0, Inst0, Inst),
             mode_info_error(WaitingVars, ModeError, !ModeInfo)
         ;
@@ -699,8 +702,8 @@
         PVars0 = []
     ;
         PVars0 = [par_conj_mode_check(NonLocals, Bound0) | PVars1],
-        ( set.member(Var0, NonLocals) ->
-            set.insert(Var0, Bound0, Bound),
+        ( set_of_var.member(NonLocals, Var0) ->
+            set_of_var.insert(Var0, Bound0, Bound),
             PVars = [par_conj_mode_check(NonLocals, Bound) | PVars1]
         ;
             PVars = PVars0
@@ -773,7 +776,7 @@
         ;
             % If the type is a type variable, or isn't a solver type,
             % then give up.
-            set.singleton_set(WaitingVars, Var0),
+            WaitingVars = set_of_var.make_singleton(Var0),
             ModeError = mode_error_implied_mode(Var0, VarInst0, InitialInst),
             mode_info_error(WaitingVars, ModeError, !ModeInfo)
         )
@@ -782,7 +785,7 @@
     ->
         % This is the case we can't handle.
         Var = Var0,
-        set.singleton_set(WaitingVars, Var0),
+        WaitingVars = set_of_var.make_singleton(Var0),
         ModeError = mode_error_implied_mode(Var0, VarInst0, InitialInst),
         mode_info_error(WaitingVars, ModeError, !ModeInfo)
     ;
@@ -876,7 +879,7 @@
             mode_info_get_module_info(!.ModeInfo, ModuleInfo),
             module_info_get_name(ModuleInfo, ModuleName)
         ),
-        NonLocals = set.make_singleton_set(Var),
+        NonLocals = set_of_var.make_singleton(Var),
         InstMapDeltaAL = [Var - Inst],
         InstMapDelta = instmap_delta_from_assoc_list(InstMapDeltaAL),
         build_call(ModuleName, PredName, [Var], [VarType], NonLocals,
@@ -905,7 +908,7 @@
     ).
 
 :- pred build_call(module_name::in, string::in, list(prog_var)::in,
-    list(mer_type)::in, set(prog_var)::in, instmap_delta::in,
+    list(mer_type)::in, set_of_progvar::in, instmap_delta::in,
     prog_context::in, maybe(call_unify_context)::in, hlds_goal::out,
     mode_info::in, mode_info::out) is semidet.
 
Index: compiler/modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modes.m,v
retrieving revision 1.396
diff -u -b -r1.396 modes.m
--- compiler/modes.m	23 May 2011 05:08:08 -0000	1.396
+++ compiler/modes.m	15 Aug 2011 14:00:22 -0000
@@ -157,6 +157,7 @@
 :- import_module libs.options.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_out.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bag.
@@ -167,7 +168,6 @@
 :- import_module maybe.
 :- import_module queue.
 :- import_module require.
-:- import_module set.
 :- import_module string.
 :- import_module term.
 
@@ -621,7 +621,7 @@
         % Construct the initial set of live vars:
         % initially, only the non-clobbered head variables are live.
         get_live_vars(HeadVars, ArgLives0, LiveVarsList),
-        set.list_to_set(LiveVarsList, LiveVars),
+        set_of_var.list_to_set(LiveVarsList, LiveVars),
 
         % Initialize the mode info.
         mode_info_init(!.ModuleInfo, PredId, ProcId, Context, LiveVars,
@@ -769,7 +769,7 @@
         BodyNonLocals = goal_info_get_nonlocals(BodyGoalInfo0),
         mode_info_get_var_types(!.ModeInfo, VarTypes0),
         SolverNonLocals = list.filter(is_solver_var(VarTypes0, ModuleInfo),
-            set.to_sorted_list(BodyNonLocals)),
+            set_of_var.to_sorted_list(BodyNonLocals)),
         SolverNonLocals = []
     ->
         BodyContext = goal_info_get_context(BodyGoalInfo0),
@@ -1063,7 +1063,7 @@
     Case = case(MainConsId, OtherConsIds, Goal).
 
 :- pred unique_modecheck_clause_disj(list(prog_var)::in, instmap::in,
-    list(mer_inst)::in, determinism::in, set(prog_var)::in, bag(prog_var)::in,
+    list(mer_inst)::in, determinism::in, set_of_progvar::in, bag(prog_var)::in,
     hlds_goal::in, hlds_goal::out, mode_info::in, mode_info::out) is det.
 
 unique_modecheck_clause_disj(HeadVars, InstMap0, ArgFinalInsts0, DisjDetism,
@@ -1255,7 +1255,7 @@
                         % I don't think this can happen. But just in case...
                         Reason = wrongly_instantiated
                     ),
-                    set.init(WaitingVars),
+                    set_of_var.init(WaitingVars),
                     ModeError = mode_error_final_inst(ArgNum, Var, VarInst,
                         Inst, Reason),
                     mode_info_error(WaitingVars, ModeError, !ModeInfo)
Index: compiler/ordering_mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ordering_mode_constraints.m,v
retrieving revision 1.33
diff -u -b -r1.33 ordering_mode_constraints.m
--- compiler/ordering_mode_constraints.m	10 May 2011 04:12:25 -0000	1.33
+++ compiler/ordering_mode_constraints.m	15 Aug 2011 09:11:24 -0000
@@ -115,6 +115,7 @@
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bimap.
 :- import_module bool.
@@ -500,7 +501,7 @@
     Nonlocals = goal_info_get_nonlocals(GoalInfo),
     GoalId = goal_info_get_goal_id(GoalInfo),
 
-    set.fold(
+    set_of_var.fold(
         (pred(NL::in, RMap0::in, RMap::out) is det :-
             multi_map.set(NL, NL `in` PredId `at` GoalId, RMap0, RMap)
         ),
Index: compiler/par_conj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/par_conj_gen.m,v
retrieving revision 1.45
diff -u -b -r1.45 par_conj_gen.m
--- compiler/par_conj_gen.m	24 May 2011 04:16:48 -0000	1.45
+++ compiler/par_conj_gen.m	15 Aug 2011 13:23:36 -0000
@@ -117,6 +117,7 @@
 :- import_module ll_backend.exprn_aux.
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module cord.
@@ -174,7 +175,7 @@
     save_variables_on_stack(Vars, SaveCode, !CI),
 
     Nonlocals = goal_info_get_code_gen_nonlocals(GoalInfo),
-    set.to_sorted_list(Nonlocals, Variables),
+    set_of_var.to_sorted_list(Nonlocals, Variables),
     get_instmap(!.CI, Initial),
     Delta = goal_info_get_instmap_delta(GoalInfo),
     instmap.apply_instmap_delta(Initial, Delta, Final),
Index: compiler/pd_cost.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_cost.m,v
retrieving revision 1.40
diff -u -b -r1.40 pd_cost.m
--- compiler/pd_cost.m	23 May 2011 05:08:09 -0000	1.40
+++ compiler/pd_cost.m	15 Aug 2011 12:50:03 -0000
@@ -43,11 +43,11 @@
 :- implementation.
 
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module int.
 :- import_module list.
 :- import_module require.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -125,7 +125,7 @@
         unexpected($module, $pred, "shorthand")
     ).
 
-:- pred unify_cost(set(prog_var)::in, unification::in, int::out) is det.
+:- pred unify_cost(set_of_progvar::in, unification::in, int::out) is det.
 
 unify_cost(NonLocals, Unification, Cost) :-
     (
@@ -139,7 +139,7 @@
         Cost = cost_of_simple_test
     ;
         Unification = construct(Var, _, Args, _, _, _, _),
-        ( set.member(Var, NonLocals) ->
+        ( set_of_var.member(NonLocals, Var) ->
             list.length(Args, Arity),
             Cost = cost_of_heap_incr + Arity * cost_of_heap_assign
         ;
@@ -154,7 +154,7 @@
             CanFail = cannot_fail,
             Cost0 = 0
         ),
-        list.filter(set.contains(NonLocals), Args, NonLocalArgs),
+        list.filter(set_of_var.contains(NonLocals), Args, NonLocalArgs),
         list.length(NonLocalArgs, NumAssigns),
         Cost = Cost0 + cost_of_heap_incr + NumAssigns * cost_of_heap_assign
     ).
Index: compiler/pd_debug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_debug.m,v
retrieving revision 1.29
diff -u -b -r1.29 pd_debug.m
--- compiler/pd_debug.m	11 Aug 2011 23:18:59 -0000	1.29
+++ compiler/pd_debug.m	15 Aug 2011 12:49:01 -0000
@@ -225,8 +225,7 @@
     pd_info_get_instmap(PDInfo, InstMap),
     pd_info_get_module_info(PDInfo, ModuleInfo),
     io.write_string(Msg, !IO),
-    goal_util.goal_vars(hlds_goal(GoalExpr, GoalInfo), VarsSet),
-    Vars = set_of_var.bitset_to_set(VarsSet),
+    goal_util.goal_vars(hlds_goal(GoalExpr, GoalInfo), Vars),
     instmap_restrict(Vars, InstMap, InstMap1),
     write_instmap(InstMap1, VarSet, yes, 1, !IO),
     io.nl(!IO),
Index: compiler/pd_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_info.m,v
retrieving revision 1.45
diff -u -b -r1.45 pd_info.m
--- compiler/pd_info.m	6 May 2011 05:03:22 -0000	1.45
+++ compiler/pd_info.m	15 Aug 2011 12:50:54 -0000
@@ -122,6 +122,7 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.pd_debug.
 :- import_module transform_hlds.pd_util.
 
@@ -590,7 +591,7 @@
         NewGoal, NewVarTypes, OldNewRenaming, TypeRenaming),
     OldGoal = hlds_goal(_, OldGoalInfo),
     OldNonLocals0 = goal_info_get_nonlocals(OldGoalInfo),
-    set.to_sorted_list(OldNonLocals0, OldNonLocalsList),
+    set_of_var.to_sorted_list(OldNonLocals0, OldNonLocalsList),
     pd_info.check_insts(ModuleInfo, OldNonLocalsList, OldNewRenaming,
         OldInstMap, NewInstMap, NewVarTypes, exact, Exact),
 
@@ -636,7 +637,7 @@
     pd_info_get_instmap(!.PDInfo, InstMap),
     Goal = hlds_goal(_, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    set.to_sorted_list(NonLocals, Args),
+    set_of_var.to_sorted_list(NonLocals, Args),
     pd_info_get_counter(!.PDInfo, Counter0),
     counter.allocate(Count, Counter0, Counter),
     pd_info_set_counter(Counter, !PDInfo),
Index: compiler/pd_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_util.m,v
retrieving revision 1.84
diff -u -b -r1.84 pd_util.m
--- compiler/pd_util.m	21 Jul 2011 06:58:26 -0000	1.84
+++ compiler/pd_util.m	15 Aug 2011 13:09:11 -0000
@@ -22,13 +22,13 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.pd_info.
 
 :- import_module bool.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -54,7 +54,7 @@
 
     % Apply unique_modes.m to the goal.
     %
-:- pred unique_modecheck_goal_live_vars(set(prog_var)::in,
+:- pred unique_modecheck_goal_live_vars(set_of_progvar::in,
     hlds_goal::in, hlds_goal::out, list(mode_error_info)::out,
     pd_info::in, pd_info::out) is det.
 
@@ -74,7 +74,7 @@
 
     % Recompute the non-locals of the goal.
     %
-:- pred pd_requantify_goal(set(prog_var)::in,
+:- pred pd_requantify_goal(set_of_progvar::in,
     hlds_goal::in, hlds_goal::out, pd_info::in, pd_info::out) is det.
 
     % Apply mode_util.recompute_instmap_delta to the goal.
@@ -325,21 +325,21 @@
     % which of the non-local variables are not clobbered by the goal.
     %
 :- pred get_goal_live_vars(pd_info::in, hlds_goal::in,
-    set(prog_var)::out) is det.
+    set_of_progvar::out) is det.
 
 get_goal_live_vars(PDInfo, hlds_goal(_, GoalInfo), !:Vars) :-
     pd_info_get_module_info(PDInfo, ModuleInfo),
     InstMapDelta = goal_info_get_instmap_delta(GoalInfo),
     pd_info_get_instmap(PDInfo, InstMap),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    set.to_sorted_list(NonLocals, NonLocalsList),
-    set.init(!:Vars),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.init(!:Vars),
     get_goal_live_vars_2(ModuleInfo, NonLocalsList, InstMap, InstMapDelta,
         !Vars).
 
 :- pred get_goal_live_vars_2(module_info::in, prog_vars::in,
     instmap::in, instmap_delta::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 get_goal_live_vars_2(_, [], _, _, !Vars).
 get_goal_live_vars_2(ModuleInfo, [NonLocal | NonLocals],
@@ -352,7 +352,7 @@
     ( inst_is_clobbered(ModuleInfo, FinalInst) ->
         true
     ;
-        set.insert(NonLocal, !Vars)
+        set_of_var.insert(NonLocal, !Vars)
     ),
     get_goal_live_vars_2(ModuleInfo, NonLocals, InstMap, InstMapDelta, !Vars).
 
@@ -428,7 +428,7 @@
     proc_info_get_vartypes(ProcInfo, VarTypes),
     instmap.init_reachable(InstMap0),
     map.init(Vars0),
-    set.init(LeftVars0),
+    set_of_var.init(LeftVars0),
     goal_to_conj_list(Goal, GoalList),
     (
         get_branch_vars_goal_2(!.ModuleInfo, GoalList, no,
@@ -491,7 +491,7 @@
     % information.
     %
 :- pred get_extra_info_headvars(prog_vars::in, int::in,
-    set(prog_var)::in, pd_var_info::in,
+    set_of_progvar::in, pd_var_info::in,
     branch_info_map(int)::in, branch_info_map(int)::out,
     set(int)::in, set(int)::out) is det.
 
@@ -503,7 +503,7 @@
     ;
         true
     ),
-    ( set.member(HeadVar, LeftVars) ->
+    ( set_of_var.member(LeftVars, HeadVar) ->
         set.insert(ArgNo, !ThisProcLeftVars)
     ;
         true
@@ -520,7 +520,7 @@
     pd_info_get_proc_arg_info(!.PDInfo, ProcArgInfo),
     pd_info_get_proc_info(!.PDInfo, ProcInfo),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    set.init(LeftVars0),
+    set_of_var.init(LeftVars0),
     map.init(Vars0),
     (
         get_branch_vars_goal_2(ModuleInfo0, [Goal], no,
@@ -533,14 +533,15 @@
 
         % OpaqueVars is only filled in for calls.
         set.init(OpaqueVars),
-        MaybeBranchInfo = yes(pd_branch_info(Vars, LeftVars, OpaqueVars))
+        MaybeBranchInfo = yes(pd_branch_info(Vars,
+            set_of_var.bitset_to_set(LeftVars), OpaqueVars))
     ;
         MaybeBranchInfo = no
     ).
 
 :- pred get_branch_vars_goal_2(module_info::in, hlds_goals::in,
     bool::in, vartypes::in, instmap::in,
-    set(prog_var)::in, set(prog_var)::out,
+    set_of_progvar::in, set_of_progvar::out,
     pd_var_info::in, pd_var_info::out) is semidet.
 
 get_branch_vars_goal_2(_, [], yes, _, _, !LeftVars, !Vars).
@@ -602,11 +603,11 @@
     % also check for if-then-elses with simple conditions.
     %
 :- pred get_left_vars(hlds_goal::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 get_left_vars(Goal, Vars0, Vars) :-
     ( Goal = hlds_goal(switch(Var, _, _), _) ->
-        set.insert(Var, Vars0, Vars)
+        set_of_var.insert(Var, Vars0, Vars)
     ;
         Vars = Vars0
     ).
@@ -638,7 +639,7 @@
             )
         ),
     instmap_delta_changed_vars(InstMapDelta, ChangedVars),
-    set.to_sorted_list(ChangedVars, ChangedVarsList),
+    set_of_var.to_sorted_list(ChangedVars, ChangedVarsList),
     list.foldl(AddExtraInfoVars, ChangedVarsList, !ExtraVars),
 
     % We have extra information about a switched-on variable
@@ -757,7 +758,7 @@
             true
         )
     ;
-        set.init(LeftVars0),
+        set_of_var.init(LeftVars0),
         map.init(!:Vars),
         get_branch_vars_goal_2(ModuleInfo, [Goal], no,
             VarTypes, InstMap, LeftVars0, _, !Vars)
@@ -796,7 +797,7 @@
         proc_info_get_vartypes(!.ProcInfo, VarTypes0),
         proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps0),
         implicitly_quantify_goal_general(ordinary_nonlocals_no_lambda,
-            set_to_bitset(NonLocals), _, Goal0, Goal, VarSet0, VarSet,
+            NonLocals, _, Goal0, Goal, VarSet0, VarSet,
             VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
         proc_info_set_varset(VarSet, !ProcInfo),
         proc_info_set_vartypes(VarTypes, !ProcInfo),
@@ -995,8 +996,8 @@
     list.map(Search, OldArgs, NewArgs),
     NewGoal = hlds_goal(_, NewGoalInfo),
     NewNonLocals = goal_info_get_nonlocals(NewGoalInfo),
-    set.delete_list(NewArgs, NewNonLocals, UnmatchedNonLocals),
-    set.empty(UnmatchedNonLocals),
+    set_of_var.delete_list(NewArgs, NewNonLocals, UnmatchedNonLocals),
+    set_of_var.is_empty(UnmatchedNonLocals),
 
     % Check that argument types of NewGoal are subsumed by those of OldGoal.
     collect_matching_arg_types(OldArgs, OldArgTypes,
@@ -1166,8 +1167,8 @@
     InstmapDelta1 = goal_info_get_instmap_delta(GoalInfo1),
     instmap_delta_changed_vars(InstmapDelta1, ChangedVars1),
     NonLocals2 = goal_info_get_nonlocals(GoalInfo2),
-    set.intersect(ChangedVars1, NonLocals2, Intersection),
-    \+ set.empty(Intersection).
+    set_of_var.intersect(ChangedVars1, NonLocals2, Intersection),
+    set_of_var.is_non_empty(Intersection).
 
 %-----------------------------------------------------------------------------%
 :- end_module transform_hlds.pd_util.
Index: compiler/polymorphism.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/polymorphism.m,v
retrieving revision 1.367
diff -u -b -r1.367 polymorphism.m
--- compiler/polymorphism.m	21 Jul 2011 06:58:26 -0000	1.367
+++ compiler/polymorphism.m	15 Aug 2011 08:28:52 -0000
@@ -1388,12 +1388,12 @@
         ExistQVars = [],
         fixup_lambda_quantification(ArgVars0, LambdaVars, ExistQVars,
             LambdaGoal1, LambdaGoal, NonLocalTypeInfos, !Info),
-        set.to_sorted_list(NonLocalTypeInfos, NonLocalTypeInfosList),
+        set_of_var.to_sorted_list(NonLocalTypeInfos, NonLocalTypeInfosList),
         ArgVars = NonLocalTypeInfosList ++ ArgVars0,
         Y1 = rhs_lambda_goal(Purity, Groundness, PredOrFunc, EvalMethod,
             ArgVars, LambdaVars, Modes, Det, LambdaGoal),
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        set.union(NonLocals0, NonLocalTypeInfos, NonLocals),
+        set_of_var.union(NonLocals0, NonLocalTypeInfos, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
 
         % Complicated (in-in) argument unifications are impossible for lambda
@@ -1437,7 +1437,7 @@
     % Insert the TypeInfoVars into the nonlocals field of the goal_info
     % for the unification goal.
     NonLocals0 = goal_info_get_nonlocals(!.GoalInfo),
-    set.insert_list(TypeInfoVars, NonLocals0, NonLocals),
+    set_of_var.insert_list(TypeInfoVars, NonLocals0, NonLocals),
     goal_info_set_nonlocals(NonLocals, !GoalInfo),
 
     % Also save those type_info vars into a field in the complicated_unify,
@@ -1557,7 +1557,7 @@
             ExtraVars, ExtraGoals, !Info),
         ArgVars = ExtraVars ++ ArgVars0,
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        set.insert_list(ExtraVars, NonLocals0, NonLocals),
+        set_of_var.insert_list(ExtraVars, NonLocals0, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
 
         % Some of the argument unifications may be complicated unifications,
@@ -1610,9 +1610,9 @@
     % to compute constraint_ids correctly.
 
     NonLocals = goal_info_get_nonlocals(GoalInfo0),
-    set.insert_list(LambdaVars, NonLocals, OutsideVars),
-    set.list_to_set(Args, InsideVars),
-    set.intersect(OutsideVars, InsideVars, LambdaNonLocals),
+    set_of_var.insert_list(LambdaVars, NonLocals, OutsideVars),
+    set_of_var.list_to_set(Args, InsideVars),
+    set_of_var.intersect(OutsideVars, InsideVars, LambdaNonLocals),
     GoalId = goal_info_get_goal_id(GoalInfo0),
     goal_info_init(LambdaGoalInfo0),
     goal_info_set_context(Context, LambdaGoalInfo0, LambdaGoalInfo1),
@@ -2130,7 +2130,7 @@
 
         % Update the nonlocals.
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        set.insert_list(ExtraVars, NonLocals0, NonLocals),
+        set_of_var.insert_list(ExtraVars, NonLocals0, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo)
     ).
 
@@ -2201,8 +2201,8 @@
         ExtraArgs, ExtraGoals, !Info),
     CallArgs = ExtraArgs ++ CallArgs0,
     NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-    NonLocals1 = set.list_to_set(ExtraArgs),
-    NonLocals = set.union(NonLocals0, NonLocals1),
+    NonLocals1 = set_of_var.list_to_set(ExtraArgs),
+    set_of_var.union(NonLocals0, NonLocals1, NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
     CallGoalExpr = plain_call(PredId, ProcId, CallArgs, BuiltinState,
         MaybeCallUnifyContext, SymName),
@@ -2263,25 +2263,26 @@
     %
 :- pred fixup_lambda_quantification(list(prog_var)::in,
     list(prog_var)::in, existq_tvars::in, hlds_goal::in, hlds_goal::out,
-    set(prog_var)::out, poly_info::in, poly_info::out) is det.
+    set_of_progvar::out, poly_info::in, poly_info::out) is det.
 
 fixup_lambda_quantification(ArgVars, LambdaVars, ExistQVars, !Goal,
         NewOutsideVars, !Info) :-
     poly_info_get_rtti_varmaps(!.Info, RttiVarMaps0),
     ( rtti_varmaps_no_tvars(RttiVarMaps0) ->
-        set.init(NewOutsideVars)
+        set_of_var.init(NewOutsideVars)
     ;
         poly_info_get_varset(!.Info, VarSet0),
         poly_info_get_var_types(!.Info, VarTypes0),
         !.Goal = hlds_goal(_, GoalInfo0),
         NonLocals = goal_info_get_nonlocals(GoalInfo0),
-        set.insert_list(ArgVars, NonLocals, NonLocalsPlusArgs0),
-        set.insert_list(LambdaVars, NonLocalsPlusArgs0, NonLocalsPlusArgs),
+        set_of_var.insert_list(ArgVars, NonLocals, NonLocalsPlusArgs0),
+        set_of_var.insert_list(LambdaVars,
+            NonLocalsPlusArgs0, NonLocalsPlusArgs),
         goal_util.extra_nonlocal_typeinfos(RttiVarMaps0, VarTypes0,
             ExistQVars, NonLocalsPlusArgs, NewOutsideVars),
-        set.union(NonLocals, NewOutsideVars, OutsideVars),
+        set_of_var.union(NonLocals, NewOutsideVars, OutsideVars),
         implicitly_quantify_goal_general(ordinary_nonlocals_maybe_lambda,
-            set_to_bitset(OutsideVars), _Warnings, !Goal,
+            OutsideVars, _Warnings, !Goal,
             VarSet0, VarSet, VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
         poly_info_set_varset_and_types(VarSet, VarTypes, !Info),
         poly_info_set_rtti_varmaps(RttiVarMaps, !Info)
@@ -2551,7 +2552,7 @@
         BaseUnification, BaseUnifyContext),
 
     % Create the unification goal.
-    set.list_to_set([BaseVar], NonLocals),
+    NonLocals = set_of_var.make_singleton(BaseVar),
     InstmapDelta = instmap_delta_bind_var(BaseVar),
     goal_info_init(NonLocals, InstmapDelta, detism_det, purity_pure,
         BaseGoalInfo),
@@ -2587,7 +2588,7 @@
 
     % Create a goal_info for the unification.
     goal_info_init(GoalInfo0),
-    set.list_to_set([TypeClassInfoVar | AllArgVars], NonLocals),
+    set_of_var.list_to_set([TypeClassInfoVar | AllArgVars], NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
     list.duplicate(NumArgVars, ground(shared, none), ArgInsts),
     % Note that we could perhaps be more accurate than `ground(shared)',
@@ -3139,7 +3140,7 @@
         UnifyContext),
 
     % Create a goal_info for the unification.
-    set.list_to_set([TypeInfoVar | ArgVars], NonLocals),
+    set_of_var.list_to_set([TypeInfoVar | ArgVars], NonLocals),
     list.duplicate(NumArgVars, ground(shared, none), ArgInsts),
     % note that we could perhaps be more accurate than `ground(shared)',
     % but it shouldn't make any difference.
@@ -3172,7 +3173,7 @@
         Unification, UnifyContext),
 
     % Create a goal_info for the unification.
-    set.list_to_set([TypeCtorInfoVar], NonLocals),
+    NonLocals = set_of_var.make_singleton(TypeCtorInfoVar),
     InstmapDelta = instmap_delta_bind_var(TypeCtorInfoVar),
     goal_info_init(NonLocals, InstmapDelta, detism_det, purity_pure, GoalInfo),
     TypeCtorInfoGoal = hlds_goal(Unify, GoalInfo).
@@ -3625,7 +3626,7 @@
         HeadVars, Modes, Detism),
 
     % Make the goal info for the call.
-    set.list_to_set(HeadVars0, NonLocals),
+    set_of_var.list_to_set(HeadVars0, NonLocals),
     instmap_delta_from_mode_list(HeadVars0, Modes0, !.ModuleInfo,
         InstmapDelta),
     pred_info_get_purity(PredInfo0, Purity),
Index: compiler/post_typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/post_typecheck.m,v
retrieving revision 1.151
diff -u -b -r1.151 post_typecheck.m
--- compiler/post_typecheck.m	16 Jun 2011 06:42:15 -0000	1.151
+++ compiler/post_typecheck.m	15 Aug 2011 08:52:48 -0000
@@ -143,6 +143,7 @@
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -1350,7 +1351,8 @@
     DeconstructArgs = VarsBeforeField ++ [SingletonFieldVar | VarsAfterField],
     OldNonLocals = goal_info_get_nonlocals(OldGoalInfo),
     NonLocalArgs = VarsBeforeField ++ VarsAfterField,
-    set.insert_list(NonLocalArgs, OldNonLocals, DeconstructRestrictNonLocals),
+    set_of_var.insert_list(NonLocalArgs, OldNonLocals,
+        DeconstructRestrictNonLocals),
 
     create_pure_atomic_unification_with_nonlocals(TermInputVar,
         rhs_functor(ConsId0, no, DeconstructArgs), OldGoalInfo,
@@ -1359,7 +1361,8 @@
 
     % Build a goal to construct the output.
     ConstructArgs = VarsBeforeField ++ [FieldVar | VarsAfterField],
-    set.insert_list(NonLocalArgs, OldNonLocals, ConstructRestrictNonLocals),
+    set_of_var.insert_list(NonLocalArgs, OldNonLocals,
+        ConstructRestrictNonLocals),
 
     % If the cons_id is existentially quantified, add a `new' prefix
     % so that polymorphism.m adds the appropriate type_infos.
@@ -1554,7 +1557,7 @@
 %-----------------------------------------------------------------------------%
 
 :- pred create_pure_atomic_unification_with_nonlocals(prog_var::in,
-    unify_rhs::in, hlds_goal_info::in, set(prog_var)::in, list(prog_var)::in,
+    unify_rhs::in, hlds_goal_info::in, set_of_progvar::in, list(prog_var)::in,
     unify_context::in, hlds_goal::out) is det.
 
 create_pure_atomic_unification_with_nonlocals(Var, RHS, OldGoalInfo,
@@ -1567,8 +1570,8 @@
     Goal0 = hlds_goal(GoalExpr0, GoalInfo0),
 
     % Compute the nonlocals of the goal.
-    set.list_to_set(VarsList, NonLocals1),
-    set.intersect(RestrictNonLocals, NonLocals1, NonLocals),
+    set_of_var.list_to_set(VarsList, NonLocals1),
+    set_of_var.intersect(RestrictNonLocals, NonLocals1, NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
 
     % Use the goal id from the original goal, so that the constraint_ids
Index: compiler/pragma_c_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pragma_c_gen.m,v
retrieving revision 1.123
diff -u -b -r1.123 pragma_c_gen.m
--- compiler/pragma_c_gen.m	21 Jul 2011 06:58:26 -0000	1.123
+++ compiler/pragma_c_gen.m	15 Aug 2011 15:33:23 -0000
@@ -481,7 +481,7 @@
         % other registers, so we need to save any live variables
         % (other than the output args) onto the stack.
         get_c_arg_list_vars(OutCArgs, OutVars),
-        set.list_to_set(OutVars, OutVarsSet),
+        set_of_var.list_to_set(OutVars, OutVarsSet),
         save_variables(OutVarsSet, _, SaveVarsCode, !CI)
     ),
 
Index: compiler/prog_data.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_data.m,v
retrieving revision 1.232
diff -u -b -r1.232 prog_data.m
--- compiler/prog_data.m	13 Jul 2011 00:41:21 -0000	1.232
+++ compiler/prog_data.m	15 Aug 2011 11:57:42 -0000
@@ -27,6 +27,7 @@
 :- import_module libs.rat.
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_item.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module char.
@@ -686,7 +687,8 @@
 :- type dead_datastruct == datastruct.
 :- type dead_datastructs == set(dead_datastruct).
 :- type live_var == prog_var.
-:- type live_vars == list(live_var).
+:- type live_vars == list(prog_var).
+:- type set_of_live_var == set_of_progvar.
 :- type live_datastruct == datastruct.
 :- type live_datastructs == list(live_datastruct).
 
@@ -1400,11 +1402,14 @@
 :- pred rename_vars_in_term_list(must_rename::in, map(var(V), var(V))::in,
     list(term(V))::in, list(term(V))::out) is det.
 
-:- pred rename_vars_in_var_set(must_rename::in, prog_var_renaming::in,
-    set(prog_var)::in, set(prog_var)::out) is det.
+:- pred rename_vars_in_var_set(must_rename::in, map(var(V), var(V))::in,
+    set(var(V))::in, set(var(V))::out) is det.
 
-:- pred rename_var_list(must_rename::in, map(var(T), var(T))::in,
-    list(var(T))::in, list(var(T))::out) is det.
+:- pred rename_vars_in_set_of_var(must_rename::in, map(var(V), var(V))::in,
+    set_of_var(V)::in, set_of_var(V)::out) is det.
+
+:- pred rename_var_list(must_rename::in, map(var(V), var(V))::in,
+    list(var(V))::in, list(var(V))::out) is det.
 
 :- pred rename_var(must_rename::in, map(var(V), var(V))::in,
     var(V)::in, var(V)::out) is det.
@@ -1432,6 +1437,11 @@
     rename_var_list(Must, Renaming, VarsList0, VarsList),
     set.list_to_set(VarsList, Vars).
 
+rename_vars_in_set_of_var(Must, Renaming, Vars0, Vars) :-
+    set_of_var.to_sorted_list(Vars0, VarsList0),
+    rename_var_list(Must, Renaming, VarsList0, VarsList),
+    set_of_var.list_to_set(VarsList, Vars).
+
 rename_var_list(_Must, _Renaming, [], []).
 rename_var_list(Must, Renaming, [Var0 | Vars0], [Var | Vars]) :-
     rename_var(Must, Renaming, Var0, Var),
Index: compiler/prog_rep.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_rep.m,v
retrieving revision 1.75
diff -u -b -r1.75 prog_rep.m
--- compiler/prog_rep.m	23 May 2011 05:08:10 -0000	1.75
+++ compiler/prog_rep.m	15 Aug 2011 13:13:52 -0000
@@ -94,6 +94,7 @@
 :- import_module mdbcomp.goal_path.
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module int.
 :- import_module maybe.
@@ -639,7 +640,7 @@
     instmap.apply_instmap_delta(Instmap0, InstmapDelta, Instmap),
     instmap_changed_vars(Instmap0, Instmap, Info ^ pri_vartypes,
         Info ^ pri_module_info, ChangedVars),
-    set.to_sorted_list(ChangedVars, BoundVars).
+    set_of_var.to_sorted_list(ChangedVars, BoundVars).
 
 :- pred cons_id_and_arity_rep_to_byte_list(cons_id_arity_rep::in, 
     list(int)::out, string_table::in, string_table::out) is det.
Index: compiler/prop_mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prop_mode_constraints.m,v
retrieving revision 1.34
diff -u -b -r1.34 prop_mode_constraints.m
--- compiler/prop_mode_constraints.m	23 May 2011 05:08:10 -0000	1.34
+++ compiler/prop_mode_constraints.m	15 Aug 2011 09:10:34 -0000
@@ -40,14 +40,14 @@
 
 %-----------------------------------------------------------------------------%
 
-    % prop_mode_constraints_in_scc(SCC, !ModuleInfo, !Varset, !VarMap,
+    % prop_mode_constraints_in_scc(SCC, !ModuleInfo, !VarSet, !VarMap,
     %   !PredConstraintsMap):
     %
     % For each predicate in SCC:
     %
     % - Add producer/consumer constraints to PredConstraintsMap.
     % - Add goal path annotations to its clauses in ModuleInfo.
-    % - Add any constraint variables it requires to Varset and VarMap.
+    % - Add any constraint variables it requires to VarSet and VarMap.
     %
 :- pred prop_mode_constraints_in_scc(list(pred_id)::in,
     module_info::in, module_info::out, mc_var_info::in, mc_var_info::out,
@@ -88,10 +88,10 @@
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module require.
-:- import_module set.
 :- import_module string.
 :- import_module term.
 :- import_module varset.
@@ -113,14 +113,14 @@
     list.foldl3(prop_mode_constraints_in_pred, SCC,
         !ModuleInfo, !VarInfo, !Constraints).
 
-    % prop_mode_constraints_in_pred(PredId, !ModuleInfo, !Varset, !VarMap,
+    % prop_mode_constraints_in_pred(PredId, !ModuleInfo, !VarSet, !VarMap,
     %   !Constraints):
     %
     % Performs a number of tasks for predicate PredId:
     %   1) Fill out the goal_id information in the ModuleInfo structure.
     %   2) Add producer/consumer constraint variables for program
     %      variables corresponding to any location at which they are
-    %      nonlocal to Varset and VarMap. (Elsewhere is is clear as to
+    %      nonlocal to VarSet and VarMap. (Elsewhere is is clear as to
     %      whether they are produced or consumed.)
     %   3) Add mode declaration constraints to Constraints.
     %   4) Add goal constraints to Constraints.
@@ -128,7 +128,7 @@
     % NOTE: This relies on the head variables for any predicate without mode
     % declarations that is called by this one (PredId) to have the constraint
     % variables corresponding to the empty goal path (i.e. the whole body
-    % of the predicate) to already be VarMap (and therefore also in Varset).
+    % of the predicate) to already be VarMap (and therefore also in VarSet).
     %
 :- pred prop_mode_constraints_in_pred(pred_id::in,
     module_info::in, module_info::out, mc_var_info::in, mc_var_info::out,
@@ -231,42 +231,42 @@
     module_info_pred_info(!.ModuleInfo, PredId, PredInfo0),
     pred_info_get_clauses_info(PredInfo0, ClausesInfo0),
     clauses_info_get_clauses_rep(ClausesInfo0, ClausesRep0, ItemNumbers),
-    clauses_info_get_varset(ClausesInfo0, Varset0),
+    clauses_info_get_varset(ClausesInfo0, VarSet0),
     clauses_info_get_vartypes(ClausesInfo0, VarTypes0),
     clauses_info_get_headvars(ClausesInfo0, HeadVars),
 
-    SeenSoFar = proc_arg_vector_to_set(HeadVars),
+    SeenSoFar = set_of_var.list_to_set(proc_arg_vector_to_list(HeadVars)),
     get_clause_list(ClausesRep0, Clauses0),
     BodyGoals0 = list.map(func(X) = clause_body(X), Clauses0),
     list.map_foldl3(ensure_unique_arguments_in_goal, BodyGoals0, BodyGoals,
-        SeenSoFar, _, Varset0, Varset, VarTypes0, VarTypes),
+        SeenSoFar, _, VarSet0, VarSet, VarTypes0, VarTypes),
 
     Clauses = list.map_corresponding(func(C, B) = C ^ clause_body := B,
         Clauses0, BodyGoals),
     set_clause_list(Clauses, ClausesRep),
     some [!ClausesInfo] (
         !:ClausesInfo = ClausesInfo0,
-        clauses_info_set_varset(Varset, !ClausesInfo),
+        clauses_info_set_varset(VarSet, !ClausesInfo),
         clauses_info_set_vartypes(VarTypes, !ClausesInfo),
         clauses_info_set_clauses_rep(ClausesRep, ItemNumbers, !ClausesInfo),
         pred_info_set_clauses_info(!.ClausesInfo, PredInfo0, PredInfo)
     ),
     module_info_set_pred_info(PredId, PredInfo, !ModuleInfo).
 
-    % ensure_unique_arguments_in_goal(!Goal, !SeenSoFar, !Varset, !VarTypes):
+    % ensure_unique_arguments_in_goal(!Goal, !SeenSoFar, !VarSet, !VarTypes):
     %
     % Create variables and introduce unifications in Goal, where appropriate,
     % to ensure that no program variable in SeenSoFar is used as an argument in
     % a predicate call and that no program variable is used as an argument for
     % more than one predicate call.
-    % Created variables are added to Varset, VarTypes and SeenSoFar. Variables
+    % Created variables are added to VarSet, VarTypes and SeenSoFar. Variables
     % used as arguments in predicate calls are added to SeenSoFar.
     %
 :- pred ensure_unique_arguments_in_goal(hlds_goal::in, hlds_goal::out,
-    set(prog_var)::in, set(prog_var)::out, prog_varset::in, prog_varset::out,
+    set_of_progvar::in, set_of_progvar::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out) is det.
 
-ensure_unique_arguments_in_goal(!Goal, !SeenSoFar, !Varset, !VarTypes) :-
+ensure_unique_arguments_in_goal(!Goal, !SeenSoFar, !VarSet, !VarTypes) :-
     some [!GoalExpr, !GoalInfo] (
         !.Goal = hlds_goal(!:GoalExpr, !:GoalInfo),
         (
@@ -274,13 +274,13 @@
             (
                 ConjType = plain_conj,
                 list.map_foldl3(ensure_unique_arguments_in_goal,
-                    Goals0, Goals1, !SeenSoFar, !Varset, !VarTypes),
+                    Goals0, Goals1, !SeenSoFar, !VarSet, !VarTypes),
                 flatten_conjunction(Goals1, Goals),
                 !:GoalExpr = conj(plain_conj, Goals)
             ;
                 ConjType = parallel_conj,
                 list.map_foldl3(ensure_unique_arguments_in_goal,
-                    Goals0, Goals, !SeenSoFar, !Varset, !VarTypes),
+                    Goals0, Goals, !SeenSoFar, !VarSet, !VarTypes),
                 !:GoalExpr = conj(parallel_conj, Goals)
             )
         ;
@@ -288,7 +288,7 @@
                 Builtin, UnifyContext, SymName),
             Context = goal_info_get_context(!.GoalInfo),
             make_unifications(Context, Unifications, Args0, Args, !SeenSoFar,
-                !Varset, !VarTypes),
+                !VarSet, !VarTypes),
             (
                 % No arguments changed.
                 Unifications = []
@@ -307,7 +307,7 @@
             !.GoalExpr = generic_call(Details, Args0, Modes, Determinism),
             Context = goal_info_get_context(!.GoalInfo),
             make_unifications(Context, Unifications, Args0, Args, !SeenSoFar,
-                !Varset, !VarTypes),
+                !VarSet, !VarTypes),
             (
                 % No arguments changed.
                 Unifications = []
@@ -328,27 +328,27 @@
         ;
             !.GoalExpr = disj(Goals0),
             list.map_foldl3(ensure_unique_arguments_in_goal, Goals0, Goals,
-                !SeenSoFar, !Varset, !VarTypes),
+                !SeenSoFar, !VarSet, !VarTypes),
             !:GoalExpr = disj(Goals)
         ;
             !.GoalExpr = negation(Goal0),
-            ensure_unique_arguments_in_goal(Goal0, Goal, !SeenSoFar, !Varset,
+            ensure_unique_arguments_in_goal(Goal0, Goal, !SeenSoFar, !VarSet,
                 !VarTypes),
             !:GoalExpr = negation(Goal)
         ;
             !.GoalExpr = scope(Reason, Goal0),
             % XXX We should special-case the handling of
             % from_ground_term_construct scopes.
-            ensure_unique_arguments_in_goal(Goal0, Goal, !SeenSoFar, !Varset,
+            ensure_unique_arguments_in_goal(Goal0, Goal, !SeenSoFar, !VarSet,
                 !VarTypes),
             !:GoalExpr = scope(Reason, Goal)
         ;
             !.GoalExpr = if_then_else(ExistVars, Cond0, Then0, Else0),
-            ensure_unique_arguments_in_goal(Cond0, Cond, !SeenSoFar, !Varset,
+            ensure_unique_arguments_in_goal(Cond0, Cond, !SeenSoFar, !VarSet,
                 !VarTypes),
-            ensure_unique_arguments_in_goal(Then0, Then, !SeenSoFar, !Varset,
+            ensure_unique_arguments_in_goal(Then0, Then, !SeenSoFar, !VarSet,
                 !VarTypes),
-            ensure_unique_arguments_in_goal(Else0, Else, !SeenSoFar, !Varset,
+            ensure_unique_arguments_in_goal(Else0, Else, !SeenSoFar, !VarSet,
                 !VarTypes),
             !:GoalExpr = if_then_else(ExistVars, Cond, Then, Else)
         ;
@@ -359,16 +359,16 @@
                 ShortHand0 = atomic_goal(GoalType, Outer, Inner,
                     MaybeOutputVars, MainGoal0, OrElseGoals0, OrElseInners),
                 ensure_unique_arguments_in_goal(MainGoal0, MainGoal,
-                    !SeenSoFar, !Varset, !VarTypes),
+                    !SeenSoFar, !VarSet, !VarTypes),
                 list.map_foldl3(ensure_unique_arguments_in_goal,
-                    OrElseGoals0, OrElseGoals, !SeenSoFar, !Varset, !VarTypes),
+                    OrElseGoals0, OrElseGoals, !SeenSoFar, !VarSet, !VarTypes),
                 ShortHand = atomic_goal(GoalType, Outer, Inner,
                     MaybeOutputVars, MainGoal, OrElseGoals, OrElseInners),
                 !:GoalExpr = shorthand(ShortHand)
             ;
                 ShortHand0 = try_goal(MaybeIO, ResultVar, SubGoal0),
                 ensure_unique_arguments_in_goal(SubGoal0, SubGoal, !SeenSoFar,
-                    !Varset, !VarTypes),
+                    !VarSet, !VarTypes),
                 ShortHand = try_goal(MaybeIO, ResultVar, SubGoal),
                 !:GoalExpr = shorthand(ShortHand)
             ;
@@ -402,41 +402,41 @@
     ).
 
     % make_unifications(Context, MaybeUnifications, Args0, Args, !SeenSoFar,
-    %   !Varset, !VarTypes):
+    %   !VarSet, !VarTypes):
     %
     % If any of the given arguments in Args0 is in SeenSoFar, creates a new
-    % argument (in Varset and VarTypes) to replace it (in Args), and generates
+    % argument (in VarSet and VarTypes) to replace it (in Args), and generates
     % a unification between new argument and old (with context Context).
     %
 :- pred make_unifications(prog_context::in, list(hlds_goal)::out,
     list(prog_var)::in, list(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out,
+    set_of_progvar::in, set_of_progvar::out,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out) is det.
 
 make_unifications(Context, Unifications, !Args, !SeenSoFar,
-        !Varset, !VarTypes) :-
+        !VarSet, !VarTypes) :-
     list.map_foldl4(make_unification(Context), !Args,
-        [], Unifications, !SeenSoFar, !Varset, !VarTypes).
+        [], Unifications, !SeenSoFar, !VarSet, !VarTypes).
 
     % make_unification(Context, Var0, Var, !Unifications, !SeenSoFar,
-    %   !Varset, !VarTypes):
+    %   !VarSet, !VarTypes):
     %
-    % If Var0 is in SeenSoFar, creates a new argument Var (in Varset and
+    % If Var0 is in SeenSoFar, creates a new argument Var (in VarSet and
     % VarTypes), and generates a unification between Var0 and Var.
     %
 :- pred make_unification(prog_context::in, prog_var::in, prog_var::out,
     list(hlds_goal)::in, list(hlds_goal)::out,
-    set(prog_var)::in, set(prog_var)::out, prog_varset::in, prog_varset::out,
+    set_of_progvar::in, set_of_progvar::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out) is det.
 
-make_unification(Context, Var0, Var, !Unifications, !SeenSoFar, !Varset,
+make_unification(Context, Var0, Var, !Unifications, !SeenSoFar, !VarSet,
         !VarTypes) :-
-    ( set.contains(!.SeenSoFar, Var0) ->
+    ( set_of_var.contains(!.SeenSoFar, Var0) ->
         % Make new variable.
-        OldVarName = varset.lookup_name(!.Varset, Var0),
+        OldVarName = varset.lookup_name(!.VarSet, Var0),
         OldVarType = map.lookup(!.VarTypes, Var0),
         NewVarName = "Arg_" ++ OldVarName,
-        varset.new_uniquely_named_var(NewVarName, Var, !Varset),
+        varset.new_uniquely_named_var(NewVarName, Var, !VarSet),
         map.set(Var, OldVarType, !VarTypes),
 
         % Make new unification.
@@ -445,7 +445,7 @@
             UnificationGoal0),
         UnificationGoal0 =
             hlds_goal(UnificationGoalExpr, UnificationGoalInfo0),
-        goal_info_set_nonlocals(set.from_list([Var0, Var]),
+        goal_info_set_nonlocals(set_of_var.list_to_set([Var0, Var]),
             UnificationGoalInfo0, UnificationGoalInfo),
         UnificationGoal =
             hlds_goal(UnificationGoalExpr, UnificationGoalInfo),
@@ -453,7 +453,7 @@
     ;
         Var = Var0
     ),
-    set.insert(Var, !SeenSoFar).
+    set_of_var.insert(Var, !SeenSoFar).
 
     % replace_call_with_conjunction(NewCallGoalExpr, Unifications, NewArgs,
     %   GoalExpr, !GoalInfo):
@@ -475,7 +475,7 @@
     CallGoalInfo0 = !.GoalInfo,
     Context = goal_info_get_context(CallGoalInfo0),
     CallNonlocals0 = goal_info_get_nonlocals(CallGoalInfo0),
-    CallNonlocals = set.insert_list(CallNonlocals0, NewArgs),
+    set_of_var.insert_list(NewArgs, CallNonlocals0, CallNonlocals),
     goal_info_set_nonlocals(CallNonlocals, CallGoalInfo0, CallGoalInfo),
     Goals = [hlds_goal(CallGoalExpr, CallGoalInfo) | Unifications],
 
@@ -502,11 +502,11 @@
     % based on the default filename for the module followed by the
     % predicate's name.
     %
-pretty_print_pred_constraints_map(ModuleInfo, ConstraintVarset,
+pretty_print_pred_constraints_map(ModuleInfo, ConstraintVarSet,
         PredConstraintsMap, !IO) :-
     ConstrainedPreds = map.keys(PredConstraintsMap),
     list.foldl(
-        pretty_print_pred_constraints(ModuleInfo, ConstraintVarset,
+        pretty_print_pred_constraints(ModuleInfo, ConstraintVarSet,
             PredConstraintsMap),
         ConstrainedPreds, !IO).
 
@@ -517,7 +517,7 @@
 :- pred pretty_print_pred_constraints(module_info::in, mc_varset::in,
     pred_constraints_map::in, pred_id::in, io::di, io::uo) is det.
 
-pretty_print_pred_constraints(ModuleInfo, ConstraintVarset,
+pretty_print_pred_constraints(ModuleInfo, ConstraintVarSet,
         PredConstraintsMap, PredId, !IO) :-
     module_info_get_globals(ModuleInfo, Globals),
 
@@ -531,10 +531,10 @@
 
     map.lookup(PredConstraintsMap, PredId, {_, PredConstraints}),
     AllProcAnnConstraints = allproc_annotated_constraints(PredConstraints),
-    dump_constraints_and_annotations(Globals, ConstraintVarset,
+    dump_constraints_and_annotations(Globals, ConstraintVarSet,
         AllProcAnnConstraints, !IO),
     list.foldl(
-        pretty_print_proc_constraints(ModuleInfo, ConstraintVarset,
+        pretty_print_proc_constraints(ModuleInfo, ConstraintVarSet,
             PredConstraints, PredId),
         pred_info_all_procids(PredInfo), !IO).
 
@@ -545,7 +545,7 @@
 :- pred pretty_print_proc_constraints(module_info::in, mc_varset::in,
     pred_p_c_constraints::in, pred_id::in, proc_id::in, io::di, io::uo) is det.
 
-pretty_print_proc_constraints(ModuleInfo, ConstraintVarset, PredConstraints,
+pretty_print_proc_constraints(ModuleInfo, ConstraintVarSet, PredConstraints,
         PredId, ProcId, !IO) :-
     module_info_get_globals(ModuleInfo, Globals),
 
@@ -556,7 +556,7 @@
         should_module_qualify, proc(PredId, ProcId)) ++ [suffix(":")], !IO),
     ProcSpecAnnConstraints =
         proc_specific_annotated_constraints(ProcId, PredConstraints),
-    dump_constraints_and_annotations(Globals, ConstraintVarset,
+    dump_constraints_and_annotations(Globals, ConstraintVarSet,
         ProcSpecAnnConstraints, !IO).
 
 %----------------------------------------------------------------------------%
Index: compiler/purity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/purity.m,v
retrieving revision 1.146
diff -u -b -r1.146 purity.m
--- compiler/purity.m	15 Jun 2011 16:36:23 -0000	1.146
+++ compiler/purity.m	15 Aug 2011 08:31:21 -0000
@@ -184,6 +184,7 @@
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -795,7 +796,7 @@
     % OuterDI and OuterUO will definitely be used by the code inside the new
     % goal, and *should* be used by code outside the goal. However, even if
     % they are not, the nonlocals set is allowed to overapproximate.
-    set.insert_list([OuterDI, OuterUO], NonLocals0, NonLocals),
+    set_of_var.insert_list([OuterDI, OuterUO], NonLocals0, NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
     goal_info_add_feature(feature_contains_stm_inner_outer, GoalInfo1,
         GoalInfo),
Index: compiler/quantification.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/quantification.m,v
retrieving revision 1.145
diff -u -b -r1.145 quantification.m
--- compiler/quantification.m	21 Jul 2011 06:58:26 -0000	1.145
+++ compiler/quantification.m	15 Aug 2011 08:14:39 -0000
@@ -40,7 +40,6 @@
 :- import_module parse_tree.set_of_var.
 
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -97,7 +96,7 @@
     % excluding unset fields of reconstructions if
     % NonLocalsToRecompute is `code_gen_nonlocals_no_lambda'.
     %
-:- func free_goal_vars(hlds_goal) = set(prog_var).
+:- func free_goal_vars(hlds_goal) = set_of_progvar.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -178,37 +177,37 @@
 %-----------------------------------------------------------------------------%
 
 implicitly_quantify_clause_body_general(NonLocalsToRecompute, HeadVars,
-        Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
+        Warnings, !Goal, !VarSet, !VarTypes, !RttiVarMaps) :-
     OutsideVars = set_of_var.list_to_set(HeadVars),
     implicitly_quantify_goal_general(NonLocalsToRecompute, OutsideVars,
-        Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps).
+        Warnings, !Goal, !VarSet, !VarTypes, !RttiVarMaps).
 
 requantify_proc_general(NonLocalsToRecompute, !ProcInfo) :-
     proc_info_get_headvars(!.ProcInfo, HeadVars),
-    proc_info_get_varset(!.ProcInfo, Varset0),
+    proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     proc_info_get_goal(!.ProcInfo, Goal0),
     proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarmaps0),
     implicitly_quantify_clause_body_general(NonLocalsToRecompute, HeadVars, _,
-        Goal0, Goal, Varset0, Varset, VarTypes0, VarTypes,
+        Goal0, Goal, VarSet0, VarSet, VarTypes0, VarTypes,
         RttiVarmaps0, RttiVarmaps),
-    proc_info_set_varset(Varset, !ProcInfo),
+    proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo),
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_rtti_varmaps(RttiVarmaps, !ProcInfo).
 
 implicitly_quantify_goal_general(NonLocalsToRecompute, OutsideVars, Warnings,
-        !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
+        !Goal, !VarSet, !VarTypes, !RttiVarMaps) :-
     (
         NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
         implicitly_quantify_goal_2(ordinary_nonlocals_maybe_lambda,
-            OutsideVars, Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps)
+            OutsideVars, Warnings, !Goal, !VarSet, !VarTypes, !RttiVarMaps)
     ;
         ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
         ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
         ),
         implicitly_quantify_goal_2(ordinary_nonlocals_no_lambda,
-            OutsideVars, Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps)
+            OutsideVars, Warnings, !Goal, !VarSet, !VarTypes, !RttiVarMaps)
     ),
     (
         NonLocalsToRecompute = code_gen_nonlocals_no_lambda,
@@ -218,7 +217,7 @@
         goal_contains_reconstruction(!.Goal)
     ->
         implicitly_quantify_goal_2(code_gen_nonlocals_no_lambda, OutsideVars,
-            _, !Goal, !Varset, !VarTypes, !RttiVarMaps)
+            _, !Goal, !VarSet, !VarTypes, !RttiVarMaps)
     ;
         true
     ).
@@ -235,12 +234,12 @@
     in, out, in, out, in, out, in, out, in, out) is det.
 
 implicitly_quantify_goal_2(NonLocalsToRecompute, OutsideVars, Warnings,
-        !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
-    init_quant_info(OutsideVars, !.Varset, !.VarTypes, !.RttiVarMaps,
+        !Goal, !VarSet, !VarTypes, !RttiVarMaps) :-
+    init_quant_info(OutsideVars, !.VarSet, !.VarTypes, !.RttiVarMaps,
         QuantInfo0),
     implicitly_quantify_goal_quant_info(!Goal, NonLocalsToRecompute,
         QuantInfo0, QuantInfo),
-    get_varset(QuantInfo, !:Varset),
+    get_varset(QuantInfo, !:VarSet),
     get_vartypes(QuantInfo, !:VarTypes),
     get_warnings(QuantInfo, Warnings0),
     get_rtti_varmaps(QuantInfo, !:RttiVarMaps),
@@ -989,7 +988,7 @@
 
         Goal = hlds_goal(_, LambdaGoalInfo),
         LambdaGoalNonLocals = goal_info_get_nonlocals(LambdaGoalInfo),
-        list.filter(contains(LambdaGoalNonLocals),
+        list.filter(set_of_var.contains(LambdaGoalNonLocals),
             LambdaNonLocals0, LambdaNonLocals),
 
         % For a unification that constructs a lambda expression, the argument
@@ -1552,23 +1551,21 @@
     % is `code_gen_nonlocals_no_lambda'.
     %
 :- func free_goal_vars_nl_maybe_lambda(nonlocals_to_recompute, hlds_goal)
-    = set(prog_var).
+    = set_of_progvar.
 :- mode free_goal_vars_nl_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
     in) = out is det.
 
-free_goal_vars_nl_maybe_lambda(NonLocalsToRecompute, Goal) =
-        bitset_to_set(BothSet) :-
+free_goal_vars_nl_maybe_lambda(NonLocalsToRecompute, Goal) = BothSet :-
     goal_vars_bitset_maybe_lambda(NonLocalsToRecompute, Goal, BothSet).
 
 :- func free_goal_vars_nl_no_lambda(nonlocals_to_recompute, hlds_goal)
-    = set(prog_var).
+    = set_of_progvar.
 :- mode free_goal_vars_nl_no_lambda(in(ordinary_nonlocals_no_lambda),
     in) = out is det.
 :- mode free_goal_vars_nl_no_lambda(in(code_gen_nonlocals_no_lambda),
     in) = out is det.
 
-free_goal_vars_nl_no_lambda(NonLocalsToRecompute, Goal) =
-        bitset_to_set(BothSet) :-
+free_goal_vars_nl_no_lambda(NonLocalsToRecompute, Goal) = BothSet :-
     goal_vars_bitset_no_lambda(NonLocalsToRecompute, Goal, BothSet).
 
 :- pred goal_vars_bitset(nonlocals_to_recompute,
@@ -2367,13 +2364,13 @@
         map.init(RenameMap)
     ;
         RenameList = to_sorted_list(RenameSet),
-        get_varset(!.Info, Varset0),
+        get_varset(!.Info, VarSet0),
         get_vartypes(!.Info, VarTypes0),
         map.init(RenameMap0),
-        clone_variables(RenameList, Varset0, VarTypes0,
-            Varset0, Varset, VarTypes0, VarTypes, RenameMap0, RenameMap),
+        clone_variables(RenameList, VarSet0, VarTypes0,
+            VarSet0, VarSet, VarTypes0, VarTypes, RenameMap0, RenameMap),
         rename_some_vars_in_goal(RenameMap, !Goal),
-        set_varset(Varset, !Info),
+        set_varset(VarSet, !Info),
         set_vartypes(VarTypes, !Info)
 
         % We don't need to add the newly created vars to the seen vars
@@ -2401,7 +2398,7 @@
     set_goal_nonlocals_translate(NonLocals, _, NonLocalsToRecompute,
         !GoalInfo, !Info).
 
-:- pred set_goal_nonlocals_translate(set_of_progvar, set(prog_var),
+:- pred set_goal_nonlocals_translate(set_of_progvar, set_of_progvar,
     nonlocals_to_recompute, hlds_goal_info, hlds_goal_info,
     quant_info, quant_info).
 :- mode set_goal_nonlocals_translate(in, out,
@@ -2411,9 +2408,8 @@
 :- mode set_goal_nonlocals_translate(in, out,
     in(code_gen_nonlocals_no_lambda), in, out, in, out) is det.
 
-set_goal_nonlocals_translate(NonLocalsBitSet, NonLocals, NonLocalsToRecompute,
+set_goal_nonlocals_translate(NonLocals, NonLocals, NonLocalsToRecompute,
         !GoalInfo, !Info) :-
-    NonLocals = bitset_to_set(NonLocalsBitSet),
     (
         ( NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda
         ; NonLocalsToRecompute = ordinary_nonlocals_no_lambda
@@ -2429,10 +2425,10 @@
 :- pred init_quant_info(set_of_progvar::in,
     prog_varset::in, vartypes::in, rtti_varmaps::in, quant_info::out) is det.
 
-init_quant_info(OutsideVars, Varset, VarTypes, RttiVarMaps, QuantInfo) :-
+init_quant_info(OutsideVars, VarSet, VarTypes, RttiVarMaps, QuantInfo) :-
     OverlapWarnings = [],
     QuantInfo = quant_info(OutsideVars, QuantVars, LambdaOutsideVars,
-        NonLocals, Seen, Varset, VarTypes, OverlapWarnings, RttiVarMaps),
+        NonLocals, Seen, VarSet, VarTypes, OverlapWarnings, RttiVarMaps),
     QuantVars = set_of_var.init,
     NonLocals = set_of_var.init,
     LambdaOutsideVars = set_of_var.init,
@@ -2487,8 +2483,8 @@
     !Q ^ qi_nonlocals := NonLocals.
 set_seen(Seen, !Q) :-
     !Q ^ qi_seen := Seen.
-set_varset(Varset, !Q) :-
-    !Q ^ qi_varset := Varset.
+set_varset(VarSet, !Q) :-
+    !Q ^ qi_varset := VarSet.
 set_vartypes(VarTypes, !Q) :-
     !Q ^ qi_vartypes := VarTypes.
 set_warnings(Warnings, !Q) :-
Index: compiler/rbmm.add_rbmm_goal_infos.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.add_rbmm_goal_infos.m,v
retrieving revision 1.13
diff -u -b -r1.13 rbmm.add_rbmm_goal_infos.m
--- compiler/rbmm.add_rbmm_goal_infos.m	23 May 2011 05:08:10 -0000	1.13
+++ compiler/rbmm.add_rbmm_goal_infos.m	15 Aug 2011 11:48:22 -0000
@@ -68,6 +68,7 @@
 :- import_module parse_tree.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.rbmm.points_to_graph.
 :- import_module transform_hlds.smm_common.
 
@@ -278,7 +279,8 @@
     RbmmInfo0 = rbmm_goal_info(Created, Removed, _, AllocatedInto, Used),
     NonLocals = goal_info_get_nonlocals(!.Info),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    NonLocalRegions = set.filter(is_region_var(VarTypes), NonLocals),
+    NonLocalRegionsSet = set_of_var.filter(is_region_var(VarTypes), NonLocals),
+    NonLocalRegions = set_of_var.bitset_to_set(NonLocalRegionsSet),
     set.difference(NonLocalRegions, set.union(Created, Removed), Carried),
     RbmmInfo = rbmm_goal_info(Created, Removed, Carried, AllocatedInto, Used),
     goal_info_set_maybe_rbmm(yes(RbmmInfo), !Info).
Index: compiler/rbmm.region_transformation.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.region_transformation.m,v
retrieving revision 1.19
diff -u -b -r1.19 rbmm.region_transformation.m
--- compiler/rbmm.region_transformation.m	23 May 2011 05:08:11 -0000	1.19
+++ compiler/rbmm.region_transformation.m	15 Aug 2011 12:22:55 -0000
@@ -84,6 +84,7 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_mode.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.rbmm.points_to_graph.
 :- import_module transform_hlds.smm_common.
 
@@ -988,7 +989,7 @@
     % I think it is always recomputed but it seems that I am wrong.
     % It should be able to be recomputed from the modes in the assigment.
     % Maybe I am missing or doing something wrong here.
-    NonLocals = set.init,
+    NonLocals = set_of_var.init,
     InstmapDelta = instmap_delta_bind_var(LeftRegVar),
     goal_info_init(NonLocals, InstmapDelta, detism_det, purity_pure,
         AssignmentInfo),
Index: compiler/saved_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/saved_vars.m,v
retrieving revision 1.91
diff -u -b -r1.91 saved_vars.m
--- compiler/saved_vars.m	23 May 2011 05:08:11 -0000	1.91
+++ compiler/saved_vars.m	15 Aug 2011 11:04:01 -0000
@@ -50,6 +50,7 @@
 :- import_module hlds.passes_aux.
 :- import_module hlds.quantification.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module io.
@@ -191,7 +192,7 @@
     % following goal that uses all the variables they define.
     %
 :- pred saved_vars_in_conj(list(hlds_goal)::in, list(hlds_goal)::out,
-    set(prog_var)::in, slot_info::in, slot_info::out) is det.
+    set_of_progvar::in, slot_info::in, slot_info::out) is det.
 
 saved_vars_in_conj([], [], _, !SlotInfo).
 saved_vars_in_conj([Goal0 | Goals0], Goals, NonLocals, !SlotInfo) :-
@@ -211,7 +212,7 @@
         OtherGoals = [First | _Rest],
         can_push(Var, First) = yes
     ->
-        set.is_member(Var, NonLocals, IsNonLocal),
+        set_of_var.is_member(NonLocals, Var, IsNonLocal),
         saved_vars_delay_goal(OtherGoals, Goals1, Goal0, Var, IsNonLocal,
             !SlotInfo),
         list.append(Constants, Goals1, Goals2),
@@ -282,7 +283,7 @@
 can_push(Var, Goal) = CanPush :-
     Goal = hlds_goal(GoalExpr, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    ( set.member(Var, NonLocals) ->
+    ( set_of_var.member(NonLocals, Var) ->
         (
             ( GoalExpr = if_then_else(_, _, _, _)
             ; GoalExpr = negation(_)
@@ -370,7 +371,7 @@
         !SlotInfo) :-
     Goal0 = hlds_goal(Goal0Expr, Goal0Info),
     Goal0NonLocals = goal_info_get_nonlocals(Goal0Info),
-    ( set.member(Var, Goal0NonLocals) ->
+    ( set_of_var.member(Goal0NonLocals, Var) ->
         (
             Goal0Expr = unify(_, _, _, _, _),
             rename_var(Var, _NewVar, Subst, !SlotInfo),
@@ -507,7 +508,7 @@
 push_into_goal_rename(Goal0, Goal, Construct, Var, !SlotInfo) :-
     Goal0 = hlds_goal(_, GoalInfo0),
     NonLocals = goal_info_get_nonlocals(GoalInfo0),
-    ( set.member(Var, NonLocals) ->
+    ( set_of_var.member(NonLocals, Var) ->
         rename_var(Var, NewVar, Subst, !SlotInfo),
         rename_some_vars_in_goal(Subst, Construct, NewConstruct),
         rename_some_vars_in_goal(Subst, Goal0, Goal1),
Index: compiler/set_of_var.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/set_of_var.m,v
retrieving revision 1.3
diff -u -b -r1.3 set_of_var.m
--- compiler/set_of_var.m	11 Aug 2011 23:18:59 -0000	1.3
+++ compiler/set_of_var.m	15 Aug 2011 15:51:58 -0000
@@ -18,6 +18,7 @@
 
 :- import_module parse_tree.prog_data.
 
+:- import_module bool.
 :- import_module list.
 :- import_module set.
 :- import_module term.
@@ -32,6 +33,8 @@
 :- func make_singleton(var(T)) = set_of_var(T).
 :- pred make_singleton(var(T)::in, set_of_var(T)::out) is det.
 
+:- func count(set_of_var(T)) = int.
+
 %---------------
 % Tests.
 
@@ -39,7 +42,14 @@
 :- pred is_non_empty(set_of_var(T)::in) is semidet.
 :- pred is_singleton(set_of_var(T)::in, var(T)::out) is semidet.
 
-:- pred member(set_of_var(T)::in, var(T)::in) is semidet.
+:- pred member(set_of_var(T), var(T)).
+:- mode member(in, in) is semidet.
+:- mode member(in, out) is nondet.
+
+:- pred is_member(set_of_var(T)::in, var(T)::in, bool::out) is det.
+
+:- pred contains(set_of_var(T)::in, var(T)::in) is semidet.
+
 :- pred equal(set_of_var(T)::in, set_of_var(T)::in) is semidet.
 
 %---------------
@@ -108,6 +118,9 @@
 :- mode fold(pred(in, in, out) is det, in, in, out) is det.
 :- mode fold(pred(in, in, out) is semidet, in, in, out) is semidet.
 
+:- pred fold_func((func(var(T), Acc) = Acc), set_of_var(T), Acc, Acc).
+:- mode fold_func(in((func(in, in) = out) is det), in, in, out) is det.
+
     % `filter(Pred, Set) = TrueSet' returns the elements of Set for which
     % Pred succeeds.
     %
@@ -171,6 +184,9 @@
 make_singleton(Elem, Set) :-
     Set = set_of_var.make_singleton(Elem).
 
+count(Set) = Count :-
+    Count = tree_bitset.count(Set).                                 % MODULE
+
 %---------------
 % Tests.
 
@@ -186,6 +202,16 @@
 member(Set, Elem) :-
     tree_bitset.member(Elem, Set).                                  % MODULE
 
+is_member(Set, Elem, IsMember) :-
+    ( set_of_var.contains(Set, Elem) ->
+        IsMember = yes
+    ;
+        IsMember = no
+    ).
+
+contains(Set, Elem) :-
+    tree_bitset.contains(Set, Elem).                                % MODULE
+
 equal(SetA, SetB) :-
     tree_bitset.equal(SetA, SetB).                                  % MODULE
 
@@ -270,6 +296,9 @@
 fold(P, Set, !Acc) :-
     tree_bitset.foldl(P, Set, !Acc).                                % MODULE
 
+fold_func(P, Set, !Acc) :-
+    !:Acc = tree_bitset.foldl(P, Set, !.Acc).                       % MODULE
+
 filter(P, Set) = tree_bitset.filter(P, Set).                        % MODULE
 
 filter(P, Set, Trues) :-
Index: compiler/simplify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/simplify.m,v
retrieving revision 1.267
diff -u -b -r1.267 simplify.m
--- compiler/simplify.m	21 Jul 2011 06:58:27 -0000	1.267
+++ compiler/simplify.m	15 Aug 2011 05:00:19 -0000
@@ -694,8 +694,7 @@
             simplify_info_get_var_types(!.Info, !:VarTypes),
             simplify_info_get_rtti_varmaps(!.Info, !:RttiVarMaps),
             implicitly_quantify_goal_general(ordinary_nonlocals_maybe_lambda,
-                set_to_bitset(NonLocals), _, !Goal,
-                !VarSet, !VarTypes, !RttiVarMaps),
+                NonLocals, _, !Goal, !VarSet, !VarTypes, !RttiVarMaps),
 
             simplify_info_set_varset(!.VarSet, !Info),
             simplify_info_set_var_types(!.VarTypes, !Info),
@@ -821,7 +820,7 @@
 
         % If the goal had any non-locals we should requantify.
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        ( set.empty(NonLocals0) ->
+        ( set_of_var.is_empty(NonLocals0) ->
             true
         ;
             simplify_info_set_requantify(!Info)
@@ -888,7 +887,7 @@
 
         % If the goal had any non-locals we should requantify.
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        ( set.empty(NonLocals0) ->
+        ( set_of_var.is_empty(NonLocals0) ->
             true
         ;
             simplify_info_set_requantify(!Info)
@@ -1216,7 +1215,7 @@
                 % Work out the nonlocals, instmap_delta
                 % and determinism of the entire conjunction.
                 NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-                set.insert(Var, NonLocals0, NonLocals),
+                set_of_var.insert(Var, NonLocals0, NonLocals),
                 InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo0),
                 simplify_info_get_instmap(!.Info, InstMap),
                 instmap_delta_bind_var_to_functor(Var, Type, MainConsId,
@@ -2333,7 +2332,8 @@
         CmpGoal0),
     CmpGoal0 = hlds_goal(CmpExpr, CmpInfo0),
     CmpNonLocals0 = goal_info_get_nonlocals(CmpInfo0),
-    goal_info_set_nonlocals(set.insert(CmpNonLocals0, R), CmpInfo0, CmpInfo),
+    set_of_var.insert(R, CmpNonLocals0, CmpNonLocals),
+    goal_info_set_nonlocals(CmpNonLocals, CmpInfo0, CmpInfo),
     CmpGoal  = hlds_goal(CmpExpr, CmpInfo),
 
     % Construct the unification R = Inequality.
@@ -2348,7 +2348,8 @@
         "replacement of inequality with call to compare/3"), []),
     UfyExpr  = unify(R, RHS, UMode, UKind, UContext),
     UfyNonLocals0 = goal_info_get_nonlocals(GoalInfo),
-    goal_info_set_nonlocals(set.insert(UfyNonLocals0, R), GoalInfo, UfyInfo),
+    set_of_var.insert(R, UfyNonLocals0, UfyNonLocals),
+    goal_info_set_nonlocals(UfyNonLocals, GoalInfo, UfyInfo),
     UfyGoal  = hlds_goal(UfyExpr, UfyInfo),
 
     (
@@ -2614,7 +2615,7 @@
             make_const_construction(Res,
                 cons(qualified(Builtin, ">"), 0, TypeCtor), ReturnGt),
 
-            NonLocals = set.from_list([Res, X, Y]),
+            NonLocals = set_of_var.list_to_set([Res, X, Y]),
             goal_info_set_nonlocals(NonLocals, !GoalInfo),
 
             RestExpr = if_then_else([], CondLt, ReturnLt, ReturnGt),
@@ -2691,7 +2692,7 @@
     ConstMode = (free -> Ground) - (Ground -> Ground),
     ConstGoalExpr = unify(ConstVar, ConstRHS, ConstMode, ConstUnification,
         ConstUnifyContext),
-    ConstNonLocals = set.make_singleton_set(ConstVar),
+    ConstNonLocals = set_of_var.make_singleton(ConstVar),
     InstMapDelta = instmap_delta_bind_var(ConstVar),
     goal_info_init(ConstNonLocals, InstMapDelta,
         detism_det, purity_pure, ConstGoalInfo),
@@ -2714,7 +2715,7 @@
 
     OpGoalInfo0 = !.GoalInfo,
     OpNonLocals0 = goal_info_get_nonlocals(OpGoalInfo0),
-    set.insert(ConstVar, OpNonLocals0, OpNonLocals),
+    set_of_var.insert(ConstVar, OpNonLocals0, OpNonLocals),
     goal_info_set_nonlocals(OpNonLocals, OpGoalInfo0, OpGoalInfo),
     OpGoal = hlds_goal(OpGoalExpr, OpGoalInfo),
 
@@ -2961,7 +2962,7 @@
 
     % Add the extra type_info vars to the nonlocals for the call.
     NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-    set.insert_list(TypeInfoVars, NonLocals0, NonLocals),
+    set_of_var.insert_list(TypeInfoVars, NonLocals0, NonLocals),
     goal_info_set_nonlocals(NonLocals, GoalInfo0, CallGoalInfo).
 
 :- pred call_builtin_compound_eq(prog_var::in, prog_var::in, module_info::in,
@@ -3312,7 +3313,7 @@
 :- type var_renaming == map(prog_var, prog_var).
 
 :- pred find_excess_assigns_in_conj(trace_level::in, bool::in,
-    prog_varset::in, set(prog_var)::in, list(hlds_goal)::in,
+    prog_varset::in, set_of_progvar::in, list(hlds_goal)::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
     var_renaming::in, var_renaming::out) is det.
 
@@ -3331,7 +3332,7 @@
         Goals, !RevGoals, !Subn).
 
 :- pred goal_is_excess_assign(trace_level::in, bool::in, prog_varset::in,
-    set(prog_var)::in, hlds_goal::in,
+    set_of_progvar::in, hlds_goal::in,
     var_renaming::in, var_renaming::out) is semidet.
 
 goal_is_excess_assign(Trace, TraceOptimized, VarSet, ConjNonLocals, Goal0,
@@ -3343,8 +3344,8 @@
     find_renamed_var(!.Subn, LeftVar0, LeftVar),
     find_renamed_var(!.Subn, RightVar0, RightVar),
 
-    CanElimLeft = ( set.member(LeftVar, ConjNonLocals) -> no ; yes ),
-    CanElimRight = ( set.member(RightVar, ConjNonLocals) -> no ; yes ),
+    CanElimLeft = ( set_of_var.member(ConjNonLocals, LeftVar) -> no ; yes ),
+    CanElimRight = ( set_of_var.member(ConjNonLocals, RightVar) -> no ; yes ),
 
     (
         CanElimLeft = yes,
@@ -3482,8 +3483,7 @@
 :- pred create_test_unification(prog_var::in, cons_id::in, int::in,
     hlds_goal::out, simplify_info::in, simplify_info::out) is det.
 
-create_test_unification(Var, ConsId, ConsArity,
-        hlds_goal(ExtraGoal, ExtraGoalInfo), !Info) :-
+create_test_unification(Var, ConsId, ConsArity, ExtraGoal, !Info) :-
     simplify_info_get_varset(!.Info, VarSet0),
     simplify_info_get_var_types(!.Info, VarTypes0),
     varset.new_vars(ConsArity, ArgVars, VarSet0, VarSet),
@@ -3513,14 +3513,15 @@
     UnifyContext = unify_context(umc_explicit, []),
     Unification = deconstruct(Var, ConsId, ArgVars, UniModes, can_fail,
         cannot_cgc),
-    ExtraGoal = unify(Var, rhs_functor(ConsId, no, ArgVars),
+    ExtraGoalExpr = unify(Var, rhs_functor(ConsId, no, ArgVars),
         UniMode, Unification, UnifyContext),
-    set.singleton_set(NonLocals, Var),
+    NonLocals = set_of_var.make_singleton(Var),
 
     % The test can't bind any variables, so the InstMapDelta should be empty.
     instmap_delta_init_reachable(InstMapDelta),
     goal_info_init(NonLocals, InstMapDelta, detism_semi, purity_pure,
-        ExtraGoalInfo).
+        ExtraGoalInfo),
+    ExtraGoal = hlds_goal(ExtraGoalExpr, ExtraGoalInfo).
 
 %-----------------------------------------------------------------------------%
 
@@ -3673,7 +3674,7 @@
 
         CondNonLocals = goal_info_get_nonlocals(CondGoalInfo),
         RestNonLocals = goal_info_get_nonlocals(RestGoalInfo),
-        set.union(CondNonLocals, RestNonLocals, NonLocals),
+        set_of_var.union(CondNonLocals, RestNonLocals, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo, NewGoalInfo0),
 
         InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo),
Index: compiler/size_prof.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/size_prof.m,v
retrieving revision 1.71
diff -u -b -r1.71 size_prof.m
--- compiler/size_prof.m	23 May 2011 05:08:12 -0000	1.71
+++ compiler/size_prof.m	15 Aug 2011 10:47:59 -0000
@@ -120,6 +120,7 @@
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.term_norm.
 
 :- import_module assoc_list.
@@ -130,7 +131,6 @@
 :- import_module maybe.
 :- import_module pair.
 :- import_module require.
-:- import_module set.
 :- import_module string.
 :- import_module term.
 :- import_module varset.
@@ -744,7 +744,7 @@
             construct_sub_info(no, yes(dynamic_size(SizeVar)))),
         UnifyExpr = unify(LHS, RHS, UniMode, Unification, UnifyContext),
         NonLocals0 = goal_info_get_nonlocals(GoalInfo0),
-        set.insert(SizeVar, NonLocals0, NonLocals),
+        set_of_var.insert(SizeVar, NonLocals0, NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
         UnifyGoal = hlds_goal(UnifyExpr, GoalInfo),
         Goals = list.condense([ArgGoals, SizeGoals, [UnifyGoal]]),
Index: compiler/stack_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_alloc.m,v
retrieving revision 1.31
diff -u -b -r1.31 stack_alloc.m
--- compiler/stack_alloc.m	22 Jul 2011 03:31:41 -0000	1.31
+++ compiler/stack_alloc.m	15 Aug 2011 15:35:24 -0000
@@ -72,8 +72,7 @@
         eff_trace_level_needs_fail_vars(ModuleInfo, PredInfo, !.ProcInfo,
             TraceLevel) = yes
     ->
-        trace_fail_vars(ModuleInfo, !.ProcInfo, FailVars0),
-        FailVars = set_to_bitset(FailVars0)
+        trace_fail_vars(ModuleInfo, !.ProcInfo, FailVars)
     ;
         FailVars = set_of_var.init
     ),
Index: compiler/stm_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stm_expand.m,v
retrieving revision 1.22
diff -u -b -r1.22 stm_expand.m
--- compiler/stm_expand.m	11 Aug 2011 23:19:00 -0000	1.22
+++ compiler/stm_expand.m	15 Aug 2011 11:18:44 -0000
@@ -238,9 +238,9 @@
     %
 :- type stm_goal_vars
     --->    stm_goal_vars(
-                vars_input               :: set(prog_var),
-                vars_local               :: set(prog_var),
-                vars_output              :: set(prog_var),
+                vars_input               :: set_of_progvar,
+                vars_local               :: set_of_progvar,
+                vars_output              :: set_of_progvar,
                 vars_innerDI             :: prog_var,       % inner STM di var
                 vars_innerUO             :: prog_var        % inner STM uo var
             ).
@@ -565,7 +565,7 @@
         Input = AGV ^ vars_input),
 
     list.map(ExtractInputSet, GoalList, InputVarList),
-    InputVars = set.union_list(InputVarList),
+    InputVars = set_of_var.union_list(InputVarList),
     GoalVar0 = list.det_head(GoalList),
     GoalVar = GoalVar0 ^ vars_input := InputVars.
 
@@ -624,17 +624,17 @@
 
     HldsGoal = hlds_goal(_, GoalInfo),
     GoalNonLocalSet0 = goal_info_get_nonlocals(GoalInfo),
-    set.delete_list(IgnoreVarList, GoalNonLocalSet0, GoalNonLocalSet),
-    GoalNonLocals = set.to_sorted_list(GoalNonLocalSet),
+    set_of_var.delete_list(IgnoreVarList, GoalNonLocalSet0, GoalNonLocalSet),
+    GoalNonLocals = set_of_var.to_sorted_list(GoalNonLocalSet),
 
     order_vars_into_groups(ModuleInfo, GoalVarList, InitInstmap, FinalInstmap,
         LocalVarsList, InputVarsList, _),
     order_vars_into_groups(ModuleInfo, GoalNonLocals, InitInstmap,
         FinalInstmap, _, _InputVarsList, OutputVarsList),
 
-    LocalVars = set.from_list(LocalVarsList),
-    InputVars = set.from_list(InputVarsList),
-    OutputVars = set.from_list(OutputVarsList),
+    LocalVars = set_of_var.list_to_set(LocalVarsList),
+    InputVars = set_of_var.list_to_set(InputVarsList),
+    OutputVars = set_of_var.list_to_set(OutputVarsList),
 
     StmGoalVars = stm_goal_vars(InputVars, LocalVars, OutputVars,
         InnerDI, InnerUO).
@@ -654,7 +654,8 @@
 remove_tail([], [], no - no, no - no).
 remove_tail([G | Gs], Goals, MaybeOutDI - MaybeOutUO,
 		MaybeInDI - MaybeInUO) :-
-    remove_tail(Gs, Goals0, MaybeOutDI0 - MaybeOutUO0,MaybeInDI0 - MaybeInUO0),
+    remove_tail(Gs, Goals0, MaybeOutDI0 - MaybeOutUO0,
+        MaybeInDI0 - MaybeInUO0),
     ( G = hlds_goal(plain_call(_, _, [_, X, V], _, _, stm_outer_inner), _) ->
         MaybeInDI = yes(V),
         MaybeInUO = MaybeInUO0,
@@ -1277,7 +1278,7 @@
     proc_info_get_varset(OldProcInfo0, OldPredVarSet0),
     proc_info_get_vartypes(OldProcInfo0, OldPredVarTypes0),
     AtomicGoalVars = stm_goal_vars(_, LocalVars, _, OrigInnerDI, OrigInnerUO),
-    LocalVarList = set.to_sorted_list(LocalVars),
+    LocalVarList = set_of_var.to_sorted_list(LocalVars),
 
     VarMapping0 = map.init,
     list.foldl5(apply_varset_to_preds, LocalVarList,
@@ -1886,7 +1887,8 @@
 
     create_simple_call(mercury_exception_module, "unsafe_try_stm",
         pf_predicate, mode_no(0), detism_cc_multi, purity_pure,
-        [RttiVar, AtomicClosureVar, ReturnExceptVar, InnerSTM0Var,InnerSTMVar],
+        [RttiVar, AtomicClosureVar, ReturnExceptVar,
+            InnerSTM0Var, InnerSTMVar],
         [],
         instmap_delta_from_assoc_list([
             RttiVar - ground(shared, none),
@@ -2159,7 +2161,7 @@
 :- pred create_var_test(prog_var::in, prog_var::in, unify_mode::in,
     hlds_goal::out, stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
-create_var_test(VarLHS, VarRHS, UnifyMode, HldsGoal, !NewPredInfo) :-
+create_var_test(VarLHS, VarRHS, UnifyMode, Goal, !NewPredInfo) :-
     Context = !.NewPredInfo ^ new_pred_context,
     ModuleInfo = !.NewPredInfo ^ new_pred_module_info,
 
@@ -2170,19 +2172,14 @@
 
     instmap_delta_from_mode_list([VarLHS, VarRHS], [ModeLHS, ModeRHS],
         ModuleInfo, InstmapDelta),
-    HldsGoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
-
-    some [!NonLocals] (
-        set.init(!:NonLocals),
-        set.insert(VarLHS, !NonLocals),
-        set.insert(VarRHS, !NonLocals),
+    GoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
 
+    set_of_var.list_to_set([VarLHS, VarRHS], NonLocals),
         Determism = detism_semi,
         Purity = purity_pure,
-        goal_info_init(!.NonLocals, InstmapDelta, Determism, Purity, Context,
-            HldsGoalInfo)
-    ),
-    HldsGoal = hlds_goal(HldsGoalExpr, HldsGoalInfo).
+    goal_info_init(NonLocals, InstmapDelta, Determism, Purity, Context,
+        GoalInfo),
+    Goal = hlds_goal(GoalExpr, GoalInfo).
 
     % Creates a unification between two variables (using the unify goal)
     % Takes the "stm_info" state
@@ -2190,7 +2187,7 @@
 :- pred create_var_unify_stm(prog_var::in, prog_var::in, unify_mode::in,
     hlds_goal::out, stm_info::in, stm_info::out) is det.
 
-create_var_unify_stm(VarLHS, VarRHS, UnifyMode, HldsGoal, !StmInfo) :-
+create_var_unify_stm(VarLHS, VarRHS, UnifyMode, Goal, !StmInfo) :-
     Context = term.context("--temp-context--", 999),
     ModuleInfo = !.StmInfo ^ stm_info_module_info,
 
@@ -2201,26 +2198,21 @@
 
     instmap_delta_from_mode_list([VarLHS, VarRHS], [ModeLHS, ModeRHS],
         ModuleInfo, InstmapDelta),
-    HldsGoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
-
-    some [!NonLocals] (
-        set.init(!:NonLocals),
-        set.insert(VarLHS, !NonLocals),
-        set.insert(VarRHS, !NonLocals),
+    GoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
 
+    set_of_var.list_to_set([VarLHS, VarRHS], NonLocals),
         Determism = detism_det,
         Purity = purity_pure,
-        goal_info_init(!.NonLocals, InstmapDelta, Determism, Purity, Context,
-            HldsGoalInfo)
-    ),
-    HldsGoal = hlds_goal(HldsGoalExpr, HldsGoalInfo).
+    goal_info_init(NonLocals, InstmapDelta, Determism, Purity, Context,
+        GoalInfo),
+    Goal = hlds_goal(GoalExpr, GoalInfo).
 
     % Creates a unification between two variables (using the unify goal)
     %
 :- pred create_var_unify(prog_var::in, prog_var::in, unify_mode::in,
     hlds_goal::out, stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
-create_var_unify(VarLHS, VarRHS, UnifyMode, HldsGoal, !NewPredInfo) :-
+create_var_unify(VarLHS, VarRHS, UnifyMode, Goal, !NewPredInfo) :-
     Context = !.NewPredInfo ^ new_pred_context,
     ModuleInfo = !.NewPredInfo ^ new_pred_module_info,
 
@@ -2231,19 +2223,14 @@
 
     instmap_delta_from_mode_list([VarLHS, VarRHS], [ModeLHS, ModeRHS],
         ModuleInfo, InstmapDelta),
-    HldsGoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
-
-    some [!NonLocals] (
-        set.init(!:NonLocals),
-        set.insert(VarLHS, !NonLocals),
-        set.insert(VarRHS, !NonLocals),
+    GoalExpr = unify(VarLHS, UnifyRHS, UnifyMode, UnifyType, UnifyContext),
 
+    set_of_var.list_to_set([VarLHS, VarRHS], NonLocals),
         Determism = detism_det,
         Purity = purity_pure,
-        goal_info_init(!.NonLocals, InstmapDelta, Determism, Purity, Context,
-            HldsGoalInfo)
-    ),
-    HldsGoal = hlds_goal(HldsGoalExpr, HldsGoalInfo).
+    goal_info_init(NonLocals, InstmapDelta, Determism, Purity, Context,
+        GoalInfo),
+    Goal = hlds_goal(GoalExpr, GoalInfo).
 
     % Creates a simple call.  If the call is polymorphic, remember to add
     % the runtime type information as well ("type_info" variable).
@@ -2297,7 +2284,7 @@
         !NewPredInfo) :-
     Context = !.NewPredInfo ^ new_pred_context,
     OutGoalExpr = if_then_else(ExistVars, Cond, Then, Else),
-    NonLocals = set.init,
+    NonLocals = set_of_var.init,
     instmap_delta_init_reachable(InstMapDelta),
     goal_info_init(NonLocals, InstMapDelta, Detism, Purity, Context,
         GoalInfo),
@@ -2312,7 +2299,7 @@
 create_switch_disjunction(ProgVar, Cases, Detism, Purity, OutGoal,
         !NewPredInfo) :-
     Context = !.NewPredInfo ^ new_pred_context,
-    NonLocals = set.init,
+    NonLocals = set_of_var.init,
     instmap_delta_init_reachable(InstMapDelta),
     OutGoalExpr = switch(ProgVar, cannot_fail, Cases),
     goal_info_init(NonLocals, InstMapDelta, Detism, Purity, Context,
@@ -2455,8 +2442,7 @@
     CallExpr = plain_call(NewPredId, NewProcId, ProcHeadVars, not_builtin, no,
         NewPredName),
 
-    set.init(CallNonLocals0),
-    set.insert_list(ProcHeadVars, CallNonLocals0, CallNonLocals),
+    set_of_var.list_to_set(ProcHeadVars, CallNonLocals),
     instmap_delta_from_mode_list(ProcHeadVars, ProcHeadModes, ModuleInfo0,
         CallInstmapDelta),
 
@@ -2563,15 +2549,15 @@
 
     % Get the list of input and output variables of the original atomic goal.
     %
-:- pred get_input_output_varlist(stm_goal_vars::in, list(prog_var)::out,
-    list(prog_var)::out) is det.
+:- pred get_input_output_varlist(stm_goal_vars::in,
+    list(prog_var)::out, list(prog_var)::out) is det.
 
 get_input_output_varlist(StmGoalVars, Input, Output) :-
     InputSet = StmGoalVars ^ vars_input,
     OutputSet = StmGoalVars ^ vars_output,
 
-    Input = set.to_sorted_list(InputSet),
-    Output = set.to_sorted_list(OutputSet).
+    Input = set_of_var.to_sorted_list(InputSet),
+    Output = set_of_var.to_sorted_list(OutputSet).
 
     % Get the list of types corresponding to the input and output
     % variables of the original atomic goal.
Index: compiler/store_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/store_alloc.m,v
retrieving revision 1.117
diff -u -b -r1.117 store_alloc.m
--- compiler/store_alloc.m	21 Jul 2011 06:58:27 -0000	1.117
+++ compiler/store_alloc.m	15 Aug 2011 15:38:24 -0000
@@ -95,17 +95,16 @@
         eff_trace_level_needs_fail_vars(ModuleInfo, PredInfo, !.ProcInfo,
             TraceLevel) = yes
     ->
-        trace_fail_vars(ModuleInfo, !.ProcInfo, ResumeVars0),
-        ResumeVars1 = set_to_bitset(ResumeVars0)
+        trace_fail_vars(ModuleInfo, !.ProcInfo, ResumeVars)
     ;
-        ResumeVars1 = set_of_var.init
+        ResumeVars = set_of_var.init
     ),
     build_input_arg_list(!.ProcInfo, InputArgLvals),
     LastLocns0 = initial_last_locns(InputArgLvals),
     proc_info_get_stack_slots(!.ProcInfo, StackSlots),
     StoreAllocInfo = store_alloc_info(ModuleInfo, StackSlots),
     store_alloc_in_goal(Goal2, Goal, Liveness0, _, LastLocns0, _,
-        ResumeVars1, StoreAllocInfo),
+        ResumeVars, StoreAllocInfo),
     proc_info_set_goal(Goal, !ProcInfo).
 
 :- func initial_last_locns(assoc_list(prog_var, lval)) = last_locns.
Index: compiler/structure_reuse.analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.analysis.m,v
retrieving revision 1.30
diff -u -b -r1.30 structure_reuse.analysis.m
--- compiler/structure_reuse.analysis.m	23 May 2011 05:08:12 -0000	1.30
+++ compiler/structure_reuse.analysis.m	15 Aug 2011 13:19:13 -0000
@@ -112,6 +112,7 @@
 :- import_module parse_tree.prog_ctgc.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.ctgc.selector.
 :- import_module transform_hlds.ctgc.structure_reuse.direct.
 :- import_module transform_hlds.ctgc.structure_reuse.domain.
@@ -409,14 +410,15 @@
     module_info_pred_proc_info(!.ModuleInfo, NewPPId, PredInfo, ProcInfo0),
     proc_info_get_headvars(ProcInfo0, HeadVars),
     get_numbered_args(1, NoClobbers, HeadVars, NoClobberVars),
-    add_vars_to_lfu(set.from_list(NoClobberVars), ProcInfo0, ProcInfo),
+    add_vars_to_lfu(set_of_var.list_to_set(NoClobberVars),
+        ProcInfo0, ProcInfo),
     module_info_set_pred_proc_info(NewPPId, PredInfo, ProcInfo, !ModuleInfo),
 
     reuse_as_table_insert_reuse_version_proc(PPId, NoClobbers, NewPPId,
         !ReuseTable).
 
-:- pred get_numbered_args(int::in, list(int)::in, prog_vars::in,
-    prog_vars::out) is det.
+:- pred get_numbered_args(int::in, list(int)::in,
+    list(prog_var)::in, list(prog_var)::out) is det.
 
 get_numbered_args(_, [], _, []).
 get_numbered_args(_, [_ | _], [], _) :-
Index: compiler/structure_reuse.direct.detect_garbage.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.direct.detect_garbage.m,v
retrieving revision 1.26
diff -u -b -r1.26 structure_reuse.direct.detect_garbage.m
--- compiler/structure_reuse.direct.detect_garbage.m	23 May 2011 05:08:12 -0000	1.26
+++ compiler/structure_reuse.direct.detect_garbage.m	15 Aug 2011 12:21:37 -0000
@@ -39,6 +39,7 @@
 :- import_module check_hlds.type_util.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_out.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.ctgc.datastruct.
 :- import_module transform_hlds.ctgc.livedata.
 
@@ -51,11 +52,11 @@
 
 :- type detect_bg_info
     --->    detect_bg_info(
-                module_info     ::  module_info,
-                pred_info       ::  pred_info,
-                proc_info       ::  proc_info,
-                sharing_table   ::  sharing_as_table,
-                very_verbose    ::  bool
+                dbgi_module_info        :: module_info,
+                dbgi_pred_info          :: pred_info,
+                dbgi_proc_info          :: proc_info,
+                dbgi_sharing_table      :: sharing_as_table,
+                dbgi_very_verbose       :: bool
             ).
 
 :- func detect_bg_info_init(module_info, pred_info, proc_info,
@@ -78,7 +79,7 @@
         sharing_as_init, _, dead_cell_table_init, DeadCellTable),
 
     % Add a newline after the "progress dots".
-    VeryVerbose = Background ^ very_verbose,
+    VeryVerbose = Background ^ dbgi_very_verbose,
     (
         VeryVerbose = yes,
         trace [io(!IO)] (
@@ -102,10 +103,10 @@
 determine_dead_deconstructions_2(Background, TopGoal, !SharingAs,
         !DeadCellTable) :-
     TopGoal = hlds_goal(GoalExpr, GoalInfo),
-    ModuleInfo = Background ^ module_info,
-    PredInfo = Background ^ pred_info,
-    ProcInfo = Background ^ proc_info,
-    SharingTable = Background ^ sharing_table,
+    ModuleInfo = Background ^ dbgi_module_info,
+    PredInfo = Background ^ dbgi_pred_info,
+    ProcInfo = Background ^ dbgi_proc_info,
+    SharingTable = Background ^ dbgi_sharing_table,
     (
         GoalExpr = conj(_, Goals),
         list.foldl2(determine_dead_deconstructions_2_with_progress(Background),
@@ -173,7 +174,7 @@
 
 determine_dead_deconstructions_2_with_progress(Background, TopGoal,
         !SharingAs, !DeadCellTable) :-
-    VeryVerbose = Background ^ very_verbose,
+    VeryVerbose = Background ^ dbgi_very_verbose,
     (
         VeryVerbose = yes,
         trace [io(!IO)] (
@@ -203,8 +204,8 @@
         Goal, !SharingAs, !DeadCellTable) :-
     determine_dead_deconstructions_2(Background, Goal, SharingBeforeDisj,
         GoalSharing, !DeadCellTable),
-    !:SharingAs = sharing_as_least_upper_bound(Background ^ module_info,
-        Background ^ proc_info, !.SharingAs, GoalSharing).
+    !:SharingAs = sharing_as_least_upper_bound(Background ^ dbgi_module_info,
+        Background ^ dbgi_proc_info, !.SharingAs, GoalSharing).
 
 :- pred determine_dead_deconstructions_generic_call(module_info::in,
     proc_info::in, generic_call::in, prog_vars::in, list(mer_mode)::in,
Index: compiler/structure_reuse.domain.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.domain.m,v
retrieving revision 1.27
diff -u -b -r1.27 structure_reuse.domain.m
--- compiler/structure_reuse.domain.m	23 May 2011 05:08:12 -0000	1.27
+++ compiler/structure_reuse.domain.m	15 Aug 2011 14:55:07 -0000
@@ -28,7 +28,6 @@
 :- import_module bool.
 :- import_module io.
 :- import_module map.
-:- import_module set.
 :- import_module list.
 
 %-----------------------------------------------------------------------------%
@@ -56,7 +55,7 @@
     % local forward and backward use, as well as the local structure sharing.
     %
 :- func reuse_condition_init(module_info, proc_info, dead_var,
-    set(live_var), set(live_var), sharing_as) = reuse_condition.
+    set_of_live_var, set_of_live_var, sharing_as) = reuse_condition.
 
 :- pred reuse_condition_is_conditional(reuse_condition::in) is semidet.
 
@@ -272,6 +271,7 @@
 :- import_module hlds.hlds_out.
 :- import_module hlds.hlds_out.hlds_out_util.
 :- import_module parse_tree.prog_ctgc.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.ctgc.datastruct.
 :- import_module transform_hlds.ctgc.util.
 
@@ -358,11 +358,11 @@
         Condition = always
     ;
         Nodes = [_ | _],
-        set.union(LFU, LBU, LU),
+        set_of_var.union(LFU, LBU, LU),
         % XXX the old implementation did not bother about extending at this
         % place, which was contrary to the theory. Check the effect of this
         % change!
-        LuData = list.map(datastruct_init, set.to_sorted_list(LU)),
+        LuData = list.map(datastruct_init, set_of_var.to_sorted_list(LU)),
         ExtendedLuData = list.map(
             extend_datastruct(ModuleInfo, ProcInfo, Sharing), LuData),
         SharedLU = list.condense(ExtendedLuData),
@@ -370,7 +370,7 @@
 
         structure_sharing.domain.sharing_as_project(HeadVars, Sharing,
             HeadVarSharing),
-        Condition = condition(set.from_list(Nodes), HeadVarSharedLU,
+        Condition = condition(set.list_to_set(Nodes), HeadVarSharedLU,
             HeadVarSharing)
     ).
 
Index: compiler/structure_reuse.indirect.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.indirect.m,v
retrieving revision 1.38
diff -u -b -r1.38 structure_reuse.indirect.m
--- compiler/structure_reuse.indirect.m	23 May 2011 05:08:12 -0000	1.38
+++ compiler/structure_reuse.indirect.m	15 Aug 2011 12:18:52 -0000
@@ -79,6 +79,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.ctgc.datastruct.
 :- import_module transform_hlds.ctgc.fixpoint_table.
 :- import_module transform_hlds.ctgc.livedata.
@@ -921,8 +922,8 @@
         Result = reuse_possible,
         LFU = goal_info_get_lfu(GoalInfo),
         LBU = goal_info_get_lbu(GoalInfo),
-        LU = set.union(LFU, LBU),
-        LuList = set.to_sorted_list(LU),
+        LU = set_of_var.union(LFU, LBU),
+        LuList = set_of_var.to_sorted_list(LU),
         LuData = list.map(datastruct_init, LuList),
         NewReuseAs = reuse_as_from_called_procedure_to_local_reuse_as(
             ModuleInfo, ProcInfo, BaseInfo ^ headvars, LuData, SharingAs,
Index: compiler/structure_reuse.lbu.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.lbu.m,v
retrieving revision 1.16
diff -u -b -r1.16 structure_reuse.lbu.m
--- compiler/structure_reuse.lbu.m	21 Jul 2011 06:58:27 -0000	1.16
+++ compiler/structure_reuse.lbu.m	15 Aug 2011 12:17:14 -0000
@@ -56,19 +56,19 @@
     proc_info_get_vartypes(!.ProcInfo, VarTypes),
 
     % Before the first goal, the set of variables in LBU is empty.
-    LBU0 = set.init,
+    LBU0 = set_of_var.init,
     backward_use_in_goal(VarTypes, Goal0, Goal, LBU0, _LBU),
 
     proc_info_set_goal(Goal, !ProcInfo).
 
 :- pred backward_use_in_goal(vartypes::in, hlds_goal::in, hlds_goal::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_goal(VarTypes, !TopGoal, !LBU) :-
     !.TopGoal = hlds_goal(Expr0, Info0),
 
     % Add resume_vars to the LBU-set.
-    set.union(get_backtrack_vars(VarTypes, Info0), !LBU),
+    set_of_var.union(get_backtrack_vars(VarTypes, Info0), !LBU),
 
     backward_use_in_goal_2(VarTypes, Info0, Expr0, Expr, !LBU),
 
@@ -76,8 +76,8 @@
     !:TopGoal = hlds_goal(Expr, Info).
 
 :- pred backward_use_in_goal_2(vartypes::in, hlds_goal_info::in,
-    hlds_goal_expr::in, hlds_goal_expr::out, set(prog_var)::in,
-    set(prog_var)::out) is det.
+    hlds_goal_expr::in, hlds_goal_expr::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_goal_2(VarTypes, Info0, !Expr, !LBU) :-
     % Handle each goal type separately:
@@ -86,9 +86,7 @@
     ;
         !.Expr = plain_call(_,_, _, _, _, _),
         Det = goal_info_get_determinism(Info0),
-        (
-            detism_allows_multiple_solns(Det)
-        ->
+        ( detism_allows_multiple_solns(Det) ->
             % Implementation of Instantiation 2 from Nancy's PhD.
             % In this instantation, a non-deterministic procedure
             % call only adds its LFU-variables to the current set
@@ -96,11 +94,9 @@
 
             goal_info_get_pre_births(Info0, PreBirths),
             goal_info_get_post_births(Info0, PostBirths),
-            !:LBU = set.union_list([goal_info_get_lfu(Info0),
-                remove_typeinfo_vars_from_set(VarTypes,
-                    bitset_to_set(PreBirths)),
-                remove_typeinfo_vars_from_set(VarTypes,
-                    bitset_to_set(PostBirths)),
+            !:LBU = set_of_var.union_list([goal_info_get_lfu(Info0),
+                remove_typeinfo_vars_from_set_of_var(VarTypes, PreBirths),
+                remove_typeinfo_vars_from_set_of_var(VarTypes, PostBirths),
                 !.LBU])
         ;
             true
@@ -166,7 +162,7 @@
 
         % Annotate Else-goal.
         backward_use_in_goal(VarTypes, Else0, Else, LBU0, LBUE),
-        set.union(LBUT, LBUE, !:LBU),
+        set_of_var.union(LBUT, LBUE, !:LBU),
         !:Expr = if_then_else(Vars, Cond, Then, Else)
     ;
         !.Expr = shorthand(_),
@@ -174,16 +170,16 @@
         unexpected($module, $pred, "shorthand")
     ).
 
-:- func get_backtrack_vars(vartypes, hlds_goal_info) = set(prog_var).
+:- func get_backtrack_vars(vartypes, hlds_goal_info) = set_of_progvar.
 
 get_backtrack_vars(VarTypes, Info) = Vars :-
     goal_info_get_resume_point(Info, ResPoint),
     (
         ResPoint = resume_point(ResVars, _),
-        Vars = remove_typeinfo_vars_from_set(VarTypes, bitset_to_set(ResVars))
+        Vars = remove_typeinfo_vars_from_set_of_var(VarTypes, ResVars)
     ;
         ResPoint = no_resume_point,
-        Vars = set.init
+        Vars = set_of_var.init
     ).
 
 :- pred detism_allows_multiple_solns(prog_data__determinism::in) is semidet.
@@ -194,43 +190,44 @@
 detism_allows_multiple_solns(detism_cc_multi).
 
 :- pred backward_use_in_conj(vartypes::in, list(hlds_goal)::in,
-    list(hlds_goal)::out, set(prog_var)::in, set(prog_var)::out) is det.
+    list(hlds_goal)::out, set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_conj(VarTypes, !Goals, !LBU) :-
     list.map_foldl(backward_use_in_goal(VarTypes), !Goals, !LBU).
 
 :- pred backward_use_in_cases(vartypes::in, list(case)::in, list(case)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_cases(VarTypes, !Cases, !LBU) :-
     % Every case is analysed with the same initial set of LBU-vars.
     LBU0 = !.LBU,
     list.map_foldl(backward_use_in_case(LBU0, VarTypes), !Cases, !LBU).
 
-:- pred backward_use_in_case(set(prog_var)::in, vartypes::in, case::in,
-    case::out, set(prog_var)::in, set(prog_var)::out) is det.
+:- pred backward_use_in_case(set_of_progvar::in, vartypes::in,
+    case::in, case::out, set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_case(LBU0, VarTypes, !Case, !LBU):-
     !.Case = case(MainConsId, OtherConsIds, Goal0),
     backward_use_in_goal(VarTypes, Goal0, Goal, LBU0, NewLBU),
     !:Case = case(MainConsId, OtherConsIds, Goal),
-    set.union(NewLBU, !LBU).
+    set_of_var.union(NewLBU, !LBU).
 
-:- pred backward_use_in_disj(vartypes::in, list(hlds_goal)::in,
-    list(hlds_goal)::out, set(prog_var)::in, set(prog_var)::out) is det.
+:- pred backward_use_in_disj(vartypes::in,
+    list(hlds_goal)::in, list(hlds_goal)::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_disj(VarTypes, !Goals, !LBU) :-
     % Every disj-goal is analysed with the same initial set of LBU-vars.
     LBU0 = !.LBU,
     list.map_foldl(backward_use_in_disj_goal(LBU0, VarTypes), !Goals, !LBU).
 
-:- pred backward_use_in_disj_goal(set(prog_var)::in, vartypes::in,
-    hlds_goal::in, hlds_goal::out, set(prog_var)::in,
-    set(prog_var)::out) is det.
+:- pred backward_use_in_disj_goal(set_of_progvar::in, vartypes::in,
+    hlds_goal::in, hlds_goal::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 backward_use_in_disj_goal(LBU0, VarTypes, !Goal, !LBU) :-
     backward_use_in_goal(VarTypes, !Goal, LBU0, NewLBU),
-    set.union(NewLBU, !LBU).
+    set_of_var.union(NewLBU, !LBU).
 
 %-----------------------------------------------------------------------------%
 :- end_module transform_hlds.ctgc.structure_reuse.lbu.
Index: compiler/structure_reuse.lfu.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.lfu.m,v
retrieving revision 1.18
diff -u -b -r1.18 structure_reuse.lfu.m
--- compiler/structure_reuse.lfu.m	21 Jul 2011 06:58:27 -0000	1.18
+++ compiler/structure_reuse.lfu.m	15 Aug 2011 15:44:47 -0000
@@ -23,8 +23,7 @@
 :- interface.
 
 :- import_module hlds.hlds_pred.
-:- import_module parse_tree.prog_data.
-:- import_module set.
+:- import_module parse_tree.set_of_var.
 
 %-----------------------------------------------------------------------------%
 
@@ -34,7 +33,7 @@
     %
     % Add the vars to all the LFU sets in the body of the procedure.
     %
-:- pred add_vars_to_lfu(set(prog_var)::in, proc_info::in, proc_info::out)
+:- pred add_vars_to_lfu(set_of_progvar::in, proc_info::in, proc_info::out)
     is det.
 
 %-----------------------------------------------------------------------------%
@@ -45,8 +44,8 @@
 :- import_module hlds.hlds_goal.
 :- import_module hlds.hlds_llds.
 :- import_module hlds.hlds_pred.
+:- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
-:- import_module parse_tree.set_of_var.
 
 :- import_module list.
 :- import_module map.
@@ -62,17 +61,17 @@
     proc_info_get_liveness_info(!.ProcInfo, InstantiatedVars0),
         % Set of variables initially "dead" = instantiated variables that
         % syntactically do not occur in the remainder of the goal.
-    set.init(DeadVars0),
+    set_of_var.init(DeadVars0),
 
     forward_use_in_goal(VarTypes, Goal0, Goal,
-        remove_typeinfo_vars_from_set(VarTypes, InstantiatedVars0),
+        remove_typeinfo_vars_from_set_of_var(VarTypes, InstantiatedVars0),
         _InstantiatedVars, DeadVars0, _DeadVars),
 
     proc_info_set_goal(Goal, !ProcInfo).
 
 :- pred forward_use_in_goal(vartypes::in, hlds_goal::in, hlds_goal::out,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_goal(VarTypes, !Goal, !InstantiatedVars, !DeadVars) :-
     !.Goal = hlds_goal(GoalExpr0, GoalInfo0),
@@ -82,20 +81,20 @@
         InstantiatedVars0 = !.InstantiatedVars,
         compute_instantiated_and_dead_vars(VarTypes, GoalInfo0,
             !InstantiatedVars, !DeadVars),
-        set.difference(InstantiatedVars0, !.DeadVars, LFU),
+        set_of_var.difference(InstantiatedVars0, !.DeadVars, LFU),
         goal_info_set_lfu(LFU, GoalInfo0, GoalInfo),
         !:Goal = hlds_goal(GoalExpr0, GoalInfo)
     ;
         HasSubGoals = has_subgoals,
         goal_info_get_pre_deaths(GoalInfo0, PreDeaths),
-        set.union(bitset_to_set(PreDeaths), !DeadVars),
+        set_of_var.union(PreDeaths, !DeadVars),
         forward_use_in_composite_goal(VarTypes, !Goal,
             !InstantiatedVars, !DeadVars)
     ).
 
 :- pred compute_instantiated_and_dead_vars(vartypes::in, hlds_goal_info::in,
-    set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 compute_instantiated_and_dead_vars(VarTypes, Info, !Inst, !Dead) :-
     % Inst = Inst0 + birth-set
@@ -104,16 +103,15 @@
     goal_info_get_post_births(Info, PostBirths),
     goal_info_get_post_deaths(Info, PostDeaths),
     goal_info_get_pre_deaths(Info, PreDeaths),
-    !:Inst = set.union_list([
-        remove_typeinfo_vars_from_set(VarTypes, bitset_to_set(PreBirths)),
-        remove_typeinfo_vars_from_set(VarTypes, bitset_to_set(PostBirths)),
+    !:Inst = set_of_var.union_list([
+        remove_typeinfo_vars_from_set_of_var(VarTypes, PreBirths),
+        remove_typeinfo_vars_from_set_of_var(VarTypes, PostBirths),
         !.Inst]),
-    !:Dead = set.union_list(
-        [bitset_to_set(PreDeaths), bitset_to_set(PostDeaths), !.Dead]).
+    !:Dead = set_of_var.union_list([PreDeaths, PostDeaths, !.Dead]).
 
 :- pred forward_use_in_composite_goal(vartypes::in, hlds_goal::in,
-    hlds_goal::out, set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+    hlds_goal::out, set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_composite_goal(VarTypes, !Goal, !InstantiatedVars,
         !DeadVars) :-
@@ -156,8 +154,8 @@
         forward_use_in_goal(VarTypes, Then0, Then,
             !InstantiatedVars, !DeadVars),
         forward_use_in_goal(VarTypes, Else0, Else, Inst0, Inst1, Dead0, Dead1),
-        set.union(Inst1, !InstantiatedVars),
-        set.union(Dead1, !DeadVars),
+        set_of_var.union(Inst1, !InstantiatedVars),
+        set_of_var.union(Dead1, !DeadVars),
         GoalExpr = if_then_else(Vars, Cond, Then, Else)
     ;
         ( GoalExpr0 = unify(_, _, _, _, _)
@@ -170,21 +168,22 @@
         GoalExpr0 = shorthand(_),
         unexpected($module, $pred, "shorthand")
     ),
-    set.difference(InstantiadedBefore, !.DeadVars, LFU),
+    set_of_var.difference(InstantiadedBefore, !.DeadVars, LFU),
     goal_info_set_lfu(LFU, GoalInfo0, GoalInfo),
     !:Goal = hlds_goal(GoalExpr, GoalInfo).
 
-:- pred forward_use_in_conj(vartypes::in, list(hlds_goal)::in,
-    list(hlds_goal)::out, set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+:- pred forward_use_in_conj(vartypes::in,
+    list(hlds_goal)::in, list(hlds_goal)::out,
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_conj(VarTypes, !Goals, !InstantiatedVars, !DeadVars) :-
     list.map_foldl2(forward_use_in_goal(VarTypes), !Goals,
         !InstantiatedVars, !DeadVars).
 
 :- pred forward_use_in_cases(vartypes::in, list(case)::in, list(case)::out,
-    set(prog_var)::in, set(prog_var)::out, set(prog_var)::in,
-    set(prog_var)::out) is det.
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_cases(VarTypes, !Cases, !InstantiatedVars, !DeadVars) :-
     Inst0 = !.InstantiatedVars,
@@ -192,21 +191,23 @@
     list.map_foldl2(forward_use_in_case(VarTypes, Inst0, Dead0),
         !Cases, !InstantiatedVars, !DeadVars).
 
-:- pred forward_use_in_case(vartypes::in, set(prog_var)::in,
-    set(prog_var)::in, case::in, case::out, set(prog_var)::in,
-    set(prog_var)::out, set(prog_var)::in, set(prog_var)::out) is det.
+:- pred forward_use_in_case(vartypes::in, set_of_progvar::in,
+    set_of_progvar::in, case::in, case::out,
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_case(VarTypes, Inst0, Dead0, !Case,
         !InstantiatedVars, !DeadVars) :-
     !.Case = case(MainConsId, OtherConsIds, Goal0),
     forward_use_in_goal(VarTypes, Goal0, Goal, Inst0, Inst, Dead0, Dead),
     !:Case = case(MainConsId, OtherConsIds, Goal),
-    set.union(Inst, !InstantiatedVars),
-    set.union(Dead, !DeadVars).
+    set_of_var.union(Inst, !InstantiatedVars),
+    set_of_var.union(Dead, !DeadVars).
 
-:- pred forward_use_in_disj(vartypes::in, list(hlds_goal)::in,
-    list(hlds_goal)::out, set(prog_var)::in, set(prog_var)::out,
-    set(prog_var)::in, set(prog_var)::out) is det.
+:- pred forward_use_in_disj(vartypes::in,
+    list(hlds_goal)::in, list(hlds_goal)::out,
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_disj(VarTypes, !Goals, !InstantiatedVars, !DeadVars):-
     Inst0 = !.InstantiatedVars,
@@ -214,15 +215,16 @@
     list.map_foldl2(forward_use_in_disj_goal(VarTypes, Inst0, Dead0),
         !Goals, !InstantiatedVars, !DeadVars).
 
-:- pred forward_use_in_disj_goal(vartypes::in, set(prog_var)::in,
-    set(prog_var)::in, hlds_goal::in, hlds_goal::out, set(prog_var)::in,
-    set(prog_var)::out, set(prog_var)::in, set(prog_var)::out) is det.
+:- pred forward_use_in_disj_goal(vartypes::in, set_of_progvar::in,
+    set_of_progvar::in, hlds_goal::in, hlds_goal::out,
+    set_of_progvar::in, set_of_progvar::out,
+    set_of_progvar::in, set_of_progvar::out) is det.
 
 forward_use_in_disj_goal(VarTypes, Inst0, Dead0, !Goal,
         !InstantiatedVars, !DeadVars) :-
     forward_use_in_goal(VarTypes, !Goal, Inst0, Inst, Dead0, Dead),
-    set.union(Inst, !InstantiatedVars),
-    set.union(Dead, !DeadVars).
+    set_of_var.union(Inst, !InstantiatedVars),
+    set_of_var.union(Dead, !DeadVars).
 
 %-----------------------------------------------------------------------------%
 
@@ -231,19 +233,19 @@
     add_vars_to_lfu_in_goal(ForceInUse, Goal0, Goal),
     proc_info_set_goal(Goal, !ProcInfo).
 
-:- pred add_vars_to_lfu_in_goal(set(prog_var)::in,
+:- pred add_vars_to_lfu_in_goal(set_of_progvar::in,
     hlds_goal::in, hlds_goal::out) is det.
 
 add_vars_to_lfu_in_goal(ForceInUse, Goal0, Goal) :-
     Goal0 = hlds_goal(Expr0, GoalInfo0),
     add_vars_to_lfu_in_goal_expr(ForceInUse, Expr0, Expr),
     LFU0 = goal_info_get_lfu(GoalInfo0),
-    LFU = set.union(ForceInUse, LFU0),
+    LFU = set_of_var.union(ForceInUse, LFU0),
     goal_info_set_lfu(LFU, GoalInfo0, GoalInfo1),
     goal_info_set_reuse(no_reuse_info, GoalInfo1, GoalInfo),
     Goal = hlds_goal(Expr, GoalInfo).
 
-:- pred add_vars_to_lfu_in_goal_expr(set(prog_var)::in,
+:- pred add_vars_to_lfu_in_goal_expr(set_of_progvar::in,
     hlds_goal_expr::in, hlds_goal_expr::out) is det.
 
 add_vars_to_lfu_in_goal_expr(ForceInUse, Expr0, Expr) :-
@@ -308,7 +310,7 @@
         Expr = shorthand(Shorthand)
     ).
 
-:- pred add_vars_to_lfu_in_goals(set(prog_var)::in,
+:- pred add_vars_to_lfu_in_goals(set_of_progvar::in,
     hlds_goals::in, hlds_goals::out) is det.
 
 add_vars_to_lfu_in_goals(_, [], []).
@@ -316,7 +318,7 @@
     add_vars_to_lfu_in_goal(ForceInUse, Goal0, Goal),
     add_vars_to_lfu_in_goals(ForceInUse, Goals0, Goals).
 
-:- pred add_vars_to_lfu_in_cases(set(prog_var)::in,
+:- pred add_vars_to_lfu_in_cases(set_of_progvar::in,
     list(case)::in, list(case)::out) is det.
 
 add_vars_to_lfu_in_cases(_, [], []).
Index: compiler/superhomogeneous.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/superhomogeneous.m,v
retrieving revision 1.46
diff -u -b -r1.46 superhomogeneous.m
--- compiler/superhomogeneous.m	11 Aug 2011 23:19:00 -0000	1.46
+++ compiler/superhomogeneous.m	15 Aug 2011 08:09:05 -0000
@@ -419,7 +419,7 @@
         % from_ground_term_construct scopes, and quantification can
         % exploit this knowledge.
         Kind = from_ground_term_construct,
-        goal_info_set_nonlocals(set.make_singleton_set(LHSVar),
+        goal_info_set_nonlocals(set_of_var.make_singleton(LHSVar),
             GoalInfo0, GoalInfo),
         ( GoalExpr0 = conj(plain_conj, Conjuncts0) ->
             mark_nonlocals_in_ground_term_construct(Conjuncts0, Conjuncts),
@@ -447,7 +447,7 @@
         GoalExpr = unify(LHSVar, RHS, _, _, _),
         RHS = rhs_functor(_, _, RHSVars)
     ->
-        set.list_to_set([LHSVar | RHSVars], NonLocals),
+        set_of_var.list_to_set([LHSVar | RHSVars], NonLocals),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
         Goal = hlds_goal(GoalExpr, GoalInfo)
     ;
Index: compiler/switch_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/switch_detection.m,v
retrieving revision 1.156
diff -u -b -r1.156 switch_detection.m
--- compiler/switch_detection.m	23 May 2011 05:08:12 -0000	1.156
+++ compiler/switch_detection.m	15 Aug 2011 08:55:49 -0000
@@ -81,6 +81,7 @@
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -275,7 +276,7 @@
         ;
             Disjuncts0 = [_ | _],
             NonLocals = goal_info_get_nonlocals(GoalInfo),
-            set.to_sorted_list(NonLocals, NonLocalsList),
+            set_of_var.to_sorted_list(NonLocals, NonLocalsList),
             detect_switches_in_disj(GoalInfo, NonLocalsList,
                 Disjuncts0, NonLocalsList, InstMap0, [], GoalExpr, !LocalInfo)
         )
@@ -782,8 +783,8 @@
                 % specific to each cons_id, so it could not be shared with
                 % other cons_ids.
                 NonLocals = goal_info_get_nonlocals(FirstGoalInfo),
-                set.delete(Var, NonLocals, OtherNonLocals),
-                set.empty(OtherNonLocals),
+                set_of_var.delete(Var, NonLocals, OtherNonLocals),
+                set_of_var.is_empty(OtherNonLocals),
 
                 all_disjuncts_are_switch_var_unifies(Var, Disjuncts,
                     DisjConsIds),
Index: compiler/table_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/table_gen.m,v
retrieving revision 1.162
diff -u -b -r1.162 table_gen.m
--- compiler/table_gen.m	6 May 2011 05:03:24 -0000	1.162
+++ compiler/table_gen.m	15 Aug 2011 10:47:04 -0000
@@ -77,6 +77,7 @@
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -666,7 +667,7 @@
     % the code generated by the tabling transformation does,
     % so we need to compute the nonlocals from the headvars rather
     % than getting it from the nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     Context = goal_info_get_context(OrigGoalInfo),
@@ -714,7 +715,7 @@
         ModuleInfo, Context, MarkActiveFailGoal),
 
     determinism_to_code_model(Detism, CodeModel),
-    set.list_to_set([TableTipVar | HeadVars], InactiveNonLocals),
+    set_of_var.list_to_set([TableTipVar | HeadVars], InactiveNonLocals),
     OutputVars = list.map(project_var, NumberedOutputVars),
     InactiveInstmapDelta = instmap_delta_bind_vars(OutputVars),
     (
@@ -729,7 +730,7 @@
 
         ThenGoalExpr = conj(plain_conj, Unifies ++ [MarkInactiveGoal]),
         ThenVars = [TableTipVar | OutputVars] ++ NewVars,
-        set.list_to_set(ThenVars, ThenNonLocals),
+        set_of_var.list_to_set(ThenVars, ThenNonLocals),
         goal_info_init_hide(ThenNonLocals, InactiveInstmapDelta, Detism,
             purity_impure, Context, ThenGoalInfo),
         ThenGoal = hlds_goal(ThenGoalExpr, ThenGoalInfo),
@@ -740,13 +741,13 @@
         CodeModel = model_non,
         AfterGoalExpr = disj([MarkInactiveGoal, MarkActiveFailGoal]),
         instmap_delta_init_reachable(AfterInstMapDelta),
-        goal_info_init_hide(set.make_singleton_set(TableTipVar),
+        goal_info_init_hide(set_of_var.make_singleton(TableTipVar),
             AfterInstMapDelta, detism_multi, purity_impure, Context,
             AfterGoalInfo),
         AfterGoal = hlds_goal(AfterGoalExpr, AfterGoalInfo),
         FirstGoalExpr = conj(plain_conj, [OrigGoal, AfterGoal]),
         OrigGINonLocals = goal_info_get_nonlocals(OrigGoalInfo),
-        set.insert(TableTipVar, OrigGINonLocals, FirstNonlocals),
+        set_of_var.insert(TableTipVar, OrigGINonLocals, FirstNonlocals),
         goal_info_set_nonlocals(FirstNonlocals, OrigGoalInfo, FirstGoalInfo),
         FirstGoal = hlds_goal(FirstGoalExpr, FirstGoalInfo),
         InactiveGoalExpr = disj([FirstGoal, MarkInactiveFailGoal])
@@ -760,7 +761,7 @@
         case(loop_inactive_cons_id, [], InactiveGoal)
     ],
     SwitchExpr = switch(StatusVar, cannot_fail, SwitchArms),
-    set.insert_list([StatusVar, TableTipVar],
+    set_of_var.insert_list([StatusVar, TableTipVar],
         InactiveNonLocals, SwitchNonLocals),
     goal_info_init_hide(SwitchNonLocals, InactiveInstmapDelta, Detism,
         purity_impure, Context, SwitchGoalInfo),
@@ -906,7 +907,7 @@
     % the code generated by the tabling transformation does,
     % so we need to compute the nonlocals from the headvars rather
     % than getting it from the nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     Context = goal_info_get_context(OrigGoalInfo),
@@ -943,7 +944,7 @@
         RestoreAnswerGoal),
     SucceededGoal = RestoreAnswerGoal,
 
-    set.list_to_set([TableTipVar | HeadVars], InactiveNonLocals),
+    set_of_var.list_to_set([TableTipVar | HeadVars], InactiveNonLocals),
     OutputVars = list.map(project_var, NumberedOutputVars),
     InactiveInstmapDelta = instmap_delta_bind_vars(OutputVars),
 
@@ -968,7 +969,7 @@
 
         ThenGoalExpr = conj(plain_conj, Unifies ++ SaveAnswerGoals),
         ThenVars = [TableTipVar | OutputVars] ++ NewVars,
-        set.list_to_set(ThenVars, ThenNonLocals),
+        set_of_var.list_to_set(ThenVars, ThenNonLocals),
         goal_info_init_hide(ThenNonLocals, InactiveInstmapDelta,
             detism_det, purity_impure, Context, ThenGoalInfo),
         ThenGoal = hlds_goal(ThenGoalExpr, ThenGoalInfo),
@@ -1001,7 +1002,7 @@
     ),
 
     SwitchExpr = switch(StatusVar, cannot_fail, SwitchArms),
-    set.insert(StatusVar, InactiveNonLocals, SwitchNonLocals),
+    set_of_var.insert(StatusVar, InactiveNonLocals, SwitchNonLocals),
     goal_info_init_hide(SwitchNonLocals, InactiveInstmapDelta,
         Detism, purity_impure, Context, SwitchGoalInfo),
     SwitchGoal = hlds_goal(SwitchExpr, SwitchGoalInfo),
@@ -1027,7 +1028,7 @@
     % the code generated by the tabling transformation does,
     % so we need to compute the nonlocals from the headvars rather
     % than getting it from the nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     Context = goal_info_get_context(OrigGoalInfo),
@@ -1085,7 +1086,7 @@
         ModuleInfo, Context, MarkCompleteGoal),
 
     OrigSaveExpr = conj(plain_conj, [OrigGoal | SaveAnswerGoals]),
-    set.insert(RecordVar, OrigNonLocals, OrigSaveNonLocals),
+    set_of_var.insert(RecordVar, OrigNonLocals, OrigSaveNonLocals),
     create_instmap_delta([OrigGoal | SaveAnswerGoals], OrigSaveIMD0),
     instmap_delta_restrict(OrigSaveNonLocals, OrigSaveIMD0, OrigSaveIMD),
     goal_info_init_hide(OrigSaveNonLocals, OrigSaveIMD, detism_non,
@@ -1093,7 +1094,7 @@
     OrigSaveGoal = hlds_goal(OrigSaveExpr, OrigSaveGoalInfo),
 
     AfterExpr = disj([MarkIncompleteGoal, MarkActiveGoal]),
-    AfterNonLocals = set.make_singleton_set(RecordVar),
+    AfterNonLocals = set_of_var.make_singleton(RecordVar),
     create_instmap_delta([], AfterInstMapDelta),
     goal_info_init_hide(AfterNonLocals, AfterInstMapDelta, detism_non,
         purity_impure, Context, AfterGoalInfo),
@@ -1105,7 +1106,7 @@
     InactiveExpr = disj([OrigSaveAfterGoal, MarkCompleteGoal]),
     InactiveGoal = hlds_goal(InactiveExpr, OrigSaveGoalInfo),
 
-    set.list_to_set([RecordVar | HeadVars], InactiveNonLocals),
+    set_of_var.list_to_set([RecordVar | HeadVars], InactiveNonLocals),
     OutputVars = list.map(project_var, NumberedOutputVars),
     InactiveInstmapDelta = instmap_delta_bind_vars(OutputVars),
 
@@ -1117,7 +1118,7 @@
     ],
 
     SwitchExpr = switch(StatusVar, cannot_fail, SwitchArms),
-    set.insert(StatusVar, InactiveNonLocals, SwitchNonLocals),
+    set_of_var.insert(StatusVar, InactiveNonLocals, SwitchNonLocals),
     goal_info_init_hide(SwitchNonLocals, InactiveInstmapDelta, Detism,
         purity_impure, Context, SwitchGoalInfo),
     SwitchGoal = hlds_goal(SwitchExpr, SwitchGoalInfo),
@@ -1330,7 +1331,7 @@
         RestoreAnswerGoal0 = hlds_goal(_, RestoreAnswerGoal0Info),
         RestoreAnswer0NonLocals =
             goal_info_get_nonlocals(RestoreAnswerGoal0Info),
-        set.insert_list([IoStateAssignFromVar, IoStateAssignToVar],
+        set_of_var.insert_list([IoStateAssignFromVar, IoStateAssignToVar],
             RestoreAnswer0NonLocals, RestoreAnswerNonLocals),
         instmap_delta_restrict(RestoreAnswerNonLocals,
             RestoreAnswerInstMapDelta0, RestoreAnswerInstMapDelta),
@@ -1361,7 +1362,7 @@
     ),
     CallSaveAnswerGoalExpr = conj(plain_conj, CallSaveAnswerGoalList),
     create_instmap_delta(CallSaveAnswerGoalList, CallSaveAnswerInstMapDelta0),
-    set.insert(TipVar, OrigNonLocals, CallSaveAnswerNonLocals),
+    set_of_var.insert(TipVar, OrigNonLocals, CallSaveAnswerNonLocals),
     instmap_delta_restrict(CallSaveAnswerNonLocals,
         CallSaveAnswerInstMapDelta0, CallSaveAnswerInstMapDelta),
     goal_info_init_hide(CallSaveAnswerNonLocals, CallSaveAnswerInstMapDelta,
@@ -1375,7 +1376,7 @@
         RestoreAnswerGoal, CallSaveAnswerGoal),
     create_instmap_delta([OccurredGoal, RestoreAnswerGoal,
         CallSaveAnswerGoal], GenIfNecInstMapDelta0),
-    set.insert(TipVar, OrigNonLocals, GenIfNecNonLocals),
+    set_of_var.insert(TipVar, OrigNonLocals, GenIfNecNonLocals),
     instmap_delta_restrict(GenIfNecNonLocals,
         GenIfNecInstMapDelta0, GenIfNecInstMapDelta),
     goal_info_init_hide(GenIfNecNonLocals, GenIfNecInstMapDelta, detism_det,
@@ -1385,7 +1386,7 @@
     CheckAndGenAnswerGoalExpr = conj(plain_conj, [LookupGoal, GenIfNecGoal]),
     create_instmap_delta([LookupGoal, GenIfNecGoal],
         CheckAndGenAnswerInstMapDelta0),
-    set.insert_list([TableVar, CounterVar, StartVar],
+    set_of_var.insert_list([TableVar, CounterVar, StartVar],
         OrigNonLocals, CheckAndGenAnswerNonLocals),
     instmap_delta_restrict(CheckAndGenAnswerNonLocals,
         CheckAndGenAnswerInstMapDelta0, CheckAndGenAnswerInstMapDelta),
@@ -1471,7 +1472,7 @@
     % the code generated by the tabling transformation does,
     % so we need to compute the nonlocals from the headvars rather
     % than getting it from the nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     Context = goal_info_get_context(OrigGoalInfo),
@@ -1491,7 +1492,8 @@
         SubgoalVar, Context, !VarSet, !VarTypes, !.TableInfo, SuspendGoal),
 
     MainExpr = conj(plain_conj, [OrigGoal | SaveAnswerGoals]),
-    set.insert_list([SubgoalVar, StatusVar], OrigNonLocals, MainNonLocals),
+    set_of_var.insert_list([SubgoalVar, StatusVar],
+        OrigNonLocals, MainNonLocals),
     create_instmap_delta([OrigGoal | SaveAnswerGoals], MainIMD0),
     instmap_delta_restrict(MainNonLocals, MainIMD0, MainIMD),
     goal_info_init_hide(MainNonLocals, MainIMD, detism_non, purity_impure,
@@ -1597,7 +1599,7 @@
     % the code generated by the tabling transformation does,
     % so we need to compute the nonlocals from the headvars rather
     % than getting it from the nonlocals field in the original goal.
-    set.list_to_set(HeadVars, OrigNonLocals),
+    set_of_var.list_to_set(HeadVars, OrigNonLocals),
     OrigGoal = hlds_goal(_, OrigGoalInfo),
     OrigInstMapDelta = goal_info_get_instmap_delta(OrigGoalInfo),
     Context = goal_info_get_context(OrigGoalInfo),
@@ -1752,7 +1754,7 @@
     table_attr_statistics::in, term.context::in,
     prog_var::in, string::in, string::in, list(foreign_arg)::in,
     list(var_mode_pos_method)::in, list(var_mode_pos_method)::in,
-    set(prog_var)::in, instmap_delta::in,
+    set_of_progvar::in, instmap_delta::in,
     vartypes::in, prog_varset::in, table_info::in, table_info::out,
     list(table_step_desc)::in, list(table_step_desc)::out) is det.
 
@@ -1791,7 +1793,7 @@
 
     MainGoalExpr = conj(plain_conj, [OrigGoal | SaveReturnAnswerGoals]),
     Detism = goal_info_get_determinism(OrigGoalInfo),
-    set.insert(GeneratorVar, OrigNonLocals, NonLocals),
+    set_of_var.insert(GeneratorVar, OrigNonLocals, NonLocals),
     goal_info_init(NonLocals, OrigInstMapDelta, Detism, purity_impure, Context,
         MainGoalInfo0),
     goal_info_add_feature(feature_hide_debug_event,
@@ -2062,7 +2064,7 @@
 
     GoalExpr = conj(plain_conj, LookupSetupGoals),
     Vars = list.map(project_var, NumberedVars),
-    set.list_to_set([StatusVar, TableTipVar | Vars], NonLocals),
+    set_of_var.list_to_set([StatusVar, TableTipVar | Vars], NonLocals),
     goal_info_init_hide(NonLocals,
         instmap_delta_bind_vars([TableTipVar, StatusVar]),
         detism_det, purity_impure, Context, GoalInfo),
@@ -2120,7 +2122,7 @@
 
     GoalExpr = conj(plain_conj, LookupSetupGoals),
     Vars = list.map(project_var, NumberedVars),
-    set.list_to_set([StatusVar, RecordVar | Vars], NonLocals),
+    set_of_var.list_to_set([StatusVar, RecordVar | Vars], NonLocals),
     goal_info_init_hide(NonLocals,
         instmap_delta_bind_vars([RecordVar, StatusVar]),
         detism_det, purity_impure, Context, GoalInfo),
@@ -2177,7 +2179,7 @@
 
     GoalExpr = conj(plain_conj, LookupSetupGoals),
     Vars = list.map(project_var, NumberedVars),
-    set.list_to_set([StatusVar, SubgoalVar | Vars], NonLocals),
+    set_of_var.list_to_set([StatusVar, SubgoalVar | Vars], NonLocals),
     goal_info_init_hide(NonLocals,
         instmap_delta_bind_vars([SubgoalVar, StatusVar]),
         detism_det, purity_impure, Context, GoalInfo),
@@ -2974,7 +2976,7 @@
         ModuleInfo, Context, ShortcutGoal),
 
     GoalExpr = conj(plain_conj, [ReturnAnswerBlocksGoal, ShortcutGoal]),
-    set.list_to_set([RecordVar | OutputVars], NonLocals),
+    set_of_var.list_to_set([RecordVar | OutputVars], NonLocals),
     goal_info_init_hide(NonLocals, OrigInstMapDelta, Detism, purity_semipure,
         Context, GoalInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo).
@@ -3048,7 +3050,7 @@
         ModuleInfo, Context, ReturnAllGoal),
     GoalExpr = conj(plain_conj, [ReturnAnswerBlocksGoal, ReturnAllGoal]),
 
-    set.list_to_set([SubgoalVar | OutputVars], NonLocals),
+    set_of_var.list_to_set([SubgoalVar | OutputVars], NonLocals),
     goal_info_init_hide(NonLocals, OrigInstMapDelta, Detism, Purity,
         Context, GoalInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo).
@@ -3138,8 +3140,8 @@
         purity_pure, instmap_delta_bind_no_var, ModuleInfo, Context, CallGoal),
 
     GoalExpr = conj(plain_conj, [MessageStrGoal, CallGoal]),
-    goal_info_init_hide(set.init, instmap_delta_bind_no_var, detism_erroneous,
-        purity_impure, Context, GoalInfo),
+    goal_info_init_hide(set_of_var.init, instmap_delta_bind_no_var,
+        detism_erroneous, purity_impure, Context, GoalInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
 %-----------------------------------------------------------------------------%
@@ -3473,7 +3475,7 @@
 var_belong_to_list(List, var_mode_pos_method(Var, _, _, _)) :-
     list.member(Var, List).
 
-:- pred goal_info_init_hide(set(prog_var)::in, instmap_delta::in,
+:- pred goal_info_init_hide(set_of_progvar::in, instmap_delta::in,
     determinism::in, purity::in, prog_context::in, hlds_goal_info::out)
     is det.
 
Index: compiler/term_constr_build.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_build.m,v
retrieving revision 1.32
diff -u -b -r1.32 term_constr_build.m
--- compiler/term_constr_build.m	23 May 2011 05:08:13 -0000	1.32
+++ compiler/term_constr_build.m	15 Aug 2011 12:53:43 -0000
@@ -72,6 +72,7 @@
 :- import_module libs.rat.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module transform_hlds.dependency_graph.
 :- import_module transform_hlds.term_constr_data.
 :- import_module transform_hlds.term_constr_errors.
@@ -1022,8 +1023,8 @@
 local_vars(hlds_goal(GoalExpr, GoalInfo)) = Locals :-
     NonLocals = goal_info_get_nonlocals(GoalInfo),
     QuantVars = free_goal_vars(hlds_goal(GoalExpr, GoalInfo)),
-    LocalsSet = set.difference(QuantVars, NonLocals),
-    Locals = set.to_sorted_list(LocalsSet).
+    LocalsSet = set_of_var.difference(QuantVars, NonLocals),
+    Locals = set_of_var.to_sorted_list(LocalsSet).
 
     % Partition the variables of a goal into a set of local variables
     % and a set of non-local variables.
@@ -1033,8 +1034,9 @@
 partition_vars(hlds_goal(GoalExpr, GoalInfo), Locals, NonLocals) :-
     NonLocals0 = goal_info_get_nonlocals(GoalInfo),
     QuantVars = free_goal_vars(hlds_goal(GoalExpr, GoalInfo)),
-    Locals = set.to_sorted_list(set.difference(QuantVars, NonLocals0)),
-    NonLocals = set.to_sorted_list(NonLocals0).
+    Locals = set_of_var.to_sorted_list(
+        set_of_var.difference(QuantVars, NonLocals0)),
+    NonLocals = set_of_var.to_sorted_list(NonLocals0).
 
 %-----------------------------------------------------------------------------%
 %
@@ -1062,7 +1064,7 @@
 
 fill_var_to_sizevar_map(Goal, !SizeVarset, SizeVarMap) :-
     ProgVarsInGoal = free_goal_vars(Goal),
-    ProgVars = set.to_sorted_list(ProgVarsInGoal),
+    ProgVars = set_of_var.to_sorted_list(ProgVarsInGoal),
     make_size_var_map(ProgVars, !SizeVarset, SizeVarMap).
 
     % Fix the map in case some variables that are present only
@@ -1116,7 +1118,7 @@
         AbstractGoal = AbstractGoal0
     ;
         NonLocalProgVars0 = goal_info_get_nonlocals(Goal ^ hlds_goal_info),
-        NonLocalProgVars = set.to_sorted_list(NonLocalProgVars0),
+        NonLocalProgVars = set_of_var.to_sorted_list(NonLocalProgVars0),
         NonLocalSizeVars = prog_vars_to_size_vars(Info ^ tti_size_var_map,
             NonLocalProgVars),
         Constraints = make_arg_constraints(NonLocalSizeVars,
Index: compiler/trace_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/trace_gen.m,v
retrieving revision 1.39
diff -u -b -r1.39 trace_gen.m
--- compiler/trace_gen.m	21 Jul 2011 06:58:27 -0000	1.39
+++ compiler/trace_gen.m	15 Aug 2011 15:35:53 -0000
@@ -57,6 +57,7 @@
 :- import_module ll_backend.llds.
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module map.
@@ -136,7 +137,7 @@
     % layouts), and those of dummy types.
     %
 :- pred trace_fail_vars(module_info::in, proc_info::in,
-    set(prog_var)::out) is det.
+    set_of_progvar::out) is det.
 
     % Figure out whether we need a slot for storing the value of maxfr
     % on entry, and record the result in the proc info.
@@ -295,7 +296,7 @@
         build_fail_vars(HeadVars, Insts, ArgInfos,
             ModuleInfo, VarTypes, FailVarsList)
     ->
-        set.list_to_set(FailVarsList, FailVars)
+        set_of_var.list_to_set(FailVarsList, FailVars)
     ;
         unexpected($module, $pred, "length mismatch")
     ).
Index: compiler/try_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/try_expand.m,v
retrieving revision 1.17
diff -u -b -r1.17 try_expand.m
--- compiler/try_expand.m	23 May 2011 05:08:14 -0000	1.17
+++ compiler/try_expand.m	15 Aug 2011 08:38:18 -0000
@@ -230,6 +230,7 @@
 :- import_module parse_tree.
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_mode.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module map.
@@ -501,7 +502,8 @@
         GoalOutputVarsSet0),
     (
         MaybeIO = yes(try_io_state_vars(_IOStateVarInitial, IOStateVarFinal)),
-        set.delete(IOStateVarFinal, GoalOutputVarsSet0, GoalOutputVarsSet)
+        set_of_var.delete(IOStateVarFinal,
+            GoalOutputVarsSet0, GoalOutputVarsSet)
     ;
         MaybeIO = no,
         GoalOutputVarsSet = GoalOutputVarsSet0
@@ -517,7 +519,7 @@
 
 :- pred expand_try_goal_2(maybe(try_io_state_vars)::in, prog_var::in,
     hlds_goal::in, hlds_goal::in, maybe(hlds_goal)::in, hlds_goal::in,
-    instmap::in, set(prog_var)::in, hlds_goal::out,
+    instmap::in, set_of_progvar::in, hlds_goal::out,
     pred_info::in, pred_info::out, proc_info::in, proc_info::out,
     module_info::in, module_info::out) is det.
 
@@ -527,7 +529,7 @@
     some [!VarTypes] (
         % Get the type of the output tuple.
         proc_info_get_vartypes(!.ProcInfo, !:VarTypes),
-        GoalOutputVars = set.to_sorted_list(GoalOutputVarsSet),
+        GoalOutputVars = set_of_var.to_sorted_list(GoalOutputVarsSet),
         map.apply_to_list(GoalOutputVars, !.VarTypes, GoalOutputVarTypes),
         OutputTupleType = tuple_type(GoalOutputVarTypes, kind_star),
 
@@ -782,16 +784,17 @@
     ).
 
 :- pred bound_nonlocals_in_goal(module_info::in, instmap::in, hlds_goal::in,
-    set(prog_var)::out) is det.
+    set_of_progvar::out) is det.
 
 bound_nonlocals_in_goal(ModuleInfo, InstMap, Goal, BoundNonLocals) :-
     Goal = hlds_goal(_, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
     InstmapDelta = goal_info_get_instmap_delta(GoalInfo),
-    BoundNonLocals = set.filter(var_is_bound_in_instmap_delta(ModuleInfo,
-        InstMap, InstmapDelta), NonLocals).
+    BoundNonLocals = set_of_var.filter(
+        var_is_bound_in_instmap_delta(ModuleInfo, InstMap, InstmapDelta),
+        NonLocals).
 
-:- pred make_try_lambda(hlds_goal::in, set(prog_var)::in, mer_type::in,
+:- pred make_try_lambda(hlds_goal::in, set_of_progvar::in, mer_type::in,
     maybe(try_io_state_vars)::in, prog_var::out, hlds_goal::out,
     proc_info::in, proc_info::out) is det.
 
@@ -799,7 +802,7 @@
         LambdaVar, AssignLambdaVar, !ProcInfo) :-
     Body0 = hlds_goal(_, BodyInfo0),
     NonLocals0 = goal_info_get_nonlocals(BodyInfo0),
-    set.difference(NonLocals0, OutputVarsSet, NonLocals1),
+    set_of_var.difference(NonLocals0, OutputVarsSet, NonLocals1),
 
     proc_info_create_var_from_type(OutputTupleType, yes("OutputTuple"),
         OutputTupleVar, !ProcInfo),
@@ -808,7 +811,7 @@
         LambdaParams = [OutputTupleVar, IOVarInitial, IOVarFinal],
         LambdaParamTypes = [OutputTupleType, io_state_type, io_state_type],
         LambdaParamModes = [out_mode, di_mode, uo_mode],
-        set.delete(IOVarFinal, NonLocals1, NonLocals)
+        set_of_var.delete(IOVarFinal, NonLocals1, NonLocals)
     ;
         MaybeIO = no,
         LambdaParams = [OutputTupleVar],
@@ -822,15 +825,16 @@
         !ProcInfo),
 
     % Add the construction of OutputTuple to the body.
-    construct_tuple(OutputTupleVar, set.to_sorted_list(OutputVarsSet),
+    construct_tuple(OutputTupleVar, set_of_var.to_sorted_list(OutputVarsSet),
         MakeOutputTuple),
     conjoin_goals(Body0, MakeOutputTuple, LambdaBody0),
 
     % Rename away output variables in the lambda body.
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
-    clone_variables(set.to_sorted_list(OutputVarsSet), VarSet0, VarTypes0,
-        VarSet0, VarSet, VarTypes0, VarTypes, map.init, Renaming),
+    clone_variables(set_of_var.to_sorted_list(OutputVarsSet),
+        VarSet0, VarTypes0, VarSet0, VarSet, VarTypes0, VarTypes,
+        map.init, Renaming),
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo),
     rename_some_vars_in_goal(Renaming, LambdaBody0, LambdaBody),
@@ -842,7 +846,7 @@
 
     % Make the lambda assignment.
     RHS = rhs_lambda_goal(purity_pure, ho_ground, pf_predicate,
-        lambda_normal, set.to_sorted_list(NonLocals),
+        lambda_normal, set_of_var.to_sorted_list(NonLocals),
         LambdaParams, LambdaParamModes, LambdaDetism, LambdaBody),
     create_pure_atomic_complicated_unification(LambdaVar, RHS,
         term.context_init, umc_implicit("try_expand"), [], AssignLambdaVar).
Index: compiler/typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck.m,v
retrieving revision 1.459
diff -u -b -r1.459 typecheck.m
--- compiler/typecheck.m	23 May 2011 05:08:14 -0000	1.459
+++ compiler/typecheck.m	15 Aug 2011 08:54:01 -0000
@@ -129,6 +129,7 @@
 :- import_module parse_tree.prog_type.
 :- import_module parse_tree.prog_type_subst.
 :- import_module parse_tree.prog_util.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module int.
@@ -1029,7 +1030,7 @@
         create_pure_atomic_complicated_unification(FuncRetVal,
             FuncRHS, Context, umc_explicit, [], Goal0),
         Goal0 = hlds_goal(GoalExpr, GoalInfo0),
-        NonLocals = proc_arg_vector_to_set(HeadVars),
+        NonLocals = set_of_var.list_to_set(proc_arg_vector_to_list(HeadVars)),
         goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo),
         Goal = hlds_goal(GoalExpr, GoalInfo),
         Clause = clause(all_modes, Goal, impl_lang_mercury, Context, []),
Index: compiler/unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unify_gen.m,v
retrieving revision 1.207
diff -u -b -r1.207 unify_gen.m
--- compiler/unify_gen.m	21 Jul 2011 06:58:27 -0000	1.207
+++ compiler/unify_gen.m	15 Aug 2011 13:19:55 -0000
@@ -1581,7 +1581,7 @@
 generate_ground_term(TermVar, Goal, !CI) :-
     Goal = hlds_goal(GoalExpr, GoalInfo),
     NonLocals = goal_info_get_nonlocals(GoalInfo),
-    set.to_sorted_list(NonLocals, NonLocalList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalList),
     (
         NonLocalList = []
         % The term being constructed by the scope is not needed, so there is
@@ -1600,7 +1600,7 @@
                     ActiveMap0, ActiveMap),
                 map.to_assoc_list(ActiveMap, ActivePairs),
                 ( ActivePairs = [TermVar - GroundTerm] ->
-                    add_forward_live_vars(set_to_bitset(NonLocals), !CI),
+                    add_forward_live_vars(NonLocals, !CI),
                     set_static_cell_info(StaticCellInfo, !CI),
                     GroundTerm = Rval - _,
                     assign_const_to_var(TermVar, Rval, !CI)
Index: compiler/unify_proc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unify_proc.m,v
retrieving revision 1.222
diff -u -b -r1.222 unify_proc.m
--- compiler/unify_proc.m	5 Jul 2011 03:34:34 -0000	1.222
+++ compiler/unify_proc.m	15 Aug 2011 08:56:51 -0000
@@ -150,6 +150,7 @@
 :- import_module parse_tree.builtin_lib_types.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.
+:- import_module parse_tree.set_of_var.
 :- import_module recompilation.
 
 :- import_module bool.
@@ -1824,7 +1825,7 @@
     ->
         GoalExpr = plain_call(PredId, ProcId, ArgVars, not_builtin, no,
             PredName),
-        set.list_to_set(ArgVars, NonLocals),
+        set_of_var.list_to_set(ArgVars, NonLocals),
         goal_info_init(NonLocals, InstmapDelta, Detism, purity_pure,
             GoalInfo0),
         goal_info_set_context(Context, GoalInfo0, GoalInfo),
Index: compiler/unique_modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unique_modes.m,v
retrieving revision 1.137
diff -u -b -r1.137 unique_modes.m
--- compiler/unique_modes.m	23 May 2011 05:08:14 -0000	1.137
+++ compiler/unique_modes.m	15 Aug 2011 14:01:30 -0000
@@ -42,10 +42,10 @@
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module bool.
 :- import_module list.
-:- import_module set.
 
 %-----------------------------------------------------------------------------%
 
@@ -67,8 +67,8 @@
 
     % Prepare for checking a disjunct in a disjunction.
     %
-:- pred prepare_for_disjunct(hlds_goal::in, determinism::in, set(prog_var)::in,
-    mode_info::in, mode_info::out) is det.
+:- pred prepare_for_disjunct(hlds_goal::in, determinism::in,
+    set_of_progvar::in, mode_info::in, mode_info::out) is det.
 
     % Make all nondet-live variables whose current inst
     % is `unique' become `mostly_unique'.
@@ -427,7 +427,7 @@
     %   ).
 
     mode_info_add_live_vars(ElseVars, !ModeInfo),
-    set.to_sorted_list(CondVars, CondVarList),
+    set_of_var.to_sorted_list(CondVars, CondVarList),
     select_live_vars(CondVarList, !.ModeInfo, CondLiveVars),
     Cond0 = hlds_goal(_, CondInfo0),
     CondDeltaInstMap0 = goal_info_get_instmap_delta(CondInfo0),
@@ -473,7 +473,7 @@
     % then the negation will succeed, and so these variables can be accessed
     % again after backtracking.
     NonLocals = goal_info_get_nonlocals(GoalInfo0),
-    set.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
     select_live_vars(NonLocalsList, !.ModeInfo, LiveNonLocals),
     make_var_list_mostly_uniq(LiveNonLocals, !ModeInfo),
 
@@ -715,7 +715,7 @@
     (
         ModeErrors = [_ | _],
         % mode error in callee for this mode
-        WaitingVars = set.list_to_set(ArgVars),
+        WaitingVars = set_of_var.list_to_set(ArgVars),
         mode_info_get_instmap(!.ModeInfo, InstMap),
         instmap_lookup_vars(InstMap, ArgVars, ArgInsts),
         mode_info_error(WaitingVars,
@@ -868,7 +868,7 @@
 make_par_conj_nonlocal_multiset([Goal | Goals], NonLocalsMultiSet) :-
     make_par_conj_nonlocal_multiset(Goals, NonLocalsMultiSet0),
     NonLocals = goal_get_nonlocals(Goal),
-    set.to_sorted_list(NonLocals, NonLocalsList),
+    set_of_var.to_sorted_list(NonLocals, NonLocalsList),
     bag.from_list(NonLocalsList, NonLocalsMultiSet1),
     bag.union(NonLocalsMultiSet0, NonLocalsMultiSet1, NonLocalsMultiSet).
 
@@ -882,7 +882,7 @@
     % which we do not allow.
     %
 :- pred unique_modes_check_par_conj(list(hlds_goal)::in, bag(prog_var)::in,
-    list(hlds_goal)::out, list(pair(instmap, set(prog_var)))::out,
+    list(hlds_goal)::out, list(pair(instmap, set_of_progvar))::out,
     mode_info::in, mode_info::out) is det.
 
 unique_modes_check_par_conj(Goals0, NonLocalVarsBag, Goals, Instmaps,
@@ -917,7 +917,7 @@
     % multiple parallel conjuncts, so we don't need to lock/unlock variables.
     %
 :- pred unique_modes_check_par_conj_1(list(hlds_goal)::in,
-    list(hlds_goal)::out, list(pair(instmap, set(prog_var)))::out,
+    list(hlds_goal)::out, list(pair(instmap, set_of_progvar))::out,
     mode_info::in, mode_info::out) is det.
 
 unique_modes_check_par_conj_1([], [], [], !ModeInfo).
@@ -937,7 +937,7 @@
     % of the resulting instmaps.
     %
 :- pred unique_modes_check_disj(list(hlds_goal)::in, determinism::in,
-    set(prog_var)::in, list(hlds_goal)::out, list(instmap)::out,
+    set_of_progvar::in, list(hlds_goal)::out, list(instmap)::out,
     mode_info::in, mode_info::out) is det.
 
 unique_modes_check_disj([], _, _, [], [], !ModeInfo).
Index: compiler/unneeded_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unneeded_code.m,v
retrieving revision 1.62
diff -u -b -r1.62 unneeded_code.m
--- compiler/unneeded_code.m	23 May 2011 05:08:15 -0000	1.62
+++ compiler/unneeded_code.m	15 Aug 2011 11:12:46 -0000
@@ -87,6 +87,7 @@
 :- import_module libs.options.
 :- import_module mdbcomp.goal_path.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -477,7 +478,7 @@
     VarTypes = UnneededInfo ^ uci_vartypes,
     instmap_changed_vars(InitInstMap, FinalInstMap, VarTypes, ModuleInfo,
         ChangedVarSet),
-    set.to_sorted_list(ChangedVarSet, ChangedVars),
+    set_of_var.to_sorted_list(ChangedVarSet, ChangedVars),
     map.init(Empty),
     !:WhereInfo = branches(Empty),
     Goal = hlds_goal(_, GoalInfo),
@@ -582,7 +583,7 @@
     Goal = hlds_goal(_, GoalInfo),
     NonLocalSet = goal_info_get_nonlocals(GoalInfo),
     GoalId = goal_info_get_goal_id(GoalInfo),
-    set.to_sorted_list(NonLocalSet, NonLocals),
+    set_of_var.to_sorted_list(NonLocalSet, NonLocals),
     ModuleInfo = UnneededInfo ^ uci_module_info,
     list.filter(nonlocal_may_be_input(ModuleInfo, InitInstMap), NonLocals,
         Inputs),
@@ -605,7 +606,7 @@
 undemand_virgin_outputs(Goal, ModuleInfo, InstMap, !WhereNeededMap) :-
     Goal = hlds_goal(_, GoalInfo),
     NonLocalSet = goal_info_get_nonlocals(GoalInfo),
-    set.to_sorted_list(NonLocalSet, NonLocals),
+    set_of_var.to_sorted_list(NonLocalSet, NonLocals),
     list.filter(nonlocal_is_virgin_output(ModuleInfo, InstMap), NonLocals,
         VirginOutputs),
     list.foldl(undemand_var, VirginOutputs, !WhereNeededMap).
Index: compiler/var_locn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/var_locn.m,v
retrieving revision 1.78
diff -u -b -r1.78 var_locn.m
--- compiler/var_locn.m	5 Jul 2011 03:34:34 -0000	1.78
+++ compiler/var_locn.m	15 Aug 2011 15:52:22 -0000
@@ -27,6 +27,7 @@
 :- import_module ll_backend.global_data.
 :- import_module ll_backend.llds.
 :- import_module parse_tree.prog_data.
+:- import_module parse_tree.set_of_var.
 
 :- import_module assoc_list.
 :- import_module bool.
@@ -55,7 +56,7 @@
     % follow_vars set; such sets give guidance as to what lvals
     % (if any) each variable will be needed in next.
     %
-:- pred init_var_locn_state(assoc_list(prog_var, lval)::in, set(prog_var)::in,
+:- pred init_var_locn_state(assoc_list(prog_var, lval)::in, set_of_progvar::in,
     prog_varset::in, vartypes::in, stack_slots::in, abs_follow_vars::in,
     var_locn_info::out) is det.
 
@@ -439,7 +440,7 @@
                 expr_rval       :: maybe(rval),
 
                 % The set of vars whose expr_rval field refers to this var.
-                using_vars      :: set(prog_var),
+                using_vars      :: set_of_progvar,
 
                 % A dead variable should be removed from var_state_map
                 % when its using_vars field becomes empty.
@@ -461,7 +462,7 @@
     % their deletion from the map, makes it simpler to manipulate
     % loc_var_maps using higher-order code.
 
-:- type loc_var_map ==  map(lval, set(prog_var)).
+:- type loc_var_map ==  map(lval, set_of_progvar).
 
 :- type var_locn_info
     --->    var_locn_info(
@@ -527,7 +528,7 @@
         AcquiredRegs, 0, []).
 
 :- pred init_var_locn_state_2(assoc_list(prog_var, lval)::in,
-    maybe(set(prog_var))::in, var_state_map::in, var_state_map::out,
+    maybe(set_of_progvar)::in, var_state_map::in, var_state_map::out,
     loc_var_map::in, loc_var_map::out) is det.
 
 init_var_locn_state_2([], _, !VarStateMap, !LocVarMap).
@@ -536,7 +537,7 @@
     expect(is_root_lval(Lval), $module, $pred, "unexpected lval"),
     (
         MaybeLiveness = yes(Liveness),
-        \+ set.member(Var, Liveness)
+        \+ set_of_var.member(Liveness, Var)
     ->
         % If a variable is not live, then we do not record its
         % state. If we did, then the variable will never die
@@ -549,8 +550,8 @@
         ( map.search(!.VarStateMap, Var, _) ->
             unexpected($module, $pred, "repeated variable")
         ;
-            set.singleton_set(NewLocs, Lval),
-            set.init(Using),
+            NewLocs = set.make_singleton_set(Lval),
+            set_of_var.init(Using),
             State = var_state(NewLocs, no, no, Using, doa_alive),
             map.det_insert(Var, State, !VarStateMap)
         ),
@@ -609,9 +610,9 @@
         map.search(!.LocVarMap, Lval, DependentVarsSet)
     ->
         map.delete(Lval, !LocVarMap),
-        set.to_sorted_list(DependentVarsSet, DependentVars),
-        list.foldl(clobber_lval_in_var_state_map(Lval, [], OkToDeleteAny),
-            DependentVars, !VarStateMap)
+        set_of_var.fold(
+            clobber_lval_in_var_state_map(Lval, [], OkToDeleteAny),
+            DependentVarsSet, !VarStateMap)
     ;
         true
     ),
@@ -655,7 +656,7 @@
         OkToDeleteAny = yes
     ;
         DeadOrAlive = doa_dead,
-        set.to_sorted_list(Using, UsingVars),
+        set_of_var.to_sorted_list(Using, UsingVars),
         recursive_using_vars_dead_and_ok_to_delete(UsingVars,
             !.VarStateMap, OkToDeleteVars)
     ),
@@ -673,7 +674,7 @@
         map.lookup(VarStateMap, Var, State),
         State = var_state(_, _, _, Using, DeadOrAlive),
         DeadOrAlive = doa_dead,
-        set.to_sorted_list(Using, UsingVars),
+        set_of_var.to_sorted_list(Using, UsingVars),
         recursive_using_vars_dead_and_ok_to_delete(UsingVars,
             VarStateMap, OkToDeleteVars)
     ),
@@ -690,16 +691,16 @@
         Using0, DeadOrAlive),
     (
         MaybeExprRval = yes(_),
-        State = var_state(Lvals, MaybeConstRval, yes(var(OldVar)), set.init,
-            doa_alive),
-        set.insert(Var, Using0, Using),
-        OldState = var_state(Lvals, MaybeConstRval, MaybeExprRval, Using,
-            DeadOrAlive),
+        State = var_state(Lvals, MaybeConstRval, yes(var(OldVar)),
+            set_of_var.init, doa_alive),
+        set_of_var.insert(Var, Using0, Using),
+        OldState = var_state(Lvals, MaybeConstRval, MaybeExprRval,
+            Using, DeadOrAlive),
         map.det_update(OldVar, OldState, VarStateMap0, VarStateMap1)
     ;
         MaybeExprRval = no,
-        set.init(Empty),
-        State = var_state(Lvals, MaybeConstRval, no, Empty, doa_alive),
+        State = var_state(Lvals, MaybeConstRval, no, set_of_var.init,
+            doa_alive),
         VarStateMap1 = VarStateMap0
     ),
     map.det_insert(Var, State, VarStateMap1, VarStateMap),
@@ -732,8 +733,8 @@
             MaybeConstRval = yes(SelectedArgRval),
             Lvals = set.map(add_field_offset(yes(Ptag),
                 const(llconst_int(Offset))), BaseVarLvals),
-            set.init(Using),
-            State = var_state(Lvals, MaybeConstRval, no, Using, doa_alive),
+            State = var_state(Lvals, MaybeConstRval, no, set_of_var.init,
+                doa_alive),
             map.det_insert(Var, State, VarStateMap0, VarStateMap),
             var_locn_set_var_state_map(VarStateMap, !VLI),
 
@@ -743,8 +744,8 @@
         ;
             set.init(Lvals),
             Expr = lval(Lval0),
-            set.init(Using),
-            State = var_state(Lvals, no, yes(Expr), Using, doa_alive),
+            State = var_state(Lvals, no, yes(Expr), set_of_var.init,
+                doa_alive),
             map.det_insert(Var, State, VarStateMap0, VarStateMap1),
             add_use_ref(BaseVar, Var, VarStateMap1, VarStateMap),
             var_locn_set_var_state_map(VarStateMap, !VLI)
@@ -754,8 +755,8 @@
         var_locn_materialize_vars_in_lval(ModuleInfo, Lval0, Lval, Code, !VLI),
 
         var_locn_get_var_state_map(!.VLI, VarStateMap0),
-        set.singleton_set(LvalSet, Lval),
-        State = var_state(LvalSet, no, no, set.init, doa_alive),
+        LvalSet = set.make_singleton_set(Lval),
+        State = var_state(LvalSet, no, no, set_of_var.init, doa_alive),
         map.det_insert(Var, State, VarStateMap0, VarStateMap),
         var_locn_set_var_state_map(VarStateMap, !VLI),
 
@@ -773,9 +774,7 @@
     check_var_is_unknown(!.VLI, Var),
     ( Lval = field(yes(_Ptag), var(BaseVar), const(llconst_int(_Offset))) ->
         var_locn_get_var_state_map(!.VLI, VarStateMap0),
-        set.init(Lvals),
-        set.init(Using),
-        State = var_state(Lvals, no, yes(Expr), Using, doa_alive),
+        State = var_state(set.init, no, yes(Expr), set_of_var.init, doa_alive),
         map.det_insert(Var, State, VarStateMap0, VarStateMap1),
         add_use_ref(BaseVar, Var, VarStateMap1, VarStateMap),
         var_locn_set_var_state_map(VarStateMap, !VLI),
@@ -791,7 +790,8 @@
 
     var_locn_get_var_state_map(!.VLI, VarStateMap0),
     ( expr_is_constant(VarStateMap0, ExprnOpts, ConstRval0, ConstRval) ->
-        State = var_state(set.init, yes(ConstRval), no, set.init, doa_alive),
+        State = var_state(set.init, yes(ConstRval), no, set_of_var.init,
+            doa_alive),
         map.det_insert(Var, State, VarStateMap0, VarStateMap),
         var_locn_set_var_state_map(VarStateMap, !VLI)
     ;
@@ -804,7 +804,7 @@
     check_var_is_unknown(!.VLI, Var),
 
     var_locn_get_var_state_map(!.VLI, VarStateMap0),
-    State = var_state(set.init, no, yes(Rval), set.init, doa_alive),
+    State = var_state(set.init, no, yes(Rval), set_of_var.init, doa_alive),
     map.det_insert(Var, State, VarStateMap0, VarStateMap1),
 
     exprn_aux.vars_in_rval(Rval, ContainedVars0),
@@ -827,7 +827,7 @@
     map.lookup(!.VarStateMap, ContainedVar, State0),
     State0 = var_state(Lvals, MaybeConstRval, MaybeExprRval, Using0,
         DeadOrAlive),
-    set.insert(UsingVar, Using0, Using),
+    set_of_var.insert(UsingVar, Using0, Using),
     State = var_state(Lvals, MaybeConstRval, MaybeExprRval, Using,
         DeadOrAlive),
     map.det_update(ContainedVar, State, !VarStateMap).
@@ -1156,7 +1156,7 @@
     var_locn_get_var_state_map(!.VLI, VarStateMap),
     map.lookup(VarStateMap, ReuseVar, ReuseVarState0),
     DepVarsSet = ReuseVarState0 ^ using_vars,
-    DepVars = set.to_sorted_list(DepVarsSet),
+    DepVars = set_of_var.to_sorted_list(DepVarsSet),
     list.map_foldl2(var_locn_save_cell_fields_2(ModuleInfo, ReuseLval),
         DepVars, SaveArgsCode, [], Regs, !VLI),
     Code = cord_list_to_cord(SaveArgsCode).
@@ -1248,7 +1248,7 @@
     map.lookup(VarStateMap0, ContainedVar, State0),
     State0 = var_state(Lvals, MaybeConstRval, MaybeExprRval, Using0,
         DeadOrAlive),
-    ( set.remove(UsingVar, Using0, Using1) ->
+    ( set_of_var.remove(UsingVar, Using0, Using1) ->
         Using = Using1
     ;
         unexpected($module, $pred, "using ref not present")
@@ -1258,7 +1258,7 @@
     map.det_update(ContainedVar, State, VarStateMap0, VarStateMap),
     var_locn_set_var_state_map(VarStateMap, !VLI),
     (
-        set.empty(Using),
+        set_of_var.is_empty(Using),
         DeadOrAlive = doa_dead
     ->
         var_locn_var_becomes_dead(ContainedVar, no, !VLI)
@@ -1282,8 +1282,8 @@
     var_locn_set_loc_var_map(LocVarMap, !VLI),
 
     var_locn_get_var_state_map(!.VLI, VarStateMap0),
-    set.singleton_set(LvalSet, Lval),
-    State = var_state(LvalSet, no, no, set.init, doa_alive),
+    LvalSet = set.make_singleton_set(Lval),
+    State = var_state(LvalSet, no, no, set_of_var.init, doa_alive),
     map.det_insert(Var, State, VarStateMap0, VarStateMap),
     var_locn_set_var_state_map(VarStateMap, !VLI).
 
@@ -1471,7 +1471,7 @@
 record_clobbering(Target, Assigns, !VLI) :-
     var_locn_get_loc_var_map(!.VLI, LocVarMap1),
     ( map.search(LocVarMap1, Target, DependentVarsSet) ->
-        set.to_sorted_list(DependentVarsSet, DependentVars),
+        set_of_var.to_sorted_list(DependentVarsSet, DependentVars),
         map.delete(Target, LocVarMap1, LocVarMap),
         var_locn_set_loc_var_map(LocVarMap, !VLI),
 
@@ -1508,7 +1508,7 @@
     (
         var_locn_get_loc_var_map(!.VLI, LocVarMap0),
         map.search(LocVarMap0, Lval, AffectedVarSet),
-        set.to_sorted_list(AffectedVarSet, AffectedVars),
+        set_of_var.to_sorted_list(AffectedVarSet, AffectedVars),
         var_locn_get_var_state_map(!.VLI, VarStateMap0),
         \+ list.foldl(
             try_clobber_lval_in_var_state_map(Lval, ToBeAssignedVars, no),
@@ -1546,8 +1546,9 @@
     (
         var_locn_get_loc_var_map(!.VLI, LocVarMap0),
         map.search(LocVarMap0, Lval, AffectedVarSet),
-        set.delete_list(ToBeAssignedVars, AffectedVarSet, EffAffectedVarSet),
-        set.to_sorted_list(EffAffectedVarSet, EffAffectedVars),
+        set_of_var.delete_list(ToBeAssignedVars, AffectedVarSet,
+            EffAffectedVarSet),
+        set_of_var.to_sorted_list(EffAffectedVarSet, EffAffectedVars),
 
         var_locn_get_var_state_map(!.VLI, VarStateMap0),
         (
@@ -1674,15 +1675,14 @@
     % Convert the list of root lvals to the list of sets of affected vars;
     % if a root lval is not in LocVarMap0, then it does not affect any
     % variables.
-    list.filter_map(map.search(LocVarMap0), NoDupRootLvals,
-        AffectedVarSets),
+    list.filter_map(map.search(LocVarMap0), NoDupRootLvals, AffectedVarSets),
 
     % Take the union of the list of sets of affected vars.
-    list.foldl(set.union, AffectedVarSets,
-        set.init, AffectedVarSet),
+    list.foldl(set_of_var.union, AffectedVarSets,
+        set_of_var.init, AffectedVarSet),
 
     % Convert the union set to a list of affected vars.
-    set.to_sorted_list(AffectedVarSet, AffectedVars),
+    set_of_var.to_sorted_list(AffectedVarSet, AffectedVars),
     list.foldl2(record_copy_for_var(Old, New), AffectedVars,
         VarStateMap0, VarStateMap, LocVarMap0, LocVarMap),
     var_locn_set_loc_var_map(LocVarMap, !VLI),
@@ -1787,7 +1787,7 @@
         ;
             DeadOrAlive0 = doa_alive
         ),
-        ( set.empty(Using) ->
+        ( set_of_var.is_empty(Using) ->
             map.det_remove(Var, _, VarStateMap0, VarStateMap),
             var_locn_set_var_state_map(VarStateMap, !VLI),
 
@@ -2030,7 +2030,7 @@
     var_locn_get_locked(VLI, Locked),
     (
         map.search(LocVarMap, Lval, UsingVars),
-        \+ set.empty(UsingVars)
+        set_of_var.is_non_empty(UsingVars)
     ;
         set.member(Lval, Acquired)
     ;
@@ -2360,7 +2360,7 @@
         Avoid, Rval0, ExprCode, !VLI),
     (
         StoreIfReq = yes,
-        set.count(UsingVars, NumUsingVars),
+        NumUsingVars = set_of_var.count(UsingVars),
         NumUsingVars > 1
     ->
         select_preferred_reg_avoid(!.VLI, Var, Avoid, Lval),
@@ -2398,10 +2398,10 @@
 make_var_depend_on_root_lval(Var, Lval, !LocVarMap) :-
     expect(is_root_lval(Lval), $module, $pred, "non-root lval"),
     ( map.search(!.LocVarMap, Lval, Vars0) ->
-        set.insert(Var, Vars0, Vars),
+        set_of_var.insert(Var, Vars0, Vars),
         map.det_update(Lval, Vars, !LocVarMap)
     ;
-        set.singleton_set(Vars, Var),
+        Vars = set_of_var.make_singleton(Var),
         map.det_insert(Lval, Vars, !LocVarMap)
     ).
 
@@ -2414,8 +2414,8 @@
 make_var_not_depend_on_root_lval(Var, Lval, !LocVarMap) :-
     expect(is_root_lval(Lval), $module, $pred, "non-root lval"),
     ( map.search(!.LocVarMap, Lval, Vars0) ->
-        set.delete(Var, Vars0, Vars),
-        ( set.empty(Vars) ->
+        set_of_var.delete(Var, Vars0, Vars),
+        ( set_of_var.is_empty(Vars) ->
             map.det_remove(Lval, _, !LocVarMap)
         ;
             map.det_update(Lval, Vars, !LocVarMap)
cvs diff: Diffing compiler/notes
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/base64
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/fixed
cvs diff: Diffing extras/gator
cvs diff: Diffing extras/gator/generations
cvs diff: Diffing extras/gator/generations/1
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_allegro
cvs diff: Diffing extras/graphics/mercury_allegro/examples
cvs diff: Diffing extras/graphics/mercury_allegro/samples
cvs diff: Diffing extras/graphics/mercury_allegro/samples/demo
cvs diff: Diffing extras/graphics/mercury_allegro/samples/mandel
cvs diff: Diffing extras/graphics/mercury_allegro/samples/pendulum2
cvs diff: Diffing extras/graphics/mercury_allegro/samples/speed
cvs diff: Diffing extras/graphics/mercury_cairo
cvs diff: Diffing extras/graphics/mercury_cairo/samples
cvs diff: Diffing extras/graphics/mercury_cairo/samples/data
cvs diff: Diffing extras/graphics/mercury_cairo/tutorial
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/log4m
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/monte
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/mopenssl
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/net
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/posix/samples
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/windows_installer_generator
cvs diff: Diffing extras/windows_installer_generator/sample
cvs diff: Diffing extras/windows_installer_generator/sample/images
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
Index: library/bag.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/bag.m,v
retrieving revision 1.39
diff -u -b -r1.39 bag.m
--- library/bag.m	8 Jun 2011 04:19:58 -0000	1.39
+++ library/bag.m	15 Aug 2011 09:55:41 -0000
@@ -78,6 +78,11 @@
 :- func bag.from_set(set(T)) = bag(T).
 :- pred bag.from_set(set(T)::in, bag(T)::out) is det.
 
+    % Make a bag from a sorted list.
+    %
+:- func bag.from_sorted_list(list(T)) = bag(T).
+:- pred bag.from_sorted_list(list(T)::in, bag(T)::out) is det.
+
     % Given a bag, produce a sorted list containing all the values in
     % the bag.  Each value will appear in the list the same number of
     % times that it appears in the bag.
@@ -344,6 +349,14 @@
     % XXX We should exploit the sortedness of List.
     bag.insert_list(List, Bag0, Bag).
 
+bag.from_sorted_list(Xs) = B :-
+    bag.from_sorted_list(Xs, B).
+
+bag.from_sorted_list(List, Bag) :-
+    bag.init(Bag0),
+    % XXX We should exploit the sortedness of List.
+    bag.insert_list(List, Bag0, Bag).
+
 bag.to_list(B) = Xs :-
     bag.to_list(B, Xs).
 
Index: library/robdd.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/robdd.m,v
retrieving revision 1.15
diff -u -b -r1.15 robdd.m
--- library/robdd.m	3 May 2011 04:35:01 -0000	1.15
+++ library/robdd.m	15 Aug 2011 10:35:33 -0000
@@ -14,11 +14,11 @@
 % implementation of Reduced Ordered Binary Decision Diagrams (ROBDDs).
 % ROBDDs are an efficient representation for Boolean constraints.
 % 
-% Boolean variables are represented using the type var(T) from the
-% `term' library module (see the `term' module documentation for
-% more information).
+% Boolean variables are represented using the type var(T) from the `term'
+% library module (see the `term' module documentation for more information).
 % 
 % Example usage:
+%
 %   % Create some variables.
 %   term.init_var_supply(VarSupply0),
 %   term.create_var(VarSupply0, A, VarSupply1),
@@ -66,37 +66,46 @@
 
 :- func empty_vars_set = vars(T).
 
-% Constants.
+    % Constants.
+    %
 :- func one = robdd(T).
 :- func zero = robdd(T).
 
-% If-then-else.
+    % If-then-else.
+    %
 :- func ite(robdd(T), robdd(T), robdd(T)) = robdd(T).
 
 % The functions *, +, =<, =:=, =\= and ~ correspond to the names
 % used in the SICStus clp(B) library.
 
-% Conjunction.
+    % Conjunction.
+    %
 :- func robdd(T) * robdd(T) = robdd(T).
 
-% Disjunction.
+    % Disjunction.
+    %
 :- func robdd(T) + robdd(T) = robdd(T).
 
-% Implication.
+    % Implication.
+    %
 :- func (robdd(T) =< robdd(T)) = robdd(T).
 
-% Equivalence.
+    % Equivalence.
+    %
 :- func (robdd(T) =:= robdd(T)) = robdd(T).
 
-% Non-equivalence (XOR).
+    % Non-equivalence (XOR).
+    %
 :- func (robdd(T) =\= robdd(T)) = robdd(T).
 
-% Negation.
+    % Negation.
+    %
 :- func (~ robdd(T)) = robdd(T).
 
 %-----------------------------------------------------------------------------%
 
     % var(X) is the ROBDD that is true iff X is true.
+    %
 :- func var(var(T)) = robdd(T).
 
 % The following functions operate on individual variables and are
@@ -104,48 +113,62 @@
 % ROBDDs as input.
 
     % not_var(V) = ~ var(V).
+    %
 :- func not_var(var(T)) = robdd(T).
 
     % ite_var(V, FA, FB) = ite(var(V), FA, FB).
+    %
 :- func ite_var(var(T), robdd(T), robdd(T)) = robdd(T).
 
     % eq_vars(X, Y) = ( var(X) =:= var(Y) ).
+    %
 :- func eq_vars(var(T), var(T)) = robdd(T).
 
     % neq_vars(X, Y) = ( var(X) =\= var(Y) ).
+    %
 :- func neq_vars(var(T), var(T)) = robdd(T).
 
     % imp_vars(X, Y) = ( var(X) =< var(Y) ).
+    %
 :- func imp_vars(var(T), var(T)) = robdd(T).
 
     % conj_vars([V1, V2, ..., Vn]) = var(V1) * var(V2) * ... * var(Vn).
+    %
 :- func conj_vars(vars(T)) = robdd(T).
 
     % conj_not_vars([V1, V2, ..., Vn]) = not_var(V1) * ... * not_var(Vn).
+    %
 :- func conj_not_vars(vars(T)) = robdd(T).
 
     % disj_vars([V1, V2, ..., Vn]) = var(V1) + var(V2) + ... + var(Vn).
+    %
 :- func disj_vars(vars(T)) = robdd(T).
 
     % at_most_one_of(Vs) =
     %   foreach pair Vi, Vj in Vs where Vi \= Vj. ~(var(Vi) * var(Vj)).
+    %
 :- func at_most_one_of(vars(T)) = robdd(T).
 
     % var_restrict_true(V, F) = restrict(V, F * var(V)).
+    %
 :- func var_restrict_true(var(T), robdd(T)) = robdd(T).
 
     % var_restrict_false(V, F) = restrict(V, F * not_var(V)).
+    %
 :- func var_restrict_false(var(T), robdd(T)) = robdd(T).
 
 %-----------------------------------------------------------------------------%
 
-    % X `entails` Y
+    % X `entails` Y:
+    %
     %   Succeed iff X entails Y.
     %   Does not create any new ROBDD nodes.
+    %
 :- pred robdd(T) `entails` robdd(T).
 :- mode in `entails` in is semidet.
 
     % Succeed iff the var is entailed by the ROBDD.
+    %
 :- pred var_entailed(robdd(T)::in, var(T)::in) is semidet.
 
 :- type entailment_result(T)
@@ -155,13 +178,16 @@
 :- type vars_entailed_result(T) == entailment_result(vars(T)).
 
     % Return the set of vars entailed by the ROBDD.
+    %
 :- func vars_entailed(robdd(T)) = vars_entailed_result(T).
 
     % Return the set of vars disentailed by the ROBDD.
+    %
 :- func vars_disentailed(robdd(T)) = vars_entailed_result(T).
 
-    % definite_vars(R, T, F) <=> T = vars_entailed(R) /\
-    %                F = vars_disentailed(R)
+    % definite_vars(R, T, F) <=>
+    %   T = vars_entailed(R) /\ F = vars_disentailed(R)
+    %
 :- pred definite_vars(robdd(T)::in, vars_entailed_result(T)::out,
         vars_entailed_result(T)::out) is det.
 
@@ -177,8 +203,8 @@
 
 :- type leader_map(T) == map(var(T), var(T)).
 
-:- type equiv_vars(T) --->
-    equiv_vars(
+:- type equiv_vars(T)
+    --->    equiv_vars(
         leader_map  :: leader_map(T)
     ).
 
@@ -200,7 +226,7 @@
 :- type imp_vars(T)
     --->    imp_vars(
             imps :: imp_map(T),     %  K =>  V  (~K \/  V)
-            rev_imps ::imp_map(T),      % ~K => ~V  ( K \/ ~V)
+                rev_imps        :: imp_map(T), % ~K => ~V  ( K \/ ~V)
             dis_imps :: imp_map(T),     %  K => ~V  (~K \/ ~V)
             rev_dis_imps :: imp_map(T)  % ~K =>  V  ( K \/  V)
         ).
@@ -210,20 +236,25 @@
 %---------------------------------------------------------------------------%
 
     % Existentially quantify away the var in the ROBDD.
+    %
 :- func restrict(var(T), robdd(T)) = robdd(T).
 
     % Existentially quantify away all vars greater than the specified var.
+    %
 :- func restrict_threshold(var(T), robdd(T)) = robdd(T).
 
     % Existentially quantify away all vars for which the predicate fails.
+    %
 :- func restrict_filter(pred(var(T)), robdd(T)) = robdd(T).
 :- mode restrict_filter(pred(in) is semidet, in) = out is det.
 
-    % restrict_filter(P, D, R)
+    % restrict_filter(P, D, R):
+    %
     %   Existentially quantify away all vars for which P fails,
     %   except, if D fails for a var, do not existentially quantify
     %   away that var or any greater than it. This means that D can be
     %   used to set a depth limit on the existential quantification.
+    %
 :- func restrict_filter(pred(var(T)), pred(var(T)), robdd(T)) = robdd(T).
 :- mode restrict_filter(pred(in) is semidet, pred(in) is semidet, in) = out
         is det.
@@ -234,26 +265,35 @@
     % equivalence class from the ROBDD.
     % Note: the leader map MUST correspond to actual equivalences within
     % the ROBDD, (e.g. have been produced by 'equivalent_vars/1').
+    %
 :- func squeeze_equiv(equiv_vars(T), robdd(T)) = robdd(T).
 
     % make_equiv(Equivalences) = Robdd:
+    %
     %   Robdd is the constraint representing all the equivalences
     %   in Equivalences.
+    %
 :- func make_equiv(equiv_vars(T)) = robdd(T).
 
     % add_equivalences(Equivalences, Robbd0) = Robdd:
-    %   Robdd is the constraint Robdd0 conjoined with
-    %   robdds representing all the equivalences in Equivalences.
+    %
+    % Robdd is the constraint Robdd0 conjoined with robdds representing
+    % all the equivalences in Equivalences.
+    %
 :- func add_equivalences(equiv_vars(T), robdd(T)) = robdd(T).
 
     % add_implications(Implications, Robbd0) = Robdd:
-    %   Robdd is the constraint Robdd0 conjoined with
-    %   robdds representing all the implications in Implications.
+    %
+    % Robdd is the constraint Robdd0 conjoined with robdds representing
+    % all the implications in Implications.
+    %
 :- func add_implications(imp_vars(T), robdd(T)) = robdd(T).
 
     % remove_implications_from_robdd(Implications, Robbd0) = Robdd:
-    %   Robdd is the constraint Robdd0 conjoined with
-    %   robdds representing all the implications in Implications.
+    %
+    % Robdd is the constraint Robdd0 conjoined with robdds representing
+    % all the implications in Implications.
+    %
 :- func remove_implications(imp_vars(T), robdd(T)) = robdd(T).
 
 %-----------------------------------------------------------------------------%
@@ -263,51 +303,65 @@
     ;   neg(var(T)).
 
     % Convert the ROBDD to disjunctive normal form.
+    %
 :- func dnf(robdd(T)) = list(list(literal(T))).
 
 %   % Convert the ROBDD to conjunctive normal form.
+%   %
 % :- func cnf(robdd(T)) = list(list(literal(T))).
 
     % Print out the ROBDD in disjunctive normal form.
-:- pred print_robdd(robdd(T)::in, io.state::di, io.state::uo) is det.
+    %
+:- pred print_robdd(robdd(T)::in, io::di, io::uo) is det.
 
-    % robdd_to_dot(ROBDD, WriteVar, FileName, IO0, IO).
+    % robdd_to_dot(ROBDD, WriteVar, FileName, !IO):
+    %
     %   Output the ROBDD in a format that can be processed by the
     %   graph-drawing program `dot'.
-:- pred robdd_to_dot(robdd(T)::in, write_var(T)::in(write_var),
-        string::in, io.state::di, io.state::uo) is det.
+    %
+:- pred robdd_to_dot(robdd(T)::in, write_var(T)::in(write_var), string::in,
+    io::di, io::uo) is det.
 
-    % robdd_to_dot(ROBDD, WriteVar, IO0, IO).
+    % robdd_to_dot(ROBDD, WriteVar, !IO):
+    %
     %   Output the ROBDD in a format that can be processed by the
     %   graph-drawing program `dot'.
+    %
 :- pred robdd_to_dot(robdd(T)::in, write_var(T)::in(write_var),
-        io.state::di, io.state::uo) is det.
+    io::di, io::uo) is det.
 
-:- type write_var(T) == pred(var(T), io.state, io.state).
+:- type write_var(T) == pred(var(T), io, io).
 :- inst write_var == (pred(in, di, uo) is det).
 
     % Apply the variable substitution to the ROBDD.
+    %
 :- func rename_vars(func(var(T)) = var(T), robdd(T)) = robdd(T).
 
     % Succeed iff ROBDD = one or ROBDD = zero.
+    %
 :- pred is_terminal(robdd(T)::in) is semidet.
 
     % Output the number of nodes and the depth of the ROBDD.
+    %
 :- pred size(robdd(T)::in, int::out, int::out) is det.
 
     % Output the number of nodes, the depth of the ROBDD and the
     % variables it contains.
+    %
 :- pred size(robdd(T)::in, int::out, int::out, list(var(T))::out) is det.
 
     % Succeed iff the var is constrained by the ROBDD.
+    %
 :- pred var_is_constrained(robdd(T)::in, var(T)::in) is semidet.
 
     % Succeed iff all the vars in the set are constrained by the ROBDD.
+    %
 :- pred vars_are_constrained(robdd(T)::in, vars(T)::in) is semidet.
 
 %-----------------------------------------------------------------------------%
 
-    % labelling(Vars, ROBDD, TrueVars, FalseVars)
+    % labelling(Vars, ROBDD, TrueVars, FalseVars):
+    %
     %   Takes a set of Vars and an ROBDD and returns a value assignment
     %   for those Vars that is a model of the Boolean function
     %   represented by the ROBDD.
@@ -316,10 +370,12 @@
     %   variables assigned the value 0).
     %
     % XXX should try using sparse_bitset here.
-:- pred labelling(vars(T)::in, robdd(T)::in, vars(T)::out,
-        vars(T)::out) is nondet.
+    %
+:- pred labelling(vars(T)::in, robdd(T)::in, vars(T)::out, vars(T)::out)
+    is nondet.
 
-    % minimal_model(Vars, ROBDD, TrueVars, FalseVars)
+    % minimal_model(Vars, ROBDD, TrueVars, FalseVars):
+    %
     %   Takes a set of Vars and an ROBDD and returns a value assignment
     %   for those Vars that is a minimal model of the Boolean function
     %   represented by the ROBDD.
@@ -328,8 +384,9 @@
     %   variables assigned the value 0).
     %
     % XXX should try using sparse_bitset here.
-:- pred minimal_model(vars(T)::in, robdd(T)::in, vars(T)::out,
-        vars(T)::out) is nondet.
+    %
+:- pred minimal_model(vars(T)::in, robdd(T)::in, vars(T)::out, vars(T)::out)
+    is nondet.
 
 %-----------------------------------------------------------------------------%
 
@@ -338,8 +395,8 @@
     % This operation is pure and does not perform any I/O, but we need
     % to either declare it impure or pass io.states to ensure that
     % the compiler won't try to optimise away the call.
-
-:- pred clear_caches(io.state::di, io.state::uo) is det.
+    %
+:- pred clear_caches(io::di, io::uo) is det.
 
 :- impure pred clear_caches is det.
 
@@ -359,9 +416,8 @@
 :- import_module set_unordlist.
 :- import_module string.
 
-% :- import_module unsafe.
-
-:- type robdd(T) ---> robdd(int).
+:- type robdd(T)
+    --->    robdd(int).
 
 % :- type robdd(T) ---> robdd(c_pointer).
 % Can't use a c_pointer since we want to memo ROBDD operations and
@@ -513,9 +569,7 @@
     ; R = zero ->
         all_vars
     ;
-        (
-            R ^ fa = zero
-        ->
+        ( R ^ fa = zero ->
             (vars_entailed(R ^ tr) `intersection` vars_entailed(R ^ fa))
                 `insert` R ^ value
         ;
@@ -531,9 +585,7 @@
     ; R = zero ->
         all_vars
     ;
-        (
-            R ^ tr = zero
-        ->
+        ( R ^ tr = zero ->
             (vars_disentailed(R ^ tr) `intersection`
                 vars_disentailed(R ^ fa)) `insert` R ^ value
         ;
@@ -645,7 +697,8 @@
         ).
 
 :- type imp_res(T) == entailment_result(imp_res_2(T)).
-:- type imp_res_2(T) ---> imps(map(var(T), vars_entailed_result(T))).
+:- type imp_res_2(T)
+    --->    imps(map(var(T), vars_entailed_result(T))).
 
 :- func implications_2(robdd(T)) = implication_result(T).
 % :- pragma memo(implications_2/1).
@@ -668,17 +721,14 @@
         FTVars = vars_entailed(R ^ fa),
 
         implications_2(R ^ tr) =
-            implication_result(Imps0, RevImps0, DisImps0,
-                RevDisImps0),
+            implication_result(Imps0, RevImps0, DisImps0, RevDisImps0),
         implications_2(R ^ fa) =
-            implication_result(Imps1, RevImps1, DisImps1,
-                RevDisImps1),
+            implication_result(Imps1, RevImps1, DisImps1, RevDisImps1),
 
         Imps2 = merge_imp_res(TTVars, FTVars, Imps0, Imps1),
         RevImps2 = merge_imp_res(TFVars, FFVars, RevImps0, RevImps1),
         DisImps2 = merge_imp_res(TFVars, FFVars, DisImps0, DisImps1),
-        RevDisImps2 = merge_imp_res(TTVars, FTVars, RevDisImps0,
-            RevDisImps1),
+        RevDisImps2 = merge_imp_res(TTVars, FTVars, RevDisImps0, RevDisImps1),
 
         % Imps2 = Imps0 `intersection` Imps1,
         % RevImps2 = RevImps0 `intersection` RevImps1,
@@ -708,8 +758,7 @@
     KeysA = map.sorted_keys(ImpsA),
     KeysB = map.sorted_keys(ImpsB),
     Keys = list.merge_and_remove_dups(KeysA, KeysB),
-    Imps = list.foldl((func(V, M) =
-            M ^ elem(V) := VsA `intersection` VsB :-
+    Imps = list.foldl((func(V, M) = M ^ elem(V) := VsA `intersection` VsB :-
         VsA = ( VsA0 = ImpsA ^ elem(V) -> VsA0 ; EntailedVarsA ),
         VsB = ( VsB0 = ImpsB ^ elem(V) -> VsB0 ; EntailedVarsB )
         ), Keys, map.init).
@@ -915,7 +964,7 @@
     ).
 
 :- pred print_robdd_2(robdd(T)::in, set_unordlist(var(T))::in,
-        set_unordlist(var(T))::in, io.state::di, io.state::uo) is det.
+    set_unordlist(var(T))::in, io::di, io::uo) is det.
 
 print_robdd_2(F, Trues, Falses) -->
     ( { F = one } ->
@@ -1212,11 +1261,10 @@
         var_to_int(Var, VarNum),
         var_to_int(LeaderVar, LeaderVarNum),
         require(LeaderVarNum < VarNum, "make_equiv_2: unordered vars"),
-        % Since LeaderVar < Var, and every leader must
-        % appear in an equivalence with itself, an ancestor
-        % invocation of this predicate must already have seen
-        % LeaderVar. If it is not in Trues, it must
-        % therefore be false.
+        % Since LeaderVar < Var, and every leader must appear in an equivalence
+        % with itself, an ancestor invocation of this predicate must already
+        % have seen LeaderVar. If it is not in Trues, it must therefore
+        % be false.
         Robdd = make_node(Var, zero, make_equiv_2(Vs, Trues))
     ).
 
@@ -1268,8 +1316,7 @@
             !:Cache = !.Cache ^ elem(R0) := R
         )
     ; LeaderVar = Var ->
-        add_equivalences_2(Vs, Trues `insert` Var,
-            R0 ^ tr, Rtr, !Cache),
+        add_equivalences_2(Vs, Trues `insert` Var, R0 ^ tr, Rtr, !Cache),
         add_equivalences_2(Vs, Trues, R0 ^ fa, Rfa, !Cache),
         R = make_node(Var, Rtr, Rfa),
         !:Cache = !.Cache ^ elem(R0) := R
@@ -1338,10 +1385,8 @@
         Nodes = Nodes0, Depth = Depth0, Seen = Seen0
     ;
         Val = term.var_to_int(F ^ value),
-        size_2(F ^ tr, Nodes0+1, Nodes1, Depth0, Depth1, Val,
-            Seen0, Seen1),
-        size_2(F ^ fa, Nodes1, Nodes, Depth0, Depth2, Val,
-            Seen1, Seen2),
+        size_2(F ^ tr, Nodes0+1, Nodes1, Depth0, Depth1, Val, Seen0, Seen1),
+        size_2(F ^ fa, Nodes1, Nodes, Depth0, Depth2, Val, Seen1, Seen2),
         max(Depth1, Depth2, Max),
         Depth = Max + 1,
         Seen = Seen2 `insert` F
@@ -1424,7 +1469,7 @@
         set_bbbtree(robdd(T))::in, set_bbbtree(robdd(T))::out,
         multi_map(var(T), robdd(T))::in,
         multi_map(var(T), robdd(T))::out,
-        io.state::di, io.state::uo) is det.
+    io::di, io::uo) is det.
 
 robdd_to_dot_2(Robdd, WV, Seen0, Seen, Ranks0, Ranks) -->
     ( { is_terminal(Robdd) } ->
@@ -1444,7 +1489,7 @@
     ).
 
 :- pred write_node(robdd(T)::in, write_var(T)::in(write_var),
-        io.state::di, io.state::uo) is det.
+    io::di, io::uo) is det.
 
 write_node(R, WV) -->
     io.format("%s [label=""<f0> %s|<f1> ",
@@ -1484,7 +1529,7 @@
     ).
 
 :- pred write_edge(robdd(T)::in, robdd(T)::in, bool::in,
-        io.state::di, io.state::uo) is det.
+    io::di, io::uo) is det.
 
 write_edge(R0, R1, Arc) -->
     ( { is_terminal(R1) } ->
@@ -1506,13 +1551,11 @@
 labelling_2([V | Vs], R0, TrueVars0, TrueVars, FalseVars0, FalseVars) :-
     R = var_restrict_false(V, R0),
     R \= zero,
-    labelling_2(Vs, R, TrueVars0, TrueVars, FalseVars0 `insert` V,
-        FalseVars).
+    labelling_2(Vs, R, TrueVars0, TrueVars, FalseVars0 `insert` V, FalseVars).
 labelling_2([V | Vs], R0, TrueVars0, TrueVars, FalseVars0, FalseVars) :-
     R = var_restrict_true(V, R0),
     R \= zero,
-    labelling_2(Vs, R, TrueVars0 `insert` V, TrueVars, FalseVars0,
-        FalseVars).
+    labelling_2(Vs, R, TrueVars0 `insert` V, TrueVars, FalseVars0, FalseVars).
 
 minimal_model(Vars, R, TrueVars, FalseVars) :-
     ( empty(Vars) ->
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/appengine
cvs diff: Diffing samples/appengine/war
cvs diff: Diffing samples/appengine/war/WEB-INF
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/standalone_c
cvs diff: Diffing samples/concurrency
cvs diff: Diffing samples/concurrency/dining_philosophers
cvs diff: Diffing samples/concurrency/midimon
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/java_interface
cvs diff: Diffing samples/java_interface/java_calls_mercury
cvs diff: Diffing samples/java_interface/mercury_calls_java
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/solver_types
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing ssdb
cvs diff: Diffing tests
cvs diff: Diffing tests/analysis
cvs diff: Diffing tests/analysis/ctgc
cvs diff: Diffing tests/analysis/excp
cvs diff: Diffing tests/analysis/ext
cvs diff: Diffing tests/analysis/sharing
cvs diff: Diffing tests/analysis/table
cvs diff: Diffing tests/analysis/trail
cvs diff: Diffing tests/analysis/unused_args
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/par_conj
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/stm
cvs diff: Diffing tests/stm/orig
cvs diff: Diffing tests/stm/orig/stm-compiler
cvs diff: Diffing tests/stm/orig/stm-compiler/test1
cvs diff: Diffing tests/stm/orig/stm-compiler/test10
cvs diff: Diffing tests/stm/orig/stm-compiler/test2
cvs diff: Diffing tests/stm/orig/stm-compiler/test3
cvs diff: Diffing tests/stm/orig/stm-compiler/test4
cvs diff: Diffing tests/stm/orig/stm-compiler/test5
cvs diff: Diffing tests/stm/orig/stm-compiler/test6
cvs diff: Diffing tests/stm/orig/stm-compiler/test7
cvs diff: Diffing tests/stm/orig/stm-compiler/test8
cvs diff: Diffing tests/stm/orig/stm-compiler/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/stmqueue
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test10
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test11
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test9
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/trailing
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
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