[m-rev.] diff: inst_util.m cleanups

Zoltan Somogyi zs at unimelb.edu.au
Wed Apr 4 13:11:43 AEST 2012


compiler/inst_util.m:
	Clean up this module in several respects.

	First, replace predicate definitions consisting of long lists of
	clauses with a single clause with an explicit disjunction, and
	in the process, impose consistent names on things. The old code
	used to do things like call the same argument different names in
	different clauses, and use the same name for different things
	in different clauses. Different clauses also followed different
	naming conventions, for example some referred to two insts being
	merged as InstA and InstB, while others referred to them as X and Y.

	Second, put the disjuncts in a more logical order.

	Third, mark with XXXs some places where the behavior of the code
	seems suspect.

Zoltan.

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/inst_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/inst_util.m,v
retrieving revision 1.66
diff -u -b -r1.66 inst_util.m
--- compiler/inst_util.m	4 Apr 2012 02:07:59 -0000	1.66
+++ compiler/inst_util.m	4 Apr 2012 02:10:09 -0000
@@ -115,7 +115,7 @@
 
     % Succeed iff the inst is any or contains any.
     %
-:- pred inst_contains_any(module_info::in, (mer_inst)::in) is semidet.
+:- pred inst_contains_any(module_info::in, mer_inst::in) is semidet.
 
     % Succeed iff the given var's inst is any or contains any.
     %
@@ -166,7 +166,7 @@
 
 %-----------------------------------------------------------------------------%
 
