for review: typeclasses & tvar renaming

David Glen JEFFERY dgj at cs.mu.OZ.AU
Wed May 27 16:28:08 AEST 1998


Hi,

Could you please review this, Fergus?

BTW, could you please make sure you look hard at the changes to typecheck.m?
I feel that there may be a better way...

===============================================================================

Estimated hours taken: 15

Fix a bug where variable names were getting mixed up for certain introduced
instance method predicates.

compiler/check_typeclass.m:
	Calculate the variables names correctly (ie. get the renaming
	right).
compiler/typecheck.m:
	Make typecheck_info_get_final_info also rename the declared
	constraints for the predicate. After getting the final info, store
	the new versions of the arg types and the renamed constraints,
	regardless of whether we are doing type inference or not.

	Although the intention was originally that the var numbers would
	only be changed in typecheck_info_get_final_info when doing
	inference (since that is the only time that redundant type vars
	would be present), this is no longer the case. Preds introduced in
	typecheck.m can also have redundant type vars in the varset at the
	time of creation since they have the *class declarations's* tvarset.
	This meant that typecheck_info_get_final_info was getting rid of
	some variables, then updating the proofs, but not updating the
	constraints, arg types etc.


===============================================================================

Index: compiler/check_typeclass.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/check_typeclass.m,v
retrieving revision 1.8
diff -u -t -r1.8 check_typeclass.m
--- check_typeclass.m	1998/05/21 12:32:02	1.8
+++ check_typeclass.m	1998/05/22 07:01:21
@@ -390,7 +390,7 @@
         in, out) is det.
 
 handle_instance_method_overloading(ClassVars, 
-                InstanceTypes0, InstanceConstraints, InstanceVarSet,
+                InstanceTypes0, InstanceConstraints0, InstanceVarSet,
                 InstancePredName, InstancePredIds, InstancePredId, 
                 InstanceProcIds, Info0, Info) :-
 
@@ -401,20 +401,24 @@
         module_info_get_predicate_table(ModuleInfo, PredicateTable),
         predicate_table_get_preds(PredicateTable, PredTable),
 
+                % Rename the instance variables apart from the class variables
         varset__merge_subst(ArgTypeVars0, InstanceVarSet, ArgTypeVars,
                 RenameSubst),
-        term__apply_substitution_to_list(InstanceTypes0,
-                RenameSubst, InstanceTypes),
-        map__from_corresponding_lists(ClassVars,
-                InstanceTypes, TypeSubst),
-        term__apply_substitution_to_list(ArgTypes0,
-                TypeSubst, ArgTypes),
+        term__apply_substitution_to_list(InstanceTypes0, RenameSubst,
+                InstanceTypes),
+        apply_subst_to_constraints(RenameSubst, InstanceConstraints0,
+                InstanceConstraints),
+
+                % 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),
+        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.
-        apply_subst_to_constraints(RenameSubst, ClassContext0, ClassContext1),
         list__append(InstanceConstraints, ClassContext1, ClassContext),
 
         Info1 = instance_method_info(ModuleInfo, PredName, PredArity, 
Index: compiler/typecheck.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/typecheck.m,v
retrieving revision 1.238
diff -u -t -r1.238 typecheck.m
--- typecheck.m	1998/05/25 21:48:59	1.238
+++ typecheck.m	1998/05/27 06:05:24
@@ -493,8 +493,9 @@
                                 TypeCheckInfo4),
                 typecheck_check_for_ambiguity(whole_pred, HeadVars,
                                 TypeCheckInfo4, TypeCheckInfo5),
-                typecheck_info_get_final_info(TypeCheckInfo5, TypeVarSet, 
-                                InferredVarTypes0, InferredTypeConstraints,
+                typecheck_info_get_final_info(TypeCheckInfo5, Constraints,
+                                TypeVarSet, InferredVarTypes0,
+                                InferredTypeConstraints, RenamedOldConstraints,
                                 ConstraintProofs),
                 map__optimize(InferredVarTypes0, InferredVarTypes),
                 ClausesInfo = clauses_info(VarSet, ExplicitVarTypes,
@@ -503,14 +504,14 @@
                 pred_info_set_typevarset(PredInfo1, TypeVarSet, PredInfo2),
                 pred_info_set_constraint_proofs(PredInfo2, ConstraintProofs,
                         PredInfo3),
+                pred_info_set_arg_types(PredInfo3, TypeVarSet,
+                                ArgTypes, PredInfo4),
+                map__apply_to_list(HeadVars, InferredVarTypes, ArgTypes),
                 ( Inferring = no ->
-                        PredInfo = PredInfo3,
+                        pred_info_set_class_context(PredInfo4,
+                                RenamedOldConstraints, PredInfo),
                         Changed = no
                 ;
-                        map__apply_to_list(HeadVars, InferredVarTypes,
-                                ArgTypes),
-                        pred_info_set_arg_types(PredInfo3, TypeVarSet,
-                                ArgTypes, PredInfo4),
                         pred_info_get_class_context(PredInfo0,
                                 OldTypeConstraints),
                         pred_info_set_class_context(PredInfo4,
@@ -2583,13 +2584,15 @@
 
 %-----------------------------------------------------------------------------%
 
-:- pred typecheck_info_get_final_info(typecheck_info, tvarset, map(var, type),
-                list(class_constraint),
+:- pred typecheck_info_get_final_info(typecheck_info, list(class_constraint),
+                tvarset, map(var, type),
+                list(class_constraint), list(class_constraint),
                 map(class_constraint, constraint_proof)).
-:- mode typecheck_info_get_final_info(in, out, out, out, out) is det.
+:- mode typecheck_info_get_final_info(in, in, out, out, out, out, out) is det.
 
-typecheck_info_get_final_info(TypeCheckInfo, NewTypeVarSet, NewVarTypes,
-                NewTypeConstraints, NewConstraintProofs) :-
+typecheck_info_get_final_info(TypeCheckInfo, OldConstraints, NewTypeVarSet,
+                NewVarTypes, NewTypeConstraints, RenamedOldConstraints,
+                NewConstraintProofs) :-
         typecheck_info_get_type_assign_set(TypeCheckInfo, TypeAssignSet),
         ( TypeAssignSet = [TypeAssign | _] ->
                 type_assign_get_typevarset(TypeAssign, OldTypeVarSet),
@@ -2653,6 +2656,8 @@
                 map__from_corresponding_lists(Vars, NewTypes, NewVarTypes),
                 list__map(rename_class_constraint(TSubst), TypeConstraints,
                         NewTypeConstraints),
+                list__map(rename_class_constraint(TSubst), OldConstraints,
+                        RenamedOldConstraints),
                 ( map__is_empty(ConstraintProofs) ->
                         % optimize simple case
                         NewConstraintProofs = ConstraintProofs

===============================================================================


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