[m-rev.] for post-commit review: speedups to quantification etc

Zoltan Somogyi zs at csse.unimelb.edu.au
Tue Sep 8 12:41:42 AEST 2009


Implement several more changes that together speed up compilation time
on training_cars_full by 12%, and also improve tools/speedtest -h by 7.2%
and tools/speedtest by 1.6%.

The first change is designed to eliminate the time that the compiler spends
constructing error messages that are then ignored. The working predicates of
prog_io_sym_name used to always return a single result, which either gave
a description of the thing being looked, or an error message. However,
in many places, the caller did not consider not finding the thing being looked
for to be an error, and thus threw away the error message, keeping only
the "not found" indication. For each predicate with such callers, this diff
provides a parallel predicate that indicates "not found" simply by failing.
This allows us to eliminate the construction of the error message, the
preparation for the construction of the error message (usually by describing
the context), and the construction of the "ok" wrapper.

The second change is to specialize the handling of from_ground_term_construct
scopes in the termination analyzer. To make this easier, I also cleaned up
of the infrastructure of the termination analyzer.

The third change is to avoid traversing from_ground_term_construct scopes
in quantification.m when finding the variables in a goal, since termination
analysis no longer needs the information it gathers.

The fourth change is to avoid traversing second and later conjuncts in
conjunctions twice. The first step in handling conjunctions is to call
implicitly_quantify_conj, which builds up a data structure that pairs each
conjunct with the variables that occur free in all the conjuncts following it.
However, after this was done and each conjunct was annotated with its
nonlocals, we used to compute the variables that occur free in the conjunction
as a whole from scratch. This diff changes the code so that we now compute that
set based on the information we gathered earlier, avoiding a redundant
traversal.

The fifth change is to create specialized, lower-arity versions of many of
the predicates in quantification.m. These versions are intended for traversals
that take place after the compiler has replaced lambda expressions with
references to separate procedures. These traversals do not need to pass around
arguments representing the variables occurring free in the (now non-existent)
lambda expressions.

compiler/prog_io_sym_name.m:
	Make the first change described above.

	Change some predicate names to adopt a consistent naming scheme
	in which predicates that do the same job and differ only in how they
	handle errors have names that differ only in a "try_" prefix.

	Add some predicate versions that do common tests on the output
	of the base versions. For example, try_parse_sym_name_and_no_args
	is a version of try_parse_sym_name_and_args that insists on finding
	an empty argument list.

	Remove the unused "error term" argument that we used to need a while
	ago.

	Move some predicate definitions to make their order match the order of 
	their declarations.

	Turn a predicate into a function for its caller's convenience.

compiler/term_constr_build.m:
	Make the second change described above by modeling each
	from_ground_term_construct scope as a single unification,
	assigning the total size of the ground term to the variable being
	built.

compiler/term_constr_util.m:
	Put the arguments of some predicates into a more standard order.

compiler/lp_rational.m:
	Change the names of some function symbols to avoid both the use of
	graphic characters that require quoting and clashes with other types.

	Change the names of some predicates to make their purpose clear,
	and to avoid ambiguity.

compiler/quantification.m:
	Make the third, fourth and fifth changes described above.