-abstractly_unify_inst(Live, InstA, InstB, UnifyIsReal, Inst, Det,
+abstractly_unify_inst(Live, InstA, InstB, UnifyIsReal, Inst, Detism,
         !ModuleInfo) :-
     % Check whether this pair of insts is already in the unify_insts table.
     ThisInstPair = unify_inst(Live, InstA, InstB, UnifyIsReal),
@@ -179,16 +179,16 @@
     % account for 116 out the 120 clock ticks spent in this predicate,
     % i.e. they account for almost 97% of its runtime.
     ( map.search(UnifyInsts0, ThisInstPair, Result) ->
-        ( Result = inst_det_known(UnifyInst, UnifyDet) ->
+        ( Result = inst_det_known(UnifyInst, UnifyDetism) ->
             Inst0 = UnifyInst,
-            Det = UnifyDet
+            Detism = UnifyDetism
         ;
             Inst0 = defined_inst(ThisInstPair),
             % It's ok to assume that the unification is deterministic here,
             % because the only time that this will happen is when we get to the
             % recursive case for a recursively defined inst. If the unification
             % as a whole is semidet then it must be semidet somewhere else too.
-            Det = detism_det
+            Detism = detism_det
         ),
         Inst1 = Inst0
     ;
@@ -201,11 +201,11 @@
         inst_expand(!.ModuleInfo, InstA, InstA2),
         inst_expand(!.ModuleInfo, InstB, InstB2),
         abstractly_unify_inst_2(Live, InstA2, InstB2, UnifyIsReal,
-            Inst0, Det, !ModuleInfo),
+            Inst0, Detism, !ModuleInfo),
 
         % If this unification cannot possible succeed, the correct inst
         % is not_reached.
-        ( determinism_components(Det, _, at_most_zero) ->
+        ( determinism_components(Detism, _, at_most_zero) ->
             Inst1 = not_reached
         ;
             Inst1 = Inst0
@@ -214,7 +214,7 @@
         % Now update the value associated with ThisInstPair.
         module_info_get_inst_table(!.ModuleInfo, InstTable2),
         inst_table_get_unify_insts(InstTable2, UnifyInsts2),
-        map.det_update(ThisInstPair, inst_det_known(Inst1, Det),
+        map.det_update(ThisInstPair, inst_det_known(Inst1, Detism),
             UnifyInsts2, UnifyInsts),
         inst_table_set_unify_insts(UnifyInsts, InstTable2, InstTable),
         module_info_set_inst_table(InstTable, !ModuleInfo)
@@ -230,20 +230,20 @@
     unify_is_real::in, mer_inst::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-abstractly_unify_inst_2(IsLive, InstA, InstB, UnifyIsReal, Inst, Det,
+abstractly_unify_inst_2(IsLive, InstA, InstB, UnifyIsReal, Inst, Detism,
         !ModuleInfo) :-
     ( InstB = not_reached ->
         Inst = not_reached,
-        Det = detism_det
+        Detism = detism_det
     ; InstA = constrained_inst_vars(InstVars, InstA1) ->
         abstractly_unify_constrained_inst_vars(IsLive, InstVars, InstA1,
-            InstB, UnifyIsReal, Inst, Det, !ModuleInfo)
+            InstB, UnifyIsReal, Inst, Detism, !ModuleInfo)
     ; InstB = constrained_inst_vars(InstVars, InstB1) ->
         abstractly_unify_constrained_inst_vars(IsLive, InstVars, InstB1,
-            InstA, UnifyIsReal, Inst, Det, !ModuleInfo)
+            InstA, UnifyIsReal, Inst, Detism, !ModuleInfo)
     ;
         abstractly_unify_inst_3(IsLive, InstA, InstB, UnifyIsReal,
-            Inst, Det, !ModuleInfo)
+            Inst, Detism, !ModuleInfo)
     ).
 
     % Abstractly unify two expanded insts.
@@ -257,277 +257,351 @@
     unify_is_real::in, mer_inst::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-abstractly_unify_inst_3(is_live, not_reached, _, _, not_reached, detism_det,
-        !ModuleInfo).
-
-abstractly_unify_inst_3(is_live, free, any(UniqY, HOInstInfo), Real,
-        any(Uniq, HOInstInfo), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, UniqY, Uniq).
-
-% abstractly_unify_inst_3(is_live, free, free, _, _, _, _, _) :- fail.
-
-abstractly_unify_inst_3(is_live, free, bound(UniqY, List0), Real,
-        bound(Uniq, List), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, UniqY, Uniq),
+abstractly_unify_inst_3(Live, InstA, InstB, Real, Inst, Detism, !ModuleInfo) :-
+    (
+        Live = is_live,
+        (
+            InstA = not_reached,
+            Inst = not_reached,
+            Detism = detism_det
+        ;
+            InstA = free,
+            InstB = free,
+            fail
+        ;
+            InstA = free,
+            InstB = ground(UniqB, HOInstInfoB),
+            unify_uniq(is_live, Real, detism_det, unique, UniqB, Uniq),
+            Inst = ground(Uniq, HOInstInfoB),
+            Detism = detism_det
+        ;
+            InstA = free,
+            InstB = any(UniqB, HOInstInfo),
+            unify_uniq(is_live, Real, detism_det, unique, UniqB, Uniq),
+            Inst = any(Uniq, HOInstInfo),
+            Detism = detism_det
+        ;
+            InstA = free,
+            InstB = bound(UniqB, BoundInstsB),
+            unify_uniq(is_live, Real, detism_det, unique, UniqB, Uniq),
         % Since both are live, we must disallow free-free unifications.
-    bound_inst_list_is_ground_or_any(List0, !.ModuleInfo),
+            bound_inst_list_is_ground_or_any(BoundInstsB, !.ModuleInfo),
         % Since both are live, we must make the result shared
         % (unless it was already shared).
-    ( ( UniqY = unique ; UniqY = mostly_unique ) ->
-        make_shared_bound_inst_list(List0, List, !ModuleInfo)
+            ( ( UniqB = unique ; UniqB = mostly_unique ) ->
+                make_shared_bound_inst_list(BoundInstsB, BoundInsts,
+                    !ModuleInfo)
     ;
-        List = List0
-    ).
-
-abstractly_unify_inst_3(is_live, free, ground(UniqY, PredInst), Real,
-        ground(Uniq, PredInst), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, UniqY, Uniq).
-
-% abstractly_unify_inst_3(is_live, free, abstract_inst(_,_), _, _, _, _) :-
-%   fail.
-
-abstractly_unify_inst_3(is_live, bound(UniqX, List0), any(UniqY, _), Real,
-        bound(Uniq, List), Det, !ModuleInfo) :-
-    allow_unify_bound_any(Real),
-    unify_uniq(is_live, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_any_bound_inst_list(List0, is_live, UniqY, Real, List, Det1,
-        !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_live, bound(UniqY, List0), free, Real,
-        bound(Uniq, List), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, UniqY, Uniq),
+                BoundInsts = BoundInstsB
+            ),
+            Inst = bound(Uniq, BoundInsts),
+            Detism = detism_det
+        ;
+            InstA = free,
+            InstB = abstract_inst(_, _),
+            fail
+        ;
+            InstA = bound(UniqB, BoundInstsA),
+            InstB = free,
+            unify_uniq(is_live, Real, detism_det, unique, UniqB, Uniq),
     % Since both are live, we must disallow free-free unifications.
-    bound_inst_list_is_ground_or_any(List0, !.ModuleInfo),
-    make_shared_bound_inst_list(List0, List, !ModuleInfo).
-
-abstractly_unify_inst_3(is_live, bound(UniqX, ListX), bound(UniqY, ListY), Real,
-        bound(Uniq, List), Det, !ModuleInfo) :-
-    abstractly_unify_bound_inst_list(is_live, ListX, ListY, Real, List, Det,
-        !ModuleInfo),
-    unify_uniq(is_live, Real, Det, UniqX, UniqY, Uniq).
-
-abstractly_unify_inst_3(is_live, bound(UniqX, BoundInsts0), ground(UniqY, _),
-        Real, bound(Uniq, BoundInsts), Det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_ground_bound_inst_list(BoundInsts0, is_live, UniqY, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-% abstract insts not supported
-% abstractly_unify_inst_3(is_live, bound(Uniq, List), abstract_inst(_,_), Real,
-%       ground(shared), detism_semi, !ModuleInfo) :-
-%   unify_uniq(is_live, Real, detism_semi, unique, UniqY, Uniq),
-%   bound_inst_list_is_ground(List, !.ModuleInfo).
-
-abstractly_unify_inst_3(is_live, ground(UniqX, higher_order(PredInst)),
-        any(UniqY, _), Real, ground(Uniq, higher_order(PredInst)),
-        detism_semi, !ModuleInfo) :-
-    Real = fake_unify,
-    unify_uniq(is_live, Real, detism_det, UniqX, UniqY, Uniq).
-
-abstractly_unify_inst_3(is_live, ground(Uniq0, higher_order(PredInst)), free,
-        Real, ground(Uniq, higher_order(PredInst)), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, Uniq0, Uniq).
-
-abstractly_unify_inst_3(is_live, ground(UniqX, higher_order(_)),
-        bound(UniqY, BoundInsts0), Real, bound(Uniq, BoundInsts),
-        Det, !ModuleInfo) :-
-    % check `Real = fake_unify' ?
-    unify_uniq(is_live, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_ground_bound_inst_list(BoundInsts0, is_live, UniqX, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_live, ground(UniqA, higher_order(PredInstA)),
-        ground(UniqB, _HOInstInfoB), Real, ground(Uniq, HOInstInfo),
-        detism_semi, !ModuleInfo) :-
+            bound_inst_list_is_ground_or_any(BoundInstsA, !.ModuleInfo),
+            make_shared_bound_inst_list(BoundInstsA, BoundInsts, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            Detism = detism_det
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = bound(UniqB, BoundInstsB),
+            abstractly_unify_bound_inst_list(is_live, BoundInstsA, BoundInstsB,
+                Real, BoundInsts, Detism, !ModuleInfo),
+            unify_uniq(is_live, Real, Detism, UniqA, UniqB, Uniq),
+            Inst = bound(Uniq, BoundInsts)
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = ground(UniqB, _),
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_ground_bound_inst_list(BoundInstsA, is_live, UniqB, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = any(UniqB, _),
+            allow_unify_bound_any(Real),
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_any_bound_inst_list(BoundInstsA, is_live, UniqB, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = bound(_Uniq, _BoundInstsA),
+            InstB = abstract_inst(_, _),
+            fail
+            % abstract insts not supported
+            % unify_uniq(is_live, Real, detism_semi, unique, UniqB, Uniq),
+            % bound_inst_list_is_ground(BoundInstsA, !.ModuleInfo),
+            % Inst = ground(shared),
+            % Detism = detism_semi
+        ;
+            InstA = ground(UniqA, higher_order(PredInst)),
+            InstB = free,
+            unify_uniq(is_live, Real, detism_det, unique, UniqA, Uniq),
+            Inst = ground(Uniq, higher_order(PredInst)),
+            Detism = detism_det
+        ;
+            InstA = ground(UniqA, HOInstInfoA),
+            InstB = ground(UniqB, _HOInstInfoB),
+            HOInstInfoA = higher_order(_PredInstA),
     % It is an error to unify higher-order preds,
     % so if Real \= fake_unify, then we must fail.
     Real = fake_unify,
-    % In theory we should choose take the union of the information specified
-    % by PredInstA and _HOInstInfoB. However, since our data representation
-    % provides no way of doing that, and since this will only happen for
-    % fake_unifys, for which it shouldn't make any difference, we just choose
-    % the information specified by PredInstA.
-    HOInstInfo = higher_order(PredInstA),
-    unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq).
-
-abstractly_unify_inst_3(is_live, ground(Uniq, none), Inst0, Real, Inst, Det,
-        !ModuleInfo) :-
-    make_ground_inst(Inst0, is_live, Uniq, Real, Inst, Det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_live, any(Uniq0, higher_order(PredInst)), free,
-        Real, any(Uniq, higher_order(PredInst)), detism_det, !ModuleInfo) :-
-    unify_uniq(is_live, Real, detism_det, unique, Uniq0, Uniq).
-
-abstractly_unify_inst_3(is_live, any(UniqX, higher_order(_)),
-        bound(UniqY, BoundInsts0), Real, bound(Uniq, BoundInsts),
-        Det, !ModuleInfo) :-
+            % In theory we should choose take the union of the information
+            % specified by PredInstA and _HOInstInfoB. However, since our data
+            % representation provides no way of doing that, and since this will
+            % only happen for fake_unifys, for which it shouldn't make any
+            % difference, we just choose the information specified by
+            % PredInstA.
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_semi
+        ;
+            InstA = ground(Uniq, none),
+            make_ground_inst(InstB, is_live, Uniq, Real, Inst, Detism,
+                !ModuleInfo)
+        ;
+            InstA = ground(UniqA, higher_order(PredInst)),
+            InstB = any(UniqB, _),
+            Real = fake_unify,
+            unify_uniq(is_live, Real, detism_det, UniqA, UniqB, Uniq),
+            Inst = ground(Uniq, higher_order(PredInst)),
+            Detism = detism_semi
+        ;
+            InstA = ground(UniqA, higher_order(_)),
+            InstB = bound(UniqB, BoundInstsB),
     % check `Real = fake_unify' ?
-    unify_uniq(is_live, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_any_bound_inst_list(BoundInsts0, is_live, UniqX, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_live, any(UniqA, higher_order(PredInstA)),
-        ground(UniqB, _HOInstInfoB), Real, ground(Uniq, HOInstInfo),
-        detism_semi, !ModuleInfo) :-
-    % See comment for the ground(_, higher_order(_)), ground(_, _) case.
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_ground_bound_inst_list(BoundInstsB, is_live, UniqA, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = free,
+            HOInstInfoA = higher_order(_PredInstA),
+            unify_uniq(is_live, Real, detism_det, unique, UniqA, Uniq),
+            Inst = any(Uniq, HOInstInfoA),
+            Detism = detism_det
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = ground(UniqB, _HOInstInfoB),
+            HOInstInfoA = higher_order(_PredInstA),
+            % See comment for the ground(_, higher_order(_)), ground(_, _)
+            % case.
     Real = fake_unify,
-    HOInstInfo = higher_order(PredInstA),
-    unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq).
-
-abstractly_unify_inst_3(is_live, any(UniqA, higher_order(PredInstA)),
-        any(UniqB, _HOInstInfoB), Real, any(Uniq, HOInstInfo), detism_semi,
-        !ModuleInfo) :-
-    % See comment for the ground(_, higher_order(_)), ground(_, _) case.
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_semi
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = bound(UniqB, BoundInstsB),
+            HOInstInfoA = higher_order(_),
+            % check `Real = fake_unify' ?
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_any_bound_inst_list(BoundInstsB, is_live, UniqA, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = any(UniqB, _HOInstInfoB),
+            HOInstInfoA = higher_order(_PredInstA),
+            % See comment for the ground(_, higher_order(_)), ground(_, _)
+            % case.
     Real = fake_unify,
-    HOInstInfo = higher_order(PredInstA),
-    unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq).
-
-abstractly_unify_inst_3(is_live, any(Uniq, none), Inst0, Real, Inst, Det,
-        !ModuleInfo) :-
-    make_any_inst(Inst0, is_live, Uniq, Real, Inst, Det, !ModuleInfo).
-
-% abstractly_unify_inst_3(is_live, abstract_inst(_,_), free, _, _, _, _, _)
-%       :- fail.
-
-% abstract insts not supported
-% abstractly_unify_inst_3(is_live, abstract_inst(_,_), bound(Uniq, List), Real,
-%       ground(shared, no), detism_semi, !ModuleInfo) :-
-%   check_not_clobbered(Real, Uniq),
-%   bound_inst_list_is_ground(List, !.ModuleInfo).
-%
-% abstractly_unify_inst_3(is_live, abstract_inst(_,_), ground(Uniq, no), Real,
-%       ground(shared, no), detism_semi, !ModuleInfo) :-
-%   check_not_clobbered(Real, Uniq).
-%
-% abstractly_unify_inst_3(is_live, abstract_inst(Name, ArgsA),
-%       abstract_inst(Name, ArgsB), Real,
-%       abstract_inst(Name, Args), Det, !ModuleInfo) :-
-%   abstractly_unify_inst_list(ArgsA, ArgsB, is_live, Real,
-%       Args, Det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, not_reached, _, _, not_reached, detism_det,
-        !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, free, Inst, _, Inst, detism_det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, bound(UniqX, List0), any(UniqY, _), Real,
-        bound(Uniq, List), Det, !ModuleInfo) :-
-    allow_unify_bound_any(Real),
-    unify_uniq(is_dead, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_any_bound_inst_list(List0, is_live, UniqY, Real, List, Det1,
-        !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_dead, bound(UniqX, List), free, Real,
-        bound(Uniq, List), detism_det, !ModuleInfo) :-
-    unify_uniq(is_dead, Real, detism_det, UniqX, unique, Uniq).
-
-abstractly_unify_inst_3(is_dead, bound(UniqX, ListX), bound(UniqY, ListY),
-        Real, bound(Uniq, List), Det, !ModuleInfo) :-
-    abstractly_unify_bound_inst_list(is_dead, ListX, ListY, Real,
-        List, Det, !ModuleInfo),
-    unify_uniq(is_dead, Real, Det, UniqX, UniqY, Uniq).
-
-abstractly_unify_inst_3(is_dead, bound(UniqX, BoundInsts0), ground(UniqY, _),
-        Real, bound(Uniq, BoundInsts), Det, !ModuleInfo) :-
-    unify_uniq(is_dead, Real, detism_semi, UniqX, UniqY, Uniq),
-    make_ground_bound_inst_list(BoundInsts0, is_dead, UniqY, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-% abstract insts aren't really supported
-% abstractly_unify_inst_3(is_dead, bound(Uniq, List), abstract_inst(N,As),
-%           Result, Det, !ModuleInfo) :-
-%   ( bound_inst_list_is_ground(List, !.ModuleInfo) ->
-%       Result = bound(Uniq, List),
-%       Det = semidet
-%   ; bound_inst_list_is_free(List, !.ModuleInfo) ->
-%       Result = abstract_inst(N,As),
-%       Det = det
-%   ;
-%       fail
-%   ).
-
-abstractly_unify_inst_3(is_dead, ground(UniqX, higher_order(PredInst)),
-        any(UniqY, _), Real, ground(Uniq, higher_order(PredInst)),
-        detism_semi, !ModuleInfo) :-
+            unify_uniq(is_live, Real, detism_semi, UniqA, UniqB, Uniq),
+            Inst = any(Uniq, HOInstInfoA),
+            Detism = detism_semi
+        ;
+            InstA = any(Uniq, none),
+            make_any_inst(InstB, is_live, Uniq, Real, Inst, Detism,
+                !ModuleInfo)
+        ;
+            InstA = abstract_inst(_, _),
+            InstB = free,
+            fail
+        ;
+            InstA = abstract_inst(_, _), 
+            InstB = bound(_Uniq, _BoundInstsB),
+            fail
+            % abstract insts not supported
+            % check_not_clobbered(Real, Uniq),
+            % bound_inst_list_is_ground(BoundInstsB, !.ModuleInfo).
+            % Inst = ground(shared, no),
+            % Detism = detism_semi
+        ;
+            InstA = abstract_inst(_, _),
+            InstB = ground(_Uniq, none),
+            fail
+            % check_not_clobbered(Real, Uniq),
+            % Inst = ground(shared, no),
+            % Detism = detism_semi
+        ;
+            InstA = abstract_inst(_NameA, _ArgsA),
+            InstB = abstract_inst(_NameB, _ArgsB),
+            fail
+            % abstractly_unify_inst_list(ArgsA, ArgsB, is_live, Real,
+            %     Args, Detism, !ModuleInfo),
+            % Inst = abstract_inst(Name, Args)
+        )
+    ;
+        Live = is_dead,
+        (
+            InstA = not_reached,
+            Inst = not_reached,
+            Detism = detism_det
+        ;
+            InstA = free,
+            Inst = InstB,
+            Detism = detism_det
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = free,
+            unify_uniq(is_dead, Real, detism_det, UniqA, unique, Uniq),
+            Inst = bound(Uniq, BoundInstsA),
+            Detism = detism_det
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = bound(UniqB, BoundInstsB),
+            abstractly_unify_bound_inst_list(is_dead, BoundInstsA, BoundInstsB,
+                Real, BoundInsts, Detism, !ModuleInfo),
+            unify_uniq(is_dead, Real, Detism, UniqA, UniqB, Uniq),
+            Inst = bound(Uniq, BoundInsts)
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = ground(UniqB, _),
+            unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_ground_bound_inst_list(BoundInstsA, is_dead, UniqB, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = bound(UniqA, BoundInstsA),
+            InstB = any(UniqB, _),
     allow_unify_bound_any(Real),
-    unify_uniq(is_dead, Real, detism_semi, UniqX, UniqY, Uniq).
-
-abstractly_unify_inst_3(is_dead, ground(Uniq, higher_order(PredInst)), free,
-        _Real, ground(Uniq, higher_order(PredInst)), detism_det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, ground(UniqA, higher_order(_)),
-        bound(UniqB, BoundInsts0), Real, bound(Uniq, BoundInsts),
-        Det, !ModuleInfo) :-
     unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
-    make_ground_bound_inst_list(BoundInsts0, is_dead, UniqA, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_dead, ground(UniqA, higher_order(PredInstA)),
-        ground(UniqB, _HOInstInfoB), Real, ground(Uniq, HOInstInfo),
-        detism_det, !ModuleInfo) :-
+            make_any_bound_inst_list(BoundInstsA, is_live, UniqB, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = bound(_Uniq, _BoundInstsA),
+            InstB = abstract_inst(_N, _As),
+            fail
+            % abstract insts aren't really supported
+            % ( bound_inst_list_is_ground(BoundInstsA, !.ModuleInfo) ->
+            %     Inst = bound(Uniq, BoundInstsA),
+            %     Detism = semidet
+            % ; bound_inst_list_is_free(BoundInstsA, !.ModuleInfo) ->
+            %     Inst = abstract_inst(N, As),
+            %     Detism = det
+            % ;
+            %     fail
+            % )
+        ;
+            InstA = ground(Uniq, HOInstInfoA),
+            InstB = free,
+            HOInstInfoA = higher_order(_PredInstA),
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_det
+        ;
+            InstA = ground(UniqA, HOInstInfoA),
+            InstB = bound(UniqB, BoundInstsB),
+            HOInstInfoA = higher_order(_PredInstA),
+            unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_ground_bound_inst_list(BoundInstsB, is_dead, UniqA, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = ground(UniqA, HOInstInfoA),
+            InstB = ground(UniqB, _HOInstInfoB),
+            HOInstInfoA = higher_order(_PredInstA),
     Real = fake_unify,
-    HOInstInfo = higher_order(PredInstA),
-    unify_uniq(is_dead, Real, detism_det, UniqA, UniqB, Uniq).
-
-abstractly_unify_inst_3(is_dead, ground(Uniq, none), Inst0, Real, Inst, Det,
-        !ModuleInfo) :-
-    make_ground_inst(Inst0, is_dead, Uniq, Real, Inst, Det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, any(UniqX, higher_order(PredInst)),
-        any(UniqY, _), Real, any(Uniq, higher_order(PredInst)),
-        detism_semi, !ModuleInfo) :-
-    unify_uniq(is_dead, Real, detism_semi, UniqX, UniqY, Uniq).
-
-abstractly_unify_inst_3(is_dead, any(Uniq, higher_order(PredInst)), free,
-        _Real, any(Uniq, higher_order(PredInst)), detism_det, !ModuleInfo).
-
-abstractly_unify_inst_3(is_dead, any(UniqA, higher_order(_)),
-        bound(UniqB, BoundInsts0), Real, bound(Uniq, BoundInsts),
-        Det, !ModuleInfo) :-
+            unify_uniq(is_dead, Real, detism_det, UniqA, UniqB, Uniq),
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_det
+        ;
+            InstA = ground(UniqA, HOInstInfoA),
+            InstB = any(UniqB, _),
+            HOInstInfoA = higher_order(_PredInstA),
+            allow_unify_bound_any(Real),
     unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
-    make_any_bound_inst_list(BoundInsts0, is_dead, UniqA, Real, BoundInsts,
-        Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-
-abstractly_unify_inst_3(is_dead, any(UniqA, higher_order(PredInstA)),
-        ground(UniqB, _HOInstInfoB), Real, ground(Uniq, HOInstInfo),
-        detism_det, !ModuleInfo) :-
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_semi
+        ;
+            InstA = ground(Uniq, none),
+            make_ground_inst(InstB, is_dead, Uniq, Real, Inst, Detism,
+                !ModuleInfo)
+        ;
+            InstA = any(Uniq, HOInstInfoA),
+            InstB = free,
+            HOInstInfoA = higher_order(_PredInstA),
+            Inst = any(Uniq, HOInstInfoA),
+            Detism = detism_det
+        ;
+            InstA = any(UniqA, higher_order(_)),
+            InstB = bound(UniqB, BoundInstsB),
+            unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
+            make_any_bound_inst_list(BoundInstsB, is_dead, UniqA, Real,
+                BoundInsts, Detism1, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts),
+            det_par_conjunction_detism(Detism1, detism_semi, Detism)
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = ground(UniqB, _HOInstInfoB),
+            HOInstInfoA = higher_order(_PredInstA),
     Real = fake_unify,
-    HOInstInfo = higher_order(PredInstA),
-    unify_uniq(is_dead, Real, detism_det, UniqA, UniqB, Uniq).
-
-abstractly_unify_inst_3(is_dead, any(Uniq, none), Inst0, Real, Inst, Det,
-        !ModuleInfo) :-
-    make_any_inst(Inst0, is_dead, Uniq, Real, Inst, Det, !ModuleInfo).
-
-% abstract insts aren't really supported
-% abstractly_unify_inst_3(is_dead, abstract_inst(N,As), bound(List), Real,
-%           ModuleInfo, Result, Det, ModuleInfo) :-
-%   ( bound_inst_list_is_ground(List, ModuleInfo) ->
-%       Result = bound(List),
-%       Det = semidet
-%   ; bound_inst_list_is_free(List, ModuleInfo) ->
-%       Result = abstract_inst(N,As),
-%       Det = det
-%   ;
-%       fail
-%   ).
-%
-% abstractly_unify_inst_3(is_dead, abstract_inst(_,_), ground, _Real,
-%       ground, detism_semi, !ModuleInfo).
-%
-% abstractly_unify_inst_3(is_dead, abstract_inst(Name, ArgsA),
-%       abstract_inst(Name, ArgsB), Real,
-%       abstract_inst(Name, Args), Det, !ModuleInfo) :-
-%   abstractly_unify_inst_list(ArgsA, ArgsB, is_dead, Real,
-%       Args, Det, !ModuleInfo).
+            unify_uniq(is_dead, Real, detism_det, UniqA, UniqB, Uniq),
+            Inst = ground(Uniq, HOInstInfoA),
+            Detism = detism_det
+        ;
+            InstA = any(UniqA, HOInstInfoA),
+            InstB = any(UniqB, _),
+            HOInstInfoA = higher_order(_PredInstA),
+            unify_uniq(is_dead, Real, detism_semi, UniqA, UniqB, Uniq),
+            Inst = any(Uniq, HOInstInfoA),
+            Detism = detism_semi
+        ;
+            InstA = any(Uniq, none),
+            make_any_inst(InstB, is_dead, Uniq, Real, Inst, Detism,
+                !ModuleInfo)
+        ;
+            InstA = abstract_inst(_N, _As),
+            InstB = bound(_, _BoundInstsB),
+            fail
+            % abstract insts aren't really supported
+            % ( bound_inst_list_is_ground(BoundInstsB, ModuleInfo) ->
+            %     Inst = bound(BoundInstsB),
+            %     Detism = semidet
+            % ; bound_inst_list_is_free(BoundInstsB, ModuleInfo) ->
+            %     Inst = abstract_inst(N, As),
+            %     Detism = det
+            % ;
+            %     fail
+            % ).
+        ;
+            InstA = abstract_inst(_, _),
+            InstB = ground(_, _),
+            fail
+        ;
+            InstA = abstract_inst(_NameA, _ArgsA),
+            InstB = abstract_inst(_NameB, _ArgsB),
+            fail
+            % abstractly_unify_inst_list(ArgsA, ArgsB, is_dead, Real,
+            %     Args, Detism, !ModuleInfo),
+            % Inst = abstract_inst(Name, Args)
+        )
+    ).
 
 %-----------------------------------------------------------------------------%
 
@@ -538,26 +612,25 @@
     module_info::in, module_info::out) is semidet.
 
 abstractly_unify_inst_list([], [], _, _, [], detism_det, !ModuleInfo).
-abstractly_unify_inst_list([X | Xs], [Y | Ys], Live, Real, [Z | Zs], Det,
-        !ModuleInfo) :-
-    abstractly_unify_inst(Live, X, Y, Real, Z, Det1, !ModuleInfo),
-    abstractly_unify_inst_list(Xs, Ys, Live, Real, Zs, Det2, !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+abstractly_unify_inst_list([InstA | InstsA], [InstB | InstsB], Live, Real,
+        [Inst | Insts], Detism, !ModuleInfo) :-
+    abstractly_unify_inst(Live, InstA, InstB, Real, Inst, Detism1, !ModuleInfo),
+    abstractly_unify_inst_list(InstsA, InstsB, Live, Real, Insts, Detism2,
+        !ModuleInfo),
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 %-----------------------------------------------------------------------------%
 
     % This is the abstract unification operation which unifies a variable
     % (or rather, it's instantiatedness) with a functor.
     %
-abstractly_unify_inst_functor(Live, InstA, ConsId, ArgInsts, ArgLives,
-        Real, Type, Inst, Det, !ModuleInfo) :-
-    inst_expand(!.ModuleInfo, InstA, InstA2),
-    ( InstA2 = constrained_inst_vars(InstVars, InstA3) ->
-        abstractly_unify_inst_functor(Live, InstA3, ConsId, ArgInsts,
-            ArgLives, Real, Type, Inst0, Det, !ModuleInfo),
-        (
-            inst_matches_final(Inst0, InstA3, !.ModuleInfo)
-        ->
+abstractly_unify_inst_functor(Live, InstA0, ConsIdB, ArgInstsB, ArgLives,
+        Real, Type, Inst, Detism, !ModuleInfo) :-
+    inst_expand(!.ModuleInfo, InstA0, InstA1),
+    ( InstA1 = constrained_inst_vars(InstVars, InstA) ->
+        abstractly_unify_inst_functor(Live, InstA, ConsIdB, ArgInstsB,
+            ArgLives, Real, Type, Inst0, Detism, !ModuleInfo),
+        ( inst_matches_final(Inst0, InstA, !.ModuleInfo) ->
             % We can keep the constrained_inst_vars.
             Inst = constrained_inst_vars(InstVars, Inst0)
         ;
@@ -567,17 +640,17 @@
             % ground as InstVars and is a subtype of InstVars. I don't think
             % this is likely to be a problem in practice because:
             % a) I don't think it's likely to occur very often in typical uses
-            %    of polymorphic modes (I suspect InstA3 will nearly always be
+            %    of polymorphic modes (I suspect InstA will nearly always be
             %    `ground' or `any' in which case the only way
             %    inst_matches_final can fail is if Inst0 is clobbered
-            %    -- it can't be less instantiated than InstA3); and
+            %    -- it can't be less instantiated than InstA); and
             % b) Even if this information is retained, I can't see what sort
             %    of situations it would actually be useful for.
             Inst = Inst0
         )
     ;
-        abstractly_unify_inst_functor_2(Live, InstA2, ConsId, ArgInsts,
-            ArgLives, Real, Type, Inst, Det, !ModuleInfo)
+        abstractly_unify_inst_functor_2(Live, InstA1, ConsIdB, ArgInstsB,
+            ArgLives, Real, Type, Inst, Detism, !ModuleInfo)
     ).
 
 :- pred abstractly_unify_inst_functor_2(is_live::in, mer_inst::in,
@@ -585,69 +658,77 @@
     mer_type::in, mer_inst::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-abstractly_unify_inst_functor_2(is_live, not_reached, _, _, _, _, _,
-        not_reached, detism_erroneous, !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_live, free, ConsId, Args0, ArgLives, _Real,
-        _, bound(unique, [bound_functor(ConsId, Args)]), detism_det,
-        !ModuleInfo) :-
-    inst_list_is_ground_or_any_or_dead(Args0, ArgLives, !.ModuleInfo),
-    maybe_make_shared_inst_list(Args0, ArgLives, Args, !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_live, any(Uniq, _), ConsId, ArgInsts,
-        ArgLives, Real, Type, Inst, Det, !ModuleInfo) :-
+abstractly_unify_inst_functor_2(Live, InstA, ConsIdB, ArgInstsB, ArgLives,
+        Real, Type, Inst, Detism, !ModuleInfo) :-
+    (
+        Live = is_live,
+        (
+            InstA = not_reached,
+            Inst = not_reached,
+            Detism = detism_erroneous
+        ;
+            InstA = free,
+            inst_list_is_ground_or_any_or_dead(ArgInstsB, ArgLives,
+                !.ModuleInfo),
+            maybe_make_shared_inst_list(ArgInstsB, ArgLives, ArgInsts,
+                !ModuleInfo),
+            Inst = bound(unique, [bound_functor(ConsIdB, ArgInsts)]),
+            Detism = detism_det
+        ;
+            InstA = any(Uniq, _),
     % We only allow `any' to unify with a functor if we know that
     % the type is not a solver type.
     \+ type_util.is_solver_type(!.ModuleInfo, Type),
-    make_any_inst_list_lives(ArgInsts, is_live, ArgLives, Uniq, Real,
-        AnyArgInsts, Det, !ModuleInfo),
-    Inst = bound(Uniq, [bound_functor(ConsId, AnyArgInsts)]).
-
-abstractly_unify_inst_functor_2(is_live, bound(Uniq, ListX), ConsId, Args,
-        ArgLives, Real, _, bound(Uniq, List), Det,
-        !ModuleInfo) :-
-    abstractly_unify_bound_inst_list_lives(ListX, ConsId, Args, ArgLives,
-        Real, List, Det, !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_live, ground(Uniq, _), ConsId, ArgInsts,
-        ArgLives, Real, _, Inst, Det, !ModuleInfo) :-
-    make_ground_inst_list_lives(ArgInsts, is_live, ArgLives, Uniq, Real,
-        GroundArgInsts, Det, !ModuleInfo),
-    Inst = bound(Uniq, [bound_functor(ConsId, GroundArgInsts)]).
-
-% abstractly_unify_inst_functor_2(is_live, abstract_inst(_,_), _, _, _, _, _,
-%       _, _) :-
-%       fail.
-
-abstractly_unify_inst_functor_2(is_dead, not_reached, _, _, _, _, _,
-        not_reached, detism_erroneous, !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_dead, free, ConsId, Args, _ArgLives,
-        _Real, _, bound(unique, [bound_functor(ConsId, Args)]), detism_det,
-        !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_dead, any(Uniq, _), ConsId, ArgInsts,
-        _ArgLives, Real, Type, Inst, Det, !ModuleInfo) :-
+            make_any_inst_list_lives(ArgInstsB, is_live, ArgLives, Uniq, Real,
+                ArgInsts, Detism, !ModuleInfo),
+            Inst = bound(Uniq, [bound_functor(ConsIdB, ArgInsts)])
+        ;
+            InstA = bound(Uniq, BoundInstsA),
+            abstractly_unify_bound_inst_list_lives(BoundInstsA, ConsIdB, ArgInstsB,
+                ArgLives, Real, BoundInsts, Detism, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts)
+        ;
+            InstA = ground(Uniq, _),
+            make_ground_inst_list_lives(ArgInstsB, is_live, ArgLives, Uniq,
+                Real, ArgInsts, Detism, !ModuleInfo),
+            Inst = bound(Uniq, [bound_functor(ConsIdB, ArgInsts)])
+        ;
+            InstA = abstract_inst(_, _),
+            fail
+        )
+    ;
+        Live = is_dead,
+        (
+            InstA = not_reached,
+            Inst = not_reached,
+            Detism = detism_erroneous
+        ;
+            InstA = free,
+            Inst = bound(unique, [bound_functor(ConsIdB, ArgInstsB)]),
+            Detism = detism_det
+        ;
+            InstA = any(Uniq, _),
     \+ type_util.is_solver_type(!.ModuleInfo, Type),
-    make_any_inst_list(ArgInsts, is_dead, Uniq, Real, AnyArgInsts, Det,
-        !ModuleInfo),
-    Inst = bound(Uniq, [bound_functor(ConsId, AnyArgInsts)]).
-
-abstractly_unify_inst_functor_2(is_dead, bound(Uniq, ListX), ConsId, Args,
-        _ArgLives, Real, _, bound(Uniq, List), Det, !ModuleInfo) :-
-    ListY = [bound_functor(ConsId, Args)],
-    abstractly_unify_bound_inst_list(is_dead, ListX, ListY, Real, List, Det,
-        !ModuleInfo).
-
-abstractly_unify_inst_functor_2(is_dead, ground(Uniq, _), ConsId, ArgInsts,
-        _ArgLives, Real, _, Inst, Det, !ModuleInfo) :-
-    make_ground_inst_list(ArgInsts, is_dead, Uniq, Real, GroundArgInsts, Det,
-        !ModuleInfo),
-    Inst = bound(Uniq, [bound_functor(ConsId, GroundArgInsts)]).
-
-% abstractly_unify_inst_functor_2(is_dead, abstract_inst(_,_), _, _, _, _,
-%       _, _, _) :-
-%       fail.
+            make_any_inst_list(ArgInstsB, is_dead, Uniq, Real, ArgInsts,
+                Detism, !ModuleInfo),
+            Inst = bound(Uniq, [bound_functor(ConsIdB, ArgInsts)])
+        ;
+            InstA = bound(Uniq, BoundInstsA),
+            % ZZZ
+            BoundInstsB = [bound_functor(ConsIdB, ArgInstsB)],
+            abstractly_unify_bound_inst_list(is_dead, BoundInstsA, BoundInstsB,
+                Real, BoundInsts, Detism, !ModuleInfo),
+            Inst = bound(Uniq, BoundInsts)
+        ;
+            InstA = ground(UniqA, _),
+            make_ground_inst_list(ArgInstsB, is_dead, UniqA, Real,
+                ArgInsts, Detism, !ModuleInfo),
+            Inst = bound(UniqA, [bound_functor(ConsIdB, ArgInsts)])
+        ;
+            InstA = abstract_inst(_, _),
+            fail
+        )
+    ).
 
 %-----------------------------------------------------------------------------%
 
@@ -665,37 +746,38 @@
     % and determinism checking of the goal for the unification
     % predicate for the type.
     %
-:- pred abstractly_unify_bound_inst_list(is_live::in, list(bound_inst)::in,
-    list(bound_inst)::in, unify_is_real::in,
+:- pred abstractly_unify_bound_inst_list(is_live::in,
+    list(bound_inst)::in, list(bound_inst)::in, unify_is_real::in,
     list(bound_inst)::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-abstractly_unify_bound_inst_list(Live, Xs, Ys, Real, L, Det, !ModuleInfo) :-
-    ( ( Xs = [] ; Ys = [] ) ->
+abstractly_unify_bound_inst_list(Live, BoundInstsA, BoundInstsB, Real,
+        BoundInsts, Detism, !ModuleInfo) :-
+    ( ( BoundInstsA = [] ; BoundInstsB = [] ) ->
         % This probably shouldn't happen. If we get here, it means that
         % a previous goal had determinism `failure' or `erroneous',
         % but we should have optimized away the rest of the conjunction
         % after that goal.
 
-        L = [],
-        Det = detism_erroneous
+        BoundInsts = [],
+        Detism = detism_erroneous
     ;
-        abstractly_unify_bound_inst_list_2(Live, Xs, Ys, Real,
-            L, Det0, !ModuleInfo),
+        abstractly_unify_bound_inst_list_2(Live, BoundInstsA, BoundInstsB, Real,
+            BoundInsts, Detism0, !ModuleInfo),
 
         % If there are multiple alternatives for either of the inputs,
         % or the constructor of the single alternative for each input
         % doesn't match, then the unification can fail, so adjust the
         % determinism.
         (
-            Xs = [bound_functor(ConsIdX, _)],
-            Ys = [bound_functor(ConsIdY, _)],
-            equivalent_cons_ids(ConsIdX, ConsIdY)
+            BoundInstsA = [bound_functor(ConsIdA, _)],
+            BoundInstsB = [bound_functor(ConsIdB, _)],
+            equivalent_cons_ids(ConsIdA, ConsIdB)
         ->
-            Det = Det0
+            Detism = Detism0
         ;
-            determinism_components(Det0, _, MaxSoln),
-            determinism_components(Det, can_fail, MaxSoln)
+            determinism_components(Detism0, _, MaxSoln),
+            determinism_components(Detism, can_fail, MaxSoln)
         )
     ).
 
@@ -710,33 +792,36 @@
         !ModuleInfo).
 abstractly_unify_bound_inst_list_2(_, [_ | _], [], _, [], detism_failure,
         !ModuleInfo).
-abstractly_unify_bound_inst_list_2(Live, [X | Xs], [Y | Ys], Real, L, Det,
-        !ModuleInfo) :-
-    X = bound_functor(ConsIdX, ArgsX),
-    Y = bound_functor(ConsIdY, ArgsY),
-    ( equivalent_cons_ids(ConsIdX, ConsIdY) ->
-        abstractly_unify_inst_list(ArgsX, ArgsY, Live, Real,
-            Args, Det1, !ModuleInfo),
-        abstractly_unify_bound_inst_list_2(Live, Xs, Ys, Real,
-            L1, Det2, !ModuleInfo),
+abstractly_unify_bound_inst_list_2(Live,
+        [BoundInstA | BoundInstsA], [BoundInstB | BoundInstsB], Real,
+        BoundInsts, Detism, !ModuleInfo) :-
+    BoundInstA = bound_functor(ConsIdA, ArgsA),
+    BoundInstB = bound_functor(ConsIdB, ArgsB),
+    ( equivalent_cons_ids(ConsIdA, ConsIdB) ->
+        abstractly_unify_inst_list(ArgsA, ArgsB, Live, Real,
+            Args, Detism1, !ModuleInfo),
+        abstractly_unify_bound_inst_list_2(Live, BoundInstsA, BoundInstsB, Real,
+            BoundInstsTail, Detism2, !ModuleInfo),
 
         % If the unification of the two cons_ids is guaranteed
         % not to succeed, don't include it in the list.
-        ( determinism_components(Det1, _, at_most_zero) ->
-            L = L1
+        ( determinism_components(Detism1, _, at_most_zero) ->
+            BoundInsts = BoundInstsTail
         ;
-            L = [bound_functor(ConsIdX, Args) | L1]
+            BoundInsts = [bound_functor(ConsIdA, Args) | BoundInstsTail]
         ),
-        det_switch_detism(Det1, Det2, Det)
+        det_switch_detism(Detism1, Detism2, Detism)
     ;
-        ( compare(<, ConsIdX, ConsIdY) ->
-            abstractly_unify_bound_inst_list_2(Live, Xs, [Y | Ys],
-                Real, L, Det1, !ModuleInfo)
-        ;
-            abstractly_unify_bound_inst_list_2(Live, [X | Xs], Ys,
-                Real, L, Det1, !ModuleInfo)
+        ( compare(<, ConsIdA, ConsIdB) ->
+            abstractly_unify_bound_inst_list_2(Live,
+                BoundInstsA, [BoundInstB | BoundInstsB], Real, BoundInsts,
+                Detism1, !ModuleInfo)
+        ;
+            abstractly_unify_bound_inst_list_2(Live,
+                [BoundInstA | BoundInstsA], BoundInstsB, Real, BoundInsts,
+                Detism1, !ModuleInfo)
         ),
-        det_switch_detism(Det1, detism_failure, Det)
+        det_switch_detism(Detism1, detism_failure, Detism)
     ).
 
 :- pred abstractly_unify_bound_inst_list_lives(list(bound_inst)::in,
@@ -746,16 +831,16 @@
 
 abstractly_unify_bound_inst_list_lives([], _, _, _, _, [], detism_failure,
         !ModuleInfo).
-abstractly_unify_bound_inst_list_lives([X | Xs], ConsIdY, ArgsY, LivesY, Real,
-        L, Det, !ModuleInfo) :-
-    X = bound_functor(ConsIdX, ArgsX),
-    ( equivalent_cons_ids(ConsIdX, ConsIdY) ->
-        abstractly_unify_inst_list_lives(ArgsX, ArgsY, LivesY, Real,
-            Args, Det, !ModuleInfo),
-        L = [bound_functor(ConsIdX, Args)]
+abstractly_unify_bound_inst_list_lives([BoundInstA | BoundInstsA], ConsIdB, ArgsB,
+        LivesB, Real, BoundInsts, Detism, !ModuleInfo) :-
+    BoundInstA = bound_functor(ConsIdA, ArgsA),
+    ( equivalent_cons_ids(ConsIdA, ConsIdB) ->
+        abstractly_unify_inst_list_lives(ArgsA, ArgsB, LivesB, Real, Args,
+            Detism, !ModuleInfo),
+        BoundInsts = [bound_functor(ConsIdA, Args)]
     ;
-        abstractly_unify_bound_inst_list_lives(Xs, ConsIdY, ArgsY,
-            LivesY, Real, L, Det, !ModuleInfo)
+        abstractly_unify_bound_inst_list_lives(BoundInstsA, ConsIdB, ArgsB,
+            LivesB, Real, BoundInsts, Detism, !ModuleInfo)
     ).
 
 :- pred abstractly_unify_inst_list_lives(list(mer_inst)::in,
@@ -764,12 +849,12 @@
     module_info::in, module_info::out) is semidet.
 
 abstractly_unify_inst_list_lives([], [], [], _, [], detism_det, !ModuleInfo).
-abstractly_unify_inst_list_lives([X | Xs], [Y | Ys], [Live | Lives], Real,
-        [Z | Zs], Det, !ModuleInfo) :-
-    abstractly_unify_inst(Live, X, Y, Real, Z, Det1, !ModuleInfo),
-    abstractly_unify_inst_list_lives(Xs, Ys, Lives, Real, Zs, Det2,
-        !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+abstractly_unify_inst_list_lives([InstA | InstsA], [InstB | InstsB], [Live | Lives],
+        Real, [Inst | Insts], Detism, !ModuleInfo) :-
+    abstractly_unify_inst(Live, InstA, InstB, Real, Inst, Detism1, !ModuleInfo),
+    abstractly_unify_inst_list_lives(InstsA, InstsB, Lives, Real, Insts,
+        Detism2, !ModuleInfo),
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 %-----------------------------------------------------------------------------%
 
@@ -778,9 +863,9 @@
     determinism::out, module_info::in, module_info::out) is semidet.
 
 abstractly_unify_constrained_inst_vars(IsLive, InstVars, InstConstraint, InstB,
-        UnifyIsReal, Inst, Det, !ModuleInfo) :-
+        UnifyIsReal, Inst, Detism, !ModuleInfo) :-
     abstractly_unify_inst(IsLive, InstConstraint, InstB, UnifyIsReal,
-        Inst0, Det, !ModuleInfo),
+        Inst0, Detism, !ModuleInfo),
     ( \+ inst_matches_final(Inst0, InstConstraint, !.ModuleInfo) ->
         % The inst has become too instantiated so the
         % constrained_inst_vars must be removed.
@@ -823,49 +908,114 @@
 :- pred unify_uniq(is_live::in, unify_is_real::in, determinism::in,
     uniqueness::in, uniqueness::in, uniqueness::out) is semidet.
 
-unify_uniq(_, _, _, shared, shared, shared).
-unify_uniq(_, _, _, shared, unique, shared).
-unify_uniq(_, _, _, shared, mostly_unique, shared).
-unify_uniq(Live, Real, Det, shared, clobbered, clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-unify_uniq(Live, Real, Det, shared, mostly_clobbered, mostly_clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-
-unify_uniq(_, _, _, unique, shared, shared).
-unify_uniq(is_live, _, _, unique, unique, shared).
-unify_uniq(is_live, _, _, unique, mostly_unique, shared).
-unify_uniq(is_dead, _, _, unique, unique, unique).
-unify_uniq(is_dead, _, _, unique, mostly_unique, mostly_unique).
-        % XXX The above line is a conservative approximation;
-        % sometimes it should return unique not mostly_unique.
-unify_uniq(Live, Real, Det, unique, clobbered, clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-unify_uniq(Live, Real, Det, unique, mostly_clobbered, mostly_clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-
-unify_uniq(_, _, _, mostly_unique, shared, shared).
-unify_uniq(is_live, _, _, mostly_unique, unique, shared).
-unify_uniq(is_live, _, _, mostly_unique, mostly_unique, shared).
-unify_uniq(is_dead, _, _, mostly_unique, unique, mostly_unique).
-        % XXX The above line is a conservative approximation;
-        % sometimes it should return unique not mostly_unique.
-unify_uniq(is_dead, _, _, mostly_unique, mostly_unique, mostly_unique).
-unify_uniq(Live, Real, Det, mostly_unique, clobbered, clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-unify_uniq(Live, Real, Det, mostly_unique, mostly_clobbered,
-        mostly_clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-
-unify_uniq(Live, Real, Det, clobbered, _, clobbered) :-
-    allow_unify_with_clobbered(Live, Real, Det).
-
-unify_uniq(Live, Real, Det, mostly_clobbered, Uniq0, Uniq) :-
-    ( Uniq0 = clobbered ->
+unify_uniq(Live, Real, Detism, UniqA, UniqB, Uniq) :-
+    (
+        UniqA = shared,
+        UniqB = shared,
+        Uniq = shared
+    ;
+        UniqA = shared,
+        UniqB = unique,
+        Uniq = shared
+    ;
+        UniqA = shared,
+        UniqB = mostly_unique,
+        Uniq = shared
+    ;
+        UniqA = shared,
+        UniqB = clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
         Uniq = clobbered
     ;
+        UniqA = shared,
+        UniqB = mostly_clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
         Uniq = mostly_clobbered
-    ),
-    allow_unify_with_clobbered(Live, Real, Det).
+    ;
+        UniqA = unique,
+        UniqB = shared,
+        Uniq = shared
+    ;
+        UniqA = unique,
+        UniqB = unique,
+        Live = is_live,
+        Uniq = shared
+    ;
+        UniqA = unique,
+        UniqB = mostly_unique,
+        Live = is_live,
+        Uniq = shared
+    ;
+        UniqA = unique,
+        UniqB = unique,
+        Live = is_dead,
+        Uniq = unique
+    ;
+        UniqA = unique,
+        UniqB = mostly_unique,
+        Live = is_dead,
+        % XXX This is a conservative approximation;
+        % sometimes we should return unique, not mostly_unique.
+        Uniq = mostly_unique
+    ;
+        UniqA = unique,
+        UniqB = clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        Uniq = clobbered
+    ;
+        UniqA = unique,
+        UniqB = mostly_clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        Uniq = mostly_clobbered
+    ;
+        UniqA = mostly_unique,
+        UniqB = shared,
+        Uniq = shared
+    ;
+        UniqA = mostly_unique,
+        UniqB = unique,
+        Live = is_live,
+        Uniq = shared
+    ;
+        UniqA = mostly_unique,
+        UniqB = mostly_unique,
+        Live = is_live,
+        Uniq = shared
+    ;
+        UniqA = mostly_unique,
+        UniqB = unique,
+        Live = is_dead,
+        % XXX This is a conservative approximation;
+        % sometimes we should return unique, not mostly_unique.
+        Uniq = mostly_unique
+    ;
+        UniqA = mostly_unique,
+        UniqB = mostly_unique,
+        Live = is_dead,
+        Uniq = mostly_unique
+    ;
+        UniqA = mostly_unique,
+        UniqB = clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        Uniq = clobbered
+    ;
+        UniqA = mostly_unique,
+        UniqB = mostly_clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        Uniq = mostly_clobbered
+    ;
+        UniqA = clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        Uniq = clobbered
+    ;
+        UniqA = mostly_clobbered,
+        allow_unify_with_clobbered(Live, Real, Detism),
+        ( UniqB = clobbered ->
+            Uniq = clobbered
+        ;
+            Uniq = mostly_clobbered
+        )
+    ).
 
 :- pred allow_unify_with_clobbered(is_live::in, unify_is_real::in,
     determinism::in) is semidet.
@@ -879,8 +1029,8 @@
 
 :- pred check_not_clobbered(uniqueness::in, unify_is_real::in) is det.
 
-    % Sanity check.
 check_not_clobbered(Uniq, Real) :-
+    % Sanity check.
     ( Real = real_unify, Uniq = clobbered ->
         unexpected($module, $pred, "clobbered inst")
     ; Real = real_unify, Uniq = mostly_clobbered ->
@@ -898,29 +1048,29 @@
 
 make_ground_inst_list_lives([], _, _, _, _, [], detism_det, !ModuleInfo).
 make_ground_inst_list_lives([Inst0 | Insts0], Live, [ArgLive | ArgLives],
-        Uniq, Real, [Inst | Insts], Det, !ModuleInfo) :-
+        Uniq, Real, [Inst | Insts], Detism, !ModuleInfo) :-
     ( Live = is_live, ArgLive = is_live ->
         BothLive = is_live
     ;
         BothLive = is_dead
     ),
-    make_ground_inst(Inst0, BothLive, Uniq, Real, Inst, Det1,
+    make_ground_inst(Inst0, BothLive, Uniq, Real, Inst, Detism1,
         !ModuleInfo),
     make_ground_inst_list_lives(Insts0, Live, ArgLives, Uniq, Real,
-        Insts, Det2, !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+        Insts, Detism2, !ModuleInfo),
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 :- pred make_ground_inst_list(list(mer_inst)::in, is_live::in, uniqueness::in,
     unify_is_real::in, list(mer_inst)::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
 make_ground_inst_list([], _, _, _, [], detism_det, !ModuleInfo).
-make_ground_inst_list([Inst0 | Insts0], Live, Uniq, Real, [Inst | Insts], Det,
+make_ground_inst_list([Inst0 | Insts0], Live, Uniq, Real, [Inst | Insts], Detism,
         !ModuleInfo) :-
-    make_ground_inst(Inst0, Live, Uniq, Real, Inst, Det1, !ModuleInfo),
-    make_ground_inst_list(Insts0, Live, Uniq, Real, Insts, Det2,
+    make_ground_inst(Inst0, Live, Uniq, Real, Inst, Detism1, !ModuleInfo),
+    make_ground_inst_list(Insts0, Live, Uniq, Real, Insts, Detism2,
         !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
     % Abstractly unify an inst with `ground' and calculate the new inst
     % and the determinism of the unification.
@@ -929,54 +1079,69 @@
     unify_is_real::in, mer_inst::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-make_ground_inst(not_reached, _, _, _, not_reached, detism_erroneous,
-        !ModuleInfo).
-make_ground_inst(any(Uniq0, HOInstInfo), IsLive, Uniq1, Real,
-        ground(Uniq, HOInstInfo), detism_semi, !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq).
-make_ground_inst(free, IsLive, Uniq0, Real, ground(Uniq, none), detism_det,
-        !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_det, unique, Uniq0, Uniq).
-make_ground_inst(free(T), IsLive, Uniq0, Real,
-        defined_inst(typed_ground(Uniq, T)), detism_det, !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_det, unique, Uniq0, Uniq).
-make_ground_inst(bound(Uniq0, BoundInsts0), IsLive, Uniq1, Real,
-        bound(Uniq, BoundInsts), Det, !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq),
-    make_ground_bound_inst_list(BoundInsts0, IsLive, Uniq1, Real,
-        BoundInsts, Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-make_ground_inst(ground(Uniq0, HOInstInfo), IsLive, Uniq1, Real,
-        ground(Uniq, HOInstInfo), detism_semi, !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq).
-make_ground_inst(inst_var(_), _, _, _, _, _, _, _) :-
-    unexpected($module, $pred, "free inst var").
-make_ground_inst(constrained_inst_vars(InstVars, InstConstraint), IsLive,
-        Uniq, Real, Inst, Det, !ModuleInfo) :-
-    abstractly_unify_constrained_inst_vars(IsLive, InstVars,
-        InstConstraint, ground(Uniq, none), Real, Inst, Det, !ModuleInfo).
-make_ground_inst(abstract_inst(_,_), _, _, _, ground(shared, none),
-        detism_semi, !ModuleInfo).
-make_ground_inst(defined_inst(InstName), IsLive, Uniq, Real, Inst, Det,
-        !ModuleInfo) :-
+make_ground_inst(Inst0, Live, Uniq1, Real, Inst, Detism, !ModuleInfo) :-
+    (
+        Inst0 = not_reached,
+        Inst = not_reached,
+        Detism = detism_erroneous
+    ;
+        Inst0 = any(Uniq0, HOInstInfo),
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        Inst = ground(Uniq, HOInstInfo),
+        Detism = detism_semi
+    ;
+        Inst0 = free,
+        unify_uniq(Live, Real, detism_det, unique, Uniq1, Uniq),
+        Inst = ground(Uniq, none),
+        Detism = detism_det
+    ;
+        Inst0 = free(T),
+        unify_uniq(Live, Real, detism_det, unique, Uniq1, Uniq),
+        Inst = defined_inst(typed_ground(Uniq, T)),
+        Detism = detism_det
+    ;
+        Inst0 = bound(Uniq0, BoundInsts0),
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        make_ground_bound_inst_list(BoundInsts0, Live, Uniq1, Real,
+            BoundInsts, Detism1, !ModuleInfo),
+        Inst = bound(Uniq, BoundInsts),
+        det_par_conjunction_detism(Detism1, detism_semi, Detism)
+    ;
+        Inst0 = ground(Uniq0, HOInstInfo),
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        Inst = ground(Uniq, HOInstInfo),
+        Detism = detism_semi
+    ;
+        Inst0 = inst_var(_),
+        unexpected($module, $pred, "free inst var")
+    ;
+        Inst0 = constrained_inst_vars(InstVars, SubInst0),
+        abstractly_unify_constrained_inst_vars(Live, InstVars,
+            SubInst0, ground(Uniq1, none), Real, Inst, Detism, !ModuleInfo)
+    ;
+        Inst0 = abstract_inst(_, _),
+        Inst = ground(shared, none),
+        Detism = detism_semi
+    ;
+        Inst0 = defined_inst(InstName),
     % Check whether the inst name is already in the ground_inst table.
     module_info_get_inst_table(!.ModuleInfo, InstTable0),
     inst_table_get_ground_insts(InstTable0, GroundInsts0),
-    GroundInstKey = ground_inst(InstName, IsLive, Uniq, Real),
+        GroundInstKey = ground_inst(InstName, Live, Uniq1, Real),
     ( map.search(GroundInsts0, GroundInstKey, Result) ->
-        ( Result = inst_det_known(GroundInst0, Det0) ->
+            ( Result = inst_det_known(GroundInst0, Detism0) ->
             GroundInst = GroundInst0,
-            Det = Det0
+                Detism = Detism0
         ;
             GroundInst = defined_inst(GroundInstKey),
-            Det = detism_det
+                Detism = detism_det
             % We can safely assume this is det, since if it were semidet,
             % we would have noticed this in the process of unfolding the
             % definition.
         )
     ;
-        % Insert the inst name in the ground_inst table, with value `unknown'
-        % for the moment.
+            % Insert the inst name in the ground_inst table, with value
+            % `unknown' for the moment.
         map.det_insert(GroundInstKey, inst_det_unknown,
             GroundInsts0, GroundInsts1),
         inst_table_set_ground_insts(GroundInsts1, InstTable0, InstTable1),
@@ -984,16 +1149,17 @@
 
         % Expand the inst name, and invoke ourself recursively on its
         % expansion.
-        inst_lookup(!.ModuleInfo, InstName, Inst0),
-        inst_expand(!.ModuleInfo, Inst0, Inst1),
-        make_ground_inst(Inst1, IsLive, Uniq, Real, GroundInst, Det,
+            inst_lookup(!.ModuleInfo, InstName, SubInst0),
+            inst_expand(!.ModuleInfo, SubInst0, SubInst1),
+            make_ground_inst(SubInst1, Live, Uniq1, Real, GroundInst, Detism,
             !ModuleInfo),
 
-        % Now that we have determined the resulting Inst, store the appropriate
-        % value `known(GroundInst, Det)' in the ground_inst table.
+            % Now that we have determined the resulting Inst, store the
+            % appropriate value `known(GroundInst, Detism)' in the ground_inst
+            % table.
         module_info_get_inst_table(!.ModuleInfo, InstTable2),
         inst_table_get_ground_insts(InstTable2, GroundInsts2),
-        map.det_update(GroundInstKey, inst_det_known(GroundInst, Det),
+            map.det_update(GroundInstKey, inst_det_known(GroundInst, Detism),
             GroundInsts2, GroundInsts),
         inst_table_set_ground_insts(GroundInsts, InstTable2, InstTable),
         module_info_set_inst_table(InstTable, !ModuleInfo)
@@ -1003,6 +1169,7 @@
         Inst = defined_inst(GroundInstKey)
     ;
         Inst = GroundInst
+        )
     ).
 
 :- pred make_ground_bound_inst_list(list(bound_inst)::in, is_live::in,
@@ -1012,14 +1179,14 @@
 
 make_ground_bound_inst_list([], _, _, _, [], detism_det, !ModuleInfo).
 make_ground_bound_inst_list([Bound0 | Bounds0], IsLive, Uniq, Real,
-            [Bound | Bounds], Det, !ModuleInfo) :-
+            [Bound | Bounds], Detism, !ModuleInfo) :-
     Bound0 = bound_functor(ConsId, ArgInsts0),
-    make_ground_inst_list(ArgInsts0, IsLive, Uniq, Real, ArgInsts, Det1,
+    make_ground_inst_list(ArgInsts0, IsLive, Uniq, Real, ArgInsts, Detism1,
         !ModuleInfo),
     Bound = bound_functor(ConsId, ArgInsts),
-    make_ground_bound_inst_list(Bounds0, IsLive, Uniq, Real, Bounds, Det2,
+    make_ground_bound_inst_list(Bounds0, IsLive, Uniq, Real, Bounds, Detism2,
         !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 %-----------------------------------------------------------------------------%
 
@@ -1030,54 +1197,69 @@
     unify_is_real::in, mer_inst::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
-make_any_inst(not_reached, _, _, _, not_reached, detism_erroneous, !ModuleInfo).
-make_any_inst(any(Uniq0, HOInstInfo), IsLive, Uniq1, Real,
-        any(Uniq, HOInstInfo), detism_semi, !ModuleInfo) :-
+make_any_inst(Inst0, Live, Uniq1, Real, Inst, Detism, !ModuleInfo) :-
+    (
+        Inst0 = not_reached,
+        Inst = not_reached,
+        Detism = detism_erroneous
+    ;
+        Inst0 = any(Uniq0, HOInstInfo),
     allow_unify_bound_any(Real),
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq).
-make_any_inst(free, IsLive, Uniq0, Real, any(Uniq, none), detism_det,
-        !ModuleInfo) :-
-    unify_uniq(IsLive, Real, detism_det, unique, Uniq0, Uniq).
-make_any_inst(free(T), IsLive, Uniq, Real, defined_inst(Any), detism_det,
-        !ModuleInfo) :-
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        Inst = any(Uniq, HOInstInfo),
+        Detism = detism_semi
+    ;
+        Inst0 = free,
+        unify_uniq(Live, Real, detism_det, unique, Uniq1, Uniq),
+        Inst = any(Uniq, none),
+        Detism = detism_det
+    ;
+        Inst0 = free(T),
     % The following is a round-about way of doing this
-    %   unify_uniq(IsLive, Real, detism_det, unique, Uniq0, Uniq),
+        %   unify_uniq(Live, Real, detism_det, unique, Uniq0, Uniq),
     %   Any = typed_any(Uniq, T).
     % without the need for a `typed_any' inst.
-    Any = typed_inst(T, unify_inst(IsLive, free, any(Uniq, none), Real)).
-make_any_inst(bound(Uniq0, BoundInsts0), IsLive, Uniq1, Real,
-        bound(Uniq, BoundInsts), Det, !ModuleInfo) :-
+        Any = typed_inst(T, unify_inst(Live, free, any(Uniq1, none), Real)),
+        Inst = defined_inst(Any),
+        Detism = detism_det
+    ;
+        Inst0 = bound(Uniq0, BoundInsts0),
     allow_unify_bound_any(Real),
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq),
-    make_any_bound_inst_list(BoundInsts0, IsLive, Uniq1, Real,
-        BoundInsts, Det1, !ModuleInfo),
-    det_par_conjunction_detism(Det1, detism_semi, Det).
-make_any_inst(ground(Uniq0, PredInst), IsLive, Uniq1, Real,
-        ground(Uniq, PredInst), detism_semi, !ModuleInfo) :-
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        make_any_bound_inst_list(BoundInsts0, Live, Uniq1, Real, BoundInsts,
+            Detism1, !ModuleInfo),
+        Inst = bound(Uniq, BoundInsts),
+        det_par_conjunction_detism(Detism1, detism_semi, Detism)
+    ;
+        Inst0 = ground(Uniq0, PredInst),
     allow_unify_bound_any(Real),
-    unify_uniq(IsLive, Real, detism_semi, Uniq0, Uniq1, Uniq).
-make_any_inst(inst_var(_), _, _, _, _, _, _, _) :-
-    unexpected($module, $pred, "free inst var").
-make_any_inst(constrained_inst_vars(InstVars, InstConstraint), IsLive,
-        Uniq, Real, Inst, Det, !ModuleInfo) :-
-    abstractly_unify_constrained_inst_vars(IsLive, InstVars,
-        InstConstraint, any(Uniq, none), Real, Inst, Det, !ModuleInfo).
-make_any_inst(abstract_inst(_,_), _, _, _, any(shared, none), detism_semi,
-        !ModuleInfo).
-make_any_inst(defined_inst(InstName), IsLive, Uniq, Real, Inst, Det,
-        !ModuleInfo) :-
-        % check whether the inst name is already in the
-        % any_inst table
+        unify_uniq(Live, Real, detism_semi, Uniq0, Uniq1, Uniq),
+        Inst = ground(Uniq, PredInst),
+        Detism = detism_semi
+    ;
+        Inst0 = inst_var(_),
+        unexpected($module, $pred, "free inst var")
+    ;
+        Inst0 = constrained_inst_vars(InstVars, SubInst0),
+        abstractly_unify_constrained_inst_vars(Live, InstVars,
+            SubInst0, any(Uniq1, none), Real, Inst, Detism, !ModuleInfo)
+    ;
+        Inst0 = abstract_inst(_, _),
+        Inst = any(shared, none),
+        Detism = detism_semi
+    ;
+        Inst0 = defined_inst(InstName),
+        % Check whether the inst name is already in the any_inst table.
     module_info_get_inst_table(!.ModuleInfo, InstTable0),
     inst_table_get_any_insts(InstTable0, AnyInsts0),
-    AnyInstKey = any_inst(InstName, IsLive, Uniq, Real),
+        AnyInstKey = any_inst(InstName, Live, Uniq1, Real),
     ( map.search(AnyInsts0, AnyInstKey, Result) ->
-        ( Result = inst_det_known(AnyInst0, Det0) ->
+            ( Result = inst_det_known(AnyInst0, Detism0) ->
             AnyInst = AnyInst0,
-            Det = Det0
+                Detism = Detism0
         ;
             AnyInst = defined_inst(AnyInstKey),
-            Det = detism_det
+                Detism = detism_det
             % We can safely assume this is det, since if it were semidet,
             % we would have noticed this in the process of unfolding the
             % definition.
@@ -1091,15 +1273,16 @@
 
         % Expand the inst name, and invoke ourself recursively on its
         % expansion.
-        inst_lookup(!.ModuleInfo, InstName, Inst0),
-        inst_expand(!.ModuleInfo, Inst0, Inst1),
-        make_any_inst(Inst1, IsLive, Uniq, Real, AnyInst, Det, !ModuleInfo),
+            inst_lookup(!.ModuleInfo, InstName, SubInst0),
+            inst_expand(!.ModuleInfo, SubInst0, SubInst1),
+            make_any_inst(SubInst1, Live, Uniq1, Real, AnyInst, Detism,
+                !ModuleInfo),
 
-        % Now that we have determined the resulting Inst, store the appropriate
-        % value `known(AnyInst, Det)' in the any_inst table.
+            % Now that we have determined the resulting Inst, store the
+            % appropriate value `known(AnyInst, Detism)' in the any_inst table.
         module_info_get_inst_table(!.ModuleInfo, InstTable2),
         inst_table_get_any_insts(InstTable2, AnyInsts2),
-        map.det_update(AnyInstKey, inst_det_known(AnyInst, Det),
+            map.det_update(AnyInstKey, inst_det_known(AnyInst, Detism),
             AnyInsts2, AnyInsts),
         inst_table_set_any_insts(AnyInsts, InstTable2, InstTable),
         module_info_set_inst_table(InstTable, !ModuleInfo)
@@ -1109,6 +1292,7 @@
         Inst = defined_inst(AnyInstKey)
     ;
         Inst = AnyInst
+        )
     ).
 
 :- pred make_any_bound_inst_list(list(bound_inst)::in, is_live::in,
@@ -1118,25 +1302,25 @@
 
 make_any_bound_inst_list([], _, _, _, [], detism_det, !ModuleInfo).
 make_any_bound_inst_list([Bound0 | Bounds0], IsLive, Uniq, Real,
-        [Bound | Bounds], Det, !ModuleInfo) :-
+        [Bound | Bounds], Detism, !ModuleInfo) :-
     Bound0 = bound_functor(ConsId, ArgInsts0),
     make_any_inst_list(ArgInsts0, IsLive, Uniq, Real,
-        ArgInsts, Det1, !ModuleInfo),
+        ArgInsts, Detism1, !ModuleInfo),
     Bound = bound_functor(ConsId, ArgInsts),
-    make_any_bound_inst_list(Bounds0, IsLive, Uniq, Real, Bounds, Det2,
+    make_any_bound_inst_list(Bounds0, IsLive, Uniq, Real, Bounds, Detism2,
         !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 :- pred make_any_inst_list(list(mer_inst)::in, is_live::in, uniqueness::in,
     unify_is_real::in, list(mer_inst)::out, determinism::out,
     module_info::in, module_info::out) is semidet.
 
 make_any_inst_list([], _, _, _, [], detism_det, !ModuleInfo).
-make_any_inst_list([Inst0 | Insts0], Live, Uniq, Real, [Inst | Insts], Det,
+make_any_inst_list([Inst0 | Insts0], Live, Uniq, Real, [Inst | Insts], Detism,
         !ModuleInfo) :-
-    make_any_inst(Inst0, Live, Uniq, Real, Inst, Det1, !ModuleInfo),
-    make_any_inst_list(Insts0, Live, Uniq, Real, Insts, Det2, !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+    make_any_inst(Inst0, Live, Uniq, Real, Inst, Detism1, !ModuleInfo),
+    make_any_inst_list(Insts0, Live, Uniq, Real, Insts, Detism2, !ModuleInfo),
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 :- pred make_any_inst_list_lives(list(mer_inst)::in, is_live::in,
     list(is_live)::in, uniqueness::in, unify_is_real::in,
@@ -1145,16 +1329,16 @@
 
 make_any_inst_list_lives([], _, _, _, _, [], detism_det, !ModuleInfo).
 make_any_inst_list_lives([Inst0 | Insts0], Live, [ArgLive | ArgLives],
-        Uniq, Real, [Inst | Insts], Det, !ModuleInfo) :-
+        Uniq, Real, [Inst | Insts], Detism, !ModuleInfo) :-
     ( Live = is_live, ArgLive = is_live ->
         BothLive = is_live
     ;
         BothLive = is_dead
     ),
-    make_any_inst(Inst0, BothLive, Uniq, Real, Inst, Det1, !ModuleInfo),
+    make_any_inst(Inst0, BothLive, Uniq, Real, Inst, Detism1, !ModuleInfo),
     make_any_inst_list_lives(Insts0, Live, ArgLives, Uniq, Real,
-        Insts, Det2, !ModuleInfo),
-    det_par_conjunction_detism(Det1, Det2, Det).
+        Insts, Detism2, !ModuleInfo),
+    det_par_conjunction_detism(Detism1, Detism2, Detism).
 
 %-----------------------------------------------------------------------------%
 
@@ -1188,33 +1372,41 @@
 :- pred make_shared_inst(mer_inst::in, mer_inst::out,
     module_info::in, module_info::out) is det.
 
-make_shared_inst(not_reached, not_reached, !ModuleInfo).
-make_shared_inst(any(Uniq0, HOInstInfo), any(Uniq, HOInstInfo), !ModuleInfo) :-
-    make_shared(Uniq0, Uniq).
-make_shared_inst(free, free, !ModuleInfo) :-
+make_shared_inst(Inst0, Inst, !ModuleInfo) :-
+    (
+        Inst0 = not_reached,
+        Inst = Inst0
+    ;
+        Inst0 = free,
     % The caller should ensure that this never happens.
-    unexpected($module, $pred, "cannot make shared version of `free'").
-make_shared_inst(free(T), free(T), !ModuleInfo) :-
+        unexpected($module, $pred, "cannot make shared version of `free'")
+    ;
+        Inst0 = free(_),
     % The caller should ensure that this never happens.
-    unexpected($module, $pred, "cannot make shared version of `free(T)'").
-make_shared_inst(bound(Uniq0, BoundInsts0), bound(Uniq, BoundInsts),
-        !ModuleInfo) :-
+        unexpected($module, $pred, "cannot make shared version of `free(T)'")
+    ;
+        Inst0 = any(Uniq0, HOInstInfo),
+        make_shared(Uniq0, Uniq),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        Inst0 = bound(Uniq0, BoundInsts0),
     % XXX This code has a performance problem.
     %
-    % The problem is that e.g. in a list of length N, you'll have N variables
-    % for the skeletons whose insts contain an average of N/2 occurences of
-    % `bound' each, so the complexity of running make_shared_inst on all their
-    % insts is quadratic in N.
+        % The problem is that e.g. in a list of length N, you will have
+        % N variables for the skeletons whose insts contain an average of
+        % N/2 occurences of `bound' each, so the complexity of running
+        % make_shared_inst on all their insts is quadratic in N.
     %
     % One potential way to fix this would be to introduce a new function
-    % symbol for insts, make_shared(mer_inst), which would have the meaning of
-    % requiring any compiler component that finds it to run make_shared_inst
-    % on its argument before using it. That would require parameterizing
-    % make_shared_inst to say whether it is being used in such a manner.
+        % symbol for insts, make_shared(mer_inst), which would have the meaning
+        % of requiring any compiler component that finds it to run
+        % make_shared_inst on its argument before using it. That would require
+        % parameterizing make_shared_inst to say whether it is being used
+        % in such a manner.
     %
     % Another similar fix would be to add an extra argument to bound/2
-    % to say whether the insts in its last argument should implicitly be made
-    % shared.
+        % to say whether the insts in its last argument should implicitly be
+        % made shared.
     %
     % If Uniq0 = shared, then all the other cells below it should also be
     % shared as well, which means we should be able to avoid the call to
@@ -1223,22 +1415,28 @@
     % construction of large ground terms, Uniq0 will in fact be `unique'.
 
     make_shared(Uniq0, Uniq),
-    make_shared_bound_inst_list(BoundInsts0, BoundInsts, !ModuleInfo).
-make_shared_inst(ground(Uniq0, PredInst), ground(Uniq, PredInst),
-        !ModuleInfo) :-
-    make_shared(Uniq0, Uniq).
-make_shared_inst(inst_var(_), _, _, _) :-
-    unexpected($module, $pred, "free inst var").
-make_shared_inst(constrained_inst_vars(InstVars, Inst0), Inst, !ModuleInfo) :-
-    make_shared_inst(Inst0, Inst1, !ModuleInfo),
-    ( \+ inst_matches_final(Inst1, Inst0, !.ModuleInfo) ->
-        Inst = Inst1
+        make_shared_bound_inst_list(BoundInsts0, BoundInsts, !ModuleInfo),
+        Inst = bound(Uniq, BoundInsts)
     ;
-        Inst = constrained_inst_vars(InstVars, Inst1)
-    ).
-make_shared_inst(abstract_inst(_,_), _, !ModuleInfo) :-
-    unexpected($module, $pred, "abstract_inst").
-make_shared_inst(defined_inst(InstName), Inst, !ModuleInfo) :-
+        Inst0 = ground(Uniq0, PredInst),
+        make_shared(Uniq0, Uniq),
+        Inst = ground(Uniq, PredInst)
+    ;
+        Inst0 = inst_var(_),
+        unexpected($module, $pred, "free inst var")
+    ;
+        Inst0 = constrained_inst_vars(InstVars, SubInst0),
+        make_shared_inst(SubInst0, SubInst1, !ModuleInfo),
+        ( \+ inst_matches_final(SubInst1, SubInst0, !.ModuleInfo) ->
+            Inst = SubInst1
+        ;
+            Inst = constrained_inst_vars(InstVars, SubInst1)
+        )
+    ;
+        Inst0 = abstract_inst(_, _),
+        unexpected($module, $pred, "abstract_inst")
+    ;
+        Inst0 = defined_inst(InstName),
     % Check whether the inst name is already in the shared_inst table.
     module_info_get_inst_table(!.ModuleInfo, InstTable0),
     inst_table_get_shared_insts(InstTable0, SharedInsts0),
@@ -1249,20 +1447,20 @@
             SharedInst = defined_inst(InstName)
         )
     ;
-        % Insert the inst name in the shared_inst table, with value `unknown'
-        % for the moment.
+            % Insert the inst name in the shared_inst table, with value
+            % `unknown' for the moment.
         map.det_insert(InstName, inst_unknown, SharedInsts0, SharedInsts1),
         inst_table_set_shared_insts(SharedInsts1, InstTable0, InstTable1),
         module_info_set_inst_table(InstTable1, !ModuleInfo),
 
         % Expand the inst name, and invoke ourself recursively on its
         % expansion.
-        inst_lookup(!.ModuleInfo, InstName, Inst0),
-        inst_expand(!.ModuleInfo, Inst0, Inst1),
-        make_shared_inst(Inst1, SharedInst, !ModuleInfo),
+            inst_lookup(!.ModuleInfo, InstName, SubInst0),
+            inst_expand(!.ModuleInfo, SubInst0, SubInst1),
+            make_shared_inst(SubInst1, SharedInst, !ModuleInfo),
 
-        % Now that we have determined the resulting Inst, store the appropriate
-        % value `known(SharedInst)' in the shared_inst table.
+            % Now that we have determined the resulting Inst, store the
+            % appropriate value `known(SharedInst)' in the shared_inst table.
         module_info_get_inst_table(!.ModuleInfo, InstTable2),
         inst_table_get_shared_insts(InstTable2, SharedInsts2),
         map.det_update(InstName, inst_known(SharedInst),
@@ -1275,6 +1473,7 @@
         Inst = defined_inst(InstName)
     ;
         Inst = SharedInst
+        )
     ).
 
 :- pred make_shared(uniqueness::in, uniqueness::out) is det.
@@ -1298,37 +1497,43 @@
 
 %-----------------------------------------------------------------------------%
 
-    % Make an inst mostly-uniq: replace all occurrences of `unique'
-    % in the inst with `mostly_unique'.  (Used by unique_modes.m to
-    % change the insts of semidet-live or nondet-live insts.)
-    %
-make_mostly_uniq_inst(not_reached, not_reached, !ModuleInfo).
-make_mostly_uniq_inst(any(Uniq0, HOInstInfo), any(Uniq, HOInstInfo),
-        !ModuleInfo) :-
-    make_mostly_uniq(Uniq0, Uniq).
-make_mostly_uniq_inst(free, free, !ModuleInfo).
-make_mostly_uniq_inst(free(T), free(T), !ModuleInfo).
-make_mostly_uniq_inst(bound(Uniq0, BoundInsts0), bound(Uniq, BoundInsts),
-        !ModuleInfo) :-
+make_mostly_uniq_inst(Inst0, Inst, !ModuleInfo) :-
+    (
+        ( Inst0 = not_reached
+        ; Inst0 = free
+        ; Inst0 = free(_)
+        ),
+        Inst = Inst0
+    ;
+        Inst0 = any(Uniq0, HOInstInfo),
+        make_mostly_uniq(Uniq0, Uniq),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        Inst0 = bound(Uniq0, BoundInsts0),
         % XXX could improve efficiency by avoiding recursion here
     make_mostly_uniq(Uniq0, Uniq),
-    make_mostly_uniq_bound_inst_list(BoundInsts0, BoundInsts, !ModuleInfo).
-make_mostly_uniq_inst(ground(Uniq0, PredInst), ground(Uniq, PredInst),
-        !ModuleInfo) :-
-    make_mostly_uniq(Uniq0, Uniq).
-make_mostly_uniq_inst(inst_var(_), _, _, _) :-
-    unexpected($module, $pred, "free inst var").
-make_mostly_uniq_inst(constrained_inst_vars(InstVars, Inst0), Inst,
-        !ModuleInfo) :-
-    make_mostly_uniq_inst(Inst0, Inst1, !ModuleInfo),
-    ( \+ inst_matches_final(Inst1, Inst0, !.ModuleInfo) ->
-        Inst = Inst1
+        make_mostly_uniq_bound_inst_list(BoundInsts0, BoundInsts, !ModuleInfo),
+        Inst = bound(Uniq, BoundInsts)
     ;
-        Inst = constrained_inst_vars(InstVars, Inst1)
-    ).
-make_mostly_uniq_inst(abstract_inst(_,_), _, !ModuleInfo) :-
-    unexpected($module, $pred, "abstract_inst").
-make_mostly_uniq_inst(defined_inst(InstName), Inst, !ModuleInfo) :-
+        Inst0 = ground(Uniq0, PredInst),
+        make_mostly_uniq(Uniq0, Uniq),
+        Inst = ground(Uniq, PredInst)
+    ;
+        Inst0 = inst_var(_),
+        unexpected($module, $pred, "free inst var")
+    ;
+        Inst0 = constrained_inst_vars(InstVars, SubInst0),
+        make_mostly_uniq_inst(SubInst0, SubInst, !ModuleInfo),
+        ( \+ inst_matches_final(SubInst, SubInst0, !.ModuleInfo) ->
+            Inst = SubInst
+        ;
+            Inst = constrained_inst_vars(InstVars, SubInst)
+        )
+    ;
+        Inst0 = abstract_inst(_, _),
+        unexpected($module, $pred, "abstract_inst")
+    ;
+        Inst0 = defined_inst(InstName),
     % Check whether the inst name is already in the mostly_uniq_inst table.
     module_info_get_inst_table(!.ModuleInfo, InstTable0),
     inst_table_get_mostly_uniq_insts(InstTable0, NondetLiveInsts0),
@@ -1339,8 +1544,8 @@
             NondetLiveInst = defined_inst(InstName)
         )
     ;
-        % Insert the inst name in the mostly_uniq_inst table, with value
-        % `unknown' for the moment.
+            % Insert the inst name in the mostly_uniq_inst table,
+            % with value `unknown' for the moment.
         map.det_insert(InstName, inst_unknown,
             NondetLiveInsts0, NondetLiveInsts1),
         inst_table_set_mostly_uniq_insts(NondetLiveInsts1,
@@ -1349,12 +1554,13 @@
 
         % Expand the inst name, and invoke ourself recursively on its
         % expansion.
-        inst_lookup(!.ModuleInfo, InstName, Inst0),
-        inst_expand(!.ModuleInfo, Inst0, Inst1),
-        make_mostly_uniq_inst(Inst1, NondetLiveInst, !ModuleInfo),
-
-        % Now that we have determined the resulting Inst, store the appropriate
-        % value `known(NondetLiveInst)' in the mostly_uniq_inst table.
+            inst_lookup(!.ModuleInfo, InstName, SubInst0),
+            inst_expand(!.ModuleInfo, SubInst0, SubInst1),
+            make_mostly_uniq_inst(SubInst1, NondetLiveInst, !ModuleInfo),
+
+            % Now that we have determined the resulting Inst, store the
+            % appropriate value `known(NondetLiveInst)' in the
+            % mostly_uniq_inst table.
         module_info_get_inst_table(!.ModuleInfo, InstTable2),
         inst_table_get_mostly_uniq_insts(InstTable2, NondetLiveInsts2),
         map.det_update(InstName, inst_known(NondetLiveInst),
@@ -1368,6 +1574,7 @@
         Inst = defined_inst(InstName)
     ;
         Inst = NondetLiveInst
+        )
     ).
 
 :- pred make_mostly_uniq(uniqueness::in, uniqueness::out) is det.
@@ -1406,7 +1613,8 @@
     %
 :- pred allow_unify_bound_any(unify_is_real::in) is det.
 
-allow_unify_bound_any(_) :- true.
+allow_unify_bound_any(_) :-
+    true.
 
 %-----------------------------------------------------------------------------%
 
@@ -1450,128 +1658,169 @@
     mer_inst::out, module_info::in, module_info::out) is semidet.
 
 inst_merge_2(InstA, InstB, MaybeType, Inst, !ModuleInfo) :-
-%   % would this test improve efficiency??
+%   % XXX Would this test improve efficiency?s
+%   % What if we compared the addresses?
 %   ( InstA = InstB ->
 %       Inst = InstA,
 %   ;
-    inst_expand(!.ModuleInfo, InstA, InstA2),
-    inst_expand(!.ModuleInfo, InstB, InstB2),
-    ( InstB2 = not_reached ->
-        Inst = InstA2
+    inst_expand(!.ModuleInfo, InstA, ExpandedInstA),
+    inst_expand(!.ModuleInfo, InstB, ExpandedInstB),
+    ( ExpandedInstB = not_reached ->
+        Inst = ExpandedInstA
     ;
-        inst_merge_3(InstA2, InstB2, MaybeType, Inst, !ModuleInfo)
+        inst_merge_3(ExpandedInstA, ExpandedInstB, MaybeType, Inst,
+            !ModuleInfo)
     ).
 
 :- pred inst_merge_3(mer_inst::in, mer_inst::in, maybe(mer_type)::in,
     mer_inst::out, module_info::in, module_info::out) is semidet.
 
 inst_merge_3(InstA, InstB, MaybeType, Inst, !ModuleInfo) :-
-    ( InstA = constrained_inst_vars(InstVarsA, InstA1) ->
-        ( InstB = constrained_inst_vars(InstVarsB, InstB1) ->
-            inst_merge(InstA1, InstB1, MaybeType, Inst0, !ModuleInfo),
-            InstVars = InstVarsA `set.intersect` InstVarsB,
+    ( InstA = constrained_inst_vars(InstVarsA, SubInstA) ->
+        ( InstB = constrained_inst_vars(InstVarsB, SubInstB) ->
+            inst_merge(SubInstA, SubInstB, MaybeType, Inst0, !ModuleInfo),
+            set.intersect(InstVarsA, InstVarsB, InstVars),
             ( set.non_empty(InstVars) ->
                 Inst = constrained_inst_vars(InstVars, Inst0)
                 % We can keep the constrained_inst_vars here since
-                % Inst0 = InstA1 `lub` InstB1 and the original constraint
+                % Inst0 = SubInstA `lub` SubInstB and the original constraint
                 % on the InstVars, InstC, must have been such that
-                % InstA1 `lub` InstB1 =< InstC.
+                % SubInstA `lub` SubInstB =< InstC.
             ;
                 Inst = Inst0
             )
         ;
-            inst_merge(InstA1, InstB, MaybeType, Inst, !ModuleInfo)
+            inst_merge(SubInstA, InstB, MaybeType, Inst, !ModuleInfo)
         )
     ;
+        % XXX Why don't we look for InstB = constrained_inst_vars/2 here?
         inst_merge_4(InstA, InstB, MaybeType, Inst, !ModuleInfo)
     ).
 
 :- pred inst_merge_4(mer_inst::in, mer_inst::in, maybe(mer_type)::in,
     mer_inst::out, module_info::in, module_info::out) is semidet.
 
-% We do not yet allow merging of `free' and `any',
-% except in the case where the any is `mostly_clobbered_any'
-% or `clobbered_any', because that would require inserting
-% additional code to initialize the free var.
-%
-% We do NOT plan to allow merging of `free' and `ground'
-% to produce `any', because that would introduce `any'
-% insts even for builtin types such as `int' which can't
-% support `any'.  It might also make the mode system
-% too weak -- it might not be able to detect bugs as well
-% as it can currently.
+inst_merge_4(InstA, InstB, MaybeType, Inst, !ModuleInfo) :-
+    % We do not yet allow merging of `free' and `any', except in the case
+    % where the any is `mostly_clobbered_any' or `clobbered_any', because
+    % that would require inserting additional code to initialize the free var.
+    %
+    % We do NOT plan to allow merging of `free' and `ground' to produce `any',
+    % because that would introduce `any' insts even for builtin types such as
+    % `int' which can't support `any'. It might also make the mode system
+    % too weak -- it might not be able to detect bugs as well as it can
+    % currently.
 
-inst_merge_4(any(UniqA, HOInstInfoA), any(UniqB, HOInstInfoB), _,
-        any(Uniq, HOInstInfo), !ModuleInfo) :-
+    (
+        InstA = any(UniqA, HOInstInfoA),
+        InstB = any(UniqB, HOInstInfoB),
     merge_ho_inst_info(HOInstInfoA, HOInstInfoB, HOInstInfo, !ModuleInfo),
-    merge_uniq(UniqA, UniqB, Uniq).
-inst_merge_4(any(Uniq, HOInstInfo), free, _, any(Uniq, HOInstInfo),
-        !ModuleInfo) :-
-    % We do not yet allow merge of any with free, except for clobbered anys.
-    ( Uniq = clobbered ; Uniq = mostly_clobbered ).
-inst_merge_4(any(UniqA, _), bound(UniqB, ListB), _, any(Uniq, none),
-        !ModuleInfo) :-
-    merge_uniq_bound(UniqA, UniqB, ListB, !.ModuleInfo, Uniq),
-    % We do not yet allow merge of any with free, except for clobbered anys.
+        merge_uniq(UniqA, UniqB, Uniq),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        InstA = any(Uniq, HOInstInfo),
+        InstB = free,
+        % We do not yet allow merge of any with free, except for
+        % clobbered anys.
+        ( Uniq = clobbered ; Uniq = mostly_clobbered ),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        InstA = any(UniqA, _),
+        InstB = bound(UniqB, BoundInstsB),
+        merge_uniq_bound(UniqA, UniqB, BoundInstsB, !.ModuleInfo, Uniq),
+        % We do not yet allow merge of any with free, except for
+        % clobbered anys.
     ( ( Uniq = clobbered ; Uniq = mostly_clobbered ) ->
         true
     ;
-        bound_inst_list_is_ground_or_any(ListB, !.ModuleInfo)
-    ).
-inst_merge_4(any(UniqA, HOInstInfoA), ground(UniqB, HOInstInfoB), _,
-        any(Uniq, HOInstInfo), !ModuleInfo) :-
+            bound_inst_list_is_ground_or_any(BoundInstsB, !.ModuleInfo)
+        ),
+        Inst = any(Uniq, none)
+    ;
+        InstA = any(UniqA, HOInstInfoA),
+        InstB = ground(UniqB, HOInstInfoB),
     merge_ho_inst_info(HOInstInfoA, HOInstInfoB, HOInstInfo, !ModuleInfo),
-    merge_uniq(UniqA, UniqB, Uniq).
-inst_merge_4(any(UniqA, _), abstract_inst(_, _), _, any(Uniq, none),
-        !ModuleInfo) :-
+        merge_uniq(UniqA, UniqB, Uniq),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        InstA = any(UniqA, _),
+        InstB = abstract_inst(_, _),
     merge_uniq(UniqA, shared, Uniq),
-    % We do not yet allow merge of any with free, except for clobbered anys.
-    ( Uniq = clobbered ; Uniq = mostly_clobbered ).
-inst_merge_4(free, any(Uniq, HOInstInfo), _, any(Uniq, HOInstInfo),
-        !ModuleInfo) :-
-    % We do not yet allow merge of any with free, except for clobbered anys.
-    ( Uniq = clobbered ; Uniq = mostly_clobbered ).
-inst_merge_4(bound(UniqA, ListA), any(UniqB, _), _, any(Uniq, none),
-        !ModuleInfo) :-
-    merge_uniq_bound(UniqB, UniqA, ListA, !.ModuleInfo, Uniq),
-    % We do not yet allow merge of any with free, except for clobbered anys.
+        % We do not yet allow merge of any with free, except for
+        % clobbered anys.
+        ( Uniq = clobbered ; Uniq = mostly_clobbered ),
+        Inst = any(Uniq, none)
+    ;
+        InstA = free,
+        InstB = any(Uniq, HOInstInfo), 
+        % We do not yet allow merge of any with free, except for
+        % clobbered anys.
+        ( Uniq = clobbered ; Uniq = mostly_clobbered ),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        InstA = bound(UniqA, BoundInstsA),
+        InstB = any(UniqB, _), 
+        merge_uniq_bound(UniqB, UniqA, BoundInstsA, !.ModuleInfo, Uniq),
+        % We do not yet allow merge of any with free, except
+        % for clobbered anys.
     ( ( Uniq = clobbered ; Uniq = mostly_clobbered ) ->
         true
     ;
-        bound_inst_list_is_ground_or_any(ListA, !.ModuleInfo)
-    ).
-inst_merge_4(ground(UniqA, HOInstInfoA), any(UniqB, HOInstInfoB), _,
-        any(Uniq, HOInstInfo), !ModuleInfo) :-
+            bound_inst_list_is_ground_or_any(BoundInstsA, !.ModuleInfo)
+        ),
+        Inst = any(Uniq, none)
+    ;
+        InstA = ground(UniqA, HOInstInfoA),
+        InstB = any(UniqB, HOInstInfoB),
     merge_ho_inst_info(HOInstInfoA, HOInstInfoB, HOInstInfo, !ModuleInfo),
-    merge_uniq(UniqA, UniqB, Uniq).
-inst_merge_4(abstract_inst(_, _), any(UniqB, _), _, any(Uniq, none),
-        !ModuleInfo) :-
+        merge_uniq(UniqA, UniqB, Uniq),
+        Inst = any(Uniq, HOInstInfo)
+    ;
+        InstA = abstract_inst(_, _),
+        InstB = any(UniqB, _), 
     merge_uniq(shared, UniqB, Uniq),
-    % We do not yet allow merge of any with free, except for clobbered anys.
-    ( Uniq = clobbered ; Uniq = mostly_clobbered ).
-inst_merge_4(free, free, _, free, !ModuleInfo).
-inst_merge_4(bound(UniqA, ListA), bound(UniqB, ListB), MaybeType,
-        bound(Uniq, List), !ModuleInfo) :-
+        % We do not yet allow merge of any with free, except for
+        % clobbered anys.
+        ( Uniq = clobbered ; Uniq = mostly_clobbered ),
+        Inst = any(Uniq, none)
+    ;
+        InstA = free,
+        InstB = free, 
+        Inst = free
+    ;
+        InstA = bound(UniqA, BoundInstsA),
+        InstB = bound(UniqB, BoundInstsB),
     merge_uniq(UniqA, UniqB, Uniq),
-    bound_inst_list_merge(ListA, ListB, MaybeType, List, !ModuleInfo).
-inst_merge_4(bound(UniqA, ListA), ground(UniqB, _), MaybeType, Result,
-        !ModuleInfo) :-
-    inst_merge_bound_ground(UniqA, ListA, UniqB, MaybeType, Result,
-        !ModuleInfo).
-inst_merge_4(ground(UniqA, _), bound(UniqB, ListB), MaybeType, Result,
-        !ModuleInfo) :-
-    inst_merge_bound_ground(UniqB, ListB, UniqA, MaybeType, Result,
-        !ModuleInfo).
-inst_merge_4(ground(UniqA, HOInstInfoA), ground(UniqB, HOInstInfoB), _,
-        ground(Uniq, HOInstInfo), !ModuleInfo) :-
+        bound_inst_list_merge(BoundInstsA, BoundInstsB, MaybeType, BoundInsts,
+            !ModuleInfo),
+        Inst = bound(Uniq, BoundInsts)
+    ;
+        InstA = bound(UniqA, BoundInstsA),
+        InstB = ground(UniqB, _),
+        inst_merge_bound_ground(UniqA, BoundInstsA, UniqB, MaybeType, Inst,
+            !ModuleInfo)
+    ;
+        InstA = ground(UniqA, _),
+        InstB = bound(UniqB, BoundInstsB),
+        inst_merge_bound_ground(UniqB, BoundInstsB, UniqA, MaybeType, Inst,
+            !ModuleInfo)
+    ;
+        InstA = ground(UniqA, HOInstInfoA),
+        InstB = ground(UniqB, HOInstInfoB),
     merge_ho_inst_info(HOInstInfoA, HOInstInfoB, HOInstInfo, !ModuleInfo),
-    merge_uniq(UniqA, UniqB, Uniq).
-inst_merge_4(abstract_inst(Name, ArgsA), abstract_inst(Name, ArgsB),
-        _, abstract_inst(Name, Args), !ModuleInfo) :-
+        merge_uniq(UniqA, UniqB, Uniq),
+        Inst = ground(Uniq, HOInstInfo)
+    ;
+        InstA = abstract_inst(Name, ArgsA),
+        InstB = abstract_inst(Name, ArgsB),
     % We don't know the arguments types of an abstract inst.
     MaybeTypes = list.duplicate(list.length(ArgsA), no),
-    inst_list_merge(ArgsA, ArgsB, MaybeTypes, Args, !ModuleInfo).
-inst_merge_4(not_reached, Inst, _, Inst, !ModuleInfo).
+        inst_list_merge(ArgsA, ArgsB, MaybeTypes, Args, !ModuleInfo),
+        Inst = abstract_inst(Name, Args)
+    ;
+        InstA = not_reached,
+        Inst = InstB
+    ).
 
     % merge_uniq(A, B, C) succeeds if C is minimum of A and B in the ordering
     % clobbered < mostly_clobbered < shared < mostly_unique < unique.
@@ -1613,16 +1862,16 @@
         HOInstInfo = none
     ).
 
-    % merge_uniq_bound(UniqA, UniqB, ListB, ModuleInfo, Uniq) succeeds iff
+    % merge_uniq_bound(UniqA, UniqB, BoundInstsB, ModuleInfo, Uniq) succeeds iff
     % Uniq is the result of merging
     %
 :- pred merge_uniq_bound(uniqueness::in, uniqueness::in, list(bound_inst)::in,
     module_info::in, uniqueness::out) is det.
 
-merge_uniq_bound(UniqA, UniqB, ListB, ModuleInfo, Uniq) :-
+merge_uniq_bound(UniqA, UniqB, BoundInstsB, ModuleInfo, Uniq) :-
     merge_uniq(UniqA, UniqB, Uniq0),
     set.init(Expansions0),
-    merge_bound_inst_list_uniq(ListB, Uniq0, ModuleInfo,
+    merge_bound_inst_list_uniq(BoundInstsB, Uniq0, ModuleInfo,
         Expansions0, _Expansions, Uniq).
 
 :- pred merge_bound_inst_list_uniq(list(bound_inst)::in, uniqueness::in,
@@ -1634,8 +1883,8 @@
         !Expansions, Uniq) :-
     BoundInst = bound_functor(_ConsId, ArgInsts),
     merge_inst_list_uniq(ArgInsts, Uniq0, ModuleInfo, !Expansions, Uniq1),
-    merge_bound_inst_list_uniq(BoundInsts, Uniq1, ModuleInfo, !Expansions,
-        Uniq).
+    merge_bound_inst_list_uniq(BoundInsts, Uniq1, ModuleInfo,
+        !Expansions, Uniq).
 
 :- pred merge_inst_list_uniq(list(mer_inst)::in, uniqueness::in,
     module_info::in, set(inst_name)::in, set(inst_name)::out, uniqueness::out)
@@ -1649,33 +1898,41 @@
 :- pred merge_inst_uniq(mer_inst::in, uniqueness::in, module_info::in,
     set(inst_name)::in, set(inst_name)::out, uniqueness::out) is det.
 
-merge_inst_uniq(any(UniqA, _), UniqB, _, !Expansions, Uniq) :-
-    merge_uniq(UniqA, UniqB, Uniq).
-merge_inst_uniq(free, Uniq, _, !Expansions, Uniq).
-merge_inst_uniq(free(_), Uniq, _, !Expansions, Uniq).
-merge_inst_uniq(bound(UniqA, ListA), UniqB, ModuleInfo, !Expansions, Uniq) :-
+merge_inst_uniq(InstA, UniqB, ModuleInfo, !Expansions, Uniq) :-
+    (
+        ( InstA = free
+        ; InstA = free(_)
+        ; InstA = not_reached
+        ),
+        Uniq = UniqB
+    ;
+        ( InstA = ground(UniqA, _)
+        ; InstA = any(UniqA, _)
+        ),
+        merge_uniq(UniqA, UniqB, Uniq)
+    ;
+        InstA = abstract_inst(_, _),
+        merge_uniq(shared, UniqB, Uniq)
+    ;
+        InstA = bound(UniqA, BoundInstsA),
     merge_uniq(UniqA, UniqB, Uniq0),
-    merge_bound_inst_list_uniq(ListA, Uniq0, ModuleInfo,
-        !Expansions, Uniq).
-merge_inst_uniq(ground(UniqA, _), UniqB, _, !Expansions, Uniq) :-
-    merge_uniq(UniqA, UniqB, Uniq).
-merge_inst_uniq(abstract_inst(_,_), UniqB, _, !Expansions, Uniq) :-
-    merge_uniq(shared, UniqB, Uniq).
-merge_inst_uniq(defined_inst(InstName), UniqB, ModuleInfo,
-        !Expansions, Uniq) :-
+        merge_bound_inst_list_uniq(BoundInstsA, Uniq0, ModuleInfo, !Expansions, Uniq)
+    ;
+        InstA = defined_inst(InstName),
     ( set.member(InstName, !.Expansions) ->
         Uniq = UniqB
     ;
         set.insert(InstName, !Expansions),
         inst_lookup(ModuleInfo, InstName, Inst),
         merge_inst_uniq(Inst, UniqB, ModuleInfo, !Expansions, Uniq)
+        )
+    ;
+        InstA = inst_var(_),
+        unexpected($module, $pred, "inst_var")
+    ;
+        InstA = constrained_inst_vars(_InstVars, SubInstA),
+        merge_inst_uniq(SubInstA, UniqB, ModuleInfo, !Expansions, Uniq)
     ).
-merge_inst_uniq(not_reached, Uniq, _, !Expansions, Uniq).
-merge_inst_uniq(inst_var(_), _, _, !Expansions, _) :-
-    unexpected($module, $pred, "inst_var").
-merge_inst_uniq(constrained_inst_vars(_InstVars, Inst0), UniqB, ModuleInfo,
-        !Expansions, Uniq) :-
-    merge_inst_uniq(Inst0, UniqB, ModuleInfo, !Expansions, Uniq).
 
 %-----------------------------------------------------------------------------%
 
@@ -1683,12 +1940,12 @@
     uniqueness::in, maybe(mer_type)::in, mer_inst::out,
     module_info::in, module_info::out) is semidet.
 
-inst_merge_bound_ground(UniqA, ListA, UniqB, MaybeType, Result, !ModuleInfo) :-
-    ( bound_inst_list_is_ground(ListA, !.ModuleInfo) ->
-        merge_uniq_bound(UniqB, UniqA, ListA, !.ModuleInfo, Uniq),
+inst_merge_bound_ground(UniqA, BoundInstsA, UniqB, MaybeType, Result, !ModuleInfo) :-
+    ( bound_inst_list_is_ground(BoundInstsA, !.ModuleInfo) ->
+        merge_uniq_bound(UniqB, UniqA, BoundInstsA, !.ModuleInfo, Uniq),
         Result = ground(Uniq, none)
     ;
-        bound_inst_list_is_ground_or_any(ListA, !.ModuleInfo),
+        bound_inst_list_is_ground_or_any(BoundInstsA, !.ModuleInfo),
         % If we know the type, we can give a more accurate result than
         % just "any".
         (
@@ -1696,13 +1953,13 @@
             type_constructors(!.ModuleInfo, Type, Constructors),
             type_to_ctor_det(Type, TypeCtor),
             constructors_to_bound_insts(!.ModuleInfo, UniqB, TypeCtor,
-                Constructors, ListB0),
-            list.sort_and_remove_dups(ListB0, ListB),
-            inst_merge_4(bound(UniqA, ListA), bound(UniqB, ListB),
+                Constructors, BoundInstsB0),
+            list.sort_and_remove_dups(BoundInstsB0, BoundInstsB),
+            inst_merge_4(bound(UniqA, BoundInstsA), bound(UniqB, BoundInstsB),
                 MaybeType, Result, !ModuleInfo)
         ;
             MaybeType = no,
-            merge_uniq_bound(UniqB, UniqA, ListA, !.ModuleInfo, Uniq),
+            merge_uniq_bound(UniqB, UniqA, BoundInstsA, !.ModuleInfo, Uniq),
             Result = any(Uniq, none)
         )
     ).
@@ -1719,40 +1976,47 @@
     inst_merge(ArgA, ArgB, MaybeType, Arg, !ModuleInfo),
     inst_list_merge(ArgsA, ArgsB, MaybeTypes, Args, !ModuleInfo).
 
-    % bound_inst_list_merge(Xs, Ys, Zs, !ModuleInfo):
+    % bound_inst_list_merge(BoundInstsA, BoundInstsB, BoundInsts, !ModuleInfo):
     %
-    % The two input lists Xs and Ys must already be sorted.
+    % The two input lists BoundInstsA and BoundInstsB must already be sorted.
     % Here we perform a sorted merge operation,
-    % so that the functors of the output list Zs are the union
-    % of the functors of the input lists Xs and Ys.
+    % so that the functors of the output list BoundInsts are the union
+    % of the functors of the input lists BoundInstsA and BoundInstsB.
     %
 :- pred bound_inst_list_merge(list(bound_inst)::in, list(bound_inst)::in,
     maybe(mer_type)::in, list(bound_inst)::out,
     module_info::in, module_info::out) is semidet.
 
-bound_inst_list_merge(Xs, Ys, MaybeType, Zs, !ModuleInfo) :-
-    ( Xs = [] ->
-        Zs = Ys
-    ; Ys = [] ->
-        Zs = Xs
-    ;
-        Xs = [X | Xs1],
-        Ys = [Y | Ys1],
-        X = bound_functor(ConsIdX, ArgsX),
-        Y = bound_functor(ConsIdY, ArgsY),
-        ( equivalent_cons_ids(ConsIdX, ConsIdY) ->
-            maybe_get_cons_id_arg_types(!.ModuleInfo, MaybeType,
-                ConsIdX, list.length(ArgsX), MaybeTypes),
-            inst_list_merge(ArgsX, ArgsY, MaybeTypes, Args, !ModuleInfo),
-            Z = bound_functor(ConsIdX, Args),
-            Zs = [Z | Zs1],
-            bound_inst_list_merge(Xs1, Ys1, MaybeType, Zs1, !ModuleInfo)
-        ; compare(<, ConsIdX, ConsIdY) ->
-            Zs = [X | Zs1],
-            bound_inst_list_merge(Xs1, Ys, MaybeType, Zs1, !ModuleInfo)
+bound_inst_list_merge(BoundInstsA, BoundInstsB, MaybeType, BoundInsts,
+        !ModuleInfo) :-
+    (
+        BoundInstsA = [],
+        BoundInsts = BoundInstsB
         ;
-            Zs = [Y | Zs1],
-            bound_inst_list_merge(Xs, Ys1, MaybeType, Zs1, !ModuleInfo)
+        BoundInstsA = [_ | _],
+        BoundInstsB = [],
+        BoundInsts = BoundInstsA
+    ;
+        BoundInstsA = [BoundInstA | BoundInstsTailA],
+        BoundInstsB = [BoundInstB | BoundInstsTailB],
+        BoundInstA = bound_functor(ConsIdA, ArgsA),
+        BoundInstB = bound_functor(ConsIdB, ArgsB),
+        ( equivalent_cons_ids(ConsIdA, ConsIdB) ->
+            maybe_get_cons_id_arg_types(!.ModuleInfo, MaybeType,
+                ConsIdA, list.length(ArgsA), MaybeTypes),
+            inst_list_merge(ArgsA, ArgsB, MaybeTypes, Args, !ModuleInfo),
+            BoundInst = bound_functor(ConsIdA, Args),
+            bound_inst_list_merge(BoundInstsTailA, BoundInstsTailB, MaybeType,
+                BoundInstsTail, !ModuleInfo),
+            BoundInsts = [BoundInst | BoundInstsTail]
+        ; compare(<, ConsIdA, ConsIdB) ->
+            bound_inst_list_merge(BoundInstsTailA, BoundInstsB, MaybeType,
+                BoundInstsTail, !ModuleInfo),
+            BoundInsts = [BoundInstA | BoundInstsTail]
+        ;
+            bound_inst_list_merge(BoundInstsA, BoundInstsTailB, MaybeType,
+                BoundInstsTail, !ModuleInfo),
+            BoundInsts = [BoundInstB | BoundInstsTail]
         )
     ).
 
@@ -1806,9 +2070,9 @@
         ContainsNonstd = no
     ;
         Inst = any(_, _),
-        % XXX This code preserves the old behavior of this predicate,
-        % but it is arguably incorrect, since any/2 insts, like ground/2 insts,
-        % contain a ho_inst_info.
+        % XXX This code preserves the old behavior of the predicate that
+        % preceded this function, but it is arguably incorrect, since
+        % any/2 insts, like ground/2 insts, contain a ho_inst_info.
         ContainsNonstd = no
     ).
 
@@ -1952,36 +2216,24 @@
 
 inst_may_restrict_cons_ids(ModuleInfo, Inst) = MayRestrict :-
     (
-        Inst = any(_, _),
-        MayRestrict = yes
-    ;
-        Inst = free,
-        MayRestrict = no
-    ;
-        Inst = free(_),
-        MayRestrict = no
-    ;
-        Inst = bound(_, _),
+        ( Inst = any(_, _)
+        ; Inst = bound(_, _)
+        ; Inst = inst_var(_)
+        ; Inst = constrained_inst_vars(_, _)    % XXX is this right?
+        ; Inst = abstract_inst(_, _)
+        ),
         MayRestrict = yes
     ;
-        Inst = ground(_, _),
-        MayRestrict = no
-    ;
-        Inst = not_reached,
+        ( Inst = free
+        ; Inst = free(_)
+        ; Inst = not_reached
+        ; Inst = ground(_, _)
+        ),
         MayRestrict = no
     ;
-        Inst = inst_var(_),
-        MayRestrict = yes
-    ;
-        Inst = constrained_inst_vars(_, _),
-        MayRestrict = yes
-    ;
         Inst = defined_inst(InstName),
         inst_lookup(ModuleInfo, InstName, NewInst),
         MayRestrict = inst_may_restrict_cons_ids(ModuleInfo, NewInst)
-    ;
-        Inst = abstract_inst(_, _),
-        MayRestrict = yes
     ).
 
 %-----------------------------------------------------------------------------%
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_glfw
cvs diff: Diffing extras/graphics/mercury_glfw/samples
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
cvs diff: Diffing m4
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/lazy_list
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/feedback
cvs diff: Diffing tests/feedback/mandelbrot
cvs diff: Diffing tests/feedback/mmc
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