[m-rev.] diff: makes vartypes an abstract type

Zoltan Somogyi zs at unimelb.edu.au
Mon Jul 2 11:12:30 AEST 2012


compiler/prog_data.m:
	Make vartypes an abstract data type, in preparation for exploring
	better representations for it.

compiler/mode_util.m:
	Provide two different versions of a predicate. The generic version
	continues to use map lookups. The other version knows it works on
	prog_vars, so it can use the abstract operations on them provided
	by prog_data.m.

compiler/accumulator.m:
compiler/add_class.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/add_trail_ops.m:
compiler/arg_info.m:
compiler/builtin_lib_types.m:
compiler/bytecode_gen.m:
compiler/call_gen.m:
compiler/clause_to_proc.m:
compiler/closure_analysis.m:
compiler/code_info.m:
compiler/common.m:
compiler/complexity.m:
compiler/const_prop.m:
compiler/constraint.m:
compiler/continuation_info.m:
compiler/cse_detection.m:
compiler/ctgc.datastruct.m:
compiler/ctgc.util.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/det_util.m:
compiler/disj_gen.m:
compiler/equiv_type_hlds.m:
compiler/erl_call_gen.m:
compiler/erl_code_gen.m:
compiler/erl_code_util.m:
compiler/exception_analysis.m:
compiler/float_regs.m:
compiler/follow_vars.m:
compiler/format_call.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/hlds_clauses.m:
compiler/hlds_goal.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_pred.m:
compiler/hlds_pred.m:
compiler/hlds_rtti.m:
compiler/inlining.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/liveness.m:
compiler/lookup_switch.m:
compiler/mercury_to_mercury.m:
compiler/ml_accurate_gc.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_disj_gen.m:
compiler/ml_lookup_switch.m:
compiler/ml_proc_gen.m:
compiler/ml_unify_gen.m:
compiler/mode_info.m:
compiler/modecheck_call.m:
compiler/modecheck_conj.m:
compiler/modecheck_goal.m:
compiler/modecheck_unify.m:
compiler/modecheck_util.m:
compiler/modes.m:
compiler/par_loop_control.m:
compiler/pd_info.m:
compiler/pd_util.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/prog_type_subst.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/qual_info.m:
compiler/rbmm.points_to_info.m:
compiler/rbmm.region_liveness_info.m:
compiler/rbmm.region_transformation.m:
compiler/saved_vars.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_alloc.m:
compiler/stack_opt.m:
compiler/store_alloc.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/switch_detection.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/term_constr_util.m:
compiler/term_traversal.m:
compiler/term_util.m:
compiler/trace_gen.m:
compiler/trailing_analysis.m:
compiler/try_expand.m:
compiler/tupling.m:
compiler/type_constraints.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
compiler/typecheck_info.m:
compiler/unify_gen.m:
compiler/unify_proc.m:
compiler/unique_modes.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/var_locn.m:
	Conform to the above.

compiler/prog_type.m:
compiler/rbmm.points_to_graph.m:
	Conform to the above.

	Move some comments where they belong.

compiler/stm_expand.m:
	Conform to the above.

	Do not export a predicate that is not used outside this module.

	Disable some debugging output unless it is asked for.

	Remove unnecessary prefixes on variable names.

library/version_array.m:
	Instead writing code for field access lookalike functions and defining
	lookup, set etc in terms of them, write code for lookup, set etc,
	and define the field access lookalike functions in terms of them.

	Change argument orders of some internal predicates to be
	more state variable friendly.

	Fix typos in comments.

tests/hard_coded/version_array_test.exp:
	Conform to the change to version_array.m.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/extra
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/extra
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/libatomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/doc
cvs diff: Diffing boehm_gc/libatomic_ops/src
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/armcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/gcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/hpc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/ibmc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/icc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/msftc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/sunc
cvs diff: Diffing boehm_gc/libatomic_ops/tests
cvs diff: Diffing boehm_gc/libatomic_ops-1.2
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/doc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/tests
cvs diff: Diffing boehm_gc/m4
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/accumulator.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/accumulator.m,v
retrieving revision 1.87
diff -u -b -r1.87 accumulator.m
--- compiler/accumulator.m	17 Jan 2012 15:49:44 -0000	1.87
+++ compiler/accumulator.m	2 Jul 2012 01:07:01 -0000
@@ -1194,8 +1194,8 @@
     varset.lookup_name(!.VarSet, OldVar, OldName),
     string.append(Prefix, OldName, NewName),
     varset.new_named_var(NewName, NewVar, !VarSet),
-    map.lookup(!.VarTypes, OldVar, Type),
-    map.det_insert(NewVar, Type, !VarTypes).
+    lookup_var_type(!.VarTypes, OldVar, Type),
+    add_var_type(NewVar, Type, !VarTypes).
 
 %-----------------------------------------------------------------------------%
 
@@ -1519,7 +1519,7 @@
     list.duplicate(list.length(Accs), list.det_head(Mode), AccModes),
     HeadModes = AccModes ++ HeadModes0,
 
-    list.map(map.lookup(VarTypes), Accs, AccTypes),
+    lookup_var_types(VarTypes, Accs, AccTypes),
 
     proc_info_create(Context, VarSet, VarTypes, HeadVars, InstVarSet,
         HeadModes, detism_decl_none, Detism, Goal, RttiVarMaps,
@@ -1932,5 +1932,5 @@
     ).
 
 %-----------------------------------------------------------------------------%
-:- end_module accumulator.
+:- end_module transform_hlds.accumulator.
 %-----------------------------------------------------------------------------%
Index: compiler/add_class.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_class.m,v
retrieving revision 1.40
diff -u -b -r1.40 add_class.m
--- compiler/add_class.m	5 Jun 2012 15:14:26 -0000	1.40
+++ compiler/add_class.m	2 Jul 2012 01:07:01 -0000
@@ -626,7 +626,7 @@
             Context, []),
 
         map.init(TVarNameMap),
-        map.from_corresponding_lists(HeadVars, ArgTypes, VarTypes),
+        vartypes_from_corresponding_lists(HeadVars, ArgTypes, VarTypes),
         HeadVarVec = proc_arg_vector_init(PredOrFunc, HeadVars),
         set_clause_list([IntroducedClause], ClausesRep),
         rtti_varmaps_init(RttiVarMaps),
Index: compiler/add_heap_ops.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_heap_ops.m,v
retrieving revision 1.50
diff -u -b -r1.50 add_heap_ops.m
--- compiler/add_heap_ops.m	13 Feb 2012 00:11:33 -0000	1.50
+++ compiler/add_heap_ops.m	2 Jul 2012 01:07:01 -0000
@@ -353,7 +353,7 @@
     VarSet0 = !.Info ^ heap_varset,
     VarTypes0 = !.Info ^ heap_var_types,
     varset.new_named_var(Name, Var, VarSet0, VarSet),
-    map.det_insert(Var, Type, VarTypes0, VarTypes),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     !Info ^ heap_varset := VarSet,
     !Info ^ heap_var_types := VarTypes.
 
Index: compiler/add_pragma.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pragma.m,v
retrieving revision 1.127
diff -u -b -r1.127 add_pragma.m
--- compiler/add_pragma.m	23 Apr 2012 03:34:47 -0000	1.127
+++ compiler/add_pragma.m	2 Jul 2012 01:07:01 -0000
@@ -1488,7 +1488,7 @@
             % sync with the predicate's tvarset after the first pass of
             % type checking.
             % map.from_corresponding_lists(Args, Types, VarTypes0)
-            map.init(VarTypes0),
+            init_vartypes(VarTypes0),
             goal_info_init(GoalInfo0),
             set_of_var.list_to_set(Args, NonLocals),
             goal_info_set_nonlocals(NonLocals, GoalInfo0, GoalInfo1),
@@ -3268,7 +3268,7 @@
             GoalExpr = call_foreign_proc(Attributes, PredId, ProcId,
                 ForeignArgs, ExtraArgs, MaybeTraceRuntimeCond, PragmaImpl),
             HldsGoal0 = hlds_goal(GoalExpr, GoalInfo),
-            map.init(EmptyVarTypes),
+            init_vartypes(EmptyVarTypes),
             rtti_varmaps_init(EmptyRttiVarmaps),
             implicitly_quantify_clause_body_general(
                 ordinary_nonlocals_maybe_lambda, HeadVarList, _Warnings,
Index: compiler/add_pred.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_pred.m,v
retrieving revision 1.49
diff -u -b -r1.49 add_pred.m
--- compiler/add_pred.m	13 Feb 2012 00:11:33 -0000	1.49
+++ compiler/add_pred.m	2 Jul 2012 01:07:01 -0000
@@ -352,8 +352,8 @@
 
     % Put the clause we just built (if any) into the pred_info,
     % annotated with the appropriate types.
-    map.from_corresponding_lists(ExtraVars ++ HeadVarList, ExtraTypes ++ Types,
-        VarTypes),
+    vartypes_from_corresponding_lists(ExtraVars ++ HeadVarList,
+        ExtraTypes ++ Types, VarTypes),
     map.init(TVarNameMap),
     rtti_varmaps_init(RttiVarMaps),
     HasForeignClauses = no,
Index: compiler/add_trail_ops.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/add_trail_ops.m,v
retrieving revision 1.62
diff -u -b -r1.62 add_trail_ops.m
--- compiler/add_trail_ops.m	13 Feb 2012 00:11:33 -0000	1.62
+++ compiler/add_trail_ops.m	2 Jul 2012 01:07:01 -0000
@@ -611,7 +611,7 @@
     VarSet0 = !.Info ^ trail_varset,
     VarTypes0 = !.Info ^ trail_var_types,
     varset.new_named_var(Name, Var, VarSet0, VarSet),
-    map.det_insert(Var, Type, VarTypes0, VarTypes),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     !Info ^ trail_varset := VarSet,
     !Info ^ trail_var_types := VarTypes.
 
Index: compiler/arg_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/arg_info.m,v
retrieving revision 1.65
diff -u -b -r1.65 arg_info.m
--- compiler/arg_info.m	13 Feb 2012 00:11:33 -0000	1.65
+++ compiler/arg_info.m	2 Jul 2012 01:07:01 -0000
@@ -475,14 +475,14 @@
     proc_info_get_headvars(ProcInfo, Vars),
     proc_info_get_argmodes(ProcInfo, Modes),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(Vars, VarTypes, Types),
+    lookup_var_types(VarTypes, Vars, Types),
     do_partition_proc_args(ModuleInfo, Vars, Types, Modes,
         Inputs, Outputs, Unuseds).
 
 partition_proc_call_args(ProcInfo, VarTypes, ModuleInfo, Vars,
         Inputs, Outputs, Unuseds) :-
     proc_info_get_argmodes(ProcInfo, Modes),
-    map.apply_to_list(Vars, VarTypes, Types),
+    lookup_var_types(VarTypes, Vars, Types),
     do_partition_proc_args(ModuleInfo, Vars, Types, Modes,
         Inputs, Outputs, Unuseds).
 
Index: compiler/builtin_lib_types.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/builtin_lib_types.m,v
retrieving revision 1.6
diff -u -b -r1.6 builtin_lib_types.m
--- compiler/builtin_lib_types.m	13 Feb 2012 00:11:33 -0000	1.6
+++ compiler/builtin_lib_types.m	2 Jul 2012 01:07:01 -0000
@@ -243,7 +243,7 @@
 %-----------------------------------------------------------------------------%
 
 is_region_var(VarTypes, Var)  :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     Type = region_type.
 
 %-----------------------------------------------------------------------------%
Index: compiler/bytecode_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/bytecode_gen.m,v
retrieving revision 1.134
diff -u -b -r1.134 bytecode_gen.m
--- compiler/bytecode_gen.m	11 Jun 2012 03:13:20 -0000	1.134
+++ compiler/bytecode_gen.m	2 Jul 2012 01:07:01 -0000
@@ -865,7 +865,7 @@
     map.det_insert(Var, N0, !VarMap),
     N1 = N0 + 1,
     varset.lookup_name(VarSet, Var, VarName),
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     create_varmap(VarList, VarSet, VarTypes, N1, !VarMap, VarInfosTail),
     VarInfos = [var_info(VarName, VarType) | VarInfosTail].
 
@@ -915,7 +915,7 @@
     mer_type::out) is det.
 
 get_var_type(ByteInfo, Var, Type) :-
-    map.lookup(ByteInfo ^ byteinfo_vartypes, Var, Type).
+    lookup_var_type(ByteInfo ^ byteinfo_vartypes, Var, Type).
 
 :- pred get_next_label(int::out, byte_info::in, byte_info::out)
     is det.
Index: compiler/call_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/call_gen.m,v
retrieving revision 1.207
diff -u -b -r1.207 call_gen.m
--- compiler/call_gen.m	13 Feb 2012 00:11:33 -0000	1.207
+++ compiler/call_gen.m	2 Jul 2012 01:07:01 -0000
@@ -198,7 +198,7 @@
         MaybeRegTypes, Det, GoalInfo, Code, !CI) :-
     get_module_info(!.CI, ModuleInfo),
     VarTypes = get_var_types(!.CI),
-    map.apply_to_list(Args, VarTypes, Types),
+    lookup_var_types(VarTypes, Args, Types),
     arg_info.generic_call_arg_reg_types(ModuleInfo, VarTypes, GenericCall,
         Args, MaybeRegTypes, ArgRegTypes),
     arg_info.compute_in_and_out_vars_sep_regs(ModuleInfo, Args, Modes, Types,
Index: compiler/clause_to_proc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/clause_to_proc.m,v
retrieving revision 1.92
diff -u -b -r1.92 clause_to_proc.m
--- compiler/clause_to_proc.m	13 Feb 2012 00:11:34 -0000	1.92
+++ compiler/clause_to_proc.m	2 Jul 2012 01:07:01 -0000
@@ -20,19 +20,17 @@
 
 %-----------------------------------------------------------------------------%
 
-    % In the hlds, we initially record the clauses for a predicate
-    % in the clauses_info data structure which is part of the
-    % pred_info data structure.  But once the clauses have been
-    % type-checked, we want to have a separate copy of each clause
-    % for each different mode of the predicate, since we may
-    % end up reordering the clauses differently in different modes.
-    % Here we copy the clauses from the clause_info data structure
-    % into the proc_info data structure.  Each clause is marked
-    % with a list of the modes for which it applies, so that
-    % there can be different code to implement different modes
-    % of a predicate (e.g. sort).  For each mode of the predicate,
-    % we select the clauses for that mode, disjoin them together,
-    % and save this in the proc_info.
+    % In the hlds, we initially record the clauses for a predicate in the
+    % clauses_info data structure which is part of the pred_info data
+    % structure. But once the clauses have been type-checked, we want to have
+    % a separate copy of each clause for each different mode of the predicate,
+    % since we may end up reordering the clauses differently in different
+    % modes. Here we copy the clauses from the clause_info data structure
+    % into the proc_info data structure. Each clause is marked with a list
+    % of the modes for which it applies, so that there can be different code
+    % to implement different modes of a predicate (e.g. sort). For each mode
+    % of the predicate, we select the clauses for that mode, disjoin them
+    % together, and save this in the proc_info.
     %
 :- pred copy_module_clauses_to_procs(list(pred_id)::in,
     module_info::in, module_info::out) is det.
@@ -390,8 +388,8 @@
     ),
 
     % Add exists_casts for any head vars which are existentially typed,
-    % and for which the type is statically bound inside the procedure.  Subn
-    % represents which existential types are bound.
+    % and for which the type is statically bound inside the procedure.
+    % Subn represents which existential types are bound.
     introduce_exists_casts_for_head(ModuleInfo, Subn, OrigArgTypes,
         OrigArgModes, OrigHeadVars1, OrigHeadVars, VarSet0, VarSet1,
         VarTypes0, VarTypes1, [], ExistsCastHeadGoals),
@@ -460,9 +458,9 @@
         InternalType \= ExternalType
     ->
         term.context_init(Context),
-        map.det_update(HeadVar0, InternalType, !VarTypes),
+        update_var_type(HeadVar0, InternalType, !VarTypes),
         make_new_exist_cast_var(HeadVar0, HeadVar, !VarSet),
-        map.det_insert(HeadVar, ExternalType, !VarTypes),
+        add_var_type(HeadVar, ExternalType, !VarTypes),
         mode_get_insts(ModuleInfo, ArgMode, _, Inst),
         generate_cast_with_insts(exists_cast, HeadVar0, HeadVar, Inst, Inst,
             Context, ExtraGoal),
@@ -490,15 +488,13 @@
         [ModeAndVar | ModesAndVars], [Var | Vars], !VarSet, !VarTypes,
         !RttiVarMaps, !ExtraGoals) :-
     ModeAndVar = ArgMode - Var0,
