diff: get rid of unnecessary tvars

David Glen JEFFERY dgj at cs.mu.OZ.AU
Mon Jun 22 15:38:22 AEST 1998


Hi,

How this, Fergus?


Estimated hours taken: 3

Get rid of unnecessary type variables when creating the auxiliary predicate
for an instance method. This avoids problems when typecheck.m gets rid of
unused tvars that it thinks were introduced by type inference.

library/varset.m:
        Add a predicate varset__squash which gets rid of unused variables.

compiler/check_typeclass.m:
        Call varset__squash to get rid of the unnecessary tvars.

compiler/typecheck.m:
        Undo a previous change which used all the info from
        typecheck_get_final_info even if we were only checking the types.
        Call varset__squash to evict unnecessary tvars.


cvs diff: Diffing .
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/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing bytecode
cvs diff: Diffing bytecode/test
cvs diff: Diffing compiler
Index: compiler/check_typeclass.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/check_typeclass.m,v
retrieving revision 1.10
diff -u -t -r1.10 check_typeclass.m
--- check_typeclass.m	1998/06/08 07:46:59	1.10
+++ check_typeclass.m	1998/06/22 05:01:13
@@ -379,7 +379,7 @@
                 Status, PredOrFunc, Context),
 
                 % Rename the instance variables apart from the class variables
-        varset__merge_subst(ArgTypeVars0, InstanceVarSet, ArgTypeVars,
+        varset__merge_subst(ArgTypeVars0, InstanceVarSet, ArgTypeVars1,
                 RenameSubst),
         term__apply_substitution_to_list(InstanceTypes0, RenameSubst,
                 InstanceTypes),
@@ -389,14 +389,23 @@
                 % Work out what the type variables are bound to for this
                 % instance, and update the class types appropriately.
         map__from_corresponding_lists(ClassVars, InstanceTypes, TypeSubst),
-        term__apply_substitution_to_list(ArgTypes0, TypeSubst, ArgTypes),
+        term__apply_substitution_to_list(ArgTypes0, TypeSubst, ArgTypes1),
         apply_subst_to_constraints(TypeSubst, ClassContext0, ClassContext1),
 
                 % Add the constraints from the instance declaration to the 
                 % constraints from the class method. This allows an instance
                 % method to have constraints on it which are part of the
                 % instance declaration as a whole.
-        list__append(InstanceConstraints, ClassContext1, ClassContext),
+        list__append(InstanceConstraints, ClassContext1, ClassContext2),
+
+                % Get rid of any unwanted type variables
+        term__vars_list(ArgTypes1, VarsToKeep),
+        varset__squash(ArgTypeVars1, VarsToKeep, ArgTypeVars, SquashSubst),
+        term__apply_variable_renaming_to_list(ArgTypes1, SquashSubst, 
+                ArgTypes),
+
+        list__map(apply_variable_renaming_to_constraint(SquashSubst),
+                ClassContext2, ClassContext),
 
                 % Introduce a new predicate which calls the implementation
                 % given in the instance declaration.
Index: compiler/type_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/type_util.m,v
retrieving revision 1.54
diff -u -t -r1.54 type_util.m
--- type_util.m	1998/05/25 21:48:57	1.54
+++ type_util.m	1998/06/22 05:02:05
@@ -182,6 +182,10 @@
         class_constraint).
 :- mode apply_subst_to_constraint(in, in, out) is det.
 
+:- pred apply_variable_renaming_to_constraint(map(var, var), 
+        class_constraint, class_constraint).
+:- mode apply_variable_renaming_to_constraint(in, in, out) is det.
+
 % strip out the term__context fields, replacing them with empty
 % term__contexts (as obtained by term__context_init/1)
 % in a type or list of types
@@ -745,6 +749,12 @@
         Constraint0 = constraint(ClassName, Types0),
         term__apply_substitution_to_list(Types0, Subst, Types),
         Constraint  = constraint(ClassName, Types).
+
+apply_variable_renaming_to_constraint(Renaming, Constraint0, Constraint) :-
+        Constraint0 = constraint(ClassName, ClassArgTypes0),
+        term__apply_variable_renaming_to_list(ClassArgTypes0,
+                Renaming, ClassArgTypes),
+        Constraint = constraint(ClassName, ClassArgTypes).
 
 strip_term_contexts(Terms, StrippedTerms) :-
         list__map(strip_term_context, Terms, StrippedTerms).
Index: compiler/typecheck.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/typecheck.m,v
retrieving revision 1.241
diff -u -t -r1.241 typecheck.m
--- typecheck.m	1998/06/09 02:14:53	1.241
+++ typecheck.m	1998/06/16 09:04:05
@@ -2524,26 +2524,7 @@
                 % Next, create a new typevarset with the same number of
                 % variables. 
                 %