compiler/*.m:
	Conform to the changes above.

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/include
cvs diff: Diffing boehm_gc/include/private
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/src/atomic_ops/sysdeps/gcc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/hpc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/ibmc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/icc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/msftc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/sunc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/tests
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing boehm_gc/windows-untested
cvs diff: Diffing boehm_gc/windows-untested/vc60
cvs diff: Diffing boehm_gc/windows-untested/vc70
cvs diff: Diffing boehm_gc/windows-untested/vc71
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/accumulator.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/accumulator.m,v
retrieving revision 1.73
diff -u -b -r1.73 accumulator.m
--- compiler/accumulator.m	3 Sep 2009 23:57:22 -0000	1.73
+++ compiler/accumulator.m	4 Sep 2009 18:28:44 -0000
@@ -1423,7 +1423,7 @@
     proc_info_set_varset(VarSet, !OrigProcInfo),
     proc_info_set_vartypes(VarTypes, !OrigProcInfo),
 
-    requantify_proc(!OrigProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !OrigProcInfo),
     update_accumulator_pred(AccPredId, AccProcId, AccGoal, !ModuleInfo).
 
 %-----------------------------------------------------------------------------%
@@ -1771,7 +1771,7 @@
     module_info_pred_proc_info(!.ModuleInfo, NewPredId, NewProcId,
         PredInfo, ProcInfo0),
     proc_info_set_goal(AccGoal, ProcInfo0, ProcInfo1),
-    requantify_proc(ProcInfo1, ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, ProcInfo1, ProcInfo),
     module_info_set_pred_proc_info(NewPredId, NewProcId,
         PredInfo, ProcInfo, !ModuleInfo).
 
Index: compiler/add_clause.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_clause.m,v
retrieving revision 1.57
diff -u -b -r1.57 add_clause.m
--- compiler/add_clause.m	19 Aug 2009 07:44:52 -0000	1.57
+++ compiler/add_clause.m	7 Sep 2009 00:58:13 -0000
@@ -600,7 +600,11 @@
         % The RTTI varmaps here are just a dummy value, because the real ones
         % are not introduced until polymorphism.
         rtti_varmaps_init(EmptyRttiVarmaps),
-        implicitly_quantify_clause_body(HeadVarList, Warnings, Goal0, Goal,
+        % XXX It should be possible to exploit the fact that lambda expressions
+        % are not yet recognized as such inside from_ground_term scopes.
+        implicitly_quantify_clause_body_general(
+            ordinary_nonlocals_maybe_lambda,
+            HeadVarList, Warnings, Goal0, Goal,
             !VarSet, VarTypes0, VarTypes, EmptyRttiVarmaps, _),
         qual_info_set_var_types(VarTypes, !QualInfo)
     ).
Index: compiler/add_heap_ops.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_heap_ops.m,v
retrieving revision 1.41
diff -u -b -r1.41 add_heap_ops.m
--- compiler/add_heap_ops.m	3 Sep 2009 23:57:22 -0000	1.41
+++ compiler/add_heap_ops.m	4 Sep 2009 18:35:49 -0000
@@ -97,7 +97,7 @@
     % so we need to requantify.
     % XXX it would be more efficient to maintain them rather than recomputing
     % them every time.
-    requantify_proc(!Proc).
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !Proc).
 
 :- pred goal_add_heap_ops(hlds_goal::in, hlds_goal::out,
     heap_ops_info::in, heap_ops_info::out) is det.
Index: compiler/add_pragma.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pragma.m,v
retrieving revision 1.97
diff -u -b -r1.97 add_pragma.m
--- compiler/add_pragma.m	4 Sep 2009 02:27:48 -0000	1.97
+++ compiler/add_pragma.m	4 Sep 2009 18:27:24 -0000
@@ -3414,7 +3414,8 @@
             HldsGoal0 = hlds_goal(GoalExpr, GoalInfo),
             map.init(EmptyVarTypes),
             rtti_varmaps_init(EmptyRttiVarmaps),
-            implicitly_quantify_clause_body(HeadVarList, _Warnings,
+            implicitly_quantify_clause_body_general(
+                ordinary_nonlocals_maybe_lambda, HeadVarList, _Warnings,
                 HldsGoal0, HldsGoal, VarSet0, VarSet, EmptyVarTypes, _,
                 EmptyRttiVarmaps, _),
             NewClause = clause(selected_modes([ProcId]), HldsGoal,
Index: compiler/add_trail_ops.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_trail_ops.m,v
retrieving revision 1.53
diff -u -b -r1.53 add_trail_ops.m
--- compiler/add_trail_ops.m	3 Sep 2009 23:57:22 -0000	1.53
+++ compiler/add_trail_ops.m	4 Sep 2009 18:28:17 -0000
@@ -127,7 +127,7 @@
     % so we need to requantify.
     % XXX it would be more efficient to maintain them rather than
     % recomputing them every time.
-    requantify_proc(!Proc).
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !Proc).
 
 :- pred goal_add_trail_ops(hlds_goal::in, hlds_goal::out,
     trail_ops_info::in, trail_ops_info::out) is det.
Index: compiler/analysis.file.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/analysis.file.m,v
retrieving revision 1.11
diff -u -b -r1.11 analysis.file.m
--- compiler/analysis.file.m	2 Sep 2009 00:30:14 -0000	1.11
+++ compiler/analysis.file.m	7 Sep 2009 02:07:32 -0000
@@ -414,7 +414,7 @@
         analysis_type(_ : unit(Call), _ : unit(Answer)) =
             analyses(Compiler, AnalysisName),
 
-        parse_module_name(CallerModuleTerm, CallerModule),
+        try_parse_module_name(CallerModuleTerm, CallerModule),
         parse_func_id(FuncIdTerm, FuncId),
         from_term(CallPatternTerm, CallPattern : Call)
     ->
@@ -464,7 +464,7 @@
         analysis_type(_ : unit(Call), _ : unit(Answer))
             = analyses(Compiler, AnalysisName),
 
-        parse_module_name(DependentModuleTerm, DependentModule),
+        try_parse_module_name(DependentModuleTerm, DependentModule),
         parse_func_id(FuncIdTerm, FuncId),
         from_term(CallPatternTerm, CallPattern : Call)
     ->
@@ -516,10 +516,10 @@
     proc_id_to_int(ProcId, ProcInt),
     FuncId = func_id(PredOrFunc, Name, Arity, ProcId).
 
-:- pred parse_module_name(term::in, module_name::out) is semidet.
+:- pred try_parse_module_name(term::in, module_name::out) is semidet.
 
-parse_module_name(Term, ModuleName) :-
-    parse_sym_name_and_args(Term, ModuleName, []).
+try_parse_module_name(Term, ModuleName) :-
+    try_parse_sym_name_and_no_args(Term, ModuleName).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/cse_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/cse_detection.m,v
retrieving revision 1.123
diff -u -b -r1.123 cse_detection.m
--- compiler/cse_detection.m	3 Sep 2009 23:07:26 -0000	1.123
+++ compiler/cse_detection.m	4 Sep 2009 18:21:40 -0000
@@ -215,7 +215,9 @@
         CseInfo = cse_info(VarSet1, VarTypes1, RttiVarMaps1, _),
         proc_info_get_headvars(ProcInfo0, HeadVars),
 
-        implicitly_quantify_clause_body(HeadVars, _Warnings,
+        implicitly_quantify_clause_body_general(
+            ordinary_nonlocals_maybe_lambda,
+            HeadVars, _Warnings,
             Goal1, Goal, VarSet1, VarSet, VarTypes1, VarTypes,
             RttiVarMaps1, RttiVarMaps),
 
Index: compiler/deforest.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deforest.m,v
retrieving revision 1.90
diff -u -b -r1.90 deforest.m
--- compiler/deforest.m	3 Sep 2009 23:57:23 -0000	1.90
+++ compiler/deforest.m	4 Sep 2009 18:28:57 -0000
@@ -223,7 +223,7 @@
         (
             Changed = yes,
             pd_info_get_module_info(!.PDInfo, !:ModuleInfo),
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             proc_info_get_goal(!.ProcInfo, !:Goal),
             proc_info_get_initial_instmap(!.ProcInfo, !.ModuleInfo, InstMap0),
             proc_info_get_vartypes(!.ProcInfo, VarTypes),
Index: compiler/delay_partial_inst.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/delay_partial_inst.m,v
retrieving revision 1.15
diff -u -b -r1.15 delay_partial_inst.m
--- compiler/delay_partial_inst.m	21 Jul 2009 02:08:48 -0000	1.15
+++ compiler/delay_partial_inst.m	4 Sep 2009 18:21:51 -0000
@@ -241,7 +241,7 @@
         proc_info_set_goal(Goal, !ProcInfo),
         proc_info_set_varset(DelayInfo ^ dpi_varset, !ProcInfo),
         proc_info_set_vartypes(DelayInfo ^ dpi_vartypes, !ProcInfo),
-        requantify_proc(!ProcInfo),
+        requantify_proc_general(ordinary_nonlocals_maybe_lambda, !ProcInfo),
         MaybeProcInfo = yes(!.ProcInfo)
     ;
         Changed = no,
Index: compiler/dep_par_conj.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/dep_par_conj.m,v
retrieving revision 1.36
diff -u -b -r1.36 dep_par_conj.m
--- compiler/dep_par_conj.m	3 Sep 2009 23:57:23 -0000	1.36
+++ compiler/dep_par_conj.m	4 Sep 2009 18:29:04 -0000
@@ -2861,7 +2861,7 @@
     pred_info::in, proc_info::in, module_info::in, module_info::out) is det.
 
 fixup_and_reinsert_proc(PredId, ProcId, !.PredInfo, !.ProcInfo, !ModuleInfo) :-
-    requantify_proc(!ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
     recompute_instmap_delta_proc(do_not_recompute_atomic_instmap_deltas,
         !ProcInfo, !ModuleInfo),
     pred_info_set_proc_info(ProcId, !.ProcInfo, !PredInfo),
Index: compiler/distance_granularity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/distance_granularity.m,v
retrieving revision 1.10
diff -u -b -r1.10 distance_granularity.m
--- compiler/distance_granularity.m	3 Sep 2009 23:57:24 -0000	1.10
+++ compiler/distance_granularity.m	4 Sep 2009 18:29:24 -0000
@@ -280,7 +280,8 @@
             % processed. That means that the predicate must be specialized.
             !:Specialized = yes,
             proc_info_set_goal(BodyClone, ProcInfo1, ProcInfo2),
-            requantify_proc(ProcInfo2, ProcInfo3),
+            requantify_proc_general(ordinary_nonlocals_no_lambda,
+                ProcInfo2, ProcInfo3),
             recompute_instmap_delta_proc(
                 do_not_recompute_atomic_instmap_deltas, ProcInfo3, ProcInfo,
                 !ModuleInfo),
@@ -853,7 +854,8 @@
     update_original_predicate_goal(Body0, Body, PredId, ProcId,
         PredIdSpecialized, SymNameSpecialized, ProcInfo0, ProcInfo1, Distance),
     proc_info_set_goal(Body, ProcInfo1, ProcInfo2),
-    requantify_proc(ProcInfo2, ProcInfo3),
+    requantify_proc_general(ordinary_nonlocals_no_lambda,
+        ProcInfo2, ProcInfo3),
     recompute_instmap_delta_proc(do_not_recompute_atomic_instmap_deltas,
         ProcInfo3, ProcInfo, !ModuleInfo),
     pred_info_set_proc_info(ProcId, ProcInfo, !PredInfo),
Index: compiler/equiv_type_hlds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/equiv_type_hlds.m,v
retrieving revision 1.57
diff -u -b -r1.57 equiv_type_hlds.m
--- compiler/equiv_type_hlds.m	4 Sep 2009 02:27:50 -0000	1.57
+++ compiler/equiv_type_hlds.m	4 Sep 2009 18:29:31 -0000
@@ -443,7 +443,7 @@
         ),
         (
             Recompute = yes,
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             recompute_instmap_delta_proc(
                 do_not_recompute_atomic_instmap_deltas, !ProcInfo, !ModuleInfo)
         ;
Index: compiler/field_access.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/field_access.m,v
retrieving revision 1.16
diff -u -b -r1.16 field_access.m
--- compiler/field_access.m	2 Sep 2009 00:30:14 -0000	1.16
+++ compiler/field_access.m	7 Sep 2009 02:06:00 -0000
@@ -302,9 +302,7 @@
         Term = term.functor(term.atom("^"),
             [FieldNameTerm, OtherFieldNamesTerm], _)
     ->
-        (
-            parse_sym_name_and_args(FieldNameTerm, FieldName, Args)
-        ->
+        ( try_parse_sym_name_and_args(FieldNameTerm, FieldName, Args) ->
             parse_field_list(OtherFieldNamesTerm, VarSet, ContextPieces,
                 MaybeFieldNamesTail),
             (
@@ -320,7 +318,7 @@
             MaybeFieldNames = error1([Spec])
         )
     ;
-        ( parse_sym_name_and_args(Term, FieldName, Args) ->
+        ( try_parse_sym_name_and_args(Term, FieldName, Args) ->
             MaybeFieldNames = ok1([FieldName - Args])
         ;
             Spec = make_field_list_error(VarSet, get_term_context(Term), Term,
Index: compiler/follow_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/follow_code.m,v
retrieving revision 1.96
diff -u -b -r1.96 follow_code.m
--- compiler/follow_code.m	23 Dec 2008 01:37:32 -0000	1.96
+++ compiler/follow_code.m	4 Sep 2009 18:35:21 -0000
@@ -82,7 +82,9 @@
             % We need to fix up the goal_info by recalculating the nonlocal
             % vars and the non-atomic instmap deltas.
             proc_info_get_headvars(!.ProcInfo, HeadVars),
-            implicitly_quantify_clause_body(HeadVars, _Warnings, Goal1, Goal2,
+            implicitly_quantify_clause_body_general(
+                ordinary_nonlocals_no_lambda,
+                HeadVars, _Warnings, Goal1, Goal2,
                 Varset0, Varset, VarTypes0, VarTypes,
                 RttiVarMaps0, RttiVarMaps),
             proc_info_get_initial_instmap(!.ProcInfo, !.ModuleInfo, InstMap0),
Index: compiler/granularity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/granularity.m,v
retrieving revision 1.14
diff -u -b -r1.14 granularity.m
--- compiler/granularity.m	3 Sep 2009 23:57:24 -0000	1.14
+++ compiler/granularity.m	4 Sep 2009 18:30:20 -0000
@@ -78,7 +78,8 @@
         ;
             Changed = yes,
             proc_info_set_goal(Goal, ProcInfo0, ProcInfo1),
-            requantify_proc(ProcInfo1, ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda,
+                ProcInfo1, ProcInfo),
             map.det_update(ProcTable0, ProcId, ProcInfo, ProcTable),
             pred_info_set_procedures(ProcTable, PredInfo0, PredInfo),
             map.det_update(PredTable0, PredId, PredInfo, PredTable),
Index: compiler/higher_order.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/higher_order.m,v
retrieving revision 1.184
diff -u -b -r1.184 higher_order.m
--- compiler/higher_order.m	3 Sep 2009 23:57:24 -0000	1.184
+++ compiler/higher_order.m	4 Sep 2009 18:30:30 -0000
@@ -487,7 +487,7 @@
             !:ModuleInfo = !.Info ^ hoi_global_info ^ hogi_module_info,
             !:ProcInfo   = !.Info ^ hoi_proc_info,
             proc_info_set_goal(Goal0, !ProcInfo),
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             proc_info_get_goal(!.ProcInfo, Goal2),
             proc_info_get_initial_instmap(!.ProcInfo, !.ModuleInfo, InstMap),
             proc_info_get_vartypes(!.ProcInfo, VarTypes),
Index: compiler/implicit_parallelism.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/implicit_parallelism.m,v
retrieving revision 1.11
diff -u -b -r1.11 implicit_parallelism.m
--- compiler/implicit_parallelism.m	30 Jan 2009 03:51:44 -0000	1.11
+++ compiler/implicit_parallelism.m	4 Sep 2009 18:30:39 -0000
@@ -568,7 +568,8 @@
             !ModuleInfo, no, _, 0, _, CallSites, _, SiteNumCounter, _),
         proc_info_set_goal(Body, ProcInfo0, ProcInfo1),
         proc_info_set_has_parallel_conj(yes, ProcInfo1, ProcInfo2),
-        requantify_proc(ProcInfo2, ProcInfo3),
+        requantify_proc_general(ordinary_nonlocals_no_lambda,
+            ProcInfo2, ProcInfo3),
         recompute_instmap_delta_proc(do_not_recompute_atomic_instmap_deltas,
             ProcInfo3, ProcInfo, !ModuleInfo),
         pred_info_set_proc_info(ProcId, ProcInfo, PredInfo0, PredInfo),
Index: compiler/inlining.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/inlining.m,v
retrieving revision 1.163
diff -u -b -r1.163 inlining.m
--- compiler/inlining.m	3 Sep 2009 23:57:25 -0000	1.163
+++ compiler/inlining.m	4 Sep 2009 18:30:48 -0000
@@ -509,7 +509,7 @@
 
         (
             Requantify = yes,
-            requantify_proc(!ProcInfo)
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo)
         ;
             Requantify = no
         ),
Index: compiler/lambda.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lambda.m,v
retrieving revision 1.140
diff -u -b -r1.140 lambda.m
--- compiler/lambda.m	11 Jun 2009 07:00:11 -0000	1.140
+++ compiler/lambda.m	4 Sep 2009 18:31:04 -0000
@@ -191,7 +191,8 @@
     % Check if we need to requantify.
     (
         MustRecomputeNonLocals = yes,
-        implicitly_quantify_clause_body(HeadVars, _Warnings,
+        implicitly_quantify_clause_body_general(
+            ordinary_nonlocals_maybe_lambda, HeadVars, _Warnings,
             Goal1, Goal2, VarSet1, VarSet, VarTypes1, VarTypes,
             RttiVarMaps1, RttiVarMaps),
         proc_info_get_initial_instmap(!.ProcInfo, !.ModuleInfo, InstMap0),
@@ -541,7 +542,9 @@
         % that we just created.
         (
             MustRecomputeNonLocals0 = yes,
-            requantify_proc(ProcInfo2, ProcInfo)
+            % ZZZ
+            requantify_proc_general(ordinary_nonlocals_maybe_lambda,
+                ProcInfo2, ProcInfo)
         ;
             MustRecomputeNonLocals0 = no,
             ProcInfo = ProcInfo2
Index: compiler/lco.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lco.m,v
retrieving revision 1.58
diff -u -b -r1.58 lco.m
--- compiler/lco.m	3 Sep 2009 23:57:26 -0000	1.58
+++ compiler/lco.m	4 Sep 2009 18:35:56 -0000
@@ -327,7 +327,8 @@
                     proc_info_set_varset(VarSet, !ProcInfo),
                     proc_info_set_vartypes(VarTypes, !ProcInfo),
                     proc_info_set_goal(Goal, !ProcInfo),
-                    requantify_proc(!ProcInfo),
+                    requantify_proc_general(ordinary_nonlocals_no_lambda,
+                        !ProcInfo),
                     svmap.det_insert(CurProc, !.ProcInfo, !CurSCCUpdates)
                 )
             ;
@@ -833,7 +834,7 @@
     transform_variant_goal(ModuleInfo, VarToAddr, InstMap0, Goal0, Goal, _),
     proc_info_set_goal(Goal, !VariantProcInfo),
     % We changed the scopes of the headvars we now return via pointers.
-    requantify_proc(!VariantProcInfo).
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !VariantProcInfo).
 
 :- pred make_addr_vars(list(prog_var)::in, list(mer_mode)::in,
     list(prog_var)::out, list(mer_mode)::out, list(int)::in,
Index: compiler/liveness.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/liveness.m,v
retrieving revision 1.168
diff -u -b -r1.168 liveness.m
--- compiler/liveness.m	3 Sep 2009 23:57:26 -0000	1.168
+++ compiler/liveness.m	4 Sep 2009 18:35:28 -0000
@@ -284,7 +284,7 @@
     <= debug_liveness_io(IO).
 
 detect_liveness_proc_2(PredId, ModuleInfo, !ProcInfo, !IO) :-
-    requantify_proc(!ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
 
     proc_info_get_goal(!.ProcInfo, Goal0),
     proc_info_get_varset(!.ProcInfo, VarSet),
Index: compiler/loop_inv.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/loop_inv.m,v
retrieving revision 1.48
diff -u -b -r1.48 loop_inv.m
--- compiler/loop_inv.m	2 Sep 2009 00:30:16 -0000	1.48
+++ compiler/loop_inv.m	4 Sep 2009 18:31:54 -0000
@@ -816,7 +816,7 @@
     AuxPredProcId = proc(AuxPredId, AuxProcId),
     hlds_pred.proc_info_set_goal(AuxBody, !AuxProcInfo),
 
-    requantify_proc(!AuxProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !AuxProcInfo),
     recompute_instmap_delta_proc(do_not_recompute_atomic_instmap_deltas,
         !AuxProcInfo, !ModuleInfo),
 
@@ -925,7 +925,8 @@
     proc_info_set_body(VarSet, VarTypes, HeadVars, Body,
         RttiVarMaps, ProcInfo0, ProcInfo1),
 
-    quantification.requantify_proc(ProcInfo1, ProcInfo2),
+    requantify_proc_general(ordinary_nonlocals_no_lambda,
+        ProcInfo1, ProcInfo2),
     recompute_instmap_delta_proc(do_not_recompute_atomic_instmap_deltas,
         ProcInfo2, ProcInfo, ModuleInfo0, ModuleInfo1),
 
Index: compiler/lp_rational.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lp_rational.m,v
retrieving revision 1.12
diff -u -b -r1.12 lp_rational.m
--- compiler/lp_rational.m	3 Sep 2009 23:07:27 -0000	1.12
+++ compiler/lp_rational.m	7 Sep 2009 03:58:36 -0000
@@ -36,7 +36,7 @@
 
 %-----------------------------------------------------------------------------%
 %
-% Linear constraints over Q^n
+% Linear constraints over Q^n.
 %
 
 :- type constant == rat.
@@ -54,7 +54,10 @@
     %
 :- func lp_rational.lp_term(lp_var) = lp_term.
 
-:- type operator ---> (=<) ; (=) ; (>=).
+:- type lp_operator
+    --->    lp_lt_eq
+    ;       lp_eq
+    ;       lp_gt_eq.
 
     % A primitive linear arithmetic constraint.
     %
@@ -66,23 +69,24 @@
 
     % Create a constraint from the given components.
     %
-:- func lp_rational.constraint(lp_terms, operator, constant) = constraint.
+:- func lp_rational.construct_constraint(lp_terms, lp_operator, constant)
+    = constraint.
 
     % Create a constraint from the given components.
     % Throws an exception if the resulting constraint is trivially false.
     %
-:- func lp_rational.non_false_constraint(lp_terms, operator, constant)
-    = constraint.
+:- func lp_rational.construct_non_false_constraint(lp_terms, lp_operator,
+    constant) = constraint.
 
     % Deconstruct the given constraint.
     %
-:- pred lp_rational.constraint(constraint::in, lp_terms::out, operator::out,
-    constant::out) is det.
+:- pred lp_rational.deconstruct_constraint(constraint::in,
+    lp_terms::out, lp_operator::out, constant::out) is det.
 
     % As above but throws an exception if the constraint is false.
     %
-:- pred lp_rational.non_false_constraint(constraint::in, lp_terms::out,
-    operator::out, constant::out) is det.
+:- pred lp_rational.deconstruct_non_false_constraint(constraint::in,
+    lp_terms::out, lp_operator::out, constant::out) is det.
 
     % Succeeds iff the given constraint contains a single variable and
     % that variable is constrained to be a nonnegative value.
@@ -359,32 +363,52 @@
 
 lp_term(Var) = Var - one.
 
-constraint([], (=<), Const) = lte([], Const).
-constraint([], (=),  Const) = eq([], Const).
-constraint([], (>=), Const) = lte([], -Const).
-constraint(Terms0 @ [_ | _], (=<), Const0) = Constraint :-
+construct_constraint(Terms0, Op, Const0) = Constraint :-
+    (
+        Terms0 = [],
+        (
+            Op = lp_lt_eq,
+            Constraint = lte([], Const0)
+        ;
+            Op = lp_eq,
+            Constraint = eq([], Const0)
+        ;
+            Op = lp_gt_eq,
+            Constraint = lte([], -Const0)
+        )
+    ;
+        Terms0 = [_ | _],
+        (
+            Op = lp_lt_eq,
     Terms1 = sum_like_terms(Terms0),
     normalize_terms_and_const(yes, Terms1, Const0, Terms, Const),
-    Constraint = lte(Terms, Const).
-constraint(Terms0 @ [_ | _], (=), Const0) = Constraint :-
+            Constraint = lte(Terms, Const)
+        ;
+            Op = lp_eq,
     Terms1 = sum_like_terms(Terms0),
     normalize_terms_and_const(no, Terms1, Const0, Terms, Const),
-    Constraint = eq(Terms, Const).
-constraint(Terms0 @ [_ | _], (>=), Const0) = Constraint :-
+            Constraint = eq(Terms, Const)
+        ;
+            Op = lp_gt_eq,
     Terms1 = sum_like_terms(Terms0),
     normalize_terms_and_const(yes, Terms1, Const0, Terms, Const),
-    Constraint = lte(negate_lp_terms(Terms), -Const).
+            Constraint = lte(negate_lp_terms(Terms), -Const)
+        )
+    ).
+
+    % This is for internal use only - it builds a constraint out of the parts
+    % but does *not* attempt to perform any standardization. It is intended for
+    % use in operations such as normalization.
+    %
+:- func unchecked_construct_constraint(lp_terms, lp_operator, constant) =
+    constraint.
 
-    % This is for internal use only - it builds a constraint out
-    % of the parts but does *not* attempt to perform any
-    % standardization.  It is intended for use in operations
-    % such as normalization.
-    %
-:- func unchecked_constraint(lp_terms, operator, constant) = constraint.
-
-unchecked_constraint(Terms, (=<), Constant) = lte(Terms, Constant).
-unchecked_constraint(Terms, (=),  Constant) = eq(Terms, Constant).
-unchecked_constraint(Terms, (>=), Constant) = gte(Terms, Constant).
+unchecked_construct_constraint(Terms, lp_lt_eq, Constant) =
+    lte(Terms, Constant).
+unchecked_construct_constraint(Terms, lp_eq,    Constant) =
+    eq(Terms, Constant).
+unchecked_construct_constraint(Terms, lp_gt_eq, Constant) =
+    gte(Terms, Constant).
 
 :- func sum_like_terms(lp_terms) = lp_terms.
 
@@ -419,33 +443,34 @@
         )
     ).
 
-non_false_constraint(Terms, Op, Constant) = Constraint :-
-    Constraint = constraint(Terms, Op, Constant),
+construct_non_false_constraint(Terms, Op, Constant) = Constraint :-
+    Constraint = construct_constraint(Terms, Op, Constant),
     ( if    is_false(Constraint)
       then  unexpected(this_file,
                 "non_false_constraints/3: false constraint.")
       else  true
     ).
 
-constraint(lte(Terms, Constant), Terms, (=<), Constant).
-constraint(eq(Terms,  Constant), Terms, (=),  Constant).
-constraint(gte(Terms, Constant), Terms, (>=), Constant).
+deconstruct_constraint(lte(Terms, Constant), Terms, lp_lt_eq, Constant).
+deconstruct_constraint(eq(Terms,  Constant), Terms, lp_eq,    Constant).
+deconstruct_constraint(gte(Terms, Constant), Terms, lp_gt_eq, Constant).
 
-non_false_constraint(Constraint, Terms, Operator, Constant) :-
+deconstruct_non_false_constraint(Constraint, Terms, Operator, Constant) :-
     ( if    is_false(Constraint)
       then  unexpected(this_file,
-                "non_false_constraint/4: false_constraint.")
+                "deconstruct_non_false_constraint/4: false_constraint.")
       else  true
     ),
     (
         Constraint = lte(Terms, Constant),
-        Operator   = (=<)
+        Operator   = lp_lt_eq
     ;
         Constraint = eq(Terms, Constant),
-        Operator   = (=)
+        Operator   = lp_eq
     ;
         Constraint = gte(_, _),
-        unexpected(this_file, "non_false_constraint/4: gte encountered.")
+        unexpected(this_file,
+            "deconstruct_non_false_constraint/4: gte encountered.")
     ).
 
 :- func lp_terms(constraint) = lp_terms.
@@ -460,31 +485,32 @@
 constant(eq(_,  Constant)) = Constant.
 constant(gte(_, Constant)) = Constant.
 
-:- func operator(constraint) = operator.
+:- func operator(constraint) = lp_operator.
 
-operator(lte(_, _)) = (=<).
-operator(eq(_,  _)) = (=).
+operator(lte(_, _)) = lp_lt_eq.
+operator(eq(_,  _)) = lp_eq.
 operator(gte(_,_))  = unexpected(this_file, "operator/1: gte.").
 
-:- func negate_operator(operator) = operator.
+:- func negate_operator(lp_operator) = lp_operator.
 
-negate_operator((=<)) = (>=).
-negate_operator((=))  = (=).
-negate_operator((>=)) = (=<).
+negate_operator(lp_lt_eq) = lp_gt_eq.
+negate_operator(lp_eq)    = lp_eq.
+negate_operator(lp_gt_eq) = lp_lt_eq.
 
 nonneg_constr(lte([_ - (-rat.one)], rat.zero)).
 nonneg_constr(gte(_, _)) :-
     unexpected(this_file, "nonneg_constr/1: gte.").
 
-make_nonneg_constr(Var) = constraint([Var - (-rat.one)], (=<), rat.zero).
+make_nonneg_constr(Var) =
+    construct_constraint([Var - (-rat.one)], lp_lt_eq, rat.zero).
 
 make_vars_eq_constraint(Var1, Var2) =
-    constraint([Var1 - rat.one, Var2 - (-rat.one)], (=), rat.zero).
+    construct_constraint([Var1 - rat.one, Var2 - (-rat.one)], lp_eq, rat.zero).
 
 make_var_const_eq_constraint(Var, Constant) =
-    constraint([Var - rat.one], (=), Constant).
+    construct_constraint([Var - rat.one], lp_eq, Constant).
 make_var_const_gte_constraint(Var, Constant) =
-    constraint([Var - rat.one], (>=), Constant).
+    construct_constraint([Var - rat.one], lp_gt_eq, Constant).
 
 true_constraint = eq([], rat.zero).
 
@@ -1580,7 +1606,7 @@
 
 find_target_equality_2(_, [], _) = no.
 find_target_equality_2(Var, [Eqn | Eqns], Acc) = MaybeTargetEqn :-
-    ( if    operator(Eqn) \= (=)
+    ( if    operator(Eqn) \= lp_eq
       then  unexpected(this_file,
                 "find_target_equality_2/3: inequality encountered.")
       else  true
@@ -1605,15 +1631,9 @@
 
 substitute_variable(Target0, Var, !Equations, !Inequations, Flag) :-
     normalize_constraint(Var, Target0, Target),
-    constraint(Target, TargetCoeffs, Op, TargetConst),
-    (
-        Op = (=)
-    ;
-        ( Op = (=<)
-        ; Op = (>=)
-        ),
-        unexpected(this_file, "substitute_variable/7: inequality encountered.")
-    ),
+    deconstruct_constraint(Target, TargetCoeffs, Op, TargetConst),
+    expect(unify(Op, lp_eq), this_file,
+        "substitute_variable/7: inequality encountered."),
     fix_coeff_and_const(Var, TargetCoeffs, TargetConst, Coeffs, Const),
     substitute_into_constraints(Var, Coeffs, Const, !Equations, EqlFlag),
     substitute_into_constraints(Var, Coeffs, Const, !Inequations, IneqlFlag),
@@ -1661,7 +1681,7 @@
 
 substitute_into_constraint(Var, SubCoeffs, SubConst, !Constraint, Flag) :-
     normalize_constraint(Var, !Constraint),
-    constraint(!.Constraint, TargetCoeffs, Op, TargetConst),
+    deconstruct_constraint(!.Constraint, TargetCoeffs, Op, TargetConst),
     ( list.member(Var - one, TargetCoeffs) ->
         FinalCoeffs0 = lp_terms_to_map(TargetCoeffs ++ SubCoeffs),
         %
@@ -1670,7 +1690,7 @@
         FinalCoeffs1 = map.delete(FinalCoeffs0, Var),
         FinalCoeffs = map.to_assoc_list(FinalCoeffs1),
         FinalConst = TargetConst + SubConst,
-        !:Constraint = constraint(FinalCoeffs, Op, FinalConst),
+        !:Constraint = construct_constraint(FinalCoeffs, Op, FinalConst),
         Flag = yes
     ;
         Flag = no
@@ -2058,7 +2078,7 @@
     is det.
 
 normalize_constraint(Var, Constraint0, Constraint) :-
-    lp_rational.constraint(Constraint0, Terms0, Op0, Constant0),
+    lp_rational.deconstruct_constraint(Constraint0, Terms0, Op0, Constant0),
     ( assoc_list.search(Terms0, Var, Coefficient) ->
         ( if    Coefficient = zero
           then  unexpected(this_file,
@@ -2078,7 +2098,7 @@
         Op       = Op0,
         Constant = Constant0
     ),
-    Constraint = lp_rational.unchecked_constraint(Terms, Op, Constant).
+    Constraint = unchecked_construct_constraint(Terms, Op, Constant).
 
 :- pred add_vectors(map(lp_var, coefficient)::in, constant::in,
     map(lp_var, coefficient)::in, constant::in,
@@ -2286,7 +2306,7 @@
 :- pred write_constraint(lp_varset::in, constraint::in, io::di, io::uo) is det.
 
 write_constraint(Varset, Constr, !IO) :-
-    constraint(Constr, Coeffs, Operator, Constant),
+    deconstruct_constraint(Constr, Coeffs, Operator, Constant),
     io.write_char('\t', !IO),
     list.foldl(write_constr_term(Varset), Coeffs, !IO),
     io.format("%s %s\n", [s(operator_to_string(Operator)),
@@ -2298,11 +2318,11 @@
     VarName = varset.lookup_name(Varset, Var),
     io.format("%s%s ", [s(rat.to_string(Coeff)), s(VarName)], !IO).
 
-:- func operator_to_string(operator) = string.
+:- func operator_to_string(lp_operator) = string.
 
-operator_to_string((=<)) = "=<".
-operator_to_string((=) ) = "=".
-operator_to_string((>=)) = ">=".
+operator_to_string(lp_lt_eq) = "=<".
+operator_to_string(lp_eq )   = "=".
+operator_to_string(lp_gt_eq) = ">=".
 
 :- pred write_vars(varset::in, lp_vars::in, io::di, io::uo) is det.
 
Index: compiler/make.module_dep_file.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/make.module_dep_file.m,v
retrieving revision 1.42
diff -u -b -r1.42 make.module_dep_file.m
--- compiler/make.module_dep_file.m	2 Sep 2009 00:30:16 -0000	1.42
+++ compiler/make.module_dep_file.m	7 Sep 2009 02:38:54 -0000
@@ -418,8 +418,8 @@
                 term.integer(module_dependencies_version_number), [], _),
             SourceFileTerm = term.functor(
                 term.string(SourceFileName), [], _),
-            parse_sym_name_and_args(SourceFileModuleNameTerm,
-                SourceFileModuleName, []),
+            try_parse_sym_name_and_no_args(SourceFileModuleNameTerm,
+                SourceFileModuleName),
             parse_sym_name_list(ParentsTerm, Parents),
             parse_sym_name_list(IntDepsTerm, IntDeps),
             parse_sym_name_list(ImplDepsTerm, ImplDeps),
@@ -449,8 +449,8 @@
                         term.string(LanguageString), [], _),
                     globals.convert_foreign_language(LanguageString,
                         Language),
-                    parse_sym_name_and_args(ImportedModuleTerm,
-                        ImportedModuleName, []),
+                    try_parse_sym_name_and_no_args(ImportedModuleTerm,
+                        ImportedModuleName),
                     ForeignImportModule = foreign_import_module_info(Language,
                         ImportedModuleName, term.context_init)
                 ), ForeignImportTerms, ForeignImports),
@@ -556,10 +556,7 @@
 :- pred parse_sym_name_list(term::in, list(sym_name)::out) is semidet.
 
 parse_sym_name_list(term.functor(term.atom("{}"), Args, _), SymNames) :-
-    list.map(
-        (pred(Arg::in, SymName::out) is semidet :-
-            parse_sym_name_and_args(Arg, SymName, [])
-        ), Args, SymNames).
+    list.map(try_parse_sym_name_and_no_args, Args, SymNames).
 
     % The module_name given must be the top level module in the source file.
     % get_module_dependencies ensures this by making the dependencies
Index: compiler/ml_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_code_gen.m,v
retrieving revision 1.222
diff -u -b -r1.222 ml_code_gen.m
--- compiler/ml_code_gen.m	4 Sep 2009 02:27:52 -0000	1.222
+++ compiler/ml_code_gen.m	4 Sep 2009 18:28:29 -0000
@@ -1326,7 +1326,7 @@
 
     module_info_pred_proc_info(!.ModuleInfo, PredId, ProcId,
         PredInfo, ProcInfo0),
-    requantify_proc(ProcInfo0, ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, ProcInfo0, ProcInfo),
     module_info_set_pred_proc_info(PredId, ProcId, PredInfo, ProcInfo,
         !ModuleInfo),
 
Index: compiler/mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_constraints.m,v
retrieving revision 1.52
diff -u -b -r1.52 mode_constraints.m
--- compiler/mode_constraints.m	19 Aug 2009 07:44:55 -0000	1.52
+++ compiler/mode_constraints.m	4 Sep 2009 18:22:01 -0000
@@ -254,9 +254,11 @@
 
 correct_nonlocals_in_clause_body(Headvars, !Goals, !Varset, !Vartypes,
         !RttiVarMaps) :-
-    implicitly_quantify_clause_body(Headvars, Warnings, !Goals, !Varset,
+    implicitly_quantify_clause_body_general(ordinary_nonlocals_maybe_lambda,
+        Headvars, Warnings, !Goals, !Varset,
         !Vartypes, !RttiVarMaps),
-    (   Warnings = []
+    (
+        Warnings = []
     ;
         Warnings = [_|_],
         unexpected(this_file, "Quantification error during constraints" ++
Index: compiler/modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modes.m,v
retrieving revision 1.384
diff -u -b -r1.384 modes.m
--- compiler/modes.m	4 Sep 2009 02:27:53 -0000	1.384
+++ compiler/modes.m	4 Sep 2009 18:22:10 -0000
@@ -1040,7 +1040,7 @@
             NeedToRequantify = do_not_need_to_requantify
         ;
             NeedToRequantify = need_to_requantify,
-            requantify_proc(!ProcInfo)
+            requantify_proc_general(ordinary_nonlocals_maybe_lambda, !ProcInfo)
         )
     ).
 
Index: compiler/module_qual.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/module_qual.m,v
retrieving revision 1.174
diff -u -b -r1.174 module_qual.m
--- compiler/module_qual.m	2 Sep 2009 00:30:21 -0000	1.174
+++ compiler/module_qual.m	7 Sep 2009 02:09:54 -0000
@@ -669,13 +669,13 @@
 
     % term_qualified_symbols(T, S)
     %
-    % Given a term, T, return the list of all the sym_names, S, in the
-    % term.  The predicate fails if any sub-term of T is unqualified.
+    % Given a term, T, return the list of all the sym_names, S, in the term.
+    % The predicate fails if any sub-term of T is unqualified.
     %
 :- pred term_qualified_symbols(term::in, list(sym_name)::out) is semidet.
 
 term_qualified_symbols(Term, Symbols) :-
-    ( parse_sym_name_and_args(Term, SymName, Args) ->
+    ( try_parse_sym_name_and_args(Term, SymName, Args) ->
         SymName = qualified(_, _),
         term_qualified_symbols_list(Args, Symbols0),
         Symbols = [SymName | Symbols0]
Index: compiler/pd_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_util.m,v
retrieving revision 1.73
diff -u -b -r1.73 pd_util.m
--- compiler/pd_util.m	3 Sep 2009 23:57:28 -0000	1.73
+++ compiler/pd_util.m	4 Sep 2009 18:32:05 -0000
@@ -788,7 +788,8 @@
         proc_info_get_varset(!.ProcInfo, VarSet0),
         proc_info_get_vartypes(!.ProcInfo, VarTypes0),
         proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps0),
-        implicitly_quantify_goal(NonLocals, _, Goal0, Goal, VarSet0, VarSet,
+        implicitly_quantify_goal_general(ordinary_nonlocals_no_lambda,
+            NonLocals, _, Goal0, Goal, VarSet0, VarSet,
             VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
         proc_info_set_varset(VarSet, !ProcInfo),
         proc_info_set_vartypes(VarTypes, !ProcInfo),
Index: compiler/polyhedron.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/polyhedron.m,v
retrieving revision 1.6
diff -u -b -r1.6 polyhedron.m
--- compiler/polyhedron.m	23 Nov 2007 07:35:19 -0000	1.6
+++ compiler/polyhedron.m	7 Sep 2009 04:02:27 -0000
@@ -429,10 +429,10 @@
 
 transform_constraint(Sigma, !Constraint, !VarMap, !Varset) :-
     some [!Terms] (
-        constraint(!.Constraint, !:Terms, Op, Const),
+        deconstruct_constraint(!.Constraint, !:Terms, Op, Const),
         list.map_foldl2(change_var, !Terms, !VarMap, !Varset),
-        list.cons( Sigma - (-Const), !Terms),
-        !:Constraint = constraint(!.Terms, Op, zero)
+        list.cons(Sigma - (-Const), !Terms),
+        !:Constraint = construct_constraint(!.Terms, Op, zero)
     ).
 
     % change_var: takes a Var-Num pair with an old variable and returns one
@@ -445,10 +445,9 @@
 change_var(!Term, !VarMap, !Varset) :-
     some [!Var] (
         !.Term = !:Var - Coefficient,
-        %
+
         % Have we already mapped this original variable to a new one?
-        %
-        ( !:Var = !.VarMap ^ elem(!.Var) ->
+        ( map.search(!.VarMap, !.Var, !:Var) ->
             true
         ;
             svvarset.new_var(NewVar, !Varset),
@@ -462,16 +461,13 @@
     constraints::in, constraints::out) is det.
 
 add_sigma_constraints(Sigmas, !Constraints) :-
-    %
     % Add non-negativity constraints for each sigma variable.
-    %
     SigmaConstraints = list.map(make_nonneg_constr, Sigmas),
     list.append(SigmaConstraints, !Constraints),
-    %
+
     % The sum of all the sigma variables is one.
-    %
     SigmaTerms = list.map(lp_term, Sigmas),
-    list.cons(constraint(SigmaTerms, (=), one), !Constraints).
+    list.cons(construct_constraint(SigmaTerms, lp_eq, one), !Constraints).
 
     % Add a constraint specifying that each variable is the sum of the
     % temporary variables to which it has been mapped.
@@ -497,13 +493,14 @@
 
 make_last_constraint(VarMaps, OriginalVar) = Constraint :-
     list.foldl(make_last_terms(OriginalVar), VarMaps, [], LastTerms),
-    Constraint = constraint([OriginalVar - one | LastTerms], (=), zero).
+    AllTerms = [OriginalVar - one | LastTerms],
+    Constraint = construct_constraint(AllTerms, lp_eq, zero).
 
 :- pred make_last_terms(lp_var::in, var_map::in, lp_terms::in, lp_terms::out)
     is semidet.
 
 make_last_terms(OriginalVar, VarMap, !Terms) :-
-    NewVar = VarMap ^ elem(OriginalVar),
+    map.search(VarMap, OriginalVar, NewVar),
     list.cons(NewVar - (-one), !Terms).
 
 %-----------------------------------------------------------------------------%
Index: compiler/polymorphism.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/polymorphism.m,v
retrieving revision 1.344
diff -u -b -r1.344 polymorphism.m
--- compiler/polymorphism.m	3 Sep 2009 23:57:28 -0000	1.344
+++ compiler/polymorphism.m	7 Sep 2009 12:47:30 -0000
@@ -1376,10 +1376,10 @@
 
     (
         % Check if variable has a higher order type.
-        type_is_higher_order_details(TypeOfX, Purity, _PredOrFunc, EvalMethod,
-            CalleeArgTypes),
         ConsId0 = closure_cons(ShroudedPredProcId, _),
-        proc(PredId, ProcId0) = unshroud_pred_proc_id(ShroudedPredProcId)
+        proc(PredId, ProcId0) = unshroud_pred_proc_id(ShroudedPredProcId),
+        type_is_higher_order_details(TypeOfX, Purity, _PredOrFunc, EvalMethod,
+            CalleeArgTypes)
     ->
         % An `invalid_proc_id' means the predicate is multi-moded. We can't
         % pick the right mode yet. Perform the rest of the transformation with
@@ -2127,7 +2127,8 @@
         poly_info_get_var_types(!.Info, VarTypes0),
         poly_info_get_rtti_varmaps(!.Info, RttiVarMaps0),
         OutsideVars = proc_arg_vector_to_set(HeadVars),
-        implicitly_quantify_goal(OutsideVars, _Warnings, Goal0, Goal,
+        implicitly_quantify_goal_general(ordinary_nonlocals_maybe_lambda,
+            OutsideVars, _Warnings, Goal0, Goal,
             VarSet0, VarSet, VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
         poly_info_set_varset_and_types(VarSet, VarTypes, !Info),
         poly_info_set_rtti_varmaps(RttiVarMaps, !Info)
@@ -2162,7 +2163,8 @@
         goal_util.extra_nonlocal_typeinfos(RttiVarMaps0, VarTypes0,
             ExistQVars, NonLocalsPlusArgs, NewOutsideVars),
         set.union(NonLocals, NewOutsideVars, OutsideVars),
-        implicitly_quantify_goal(OutsideVars, _Warnings, !Goal,
+        implicitly_quantify_goal_general(ordinary_nonlocals_maybe_lambda,
+            OutsideVars, _Warnings, !Goal,
             VarSet0, VarSet, VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
         poly_info_set_varset_and_types(VarSet, VarTypes, !Info),
         poly_info_set_rtti_varmaps(RttiVarMaps, !Info)
Index: compiler/post_typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/post_typecheck.m,v
retrieving revision 1.136
diff -u -b -r1.136 post_typecheck.m
--- compiler/post_typecheck.m	4 Sep 2009 02:27:54 -0000	1.136
+++ compiler/post_typecheck.m	7 Sep 2009 12:53:48 -0000
@@ -314,11 +314,11 @@
         type_vars(Type, TVars),
         set.list_to_set(TVars, TVarsSet0),
         set.delete_list(TVarsSet0, HeadTypeParams, TVarsSet1),
-        ( \+ set.empty(TVarsSet1) ->
+        ( set.empty(TVarsSet1) ->
+            true
+        ;
             !:UnresolvedVarsTypes = [Var - Type | !.UnresolvedVarsTypes],
             set.union(!.Set, TVarsSet1, !:Set)
-        ;
-            true
         ),
         check_var_type_bindings_2(VarTypes, HeadTypeParams,
             VarsToDo - 1, LeftOverVarTypes, !UnresolvedVarsTypes, !Set)
Index: compiler/prog_ctgc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_ctgc.m,v
retrieving revision 1.24
diff -u -b -r1.24 prog_ctgc.m
--- compiler/prog_ctgc.m	2 Sep 2009 00:30:21 -0000	1.24
+++ compiler/prog_ctgc.m	7 Sep 2009 02:10:08 -0000
@@ -203,7 +203,7 @@
             Args = [ConsTerm, ArityTerm, PosTerm]
         ->
             (
-                parse_sym_name_and_args(ConsTerm, ConsIdName, []),
+                try_parse_sym_name_and_no_args(ConsTerm, ConsIdName),
                 ArityTerm = term.functor(term.integer(Arity), _, _),
                 PosTerm = term.functor(term.integer(Pos), _, _)
             ->
Index: compiler/prog_io.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io.m,v
retrieving revision 1.299
diff -u -b -r1.299 prog_io.m
--- compiler/prog_io.m	14 Aug 2009 20:37:48 -0000	1.299
+++ compiler/prog_io.m	7 Sep 2009 02:40:32 -0000
@@ -642,9 +642,8 @@
     % of the special "root" module (so that any `:- module' declaration
     % is taken to be a non-nested module unless explicitly qualified).
     parser.read_term_filename(!.SourceFileName, MaybeFirstTerm, !IO),
-    root_module_name(RootModuleName),
-    read_term_to_item_result(RootModuleName, !.SourceFileName, MaybeFirstTerm,
-        !SeqNumCounter, MaybeFirstItem),
+    read_term_to_item_result(root_module_name, !.SourceFileName,
+        MaybeFirstTerm, !SeqNumCounter, MaybeFirstItem),
     (
         % Apply and then skip `pragma source_file' decls, by calling ourselves
         % recursively with the new source file name.
@@ -842,9 +841,8 @@
             !:ModuleName = NestedModuleName,
             !:Items = [Item | !.Items]
         ; ModuleDefn = md_end_module(NestedModuleName) ->
-            root_module_name(RootModuleName),
-            sym_name_get_module_name_default(NestedModuleName, RootModuleName,
-                ParentModuleName),
+            sym_name_get_module_name_default(NestedModuleName,
+                root_module_name, ParentModuleName),
             !:ModuleName = ParentModuleName,
             !:Items = [Item | !.Items]
         ; ModuleDefn = md_import(Modules) ->
@@ -942,14 +940,14 @@
             BodyTerm = term.functor(term.atom("true"), [], ClauseContext)
         ),
         varset.coerce(VarSet, ProgVarSet),
-        parse_clause(ModuleName, Term, HeadTerm, BodyTerm, ProgVarSet,
+        parse_clause(ModuleName, HeadTerm, BodyTerm, ProgVarSet,
             ClauseContext, SeqNum, Result)
     ).
 
-:- pred parse_clause(module_name::in, term::in, term::in, term::in,
+:- pred parse_clause(module_name::in, term::in, term::in,
     prog_varset::in, term.context::in, int::in, maybe1(item)::out) is det.
 
-parse_clause(ModuleName, Term, HeadTerm, BodyTerm0, ProgVarSet0, Context,
+parse_clause(ModuleName, HeadTerm, BodyTerm0, ProgVarSet0, Context,
         SeqNum, MaybeItem) :-
     GoalContextPieces = [],
     parse_goal(BodyTerm0, GoalContextPieces, MaybeBodyGoal,
@@ -963,8 +961,8 @@
             FuncHeadTerm = desugar_field_access(FuncHeadTerm0)
         ->
             HeadContextPieces = [words("In equation head:")],
-            parse_implicitly_qualified_term(ModuleName, FuncHeadTerm, HeadTerm,
-                VarSet, HeadContextPieces, MaybeFunctor),
+            parse_implicitly_qualified_sym_name_and_args(ModuleName,
+                FuncHeadTerm, VarSet, HeadContextPieces, MaybeFunctor),
             (
                 MaybeFunctor = ok2(Name, ArgTerms0),
                 list.map(term.coerce, ArgTerms0 ++ [FuncResultTerm],
@@ -979,7 +977,7 @@
             )
         ;
             HeadContextPieces = [words("In clause head:")],
-            parse_implicitly_qualified_term(ModuleName, HeadTerm, Term,
+            parse_implicitly_qualified_sym_name_and_args(ModuleName, HeadTerm,
                 VarSet, HeadContextPieces, MaybeFunctor),
             (
                 MaybeFunctor = ok2(Name, ArgTerms),
@@ -1181,8 +1179,7 @@
         % module being ended, so the default module name here (ModuleName)
         % is the parent of the previous default module name.
 
-        root_module_name(RootModuleName),
-        sym_name_get_module_name_default(ModuleName, RootModuleName,
+        sym_name_get_module_name_default(ModuleName, root_module_name,
             ParentOfModuleName),
         parse_module_name(ParentOfModuleName, VarSet, ModuleNameTerm,
             MaybeModuleNameSym),
@@ -1285,16 +1282,14 @@
 :- pred process_version_numbers(module_name::in, varset::in, list(term)::in,
     decl_attrs::in, prog_context::in, int::in, maybe1(item)::out) is semidet.
 
-process_version_numbers(ModuleName, VarSet, ArgTerms, Attributes, Context,
+process_version_numbers(ModuleName, _VarSet, ArgTerms, Attributes, Context,
         SeqNum, MaybeItem) :-
     ArgTerms = [VersionNumberTerm, ModuleNameTerm, VersionNumbersTerm],
-    parse_module_specifier(VarSet, ModuleNameTerm, MaybeModuleName),
     (
         VersionNumberTerm = term.functor(term.integer(VersionNumber), [], _),
         VersionNumber = version_numbers_version_number
     ->
-        (
-            MaybeModuleName = ok1(ModuleName),
+        ( try_parse_symbol_name(ModuleNameTerm, ModuleName) ->
             recompilation.version.parse_version_numbers(VersionNumbersTerm,
                 MaybeItem0),
             (
@@ -1310,8 +1305,6 @@
                 MaybeItem = error1(Specs)
             )
         ;
-            % XXX _Spec
-            MaybeModuleName = error1(_Spec),
             Pieces = [words("Error: invalid module name in"),
                 quote(":- version_numbers"), suffix("."), nl],
             Spec = error_spec(severity_error, phase_term_to_parse_tree,
@@ -1427,7 +1420,7 @@
             ok3(ExistQVars, Constraints, InstConstraints),
         ContextPieces = [words("In")] ++ pred_or_func_decl_pieces(PredOrFunc)
             ++ [suffix(":")],
-        parse_implicitly_qualified_term(ModuleName, PredTypeTerm, PredTypeTerm,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, PredTypeTerm,
             VarSet, ContextPieces, MaybePredNameAndArgs),
         (
             MaybePredNameAndArgs = error2(Specs),
@@ -1530,7 +1523,7 @@
             FuncTerm = desugar_field_access(MaybeSugaredFuncTerm),
             ContextPieces = [words("In"), quote(":- func"),
                 words("declaration")],
-            parse_implicitly_qualified_term(ModuleName, FuncTerm, Term,
+            parse_implicitly_qualified_sym_name_and_args(ModuleName, FuncTerm,
                 VarSet, ContextPieces, MaybeFuncNameAndArgs),
             (
                 MaybeFuncNameAndArgs = error2(Specs),
@@ -1767,7 +1760,7 @@
         FuncTerm = desugar_field_access(MaybeSugaredFuncTerm),
         ContextPieces = [words("In function"), quote(":- mode"),
             words("declaration")],
-        parse_implicitly_qualified_term(ModuleName, FuncTerm, Term,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, FuncTerm,
             VarSet, ContextPieces, MaybeFunctorArgs),
         (
             MaybeFunctorArgs = error2(Specs),
@@ -1780,7 +1773,7 @@
         )
     ;
         ContextPieces = [words("In"), quote(":- mode"), words("declaration")],
-        parse_implicitly_qualified_term(ModuleName, Term, Term,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, Term,
             VarSet, ContextPieces, MaybeFunctorArgs),
         (
             MaybeFunctorArgs = error2(Specs),
Index: compiler/prog_io_dcg.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_dcg.m,v
retrieving revision 1.48
diff -u -b -r1.48 prog_io_dcg.m
--- compiler/prog_io_dcg.m	2 Sep 2009 00:30:22 -0000	1.48
+++ compiler/prog_io_dcg.m	7 Sep 2009 02:40:39 -0000
@@ -73,7 +73,7 @@
     (
         MaybeBody = ok1(Body),
         HeadContextPieces = [words("In DCG clause head:")],
-        parse_implicitly_qualified_term(ModuleName, DCG_Head, DCG_Body,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, DCG_Head,
             VarSet0, HeadContextPieces, HeadResult),
         process_dcg_clause(HeadResult, ProgVarSet, DCG_0_Var, DCG_Var, Body,
             DCG_Context, SeqNum, MaybeItem)
@@ -122,7 +122,7 @@
     % Next, parse it.
     (
         term.coerce(Term, ProgTerm),
-        parse_sym_name_and_args(ProgTerm, SymName, Args0)
+        try_parse_sym_name_and_args(ProgTerm, SymName, Args0)
     ->
         % First check for the special cases:
         (
Index: compiler/prog_io_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_goal.m,v
retrieving revision 1.60
diff -u -b -r1.60 prog_io_goal.m
--- compiler/prog_io_goal.m	2 Sep 2009 00:30:22 -0000	1.60
+++ compiler/prog_io_goal.m	7 Sep 2009 02:15:18 -0000
@@ -132,7 +132,7 @@
         % It's not a builtin.
         term.coerce(Term, ArgsTerm),
         % Check for predicate calls.
-        ( parse_sym_name_and_args(ArgsTerm, SymName, Args) ->
+        ( try_parse_sym_name_and_args(ArgsTerm, SymName, Args) ->
             GoalExpr = call_expr(SymName, Args, purity_pure)
         ;
             % A call to a free variable, or to a number or string.
Index: compiler/prog_io_mode_defn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_mode_defn.m,v
retrieving revision 1.1
diff -u -b -r1.1 prog_io_mode_defn.m
--- compiler/prog_io_mode_defn.m	3 Dec 2008 05:01:41 -0000	1.1
+++ compiler/prog_io_mode_defn.m	7 Sep 2009 02:40:55 -0000
@@ -83,7 +83,7 @@
 parse_inst_defn_base(ModuleName, VarSet, HeadTerm, BodyTerm, Condition,
         Context, SeqNum, MaybeItem) :-
     ContextPieces = [words("In inst definition:")],
-    parse_implicitly_qualified_term(ModuleName, HeadTerm, BodyTerm,
+    parse_implicitly_qualified_sym_name_and_args(ModuleName, HeadTerm,
         VarSet, ContextPieces, MaybeNameAndArgs),
     (
         MaybeNameAndArgs = error2(Specs),
@@ -171,7 +171,7 @@
 parse_abstract_inst_defn(ModuleName, VarSet, HeadTerm, Condition, Context,
         SeqNum, MaybeItem) :-
     ContextPieces = [words("In inst definition:")],
-    parse_implicitly_qualified_term(ModuleName, HeadTerm, HeadTerm,
+    parse_implicitly_qualified_sym_name_and_args(ModuleName, HeadTerm,
         VarSet, ContextPieces, MaybeNameAndArgs),
     (
         MaybeNameAndArgs = error2(Specs),
@@ -225,7 +225,7 @@
 parse_mode_defn(ModuleName, VarSet, HeadTerm, BodyTerm, Condition, Context,
         SeqNum, MaybeItem) :-
     ContextPieces = [words("In mode definition:")],
-    parse_implicitly_qualified_term(ModuleName, HeadTerm, HeadTerm,
+    parse_implicitly_qualified_sym_name_and_args(ModuleName, HeadTerm,
         VarSet, ContextPieces, MaybeModeNameAndArgs),
     (
         MaybeModeNameAndArgs = error2(Specs),
Index: compiler/prog_io_pragma.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_pragma.m,v
retrieving revision 1.139
diff -u -b -r1.139 prog_io_pragma.m
--- compiler/prog_io_pragma.m	2 Sep 2009 00:30:22 -0000	1.139
+++ compiler/prog_io_pragma.m	7 Sep 2009 02:51:22 -0000
@@ -277,16 +277,16 @@
             VarSet, Context, SeqNum, MaybeItem)
     ;
         PragmaName = "exceptions",
-        parse_pragma_exceptions(ModuleName, PragmaTerms, ErrorTerm, VarSet,
+        parse_pragma_exceptions(ModuleName, PragmaTerms, ErrorTerm,
             Context, SeqNum, MaybeItem)
     ;
         PragmaName = "trailing_info",
-        parse_pragma_trailing_info(ModuleName, PragmaTerms, ErrorTerm, VarSet,
+        parse_pragma_trailing_info(ModuleName, PragmaTerms, ErrorTerm,
             Context, SeqNum, MaybeItem)
     ;
         PragmaName = "mm_tabling_info",
         parse_pragma_mm_tabling_info(ModuleName, PragmaTerms, ErrorTerm,
-            VarSet, Context, SeqNum, MaybeItem)
+            Context, SeqNum, MaybeItem)
     ;
         PragmaName = "require_feature_set",
         parse_pragma_require_feature_set(PragmaTerms, VarSet, ErrorTerm,
@@ -533,7 +533,7 @@
     Functor = term.atom("-"),
     Args = [SymNameTerm, StringTerm],
     StringTerm = functor(term.string(String), _, _),
-    parse_qualified_term(SymNameTerm, SymNameTerm, VarSet, ContextPieces,
+    parse_sym_name_and_args(SymNameTerm, VarSet, ContextPieces,
         MaybeSymNameResult),
     (
         MaybeSymNameResult = ok2(SymName, []),
@@ -806,7 +806,7 @@
         MaybeItem) :-
     (
         PragmaTerms = [ImportTerm],
-        parse_sym_name_and_args(ImportTerm, Import, [])
+        try_parse_sym_name_and_no_args(ImportTerm, Import)
     ->
         Pragma = pragma_foreign_import_module(lang_c, Import),
         ItemPragma = item_pragma_info(user, Pragma, Context, SeqNum),
@@ -828,7 +828,7 @@
         MaybeItem) :-
     (
         PragmaTerms = [LangTerm, ImportTerm],
-        parse_sym_name_and_args(ImportTerm, Import, [])
+        try_parse_sym_name_and_no_args(ImportTerm, Import)
     ->
         ( parse_foreign_language(LangTerm, Language) ->
             Pragma = pragma_foreign_import_module(Language, Import),
@@ -970,11 +970,8 @@
         ArityTerm = term.functor(term.integer(Arity), [], _),
         ModeNumTerm = term.functor(term.integer(ModeNum), [], _),
         parse_predicate_or_function(PredOrFuncTerm, PredOrFunc),
-        ContextPieces = [words("In"), quote(":- pragma unused_args"),
-            words("declaration:")],
-        parse_implicitly_qualified_term(ModuleName, PredNameTerm, ErrorTerm,
-            VarSet, ContextPieces, MaybePredName),
-        MaybePredName = ok2(PredName, []),
+        try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+            PredNameTerm, PredName),
         convert_int_list(VarSet, UnusedArgsTerm, MaybeUnusedArgs),
         MaybeUnusedArgs = ok1(UnusedArgs)
     ->
@@ -1009,12 +1006,8 @@
             term.context_file(SpecContext, FileName),
             \+ string.remove_suffix(FileName, ".m", _),
 
-            % The value of ContextPieces does not matter here since we succeed
-            % only if it isn't used.
-            NameContextPieces = [],
-            parse_implicitly_qualified_term(ModuleName, SpecNameTerm,
-                ErrorTerm, VarSet, NameContextPieces, NameResult),
-            NameResult = ok2(SpecName, []),
+            try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+                SpecNameTerm, SpecName),
             MaybeName = yes(SpecName)
         )
     ->
@@ -1298,9 +1291,9 @@
     ).
 
 :- pred parse_pragma_exceptions(module_name::in, list(term)::in, term::in,
-    varset::in, prog_context::in, int::in, maybe1(item)::out) is det.
+    prog_context::in, int::in, maybe1(item)::out) is det.
 
-parse_pragma_exceptions(ModuleName, PragmaTerms, ErrorTerm, VarSet, Context,
+parse_pragma_exceptions(ModuleName, PragmaTerms, ErrorTerm, Context,
         SeqNum, MaybeItem) :-
     (
         PragmaTerms = [PredOrFuncTerm, PredNameTerm, ArityTerm, ModeNumTerm,
@@ -1308,11 +1301,8 @@
         parse_predicate_or_function(PredOrFuncTerm, PredOrFunc),
         ArityTerm = term.functor(term.integer(Arity), [], _),
         ModeNumTerm = term.functor(term.integer(ModeNum), [], _),
-        ContextPieces = [words("In"), quote(":- pragma exceptions"),
-            words("declaration:")],
-        parse_implicitly_qualified_term(ModuleName, PredNameTerm, ErrorTerm,
-            VarSet, ContextPieces, MaybePredNameAndArgs),
-        MaybePredNameAndArgs = ok2(PredName, []),
+        try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+            PredNameTerm, PredName),
         ThrowStatusTerm = term.functor(term.atom(ThrowStatusFunctor),
             ThrowStatusArgTerms, _),
         (
@@ -1352,9 +1342,9 @@
     ).
 
 :- pred parse_pragma_trailing_info(module_name::in, list(term)::in, term::in,
-    varset::in, prog_context::in, int::in, maybe1(item)::out) is det.
+    prog_context::in, int::in, maybe1(item)::out) is det.
 
-parse_pragma_trailing_info(ModuleName, PragmaTerms, ErrorTerm, VarSet, Context,
+parse_pragma_trailing_info(ModuleName, PragmaTerms, ErrorTerm, Context,
         SeqNum, MaybeItem) :-
     (
         PragmaTerms = [PredOrFuncTerm, PredNameTerm, ArityTerm, ModeNumTerm,
@@ -1362,11 +1352,8 @@
         parse_predicate_or_function(PredOrFuncTerm, PredOrFunc),
         ArityTerm = term.functor(term.integer(Arity), [], _),
         ModeNumTerm = term.functor(term.integer(ModeNum), [], _),
-        ContextPieces = [words("In"), quote(":- pragma trailing_info"),
-            words("declaration:")],
-        parse_implicitly_qualified_term(ModuleName, PredNameTerm, ErrorTerm,
-            VarSet, ContextPieces, MaybePredNameAndArgs),
-        MaybePredNameAndArgs = ok2(PredName, []),
+        try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+            PredNameTerm, PredName),
         TrailingStatusTerm = term.functor(term.atom(TrailingStatusFunctor),
             [], _),
         (
@@ -1394,9 +1381,9 @@
     ).
 
 :- pred parse_pragma_mm_tabling_info(module_name::in, list(term)::in, term::in,
-    varset::in, prog_context::in, int::in, maybe1(item)::out) is det.
+    prog_context::in, int::in, maybe1(item)::out) is det.
 
-parse_pragma_mm_tabling_info(ModuleName, PragmaTerms, ErrorTerm, VarSet,
+parse_pragma_mm_tabling_info(ModuleName, PragmaTerms, ErrorTerm,
         Context, SeqNum, MaybeItem) :-
     (
         PragmaTerms = [PredOrFuncTerm, PredNameTerm, ArityTerm, ModeNumTerm,
@@ -1404,11 +1391,8 @@
         parse_predicate_or_function(PredOrFuncTerm, PredOrFunc),
         ArityTerm = term.functor(term.integer(Arity), [], _),
         ModeNumTerm = term.functor(term.integer(ModeNum), [], _),
-        ContextPieces = [words("In"), quote(":- pragma mm_tabling_info"),
-            words("declaration:")],
-        parse_implicitly_qualified_term(ModuleName, PredNameTerm, ErrorTerm,
-            VarSet, ContextPieces, MaybePredNameAndArgs),
-        MaybePredNameAndArgs = ok2(PredName, []),
+        try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+            PredNameTerm, PredName),
         MM_TablingStatusTerm = term.functor(term.atom(MM_TablingStatusFunctor),
             [], _),
         (
@@ -2624,7 +2608,7 @@
     ContextPieces = [words("In"), quote(":- pragma c_code"),
         words("declaration:")],
     parse_pred_or_func_and_args_general(yes(ModuleName), PredAndVarsTerm0,
-        PredAndVarsTerm0, VarSet, ContextPieces, MaybePredAndArgs),
+        VarSet, ContextPieces, MaybePredAndArgs),
     (
         MaybePredAndArgs = ok2(PredName, VarList0 - MaybeRetTerm),
         % Is this a function or a predicate?
@@ -3013,12 +2997,8 @@
             [PredNameTerm, ArityTerm], _)
     ->
         (
-            % The value of ContextPieces does not matter here since we succeed
-            % only if it isn't used.
-            PredNameContextPieces = [],
-            parse_implicitly_qualified_term(ModuleName, PredNameTerm,
-                PredAndModesTerm0, VarSet, PredNameContextPieces,
-                ok2(PredName, [])),
+            try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+                PredNameTerm, PredName),
             ArityTerm = term.functor(term.integer(Arity), [], _)
         ->
             MaybeArityOrModes = ok1(arity_or_modes(PredName, Arity, no, no))
@@ -3062,7 +3042,7 @@
 parse_pred_or_func_and_arg_modes(MaybeModuleName, PredAndModesTerm,
         ErrorTerm, VarSet, ContextPieces, MaybeNameAndModes) :-
     parse_pred_or_func_and_args_general(MaybeModuleName, PredAndModesTerm,
-        ErrorTerm, VarSet, ContextPieces, MaybePredAndArgs),
+        VarSet, ContextPieces, MaybePredAndArgs),
     (
         MaybePredAndArgs = ok2(PredName, ArgModeTerms - MaybeRetModeTerm),
         (
Index: compiler/prog_io_sym_name.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_sym_name.m,v
retrieving revision 1.2
diff -u -b -r1.2 prog_io_sym_name.m
--- compiler/prog_io_sym_name.m	2 Sep 2009 00:30:22 -0000	1.2
+++ compiler/prog_io_sym_name.m	7 Sep 2009 22:43:43 -0000
@@ -5,6 +5,19 @@
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %-----------------------------------------------------------------------------%
+%
+% Many of the predicates below exist in two or three forms.
+%
+% - The first form, whose name is of the form parse_<something>, always
+%   succeeds; if it doesn't find the thing it is looking for, it returns
+%   an error message.
+% - The second form, whose name is of the form try_parse_<something>, exists
+%   because building this error message is a waste of time if not finding
+%   <something> is not in fact an error, which happens often. This second form
+%   simply fails when it does not find <something>.
+% - The third form exists in cases where our caller imposes further
+%   restrictions on the form of the thing being looked for, such as a list
+%   of arguments being empty.
 
 :- module parse_tree.prog_io_sym_name.
 :- interface.
@@ -18,61 +31,64 @@
 :- import_module term.
 :- import_module varset.
 
-% A QualifiedTerm is one of
-%   Name(Args)
-%   Module.Name(Args)
-% (or if Args is empty, one of
-%   Name
-%   Module.Name)
-% where Module is a SymName. For backwards compatibility, we allow `__'
-% as an alternative to `.'.
-
-    % Sym_name_and_args takes a term and returns a sym_name that is its
-    % top function symbol, and a list of its argument terms. It fails
-    % if the input is not valid syntax for a QualifiedTerm.
-    %
-:- pred parse_sym_name_and_args(term(T)::in, sym_name::out, list(term(T))::out)
-    is semidet.
+    % A SymNameAndArgs is one of
+    %   Name(Args)
+    %   Module.Name(Args)
+    % (or if Args is empty, one of
+    %   Name
+    %   Module.Name)
+    % where Module is a SymName. For backwards compatibility, we allow `__'
+    % as an alternative to `.'.
 
-    % parse_qualified_term(Term, _ContainingTerm, VarSet, ContextPieces,
-    %   Result):
+    % parse_sym_name_and_args(Term, VarSet, ContextPieces, Result):
     %
     % Parse Term into a sym_name that is its top function symbol and a
     % list of its argument terms, and if successful return them in Result.
-    % (parse_qualified_term thus does the same job as sym_name_and_args
-    % if it succeeds.) However, in case it does not succced,
-    % parse_qualified_term also takes as input Varset (from which the variables
-    % in Term are taken), the term containing Term, and a format_component
-    % list describing the context from which it was called, e.g.
-    % "In clause head:". XXX Currently, _ContainingTerm isn't used;
-    % maybe it should be deleted.
-    %
-    % Note: parse_qualified_term is used for places where a symbol is _used_,
-    % where no default module name exists for the sym_name. For places
-    % where a symbol is _defined_, use parse_implicitly_qualified_term.
-    %
-    % If you care only about the case where Result = ok2(SymName, Args),
-    % use sym_name_and_args.
+    % However, in case it does not succced, parse_sym_name_and_args
+    % also takes as input Varset (from which the variables in Term are taken),
+    % and a format_component list describing the context from which it was
+    % called, e.g. "In clause head:".
+    %
+    % Note: parse_sym_name_and_args is intended for places where a symbol
+    % is _used_, where no default module name exists for the sym_name.
+    % For places where a symbol is _defined_, use the related predicate
+    % parse_implicitly_qualified_sym_name_and_args.
     %
-:- pred parse_qualified_term(term(T)::in, term(T)::in, varset::in,
+:- pred parse_sym_name_and_args(term(T)::in, varset::in,
     list(format_component)::in, maybe_functor(T)::out) is det.
+:- pred try_parse_sym_name_and_args(term(T)::in,
+    sym_name::out, list(term(T))::out) is semidet.
+:- pred try_parse_sym_name_and_no_args(term(T)::in, sym_name::out)
+    is semidet.
 
-    % parse_implicitly_qualified_term(ModuleName, Term, _ContainingTerm,
+    % parse_implicitly_qualified_sym_name_and_args(ModuleName, Term,
     %   VarSet, ContextPieces, Result):
     %
     % Parse Term into a sym_name that is its top function symbol and a
     % list of its argument terms, and if successful return them in Result.
     % This predicate thus does almost the same job as the predicate
-    % parse_implicitly_qualified_term above, the difference being that
-    % that if the sym_name is qualified, then we check whether it is qualified
+    % parse_sym_name_and_args above, the difference being that
+    % if the sym_name is qualified, then we check whether it is qualified
     % with ModuleName, and if it isn't qualified, then we qualify it with
-    % Modulename (unless ModuleName is root_module_name). This is the
+    % ModuleName (unless ModuleName is root_module_name). This is the
     % right thing to do for clause heads, which is the intended job of
-    % parse_implicitly_qualified_term.
+    % parse_implicitly_qualified_sym_name_and_args.
     %
-:- pred parse_implicitly_qualified_term(module_name::in, term(T)::in,
+:- pred parse_implicitly_qualified_sym_name_and_args(module_name::in,
     term(T)::in, varset::in, list(format_component)::in, maybe_functor(T)::out)
     is det.
+:- pred try_parse_implicitly_qualified_sym_name_and_args(module_name::in,
+    term(T)::in, sym_name::out, list(term(T))::out) is semidet.
+:- pred try_parse_implicitly_qualified_sym_name_and_no_args(module_name::in,
+    term(T)::in, sym_name::out) is semidet.
+
+    % A SymbolNameSpecifier is one of
+    %   SymbolName
+    %   SymbolName/Arity
+    %       Matches only symbols of the specified arity.
+    %
+:- pred parse_symbol_name_specifier(varset::in, term::in,
+    maybe1(sym_name_specifier)::out) is det.
 
     % A SymbolName is one of
     %   Name
@@ -87,26 +103,19 @@
     %
 :- pred parse_symbol_name(varset(T)::in, term(T)::in, maybe1(sym_name)::out)
     is det.
+:- pred try_parse_symbol_name(term(T)::in, sym_name::out) is semidet.
 
 :- pred parse_implicitly_qualified_symbol_name(module_name::in, varset::in,
     term::in, maybe1(sym_name)::out) is det.
 
-    % A SymbolNameSpecifier is one of
-    %   SymbolName
-    %   SymbolName/Arity
-    %       Matches only symbols of the specified arity.
-    %
-:- pred parse_symbol_name_specifier(varset::in, term::in,
-    maybe1(sym_name_specifier)::out) is det.
-
 :- pred parse_implicitly_qualified_symbol_name_specifier(module_name::in,
     varset::in, term::in, maybe1(sym_name_specifier)::out) is det.
 
     % We use the empty module name ('') as the "root" module name; when adding
-    % default module qualifiers in parse_implicitly_qualified_{term,symbol},
+    % default module qualifiers in parse_implicitly_qualified_*,
     % if the default module is the root module then we don't add any qualifier.
     %
-:- pred root_module_name(module_name::out) is det.
+:- func root_module_name = module_name.
 
 %-----------------------------------------------------------------------------e
 
@@ -116,17 +125,7 @@
 
 :- import_module int.
 
-parse_sym_name_and_args(Term, SymName, Args) :-
-    % The values of VarSet and ContextPieces do not matter here, since
-    % we succeed only if they aren't used.
-    VarSet = varset.init,
-    ContextPieces = [],
-    parse_qualified_term(Term, Term, VarSet, ContextPieces,
-        ok2(SymName, Args)).
-
-parse_qualified_term(Term, _ContainingTerm, VarSet, ContextPieces,
-        MaybeSymNameAndArgs) :-
-    % XXX We should delete the _ContainingTerm argument.
+parse_sym_name_and_args(Term, VarSet, ContextPieces, MaybeSymNameAndArgs) :-
     (
         Term = term.functor(Functor, FunctorArgs, TermContext),
         Functor = term.atom("."),
@@ -177,14 +176,43 @@
         )
     ).
 
-parse_implicitly_qualified_term(DefaultModuleName, Term, ContainingTerm,
+try_parse_sym_name_and_args(Term, SymName, Args) :-
+    (
+        Term = term.functor(Functor, FunctorArgs, _TermContext),
+        Functor = term.atom("."),
+        FunctorArgs = [ModuleTerm, NameArgsTerm]
+    ->
+        NameArgsTerm = term.functor(term.atom(Name), Args, _),
+        try_parse_symbol_name(ModuleTerm, Module),
+        SymName = qualified(Module, Name)
+    ;
+        Term = term.functor(term.atom(Name), Args, _),
+        SymName = string_to_sym_name_sep(Name, "__")
+    ).
+
+try_parse_sym_name_and_no_args(Term, SymName) :-
+    (
+        Term = term.functor(Functor, FunctorArgs, _TermContext),
+        Functor = term.atom("."),
+        FunctorArgs = [ModuleTerm, NameArgsTerm]
+    ->
+        NameArgsTerm = term.functor(term.atom(Name), [], _),
+        try_parse_symbol_name(ModuleTerm, Module),
+        SymName = qualified(Module, Name)
+    ;
+        Term = term.functor(term.atom(Name), [], _),
+        SymName = string_to_sym_name_sep(Name, "__")
+    ).
+
+%-----------------------------------------------------------------------------e
+
+parse_implicitly_qualified_sym_name_and_args(DefaultModuleName, Term,
         VarSet, ContextPieces, MaybeSymNameAndArgs) :-
-    parse_qualified_term(Term, ContainingTerm, VarSet, ContextPieces,
-        MaybeSymNameAndArgs0),
+    parse_sym_name_and_args(Term, VarSet, ContextPieces, MaybeSymNameAndArgs0),
     (
         MaybeSymNameAndArgs0 = ok2(SymName, Args),
         (
-            root_module_name(DefaultModuleName)
+            DefaultModuleName = root_module_name
         ->
             MaybeSymNameAndArgs = MaybeSymNameAndArgs0
         ;
@@ -207,6 +235,40 @@
         MaybeSymNameAndArgs = MaybeSymNameAndArgs0
     ).
 
+try_parse_implicitly_qualified_sym_name_and_args(DefaultModuleName, Term,
+        SymName, Args) :-
+    try_parse_sym_name_and_args(Term, SymName0, Args),
+    (
+        DefaultModuleName = root_module_name
+    ->
+        SymName = SymName0
+    ;
+        SymName0 = qualified(ModuleName, _),
+        \+ match_sym_name(ModuleName, DefaultModuleName)
+    ->
+        fail
+    ;
+        UnqualName = unqualify_name(SymName0),
+        SymName = qualified(DefaultModuleName, UnqualName)
+    ).
+
+try_parse_implicitly_qualified_sym_name_and_no_args(DefaultModuleName, Term,
+        SymName) :-
+    try_parse_sym_name_and_no_args(Term, SymName0),
+    (
+        DefaultModuleName = root_module_name
+    ->
+        SymName = SymName0
+    ;
+        SymName0 = qualified(ModuleName, _),
+        \+ match_sym_name(ModuleName, DefaultModuleName)
+    ->
+        fail
+    ;
+        UnqualName = unqualify_name(SymName0),
+        SymName = qualified(DefaultModuleName, UnqualName)
+    ).
+
 %-----------------------------------------------------------------------------e
 
 parse_symbol_name(VarSet, Term, MaybeSymName) :-
@@ -254,13 +316,31 @@
         )
     ).
 
+try_parse_symbol_name(Term, SymName) :-
+    (
+        Term = term.functor(term.atom(FunctorName), [ModuleTerm, NameTerm],
+            _TermContext),
+        ( FunctorName = ":"
+        ; FunctorName = "."
+        )
+    ->
+        NameTerm = term.functor(term.atom(Name), [], _),
+        try_parse_symbol_name(ModuleTerm, Module),
+        SymName = qualified(Module, Name)
+    ;
+        Term = term.functor(term.atom(Name), [], _),
+        SymName = string_to_sym_name_sep(Name, "__")
+    ).
+
+%-----------------------------------------------------------------------------e
+
 parse_implicitly_qualified_symbol_name(DefaultModuleName, VarSet, Term,
         MaybeSymName) :-
     parse_symbol_name(VarSet, Term, MaybeSymName0),
     (
         MaybeSymName0 = ok1(SymName),
         (
-            root_module_name(DefaultModuleName)
+            DefaultModuleName = root_module_name
         ->
             MaybeSymName = MaybeSymName0
         ;
@@ -285,8 +365,7 @@
 %-----------------------------------------------------------------------------e
 
 parse_symbol_name_specifier(VarSet, Term, MaybeSymNameSpecifier) :-
-    root_module_name(DefaultModule),
-    parse_implicitly_qualified_symbol_name_specifier(DefaultModule, VarSet,
+    parse_implicitly_qualified_symbol_name_specifier(root_module_name, VarSet,
         Term, MaybeSymNameSpecifier).
 
 parse_implicitly_qualified_symbol_name_specifier(DefaultModule, VarSet, Term,
@@ -331,6 +410,6 @@
 
 %-----------------------------------------------------------------------------e
 
-root_module_name(unqualified("")).
+root_module_name = unqualified("").
 
 %-----------------------------------------------------------------------------e
Index: compiler/prog_io_type_defn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_type_defn.m,v
retrieving revision 1.1
diff -u -b -r1.1 prog_io_type_defn.m
--- compiler/prog_io_type_defn.m	3 Dec 2008 05:01:41 -0000	1.1
+++ compiler/prog_io_type_defn.m	7 Sep 2009 02:20:58 -0000
@@ -215,7 +215,7 @@
         ( parse_list_of_vars(VarsTerm, ExistQVars) ->
             list.map(term.coerce_var, ExistQVars, ExistQTVars),
             MaybeConstructor = parse_constructor_2(ModuleName, VarSet,
-                ExistQTVars, Term, SubTerm)
+                ExistQTVars, SubTerm)
         ;
             TermStr = describe_error_term(VarSet, Term),
             Pieces = [words("Error: syntax error in variable list at"),
@@ -227,14 +227,13 @@
     ;
         ExistQVars = [],
         MaybeConstructor = parse_constructor_2(ModuleName, VarSet, ExistQVars,
-            Term, Term)
+            Term)
     ).
 
-:- func parse_constructor_2(module_name, varset, list(tvar), term, term) =
+:- func parse_constructor_2(module_name, varset, list(tvar), term) =
     maybe1(constructor).
 
-parse_constructor_2(ModuleName, VarSet, ExistQVars, ContainingTerm, Term)
-        = MaybeConstructor :-
+parse_constructor_2(ModuleName, VarSet, ExistQVars, Term) = MaybeConstructor :-
     get_existential_constraints_from_term(ModuleName, VarSet, Term,
         BeforeConstraintsTerm, MaybeConstraints),
     (
@@ -254,7 +253,7 @@
             MainTerm = BeforeConstraintsTerm
         ),
         ContextPieces = [words("In constructor definition:")],
-        parse_implicitly_qualified_term(ModuleName, MainTerm, ContainingTerm,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, MainTerm,
             VarSet, ContextPieces, MaybeFunctorAndArgTerms),
         (
             MaybeFunctorAndArgTerms = error2(Specs),
@@ -298,7 +297,7 @@
         = MaybeConstructorArgs :-
     ( Term = term.functor(term.atom("::"), [NameTerm, TypeTerm], _) ->
         ContextPieces = [words("In field name:")],
-        parse_implicitly_qualified_term(ModuleName, NameTerm, Term,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, NameTerm,
             VarSet, ContextPieces, MaybeSymNameAndArgs),
         (
             MaybeSymNameAndArgs = error2(Specs),
@@ -1089,7 +1088,7 @@
     ;
         HeadTerm = term.functor(_, _, HeadContext),
         ContextPieces = [words("In type definition:")],
-        parse_implicitly_qualified_term(ModuleName, HeadTerm, HeadTerm,
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, HeadTerm,
             VarSet, ContextPieces, HeadResult),
         (
             HeadResult = error2(Specs),
Index: compiler/prog_io_typeclass.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_typeclass.m,v
retrieving revision 1.65
diff -u -b -r1.65 prog_io_typeclass.m
--- compiler/prog_io_typeclass.m	2 Sep 2009 00:30:22 -0000	1.65
+++ compiler/prog_io_typeclass.m	7 Sep 2009 02:51:29 -0000
@@ -309,7 +309,7 @@
         MaybeTypeClassInfo) :-
     ContextPieces = [words("In typeclass declaration:")],
     varset.coerce(TVarSet, VarSet),
-    parse_implicitly_qualified_term(ModuleName, NameTerm, NameTerm,
+    parse_implicitly_qualified_sym_name_and_args(ModuleName, NameTerm,
         VarSet, ContextPieces, MaybeClassName),
     (
         MaybeClassName = ok2(ClassName, TermVars0),
@@ -559,7 +559,7 @@
     ->
         Result = Result0
     ;
-        parse_sym_name_and_args(ConstraintTerm, ClassName, Args0)
+        try_parse_sym_name_and_args(ConstraintTerm, ClassName, Args0)
     ->
         % XXX ArgsResultContextPieces = [words("In typeclass constraint:")]
         ArgsResultContextPieces = [],
@@ -703,7 +703,7 @@
     % could well be for a typeclass defined in another module.
     NameContextPieces = [words("In instance declaration:")],
     varset.coerce(TVarSet, VarSet),
-    parse_qualified_term(NameTerm, NameTerm, VarSet, NameContextPieces,
+    parse_sym_name_and_args(NameTerm, VarSet, NameContextPieces,
         MaybeClassName),
     (
         MaybeClassName = ok2(ClassName, TermTypes),
@@ -827,10 +827,10 @@
                 [PredNameTerm, ArityTerm], _)
         ->
             (
-                parse_sym_name_and_args(PredNameTerm, PredName, []),
+                try_parse_sym_name_and_no_args(PredNameTerm, PredName),
                 ArityTerm = term.functor(term.integer(ArityInt), [], _),
-                parse_sym_name_and_args(InstanceMethodTerm, InstanceMethodName,
-                    [])
+                try_parse_sym_name_and_no_args(InstanceMethodTerm,
+                    InstanceMethodName)
             ->
                 InstanceMethod = instance_method(pf_predicate, PredName,
                     instance_proc_def_name(InstanceMethodName), ArityInt,
@@ -853,10 +853,10 @@
                 [FuncNameTerm, ArityTerm], _)
         ->
             (
-                parse_sym_name_and_args(FuncNameTerm, FuncName, []),
+                try_parse_sym_name_and_no_args(FuncNameTerm, FuncName),
                 ArityTerm = term.functor(term.integer(ArityInt), [], _),
-                parse_sym_name_and_args(InstanceMethodTerm, InstanceMethodName,
-                    [])
+                try_parse_sym_name_and_no_args(InstanceMethodTerm,
+                    InstanceMethodName)
             ->
                 InstanceMethod = instance_method(pf_function, FuncName,
                     instance_proc_def_name(InstanceMethodName), ArityInt,
Index: compiler/prog_io_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_io_util.m,v
retrieving revision 1.67
diff -u -b -r1.67 prog_io_util.m
--- compiler/prog_io_util.m	2 Sep 2009 00:30:22 -0000	1.67
+++ compiler/prog_io_util.m	7 Sep 2009 02:51:43 -0000
@@ -117,7 +117,7 @@
     pred_or_func::out, sym_name::out, list(term(_T))::out) is semidet.
 
 :- pred parse_pred_or_func_and_args_general(maybe(module_name)::in,
-    term(_T)::in, term(_T)::in, varset(_T)::in, list(format_component)::in,
+    term(_T)::in, varset(_T)::in, list(format_component)::in,
     maybe_pred_or_func(term(_T))::out) is det.
 
 :- pred maybe_parse_type(term::in, mer_type::out) is semidet.
@@ -258,12 +258,8 @@
 parse_name_and_arity(ModuleName, PredAndArityTerm, SymName, Arity) :-
     PredAndArityTerm = term.functor(term.atom("/"),
         [PredNameTerm, ArityTerm], _),
-    % The values of VarSet and ContextPieces do not matter here since
-    % we succeed only if they aren't needed.
-    VarSet = varset.init,
-    ContextPieces = [],
-    parse_implicitly_qualified_term(ModuleName, PredNameTerm, PredNameTerm,
-        VarSet, ContextPieces, ok2(SymName, [])),
+    try_parse_implicitly_qualified_sym_name_and_no_args(ModuleName,
+        PredNameTerm, SymName),
     ArityTerm = term.functor(term.integer(Arity), [], _).
 
 parse_name_and_arity(PredAndArityTerm, SymName, Arity) :-
@@ -285,16 +281,16 @@
         PredAndArgsTerm = term.functor(term.atom("="),
             [FuncAndArgsTerm, FuncResultTerm], _)
     ->
-        parse_sym_name_and_args(FuncAndArgsTerm, SymName, ArgTerms0),
+        try_parse_sym_name_and_args(FuncAndArgsTerm, SymName, ArgTerms0),
         PredOrFunc = pf_function,
         ArgTerms = ArgTerms0 ++ [FuncResultTerm]
     ;
-        parse_sym_name_and_args(PredAndArgsTerm, SymName, ArgTerms),
+        try_parse_sym_name_and_args(PredAndArgsTerm, SymName, ArgTerms),
         PredOrFunc = pf_predicate
     ).
 
 parse_pred_or_func_and_args_general(MaybeModuleName, PredAndArgsTerm,
-        ErrorTerm, VarSet, ContextPieces, PredAndArgsResult) :-
+        VarSet, ContextPieces, PredAndArgsResult) :-
     (
         PredAndArgsTerm = term.functor(term.atom("="),
             [FuncAndArgsTerm, FuncResultTerm], _)
@@ -308,12 +304,12 @@
     varset.coerce(VarSet, GenericVarSet),
     (
         MaybeModuleName = yes(ModuleName),
-        parse_implicitly_qualified_term(ModuleName, FunctorTerm,
-            ErrorTerm, GenericVarSet, ContextPieces, Result)
+        parse_implicitly_qualified_sym_name_and_args(ModuleName, FunctorTerm,
+            GenericVarSet, ContextPieces, Result)
     ;
         MaybeModuleName = no,
-        parse_qualified_term(FunctorTerm,
-            ErrorTerm, GenericVarSet, ContextPieces, Result)
+        parse_sym_name_and_args(FunctorTerm, GenericVarSet, ContextPieces,
+            Result)
     ),
     (
         Result = ok2(SymName, Args),
@@ -372,7 +368,7 @@
     ;
         % We don't support kind annotations yet, and we don't report
         % an error either. Perhaps we should?
-        parse_qualified_term(Term, Term, VarSet, ContextPieces, NameResult),
+        parse_sym_name_and_args(Term, VarSet, ContextPieces, NameResult),
         (
             NameResult = ok2(SymName, ArgTerms),
             parse_types(ArgTerms, VarSet, ContextPieces, ArgsResult),
@@ -612,7 +608,7 @@
     ;
         % If the sym_name_and_args fails, we should report the error
         % (we would need to call parse_qualified_term instead).
-        parse_sym_name_and_args(Term, Name, Args),
+        try_parse_sym_name_and_args(Term, Name, Args),
         convert_inst_list(AllowConstrainedInstVar, Args, ConvertedArgs),
         Mode = user_defined_mode(Name, ConvertedArgs)
     ).
@@ -721,7 +717,7 @@
             term.coerce_var(Var)), Inst)
     ;
         % Anything else must be a user-defined inst.
-        parse_sym_name_and_args(Term, QualifiedName, Args1),
+        try_parse_sym_name_and_args(Term, QualifiedName, Args1),
         (
             BuiltinModule = mercury_public_builtin_module,
             sym_name_get_module_name_default(QualifiedName, unqualified(""),
@@ -818,7 +814,7 @@
     InstTerm = term.functor(Functor, Args0, _),
     (
         Functor = term.atom(_),
-        parse_sym_name_and_args(InstTerm, SymName, Args1),
+        try_parse_sym_name_and_args(InstTerm, SymName, Args1),
         list.length(Args1, Arity),
         ConsId = cons(SymName, Arity, cons_id_dummy_type_ctor)
     ;
Index: compiler/purity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/purity.m,v
retrieving revision 1.133
diff -u -b -r1.133 purity.m
--- compiler/purity.m	3 Sep 2009 23:57:29 -0000	1.133
+++ compiler/purity.m	4 Sep 2009 18:22:38 -0000
@@ -381,7 +381,8 @@
     proc_info_set_varset(VarSet, ProcInfo2, ProcInfo3),
     (
         NeedToRequantify = need_to_requantify,
-        requantify_proc(ProcInfo3, ProcInfo)
+        requantify_proc_general(ordinary_nonlocals_maybe_lambda,
+            ProcInfo3, ProcInfo)
     ;
         NeedToRequantify = do_not_need_to_requantify,
         ProcInfo = ProcInfo3
@@ -497,7 +498,9 @@
         % The RTTI varmaps here are just a dummy value, because the real ones
         % are not introduced until polymorphism.
         rtti_varmaps_init(EmptyRttiVarmaps),
-        implicitly_quantify_clause_body(HeadVars, _Warnings, Goal1, Goal,
+        implicitly_quantify_clause_body_general(
+            ordinary_nonlocals_maybe_lambda,
+            HeadVars, _Warnings, Goal1, Goal,
             VarSet1, VarSet, VarTypes1, VarTypes, EmptyRttiVarmaps, _),
         !Info ^ pi_vartypes := VarTypes,
         !Info ^ pi_varset := VarSet
Index: compiler/quantification.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/quantification.m,v
retrieving revision 1.134
diff -u -b -r1.134 quantification.m
--- compiler/quantification.m	2 Sep 2009 00:30:22 -0000	1.134
+++ compiler/quantification.m	7 Sep 2009 22:37:47 -0000
@@ -66,39 +66,23 @@
     %   Foo0 = foo(A, B, C, D, E, F, G, H).
     %
 :- type nonlocals_to_recompute
-    --->    ordinary_nonlocals
-    ;       code_gen_nonlocals.
+    --->    ordinary_nonlocals_maybe_lambda
+    ;       ordinary_nonlocals_no_lambda
+    ;       code_gen_nonlocals_no_lambda.
 
 :- pred implicitly_quantify_clause_body_general(nonlocals_to_recompute::in,
     list(prog_var)::in, list(quant_warning)::out,
     hlds_goal::in, hlds_goal::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out, rtti_varmaps::in, rtti_varmaps::out) is det.
 
-    % As above, with `ordinary_nonlocals' passed as the first argument.
-    %
-:- pred implicitly_quantify_clause_body(list(prog_var)::in,
-    list(quant_warning)::out, hlds_goal::in, hlds_goal::out,
-    prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
-    rtti_varmaps::in, rtti_varmaps::out) is det.
-
 :- pred implicitly_quantify_goal_general(nonlocals_to_recompute::in,
     set(prog_var)::in, list(quant_warning)::out,
     hlds_goal::in, hlds_goal::out, prog_varset::in, prog_varset::out,
     vartypes::in, vartypes::out, rtti_varmaps::in, rtti_varmaps::out) is det.
 
-    % As above, with `ordinary_nonlocals' passed as the first argument.
-    %
-:- pred implicitly_quantify_goal(set(prog_var)::in, list(quant_warning)::out,
-    hlds_goal::in, hlds_goal::out, prog_varset::in, prog_varset::out,
-    vartypes::in, vartypes::out, rtti_varmaps::in, rtti_varmaps::out) is det.
-
 :- pred requantify_proc_general(nonlocals_to_recompute::in,
     proc_info::in, proc_info::out) is det.
 
-    % As above, with `ordinary_nonlocals' passed as the first argument.
-    %
-:- pred requantify_proc(proc_info::in, proc_info::out) is det.
-
     % We return a list of warnings back to make_hlds.m.
     % Currently the only thing we warn about is variables with
     % overlapping scopes.
@@ -110,7 +94,7 @@
     %
     % Vars is the set of variables that occur free (unquantified) in Goal
     % excluding unset fields of reconstructions if
-    % NonLocalsToRecompute is `code_gen_nonlocals'.
+    % NonLocalsToRecompute is `code_gen_nonlocals_no_lambda'.
     %
 :- func free_goal_vars(hlds_goal) = set(prog_var).
 
@@ -195,25 +179,18 @@
     % OutsideVars will be [X] and QuantifiedVars will be [],
     % since the quantification can't be pushed inside the negation.
 
-:- inst ordinary_nonlocals ---> ordinary_nonlocals.
-:- inst code_gen_nonlocals ---> code_gen_nonlocals.
+:- inst ordinary_nonlocals_maybe_lambda ---> ordinary_nonlocals_maybe_lambda.
+:- inst ordinary_nonlocals_no_lambda ---> ordinary_nonlocals_no_lambda.
+:- inst code_gen_nonlocals_no_lambda ---> code_gen_nonlocals_no_lambda.
 
 %-----------------------------------------------------------------------------%
 
-implicitly_quantify_clause_body(HeadVars,
-        Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
-    implicitly_quantify_clause_body_general(ordinary_nonlocals, HeadVars,
-        Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps).
-
 implicitly_quantify_clause_body_general(NonLocalsToRecompute, HeadVars,
         Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
     list_to_set(HeadVars, OutsideVars),
     implicitly_quantify_goal_general(NonLocalsToRecompute, OutsideVars,
         Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps).
 
-requantify_proc(ProcInfo0, ProcInfo) :-
-    requantify_proc_general(ordinary_nonlocals, ProcInfo0, ProcInfo).
-
 requantify_proc_general(NonLocalsToRecompute, !ProcInfo) :-
     proc_info_get_headvars(!.ProcInfo, HeadVars),
     proc_info_get_varset(!.ProcInfo, Varset0),
@@ -228,24 +205,28 @@
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_rtti_varmaps(RttiVarmaps, !ProcInfo).
 
-implicitly_quantify_goal(OutsideVars, Warnings, !Goal, !Varset, !VarTypes,
-        !RttiVarMaps) :-
-    implicitly_quantify_goal_general(ordinary_nonlocals, OutsideVars, Warnings,
-        !Goal, !Varset, !VarTypes, !RttiVarMaps).
-
 implicitly_quantify_goal_general(NonLocalsToRecompute, OutsideVars, Warnings,
         !Goal, !Varset, !VarTypes, !RttiVarMaps) :-
-    implicitly_quantify_goal_2(ordinary_nonlocals, OutsideVars, Warnings,
-        !Goal, !Varset, !VarTypes, !RttiVarMaps),
     (
-        NonLocalsToRecompute = code_gen_nonlocals,
+        NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+        implicitly_quantify_goal_2(ordinary_nonlocals_maybe_lambda,
+            OutsideVars, Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps)
+    ;
+        ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+        ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+        ),
+        implicitly_quantify_goal_2(ordinary_nonlocals_no_lambda,
+            OutsideVars, Warnings, !Goal, !Varset, !VarTypes, !RttiVarMaps)
+    ),
+    (
+        NonLocalsToRecompute = code_gen_nonlocals_no_lambda,
 
         % If the goal does not contain a reconstruction, the code-gen nonlocals
         % and the ordinary nonlocals are the same.
         goal_contains_reconstruction(!.Goal)
     ->
-        implicitly_quantify_goal_2(code_gen_nonlocals, OutsideVars, _,
-            !Goal, !Varset, !VarTypes, !RttiVarMaps)
+        implicitly_quantify_goal_2(code_gen_nonlocals_no_lambda, OutsideVars,
+            _, !Goal, !Varset, !VarTypes, !RttiVarMaps)
     ;
         true
     ).
@@ -254,9 +235,11 @@
     set(prog_var), list(quant_warning),
     hlds_goal, hlds_goal, prog_varset, prog_varset,
     vartypes, vartypes, rtti_varmaps, rtti_varmaps) is det.
-:- mode implicitly_quantify_goal_2(in(ordinary_nonlocals),
+:- mode implicitly_quantify_goal_2(in(ordinary_nonlocals_maybe_lambda),
+    in, out, in, out, in, out, in, out, in, out) is det.
+:- mode implicitly_quantify_goal_2(in(ordinary_nonlocals_no_lambda),
     in, out, in, out, in, out, in, out, in, out) is det.
-:- mode implicitly_quantify_goal_2(in(code_gen_nonlocals),
+:- mode implicitly_quantify_goal_2(in(code_gen_nonlocals_no_lambda),
     in, out, in, out, in, out, in, out, in, out) is det.
 
 implicitly_quantify_goal_2(NonLocalsToRecompute, OutsideVars0, Warnings,
@@ -275,9 +258,11 @@
 :- pred implicitly_quantify_goal_quant_info(hlds_goal, hlds_goal,
     nonlocals_to_recompute, quant_info, quant_info).
 :- mode implicitly_quantify_goal_quant_info(in, out,
-    in(ordinary_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_maybe_lambda), in, out) is det.
 :- mode implicitly_quantify_goal_quant_info(in, out,
-    in(code_gen_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_no_lambda), in, out) is det.
+:- mode implicitly_quantify_goal_quant_info(in, out,
+    in(code_gen_nonlocals_no_lambda), in, out) is det.
 
 implicitly_quantify_goal_quant_info(Goal0, Goal, NonLocalsToRecompute,
         !Info) :-
@@ -336,9 +321,11 @@
     hlds_goal_info, nonlocals_to_recompute, set_of_var,
     quant_info, quant_info).
 :- mode implicitly_quantify_goal_quant_info_2(in, out, in,
-    in(ordinary_nonlocals), out, in, out) is det.
+    in(ordinary_nonlocals_maybe_lambda), out, in, out) is det.
+:- mode implicitly_quantify_goal_quant_info_2(in, out, in,
+    in(ordinary_nonlocals_no_lambda), out, in, out) is det.
 :- mode implicitly_quantify_goal_quant_info_2(in, out, in,
-    in(code_gen_nonlocals), out, in, out) is det.
+    in(code_gen_nonlocals_no_lambda), out, in, out) is det.
 
 implicitly_quantify_goal_quant_info_2(GoalExpr0, GoalExpr, GoalInfo0,
         NonLocalsToRecompute, PossiblyNonLocalGoalVars0, !Info) :-
@@ -349,10 +336,19 @@
             PossiblyNonLocalGoalVars0, !Info)
     ;
         GoalExpr0 = conj(ConjType, Goals0),
-        implicitly_quantify_conj(Goals0, Goals, NonLocalsToRecompute, !Info),
-        GoalExpr = conj(ConjType, Goals),
-        goal_expr_vars_bitset(NonLocalsToRecompute, GoalExpr0,
-            PossiblyNonLocalGoalVars0)
+        (
+            NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+            implicitly_quantify_conj_maybe_lambda(Goals0, Goals,
+                NonLocalsToRecompute, PossiblyNonLocalGoalVars0, !Info),
+            GoalExpr = conj(ConjType, Goals)
+        ;
+            ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+            ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+            ),
+            implicitly_quantify_conj_no_lambda(Goals0, Goals,
+                NonLocalsToRecompute, PossiblyNonLocalGoalVars0, !Info),
+            GoalExpr = conj(ConjType, Goals)
+        )
     ;
         GoalExpr0 = disj(Goals0),
         NonLocalVarSets0 = [],
@@ -424,7 +420,17 @@
             rename_var_list(need_not_rename, RenameMap, Vars0, Vars)
         ),
         insert_list(QuantVars, Vars, QuantVars1),
-        goal_vars_both(NonLocalsToRecompute, Then1, VarsThen, LambdaVarsThen),
+        (
+            NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+            goal_vars_both_maybe_lambda(NonLocalsToRecompute, Then1,
+                VarsThen, LambdaVarsThen)
+        ;
+            ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+            ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+            ),
+            goal_vars_both_no_lambda(NonLocalsToRecompute, Then1, VarsThen),
+            LambdaVarsThen = init
+        ),
         union(OutsideVars, VarsThen, OutsideVars1),
         union(LambdaOutsideVars, LambdaVarsThen, LambdaOutsideVars1),
         set_quant_vars(QuantVars1, !Info),
@@ -624,10 +630,12 @@
 :- pred rename_or_else_inner_vars(nonlocals_to_recompute,
     atomic_interface_vars, list(hlds_goal), list(hlds_goal),
     list(atomic_interface_vars), quant_info, quant_info).
-:- mode rename_or_else_inner_vars(in(ordinary_nonlocals), in, in, out, out,
-                                  in, out) is det.
-:- mode rename_or_else_inner_vars(in(code_gen_nonlocals), in, in, out, out,
-                                  in, out) is det.
+:- mode rename_or_else_inner_vars(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, out, in, out) is det.
+:- mode rename_or_else_inner_vars(in(ordinary_nonlocals_no_lambda),
+    in, in, out, out, in, out) is det.
+:- mode rename_or_else_inner_vars(in(code_gen_nonlocals_no_lambda),
+    in, in, out, out, in, out) is det.
 
 rename_or_else_inner_vars(_, _, [], [], [], !Info).
 rename_or_else_inner_vars(NonLocalsToRecompute, Inner,
@@ -648,9 +656,11 @@
     hlds_goal_expr, hlds_goal_info, nonlocals_to_recompute, set_of_var,
     quant_info, quant_info).
 :- mode implicitly_quantify_goal_quant_info_scope(in, in, out, in,
-    in(ordinary_nonlocals), out, in, out) is det.
+    in(ordinary_nonlocals_maybe_lambda), out, in, out) is det.
 :- mode implicitly_quantify_goal_quant_info_scope(in, in, out, in,
-    in(code_gen_nonlocals), out, in, out) is det.
+    in(ordinary_nonlocals_no_lambda), out, in, out) is det.
+:- mode implicitly_quantify_goal_quant_info_scope(in, in, out, in,
+    in(code_gen_nonlocals_no_lambda), out, in, out) is det.
 
 implicitly_quantify_goal_quant_info_scope(Reason0, SubGoal0, GoalExpr,
         GoalInfo0, NonLocalsToRecompute, PossiblyNonLocalGoalVars0, !Info) :-
@@ -716,9 +726,11 @@
     list(prog_var), list(prog_var), hlds_goal_info, nonlocals_to_recompute,
     quant_info, quant_info).
 :- mode implicitly_quantify_goal_quant_info_scope_rename_vars(in, out,
-    in, out, in, out, in, in(ordinary_nonlocals), in, out) is det.
+    in, out, in, out, in, in(ordinary_nonlocals_maybe_lambda), in, out) is det.
+:- mode implicitly_quantify_goal_quant_info_scope_rename_vars(in, out,
+    in, out, in, out, in, in(ordinary_nonlocals_no_lambda), in, out) is det.
 :- mode implicitly_quantify_goal_quant_info_scope_rename_vars(in, out,
-    in, out, in, out, in, in(code_gen_nonlocals), in, out) is det.
+    in, out, in, out, in, in(code_gen_nonlocals_no_lambda), in, out) is det.
 
 implicitly_quantify_goal_quant_info_scope_rename_vars(Reason0, Reason,
         SubGoal0, SubGoal, Vars0, Vars, GoalInfo0, NonLocalsToRecompute,
@@ -769,9 +781,11 @@
     hlds_goal, hlds_goal, hlds_goal_expr, hlds_goal_info,
     nonlocals_to_recompute, quant_info, quant_info).
 :- mode implicitly_quantify_goal_quant_info_bi_implication(in, in, out, in,
-    in(ordinary_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_maybe_lambda), in, out) is det.
 :- mode implicitly_quantify_goal_quant_info_bi_implication(in, in, out, in,
-    in(code_gen_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_no_lambda), in, out) is det.
+:- mode implicitly_quantify_goal_quant_info_bi_implication(in, in, out, in,
+    in(code_gen_nonlocals_no_lambda), in, out) is det.
 
 implicitly_quantify_goal_quant_info_bi_implication(LHS0, RHS0, GoalExpr,
         OldGoalInfo, NonLocalsToRecompute, !Info) :-
@@ -792,7 +806,17 @@
 
     % Prepare for quantifying the LHS: add variables from the RHS to the
     % outside vars and the outside lambda vars sets.
-    goal_vars_both(NonLocalsToRecompute, RHS0, RHS_Vars, RHS_LambdaVars),
+    (
+        NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+        goal_vars_both_maybe_lambda(NonLocalsToRecompute, RHS0,
+            RHS_Vars, RHS_LambdaVars)
+    ;
+        ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+        ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+        ),
+        goal_vars_both_no_lambda(NonLocalsToRecompute, RHS0, RHS_Vars),
+        RHS_LambdaVars = init
+    ),
     union(OutsideVars1, RHS_Vars, LHS_OutsideVars),
     union(LambdaOutsideVars1, RHS_LambdaVars, LHS_LambdaOutsideVars),
 
@@ -848,14 +872,14 @@
         GoalInfo1, GI, !Info),
     NotLHS = hlds_goal(negation(LHS), LHS_GI),
     NotRHS = hlds_goal(negation(RHS), RHS_GI),
-    ForwardsImplication = hlds_goal(
+    ForwardsImplicationExpr =
         negation(hlds_goal(conj(plain_conj, [LHS, NotRHS]), GI)),
-        GI),
+    ForwardsImplication = hlds_goal(ForwardsImplicationExpr, GI),
 
     % Rename apart the local variables of the goals we've just duplicated.
-    ReverseImplication0 = hlds_goal(
+    ReverseImplicationExpr0 =
         negation(hlds_goal(conj(plain_conj, [RHS, NotLHS]), GI)),
-        GI),
+    ReverseImplication0 = hlds_goal(ReverseImplicationExpr0, GI),
     goal_vars_bitset(NonLocalsToRecompute, ReverseImplication0, GoalVars),
     difference(GoalVars, NonLocalVars, RenameVars),
     rename_apart(RenameVars, _, NonLocalsToRecompute,
@@ -880,9 +904,11 @@
     hlds_goal_info, unify_rhs, unify_rhs,
     unification, unification, nonlocals_to_recompute, quant_info, quant_info).
 :- mode implicitly_quantify_unify_rhs(in, in, in, out, in, out,
-    in(ordinary_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_maybe_lambda), in, out) is det.
 :- mode implicitly_quantify_unify_rhs(in, in, in, out, in, out,
-    in(code_gen_nonlocals), in, out) is det.
+    in(ordinary_nonlocals_no_lambda), in, out) is det.
+:- mode implicitly_quantify_unify_rhs(in, in, in, out, in, out,
+    in(code_gen_nonlocals_no_lambda), in, out) is det.
 
 implicitly_quantify_unify_rhs(ReuseArgs, GoalInfo0, !RHS, !Unification,
         NonLocalsToRecompute, !Info) :-
@@ -893,7 +919,7 @@
     ;
         !.RHS = rhs_functor(_, _, ArgVars),
         (
-            NonLocalsToRecompute = code_gen_nonlocals,
+            NonLocalsToRecompute = code_gen_nonlocals_no_lambda,
             ReuseArgs = yes(SetArgs)
         ->
             % The fields taken from the reused cell aren't counted
@@ -1011,34 +1037,47 @@
         )
     ).
 
-:- pred implicitly_quantify_conj(list(hlds_goal), list(hlds_goal),
-    nonlocals_to_recompute, quant_info, quant_info).
-:- mode implicitly_quantify_conj(in, out, in(ordinary_nonlocals),
-    in, out) is det.
-:- mode implicitly_quantify_conj(in, out, in(code_gen_nonlocals),
-    in, out) is det.
-
-implicitly_quantify_conj(!Goals, NonLocalsToRecompute, !Info) :-
-    get_vars(NonLocalsToRecompute, !.Goals, FollowingVarsList),
-    implicitly_quantify_conj_2(FollowingVarsList, !Goals, NonLocalsToRecompute,
-        !Info).
+:- pred implicitly_quantify_conj_maybe_lambda(list(hlds_goal), list(hlds_goal),
+    nonlocals_to_recompute, set_of_var, quant_info, quant_info).
+:- mode implicitly_quantify_conj_maybe_lambda(in, out,
+    in(ordinary_nonlocals_maybe_lambda), out, in, out) is det.
+
+implicitly_quantify_conj_maybe_lambda(!Goals, NonLocalsToRecompute,
+        PossiblyNonLocalGoalVars, !Info) :-
+    get_following_vars_maybe_lambda(NonLocalsToRecompute, !.Goals,
+        FollowingVarsList, PossiblyNonLocalGoalVars),
+    implicitly_quantify_conj_maybe_lambda_2(FollowingVarsList, !Goals,
+        NonLocalsToRecompute, !Info).
+
+:- pred implicitly_quantify_conj_no_lambda(list(hlds_goal), list(hlds_goal),
+    nonlocals_to_recompute, set_of_var, quant_info, quant_info).
+:- mode implicitly_quantify_conj_no_lambda(in, out,
+    in(ordinary_nonlocals_no_lambda), out, in, out) is det.
+:- mode implicitly_quantify_conj_no_lambda(in, out,
+    in(code_gen_nonlocals_no_lambda), out, in, out) is det.
+
+implicitly_quantify_conj_no_lambda(!Goals, NonLocalsToRecompute,
+        PossiblyNonLocalGoalVars, !Info) :-
+    get_following_vars_no_lambda(NonLocalsToRecompute, !.Goals,
+        FollowingVarsList, PossiblyNonLocalGoalVars),
+    implicitly_quantify_conj_no_lambda_2(FollowingVarsList, !Goals,
+        NonLocalsToRecompute, !Info).
 
-:- pred implicitly_quantify_conj_2(list(pair(set_of_var)),
+:- pred implicitly_quantify_conj_maybe_lambda_2(list(pair(set_of_var)),
     list(hlds_goal), list(hlds_goal),
     nonlocals_to_recompute, quant_info, quant_info).
-:- mode implicitly_quantify_conj_2(in, in, out, in(ordinary_nonlocals),
-    in, out) is det.
-:- mode implicitly_quantify_conj_2(in, in, out, in(code_gen_nonlocals),
-    in, out) is det.
+:- mode implicitly_quantify_conj_maybe_lambda_2(in, in, out,
+    in(ordinary_nonlocals_maybe_lambda), in, out) is det.
 
-implicitly_quantify_conj_2(_, [], [], _, !Info) :-
+implicitly_quantify_conj_maybe_lambda_2(_, [], [], _, !Info) :-
     NonLocalVars = init,
     set_nonlocals(NonLocalVars, !Info).
-implicitly_quantify_conj_2([], [_ | _], _, _, _, _) :-
-    unexpected(this_file, "implicitly_quantify_conj_2: length mismatch").
-implicitly_quantify_conj_2(
-        [FollowingVars - LambdaFollowingVars | FollowingVarsList],
+implicitly_quantify_conj_maybe_lambda_2([], [_ | _], _, _, _, _) :-
+    unexpected(this_file,
+        "implicitly_quantify_conj_maybe_lambda_2: length mismatch").
+implicitly_quantify_conj_maybe_lambda_2([FollowingVarPair | FollowingVarPairs],
         [Goal0 | Goals0], [Goal | Goals], NonLocalsToRecompute, !Info) :-
+    FollowingVarPair = FollowingVars - LambdaFollowingVars,
     get_outside(!.Info, OutsideVars),
     get_lambda_outside(!.Info, LambdaOutsideVars),
     union(OutsideVars, FollowingVars, OutsideVars1),
@@ -1051,7 +1090,7 @@
     union(OutsideVars, NonLocalVars1, OutsideVars2),
     set_outside(OutsideVars2, !Info),
     set_lambda_outside(LambdaOutsideVars, !Info),
-    implicitly_quantify_conj_2(FollowingVarsList, Goals0, Goals,
+    implicitly_quantify_conj_maybe_lambda_2(FollowingVarPairs, Goals0, Goals,
         NonLocalsToRecompute, !Info),
     get_nonlocals(!.Info, NonLocalVars2),
     union(NonLocalVars1, NonLocalVars2, NonLocalVarsConj),
@@ -1061,12 +1100,46 @@
     set_outside(OutsideVars, !Info),
     set_nonlocals(NonLocalVars, !Info).
 
+:- pred implicitly_quantify_conj_no_lambda_2(list(set_of_var),
+    list(hlds_goal), list(hlds_goal),
+    nonlocals_to_recompute, quant_info, quant_info).
+:- mode implicitly_quantify_conj_no_lambda_2(in, in, out,
+    in(ordinary_nonlocals_no_lambda), in, out) is det.
+:- mode implicitly_quantify_conj_no_lambda_2(in, in, out,
+    in(code_gen_nonlocals_no_lambda), in, out) is det.
+
+implicitly_quantify_conj_no_lambda_2(_, [], [], _, !Info) :-
+    NonLocalVars = init,
+    set_nonlocals(NonLocalVars, !Info).
+implicitly_quantify_conj_no_lambda_2([], [_ | _], _, _, _, _) :-
+    unexpected(this_file,
+        "implicitly_quantify_conj_no_lambda_2: length mismatch").
+implicitly_quantify_conj_no_lambda_2([FollowingVars | FollowingVarsList],
+        [Goal0 | Goals0], [Goal | Goals], NonLocalsToRecompute, !Info) :-
+    get_outside(!.Info, OutsideVars),
+    union(OutsideVars, FollowingVars, OutsideVars1),
+    set_outside(OutsideVars1, !Info),
+    implicitly_quantify_goal_quant_info(Goal0, Goal, NonLocalsToRecompute,
+        !Info),
+    get_nonlocals(!.Info, NonLocalVars1),
+    union(OutsideVars, NonLocalVars1, OutsideVars2),
+    set_outside(OutsideVars2, !Info),
+    implicitly_quantify_conj_no_lambda_2(FollowingVarsList, Goals0, Goals,
+        NonLocalsToRecompute, !Info),
+    get_nonlocals(!.Info, NonLocalVars2),
+    union(NonLocalVars1, NonLocalVars2, NonLocalVarsConj),
+    intersect(NonLocalVarsConj, OutsideVars, NonLocalVars),
+    set_outside(OutsideVars, !Info),
+    set_nonlocals(NonLocalVars, !Info).
+
 :- pred implicitly_quantify_disj(list(hlds_goal), list(hlds_goal),
     nonlocals_to_recompute, quant_info, quant_info,
     list(set_of_var), list(set_of_var)).
-:- mode implicitly_quantify_disj(in, out, in(ordinary_nonlocals),
+:- mode implicitly_quantify_disj(in, out, in(ordinary_nonlocals_maybe_lambda),
     in, out, in, out) is det.
-:- mode implicitly_quantify_disj(in, out, in(code_gen_nonlocals),
+:- mode implicitly_quantify_disj(in, out, in(ordinary_nonlocals_no_lambda),
+    in, out, in, out) is det.
+:- mode implicitly_quantify_disj(in, out, in(code_gen_nonlocals_no_lambda),
     in, out, in, out) is det.
 
 implicitly_quantify_disj([], [], _, !Info, !NonLocalVarSets).
@@ -1082,9 +1155,11 @@
 :- pred implicitly_quantify_cases(list(case), list(case),
     nonlocals_to_recompute, quant_info, quant_info,
     list(set_of_var), list(set_of_var)).
-:- mode implicitly_quantify_cases(in, out, in(ordinary_nonlocals),
+:- mode implicitly_quantify_cases(in, out, in(ordinary_nonlocals_maybe_lambda),
+    in, out, in, out) is det.
+:- mode implicitly_quantify_cases(in, out, in(ordinary_nonlocals_no_lambda),
     in, out, in, out) is det.
-:- mode implicitly_quantify_cases(in, out, in(code_gen_nonlocals),
+:- mode implicitly_quantify_cases(in, out, in(code_gen_nonlocals_no_lambda),
     in, out, in, out) is det.
 
 implicitly_quantify_cases([], [], _, !Info, !NonLocalVarSets).
@@ -1120,50 +1195,102 @@
     % contains following variables that occur not in lambda goals, and the
     % second contains following variables that occur in lambda goals.
     %
-:- pred get_vars(nonlocals_to_recompute, list(hlds_goal),
-    list(pair(set_of_var))).
-:- mode get_vars(in(ordinary_nonlocals), in, out) is det.
-:- mode get_vars(in(code_gen_nonlocals), in, out) is det.
-
-get_vars(_, [], []).
-get_vars(NonLocalsToRecompute, [_Goal | Goals],
-        [Set - LambdaSet | SetPairs]) :-
-    get_vars_2(NonLocalsToRecompute, Goals, Set, LambdaSet, SetPairs).
-
-:- pred get_vars_2(nonlocals_to_recompute, list(hlds_goal),
-    set_of_var, set_of_var, list(pair(set_of_var))).
-:- mode get_vars_2(in(ordinary_nonlocals), in, out, out, out) is det.
-:- mode get_vars_2(in(code_gen_nonlocals), in, out, out, out) is det.
+:- pred get_following_vars_maybe_lambda(nonlocals_to_recompute,
+    list(hlds_goal), list(pair(set_of_var)), set_of_var).
+:- mode get_following_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, out, out) is det.
+
+get_following_vars_maybe_lambda(_, [], [], init).
+get_following_vars_maybe_lambda(NonLocalsToRecompute, [Goal | Goals],
+        [Set - LambdaSet | SetPairs], PossiblyNonLocalGoalVars) :-
+    get_following_vars_maybe_lambda_2(NonLocalsToRecompute, Goals,
+        Set, LambdaSet, SetPairs),
+    union(Set, LambdaSet, GoalsBothSet),
+    goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal,
+        GoalSet, GoalLambdaSet),
+    union(GoalSet, GoalLambdaSet, GoalBothSet),
+    union(GoalBothSet, GoalsBothSet, PossiblyNonLocalGoalVars).
+
+:- pred get_following_vars_no_lambda(nonlocals_to_recompute, list(hlds_goal),
+    list(set_of_var), set_of_var).
+:- mode get_following_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, out, out) is det.
+:- mode get_following_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, out, out) is det.
+
+get_following_vars_no_lambda(_, [], [], init).
+get_following_vars_no_lambda(NonLocalsToRecompute, [Goal | Goals],
+        [Set | Sets], PossiblyNonLocalGoalVars) :-
+    get_following_vars_no_lambda_2(NonLocalsToRecompute, Goals, Set, Sets),
+    goal_vars_both_no_lambda(NonLocalsToRecompute, Goal, GoalSet),
+    union(GoalSet, Set, PossiblyNonLocalGoalVars).
+
+:- pred get_following_vars_maybe_lambda_2(nonlocals_to_recompute,
+    list(hlds_goal), set_of_var, set_of_var, list(pair(set_of_var))).
+:- mode get_following_vars_maybe_lambda_2(in(ordinary_nonlocals_maybe_lambda),
+    in, out, out, out) is det.
 
-get_vars_2(_, [], Set, LambdaSet, []) :-
+get_following_vars_maybe_lambda_2(_, [], Set, LambdaSet, []) :-
     Set = init,
     LambdaSet = init.
-get_vars_2(NonLocalsToRecompute, [Goal | Goals], Set, LambdaSet,
-        SetPairList) :-
-    get_vars_2(NonLocalsToRecompute, Goals, Set0, LambdaSet0, SetPairList0),
-    goal_vars_both(NonLocalsToRecompute, Goal, Set1, LambdaSet1),
+get_following_vars_maybe_lambda_2(NonLocalsToRecompute, [Goal | Goals],
+        Set, LambdaSet, SetPairList) :-
+    get_following_vars_maybe_lambda_2(NonLocalsToRecompute, Goals,
+        Set0, LambdaSet0, SetPairList0),
+    goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal, Set1, LambdaSet1),
     union(Set0, Set1, Set),
     union(LambdaSet0, LambdaSet1, LambdaSet),
     SetPairList = [Set0 - LambdaSet0 | SetPairList0].
 
-:- pred conj_vars(nonlocals_to_recompute, list(hlds_goal),
+:- pred get_following_vars_no_lambda_2(nonlocals_to_recompute, list(hlds_goal),
+    set_of_var, list(set_of_var)).
+:- mode get_following_vars_no_lambda_2(in(ordinary_nonlocals_no_lambda),
+    in, out, out) is det.
+:- mode get_following_vars_no_lambda_2(in(code_gen_nonlocals_no_lambda),
+    in, out, out) is det.
+
+get_following_vars_no_lambda_2(_, [], Set, []) :-
+    Set = init.
+get_following_vars_no_lambda_2(NonLocalsToRecompute, [Goal | Goals],
+        Set, SetList) :-
+    get_following_vars_no_lambda_2(NonLocalsToRecompute, Goals, Set0, SetList0),
+    goal_vars_both_no_lambda(NonLocalsToRecompute, Goal, Set1),
+    union(Set0, Set1, Set),
+    SetList = [Set0 | SetList0].
+
+:- pred conj_vars_maybe_lambda(nonlocals_to_recompute, list(hlds_goal),
     set_of_var, set_of_var, set_of_var, set_of_var).
-:- mode conj_vars(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode conj_vars(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode conj_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
+
+conj_vars_maybe_lambda(_, [], !Set, !LambdaSet).
+conj_vars_maybe_lambda(NonLocalsToRecompute, [Goal | Goals],
+        !Set, !LambdaSet) :-
+    Goal = hlds_goal(GoalExpr, _GoalInfo),
+    goal_expr_vars_maybe_lambda_2(NonLocalsToRecompute, GoalExpr,
+        !Set, !LambdaSet),
+    conj_vars_maybe_lambda(NonLocalsToRecompute, Goals, !Set, !LambdaSet).
 
-conj_vars(_, [], !Set, !LambdaSet).
-conj_vars(NonLocalsToRecompute, [Goal | Goals], !Set, !LambdaSet) :-
+:- pred conj_vars_no_lambda(nonlocals_to_recompute, list(hlds_goal),
+    set_of_var, set_of_var).
+:- mode conj_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode conj_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+conj_vars_no_lambda(_, [], !Set).
+conj_vars_no_lambda(NonLocalsToRecompute, [Goal | Goals], !Set) :-
     Goal = hlds_goal(GoalExpr, _GoalInfo),
-    goal_expr_vars_2(NonLocalsToRecompute, GoalExpr, !Set, !LambdaSet),
-    conj_vars(NonLocalsToRecompute, Goals, !Set, !LambdaSet).
+    goal_expr_vars_no_lambda_2(NonLocalsToRecompute, GoalExpr, !Set),
+    conj_vars_no_lambda(NonLocalsToRecompute, Goals, !Set).
 
-:- pred disj_vars(nonlocals_to_recompute, list(hlds_goal),
+:- pred disj_vars_maybe_lambda(nonlocals_to_recompute, list(hlds_goal),
     set_of_var, set_of_var, set_of_var, set_of_var).
-:- mode disj_vars(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode disj_vars(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode disj_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
 
-disj_vars(NonLocalsToRecompute, Goals, !Set, !LambdaSet) :-
-    compute_disj_vars(NonLocalsToRecompute, Goals,
+disj_vars_maybe_lambda(NonLocalsToRecompute, Goals, !Set, !LambdaSet) :-
+    compute_disj_vars_maybe_lambda(NonLocalsToRecompute, Goals,
         [], GoalSets, [], GoalLambdaSets),
     (
         GoalSets = [],
@@ -1182,25 +1309,59 @@
     union(GoalsSet, !Set),
     union(GoalsLambdaSet, !LambdaSet).
 
-:- pred compute_disj_vars(nonlocals_to_recompute, list(hlds_goal),
+:- pred disj_vars_no_lambda(nonlocals_to_recompute, list(hlds_goal),
+    set_of_var, set_of_var).
+:- mode disj_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode disj_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+disj_vars_no_lambda(NonLocalsToRecompute, Goals, !Set) :-
+    compute_disj_vars_no_lambda(NonLocalsToRecompute, Goals, [], GoalSets),
+    (
+        GoalSets = [],
+        GoalsSet = init
+    ;
+        GoalSets = [_ | _],
+        union_list(GoalSets, GoalsSet)
+    ),
+    union(GoalsSet, !Set).
+
+:- pred compute_disj_vars_maybe_lambda(nonlocals_to_recompute, list(hlds_goal),
     list(set_of_var), list(set_of_var), list(set_of_var), list(set_of_var)).
-:- mode compute_disj_vars(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode compute_disj_vars(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode compute_disj_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
 
-compute_disj_vars(_, [], !Sets, !LambdaSets).
-compute_disj_vars(NonLocalsToRecompute, [Goal | Goals], !Sets, !LambdaSets) :-
-    goal_vars_both(NonLocalsToRecompute, Goal, GoalSet, GoalLambdaSet),
+compute_disj_vars_maybe_lambda(_, [], !Sets, !LambdaSets).
+compute_disj_vars_maybe_lambda(NonLocalsToRecompute, [Goal | Goals],
+        !Sets, !LambdaSets) :-
+    goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal,
+        GoalSet, GoalLambdaSet),
     !:Sets = [GoalSet | !.Sets],
     !:LambdaSets = [GoalLambdaSet | !.LambdaSets],
-    compute_disj_vars(NonLocalsToRecompute, Goals, !Sets, !LambdaSets).
+    compute_disj_vars_maybe_lambda(NonLocalsToRecompute, Goals,
+        !Sets, !LambdaSets).
+
+:- pred compute_disj_vars_no_lambda(nonlocals_to_recompute, list(hlds_goal),
+    list(set_of_var), list(set_of_var)).
+:- mode compute_disj_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode compute_disj_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+compute_disj_vars_no_lambda(_, [], !Sets).
+compute_disj_vars_no_lambda(NonLocalsToRecompute, [Goal | Goals], !Sets) :-
+    goal_vars_both_no_lambda(NonLocalsToRecompute, Goal, GoalSet),
+    !:Sets = [GoalSet | !.Sets],
+    compute_disj_vars_no_lambda(NonLocalsToRecompute, Goals, !Sets).
 
-:- pred case_vars(nonlocals_to_recompute, list(case),
+:- pred case_vars_maybe_lambda(nonlocals_to_recompute, list(case),
     set_of_var, set_of_var, set_of_var, set_of_var).
-:- mode case_vars(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode case_vars(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode case_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
 
-case_vars(NonLocalsToRecompute, Cases, !Set, !LambdaSet) :-
-    compute_case_vars(NonLocalsToRecompute, Cases,
+case_vars_maybe_lambda(NonLocalsToRecompute, Cases, !Set, !LambdaSet) :-
+    compute_case_vars_maybe_lambda(NonLocalsToRecompute, Cases,
         [], CaseSets, [], CaseLambdaSets),
     (
         CaseSets = [],
@@ -1219,18 +1380,53 @@
     union(CasesSet, !Set),
     union(CasesLambdaSet, !LambdaSet).
 
-:- pred compute_case_vars(nonlocals_to_recompute, list(case),
+:- pred case_vars_no_lambda(nonlocals_to_recompute, list(case),
+    set_of_var, set_of_var).
+:- mode case_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode case_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+case_vars_no_lambda(NonLocalsToRecompute, Cases, !Set) :-
+    compute_case_vars_no_lambda(NonLocalsToRecompute, Cases, [], CaseSets),
+    (
+        CaseSets = [],
+        unexpected(this_file, "case_vars: no cases (1)")
+    ;
+        CaseSets = [_ | _],
+        union_list(CaseSets, CasesSet)
+    ),
+    union(CasesSet, !Set).
+
+:- pred compute_case_vars_maybe_lambda(nonlocals_to_recompute, list(case),
     list(set_of_var), list(set_of_var), list(set_of_var), list(set_of_var)).
-:- mode compute_case_vars(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode compute_case_vars(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode compute_case_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
 
-compute_case_vars(_, [], !Sets, !LambdaSets).
-compute_case_vars(NonLocalsToRecompute, [Case | Cases], !Sets, !LambdaSets) :-
+compute_case_vars_maybe_lambda(_, [], !Sets, !LambdaSets).
+compute_case_vars_maybe_lambda(NonLocalsToRecompute, [Case | Cases],
+        !Sets, !LambdaSets) :-
     Case = case(_MainConsId, _OtherConsIds, Goal),
-    goal_vars_both(NonLocalsToRecompute, Goal, GoalSet, GoalLambdaSet),
+    goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal,
+        GoalSet, GoalLambdaSet),
     !:Sets = [GoalSet | !.Sets],
     !:LambdaSets = [GoalLambdaSet | !.LambdaSets],
-    compute_case_vars(NonLocalsToRecompute, Cases, !Sets, !LambdaSets).
+    compute_case_vars_maybe_lambda(NonLocalsToRecompute, Cases,
+        !Sets, !LambdaSets).
+
+:- pred compute_case_vars_no_lambda(nonlocals_to_recompute, list(case),
+    list(set_of_var), list(set_of_var)).
+:- mode compute_case_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode compute_case_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+compute_case_vars_no_lambda(_, [], !Sets).
+compute_case_vars_no_lambda(NonLocalsToRecompute, [Case | Cases], !Sets) :-
+    Case = case(_MainConsId, _OtherConsIds, Goal),
+    goal_vars_both_no_lambda(NonLocalsToRecompute, Goal, GoalSet),
+    !:Sets = [GoalSet | !.Sets],
+    compute_case_vars_no_lambda(NonLocalsToRecompute, Cases, !Sets).
 
 :- pred union_list(list(set_of_var)::in, set_of_var::out) is det.
 
@@ -1260,40 +1456,121 @@
     union_list_pass(Sets, !MergedSets).
 
 free_goal_vars(Goal) =
-    free_goal_vars_nl(ordinary_nonlocals, Goal).
+    free_goal_vars_nl_maybe_lambda(ordinary_nonlocals_maybe_lambda, Goal).
 
     % free_goal_vars_nl(NonLocalsToRecompute, Goal) = Vars:
     %
     % Vars is the set of variables that occur free (unquantified) in Goal,
     % excluding unset fields of reconstructions if NonLocalsToRecompute
-    % is `code_gen_nonlocals'.
+    % is `code_gen_nonlocals_no_lambda'.
     %
-:- func free_goal_vars_nl(nonlocals_to_recompute, hlds_goal) = set(prog_var).
-:- mode free_goal_vars_nl(in(ordinary_nonlocals), in) = out is det.
-:- mode free_goal_vars_nl(in(code_gen_nonlocals), in) = out is det.
-
-free_goal_vars_nl(NonLocalsToRecompute, Goal) = bitset_to_set(BothSet) :-
-    goal_vars_bitset(NonLocalsToRecompute, Goal, BothSet).
-
-:- pred goal_vars_bitset(nonlocals_to_recompute, hlds_goal, set_of_var).
-:- mode goal_vars_bitset(in(ordinary_nonlocals), in, out) is det.
-:- mode goal_vars_bitset(in(code_gen_nonlocals), in, out) is det.
+:- func free_goal_vars_nl_maybe_lambda(nonlocals_to_recompute, hlds_goal)
+    = set(prog_var).
+:- mode free_goal_vars_nl_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in) = out is det.
+
+free_goal_vars_nl_maybe_lambda(NonLocalsToRecompute, Goal) =
+        bitset_to_set(BothSet) :-
+    goal_vars_bitset_maybe_lambda(NonLocalsToRecompute, Goal, BothSet).
+
+:- func free_goal_vars_nl_no_lambda(nonlocals_to_recompute, hlds_goal)
+    = set(prog_var).
+:- mode free_goal_vars_nl_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in) = out is det.
+:- mode free_goal_vars_nl_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in) = out is det.
+
+free_goal_vars_nl_no_lambda(NonLocalsToRecompute, Goal) =
+        bitset_to_set(BothSet) :-
+    goal_vars_bitset_no_lambda(NonLocalsToRecompute, Goal, BothSet).
+
+:- pred goal_vars_bitset(nonlocals_to_recompute,
+    hlds_goal, set_of_var).
+:- mode goal_vars_bitset(in(ordinary_nonlocals_maybe_lambda),
+    in, out) is det.
+:- mode goal_vars_bitset(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_vars_bitset(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
 
 goal_vars_bitset(NonLocalsToRecompute, Goal, BothSet) :-
+    (
+        NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+        goal_vars_bitset_maybe_lambda(NonLocalsToRecompute, Goal, BothSet)
+    ;
+        ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+        ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+        ),
+        goal_vars_bitset_no_lambda(NonLocalsToRecompute, Goal, BothSet)
+    ).
+
+:- pred goal_vars_bitset_maybe_lambda(nonlocals_to_recompute,
+    hlds_goal, set_of_var).
+:- mode goal_vars_bitset_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, out) is det.
+
+goal_vars_bitset_maybe_lambda(NonLocalsToRecompute, Goal, BothSet) :-
     Goal = hlds_goal(GoalExpr, _),
-    goal_expr_vars_both(NonLocalsToRecompute, GoalExpr, Set, LambdaSet),
+    goal_expr_vars_both_maybe_lambda(NonLocalsToRecompute, GoalExpr,
+        Set, LambdaSet),
     BothSet = union(Set, LambdaSet).
 
+:- pred goal_vars_bitset_no_lambda(nonlocals_to_recompute,
+    hlds_goal, set_of_var).
+:- mode goal_vars_bitset_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_vars_bitset_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
+
+goal_vars_bitset_no_lambda(NonLocalsToRecompute, Goal, BothSet) :-
+    Goal = hlds_goal(GoalExpr, _),
+    goal_expr_vars_both_no_lambda(NonLocalsToRecompute, GoalExpr, Set),
+    BothSet = Set.
+
 :- pred goal_expr_vars_bitset(nonlocals_to_recompute,
     hlds_goal_expr, set_of_var).
-:- mode goal_expr_vars_bitset(in(ordinary_nonlocals), in, out) is det.
-:- mode goal_expr_vars_bitset(in(code_gen_nonlocals), in, out) is det.
+:- mode goal_expr_vars_bitset(in(ordinary_nonlocals_maybe_lambda),
+    in, out) is det.
+:- mode goal_expr_vars_bitset(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_expr_vars_bitset(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
 
 goal_expr_vars_bitset(NonLocalsToRecompute, GoalExpr, BothSet) :-
-    goal_expr_vars_both(NonLocalsToRecompute, GoalExpr, Set, LambdaSet),
+    (
+        NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda,
+        goal_expr_vars_bitset_maybe_lambda(NonLocalsToRecompute, GoalExpr,
+            BothSet)
+    ;
+        ( NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+        ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
+        ),
+        goal_expr_vars_bitset_no_lambda(NonLocalsToRecompute, GoalExpr,
+            BothSet)
+    ).
+
+:- pred goal_expr_vars_bitset_maybe_lambda(nonlocals_to_recompute,
+    hlds_goal_expr, set_of_var).
+:- mode goal_expr_vars_bitset_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, out) is det.
+
+goal_expr_vars_bitset_maybe_lambda(NonLocalsToRecompute, GoalExpr, BothSet) :-
+    goal_expr_vars_both_maybe_lambda(NonLocalsToRecompute, GoalExpr,
+        Set, LambdaSet),
     BothSet = union(Set, LambdaSet).
 
-    % goal_vars_both(NonLocalsToRecompute, Goal,
+:- pred goal_expr_vars_bitset_no_lambda(nonlocals_to_recompute,
+    hlds_goal_expr, set_of_var).
+:- mode goal_expr_vars_bitset_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_expr_vars_bitset_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
+
+goal_expr_vars_bitset_no_lambda(NonLocalsToRecompute, GoalExpr, BothSet) :-
+    goal_expr_vars_both_no_lambda(NonLocalsToRecompute, GoalExpr, Set),
+    BothSet = Set.
+
+    % goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal,
     %   NonLambdaSet, LambdaSet):
     %
     % Set is the set of variables that occur free (unquantified) in Goal,
@@ -1301,50 +1578,72 @@
     % of variables that occur free (unquantified) in lambda expressions
     % in Goal.
     %
-:- pred goal_vars_both(nonlocals_to_recompute, hlds_goal,
+:- pred goal_vars_both_maybe_lambda(nonlocals_to_recompute, hlds_goal,
     set_of_var, set_of_var).
-:- mode goal_vars_both(in(ordinary_nonlocals), in, out, out) is det.
-:- mode goal_vars_both(in(code_gen_nonlocals), in, out, out) is det.
+:- mode goal_vars_both_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, out, out) is det.
+
+goal_vars_both_maybe_lambda(NonLocalsToRecompute, Goal, Set, LambdaSet) :-
+    Goal = hlds_goal(GoalExpr, _),
+    goal_expr_vars_both_maybe_lambda(NonLocalsToRecompute, GoalExpr,
+        Set, LambdaSet).
 
-goal_vars_both(NonLocalsToRecompute, Goal, Set, LambdaSet) :-
+:- pred goal_vars_both_no_lambda(nonlocals_to_recompute, hlds_goal,
+    set_of_var).
+:- mode goal_vars_both_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_vars_both_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
+
+goal_vars_both_no_lambda(NonLocalsToRecompute, Goal, Set) :-
     Goal = hlds_goal(GoalExpr, _),
-    goal_expr_vars_both(NonLocalsToRecompute, GoalExpr, Set, LambdaSet).
+    goal_expr_vars_both_no_lambda(NonLocalsToRecompute, GoalExpr, Set).
 
-:- pred goal_expr_vars_both(nonlocals_to_recompute, hlds_goal_expr,
+:- pred goal_expr_vars_both_maybe_lambda(nonlocals_to_recompute, hlds_goal_expr,
     set_of_var, set_of_var).
-:- mode goal_expr_vars_both(in(ordinary_nonlocals), in, out, out) is det.
-:- mode goal_expr_vars_both(in(code_gen_nonlocals), in, out, out) is det.
+:- mode goal_expr_vars_both_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, out, out) is det.
 
-goal_expr_vars_both(NonLocalsToRecompute, GoalExpr, Set, LambdaSet) :-
+goal_expr_vars_both_maybe_lambda(NonLocalsToRecompute, GoalExpr,
+        Set, LambdaSet) :-
     Set0 = init,
     LambdaSet0 = init,
-    goal_expr_vars_2(NonLocalsToRecompute, GoalExpr, Set0, Set,
+    goal_expr_vars_maybe_lambda_2(NonLocalsToRecompute, GoalExpr, Set0, Set,
         LambdaSet0, LambdaSet).
 
-:- pred goal_expr_vars_2(nonlocals_to_recompute, hlds_goal_expr,
+:- pred goal_expr_vars_both_no_lambda(nonlocals_to_recompute, hlds_goal_expr,
+    set_of_var).
+:- mode goal_expr_vars_both_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, out) is det.
+:- mode goal_expr_vars_both_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, out) is det.
+
+goal_expr_vars_both_no_lambda(NonLocalsToRecompute, GoalExpr, Set) :-
+    Set0 = init,
+    goal_expr_vars_no_lambda_2(NonLocalsToRecompute, GoalExpr, Set0, Set).
+
+:- pred goal_expr_vars_maybe_lambda_2(nonlocals_to_recompute, hlds_goal_expr,
     set_of_var, set_of_var, set_of_var, set_of_var).
-:- mode goal_expr_vars_2(in(ordinary_nonlocals), in, in, out, in, out) is det.
-:- mode goal_expr_vars_2(in(code_gen_nonlocals), in, in, out, in, out) is det.
+:- mode goal_expr_vars_maybe_lambda_2(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
 
-goal_expr_vars_2(NonLocalsToRecompute, GoalExpr, !Set, !LambdaSet) :-
+goal_expr_vars_maybe_lambda_2(NonLocalsToRecompute, GoalExpr,
+        !Set, !LambdaSet) :-
     (
         GoalExpr = unify(LHS, RHS, _, Unification, _),
         insert(!.Set, LHS, !:Set),
         (
             Unification = construct(_, _, _, _, How, _, SubInfo),
             (
-                How = reuse_cell(cell_to_reuse(ReuseVar, _, SetArgs)),
-                MaybeSetArgs = yes(SetArgs),
+                How = reuse_cell(cell_to_reuse(ReuseVar, _, _)),
                 insert(!.Set, ReuseVar, !:Set)
             ;
                 How = construct_in_region(RegionVar),
-                MaybeSetArgs = no,
                 insert(!.Set, RegionVar, !:Set)
             ;
                 ( How = construct_statically
                 ; How = construct_dynamically
-                ),
-                MaybeSetArgs = no
+                )
             ),
             (
                 SubInfo = construct_sub_info(_, MaybeSize),
@@ -1356,16 +1655,14 @@
             )
         ;
             Unification = complicated_unify(_, _, TypeInfoVars),
-            MaybeSetArgs = no,
             insert_list(!.Set, TypeInfoVars, !:Set)
         ;
             ( Unification = deconstruct(_, _, _, _, _, _)
             ; Unification = assign(_, _)
             ; Unification = simple_test(_, _)
+            )
             ),
-            MaybeSetArgs = no
-        ),
-        unify_rhs_vars(NonLocalsToRecompute, RHS, MaybeSetArgs,
+        unify_rhs_vars_maybe_lambda(NonLocalsToRecompute, RHS,
             !Set, !LambdaSet)
     ;
         GoalExpr = plain_call(_, _, ArgVars, _, _, _),
@@ -1388,22 +1685,25 @@
         ;
             ConjType = parallel_conj
         ),
-        conj_vars(NonLocalsToRecompute, Goals, !Set, !LambdaSet)
+        conj_vars_maybe_lambda(NonLocalsToRecompute, Goals, !Set, !LambdaSet)
     ;
         GoalExpr = disj(Goals),
-        disj_vars(NonLocalsToRecompute, Goals, !Set, !LambdaSet)
+        disj_vars_maybe_lambda(NonLocalsToRecompute, Goals, !Set, !LambdaSet)
     ;
         GoalExpr = switch(Var, _Det, Cases),
         insert(!.Set, Var, !:Set),
-        case_vars(NonLocalsToRecompute, Cases, !Set, !LambdaSet)
+        case_vars_maybe_lambda(NonLocalsToRecompute, Cases, !Set, !LambdaSet)
     ;
         GoalExpr = if_then_else(Vars, Cond, Then, Else),
         % This code does the following:
         %     !:Set = !.Set + ( (vars(Cond) + vars(Then)) \ Vars ) + vars(Else)
         % where `+' is set union and `\' is relative complement.
-        goal_vars_both(NonLocalsToRecompute, Cond, CondSet, CondLambdaSet),
-        goal_vars_both(NonLocalsToRecompute, Then, ThenSet, ThenLambdaSet),
-        goal_vars_both(NonLocalsToRecompute, Else, ElseSet, ElseLambdaSet),
+        goal_vars_both_maybe_lambda(NonLocalsToRecompute, Cond,
+            CondSet, CondLambdaSet),
+        goal_vars_both_maybe_lambda(NonLocalsToRecompute, Then,
+            ThenSet, ThenLambdaSet),
+        goal_vars_both_maybe_lambda(NonLocalsToRecompute, Else,
+            ElseSet, ElseLambdaSet),
         union(CondSet, ThenSet, CondThenSet),
         union(CondLambdaSet, ThenLambdaSet, CondThenLambdaSet),
         delete_list(CondThenSet, Vars, SomeCondThenSet),
@@ -1415,7 +1715,8 @@
     ;
         GoalExpr = negation(SubGoal),
         SubGoal = hlds_goal(SubGoalExpr, _SubGoalInfo),
-        goal_expr_vars_2(NonLocalsToRecompute, SubGoalExpr, !Set, !LambdaSet)
+        goal_expr_vars_maybe_lambda_2(NonLocalsToRecompute, SubGoalExpr,
+            !Set, !LambdaSet)
     ;
         GoalExpr = scope(Reason, SubGoal),
         Set0 = !.Set,
@@ -1426,24 +1727,38 @@
             ; Reason = barrier(_)
             ; Reason = trace_goal(_, _, _, _, _)
             ),
-            goal_vars_both(NonLocalsToRecompute, SubGoal, !:Set, !:LambdaSet)
+            goal_vars_both_maybe_lambda(NonLocalsToRecompute, SubGoal,
+                !:Set, !:LambdaSet)
         ;
             Reason = exist_quant(Vars),
-            goal_vars_both(NonLocalsToRecompute, SubGoal, !:Set, !:LambdaSet),
+            goal_vars_both_maybe_lambda(NonLocalsToRecompute, SubGoal,
+                !:Set, !:LambdaSet),
             delete_list(!.Set, Vars, !:Set),
             delete_list(!.LambdaSet, Vars, !:LambdaSet)
         ;
             Reason = promise_solutions(Vars, _Kind),
-            goal_vars_both(NonLocalsToRecompute, SubGoal, !:Set, !:LambdaSet),
+            goal_vars_both_maybe_lambda(NonLocalsToRecompute, SubGoal,
+                !:Set, !:LambdaSet),
             insert_list(!.Set, Vars, !:Set)
         ;
-            Reason = from_ground_term(_TermVar, _),
-            % We need to traverse SubGoal because termination analysis needs
-            % to know all the variables in the goal (even the local ones)
-            % so it can allocate size_var analogues to them.
-            goal_vars_both(NonLocalsToRecompute, SubGoal, !:Set, !:LambdaSet)
-            % _TermVar should have been put into the relevant sets when we
+            Reason = from_ground_term(TermVar, Kind),
+            (
+                Kind = from_ground_term_construct,
+                !:Set = init,
+                insert(!.Set, TermVar, !:Set),
+                !:LambdaSet = init
+            ;
+                ( Kind = from_ground_term_deconstruct
+                ; Kind = from_ground_term_other
+                ),
+                % Unfortunately, while there will never by any lambda goals
+                % inside such SubGoal when the scope is built, there may be
+                % lambda goals inside SubGoal after typechecking.
+                goal_vars_both_maybe_lambda(NonLocalsToRecompute, SubGoal,
+                    !:Set, !:LambdaSet)
+                % TermVar should have been put into the relevant sets when we
             % processed SubGoal, since it should appear in SubGoal.
+            )
         ),
         union(Set0, !Set),
         union(LambdaSet0, !LambdaSet)
@@ -1456,35 +1771,196 @@
             Outer = atomic_interface_vars(OuterDI, OuterUO),
             Inner = atomic_interface_vars(InnerDI, InnerUO),
             insert_list(!.Set, [OuterDI, OuterUO, InnerDI, InnerUO], !:Set),
-            disj_vars(NonLocalsToRecompute, [MainGoal | OrElseGoals],
+            disj_vars_maybe_lambda(NonLocalsToRecompute,
+                [MainGoal | OrElseGoals], !Set, !LambdaSet)
+        ;
+            ShortHand = try_goal(_MaybeIO, _ResultVar, SubGoal),
+            % IO state variables and ResultVar are already in SubGoal.
+            SubGoal = hlds_goal(SubGoalExpr, _SubGoalInfo),
+            goal_expr_vars_maybe_lambda_2(NonLocalsToRecompute, SubGoalExpr,
+                !Set, !LambdaSet)
+        ;
+            ShortHand = bi_implication(LHS, RHS),
+            conj_vars_maybe_lambda(NonLocalsToRecompute, [LHS, RHS],
                 !Set, !LambdaSet)
+        )
+    ).
+
+:- pred goal_expr_vars_no_lambda_2(nonlocals_to_recompute, hlds_goal_expr,
+    set_of_var, set_of_var).
+:- mode goal_expr_vars_no_lambda_2(in(ordinary_nonlocals_no_lambda),
+    in, in, out) is det.
+:- mode goal_expr_vars_no_lambda_2(in(code_gen_nonlocals_no_lambda),
+    in, in, out) is det.
+
+goal_expr_vars_no_lambda_2(NonLocalsToRecompute, GoalExpr, !Set) :-
+    (
+        GoalExpr = unify(LHS, RHS, _, Unification, _),
+        insert(!.Set, LHS, !:Set),
+        (
+            Unification = construct(_, _, _, _, How, _, SubInfo),
+            (
+                How = reuse_cell(cell_to_reuse(ReuseVar, _, SetArgs)),
+                MaybeSetArgs = yes(SetArgs),
+                insert(!.Set, ReuseVar, !:Set)
+            ;
+                How = construct_in_region(RegionVar),
+                MaybeSetArgs = no,
+                insert(!.Set, RegionVar, !:Set)
+            ;
+                ( How = construct_statically
+                ; How = construct_dynamically
+                ),
+                MaybeSetArgs = no
+            ),
+            (
+                SubInfo = construct_sub_info(_, MaybeSize),
+                MaybeSize = yes(dynamic_size(SizeVar))
+            ->
+                insert(!.Set, SizeVar, !:Set)
+            ;
+                true
+            )
+        ;
+            Unification = complicated_unify(_, _, TypeInfoVars),
+            MaybeSetArgs = no,
+            insert_list(!.Set, TypeInfoVars, !:Set)
+        ;
+            ( Unification = deconstruct(_, _, _, _, _, _)
+            ; Unification = assign(_, _)
+            ; Unification = simple_test(_, _)
+            ),
+            MaybeSetArgs = no
+        ),
+        unify_rhs_vars_no_lambda(NonLocalsToRecompute, RHS, MaybeSetArgs, !Set)
+    ;
+        GoalExpr = plain_call(_, _, ArgVars, _, _, _),
+        insert_list(!.Set, ArgVars, !:Set)
+    ;
+        GoalExpr = generic_call(GenericCall, ArgVars1, _, _),
+        goal_util.generic_call_vars(GenericCall, ArgVars0),
+        insert_list(!.Set, ArgVars0, !:Set),
+        insert_list(!.Set, ArgVars1, !:Set)
+    ;
+        GoalExpr = call_foreign_proc(_, _, _, Args, ExtraArgs, _, _),
+        Vars = list.map(foreign_arg_var, Args),
+        ExtraVars = list.map(foreign_arg_var, ExtraArgs),
+        list.append(Vars, ExtraVars, AllVars),
+        insert_list(!.Set, AllVars, !:Set)
+    ;
+        GoalExpr = conj(ConjType, Goals),
+        (
+            ConjType = plain_conj
+        ;
+            ConjType = parallel_conj
+        ),
+        conj_vars_no_lambda(NonLocalsToRecompute, Goals, !Set)
+    ;
+        GoalExpr = disj(Goals),
+        disj_vars_no_lambda(NonLocalsToRecompute, Goals, !Set)
+    ;
+        GoalExpr = switch(Var, _Det, Cases),
+        insert(!.Set, Var, !:Set),
+        case_vars_no_lambda(NonLocalsToRecompute, Cases, !Set)
+    ;
+        GoalExpr = if_then_else(Vars, Cond, Then, Else),
+        % This code does the following:
+        %     !:Set = !.Set + ( (vars(Cond) + vars(Then)) \ Vars ) + vars(Else)
+        % where `+' is set union and `\' is relative complement.
+        goal_vars_both_no_lambda(NonLocalsToRecompute, Cond, CondSet),
+        goal_vars_both_no_lambda(NonLocalsToRecompute, Then, ThenSet),
+        goal_vars_both_no_lambda(NonLocalsToRecompute, Else, ElseSet),
+        union(CondSet, ThenSet, CondThenSet),
+        delete_list(CondThenSet, Vars, SomeCondThenSet),
+        union(!.Set, SomeCondThenSet, !:Set),
+        union(!.Set, ElseSet, !:Set)
+    ;
+        GoalExpr = negation(SubGoal),
+        SubGoal = hlds_goal(SubGoalExpr, _SubGoalInfo),
+        goal_expr_vars_no_lambda_2(NonLocalsToRecompute, SubGoalExpr, !Set)
+    ;
+        GoalExpr = scope(Reason, SubGoal),
+        Set0 = !.Set,
+        (
+            ( Reason = promise_purity(_)
+            ; Reason = commit(_)
+            ; Reason = barrier(_)
+            ; Reason = trace_goal(_, _, _, _, _)
+            ),
+            goal_vars_both_no_lambda(NonLocalsToRecompute, SubGoal, !:Set)
+        ;
+            Reason = exist_quant(Vars),
+            goal_vars_both_no_lambda(NonLocalsToRecompute, SubGoal, !:Set),
+            delete_list(!.Set, Vars, !:Set)
+        ;
+            Reason = promise_solutions(Vars, _Kind),
+            goal_vars_both_no_lambda(NonLocalsToRecompute, SubGoal, !:Set),
+            insert_list(!.Set, Vars, !:Set)
+        ;
+            Reason = from_ground_term(_TermVar, _),
+            goal_vars_both_no_lambda(NonLocalsToRecompute, SubGoal, !:Set)
+            % _TermVar should have been put into the relevant sets when we
+            % processed SubGoal, since it should appear in SubGoal.
+        ),
+        union(Set0, !Set)
+    ;
+        GoalExpr = shorthand(ShortHand),
+        (
+            ShortHand = atomic_goal(_GoalType, Outer, Inner,
+                _MaybeOutputVars, MainGoal, OrElseGoals, _OrElseInners),
+            % XXX STM
+            Outer = atomic_interface_vars(OuterDI, OuterUO),
+            Inner = atomic_interface_vars(InnerDI, InnerUO),
+            insert_list(!.Set, [OuterDI, OuterUO, InnerDI, InnerUO], !:Set),
+            disj_vars_no_lambda(NonLocalsToRecompute, [MainGoal | OrElseGoals],
+                !Set)
         ;
             ShortHand = try_goal(_MaybeIO, _ResultVar, SubGoal),
             % IO state variables and ResultVar are already in SubGoal.
             SubGoal = hlds_goal(SubGoalExpr, _SubGoalInfo),
-            goal_expr_vars_2(NonLocalsToRecompute, SubGoalExpr, !Set,
-                !LambdaSet)
+            goal_expr_vars_no_lambda_2(NonLocalsToRecompute, SubGoalExpr, !Set)
         ;
             ShortHand = bi_implication(LHS, RHS),
-            conj_vars(NonLocalsToRecompute, [LHS, RHS], !Set, !LambdaSet)
+            conj_vars_no_lambda(NonLocalsToRecompute, [LHS, RHS], !Set)
         )
     ).
 
-:- pred unify_rhs_vars(nonlocals_to_recompute, unify_rhs,
-    maybe(list(needs_update)), set_of_var, set_of_var, set_of_var, set_of_var).
-:- mode unify_rhs_vars(in(ordinary_nonlocals), in, in, in, out, in, out)
-    is det.
-:- mode unify_rhs_vars(in(code_gen_nonlocals), in, in, in, out, in, out)
-    is det.
+:- pred unify_rhs_vars_maybe_lambda(nonlocals_to_recompute, unify_rhs,
+    set_of_var, set_of_var, set_of_var, set_of_var).
+:- mode unify_rhs_vars_maybe_lambda(in(ordinary_nonlocals_maybe_lambda),
+    in, in, out, in, out) is det.
+
+unify_rhs_vars_maybe_lambda(NonLocalsToRecompute, RHS, !Set, !LambdaSet) :-
+    (
+        RHS = rhs_var(Y), 
+        insert(!.Set, Y, !:Set)
+    ;
+        RHS = rhs_functor(_, _, ArgVars),
+        insert_list(!.Set, ArgVars, !:Set)
+    ;
+        RHS = rhs_lambda_goal(_, _, _, _, _, LambdaVars, _, _, Goal),
+        % Note that the NonLocals list is not counted, since all the
+        % variables in that list must occur in the goal.
+        goal_vars_bitset_maybe_lambda(NonLocalsToRecompute, Goal, GoalVars),
+        delete_list(GoalVars, LambdaVars, GoalVars1),
+        union(!.LambdaSet, GoalVars1, !:LambdaSet)
+    ).
+
+:- pred unify_rhs_vars_no_lambda(nonlocals_to_recompute, unify_rhs,
+    maybe(list(needs_update)), set_of_var, set_of_var).
+:- mode unify_rhs_vars_no_lambda(in(ordinary_nonlocals_no_lambda),
+    in, in, in, out) is det.
+:- mode unify_rhs_vars_no_lambda(in(code_gen_nonlocals_no_lambda),
+    in, in, in, out) is det.
 
-unify_rhs_vars(NonLocalsToRecompute, RHS, MaybeSetArgs, !Set, !LambdaSet) :-
+unify_rhs_vars_no_lambda(NonLocalsToRecompute, RHS, MaybeSetArgs, !Set) :-
     (
         RHS = rhs_var(Y), 
         insert(!.Set, Y, !:Set)
     ;
         RHS = rhs_functor(_, _, ArgVars),
         (
-            NonLocalsToRecompute = code_gen_nonlocals,
+            NonLocalsToRecompute = code_gen_nonlocals_no_lambda,
             MaybeSetArgs = yes(SetArgs)
         ->
             % Ignore the fields taken from the reused cell.
@@ -1494,12 +1970,8 @@
             insert_list(!.Set, ArgVars, !:Set)
         )
     ;
-        RHS = rhs_lambda_goal(_, _, _, _, _, LambdaVars, _, _, Goal),
-        % Note that the NonLocals list is not counted, since all the
-        % variables in that list must occur in the goal.
-        goal_vars_bitset(NonLocalsToRecompute, Goal, GoalVars),
-        delete_list(GoalVars, LambdaVars, GoalVars1),
-        union(!.LambdaSet, GoalVars1, !:LambdaSet)
+        RHS = rhs_lambda_goal(_, _, _, _, _, _, _, _, _),
+        unexpected(this_file, "unify_rhs_vars_no_lambda: found lambda")
     ).
 
 :- pred insert_set_fields(list(needs_update)::in, list(prog_var)::in,
@@ -1568,8 +2040,12 @@
     %
 :- pred rename_apart(set_of_var, map(prog_var, prog_var),
     nonlocals_to_recompute, hlds_goal, hlds_goal, quant_info, quant_info).
-:- mode rename_apart(in, out, in(ordinary_nonlocals), in, out, in, out) is det.
-:- mode rename_apart(in, out, in(code_gen_nonlocals), in, out, in, out) is det.
+:- mode rename_apart(in, out, in(ordinary_nonlocals_maybe_lambda),
+    in, out, in, out) is det.
+:- mode rename_apart(in, out, in(ordinary_nonlocals_no_lambda),
+    in, out, in, out) is det.
+:- mode rename_apart(in, out, in(code_gen_nonlocals_no_lambda),
+    in, out, in, out) is det.
 
 rename_apart(RenameSet, RenameMap, NonLocalsToRecompute, !Goal, !Info) :-
     (
@@ -1580,7 +2056,7 @@
         % been done while recomputing the ordinary nonlocals.
 
         ( empty(RenameSet)
-        ; NonLocalsToRecompute = code_gen_nonlocals
+        ; NonLocalsToRecompute = code_gen_nonlocals_no_lambda
         )
     ->
         map.init(RenameMap)
@@ -1609,9 +2085,11 @@
 
 :- pred set_goal_nonlocals(set_of_var, nonlocals_to_recompute,
     hlds_goal_info, hlds_goal_info, quant_info, quant_info).
-:- mode set_goal_nonlocals(in, in(ordinary_nonlocals),
+:- mode set_goal_nonlocals(in, in(ordinary_nonlocals_maybe_lambda),
     in, out, in, out) is det.
-:- mode set_goal_nonlocals(in, in(code_gen_nonlocals),
+:- mode set_goal_nonlocals(in, in(ordinary_nonlocals_no_lambda),
+    in, out, in, out) is det.
+:- mode set_goal_nonlocals(in, in(code_gen_nonlocals_no_lambda),
     in, out, in, out) is det.
 
 set_goal_nonlocals(NonLocals, NonLocalsToRecompute, !GoalInfo, !Info) :-
@@ -1621,19 +2099,23 @@
 :- pred set_goal_nonlocals_translate(set_of_var, set(prog_var),
     nonlocals_to_recompute, hlds_goal_info, hlds_goal_info,
     quant_info, quant_info).
-:- mode set_goal_nonlocals_translate(in, out, in(ordinary_nonlocals),
-    in, out, in, out) is det.
-:- mode set_goal_nonlocals_translate(in, out, in(code_gen_nonlocals),
-    in, out, in, out) is det.
+:- mode set_goal_nonlocals_translate(in, out,
+    in(ordinary_nonlocals_maybe_lambda), in, out, in, out) is det.
+:- mode set_goal_nonlocals_translate(in, out,
+    in(ordinary_nonlocals_no_lambda), in, out, in, out) is det.
+:- mode set_goal_nonlocals_translate(in, out,
+    in(code_gen_nonlocals_no_lambda), in, out, in, out) is det.
 
 set_goal_nonlocals_translate(NonLocalsBitSet, NonLocals, NonLocalsToRecompute,
         !GoalInfo, !Info) :-
     NonLocals = bitset_to_set(NonLocalsBitSet),
     (
-        NonLocalsToRecompute = ordinary_nonlocals,
+        ( NonLocalsToRecompute = ordinary_nonlocals_maybe_lambda
+        ; NonLocalsToRecompute = ordinary_nonlocals_no_lambda
+        ),
         goal_info_set_nonlocals(NonLocals, !GoalInfo)
     ;
-        NonLocalsToRecompute = code_gen_nonlocals,
+        NonLocalsToRecompute = code_gen_nonlocals_no_lambda,
         goal_info_set_code_gen_nonlocals(NonLocals, !GoalInfo)
     ).
 
Index: compiler/rbmm.region_transformation.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.region_transformation.m,v
retrieving revision 1.11
diff -u -b -r1.11 rbmm.region_transformation.m
--- compiler/rbmm.region_transformation.m	3 Sep 2009 23:57:29 -0000	1.11
+++ compiler/rbmm.region_transformation.m	4 Sep 2009 18:32:22 -0000
@@ -254,7 +254,7 @@
         RegionInstructionProc, ResurRenamingAnnoProc, IteRenamingAnnoProc,
         VarSet0, _, VarTypes0, _, HeadVars0, _, ActualArgModes0, _,
         Goal0, _, NameToVar0, NameToVar, ProcInfo1, ProcInfo2),
-    requantify_proc(ProcInfo2, ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, ProcInfo2, ProcInfo),
     module_info_set_pred_proc_info(PPId, PredInfo0, ProcInfo, !ModuleInfo),
     svmap.det_insert(PPId, NameToVar, !NameToVarTable).
 
Index: compiler/recompilation.check.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/recompilation.check.m,v
retrieving revision 1.48
diff -u -b -r1.48 recompilation.check.m
--- compiler/recompilation.check.m	2 Sep 2009 00:30:23 -0000	1.48
+++ compiler/recompilation.check.m	7 Sep 2009 02:42:03 -0000
@@ -294,11 +294,7 @@
     (
         SubModulesTerm = term.functor(term.atom("sub_modules"),
             SubModuleTerms, _),
-        list.map(
-            (pred(Term::in, SubModule::out) is semidet :-
-                parse_sym_name_and_args(Term, SubModule, [])
-            ),
-            SubModuleTerms, SubModules)
+        list.map(try_parse_sym_name_and_no_args, SubModuleTerms, SubModules)
     ->
         !Info ^ rci_sub_modules := SubModules
     ;
@@ -366,7 +362,7 @@
     conjunction_to_list(Term, Args),
     (
         Args = [ModuleNameTerm, SuffixTerm, TimestampTerm | MaybeOtherTerms],
-        parse_sym_name_and_args(ModuleNameTerm, ModuleName0, []),
+        try_parse_sym_name_and_no_args(ModuleNameTerm, ModuleName0),
         SuffixTerm = term.functor(term.string(Suffix), [], _),
         Timestamp = term_to_timestamp(TimestampTerm),
         (
@@ -462,10 +458,10 @@
             Term = term.functor(term.atom("=>"),
                 [QualifierTerm, ModuleNameTerm], _)
         ->
-            parse_sym_name_and_args(QualifierTerm, Qualifier, []),
-            parse_sym_name_and_args(ModuleNameTerm, ModuleName, [])
+            try_parse_sym_name_and_no_args(QualifierTerm, Qualifier),
+            try_parse_sym_name_and_no_args(ModuleNameTerm, ModuleName)
         ;
-            parse_sym_name_and_args(Term, ModuleName, []),
+            try_parse_sym_name_and_no_args(Term, ModuleName),
             Qualifier = ModuleName
         )
     ->
@@ -492,16 +488,16 @@
             Term = term.functor(term.atom("=>"),
                 [QualifierTerm, MatchesTerm], _)
         ->
-            parse_sym_name_and_args(QualifierTerm, Qualifier, []),
+            try_parse_sym_name_and_no_args(QualifierTerm, Qualifier),
             conjunction_to_list(MatchesTerm, MatchesList),
             list.map(
                 (pred(MatchTerm::in, Match::out) is semidet :-
-                    parse_sym_name_and_args(MatchTerm, MatchName, []),
+                    try_parse_sym_name_and_no_args(MatchTerm, MatchName),
                     Match = PredId - MatchName
                 ),
                 MatchesList, Matches)
         ;
-            parse_sym_name_and_args(Term, Qualifier, []),
+            try_parse_sym_name_and_no_args(Term, Qualifier),
             Matches = [PredId - Qualifier]
         )
     ->
@@ -525,7 +521,7 @@
     (
         Term = term.functor(term.atom("=>"),
             [QualifierTerm, MatchesTerm], _),
-        parse_sym_name_and_args(QualifierTerm, Qualifier, [])
+        try_parse_sym_name_and_no_args(QualifierTerm, Qualifier)
     ->
         conjunction_to_list(MatchesTerm, MatchesList),
         list.map(parse_resolved_functor(Info), MatchesList, Matches),
@@ -546,7 +542,7 @@
         ( PredOrFuncStr = "predicate", PredOrFunc = pf_predicate
         ; PredOrFuncStr = "function", PredOrFunc = pf_function
         ),
-        parse_sym_name_and_args(ModuleTerm, ModuleName, []),
+        try_parse_sym_name_and_no_args(ModuleTerm, ModuleName),
         ArityTerm = term.functor(term.integer(Arity), [], _)
     ->
         PredId = invalid_pred_id,
Index: compiler/recompilation.version.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/recompilation.version.m,v
retrieving revision 1.68
diff -u -b -r1.68 recompilation.version.m
--- compiler/recompilation.version.m	3 Sep 2009 23:07:29 -0000	1.68
+++ compiler/recompilation.version.m	7 Sep 2009 02:25:07 -0000
@@ -1273,12 +1273,8 @@
     ;
         Term = term.functor(term.atom("instance"), InstanceVNsTerms, _)
     ->
-        ParseName =
-            (pred(NameTerm::in, Name::out) is semidet :-
-                parse_sym_name_and_args(NameTerm, Name, [])
-            ),
-        map_parser(parse_item_version_number(ParseName), InstanceVNsTerms,
-            Result1),
+        map_parser(parse_item_version_number(try_parse_sym_name_and_no_args),
+            InstanceVNsTerms, Result1),
         (
             Result1 = ok1(VNsAL),
             map.from_assoc_list(VNsAL, VNsMap),
Index: compiler/saved_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/saved_vars.m,v
retrieving revision 1.81
diff -u -b -r1.81 saved_vars.m
--- compiler/saved_vars.m	19 Feb 2009 03:49:18 -0000	1.81
+++ compiler/saved_vars.m	4 Sep 2009 18:35:35 -0000
@@ -90,7 +90,8 @@
     % hlds_out.write_goal(Goal1, !.ModuleInfo, Varset1, 0, "\n"),
 
     % Recompute the nonlocals for each goal.
-    implicitly_quantify_clause_body(HeadVars, _Warnings, Goal1, Goal2,
+    implicitly_quantify_clause_body_general(ordinary_nonlocals_no_lambda,
+        HeadVars, _Warnings, Goal1, Goal2,
         Varset1, Varset, VarTypes1, VarTypes, RttiVarMaps1, RttiVarMaps),
     proc_info_get_initial_instmap(!.ProcInfo, !.ModuleInfo, InstMap0),
     proc_info_get_inst_varset(!.ProcInfo, InstVarSet),
Index: compiler/simplify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/simplify.m,v
retrieving revision 1.245
diff -u -b -r1.245 simplify.m
--- compiler/simplify.m	3 Sep 2009 23:57:29 -0000	1.245
+++ compiler/simplify.m	4 Sep 2009 18:26:04 -0000
@@ -473,7 +473,8 @@
             % the nonlocal fields of the original scopes. And since
             % instmap_deltas are restricted to the goal's nonlocals,
             % they need to be recomputed as well.
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_maybe_lambda,
+                !ProcInfo),
             recompute_instmap_delta_proc(
                 do_not_recompute_atomic_instmap_deltas,
                 !ProcInfo, !ModuleInfo),
@@ -656,8 +657,8 @@
             simplify_info_get_varset(!.Info, !:VarSet),
             simplify_info_get_var_types(!.Info, !:VarTypes),
             simplify_info_get_rtti_varmaps(!.Info, !:RttiVarMaps),
-            implicitly_quantify_goal(NonLocals, _, !Goal,
-                !VarSet, !VarTypes, !RttiVarMaps),
+            implicitly_quantify_goal_general(ordinary_nonlocals_maybe_lambda,
+                NonLocals, _, !Goal, !VarSet, !VarTypes, !RttiVarMaps),
 
             simplify_info_set_varset(!.VarSet, !Info),
             simplify_info_set_var_types(!.VarTypes, !Info),
Index: compiler/size_prof.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/size_prof.m,v
retrieving revision 1.61
diff -u -b -r1.61 size_prof.m
--- compiler/size_prof.m	3 Sep 2009 23:57:29 -0000	1.61
+++ compiler/size_prof.m	4 Sep 2009 18:32:34 -0000
@@ -269,7 +269,8 @@
         % the nonlocal vars and the non-atomic instmap deltas.
     proc_info_get_headvars(!.ProcInfo, HeadVars),
     proc_info_get_inst_varset(!.ProcInfo, InstVarSet),
-    implicitly_quantify_clause_body(HeadVars, _Warnings, Goal1, Goal2,
+    implicitly_quantify_clause_body_general(ordinary_nonlocals_no_lambda,
+        HeadVars, _Warnings, Goal1, Goal2,
         Info ^ spi_varset, VarSet, Info ^ spi_vartypes, VarTypes,
         Info ^ spi_rtti_varmaps, RttiVarMaps),
     recompute_instmap_delta(do_not_recompute_atomic_instmap_deltas,
Index: compiler/ssdebug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ssdebug.m,v
retrieving revision 1.21
diff -u -b -r1.21 ssdebug.m
--- compiler/ssdebug.m	3 Sep 2009 23:57:29 -0000	1.21
+++ compiler/ssdebug.m	4 Sep 2009 18:32:43 -0000
@@ -839,7 +839,7 @@
     proc_info_set_varset(Varset, !ProcInfo),
     proc_info_set_vartypes(Vartypes, !ProcInfo),
     proc_info_set_goal(Goal, !ProcInfo),
-    requantify_proc(!ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
     recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
         !ProcInfo, !ModuleInfo),
     pred_info_set_proc_info(ProcId, !.ProcInfo, !PredInfo),
Index: compiler/stack_opt.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_opt.m,v
retrieving revision 1.40
diff -u -b -r1.40 stack_opt.m
--- compiler/stack_opt.m	4 Sep 2009 02:27:54 -0000	1.40
+++ compiler/stack_opt.m	4 Sep 2009 18:35:42 -0000
@@ -198,7 +198,7 @@
         Changed = yes,
         maybe_write_progress_message("\nafter stack opt transformation",
             DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO),
-        requantify_proc(!ProcInfo),
+        requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
         maybe_write_progress_message("\nafter stack opt requantify",
             DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO),
         recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
Index: compiler/stm_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stm_expand.m,v
retrieving revision 1.7
diff -u -b -r1.7 stm_expand.m
--- compiler/stm_expand.m	3 Sep 2009 23:57:29 -0000	1.7
+++ compiler/stm_expand.m	4 Sep 2009 18:32:58 -0000
@@ -294,7 +294,7 @@
 
     (
         RecalcInfo = yes,
-        requantify_proc(!ProcInfo),
+        requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
         recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
             !ProcInfo, !ModuleInfo)
     ;
@@ -2509,7 +2509,8 @@
 run_quantification_over_pred(!NewPredInfo) :-
     ProcInfo0 = !.NewPredInfo ^ new_pred_proc_info,
     ModuleInfo0 = !.NewPredInfo ^ new_pred_module_info,
-    requantify_proc(ProcInfo0, ProcInfo1),
+    requantify_proc_general(ordinary_nonlocals_no_lambda,
+        ProcInfo0, ProcInfo1),
     recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
         ProcInfo1, ProcInfo, ModuleInfo0, ModuleInfo),
     !:NewPredInfo = !.NewPredInfo ^ new_pred_module_info := ModuleInfo,
Index: compiler/structure_reuse.versions.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.versions.m,v
retrieving revision 1.19
diff -u -b -r1.19 structure_reuse.versions.m
--- compiler/structure_reuse.versions.m	23 Dec 2008 01:37:41 -0000	1.19
+++ compiler/structure_reuse.versions.m	4 Sep 2009 18:28:51 -0000
@@ -314,7 +314,7 @@
             % construction unification in which its space is reused, so we
             % requantify.  Then we recompute instmap deltas with the updated
             % non-local sets.
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             recompute_instmap_delta_proc(
                 do_not_recompute_atomic_instmap_deltas,
                 !ProcInfo, !ModuleInfo),
Index: compiler/superhomogeneous.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/superhomogeneous.m,v
retrieving revision 1.34
diff -u -b -r1.34 superhomogeneous.m
--- compiler/superhomogeneous.m	2 Sep 2009 00:30:23 -0000	1.34
+++ compiler/superhomogeneous.m	7 Sep 2009 02:06:18 -0000
@@ -755,7 +755,7 @@
         % Handle the usual case.
         % XXX Why do we use Args1 instead of Args here?
         RHS = term.functor(F, Args1, FunctorContext),
-        ( parse_sym_name_and_args(RHS, FunctorName, FunctorArgsPrime) ->
+        ( try_parse_sym_name_and_args(RHS, FunctorName, FunctorArgsPrime) ->
             FunctorArgs = FunctorArgsPrime,
             list.length(FunctorArgs, Arity),
             ConsId = cons(FunctorName, Arity, cons_id_dummy_type_ctor)
Index: compiler/switch_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/switch_detection.m,v
retrieving revision 1.147
diff -u -b -r1.147 switch_detection.m
--- compiler/switch_detection.m	21 Jul 2009 04:10:43 -0000	1.147
+++ compiler/switch_detection.m	4 Sep 2009 18:26:19 -0000
@@ -182,7 +182,8 @@
     proc_info_set_goal(Goal, ProcInfo0, ProcInfo1),
     (
         Requant = need_to_requantify,
-        requantify_proc(ProcInfo1, ProcInfo)
+        requantify_proc_general(ordinary_nonlocals_maybe_lambda,
+            ProcInfo1, ProcInfo)
     ;
         Requant = do_not_need_to_requantify,
         ProcInfo = ProcInfo1
Index: compiler/term_constr_build.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_build.m,v
retrieving revision 1.24
diff -u -b -r1.24 term_constr_build.m
--- compiler/term_constr_build.m	20 Jul 2009 00:46:12 -0000	1.24
+++ compiler/term_constr_build.m	7 Sep 2009 22:38:04 -0000
@@ -273,8 +273,8 @@
 % * Any calls that are made from the SCC being processed to lower SCCs
 %   that do not terminate.
 
-:- type traversal_info
-    --->    traversal_info(
+:- type tti_traversal_info
+    --->    tti_traversal_info(
                 % What type of recursion is present in the procedure,
                 % i.e. `none', `direct', `mutual'.
                 tti_recursion                   :: recursion_type,
@@ -330,41 +330,41 @@
 
 :- func init_traversal_info(module_info, functor_info, pred_proc_id,
     term.context, vartypes, zero_vars, size_var_map, list(pred_proc_id),
-    bool, bool) = traversal_info.
+    bool, bool) = tti_traversal_info.
 
 init_traversal_info(ModuleInfo, Norm, PPId, Context, Types, Zeros,
         VarMap, SCC, FailConstrs, ArgSizeOnly)
-    = traversal_info(none, not_mutually_recursive, [], ModuleInfo, Norm,
+    = tti_traversal_info(none, not_mutually_recursive, [], ModuleInfo, Norm,
         PPId, Context, Types, Zeros, VarMap, SCC, 0, FailConstrs, [],
         ArgSizeOnly).
 
-:- pred info_increment_maxcalls(traversal_info::in, traversal_info::out)
-    is det.
+:- pred info_increment_maxcalls(tti_traversal_info::in,
+    tti_traversal_info::out) is det.
 
 info_increment_maxcalls(!Info) :-
     !Info ^ tti_maxcalls := !.Info ^ tti_maxcalls + 1.
 
-:- pred info_update_errors(term_constr_errors.error::in, traversal_info::in,
-    traversal_info::out) is det.
+:- pred info_update_errors(term_constr_errors.error::in,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 info_update_errors(Error, !Info) :-
-    !:Info = !.Info ^ tti_errors := [Error | !.Info ^ tti_errors].
+    !Info ^ tti_errors := [Error | !.Info ^ tti_errors].
 
 :- pred info_update_recursion(recursion_type::in,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 info_update_recursion(RecType, !Info) :-
     UpdatedRecType = combine_recursion_types(!.Info ^ tti_recursion, RecType),
     !Info ^ tti_recursion := UpdatedRecType.
 
 :- pred info_update_ho_info(context::in,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 info_update_ho_info(Context, !Info) :-
     !Info ^ tti_ho_info := [ho_call(Context) | !.Info ^ tti_ho_info].
 
 :- pred set_intermod_status(intermod_status::in,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 set_intermod_status(Status, !TraversalInfo) :-
     !TraversalInfo ^ tti_intermod_status := Status.
@@ -379,7 +379,7 @@
 % (See comments about local variables in term_constr_data.m for more details.)
 
 :- pred build_abstract_goal(hlds_goal::in, abstract_goal::out,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_goal(Goal, AbstractGoal, !Info) :-
     Goal = hlds_goal(GoalExpr, GoalInfo),
@@ -392,7 +392,7 @@
         Locals, NonLocals).
 
 :- pred build_abstract_goal_2(hlds_goal_expr::in, hlds_goal_info::in,
-    abstract_goal::out, traversal_info::in, traversal_info::out) is det.
+    abstract_goal::out, tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_goal_2(GoalExpr, GoalInfo, AbstractGoal, !Info) :-
     (
@@ -415,7 +415,7 @@
         % Work out a failure constraint for the Cond and then abstract the else
         % branch.  We won't bother do any other simplifications here as the AR
         % simplification pass will sort all of this out.
-        CondFail = find_failure_constraint_for_goal(Cond, !.Info),
+        CondFail = find_failure_constraint_for_goal(!.Info, Cond),
 
         % XXX FIXME - the local/non-local variable sets end up
         % being incorrect here.
@@ -424,10 +424,13 @@
         AbstractDisjuncts = [AbstractSuccessGoal, AbstractFailureGoal],
         AbstractGoal = term_disj(AbstractDisjuncts, 2, [], [])
     ;
-        GoalExpr = scope(_Reason, SubGoal),
-        % XXX We should special-case the handling of from_ground_term_construct
-        % scopes.
+        GoalExpr = scope(Reason, SubGoal),
+        ( Reason = from_ground_term(TermVar, from_ground_term_construct) ->
+            build_abstract_from_ground_term_goal(TermVar, SubGoal,
+                AbstractGoal, !Info)
+        ;
         build_abstract_goal(SubGoal, AbstractGoal, !Info)
+        )
     ;
         GoalExpr = plain_call(CallPredId, CallProcId, CallArgs, _, _, _),
         CallSizeArgs = prog_vars_to_size_vars(!.Info ^ tti_size_var_map,
@@ -447,7 +450,7 @@
         % `--term2-propagate-failure-constraints' is enabled,
         % otherwise just use the constraint that all non-zero input vars
         % should be non-negative.
-        AbstractGoal = find_failure_constraint_for_goal(SubGoal, !.Info)
+        AbstractGoal = find_failure_constraint_for_goal(!.Info, SubGoal)
     ;
         GoalExpr = call_foreign_proc(Attrs, PredId, ProcId, Args, ExtraArgs,
             _, _),
@@ -499,7 +502,7 @@
 %
 
 :- pred build_abstract_conj(hlds_goals::in, abstract_goal::out,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_conj(Conjuncts, AbstractGoal, !Info) :-
     list.map_foldl(build_abstract_goal,Conjuncts, AbstractGoals0, !Info),
@@ -512,8 +515,8 @@
 %
 
 :- pred build_abstract_call(pred_proc_id::in, size_vars::in,
-    hlds_goal_info::in, abstract_goal::out, traversal_info::in,
-    traversal_info::out) is det.
+    hlds_goal_info::in, abstract_goal::out,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_call(CalleePPId, CallerArgs, GoalInfo, AbstractGoal, !Info) :-
     Context = goal_info_get_context(GoalInfo),
@@ -529,7 +532,8 @@
     % to represent it - see term_constr_data.m for details.
     %
 :- pred build_recursive_call(pred_proc_id::in, size_vars::in, prog_context::in,
-    abstract_goal::out, traversal_info::in, traversal_info::out) is det.
+    abstract_goal::out,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_recursive_call(CalleePPId, CallerArgs, Context, AbstractGoal, !Info) :-
     CallerPPId = !.Info ^ tti_ppid,
@@ -553,8 +557,8 @@
     % callee procedure.
     %
 :- pred build_non_recursive_call(pred_proc_id::in, size_vars::in,
-    prog_context::in, abstract_goal::out, traversal_info::in,
-    traversal_info::out) is det.
+    prog_context::in, abstract_goal::out,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_non_recursive_call(CalleePPId, CallerArgs, Context, AbstractGoal,
         !Info) :-
@@ -631,7 +635,7 @@
     ;       non_switch(hlds_goals).
 
 :- pred build_abstract_disj(disj_info::in, abstract_goal::out,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_disj(Type, AbstractGoal, !Info) :-
     (
@@ -654,7 +658,8 @@
     ).
 
 :- pred build_abstract_disj_acc(hlds_goals::in, abstract_goals::in,
-    abstract_goals::out, traversal_info::in, traversal_info::out) is det.
+    abstract_goals::out,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_disj_acc([], !AbstractGoals, !Info).
 build_abstract_disj_acc([Goal | Goals], !AbstractGoals, !Info) :-
@@ -674,8 +679,8 @@
     % goal and conjoining that to the rest.
     %
 :- pred build_abstract_switch_acc(prog_var::in, list(case)::in,
-    abstract_goals::in, abstract_goals::out, traversal_info::in,
-    traversal_info::out) is det.
+    abstract_goals::in, abstract_goals::out,
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_switch_acc(_, [], !AbstractGoals, !Info).
 build_abstract_switch_acc(SwitchProgVar, [Case | Cases], !AbstractGoals,
@@ -703,9 +708,11 @@
         map.lookup(TypeMap, SwitchProgVar, SwitchVarType),
         SwitchSizeVar = prog_var_to_size_var(SizeVarMap, SwitchProgVar),
         type_to_ctor_and_args_det(SwitchVarType, TypeCtor, _),
-        Size = functor_lower_bound(!.Info ^ tti_norm, TypeCtor, MainConsId,
-            !.Info ^ tti_module_info),
-        ( set.member(SwitchSizeVar, !.Info ^ tti_zeros) ->
+        ModuleInfo = !.Info ^ tti_module_info,
+        Norm = !.Info ^ tti_norm,
+        Zeros = !.Info ^ tti_zeros,
+        Size = functor_lower_bound(ModuleInfo, Norm, TypeCtor, MainConsId),
+        ( set.member(SwitchSizeVar, Zeros) ->
             ExtraConstr = []
         ;
             SwitchVarConst = rat(Size),
@@ -748,11 +755,96 @@
 
 %------------------------------------------------------------------------------%
 %
+% Additional predicates for abstracting from_ground_term scopes,
+% which act like giant construction unifications.
+%
+
+:- pred build_abstract_from_ground_term_goal(prog_var::in, hlds_goal::in,
+    abstract_goal::out, tti_traversal_info::in, tti_traversal_info::out)
+    is det.
+
+build_abstract_from_ground_term_goal(TermVar, SubGoal, AbstractGoal, !Info) :-
+    SubGoal = hlds_goal(SubGoalExpr, _SubGoalInfo),
+    ( SubGoalExpr = conj(plain_conj, Conjuncts) ->
+        SizeVarMap = !.Info ^ tti_size_var_map,
+        Zeros  = !.Info ^ tti_zeros,
+        TermSizeVar = prog_var_to_size_var(SizeVarMap, TermVar),
+        ( set.member(TermSizeVar, Zeros) ->
+            Constraints = []
+        ;
+            ModuleInfo = !.Info ^ tti_module_info,
+            Norm   = !.Info ^ tti_norm,
+            VarTypes = !.Info ^ tti_vartypes,
+            abstract_from_ground_term_conjuncts(ModuleInfo, Norm, VarTypes,
+                Conjuncts, map.init, SizeMap),
+            map.lookup(SizeMap, TermVar, KnownTermVarSize),
+            Terms = [TermSizeVar - one],
+            Constraint =
+                construct_constraint(Terms, lp_eq, rat(KnownTermVarSize)),
+            Constraints = [Constraint]
+        ),
+        AbstractGoal = build_goal_from_unify(Constraints)
+    ;
+        unexpected(this_file, "build_abstract_from_ground_term_goal: not conj")
+    ).
+
+:- pred abstract_from_ground_term_conjuncts(module_info::in, functor_info::in,
+    vartypes::in, list(hlds_goal)::in,
+    map(prog_var, int)::in, map(prog_var, int)::out) is det.
+
+abstract_from_ground_term_conjuncts(_ModuleInfo, _Norm, _VarTypes, [],
+        !SizeMap).
+abstract_from_ground_term_conjuncts(ModuleInfo, Norm, VarTypes, [Goal | Goals],
+        !SizeMap) :-
+    abstract_from_ground_term_conjunct(ModuleInfo, Norm, VarTypes, Goal,
+        !SizeMap),
+    abstract_from_ground_term_conjuncts(ModuleInfo, Norm, VarTypes, Goals,
+        !SizeMap).
+
+:- pred abstract_from_ground_term_conjunct(module_info::in, functor_info::in,
+    vartypes::in, hlds_goal::in,
+    map(prog_var, int)::in, map(prog_var, int)::out) is det.
+
+abstract_from_ground_term_conjunct(ModuleInfo, Norm, VarTypes, Goal,
+        !SizeMap) :-
+    Goal = hlds_goal(GoalExpr, _GoalInfo),
+    (
+        GoalExpr = unify(_, _, _, Unify, _),
+        Unify = construct(Var, ConsId, ArgVars, Modes, _, _, _)
+    ->
+        strip_typeinfos_from_args_and_modes(VarTypes, ArgVars, FixedArgVars,
+            Modes, FixedModes),
+        map.lookup(VarTypes, Var, Type),
+        type_to_ctor_det(Type, TypeCtor),
+        functor_norm(ModuleInfo, Norm, TypeCtor, ConsId, ConsIdSize,
+            FixedArgVars, CountedVars, FixedModes, _),
+
+        % Note that any vars that are in ArgVars but not in CountedVars
+        % will be left in !:SizeMap, which is a performance problem (but not
+        % correctness problem) for the later goals in the conjunction.
+        list.map_foldl(svmap.det_remove, CountedVars, ArgSizes, !SizeMap),
+        accumulate_sum(ArgSizes, 0, TotalArgSize),
+        Size = ConsIdSize + TotalArgSize,
+        svmap.det_insert(Var, Size, !SizeMap)
+    ;
+        unexpected(this_file,
+            "abstract_from_ground_term_conjunct: malformed conjunct")
+    ).
+
+:- pred accumulate_sum(list(int)::in, int::in, int::out) is det.
+
+accumulate_sum([], !TotalSize).
+accumulate_sum([Size | Sizes], !TotalSize) :-
+    !:TotalSize = !.TotalSize + Size,
+    accumulate_sum(Sizes, !TotalSize).
+
+%------------------------------------------------------------------------------%
+%
 % Additional predicates for abstracting unifications.
 %
 
 :- pred build_abstract_unification(unification::in, abstract_goal::out,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_unification(Unification, AbstractGoal, !Info) :-
     (
@@ -785,69 +877,69 @@
     %
 :- pred build_abstract_decon_or_con_unify(prog_var::in, cons_id::in,
     prog_vars::in, list(uni_mode)::in, constraints::out,
-    traversal_info::in, traversal_info::out) is det.
+    tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_decon_or_con_unify(Var, ConsId, ArgVars, Modes, Constraints,
         !Info) :-
     VarTypes = !.Info ^ tti_vartypes,
     map.lookup(VarTypes, Var, Type),
-    (
-        not type_is_higher_order(Type),
-        type_to_ctor_and_args(Type, TypeCtor, _)
-    ->
-        Norm   = !.Info ^ tti_norm,
-        ModuleInfo = !.Info ^ tti_module_info,
-        Zeros  = !.Info ^ tti_zeros,
-
+    ( type_is_higher_order(Type) ->
+        % The only valid higher-order unifications are assignments.
+        % For the purposes of the IR analysis, we can ignore them.
+        Constraints = []
+    ;
         % We need to strip out any typeinfo related variables before
         % measuring the size of the term; otherwise functor_norm will
         % raise a software error if we are using the `num-data-elems'
         % norm and the term has existential typeclass constraints.
 
-        strip_typeinfos_from_args_and_modes(VarTypes, ArgVars, FixedArgs,
+        strip_typeinfos_from_args_and_modes(VarTypes, ArgVars, FixedArgVars,
             Modes, FixedModes),
-
-        functor_norm(Norm, TypeCtor, ConsId, ModuleInfo, Constant,
-            FixedArgs, CountedVars, FixedModes, _),
+        ModuleInfo = !.Info ^ tti_module_info,
+        Norm   = !.Info ^ tti_norm,
+        type_to_ctor_det(Type, TypeCtor),
+        functor_norm(ModuleInfo, Norm, TypeCtor, ConsId, Constant,
+            FixedArgVars, CountedVars, FixedModes, _),
 
         % The constraint from this unification is:
         %
         %      |Var| = Constant + sum(CountedVars)
         %
-        % |Var| is just the size_var corresponding to Var.  The
-        % value of `Constant' will depend upon the norm being used.
+        % |Var| is just the size_var corresponding to Var. The value of
+        % `Constant' will depend upon the norm being used.
 
-        SizeVar = prog_var_to_size_var(!.Info ^ tti_size_var_map, Var),
+        SizeVarMap = !.Info ^ tti_size_var_map,
+        Zeros  = !.Info ^ tti_zeros,
+
+        SizeVar = prog_var_to_size_var(SizeVarMap, Var),
         ( set.member(SizeVar, Zeros) ->
-            FirstTerm = []
-        ;
-            FirstTerm = [SizeVar - one]
-        ),
-        AddTerms = (func(Var1, Terms0) = Terms1 :-
-            SizeVar1 = prog_var_to_size_var(!.Info ^ tti_size_var_map, Var1),
-            ( set.member(SizeVar1, Zeros) ->
-                Terms1 = Terms0
+            FirstTerms = []
             ;
-                Terms1 = [SizeVar1 - (-one) | Terms0]
-            )
+            FirstTerms = [SizeVar - one]
         ),
-        Terms = list.foldl(AddTerms, CountedVars, FirstTerm),
-        Constraint = constraint(Terms, (=), rat(Constant)),
+        list.foldl(accumulate_nonzero_arg_coeffs(SizeVarMap, Zeros, -one),
+            CountedVars, FirstTerms, Terms),
+        Constraint = construct_constraint(Terms, lp_eq, rat(Constant)),
         ( is_false(Constraint) ->
             unexpected(this_file, "false constraint from unification.")
         ;
-            SizeVars0 = prog_vars_to_size_vars(!.Info ^ tti_size_var_map,
-                ArgVars),
+            SizeVars0 = prog_vars_to_size_vars(SizeVarMap, ArgVars),
             SizeVars1 = [SizeVar | SizeVars0],
-            SizeVars  = list.filter(isnt(is_zero_size_var(!.Info ^ tti_zeros)),
-                SizeVars1)
+            SizeVars  = list.filter(isnt(is_zero_size_var(Zeros)), SizeVars1)
         ),
         NonNegConstraints = list.map(make_nonneg_constr, SizeVars),
-        Constraints  = [ Constraint | NonNegConstraints ]
+        Constraints  = [Constraint | NonNegConstraints]
+    ).
+
+:- pred accumulate_nonzero_arg_coeffs(size_var_map::in, set(size_var)::in,
+    coefficient::in, prog_var::in, lp_terms::in, lp_terms::out) is det.
+
+accumulate_nonzero_arg_coeffs(SizeVarMap, Zeros, Coeff, Var, !Terms) :-
+    SizeVar = prog_var_to_size_var(SizeVarMap, Var),
+    ( set.member(SizeVar, Zeros) ->
+        true
     ;
-        % The only valid higher-order unifications are assignments
-        % For the purposes of the IR analysis we can ignore them.
-        Constraints = []
+        !:Terms = [SizeVar - Coeff | !.Terms]
     ).
 
 :- pred strip_typeinfos_from_args_and_modes(vartypes::in,
@@ -882,7 +974,7 @@
     % are abstracted as |X| - |Y| = 0.
     %
 :- pred build_abstract_simple_or_assign_unify(prog_var::in, prog_var::in,
-    constraints::out, traversal_info::in, traversal_info::out) is det.
+    constraints::out, tti_traversal_info::in, tti_traversal_info::out) is det.
 
 build_abstract_simple_or_assign_unify(LeftProgVar, RightProgVar, Constraints,
         !Info) :-
@@ -906,7 +998,7 @@
         NonNegConstrs = list.map(make_nonneg_constr,
             [LeftSizeVar, RightSizeVar]),
         Terms = [LeftSizeVar - one, RightSizeVar - (-one)],
-        AssignConstr = constraint(Terms, (=), zero),
+        AssignConstr = construct_constraint(Terms, lp_eq, zero),
         % XXX I don't think this call to simplify helps anymore.
         Constraints = simplify_constraints([AssignConstr | NonNegConstrs])
     ).
@@ -1019,13 +1111,13 @@
 %
 % TODO Better failure constraints for goals other than unifications.
 
-:-  func find_failure_constraint_for_goal(hlds_goal, traversal_info)
+:-  func find_failure_constraint_for_goal(tti_traversal_info, hlds_goal)
     = abstract_goal.
 
-find_failure_constraint_for_goal(Goal, Info) = AbstractGoal :-
+find_failure_constraint_for_goal(Info, Goal) = AbstractGoal :-
     (
         Info ^ tti_find_fail_constrs = yes,
-        find_failure_constraint_for_goal_2(Goal, Info, AbstractGoal0)
+        find_failure_constraint_for_goal_2(Info, Goal, AbstractGoal0)
     ->
         AbstractGoal = AbstractGoal0
     ;
@@ -1039,17 +1131,20 @@
         AbstractGoal = term_primitive(FailPoly, [], [])
     ).
 
-:- pred find_failure_constraint_for_goal_2(hlds_goal::in,
-    traversal_info::in, abstract_goal::out) is semidet.
+:- pred find_failure_constraint_for_goal_2(tti_traversal_info::in,
+    hlds_goal::in, abstract_goal::out) is semidet.
 
     % XXX We could factor out a lot of the code used for
     % substitutions below as the same code is used elsewhere.
     %
-find_failure_constraint_for_goal_2(hlds_goal(GoalExpr, _), Info,
-        AbstractGoal) :-
+find_failure_constraint_for_goal_2(Info, Goal, AbstractGoal) :-
+    Goal = hlds_goal(GoalExpr, _),
+    (
     GoalExpr = plain_call(PredId, ProcId, CallArgs, _, _, _),
-    CallSizeArgs0 = prog_vars_to_size_vars(Info ^ tti_size_var_map, CallArgs),
-    CallSizeArgs = list.filter(isnt(is_zero_size_var(Info ^ tti_zeros)),
+        SizeVarMap = Info ^ tti_size_var_map,
+        CallSizeArgs0 = prog_vars_to_size_vars(SizeVarMap, CallArgs),
+        Zeros = Info ^ tti_zeros,
+        CallSizeArgs = list.filter(isnt(is_zero_size_var(Zeros)),
         CallSizeArgs0),
     ModuleInfo = Info ^ tti_module_info,
     module_info_pred_proc_info(ModuleInfo, PredId, ProcId, _, ProcInfo),
@@ -1068,28 +1163,20 @@
         ;
             CalleeFailureConstraints = [_ | _],
             CalleeHeadVars = TermInfo ^ head_vars,
-            SubstMap = create_var_substitution(CallSizeArgs, CalleeHeadVars),
-            FailureConstraints = substitute_size_vars(CalleeFailureConstraints,
-                SubstMap)
+                SubstMap =
+                    create_var_substitution(CallSizeArgs, CalleeHeadVars),
+                FailureConstraints =
+                    substitute_size_vars(CalleeFailureConstraints, SubstMap)
         )
     ),
     FailurePolyhedron = polyhedron.from_constraints(FailureConstraints),
-    AbstractGoal = term_primitive(FailurePolyhedron, [], []).
-
-find_failure_constraint_for_goal_2(
-        hlds_goal(GoalExpr @ unify(_, _, _, _, _), _),
-        Info, AbstractGoal) :-
-    find_deconstruct_fail_bound(GoalExpr, Info, Polyhedron),
-    AbstractGoal = term_primitive(Polyhedron, [], []).
-
+        AbstractGoal = term_primitive(FailurePolyhedron, [], [])
+    ;
     % Given a deconstruction unification and assuming that it has failed,
     % find a bound on the size of the variable being deconstructed.
-    %
-:- pred find_deconstruct_fail_bound(hlds_goal_expr::in, traversal_info::in,
-    polyhedron::out) is semidet.
 
-find_deconstruct_fail_bound(unify(_, _, _, Kind, _), Info, Polyhedron) :-
-    Kind = deconstruct(Var, ConsId, _, _, can_fail, _),
+        GoalExpr = unify(_, _, _, Unification, _),
+        Unification = deconstruct(Var, ConsId, _, _, can_fail, _),
     map.lookup(Info ^ tti_vartypes, Var, Type),
     type_to_ctor_det(Type, TypeCtor),
     ModuleInfo = Info ^ tti_module_info,
@@ -1110,9 +1197,13 @@
         unexpected(this_file,
             "find_deconstruct_fail_bound/3: non cons cons_id.")
     ),
-    SizeVar = prog_var_to_size_var(Info ^ tti_size_var_map, Var),
-    bounds_on_var(Info ^ tti_norm, ModuleInfo, TypeCtor, SizeVar,
-        Constructors, Polyhedron).
+        SizeVarMap = Info ^ tti_size_var_map,
+        SizeVar = prog_var_to_size_var(SizeVarMap, Var),
+        Norm = Info ^ tti_norm,
+        bounds_on_var(Norm, ModuleInfo, TypeCtor, SizeVar, Constructors,
+            Polyhedron),
+        AbstractGoal = term_primitive(Polyhedron, [], [])
+    ).
 
     % Given a variable, its type and a list of constructors to which
     % it could be bound, return a polyhedron representing the bounds
@@ -1137,7 +1228,7 @@
                 "bounds_on_var/6: no other constructors for type.")
         ;
             ZeroSizeCtors = [_ | _],
-            Constraints = [constraint([Var - one], (=), zero)]
+            Constraints = [construct_constraint([Var - one], lp_eq, zero)]
         )
     ;
         NonZeroSizeCtors = [C | Cs],
@@ -1146,10 +1237,12 @@
         (
             ZeroSizeCtors = [],
             LowerBound = list.foldl(int.min, Cs, C),
-            LowerBoundConstr = [constraint([Var - one], (>=), rat(LowerBound))]
+            LowerBoundConstr =
+                [construct_constraint([Var - one], lp_gt_eq, rat(LowerBound))]
         ;
             ZeroSizeCtors = [_ | _],
-            LowerBoundConstr = [constraint([Var - one], (>=), zero)]
+            LowerBoundConstr =
+                [construct_constraint([Var - one], lp_gt_eq, zero)]
         ),
         Constraints = LowerBoundConstr ++ UpperBoundConstr
     ),
@@ -1161,7 +1254,7 @@
     Constructor = ctor(_, _, SymName, Args, _),
     Arity = list.length(Args),
     ConsId = cons(SymName, Arity, TypeCtor),
-    LowerBound = functor_lower_bound(Norm, TypeCtor, ConsId, ModuleInfo).
+    LowerBound = functor_lower_bound(ModuleInfo, Norm, TypeCtor, ConsId).
 
     % Given a variable, its type and a set of constructors to which it
     % could be bound, return a constraint that specifies an upper bound
@@ -1189,14 +1282,15 @@
         ),
         Arity = list.length(Args),
         ConsId = cons(SymName, Arity, TypeCtor),
-        Bound = functor_lower_bound(Norm, TypeCtor, ConsId, ModuleInfo),
+        Bound = functor_lower_bound(ModuleInfo, Norm, TypeCtor, ConsId),
         ( if Bound > !.B then !:B = Bound else true )
     ),
     ( list.foldl(FindUpperBound, Ctors, 0, Bound0) ->
         ( Bound0 = 0 ->
             unexpected(this_file, "zero upper bound.")
         ;
-            Constraints = [constraint([Var - one], (=<), rat(Bound0))]
+            Constraints =
+                [construct_constraint([Var - one], lp_lt_eq, rat(Bound0))]
         )
     ;
         Constraints = []
Index: compiler/term_constr_initial.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_initial.m,v
retrieving revision 1.25
diff -u -b -r1.25 term_constr_initial.m
--- compiler/term_constr_initial.m	9 Sep 2008 01:37:30 -0000	1.25
+++ compiler/term_constr_initial.m	7 Sep 2009 04:03:37 -0000
@@ -236,10 +236,10 @@
 
 create_arg_size_constraint(SubstMap, le(Terms0, Constant), Constraint) :-
     list.map(create_lp_term(SubstMap), Terms0, Terms),
-    Constraint = constraint(Terms, (=<), Constant).
+    Constraint = construct_constraint(Terms, lp_lt_eq, Constant).
 create_arg_size_constraint(SubstMap, eq(Terms0, Constant), Constraint) :-
     list.map(create_lp_term(SubstMap), Terms0, Terms),
-    Constraint = constraint(Terms, (=), Constant).
+    Constraint = construct_constraint(Terms, lp_eq, Constant).
 
 :- pred create_lp_term(map(int, var)::in, arg_size_term::in, lp_term::out)
     is det.
Index: compiler/term_constr_main.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_main.m,v
retrieving revision 1.19
diff -u -b -r1.19 term_constr_main.m
--- compiler/term_constr_main.m	20 Jul 2009 00:46:12 -0000	1.19
+++ compiler/term_constr_main.m	7 Sep 2009 05:04:52 -0000
@@ -288,7 +288,7 @@
     %   - which norm we are using.
     %   - whether we are propagating failure constraints.
     globals.io_get_termination2_norm(Norm, !IO),
-    FunctorInfo = set_functor_info(Norm, !.ModuleInfo),
+    FunctorInfo = set_functor_info(!.ModuleInfo, Norm),
     globals.io_lookup_bool_option(propagate_failure_constrs, Fail, !IO),
     globals.io_lookup_bool_option(arg_size_analysis_only, ArgSizeOnly, !IO),
     BuildOptions = term_build_options_init(FunctorInfo, Fail, ArgSizeOnly),
Index: compiler/term_constr_pass2.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_pass2.m,v
retrieving revision 1.11
diff -u -b -r1.11 term_constr_pass2.m
--- compiler/term_constr_pass2.m	21 Jan 2008 00:32:55 -0000	1.11
+++ compiler/term_constr_pass2.m	7 Sep 2009 04:04:31 -0000
@@ -523,7 +523,7 @@
     % NOTE: If you examine the condition it may contain fewer variables
     % than you expect. This is because if the same argument occurs in the head
     % and the call they will cancel each other out.
-    Condition = constraint(Terms, (=<), -one),
+    Condition = construct_constraint(Terms, lp_lt_eq, -one),
     Label = polyhedron.non_false_constraints(Loop ^ tcge_label),
     entailed(SizeVarSet, Label, Condition).
 
@@ -630,9 +630,9 @@
     = constraint.
 
 subst_size_var_eqn(Map, Eqn0) = Eqn :-
-    constraint(Eqn0, Coeffs0, Operator, Constant),
+    deconstruct_constraint(Eqn0, Coeffs0, Operator, Constant),
     Coeffs = list.map(subst_size_var_coeff(Map), Coeffs0),
-    Eqn = constraint(Coeffs, Operator, Constant).
+    Eqn = construct_constraint(Coeffs, Operator, Constant).
 
 :- func subst_size_var_coeff(bimap(size_var, size_var), lp_term) = lp_term.
 
Index: compiler/term_constr_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_util.m,v
retrieving revision 1.14
diff -u -b -r1.14 term_constr_util.m
--- compiler/term_constr_util.m	11 Feb 2008 21:26:10 -0000	1.14
+++ compiler/term_constr_util.m	7 Sep 2009 04:12:38 -0000
@@ -270,9 +270,8 @@
     ProgVars = map.keys(SizeVarMap),
     ZeroProgVars = list.filter(is_zero_size_prog_var(ModuleInfo, VarTypes),
         ProgVars),
-    %
+
     % Build zeros from corresponding size_vars.
-    %
     ZerosList = prog_vars_to_size_vars(SizeVarMap, ZeroProgVars),
     Zeros = set.from_list(ZerosList).
 
@@ -299,11 +298,12 @@
 
 substitute_size_vars(Constraints0, SubstMap) = Constraints :-
     SubVarInCoeff = (func(OldVar - Rat) = NewVar - Rat :-
-        NewVar = SubstMap ^ det_elem(OldVar)
+        map.lookup(SubstMap, OldVar, NewVar)
     ),
-    SubVarInEqn = (func(Constr) = constraint(Coeffs, Op, Rat) :-
-        constraint(Constr, Coeffs0, Op, Rat),
-        Coeffs = list.map(SubVarInCoeff, Coeffs0)
+    SubVarInEqn = (func(Constr0) = Constr :-
+        deconstruct_constraint(Constr0, Coeffs0, Op, Rat),
+        Coeffs = list.map(SubVarInCoeff, Coeffs0),
+        Constr = construct_constraint(Coeffs, Op, Rat)
     ),
     Constraints = list.map(SubVarInEqn, Constraints0).
 
@@ -341,10 +341,11 @@
 make_arg_constraints([], _) = [].
 make_arg_constraints([Var | Vars], Zeros) = Constraints :-
     Constraints0 = make_arg_constraints(Vars, Zeros),
-    ( if    set.member(Var, Zeros)
-      then  Constraints = Constraints0
-      else  Constraints =
-                [ constraint([Var - one], (>=), zero) | Constraints0 ]
+    ( set.member(Var, Zeros) ->
+        Constraints = Constraints0
+    ;
+        NewConstraint = construct_constraint([Var - one], lp_gt_eq, zero),
+        Constraints = [NewConstraint | Constraints0]
     ).
 
 is_zero_size_var(Zeros, SizeVar) :- set.member(SizeVar, Zeros).
Index: compiler/term_norm.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_norm.m,v
retrieving revision 1.30
diff -u -b -r1.30 term_norm.m
--- compiler/term_norm.m	4 Sep 2009 02:27:55 -0000	1.30
+++ compiler/term_norm.m	7 Sep 2009 04:33:50 -0000
@@ -33,7 +33,7 @@
     % This predicate sets the functor_info depending on the value of the
     % termination_norm or termination2_norm option.
     %
-:- func set_functor_info(globals.termination_norm, module_info) = functor_info.
+:- func set_functor_info(module_info, globals.termination_norm) = functor_info.
 
     % This predicate computes the weight of a functor and the set of arguments
     % of that functor whose sizes should be counted towards the size of the
@@ -48,8 +48,8 @@
     % whole lot becomes inconsistent - in the end it's just easier to ignore
     % them).
     %
-:- pred functor_norm(functor_info::in, type_ctor::in, cons_id::in,
-    module_info::in, int::out, list(prog_var)::in, list(prog_var)::out,
+:- pred functor_norm(module_info::in, functor_info::in, type_ctor::in,
+    cons_id::in, int::out, list(prog_var)::in, list(prog_var)::out,
     list(uni_mode)::in, list(uni_mode)::out) is det.
 
     % This function computes a lower bound on the weight of a fuctor.  If the
@@ -58,7 +58,7 @@
     % of the functor.  (And if there were this function wouldn't tell you about
     % it anyhow).
     %
-:- func functor_lower_bound(functor_info, type_ctor, cons_id, module_info)
+:- func functor_lower_bound(module_info, functor_info, type_ctor, cons_id)
     = int.
 
     % Succeeds if all values of the given type are zero size (for all norms).
@@ -241,20 +241,20 @@
 
 %-----------------------------------------------------------------------------%
 
-set_functor_info(norm_total, _ModuleInfo) = total.
-set_functor_info(norm_simple, _ModuleInfo) = simple.
-set_functor_info(norm_num_data_elems, ModuleInfo) =
-        use_map_and_args(WeightMap) :-
-    find_weights(ModuleInfo, WeightMap).
-set_functor_info(norm_size_data_elems, ModuleInfo) = use_map(WeightMap) :-
-    find_weights(ModuleInfo, WeightMap).
+set_functor_info(_ModuleInfo, norm_total) = total.
+set_functor_info(_ModuleInfo, norm_simple) = simple.
+set_functor_info(ModuleInfo, norm_num_data_elems) = FunctorInfo :-
+    find_weights(ModuleInfo, WeightMap),
+    FunctorInfo = use_map_and_args(WeightMap).
+set_functor_info(ModuleInfo, norm_size_data_elems) = FunctorInfo :-
+    find_weights(ModuleInfo, WeightMap),
+    FunctorInfo = use_map(WeightMap).
 
 %-----------------------------------------------------------------------------%
 
-% Although the module info is not used in any of these norms, it could
-% be needed for other norms, so it should not be removed.
-
-functor_norm(FunctorInfo, TypeCtor, ConsId, _ModuleInfo, Int, !Args, !Modes) :-
+functor_norm(_ModuleInfo, FunctorInfo, TypeCtor, ConsId, Int, !Args, !Modes) :-
+    % Although the module info is not used in any of these norms, it could
+    % be needed for other norms, so it should not be removed.
     (
         FunctorInfo = simple,
         (
@@ -283,9 +283,7 @@
         FunctorInfo = use_map_and_args(WeightMap),
         ( search_weight_table(WeightMap, TypeCtor, ConsId, WeightInfo) ->
             WeightInfo = weight(Int, UseArgList),
-            (
-                functor_norm_filter_args(UseArgList, !Args, !Modes)
-            ->
+            ( functor_norm_filter_args(UseArgList, !Args, !Modes) ->
                 true
             ;
                 unexpected(this_file,
@@ -313,7 +311,7 @@
 
 %-----------------------------------------------------------------------------%
 
-functor_lower_bound(FunctorInfo, TypeCtor, ConsId, _ModuleInfo) = Weight :-
+functor_lower_bound(_ModuleInfo, FunctorInfo, TypeCtor, ConsId) = Weight :-
     (
         FunctorInfo = simple,
         Weight = ( if ConsId = cons(_, Arity, _), Arity \= 0 then 1 else 0 )
@@ -336,8 +334,8 @@
 
 %-----------------------------------------------------------------------------%
 
-zero_size_type(Module, Type) :-
-    CtorCat = classify_type(Module, Type),
+zero_size_type(ModuleInfo, Type) :-
+    CtorCat = classify_type(ModuleInfo, Type),
     zero_size_type_category(CtorCat, yes).
 
 :- pred zero_size_type_category(type_ctor_category::in, bool::out) is det.
Index: compiler/term_traversal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_traversal.m,v
retrieving revision 1.61
diff -u -b -r1.61 term_traversal.m
--- compiler/term_traversal.m	23 Dec 2008 01:37:41 -0000	1.61
+++ compiler/term_traversal.m	7 Sep 2009 05:05:17 -0000
@@ -532,8 +532,8 @@
     \+ type_is_higher_order(Type),
     type_to_ctor_and_args_det(Type, TypeCtor, _),
     filter_args_and_modes(VarTypes, Args0, Args1, Modes0, Modes1),
-    functor_norm(FunctorInfo, TypeCtor, ConsId, ModuleInfo,
-        Gamma, Args1, Args, Modes1, Modes),
+    functor_norm(ModuleInfo, FunctorInfo, TypeCtor, ConsId, Gamma,
+        Args1, Args, Modes1, Modes),
     split_unification_vars(Args, Modes, ModuleInfo, InVars, OutVars).
 
 :- pred filter_args_and_modes(vartypes::in,
Index: compiler/termination.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/termination.m,v
retrieving revision 1.81
diff -u -b -r1.81 termination.m
--- compiler/termination.m	21 Jul 2008 03:10:14 -0000	1.81
+++ compiler/termination.m	7 Sep 2009 05:05:30 -0000
@@ -91,7 +91,7 @@
 
 analyse_termination_in_module(!ModuleInfo, !IO) :-
     globals.io_get_termination_norm(TermNorm, !IO),
-    FunctorInfo = set_functor_info(TermNorm, !.ModuleInfo),
+    FunctorInfo = set_functor_info(!.ModuleInfo, TermNorm),
     globals.io_lookup_int_option(termination_error_limit, MaxErrors, !IO),
     globals.io_lookup_int_option(termination_path_limit, MaxPaths, !IO),
     PassInfo = pass_info(FunctorInfo, MaxErrors, MaxPaths),
Index: compiler/try_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/try_expand.m,v
retrieving revision 1.7
diff -u -b -r1.7 try_expand.m
--- compiler/try_expand.m	3 Sep 2009 23:57:30 -0000	1.7
+++ compiler/try_expand.m	4 Sep 2009 18:26:51 -0000
@@ -312,7 +312,7 @@
 update_changed_proc(Goal, PredId, ProcId, PredInfo, !.ProcInfo, !ModuleInfo,
         !Specs) :-
     proc_info_set_goal(Goal, !ProcInfo),
-    requantify_proc(!ProcInfo),
+    requantify_proc_general(ordinary_nonlocals_maybe_lambda, !ProcInfo),
     module_info_set_pred_proc_info(PredId, ProcId, PredInfo, !.ProcInfo,
         !ModuleInfo),
 
Index: compiler/tupling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/tupling.m,v
retrieving revision 1.47
diff -u -b -r1.47 tupling.m
--- compiler/tupling.m	23 Dec 2008 01:37:41 -0000	1.47
+++ compiler/tupling.m	4 Sep 2009 18:33:13 -0000
@@ -702,7 +702,7 @@
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo),
-    requantify_proc(!ProcInfo).
+    requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo).
 
 :- pred insert_proc_start_deconstruction(hlds_goal::in, hlds_goal::out,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
@@ -1685,7 +1685,7 @@
             proc_info_set_varset(VarSet, !ProcInfo),
             proc_info_set_vartypes(VarTypes, !ProcInfo),
             proc_info_set_rtti_varmaps(RttiVarMaps, !ProcInfo),
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
                 !ProcInfo, !ModuleInfo),
             module_info_set_pred_proc_info(PredId, ProcId,
@@ -1734,7 +1734,8 @@
             ),
             conj_list_to_goal([ConstructGoal, CallGoal], GoalInfo0, Goal1),
             RequantifyVars = set.from_list([CellVar | Args0]),
-            implicitly_quantify_goal(RequantifyVars, _, Goal1, Goal,
+            implicitly_quantify_goal_general(ordinary_nonlocals_no_lambda,
+                RequantifyVars, _, Goal1, Goal,
                 !VarSet, !VarTypes, !RttiVarMaps)
         ;
             Goal = hlds_goal(GoalExpr0, GoalInfo0)
cvs diff: cannot find compiler/type_util.m
Index: compiler/unify_proc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unify_proc.m,v
retrieving revision 1.208
diff -u -b -r1.208 unify_proc.m
--- compiler/unify_proc.m	4 Sep 2009 02:27:56 -0000	1.208
+++ compiler/unify_proc.m	4 Sep 2009 18:27:03 -0000
@@ -1996,7 +1996,8 @@
     info_get_varset(!.Info, Varset0),
     info_get_types(!.Info, Types0),
     info_get_rtti_varmaps(!.Info, RttiVarMaps0),
-    implicitly_quantify_clause_body(HeadVars, _Warnings, Goal0, Goal,
+    implicitly_quantify_clause_body_general(ordinary_nonlocals_maybe_lambda,
+        HeadVars, _Warnings, Goal0, Goal,
         Varset0, Varset, Types0, Types, RttiVarMaps0, RttiVarMaps),
     info_set_varset(Varset, !Info),
     info_set_types(Types, !Info),
Index: compiler/unneeded_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unneeded_code.m,v
retrieving revision 1.50
diff -u -b -r1.50 unneeded_code.m
--- compiler/unneeded_code.m	3 Sep 2009 23:07:30 -0000	1.50
+++ compiler/unneeded_code.m	4 Sep 2009 18:33:29 -0000
@@ -251,7 +251,8 @@
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps0),
-    implicitly_quantify_clause_body(HeadVars, _Warnings, Goal0, Goal,
+    implicitly_quantify_clause_body_general(ordinary_nonlocals_no_lambda,
+        HeadVars, _Warnings, Goal0, Goal,
         VarSet0, VarSet, VarTypes0, VarTypes, RttiVarMaps0, RttiVarMaps),
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_varset(VarSet, !ProcInfo),
@@ -357,7 +358,8 @@
         proc_info_get_headvars(!.ProcInfo, HeadVars),
         proc_info_get_inst_varset(!.ProcInfo, InstVarSet),
         proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps0),
-        implicitly_quantify_clause_body(HeadVars, _Warnings,
+        implicitly_quantify_clause_body_general(ordinary_nonlocals_no_lambda,
+            HeadVars, _Warnings,
             Goal2, Goal3, VarSet0, VarSet, VarTypes0, VarTypes,
             RttiVarMaps0, RttiVarMaps),
         recompute_instmap_delta(do_not_recompute_atomic_instmap_deltas,
Index: compiler/untupling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/untupling.m,v
retrieving revision 1.33
diff -u -b -r1.33 untupling.m
--- compiler/untupling.m	4 Sep 2009 02:27:57 -0000	1.33
+++ compiler/untupling.m	4 Sep 2009 18:33:39 -0000
@@ -240,7 +240,7 @@
         proc_info_set_goal(Goal, !ProcInfo),
         proc_info_set_varset(VarSet, !ProcInfo),
         proc_info_set_vartypes(VarTypes, !ProcInfo),
-        requantify_proc(!ProcInfo),
+        requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
         recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
             !ProcInfo, !ModuleInfo),
 
@@ -489,7 +489,7 @@
             proc_info_set_goal(Goal, !ProcInfo),
             proc_info_set_varset(VarSet, !ProcInfo),
             proc_info_set_vartypes(VarTypes, !ProcInfo),
-            requantify_proc(!ProcInfo),
+            requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
             recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
                 !ProcInfo, !ModuleInfo),
             module_info_set_pred_proc_info(PredId, ProcId,
Index: compiler/unused_args.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unused_args.m,v
retrieving revision 1.159
diff -u -b -r1.159 unused_args.m
--- compiler/unused_args.m	2 Sep 2009 00:30:23 -0000	1.159
+++ compiler/unused_args.m	4 Sep 2009 18:33:49 -0000
@@ -1214,7 +1214,8 @@
     GoalExpr = plain_call(NewPredId, NewProcId, NewHeadVars,
         not_builtin, no, qualified(PredModule, PredName)),
     Goal1 = hlds_goal(GoalExpr, GoalInfo1),
-    implicitly_quantify_goal(NonLocals, _, Goal1, Goal, VarSet0, VarSet,
+    implicitly_quantify_goal_general(ordinary_nonlocals_no_lambda,
+        NonLocals, _, Goal1, Goal, VarSet0, VarSet,
         VarTypes1, VarTypes, RttiVarMaps0, RttiVarMaps),
     proc_info_set_goal(Goal, !OldProc),
     proc_info_set_varset(VarSet, !OldProc),
@@ -1381,8 +1382,8 @@
             % If anything has changed, rerun quantification.
             set.list_to_set(HeadVars, NonLocals),
             proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps0),
-            implicitly_quantify_goal(NonLocals, _, !Goal,
-                VarSet1, VarSet, VarTypes1, VarTypes,
+            implicitly_quantify_goal_general(ordinary_nonlocals_no_lambda,
+                NonLocals, _, !Goal, VarSet1, VarSet, VarTypes1, VarTypes,
                 RttiVarMaps0, RttiVarMaps),
             proc_info_set_goal(!.Goal, !ProcInfo),
             proc_info_set_varset(VarSet, !ProcInfo),
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing debian/patches
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/concurrency
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_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/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 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/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/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/solver_types
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing ssdb
cvs diff: Diffing tests
cvs diff: Diffing tests/analysis
cvs diff: Diffing tests/analysis/ctgc
cvs diff: Diffing tests/analysis/excp
cvs diff: Diffing tests/analysis/ext
cvs diff: Diffing tests/analysis/sharing
cvs diff: Diffing tests/analysis/table
cvs diff: Diffing tests/analysis/trail
cvs diff: Diffing tests/analysis/unused_args
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/par_conj
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/stm
cvs diff: Diffing tests/stm/orig
cvs diff: Diffing tests/stm/orig/stm-compiler
cvs diff: Diffing tests/stm/orig/stm-compiler/test1
cvs diff: Diffing tests/stm/orig/stm-compiler/test10
cvs diff: Diffing tests/stm/orig/stm-compiler/test2
cvs diff: Diffing tests/stm/orig/stm-compiler/test3
cvs diff: Diffing tests/stm/orig/stm-compiler/test4
cvs diff: Diffing tests/stm/orig/stm-compiler/test5
cvs diff: Diffing tests/stm/orig/stm-compiler/test6
cvs diff: Diffing tests/stm/orig/stm-compiler/test7
cvs diff: Diffing tests/stm/orig/stm-compiler/test8
cvs diff: Diffing tests/stm/orig/stm-compiler/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/stmqueue
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test10
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test11
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test9
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/trailing
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list