-    (
-        mode_is_output(ModuleInfo, ArgMode)
-    ->
+    ( mode_is_output(ModuleInfo, ArgMode) ->
         % Create the exists_cast goal.
 
         term.context_init(Context),
         make_new_exist_cast_var(Var0, Var, !VarSet),
-        map.lookup(!.VarTypes, Var0, VarType),
-        map.det_insert(Var, VarType, !VarTypes),
+        lookup_var_type(!.VarTypes, Var0, VarType),
+        add_var_type(Var, VarType, !VarTypes),
         generate_cast(exists_cast, Var0, Var, Context, ExtraGoal),
         !:ExtraGoals = [ExtraGoal | !.ExtraGoals],
 
Index: compiler/closure_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/closure_analysis.m,v
retrieving revision 1.27
diff -u -b -r1.27 closure_analysis.m
--- compiler/closure_analysis.m	13 Feb 2012 00:11:34 -0000	1.27
+++ compiler/closure_analysis.m	2 Jul 2012 01:07:01 -0000
@@ -124,7 +124,7 @@
 :- pred var_has_ho_type(vartypes::in, prog_var::in) is semidet.
 
 var_has_ho_type(VarTypes, Var) :-
-    Type = map.lookup(VarTypes, Var),
+    lookup_var_type(VarTypes, Var, Type),
     type_is_higher_order(Type).
 
     % Insert the given prog_var into the closure_info and set the
Index: compiler/code_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/code_info.m,v
retrieving revision 1.400
diff -u -b -r1.400 code_info.m
--- compiler/code_info.m	8 Jun 2012 15:36:54 -0000	1.400
+++ compiler/code_info.m	2 Jul 2012 01:07:01 -0000
@@ -1076,7 +1076,7 @@
     proc_info_get_vartypes(ProcInfo, VarTypes).
 
 variable_type(CI, Var) = Type :-
-    map.lookup(get_var_types(CI), Var, Type).
+    lookup_var_type(get_var_types(CI), Var, Type).
 
 variable_is_of_dummy_type(CI, Var) = IsDummy :-
     VarType = variable_type(CI, Var),
@@ -4176,7 +4176,7 @@
     pair(prog_var, lval)::in) is semidet.
 
 valid_stack_slot(ModuleInfo, VarTypes, Var - Lval) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     check_dummy_type(ModuleInfo, Type) = is_not_dummy_type,
     (
         ( Lval = stackvar(N)
Index: compiler/common.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/common.m,v
retrieving revision 1.119
diff -u -b -r1.119 common.m
--- compiler/common.m	14 Jun 2012 07:03:56 -0000	1.119
+++ compiler/common.m	2 Jul 2012 01:07:01 -0000
@@ -400,7 +400,7 @@
 
 lookup_var_type_ctor(Info, Var) = TypeCtor :-
     simplify_info_get_var_types(Info, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     % If we unify a variable with a function symbol, we *must* know
     % what the principal type constructor of its type is.
     type_to_ctor_det(Type, TypeCtor).
@@ -565,8 +565,8 @@
                 % Don't warn for cases such as:
                 % set.init(Set1 : set(int)),
                 % set.init(Set2 : set(float)).
-                map.apply_to_list(OutputArgs, VarTypes, OutputArgTypes1),
-                map.apply_to_list(OutputArgs2, VarTypes, OutputArgTypes2),
+                lookup_var_types(VarTypes, OutputArgs, OutputArgTypes1),
+                lookup_var_types(VarTypes, OutputArgs2, OutputArgTypes2),
                 types_match_exactly_list(OutputArgTypes1, OutputArgTypes2)
             ->
                 Context = goal_info_get_context(GoalInfo),
@@ -644,7 +644,7 @@
     partition_call_args(VarTypes, ModuleInfo, ArgModes, Args,
         InputArgs1, OutputArgs1, OutputModes1),
     mode_get_insts(ModuleInfo, ArgMode, InitialInst, FinalInst),
-    map.lookup(VarTypes, Arg, Type),
+    lookup_var_type(VarTypes, Arg, Type),
     ( inst_matches_binding(InitialInst, FinalInst, Type, ModuleInfo) ->
         InputArgs = [Arg | InputArgs1],
         OutputArgs = OutputArgs1,
@@ -771,8 +771,8 @@
         !Info) :-
     apply_induced_substitutions(ToVar, FromVar, !Info),
     simplify_info_get_var_types(!.Info, VarTypes),
-    map.lookup(VarTypes, ToVar, ToVarType),
-    map.lookup(VarTypes, FromVar, FromVarType),
+    lookup_var_type(VarTypes, ToVar, ToVarType),
+    lookup_var_type(VarTypes, FromVar, FromVarType),
 
     set_of_var.list_to_set([ToVar, FromVar], NonLocals),
     UniMode = ((_ - ToVarInst0) -> (_ - ToVarInst)),
Index: compiler/complexity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/complexity.m,v
retrieving revision 1.44
diff -u -b -r1.44 complexity.m
--- compiler/complexity.m	16 Aug 2011 03:26:29 -0000	1.44
+++ compiler/complexity.m	2 Jul 2012 01:07:01 -0000
@@ -451,13 +451,13 @@
         SlotVarName, PredId, !ProcInfo, !ModuleInfo, Goals,
         ForeignArgs, CodeStr) :-
     proc_info_get_vartypes(!.ProcInfo, VarTypes1),
-    map.lookup(VarTypes1, ArgVar, VarType),
+    lookup_var_type(VarTypes1, ArgVar, VarType),
     MacroName = "MR_complexity_fill_size_slot",
     make_type_info_var(VarType, Context, PredId, !ProcInfo, !ModuleInfo,
         TypeInfoVar, Goals),
     % Since we just created TypeInfoVar, it isn't in VarTypes1.
     proc_info_get_vartypes(!.ProcInfo, VarTypes2),
-    map.lookup(VarTypes2, TypeInfoVar, TypeInfoType),
+    lookup_var_type(VarTypes2, TypeInfoVar, TypeInfoType),
     ArgName = "arg" ++ int_to_string(VarSeqNum),
     TypeInfoArgName = "input_typeinfo" ++ int_to_string(VarSeqNum),
     ForeignArg = foreign_arg(ArgVar,
@@ -482,7 +482,7 @@
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     varset.new_named_var(Name, Var, VarSet0, VarSet),
-    map.set(Var, Type, VarTypes0, VarTypes),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo).
 
@@ -522,7 +522,7 @@
         MaybeName = no
     ),
     ( mode_is_fully_input(ModuleInfo, Mode) ->
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         ( zero_size_type(ModuleInfo, VarType) ->
             Kind = complexity_input_fixed_size
         ;
Index: compiler/const_prop.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/const_prop.m,v
retrieving revision 1.50
diff -u -b -r1.50 const_prop.m
--- compiler/const_prop.m	23 Apr 2012 03:34:47 -0000	1.50
+++ compiler/const_prop.m	2 Jul 2012 01:07:01 -0000
@@ -82,7 +82,7 @@
     globals.lookup_bool_option(Globals, cross_compiling, CrossCompiling),
     LookupArgs = (func(Var) = arg_hlds_info(Var, Type, Inst) :-
         instmap_lookup_var(InstMap, Var, Inst),
-        Type = VarTypes ^ det_elem(Var)
+        lookup_var_type(VarTypes, Var, Type)
     ),
     ArgHldsInfos = list.map(LookupArgs, Args),
     evaluate_call_2(ModuleName, PredName, ProcIdInt, ArgHldsInfos,
Index: compiler/constraint.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/constraint.m,v
retrieving revision 1.106
diff -u -b -r1.106 constraint.m
--- compiler/constraint.m	13 Feb 2012 00:11:34 -0000	1.106
+++ compiler/constraint.m	2 Jul 2012 01:07:01 -0000
@@ -322,8 +322,8 @@
         (pred(Var::in) is semidet :-
             instmap_lookup_var(InstMap0, Var, InstBefore),
             instmap_delta_search_var(InstMapDelta, Var, InstAfter),
-            \+ inst_matches_initial(InstAfter, InstBefore,
-                map.lookup(VarTypes, Var), ModuleInfo)
+            lookup_var_type(VarTypes, Var, Type),
+            \+ inst_matches_initial(InstAfter, InstBefore, Type, ModuleInfo)
         ),
     IncompatibleInstVars = set_of_var.list_to_set(
         list.filter(InCompatible, InstMapVars)),
@@ -336,8 +336,8 @@
         (pred(Var::in) is semidet :-
             instmap_lookup_var(InstMap0, Var, InstBefore),
             instmap_delta_search_var(InstMapDelta, Var, InstAfter),
-            \+ inst_matches_binding(InstAfter, InstBefore,
-                map.lookup(VarTypes, Var), ModuleInfo)
+            lookup_var_type(VarTypes, Var, Type),
+            \+ inst_matches_binding(InstAfter, InstBefore, Type, ModuleInfo)
         ),
     BoundVars = set_of_var.list_to_set(list.filter(Bound, InstMapVars)),
 
@@ -565,8 +565,8 @@
         VarSet0 = !.Info ^ constr_varset,
         VarTypes0 = !.Info ^ constr_vartypes,
         varset.new_var(NewVar, VarSet0, VarSet),
-        map.lookup(VarTypes0, ConstructVar, VarType),
-        map.det_insert(NewVar, VarType, VarTypes0, VarTypes),
+        lookup_var_type(VarTypes0, ConstructVar, VarType),
+        add_var_type(NewVar, VarType, VarTypes0, VarTypes),
         !Info ^ constr_varset := VarSet,
         !Info ^ constr_vartypes := VarTypes,
         map.from_assoc_list([ConstructVar - NewVar], Subn),
@@ -783,7 +783,7 @@
     InstMap0 = !.Info ^ constr_instmap,
     ModuleInfo0 = !.Info ^ constr_module_info,
     VarTypes = !.Info ^ constr_vartypes,
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     bind_var_to_functors(Var, Type, MainConsId, OtherConsIds,
         InstMap0, InstMap, ModuleInfo0, ModuleInfo),
     !Info ^ constr_instmap := InstMap,
Index: compiler/continuation_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/continuation_info.m,v
retrieving revision 1.106
diff -u -b -r1.106 continuation_info.m
--- compiler/continuation_info.m	13 Feb 2012 00:11:35 -0000	1.106
+++ compiler/continuation_info.m	2 Jul 2012 01:07:01 -0000
@@ -706,7 +706,7 @@
 generate_resume_layout_for_vars([Var - LvalSet | VarLvals], InstMap,
         VarTypes, ProcInfo, ModuleInfo, !VarInfos, !TVars) :-
     (
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         check_dummy_type(ModuleInfo, Type) = is_dummy_type
     ->
         true
@@ -771,7 +771,7 @@
     ;
         Name = ""
     ),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
 
 %   In some programs, specifically zm_enum.m, the Mercury program generated
 %   for the enum.zinc g12 test case, this call to inst_is_ground can be
@@ -908,7 +908,7 @@
         [ArgLayout | ArgLayouts], !TypeVars) :-
     term.var_to_int(Var, VarNum),
     varset.lookup_name(VarSet, Var, VarName),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     ArgLayout = table_arg_info(VarNum, VarName, SlotNum, Type),
     type_vars(Type, VarTypeVars),
     set.insert_list(VarTypeVars, !TypeVars),
Index: compiler/cse_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/cse_detection.m,v
retrieving revision 1.137
diff -u -b -r1.137 cse_detection.m
--- compiler/cse_detection.m	11 Apr 2012 04:52:34 -0000	1.137
+++ compiler/cse_detection.m	2 Jul 2012 01:07:01 -0000
@@ -718,8 +718,8 @@
     VarSet0 = !.CseInfo ^ csei_varset,
     VarTypes0 = !.CseInfo ^ csei_vartypes,
     varset.new_var(NFV, VarSet0, VarSet),
-    map.lookup(VarTypes0, OFV, Type),
-    map.det_insert(NFV, Type, VarTypes0, VarTypes),
+    lookup_var_type(VarTypes0, OFV, Type),
+    add_var_type(NFV, Type, VarTypes0, VarTypes),
     !:OldNewVar = [OFV - NFV | !.OldNewVar],
     UnifyContext = unify_context(MainCtxt, SubCtxt),
     % It is ok to create complicated unifications here, because we rerun
@@ -852,7 +852,7 @@
         UnifyInfo = deconstruct(Var, ConsId, _, _, _, _),
         ModuleInfo = !.CseInfo ^ csei_module_info,
         VarTypes = !.CseInfo ^ csei_vartypes,
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         cons_id_is_existq_cons(ModuleInfo, Type, ConsId)
     ->
         update_existential_data_structures(FirstOldNew, LaterOldNew, !CseInfo)
@@ -893,7 +893,7 @@
     map.from_assoc_list(OldNew, OldNewMap),
     apply_substitutions_to_rtti_varmaps(Renaming, map.init, OldNewMap,
         RttiVarMaps0, RttiVarMaps),
-    map.map_values_only(apply_variable_renaming_to_type(Renaming),
+    transform_var_types(apply_variable_renaming_to_type(Renaming),
         VarTypes0, VarTypes),
 
     !CseInfo ^ csei_rtti_varmaps := RttiVarMaps,
Index: compiler/ctgc.datastruct.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ctgc.datastruct.m,v
retrieving revision 1.15
diff -u -b -r1.15 ctgc.datastruct.m
--- compiler/ctgc.datastruct.m	16 Jul 2009 07:27:11 -0000	1.15
+++ compiler/ctgc.datastruct.m	2 Jul 2012 01:07:01 -0000
@@ -124,7 +124,7 @@
 
         % Keep datastruct seletors normalized.
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         normalize_selector_with_type_information(ModuleInfo, Type,
             NewSel0, NewSel),
 
@@ -134,7 +134,7 @@
 normalize_datastruct(ModuleInfo, ProcInfo, Data0) = Data :-
     Data0 = selected_cel(Var, DSel0),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     normalize_selector_with_type_information(ModuleInfo, Type, DSel0, DSel),
     Data = selected_cel(Var, DSel).
 
@@ -143,7 +143,7 @@
     Data1 = selected_cel(Var, Sel1),
     Data2 = selected_cel(Var, Sel2),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     selector_subsumed_by(ModuleInfo, already_normalized,
         Sel1, Sel2, Type, Extension).
 
@@ -181,7 +181,7 @@
 datastruct_apply_widening(ModuleInfo, ProcInfo, Data0, Data) :-
     Data0 = selected_cel(Var, Sel0),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     selector_apply_widening(ModuleInfo, Type, Sel0, Sel),
     Data = selected_cel(Var, Sel).
 
Index: compiler/ctgc.util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ctgc.util.m,v
retrieving revision 1.25
diff -u -b -r1.25 ctgc.util.m
--- compiler/ctgc.util.m	23 May 2011 05:08:01 -0000	1.25
+++ compiler/ctgc.util.m	2 Jul 2012 01:07:01 -0000
@@ -175,7 +175,7 @@
 
 var_needs_sharing_analysis(ModuleInfo, ProcInfo, Var) :-
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     type_needs_sharing_analysis(ModuleInfo, Type).
 
 type_needs_sharing_analysis(ModuleInfo, Type) :-
Index: compiler/deep_profiling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deep_profiling.m,v
retrieving revision 1.117
diff -u -b -r1.117 deep_profiling.m
--- compiler/deep_profiling.m	13 Feb 2012 00:11:35 -0000	1.117
+++ compiler/deep_profiling.m	2 Jul 2012 01:07:01 -0000
@@ -1846,7 +1846,7 @@
 
 generate_var_2(Name, Type, Var, !VarSet, !VarTypes) :-
     varset.new_named_var(Name, Var, !VarSet),
-    map.det_insert(Var, Type, !VarTypes).
+    add_var_type(Var, Type, !VarTypes).
 
 :- pred maybe_generate_activation_ptr(bool::in, prog_var::in, prog_var::in,
     maybe(prog_var)::out, hlds_deep_excp_vars::out,
Index: compiler/deforest.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deforest.m,v
retrieving revision 1.106
diff -u -b -r1.106 deforest.m
--- compiler/deforest.m	13 Feb 2012 00:11:35 -0000	1.106
+++ compiler/deforest.m	2 Jul 2012 01:07:01 -0000
@@ -1242,7 +1242,7 @@
 
             pd_info_get_proc_info(!.PDInfo, ProcInfo1),
             proc_info_get_vartypes(ProcInfo1, VarTypes),
-            map.apply_to_list(NonLocalsList, VarTypes, ArgTypes),
+            lookup_var_types(VarTypes, NonLocalsList, ArgTypes),
             VersionInfo = version_info(FoldGoal, CalledPreds, NonLocalsList,
                 ArgTypes, InstMap0, 0, 0, Parents0, MaybeGeneralised),
             pd_info_get_global_term_info(!.PDInfo, TermInfo0),
@@ -1355,7 +1355,7 @@
         % The variable is local to the call. Create a fresh variable.
         varset.new_var(Arg, !VarSet),
         apply_subst_to_type(TypeSubn, ArgType, SubnArgType),
-        map.det_insert(Arg, SubnArgType, !VarTypes)
+        add_var_type(Arg, SubnArgType, !VarTypes)
     ),
     create_deforest_call_args(OldArgs, ArgTypes, Renaming,
         TypeSubn, Args, !VarSet, !VarTypes).
Index: compiler/dep_par_conj.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/dep_par_conj.m,v
retrieving revision 1.62
diff -u -b -r1.62 dep_par_conj.m
--- compiler/dep_par_conj.m	13 Feb 2012 00:11:35 -0000	1.62
+++ compiler/dep_par_conj.m	2 Jul 2012 01:07:01 -0000
@@ -1708,7 +1708,7 @@
 
 map_arg_to_new_future(HeadVars, FutureArg, !FutureMap, !VarSet, !VarTypes) :-
     HeadVar = list.det_index1(HeadVars, FutureArg),
-    map.lookup(!.VarTypes, HeadVar, VarType),
+    lookup_var_type(!.VarTypes, HeadVar, VarType),
     varset.lookup_name(!.VarSet, HeadVar, HeadVarName),
     make_future_var(HeadVarName, VarType, FutureVar, _FutureVarType, !VarSet,
         !VarTypes),
@@ -3112,7 +3112,7 @@
 
 allocate_future(ModuleInfo, SharedVar, Goals, !VarSet, !VarTypes,
         !FutureMap, !TSStringTable) :-
-    map.lookup(!.VarTypes, SharedVar, SharedVarType),
+    lookup_var_type(!.VarTypes, SharedVar, SharedVarType),
     varset.lookup_name(!.VarSet, SharedVar, SharedVarName),
     make_future_var(SharedVarName, SharedVarType, FutureVar, FutureVarType,
         !VarSet, !VarTypes),
@@ -3159,7 +3159,7 @@
         !VarSet, !VarTypes) :-
     FutureVarType = future_type(SharedVarType),
     varset.new_named_var("Future" ++ SharedVarName, FutureVar, !VarSet),
-    map.det_insert(FutureVar, FutureVarType, !VarTypes).
+    add_var_type(FutureVar, FutureVarType, !VarTypes).
 
 :- pred make_future_name_var_and_goal(string::in, prog_var::out, hlds_goal::out,
     prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
@@ -3168,7 +3168,7 @@
 make_future_name_var_and_goal(Name, FutureNameVar, Goal, !VarSet, !VarTypes, !TSStringTable) :-
     varset.new_named_var("FutureName" ++ Name, FutureNameVar, !VarSet),
     IntType = builtin_type(builtin_type_int),
-    map.det_insert(FutureNameVar, IntType, !VarTypes),
+    add_var_type(FutureNameVar, IntType, !VarTypes),
     allocate_ts_string(Name, NameId, !TSStringTable),
     Ground = ground(unique, none),
     GoalExpr = unify(FutureNameVar, rhs_functor(int_const(NameId), no, []),
@@ -3230,10 +3230,12 @@
     ;
         ShouldInline = yes,
         ForeignAttrs = par_builtin_foreign_proc_attributes(Purity, no),
+        lookup_var_type(VarTypes, FutureVar, FutureVarType),
+        lookup_var_type(VarTypes, ConsumedVar, ConsumedVarType),
         Arg1 = foreign_arg(FutureVar, yes("Future" - in_mode),
-            map.lookup(VarTypes, FutureVar), native_if_possible),
+            FutureVarType, native_if_possible),
         Arg2 = foreign_arg(ConsumedVar, yes("Value" - out_mode),
-            map.lookup(VarTypes, ConsumedVar), native_if_possible),
+            ConsumedVarType, native_if_possible),
         Args = [Arg1, Arg2],
         ExtraArgs = [],
         generate_foreign_proc(ModuleName, PredName, pf_predicate,
@@ -3262,10 +3264,12 @@
         ShouldInline = yes,
         ForeignAttrs = par_builtin_foreign_proc_attributes(purity_impure,
             yes(needs_call_standard_output_registers)),
+        lookup_var_type(VarTypes, FutureVar, FutureVarType),
+        lookup_var_type(VarTypes, ProducedVar, ProducedVarType),
         Arg1 = foreign_arg(FutureVar, yes("Future" - in_mode),
-            map.lookup(VarTypes, FutureVar), native_if_possible),
+            FutureVarType, native_if_possible),
         Arg2 = foreign_arg(ProducedVar, yes("Value" - in_mode),
-            map.lookup(VarTypes, ProducedVar), native_if_possible),
+            ProducedVarType, native_if_possible),
         Args = [Arg1, Arg2],
         ExtraArgs = [],
         Code = "MR_par_builtin_signal_future(Future, Value);",
Index: compiler/det_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_analysis.m,v
retrieving revision 1.239
diff -u -b -r1.239 det_analysis.m
--- compiler/det_analysis.m	14 Jun 2012 07:03:56 -0000	1.239
+++ compiler/det_analysis.m	2 Jul 2012 01:07:01 -0000
@@ -986,7 +986,7 @@
     Case0 = case(MainConsId, OtherConsIds, Goal0),
     det_info_get_module_info(!.DetInfo, ModuleInfo0),
     det_info_get_vartypes(!.DetInfo, VarTypes),
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     bind_var_to_functors(Var, VarType, MainConsId, OtherConsIds,
         InstMap0, InstMap1, ModuleInfo0, ModuleInfo),
     det_info_set_module_info(ModuleInfo, !DetInfo),
@@ -1878,7 +1878,7 @@
         ExaminesRepresentation = yes,
         det_get_proc_info(!.DetInfo, ProcInfo),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         det_type_has_user_defined_equality_pred(!.DetInfo, Type)
     ->
         (
Index: compiler/det_report.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_report.m,v
retrieving revision 1.160
diff -u -b -r1.160 det_report.m
--- compiler/det_report.m	13 Feb 2012 00:11:36 -0000	1.160
+++ compiler/det_report.m	2 Jul 2012 01:07:01 -0000
@@ -638,7 +638,7 @@
             Msgs1 = []
         ),
         det_info_get_vartypes(!.DetInfo, VarTypes),
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         det_diagnose_switch_arms(Var, VarType, Cases, InstMap0,
             Desired, SwitchContexts, !DetInfo, Msgs2),
         Msgs = Msgs1 ++ Msgs2
@@ -1028,7 +1028,7 @@
     ;
         GoalExpr = switch(Var, _, Cases),
         det_info_get_vartypes(!.DetInfo, VarTypes),
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         reqscope_check_switch(Var, VarType, Cases, InstMap0, !DetInfo)
     ;
         GoalExpr = if_then_else(_, Cond, Then, Else),
@@ -1235,7 +1235,7 @@
             inst_is_bound_to_functors(ModuleInfo, VarInst, BoundInsts)
         ->
             det_info_get_vartypes(DetInfo, VarTypes),
-            map.lookup(VarTypes, Var, VarType),
+            lookup_var_type(VarTypes, Var, VarType),
             type_to_ctor_det(VarType, VarTypeCtor),
             list.map(bound_inst_to_cons_id(VarTypeCtor),
                 BoundInsts, ConsIds)
Index: compiler/det_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/det_util.m,v
retrieving revision 1.57
diff -u -b -r1.57 det_util.m
--- compiler/det_util.m	14 Jun 2012 07:03:56 -0000	1.57
+++ compiler/det_util.m	2 Jul 2012 01:07:01 -0000
@@ -208,7 +208,7 @@
 
 det_lookup_var_type(ModuleInfo, ProcInfo, Var, TypeDefn) :-
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     type_to_ctor_det(Type, TypeCtor),
     module_info_get_type_table(ModuleInfo, TypeTable),
     search_type_ctor_defn(TypeTable, TypeCtor, TypeDefn).
Index: compiler/disj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/disj_gen.m,v
retrieving revision 1.122
diff -u -b -r1.122 disj_gen.m
--- compiler/disj_gen.m	17 Oct 2011 04:31:28 -0000	1.122
+++ compiler/disj_gen.m	2 Jul 2012 01:07:01 -0000
@@ -155,7 +155,7 @@
 
     figure_out_output_vars(!.CI, DisjGoalInfo, OutVars),
     VarTypes = get_var_types(!.CI),
-    list.map(map.lookup(VarTypes), OutVars, OutTypes),
+    lookup_var_types(VarTypes, OutVars, OutTypes),
 
     produce_vars(set_of_var.to_sorted_list(ResumeVars), ResumeMap,
         FlushCode, !CI),
Index: compiler/equiv_type_hlds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/equiv_type_hlds.m,v
retrieving revision 1.75
diff -u -b -r1.75 equiv_type_hlds.m
--- compiler/equiv_type_hlds.m	22 Jun 2012 17:20:10 -0000	1.75
+++ compiler/equiv_type_hlds.m	2 Jul 2012 01:07:01 -0000
@@ -411,7 +411,7 @@
         ),
 
         proc_info_get_vartypes(!.ProcInfo, VarTypes0),
-        map.map_values_foldl(hlds_replace_in_type(EqvMap),
+        transform_foldl_var_types(hlds_replace_in_type(EqvMap),
             VarTypes0, VarTypes, !TVarSet),
         proc_info_set_vartypes(VarTypes, !ProcInfo),
 
@@ -1238,7 +1238,7 @@
         module_info_get_type_table(!.Info ^ ethri_module_info, TypeTable),
         proc_info_get_vartypes(!.Info ^ ethri_proc_info, VarTypes),
         proc_info_get_rtti_varmaps(!.Info ^ ethri_proc_info, RttiVarMaps),
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         TypeCtorCat = classify_type(!.Info ^ ethri_module_info, VarType),
         (
             % If this goal constructs a type_info for an equivalence type,
Index: compiler/erl_call_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_call_gen.m,v
retrieving revision 1.20
diff -u -b -r1.20 erl_call_gen.m
--- compiler/erl_call_gen.m	13 Apr 2012 07:18:21 -0000	1.20
+++ compiler/erl_call_gen.m	2 Jul 2012 01:07:01 -0000
@@ -153,10 +153,10 @@
 :- func var_to_expr_or_false(module_info, vartypes, prog_var) = elds_expr.
 
 var_to_expr_or_false(ModuleInfo, VarTypes, Var) = Expr :-
-    (if
+    ( if
         % The variable may not be in VarTypes if it did not exist in the
         % HLDS, i.e. we invented the variable.  Those should be kept.
-        map.search(VarTypes, Var, Type),
+        search_var_type(VarTypes, Var, Type),
         check_dummy_type(ModuleInfo, Type) = is_dummy_type
     then
         Expr = elds_term(elds_false)
@@ -194,7 +194,7 @@
     CallExpr = elds_call(CallTarget, InputExprs),
     (
         OutputVars = [],
-        (if 
+        ( if
             ( MaybeSuccessExpr = yes(elds_term(elds_empty_tuple))
             ; MaybeSuccessExpr = no
             )
@@ -207,7 +207,7 @@
     ;
         OutputVars = [_ | _],
         UnpackTerm = tuple_or_single_expr(exprs_from_vars(OutputVars)),
-        (if
+        ( if
             MaybeSuccessExpr = yes(UnpackTerm)
         then
             % Preserve tail calls.
@@ -224,7 +224,7 @@
 make_semidet_call(CallTarget, InputExprs, OutputVars, SuccessExpr, Statement) :-
     CallExpr = elds_call(CallTarget, InputExprs),
     UnpackTerm = elds_tuple(exprs_from_vars(OutputVars)),
-    (if
+    ( if
         SuccessExpr = elds_term(UnpackTerm)
     then
         % Avoid unnecessary unpacking.
@@ -234,7 +234,6 @@
         %   {OutputVars, ...} -> SuccessExpr ;
         %   _ -> fail
         % end
-        %
         Statement0 = elds_case_expr(CallExpr, [TrueCase, FalseCase]),
         TrueCase  = elds_case(UnpackTerm, SuccessExpr),
         FalseCase = elds_case(elds_anon_var, elds_term(elds_fail)),
@@ -246,13 +245,11 @@
 
 make_nondet_call(CallTarget, InputExprs, OutputVars, SuccessCont0,
         Statement) :-
-    %
     % Proc(InputExprs, ...,
     %   fun(OutputVars, ...) ->
     %       SuccessCont0
     %   end)
-    %
-    (if
+    ( if
         SuccessCont0 = elds_call(elds_call_ho(SuccessCont1),
             exprs_from_vars(OutputVars))
     then
@@ -391,7 +388,7 @@
             (
                 % We need to avoid generating assignments to dummy variables
                 % introduced for types such as io.state.
-                map.lookup(VarTypes, Lval, LvalType),
+                lookup_var_type(VarTypes, Lval, LvalType),
                 check_dummy_type(ModuleInfo, LvalType) = is_dummy_type
             ->
                 Statement = expr_or_void(MaybeSuccessExpr)
Index: compiler/erl_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_gen.m,v
retrieving revision 1.45
diff -u -b -r1.45 erl_code_gen.m
--- compiler/erl_code_gen.m	8 Jun 2012 15:36:55 -0000	1.45
+++ compiler/erl_code_gen.m	2 Jul 2012 01:07:01 -0000
@@ -235,7 +235,7 @@
         SpecialId = spec_pred_unify,
         in_in_unification_proc_id(ProcId),
         list.reverse(Args, [Y, X | _]),
-        map.lookup(VarTypes, Y, Type),
+        lookup_var_type(VarTypes, Y, Type),
         check_dummy_type(ModuleInfo, Type) = is_not_dummy_type,
         type_definitely_has_no_user_defined_equality_pred(ModuleInfo, Type),
         erl_gen_simple_in_in_unification(ModuleInfo, PredId, ProcId, X, Y,
@@ -243,7 +243,7 @@
     ;
         SpecialId = spec_pred_compare,
         list.reverse(Args, [Y, X, _Res | _]),
-        map.lookup(VarTypes, Y, Type),
+        lookup_var_type(VarTypes, Y, Type),
         check_dummy_type(ModuleInfo, Type) = is_not_dummy_type,
         type_definitely_has_no_user_defined_equality_pred(ModuleInfo, Type),
         erl_gen_simple_compare(ModuleInfo, PredId, ProcId, X, Y, ProcDefn)
Index: compiler/erl_code_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_util.m,v
retrieving revision 1.26
diff -u -b -r1.26 erl_code_util.m
--- compiler/erl_code_util.m	16 Aug 2011 03:26:30 -0000	1.26
+++ compiler/erl_code_util.m	2 Jul 2012 01:07:01 -0000
@@ -308,7 +308,7 @@
 
 erl_variable_type(Info, Var, Type) :-
     erl_gen_info_get_var_types(Info, VarTypes),
-    map.lookup(VarTypes, Var, Type).
+    lookup_var_type(VarTypes, Var, Type).
 
 erl_gen_info_add_env_var_name(Name, !Info) :-
     EnvVarNames0 = !.Info ^ egi_env_var_names,
@@ -408,7 +408,7 @@
 
 is_bound_and_not_dummy(ModuleInfo, VarTypes, InstMap, InstmapDelta, Var) :-
     var_is_bound_in_instmap_delta(ModuleInfo, InstMap, InstmapDelta, Var),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     check_dummy_type(ModuleInfo, Type) = is_not_dummy_type.
 
 erl_bind_unbound_vars(Info, VarsToBind, Goal, InstMap,
@@ -524,7 +524,7 @@
 
 erl_var_or_dummy_replacement(ModuleInfo, VarTypes, DummyVarReplacement, Var) =
     (if
-        map.search(VarTypes, Var, Type),
+        search_var_type(VarTypes, Var, Type),
         check_dummy_type(ModuleInfo, Type) = is_dummy_type
     then
         elds_term(DummyVarReplacement)
Index: compiler/exception_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/exception_analysis.m,v
retrieving revision 1.60
diff -u -b -r1.60 exception_analysis.m
--- compiler/exception_analysis.m	11 Apr 2012 04:52:34 -0000	1.60
+++ compiler/exception_analysis.m	2 Jul 2012 01:07:01 -0000
@@ -465,7 +465,7 @@
         % Handle (mutually-)recursive calls.
         list.member(CallPPId, SCC)
     ->
-        Types = list.map((func(Var) = VarTypes ^ det_elem(Var)), CallArgs),
+        lookup_var_types(VarTypes, CallArgs, Types),
         TypeStatus = check_types(!.ModuleInfo, Types),
         combine_type_status(TypeStatus, !.Result ^ rec_calls, NewTypeStatus),
         !Result ^ rec_calls := NewTypeStatus
@@ -773,7 +773,7 @@
     maybe(analysis_status)::in, proc_result::in, proc_result::out) is det.
 
 check_vars(ModuleInfo, VarTypes, Vars, MaybeAnalysisStatus, !Result) :-
-    Types = list.map((func(Var) = VarTypes ^ det_elem(Var)), Vars),
+    lookup_var_types(VarTypes, Vars, Types),
     TypeStatus = check_types(ModuleInfo, Types),
     (
         TypeStatus = type_will_not_throw
Index: compiler/float_regs.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/float_regs.m,v
retrieving revision 1.2
diff -u -b -r1.2 float_regs.m
--- compiler/float_regs.m	23 Apr 2012 03:34:47 -0000	1.2
+++ compiler/float_regs.m	2 Jul 2012 01:07:01 -0000
@@ -835,7 +835,7 @@
         !Info, !Specs) :-
     lambda_info_get_module_info(!.Info, ModuleInfo),
     lambda_info_get_vartypes(!.Info, VarTypes),
-    map.lookup(VarTypes, CellVar, CellType),
+    lookup_var_type(VarTypes, CellVar, CellType),
     (
         % Replace all type parameters by phony type variables.
         % See EXAMPLE 3 at the top of the file.
@@ -1053,7 +1053,7 @@
 insert_reg_wrappers_switch(Var, Cases0, Cases, NonLocals, InstMap0, InstMap,
         !Info, !Specs) :-
     lambda_info_get_vartypes(!.Info, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     list.map2_foldl2(insert_reg_wrappers_case(Var, Type, InstMap0),
         Cases0, Cases1, InstMaps1, !Info, !Specs),
     common_instmap_delta(InstMap0, NonLocals, InstMaps1, CommonDelta, !Info),
@@ -1153,7 +1153,7 @@
         WrapGoals, InstMap0, Context, !Info, !Specs) :-
     lambda_info_get_module_info(!.Info, ModuleInfo),
     lambda_info_get_vartypes(!.Info, VarTypes),
-    map.lookup(VarTypes, CallVar, CallVarType),
+    lookup_var_type(VarTypes, CallVar, CallVarType),
     instmap_lookup_var(InstMap0, CallVar, CallVarInst),
     type_is_higher_order_details_det(CallVarType, _, PredOrFunc, _, ArgTypes),
     list.length(ArgTypes, Arity),
@@ -1298,7 +1298,7 @@
             ArgPredArgTypes),
         ArgPredArgTypes = [_ | _]
     ->
-        map.lookup(VarTypes, OrigVar, OrigVarType),
+        lookup_var_type(VarTypes, OrigVar, OrigVarType),
         type_is_higher_order_details_det(OrigVarType, _, _, _,
             OrigPredArgTypes),
         list.length(OrigPredArgTypes, Arity),
@@ -1530,7 +1530,7 @@
     ( inst_is_free(ModuleInfo, ExpectInst) ->
         true
     ;
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         match_arg(InstMap0, Context, VarType, ExpectInst, Var, SubstVar,
             [], WrapGoals, !Info, !Specs),
         ( Var = SubstVar ->
@@ -1554,7 +1554,7 @@
     lambda_info_get_vartypes(!.Info, VarTypes0),
     lambda_info_get_module_info(!.Info, ModuleInfo0),
 
-    map.lookup(VarTypes0, OrigVar, OrigVarType),
+    lookup_var_type(VarTypes0, OrigVar, OrigVarType),
     type_is_higher_order_details_det(OrigVarType, Purity, PredOrFunc,
         EvalMethod, PredArgTypes),
 
@@ -1579,7 +1579,7 @@
 
     % Create the replacement variable Var.
     varset.new_var(Var, VarSet1, VarSet),
-    map.det_insert(Var, OrigVarType, VarTypes1, VarTypes),
+    add_var_type(Var, OrigVarType, VarTypes1, VarTypes),
     lambda_info_set_varset(VarSet, !Info),
     lambda_info_set_vartypes(VarTypes, !Info),
 
@@ -1624,7 +1624,7 @@
 create_fresh_vars([], [], !VarSet, !VarTypes).
 create_fresh_vars([Type | Types], [Var | Vars], !VarSet, !VarTypes) :-
     varset.new_var(Var, !VarSet),
-    map.det_insert(Var, Type, !VarTypes),
+    add_var_type(Var, Type, !VarTypes),
     create_fresh_vars(Types, Vars, !VarSet, !VarTypes).
 
 :- pred make_reg_r_headvars(vartypes::in, prog_var::in, ho_arg_reg::in,
@@ -1633,7 +1633,7 @@
 make_reg_r_headvars(VarTypes, Var, RegType, !RegR_HeadVars) :-
     (
         RegType = ho_arg_reg_r,
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         ( VarType = float_type ->
             set_of_var.insert(Var, !RegR_HeadVars)
         ;
Index: compiler/follow_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/follow_vars.m,v
retrieving revision 1.98
diff -u -b -r1.98 follow_vars.m
--- compiler/follow_vars.m	13 Feb 2012 00:11:37 -0000	1.98
+++ compiler/follow_vars.m	2 Jul 2012 01:07:01 -0000
@@ -261,7 +261,7 @@
             ; GenericCall = event_call(_)
             ),
             determinism_to_code_model(Det, CodeModel),
-            map.apply_to_list(Args, VarTypes, Types),
+            lookup_var_types(VarTypes, Args, Types),
             generic_call_arg_reg_types(ModuleInfo, VarTypes, GenericCall,
                 Args, MaybeArgRegs, ArgRegTypes),
             make_arg_infos(Types, Modes, ArgRegTypes, CodeModel, ModuleInfo,
Index: compiler/format_call.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/format_call.m,v
retrieving revision 1.31
diff -u -b -r1.31 format_call.m
--- compiler/format_call.m	23 Apr 2012 03:34:47 -0000	1.31
+++ compiler/format_call.m	2 Jul 2012 01:07:01 -0000
@@ -1319,7 +1319,7 @@
     ;
         MaybeResultVar = no,
         varset.new_var(ResultVar, !VarSet),
-        map.det_insert(ResultVar, string_type, !VarTypes)
+        add_var_type(ResultVar, string_type, !VarTypes)
     ).
 
 :- pred represent_component(module_info::in, string_component::in,
Index: compiler/goal_path.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_path.m,v
retrieving revision 1.64
diff -u -b -r1.64 goal_path.m
--- compiler/goal_path.m	13 Feb 2012 00:11:38 -0000	1.64
+++ compiler/goal_path.m	2 Jul 2012 01:07:01 -0000
@@ -178,7 +178,7 @@
         GoalExpr0 = switch(Var, CanFail, Cases0),
         VarTypes = SlotInfo ^ slot_info_vartypes,
         ModuleInfo = SlotInfo ^ slot_info_module_info,
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         ( switch_type_num_functors(ModuleInfo, Type, NumFunctors) ->
             MaybeNumFunctors = known_num_functors_in_type(NumFunctors)
         ;
@@ -324,7 +324,7 @@
         GoalExpr0 = switch(Var, CanFail, Cases0),
         VarTypes = SlotInfo ^ slot_info_vartypes,
         ModuleInfo = SlotInfo ^ slot_info_module_info,
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         ( switch_type_num_functors(ModuleInfo, Type, NumFunctors) ->
             MaybeNumFunctors = known_num_functors_in_type(NumFunctors)
         ;
Index: compiler/goal_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_util.m,v
retrieving revision 1.194
diff -u -b -r1.194 goal_util.m
--- compiler/goal_util.m	13 Feb 2012 00:11:38 -0000	1.194
+++ compiler/goal_util.m	2 Jul 2012 01:07:01 -0000
@@ -482,8 +482,8 @@
 create_renaming_2([OrigVar | OrigVars], InstMapDelta, !VarSet, !VarTypes,
         !RevUnifies, !RevNewVars, !Renaming) :-
     varset.new_var(NewVar, !VarSet),
-    map.lookup(!.VarTypes, OrigVar, Type),
-    map.det_insert(NewVar, Type, !VarTypes),
+    lookup_var_type(!.VarTypes, OrigVar, Type),
+    add_var_type(NewVar, Type, !VarTypes),
     ( instmap_delta_search_var(InstMapDelta, OrigVar, DeltaInst) ->
         NewInst = DeltaInst
     ;
@@ -518,10 +518,11 @@
             true
         ),
         map.det_insert(Var, CloneVar, !Renaming),
-        ( map.search(OldVarTypes, Var, VarType) ->
-            map.set(CloneVar, VarType, !VarTypes)
+        ( search_var_type(OldVarTypes, Var, VarType) ->
+            add_var_type(CloneVar, VarType, !VarTypes)
         ;
-            % XXX This should never happen after typechecking.
+            % This should never happen after typechecking, but may happen
+            % before it.
             true
         )
     ).
@@ -814,7 +815,7 @@
     % quantified or type vars that appear in the type of a non-local prog_var.
 
     set_of_var.to_sorted_list(NonLocals, NonLocalsList),
-    map.apply_to_list(NonLocalsList, VarTypes, NonLocalsTypes),
+    lookup_var_types(VarTypes, NonLocalsList, NonLocalsTypes),
     type_vars_list(NonLocalsTypes, NonLocalTypeVarsList0),
     NonLocalTypeVarsList = ExistQVars ++ NonLocalTypeVarsList0,
     set_of_var.list_to_set(NonLocalTypeVarsList, NonLocalTypeVars),
@@ -1378,9 +1379,9 @@
         !ModuleInfo) :-
     ConsArity = cons_id_arity(ConsId),
     varset.new_vars(ConsArity, ArgVars, !VarSet),
-    map.lookup(!.VarTypes, Var, VarType),
+    lookup_var_type(!.VarTypes, Var, VarType),
     type_util.get_cons_id_arg_types(!.ModuleInfo, VarType, ConsId, ArgTypes),
-    map.det_insert_from_corresponding_lists(ArgVars, ArgTypes, !VarTypes),
+    vartypes_add_corresponding_lists(ArgVars, ArgTypes, !VarTypes),
     instmap_lookup_var(InstMap, Var, Inst0),
     (
         inst_expand(!.ModuleInfo, Inst0, Inst1),
Index: compiler/hhf.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hhf.m,v
retrieving revision 1.48
diff -u -b -r1.48 hhf.m
--- compiler/hhf.m	8 Jun 2012 15:36:55 -0000	1.48
+++ compiler/hhf.m	2 Jul 2012 01:07:01 -0000
@@ -141,7 +141,8 @@
 convert_clauses_info_to_hhf(Simple, ModuleInfo, !ClausesInfo, InstGraph) :-
     clauses_info_get_varset(!.ClausesInfo, VarSet0),
     clauses_info_get_vartypes(!.ClausesInfo, VarTypes0),
-    inst_graph.init(VarTypes0 ^ keys, InstGraph0),
+    vartypes_vars(VarTypes0, Vars0),
+    inst_graph.init(Vars0, InstGraph0),
     Info0 = hhf_info(InstGraph0, VarSet0, VarTypes0),
 
     clauses_info_get_headvar_list(!.ClausesInfo, HeadVars),
@@ -169,7 +170,8 @@
     Info = hhf_info(InstGraph1, VarSet, VarTypes),
     (
         Simple = yes,
-        inst_graph.init(VarTypes ^ keys, InstGraph)
+        vartypes_vars(VarTypes, Vars),
+        inst_graph.init(Vars, InstGraph)
     ;
         Simple = no,
         InstGraph = InstGraph1
@@ -368,8 +370,8 @@
         VarSet0 = !.HI ^ hhfi_varset,
         VarTypes0 = !.HI ^ hhfi_vartypes,
         varset.new_var(V, VarSet0, VarSet),
-        map.lookup(VarTypes0, A, Type),
-        map.det_insert(V, Type, VarTypes0, VarTypes),
+        lookup_var_type(VarTypes0, A, Type),
+        add_var_type(V, Type, VarTypes0, VarTypes),
         map.init(Empty),
         map.det_insert(V, node(Empty, top_level), InstGraph0, InstGraph),
         !HI ^ hhfi_varset := VarSet,
@@ -398,7 +400,7 @@
 complete_inst_graph_node(ModuleInfo, BaseVars, Var, !HI) :-
     VarTypes0 = !.HI ^ hhfi_vartypes,
     (
-        map.search(VarTypes0, Var, Type),
+        search_var_type(VarTypes0, Var, Type),
         type_constructors(ModuleInfo, Type, Constructors),
         type_to_ctor(Type, TypeCtor)
     ->
@@ -452,7 +454,7 @@
         NewVar = NewVar0
     ;
         varset.new_var(NewVar, VarSet0, VarSet),
-        map.det_insert(NewVar, ArgType, VarTypes0, VarTypes),
+        add_var_type(NewVar, ArgType, VarTypes0, VarTypes),
         map.init(Empty),
         map.det_insert(NewVar, node(Empty, parent(Var)),
             InstGraph0, InstGraph),
@@ -465,7 +467,7 @@
 
 find_var_with_type(Var0, Type, InstGraph, VarTypes, BaseVars, Var) :-
     (
-        map.search(VarTypes, Var0, Type0),
+        search_var_type(VarTypes, Var0, Type0),
         same_type(Type0, Type)
     ->
         Var = Var0
Index: compiler/higher_order.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/higher_order.m,v
retrieving revision 1.204
diff -u -b -r1.204 higher_order.m
--- compiler/higher_order.m	11 Jun 2012 03:13:20 -0000	1.204
+++ compiler/higher_order.m	2 Jul 2012 01:07:01 -0000
@@ -1302,7 +1302,7 @@
         proc(PredId, ProcId) = PredProcId,
         map.contains(NewPredMap, PredProcId),
         proc_info_get_vartypes(ProcInfo0, VarTypes0),
-        map.lookup(VarTypes0, LVar, LVarType),
+        lookup_var_type(VarTypes0, LVar, LVarType),
         type_is_higher_order_details(LVarType, _, _, _, ArgTypes)
     ->
         % Create variables to represent
@@ -1356,7 +1356,7 @@
             % The dummy arguments can't be used anywhere.
             ProcInfo2 = !.Info ^ hoi_proc_info,
             proc_info_get_vartypes(ProcInfo2, VarTypes2),
-            map.delete_list(UncurriedArgs, VarTypes2, VarTypes),
+            delete_var_types(UncurriedArgs, VarTypes2, VarTypes),
             proc_info_set_vartypes(VarTypes, ProcInfo2, ProcInfo),
             !Info ^ hoi_proc_info := ProcInfo,
 
@@ -1416,7 +1416,7 @@
     pred_info_get_import_status(CalleePredInfo, CalleeStatus),
     proc_info_get_vartypes(CalleeProcInfo, CalleeVarTypes),
     proc_info_get_headvars(CalleeProcInfo, CalleeHeadVars),
-    map.apply_to_list(CalleeHeadVars, CalleeVarTypes, CalleeArgTypes),
+    lookup_var_types(CalleeVarTypes, CalleeHeadVars, CalleeArgTypes),
 
     CallerProcInfo0 = !.Info ^ hoi_proc_info,
     proc_info_get_vartypes(CallerProcInfo0, VarTypes),
@@ -1439,7 +1439,7 @@
         ;
             !.Info ^ hoi_global_info ^ hogi_params ^ param_do_user_type_spec
                 = yes,
-            map.apply_to_list(Args0, VarTypes, ArgTypes),
+            lookup_var_types(VarTypes, Args0, ArgTypes),
 
             % Check whether any typeclass constraints now match an instance.
             pred_info_get_class_context(CalleePredInfo, CalleeClassContext),
@@ -1538,7 +1538,7 @@
     ->
         % Find any known higher-order arguments in the list of curried
         % arguments.
-        map.apply_to_list(CurriedArgs, VarTypes, CurriedArgTypes),
+        lookup_var_types(VarTypes, CurriedArgs, CurriedArgTypes),
         list.map(rtti_varmaps_var_info(RttiVarMaps), CurriedArgs,
             CurriedArgRttiInfo),
         ( ConsId = closure_cons(ShroudedPredProcId, _) ->
@@ -1667,7 +1667,7 @@
     compute_extra_typeinfos(Info, Args, ExtraTypeInfoTVars),
 
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(Args0, VarTypes, CallArgTypes),
+    lookup_var_types(VarTypes, Args0, CallArgTypes),
     pred_info_get_typevarset(PredInfo, TVarSet),
 
     Request = ho_request(Caller, proc(CalledPred, CalledProc), Args0,
@@ -1739,7 +1739,7 @@
     % which will vary between calls).
     ProcInfo = Info ^ hoi_proc_info,
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(Args, VarTypes, ArgTypes),
+    lookup_var_types(VarTypes, Args, ArgTypes),
     type_vars_list(ArgTypes, AllTVars),
     (
         AllTVars = [],
@@ -2086,8 +2086,8 @@
 
             % Sanity check.
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.lookup(VarTypes, OutputVar, OutputVarType),
-            map.lookup(VarTypes, SelectedArg, SelectedArgType),
+            lookup_var_type(VarTypes, OutputVar, OutputVarType),
+            lookup_var_type(VarTypes, SelectedArg, SelectedArgType),
             ( OutputVarType = SelectedArgType ->
                 true
             ;
@@ -2207,7 +2207,7 @@
     PredArity = pred_info_orig_arity(CalledPredInfo),
     special_pred_name_arity(SpecialId, PredName, _, PredArity),
     special_pred_get_type(SpecialId, Args, Var),
-    map.lookup(VarTypes, Var, SpecialPredType),
+    lookup_var_type(VarTypes, Var, SpecialPredType),
     SpecialPredType \= type_variable(_, _),
 
     % Don't specialize tuple types -- the code to unify them only exists
@@ -2789,7 +2789,7 @@
     pred_info_get_class_context(PredInfo0, ClassContext),
     pred_info_get_var_name_remap(PredInfo0, VarNameRemap),
     varset.init(EmptyVarSet),
-    map.init(EmptyVarTypes),
+    init_vartypes(EmptyVarTypes),
     map.init(EmptyTVarNameMap),
     map.init(EmptyProofs),
     map.init(EmptyConstraintMap),
@@ -3132,7 +3132,7 @@
     proc_info_reset_imported_structure_reuse(!NewProcInfo),
 
     proc_info_get_vartypes(!.NewProcInfo, VarTypes7),
-    map.apply_to_list(ExtraHeadVars, VarTypes7, ExtraHeadVarTypes0),
+    lookup_var_types(VarTypes7, ExtraHeadVars, ExtraHeadVarTypes0),
     remove_const_higher_order_args(1, OriginalArgTypes,
         HOArgs, ModifiedOriginalArgTypes),
     list.condense([ExtraTypeInfoTypes, ExtraHeadVarTypes0,
@@ -3156,7 +3156,7 @@
         ExistQVars = []
     ;
         ExistQVars = [_ | _],
-        map.apply_to_list(HeadVars0, VarTypes7, OriginalHeadTypes),
+        lookup_var_types(VarTypes7, HeadVars0, OriginalHeadTypes),
         (
             type_list_subsumes(OriginalArgTypes,
                 OriginalHeadTypes, ExistentialSubn)
@@ -3190,7 +3190,8 @@
 
 update_var_types(VarAndType, !VarTypes) :-
     VarAndType = Var - Type,
-    map.det_update(Var, Type, !VarTypes).
+    update_var_type(Var, Type, !VarTypes).
 
     % Take an original list of headvars and arg_modes and return these
     % with curried arguments added.  The old higher-order arguments are
Index: compiler/hlds_clauses.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_clauses.m,v
retrieving revision 1.14
diff -u -b -r1.14 hlds_clauses.m
--- compiler/hlds_clauses.m	23 May 2011 05:08:03 -0000	1.14
+++ compiler/hlds_clauses.m	2 Jul 2012 01:07:01 -0000
@@ -375,7 +375,7 @@
 %-----------------------------------------------------------------------------%
 
 clauses_info_init(PredOrFunc, Arity, ItemNumbers, ClausesInfo) :-
-    map.init(VarTypes),
+    init_vartypes(VarTypes),
     map.init(TVarNameMap),
     varset.init(VarSet0),
     make_n_fresh_vars("HeadVar__", Arity, HeadVars, VarSet0, VarSet),
@@ -388,7 +388,7 @@
 
 clauses_info_init_for_assertion(HeadVars, ClausesInfo) :-
     varset.init(VarSet),
-    map.init(VarTypes),
+    init_vartypes(VarTypes),
     map.init(TVarNameMap),
     % Procedures introduced for assertions are always predicates, never
     % functions.
Index: compiler/hlds_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_goal.m,v
retrieving revision 1.238
diff -u -b -r1.238 hlds_goal.m
--- compiler/hlds_goal.m	23 Apr 2012 03:34:47 -0000	1.238
+++ compiler/hlds_goal.m	2 Jul 2012 01:07:01 -0000
@@ -3577,31 +3577,31 @@
 make_int_const_construction_alloc(Int, MaybeName, Goal, Var,
         !VarSet, !VarTypes) :-
     varset.new_maybe_named_var(MaybeName, Var, !VarSet),
-    map.det_insert(Var, int_type, !VarTypes),
+    add_var_type(Var, int_type, !VarTypes),
     make_int_const_construction(Var, Int, Goal).
 
 make_string_const_construction_alloc(String, MaybeName, Goal, Var,
         !VarSet, !VarTypes) :-
     varset.new_maybe_named_var(MaybeName, Var, !VarSet),
-    map.det_insert(Var, string_type, !VarTypes),
+    add_var_type(Var, string_type, !VarTypes),
     make_string_const_construction(Var, String, Goal).
 
 make_float_const_construction_alloc(Float, MaybeName, Goal, Var,
         !VarSet, !VarTypes) :-
     varset.new_maybe_named_var(MaybeName, Var, !VarSet),
-    map.det_insert(Var, float_type, !VarTypes),
+    add_var_type(Var, float_type, !VarTypes),
     make_float_const_construction(Var, Float, Goal).
 
 make_char_const_construction_alloc(Char, MaybeName, Goal, Var,
         !VarSet, !VarTypes) :-
     varset.new_maybe_named_var(MaybeName, Var, !VarSet),
-    map.det_insert(Var, char_type, !VarTypes),
+    add_var_type(Var, char_type, !VarTypes),
     make_char_const_construction(Var, Char, Goal).
 
 make_const_construction_alloc(ConsId, Type, MaybeName, Goal, Var,
         !VarSet, !VarTypes) :-
     varset.new_maybe_named_var(MaybeName, Var, !VarSet),
-    map.det_insert(Var, Type, !VarTypes),
+    add_var_type(Var, Type, !VarTypes),
     make_const_construction(Var, ConsId, Goal).
 
 make_int_const_construction(Var, Int, Goal) :-
Index: compiler/hlds_out_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out_goal.m,v
retrieving revision 1.19
diff -u -b -r1.19 hlds_out_goal.m
--- compiler/hlds_out_goal.m	8 Jun 2012 15:36:56 -0000	1.19
+++ compiler/hlds_out_goal.m	2 Jul 2012 01:07:01 -0000
@@ -859,7 +859,7 @@
     io.write_string(" = ", !IO),
     (
         TypeQual = varset_vartypes(_, VarTypes),
-        map.lookup(VarTypes, LHS, UniType),
+        lookup_var_type(VarTypes, LHS, UniType),
         VarType = yes(UniType)
     ;
         TypeQual = no_varset_vartypes,
@@ -1297,7 +1297,7 @@
             MaybeUnifyContext = yes(CallUnifyContext),
             (
                 TypeQual = varset_vartypes(_, VarTypes),
-                map.lookup(VarTypes, Var, UniType),
+                lookup_var_type(VarTypes, Var, UniType),
                 VarType = yes(UniType)
             ;
                 TypeQual = no_varset_vartypes,
Index: compiler/hlds_out_pred.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out_pred.m,v
retrieving revision 1.12
diff -u -b -r1.12 hlds_out_pred.m
--- compiler/hlds_out_pred.m	23 Apr 2012 03:34:47 -0000	1.12
+++ compiler/hlds_out_pred.m	2 Jul 2012 01:07:01 -0000
@@ -522,11 +522,11 @@
     vartypes::in, tvarset::in, io::di, io::uo) is det.
 
 write_var_types(Indent, VarSet, AppendVarNums, VarTypes, TVarSet, !IO) :-
-    map.count(VarTypes, NumVarTypes),
+    vartypes_count(VarTypes, NumVarTypes),
     write_indent(Indent, !IO),
     io.write_string("% variable types map ", !IO),
     io.format("(%d entries):\n", [i(NumVarTypes)], !IO),
-    map.keys(VarTypes, Vars),
+    vartypes_vars(VarTypes, Vars),
     write_var_types_loop(Vars, Indent, VarSet, AppendVarNums, VarTypes,
         TVarSet, !IO).
 
@@ -536,7 +536,7 @@
 write_var_types_loop([], _, _, _, _, _, !IO).
 write_var_types_loop([Var | Vars], Indent, VarSet, AppendVarNums, VarTypes,
         TypeVarSet, !IO) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     write_indent(Indent, !IO),
     io.write_string("% ", !IO),
     mercury_output_var(VarSet, AppendVarNums, Var, !IO),
Index: compiler/hlds_pred.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_pred.m,v
retrieving revision 1.281
diff -u -b -r1.281 hlds_pred.m
--- compiler/hlds_pred.m	7 Jun 2012 14:04:38 -0000	1.281
+++ compiler/hlds_pred.m	2 Jul 2012 01:07:01 -0000
@@ -1225,7 +1225,7 @@
     goal_util.goal_vars(Goal0, GoalVars0),
     set_of_var.insert_list(ArgVars, GoalVars0, GoalVars),
     GoalVarsSet = set_of_var.bitset_to_set(GoalVars),
-    map.select(VarTypes0, GoalVarsSet, VarTypes),
+    vartypes_select(GoalVarsSet, VarTypes0, VarTypes),
     varset.select(GoalVarsSet, VarSet0, VarSet),
 
     % Approximate the termination information for the new procedure.
@@ -1261,7 +1261,7 @@
 compute_arg_types_modes([], _, _, _, [], []).
 compute_arg_types_modes([Var | Vars], VarTypes, InstMap0, InstMap,
         [Type | Types], [Mode | Modes]) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     instmap_lookup_var(InstMap0, Var, Inst0),
     instmap_lookup_var(InstMap, Var, Inst),
     Mode = (Inst0 -> Inst),
@@ -2569,7 +2569,7 @@
 
     make_n_fresh_vars("HeadVar__", Arity, HeadVars, varset.init, BodyVarSet),
     varset.init(InstVarSet),
-    map.from_corresponding_lists(HeadVars, Types, BodyTypes),
+    vartypes_from_corresponding_lists(HeadVars, Types, BodyTypes),
     ModeErrors = [],
     InferredDet = detism_erroneous,
     map.init(StackSlots),
@@ -2877,7 +2877,7 @@
 
 proc_info_ensure_unique_names(!ProcInfo) :-
     proc_info_get_vartypes(!.ProcInfo, VarTypes),
-    map.keys(VarTypes, AllVars),
+    vartypes_vars(VarTypes, AllVars),
     proc_info_get_varset(!.ProcInfo, VarSet0),
     varset.ensure_unique_names(AllVars, "p", VarSet0, VarSet),
     proc_info_set_varset(VarSet, !ProcInfo).
@@ -2886,7 +2886,7 @@
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     varset.new_maybe_named_var(MaybeName, NewVar, VarSet0, VarSet),
-    map.det_insert(NewVar, Type, VarTypes0, VarTypes),
+    add_var_type(NewVar, Type, VarTypes0, VarTypes),
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo).
 
@@ -2895,8 +2895,7 @@
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     varset.new_vars(NumVars, NewVars, VarSet0, VarSet),
-    map.det_insert_from_corresponding_lists(NewVars, Types,
-        VarTypes0, VarTypes),
+    vartypes_add_corresponding_lists(NewVars, Types, VarTypes0, VarTypes),
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo).
 
@@ -2907,7 +2906,7 @@
     assoc_list.from_corresponding_lists(HeadVars, ArgModes, HeadVarModes),
     IsInstChanged = (pred(VarMode::in, Var::out) is semidet :-
         VarMode = Var - Mode,
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         mode_get_insts(ModuleInfo, Mode, Inst1, Inst2),
         \+ inst_matches_binding(Inst1, Inst2, Type, ModuleInfo)
     ),
@@ -2921,7 +2920,7 @@
     assoc_list.from_corresponding_lists(HeadVars, ArgModes, HeadVarModes),
     IsInstUnchanged = (pred(VarMode::in, Var::out) is semidet :-
         VarMode = Var - Mode,
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         mode_get_insts(ModuleInfo, Mode, Inst1, Inst2),
         inst_matches_binding(Inst1, Inst2, Type, ModuleInfo)
     ),
@@ -3019,7 +3018,7 @@
 proc_info_has_io_state_pair_2([Var - Mode | VarModes], ModuleInfo, VarTypes,
         ArgNum, !MaybeIn, !MaybeOut) :-
     (
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         type_is_io_state(VarType)
     ->
         ( mode_is_fully_input(ModuleInfo, Mode) ->
@@ -3057,7 +3056,7 @@
 proc_info_has_higher_order_arg_from_details(ModuleInfo, VarTypes,
         [HeadVar | HeadVars]) :-
     (
-        map.lookup(VarTypes, HeadVar, VarType),
+        lookup_var_type(VarTypes, HeadVar, VarType),
         type_is_higher_order(VarType)
     ;
         proc_info_has_higher_order_arg_from_details(ModuleInfo, VarTypes,
@@ -3102,11 +3101,11 @@
     ensure_all_headvars_are_named_2(Vars, SeqNum + 1, !VarSet).
 
 var_is_of_dummy_type(ModuleInfo, VarTypes, Var) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     check_dummy_type(ModuleInfo, Type) = is_dummy_type.
 
 var_is_of_non_dummy_type(ModuleInfo, VarTypes, Var) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     check_dummy_type(ModuleInfo, Type) = is_not_dummy_type.
 
 %-----------------------------------------------------------------------------%
Index: compiler/hlds_rtti.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_rtti.m,v
retrieving revision 1.26
diff -u -b -r1.26 hlds_rtti.m
--- compiler/hlds_rtti.m	21 Jul 2011 06:58:25 -0000	1.26
+++ compiler/hlds_rtti.m	2 Jul 2012 01:07:01 -0000
@@ -871,7 +871,7 @@
 
 get_typeinfo_vars_2([], _, _, []).
 get_typeinfo_vars_2([Var | Vars], VarTypes, TVarMap, TypeInfoVars) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     type_vars(Type, TypeVars),
     (
         TypeVars = [],
Index: compiler/inlining.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/inlining.m,v
retrieving revision 1.176
diff -u -b -r1.176 inlining.m
--- compiler/inlining.m	13 Feb 2012 00:11:40 -0000	1.176
+++ compiler/inlining.m	2 Jul 2012 01:07:01 -0000
@@ -786,8 +786,8 @@
     % for type variables which have been substituted away,
     % because those entries simply won't be used.
 
-    map.apply_to_list(HeadVars, CalleeVarTypes1, HeadTypes),
-    map.apply_to_list(ArgVars, VarTypes0, ArgTypes),
+    lookup_var_types(CalleeVarTypes1, HeadVars, HeadTypes),
+    lookup_var_types(VarTypes0, ArgVars, ArgTypes),
 
     pred_info_get_exist_quant_tvars(PredInfo, CalleeExistQVars),
     get_type_substitution(HeadTypes, ArgTypes, HeadTypeParams,
Index: compiler/instmap.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/instmap.m,v
retrieving revision 1.74
diff -u -b -r1.74 instmap.m
--- compiler/instmap.m	24 Apr 2012 09:18:23 -0000	1.74
+++ compiler/instmap.m	2 Jul 2012 01:07:01 -0000
@@ -520,7 +520,7 @@
 
     instmap_lookup_var(InstMapA, VarB, InitialInst),
     instmap_lookup_var(InstMapB, VarB, FinalInst),
-    map.lookup(VarTypes, VarB, Type),
+    lookup_var_type(VarTypes, VarB, Type),
 
     ( inst_matches_final_typed(InitialInst, FinalInst, Type, ModuleInfo) ->
         ChangedVars = ChangedVars0
@@ -926,7 +926,7 @@
         !ModuleInfo, !:ErrorList) :-
     merge_insts_of_vars(Vars, InstMapList, VarTypes, !InstMapping,
         !ModuleInfo, !:ErrorList),
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     list.map(lookup_var_in_instmap(Var), InstMapList, InstList),
     merge_var_insts(InstList, VarType, !ModuleInfo, MaybeInst),
     (
@@ -1260,7 +1260,7 @@
         % The right fix for this would be to generalize inst_matches_binding,
         % to allow the caller to specify what kinds of deviations from an exact
         % syntactic match are ok.
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         inst_matches_binding(NewInst, OldInst, Type, ModuleInfo)
     ;
         % If the instmap delta doesn't contain the variable, it may still
@@ -1314,8 +1314,8 @@
         instmap_lookup_var(InstMap, Var, InstB)
     ),
     (
-        inst_merge(InstA, InstB, yes(VarTypes ^ det_elem(Var)), Inst1,
-            !ModuleInfo)
+        lookup_var_type(VarTypes, Var, VarType),
+        inst_merge(InstA, InstB, yes(VarType), Inst1, !ModuleInfo)
     ->
         % XXX Given instmap_lookup_var(InstMap, Var, OldInst),
         % we should probably set Inst not directly from Inst1, but
Index: compiler/intermod.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/intermod.m,v
retrieving revision 1.271
diff -u -b -r1.271 intermod.m
--- compiler/intermod.m	5 Jun 2012 15:14:26 -0000	1.271
+++ compiler/intermod.m	2 Jul 2012 01:07:01 -0000
@@ -233,7 +233,7 @@
     pred_info_get_clauses_info(PredInfo0, ClausesInfo0),
     (
         clauses_info_get_explicit_vartypes(ClausesInfo0, ExplicitVarTypes),
-        map.is_empty(ExplicitVarTypes),
+        vartypes_is_empty(ExplicitVarTypes),
         should_be_processed(ProcessLocalPreds, PredId, PredInfo0,
             TypeSpecForcePreds, InlineThreshold, HigherOrderSizeLimit,
             Deforestation, ModuleInfo0)
@@ -419,7 +419,7 @@
         [HeadVar | HeadVars], [ArgMode | ArgModes]) :-
     (
         mode_is_input(ModuleInfo, ArgMode),
-        map.lookup(VarTypes, HeadVar, Type),
+        lookup_var_type(VarTypes, HeadVar, Type),
         classify_type(ModuleInfo, Type) = ctor_cat_higher_order
     ;
         check_for_ho_input_args(ModuleInfo, VarTypes, HeadVars, ArgModes)
@@ -2109,7 +2109,7 @@
     set.init(Modules),
     set.init(Procs),
     set.init(ProcDecls),
-    map.init(VarTypes),
+    init_vartypes(VarTypes),
     varset.init(TVarSet),
     Instances = [],
     Types = [],
Index: compiler/interval.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/interval.m,v
retrieving revision 1.56
diff -u -b -r1.56 interval.m
--- compiler/interval.m	13 Feb 2012 00:11:40 -0000	1.56
+++ compiler/interval.m	2 Jul 2012 01:07:01 -0000
@@ -291,7 +291,7 @@
         IntParams = !.IntervalInfo ^ ii_interval_params,
         ModuleInfo = IntParams ^ ip_module_info,
         VarTypes = IntParams ^ ip_var_types,
-        map.apply_to_list(ArgVars, VarTypes, ArgTypes),
+        lookup_var_types(VarTypes, ArgVars, ArgTypes),
         arg_info.generic_call_arg_reg_types(ModuleInfo, VarTypes, GenericCall,
             ArgVars, MaybeArgRegs, ArgRegTypes),
         arg_info.compute_in_and_out_vars_sep_regs(ModuleInfo, ArgVars,
@@ -1118,8 +1118,8 @@
         !VarRename, !VoidRename) :-
     varset.lookup_name(!.VarSet, Arg, Name),
     varset.new_named_var(Name, Shadow, !VarSet),
-    map.lookup(!.VarTypes, Arg, Type),
-    map.det_insert(Shadow, Type, !VarTypes),
+    lookup_var_type(!.VarTypes, Arg, Type),
+    add_var_type(Shadow, Type, !VarTypes),
     ( set_of_var.member(VarsToExtract, Arg) ->
         map.det_insert(Arg, Shadow, !VarRename)
     ;
Index: compiler/lambda.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lambda.m,v
retrieving revision 1.158
diff -u -b -r1.158 lambda.m
--- compiler/lambda.m	25 Jun 2012 02:40:30 -0000	1.158
+++ compiler/lambda.m	2 Jul 2012 01:07:01 -0000
@@ -424,7 +424,7 @@
     % Note currently we only allow lambda expressions to have universally
     % quantified constraints.
     rtti_varmaps_reusable_constraints(RttiVarMaps, AllConstraints),
-    map.apply_to_list(Vars, VarTypes, LambdaVarTypeList),
+    lookup_var_types(VarTypes, Vars, LambdaVarTypeList),
     list.map(type_vars, LambdaVarTypeList, LambdaTypeVarsList),
     list.condense(LambdaTypeVarsList, LambdaTypeVars),
     list.filter(constraint_contains_vars(LambdaTypeVars),
@@ -604,7 +604,7 @@
         modes_to_uni_modes(ModuleInfo1, ArgModes1, ArgModes1, UniModes),
 
         list.append(ArgModes1, Modes, AllArgModes),
-        map.apply_to_list(AllArgVars, VarTypes, ArgTypes),
+        lookup_var_types(VarTypes, AllArgVars, ArgTypes),
         list.foldl_corresponding(check_lambda_arg_type_and_mode(ModuleInfo1),
             ArgTypes, AllArgModes, 0, _),
 
@@ -748,10 +748,10 @@
     mark_vars_as_used(HeadVars, VarUses0, VarUses1),
     find_used_vars_in_goal(Goal, VarUses1, VarUses),
 
-    map.to_assoc_list(!.VarTypes, VarTypesList0),
+    vartypes_to_assoc_list(!.VarTypes, VarTypesList0),
     filter_vartypes(VarTypesList0, [], RevVarTypesList, VarUses),
     list.reverse(RevVarTypesList, VarTypesList),
-    map.from_sorted_assoc_list(VarTypesList, !:VarTypes),
+    vartypes_from_sorted_assoc_list(VarTypesList, !:VarTypes),
 
     restrict_rtti_varmaps(VarUses, !RttiVarMaps).
 
Index: compiler/lco.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lco.m,v
retrieving revision 1.74
diff -u -b -r1.74 lco.m
--- compiler/lco.m	23 Apr 2012 03:34:48 -0000	1.74
+++ compiler/lco.m	2 Jul 2012 01:07:01 -0000
@@ -340,7 +340,7 @@
 
     proc_info_get_headvars(VariantProcInfo, HeadVars),
     proc_info_get_vartypes(VariantProcInfo, VarTypes),
-    map.apply_to_list(HeadVars, VarTypes, ArgTypes),
+    lookup_var_types(VarTypes, HeadVars, ArgTypes),
 
     some [!VariantPredInfo, !PredTable] (
         module_info_get_preds(!.ModuleInfo, !:PredTable),
@@ -401,7 +401,7 @@
             proc_info_get_vartypes(ProcInfo0, VarTypes0),
             proc_info_get_headvars(ProcInfo0, HeadVars),
             proc_info_get_argmodes(ProcInfo0, ArgModes),
-            map.apply_to_list(HeadVars, VarTypes0, ArgTypes),
+            lookup_var_types(VarTypes0, HeadVars, ArgTypes),
             arg_info.compute_in_and_out_vars(!.ModuleInfo, HeadVars,
                 ArgModes, ArgTypes, _InputHeadVars, OutputHeadVars),
             proc_info_get_inferred_determinism(ProcInfo0, CurProcDetism),
@@ -748,7 +748,7 @@
     unexpected($module, $pred, "mismatched lists").
 update_call_args(ModuleInfo, VarTypes, [CalleeMode | CalleeModes],
         [Arg | Args], !.UpdatedCallOutArgs, !:UpdatedArgs) :-
-    map.lookup(VarTypes, Arg, CalleeType),
+    lookup_var_type(VarTypes, Arg, CalleeType),
     mode_to_arg_mode(ModuleInfo, CalleeMode, CalleeType, ArgMode),
     (
         ArgMode = top_in,
@@ -786,7 +786,7 @@
         [CalleeMode | CalleeModes], !:InArgs, !:OutArgs, !:UnusedArgs) :-
     classify_proc_call_args(ModuleInfo, VarTypes, Args, CalleeModes,
         !:InArgs, !:OutArgs, !:UnusedArgs),
-    map.lookup(VarTypes, Arg, CalleeType),
+    lookup_var_type(VarTypes, Arg, CalleeType),
     mode_to_arg_mode(ModuleInfo, CalleeMode, CalleeType, ArgMode),
     (
         ArgMode = top_in,
@@ -815,7 +815,7 @@
         ArgNum + 1, MismatchesTail, UpdatedCallArgs, !Subst, !Info),
     (
         !.Info ^ lco_allow_float_addr = do_not_allow_float_addr,
-        map.lookup(!.Info ^ lco_var_types, CallArg, CallArgType),
+        lookup_var_type(!.Info ^ lco_var_types, CallArg, CallArgType),
         type_to_ctor(CallArgType, CallArgTypeCtor),
         CallArgTypeCtor = type_ctor(unqualified("float"), 0)
     ->
@@ -872,9 +872,9 @@
     HighLevelData = ConstInfo ^ lci_highlevel_data,
     (
         HighLevelData = no,
-        map.lookup(VarTypes0, Var, FieldType),
+        lookup_var_type(VarTypes0, Var, FieldType),
         AddrVarType = make_ref_type(FieldType),
-        map.det_insert(AddrVar, AddrVarType, VarTypes0, VarTypes)
+        add_var_type(AddrVar, AddrVarType, VarTypes0, VarTypes)
     ;
         HighLevelData = yes,
         % We set the type later when it is more convenient.
@@ -1024,7 +1024,7 @@
         % likely to be recomputed incorrectly.
         HighLevelData = ConstInfo ^ lci_highlevel_data,
         VarTypes0 = !.Info ^ lco_var_types,
-        map.lookup(VarTypes0, Var, VarType),
+        lookup_var_type(VarTypes0, Var, VarType),
         InstMapDelta0 = goal_info_get_instmap_delta(GoalInfo0),
         update_construct_args(Subst, HighLevelData, VarType, ConsId, 1,
             ArgVars, UpdatedArgVars, AddrFields, InstMapDelta0, InstMapDelta,
@@ -1089,7 +1089,7 @@
             BoundInst = bound_inst_with_free_arg(ConsId, ArgNum),
             FinalInst = bound(shared, inst_test_no_results, [BoundInst]),
             % We didn't do this when we initially created the variable.
-            map.det_insert(AddrVar, VarType, !VarTypes)
+            add_var_type(AddrVar, VarType, !VarTypes)
         ),
         instmap_delta_set_var(AddrVar, FinalInst, !InstMapDelta),
         map.det_insert(OrigVar, field_id(VarType, ConsId, ArgNum),
@@ -1180,7 +1180,7 @@
 make_addr_vars([HeadVar0 | HeadVars0], [Mode0 | Modes0],
         [HeadVar | HeadVars], [Mode | Modes], !.AddrOutArgs,
         NextOutArgNum, ModuleInfo, VarToAddr, !VarSet, !VarTypes) :-
-    map.lookup(!.VarTypes, HeadVar0, HeadVarType),
+    lookup_var_type(!.VarTypes, HeadVar0, HeadVarType),
     mode_to_arg_mode(ModuleInfo, Mode0, HeadVarType, ArgMode),
     (
         ArgMode = top_in,
@@ -1198,19 +1198,19 @@
             AddrName = "AddrOf" ++ Name,
             varset.new_named_var(AddrName, AddrVar, !VarSet),
             HeadVar = AddrVar,
-            map.lookup(!.VarTypes, HeadVar0, OldType),
+            lookup_var_type(!.VarTypes, HeadVar0, OldType),
             (
                 MaybeFieldId = no,
                 % For low-level data we replace the output argument with a
                 % store_at_ref_type(T) input argument.
-                map.det_insert(AddrVar, make_ref_type(OldType), !VarTypes),
+                add_var_type(AddrVar, make_ref_type(OldType), !VarTypes),
                 Mode = in_mode
             ;
                 MaybeFieldId = yes(field_id(AddrVarType, ConsId, ArgNum)),
                 % For high-level data we replace the output argument with a
                 % partially instantiated structure. The structure has one
                 % argument left unfilled.
-                map.det_insert(AddrVar, AddrVarType, !VarTypes),
+                add_var_type(AddrVar, AddrVarType, !VarTypes),
                 BoundInst = bound_inst_with_free_arg(ConsId, ArgNum),
                 InitialInst = bound(shared, inst_test_no_results, [BoundInst]),
                 Mode = (InitialInst -> ground_inst)
Index: compiler/live_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/live_vars.m,v
retrieving revision 1.153
diff -u -b -r1.153 live_vars.m
--- compiler/live_vars.m	13 Feb 2012 00:11:41 -0000	1.153
+++ compiler/live_vars.m	2 Jul 2012 01:07:01 -0000
@@ -99,7 +99,7 @@
 %-----------------------------------------------------------------------------%
 
 build_dummy_type_array(ModuleInfo, VarTypes, DummyTypeArray, DummyVars) :-
-    map.to_assoc_list(VarTypes, VarsTypes),
+    vartypes_to_assoc_list(VarTypes, VarsTypes),
     list.foldl(max_var_num, VarsTypes, 0, MaxVarNum),
     % We want to index the array with variable numbers, which will be from
     % 1 to MaxVarNum.
@@ -468,7 +468,7 @@
             ),
             ProcInfo = AllocData ^ ad_proc_info,
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.apply_to_list(ArgVars, VarTypes, Types),
+            lookup_var_types(VarTypes, ArgVars, Types),
             ModuleInfo = AllocData ^ ad_module_info,
             arg_info.partition_generic_call_args(ModuleInfo, ArgVars,
                 Types, Modes, _InVars, OutVars, _UnusedVars),
Index: compiler/liveness.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/liveness.m,v
retrieving revision 1.185
diff -u -b -r1.185 liveness.m
--- compiler/liveness.m	13 Feb 2012 00:11:41 -0000	1.185
+++ compiler/liveness.m	2 Jul 2012 01:07:01 -0000
@@ -316,7 +316,8 @@
         (
             varset.num_allocated(VarSet) =< DelayDeathMaxVars
         ;
-            map.count(VarTypes) =< DelayDeathMaxVars
+            vartypes_count(VarTypes, NumVars),
+            NumVars =< DelayDeathMaxVars
         ),
         pred_info_get_origin(PredInfo, Origin),
         Origin \= origin_special_pred(_)
@@ -1735,7 +1736,7 @@
     proc_info_get_headvars(ProcInfo, Vars),
     proc_info_get_argmodes(ProcInfo, Modes),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(Vars, VarTypes, Types),
+    lookup_var_types(VarTypes, Vars, Types),
     !:Liveness = set_of_var.init,
     ( initial_liveness_2(Vars, Modes, Types, ModuleInfo, !Liveness) ->
         true
@@ -1832,7 +1833,7 @@
 find_value_giving_occurrences([Var | Vars], LiveInfo, InstMapDelta,
         !ValueVars) :-
     VarTypes = LiveInfo ^ li_vartypes,
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     (
         instmap_delta_search_var(InstMapDelta, Var, Inst),
         ModuleInfo = LiveInfo ^ li_module_info,
Index: compiler/lookup_switch.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/lookup_switch.m,v
retrieving revision 1.97
diff -u -b -r1.97 lookup_switch.m
--- compiler/lookup_switch.m	17 Oct 2011 04:31:29 -0000	1.97
+++ compiler/lookup_switch.m	2 Jul 2012 01:07:01 -0000
@@ -192,7 +192,7 @@
     map.to_assoc_list(CaseSolnMap, CaseSolns),
     reset_to_position(CurPos, !CI),
     VarTypes = get_var_types(!.CI),
-    list.map(map.lookup(VarTypes), OutVars, OutTypes),
+    lookup_var_types(VarTypes, OutVars, OutTypes),
     ( project_all_to_one_solution(CaseSolns, CaseValuePairs) ->
         CaseConsts = all_one_soln(CaseValuePairs),
         assoc_list.values(CaseValuePairs, CaseValues)
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.359
diff -u -b -r1.359 mercury_to_mercury.m
--- compiler/mercury_to_mercury.m	11 Jun 2012 03:13:21 -0000	1.359
+++ compiler/mercury_to_mercury.m	2 Jul 2012 01:07:01 -0000
@@ -5221,7 +5221,7 @@
     io::di, io::uo) is det.
 
 write_type_of_var(VarTypes, TypeVarSet, Var, !IO):-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     mercury_output_type(TypeVarSet, no, VarType, !IO).
 
 %---------------------------------------------------------------------------%
Index: compiler/ml_accurate_gc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_accurate_gc.m,v
retrieving revision 1.10
diff -u -b -r1.10 ml_accurate_gc.m
--- compiler/ml_accurate_gc.m	8 Jun 2012 15:46:20 -0000	1.10
+++ compiler/ml_accurate_gc.m	2 Jul 2012 01:07:01 -0000
@@ -339,7 +339,7 @@
     GenLocalVarDecl =
         (func(Var) = VarDefn :-
             LocalVarName = ml_gen_var_name(VarSet, Var),
-            map.lookup(VarTypes, Var, LocalVarType),
+            lookup_var_type(VarTypes, Var, LocalVarType),
             VarDefn = ml_gen_mlds_var_decl(mlds_data_var(LocalVarName),
                 mercury_type_to_mlds_type(ModuleInfo, LocalVarType),
                 gc_no_stmt, MLDS_Context)
Index: compiler/ml_closure_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_closure_gen.m,v
retrieving revision 1.68
diff -u -b -r1.68 ml_closure_gen.m
--- compiler/ml_closure_gen.m	23 May 2011 05:08:06 -0000	1.68
+++ compiler/ml_closure_gen.m	2 Jul 2012 01:07:01 -0000
@@ -804,7 +804,7 @@
             NondetCopyOut = yes,
             map.from_corresponding_lists(WrapperHeadVarLvals,
                 WrapperBoxedArgTypes, WrapperBoxedVarTypes),
-            WrapperOutputLvals = select_output_vars(ModuleInfo,
+            WrapperOutputLvals = select_output_things(ModuleInfo,
                 WrapperHeadVarLvals, WrapperArgModes,
                 WrapperBoxedVarTypes),
             WrapperOutputTypes = map.apply_to_list(WrapperOutputLvals,
Index: compiler/ml_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_code_gen.m,v
retrieving revision 1.233
diff -u -b -r1.233 ml_code_gen.m
--- compiler/ml_code_gen.m	13 Feb 2012 00:11:42 -0000	1.233
+++ compiler/ml_code_gen.m	2 Jul 2012 01:07:01 -0000
@@ -934,7 +934,7 @@
 ml_gen_local_var_decls(_VarSet, _VarTypes, _Context, [], [], !Info).
 ml_gen_local_var_decls(VarSet, VarTypes, Context, [Var | Vars], Defns,
         !Info) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     ml_gen_info_get_module_info(!.Info, ModuleInfo),
     IsDummy = check_dummy_type(ModuleInfo, Type),
     (
Index: compiler/ml_code_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_code_util.m,v
retrieving revision 1.161
diff -u -b -r1.161 ml_code_util.m
--- compiler/ml_code_util.m	8 Jun 2012 15:46:20 -0000	1.161
+++ compiler/ml_code_util.m	2 Jul 2012 01:07:01 -0000
@@ -1287,7 +1287,7 @@
 
 ml_variable_type(Info, Var, Type) :-
     ml_gen_info_get_var_types(Info, VarTypes),
-    map.lookup(VarTypes, Var, Type).
+    lookup_var_type(VarTypes, Var, Type).
 
 ml_gen_var_names(VarSet, Vars) = list.map(ml_gen_var_name(VarSet), Vars).
 
Index: compiler/ml_disj_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_disj_gen.m,v
retrieving revision 1.8
diff -u -b -r1.8 ml_disj_gen.m
--- compiler/ml_disj_gen.m	16 Aug 2011 03:26:31 -0000	1.8
+++ compiler/ml_disj_gen.m	2 Jul 2012 01:07:01 -0000
@@ -313,7 +313,7 @@
     ml_gen_info_get_target(!.Info, Target),
 
     ml_gen_info_get_var_types(!.Info, VarTypes),
-    list.map(map.lookup(VarTypes), OutVars, FieldTypes),
+    lookup_var_types(VarTypes, OutVars, FieldTypes),
     MLDS_FieldTypes =
         list.map(mercury_type_to_mlds_type(ModuleInfo), FieldTypes),
 
Index: compiler/ml_lookup_switch.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_lookup_switch.m,v
retrieving revision 1.14
diff -u -b -r1.14 ml_lookup_switch.m
--- compiler/ml_lookup_switch.m	6 Sep 2011 00:22:58 -0000	1.14
+++ compiler/ml_lookup_switch.m	2 Jul 2012 01:07:01 -0000
@@ -133,7 +133,7 @@
     ),
     ml_gen_info_get_module_info(!.Info, ModuleInfo),
     ml_gen_info_get_var_types(!.Info, VarTypes),
-    list.map(map.lookup(VarTypes), OutVars, OutTypes),
+    lookup_var_types(VarTypes, OutVars, OutTypes),
     FieldTypes =
         list.map(mercury_type_to_mlds_type(ModuleInfo), OutTypes),
     LookupSwitchInfo = ml_lookup_switch_info(CaseConsts, OutVars, FieldTypes).
Index: compiler/ml_proc_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_proc_gen.m,v
retrieving revision 1.19
diff -u -b -r1.19 ml_proc_gen.m
--- compiler/ml_proc_gen.m	8 Jun 2012 15:46:20 -0000	1.19
+++ compiler/ml_proc_gen.m	2 Jul 2012 01:07:01 -0000
@@ -465,10 +465,9 @@
                 proc_info_get_vartypes(ProcInfo, VarTypes),
                 % Note that for headvars we must use the types from
                 % the procedure interface, not from the procedure body.
-                HeadVarTypes = map.from_corresponding_lists(HeadVars,
-                    ArgTypes),
-                ml_gen_local_var_decls(VarSet,
-                    map.overlay(VarTypes, HeadVarTypes),
+                vartypes_overlay_corresponding_lists(HeadVars, ArgTypes,
+                    VarTypes, UpdatedVarTypes),
+                ml_gen_local_var_decls(VarSet, UpdatedVarTypes,
                     Context, CopiedOutputVars, OutputVarLocals, !Info)
             ),
             MLDS_Context = mlds_make_context(Context),
Index: compiler/ml_unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_unify_gen.m,v
retrieving revision 1.167
diff -u -b -r1.167 ml_unify_gen.m
--- compiler/ml_unify_gen.m	11 Jun 2012 03:13:21 -0000	1.167
+++ compiler/ml_unify_gen.m	2 Jul 2012 01:07:01 -0000
@@ -2532,7 +2532,7 @@
         Unify = construct(Var, ConsId, Args, _, _HowToConstruct, _, SubInfo),
         SubInfo = no_construct_sub_info
     ->
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         MLDS_Type = mercury_type_to_mlds_type(ModuleInfo, VarType),
         ConsTag = cons_id_to_tag(ModuleInfo, ConsId),
         Context = goal_info_get_context(GoalInfo),
@@ -2681,7 +2681,7 @@
 
     % This part does a simplied version of the job of
     % get_maybe_cons_id_arg_types.
-    list.map(map.lookup(VarTypes), Args, ArgTypes),
+    lookup_var_types(VarTypes, Args, ArgTypes),
     (
         ConsId = cons(_, _, _),
         \+ is_introduced_type_info_type(VarType)
Index: compiler/mode_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_info.m,v
retrieving revision 1.106
diff -u -b -r1.106 mode_info.m
--- compiler/mode_info.m	31 Aug 2011 07:59:33 -0000	1.106
+++ compiler/mode_info.m	2 Jul 2012 01:07:01 -0000
@@ -765,7 +765,7 @@
 
 mode_info_get_types_of_vars(ModeInfo, Vars, TypesOfVars) :-
     mode_info_get_var_types(ModeInfo, VarTypes),
-    map.apply_to_list(Vars, VarTypes, TypesOfVars).
+    lookup_var_types(VarTypes, Vars, TypesOfVars).
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
Index: compiler/mode_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_util.m,v
retrieving revision 1.225
diff -u -b -r1.225 mode_util.m
--- compiler/mode_util.m	11 Jun 2012 03:13:21 -0000	1.225
+++ compiler/mode_util.m	2 Jul 2012 01:07:01 -0000
@@ -90,8 +90,10 @@
     % Given a list of variables and their corresponding modes,
     % return a list containing only those variables which have an output mode.
     %
-:- func select_output_vars(module_info, list(Var), list(mer_mode),
-    map(Var, mer_type)) = list(Var).
+:- func select_output_vars(module_info, list(prog_var), list(mer_mode),
+    vartypes) = list(prog_var).
+:- func select_output_things(module_info, list(Thing), list(mer_mode),
+    map(Thing, mer_type)) = list(Thing).
 
 :- func mode_get_initial_inst(module_info, mer_mode) = mer_inst.
 
@@ -338,7 +340,7 @@
     ;
         HeadVars = [Var | Vars],
         HeadModes = [Mode | Modes],
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         mode_to_arg_mode(ModuleInfo, Mode, VarType, Top),
         (
             Top = top_out,
@@ -361,6 +363,39 @@
         unexpected($module, $pred, "length mismatch")
     ).
 
+select_output_things(ModuleInfo, HeadThings, HeadModes, ThingTypes) =
+        OutputThings :-
+    (
+        HeadThings = [],
+        HeadModes = [],
+        OutputThings = []
+    ;
+        HeadThings = [Thing | Things],
+        HeadModes = [Mode | Modes],
+        map.lookup(ThingTypes, Thing, ThingType),
+        mode_to_arg_mode(ModuleInfo, Mode, ThingType, Top),
+        (
+            Top = top_out,
+            OutputThings1 = select_output_things(ModuleInfo, Things, Modes,
+                ThingTypes),
+            OutputThings = [Thing | OutputThings1]
+        ;
+            ( Top = top_in
+            ; Top = top_unused
+            ),
+            OutputThings = select_output_things(ModuleInfo, Things, Modes,
+                ThingTypes)
+        )
+    ;
+        HeadThings = [],
+        HeadModes = [_ | _],
+        unexpected($module, $pred, "length mismatch")
+    ;
+        HeadThings = [_ | _],
+        HeadModes = [],
+        unexpected($module, $pred, "length mismatch")
+    ).
+
 %-----------------------------------------------------------------------------%
 
     % get_single_arg_inst(ModuleInfo, Inst, ConsId, ArgInsts):
@@ -1406,7 +1441,7 @@
         [Case0 | Cases0], [Case | Cases], VarTypes, InstMap0, NonLocals,
         [InstMapDelta | InstMapDeltas], !RI) :-
     Case0 = case(MainConsId, OtherConsIds, Goal0),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     update_module_info(bind_var_to_functors(Var, Type,
         MainConsId, OtherConsIds, InstMap0), InstMap1, !RI),
     recompute_instmap_delta_1(RecomputeAtomic, Goal0, Goal, VarTypes, InstMap1,
@@ -1477,7 +1512,7 @@
     SaveModuleInfo = !.ModuleInfo,
     SaveSub = !.Sub,
     instmap_lookup_var(InstMap, Arg, ArgInst),
-    map.lookup(VarTypes, Arg, Type),
+    lookup_var_type(VarTypes, Arg, Type),
     ( inst_matches_initial_sub(ArgInst, Inst, Type, !ModuleInfo, !Sub) ->
         true
     ;
Index: compiler/modecheck_call.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_call.m,v
retrieving revision 1.92
diff -u -b -r1.92 modecheck_call.m
--- compiler/modecheck_call.m	8 Jun 2012 15:36:59 -0000	1.92
+++ compiler/modecheck_call.m	2 Jul 2012 01:07:01 -0000
@@ -237,7 +237,7 @@
             % a function type, then assume the default function mode.
             HOInstInfo = none,
             mode_info_get_var_types(!.ModeInfo, VarTypes),
-            map.lookup(VarTypes, PredVar, Type),
+            lookup_var_type(VarTypes, PredVar, Type),
             type_is_higher_order_details(Type, _, pf_function, _, ArgTypes),
             PredInstInfo = pred_inst_info_standard_func_mode(
                 list.length(ArgTypes))
@@ -335,7 +335,7 @@
     % If we are inferring modes for this called predicate, then
     % just insert a new mode declaration which will match.
     % Otherwise, report an error.
-    %
+
     mode_info_get_preds(!.ModeInfo, Preds),
     map.lookup(Preds, PredId, PredInfo),
     pred_info_get_markers(PredInfo, Markers),
@@ -486,7 +486,6 @@
     % Call unify_proc.request_proc, which will create the new procedure,
     % set its "can-process" flag to `no', and insert it into the queue
     % of requested procedures.
-    %
     unify_proc.request_proc(PredId, Modes, InstVarSet, yes(ArgLives),
         MaybeDet, Context, ProcId, ModuleInfo0, ModuleInfo),
 
@@ -507,7 +506,7 @@
     mode_info_get_instmap(ModeInfo, InstMap),
     mode_info_get_var_types(ModeInfo, VarTypes),
     instmap_lookup_var(InstMap, Var, Inst0),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     normalise_inst(ModuleInfo, Type, Inst0, Inst),
 
     mode_info_var_is_live(ModeInfo, Var, IsLive0),
@@ -535,24 +534,21 @@
 
 %-----------------------------------------------------------------------------%
 
-    % Given two modes of a predicate, figure out whether
-    % they are indistinguishable; that is, whether any valid call to
-    % one mode would also be a valid call to the other.
-    % (If so, it is a mode error.)
+    % Given two modes of a predicate, figure out whether they are
+    % indistinguishable; that is, whether any valid call to one mode
+    % would also be a valid call to the other. (If so, it is a mode error.)
     % Note that mode declarations which only have different final insts
     % do not count as distinguishable.
     %
-    % The code for this is similar to the code for
-    % modes_are_identical/4 and compare_proc/5 below.
+    % The code for this is similar to the code for modes_are_identical/4
+    % and compare_proc/5 below.
     %
 modes_are_indistinguishable(ProcId, OtherProcId, PredInfo, ModuleInfo) :-
     pred_info_get_procedures(PredInfo, Procs),
     map.lookup(Procs, ProcId, ProcInfo),
     map.lookup(Procs, OtherProcId, OtherProcInfo),
 
-    %
-    % Compare the initial insts of the arguments
-    %
+    % Compare the initial insts of the arguments.
     proc_info_get_argmodes(ProcInfo, ProcArgModes),
     proc_info_get_argmodes(OtherProcInfo, OtherProcArgModes),
     mode_list_get_initial_insts(ModuleInfo, ProcArgModes, InitialInsts),
@@ -563,19 +559,14 @@
         ArgTypes, CompareInsts),
     CompareInsts = same,
 
-    %
-    % Compare the expected livenesses of the arguments
-    %
+    % Compare the expected livenesses of the arguments.
     get_arg_lives(ModuleInfo, ProcArgModes, ProcArgLives),
     get_arg_lives(ModuleInfo, OtherProcArgModes, OtherProcArgLives),
     compare_liveness_list(ProcArgLives, OtherProcArgLives, CompareLives),
     CompareLives = same,
 
-    %
-    % Compare the determinisms --
-    %   If both are cc_, or if both are not cc_,
+    % Compare the determinisms; if both are cc_, or if both are not cc_,
     %   then they are indistinguishable.
-    %
     proc_info_interface_determinism(ProcInfo, Detism),
     proc_info_interface_determinism(OtherProcInfo, OtherDetism),
     determinism_components(Detism, _CanFail, Solns),
@@ -586,9 +577,8 @@
 
 %-----------------------------------------------------------------------------%
 
-    % Given two modes of a predicate, figure out whether
-    % they are identical, except that one is cc_nondet/cc_multi
-    % and the other is nondet/multi.
+    % Given two modes of a predicate, figure out whether they are identical,
+    % except that one is cc_nondet/cc_multi and the other is nondet/multi.
     %
     % The code for this is similar to the code for compare_proc/5 below
     % and modes_are_indistinguishable/4 above.
@@ -598,9 +588,7 @@
     map.lookup(Procs, ProcId, ProcInfo),
     map.lookup(Procs, OtherProcId, OtherProcInfo),
 
-    %
     % Compare the initial insts of the arguments
-    %
     proc_info_get_argmodes(ProcInfo, ProcArgModes),
     proc_info_get_argmodes(OtherProcInfo, OtherProcArgModes),
     mode_list_get_initial_insts(ModuleInfo, ProcArgModes, InitialInsts),
@@ -611,9 +599,7 @@
         ArgTypes, CompareInitialInsts),
     CompareInitialInsts = same,
 
-    %
     % Compare the final insts of the arguments
-    %
     mode_list_get_final_insts(ModuleInfo, ProcArgModes, FinalInsts),
     mode_list_get_final_insts(ModuleInfo, OtherProcArgModes,
         OtherFinalInsts),
@@ -621,17 +607,13 @@
         ArgTypes, CompareFinalInsts),
     CompareFinalInsts = same,
 
-    %
     % Compare the expected livenesses of the arguments
-    %
     get_arg_lives(ModuleInfo, ProcArgModes, ProcArgLives),
     get_arg_lives(ModuleInfo, OtherProcArgModes, OtherProcArgLives),
     compare_liveness_list(ProcArgLives, OtherProcArgLives, CompareLives),
     CompareLives = same,
 
-    %
     % Compare the determinisms, ignoring the cc part.
-    %
     proc_info_interface_determinism(ProcInfo, Detism),
     proc_info_interface_determinism(OtherProcInfo, OtherDetism),
     determinism_components(Detism, CanFail, Solns),
@@ -689,9 +671,7 @@
 choose_best_match(ModeInfo,
         [proc_mode(ProcId, InstVarSub, ArgModes) | ProcIds], PredId,
         Procs, ArgVars, TheProcId, TheInstVarSub, TheArgModes) :-
-    %
     % This ProcId is best iff there is no other proc_id which is better.
-    %
     (
         \+ (
             list.member(proc_mode(OtherProcId, _, _), ProcIds),
@@ -719,29 +699,26 @@
 compare_proc(ModeInfo, ProcId, OtherProcId, ArgVars, Procs, Compare) :-
     map.lookup(Procs, ProcId, ProcInfo),
     map.lookup(Procs, OtherProcId, OtherProcInfo),
-    %
-    % Compare the initial insts of the arguments
-    %
+
+    % Compare the initial insts of the arguments.
     proc_info_get_argmodes(ProcInfo, ProcArgModes),
     proc_info_get_argmodes(OtherProcInfo, OtherProcArgModes),
     mode_info_get_module_info(ModeInfo, ModuleInfo),
     mode_info_get_var_types(ModeInfo, VarTypes),
-    list.map(map.lookup(VarTypes), ArgVars, ArgTypes),
+    lookup_var_types(VarTypes, ArgVars, ArgTypes),
     mode_list_get_initial_insts(ModuleInfo, ProcArgModes, InitialInsts),
     mode_list_get_initial_insts(ModuleInfo, OtherProcArgModes,
         OtherInitialInsts),
     get_var_insts_and_lives(ArgVars, ModeInfo, ArgInitialInsts, _ArgLives),
     compare_inst_list(ModuleInfo, InitialInsts, OtherInitialInsts,
         yes(ArgInitialInsts), ArgTypes, CompareInsts),
-    %
-    % Compare the expected livenesses of the arguments
-    %
+
+    % Compare the expected livenesses of the arguments.
     get_arg_lives(ModuleInfo, ProcArgModes, ProcArgLives),
     get_arg_lives(ModuleInfo, OtherProcArgModes, OtherProcArgLives),
     compare_liveness_list(ProcArgLives, OtherProcArgLives, CompareLives),
-    %
-    % Compare the determinisms
-    %
+
+    % Compare the determinisms.
     proc_info_interface_determinism(ProcInfo, Detism),
     proc_info_interface_determinism(OtherProcInfo, OtherDetism),
     compare_determinisms(Detism, OtherDetism, CompareDet0),
@@ -749,10 +726,9 @@
     ; CompareDet0 = looser, CompareDet = worse
     ; CompareDet0 = sameas, CompareDet = same
     ),
-    %
+
     % Combine the results, with the insts & lives comparisons
     % taking priority over the determinism comparison.
-    %
     combine_results(CompareInsts, CompareLives, Compare0),
     prioritized_combine_results(Compare0, CompareDet, Compare).
 
Index: compiler/modecheck_conj.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_conj.m,v
retrieving revision 1.10
diff -u -b -r1.10 modecheck_conj.m
--- compiler/modecheck_conj.m	11 Apr 2012 04:52:35 -0000	1.10
+++ compiler/modecheck_conj.m	2 Jul 2012 01:07:01 -0000
@@ -354,7 +354,7 @@
                 set_of_var.member(CandidateInitVars, Var)
             =>
                 (
-                    map.lookup(VarTypes, Var, VarType),
+                    lookup_var_type(VarTypes, Var, VarType),
                     type_is_solver_type_with_auto_init(ModuleInfo, VarType)
                 )
             ),
@@ -558,7 +558,7 @@
     prog_var::in) is semidet.
 
 does_not_contain_solver_type(ModuleInfo, VarTypes, Var) :-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     not type_is_or_may_contain_solver_type(ModuleInfo, VarType).
 
     % Update !NonFree and !CandidateVars given the args and modes for a call.
Index: compiler/modecheck_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_goal.m,v
retrieving revision 1.18
diff -u -b -r1.18 modecheck_goal.m
--- compiler/modecheck_goal.m	23 Apr 2012 03:34:48 -0000	1.18
+++ compiler/modecheck_goal.m	2 Jul 2012 01:07:01 -0000
@@ -947,8 +947,8 @@
     mode_info_get_var_types(!.ModeInfo, VarTypes0),
     mode_info_get_varset(!.ModeInfo, VarSet0),
     varset.new_var(CloneVar, VarSet0, VarSet),
-    map.lookup(VarTypes0, TermVar, TermVarType),
-    map.det_insert(CloneVar, TermVarType, VarTypes0, VarTypes),
+    lookup_var_type(VarTypes0, TermVar, TermVarType),
+    add_var_type(CloneVar, TermVarType, VarTypes0, VarTypes),
     mode_info_set_varset(VarSet, !ModeInfo),
     mode_info_set_var_types(VarTypes, !ModeInfo),
     Rename = map.singleton(TermVar, CloneVar),
@@ -1364,7 +1364,7 @@
             mode_info_get_module_info(!.ModeInfo, ModuleInfo),
             module_info_get_type_table(ModuleInfo, TypeTable),
             mode_info_get_var_types(!.ModeInfo, VarTypes),
-            map.lookup(VarTypes, Arg1, ArgType1),
+            lookup_var_type(VarTypes, Arg1, ArgType1),
             type_to_ctor(ArgType1, ArgTypeCtor1),
             lookup_type_ctor_defn(TypeTable, ArgTypeCtor1, CtorDefn),
             get_type_defn_body(CtorDefn, Body),
@@ -1497,8 +1497,8 @@
         % check here (also: types of variables must be known at this point).
 
         Outer = atomic_interface_vars(OuterDI, OuterUO),
-        map.lookup(VarTypes, OuterDI, OuterDIType),
-        map.lookup(VarTypes, OuterUO, OuterUOType),
+        lookup_var_type(VarTypes, OuterDI, OuterDIType),
+        lookup_var_type(VarTypes, OuterUO, OuterUOType),
         (
             ( OuterDIType = io_state_type
             ; OuterDIType = io_io_type
@@ -1517,8 +1517,8 @@
         expect(unify(OuterDIType, OuterUOType), $module, $pred,
             "atomic_goal: mismatched outer var type"),
         Inner = atomic_interface_vars(InnerDI, InnerUO),
-        map.lookup(VarTypes, InnerDI, InnerDIType),
-        map.lookup(VarTypes, InnerUO, InnerUOType),
+        lookup_var_type(VarTypes, InnerDI, InnerDIType),
+        lookup_var_type(VarTypes, InnerUO, InnerUOType),
         expect(unify(InnerDIType, stm_atomic_type), $module, $pred,
             "atomic_goal: invalid inner var type"),
         expect(unify(InnerUOType, stm_atomic_type), $module, $pred,
@@ -1597,7 +1597,7 @@
     list(instmap)::in, prog_var::in) is semidet.
 
 solver_var_must_be_initialised(VarTypes, ModuleInfo, InstMaps, Var) :-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     type_is_solver_type_with_auto_init(ModuleInfo, VarType),
     list.member(InstMap, InstMaps),
     instmap_lookup_var(InstMap, Var, Inst),
Index: compiler/modecheck_unify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_unify.m,v
retrieving revision 1.147
diff -u -b -r1.147 modecheck_unify.m
--- compiler/modecheck_unify.m	8 Jun 2012 15:36:59 -0000	1.147
+++ compiler/modecheck_unify.m	2 Jul 2012 01:07:01 -0000
@@ -167,7 +167,7 @@
         mode_info_may_init_solver_vars(!.ModeInfo),
         InstOfX0   = free,
         InstOfY0   = free,
-        map.search(VarTypes, X, VarType),
+        search_var_type(VarTypes, X, VarType),
         type_is_solver_type_with_auto_init(ModuleInfo0, VarType)
     ->
         construct_initialisation_call(X, VarType, any_inst,
@@ -254,7 +254,7 @@
 modecheck_unification_functor(X, ConsId, IsExistConstruction, ArgVars0,
         Unification0, UnifyContext, GoalInfo0, GoalExpr, !ModeInfo) :-
     mode_info_get_var_types(!.ModeInfo, VarTypes0),
-    map.lookup(VarTypes0, X, TypeOfX),
+    lookup_var_type(VarTypes0, X, TypeOfX),
 
     (
         % We replace any unifications with higher-order pred constants
@@ -401,7 +401,7 @@
         Groundness = ho_any,
         mode_info_get_var_types(!.ModeInfo, NonLocalTypes),
         NonLocals = set_of_var.filter((pred(NonLocal::in) is semidet :-
-                map.lookup(NonLocalTypes, NonLocal, NonLocalType),
+                lookup_var_type(NonLocalTypes, NonLocal, NonLocalType),
                 instmap_lookup_var(InstMap1, NonLocal, NonLocalInst),
                 \+ inst_matches_initial(NonLocalInst, any(shared, none),
                     NonLocalType, ModuleInfo0)
@@ -758,7 +758,7 @@
             list.member(InstArg, InstArgs),
             inst_is_free(ModuleInfo0, InstArg),
             list.member(ArgVar, ArgVars0),
-            map.lookup(VarTypes, ArgVar, ArgType),
+            lookup_var_type(VarTypes, ArgVar, ArgType),
             type_is_or_may_contain_solver_type(ModuleInfo0, ArgType)
         ),
         abstractly_unify_inst_functor(LiveX, InstOfX, InstConsId,
@@ -950,7 +950,7 @@
 all_arg_vars_are_non_free_or_solver_vars([ArgVar | ArgVars], [Inst | Insts],
         VarTypes, ModuleInfo, ArgVarsToInit) :-
     ( inst_match.inst_is_free(ModuleInfo, Inst) ->
-        map.lookup(VarTypes, ArgVar, ArgType),
+        lookup_var_type(VarTypes, ArgVar, ArgType),
         type_is_or_may_contain_solver_type(ModuleInfo, ArgType),
         all_arg_vars_are_non_free_or_solver_vars(ArgVars, Insts,
             VarTypes, ModuleInfo, ArgVarsToInitTail),
@@ -1005,7 +1005,7 @@
     ModeX = (InitialInstX -> FinalInstX),
     ModeY = (InitialInstY -> FinalInstY),
     mode_info_get_var_types(!.ModeInfo, VarTypes0),
-    map.lookup(VarTypes0, Var0, VarType),
+    lookup_var_type(VarTypes0, Var0, VarType),
     (
         mode_to_arg_mode(ModuleInfo, ModeX, VarType, top_in),
         mode_to_arg_mode(ModuleInfo, ModeY, VarType, top_in)
@@ -1031,8 +1031,8 @@
     mode_info_get_varset(!.ModeInfo, VarSet0),
     mode_info_get_var_types(!.ModeInfo, VarTypes0),
     varset.new_var(Var, VarSet0, VarSet),
-    map.lookup(VarTypes0, Var0, VarType),
-    map.set(Var, VarType, VarTypes0, VarTypes),
+    lookup_var_type(VarTypes0, Var0, VarType),
+    add_var_type(Var, VarType, VarTypes0, VarTypes),
     mode_info_set_varset(VarSet, !ModeInfo),
     mode_info_set_var_types(VarTypes, !ModeInfo),
 
@@ -1137,7 +1137,7 @@
 
         Unification = simple_test(X, Y)
     ;
-        map.lookup(VarTypes, X, Type),
+        lookup_var_type(VarTypes, X, Type),
         (
             type_is_atomic(ModuleInfo0, Type),
             not type_has_user_defined_equality_pred(ModuleInfo0, Type, _)
@@ -1400,7 +1400,7 @@
                 PredModule = pred_info_module(PredInfo),
                 PredName = pred_info_name(PredInfo),
                 mode_info_get_var_types(!.ModeInfo, VarTypes),
-                map.lookup(VarTypes, X, Type),
+                lookup_var_type(VarTypes, X, Type),
                 ( Type = higher_order_type(_, MaybeReturnType, _, _) ->
                     (
                         MaybeReturnType = no,
@@ -1429,7 +1429,7 @@
         % since not_reached is considered bound.
         set_of_var.init(WaitingVars),
         mode_info_get_var_types(!.ModeInfo, VarTypes0),
-        map.lookup(VarTypes0, X, Type),
+        lookup_var_type(VarTypes0, X, Type),
         ModeError = mode_error_unify_pred(X,
             error_at_lambda(ArgVars, ArgModes0), Type, PredOrFunc),
         mode_info_error(WaitingVars, ModeError, !ModeInfo),
@@ -1456,7 +1456,7 @@
         X, NewConsId, ArgVars, VarTypes, UnifyContext,
         Unification0, Unification, !ModeInfo) :-
     mode_info_get_module_info(!.ModeInfo, ModuleInfo),
-    map.lookup(VarTypes, X, TypeOfX),
+    lookup_var_type(VarTypes, X, TypeOfX),
     % If we are re-doing mode analysis, preserve the existing cons_id.
     (
         Unification0 = construct(_, ConsIdPrime, _, _, _, _, SubInfo0),
@@ -1546,7 +1546,7 @@
 check_type_info_args_are_ground([ArgVar | ArgVars], VarTypes, UnifyContext,
         !ModeInfo) :-
     (
-        map.lookup(VarTypes, ArgVar, ArgType),
+        lookup_var_type(VarTypes, ArgVar, ArgType),
         is_introduced_type_info_type(ArgType)
     ->
         mode_info_set_call_arg_context(1, !ModeInfo),
@@ -1626,7 +1626,7 @@
     Initial = mode_get_initial_inst(ModuleInfo, ArgMode),
     ( Initial = ground(_, higher_order(_)) ->
         instmap_lookup_var(InstMap, Arg, ArgInst),
-        map.lookup(VarTypes, Arg, ArgType),
+        lookup_var_type(VarTypes, Arg, ArgType),
         ( inst_matches_initial(ArgInst, Initial, ArgType, ModuleInfo) ->
             match_mode_by_higher_order_insts(ModuleInfo, InstMap, VarTypes,
                 Args, ArgModes, Result)
Index: compiler/modecheck_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_util.m,v
retrieving revision 1.12
diff -u -b -r1.12 modecheck_util.m
--- compiler/modecheck_util.m	8 Jun 2012 15:36:59 -0000	1.12
+++ compiler/modecheck_util.m	2 Jul 2012 01:07:01 -0000
@@ -333,7 +333,7 @@
     % or typeclass-infos inserted for existential data types.
     mode_info_get_module_info(!.ModeInfo, ModuleInfo),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     BoundInst = cons_id_to_bound_inst(ModuleInfo, Type, ConsId),
 
     % Record the fact that Var was bound to ConsId.
@@ -345,7 +345,7 @@
     % or typeclass-infos inserted for existential data types.
     mode_info_get_module_info(!.ModeInfo, ModuleInfo),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     BoundInsts = list.map(cons_id_to_bound_inst(ModuleInfo, Type),
         [MainConsId | OtherConsIds]),
 
@@ -514,7 +514,7 @@
     mode_info_get_instmap(!.ModeInfo, InstMap),
     instmap_lookup_var(InstMap, Var, VarInst),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     mode_info_get_module_info(!.ModeInfo, ModuleInfo0),
     (
         inst_matches_initial_no_implied_modes_sub(VarInst, Inst, Type,
@@ -535,7 +535,7 @@
     mode_info_get_instmap(!.ModeInfo, InstMap),
     instmap_lookup_var(InstMap, Var, VarInst),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     mode_info_get_module_info(!.ModeInfo, ModuleInfo0),
     (
         inst_matches_initial_sub(VarInst, Inst, Type, ModuleInfo0, ModuleInfo,
@@ -649,7 +649,7 @@
         ),
         mode_info_set_module_info(ModuleInfo, !ModeInfo),
         mode_info_get_var_types(!.ModeInfo, VarTypes),
-        map.lookup(VarTypes, Var0, Type),
+        lookup_var_type(VarTypes, Var0, Type),
         (
             % If the top-level inst of the variable is not_reached,
             % then the instmap as a whole must be unreachable.
@@ -729,7 +729,7 @@
     inst_expand(ModuleInfo0, VarInst0, VarInst1),
 
     mode_info_get_var_types(!.ModeInfo, VarTypes0),
-    map.lookup(VarTypes0, Var0, VarType),
+    lookup_var_type(VarTypes0, Var0, VarType),
     (
         % If the initial inst of the variable matches_final the initial inst
         % specified in the pred's mode declaration, then it's not a call
@@ -799,7 +799,7 @@
         % Introduce a new variable.
         mode_info_get_varset(!.ModeInfo, VarSet0),
         varset.new_var(Var, VarSet0, VarSet),
-        map.set(Var, VarType, VarTypes0, VarTypes),
+        add_var_type(Var, VarType, VarTypes0, VarTypes),
         mode_info_set_varset(VarSet, !ModeInfo),
         mode_info_set_var_types(VarTypes, !ModeInfo),
 
@@ -863,7 +863,7 @@
 construct_initialisation_calls([], [], !ModeInfo).
 construct_initialisation_calls([Var | Vars], [Goal | Goals], !ModeInfo) :-
     mode_info_get_var_types(!.ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     InitialInst           = free,
     Context               = term.context_init,
     MaybeCallUnifyContext = no,
Index: compiler/modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modes.m,v
retrieving revision 1.401
diff -u -b -r1.401 modes.m
--- compiler/modes.m	11 Apr 2012 04:52:35 -0000	1.401
+++ compiler/modes.m	2 Jul 2012 01:07:01 -0000
@@ -1170,7 +1170,7 @@
     ),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
     instmap_lookup_vars(InstMap, HeadVars, VarFinalInsts1),
-    map.apply_to_list(HeadVars, VarTypes, ArgTypes),
+    lookup_var_types(VarTypes, HeadVars, ArgTypes),
     (
         InferModes = yes,
         normalise_insts(ModuleInfo, ArgTypes, VarFinalInsts1, VarFinalInsts2),
@@ -1232,7 +1232,7 @@
         VarInsts = [VarInst | VarInstsTail]
     ->
         mode_info_get_var_types(!.ModeInfo, VarTypes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         ( inst_matches_final_typed(VarInst, Inst, Type, ModuleInfo) ->
             true
         ;
Index: compiler/par_loop_control.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/par_loop_control.m,v
retrieving revision 1.8
diff -u -b -r1.8 par_loop_control.m
--- compiler/par_loop_control.m	13 Feb 2012 00:11:44 -0000	1.8
+++ compiler/par_loop_control.m	2 Jul 2012 01:07:01 -0000
@@ -618,7 +618,7 @@
         proc_info_get_goal(OldProcInfo, !:Body),
 
         varset.new_named_var("LC", LCVar, !VarSet),
-        map.det_insert(LCVar, loop_control_var_type, !VarTypes),
+        add_var_type(LCVar, loop_control_var_type, !VarTypes),
         should_preserve_tail_recursion(!.ModuleInfo, PreserveTailRecursion),
         get_lc_wait_free_slot_proc(!.ModuleInfo, WaitFreeSlotProc),
         get_lc_join_and_terminate_proc(!.ModuleInfo, JoinAndTerminateProc),
@@ -1234,7 +1234,7 @@
 create_get_free_slot_goal(Info, LCSVar, Goal, !VarSet,
         !VarTypes) :-
     varset.new_named_var("LCS", LCSVar, !VarSet),
-    map.det_insert(LCSVar, loop_control_slot_var_type, !VarTypes),
+    add_var_type(LCSVar, loop_control_slot_var_type, !VarTypes),
     LCVar = Info ^ lci_lc_var,
     proc(PredId, ProcId) = Info ^ lci_wait_free_slot_proc,
     SymName = Info ^ lci_wait_free_slot_proc_name,
@@ -1255,7 +1255,7 @@
 create_create_loop_control_goal(ModuleInfo, NumContextsVar, LCVar, Goal,
         !VarSet, !VarTypes) :-
     varset.new_named_var("LC", LCVar, !VarSet),
-    map.det_insert(LCVar, loop_control_var_type, !VarTypes),
+    add_var_type(LCVar, loop_control_var_type, !VarTypes),
     get_lc_create_proc(ModuleInfo, LCCreatePredId, LCCreateProcId),
     GoalExpr = plain_call(LCCreatePredId, LCCreateProcId,
         [NumContextsVar, LCVar], not_builtin, no, lc_create_name),
@@ -1337,7 +1337,7 @@
     some [!VarSet, !VarTypes] (
         % Re-build the variables in the procedure with smaller sets.
         varset.init(!:VarSet),
-        map.init(!:VarTypes),
+        init_vartypes(!:VarTypes),
         proc_info_get_varset(!.ProcInfo, OldVarSet),
         foldl3_corresponding(add_old_var_to_sets(OldVarSet), HeadVars0,
             HeadVarTypes, !VarSet, !VarTypes, map.init, Remap),
@@ -1355,7 +1355,7 @@
         % (for auto-parallelisation), but for now we just set it using
         % a runtime call so that it can be tuned.
         varset.new_named_var("NumContexts", NumContextsVar, !VarSet),
-        map.det_insert(NumContextsVar, builtin_type(builtin_type_int),
+        add_var_type(NumContextsVar, builtin_type(builtin_type_int),
             !VarTypes),
         get_lc_default_num_contexts_proc(ModuleInfo,
             LCDefaultNumContextsPredId, LCDefaultNumContextsProcId),
@@ -1423,7 +1423,7 @@
     ;
         varset.new_var(Var, !VarSet)
     ),
-    map.det_insert(Var, VarType, !VarTypes),
+    add_var_type(Var, VarType, !VarTypes),
     map.det_insert(OldVar, Var, !Remap).
 
 :- pred remap_instmap(map(prog_var, prog_var)::in,
Index: compiler/pd_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_info.m,v
retrieving revision 1.47
diff -u -b -r1.47 pd_info.m
--- compiler/pd_info.m	17 Jan 2012 15:49:44 -0000	1.47
+++ compiler/pd_info.m	2 Jul 2012 01:07:01 -0000
@@ -209,7 +209,7 @@
     pd_info_get_module_info(!.PDInfo, ModuleInfo0),
     pd_info_get_proc_info(!.PDInfo, ProcInfo),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     bind_var_to_functors(Var, Type, MainConsId, OtherConsIds,
         InstMap0, InstMap, ModuleInfo0, ModuleInfo),
     pd_info_set_instmap(InstMap, !PDInfo),
@@ -613,7 +613,7 @@
     instmap_lookup_var(OldInstMap, OldVar, OldVarInst),
     map.lookup(VarRenaming, OldVar, NewVar),
     instmap_lookup_var(NewInstMap, NewVar, NewVarInst),
-    map.lookup(VarTypes, NewVar, Type),
+    lookup_var_type(VarTypes, NewVar, Type),
     inst_matches_initial(NewVarInst, OldVarInst, Type, ModuleInfo),
     (
         !.ExactSoFar = exact,
Index: compiler/pd_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_util.m,v
retrieving revision 1.89
diff -u -b -r1.89 pd_util.m
--- compiler/pd_util.m	14 Jun 2012 07:03:56 -0000	1.89
+++ compiler/pd_util.m	2 Jul 2012 01:07:01 -0000
@@ -725,7 +725,7 @@
 examine_case_list(_, _, _, [], _, _, !Vars, !ModuleInfo).
 examine_case_list(ProcArgInfo, BranchNo, Var, [Case | Cases],
         VarTypes, InstMap0, !Vars, !ModuleInfo) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     Case = case(MainConsId, OtherConsIds, Goal),
     bind_var_to_functors(Var, Type, MainConsId, OtherConsIds,
         InstMap0, InstMap1, !ModuleInfo),
@@ -1030,7 +1030,7 @@
     % Check that argument types of NewGoal are subsumed by those of OldGoal.
     collect_matching_arg_types(OldArgs, OldArgTypes,
         OldNewRenaming, [], MatchingArgTypes),
-    map.apply_to_list(NewArgs, NewVarTypes, NewArgTypes),
+    lookup_var_types(NewVarTypes, NewArgs, NewArgTypes),
     type_list_subsumes(MatchingArgTypes, NewArgTypes, TypeSubn).
 
 :- pred collect_matching_arg_types(prog_vars::in, list(mer_type)::in,
Index: compiler/polymorphism.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/polymorphism.m,v
retrieving revision 1.382
diff -u -b -r1.382 polymorphism.m
--- compiler/polymorphism.m	26 Jun 2012 14:31:19 -0000	1.382
+++ compiler/polymorphism.m	2 Jul 2012 01:07:01 -0000
@@ -477,7 +477,7 @@
     proc_arg_vector_partition_poly_args(HeadVars, ExtraHeadVarList,
         OldHeadVarList),
 
-    map.apply_to_list(ExtraHeadVarList, VarTypes0, ExtraArgTypes),
+    lookup_var_types(VarTypes0, ExtraHeadVarList, ExtraArgTypes),
     ArgTypes = ExtraArgTypes ++ ArgTypes0,
     pred_info_set_arg_types(TypeVarSet, ExistQVars, ArgTypes,
         PredInfo0, PredInfo1),
@@ -491,7 +491,7 @@
     (
         ExistQVars = [_ | _],
         % This can fail for unification procedures of equivalence types.
-        map.apply_to_list(OldHeadVarList, VarTypes0, OldHeadVarTypes),
+        lookup_var_types(VarTypes0, OldHeadVarList, OldHeadVarTypes),
         type_list_subsumes(ArgTypes0, OldHeadVarTypes, Subn),
         \+ map.is_empty(Subn)
     ->
@@ -998,12 +998,12 @@
     % Figure out the bindings for any unconstrained existentially quantified
     % type variables in the head.
 
-    ( map.is_empty(VarTypes0) ->
+    ( vartypes_is_empty(VarTypes0) ->
         % This can happen for compiler generated procedures.
         map.init(PredToActualTypeSubst)
     ;
         HeadVarList = proc_arg_vector_to_list(HeadVars),
-        map.apply_to_list(HeadVarList, VarTypes0, ActualArgTypes),
+        lookup_var_types(VarTypes0, HeadVarList, ActualArgTypes),
         type_list_subsumes(ArgTypes, ActualArgTypes, ArgTypeSubst)
     ->
         PredToActualTypeSubst = ArgTypeSubst
@@ -1388,7 +1388,7 @@
         % requantifying things.
 
         poly_info_get_var_types(!.Info, VarTypes),
-        map.lookup(VarTypes, XVar, Type),
+        lookup_var_type(VarTypes, XVar, Type),
         unification_typeinfos(Type, Unification0, Unification,
             GoalInfo0, GoalInfo, _Changed, !Info),
         Goal = hlds_goal(unify(XVar, Y, Mode, Unification, UnifyContext),
@@ -1503,7 +1503,7 @@
         UnifyContext, GoalInfo0, Goal, Changed, !Info) :-
     poly_info_get_module_info(!.Info, ModuleInfo0),
     poly_info_get_var_types(!.Info, VarTypes0),
-    map.lookup(VarTypes0, X0, TypeOfX),
+    lookup_var_type(VarTypes0, X0, TypeOfX),
     list.length(ArgVars0, Arity),
 
     % We replace any unifications with higher order pred constants
@@ -1589,7 +1589,7 @@
         % Add extra arguments to the unification for the
         % type_info and/or type_class_info variables.
 
-        map.apply_to_list(ArgVars0, VarTypes0, ActualArgTypes),
+        lookup_var_types(VarTypes0, ArgVars0, ActualArgTypes),
         polymorphism_process_existq_unify_functor(ConsDefn,
             IsConstruction, ActualArgTypes, TypeOfX, GoalInfo0,
             ExtraVars, ExtraGoals, !Info),
@@ -1739,7 +1739,7 @@
 create_fresh_vars([], [], !VarSet, !VarTypes).
 create_fresh_vars([Type | Types], [Var | Vars], !VarSet, !VarTypes) :-
     varset.new_var(Var, !VarSet),
-    map.det_insert(Var, Type, !VarTypes),
+    add_var_type(Var, Type, !VarTypes),
     create_fresh_vars(Types, Vars, !VarSet, !VarTypes).
 
 %-----------------------------------------------------------------------------%
@@ -2117,7 +2117,7 @@
             ParentUnconstrainedUnivTVars, ParentUnconstrainedExistTVars),
 
         % Calculate the "parent to actual" binding.
-        map.apply_to_list(ArgVars0, VarTypes, ActualArgTypes),
+        lookup_var_types(VarTypes, ArgVars0, ActualArgTypes),
         type_list_subsumes_det(ParentArgTypes, ActualArgTypes,
             ParentToActualTypeSubst),
 
@@ -2192,7 +2192,7 @@
     %
     poly_info_get_typevarset(!.Info, TVarSet0),
     poly_info_get_var_types(!.Info, VarTypes0),
-    ActualArgTypes0 = map.apply_to_list(CallArgs0, VarTypes0),
+    lookup_var_types(VarTypes0, CallArgs0, ActualArgTypes0),
     pred_info_get_arg_types(CalleePredInfo, PredTVarSet, _PredExistQVars,
         PredArgTypes),
     proc_info_get_headvars(CalleeProcInfo, CalleeHeadVars),
@@ -3313,7 +3313,7 @@
         TypeCtorConstArg = csa_constant(TypeCtorConsId, type_info_type),
         MCA = yes(TypeCtorConstArg),
         ExtraGoals = ArgTypeInfoGoals ++ TypeCtorGoals,
-        map.det_update(TypeCtorVar, TypeInfoType, VarTypes1, VarTypes),
+        update_var_type(TypeCtorVar, TypeInfoType, VarTypes1, VarTypes),
         poly_info_set_varset_and_types(VarSet1, VarTypes, !Info)
     ;
         % We do need a second cell for a separate typeinfo.
@@ -3635,7 +3635,7 @@
     ),
     Name = Prefix ++ VarNumStr,
     varset.name_var(Var, Name, !VarSet),
-    map.det_insert(Var, type_info_type, !VarTypes).
+    add_var_type(Var, type_info_type, !VarTypes).
 
 %---------------------------------------------------------------------------%
 
@@ -3888,7 +3888,7 @@
     ),
     varset.name_var(Var, Name, VarSet1, VarSet),
     build_typeclass_info_type(Constraint, VarType),
-    map.set(Var, VarType, VarTypes0, VarTypes),
+    add_var_type(Var, VarType, VarTypes0, VarTypes),
     rtti_det_insert_typeclass_info_var(Constraint, Var,
         RttiVarMaps0, RttiVarMaps),
 
Index: compiler/post_typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/post_typecheck.m,v
retrieving revision 1.159
diff -u -b -r1.159 post_typecheck.m
--- compiler/post_typecheck.m	19 Jun 2012 07:21:23 -0000	1.159
+++ compiler/post_typecheck.m	2 Jul 2012 01:07:01 -0000
@@ -269,7 +269,7 @@
     pred_info_get_head_type_params(!.PredInfo, HeadTypeParams),
     clauses_info_get_varset(ClausesInfo0, VarSet),
     clauses_info_get_vartypes(ClausesInfo0, VarTypesMap0),
-    map.to_assoc_list(VarTypesMap0, VarTypesList),
+    vartypes_to_assoc_list(VarTypesMap0, VarTypesList),
     set.init(Set0),
     check_var_type_bindings(VarTypesList, HeadTypeParams,
         [], UnresolvedVarsTypes, Set0, Set),
@@ -561,7 +561,7 @@
         pred_info_get_markers(CallerPredInfo, Markers),
         pred_info_get_clauses_info(CallerPredInfo, ClausesInfo),
         clauses_info_get_vartypes(ClausesInfo, VarTypes),
-        map.apply_to_list(Args0, VarTypes, ArgTypes),
+        lookup_var_types(VarTypes, Args0, ArgTypes),
         resolve_pred_overloading(ModuleInfo, Markers, TVarSet, ExistQVars,
             ArgTypes, HeadTypeParams, Context, !PredName, !:PredId)
     ;
@@ -592,7 +592,7 @@
         pred_info_get_clauses_info(!.PredInfo, ClausesInfo0),
         clauses_info_get_headvar_list(ClausesInfo0, HeadVars),
         pred_info_get_arg_types(!.PredInfo, ArgTypes),
-        map.from_corresponding_lists(HeadVars, ArgTypes, VarTypes),
+        vartypes_from_corresponding_lists(HeadVars, ArgTypes, VarTypes),
         clauses_info_set_vartypes(VarTypes, ClausesInfo0, ClausesInfo),
         pred_info_set_clauses_info(ClausesInfo, !PredInfo)
     ),
@@ -773,7 +773,7 @@
         RHS = rhs_functor(ConsId, _, _),
         pred_info_get_clauses_info(PredInfo, ClausesInfo),
         clauses_info_get_vartypes(ClausesInfo, VarTypes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         type_to_ctor_det(Type, TypeCtor),
         module_info_get_type_table(ModuleInfo, TypeTable),
         lookup_type_ctor_defn(TypeTable, TypeCtor, TypeDefn),
@@ -1029,7 +1029,7 @@
 resolve_unify_functor(X0, ConsId0, ArgVars0, Mode0, Unification0, UnifyContext,
         GoalInfo0, ModuleInfo, !PredInfo, !VarSet, !VarTypes,
         Goal, IsPlainUnify) :-
-    map.lookup(!.VarTypes, X0, TypeOfX),
+    lookup_var_type(!.VarTypes, X0, TypeOfX),
     list.length(ArgVars0, Arity),
     (
         % Is the function symbol apply/N or ''/N, representing a higher-order
@@ -1091,7 +1091,7 @@
         pred_info_get_typevarset(!.PredInfo, TVarSet),
         pred_info_get_exist_quant_tvars(!.PredInfo, ExistQTVars),
         pred_info_get_head_type_params(!.PredInfo, HeadTypeParams),
-        map.apply_to_list(ArgVars0, !.VarTypes, ArgTypes0),
+        lookup_var_types(!.VarTypes, ArgVars0, ArgTypes0),
         ArgTypes = ArgTypes0 ++ [TypeOfX],
         pred_info_get_constraint_map(!.PredInfo, ConstraintMap),
         GoalId = goal_info_get_goal_id(GoalInfo0),
@@ -1125,7 +1125,7 @@
         \+ pred_info_is_field_access_function(ModuleInfo, !.PredInfo),
 
         % Find the pred_id of the constant.
-        map.apply_to_list(ArgVars0, !.VarTypes, ArgTypes0),
+        lookup_var_types(!.VarTypes, ArgVars0, ArgTypes0),
         AllArgTypes = ArgTypes0 ++ HOArgTypes,
         pred_info_get_typevarset(!.PredInfo, TVarSet),
         pred_info_get_exist_quant_tvars(!.PredInfo, ExistQVars),
@@ -1175,7 +1175,7 @@
         % otherwise there would have been an error reported for unresolved
         % overloading.
         pred_info_get_typevarset(!.PredInfo, TVarSet),
-        map.apply_to_list(ArgVars0, !.VarTypes, ArgTypes0),
+        lookup_var_types(!.VarTypes, ArgVars0, ArgTypes0),
         \+ find_matching_constructor(ModuleInfo, TVarSet, ConsId0,
             TypeOfX, ArgTypes0)
     ->
@@ -1288,7 +1288,7 @@
 
 translate_get_function(ModuleInfo, !PredInfo, !VarTypes, !VarSet, FieldName,
         UnifyContext, FieldVar, TermInputVar, OldGoalInfo, GoalExpr) :-
-    map.lookup(!.VarTypes, TermInputVar, TermType),
+    lookup_var_type(!.VarTypes, TermInputVar, TermType),
     get_constructor_containing_field(ModuleInfo, TermType, FieldName,
         ConsId, FieldNumber),
 
@@ -1306,7 +1306,7 @@
     % by typecheck.m because the result can't be well-typed).
     (
         ExistQVars = [_ | _],
-        map.lookup(!.VarTypes, FieldVar, FieldType),
+        lookup_var_type(!.VarTypes, FieldVar, FieldType),
         list.det_index1(ArgTypes0, FieldNumber, FieldArgType),
         ( type_list_subsumes([FieldArgType], [FieldType], FieldSubst) ->
             apply_rec_subst_to_type_list(FieldSubst, ArgTypes0, ArgTypes)
@@ -1341,7 +1341,7 @@
 translate_set_function(ModuleInfo, !PredInfo, !VarTypes, !VarSet, FieldName,
         UnifyContext, FieldVar, TermInputVar, TermOutputVar, OldGoalInfo,
         Goal) :-
-    map.lookup(!.VarTypes, TermInputVar, TermType),
+    lookup_var_type(!.VarTypes, TermInputVar, TermType),
     get_constructor_containing_field(ModuleInfo, TermType, FieldName,
         ConsId0, FieldNumber),
 
@@ -1595,14 +1595,14 @@
 make_new_vars(Types, Vars, !VarTypes, !VarSet) :-
     list.length(Types, NumVars),
     varset.new_vars(NumVars, Vars, !VarSet),
-    map.det_insert_from_corresponding_lists(Vars, Types, !VarTypes).
+    vartypes_add_corresponding_lists(Vars, Types, !VarTypes).
 
 :- pred make_new_var(mer_type::in, prog_var::out, vartypes::in, vartypes::out,
     prog_varset::in, prog_varset::out) is det.
 
 make_new_var(Type, Var, !VarTypes, !VarSet) :-
     varset.new_var(Var, !VarSet),
-    map.det_insert(Var, Type, !VarTypes).
+    add_var_type(Var, Type, !VarTypes).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/prog_data.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_data.m,v
retrieving revision 1.243
diff -u -b -r1.243 prog_data.m
--- compiler/prog_data.m	19 Jun 2012 07:21:23 -0000	1.243
+++ compiler/prog_data.m	2 Jul 2012 01:07:01 -0000
@@ -1903,7 +1903,64 @@
             % A type expression with an explicit kind annotation.
             % (These are not yet used.)
 
-:- type vartypes == map(prog_var, mer_type).
+:- type vartypes.
+
+:- pred init_vartypes(vartypes::out) is det.
+
+:- pred vartypes_is_empty(vartypes::in) is semidet.
+
+:- pred vartypes_count(vartypes::in, int::out) is det.
+
+:- pred vartypes_select(set(prog_var)::in, vartypes::in, vartypes::out) is det.
+
+:- pred vartypes_optimize(vartypes::in, vartypes::out) is det.
+
+:- pred add_var_type(prog_var::in, mer_type::in,
+    vartypes::in, vartypes::out) is det.
+:- pred update_var_type(prog_var::in, mer_type::in,
+    vartypes::in, vartypes::out) is det.
+
+:- pred search_insert_var_type(prog_var::in, mer_type::in,
+    maybe(mer_type)::out, vartypes::in, vartypes::out) is det.
+
+:- pred is_in_vartypes(vartypes::in, prog_var::in) is semidet.
+
+:- pred search_var_type(vartypes::in, prog_var::in, mer_type::out) is semidet.
+
+:- pred lookup_var_type(vartypes::in, prog_var::in, mer_type::out) is det.
+:- pred lookup_var_types(vartypes::in, list(prog_var)::in,
+    list(mer_type)::out) is det.
+
+:- pred vartypes_vars(vartypes::in, list(prog_var)::out) is det.
+:- pred vartypes_types(vartypes::in, list(mer_type)::out) is det.
+
+:- pred vartypes_to_assoc_list(vartypes::in,
+    assoc_list(prog_var, mer_type)::out) is det.
+
+:- pred vartypes_from_corresponding_lists(list(prog_var)::in,
+    list(mer_type)::in, vartypes::out) is det.
+
+:- pred vartypes_from_sorted_assoc_list(assoc_list(prog_var, mer_type)::in,
+    vartypes::out) is det.
+
+:- pred vartypes_add_corresponding_lists(list(prog_var)::in,
+    list(mer_type)::in, vartypes::in, vartypes::out) is det.
+:- pred vartypes_overlay_corresponding_lists(list(prog_var)::in,
+    list(mer_type)::in, vartypes::in, vartypes::out) is det.
+
+:- pred delete_var_type(prog_var::in,
+    vartypes::in, vartypes::out) is det.
+:- pred delete_var_types(list(prog_var)::in,
+    vartypes::in, vartypes::out) is det.
+:- pred delete_sorted_var_types(list(prog_var)::in,
+    vartypes::in, vartypes::out) is det.
+
+:- pred transform_var_types(pred(mer_type, mer_type)::in(pred(in, out) is det),
+    vartypes::in, vartypes::out) is det.
+
+:- pred transform_foldl_var_types(
+    pred(mer_type, mer_type, T, T)::in(pred(in, out, in, out) is det),
+    vartypes::in, vartypes::out, T::in, T::out) is det.
 
 :- type prog_var_set_types
     --->    prog_var_set_types(prog_varset, vartypes).
@@ -1971,6 +2028,89 @@
 
 :- implementation.
 
+:- type vartypes == map(prog_var, mer_type).
+
+init_vartypes(VarTypes) :-
+    map.init(VarTypes).
+
+vartypes_is_empty(VarTypes) :-
+    map.is_empty(VarTypes).
+
+vartypes_count(VarTypes, Count) :-
+    map.count(VarTypes, Count).
+
+vartypes_select(SelectedVars, !VarTypes) :-
+    map.select(!.VarTypes, SelectedVars, !:VarTypes).
+
+vartypes_optimize(!VarTypes) :-
+    map.optimize(!VarTypes).
+
+add_var_type(Var, Type, !VarTypes) :-
+    map.det_insert(Var, Type, !VarTypes).
+
+update_var_type(Var, Type, !VarTypes) :-
+    map.det_update(Var, Type, !VarTypes).
+
+search_insert_var_type(Var, NewType, MaybeOldType, !VarTypes) :-
+    map.search_insert(Var, NewType, MaybeOldType, !VarTypes).
+
+is_in_vartypes(VarTypes, Var) :-
+    map.contains(VarTypes, Var).
+
+search_var_type(VarTypes, Var, Type) :-
+    map.search(VarTypes, Var, Type).
+
+lookup_var_type(VarTypes, Var, Type) :-
+    map.lookup(VarTypes, Var, Type).
+
+lookup_var_types(_VarTypes, [], []).
+lookup_var_types(VarTypes, [Var | Vars], [Type | Types]) :-
+    lookup_var_type(VarTypes, Var, Type),
+    lookup_var_types(VarTypes, Vars, Types).
+
+vartypes_vars(VarTypes, Vars) :-
+    map.keys(VarTypes, Vars).
+
+vartypes_types(VarTypes, Types) :-
+    map.values(VarTypes, Types).
+
+vartypes_to_assoc_list(VarTypes, AssocList) :-
+    map.to_assoc_list(VarTypes, AssocList).
+
+vartypes_from_corresponding_lists(Vars, Types, VarTypes) :-
+    map.from_corresponding_lists(Vars, Types, VarTypes).
+
+vartypes_from_sorted_assoc_list(AssocList, VarTypes) :-
+    map.from_sorted_assoc_list(AssocList, VarTypes).
+
+vartypes_add_corresponding_lists(Vars, Types, !VarTypes) :-
+    map.det_insert_from_corresponding_lists(Vars, Types, !VarTypes).
+
+vartypes_overlay_corresponding_lists([], [], !VarTypes).
+vartypes_overlay_corresponding_lists([], [_ | _], !VarTypes) :-
+    unexpected($module, $pred, "mismatched list lengths").
+vartypes_overlay_corresponding_lists([_ | _], [], !VarTypes) :-
+    unexpected($module, $pred, "mismatched list lengths").
+vartypes_overlay_corresponding_lists([Var | Vars], [Type | Types],
+        !VarTypes) :-
+    map.set(Var, Type, !VarTypes),
+    vartypes_overlay_corresponding_lists(Vars, Types, !VarTypes).
+
+delete_var_type(Var, !VarTypes) :-
+    map.delete(Var, !VarTypes).
+
+delete_var_types(Vars, !VarTypes) :-
+    map.delete_list(Vars, !VarTypes).
+
+delete_sorted_var_types(SortedVars, !VarTypes) :-
+    map.delete_sorted_list(SortedVars, !VarTypes).
+
+transform_var_types(Transform, !VarTypes) :-
+    map.map_values_only(Transform, !VarTypes).
+
+transform_foldl_var_types(Transform, !VarTypes, !Acc) :-
+    map.map_values_foldl(Transform, !VarTypes, !Acc).
+
 tvarset_merge_renaming(TVarSetA, TVarSetB, TVarSet, Renaming) :-
     varset.merge_renaming(TVarSetA, TVarSetB, TVarSet, Renaming).
 
Index: compiler/prog_type.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_type.m,v
retrieving revision 1.61
diff -u -b -r1.61 prog_type.m
--- compiler/prog_type.m	11 Jun 2012 03:13:21 -0000	1.61
+++ compiler/prog_type.m	2 Jul 2012 01:07:01 -0000
@@ -309,6 +309,10 @@
     % Given a constant and an arity, return a type_ctor.
     % Fails if the constant is not an atom.
     %
+    % This really ought to take a name and an arity -
+    % use of integers/floats/strings as type names should
+    % be rejected by the parser in prog_io.m, not in module_qual.m.
+    %
 :- pred make_type_ctor(const::in, int::in, type_ctor::out) is semidet.
 
 :- type polymorphism_cell
@@ -876,7 +880,7 @@
     is semidet.
 
 var_is_introduced_type_info_type(VarTypes, Var) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     is_introduced_type_info_type(Type).
 
 remove_new_prefix(unqualified(Name0), unqualified(Name)) :-
@@ -891,11 +895,6 @@
 
 %-----------------------------------------------------------------------------%
 
-    % Given a constant and an arity, return a type_ctor.
-    % This really ought to take a name and an arity -
-    % use of integers/floats/strings as type names should
-    % be rejected by the parser in prog_io.m, not in module_qual.m.
-
 make_type_ctor(term.atom(Name), Arity, type_ctor(unqualified(Name), Arity)).
 
 %-----------------------------------------------------------------------------%
Index: compiler/prog_type_subst.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prog_type_subst.m,v
retrieving revision 1.10
diff -u -b -r1.10 prog_type_subst.m
--- compiler/prog_type_subst.m	23 May 2011 05:08:10 -0000	1.10
+++ compiler/prog_type_subst.m	2 Jul 2012 01:07:01 -0000
@@ -320,14 +320,14 @@
 
 %-----------------------------------------------------------------------------%
 
-apply_variable_renaming_to_vartypes(Renaming, !Map) :-
-    map.map_values_only(apply_variable_renaming_to_type(Renaming), !Map).
+apply_variable_renaming_to_vartypes(Renaming, !VarTypes) :-
+    transform_var_types(apply_variable_renaming_to_type(Renaming), !VarTypes).
 
 apply_subst_to_vartypes(Subst, !VarTypes) :-
-    map.map_values_only(apply_subst_to_type(Subst), !VarTypes).
+    transform_var_types(apply_subst_to_type(Subst), !VarTypes).
 
 apply_rec_subst_to_vartypes(Subst, !VarTypes) :-
-    map.map_values_only(apply_rec_subst_to_type(Subst), !VarTypes).
+    transform_var_types(apply_rec_subst_to_type(Subst), !VarTypes).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/prop_mode_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/prop_mode_constraints.m,v
retrieving revision 1.36
diff -u -b -r1.36 prop_mode_constraints.m
--- compiler/prop_mode_constraints.m	13 Feb 2012 00:11:46 -0000	1.36
+++ compiler/prop_mode_constraints.m	2 Jul 2012 01:07:01 -0000
@@ -436,10 +436,10 @@
     ( set_of_var.contains(!.SeenSoFar, Var0) ->
         % Make new variable.
         OldVarName = varset.lookup_name(!.VarSet, Var0),
-        OldVarType = map.lookup(!.VarTypes, Var0),
+        lookup_var_type(!.VarTypes, Var0, OldVarType),
         NewVarName = "Arg_" ++ OldVarName,
         varset.new_uniquely_named_var(NewVarName, Var, !VarSet),
-        map.set(Var, OldVarType, !VarTypes),
+        add_var_type(Var, OldVarType, !VarTypes),
 
         % Make new unification.
         create_atomic_complicated_unification(Var0, rhs_var(Var), Context,
Index: compiler/purity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/purity.m,v
retrieving revision 1.155
diff -u -b -r1.155 purity.m
--- compiler/purity.m	16 Apr 2012 08:13:19 -0000	1.155
+++ compiler/purity.m	2 Jul 2012 01:07:01 -0000
@@ -1222,7 +1222,7 @@
     % If the unification involves a higher order RHS, check that
     % the purity of the ConsId matches the purity of the variable's type.
     VarTypes = Info ^ pi_vartypes,
-    map.lookup(VarTypes, Var, TypeOfVar),
+    lookup_var_type(VarTypes, Var, TypeOfVar),
     PredInfo = Info ^ pi_pred_info,
     pred_info_get_markers(PredInfo, CallerMarkers),
     Context = goal_info_get_context(GoalInfo),
@@ -1234,7 +1234,7 @@
         pred_info_get_typevarset(PredInfo, TVarSet),
         pred_info_get_exist_quant_tvars(PredInfo, ExistQTVars),
         pred_info_get_head_type_params(PredInfo, HeadTypeParams),
-        map.apply_to_list(Args, VarTypes, ArgTypes0),
+        lookup_var_types(VarTypes, Args, ArgTypes0),
         list.append(ArgTypes0, VarArgTypes, PredArgTypes),
         ModuleInfo = Info ^ pi_module_info,
         (
@@ -1302,7 +1302,7 @@
     prog_var::in, mer_type::out, list(error_spec)::out) is det.
 
 check_outer_var_type(Context, VarTypes, VarSet, Var, VarType, Specs) :-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     (
         ( VarType = io_state_type
         ; VarType = stm_atomic_type
Index: compiler/qual_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/qual_info.m,v
retrieving revision 1.29
diff -u -b -r1.29 qual_info.m
--- compiler/qual_info.m	16 Apr 2012 08:13:19 -0000	1.29
+++ compiler/qual_info.m	2 Jul 2012 01:07:01 -0000
@@ -140,7 +140,7 @@
     varset.init(TVarSet),
     map.init(Renaming),
     map.init(Index),
-    map.init(VarTypes),
+    init_vartypes(VarTypes),
     FoundSyntaxError = no,
     QualInfo = qual_info(EqvMap, TVarSet, Renaming, Index, VarTypes,
         MQInfo, status_local, FoundSyntaxError).
@@ -221,7 +221,7 @@
     list(error_spec)::in, list(error_spec)::out) is det.
 
 update_var_types(Var, Type, Context, !VarTypes, !Specs) :-
-    ( map.search(!.VarTypes, Var, Type0) ->
+    ( search_var_type(!.VarTypes, Var, Type0) ->
         ( Type = Type0 ->
             true
         ;
@@ -232,7 +232,7 @@
             !:Specs = [Spec | !.Specs]
         )
     ;
-        map.det_insert(Var, Type, !VarTypes)
+        add_var_type(Var, Type, !VarTypes)
     ).
 
 %-----------------------------------------------------------------------------%
Index: compiler/rbmm.points_to_graph.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.points_to_graph.m,v
retrieving revision 1.10
diff -u -b -r1.10 rbmm.points_to_graph.m
--- compiler/rbmm.points_to_graph.m	23 May 2011 05:08:11 -0000	1.10
+++ compiler/rbmm.points_to_graph.m	2 Jul 2012 01:07:01 -0000
@@ -148,8 +148,9 @@
     %
 :- pred rptg_equal(rpt_graph::in, rpt_graph::in) is semidet.
 
-    % This predicate returns a set of nodes (regions) reachable from a
-    % variable X.
+    % This predicate finds all regions that are reachable from X.
+    % The regions must be reached by edges with labels (type selectors)
+    % which are valid with the type of X.
     %
 :- pred rptg_reach_from_a_variable(rpt_graph::in, module_info::in,
     proc_info::in, prog_var::in, set(rptg_node)::in, set(rptg_node)::out)
@@ -933,16 +934,12 @@
     simple_map_equal(V1, V2),
     complex_map_equal_2(Ks, Map1, Map2).
 
-    % This predicate finds all regions that are reachable from X.
-    % The regions must be reached by edges with labels (type selectors)
-    % which are valid with the type of X.
-    %
 rptg_reach_from_a_variable(Graph, HLDS, ProcInfo, X, !Reach_X) :-
     rptg_get_node_by_variable(Graph, X, N_X),
     Node_Selector = pair(N_X, []),
 
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, X, TypeX),
+    lookup_var_type(VarTypes, X, TypeX),
 
     % Find regions reached from X.
     reach_from_a_variable_2([Node_Selector], Graph, HLDS,
Index: compiler/rbmm.points_to_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.points_to_info.m,v
retrieving revision 1.8
diff -u -b -r1.8 rbmm.points_to_info.m
--- compiler/rbmm.points_to_info.m	23 May 2011 05:08:11 -0000	1.8
+++ compiler/rbmm.points_to_info.m	2 Jul 2012 01:07:01 -0000
@@ -88,17 +88,17 @@
 
 rpta_info_init(ProcInfo) = RptaInfo :-
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.keys(VarTypes, Vars),
+    vartypes_vars(VarTypes, Vars),
     list.foldl2(add_node_from_var(VarTypes), Vars, 1, _Reg,
         rpt_graph_init, Graph),
     map.init(AlphaMapping),
     RptaInfo = rpta_info(Graph, AlphaMapping).
 
-:- pred add_node_from_var(map(prog_var, mer_type)::in, prog_var::in, int::in,
+:- pred add_node_from_var(vartypes::in, prog_var::in, int::in,
     int::out, rpt_graph::in, rpt_graph::out) is det.
 
 add_node_from_var(VarTypes, Var, Reg0, Reg, !Graph) :-
-    map.lookup(VarTypes, Var, NodeType),
+    lookup_var_type(VarTypes, Var, NodeType),
     set.init(Varset0),
     set.insert(Var, Varset0, Varset),
     Reg = Reg0 + 1,
Index: compiler/rbmm.region_liveness_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.region_liveness_info.m,v
retrieving revision 1.3
diff -u -b -r1.3 rbmm.region_liveness_info.m
--- compiler/rbmm.region_liveness_info.m	23 May 2011 05:08:11 -0000	1.3
+++ compiler/rbmm.region_liveness_info.m	2 Jul 2012 01:07:01 -0000
@@ -119,7 +119,7 @@
 find_input_output_args(ModuleInfo, CalleeProcInfo, Inputs, Outputs) :-
     proc_info_get_headvars(CalleeProcInfo, ArgVars),
     proc_info_get_vartypes(CalleeProcInfo, VarTypes),
-    list.map(map.lookup(VarTypes), ArgVars, ArgTypes),
+    lookup_var_types(VarTypes, ArgVars, ArgTypes),
     proc_info_get_argmodes(CalleeProcInfo, ArgModes),
     arg_info.compute_in_and_out_vars(ModuleInfo, ArgVars, ArgModes, ArgTypes,
         Inputs, Outputs).
Index: compiler/rbmm.region_transformation.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.region_transformation.m,v
retrieving revision 1.23
diff -u -b -r1.23 rbmm.region_transformation.m
--- compiler/rbmm.region_transformation.m	13 Feb 2012 00:11:47 -0000	1.23
+++ compiler/rbmm.region_transformation.m	2 Jul 2012 01:07:01 -0000
@@ -774,7 +774,7 @@
         RegVar = RegVar0
     ;
         varset.new_named_var(Name, RegVar, !VarSet),
-        map.det_insert(RegVar, region_type, !VarTypes),
+        add_var_type(RegVar, region_type, !VarTypes),
         map.det_insert(Name, RegVar, !NameToVar)
     ).
 
Index: compiler/saved_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/saved_vars.m,v
retrieving revision 1.99
diff -u -b -r1.99 saved_vars.m
--- compiler/saved_vars.m	26 Mar 2012 00:43:33 -0000	1.99
+++ compiler/saved_vars.m	2 Jul 2012 01:07:01 -0000
@@ -600,8 +600,8 @@
     !.SlotInfo = slot_info(Varset0, VarTypes0, RttiVarMaps0, TypeInfoLiveness),
     varset.new_var(NewVar, Varset0, Varset),
     map.from_assoc_list([Var - NewVar], Substitution),
-    map.lookup(VarTypes0, Var, Type),
-    map.det_insert(NewVar, Type, VarTypes0, VarTypes),
+    lookup_var_type(VarTypes0, Var, Type),
+    add_var_type(NewVar, Type, VarTypes0, VarTypes),
     rtti_var_info_duplicate(Var, NewVar, RttiVarMaps0, RttiVarMaps),
     !:SlotInfo = slot_info(Varset, VarTypes, RttiVarMaps,
         TypeInfoLiveness).
@@ -621,7 +621,7 @@
 slot_info_do_not_duplicate_var(SlotInfo, Var) :-
     SlotInfo = slot_info(_, VarTypes, _, TypeInfoLiveness),
     TypeInfoLiveness = yes,
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     polymorphism.type_is_type_info_or_ctor_type(Type).
 
 %-----------------------------------------------------------------------------%
Index: compiler/simplify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/simplify.m,v
retrieving revision 1.281
diff -u -b -r1.281 simplify.m
--- compiler/simplify.m	18 Jun 2012 07:00:46 -0000	1.281
+++ compiler/simplify.m	2 Jul 2012 01:07:01 -0000
@@ -448,7 +448,7 @@
     list.sort(ElimVarsLists0, ElimVarsLists),
     list.condense(ElimVarsLists, ElimVars),
     varset.delete_sorted_vars(ElimVars, VarSet0, VarSet1),
-    map.delete_sorted_list(ElimVars, VarTypes1, VarTypes),
+    delete_sorted_var_types(ElimVars, VarTypes1, VarTypes),
     % We only eliminate vars that cannot occur in RttiVarMaps.
     ( simplify_do_after_front_end(Info) ->
         proc_info_get_var_name_remap(!.ProcInfo, VarNameRemap),
@@ -492,7 +492,7 @@
 simplify_proc_maybe_vary_parameters(ModuleInfo, PredId, ProcInfo,
         !Simplifications) :-
     proc_info_get_vartypes(ProcInfo, VarTypes0),
-    NumVars = map.count(VarTypes0),
+    vartypes_count(VarTypes0, NumVars),
     ( NumVars > turn_off_common_struct_threshold ->
         % If we have too many variables, common_struct takes so long that
         % either the compiler runs out of memory or the user runs out of
@@ -1183,7 +1183,7 @@
     instmap_lookup_var(InstMap0, Var, VarInst),
     simplify_info_get_var_types(!.Info, VarTypes),
     ( inst_is_bound_to_functors(ModuleInfo0, VarInst, BoundInsts) ->
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         type_to_ctor_det(VarType, VarTypeCtor),
         list.map(bound_inst_to_cons_id(VarTypeCtor), BoundInsts, ConsIds0),
         list.sort(ConsIds0, ConsIds),
@@ -1220,7 +1220,7 @@
             % existential type variables in the types of the constructor
             % arguments or their typeinfos.
 
-            map.lookup(VarTypes, Var, Type),
+            lookup_var_type(VarTypes, Var, Type),
             simplify_info_get_module_info(!.Info, ModuleInfo1),
             ( cons_id_is_existq_cons(ModuleInfo1, Type, MainConsId) ->
                 GoalExpr = switch(Var, SwitchCanFail, Cases),
@@ -1724,7 +1724,7 @@
         ;
             Unification = deconstruct(LHSVar, _ConsId, _Args, _ArgModes,
                 _CanFail, _CanCGC),
-            map.lookup(VarTypes, LHSVar, LHSVarType),
+            lookup_var_type(VarTypes, LHSVar, LHSVarType),
             ( type_to_type_defn_body(ModuleInfo, LHSVarType, TypeBody) ->
                 CanSwitchOnType = can_switch_on_type(TypeBody),
                 (
@@ -2093,7 +2093,7 @@
     ;
         unexpected($module, $pred, "not construction unification")
     ),
-    map.lookup(VarTypes, LHSVar, TermType),
+    lookup_var_type(VarTypes, LHSVar, TermType),
     (
         RHSVars = [],
         Arg = csa_constant(ConsId, TermType)
@@ -2432,7 +2432,7 @@
 
     % We have to add the type of R to the var_types.
     simplify_info_get_var_types(!.Info, VarTypes0),
-    map.det_insert(R, comparison_result_type, VarTypes0, VarTypes),
+    add_var_type(R, comparison_result_type, VarTypes0, VarTypes),
     simplify_info_set_var_types(VarTypes, !Info),
 
     % Construct the call to compare/3.
@@ -2714,7 +2714,7 @@
         list.reverse(Args, [Y, X, Res | _]),
         simplify_info_get_module_info(!.Info, ModuleInfo),
         simplify_info_get_var_types(!.Info, VarTypes),
-        map.lookup(VarTypes, Y, Type),
+        lookup_var_type(VarTypes, Y, Type),
         type_definitely_has_no_user_defined_equality_pred(ModuleInfo, Type),
 
         require_det (
@@ -2802,7 +2802,7 @@
     simplify_info_get_varset(!.Info, VarSet0),
     simplify_info_get_var_types(!.Info, VarTypes0),
     varset.new_var(ConstVar, VarSet0, VarSet),
-    map.det_insert(ConstVar, int_type, VarTypes0, VarTypes),
+    add_var_type(ConstVar, int_type, VarTypes0, VarTypes),
     simplify_info_set_varset(VarSet, !Info),
     simplify_info_set_var_types(VarTypes, !Info),
 
@@ -2971,7 +2971,7 @@
         GoalInfo0, Goal, !Info) :-
     simplify_info_get_module_info(!.Info, ModuleInfo),
     simplify_info_get_var_types(!.Info, VarTypes),
-    map.lookup(VarTypes, XVar, Type),
+    lookup_var_type(VarTypes, XVar, Type),
     ( Type = type_variable(TypeVar, Kind) ->
         % Convert polymorphic unifications into calls to `unify/2',
         % the general unification predicate, passing the appropriate type_info:
@@ -3553,7 +3553,7 @@
     Case0 = case(MainConsId, OtherConsIds, Goal0),
     simplify_info_get_module_info(!.Info, ModuleInfo0),
     simplify_info_get_var_types(!.Info, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     bind_var_to_functors(Var, Type, MainConsId, OtherConsIds,
         InstMap0, InstMap1, ModuleInfo0, ModuleInfo1),
     simplify_info_set_module_info(ModuleInfo1, !Info),
@@ -3608,11 +3608,10 @@
     simplify_info_get_varset(!.Info, VarSet0),
     simplify_info_get_var_types(!.Info, VarTypes0),
     varset.new_vars(ConsArity, ArgVars, VarSet0, VarSet),
-    map.lookup(VarTypes0, Var, VarType),
+    lookup_var_type(VarTypes0, Var, VarType),
     simplify_info_get_module_info(!.Info, ModuleInfo),
     type_util.get_cons_id_arg_types(ModuleInfo, VarType, ConsId, ArgTypes),
-    map.det_insert_from_corresponding_lists(ArgVars, ArgTypes,
-        VarTypes0, VarTypes),
+    vartypes_add_corresponding_lists(ArgVars, ArgTypes, VarTypes0, VarTypes),
     simplify_info_set_varset(VarSet, !Info),
     simplify_info_set_var_types(VarTypes, !Info),
     simplify_info_get_instmap(!.Info, InstMap),
@@ -3965,7 +3964,7 @@
         !Info) :-
     simplify_info_get_var_types(!.Info, VarTypes0),
     simplify_info_get_rtti_varmaps(!.Info, RttiVarMaps0),
-    map.map_values_only(apply_rec_subst_to_type(TSubst), VarTypes0, VarTypes),
+    transform_var_types(apply_rec_subst_to_type(TSubst), VarTypes0, VarTypes),
     Renaming = map.singleton(ToVar, FromVar),
     apply_substitutions_to_rtti_varmaps(map.init, TSubst, Renaming,
         RttiVarMaps0, RttiVarMaps1),
Index: compiler/size_prof.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/size_prof.m,v
retrieving revision 1.74
diff -u -b -r1.74 size_prof.m
--- compiler/size_prof.m	8 Jun 2012 15:37:00 -0000	1.74
+++ compiler/size_prof.m	2 Jul 2012 01:07:01 -0000
@@ -190,7 +190,7 @@
 
 :- type type_info_map       == map(mer_type, prog_var).
 :- type type_ctor_map       == map(type_ctor, prog_var).
-:- type rev_type_info_map   == vartypes.
+:- type rev_type_info_map   == map(prog_var, mer_type).
 :- type rev_type_ctor_map   == map(prog_var, type_ctor).
 :- type known_size_map      == map(prog_var, int).
 
@@ -613,7 +613,7 @@
 
 size_prof_process_construct(LHS, RHS, UniMode, UnifyContext, Var, ConsId,
         Args, ArgModes, How, Unique, GoalInfo, GoalExpr, !Info) :-
-    map.lookup(!.Info ^ spi_vartypes, Var, VarType),
+    lookup_var_type(!.Info ^ spi_vartypes, Var, VarType),
     type_to_ctor_det(VarType, VarTypeCtor),
     type_ctor_module_name_arity(VarTypeCtor,
         VarTypeCtorModule, VarTypeCtorName, _),
@@ -673,7 +673,7 @@
 
 size_prof_process_deconstruct(Var, ConsId, Args, ArgModes, Goal0, GoalExpr,
         !Info) :-
-    map.lookup(!.Info ^ spi_vartypes, Var, VarType),
+    lookup_var_type(!.Info ^ spi_vartypes, Var, VarType),
     type_to_ctor_det(VarType, VarTypeCtor),
     type_ctor_module_name_arity(VarTypeCtor, VarTypeCtorModule,
         VarTypeCtorName, _),
@@ -792,7 +792,7 @@
 size_prof_process_args([], !KnownSize, !MaybeSizeVar, _, [], !Info).
 size_prof_process_args([Arg | Args], !KnownSize, !MaybeSizeVar, Context, Goals,
         !Info) :-
-    map.lookup(!.Info ^ spi_vartypes, Arg, Type),
+    lookup_var_type(!.Info ^ spi_vartypes, Arg, Type),
     ( map.search(!.Info ^ spi_known_size_map, Arg, ArgSize) ->
         !:KnownSize = !.KnownSize + ArgSize,
         ArgGoals = []
@@ -1042,7 +1042,7 @@
     string.int_to_string(VarNum, VarNumStr),
     string.append(Prefix, VarNumStr, Name),
     varset.name_var(Var, Name, VarSet1,  VarSet),
-    map.set(Var, Type, VarTypes0, VarTypes),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     !Info ^ spi_varset := VarSet,
     !Info ^ spi_vartypes := VarTypes.
 
Index: compiler/ssdebug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ssdebug.m,v
retrieving revision 1.37
diff -u -b -r1.37 ssdebug.m
--- compiler/ssdebug.m	13 Feb 2012 00:11:49 -0000	1.37
+++ compiler/ssdebug.m	2 Jul 2012 01:07:01 -0000
@@ -981,7 +981,7 @@
     TypeCtor = type_ctor(qualified(SSDBModule, "ssdb_retry"), 0),
     construct_type(TypeCtor, [], RetryType),
     varset.new_named_var(VarName, RetryVar, !VarSet),
-    map.det_insert(RetryVar, RetryType, !VarTypes).
+    add_var_type(RetryVar, RetryType, !VarTypes).
 
     % Create the goal for recursive call in the case of a retry.
     %
@@ -1150,7 +1150,7 @@
     varset.new_named_var("ProcId", ProcIdVar, !VarSet),
     ConsId = cons(qualified(SSDBModule, "ssdb_proc_id"), 2, TypeCtor),
     construct_type(TypeCtor, [], ProcIdType),
-    map.det_insert(ProcIdVar, ProcIdType, !VarTypes),
+    add_var_type(ProcIdVar, ProcIdType, !VarTypes),
     construct_functor(ProcIdVar, ConsId, [ModuleNameVar, PredNameVar],
         ConstructProcIdGoal),
 
@@ -1204,7 +1204,7 @@
 make_arg_list(_Pos, _InstMap, [], _Renaming, OutVar, [Goal], !ModuleInfo,
         !ProcInfo, !PredInfo, !VarSet, !VarTypes, !BoundVarDescs) :-
     varset.new_named_var("EmptyVarList", OutVar, !VarSet),
-    map.det_insert(OutVar, list_var_value_type, !VarTypes),
+    add_var_type(OutVar, list_var_value_type, !VarTypes),
     ListTypeSymName = qualified(mercury_list_module, "list"),
     ListTypeCtor = type_ctor(ListTypeSymName, 1),
     ConsId = cons(qualified(mercury_list_module, "[]" ), 0, ListTypeCtor),
@@ -1217,7 +1217,7 @@
     make_arg_list(Pos, InstMap, ProgVars, Renaming, OutVar0, Goals0,
         !ModuleInfo, !ProcInfo, !PredInfo, !VarSet, !VarTypes, !BoundVarDescs),
 
-    map.lookup(!.VarTypes, ProgVar, ProgVarType),
+    lookup_var_type(!.VarTypes, ProgVar, ProgVarType),
     (
         ( ProgVarType = io_state_type
         ; ProgVarType = io_io_type
@@ -1241,7 +1241,7 @@
         ),
 
         varset.new_named_var("FullListVar", OutVar, !VarSet),
-        map.det_insert(OutVar, list_var_value_type, !VarTypes),
+        add_var_type(OutVar, list_var_value_type, !VarTypes),
         ListTypeSymName = qualified(mercury_list_module, "list"),
         ListTypeCtor = type_ctor(ListTypeSymName, 1),
         ConsId = cons(qualified(unqualified("list"), "[|]" ), 2, ListTypeCtor),
@@ -1304,7 +1304,7 @@
 
         create_poly_info(!.ModuleInfo, !.PredInfo, !.ProcInfo, PolyInfo0),
         term.context_init(Context),
-        map.lookup(!.VarTypes, VarToInspect, MerType),
+        lookup_var_type(!.VarTypes, VarToInspect, MerType),
         polymorphism_make_type_info_var(MerType, Context, TypeInfoVar,
             TypeInfoGoals0, PolyInfo0, PolyInfo),
         poly_info_extract(PolyInfo, !PredInfo, !ProcInfo, !:ModuleInfo),
@@ -1315,7 +1315,7 @@
         % Constructor of the variable's description.
         ConsId = cons(qualified(SSDBModule, "bound_head_var"), 3,
             VarValueTypeCtor),
-        map.det_insert(VarDesc, VarValueType, !VarTypes),
+        add_var_type(VarDesc, VarValueType, !VarTypes),
 
         % Renaming contains the names of all instantiated arguments
         % during the execution of the procedure's body.
@@ -1341,7 +1341,7 @@
     ;
         ConsId = cons(qualified(SSDBModule, "unbound_head_var"), 2,
             VarValueTypeCtor),
-        map.det_insert(VarDesc, VarValueType, !VarTypes),
+        add_var_type(VarDesc, VarValueType, !VarTypes),
         construct_functor(VarDesc, ConsId, [VarNameVar, VarPosVar],
             ConstructVarGoal),
 
Index: compiler/stack_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_alloc.m,v
retrieving revision 1.34
diff -u -b -r1.34 stack_alloc.m
--- compiler/stack_alloc.m	17 Oct 2011 04:31:30 -0000	1.34
+++ compiler/stack_alloc.m	2 Jul 2012 01:07:01 -0000
@@ -312,7 +312,7 @@
 :- pred var_is_float(vartypes::in, prog_var::in) is semidet.
 
 var_is_float(VarTypes, Var) :-
-    map.lookup(VarTypes, Var, float_type).
+    lookup_var_type(VarTypes, Var, float_type).
 
 :- pred allocate_same_stack_slot(list(prog_var)::in, stack_slot::in,
     stack_slots::in, stack_slots::out) is det.
Index: compiler/stack_opt.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_opt.m,v
retrieving revision 1.58
diff -u -b -r1.58 stack_opt.m
--- compiler/stack_opt.m	8 Jun 2012 15:46:21 -0000	1.58
+++ compiler/stack_opt.m	2 Jul 2012 01:07:01 -0000
@@ -419,7 +419,7 @@
         ConsId = cons(_Name, _Arity, _TypeCtor),
         IntParams = !.IntervalInfo ^ ii_interval_params,
         VarTypes = IntParams ^ ip_var_types,
-        map.lookup(VarTypes, CellVar, Type),
+        lookup_var_type(VarTypes, CellVar, Type),
         (
             type_is_tuple(Type, _)
         ->
Index: compiler/stm_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stm_expand.m,v
retrieving revision 1.27
diff -u -b -r1.27 stm_expand.m
--- compiler/stm_expand.m	13 Feb 2012 00:11:49 -0000	1.27
+++ compiler/stm_expand.m	2 Jul 2012 01:07:01 -0000
@@ -31,14 +31,12 @@
 %   foo(X, Y, IO0, IO) :-
 %       'StmExpanded_toplevel_0_0_0'(X, Y, IO0, IO).
 %
-%
 %   :- pred 'StmExpanded_toplevel_0_0_0'(int::in, int::out, io::di, io::uo)
 %           is det.
 %   'StmExpanded_toplevel_0_0_0'(X, Y, IO0, IO) :-
 %       'StmExpanded_rollback_0_0_0'(X, Y),
 %       IO0 = IO.
 %
-%
 %   :- pred 'StmExpanded_rollback_0_0_0'(int::in, int::out) is cc_multi.
 %   'StmExpanded_rollback_0_0_0'(X, Y) :-
 %       promise_pure (
@@ -80,7 +78,6 @@
 %           )
 %       ).
 %
-%
 %   :- pred 'StmExpanded_wrapper_0_0_0'(int::in, int::out, stm::di, stm::uo)
 %           is det.
 %   'StmExpanded_wrapper_0_0_0'(X, Result, STM0, STM) :-
@@ -100,7 +97,6 @@
 %               throw(rollback_invalid_transaction)
 %           ).
 %
-%
 % Currently, the atomic goal supports a single STM transaction with any number
 % of input and output arguments.  As the atomic goal may need to unroll the
 % call stack (when performing a retry or a rollback), the exception module
@@ -160,15 +156,11 @@
 :- interface.
 
 :- import_module hlds.hlds_module.
-:- import_module hlds.hlds_pred.
 
 %-----------------------------------------------------------------------------%
 
 :- pred stm_process_module(module_info::in, module_info::out) is det.
 
-:- pred stm_process_pred(pred_id::in, module_info::in, module_info::out)
-    is det.
-
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -180,6 +172,7 @@
 :- import_module hlds.goal_util.
 :- import_module hlds.hlds_goal.
 :- import_module hlds.hlds_pred.
+:- import_module hlds.hlds_pred.
 :- import_module hlds.instmap.
 :- import_module hlds.pred_table.
 :- import_module hlds.quantification.
@@ -252,13 +245,16 @@
     list.foldl(stm_process_pred, PredIds, !ModuleInfo),
     module_info_clobber_dependency_info(!ModuleInfo).
 
+:- pred stm_process_pred(pred_id::in, module_info::in, module_info::out)
+    is det.
+
 stm_process_pred(PredId, !ModuleInfo) :-
     module_info_pred_info(!.ModuleInfo, PredId, PredInfo),
     ProcIds = pred_info_procids(PredInfo),
     list.foldl(stm_process_proc(PredId), ProcIds, !ModuleInfo).
 
-:- pred stm_process_proc(pred_id::in, proc_id::in, module_info::in,
-    module_info::out) is det.
+:- pred stm_process_proc(pred_id::in, proc_id::in,
+    module_info::in, module_info::out) is det.
 
 stm_process_proc(PredId, ProcId, !ModuleInfo) :-
     module_info_get_preds(!.ModuleInfo, PredTable0),
@@ -497,7 +493,7 @@
             FinalGoal, !StmInfo)
     ;
         GoalType = nested_atomic_goal,
-        trace [io(!IO)] (
+        trace [compiletime(flag("debug_stm")), io(!IO)] (
             io.write_string("Creating nested atomic goal\n",!IO)
         ),
         create_nested_goal(InitInstmap, FinalInstmap,
@@ -576,7 +572,6 @@
 common_goal_vars_from_list(GoalList, GoalVar) :-
     ExtractInputSet = (pred(AGV::in, Input::out) is det :-
         Input = AGV ^ vars_input),
-
     list.map(ExtractInputSet, GoalList, InputVarList),
     InputVars = set_of_var.union_list(InputVarList),
     GoalVar0 = list.det_head(GoalList),
@@ -594,26 +589,26 @@
     hlds_goals::in, list(prog_var)::in, list(prog_var)::in, list(prog_var)::in,
     list(stm_goal_vars)::out, stm_info::in, stm_info::out) is det.
 
-calc_pred_variables_list(InitInstmap, FinalInstmap, HldsGoals,
-        InnerDIs, InnerUOs, IgnoreVarList0, StmGoalVarList,
-        !StmInfo) :-
+calc_pred_variables_list(InitInstmap, FinalInstmap, Goals, InnerDIs, InnerUOs,
+        IgnoreVarList0, StmGoalVarList, !StmInfo) :-
     (
-        HldsGoals = [],
+        Goals = [],
         InnerDIs = [],
         InnerUOs = []
     ->
         StmGoalVarList = []
     ;
-        HldsGoals = [HldsGoal | HldsGoals0],
-        InnerDIs = [InnerDI | InnerDIs0],
-        InnerUOs = [InnerUO | InnerUOs0]
+        Goals = [HeadGoal | TailGoals],
+        InnerDIs = [HeadInnerDI | TailInnerDIs],
+        InnerUOs = [HeadInnerUO | TailInnerUOs]
     ->
-        IgnoreVarList = [InnerDI, InnerUO | IgnoreVarList0],
+        IgnoreVarList = [HeadInnerDI, HeadInnerUO | IgnoreVarList0],
 
-        calc_pred_variables(InitInstmap, FinalInstmap, HldsGoal, InnerDI,
-            InnerUO, IgnoreVarList, StmGoalVar, !StmInfo),
-        calc_pred_variables_list(InitInstmap, FinalInstmap, HldsGoals0,
-            InnerDIs0, InnerUOs0, IgnoreVarList, StmGoalVarList0, !StmInfo),
+        calc_pred_variables(InitInstmap, FinalInstmap, HeadGoal, HeadInnerDI,
+            HeadInnerUO, IgnoreVarList, StmGoalVar, !StmInfo),
+        calc_pred_variables_list(InitInstmap, FinalInstmap, TailGoals,
+            TailInnerDIs, TailInnerUOs, IgnoreVarList, StmGoalVarList0,
+            !StmInfo),
         StmGoalVarList = [StmGoalVar | StmGoalVarList0]
     ;
         unexpected($module, $pred, "lengths mismatch")
@@ -627,15 +622,15 @@
     hlds_goal::in, prog_var::in, prog_var::in, list(prog_var)::in,
     stm_goal_vars::out, stm_info::in, stm_info::out) is det.
 
-calc_pred_variables(InitInstmap, FinalInstmap, HldsGoal,
+calc_pred_variables(InitInstmap, FinalInstmap, Goal,
         InnerDI, InnerUO, IgnoreVarList, StmGoalVars, !StmInfo) :-
     ModuleInfo = !.StmInfo ^ stm_info_module_info,
 
-    goal_vars(HldsGoal, GoalVars0),
+    goal_vars(Goal, GoalVars0),
     set_of_var.delete_list(IgnoreVarList, GoalVars0, GoalVars),
     GoalVarList = set_of_var.to_sorted_list(GoalVars),
 
-    HldsGoal = hlds_goal(_, GoalInfo),
+    Goal = hlds_goal(_, GoalInfo),
     GoalNonLocalSet0 = goal_info_get_nonlocals(GoalInfo),
     set_of_var.delete_list(IgnoreVarList, GoalNonLocalSet0, GoalNonLocalSet),
     GoalNonLocals = set_of_var.to_sorted_list(GoalNonLocalSet),
@@ -665,8 +660,7 @@
 	pair(maybe(prog_var), maybe(prog_var))::out) is det.
 
 remove_tail([], [], no - no, no - no).
-remove_tail([G | Gs], Goals, MaybeOutDI - MaybeOutUO,
-		MaybeInDI - MaybeInUO) :-
+remove_tail([G | Gs], Goals, MaybeOutDI - MaybeOutUO, MaybeInDI - MaybeInUO) :-
     remove_tail(Gs, Goals0, MaybeOutDI0 - MaybeOutUO0,
         MaybeInDI0 - MaybeInUO0),
     ( G = hlds_goal(plain_call(_, _, [_, X, V], _, _, stm_outer_inner), _) ->
@@ -743,7 +737,7 @@
     stm_info::in, stm_info::out) is det.
 
 create_nested_goal(InitInstmap, FinalInstmap, OuterDI, OuterUO,
-        _InnerDI, _InnerUO, AtomicGoal0, OrElseGoals0, HldsGoal, !StmInfo) :-
+        _InnerDI, _InnerUO, AtomicGoal0, OrElseGoals0, Goal, !StmInfo) :-
     strip_goal_calls(AtomicGoal0, AtomicGoal, MainOuterDI, MainOuterUO,
         MainInnerDI, MainInnerUO),
     list.map5(strip_goal_calls, OrElseGoals0, OrElseGoals, _, _,
@@ -756,7 +750,7 @@
             uo_mode - di_mode, CopyDIVars, !StmInfo),
         create_var_unify_stm(MainOuterUO, MainInnerUO,
             uo_mode - di_mode, CopyUOVars, !StmInfo),
-        create_plain_conj([CopyDIVars, AtomicGoal, CopyUOVars], HldsGoal)
+        create_plain_conj([CopyDIVars, AtomicGoal, CopyUOVars], Goal)
     ;
         OrElseGoals = [_ | _],
 
@@ -772,7 +766,7 @@
 %           AtomicGoalVarList1),
         AtomicGoalVarList1 = AtomicGoalVarList,
 
-        trace [io(!IO)] (
+        trace [compiletime(flag("debug_stm")), io(!IO)] (
             io.write_string("Local: " ++
                 string(AtomicGoalVars ^ vars_local) ++ "\n", !IO),
             io.write_string("Inner: " ++
@@ -785,12 +779,12 @@
         make_return_type(OutputTypes, ResultType),
         create_aux_variable_stm(ResultType, yes("res"), ResultVar, !StmInfo),
         CreateWrapperForEachGoal =
-            ( pred(Goal::in, GoalVars::in, PPID::out, SInfo0::in,
+            ( pred(ThisGoal::in, GoalVars::in, PPID::out, SInfo0::in,
                     SInfo::out) is det :-
                 % These predicates should be plain predicates without code to
                 % validate logs.
                 create_simple_wrapper_pred(GoalVars, ResultType, ResultVar,
-                    Goal, PPID, _, SInfo0, SInfo)
+                    ThisGoal, PPID, _, SInfo0, SInfo)
             ),
         map2_in_foldl(CreateWrapperForEachGoal, GoalList, AtomicGoalVarList1,
             PPIDList, !StmInfo),
@@ -801,7 +795,7 @@
             uo_mode - di_mode, CopyDIVars, !StmInfo),
         create_var_unify_stm(MainOuterUO, MainInnerUO,
             uo_mode - di_mode, CopyUOVars, !StmInfo),
-        create_plain_conj([CopyDIVars, OrElseCall, CopyUOVars], HldsGoal)
+        create_plain_conj([CopyDIVars, OrElseCall, CopyUOVars], Goal)
     ).
 
     % Creates the top level predicate and returns a call to that predicate
@@ -812,7 +806,7 @@
     stm_info::in, stm_info::out) is det.
 
 create_top_level_goal(InitInstmap, FinalInstmap, OuterDI, OuterUO,
-        _InnerDI, _InnerUO, AtomicGoal0, OrElseGoals0, HldsGoal, !StmInfo) :-
+        _InnerDI, _InnerUO, AtomicGoal0, OrElseGoals0, Goal, !StmInfo) :-
     strip_goal_calls(AtomicGoal0, AtomicGoal, _, _, MainInnerDI, MainInnerUO),
     list.map5(strip_goal_calls, OrElseGoals0, OrElseGoals, _, _,
         OrElseInnerDIs, OrElseInnerUOs),
@@ -826,8 +820,7 @@
         AtomicGoalVarList, !StmInfo),
 
     create_top_level_pred(AtomicGoalVarList, OuterDI, OuterUO, AtomicGoal,
-        OrElseGoals, TopLevelCall, !StmInfo),
-    HldsGoal = TopLevelCall.
+        OrElseGoals, Goal, !StmInfo).
 
     % Creates the top level predicate.  Calling this implicitly creates the
     % rollback and wrapper predicate.
@@ -837,7 +830,7 @@
     stm_info::in, stm_info::out) is det.
 
 create_top_level_pred(AtomicGoalVarList, OuterDI, OuterUO, AtomicGoal,
-        OrElseGoals, HldsGoal, !StmInfo) :-
+        OrElseGoals, Goal, !StmInfo) :-
     create_rollback_pred(AtomicGoalVarList, WrapperCall, AtomicGoal,
         OrElseGoals, !StmInfo),
 
@@ -849,7 +842,7 @@
     create_cloned_pred(InputVars ++ OutputVars ++ [OuterDI, OuterUO],
         InputTypes ++ OutputTypes ++ [io_io_type, io_io_type],
         InputModes ++ OutputModes ++ [di_mode, uo_mode], "toplevel",
-        AtomicGoal, no, NewPredInfo0, HldsGoal, !StmInfo),
+        AtomicGoal, no, NewPredInfo0, Goal, !StmInfo),
 
     create_var_unify(OuterUO, OuterDI, uo_mode - di_mode,
         CopyIOAssign, NewPredInfo0, NewPredInfo1),
@@ -883,7 +876,7 @@
     stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
 template_if_exceptres_is_cons(RttiVar, ExceptVar, RollbackExceptCons,
-        TrueGoal, FalseGoal, HldsGoal, !NewPredInfo) :-
+        TrueGoal, FalseGoal, Goal, !NewPredInfo) :-
     create_aux_variable(stm_rollback_exception_type, yes("UnivPayload"),
         UnivPayloadVar, !NewPredInfo),
     create_aux_variable_assignment(RollbackExceptCons,
@@ -909,7 +902,7 @@
     ITEPurity = purity_impure,
 
     create_if_then_else([], CondGoal, TrueGoal, FalseGoal, ITEDetermism,
-        ITEPurity, HldsGoal, !NewPredInfo).
+        ITEPurity, Goal, !NewPredInfo).
 
     % Predicate that creates the following goals.
     %
@@ -932,7 +925,7 @@
     stm_new_pred_info::out) is det.
 
 template_lock_and_validate(StmVar, UnlockAfterwards, ValidGoal, InvalidGoal,
-        HldsGoals, !NewPredInfo) :-
+        Goals, !NewPredInfo) :-
     create_aux_variable(stm_valid_result_type, yes("ValidResult"),
         IsValidVar, !NewPredInfo),
     create_simple_call(mercury_stm_builtin_module, "stm_lock", pf_predicate,
@@ -953,21 +946,21 @@
         create_simple_call(mercury_stm_builtin_module, "stm_unlock",
             pf_predicate, only_mode, detism_det, purity_impure, [], [],
             instmap_delta_bind_no_var, UnlockCall, !NewPredInfo),
-        HldsGoals = [LockCall, ValidCall, UnlockCall, DisjGoal]
+        Goals = [LockCall, ValidCall, UnlockCall, DisjGoal]
     ;
         UnlockAfterwards = no,
-        HldsGoals = [LockCall, ValidCall, DisjGoal]
+        Goals = [LockCall, ValidCall, DisjGoal]
     ).
 
     % Lock and validate a number of transactions.  The success branch will
-    % be passed if all transactions are valid,.
+    % be passed if all transactions are valid.
     %
 :- pred template_lock_and_validate_many(list(prog_var)::in, bool::in,
     hlds_goal::in, hlds_goal::in, hlds_goals::out, stm_new_pred_info::in,
     stm_new_pred_info::out) is det.
 
 template_lock_and_validate_many(StmVars, UnlockAfterwards, ValidGoal,
-        InvalidGoal, HldsGoals, !NewPredInfo) :-
+        InvalidGoal, Goals, !NewPredInfo) :-
     create_aux_variable_assignment(stm_validres_valid_functor,
         stm_valid_result_type, yes("IsValidConst"), AssignValidConst,
         IsValidConstVar, !NewPredInfo),
@@ -1010,11 +1003,11 @@
         create_simple_call(mercury_stm_builtin_module, "stm_unlock",
             pf_predicate, only_mode, detism_det, purity_impure, [], [],
             instmap_delta_bind_no_var, UnlockCall, !NewPredInfo),
-        HldsGoals = [AssignValidConst, LockCall] ++ ValidCalls ++
+        Goals = [AssignValidConst, LockCall | ValidCalls] ++
             [UnlockCall, ITEGoal]
     ;
         UnlockAfterwards = no,
-        HldsGoals = [AssignValidConst, LockCall] ++ ValidCalls ++ [ITEGoal]
+        Goals = [AssignValidConst, LockCall | ValidCalls] ++ [ITEGoal]
     ).
 
 %-----------------------------------------------------------------------------%
@@ -1048,23 +1041,23 @@
     stm_new_pred_info::out) is det.
 
 create_validate_exception_goal(StmVar, ExceptionVar, ReturnType, RecursiveCall,
-        HldsGoal, !NewPredInfo) :-
+        Goal, !NewPredInfo) :-
     make_type_info(ReturnType, TypeInfoVar, CreateTypeInfoGoals, !NewPredInfo),
     create_simple_call(mercury_exception_module, "rethrow", pf_predicate,
         only_mode, detism_erroneous, purity_pure, [TypeInfoVar, ExceptionVar],
         [], instmap_delta_bind_vars([TypeInfoVar, ExceptionVar]),
-        HldsGoal_ExceptionThrow_Call, !NewPredInfo),
-    create_plain_conj(CreateTypeInfoGoals ++ [HldsGoal_ExceptionThrow_Call],
-        HldsGoal_ValidBranch),
+        Goal_ExceptionThrow_Call, !NewPredInfo),
+    create_plain_conj(CreateTypeInfoGoals ++ [Goal_ExceptionThrow_Call],
+        Goal_ValidBranch),
     create_simple_call(mercury_stm_builtin_module,
         "stm_discard_transaction_log",
         pf_predicate, only_mode, detism_det, purity_impure, [StmVar], [],
         instmap_delta_from_assoc_list([StmVar - ground(clobbered, none)]),
         DropStateCall, !NewPredInfo),
-    create_plain_conj([DropStateCall, RecursiveCall], HldsGoal_InvalidBranch),
-    template_lock_and_validate(StmVar, yes, HldsGoal_ValidBranch,
-        HldsGoal_InvalidBranch, HldsGoals, !NewPredInfo),
-    create_plain_conj(HldsGoals, HldsGoal).
+    create_plain_conj([DropStateCall, RecursiveCall], Goal_InvalidBranch),
+    template_lock_and_validate(StmVar, yes, Goal_ValidBranch,
+        Goal_InvalidBranch, Goals, !NewPredInfo),
+    create_plain_conj(Goals, Goal).
 
     % Creates the necessary goals for handling explicit retries.  The role
     % of these goals is to validate the log and block the thread if the
@@ -1074,7 +1067,7 @@
 :- pred create_retry_handler_branch(prog_var::in, hlds_goal::in,
     hlds_goal::out, stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
-create_retry_handler_branch(StmVar, RecCall, HldsGoal, !NewPredInfo) :-
+create_retry_handler_branch(StmVar, RecCall, Goal, !NewPredInfo) :-
     create_simple_call(mercury_stm_builtin_module, "stm_block", pf_predicate,
         only_mode, detism_det, purity_impure, [StmVar], [],
         instmap_delta_bind_var(StmVar), BlockGoal, !NewPredInfo),
@@ -1089,7 +1082,7 @@
         instmap_delta_from_assoc_list([StmVar - ground(clobbered, none)]),
         DropStateCall, !NewPredInfo),
     create_plain_conj(LockAndValidateGoals ++ [DropStateCall, RecCall],
-        HldsGoal).
+        Goal).
 
     % Creates the necessary goals for switching on an exception.  The role of
     % the created goals is to extract the exception from the exception result
@@ -1100,7 +1093,7 @@
     hlds_goal::in, hlds_goal::out, stm_new_pred_info::in,
     stm_new_pred_info::out) is det.
 
-create_test_on_exception(ExceptVar, StmVar, ReturnType, RecCall, HldsGoal,
+create_test_on_exception(ExceptVar, StmVar, ReturnType, RecCall, Goal,
         !NewPredInfo) :-
     create_aux_variable(univ_type, yes("ExceptUnivVar"), ExceptUnivVar,
         !NewPredInfo),
@@ -1127,12 +1120,12 @@
         stm_rollback_exception_functor, TrueGoal, FalseGoal, IfThenElseGoal,
         !NewPredInfo),
     create_plain_conj([DeconstructException] ++ TypeInfoRollbackAssign ++
-        [IfThenElseGoal], HldsGoal).
+        [IfThenElseGoal], Goal).
 
     % Creates the main goal for the rollback predicate.  The goals created
     % by this predicate create the closure for the wrapper predicate and
-    % deconstructs the value returned if no exception is present.  It
-    % relies on the above predicates to generate code for handling exceptions.
+    % deconstructs the value returned if no exception is present. It relies
+    % on the above predicates to generate code for handling exceptions.
     %
 :- pred create_rollback_handler_goal(stm_goal_vars::in, mer_type::in,
     prog_var::in, prog_var::in, pred_proc_id::in, hlds_goal::in,
@@ -1140,7 +1133,7 @@
     stm_new_pred_info::out) is det.
 
 create_rollback_handler_goal(AtomicGoalVars, ReturnType, StmVarDI, StmVarUO,
-        WrapperID, RecCall, HldsGoal, StmInfo, !NewPredInfo) :-
+        WrapperID, RecCall, Goal, StmInfo, !NewPredInfo) :-
     get_input_output_varlist(AtomicGoalVars, InputVars, _),
     get_input_output_types(AtomicGoalVars, StmInfo, InputTypes, _),
     get_input_output_modes(AtomicGoalVars, InputModes, _),
@@ -1166,7 +1159,7 @@
         "stm_create_transaction_log",
         pf_predicate, only_mode, detism_det, purity_impure, [StmVarDI], [],
         instmap_delta_from_assoc_list([StmVarDI - ground(unique, none)]),
-        HldsGoal_StmCreate, !NewPredInfo),
+        Goal_StmCreate, !NewPredInfo),
 
     % TODO: Select mode based on determism of actual goal.  0 if determistic,
     % 1 if cc_multi.
@@ -1181,7 +1174,7 @@
             ReturnExceptVar - ground(shared, none),
             StmVarDI - ground(clobbered, none),
             StmVarUO - ground(unique, none)]),
-        HldsGoal_TryStm, !NewPredInfo),
+        Goal_TryStm, !NewPredInfo),
 
     % For successfull execution, deconstruct and return true
     deconstruct_output(AtomicGoalVars, ReturnType, ReturnExceptVar,
@@ -1194,9 +1187,9 @@
         case(ExceptRes_Success_Functor, [], Branch_AtomicSuccess)],
         detism_det, purity_impure, DisjGoal, !NewPredInfo),
 
-    create_plain_conj([HldsGoal_StmCreate] ++ RttiTypeVarAssign ++
-        [ClosureAssign, HldsGoal_TryStm, DisjGoal], HldsGoal0),
-    create_promise_purity_scope(HldsGoal0, purity_pure, HldsGoal).
+    create_plain_conj([Goal_StmCreate | RttiTypeVarAssign] ++
+        [ClosureAssign, Goal_TryStm, DisjGoal], Goal0),
+    create_promise_purity_scope(Goal0, purity_pure, Goal).
 
     % Creates the rollback predicate.  This predicate is responsible for
     % making the closure to the wrapper predicate and executing it whilst
@@ -1267,11 +1260,11 @@
 
 apply_varset_to_preds(ProgVar, !NewPredVarSet, !NewPredVarTypes,
         !OldPredVarSet, !OldPredVarTypes, !VarMapping) :-
-    map.lookup(!.OldPredVarTypes, ProgVar, ProgType),
+    lookup_var_type(!.OldPredVarTypes, ProgVar, ProgType),
 %   delete_var(!.OldPredVarSet, ProgVar, !:OldPredVarSet),
 %   map.delete(!.OldPredVarTypes, ProgVar, !:OldPredVarTypes),
     varset.new_var(NewProgVar, !NewPredVarSet),
-    map.det_insert(NewProgVar, ProgType, !NewPredVarTypes),
+    add_var_type(NewProgVar, ProgType, !NewPredVarTypes),
     map.det_insert(ProgVar, NewProgVar, !VarMapping).
 
     % Moves all local variables from the original predicate to the newly
@@ -1421,10 +1414,9 @@
     commit_new_pred(!.NewPredInfo, !StmInfo).
 
     % Creates the goals for validating and committing (or raising a rollback
-    % exception) a transaction log.  These goals appear after the original
-    % goal.  If the value of CopySTM is "yes", a goal unifying the variable
-    % in StmDI and the variable in StmUO will be created before the log
-    % is validated.
+    % exception) a transaction log. These goals appear after the original goal.
+    % If the value of CopySTM is "yes", a goal unifying the variable in StmDI
+    % and the variable in StmUO will be created before the log is validated.
     %
 :- pred create_post_wrapper_goal(stm_goal_vars::in, hlds_goal::in,
     mer_type::in, prog_var::in, prog_var::in, prog_var::in, bool::in,
@@ -1432,7 +1424,7 @@
     stm_new_pred_info::out) is det.
 
 create_post_wrapper_goal(AtomicGoalVars, AtomicGoal, ResultType, ResultVar,
-        StmDI, StmUO, CopySTM, HldsGoal, StmInfo, !NewPredInfo) :-
+        StmDI, StmUO, CopySTM, Goal, StmInfo, !NewPredInfo) :-
     StmModuleName = mercury_stm_builtin_module,
     ExceptionModuleName = mercury_exception_module,
 
@@ -1452,19 +1444,19 @@
         !NewPredInfo),
     create_simple_call(StmModuleName, "stm_lock", pf_predicate, only_mode,
         detism_det, purity_impure, [], [], instmap_delta_bind_no_var,
-        HldsGoal_StmLock_Call, !NewPredInfo),
+        Goal_StmLock_Call, !NewPredInfo),
     create_simple_call(StmModuleName, "stm_unlock", pf_predicate, only_mode,
         detism_det, purity_impure, [], [], instmap_delta_bind_no_var,
-        HldsGoal_StmUnLock_Call, !NewPredInfo),
+        Goal_StmUnLock_Call, !NewPredInfo),
     create_simple_call(StmModuleName, "stm_validate", pf_predicate, only_mode,
         detism_det, purity_impure, [StmUO, IsValidVar], [],
         instmap_delta_from_assoc_list([StmUO - ground(unique, none),
             IsValidVar - ground(shared, none)]),
-        HldsGoal_StmValidate_Call, !NewPredInfo),
+        Goal_StmValidate_Call, !NewPredInfo),
     create_simple_call(StmModuleName, "stm_commit", pf_predicate, only_mode,
         detism_det, purity_impure, [StmUO], [],
         instmap_delta_from_assoc_list([StmUO - ground(unique, none)]),
-        HldsGoal_StmCommit_Call, !NewPredInfo),
+        Goal_StmCommit_Call, !NewPredInfo),
 
     make_type_info(stm_rollback_exception_type, TypeInfoVar,
         CreateTypeInfoGoals, !NewPredInfo),
@@ -1472,23 +1464,23 @@
     create_simple_call(ExceptionModuleName, "throw", pf_predicate, only_mode,
         detism_erroneous, purity_pure, [TypeInfoVar, RollbackVar], [],
         instmap_delta_bind_vars([TypeInfoVar, RollbackVar]),
-        HldsGoal_ExceptionThrow_Call, !NewPredInfo),
+        Goal_ExceptionThrow_Call, !NewPredInfo),
 
     % Creates the branch on the validation result of the log.
-    create_plain_conj([HldsGoal_StmCommit_Call, HldsGoal_StmUnLock_Call],
-        HldsGoal_ValidBranch),
-    create_plain_conj([HldsGoal_StmUnLock_Call, ConstRollbackGoal] ++
-        CreateTypeInfoGoals ++ [HldsGoal_ExceptionThrow_Call],
-        HldsGoal_InvalidBranch),
+    create_plain_conj([Goal_StmCommit_Call, Goal_StmUnLock_Call],
+        Goal_ValidBranch),
+    create_plain_conj([Goal_StmUnLock_Call, ConstRollbackGoal] ++
+        CreateTypeInfoGoals ++ [Goal_ExceptionThrow_Call],
+        Goal_InvalidBranch),
 
     create_switch_disjunction(IsValidVar,
-        [case(ValidTrueFunctor, [], HldsGoal_ValidBranch),
-         case(ValidFalseFunctor, [], HldsGoal_InvalidBranch)],
+        [case(ValidTrueFunctor, [], Goal_ValidBranch),
+         case(ValidFalseFunctor, [], Goal_InvalidBranch)],
         detism_det, purity_impure, DisjGoal, !NewPredInfo),
 
     % Creates the main validation and commission goal.
-    PostAtomicTopLevelList = [HldsGoal_StmLock_Call,
-        HldsGoal_StmValidate_Call, DisjGoal],
+    PostAtomicTopLevelList = [Goal_StmLock_Call,
+        Goal_StmValidate_Call, DisjGoal],
 
     create_plain_conj(PostAtomicTopLevelList, PostAtomicGoal0),
     create_promise_purity_scope(PostAtomicGoal0, purity_pure, PostAtomicGoal),
@@ -1507,7 +1499,7 @@
     ),
 
     flatten_conj(TopLevelGoalList0, TopLevelGoalList),
-    create_plain_conj(TopLevelGoalList, HldsGoal).
+    create_plain_conj(TopLevelGoalList, Goal).
 
     % Creates a simpler wrapper predicate for or_else branches.
     %
@@ -1571,14 +1563,13 @@
 :- pred create_probe_call(string::in, prog_var::in, hlds_goals::out,
         stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
-create_probe_call(_Name, _Var, HldsGoals, !NewPredInfo) :-
-    HldsGoals = [].
+create_probe_call(_Name, _Var, Goals, !NewPredInfo) :-
+    Goals = [].
 
     % Creates the goals for validating and committing (or raising a rollback
-    % exception) a transaction log.  These goals appear after the original
-    % goal.  If the value of CopySTM is "yes", a goal unifying the variable
-    % in StmDI and the variable in StmUO will be created before the log
-    % is validated.
+    % exception) a transaction log. These goals appear after the original goal.
+    % If the value of CopySTM is "yes", a goal unifying the variable in StmDI
+    % and the variable in StmUO will be created before the log is validated.
     %
 :- pred create_simple_post_wrapper_goal(stm_goal_vars::in, hlds_goal::in,
     mer_type::in, prog_var::in, prog_var::in, prog_var::in, bool::in,
@@ -1586,7 +1577,7 @@
     stm_new_pred_info::out) is det.
 
 create_simple_post_wrapper_goal(AtomicGoalVars, AtomicGoal, ResultType,
-        ResultVar, StmDI, StmUO, CopySTM, HldsGoal, StmInfo, !NewPredInfo) :-
+        ResultVar, StmDI, StmUO, CopySTM, Goal, StmInfo, !NewPredInfo) :-
     construct_output(AtomicGoalVars, ResultType, ResultVar, StmInfo,
         AssignResult, !NewPredInfo),
 
@@ -1604,7 +1595,7 @@
     ),
 
     flatten_conj(TopLevelGoalList0, TopLevelGoalList),
-    create_plain_conj(TopLevelGoalList, HldsGoal).
+    create_plain_conj(TopLevelGoalList, Goal).
 
 %-----------------------------------------------------------------------------%
 %
@@ -1684,13 +1675,13 @@
 
 create_or_else_branches(AtomicGoalVars, ReturnType, OuterStmDIVar,
         OuterStmUOVar, InnerSTMVars, RttiVar, RollbackExceptionRttiVar,
-        WrapperIDs, EndBranch, HldsGoal, StmInfo, !NewPredInfo) :-
+        WrapperIDs, EndBranch, Goal, StmInfo, !NewPredInfo) :-
     (
         InnerSTMVars = [],
         WrapperIDs = [],
         AtomicGoalVars = []
     ->
-        HldsGoal = EndBranch
+        Goal = EndBranch
     ;
         AtomicGoalVars = [AGV | AGVs],
         InnerSTMVars = [InnerVar | InnerSTMVars0],
@@ -1698,10 +1689,10 @@
     ->
         create_or_else_branches(AGVs, ReturnType, OuterStmDIVar,
             OuterStmUOVar, InnerSTMVars0, RttiVar, RollbackExceptionRttiVar,
-            WrapperIDs0, EndBranch, HldsGoal0, StmInfo, !NewPredInfo),
+            WrapperIDs0, EndBranch, Goal0, StmInfo, !NewPredInfo),
         create_or_else_branch(AGV, ReturnType, OuterStmDIVar,
             OuterStmUOVar, InnerVar, RttiVar, RollbackExceptionRttiVar,
-            WrapID, HldsGoal0, HldsGoal, StmInfo, !NewPredInfo)
+            WrapID, Goal0, Goal, StmInfo, !NewPredInfo)
     ;
         unexpected($module, $pred, "mismatched lists")
     ).
@@ -1787,11 +1778,12 @@
     stm_new_pred_info::out) is det.
 
 create_or_else_end_branch(StmVars, OuterSTMDI, OuterSTMUO, ExceptionRttiVar,
-        HldsGoal, !NewPredInfo) :-
-
-    MakeIntermediateStmVars = (pred(_::in, Var::out, NPI0::in, NPI::out)
-            is det:-
-        create_aux_variable(stm_state_type, yes("InterSTM"), Var, NPI0, NPI)),
+        Goal, !NewPredInfo) :-
+    MakeIntermediateStmVars =
+        (pred(_::in, Var::out, NPI0::in, NPI::out) is det:-
+            create_aux_variable(stm_state_type, yes("InterSTM"), Var,
+                NPI0, NPI)
+        ),
 
     % We don't actually need the list as it is simply used as a counter.
     StmVarsMinusHead = list.det_tail(StmVars),
@@ -1801,14 +1793,17 @@
     MergeStmVarsIn = [OuterSTMDI | IntermediateStmVars],
     MergeStmVarsOut = IntermediateStmVars ++ [OuterSTMUO],
 
-    MakeMergeGoals = (pred(StmVar::in, ThreadSTMDI::in, ThreadSTMUO::in,
-            Goal::out, NPI0::in, NPI::out) is det :-
-        create_simple_call(mercury_stm_builtin_module, "stm_merge_nested_logs",
+    MakeMergeGoals =
+        (pred(StmVar::in, ThreadSTMDI::in, ThreadSTMUO::in,
+                ThisGoal::out, NPI0::in, NPI::out) is det :-
+            create_simple_call(mercury_stm_builtin_module,
+                "stm_merge_nested_logs",
             pf_predicate, only_mode, detism_det, purity_impure,
             [StmVar, ThreadSTMDI, ThreadSTMUO], [],
             instmap_delta_from_assoc_list([StmVar - ground(unique, none),
                 ThreadSTMDI - free, ThreadSTMUO - ground(unique, none)]),
-            Goal, NPI0, NPI)),
+                ThisGoal, NPI0, NPI)
+        ),
 
     map3_in_foldl(MakeMergeGoals, StmVars, MergeStmVarsIn, MergeStmVarsOut,
         MergeGoals, !NewPredInfo),
@@ -1848,8 +1843,8 @@
         InvalidGoal),
 
     template_lock_and_validate_many(StmVars, no, ValidGoal, InvalidGoal,
-        HldsGoals, !NewPredInfo),
-    create_plain_conj(HldsGoals, HldsGoal).
+        Goals, !NewPredInfo),
+    create_plain_conj(Goals, Goal).
 
     % Variables are:
     %
@@ -1867,7 +1862,6 @@
     %   outside this predicate as it needs to be known to the validate & merge
     %   branch.
     %
-    %
 :- pred create_or_else_branch(stm_goal_vars::in, mer_type::in, prog_var::in,
     prog_var::in, prog_var::in, prog_var::in, prog_var::in,
     pred_proc_id::in, hlds_goal::in, hlds_goal::out, stm_info::in,
@@ -1875,7 +1869,7 @@
 
 create_or_else_branch(AtomicGoalVars, ReturnType, OuterStmDIVar,
         OuterStmUOVar, InnerSTMVar, RttiVar, RollbackExceptionRttiVar,
-        WrapperID, RetryBranch, HldsGoal, StmInfo, !NewPredInfo) :-
+        WrapperID, RetryBranch, Goal, StmInfo, !NewPredInfo) :-
     get_input_output_varlist(AtomicGoalVars, InputVars, _),
     get_input_output_types(AtomicGoalVars, StmInfo, InputTypes, _),
     get_input_output_modes(AtomicGoalVars, InputModes, _),
@@ -1955,7 +1949,7 @@
         detism_det, purity_impure, DisjGoal, !NewPredInfo),
 
     create_plain_conj([CreateNestedLogCall, ClosureAssign, TryStmCall,
-        DisjGoal], HldsGoal).
+        DisjGoal], Goal).
 
 %-----------------------------------------------------------------------------%
 %
@@ -1987,7 +1981,7 @@
     stm_new_pred_info::out) is det.
 
 deconstruct_output(AtomicGoalVars, ReturnType, ReturnExceptVar,
-        HldsGoal, StmInfo, !NewPredInfo) :-
+        Goal, StmInfo, !NewPredInfo) :-
     get_input_output_varlist(AtomicGoalVars, _, OutputVars),
     get_input_output_types(AtomicGoalVars, StmInfo, _, OutputTypes),
 
@@ -1996,32 +1990,29 @@
         % Extract the return type but do nothing with it.  For reasons that
         % I do not know, this is the bare minimum that is required without
         % causing an exception in a later stage.
-
-        create_aux_variable(ReturnType, yes("BoringResult"), SucessResultVar,
+        create_aux_variable(ReturnType, yes("BoringResult"), SuccessResultVar,
             !NewPredInfo),
         deconstruct_functor(ReturnExceptVar, exception_succeeded_functor,
-            [SucessResultVar], HldsGoal)
+            [SuccessResultVar], Goal)
     ;
         OutputTypes = [_],
         % Wrapper returns a single value -- Simply get the value from the
         % exception result and return.
-
         OutVar = list.det_head(OutputVars),
         deconstruct_functor(ReturnExceptVar, exception_succeeded_functor,
-            [OutVar], HldsGoal)
+            [OutVar], Goal)
     ;
         OutputTypes = [_, _ | _],
         % Wrapper returns a tuple.  Get the tuple result and return it.
-
         make_type_info(ReturnType, _, MakeType, !NewPredInfo),
-        create_aux_variable(ReturnType, yes("SucessResult"), SucessResultVar,
+        create_aux_variable(ReturnType, yes("SuccessResult"), SuccessResultVar,
             !NewPredInfo),
         deconstruct_functor(ReturnExceptVar, exception_succeeded_functor,
-            [SucessResultVar], DeconstructGoal),
-        deconstruct_tuple(SucessResultVar, OutputVars, UnifyOutputGoal),
+            [SuccessResultVar], DeconstructGoal),
+        deconstruct_tuple(SuccessResultVar, OutputVars, UnifyOutputGoal),
 
         create_plain_conj([DeconstructGoal, UnifyOutputGoal | MakeType],
-            HldsGoal)
+            Goal)
     ).
 
     % Creates the goals necessary for constructing the output variables
@@ -2033,36 +2024,30 @@
     stm_info::in, hlds_goals::out, stm_new_pred_info::in,
     stm_new_pred_info::out) is det.
 
-construct_output(AtomicGoalVars, ResultType, ResultVar, StmInfo, HldsGoals,
+construct_output(AtomicGoalVars, ResultType, ResultVar, StmInfo, Goals,
         !NewPredInfo) :-
     get_input_output_varlist(AtomicGoalVars, _, OutputVars),
     get_input_output_types(AtomicGoalVars, StmInfo, _, OutputTypes),
-
     (
         OutputTypes = [],
         % Since a value must be returned, simply return a value which will be
         % discarded.
-
-        create_const_assign(ResultVar, stm_dummy_output_functor, HldsGoal),
-        HldsGoals = [HldsGoal]
+        create_const_assign(ResultVar, stm_dummy_output_functor, Goal),
+        Goals = [Goal]
     ;
         OutputTypes = [_],
         % Wrapper returns a single value -- Simply get the value from the
         % exception result and return.
-
         OutVar = list.det_head(OutputVars),
-        create_var_unify(ResultVar, OutVar, out_mode - in_mode,
-            HldsGoal, !NewPredInfo),
-
-        HldsGoals = [HldsGoal]
+        create_var_unify(ResultVar, OutVar, out_mode - in_mode, Goal,
+            !NewPredInfo),
+        Goals = [Goal]
     ;
         OutputTypes = [_, _ | _],
         % Wrapper returns a tuple.  Creates a tuple from the output values.
-
         make_type_info(ResultType, _, MakeType, !NewPredInfo),
-        hlds_goal.construct_tuple(ResultVar, OutputVars, HldsGoal),
-
-        HldsGoals = [HldsGoal | MakeType]
+        hlds_goal.construct_tuple(ResultVar, OutputVars, Goal),
+        Goals = [Goal | MakeType]
     ).
 
     % Renames the value of a variable in a predicate.
@@ -2072,16 +2057,16 @@
     hlds_goal::in, hlds_goal::out) is det.
 
 rename_var_in_wrapper_pred(Name, ResultVar0, ResultType, ResultVar,
-        !NewPredInfo, !HldsGoal) :-
+        !NewPredInfo, !Goal) :-
     NewProcInfo0 = !.NewPredInfo ^ new_pred_proc_info,
     proc_info_get_varset(NewProcInfo0, NewPredVarSet0),
     proc_info_get_vartypes(NewProcInfo0, NewPredVarTypes0),
     proc_info_get_headvars(NewProcInfo0, NewHeadVars0),
     varset.delete_var(ResultVar0, NewPredVarSet0, NewPredVarSet1),
-    map.delete(ResultVar0, NewPredVarTypes0, NewPredVarTypes1),
+    delete_var_type(ResultVar0, NewPredVarTypes0, NewPredVarTypes1),
 
     varset.new_named_var(Name, ResultVar, NewPredVarSet1, NewPredVarSet),
-    map.det_insert(ResultVar, ResultType, NewPredVarTypes1, NewPredVarTypes),
+    add_var_type(ResultVar, ResultType, NewPredVarTypes1, NewPredVarTypes),
     VarMapping = map.singleton(ResultVar0, ResultVar),
 
     MapLambda = ((pred(X::in, Y::out) is det) :-
@@ -2093,7 +2078,7 @@
     ),
     list.map(MapLambda, NewHeadVars0, NewHeadVars),
 
-    rename_some_vars_in_goal(VarMapping, !HldsGoal),
+    rename_some_vars_in_goal(VarMapping, !Goal),
     proc_info_set_varset(NewPredVarSet, NewProcInfo0, NewProcInfo1),
     proc_info_set_vartypes(NewPredVarTypes, NewProcInfo1, NewProcInfo2),
     proc_info_set_headvars(NewHeadVars, NewProcInfo2, NewProcInfo),
@@ -2324,8 +2309,8 @@
 :- pred create_promise_purity_scope(hlds_goal::in, purity::in,
         hlds_goal::out) is det.
 
-create_promise_purity_scope(HldsGoalIn, ScopePurity, HldsGoalOut) :-
-    HldsGoalIn = hlds_goal(_, GoalInInfo),
+create_promise_purity_scope(GoalIn, ScopePurity, GoalOut) :-
+    GoalIn = hlds_goal(_, GoalInInfo),
     NonLocals = goal_info_get_nonlocals(GoalInInfo),
     InstMapDelta = goal_info_get_instmap_delta(GoalInInfo),
     Detism = goal_info_get_determinism(GoalInInfo),
@@ -2334,8 +2319,8 @@
     goal_info_init(NonLocals, InstMapDelta, Detism, GoalPurity, Context,
         GoalInfo),
     Reason = promise_purity(ScopePurity),
-    HldsGoalOutExpr = scope(Reason, HldsGoalIn),
-    HldsGoalOut = hlds_goal(HldsGoalOutExpr, GoalInfo).
+    GoalOutExpr = scope(Reason, GoalIn),
+    GoalOut = hlds_goal(GoalOutExpr, GoalInfo).
 
     % Creates a list of regular conjoined goals.
     %
@@ -2359,11 +2344,11 @@
 :- pred make_type_info(mer_type::in, prog_var::out,
     hlds_goals::out, stm_new_pred_info::in, stm_new_pred_info::out) is det.
 
-make_type_info(Type, Var, HldsGoals, NewPredInfo0, NewPredInfo) :-
+make_type_info(Type, Var, Goals, NewPredInfo0, NewPredInfo) :-
     NewPredInfo0 = stm_new_pred_info(ModuleInfo0, PredId, ProcId,
         PredInfo0, ProcInfo0, Context, VarCnt),
     create_poly_info(ModuleInfo0, PredInfo0, ProcInfo0, PolyInfo0),
-    polymorphism_make_type_info_var(Type, Context, Var, HldsGoals,
+    polymorphism_make_type_info_var(Type, Context, Var, Goals,
         PolyInfo0, PolyInfo),
     poly_info_extract(PolyInfo, PredInfo0, PredInfo, ProcInfo0, ProcInfo,
         ModuleInfo),
@@ -2531,18 +2516,18 @@
 :- pred new_pred_set_goal(hlds_goal::in, stm_new_pred_info::in,
     stm_new_pred_info::out) is det.
 
-new_pred_set_goal(HldsGoal, !NewPredInfo) :-
+new_pred_set_goal(Goal, !NewPredInfo) :-
     ProcInfo0 = !.NewPredInfo ^ new_pred_proc_info,
-    goal_vars(HldsGoal, GoalVars),
+    goal_vars(Goal, GoalVars),
     GoalVarsSet = set_of_var.bitset_to_set(GoalVars),
     proc_info_get_varset(ProcInfo0, ProcVarSet0),
     proc_info_get_vartypes(ProcInfo0, ProcVarTypes0),
 
     varset.select(GoalVarsSet, ProcVarSet0, ProgVarSet),
-    map.select(ProcVarTypes0, GoalVarsSet, ProcVarTypes),
+    vartypes_select(GoalVarsSet, ProcVarTypes0, ProcVarTypes),
 
     proc_info_set_varset(ProgVarSet, ProcInfo0, ProcInfo1),
-    proc_info_set_goal(HldsGoal, ProcInfo1, ProcInfo2),
+    proc_info_set_goal(Goal, ProcInfo1, ProcInfo2),
     proc_info_set_vartypes(ProcVarTypes, ProcInfo2, ProcInfo),
     !NewPredInfo ^ new_pred_proc_info := ProcInfo.
 
@@ -2583,8 +2568,8 @@
     proc_info_get_vartypes(ProcInfo0, VarTypes),
     get_input_output_varlist(StmGoalVars, InputVars, OutputVars),
 
-    list.map(map.lookup(VarTypes), InputVars, InputTypes),
-    list.map(map.lookup(VarTypes), OutputVars, OutputTypes).
+    lookup_var_types(VarTypes, InputVars, InputTypes),
+    lookup_var_types(VarTypes, OutputVars, OutputTypes).
 
     % Used by "get_input_output_modes".
     %
Index: compiler/store_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/store_alloc.m,v
retrieving revision 1.120
diff -u -b -r1.120 store_alloc.m
--- compiler/store_alloc.m	13 Feb 2012 00:11:49 -0000	1.120
+++ compiler/store_alloc.m	2 Jul 2012 01:07:01 -0000
@@ -495,7 +495,7 @@
         RegType = reg_r
     ;
         FloatRegType = reg_f,
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         ( VarType = float_type ->
             RegType = reg_f
         ;
Index: compiler/structure_reuse.analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.analysis.m,v
retrieving revision 1.31
diff -u -b -r1.31 structure_reuse.analysis.m
--- compiler/structure_reuse.analysis.m	16 Aug 2011 03:26:33 -0000	1.31
+++ compiler/structure_reuse.analysis.m	2 Jul 2012 01:07:01 -0000
@@ -831,7 +831,7 @@
             proc_info_get_varset(ProcInfo, VarSet),
             proc_info_get_headvars(ProcInfo, HeadVars),
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            list.map(map.lookup(VarTypes), HeadVars, HeadVarTypes),
+            lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
                 MaybeReuse = yes(Reuse),
             write_pragma_structure_reuse_info(PredOrFunc, SymName, Modes,
                 Context, HeadVars, yes(VarSet), HeadVarTypes, yes(TypeVarSet),
@@ -943,7 +943,7 @@
             FuncInfo = structure_reuse_func_info(ModuleInfo, ProcInfo),
             proc_info_get_headvars(ProcInfo, HeadVars),
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+            lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
             structure_reuse_answer_to_domain(HeadVarTypes, ProcInfo, Answer1,
                 Reuse1),
             structure_reuse_answer_to_domain(HeadVarTypes, ProcInfo, Answer2,
@@ -964,7 +964,7 @@
             FuncInfo = structure_reuse_func_info(ModuleInfo, ProcInfo),
             proc_info_get_headvars(ProcInfo, HeadVars),
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+            lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
             structure_reuse_answer_to_domain(HeadVarTypes, ProcInfo, Answer1,
                 Reuse1),
             structure_reuse_answer_to_domain(HeadVarTypes, ProcInfo, Answer2,
@@ -1082,7 +1082,7 @@
         module_info_proc_info(ModuleInfo, PPId, ProcInfo),
         proc_info_get_headvars(ProcInfo, HeadVars),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+        lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
         Answer = structure_reuse_answer_conditional(HeadVars, HeadVarTypes,
             Conditions)
     ).
Index: compiler/structure_reuse.direct.choose_reuse.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.direct.choose_reuse.m,v
retrieving revision 1.34
diff -u -b -r1.34 structure_reuse.direct.choose_reuse.m
--- compiler/structure_reuse.direct.choose_reuse.m	13 Feb 2012 00:11:49 -0000	1.34
+++ compiler/structure_reuse.direct.choose_reuse.m	2 Jul 2012 01:07:01 -0000
@@ -814,7 +814,7 @@
         (
             Unification = construct(Var, Cons, Args, _, _, _, _),
             (
-                map.lookup(Background ^ back_vartypes, Var, VarType),
+                lookup_var_type(Background ^ back_vartypes, Var, VarType),
                 top_cell_may_be_reusable(Background ^ back_module_info,
                     VarType),
 
@@ -1090,7 +1090,7 @@
 
 has_secondary_tag(ModuleInfo, VarTypes, Var, ConsId, SecondaryTag) :-
     (
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         type_to_type_defn_body(ModuleInfo, Type, TypeBody),
         TypeBody = hlds_du_type(_, ConsTagValues, _, _, _, _, _, _, _),
         map.search(ConsTagValues, ConsId, ConsTag),
Index: compiler/structure_reuse.direct.detect_garbage.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.direct.detect_garbage.m,v
retrieving revision 1.28
diff -u -b -r1.28 structure_reuse.direct.detect_garbage.m
--- compiler/structure_reuse.direct.detect_garbage.m	13 Feb 2012 00:11:49 -0000	1.28
+++ compiler/structure_reuse.direct.detect_garbage.m	2 Jul 2012 01:07:01 -0000
@@ -219,7 +219,7 @@
         ; GenDetails = class_method(_, _, _, _)
         ),
         proc_info_get_vartypes(ProcInfo, CallerVarTypes),
-        map.apply_to_list(CallArgs, CallerVarTypes, ActualTypes),
+        lookup_var_types(CallerVarTypes, CallArgs, ActualTypes),
         (
             bottom_sharing_is_safe_approximation_by_args(ModuleInfo, Modes,
                 ActualTypes)
@@ -269,7 +269,7 @@
 
             % No-tag values don't have a cell to reuse.
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.lookup(VarTypes, Var, Type),
+            lookup_var_type(VarTypes, Var, Type),
             \+ type_is_no_tag_type(ModuleInfo, Type),
 
             % Check if the top cell datastructure of Var is not live.
Index: compiler/structure_reuse.indirect.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_reuse.indirect.m,v
retrieving revision 1.40
diff -u -b -r1.40 structure_reuse.indirect.m
--- compiler/structure_reuse.indirect.m	13 Feb 2012 00:11:49 -0000	1.40
+++ compiler/structure_reuse.indirect.m	2 Jul 2012 01:07:01 -0000
@@ -642,7 +642,7 @@
         ; GenDetails = class_method(_, _, _, _)
         ),
         proc_info_get_vartypes(ProcInfo, CallerVarTypes),
-        map.apply_to_list(CallArgs, CallerVarTypes, ActualTypes),
+        lookup_var_types(CallerVarTypes, CallArgs, ActualTypes),
         (
             bottom_sharing_is_safe_approximation_by_args(ModuleInfo, Modes,
                 ActualTypes)
@@ -908,7 +908,7 @@
     proc_info_get_vartypes(ProcInfo, ActualVarTypes),
     pred_info_get_typevarset(PredInfo, CallerTypeVarSet),
     pred_info_get_univ_quant_tvars(PredInfo, CallerHeadTypeParams),
-    map.apply_to_list(CalleeArgs, ActualVarTypes, CalleeTypes),
+    lookup_var_types(ActualVarTypes, CalleeArgs, CalleeTypes),
     reuse_as_rename_using_module_info(ModuleInfo, CalleePPId,
         CalleeArgs, CalleeTypes, CallerTypeVarSet, CallerHeadTypeParams,
         FormalReuseAs, ActualReuseAs),
Index: compiler/structure_sharing.analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_sharing.analysis.m,v
retrieving revision 1.53
diff -u -b -r1.53 structure_sharing.analysis.m
--- compiler/structure_sharing.analysis.m	13 Feb 2012 00:11:49 -0000	1.53
+++ compiler/structure_sharing.analysis.m	2 Jul 2012 01:07:01 -0000
@@ -595,7 +595,7 @@
 
         % Rename
         proc_info_get_vartypes(ProcInfo, CallerVarTypes),
-        map.apply_to_list(CallArgs, CallerVarTypes, ActualTypes),
+        lookup_var_types(CallerVarTypes, CallArgs, ActualTypes),
         pred_info_get_typevarset(PredInfo, CallerTypeVarSet),
         pred_info_get_univ_quant_tvars(PredInfo, CallerHeadParams),
         sharing_as_rename_using_module_info(ModuleInfo, CalleePPId, CallArgs,
@@ -763,7 +763,7 @@
         ; GenDetails = class_method(_, _, _, _)
         ),
         proc_info_get_vartypes(ProcInfo, CallerVarTypes),
-        map.apply_to_list(CallArgs, CallerVarTypes, ActualTypes),
+        lookup_var_types(CallerVarTypes, CallArgs, ActualTypes),
         (
             bottom_sharing_is_safe_approximation_by_args(ModuleInfo, Modes,
                 ActualTypes)
@@ -981,7 +981,7 @@
         proc_info_get_varset(ProcInfo, VarSet),
         proc_info_get_headvars(ProcInfo, HeadVars),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+        lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
         (
             MaybeSharingStatus = yes(
                 structure_sharing_domain_and_status(Sharing, _Status)),
@@ -1075,7 +1075,7 @@
         FuncInfo = structure_sharing_func_info(ModuleInfo, ProcInfo),
         proc_info_get_headvars(ProcInfo, HeadVars),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+        lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
         structure_sharing_answer_to_domain(no, HeadVarTypes, ProcInfo,
             Answer1, Sharing1),
         structure_sharing_answer_to_domain(no, HeadVarTypes, ProcInfo,
@@ -1096,7 +1096,7 @@
             FuncInfo = structure_sharing_func_info(ModuleInfo, ProcInfo),
             proc_info_get_headvars(ProcInfo, HeadVars),
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+            lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
             structure_sharing_answer_to_domain(no, HeadVarTypes, ProcInfo,
                 Answer1, Sharing1),
             structure_sharing_answer_to_domain(no, HeadVarTypes, ProcInfo,
@@ -1235,7 +1235,7 @@
             Sharing = structure_sharing_real(SharingPairs),
             proc_info_get_headvars(ProcInfo, HeadVars),
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.apply_to_list(HeadVars, VarTypes, HeadVarTypes),
+            lookup_var_types(VarTypes, HeadVars, HeadVarTypes),
             Answer = structure_sharing_answer_real(HeadVars, HeadVarTypes,
                 SharingPairs),
             Status = Status0
Index: compiler/structure_sharing.domain.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_sharing.domain.m,v
retrieving revision 1.47
diff -u -b -r1.47 structure_sharing.domain.m
--- compiler/structure_sharing.domain.m	21 Jul 2011 06:58:27 -0000	1.47
+++ compiler/structure_sharing.domain.m	2 Jul 2012 01:07:01 -0000
@@ -534,7 +534,7 @@
 
 is_introduced_typeinfo_arg(ProcInfo, Var) :-
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     is_introduced_type_info_type(Type).
 
 :- pred number_args(prog_vars::in, list(pair(int, prog_var))::out) is det.
@@ -632,7 +632,7 @@
 
     ActualVars = list.map(foreign_arg_var, Args),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(ActualVars, VarTypes, ActualTypes),
+    lookup_var_types(VarTypes, ActualVars, ActualTypes),
     pred_info_get_typevarset(PredInfo, CallerTypeVarSet),
     pred_info_get_head_type_params(PredInfo, CallerHeadTypeParams),
 
@@ -848,7 +848,7 @@
         _Status, _IsPredicted),
 
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(ActualVars, VarTypes, ActualTypes),
+    lookup_var_types(VarTypes, ActualVars, ActualTypes),
 
     pred_info_get_typevarset(PredInfo, CallerTypeVarSet),
     pred_info_get_univ_quant_tvars(PredInfo, CallerHeadTypeParams),
@@ -972,7 +972,7 @@
         proc_info_get_headvars(ProcInfo, HeadVars),
         proc_info_get_argmodes(ProcInfo, Modes),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.apply_to_list(HeadVars, VarTypes, Types),
+        lookup_var_types(VarTypes, HeadVars, Types),
         bottom_sharing_is_safe_approximation_by_args(ModuleInfo, Modes, Types)
     ).
 
@@ -1246,7 +1246,7 @@
     %
     list.foldl(
         (pred(Var::in, !.SS::in, !:SS::out) is det :-
-            map.lookup(VarTypes, Var, Type),
+            lookup_var_type(VarTypes, Var, Type),
             map.lookup(NewMap1, Var, NewSelSet),
             map.lookup(OldMap1, Var, OldSelSet),
             SharingPairs = selector_sharing_set_altclos(ModuleInfo, ProcInfo,
@@ -1349,9 +1349,8 @@
     ( map.search(SharingMap, Var, SelectorSet) ->
         % The type of the variable is needed to be able to compare
         % datastructures.
-        %
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         Datastructures = selector_sharing_set_extend_datastruct(ModuleInfo,
             ProcInfo, VarType, Selector, SelectorSet)
     ;
@@ -1506,8 +1505,8 @@
     ),
 
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var1, Type1),
-    map.lookup(VarTypes, Var2, Type2),
+    lookup_var_type(VarTypes, Var1, Type1),
+    lookup_var_type(VarTypes, Var2, Type2),
 
     map.search(SharingMap, Var1, SelSharingSet),
     SelSharingSet = selector_sharing_set(_, SelSharingMap),
@@ -1567,8 +1566,8 @@
     ),
 
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.lookup(VarTypes, Var1, Type1),
-    map.lookup(VarTypes, Var2, Type2),
+    lookup_var_type(VarTypes, Var1, Type1),
+    lookup_var_type(VarTypes, Var2, Type2),
 
     ( map.search(SharingMap, Var1, SelSharingSet) ->
         SelSharingSet = selector_sharing_set(_, SelSharingMap),
Index: compiler/switch_detection.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/switch_detection.m,v
retrieving revision 1.159
diff -u -b -r1.159 switch_detection.m
--- compiler/switch_detection.m	13 Feb 2012 00:11:49 -0000	1.159
+++ compiler/switch_detection.m	2 Jul 2012 01:07:01 -0000
@@ -379,7 +379,7 @@
     Case0 = case(MainConsId, OtherConsIds, Goal0),
     VarTypes = !.LocalInfo ^ lsdi_vartypes,
     ModuleInfo0 = !.LocalInfo ^ lsdi_module_info,
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     bind_var_to_functors(Var, VarType, MainConsId, OtherConsIds,
         InstMap0, InstMap1, ModuleInfo0, ModuleInfo),
     !LocalInfo ^ lsdi_module_info := ModuleInfo,
@@ -1049,7 +1049,7 @@
     ModuleInfo = !.LocalInfo ^ lsdi_module_info,
     VarTypes = !.LocalInfo ^ lsdi_vartypes,
     instmap_lookup_var(InstMap, Var, VarInst),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     ( inst_is_bound_to_functors(ModuleInfo, VarInst, Functors) ->
         type_to_ctor_det(Type, TypeCtor),
         bound_insts_to_cons_ids(TypeCtor, Functors, ConsIds),
Index: compiler/table_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/table_gen.m,v
retrieving revision 1.163
diff -u -b -r1.163 table_gen.m
--- compiler/table_gen.m	16 Aug 2011 03:26:34 -0000	1.163
+++ compiler/table_gen.m	2 Jul 2012 01:07:01 -0000
@@ -2323,7 +2323,7 @@
     VarModePos = var_mode_pos_method(Var, _, VarSeqNum, ArgMethod),
     varset.lookup_name(!.VarSet, Var, VarName),
     ModuleInfo = !.TableInfo ^ table_module_info,
-    map.lookup(!.VarTypes, Var, VarType),
+    lookup_var_type(!.VarTypes, Var, VarType),
     CtorCat = classify_type(ModuleInfo, VarType),
     (
         ArgMethod = arg_promise_implied,
@@ -2585,7 +2585,7 @@
     table_gen_make_type_info_var(Type, Context, !VarSet, !VarTypes,
         !TableInfo, TypeInfoVar, PrefixGoals),
     TypeInfoArgName = "input_typeinfo" ++ int_to_string(VarSeqNum),
-    map.lookup(!.VarTypes, TypeInfoVar, TypeInfoType),
+    lookup_var_type(!.VarTypes, TypeInfoVar, TypeInfoType),
     ForeignTypeInfoArg = foreign_arg(TypeInfoVar,
         yes(TypeInfoArgName - in_mode), TypeInfoType,
         native_if_possible),
@@ -2846,7 +2846,7 @@
         PrefixGoals ++ RestPrefixGoals, CodeStr ++ RestCodeStr) :-
     NumberedVar = var_mode_pos_method(Var, _Mode, Offset, _),
     ModuleInfo = !.TableInfo ^ table_module_info,
-    map.lookup(!.VarTypes, Var, VarType),
+    lookup_var_type(!.VarTypes, Var, VarType),
     CtorCat = classify_type(ModuleInfo, VarType),
     gen_save_call_for_type(CtorCat, VarType, Var, Offset, DebugArgStr, Context,
         !VarSet, !VarTypes, !TableInfo, Args, PrefixGoals, CodeStr),
@@ -2879,7 +2879,7 @@
         table_gen_make_type_info_var(Type, Context, !VarSet, !VarTypes,
             !TableInfo, TypeInfoVar, PrefixGoals),
         TypeInfoName = "save_arg_typeinfo" ++ int_to_string(Offset),
-        map.lookup(!.VarTypes, TypeInfoVar, TypeInfoType),
+        lookup_var_type(!.VarTypes, TypeInfoVar, TypeInfoType),
         TypeInfoForeignArg = foreign_arg(TypeInfoVar,
             yes(TypeInfoName - in_mode), TypeInfoType, native_if_possible),
         SaveMacroName = "MR_tbl_save_any_answer",
@@ -3070,7 +3070,7 @@
         DebugArgStr, ModuleInfo, !VarSet, !VarTypes, [VarInst | VarInsts],
         [Arg | Args], CodeStr ++ RestCodeStr) :-
     NumberedVar = var_mode_pos_method(Var, _Mode, Offset, _),
-    map.lookup(!.VarTypes, Var, VarType),
+    lookup_var_type(!.VarTypes, Var, VarType),
     CtorCat = classify_type(ModuleInfo, VarType),
     gen_restore_call_for_type(DebugArgStr, CtorCat, VarType, OrigInstmapDelta,
         Var, Offset, VarInst, Arg, CodeStr),
@@ -3152,7 +3152,7 @@
 
 generate_new_table_var(Name, Type, !VarSet, !VarTypes, Var) :-
     varset.new_named_var(Name, Var, !VarSet),
-    map.set(Var, Type, !VarTypes).
+    add_var_type(Var, Type, !VarTypes).
 
 :- pred table_generate_call(string::in, determinism::in, list(prog_var)::in,
     purity::in, instmap_delta::in, module_info::in, term.context::in,
@@ -3707,7 +3707,7 @@
 :- pred var_is_io_state(vartypes::in, prog_var::in) is semidet.
 
 var_is_io_state(VarTypes, Var) :-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     type_is_io_state(VarType).
 
 %-----------------------------------------------------------------------------%
Index: compiler/term_constr_build.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_build.m,v
retrieving revision 1.36
diff -u -b -r1.36 term_constr_build.m
--- compiler/term_constr_build.m	11 Jun 2012 03:13:22 -0000	1.36
+++ compiler/term_constr_build.m	2 Jul 2012 01:07:01 -0000
@@ -223,7 +223,7 @@
     % Work out which arguments can be used in termination proofs.
     % An argument may be used if (a) it is input and (b) it has non-zero size.
     ChooseArg = (func(Var, Mode) = UseArg :-
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         (
             not zero_size_type(ModuleInfo, Type),
             mode_util.mode_is_input(ModuleInfo, Mode)
@@ -702,7 +702,7 @@
     ;
         TypeMap = !.Info ^ tti_vartypes,
         SizeVarMap = !.Info ^ tti_size_var_map,
-        map.lookup(TypeMap, SwitchProgVar, SwitchVarType),
+        lookup_var_type(TypeMap, SwitchProgVar, SwitchVarType),
         SwitchSizeVar = prog_var_to_size_var(SizeVarMap, SwitchProgVar),
         type_to_ctor_det(SwitchVarType, TypeCtor),
         ModuleInfo = !.Info ^ tti_module_info,
@@ -810,7 +810,7 @@
     ->
         strip_typeinfos_from_args_and_modes(VarTypes, ArgVars, FixedArgVars,
             Modes, FixedModes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         type_to_ctor_det(Type, TypeCtor),
         functor_norm(ModuleInfo, Norm, TypeCtor, ConsId, ConsIdSize,
             FixedArgVars, CountedVars, FixedModes, _),
@@ -877,7 +877,7 @@
 build_abstract_decon_or_con_unify(Var, ConsId, ArgVars, Modes, Constraints,
         !Info) :-
     VarTypes = !.Info ^ tti_vartypes,
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     (
         % The only valid higher-order unifications are assignments.
         % For the purposes of the IR analysis, we can ignore them.
@@ -963,7 +963,7 @@
 strip_typeinfos_from_args_and_modes_2(VarTypes, [Arg | !.Args], !:Args,
         [Mode | !.Modes], !:Modes) :-
     strip_typeinfos_from_args_and_modes_2(VarTypes, !Args, !Modes),
-    map.lookup(VarTypes, Arg, Type),
+    lookup_var_type(VarTypes, Arg, Type),
     ( is_introduced_type_info_type(Type) ->
         true
     ;
@@ -1179,7 +1179,7 @@
 
         GoalExpr = unify(_, _, _, Unification, _),
         Unification = deconstruct(Var, ConsId, _, _, can_fail, _),
-        map.lookup(Info ^ tti_vartypes, Var, Type),
+        lookup_var_type(Info ^ tti_vartypes, Var, Type),
         type_to_ctor_det(Type, TypeCtor),
         ModuleInfo = Info ^ tti_module_info,
         type_util.type_constructors(ModuleInfo, Type, Constructors0),
Index: compiler/term_constr_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_constr_util.m,v
retrieving revision 1.23
diff -u -b -r1.23 term_constr_util.m
--- compiler/term_constr_util.m	23 May 2011 05:08:13 -0000	1.23
+++ compiler/term_constr_util.m	2 Jul 2012 01:07:01 -0000
@@ -268,7 +268,7 @@
     prog_var::in) is semidet.
 
 is_zero_size_prog_var(ModuleInfo, VarTypes, Var) :-
-    Type = VarTypes ^ det_elem(Var),
+    lookup_var_type(VarTypes, Var, Type),
     (
         term_norm.zero_size_type(ModuleInfo, Type)
     ;
Index: compiler/term_traversal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_traversal.m,v
retrieving revision 1.69
diff -u -b -r1.69 term_traversal.m
--- compiler/term_traversal.m	11 Jun 2012 03:13:22 -0000	1.69
+++ compiler/term_traversal.m	2 Jul 2012 01:07:01 -0000
@@ -524,7 +524,7 @@
         InVars, OutVars) :-
     params_get_functor_info(Params, FunctorInfo),
     params_get_var_types(Params, VarTypes),
-    map.lookup(VarTypes, OutVar, Type),
+    lookup_var_type(VarTypes, OutVar, Type),
     \+ type_is_higher_order(Type),
     \+ (
         ConsId = type_info_const(_)
@@ -553,7 +553,7 @@
         [Mode0 | Modes0], Modes) :-
     filter_typeinfos_from_args_and_modes(VarTypes, Args0, TailArgs,
         Modes0, TailModes),
-    map.lookup(VarTypes, Arg0, Type),
+    lookup_var_type(VarTypes, Arg0, Type),
     ( is_introduced_type_info_type(Type) ->
         Args = TailArgs,
         Modes = TailModes
Index: compiler/term_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/term_util.m,v
retrieving revision 1.64
diff -u -b -r1.64 term_util.m
--- compiler/term_util.m	11 Jun 2012 03:13:22 -0000	1.64
+++ compiler/term_util.m	2 Jul 2012 01:07:01 -0000
@@ -334,7 +334,7 @@
 
 horder_vars([Arg | Args], VarType) :-
     (
-        map.lookup(VarType, Arg, Type),
+        lookup_var_type(VarType, Arg, Type),
         type_is_higher_order(Type)
     ;
         horder_vars(Args, VarType)
Index: compiler/trace_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/trace_gen.m,v
retrieving revision 1.42
diff -u -b -r1.42 trace_gen.m
--- compiler/trace_gen.m	17 Oct 2011 04:31:31 -0000	1.42
+++ compiler/trace_gen.m	2 Jul 2012 01:07:01 -0000
@@ -1198,7 +1198,7 @@
 trace_produce_vars([], _, _, _, _, !TVars, !VarInfos, empty, !CI).
 trace_produce_vars([Var | Vars], VarSet, VarTypes, InstMap, Port,
         !TVars, !VarInfos, VarCode ++ VarsCode, !CI) :-
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     get_module_info(!.CI, ModuleInfo),
     IsDummy = check_dummy_type(ModuleInfo, Type),
     (
@@ -1251,7 +1251,7 @@
     (
         ArgMode = top_in,
         \+ inst_is_clobbered(ModuleInfo, Inst),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         check_dummy_type(ModuleInfo, Type) = is_not_dummy_type
     ->
         FailVars = [Var | FailVars0]
Index: compiler/trailing_analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/trailing_analysis.m,v
retrieving revision 1.45
diff -u -b -r1.45 trailing_analysis.m
--- compiler/trailing_analysis.m	11 Apr 2012 04:52:35 -0000	1.45
+++ compiler/trailing_analysis.m	2 Jul 2012 01:07:01 -0000
@@ -340,7 +340,7 @@
             % Handle (mutually-)recursive calls.
             list.member(CallPPId, SCC)
         ->
-            Types = list.map((func(Var) = VarTypes ^ det_elem(Var)), CallArgs),
+            lookup_var_types(VarTypes, CallArgs, Types),
             TrailingStatus = check_types(!.ModuleInfo, Types),
             Result = TrailingStatus,
             MaybeAnalysisStatus = yes(optimal)
@@ -710,7 +710,7 @@
 :- func check_vars(module_info, vartypes, prog_vars) = trailing_status.
 
 check_vars(ModuleInfo, VarTypes, Vars) = Result :-
-    Types = list.map((func(Var) = VarTypes ^ det_elem(Var)), Vars),
+    lookup_var_types(VarTypes, Vars, Types),
     Result = check_types(ModuleInfo, Types).
 
 %----------------------------------------------------------------------------%
Index: compiler/try_expand.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/try_expand.m,v
retrieving revision 1.23
diff -u -b -r1.23 try_expand.m
--- compiler/try_expand.m	23 Apr 2012 03:34:49 -0000	1.23
+++ compiler/try_expand.m	2 Jul 2012 01:07:01 -0000
@@ -534,7 +534,7 @@
         % Get the type of the output tuple.
         proc_info_get_vartypes(!.ProcInfo, !:VarTypes),
         GoalOutputVars = set_of_var.to_sorted_list(GoalOutputVarsSet),
-        map.apply_to_list(GoalOutputVars, !.VarTypes, GoalOutputVarTypes),
+        lookup_var_types(!.VarTypes, GoalOutputVars, GoalOutputVarTypes),
         OutputTupleType = tuple_type(GoalOutputVarTypes, kind_star),
 
         % Fix the type of the result of the try call, now that we know what it
@@ -542,7 +542,7 @@
         RealResultVarType = defined_type(
             qualified(mercury_exception_module, "exception_result"),
             [OutputTupleType], kind_star),
-        map.det_update(ResultVar, RealResultVarType, !VarTypes),
+        update_var_type(ResultVar, RealResultVarType, !VarTypes),
         proc_info_set_vartypes(!.VarTypes, !ProcInfo)
     ),
 
Index: compiler/tupling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/tupling.m,v
retrieving revision 1.69
diff -u -b -r1.69 tupling.m
--- compiler/tupling.m	13 Feb 2012 00:11:49 -0000	1.69
+++ compiler/tupling.m	2 Jul 2012 01:07:01 -0000
@@ -460,7 +460,7 @@
     % We only tuple input arguments.
     mode_is_input(ModuleInfo, ArgMode),
     % Don't touch introduced typeinfo arguments.
-    map.lookup(VarTypes, HeadVar, Type),
+    lookup_var_type(VarTypes, HeadVar, Type),
     not is_introduced_type_info_type(Type),
     varset.search_name(VarSet, HeadVar, Name),
     Origins = map.singleton(PredProcId, HeadVar).
@@ -642,7 +642,7 @@
         % Create the cell variable.
         list.length(FieldVars, TupleArity),
         proc_info_get_vartypes(!.ProcInfo, VarTypes),
-        list.map(map.lookup(VarTypes), FieldVars, TupleArgTypes),
+        lookup_var_types(VarTypes, FieldVars, TupleArgTypes),
         construct_type(type_ctor(unqualified("{}"), TupleArity), TupleArgTypes,
             TupleConsType),
         proc_info_create_var_from_type(TupleConsType,
@@ -1068,7 +1068,7 @@
         ModuleInfo = CountInfo ^ ci_module,
         goal_info_get_maybe_need_across_call(GoalInfo, MaybeNeedAcrossCall),
         proc_info_get_vartypes(ProcInfo, VarTypes),
-        map.apply_to_list(ArgVars, VarTypes, ArgTypes),
+        lookup_var_types(VarTypes, ArgVars, ArgTypes),
         arg_info.generic_call_arg_reg_types(ModuleInfo, VarTypes,
             GenericCall, ArgVars, MaybeArgRegs, ArgRegTypes),
         arg_info.compute_in_and_out_vars_sep_regs(ModuleInfo, ArgVars,
@@ -1747,7 +1747,7 @@
                 hlds_goal(CallAux0, CallAuxInfo))
         ->
             varset.new_named_var("TuplingCellVarForCall", CellVar, !VarSet),
-            map.det_insert(CellVar, TupleConsType, !VarTypes),
+            add_var_type(CellVar, TupleConsType, !VarTypes),
             extract_tupled_args_from_list(Args0, ArgsToTuple,
                 TupledArgs, UntupledArgs),
             construct_tuple(CellVar, TupledArgs, ConstructGoal),
Index: compiler/type_constraints.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/type_constraints.m,v
retrieving revision 1.22
diff -u -b -r1.22 type_constraints.m
--- compiler/type_constraints.m	13 Feb 2012 00:11:50 -0000	1.22
+++ compiler/type_constraints.m	2 Jul 2012 01:07:01 -0000
@@ -481,8 +481,7 @@
     list.map2(find_variable_type(Context, ProgVarSet, TVarSet, VarMap,
         DomainMap, ReplacementMap), ProgVars, Types, MaybeErrors),
     list.filter_map(remove_maybe, MaybeErrors, Errors),
-    map.det_insert_from_corresponding_lists(ProgVars, Types,
-        map.init, Vartypes).
+    vartypes_from_corresponding_lists(ProgVars, Types, Vartypes).
 
     % If a variable has a domain consisting of one type, gives it that type.
     % Otherwise, assign it to a type consisting of the type variable assigned
@@ -1397,11 +1396,11 @@
     vartypes::in, vartypes::out) is det.
 
 add_unused_prog_var(TCInfo, Var, !Vartypes) :-
-    ( map.contains(!.Vartypes, Var) ->
+    ( is_in_vartypes(!.Vartypes, Var) ->
         true
     ;
         bimap.lookup(TCInfo ^ tconstr_var_map, Var, TVar),
-        map.det_insert(Var, tvar_to_type(TVar), !Vartypes)
+        add_var_type(Var, tvar_to_type(TVar), !Vartypes)
     ).
 
 :- pred get_constraints_from_conj(conj_type_constraint::in,
Index: compiler/type_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/type_util.m,v
retrieving revision 1.217
diff -u -b -r1.217 type_util.m
--- compiler/type_util.m	19 Jun 2012 07:21:23 -0000	1.217
+++ compiler/type_util.m	2 Jul 2012 01:07:01 -0000
@@ -561,7 +561,7 @@
         ArgTypes, !SeenTypes).
 
 var_is_or_may_contain_solver_type(ModuleInfo, VarTypes, Var) :-
-    map.lookup(VarTypes, Var, VarType),
+    lookup_var_type(VarTypes, Var, VarType),
     type_is_or_may_contain_solver_type(ModuleInfo, VarType).
 
 type_is_solver_type_with_auto_init(ModuleInfo, Type) :-
Index: compiler/typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck.m,v
retrieving revision 1.469
diff -u -b -r1.469 typecheck.m
--- compiler/typecheck.m	19 Jun 2012 07:21:24 -0000	1.469
+++ compiler/typecheck.m	2 Jul 2012 01:07:01 -0000
@@ -508,7 +508,8 @@
             ( check_marker(Markers0, marker_class_method) ->
                 % For the moment, we just insert the types of the head vars
                 % into the clauses_info.
-                map.from_corresponding_lists(HeadVars, ArgTypes0, VarTypes),
+                vartypes_from_corresponding_lists(HeadVars, ArgTypes0,
+                    VarTypes),
                 clauses_info_set_vartypes(VarTypes, !ClausesInfo),
                 pred_info_set_clauses_info(!.ClausesInfo, !PredInfo),
                 % We also need to set the head_type_params field to indicate
@@ -596,7 +597,7 @@
             InferredTypeConstraints0, ConstraintProofs, ConstraintMap,
             TVarRenaming, ExistTypeRenaming),
         typecheck_info_get_pred_markers(!.Info, PredMarkers),
-        map.optimize(InferredVarTypes0, InferredVarTypes),
+        vartypes_optimize(InferredVarTypes0, InferredVarTypes),
         clauses_info_set_vartypes(InferredVarTypes, !ClausesInfo),
 
         % Apply substitutions to the explicit vartypes.
@@ -623,7 +624,7 @@
         % Split the inferred type class constraints into those that apply
         % only to the head variables, and those that apply to type variables
         % which occur only in the body.
-        map.apply_to_list(HeadVars, InferredVarTypes, ArgTypes),
+        lookup_var_types(InferredVarTypes, HeadVars, ArgTypes),
         type_vars_list(ArgTypes, ArgTypeVars),
         restrict_to_head_vars(InferredTypeConstraints0, ArgTypeVars,
             InferredTypeConstraints, UnprovenBodyConstraints),
@@ -1159,8 +1160,8 @@
                 type_assign_get_var_types(TypeAssign2, VarTypes2),
                 type_assign_get_type_bindings(TypeAssign1, TypeBindings1),
                 type_assign_get_type_bindings(TypeAssign2, TypeBindings2),
-                map.apply_to_list(HeadVars, VarTypes1, HeadTypes1),
-                map.apply_to_list(HeadVars, VarTypes2, HeadTypes2),
+                lookup_var_types(VarTypes1, HeadVars, HeadTypes1),
+                lookup_var_types(VarTypes2, HeadVars, HeadTypes2),
                 apply_rec_subst_to_type_list(TypeBindings1, HeadTypes1,
                     FinalHeadTypes1),
                 apply_rec_subst_to_type_list(TypeBindings2, HeadTypes2,
@@ -1840,7 +1841,8 @@
     type_assign_get_var_types(TypeAssign0, VarTypes0),
     (
         ArgTypes0 = [Type | ArgTypes],
-        map.search_insert(Var, Type, MaybeOldVarType, VarTypes0, VarTypes),
+        search_insert_var_type(Var, Type, MaybeOldVarType,
+            VarTypes0, VarTypes),
         (
             MaybeOldVarType = yes(OldVarType),
             (
@@ -1872,7 +1874,7 @@
 type_assign_var_has_one_of_these_types(TypeAssign0, Var, TypeA, TypeB,
         !TypeAssignSet) :-
     type_assign_get_var_types(TypeAssign0, VarTypes0),
-    ( map.search(VarTypes0, Var, VarType) ->
+    ( search_var_type(VarTypes0, Var, VarType) ->
         ( type_assign_unify_type(TypeAssign0, VarType, TypeA, TypeAssignA) ->
             !:TypeAssignSet = [TypeAssignA | !.TypeAssignSet]
         ;
@@ -1884,10 +1886,9 @@
             !:TypeAssignSet = !.TypeAssignSet
         )
     ;
-        % YYY
-        map.det_insert(Var, TypeA, VarTypes0, VarTypesA),
+        add_var_type(Var, TypeA, VarTypes0, VarTypesA),
         type_assign_set_var_types(VarTypesA, TypeAssign0, TypeAssignA),
-        map.det_insert(Var, TypeB, VarTypes0, VarTypesB),
+        add_var_type(Var, TypeB, VarTypes0, VarTypesB),
         type_assign_set_var_types(VarTypesB, TypeAssign0, TypeAssignB),
         !:TypeAssignSet = [TypeAssignA, TypeAssignB | !.TypeAssignSet]
     ).
@@ -1940,7 +1941,7 @@
 
 type_assign_var_has_type(TypeAssign0, Var, Type, !TypeAssignSet) :-
     type_assign_get_var_types(TypeAssign0, VarTypes0),
-    map.search_insert(Var, Type, MaybeOldVarType, VarTypes0, VarTypes),
+    search_insert_var_type(Var, Type, MaybeOldVarType, VarTypes0, VarTypes),
     (
         MaybeOldVarType = yes(OldVarType),
         ( type_assign_unify_type(TypeAssign0, OldVarType, Type, TypeAssign1) ->
@@ -2281,8 +2282,8 @@
 
 type_assign_unify_var_var(X, Y, TypeAssign0, !TypeAssignSet) :-
     type_assign_get_var_types(TypeAssign0, VarTypes0),
-    ( map.search(VarTypes0, X, TypeX) ->
-        map.search_insert(Y, TypeX, MaybeTypeY, VarTypes0, VarTypes),
+    ( search_var_type(VarTypes0, X, TypeX) ->
+        search_insert_var_type(Y, TypeX, MaybeTypeY, VarTypes0, VarTypes),
         (
             MaybeTypeY = yes(TypeY),
             % Both X and Y already have types - just unify their types.
@@ -2297,9 +2298,9 @@
             !:TypeAssignSet = [TypeAssign | !.TypeAssignSet]
         )
     ;
-        ( map.search(VarTypes0, Y, TypeY) ->
+        ( search_var_type(VarTypes0, Y, TypeY) ->
             % X is a fresh variable which hasn't been assigned a type yet.
-            map.det_insert(X, TypeY, VarTypes0, VarTypes),
+            add_var_type(X, TypeY, VarTypes0, VarTypes),
             type_assign_set_var_types(VarTypes, TypeAssign0, TypeAssign),
             !:TypeAssignSet = [TypeAssign | !.TypeAssignSet]
         ;
@@ -2309,9 +2310,9 @@
             varset.new_var(TypeVar, TypeVarSet0, TypeVarSet),
             type_assign_set_typevarset(TypeVarSet, TypeAssign0, TypeAssign1),
             Type = type_variable(TypeVar, kind_star),
-            map.det_insert(X, Type, VarTypes0, VarTypes1),
+            add_var_type(X, Type, VarTypes0, VarTypes1),
             ( X \= Y ->
-                map.det_insert(Y, Type, VarTypes1, VarTypes)
+                add_var_type(Y, Type, VarTypes1, VarTypes)
             ;
                 VarTypes = VarTypes1
             ),
@@ -2330,7 +2331,7 @@
         !ArgsTypeAssignSet) :-
     % Unify the type of Var with the type of the constructor.
     type_assign_get_var_types(TypeAssign0, VarTypes0),
-    map.search_insert(Y, ConsType, MaybeTypeY, VarTypes0, VarTypes),
+    search_insert_var_type(Y, ConsType, MaybeTypeY, VarTypes0, VarTypes),
     (
         MaybeTypeY = yes(TypeY),
         ( type_assign_unify_type(TypeAssign0, ConsType, TypeY, TypeAssign) ->
@@ -2362,7 +2363,7 @@
         !TypeAssignSet) :-
     % Unify the type of Var with the type of the constructor.
     type_assign_get_var_types(TypeAssign0, VarTypes0),
-    map.search_insert(Y, ConsType, MaybeTypeY, VarTypes0, VarTypes),
+    search_insert_var_type(Y, ConsType, MaybeTypeY, VarTypes0, VarTypes),
     (
         MaybeTypeY = yes(TypeY),
         ( type_assign_unify_type(TypeAssign0, ConsType, TypeY, TypeAssign) ->
@@ -2509,7 +2510,7 @@
 type_assign_get_types_of_vars([Var | Vars], [Type | Types], !TypeAssign) :-
     % Check whether the variable already has a type.
     type_assign_get_var_types(!.TypeAssign, VarTypes0),
-    ( map.search(VarTypes0, Var, VarType) ->
+    ( search_var_type(VarTypes0, Var, VarType) ->
         % If so, use that type.
         Type = VarType
     ;
@@ -2519,7 +2520,7 @@
         varset.new_var(TypeVar, TypeVarSet0, TypeVarSet),
         type_assign_set_typevarset(TypeVarSet, !TypeAssign),
         Type = type_variable(TypeVar, kind_star),
-        map.det_insert(Var, Type, VarTypes0, VarTypes1),
+        add_var_type(Var, Type, VarTypes0, VarTypes1),
         type_assign_set_var_types(VarTypes1, !TypeAssign)
     ),
     % Recursively process the rest of the variables.
Index: compiler/typecheck_errors.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck_errors.m,v
retrieving revision 1.56
diff -u -b -r1.56 typecheck_errors.m
--- compiler/typecheck_errors.m	19 Jun 2012 07:21:24 -0000	1.56
+++ compiler/typecheck_errors.m	2 Jul 2012 01:07:01 -0000
@@ -1302,7 +1302,7 @@
         [words("error: ambiguous overloading causes type ambiguity."), nl],
     typecheck_info_get_varset(Info, VarSet),
     type_assign_get_var_types(TypeAssign1, VarTypes1),
-    map.keys(VarTypes1, Vars1),
+    vartypes_vars(VarTypes1, Vars1),
     AmbiguityPieces = ambiguity_error_possibilities_to_pieces(Vars1, VarSet,
         TypeAssign1, TypeAssign2),
     (
@@ -1350,8 +1350,8 @@
     type_assign_get_head_type_params(TypeAssign1, HeadTypeParams1),
     type_assign_get_head_type_params(TypeAssign2, HeadTypeParams2),
     (
-        map.search(VarTypes1, Var, Type1),
-        map.search(VarTypes2, Var, Type2),
+        search_var_type(VarTypes1, Var, Type1),
+        search_var_type(VarTypes2, Var, Type2),
         apply_rec_subst_to_type(TypeBindings1, Type1, T1),
         apply_rec_subst_to_type(TypeBindings2, Type2, T2),
         \+ identical_types(T1, T2)
@@ -1855,7 +1855,7 @@
     type_assign_get_type_bindings(TypeAssign, TypeBindings),
     type_assign_get_typevarset(TypeAssign, TVarSet),
     type_assign_get_var_types(TypeAssign, VarTypes),
-    ( map.search(VarTypes, Var, Type0) ->
+    ( search_var_type(VarTypes, Var, Type0) ->
         Type = Type0
     ;
         % This shouldn't happen - how can a variable which has not yet been
@@ -1903,7 +1903,7 @@
     type_assign_get_type_bindings(TypeAssign, TypeBindings),
     type_assign_get_typevarset(TypeAssign, TVarSet),
     type_assign_get_var_types(TypeAssign, VarTypes),
-    ( map.search(VarTypes, Var, VarType0) ->
+    ( search_var_type(VarTypes, Var, VarType0) ->
         VarType = VarType0
     ;
         % This shouldn't happen - how can a variable which has
Index: compiler/typecheck_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck_info.m,v
retrieving revision 1.40
diff -u -b -r1.40 typecheck_info.m
--- compiler/typecheck_info.m	19 Jun 2012 07:21:24 -0000	1.40
+++ compiler/typecheck_info.m	2 Jul 2012 01:07:01 -0000
@@ -440,11 +440,11 @@
             VarTypes1 = VarTypes0,
             ConstraintProofs = ConstraintProofs0,
             ConstraintMap1 = ConstraintMap0,
-            map.values(VarTypes1, Types1),
+            vartypes_types(VarTypes1, Types1),
             type_vars_list(Types1, TypeVars1)
         ;
-            map.map_foldl(expand_types(TypeBindings), VarTypes0, VarTypes1,
-                set.init, TypeVarsSet1),
+            transform_foldl_var_types(expand_types(TypeBindings),
+                VarTypes0, VarTypes1, set.init, TypeVarsSet1),
             set.to_sorted_list(TypeVarsSet1, TypeVars1),
             apply_rec_subst_to_constraint_proofs(TypeBindings,
                 ConstraintProofs0, ConstraintProofs),
@@ -495,7 +495,7 @@
         % (XXX should we do the same for TypeConstraints and ConstraintProofs
         % too?)
 
-        map.values(OldExplicitVarTypes, ExplicitTypes),
+        vartypes_types(OldExplicitVarTypes, ExplicitTypes),
         type_vars_list(ExplicitTypes, ExplicitTypeVars0),
         map.keys(ExistTypeRenaming, ExistQVarsToBeRenamed),
         list.delete_elems(OldExistQVars, ExistQVarsToBeRenamed,
@@ -517,7 +517,7 @@
             NewConstraintProofs = ConstraintProofs,
             NewConstraintMap = ConstraintMap
         ;
-            map.map_values_only(apply_variable_renaming_to_type(TSubst),
+            transform_var_types(apply_variable_renaming_to_type(TSubst),
                 VarTypes1, NewVarTypes),
             map.apply_to_list(HeadTypeParams, TSubst, NewHeadTypeParams),
             apply_variable_renaming_to_prog_constraints(TSubst,
@@ -536,10 +536,10 @@
     % We also accumulate the set of type variables we have seen so far,
     % since doing so saves having to do a separate traversal for that.
     %
-:- pred expand_types(tsubst::in, prog_var::in, mer_type::in, mer_type::out,
+:- pred expand_types(tsubst::in, mer_type::in, mer_type::out,
     set(tvar)::in, set(tvar)::out) is det.
 
-expand_types(TypeSubst, _Var, Type0, Type, !TypeVarsSet) :-
+expand_types(TypeSubst, Type0, Type, !TypeVarsSet) :-
     apply_rec_subst_to_type(TypeSubst, Type0, Type),
     type_vars(Type, TypeVars),
     set.insert_list(TypeVars, !TypeVarsSet).
@@ -806,7 +806,7 @@
     type_assign_get_typeclass_constraints(TypeAssign, Constraints),
     type_assign_get_type_bindings(TypeAssign, TypeBindings),
     type_assign_get_typevarset(TypeAssign, TypeVarSet),
-    map.keys(VarTypes, Vars),
+    vartypes_vars(VarTypes, Vars),
     (
         HeadTypeParams = []
     ;
@@ -842,7 +842,7 @@
     type_assign_get_typeclass_constraints(TypeAssign, Constraints),
     type_assign_get_type_bindings(TypeAssign, TypeBindings),
     type_assign_get_typevarset(TypeAssign, TypeVarSet),
-    map.keys(VarTypes, Vars),
+    vartypes_vars(VarTypes, Vars),
     (
         HeadTypeParams = [],
         HeadPieces = []
@@ -869,7 +869,7 @@
     ).
 write_type_assign_types([Var | Vars], VarSet, VarTypes, TypeBindings,
         TypeVarSet, FoundOne, !IO) :-
-    ( map.search(VarTypes, Var, Type) ->
+    ( search_var_type(VarTypes, Var, Type) ->
         (
             FoundOne = yes,
             io.write_string("\n\t", !IO)
@@ -899,7 +899,7 @@
     ).
 type_assign_types_to_pieces([Var | Vars], VarSet, VarTypes, TypeBindings,
         TypeVarSet, FoundOne) = Pieces :-
-    ( map.search(VarTypes, Var, Type) ->
+    ( search_var_type(VarTypes, Var, Type) ->
         (
             FoundOne = yes,
             PrefixPieces = [nl]
@@ -1032,7 +1032,7 @@
         TypeAssignSet = [TypeAssign | _]
     ->
         type_assign_get_var_types(TypeAssign, VarTypes),
-        checkpoint_tree_stats("\t`var -> type' map", VarTypes, !IO),
+        checkpoint_vartypes_stats("\t`var -> type' map", VarTypes, !IO),
         type_assign_get_type_bindings(TypeAssign, TypeBindings),
         checkpoint_tree_stats("\t`type var -> type' map", TypeBindings, !IO)
     ;
@@ -1051,6 +1051,16 @@
     io.write_int(Count, !IO),
     io.write_string("\n", !IO).
 
+:- pred checkpoint_vartypes_stats(string::in, vartypes::in, io::di, io::uo)
+    is det.
+
+checkpoint_vartypes_stats(Description, Tree, !IO) :-
+    vartypes_count(Tree, Count),
+    io.write_string(Description, !IO),
+    io.write_string(": count = ", !IO),
+    io.write_int(Count, !IO),
+    io.write_string("\n", !IO).
+
 %-----------------------------------------------------------------------------%
 :- end_module check_hlds.typecheck_info.
 %-----------------------------------------------------------------------------%
Index: compiler/unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unify_gen.m,v
retrieving revision 1.213
diff -u -b -r1.213 unify_gen.m
--- compiler/unify_gen.m	11 Jun 2012 03:13:22 -0000	1.213
+++ compiler/unify_gen.m	2 Jul 2012 01:07:01 -0000
@@ -960,7 +960,7 @@
         ),
         CellArg = cell_arg_skip
     ),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     get_module_info(CI, ModuleInfo),
     update_type_may_use_atomic_alloc(ModuleInfo, Type, !MayUseAtomic),
     generate_pred_args(CI, VarTypes, Vars, ArgInfos, ArgsR0, ArgsF0,
@@ -1205,7 +1205,7 @@
 var_types(CI, Vars, Types) :-
     get_proc_info(CI, ProcInfo),
     proc_info_get_vartypes(ProcInfo, VarTypes),
-    map.apply_to_list(Vars, VarTypes, Types).
+    lookup_var_types(VarTypes, Vars, Types).
 
 %---------------------------------------------------------------------------%
 
Index: compiler/unify_proc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unify_proc.m,v
retrieving revision 1.223
diff -u -b -r1.223 unify_proc.m
--- compiler/unify_proc.m	16 Aug 2011 03:26:34 -0000	1.223
+++ compiler/unify_proc.m	2 Jul 2012 01:07:01 -0000
@@ -2074,19 +2074,23 @@
 
 info_init(ModuleInfo, UPI) :-
     varset.init(VarSet),
-    map.init(Types),
+    init_vartypes(VarTypes),
     rtti_varmaps_init(RttiVarMaps),
-    UPI = unify_proc_info(VarSet, Types, RttiVarMaps, ModuleInfo).
+    UPI = unify_proc_info(VarSet, VarTypes, RttiVarMaps, ModuleInfo).
 
 info_new_var(Type, Var, !UPI) :-
-    varset.new_var(Var, !.UPI ^ upi_varset, VarSet),
-    map.det_insert(Var, Type, !.UPI ^ upi_vartypes, VarTypes),
+    VarSet0 = !.UPI ^ upi_varset,
+    VarTypes0 = !.UPI ^ upi_vartypes,
+    varset.new_var(Var, VarSet0, VarSet),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     !UPI ^ upi_varset := VarSet,
     !UPI ^ upi_vartypes := VarTypes.
 
 info_new_named_var(Type, Name, Var, !UPI) :-
-    varset.new_named_var(Name, Var, !.UPI ^ upi_varset, VarSet),
-    map.det_insert(Var, Type, !.UPI ^ upi_vartypes, VarTypes),
+    VarSet0 = !.UPI ^ upi_varset,
+    VarTypes0 = !.UPI ^ upi_vartypes,
+    varset.new_named_var(Name, Var, VarSet0, VarSet),
+    add_var_type(Var, Type, VarTypes0, VarTypes),
     !UPI ^ upi_varset := VarSet,
     !UPI ^ upi_vartypes := VarTypes.
 
Index: compiler/unique_modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unique_modes.m,v
retrieving revision 1.144
diff -u -b -r1.144 unique_modes.m
--- compiler/unique_modes.m	23 Apr 2012 03:34:49 -0000	1.144
+++ compiler/unique_modes.m	2 Jul 2012 01:07:01 -0000
@@ -208,7 +208,7 @@
     mode_info_get_instmap(ModeInfo, InstMap0),
     instmap_lookup_var(InstMap0, Var, Inst0),
     mode_info_get_var_types(ModeInfo, VarTypes),
-    map.lookup(VarTypes, Var, Type),
+    lookup_var_type(VarTypes, Var, Type),
     (
         instmap_delta_is_reachable(DeltaInstMap),
         instmap_delta_search_var(DeltaInstMap, Var, Inst),
Index: compiler/untupling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/untupling.m,v
retrieving revision 1.42
diff -u -b -r1.42 untupling.m
--- compiler/untupling.m	13 Feb 2012 00:11:50 -0000	1.42
+++ compiler/untupling.m	2 Jul 2012 01:07:01 -0000
@@ -322,7 +322,7 @@
 expand_one_arg_in_proc_2(HeadVar0, ArgMode0, MaybeHeadVarsAndArgModes,
         !Goal, !VarSet, !VarTypes, ContainerTypes0, ContainerTypes,
         TypeTable) :-
-    map.lookup(!.VarTypes, HeadVar0, Type),
+    lookup_var_type(!.VarTypes, HeadVar0, Type),
     expand_argument(ArgMode0, Type, ContainerTypes0, TypeTable, Expansion),
     (
         Expansion = expansion(ConsId, NewTypes),
@@ -356,7 +356,7 @@
         !VarSet, !VarTypes) :-
     string.format("Untupled_%s_%d", [s(ParentName), i(Num)], Name),
     varset.new_named_var(Name, NewVar, !VarSet),
-    map.det_insert(NewVar, Type, !VarTypes),
+    add_var_type(NewVar, Type, !VarTypes),
     create_untuple_vars(ParentName, Num+1, Types, NewVars, !VarSet, !VarTypes).
 
 :- pred conjoin_goals_keep_detism(hlds_goal::in, hlds_goal::in,
@@ -657,14 +657,13 @@
 expand_call_args_2([Arg0 | Args0], [ArgMode | ArgModes], Args,
         EnterUnifs, ExitUnifs, !VarSet, !VarTypes,
         ContainerTypes0, TypeTable) :-
-    map.lookup(!.VarTypes, Arg0, Arg0Type),
+    lookup_var_type(!.VarTypes, Arg0, Arg0Type),
     expand_argument(ArgMode, Arg0Type, ContainerTypes0, TypeTable, Expansion),
     (
         Expansion = expansion(ConsId, Types),
         NumVars = list.length(Types),
         varset.new_vars(NumVars, ReplacementArgs, !VarSet),
-        map.det_insert_from_corresponding_lists(
-            ReplacementArgs, Types, !VarTypes),
+        vartypes_add_corresponding_lists(ReplacementArgs, Types, !VarTypes),
         list.duplicate(NumVars, ArgMode, ReplacementModes),
         ContainerTypes = [Arg0Type | ContainerTypes0],
         ( ArgMode = in_mode ->
Index: compiler/unused_args.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unused_args.m,v
retrieving revision 1.174
diff -u -b -r1.174 unused_args.m
--- compiler/unused_args.m	8 Jun 2012 15:37:01 -0000	1.174
+++ compiler/unused_args.m	2 Jul 2012 01:07:01 -0000
@@ -475,7 +475,7 @@
             true
         ;
             proc_info_get_vartypes(ProcInfo, VarTypes),
-            map.keys(VarTypes, Vars),
+            vartypes_vars(VarTypes, Vars),
             initialise_vardep(Vars, !VarDep),
             setup_output_args(!.ModuleInfo, ProcInfo, !VarDep),
 
@@ -532,7 +532,7 @@
     pred_proc_id::in, rtti_varmaps::in, var_dep::in, var_dep::out) is det.
 
 setup_typeinfo_dep(Var, VarTypeMap, PredProcId, RttiVarMaps, !VarDep) :-
-    map.lookup(VarTypeMap, Var, Type),
+    lookup_var_type(VarTypeMap, Var, Type),
     type_vars(Type, TVars),
     list.map(tvar_to_type_info_var(RttiVarMaps), TVars, TypeInfoVars),
     list.foldl(add_rev_arg_dep(Var, PredProcId), TypeInfoVars, !VarDep).
@@ -790,7 +790,7 @@
         partition_deconstruct_args(Info, Vars, Modes, InputVars1, OutputVars1),
         Mode = ((InitialInst1 - InitialInst2) -> (FinalInst1 - FinalInst2)),
 
-        map.lookup(Info ^ unarg_vartypes, Var, Type),
+        lookup_var_type(Info ^ unarg_vartypes, Var, Type),
 
         % If the inst of the argument of the LHS is changed,
         % the argument is input.
@@ -1202,8 +1202,8 @@
 
     proc_info_get_vartypes(!.OldProc, VarTypes0),
     set.list_to_set(HeadVars, NonLocals),
-    map.apply_to_list(HeadVars, VarTypes0, VarTypeList),
-    map.from_corresponding_lists(HeadVars, VarTypeList, VarTypes1),
+    lookup_var_types(VarTypes0, HeadVars, VarTypeList),
+    vartypes_from_corresponding_lists(HeadVars, VarTypeList, VarTypes1),
     % The varset should probably be fixed up, but it shouldn't make
     % too much difference.
     proc_info_get_varset(!.OldProc, VarSet0),
@@ -1545,8 +1545,8 @@
         ;
             varset.new_var(NewVar, VarSet0, VarSet)
         ),
-        map.lookup(VarTypes0, OldVar, Type),
-        map.det_insert(NewVar, Type, VarTypes0, VarTypes),
+        lookup_var_type(VarTypes0, OldVar, Type),
+        add_var_type(NewVar, Type, VarTypes0, VarTypes),
         !Info ^ fixup_varset := VarSet,
         !Info ^ fixup_vartypes := VarTypes,
 
Index: compiler/var_locn.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/var_locn.m,v
retrieving revision 1.83
diff -u -b -r1.83 var_locn.m
--- compiler/var_locn.m	8 Jun 2012 15:37:02 -0000	1.83
+++ compiler/var_locn.m	2 Jul 2012 01:07:01 -0000
@@ -1149,7 +1149,7 @@
         Rval0 = var(Var),
         materialize_if_var(ModuleInfo, Rval0, EvalCode, Rval, !VLI),
         var_locn_get_vartypes(!.VLI, VarTypes),
-        map.lookup(VarTypes, Var, Type),
+        lookup_var_type(VarTypes, Var, Type),
         IsDummy = check_dummy_type(ModuleInfo, Type),
         (
             IsDummy = is_dummy_type,
@@ -1238,7 +1238,7 @@
             !VLI)
     ),
     var_locn_get_vartypes(!.VLI, VarTypes),
-    map.lookup(VarTypes, DepVar, DepVarType),
+    lookup_var_type(VarTypes, DepVar, DepVarType),
     IsDummy = check_dummy_type(ModuleInfo, DepVarType),
     (
         IsDummy = is_dummy_type,
@@ -1441,7 +1441,7 @@
     ;
         FloatRegType = reg_f,
         var_locn_get_vartypes(VLI, VarTypes),
-        map.lookup(VarTypes, Var, VarType),
+        lookup_var_type(VarTypes, Var, VarType),
         ( VarType = float_type ->
             RegType = reg_f
         ;
@@ -1546,7 +1546,7 @@
                     " (depth ", LengthStr, ")"], Msg)
             ),
             var_locn_get_vartypes(!.VLI, VarTypes),
-            map.lookup(VarTypes, Var, Type),
+            lookup_var_type(VarTypes, Var, Type),
             IsDummy = check_dummy_type(ModuleInfo, Type),
             (
                 IsDummy = is_dummy_type,
cvs diff: Diffing compiler/notes
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/base64
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/fixed
cvs diff: Diffing extras/gator
cvs diff: Diffing extras/gator/generations
cvs diff: Diffing extras/gator/generations/1
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_allegro
cvs diff: Diffing extras/graphics/mercury_allegro/examples
cvs diff: Diffing extras/graphics/mercury_allegro/samples
cvs diff: Diffing extras/graphics/mercury_allegro/samples/demo
cvs diff: Diffing extras/graphics/mercury_allegro/samples/mandel
cvs diff: Diffing extras/graphics/mercury_allegro/samples/pendulum2
cvs diff: Diffing extras/graphics/mercury_allegro/samples/speed
cvs diff: Diffing extras/graphics/mercury_cairo
cvs diff: Diffing extras/graphics/mercury_cairo/samples
cvs diff: Diffing extras/graphics/mercury_cairo/samples/data
cvs diff: Diffing extras/graphics/mercury_cairo/tutorial
cvs diff: Diffing extras/graphics/mercury_glfw
cvs diff: Diffing extras/graphics/mercury_glfw/samples
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/log4m
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/monte
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/mopenssl
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/net
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/posix/samples
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/windows_installer_generator
cvs diff: Diffing extras/windows_installer_generator/sample
cvs diff: Diffing extras/windows_installer_generator/sample/images
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
Index: library/version_array.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/version_array.m,v
retrieving revision 1.38
diff -u -b -r1.38 version_array.m
--- library/version_array.m	10 May 2012 00:17:06 -0000	1.38
+++ library/version_array.m	2 Jul 2012 01:07:02 -0000
@@ -71,7 +71,7 @@
     %
 :- func empty = version_array(T).
 
-    % init(N, X returns an array of size N with each item initialised to X.
+    % init(N, X) returns an array of size N with each item initialised to X.
     %
 :- func init(int, T) = version_array(T).
 
@@ -118,9 +118,9 @@
     %
 :- func lookup(version_array(T), int) = T.
 
-    % (A ^ elem(I) := X) is a copy of array A with item I updated to be
-    % X.  An exception is thrown if I is out of bounds.  set/4 is an
-    % equivalent predicate.
+    % (A ^ elem(I) := X) is a copy of array A with item I updated to be X.
+    % An exception is thrown if I is out of bounds. set/4 is an equivalent
+    % predicate.
     %
 :- func (version_array(T) ^ elem(int) := T) = version_array(T).
 
@@ -138,8 +138,8 @@
 
     % resize(A, N, X) returns a new array whose items from
     % 0..min(size(A), N - 1) are taken from A and whose items
-    % from min(size(A), N - 1)..(N - 1) (if any) are initialised
-    % to X.  A predicate version is also provided.
+    % from min(size(A), N - 1)..(N - 1) (if any) are initialised to X.
+    % A predicate version is also provided.
     %
 :- func resize(version_array(T), int, T) = version_array(T).
 :- pred resize(int::in, T::in, version_array(T)::in, version_array(T)::out)
@@ -215,11 +215,11 @@
     %
 :- func copy(version_array(T)) = version_array(T).
 
-    % unsafe_rewind(A) produces a version of A for which all accesses are
-    % O(1).  Invoking this predicate renders A and all later versions undefined
-    % that were derived by performing individual updates.  Only use this when
-    % you are absolutely certain there are no live references to A or later
-    % versions of A.  (A predicate version is also provided.)
+    % unsafe_rewind(A) produces a version of A for which all accesses are O(1).
+    % Invoking this predicate renders A and all later versions undefined that
+    % were derived by performing individual updates. Only use this when you are
+    % absolutely certain there are no live references to A or later versions
+    % of A. (A predicate version is also provided.)
     %
 :- func unsafe_rewind(version_array(T)) = version_array(T).
 :- pred unsafe_rewind(version_array(T)::in, version_array(T)::out) is det.
@@ -250,15 +250,17 @@
 %-----------------------------------------------------------------------------%
 
 version_array([]) = version_array.empty.
-
-version_array([X | Xs]) =
-    version_array_2(1, Xs, version_array.init(1 + length(Xs), X)).
-
-:- func version_array_2(int, list(T), version_array(T)) = version_array(T).
-
-version_array_2(_, [],       VA) = VA.
-version_array_2(I, [X | Xs], VA) =
-    version_array_2(I + 1, Xs, VA ^ elem(I) := X).
+version_array([X | Xs]) = VA :-
+    VA0 = version_array.init(1 + list.length(Xs), X),
+    version_array_2(1, Xs, VA0, VA).
+
+:- pred version_array_2(int::in, list(T)::in,
+    version_array(T)::in, version_array(T)::out) is det.
+
+version_array_2(_, [], !VA).
+version_array_2(I, [X | Xs], !VA) :-
+    set(I, X, !VA),
+    version_array_2(I + 1, Xs, !VA).
 
 from_list(Xs) = version_array(Xs).
 
@@ -266,27 +268,29 @@
 
 :- pragma inline(version_array.elem/2).
 
-VA ^ elem(I) = X :-
+lookup(VA, I) = X :-
     ( if get_if_in_range(VA, I, X0) then
         X = X0
       else
-        out_of_bounds_error(I, max(VA), "version_array.elem")
+        out_of_bounds_error(I, max(VA), "version_array.lookup")
     ).
 
-lookup(VA, I) = VA ^ elem(I).
+VA ^ elem(I) =
+    lookup(VA, I).
 
 %-----------------------------------------------------------------------------%
 
 :- pragma inline(version_array.'elem :='/3).
 
-(VA0 ^ elem(I) := X) = VA :-
-    ( if set_if_in_range(VA0, I, X, VA1) then
-        VA = VA1
+set(I, X, !VA) :-
+    ( if set_if_in_range(I, X, !VA) then
+        true
       else
-        out_of_bounds_error(I, max(VA0), "version_array.'elem :='")
+        out_of_bounds_error(I, max(!.VA), "version_array.set")
     ).
 
-set(I, X, VA, VA ^ elem(I) := X).
+(VA0 ^ elem(I) := X) = VA :-
+    set(I, X, VA0, VA).
 
 %-----------------------------------------------------------------------------%
 
@@ -295,8 +299,10 @@
 %-----------------------------------------------------------------------------%
 
 copy(VA) =
-    ( if size(VA) = 0 then VA
-                      else resize(VA, size(VA), VA ^ elem(0))
+    ( if size(VA) = 0 then
+        VA
+    else
+        resize(VA, size(VA), lookup(VA, 0))
     ).
 
 %-----------------------------------------------------------------------------%
@@ -312,8 +318,10 @@
 :- func do_foldl_func(func(T1, T2) = T2, version_array(T1), T2, int, int) = T2.
 
 do_foldl_func(F, VA, Acc, Lo, Hi) =
-    ( if Lo < Hi then do_foldl_func(F, VA, F(VA ^ elem(Lo), Acc), Lo + 1, Hi)
-                 else Acc
+    ( if Lo < Hi then
+        do_foldl_func(F, VA, F(lookup(VA, Lo), Acc), Lo + 1, Hi)
+    else
+        Acc
     ).
 
 %-----------------------------------------------------------------------------%
@@ -334,7 +342,7 @@
 
 do_foldl_pred(P, VA, Lo, Hi, !Acc) :-
     ( if Lo < Hi then
-        P(VA ^ elem(Lo), !Acc),
+        P(lookup(VA, Lo), !Acc),
         do_foldl_pred(P, VA, Lo + 1, Hi, !Acc)
       else
         true
@@ -362,7 +370,7 @@
 
 do_foldl2(P, VA, Lo, Hi, !Acc1, !Acc2) :-
     ( if Lo < Hi then
-        P(VA ^ elem(Lo), !Acc1, !Acc2),
+        P(lookup(VA, Lo), !Acc1, !Acc2),
         do_foldl2(P, VA, Lo + 1, Hi, !Acc1, !Acc2)
       else
         true
@@ -375,8 +383,10 @@
 :- func do_foldr_func(func(T1, T2) = T2, version_array(T1), T2, int) = T2.
 
 do_foldr_func(F, VA, Acc, Hi) =
-    ( if 0 =< Hi then do_foldr_func(F, VA, F(VA ^ elem(Hi), Acc), Hi - 1)
-                 else Acc
+    ( if 0 =< Hi then
+        do_foldr_func(F, VA, F(lookup(VA, Hi), Acc), Hi - 1)
+    else
+        Acc
     ).
 
 %-----------------------------------------------------------------------------%
@@ -397,7 +407,7 @@
 
 do_foldr_pred(P, VA, I, !Acc) :-
     ( if I >= 0 then
-        P(VA ^ elem(I), !Acc),
+        P(lookup(VA, I), !Acc),
         do_foldr_pred(P, VA, I - 1, !Acc)
       else
         true
@@ -425,7 +435,7 @@
 
 do_foldr2(P, VA, I, !Acc1, !Acc2) :-
     ( if I >= 0 then
-        P(VA ^ elem(I), !Acc1, !Acc2),
+        P(lookup(VA, I), !Acc1, !Acc2),
         do_foldr2(P, VA, I - 1, !Acc1, !Acc2)
       else
         true
@@ -482,7 +492,7 @@
 
 eq_version_array_2(I, VAa, VAb) :-
     ( if I >= 0 then
-        VAa ^ elem(I) = VAb ^ elem(I),
+        lookup(VAa, I) = lookup(VAb, I),
         eq_version_array_2(I - 1, VAa, VAb)
       else
         true
@@ -513,7 +523,7 @@
     ( if I >= Size then
         R = (=)
       else
-        compare(R0, VAa ^ elem(I), VAb ^ elem(I)),
+        compare(R0, lookup(VAa, I), lookup(VAb, I)),
         (
             R0 = (=),
             cmp_version_array_2(I + 1, Size, VAa, VAb, R)
@@ -777,11 +787,11 @@
     }
 ").
 
-:- pred set_if_in_range(version_array(T)::in, int::in, T::in,
-    version_array(T)::out) is semidet.
+:- pred set_if_in_range(int::in, T::in,
+    version_array(T)::in, version_array(T)::out) is semidet.
 
 :- pragma foreign_proc("C",
-    set_if_in_range(VA0::in, I::in, X::in, VA::out),
+    set_if_in_range(I::in, X::in, VA0::in, VA::out),
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
         does_not_affect_liveness],
 "
@@ -789,7 +799,7 @@
 ").
 
 :- pragma foreign_proc("C#",
-    set_if_in_range(VA0::in, I::in, X::in, VA::out),
+    set_if_in_range(I::in, X::in, VA0::in, VA::out),
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
         does_not_affect_liveness],
 "
@@ -803,7 +813,7 @@
 ").
 
 :- pragma foreign_proc("Java",
-    set_if_in_range(VA0::in, I::in, X::in, VA::out),
+    set_if_in_range(I::in, X::in, VA0::in, VA::out),
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
         does_not_affect_liveness],
 "
@@ -1848,14 +1858,18 @@
 
 :- func version_array_to_doc_2(int, version_array(T)) = doc.
 
-version_array_to_doc_2(I, A) =
-    ( if I > version_array.max(A) then
+version_array_to_doc_2(I, VA) =
+    ( if I > version_array.max(VA) then
         str("")
       else
         docs([
-          format_arg(format(A ^ elem(I))),
-          ( if I = version_array.max(A) then str("") else group([str(", "), nl]) ),
-          format_susp((func) = version_array_to_doc_2(I + 1, A))
+            format_arg(format(lookup(VA, I))),
+            ( if I = version_array.max(VA) then
+                str("")
+            else
+                group([str(", "), nl])
+            ),
+            format_susp((func) = version_array_to_doc_2(I + 1, VA))
         ])
     ).
 
cvs diff: Diffing m4
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/appengine
cvs diff: Diffing samples/appengine/war
cvs diff: Diffing samples/appengine/war/WEB-INF
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/standalone_c
cvs diff: Diffing samples/concurrency
cvs diff: Diffing samples/concurrency/dining_philosophers
cvs diff: Diffing samples/concurrency/midimon
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/java_interface
cvs diff: Diffing samples/java_interface/java_calls_mercury
cvs diff: Diffing samples/java_interface/mercury_calls_java
cvs diff: Diffing samples/lazy_list
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/solver_types
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing ssdb
cvs diff: Diffing tests
cvs diff: Diffing tests/analysis
cvs diff: Diffing tests/analysis/ctgc
cvs diff: Diffing tests/analysis/excp
cvs diff: Diffing tests/analysis/ext
cvs diff: Diffing tests/analysis/sharing
cvs diff: Diffing tests/analysis/table
cvs diff: Diffing tests/analysis/trail
cvs diff: Diffing tests/analysis/unused_args
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/feedback
cvs diff: Diffing tests/feedback/mandelbrot
cvs diff: Diffing tests/feedback/mmc
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
Index: tests/hard_coded/version_array_test.exp
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/hard_coded/version_array_test.exp,v
retrieving revision 1.4
diff -u -b -r1.4 version_array_test.exp
--- tests/hard_coded/version_array_test.exp	17 May 2012 02:21:33 -0000	1.4
+++ tests/hard_coded/version_array_test.exp	2 Jul 2012 01:07:02 -0000
@@ -12,8 +12,8 @@
 7, 7, 7, 7, 9, 9, 9, 9, 9 (size 9)
  (sum 73)
 7, 7, 7, 7 (size 4)
-Found exception as expected: index_out_of_bounds("version_array.elem: index -1 not in range [0, 3]")
-Found exception as expected: index_out_of_bounds("version_array.\'elem :=\': index -1 not in range [0, 3]")
-Found exception as expected: index_out_of_bounds("version_array.\'elem :=\': index -1 not in range [0, 3]")
-Found exception as expected: index_out_of_bounds("version_array.elem: index -1 not in range [0, 3]")
-Found exception as expected: index_out_of_bounds("version_array.elem: index 4 not in range [0, 3]")
+Found exception as expected: index_out_of_bounds("version_array.lookup: index -1 not in range [0, 3]")
+Found exception as expected: index_out_of_bounds("version_array.set: index -1 not in range [0, 3]")
+Found exception as expected: index_out_of_bounds("version_array.set: index -1 not in range [0, 3]")
+Found exception as expected: index_out_of_bounds("version_array.lookup: index -1 not in range [0, 3]")
+Found exception as expected: index_out_of_bounds("version_array.lookup: index 4 not in range [0, 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/stm
cvs diff: Diffing tests/stm/orig
cvs diff: Diffing tests/stm/orig/stm-compiler
cvs diff: Diffing tests/stm/orig/stm-compiler/test1
cvs diff: Diffing tests/stm/orig/stm-compiler/test10
cvs diff: Diffing tests/stm/orig/stm-compiler/test2
cvs diff: Diffing tests/stm/orig/stm-compiler/test3
cvs diff: Diffing tests/stm/orig/stm-compiler/test4
cvs diff: Diffing tests/stm/orig/stm-compiler/test5
cvs diff: Diffing tests/stm/orig/stm-compiler/test6
cvs diff: Diffing tests/stm/orig/stm-compiler/test7
cvs diff: Diffing tests/stm/orig/stm-compiler/test8
cvs diff: Diffing tests/stm/orig/stm-compiler/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/stmqueue
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test10
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test11
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test9
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/trailing
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list