-                list__length(TypeVars, NumTypeVars),
-                varset__init(NewTypeVarSet0),
-                varset__new_vars(NewTypeVarSet0, NumTypeVars, 
-                        NewTypeVars0, NewTypeVarSet1),
-                %
-                % We need to sort the fresh variables, to
-                % ensure that the type substitution that we create below
-                % does not alter the relative ordering of the type variables
-                % (since that affects the order in which type_info
-                % parameters will be passed).
-                %
-                list__sort(NewTypeVars0, NewTypeVars),
-                %
-                % Copy the type variable names across from the old
-                % typevarset to the new typevarset.
-                %
-                varset__var_name_list(OldTypeVarSet, TypeVarNames),
-                map__from_corresponding_lists(TypeVars, NewTypeVars, TSubst),
-                copy_type_var_names(TypeVarNames, TSubst, NewTypeVarSet1,
-                        NewTypeVarSet),
+                varset__squash(OldTypeVarSet, TypeVars, NewTypeVarSet, TSubst),
                 %
                 % Finally, rename the types and type class constraints
                 % to use the new typevarset type variables.
@@ -2583,21 +2564,6 @@
         term__apply_rec_substitution(Type0, TypeSubst, Type),
         map__det_update(VarTypes0, Var, Type, VarTypes1),
         expand_types(Vars, TypeSubst, VarTypes1, VarTypes).
-
-:- pred copy_type_var_names(assoc_list(tvar, string), map(tvar, tvar),
-                                tvarset, tvarset).
-:- mode copy_type_var_names(in, in, in, out) is det.
-
-copy_type_var_names([], _TSubst, NewTypeVarSet, NewTypeVarSet).
-copy_type_var_names([OldTypeVar - Name | Rest], TypeSubst, NewTypeVarSet0,
-                        NewTypeVarSet) :-
-        ( map__search(TypeSubst, OldTypeVar, NewTypeVar) ->
-                varset__name_var(NewTypeVarSet0, NewTypeVar, Name,
-                        NewTypeVarSet1)
-        ;
-                NewTypeVarSet1 = NewTypeVarSet0
-        ),
-        copy_type_var_names(Rest, TypeSubst, NewTypeVarSet1, NewTypeVarSet).
 
 :- pred rename_class_constraint(map(tvar, tvar), class_constraint,
                                 class_constraint).
cvs diff: Diffing compiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/Togl-1.2
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/maze
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/references
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing library
Index: library/varset.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/varset.m,v
retrieving revision 1.58
diff -u -t -r1.58 varset.m
--- varset.m	1998/04/27 04:04:06	1.58
+++ varset.m	1998/06/22 04:57:35
@@ -151,6 +151,14 @@
 :- pred varset__select(varset, set(var), varset).
 :- mode varset__select(in, in, out) is det.
 
+        % Given a varset and a list of variables, construct a new varset
+        % containing one variable for each one in the list (and no others).
+        % Also return a substitution mapping the selected variables in the
+        % original varset into variables in the new varset. The relative
+        % ordering of variables in the original varset is maintained.
+:- pred varset__squash(varset, list(var), varset, map(var, var)).
+:- mode varset__squash(in, in, out, out) is det.
+
 %-----------------------------------------------------------------------------%
 
 :- implementation.
@@ -428,6 +436,43 @@
                 varset(Supply, VarNameMap, Values)) :-
         map__select(VarNameMap0, Vars, VarNameMap),
         map__select(Values0, Vars, Values).
+
+%-----------------------------------------------------------------------------%
+
+varset__squash(OldVarSet, KeptVars, NewVarSet, Subst) :-
+        %
+        % Create a new varset with the same number of variables. 
+        %
+        list__length(KeptVars, NumVars),
+        varset__init(NewVarSet0),
+        varset__new_vars(NewVarSet0, NumVars, 
+                NewVars0, NewVarSet1),
+        %
+        % We need to sort the fresh variables, to
+        % ensure that the substitution that we create below
+        % does not alter the relative ordering of the variables
+        %
+        list__sort(NewVars0, NewVars),
+
+        %
+        % Copy the variable names across from the old
+        % varset to the new varset.
+        %
+        varset__var_name_list(OldVarSet, VarNames),
+        map__from_corresponding_lists(KeptVars, NewVars, Subst),
+        copy_var_names(VarNames, Subst, NewVarSet1, NewVarSet).
+
+:- pred copy_var_names(assoc_list(var, string), map(var, var), varset, varset).
+:- mode copy_var_names(in, in, in, out) is det.
+
+copy_var_names([], _Subst, NewVarSet, NewVarSet).
+copy_var_names([OldVar - Name | Rest], Subst, NewVarSet0, NewVarSet) :-
+        ( map__search(Subst, OldVar, NewVar) ->
+                varset__name_var(NewVarSet0, NewVar, Name, NewVarSet1)
+        ;
+                NewVarSet1 = NewVarSet0
+        ),
+        copy_var_names(Rest, Subst, NewVarSet1, NewVarSet).
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
cvs diff: Diffing lp_solve
cvs diff: Diffing lp_solve/lp_examples
cvs diff: Diffing profiler
cvs diff: Diffing runtime
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing scripts
cvs diff: Diffing tools
cvs diff: Diffing trial
cvs diff: Diffing util


love and cuddles,
dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) |  Marge: Did you just call everyone "chicken"?
MEngSc student,                 |  Homer: Noooo.  I swear on this Bible!
Department of Computer Science  |  Marge: That's not a Bible; that's a book of
University of Melbourne         |         carpet samples!
Australia                       |  Homer: Ooooh... Fuzzy.



More information about the developers mailing list