[m-rev.] diff: trace goals as scopes

Zoltan Somogyi zs at csse.unimelb.edu.au
Mon Nov 6 18:54:35 AEDT 2006


Treat trace goals as quantifying the variables that occur in their io() and/or
state() components.

compiler/hlds_goal.m:
	Extend trace scopes with a field for recording the set of quantified
	variables.

compiler/add_clause.m:
	Record the list of quantified variables.

compiler/quantification.m:
	Treat the list of quantified variables as for other scopes.

compiler/hlds_out.m:
	Write out the new field.

compiler/mercury_to_mercury.m:
	Reorder the arguments of some predicates to make them easier to curry,
	e.g. for the new code in hlds_out.m.

	Rename some predicates to avoid ambiguities.

compiler/*.m:
	Conform to the changes in hlds_goal.m and/or mercury_to_mercury.m.

tests/hard_coded/trace_goal_3.{m,exp}:
	New test case to test the new functionality.

tests/hard_coded/Mmakefile:
	Enable the new test case.

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/add_clause.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_clause.m,v
retrieving revision 1.37
diff -u -b -r1.37 add_clause.m
--- compiler/add_clause.m	1 Nov 2006 06:32:48 -0000	1.37
+++ compiler/add_clause.m	3 Nov 2006 03:38:07 -0000
@@ -690,8 +690,6 @@
         GetGoals = MutableGetGoals,
         SetGoals = MutableSetGoals
     ),
-    Reason = trace_goal(MaybeCompileTime, MaybeRunTime, MaybeIOHLDS,
-        MutableHLDSs),
     Goal1 = goal_list_to_conj(Context, GetGoals ++ [Goal0] ++ SetGoals),
     BeforeSInfo = !.SInfo,
     substitute_vars(StateVars0, Subst, StateVars),
@@ -699,7 +697,9 @@
     transform_goal(Goal1, Subst, Goal, NumAdded1, !VarSet, !ModuleInfo,
         !QualInfo, !SInfo, !Specs),
     NumAdded = list.length(GetGoals) + NumAdded1 + list.length(SetGoals),
-    finish_local_state_vars(StateVars, _Vars, BeforeSInfo, !SInfo),
+    finish_local_state_vars(StateVars, Vars, BeforeSInfo, !SInfo),
+    Reason = trace_goal(MaybeCompileTime, MaybeRunTime, MaybeIOHLDS,
+        MutableHLDSs, Vars),
     goal_info_init(GoalInfo).
 transform_goal_2(if_then_else_expr(Vars0, StateVars0, Cond0, Then0, Else0),
         Context, Subst, if_then_else(Vars, Cond, Then, Else) - GoalInfo,
@@ -1043,7 +1043,7 @@
         Action = "extraction"
     ),
     GenericVarSet = varset.coerce(VarSet),
-    TermStr = mercury_term_to_string(ErrorTerm, GenericVarSet, no),
+    TermStr = mercury_term_to_string(GenericVarSet, no, ErrorTerm),
     Pieces = [words("In DCG field"), words(Action), words("goal:"), nl,
         words("error:"), words(ErrorMsg), words("at term"),
         quote(TermStr), suffix("."), nl],
Index: compiler/add_pragma.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pragma.m,v
retrieving revision 1.52
diff -u -b -r1.52 add_pragma.m
--- compiler/add_pragma.m	2 Oct 2006 05:21:08 -0000	1.52
+++ compiler/add_pragma.m	23 Oct 2006 01:44:12 -0000
@@ -1080,7 +1080,7 @@
 
 report_variables(SubExistQVars, VarSet) =
     [words(choose_number(SubExistQVars, "variable", "variables")),
-    quote(mercury_vars_to_string(SubExistQVars, VarSet, no))].
+    quote(mercury_vars_to_string(VarSet, no, SubExistQVars))].
 
     % Check that the mode list for a `:- pragma type_spec' declaration
     % specifies a known procedure.
@@ -2519,12 +2519,12 @@
         (
             MultipleArgs = [MultipleArg],
             Pieces2 = [words("error: variable"),
-                quote(mercury_var_to_string(MultipleArg, PVarSet, no)),
+                quote(mercury_var_to_string(PVarSet, no, MultipleArg)),
                 words("occurs multiple times in the argument list.")]
         ;
             MultipleArgs = [_, _ | _],
             Pieces2 = [words("error: variables"),
-                quote(mercury_vars_to_string(MultipleArgs, PVarSet, no)),
+                quote(mercury_vars_to_string(PVarSet, no, MultipleArgs)),
                 words("occur multiple times in the argument list.")]
         ),
         Msg = simple_msg(Context, [always(Pieces1 ++ Pieces2)]),
Index: compiler/check_typeclass.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/check_typeclass.m,v
retrieving revision 1.107
diff -u -b -r1.107 check_typeclass.m
--- compiler/check_typeclass.m	1 Nov 2006 06:32:48 -0000	1.107
+++ compiler/check_typeclass.m	6 Nov 2006 07:46:30 -0000
@@ -1201,7 +1201,6 @@
     ;
         true
     ).
-
 %---------------------------------------------------------------------------%
 
     % Look for pred or func declarations for which the type variables in
@@ -1631,9 +1630,7 @@
     TVarSet = InstanceDefn ^ instance_tvarset,
     Context = InstanceDefn ^ instance_context,
 
-    VarsStrs = list.map((func(Var) = mercury_var_to_string(Var, TVarSet, no)),
-        Vars),
-
+    VarsStrs = list.map(mercury_var_to_string(TVarSet, no), Vars),
     Pieces = [words("In instance for typeclass"),
         sym_name_and_arity(SymName / Arity), suffix(":"), nl,
         words("functional dependency not satisfied:"),
@@ -1662,8 +1659,8 @@
     FunDep = fundep(Domain, Range),
     DomainParams = restrict_list_elements(Domain, Params),
     RangeParams = restrict_list_elements(Range, Params),
-    DomainList = mercury_vars_to_string(DomainParams, TVarSet, no),
-    RangeList = mercury_vars_to_string(RangeParams, TVarSet, no),
+    DomainList = mercury_vars_to_string(TVarSet, no, DomainParams),
+    RangeList = mercury_vars_to_string(TVarSet, no, RangeParams),
     FunDepStr = "`(" ++ DomainList ++ " -> " ++ RangeList ++ ")'",
 
     PiecesA = [words("Inconsistent instance declaration for typeclass"),
@@ -1708,8 +1705,7 @@
     Arity = length(ArgTypes),
     PredOrFunc = pred_info_is_pred_or_func(PredInfo),
 
-    VarsStrs = list.map((func(Var) = mercury_var_to_string(Var, TVarSet, no)),
-        Vars),
+    VarsStrs = list.map(mercury_var_to_string(TVarSet, no), Vars),
 
     Pieces0 = [words("In declaration for"),
         simple_call(simple_call_id(PredOrFunc, SymName, Arity)),
@@ -1741,8 +1737,7 @@
     get_type_defn_tvarset(TypeDefn, TVarSet),
     TypeCtor = type_ctor(SymName, Arity),
 
-    VarsStrs = list.map((func(Var) = mercury_var_to_string(Var, TVarSet, no)),
-        Vars),
+    VarsStrs = list.map(mercury_var_to_string(TVarSet, no), Vars),
 
     Pieces = [words("In declaration for type"),
         sym_name_and_arity(SymName / Arity), suffix(":"), nl,
@@ -1807,8 +1802,7 @@
         [suffix(":")],
     TypeVariables = [words("type variable"),
         suffix(choose_number(TVars, "", "s"))],
-    TVarsStrs = list.map((func(V) = mercury_var_to_string(V, TVarSet, no)),
-        TVars),
+    TVarsStrs = list.map(mercury_var_to_string(TVarSet, no), TVars),
     TVarsPart = list_to_pieces(TVarsStrs),
     Are = words(choose_number(TVars, "is", "are")),
     (
Index: compiler/commit_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/commit_gen.m,v
retrieving revision 1.13
diff -u -b -r1.13 commit_gen.m
--- compiler/commit_gen.m	27 Jul 2006 05:00:59 -0000	1.13
+++ compiler/commit_gen.m	23 Oct 2006 01:31:07 -0000
@@ -47,7 +47,7 @@
 
 generate_scope(Reason, AddTrailOps, OuterCodeModel, Goal, Code, !CI) :-
     (
-        Reason = trace_goal(_, MaybeTraceRuntimeCond, _, _),
+        Reason = trace_goal(_, MaybeTraceRuntimeCond, _, _, _),
         MaybeTraceRuntimeCond = yes(_)
     ->
         % These goals should have been transformed into other forms of goals
Index: compiler/constraint.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/constraint.m,v
retrieving revision 1.82
diff -u -b -r1.82 constraint.m
--- compiler/constraint.m	15 Oct 2006 23:26:36 -0000	1.82
+++ compiler/constraint.m	23 Oct 2006 01:31:12 -0000
@@ -177,7 +177,7 @@
             ; Reason = promise_purity(_, _)
             ; Reason = commit(_)
             ; Reason = barrier(_)
-            ; Reason = trace_goal(_, _, _, _)
+            ; Reason = trace_goal(_, _, _, _, _)
             ),
             % We can't safely propagate constraints into one of these scopes.
             % However, we can propagate constraints inside the scope goal.
Index: compiler/det_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_analysis.m,v
retrieving revision 1.202
diff -u -b -r1.202 det_analysis.m
--- compiler/det_analysis.m	15 Oct 2006 23:26:38 -0000	1.202
+++ compiler/det_analysis.m	23 Oct 2006 01:29:39 -0000
@@ -1444,7 +1444,7 @@
             RightFailingContexts, MaybePromiseEqvSolutionSets, DetInfo, Detism,
             GoalFailingContexts, !Specs)
     ;
-        Reason = trace_goal(_, _, _, _),
+        Reason = trace_goal(_, _, _, _, _),
         det_infer_goal(Goal0, Goal, InstMap0, SolnContext,
             RightFailingContexts, MaybePromiseEqvSolutionSets0, DetInfo,
             Detism, GoalFailingContexts, !Specs),
@@ -1538,7 +1538,7 @@
             proc_info_get_varset(ProcInfo, VarSet),
             (
                 GoalContext = ccuc_switch,
-                VarStr = mercury_var_to_string(Var, VarSet, no),
+                VarStr = mercury_var_to_string(VarSet, no, Var),
                 Pieces0 = [words("In switch on variable"), quote(VarStr),
                     suffix(":"), nl]
             ;
@@ -1578,7 +1578,7 @@
             proc_info_get_varset(ProcInfo, VarSet),
             (
                 GoalContext = ccuc_switch,
-                VarStr = mercury_var_to_string(Var, VarSet, no),
+                VarStr = mercury_var_to_string(VarSet, no, Var),
                 Pieces0 = [words("In switch on variable `" ++ VarStr ++ "':"),
                     nl]
             ;
Index: compiler/det_report.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_report.m,v
retrieving revision 1.131
diff -u -b -r1.131 det_report.m
--- compiler/det_report.m	13 Oct 2006 04:52:17 -0000	1.131
+++ compiler/det_report.m	23 Oct 2006 01:37:01 -0000
@@ -539,7 +539,7 @@
         det_get_proc_info(DetInfo, ProcInfo),
         proc_info_get_varset(ProcInfo, VarSet),
         det_info_get_module_info(DetInfo, ModuleInfo),
-        VarStr = mercury_var_to_string(Var, VarSet, no),
+        VarStr = mercury_var_to_string(VarSet, no, Var),
         (
             det_lookup_var_type(ModuleInfo, ProcInfo, Var, TypeDefn),
             hlds_data.get_type_defn_body(TypeDefn, TypeBody),
@@ -811,7 +811,7 @@
     proc_info_get_varset(ProcInfo, VarSet),
     SwitchContext = switch_context(Var, ConsId),
     ConsIdStr = cons_id_to_string(ConsId),
-    VarStr = mercury_var_to_string(Var, VarSet, no),
+    VarStr = mercury_var_to_string(VarSet, no, Var),
     HeadPieces = [words("Inside the case"), fixed(ConsIdStr),
         words("of the switch on"), fixed(VarStr), words(":")],
     det_diagnose_switch_context(SwitchContexts, DetInfo, TailPieces).
@@ -917,10 +917,10 @@
             \+ varset.search_name(VarSet, RV, _)
         ->
             Pieces = [words(StartWords), words("with"),
-                words(add_quotes(mercury_var_to_string(LHS, VarSet, no)))]
+                words(add_quotes(mercury_var_to_string(VarSet, no, LHS)))]
         ;
             Pieces = [words(StartWords), words("of"),
-                words(add_quotes(mercury_var_to_string(LHS, VarSet, no))),
+                words(add_quotes(mercury_var_to_string(VarSet, no, LHS))),
                 words("and"),
                 words(add_quotes(
                     unify_rhs_to_string(RHS, ModuleInfo, VarSet, no)))]
@@ -942,7 +942,7 @@
     = "arbitrary".
 
 lookup_var_name_in_varset(VarSet, Var) =
-    mercury_var_to_string(Var, VarSet, no).
+    mercury_var_to_string(VarSet, no, Var).
 
 failing_contexts_description(ModuleInfo, VarSet, FailingContexts) =
     list.map(failing_context_description(ModuleInfo, VarSet), FailingContexts).
@@ -953,20 +953,20 @@
 failing_context_description(ModuleInfo, VarSet, Context - FailingGoal) = Msg :-
     (
         FailingGoal = incomplete_switch(Var),
-        VarStr = mercury_var_to_string(Var, VarSet, no),
+        VarStr = mercury_var_to_string(VarSet, no, Var),
         Pieces = [words("Switch on"), fixed(VarStr), words("is incomplete.")]
     ;
         FailingGoal = fail_goal,
         Pieces = [words("Fail goal can fail.")]
     ;
         FailingGoal = test_goal(Var1, Var2),
-        Var1Str = mercury_var_to_string(Var1, VarSet, no),
-        Var2Str = mercury_var_to_string(Var2, VarSet, no),
+        Var1Str = mercury_var_to_string(VarSet, no, Var1),
+        Var2Str = mercury_var_to_string(VarSet, no, Var2),
         Pieces = [words("Unification of"), fixed(Var1Str),
             words("and"), fixed(Var2Str), words("can fail.")]
     ;
         FailingGoal = deconstruct_goal(Var, ConsId),
-        VarStr = mercury_var_to_string(Var, VarSet, no),
+        VarStr = mercury_var_to_string(VarSet, no, Var),
         ConsIdStr = cons_id_to_string(ConsId),
         Pieces = [words("Unification of"), fixed(VarStr),
             words("with"), fixed(ConsIdStr), words("can fail.")]
Index: compiler/goal_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_util.m,v
retrieving revision 1.141
diff -u -b -r1.141 goal_util.m
--- compiler/goal_util.m	3 Nov 2006 08:31:07 -0000	1.141
+++ compiler/goal_util.m	3 Nov 2006 08:45:54 -0000
@@ -585,7 +585,7 @@
         rename_var(Must, Subn, Var0, Var),
         Reason = from_ground_term(Var)
     ;
-        Reason0 = trace_goal(_Flag, _Grade, _Env, _Vars),
+        Reason0 = trace_goal(_Flag, _Grade, _Env, _Vars, _QuantVars),
         Reason = Reason0
     ),
     rename_vars_in_goal(Must, Subn, Goal0, Goal).
@@ -859,7 +859,7 @@
         Reason = from_ground_term(Var),
         set.insert(!.Set, Var, !:Set)
     ;
-        Reason = trace_goal(_, _, _, _)
+        Reason = trace_goal(_, _, _, _, _)
     ),
     goal_vars_2(Goal, !Set).
 
Index: compiler/hlds_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_goal.m,v
retrieving revision 1.169
diff -u -b -r1.169 hlds_goal.m
--- compiler/hlds_goal.m	6 Nov 2006 07:21:40 -0000	1.169
+++ compiler/hlds_goal.m	6 Nov 2006 07:27:33 -0000
@@ -313,7 +313,8 @@
                 trace_compiletime   :: maybe(trace_expr(trace_compiletime)),
                 trace_runtime       :: maybe(trace_expr(trace_runtime)),
                 trace_maybe_io      :: maybe(string),
-                trace_mutable_vars  :: list(trace_mutable_var_hlds)
+                trace_mutable_vars  :: list(trace_mutable_var_hlds),
+                trace_quant_vars    :: list(prog_var)
             ).
             % The goal inside the scope is trace code that is executed only
             % conditionally, and should have no effect on the semantics of
Index: compiler/hlds_out.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out.m,v
retrieving revision 1.410
diff -u -b -r1.410 hlds_out.m
--- compiler/hlds_out.m	1 Nov 2006 06:32:52 -0000	1.410
+++ compiler/hlds_out.m	3 Nov 2006 03:38:07 -0000
@@ -889,7 +889,7 @@
             HeadTypeParams = [_ | _],
             io.write_string("% head_type_params:\n", !IO),
             io.write_string("% ", !IO),
-            mercury_output_vars(HeadTypeParams, TVarSet, AppendVarNums, !IO),
+            mercury_output_vars(TVarSet, AppendVarNums, HeadTypeParams, !IO),
             io.write_string("\n", !IO)
         ;
             HeadTypeParams = []
@@ -1195,8 +1195,8 @@
         write_qualified_functor_with_term_args(ModuleName,
             term.atom(PredName), FuncArgs, VarSet, AppendVarNums, !IO),
         io.write_string(" = ", !IO),
-        mercury_output_term(RetVal, VarSet, AppendVarNums,
-            next_to_graphic_token, !IO)
+        mercury_output_term_nq(VarSet, AppendVarNums, next_to_graphic_token,
+            RetVal, !IO)
     ;
         PredOrFunc = predicate,
         write_qualified_functor_with_term_args(ModuleName,
@@ -1256,7 +1256,7 @@
             NonLocalsList = [_ | _],
             write_indent(Indent, !IO),
             io.write_string("% nonlocals: ", !IO),
-            mercury_output_vars(NonLocalsList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, NonLocalsList, !IO),
             io.write_string("\n", !IO)
         ;
             NonLocalsList = []
@@ -1272,7 +1272,7 @@
         ->
             write_indent(Indent, !IO),
             io.write_string("% pre-deaths: ", !IO),
-            mercury_output_vars(PreDeathList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, PreDeathList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1284,7 +1284,7 @@
         ->
             write_indent(Indent, !IO),
             io.write_string("% pre-births: ", !IO),
-            mercury_output_vars(PreBirthList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, PreBirthList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1298,7 +1298,7 @@
             set.to_sorted_list(ProducingVars, ProducingVarsList),
             write_indent(Indent, !IO),
             io.write_string("% producing vars: ", !IO),
-            mercury_output_vars(ProducingVarsList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, ProducingVarsList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1309,7 +1309,7 @@
             set.to_sorted_list(ConsumingVars, ConsumingVarsList),
             write_indent(Indent, !IO),
             io.write_string("% consuming vars: ", !IO),
-            mercury_output_vars(ConsumingVarsList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, ConsumingVarsList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1320,7 +1320,7 @@
             set.to_sorted_list(MakeVisibleVars, MakeVisibleVarsList),
             write_indent(Indent, !IO),
             io.write_string("% make_visible vars: ", !IO),
-            mercury_output_vars(MakeVisibleVarsList, VarSet, AppendVarNums,
+            mercury_output_vars(VarSet, AppendVarNums, MakeVisibleVarsList,
                 !IO),
             io.write_string("\n", !IO)
         ;
@@ -1332,7 +1332,7 @@
             set.to_sorted_list(NeedVisibleVars, NeedVisibleVarsList),
             write_indent(Indent, !IO),
             io.write_string("% need_visible vars: ", !IO),
-            mercury_output_vars(NeedVisibleVarsList, VarSet, AppendVarNums,
+            mercury_output_vars(VarSet, AppendVarNums, NeedVisibleVarsList,
                 !IO),
             io.write_string("\n", !IO)
         ;
@@ -1385,7 +1385,7 @@
         ->
             write_indent(Indent, !IO),
             io.write_string("% post-deaths: ", !IO),
-            mercury_output_vars(PostDeathList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, PostDeathList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1397,7 +1397,7 @@
         ->
             write_indent(Indent, !IO),
             io.write_string("% post-births: ", !IO),
-            mercury_output_vars(PostBirthList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, PostBirthList, !IO),
             io.write_string("\n", !IO)
         ;
             true
@@ -1415,11 +1415,11 @@
         ->
             write_indent(Indent, !IO),
             io.write_string("% LFU: ", !IO),
-            mercury_output_vars(ListLFU, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, ListLFU, !IO),
             io.write_string("\n", !IO),
             write_indent(Indent, !IO),
             io.write_string("% LBU: ", !IO),
-            mercury_output_vars(ListLBU, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, ListLBU, !IO),
             io.write_string("\n", !IO),
 
             write_indent(Indent, !IO),
@@ -1486,7 +1486,7 @@
     io.write_string("( % ", !IO),
     write_can_fail(CanFail, !IO),
     io.write_string(" switch on `", !IO),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string("'\n", !IO),
     Indent1 = Indent + 1,
     (
@@ -1510,7 +1510,7 @@
     (
         Reason = exist_quant(Vars),
         io.write_string("some [", !IO),
-        mercury_output_vars(Vars, VarSet, AppendVarNums, !IO),
+        mercury_output_vars(VarSet, AppendVarNums, Vars, !IO),
         io.write_string("] (\n", !IO)
     ;
         Reason = promise_purity(Implicit, Purity),
@@ -1544,7 +1544,7 @@
             io.write_string("arbitrary", !IO)
         ),
         io.write_string(" [", !IO),
-        mercury_output_vars(Vars, VarSet, AppendVarNums, !IO),
+        mercury_output_vars(VarSet, AppendVarNums, Vars, !IO),
         io.write_string("] (\n", !IO)
     ;
         Reason = barrier(removable),
@@ -1571,11 +1571,11 @@
         io.write_string("(\n", !IO),
         write_indent(Indent, !IO),
         io.write_string("% from_ground_term [", !IO),
-        mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+        mercury_output_var(VarSet, AppendVarNums, Var, !IO),
         io.write_string("]\n", !IO)
     ;
         Reason = trace_goal(MaybeCompileTime, MaybeRunTime, MaybeIO,
-            MutableVars),
+            MutableVars, QuantVars),
         io.write_string("(\n", !IO),
         write_indent(Indent + 1, !IO),
         io.write_string("% trace\n", !IO),
@@ -1606,7 +1606,11 @@
         ;
             MaybeIO = no
         ),
-        list.foldl(write_trace_mutable_var_hlds(Indent + 1), MutableVars, !IO)
+        list.foldl(write_trace_mutable_var_hlds(Indent + 1), MutableVars, !IO),
+        write_indent(Indent + 1, !IO),
+        io.write_string("% quantified vars ", !IO),
+        list.foldl(mercury_output_var(VarSet, AppendVarNums), QuantVars, !IO),
+        io.nl(!IO)
     ),
     write_goal_a(Goal, ModuleInfo, VarSet, AppendVarNums, Indent + 1, "\n",
         TypeQual, !IO),
@@ -1746,7 +1750,7 @@
                 FuncArgVars, FuncRetVar),
             write_indent(Indent, !IO),
             write_purity_prefix(Purity, !IO),
-            mercury_output_var(FuncRetVar, VarSet, AppendVarNums, !IO),
+            mercury_output_var(VarSet, AppendVarNums, FuncRetVar, !IO),
             io.write_string(" = ", !IO),
             write_functor(term.atom("apply"), FuncArgVars, VarSet,
                 AppendVarNums, !IO)
@@ -1769,7 +1773,7 @@
         Term = term.functor(Functor, [TCInfoTerm, MethodNumTerm | ArgTerms],
             Context),
         write_indent(Indent, !IO),
-        mercury_output_term(Term, VarSet, AppendVarNums, !IO),
+        mercury_output_term(VarSet, AppendVarNums, Term, !IO),
         io.write_string(Follow, !IO)
     ;
         GenericCall = event_call(EventName),
@@ -1785,7 +1789,7 @@
         term.var_list_to_term_list(ArgVars, ArgTerms),
         Term = term.functor(Functor, ArgTerms, Context),
         write_indent(Indent, !IO),
-        mercury_output_term(Term, VarSet, AppendVarNums, !IO),
+        mercury_output_term(VarSet, AppendVarNums, Term, !IO),
         io.write_string(Follow, !IO)
     ;
         GenericCall = cast(CastType),
@@ -1811,7 +1815,7 @@
         term.context_init(Context),
         Term = term.functor(Functor, ArgTerms, Context),
         write_indent(Indent, !IO),
-        mercury_output_term(Term, VarSet, AppendVarNums, !IO),
+        mercury_output_term(VarSet, AppendVarNums, Term, !IO),
         io.write_string(Follow, !IO)
     ).
 
@@ -1850,7 +1854,7 @@
     ;
         PredOrFunc = function,
         pred_args_to_func_args(ArgVars, NewArgVars, LHSVar),
-        mercury_output_var(LHSVar, VarSet, AppendVarNums, !IO),
+        mercury_output_var(VarSet, AppendVarNums, LHSVar, !IO),
         io.write_string(" = ", !IO)
     ),
     write_sym_name_and_args(PredName, NewArgVars, VarSet, AppendVarNums, !IO),
@@ -1877,7 +1881,7 @@
             CallUnifyContext = call_unify_context(Var, RHS, _UnifyContext),
             write_indent(Indent, !IO),
             io.write_string("% unify context: ", !IO),
-            mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+            mercury_output_var(VarSet, AppendVarNums, Var, !IO),
             io.write_string(" = ", !IO),
             % XXX Fake the inst varset.
             varset.init(InstVarSet),
@@ -1893,7 +1897,7 @@
 write_goal_2(unify(A, B, _, Unification, _), ModuleInfo, VarSet, AppendVarNums,
         Indent, Follow, TypeQual, !IO) :-
     write_indent(Indent, !IO),
-    mercury_output_var(A, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, A, !IO),
     io.write_string(" = ", !IO),
     (
         TypeQual = varset_vartypes(_, VarTypes),
@@ -2052,7 +2056,7 @@
 write_foreign_args([], _, _, _, !IO).
 write_foreign_args([Arg | Args], VarSet, TVarSet, AppendVarNums, !IO) :-
     Arg = foreign_arg(Var, MaybeNameMode, Type, BoxPolicy),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     (
         MaybeNameMode = yes(Name - Mode),
         io.write_string("/" ++ Name ++ "(", !IO),
@@ -2127,7 +2131,7 @@
                 Locs = resume_locs_stack_and_orig,
                 io.write_string("stack and orig ", !IO)
             ),
-            mercury_output_vars(ResumeVarList, VarSet, AppendVarNums, !IO),
+            mercury_output_vars(VarSet, AppendVarNums, ResumeVarList, !IO),
             io.write_string("\n", !IO)
         )
     ;
@@ -2163,7 +2167,7 @@
             io.write_string("none\n", !IO)
         ;
             CallForwardList = [_ | _],
-            write_vars(CallForwardList, VarSet, AppendVarNums, !IO),
+            write_vars(VarSet, AppendVarNums, CallForwardList, !IO),
             io.write_string("\n", !IO)
         ),
 
@@ -2174,7 +2178,7 @@
             io.write_string("none\n", !IO)
         ;
             CallResumeList = [_ | _],
-            write_vars(CallResumeList, VarSet, AppendVarNums, !IO),
+            write_vars(VarSet, AppendVarNums, CallResumeList, !IO),
             io.write_string("\n", !IO)
         ),
 
@@ -2185,7 +2189,7 @@
             io.write_string("none\n", !IO)
         ;
             CallNondetList = [_ | _],
-            write_vars(CallNondetList, VarSet, AppendVarNums, !IO),
+            write_vars(VarSet, AppendVarNums, CallNondetList, !IO),
             io.write_string("\n", !IO)
         )
     ;
@@ -2216,7 +2220,7 @@
             io.write_string("none\n", !IO)
         ;
             ResumeResumeList = [_ | _],
-            write_vars(ResumeResumeList, VarSet, AppendVarNums, !IO),
+            write_vars(VarSet, AppendVarNums, ResumeResumeList, !IO),
             io.write_string("\n", !IO)
         ),
 
@@ -2227,7 +2231,7 @@
             io.write_string("none\n", !IO)
         ;
             ResumeNondetList = [_ | _],
-            write_vars(ResumeNondetList, VarSet, AppendVarNums, !IO),
+            write_vars(VarSet, AppendVarNums, ResumeNondetList, !IO),
             io.write_string("\n", !IO)
         )
     ;
@@ -2242,22 +2246,22 @@
         set.to_sorted_list(ParConjSet, ParConjList),
         write_indent(Indent, !IO),
         io.write_string("% need in par_conj vars: ", !IO),
-        write_vars(ParConjList, VarSet, AppendVarNums, !IO),
+        write_vars(VarSet, AppendVarNums, ParConjList, !IO),
         io.write_string("\n", !IO)
     ;
         true
     ).
 
-:- pred write_vars(list(prog_var)::in, prog_varset::in, bool::in,
+:- pred write_vars(prog_varset::in, bool::in, list(prog_var)::in,
     io::di, io::uo) is det.
 
-write_vars([], _, _, !IO).
-write_vars([Var], VarSet, AppendVarNums, !IO) :-
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO).
-write_vars([Var1, Var2 | Vars], VarSet, AppendVarNums, !IO) :-
-    mercury_output_var(Var1, VarSet, AppendVarNums, !IO),
+write_vars(_, _, [], !IO).
+write_vars(VarSet, AppendVarNums, [Var], !IO) :-
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO).
+write_vars(VarSet, AppendVarNums, [Var1, Var2 | Vars], !IO) :-
+    mercury_output_var(VarSet, AppendVarNums, Var1, !IO),
     io.write_string(", ", !IO),
-    write_vars([Var2 | Vars], VarSet, AppendVarNums, !IO).
+    write_vars(VarSet, AppendVarNums, [Var2 | Vars], !IO).
 
 :- pred write_varnum_list(list(prog_var)::in, io::di, io::uo) is det.
 
@@ -2298,18 +2302,18 @@
         Indent, !IO) :-
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(X, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, X, !IO),
     io.write_string(" := ", !IO),
-    mercury_output_var(Y, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, Y, !IO),
     io.write_string("\n", !IO).
 
 write_unification(simple_test(X, Y), _, ProgVarSet, _, AppendVarNums, Indent,
         !IO) :-
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(X, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, X, !IO),
     io.write_string(" == ", !IO),
-    mercury_output_var(Y, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, Y, !IO),
     io.write_string("\n", !IO).
 
 write_unification(construct(Var, ConsId, ArgVars, ArgModes, ConstructHow,
@@ -2317,7 +2321,7 @@
         AppendVarNums, Indent, !IO) :-
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(Var, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, Var, !IO),
     io.write_string(" := ", !IO),
     write_functor_and_submodes(ConsId, ArgVars, ArgModes, ModuleInfo,
         ProgVarSet, InstVarSet, AppendVarNums, Indent, !IO),
@@ -2353,7 +2357,7 @@
             ;
                 SizeSource = dynamic_size(SizeVar),
                 io.write_string("var ", !IO),
-                mercury_output_var(SizeVar, ProgVarSet, AppendVarNums, !IO),
+                mercury_output_var(ProgVarSet, AppendVarNums, SizeVar, !IO),
                 io.write_string("\n", !IO)
             )
         ;
@@ -2373,7 +2377,7 @@
         CellToReuse = cell_to_reuse(ReuseVar, _ReuseConsIds, _FieldAssigns),
         write_indent(Indent, !IO),
         io.write_string("% reuse cell: ", !IO),
-        mercury_output_var(ReuseVar, ProgVarSet, AppendVarNums, !IO),
+        mercury_output_var(ProgVarSet, AppendVarNums, ReuseVar, !IO),
         io.write_string("\n", !IO)
     ).
 
@@ -2390,7 +2394,7 @@
     ),
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(Var, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_var(ProgVarSet, AppendVarNums, Var, !IO),
     (
         CanFail = can_fail,
         io.write_string(" ?= ", !IO)
@@ -2417,7 +2421,7 @@
     io.write_string("\n", !IO),
     write_indent(Indent, !IO),
     io.write_string("% type-info vars: ", !IO),
-    mercury_output_vars(TypeInfoVars, ProgVarSet, AppendVarNums, !IO),
+    mercury_output_vars(ProgVarSet, AppendVarNums, TypeInfoVars, !IO),
     io.write_string("\n", !IO).
 
 :- pred write_static_cons(int::in, int::in, prog_varset::in, bool::in,
@@ -2437,7 +2441,7 @@
         write_indent(Indent, !IO),
         io.write_string("% ", !IO),
         write_indent(Depth, !IO),
-        mercury_output_vars(ArgVars, VarSet, AppendVarNums, !IO),
+        mercury_output_vars(VarSet, AppendVarNums, ArgVars, !IO),
         io.write_string("\n", !IO)
     ),
     list.foldl(write_static_cons(Indent, Depth + 1, VarSet, AppendVarNums),
@@ -2456,7 +2460,7 @@
     ;
         ArgVars = [_ | _],
         io.write_string(" (", !IO),
-        mercury_output_vars(ArgVars, ProgVarSet, AppendVarNums, !IO),
+        mercury_output_vars(ProgVarSet, AppendVarNums, ArgVars, !IO),
         io.write_string(")\n", !IO),
         globals.io_lookup_string_option(dump_hlds_options, Verbose, !IO),
         ( string.contains_char(Verbose, 'a') ->
@@ -2489,7 +2493,7 @@
     maybe_vartypes::in, io::di, io::uo) is det.
 
 write_unify_rhs_3(rhs_var(Var), _, VarSet, _, AppendVarNums, _, _, _, !IO) :-
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO).
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO).
 write_unify_rhs_3(rhs_functor(ConsId0, IsExistConstruct, ArgVars), ModuleInfo,
         VarSet, _, AppendVarNums, _Indent, MaybeType, TypeQual, !IO) :-
     (
@@ -2578,7 +2582,7 @@
             NonLocals = [_ | _],
             write_indent(Indent1, !IO),
             io.write_string("% lambda nonlocals: ", !IO),
-            mercury_output_vars(NonLocals, VarSet, AppendVarNums, !IO)
+            mercury_output_vars(VarSet, AppendVarNums, NonLocals, !IO)
         ;
             NonLocals = []
         )
@@ -2587,7 +2591,7 @@
     ).
 
 unify_rhs_to_string(rhs_var(Var), _ModuleInfo, VarSet, AppendVarNums)
-    = mercury_var_to_string(Var, VarSet, AppendVarNums).
+    = mercury_var_to_string(VarSet, AppendVarNums, Var).
 unify_rhs_to_string(rhs_functor(ConsId0, IsExistConstruct, ArgVars),
         ModuleInfo, VarSet, AppendVarNums) = Str :-
     (
@@ -2642,8 +2646,8 @@
     term.context_init(Context),
     term.var_list_to_term_list(ArgVars, ArgTerms),
     Term = term.functor(Functor, ArgTerms, Context),
-    Str = mercury_term_to_string(Term, VarSet, AppendVarNums,
-        NextToGraphicToken).
+    Str = mercury_term_nq_to_string(VarSet, AppendVarNums, NextToGraphicToken,
+        Term).
 
 :- pred write_qualified_functor(module_name::in, const::in,
     list(prog_var)::in, prog_varset::in, bool::in, io::di, io::uo) is det.
@@ -2678,8 +2682,9 @@
         VarSet, AppendVarNums) = Str :-
     ModuleNameStr = mercury_bracketed_sym_name_to_string(ModuleName),
     term.context_init(Context),
-    TermStr = mercury_term_to_string(term.functor(Functor, ArgTerms, Context),
-        VarSet, AppendVarNums, next_to_graphic_token),
+    Term = term.functor(Functor, ArgTerms, Context),
+    TermStr = mercury_term_nq_to_string(VarSet, AppendVarNums,
+        next_to_graphic_token, Term),
     Str = ModuleNameStr ++ "." ++ TermStr.
 
 write_functor_cons_id(ConsId, ArgVars, VarSet, ModuleInfo, AppendVarNums,
@@ -2786,7 +2791,7 @@
     pair(prog_var, mer_mode)) = string.
 
 var_mode_to_string(VarSet, InstVarSet, AppendVarNums, Var - Mode) =
-    mercury_var_to_string(Var, VarSet, AppendVarNums)
+    mercury_var_to_string(VarSet, AppendVarNums, Var)
         ++ "::" ++ mercury_mode_to_string(Mode, InstVarSet).
 
 :- pred write_conj(hlds_goal::in, list(hlds_goal)::in,
@@ -2838,7 +2843,7 @@
         VarTypes, !IO) :-
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string(" has functor ", !IO),
     write_cons_id(ConsId, !IO),
     io.write_string("\n", !IO),
@@ -2886,7 +2891,7 @@
 
 write_instmap_2([], _, _, _, !IO).
 write_instmap_2([Var - Inst | Rest], VarSet, AppendVarNums, Indent, !IO) :-
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string(" -> ", !IO),
     varset.init(InstVarSet),
     mercury_output_inst(Inst, InstVarSet, !IO),
@@ -2919,7 +2924,7 @@
     ;
         instmap_delta_to_assoc_list(InstMapDelta, AssocList),
         assoc_list.keys(AssocList, Vars),
-        write_vars(Vars, VarSet, AppendVarNums, !IO)
+        write_vars(VarSet, AppendVarNums, Vars, !IO)
     ).
 
 write_import_status(Status, !IO) :-
@@ -2987,7 +2992,7 @@
     map.lookup(VarTypes, Var, Type),
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string(" (number ", !IO),
     term.var_to_int(Var, VarNum),
     io.write_int(VarNum, !IO),
@@ -3026,7 +3031,7 @@
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
 
-    mercury_output_var(TVar, TVarSet, AppendVarNums, !IO),
+    mercury_output_var(TVarSet, AppendVarNums, TVar, !IO),
     io.write_string(" (number ", !IO),
     term.var_to_int(TVar, TVarNum),
     io.write_int(TVarNum, !IO),
@@ -3037,12 +3042,12 @@
     (
         Locn = type_info(Var),
         io.write_string("type_info(", !IO),
-        mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+        mercury_output_var(VarSet, AppendVarNums, Var, !IO),
         io.write_string(") ", !IO)
     ;
         Locn = typeclass_info(Var, Index),
         io.write_string("typeclass_info(", !IO),
-        mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+        mercury_output_var(VarSet, AppendVarNums, Var, !IO),
         io.write_string(", ", !IO),
         io.write_int(Index, !IO),
         io.write_string(") ", !IO)
@@ -3063,7 +3068,7 @@
     mercury_output_constraint(TVarSet, AppendVarNums, Constraint, !IO),
     io.write_string(" -> ", !IO),
     rtti_lookup_typeclass_info_var(RttiVarMaps, Constraint, Var),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.nl(!IO).
 
 :- pred write_rtti_var_info(int::in, bool::in, rtti_varmaps::in,
@@ -3073,7 +3078,7 @@
         !IO) :-
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string(" (number ", !IO),
     term.var_to_int(Var, VarNum),
     io.write_int(VarNum, !IO),
@@ -3111,7 +3116,7 @@
         Indent, !IO) :-
     write_indent(Indent, !IO),
     io.write_string("%\t", !IO),
-    mercury_output_var(Var, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, Var, !IO),
     io.write_string("\t-> ", !IO),
     io.write_string(abs_locn_to_string(Loc), !IO),
     io.write_string("\n", !IO),
@@ -3133,9 +3138,9 @@
 write_untuple_info_2(VarSet, AppendVarNums, Indent, OldVar, NewVars, !IO) :-
     write_indent(Indent, !IO),
     io.write_string("%\t", !IO),
-    mercury_output_var(OldVar, VarSet, AppendVarNums, !IO),
+    mercury_output_var(VarSet, AppendVarNums, OldVar, !IO),
     io.write_string("\t-> ", !IO),
-    mercury_output_vars(NewVars, VarSet, AppendVarNums, !IO),
+    mercury_output_vars(VarSet, AppendVarNums, NewVars, !IO),
     io.nl(!IO).
 
 %-----------------------------------------------------------------------------%
@@ -3212,12 +3217,12 @@
 write_type_params(_TVarSet, [], !IO).
 write_type_params(TVarSet, [P], !IO) :-
     io.write_string("(", !IO),
-    mercury_output_var(P, TVarSet, no, !IO),
+    mercury_output_var(TVarSet, no, P, !IO),
     io.write_string(")", !IO).
 write_type_params(TVarSet, [P | Ps], !IO) :-
     Ps = [_ | _],
     io.write_string("(", !IO),
-    mercury_output_var(P, TVarSet, no, !IO),
+    mercury_output_var(TVarSet, no, P, !IO),
     write_type_params_2(TVarSet, Ps, !IO).
 
 :- pred write_type_params_2(tvarset::in, list(type_param)::in,
@@ -3227,7 +3232,7 @@
     io.write_string(")", !IO).
 write_type_params_2(TVarSet, [P | Ps], !IO) :-
     io.write_string(", ", !IO),
-    mercury_output_var(P, TVarSet, no, !IO),
+    mercury_output_var(TVarSet, no, P, !IO),
     write_type_params_2(TVarSet, Ps, !IO).
 
 :- pred write_type_body(int::in, tvarset::in, hlds_type_body::in,
@@ -3376,7 +3381,7 @@
 
     write_indent(Indent, !IO),
     io.write_string("% Vars: ", !IO),
-    mercury_output_vars(Vars, VarSet, AppendVarNums, !IO),
+    mercury_output_vars(VarSet, AppendVarNums, Vars, !IO),
     io.nl(!IO),
 
     write_indent(Indent, !IO),
@@ -3652,7 +3657,7 @@
     (
         MaybeCallTableTip = yes(CallTableTip),
         io.write_string("% call table tip: ", !IO),
-        mercury_output_var(CallTableTip, VarSet, AppendVarNums, !IO),
+        mercury_output_var(VarSet, AppendVarNums, CallTableTip, !IO),
         io.write_string("\n", !IO)
     ;
         MaybeCallTableTip = no
@@ -3690,13 +3695,13 @@
                 MaybeOldOutermost),
             io.write_string("% deep layout info: ", !IO),
             io.write_string("TopCSD is ", !IO),
-            mercury_output_var(TopCSD, VarSet, AppendVarNums, !IO),
+            mercury_output_var(VarSet, AppendVarNums, TopCSD, !IO),
             io.write_string(", MiddleCSD is ", !IO),
-            mercury_output_var(MiddleCSD, VarSet, AppendVarNums, !IO),
+            mercury_output_var(VarSet, AppendVarNums, MiddleCSD, !IO),
             (
                 MaybeOldOutermost = yes(OldOutermost),
                 io.write_string(", OldOutermost is ", !IO),
-                mercury_output_var(OldOutermost, VarSet, AppendVarNums, !IO)
+                mercury_output_var(VarSet, AppendVarNums, OldOutermost, !IO)
             ;
                 MaybeOldOutermost = no
             ),
@@ -4189,7 +4194,7 @@
     ;
         ShortDescription = cell_reused(Var, IsConditional, _, _),
         io.write_string("cell reuse - ", !IO),
-        mercury_output_var(Var, VarSet, AppendVarnums, !IO),
+        mercury_output_var(VarSet, AppendVarnums, Var, !IO),
         io.write_string(" - ", !IO), 
         write_is_conditional(IsConditional, !IO)
     ;
Index: compiler/interval.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/interval.m,v
retrieving revision 1.24
diff -u -b -r1.24 interval.m
--- compiler/interval.m	15 Oct 2006 23:26:41 -0000	1.24
+++ compiler/interval.m	23 Oct 2006 01:27:32 -0000
@@ -929,7 +929,7 @@
         rename_var(no, !.VarRename, Var0, Var),
         Reason = from_ground_term(Var)
     ;
-        Reason0 = trace_goal(_, _, _, _),
+        Reason0 = trace_goal(_, _, _, _, _),
         Reason = Reason0
     ),
     record_decisions_in_goal(SubGoal0, SubGoal, !VarInfo, !VarRename,
Index: compiler/make_hlds_warn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/make_hlds_warn.m,v
retrieving revision 1.19
diff -u -b -r1.19 make_hlds_warn.m
--- compiler/make_hlds_warn.m	12 Sep 2006 04:41:39 -0000	1.19
+++ compiler/make_hlds_warn.m	23 Oct 2006 04:29:46 -0000
@@ -101,11 +101,11 @@
         [words("In clause for"), simple_call(PredCallId), suffix(":"), nl],
     ( Vars = [Var] ->
         Pieces2 = [words("warning: variable"),
-            quote(mercury_var_to_string(Var, VarSet, no)),
+            quote(mercury_var_to_string(VarSet, no, Var)),
             words("has overlapping scopes.")]
     ;
         Pieces2 = [words("warning: variables"),
-            quote(mercury_vars_to_string(Vars, VarSet, no)),
+            quote(mercury_vars_to_string(VarSet, no, Vars)),
             words("each have overlapping scopes.")]
     ),
     Msg = simple_msg(Context,
@@ -329,7 +329,7 @@
         SinglesPreamble = [words("In clause for"),
             simple_call(PredOrFuncCallId), suffix(":"), nl],
         SingleVarsPiece =
-            quote(mercury_vars_to_string(SingletonVars, VarSet, no)),
+            quote(mercury_vars_to_string(VarSet, no, SingletonVars)),
         ( SingletonVars = [_] ->
             SinglesPieces = [words("warning: variable"), SingleVarsPiece,
                 words("occurs only once in this scope."), nl]
@@ -359,7 +359,7 @@
         MultiVars = [_ | _],
         MultiPreamble = [words("In clause for"),
             simple_call(PredOrFuncCallId), suffix(":"), nl],
-        MultiVarsPiece = quote(mercury_vars_to_string(MultiVars, VarSet, no)),
+        MultiVarsPiece = quote(mercury_vars_to_string(VarSet, no, MultiVars)),
         ( MultiVars = [_] ->
             MultiPieces = [words("warning: variable"), MultiVarsPiece,
                 words("occurs more than once in this scope."), nl]
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.307
diff -u -b -r1.307 mercury_to_mercury.m
--- compiler/mercury_to_mercury.m	1 Nov 2006 06:32:57 -0000	1.307
+++ compiler/mercury_to_mercury.m	3 Nov 2006 08:45:42 -0000
@@ -242,27 +242,27 @@
     % the variable number appears in the variable name if the boolean
     % argument is set to `yes'.
     %
-:- pred mercury_output_vars(list(var(T))::in, varset(T)::in, bool::in,
+:- pred mercury_output_vars(varset(T)::in, bool::in, list(var(T))::in,
     io::di, io::uo) is det.
-:- func mercury_vars_to_string(list(var(T)), varset(T), bool) = string.
+:- func mercury_vars_to_string(varset(T), bool, list(var(T))) = string.
 
     % Output a variable, making sure that the variable number appears
     % in the variable name if the boolean argument is set to `yes'.
     %
-:- pred mercury_output_var(var(T)::in, varset(T)::in, bool::in,
+:- pred mercury_output_var(varset(T)::in, bool::in, var(T)::in,
     io::di, io::uo) is det.
-:- func mercury_var_to_string(var(T), varset(T), bool) = string.
+:- func mercury_var_to_string(varset(T), bool, var(T)) = string.
 
     % Output a term, making sure that the variable number appears
     % in variable names if the boolean argument is set to `yes'.
     %
-:- pred mercury_output_term(term(T)::in, varset(T)::in, bool::in,
+:- pred mercury_output_term(varset(T)::in, bool::in, term(T)::in,
     io::di, io::uo) is det.
-:- func mercury_term_to_string(term(T), varset(T), bool) = string.
+:- func mercury_term_to_string(varset(T), bool, term(T)) = string.
 
-:- pred mercury_output_term(term(T)::in, varset(T)::in, bool::in,
-    needs_quotes::in, io::di, io::uo) is det.
-:- func mercury_term_to_string(term(T), varset(T), bool, needs_quotes)
+:- pred mercury_output_term_nq(varset(T)::in, bool::in, needs_quotes::in,
+    term(T)::in, io::di, io::uo) is det.
+:- func mercury_term_nq_to_string(varset(T), bool, needs_quotes, term(T))
     = string.
 
 :- pred mercury_output_type(tvarset::in, bool::in, mer_type::in,
@@ -778,7 +778,7 @@
         % of the declaration
         io.write_string(":- all [", !IO),
         AppendVarNum = no,
-        mercury_output_vars(UnivVars, VarSet, AppendVarNum, !IO),
+        mercury_output_vars(VarSet, AppendVarNum, UnivVars, !IO),
         io.write_string("]", !IO),
         mercury_output_newline(Indent, !IO),
         prog_out.write_promise_type(PromiseType, !IO),
@@ -861,7 +861,7 @@
     % See the comments for prog_io.read_mutable_decl for the reason we
     % _must_ use MutVarset here.
     %
-    mercury_output_term(InitTerm, MutVarset, no, !IO),
+    mercury_output_term(MutVarset, no, InitTerm, !IO),
     io.write_string(", ", !IO),
     mercury_output_inst(Inst, varset.init, !IO),
     io.write_string(", ", !IO),
@@ -1068,13 +1068,13 @@
     io.write_string(":- inst (", !IO),
     ArgTerms = list.map(func(V) = variable(V, Context), Args),
     construct_qualified_term(Name, ArgTerms, Context, InstTerm),
-    mercury_output_term(InstTerm, VarSet, no, !IO),
+    mercury_output_term(VarSet, no, InstTerm, !IO),
     io.write_string(").\n", !IO).
 mercury_output_inst_defn(VarSet, Name, Args, eqv_inst(Body), Context, !IO) :-
     io.write_string(":- inst (", !IO),
     ArgTerms = list.map(func(V) = variable(V, Context), Args),
     construct_qualified_term(Name, ArgTerms, Context, InstTerm),
-    mercury_output_term(InstTerm, VarSet, no, !IO),
+    mercury_output_term(VarSet, no, InstTerm, !IO),
     io.write_string(") == ", !IO),
     mercury_output_inst(Body, VarSet, !IO),
     io.write_string(".\n", !IO).
@@ -1650,7 +1650,7 @@
     add_string(":- mode (", !U),
     ArgTerms = list.map(func(V) = variable(V, Context), Args),
     construct_qualified_term(Name, ArgTerms, Context, ModeTerm),
-    mercury_format_term(ModeTerm, VarSet, no, !U),
+    mercury_format_term(VarSet, no, ModeTerm, !U),
     add_string(") == ", !U),
     mercury_format_mode(Mode, simple_inst_info(VarSet), !U),
     add_string(".\n", !U).
@@ -1722,7 +1722,7 @@
     mercury_output_begin_type_decl(IsSolverType, !IO),
     Args = list.map((func(V) = term.variable(V, Context)), TParams),
     construct_qualified_term(Name, Args, Context, TypeTerm),
-    mercury_output_term(TypeTerm, TVarSet, no, next_to_graphic_token, !IO),
+    mercury_output_term_nq(TVarSet, no, next_to_graphic_token, TypeTerm, !IO),
     io.write_string(".\n", !IO).
 
 mercury_output_type_defn(TVarSet, Name, TParams, parse_tree_eqv_type(Body),
@@ -1730,7 +1730,7 @@
     mercury_output_begin_type_decl(non_solver_type, !IO),
     Args = list.map((func(V) = term.variable(V, Context)), TParams),
     construct_qualified_term(Name, Args, Context, TypeTerm),
-    mercury_output_term(TypeTerm, TVarSet, no, !IO),
+    mercury_output_term(TVarSet, no, TypeTerm, !IO),
     io.write_string(" == ", !IO),
     mercury_output_type(TVarSet, no, Body, !IO),
     io.write_string(".\n", !IO).
@@ -1740,7 +1740,7 @@
     mercury_output_begin_type_decl(non_solver_type, !IO),
     Args = list.map((func(V) = term.variable(V, Context)), TParams),
     construct_qualified_term(Name, Args, Context, TypeTerm),
-    mercury_output_term(TypeTerm, TVarSet, no, !IO),
+    mercury_output_term(TVarSet, no, TypeTerm, !IO),
     io.write_string("\n\t--->\t", !IO),
     mercury_output_ctors(Ctors, TVarSet, !IO),
     mercury_output_where_attributes(TVarSet, no, MaybeUserEqComp, !IO),
@@ -1752,7 +1752,7 @@
     mercury_output_begin_type_decl(solver_type, !IO),
     Args = list.map((func(V) = term.variable(V, Context)), TParams),
     construct_qualified_term(Name, Args, Context, TypeTerm),
-    mercury_output_term(TypeTerm, TVarSet, no, !IO),
+    mercury_output_term(TVarSet, no, TypeTerm, !IO),
     mercury_output_where_attributes(TVarSet, yes(SolverTypeDetails),
         MaybeUserEqComp, !IO),
     io.write_string(".\n", !IO).
@@ -1773,7 +1773,7 @@
     ),
     Args = list.map((func(V) = term.variable(V, context_init)), TParams),
     construct_qualified_term(Name, Args, MercuryType),
-    mercury_output_term(MercuryType, TVarSet, no, !IO),
+    mercury_output_term(TVarSet, no, MercuryType, !IO),
     io.write_string(", \"", !IO),
     (
         ForeignType = il(il_type(RefOrVal, ForeignLocStr, ForeignTypeName)),
@@ -2244,7 +2244,7 @@
     ;
         ExistQVars = [_ | _],
         add_string("some [", !U),
-        mercury_format_vars(ExistQVars, VarSet, AppendVarNums, !U),
+        mercury_format_vars(VarSet, AppendVarNums, ExistQVars, !U),
         add_string("] ", !U)
     ).
 
@@ -2377,7 +2377,7 @@
 mercury_format_type(TVarSet, AppendVarNums, Type, !U) :-
     unparse_type(Type, Term),
     VarSet = varset.coerce(TVarSet),
-    mercury_format_term(Term, VarSet, AppendVarNums, !U).
+    mercury_format_term(VarSet, AppendVarNums, Term, !U).
 
 %-----------------------------------------------------------------------------%
 
@@ -2576,8 +2576,8 @@
     (
         Args = [Arg | Args0],
         io.write_string("(", !IO),
-        mercury_format_term(Arg, VarSet, no, !IO),
-        mercury_format_remaining_terms(Args0, VarSet, no, !IO),
+        mercury_format_term(VarSet, no, Arg, !IO),
+        mercury_format_remaining_terms(VarSet, no, Args0, !IO),
         io.write_string(")", !IO)
     ;
         Args = []
@@ -2601,17 +2601,17 @@
     (
         Args = [Arg | Args0],
         io.write_string("(", !IO),
-        mercury_format_term(Arg, VarSet, no, !IO),
-        mercury_format_remaining_terms(Args0, VarSet, no, !IO),
+        mercury_format_term(VarSet, no, Arg, !IO),
+        mercury_format_remaining_terms(VarSet, no, Args0, !IO),
         io.write_string(")", !IO)
     ;
         Args = []
     ),
     io.write_string(" = ", !IO),
     ( Body = true_expr - _Context0 ->
-        mercury_format_term(Result, VarSet, no, next_to_graphic_token, !IO)
+        mercury_format_term_nq(VarSet, no, next_to_graphic_token, Result, !IO)
     ;
-        mercury_format_term(Result, VarSet, no, !IO),
+        mercury_format_term(VarSet, no, Result, !IO),
         io.write_string(" :-\n\t", !IO),
         mercury_output_goal(Body, VarSet, 1, !IO)
     ).
@@ -2662,7 +2662,7 @@
     ;
         Vars = [_ | _],
         io.write_string("some [", !IO),
-        mercury_output_vars(Vars, VarSet, no, !IO),
+        mercury_output_vars(VarSet, no, Vars, !IO),
         io.write_string("] (", !IO),
         Indent1 = Indent + 1,
         mercury_output_newline(Indent1, !IO),
@@ -2678,7 +2678,7 @@
     ;
         Vars = [_ | _],
         io.write_string("some [", !IO),
-        mercury_output_state_vars(Vars, VarSet, no, !IO),
+        mercury_output_state_vars(VarSet, no, Vars, !IO),
         io.write_string("] (", !IO),
         Indent1 = Indent + 1,
         mercury_output_newline(Indent1, !IO),
@@ -2694,7 +2694,7 @@
     ;
         Vars = [_ | _],
         io.write_string("all [", !IO),
-        mercury_output_vars(Vars, VarSet, no, !IO),
+        mercury_output_vars(VarSet, no, Vars, !IO),
         io.write_string("] (", !IO),
         Indent1 = Indent + 1,
         mercury_output_newline(Indent1, !IO),
@@ -2710,7 +2710,7 @@
     ;
         Vars = [_ | _],
         io.write_string("all [", !IO),
-        mercury_output_state_vars(Vars, VarSet, no, !IO),
+        mercury_output_state_vars(VarSet, no, Vars, !IO),
         io.write_string("] (", !IO),
         Indent1 = Indent + 1,
         mercury_output_newline(Indent1, !IO),
@@ -2785,7 +2785,7 @@
             MaybeIO = yes(IOStateVar),
             mercury_output_comma_if_needed(!.NeedComma, !IO),
             io.write_string("io(!", !IO),
-            mercury_output_var(IOStateVar, VarSet, no, !IO),
+            mercury_output_var(VarSet, no, IOStateVar, !IO),
             io.write_string(")", !IO),
             !:NeedComma = yes
         ;
@@ -2860,9 +2860,9 @@
 
 mercury_output_goal_2(unify_expr(A, B, Purity), VarSet, _Indent, !IO) :-
     write_purity_prefix(Purity, !IO),
-    mercury_output_term(A, VarSet, no, !IO),
+    mercury_output_term(VarSet, no, A, !IO),
     io.write_string(" = ", !IO),
-    mercury_output_term(B, VarSet, no, next_to_graphic_token, !IO).
+    mercury_output_term_nq(VarSet, no, next_to_graphic_token, B, !IO).
 
 :- pred mercury_output_promise_eqv_solutions_goal(prog_vars::in,
     prog_vars::in, prog_vars::in, goal::in, prog_varset::in, int::in,
@@ -2881,7 +2881,7 @@
     ;
         io.write_string(Keyword, !IO),
         io.write_string(" [", !IO),
-        mercury_output_vars(Vars, VarSet, no, !IO),
+        mercury_output_vars(VarSet, no, Vars, !IO),
         (
             Vars = [_ | _],
             DotSVars = [_ | _]
@@ -2986,7 +2986,7 @@
     io.write_string("state(", !IO),
     io.write_string(MutableName, !IO),
     io.write_string(", !", !IO),
-    mercury_output_var(StateVar, VarSet, AppendVarnums, !IO),
+    mercury_output_var(VarSet, AppendVarnums, StateVar, !IO),
     io.write_string(")", !IO).
 
 :- pred mercury_output_trace_mutable_var_and_comma(prog_varset::in, bool::in,
@@ -3008,13 +3008,13 @@
             next_to_graphic_token, !IO),
         io.write_string(".", !IO),
         term.context_init(Context0),
-        mercury_output_term(term.functor(term.atom(PredName),
-            Term, Context0), VarSet, no, next_to_graphic_token, !IO)
+        SubTerm = term.functor(term.atom(PredName), Term, Context0),
+        mercury_output_term_nq(VarSet, no, next_to_graphic_token, SubTerm, !IO)
     ;
         Name = unqualified(PredName),
         term.context_init(Context0),
-        mercury_output_term(term.functor(term.atom(PredName),
-            Term, Context0), VarSet, no, next_to_graphic_token, !IO)
+        SubTerm = term.functor(term.atom(PredName), Term, Context0),
+        mercury_output_term_nq(VarSet, no, next_to_graphic_token, SubTerm, !IO)
     ).
 
 :- pred mercury_output_disj(goal::in, prog_varset::in, int::in,
@@ -3057,13 +3057,13 @@
         )
     ->
         io.write_string(" some [", !IO),
-        mercury_output_vars(Vars, VarSet, no, !IO),
+        mercury_output_vars(VarSet, no, Vars, !IO),
         (
             Vars = [_ | _],
             StateVars = [_ | _]
         ->
             io.write_string(", ", !IO),
-            mercury_output_state_vars(StateVars, VarSet, no, !IO)
+            mercury_output_state_vars(VarSet, no, StateVars, !IO)
         ;
             true
         ),
@@ -3403,7 +3403,7 @@
     pair(tvar, mer_type)::in, io::di, io::uo) is det.
 
 mercury_output_type_subst(VarSet, AppendVarnums, Var - Type, !IO) :-
-    mercury_output_var(Var, VarSet, AppendVarnums, !IO),
+    mercury_output_var(VarSet, AppendVarnums, Var, !IO),
     io.write_string(" = ", !IO),
     mercury_output_type(VarSet, AppendVarnums, Type, !IO).
 
@@ -3667,52 +3667,54 @@
 
     % write a term to standard output.
 
-mercury_output_term(Term, VarSet, AppendVarnums, !IO) :-
-    mercury_output_term(Term, VarSet, AppendVarnums,
-        not_next_to_graphic_token, !IO).
-
-mercury_output_term(Term, VarSet, AppendVarnums, NextToGraphicToken, !IO) :-
-    mercury_format_term(Term, VarSet, AppendVarnums, NextToGraphicToken, !IO).
-
-mercury_term_to_string(Term, VarSet, AppendVarnums) =
-    mercury_term_to_string(Term, VarSet, AppendVarnums,
-        not_next_to_graphic_token).
+mercury_output_term(VarSet, AppendVarnums, Term, !IO) :-
+    mercury_output_term_nq(VarSet, AppendVarnums, not_next_to_graphic_token,
+        Term, !IO).
+
+mercury_output_term_nq(VarSet, AppendVarnums, NextToGraphicToken, Term, !IO) :-
+    mercury_format_term_nq(VarSet, AppendVarnums, NextToGraphicToken, Term,
+        !IO).
+
+mercury_term_to_string(VarSet, AppendVarnums, Term) =
+    mercury_term_nq_to_string(VarSet, AppendVarnums, not_next_to_graphic_token,
+        Term).
 
-mercury_term_to_string(Term, VarSet, AppendVarnums, NextToGraphicToken)
+mercury_term_nq_to_string(VarSet, AppendVarnums, NextToGraphicToken, Term)
         = String :-
-    mercury_format_term(Term, VarSet, AppendVarnums, NextToGraphicToken,
+    mercury_format_term_nq(VarSet, AppendVarnums, NextToGraphicToken, Term,
         "", String).
 
-:- pred mercury_format_term(term(T)::in, varset(T)::in, bool::in,
+:- pred mercury_format_term(varset(T)::in, bool::in, term(T)::in,
     U::di, U::uo) is det <= output(U).
 
-mercury_format_term(Term, VarSet, AppendVarnums, !U) :-
-    mercury_format_term(Term, VarSet, AppendVarnums,
-        not_next_to_graphic_token, !U).
+mercury_format_term(VarSet, AppendVarnums, Term, !U) :-
+    mercury_format_term_nq(VarSet, AppendVarnums, not_next_to_graphic_token,
+        Term, !U).
 
-:- pred mercury_format_term(term(T)::in, varset(T)::in, bool::in,
-    needs_quotes::in, U::di, U::uo) is det <= output(U).
+:- pred mercury_format_term_nq(varset(T)::in, bool::in, needs_quotes::in,
+    term(T)::in, U::di, U::uo) is det <= output(U).
 
-mercury_format_term(term.variable(Var, _), VarSet, AppendVarnums, _, !U) :-
+mercury_format_term_nq(VarSet, AppendVarnums, _, term.variable(Var, _), !U) :-
     mercury_format_var(VarSet, AppendVarnums, Var, !U).
-mercury_format_term(term.functor(Functor, Args, _), VarSet, AppendVarnums,
-        NextToGraphicToken, !U) :-
+mercury_format_term_nq(VarSet, AppendVarnums, NextToGraphicToken,
+        term.functor(Functor, Args, _), !U) :-
     (
         Functor = term.atom(""),
         Args = [F, X | Xs]
     ->
-        mercury_format_term(F, VarSet, AppendVarnums, NextToGraphicToken, !U),
+        mercury_format_term_nq(VarSet, AppendVarnums, NextToGraphicToken, F,
+            !U),
         add_string("(", !U),
-        mercury_format_term(X, VarSet, AppendVarnums, !U),
-        mercury_format_remaining_terms(Xs, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, X, !U),
+        mercury_format_remaining_terms(VarSet, AppendVarnums, Xs, !U),
         add_string(")", !U)
     ;
         Functor = term.atom("[|]"),
         Args = [X, Xs]
     ->
         add_string("[", !U),
-        mercury_format_term(X, VarSet, AppendVarnums, !U),
-        mercury_format_list_args(Xs, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, X, !U),
+        mercury_format_list_args(VarSet, AppendVarnums, Xs, !U),
         add_string("]", !U)
     ;
         Functor = term.atom("{}"),
@@ -3721,15 +3723,15 @@
         % A unary tuple is usually a DCG escape,
         % so add some extra space.
         add_string("{ ", !U),
-        mercury_format_term(X, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, X, !U),
         add_string(" }", !U)
     ;
         Functor = term.atom("{}"),
         Args = [X | Xs]
     ->
         add_string("{", !U),
-        mercury_format_term(X, VarSet, AppendVarnums, !U),
-        mercury_format_remaining_terms(Xs, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, X, !U),
+        mercury_format_remaining_terms(VarSet, AppendVarnums, Xs, !U),
         add_string("}", !U)
     ;
         Args = [BinaryPrefixArg1, BinaryPrefixArg2],
@@ -3739,9 +3741,9 @@
         add_string("(", !U),
         add_string(FunctorName, !U),
         add_string(" ", !U),
-        mercury_format_term(BinaryPrefixArg1, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, BinaryPrefixArg1, !U),
         add_string(" ", !U),
-        mercury_format_term(BinaryPrefixArg2, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, BinaryPrefixArg2, !U),
         add_string(")", !U)
     ;
         Args = [PrefixArg],
@@ -3751,7 +3753,7 @@
         add_string("(", !U),
         add_string(FunctorName, !U),
         add_string(" ", !U),
-        mercury_format_term(PrefixArg, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, PrefixArg, !U),
         add_string(")", !U)
     ;
         Args = [PostfixArg],
@@ -3759,7 +3761,7 @@
         mercury_unary_postfix_op(FunctorName)
     ->
         add_string("(", !U),
-        mercury_format_term(PostfixArg, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, PostfixArg, !U),
         add_string(" ", !U),
         add_string(FunctorName, !U),
         add_string(")", !U)
@@ -3774,19 +3776,19 @@
             ; FunctorName = "."
             )
         ->
-            mercury_format_term(Arg1, VarSet, AppendVarnums,
-                next_to_graphic_token, !U),
+            mercury_format_term_nq(VarSet, AppendVarnums,
+                next_to_graphic_token, Arg1, !U),
             add_string(".", !U),
-            mercury_format_term(Arg2, VarSet, AppendVarnums,
-                next_to_graphic_token, !U)
+            mercury_format_term_nq(VarSet, AppendVarnums,
+                next_to_graphic_token, Arg2, !U)
         ;
-            mercury_format_term(Arg1, VarSet, AppendVarnums,
-                not_next_to_graphic_token, !U),
+            mercury_format_term_nq(VarSet, AppendVarnums,
+                not_next_to_graphic_token, Arg1, !U),
             add_string(" ", !U),
             add_string(FunctorName, !U),
             add_string(" ", !U),
-            mercury_format_term(Arg2, VarSet, AppendVarnums,
-                not_next_to_graphic_token, !U)
+            mercury_format_term_nq(VarSet, AppendVarnums,
+                not_next_to_graphic_token, Arg2, !U)
         ),
         add_string(")", !U)
     ;
@@ -3794,49 +3796,49 @@
     ->
         mercury_format_constant(Functor, NextToGraphicToken, !U),
         add_string("(", !U),
-        mercury_format_term(Y, VarSet, AppendVarnums, !U),
-        mercury_format_remaining_terms(Ys, VarSet, AppendVarnums, !U),
+        mercury_format_term(VarSet, AppendVarnums, Y, !U),
+        mercury_format_remaining_terms(VarSet, AppendVarnums, Ys, !U),
         add_string(")", !U)
     ;
         mercury_format_bracketed_constant(Functor, NextToGraphicToken, !U)
     ).
 
-:- pred mercury_format_list_args(term(T)::in, varset(T)::in, bool::in,
+:- pred mercury_format_list_args(varset(T)::in, bool::in, term(T)::in,
     U::di, U::uo) is det <= output(U).
 
-mercury_format_list_args(Term, VarSet, AppendVarnums, !U) :-
+mercury_format_list_args(VarSet, AppendVarnums, Term, !U) :-
     (
         Term = term.functor(term.atom("[|]"), Args, _),
         Args = [X, Xs]
     ->
         add_string(", ", !U),
-        mercury_format_term(X, VarSet, AppendVarnums, !U),
-        mercury_format_list_args(Xs, VarSet, AppendVarnums, !U)
+        mercury_format_term(VarSet, AppendVarnums, X, !U),
+        mercury_format_list_args(VarSet, AppendVarnums, Xs, !U)
     ;
         Term = term.functor(term.atom("[]"), [], _)
     ->
         true
     ;
         add_string(" | ", !U),
-        mercury_format_term(Term, VarSet, AppendVarnums, !U)
+        mercury_format_term(VarSet, AppendVarnums, Term, !U)
     ).
 
-:- pred mercury_format_remaining_terms(list(term(T))::in, varset(T)::in,
-    bool::in, U::di, U::uo) is det <= output(U).
+:- pred mercury_format_remaining_terms(varset(T)::in, bool::in,
+    list(term(T))::in, U::di, U::uo) is det <= output(U).
 
-mercury_format_remaining_terms([], _VarSet, _AppendVarnums, !U).
-mercury_format_remaining_terms([Term | Terms], VarSet, AppendVarnums, !U) :-
+mercury_format_remaining_terms(_VarSet, _AppendVarnums, [], !U).
+mercury_format_remaining_terms(VarSet, AppendVarnums, [Term | Terms], !U) :-
     add_string(", ", !U),
-    mercury_format_term(Term, VarSet, AppendVarnums, !U),
-    mercury_format_remaining_terms(Terms, VarSet, AppendVarnums, !U).
+    mercury_format_term(VarSet, AppendVarnums, Term, !U),
+    mercury_format_remaining_terms(VarSet, AppendVarnums, Terms, !U).
 
     % Similar to mercury_output_vars//3, but prefixes each variable
     % with `!' to indicate that it is a state variable.
     %
-:- pred mercury_output_state_vars(list(var(T))::in, varset(T)::in, bool::in,
+:- pred mercury_output_state_vars(varset(T)::in, bool::in, list(var(T))::in,
     io::di, io::uo) is det.
 
-mercury_output_state_vars(StateVars, VarSet, AppendVarnums, !IO) :-
+mercury_output_state_vars(VarSet, AppendVarnums, StateVars, !IO) :-
     io.write_list(StateVars, ", ",
         mercury_output_state_var(VarSet, AppendVarnums), !IO).
 
@@ -3845,20 +3847,20 @@
 
 mercury_output_state_var(VarSet, AppendVarnum, Var, !IO) :-
     io.write_string("!", !IO),
-    mercury_output_var(Var, VarSet, AppendVarnum, !IO).
+    mercury_output_var(VarSet, AppendVarnum, Var, !IO).
 
     % output a comma-separated list of variables
 
-mercury_output_vars(Vars, VarSet, AppendVarnum, !IO) :-
-    mercury_format_vars(Vars, VarSet, AppendVarnum, !IO).
+mercury_output_vars(VarSet, AppendVarnum, Vars, !IO) :-
+    mercury_format_vars(VarSet, AppendVarnum, Vars, !IO).
 
-mercury_vars_to_string(Vars, VarSet, AppendVarnum) = String :-
-    mercury_format_vars(Vars, VarSet, AppendVarnum, "", String).
+mercury_vars_to_string(VarSet, AppendVarnum, Vars) = String :-
+    mercury_format_vars(VarSet, AppendVarnum, Vars, "", String).
 
-:- pred mercury_format_vars(list(var(T))::in, varset(T)::in,
-    bool::in, U::di, U::uo) is det <= output(U).
+:- pred mercury_format_vars(varset(T)::in, bool::in, list(var(T))::in,
+    U::di, U::uo) is det <= output(U).
 
-mercury_format_vars(Vars, VarSet, AppendVarnum, !U) :-
+mercury_format_vars(VarSet, AppendVarnum, Vars, !U) :-
     add_list(Vars, ", ", mercury_format_var(VarSet, AppendVarnum), !U).
 
     % Output a single variable.
@@ -3867,10 +3869,10 @@
     % Variables whose name originally started with `V_' have their
     % name changed to start with `V__' to avoid name clashes.
 
-mercury_output_var(Var, VarSet, AppendVarnum, !IO) :-
+mercury_output_var(VarSet, AppendVarnum, Var, !IO) :-
     mercury_format_var(VarSet, AppendVarnum, Var, !IO).
 
-mercury_var_to_string(Var, VarSet, AppendVarnum) = String :-
+mercury_var_to_string(VarSet, AppendVarnum, Var) = String :-
     mercury_format_var(VarSet, AppendVarnum, Var, "", String).
 
 :- pred mercury_format_var(varset(T)::in, bool::in, var(T)::in, U::di, U::uo)
@@ -4552,7 +4554,7 @@
     ;
         HeadVars = [_ | _],
         io.write_string("vars(", !IO),
-        mercury_output_vars(HeadVars, VarSet, no, !IO),
+        mercury_output_vars(VarSet, no, HeadVars, !IO),
         io.write_string("), ", !IO),
 
         io.write_string("types(", !IO),
Index: compiler/mode_debug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_debug.m,v
retrieving revision 1.28
diff -u -b -r1.28 mode_debug.m
--- compiler/mode_debug.m	27 Sep 2006 06:16:54 -0000	1.28
+++ compiler/mode_debug.m	23 Oct 2006 01:37:13 -0000
@@ -125,7 +125,7 @@
         (
             Verbose = yes,
             io.write_string("\t", !IO),
-            mercury_output_var(Var, VarSet, no, !IO),
+            mercury_output_var(VarSet, no, Var, !IO),
             io.write_string(" ::", !IO),
             io.write_string(" unchanged\n", !IO)
         ;
@@ -133,7 +133,7 @@
         )
     ;
         io.write_string("\t", !IO),
-        mercury_output_var(Var, VarSet, no, !IO),
+        mercury_output_var(VarSet, no, Var, !IO),
         io.write_string(" ::", !IO),
         (
             Minimal = yes,
Index: compiler/mode_errors.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_errors.m,v
retrieving revision 1.111
diff -u -b -r1.111 mode_errors.m
--- compiler/mode_errors.m	13 Oct 2006 04:52:21 -0000	1.111
+++ compiler/mode_errors.m	23 Oct 2006 04:28:25 -0000
@@ -509,7 +509,7 @@
     DelayedGoal = delayed_goal(Vars, Error, Goal),
     set.to_sorted_list(Vars, VarList),
     Pieces1 = [words("Floundered goal, waiting on {"),
-        words(mercury_vars_to_string(VarList, VarSet, no)),
+        words(mercury_vars_to_string(VarSet, no, VarList)),
         words("}:"), nl],
     Msg1 = simple_msg(Context,
         [option_is_set(debug_modes, yes, [always(Pieces1)])]),
@@ -567,7 +567,7 @@
 
 merge_error_to_pieces(ModeInfo, Var - Insts) = Pieces :-
     mode_info_get_varset(ModeInfo, VarSet),
-    Pieces = [words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+    Pieces = [words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         fixed("::"),
         words(inst_list_to_string(ModeInfo, Insts)), suffix("."), nl].
 
@@ -607,7 +607,7 @@
     ),
     MainPieces = [words("scope error:"), words(ReasonStr), nl,
         words("Variable"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))), suffix(","), nl,
         words("expected instantiatedness was"),
@@ -657,7 +657,7 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("mode error: variable"),
-        fixed(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        fixed(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))),
         suffix(","), nl,
@@ -678,7 +678,7 @@
     mode_info_get_context(!.ModeInfo, Context),
     mode_info_get_varset(!.ModeInfo, VarSet),
     MainPieces = [words("mode error: arguments"),
-        words(add_quotes(mercury_vars_to_string(Vars, VarSet, no))),
+        words(add_quotes(mercury_vars_to_string(VarSet, no, Vars))),
         words("have the following insts:"), nl_indent_delta(1)] ++
         inst_list_to_sep_lines(!.ModeInfo, Insts) ++
         [words("which does not match any of the valid modes for")],
@@ -745,7 +745,7 @@
             "report_mode_error_no_matching_mode: invalid context")
     ),
     Pieces = [words("mode error: arguments"),
-        fixed(add_quotes(mercury_vars_to_string(Vars, VarSet, no))),
+        fixed(add_quotes(mercury_vars_to_string(VarSet, no, Vars))),
         words("have the following insts:"), nl_indent_delta(1)] ++
         inst_list_to_sep_lines(ModeInfo, Insts) ++
         [words("which does not match any of the modes for"),
@@ -771,7 +771,7 @@
             int_to_string(Arity - 1) ++ ")."
     ),
     Pieces = [words("mode error: variable"),
-        fixed(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        fixed(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))),
         suffix(","), nl,
@@ -788,7 +788,7 @@
     mode_info_get_varset(ModeInfo, VarSet),
     MainPieces = [words("in polymorphically-typed unification:"), nl,
         words("mode error: variable"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))), suffix(","), nl,
         words("expected instantiatedness was `ground' or `any'."), nl],
@@ -809,7 +809,7 @@
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("unique-mode error: the called procedure"),
         words("would clobber its argument, but variable"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("is still live."), nl],
     Spec = error_spec(severity_error, phase_mode_check,
         [simple_msg(Context, [always(Preamble ++ Pieces)])]).
@@ -822,7 +822,7 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("mode error: variable"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))), suffix(","), nl,
         words("expected instantiatedness was"),
@@ -841,7 +841,7 @@
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("sorry, implied modes not implemented."), nl,
         words("Variable"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, VarInst))),
         suffix(","), nl,
@@ -874,7 +874,7 @@
     mode_info_get_module_info(ModeInfo, ModuleInfo),
     (
         RHS = error_at_var(Y),
-        RHSStr = mercury_var_to_string(Y, VarSet, no)
+        RHSStr = mercury_var_to_string(VarSet, no, Y)
     ;
         RHS = error_at_functor(ConsId, ArgVars),
         RHSStr = hlds_out.functor_cons_id_to_string(ConsId, ArgVars, VarSet,
@@ -888,7 +888,7 @@
     ),
     varset.init(TypeVarSet),
     MainPieces = [words("In unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("with"), words(add_quotes(RHSStr)), suffix(":"), nl,
         words("mode error: attempt at higher-order unification."), nl,
         words("Cannot unify two terms of type"),
@@ -919,16 +919,16 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("mode error in unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("and"),
-        words(add_quotes(mercury_var_to_string(Y, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Y))),
         suffix("."), nl,
         words("Variable"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstX))), suffix(","), nl,
         words("variable"),
-        words(add_quotes(mercury_var_to_string(Y, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Y))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstY))), suffix("."), nl],
     Spec = error_spec(severity_error, phase_mode_check,
@@ -944,10 +944,10 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("mode error in unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("and lambda expression."), nl,
         words("Variable"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstX))), suffix(","), nl,
         words("lambda expression has instantiatedness"),
@@ -967,12 +967,12 @@
     mode_info_get_varset(ModeInfo, VarSet),
     mode_info_get_module_info(ModeInfo, ModuleInfo),
     Pieces1 = [words("mode error in unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("and"),
         words(add_quotes(hlds_out.functor_cons_id_to_string(ConsId, Args,
             VarSet, ModuleInfo, no))), suffix("."), nl,
         words("Variable"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstX))), suffix(","), nl,
         words("term"),
@@ -1005,14 +1005,14 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("warning: unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("and"),
-        words(add_quotes(mercury_var_to_string(Y, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Y))),
         words("cannot succeed"), nl,
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstX))), suffix(","), nl,
-        words(add_quotes(mercury_var_to_string(Y, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Y))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstY))), suffix("."), nl],
     Spec = error_spec(severity_warning, phase_mode_check,
@@ -1026,11 +1026,11 @@
     mode_info_get_context(ModeInfo, Context),
     mode_info_get_varset(ModeInfo, VarSet),
     Pieces = [words("warning: unification of"),
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("and"),
         words(mercury_cons_id_to_string(ConsId, does_not_need_brackets)),
         words("cannot succeed"), nl,
-        words(add_quotes(mercury_var_to_string(X, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, X))),
         words("has instantiatedness"),
         words(add_quotes(inst_to_string(ModeInfo, InstX))), suffix("."), nl],
     Spec = error_spec(severity_warning, phase_mode_check,
@@ -1105,7 +1105,7 @@
     Pieces = [words("mode error: argument"), fixed(int_to_string(ArgNum)),
         words(Problem), nl,
         words("Final instantiatedness of"),
-        words(add_quotes(mercury_var_to_string(Var, VarSet, no))),
+        words(add_quotes(mercury_var_to_string(VarSet, no, Var))),
         words("was"), words(add_quotes(inst_to_string(ModeInfo, VarInst))),
         suffix(","), nl,
         words("expected final instantiatedness was"),
@@ -1130,7 +1130,7 @@
             words("purity error: if-then-else"),
             words("should be inside a promise_purity"),
             words("scope because non-local variable"),
-            words(mercury_var_to_string(Var, VarSet, no)),
+            words(mercury_var_to_string(VarSet, no, Var)),
             words("has inst any and appears in the condition.")
         ]
     ;
@@ -1139,7 +1139,7 @@
             words("purity error: negation"),
             words("should be inside a promise_purity"),
             words("scope because non-local variable"),
-            words(mercury_var_to_string(Var, VarSet, no)),
+            words(mercury_var_to_string(VarSet, no, Var)),
             words("has inst any and appears in the body.")
         ]
     ),
@@ -1159,7 +1159,7 @@
         words("purity error: lambda should be impure because it"),
         words("contains the following non-local variables"),
         words("whose insts contain `any':"),
-        words(mercury_vars_to_string(Vars, VarSet, no)),
+        words(mercury_vars_to_string(VarSet, no, Vars)),
         suffix("."), nl
     ],
     Spec = error_spec(severity_error, phase_mode_check,
Index: compiler/modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modes.m,v
retrieving revision 1.348
diff -u -b -r1.348 modes.m
--- compiler/modes.m	13 Oct 2006 04:52:22 -0000	1.348
+++ compiler/modes.m	23 Oct 2006 01:29:47 -0000
@@ -1395,7 +1395,7 @@
 modecheck_goal_expr(scope(Reason, SubGoal0), GoalInfo0, GoalExpr, !ModeInfo,
         !IO) :-
     (
-        Reason = trace_goal(_, _, _, _),
+        Reason = trace_goal(_, _, _, _, _),
         mode_checkpoint(enter, "scope", !ModeInfo, !IO),
         mode_info_get_instmap(!.ModeInfo, InstMap0),
         goal_info_get_nonlocals(GoalInfo0, NonLocals),
Index: compiler/pd_debug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_debug.m,v
retrieving revision 1.22
diff -u -b -r1.22 pd_debug.m
--- compiler/pd_debug.m	15 Oct 2006 23:26:49 -0000	1.22
+++ compiler/pd_debug.m	23 Oct 2006 04:30:54 -0000
@@ -152,7 +152,7 @@
     proc_info_get_varset(ProcInfo, VarSet),
     instmap_restrict(NonLocals, InstMap, InstMap1),
     io.write_string(" args: ", !IO),
-    mercury_output_vars(Args, VarSet, yes, !IO),
+    mercury_output_vars(VarSet, yes, Args, !IO),
     io.nl(!IO),
     hlds_out.write_instmap(InstMap1, VarSet, yes, 1, !IO),
     io.nl(!IO),
Index: compiler/post_typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/post_typecheck.m,v
retrieving revision 1.111
diff -u -b -r1.111 post_typecheck.m
--- compiler/post_typecheck.m	1 Nov 2006 06:33:04 -0000	1.111
+++ compiler/post_typecheck.m	3 Nov 2006 03:38:09 -0000
@@ -371,7 +371,7 @@
     pair(prog_var, mer_type)) = list(format_component).
 
 var_and_type_to_pieces(VarSet, TVarSet, Var - Type) =
-    [words(mercury_var_to_string(Var, VarSet, no)), suffix(":"),
+    [words(mercury_var_to_string(VarSet, no, Var)), suffix(":"),
     words(mercury_type_to_string(TVarSet, no, Type)), nl].
 
 %-----------------------------------------------------------------------------%
Index: compiler/purity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/purity.m,v
retrieving revision 1.107
diff -u -b -r1.107 purity.m
--- compiler/purity.m	6 Nov 2006 07:21:41 -0000	1.107
+++ compiler/purity.m	6 Nov 2006 07:27:33 -0000
@@ -622,7 +622,7 @@
         Reason = from_ground_term(_),
         compute_goal_purity(Goal0, Goal, Purity, ContainsTrace, !Info)
     ;
-        Reason = trace_goal(_, _, _, _),
+        Reason = trace_goal(_, _, _, _, _),
         compute_goal_purity(Goal0, Goal, _SubPurity, _, !Info),
         Purity = purity_pure,
         ContainsTrace = contains_trace_goal
Index: compiler/quantification.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/quantification.m,v
retrieving revision 1.110
diff -u -b -r1.110 quantification.m
--- compiler/quantification.m	3 Nov 2006 08:31:11 -0000	1.110
+++ compiler/quantification.m	3 Nov 2006 08:45:54 -0000
@@ -309,19 +309,17 @@
     instmap_delta_restrict(NonLocalVarsSet, InstMapDelta0, InstMapDelta),
     goal_info_set_instmap_delta(InstMapDelta, GoalInfo2, GoalInfo).
 
+    % After this pass, explicit quantifiers are redundant, since all variables
+    % which were explicitly quantified have been renamed apart. So we don't
+    % keep them. We need to keep the structure, though, so that mode analysis
+    % doesn't try to reorder through quantifiers. (Actually it would make sense
+    % to allow mode analysis to do that, but the reference manual says it
+    % doesn't, so we don't.)  Thus we replace `scope(exist_quant(Vars), Goal0)'
+    % with an empty quantifier `scope(exist_quant([]), Goal)'.
+    %
 :- pred implicitly_quantify_goal_2(hlds_goal_expr::in, hlds_goal_expr::out,
     prog_context::in, quant_info::in, quant_info::out) is det.
 
-    % After this pass, explicit quantifiers are redundant,
-    % since all variables which were explicitly quantified
-    % have been renamed apart.  So we don't keep them.
-    % We need to keep the structure, though, so that mode
-    % analysis doesn't try to reorder through quantifiers.
-    % (Actually it would make sense to allow mode analysis
-    % to do that, but the reference manual says it doesn't,
-    % so we don't.)  Thus we replace `scope(exist_quant(Vars), Goal0)'
-    % with an empty quantifier `scope(exist_quant([]), Goal)'.
-
 implicitly_quantify_goal_2(Expr0, Expr, Context, !Info) :-
     Expr0 = scope(Reason0, Goal0),
     (
@@ -348,9 +346,8 @@
         Reason1 = Reason0,
         Vars0 = []
     ;
-        Reason0 = trace_goal(_, _, _, _),
-        Reason1 = Reason0,
-        Vars0 = []
+        Reason0 = trace_goal(_, _, _, _, Vars0),
+        Reason1 = Reason0
     ),
     get_outside(!.Info, OutsideVars),
     get_lambda_outside(!.Info, LambdaOutsideVars),
@@ -382,7 +379,7 @@
             ; Reason1 = commit(_)
             ; Reason1 = barrier(_)
             ; Reason1 = from_ground_term(_)
-            ; Reason1 = trace_goal(_, _, _, _)
+            ; Reason1 = trace_goal(_, _, _, _, _)
             ),
             Reason = Reason1
         )
@@ -1175,7 +1172,7 @@
     ;
         Reason = from_ground_term(_)
     ;
-        Reason = trace_goal(_, _, _, _)
+        Reason = trace_goal(_, _, _, _, _)
     ),
     union(Set0, !Set),
     union(LambdaSet0, !LambdaSet).
Index: compiler/simplify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/simplify.m,v
retrieving revision 1.196
diff -u -b -r1.196 simplify.m
--- compiler/simplify.m	6 Nov 2006 07:21:41 -0000	1.196
+++ compiler/simplify.m	6 Nov 2006 07:27:33 -0000
@@ -1401,7 +1401,7 @@
             KeepCommon = no
         ;
             FinalReason = trace_goal(MaybeCompiletimeExpr, MaybeRuntimeExpr,
-                _, _),
+                _, _, _),
             ( simplify_do_after_front_end(!.Info) ->
                 (
                     MaybeCompiletimeExpr = yes(CompiletimeExpr),
Index: compiler/superhomogeneous.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/superhomogeneous.m,v
retrieving revision 1.22
diff -u -b -r1.22 superhomogeneous.m
--- compiler/superhomogeneous.m	1 Nov 2006 06:33:20 -0000	1.22
+++ compiler/superhomogeneous.m	3 Nov 2006 03:38:09 -0000
@@ -801,7 +801,7 @@
 
 report_error_in_type_qualification(GenericVarSet, Context, Error, !Specs) :-
     Error = ErrorMsg - ErrorTerm,
-    TermStr = mercury_term_to_string(ErrorTerm, GenericVarSet, no),
+    TermStr = mercury_term_to_string(GenericVarSet, no, ErrorTerm),
     Pieces = [words("In explicit type qualification:"),
         words(ErrorMsg), suffix(":"), quote(TermStr), suffix("."), nl],
     Msg = simple_msg(Context, [always(Pieces)]),
@@ -1077,7 +1077,7 @@
     list(error_spec)::in, list(error_spec)::out) is det.
 
 report_string_term_error(Context, VarSet, ErrorMsg - ErrorTerm, !Specs) :-
-    TermStr = mercury_term_to_string(ErrorTerm, VarSet, no),
+    TermStr = mercury_term_to_string(VarSet, no, ErrorTerm),
     Pieces = [words("Error:"), words(ErrorMsg), suffix(":"),
         quote(TermStr), suffix("."), nl],
     Msg = simple_msg(Context, [always(Pieces)]),
Index: compiler/typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck.m,v
retrieving revision 1.409
diff -u -b -r1.409 typecheck.m
--- compiler/typecheck.m	1 Nov 2006 06:33:24 -0000	1.409
+++ compiler/typecheck.m	3 Nov 2006 03:38:10 -0000
@@ -1330,7 +1330,7 @@
         ;
             Reason = from_ground_term(_)
         ;
-            Reason = trace_goal(_, _, _, _)
+            Reason = trace_goal(_, _, _, _, _)
         ),
         GoalExpr = scope(Reason, SubGoal)
     ;
Index: compiler/typecheck_errors.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck_errors.m,v
retrieving revision 1.32
diff -u -b -r1.32 typecheck_errors.m
--- compiler/typecheck_errors.m	1 Nov 2006 06:33:27 -0000	1.32
+++ compiler/typecheck_errors.m	3 Nov 2006 03:38:10 -0000
@@ -485,12 +485,12 @@
     unify_context_to_pieces(UnifyContext, [], UnifyContextPieces),
 
     MainPieces = [words("type error in unification of variable"),
-        quote(mercury_var_to_string(X, VarSet, no)), nl,
+        quote(mercury_var_to_string(VarSet, no, X)), nl,
         words("and variable"),
-        quote(mercury_var_to_string(Y, VarSet, no)), suffix("."), nl,
-        quote(mercury_var_to_string(X, VarSet, no))] ++
+        quote(mercury_var_to_string(VarSet, no, Y)), suffix("."), nl,
+        quote(mercury_var_to_string(VarSet, no, X))] ++
         type_of_var_to_pieces(TypeAssignSet, X) ++ [suffix(","), nl,
-        quote(mercury_var_to_string(Y, VarSet, no))] ++
+        quote(mercury_var_to_string(VarSet, no, Y))] ++
         type_of_var_to_pieces(TypeAssignSet, Y) ++ [suffix("."), nl],
     VerbosePieces = type_assign_set_msg_to_pieces(TypeAssignSet, VarSet),
     Msg = simple_msg(Context,
@@ -514,15 +514,15 @@
     (
         PredOrFunc = predicate,
         Pieces2 = [words("and"), prefix("pred("),
-            words(mercury_vars_to_string(ArgVars, VarSet, no)),
+            words(mercury_vars_to_string(VarSet, no, ArgVars)),
             suffix(")"), words(":- ...':"), nl]
     ;
         PredOrFunc = function,
         pred_args_to_func_args(ArgVars, FuncArgs, RetVar),
         Pieces2 = [words("and"), prefix("func("),
-            words(mercury_vars_to_string(FuncArgs, VarSet, no)),
+            words(mercury_vars_to_string(VarSet, no, FuncArgs)),
             suffix(")"), fixed("="),
-            words(mercury_var_to_string(RetVar, VarSet, no)),
+            words(mercury_var_to_string(VarSet, no, RetVar)),
             words(":- ...':"), nl]
     ),
 
@@ -793,7 +793,7 @@
     ),
     ( varset.search_name(VarSet, Var, _) ->
         Pieces2 = [prefix("("),
-            words(mercury_var_to_string(Var, VarSet, no)),
+            words(mercury_var_to_string(VarSet, no, Var)),
             suffix(")")]
     ;
         Pieces2 = []
@@ -1126,11 +1126,11 @@
                 "report_invalid_field_update: no type variables")
         ;
             TVars = [TVar],
-            TVarsStr = mercury_var_to_string(TVar, TVarSet, no),
+            TVarsStr = mercury_var_to_string(TVarSet, no, TVar),
             Pieces2 = [words("variable"), quote(TVarsStr), words("occurs")]
         ;
             TVars = [_, _ | _],
-            TVarsStr = mercury_vars_to_string(TVars, TVarSet, no),
+            TVarsStr = mercury_vars_to_string(TVarSet, no, TVars),
             Pieces2 = [words("variables"), quote(TVarsStr), words("occur")]
         ),
         ConsIdStr = cons_id_to_string(ConsId),
@@ -1213,7 +1213,7 @@
         type_assign_get_typevarset(TypeAssign1, TVarSet1),
         type_assign_get_typevarset(TypeAssign2, TVarSet2),
         HeadPieces =
-            [words(mercury_var_to_string(Var, VarSet, no)), suffix(":")] ++
+            [words(mercury_var_to_string(VarSet, no, Var)), suffix(":")] ++
             type_to_pieces(T1, TVarSet1, HeadTypeParams1) ++ [words("or")] ++
             type_to_pieces(T2, TVarSet2, HeadTypeParams2) ++ [nl]
     ;
@@ -1302,7 +1302,7 @@
 argument_name_to_pieces(VarSet, Var) = Pieces :-
     ( varset.search_name(VarSet, Var, _) ->
         Pieces = [words("variable"),
-            quote(mercury_var_to_string(Var, VarSet, no))]
+            quote(mercury_var_to_string(VarSet, no, Var))]
     ;
         Pieces = [words("argument")]
     ).
@@ -1469,7 +1469,7 @@
     list.map(term.coerce_var, HeadTypeParams, ExistQVars),
     maybe_add_existential_quantifier(ExistQVars, Term0, Term),
     varset.coerce(TVarSet, VarSet),
-    Pieces = [words(mercury_term_to_string(Term, VarSet, no))].
+    Pieces = [words(mercury_term_to_string(VarSet, no, Term))].
 
     % Return a description of the given list of types.
     %
@@ -1719,7 +1719,7 @@
     list.map(term.coerce_var, HeadTypeParams, ExistQVars),
     maybe_add_existential_quantifier(ExistQVars, Term0, Term),
     varset.coerce(TypeVarSet, VarSet),
-    TypeStr = mercury_term_to_string(Term, VarSet, no).
+    TypeStr = mercury_term_to_string(VarSet, no, Term).
 
     % Given an arg type assignment set and a variable id, return the list of
     % possible different types for the argument and the variable.
Index: compiler/typecheck_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck_info.m,v
retrieving revision 1.19
diff -u -b -r1.19 typecheck_info.m
--- compiler/typecheck_info.m	22 Oct 2006 09:14:26 -0000	1.19
+++ compiler/typecheck_info.m	23 Oct 2006 01:43:23 -0000
@@ -719,7 +719,7 @@
     ;
         HeadTypeParams = [_ | _],
         io.write_string("some [", !IO),
-        mercury_output_vars(HeadTypeParams, TypeVarSet, varnums, !IO),
+        mercury_output_vars(TypeVarSet, varnums, HeadTypeParams, !IO),
         io.write_string("]\n\t", !IO)
     ),
     write_type_assign_types(Vars, VarSet, VarTypes, TypeBindings, TypeVarSet,
@@ -755,7 +755,7 @@
         HeadPieces = []
     ;
         HeadTypeParams = [_ | _],
-        VarsStr = mercury_vars_to_string(HeadTypeParams, TypeVarSet, varnums),
+        VarsStr = mercury_vars_to_string(TypeVarSet, varnums, HeadTypeParams), 
         HeadPieces = [words("some [" ++ VarsStr ++ "]"), nl]
     ),
     TypePieces = type_assign_types_to_pieces(Vars, VarSet, VarTypes,
@@ -783,7 +783,7 @@
         ;
             FoundOne = no
         ),
-        mercury_output_var(Var, VarSet, varnums, !IO),
+        mercury_output_var(VarSet, varnums, Var, !IO),
         io.write_string(": ", !IO),
         write_type_with_bindings(Type, TypeVarSet, TypeBindings, !IO),
         write_type_assign_types(Vars, VarSet, VarTypes, TypeBindings,
@@ -814,7 +814,7 @@
             FoundOne = no,
             PrefixPieces = []
         ),
-        VarStr = mercury_var_to_string(Var, VarSet, varnums),
+        VarStr = mercury_var_to_string(VarSet, varnums, Var),
         TypeStr = type_with_bindings_to_string(Type, TypeVarSet, TypeBindings),
         AssignPieces = [fixed(VarStr), suffix(":"), words(TypeStr)],
         TailPieces = type_assign_types_to_pieces(Vars, VarSet, VarTypes,
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/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/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_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/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
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/odbc
cvs diff: Diffing extras/posix
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/stream
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/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
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 tests
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
Index: tests/hard_coded/trace_goal_3.exp
===================================================================
RCS file: tests/hard_coded/trace_goal_3.exp
diff -N tests/hard_coded/trace_goal_3.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/trace_goal_3.exp	5 Nov 2006 03:42:34 -0000
@@ -0,0 +1,3 @@
+<abc>42<abc>
+<xyz>43<xyz>
+44xx45
Index: tests/hard_coded/trace_goal_3.m
===================================================================
RCS file: tests/hard_coded/trace_goal_3.m
diff -N tests/hard_coded/trace_goal_3.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/trace_goal_3.m	5 Nov 2006 03:41:35 -0000
@@ -0,0 +1,32 @@
+:- module trace_goal_3.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module int.
+:- import_module string.
+
+main(!IO) :-
+	p(42, X),
+	io.write_string(X, !IO),
+	io.nl(!IO).
+
+:- pred p(int::in, string::out) is det.
+
+p(N, !:S) :-
+	trace [compiletime(flag("abc") or flag("xyz")), io(!S)] (
+		io.write_string("<abc>", !S),
+		io.write_int(N, !S),
+		io.write_string("<abc>\n", !S)
+	),
+	trace [compiletime(flag("abc") or flag("xyz")), io(!S)] (
+		io.write_string("<xyz>", !S),
+		io.write_int(N+1, !S),
+		io.write_string("<xyz>\n", !S)
+	),
+	!:S = int_to_string(N+2) ++ "xx" ++ int_to_string(N+3).
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/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