[m-rev.] for review: typeclasses bugfix

Mark Brown mark at cs.mu.OZ.AU
Fri Mar 4 12:00:15 AEDT 2005


Estimated hours taken: 1.5
Branches: main, release

Fix a bug with the handling of ground constraints on function calls.

compiler/typecheck.m:
	When processing unifications, add constraints from matching
	constructors even if the varset is empty, since there may still
	be some ground constraints.

tests/hard_coded/typeclasses/Mmakefile:
tests/hard_coded/typeclasses/ground_constraint_2.exp:
tests/hard_coded/typeclasses/ground_constraint_2.m:
	A test case.

Index: compiler/typecheck.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/typecheck.m,v
retrieving revision 1.363
diff -u -r1.363 typecheck.m
--- compiler/typecheck.m	27 Jan 2005 03:38:10 -0000	1.363
+++ compiler/typecheck.m	3 Mar 2005 16:46:18 -0000
@@ -2708,7 +2708,8 @@
 	( varset__is_empty(ConsTypeVarSet) ->
 		ConsType = ConsType0,
 		ArgTypes = ArgTypes0,
-		TypeAssign = TypeAssign0
+		TypeAssign2 = TypeAssign0,
+		ConstraintsToAdd = ClassConstraints0
 	;
 		type_assign_rename_apart(TypeAssign0, ConsTypeVarSet,
 			[ConsType0 | ArgTypes0],
@@ -2718,33 +2719,29 @@
 			ConsExistQVars),
 		apply_subst_to_constraints(Subst, ClassConstraints0,
 			ConstraintsToAdd),
-		type_assign_get_typeclass_constraints(TypeAssign1,
-			OldConstraints),
-		%
-		% add the constraints for this functor
-		% to the current constraint set
-		% For functors which are data constructors,
-		% the fact that we don't take the dual
-		% corresponds to assuming that they
-		% will be used as deconstructors rather than as
-		% constructors.
-		%
-		add_constraints(OldConstraints, ConstraintsToAdd,
-			ClassConstraints),
-		type_assign_set_typeclass_constraints(ClassConstraints,
-			TypeAssign1, TypeAssign2),
-		type_assign_get_head_type_params(TypeAssign2,
-			HeadTypeParams0),
-		list__append(ConsExistQVars, HeadTypeParams0,
-			HeadTypeParams),
-		type_assign_set_head_type_params(HeadTypeParams,
-			TypeAssign2, TypeAssign),
+		type_assign_get_head_type_params(TypeAssign1, HeadTypeParams0),
+		list__append(ConsExistQVars, HeadTypeParams0, HeadTypeParams),
+		type_assign_set_head_type_params(HeadTypeParams, TypeAssign1,
+			TypeAssign2),
 
 		ConsType = ConsType1,
 		ArgTypes = ArgTypes1
 	;
 		error("get_cons_stuff: type_assign_rename_apart failed")
-	).
+	),
+		%
+		% Add the constraints for this functor to the current
+		% constraint set.  Note that there can still be (ground)
+		% constraints even if the varset is empty.
+		%
+		% For functors which are data constructors, the fact that we
+		% don't take the dual corresponds to assuming that they will
+		% be used as deconstructors rather than as constructors.
+		%
+	type_assign_get_typeclass_constraints(TypeAssign2, OldConstraints),
+	add_constraints(OldConstraints, ConstraintsToAdd, ClassConstraints),
+	type_assign_set_typeclass_constraints(ClassConstraints, TypeAssign2,
+		TypeAssign).
 
 	%
 	% compute the dual of a set of constraints:
Index: tests/hard_coded/typeclasses/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/typeclasses/Mmakefile,v
retrieving revision 1.52
diff -u -r1.52 Mmakefile
--- tests/hard_coded/typeclasses/Mmakefile	20 Feb 2004 06:01:42 -0000	1.52
+++ tests/hard_coded/typeclasses/Mmakefile	3 Mar 2005 14:37:38 -0000
@@ -20,6 +20,7 @@
 	extra_typeinfo \
 	func_default_mode_bug \
 	ground_constraint \
+	ground_constraint_2 \
 	ho_map \
 	implied_instance \
 	implied_instance_multi_constraint \
Index: tests/hard_coded/typeclasses/ground_constraint_2.exp
===================================================================
RCS file: tests/hard_coded/typeclasses/ground_constraint_2.exp
diff -N tests/hard_coded/typeclasses/ground_constraint_2.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/typeclasses/ground_constraint_2.exp	3 Mar 2005 14:33:28 -0000
@@ -0,0 +1 @@
+bar
Index: tests/hard_coded/typeclasses/ground_constraint_2.m
===================================================================
RCS file: tests/hard_coded/typeclasses/ground_constraint_2.m
diff -N tests/hard_coded/typeclasses/ground_constraint_2.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/typeclasses/ground_constraint_2.m	3 Mar 2005 14:32:20 -0000
@@ -0,0 +1,22 @@
+:- module ground_constraint_2.
+:- interface.
+:- import_module io.
+:- pred main(io__state::di, io__state::uo) is det.
+:- implementation.
+
+main -->
+	{ S1 = f(0) },
+	io__write_string(S1),
+	io__nl.
+
+:- typeclass foo(T) where [
+	func s(T) = string
+].
+
+:- instance foo(int) where [
+	(s(_) = "bar")
+].
+
+:- func f(int) = string <= foo(int).
+f(N) = s(N).
+
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list