diff: get rid of unnecessary tvars

David Glen JEFFERY dgj at cs.mu.OZ.AU
Fri Jun 19 14:19:04 AEST 1998


Hi,

Could you please review this, Fergus?

(Can you think of a better name than varset__squash?)

-------------------------------------------------------------------------------

Estimated hours taken: 3

Get rid of unnecessary type variables when creating the auxiliary predicate
for an instance method.

compiler/check_typeclass.m:
	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.

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


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/16 09:32:38
@@ -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,29 @@
                 % 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),
+                % XXX this should go in type_util.m
+        RenameConstraint = lambda([C0::in, C::out] is det,
+                (
+                        C0 = constraint(ClassName, ClassArgTypes0),
+                        term__apply_variable_renaming_to_list(ClassArgTypes0,
+                                SquashSubst, ClassArgTypes),
+                        C = constraint(ClassName, ClassArgTypes)
+                )),
+        list__map(RenameConstraint, ClassContext2, ClassContext),
 
                 % Introduce a new predicate which calls the implementation
                 % given in the instance declaration.
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/16 08:34:07
@@ -151,6 +151,13 @@
 :- 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 only those variables. Also return a substitution
+        % mapping the selected variables in the original varset into 
+        % variables in the new varset.
+:- pred varset__squash(varset, list(var), varset, map(var, var)).
+:- mode varset__squash(in, in, out, out) is det.
+
 %-----------------------------------------------------------------------------%
 
 :- implementation.
@@ -428,6 +435,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