[m-rev.] for review: repeated type class constraints

David Jeffery dgj at cs.mu.OZ.AU
Wed May 23 17:29:24 AEST 2001


For Fergus to review.

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

Estimated hours taken: 4

Fix a bug reported by Nick Nethercote. The symptom is this bug was that
the compiler aborted during the polymorphism transformation for predicate's
whose type contained a repeated type class constraint.

library/map.m:
	Add a new predicate map__set_from_corresponding lists which is like
	map__det_insert_from_corresponding_lists but uses map__set rather
	than map__det_insert. ie. it does not abort if a duplicate key
	is provided. Also add a pred map__set_from_assoc_list and function
	versions of these two new preds to be consistent with
	map__det_insert_from_corresponding_lists.

compiler/polymorphism.m:
	When building the initial type class info variable map, use
	map__set_from_corresponding rather than
	map__det_insert_from_corresponding_lists to take the case of repeated
	type class constraints into account.

tests/valid/Mmakefile:
tests/valid/repeated_class_constraint.m:
	A test case for this.

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

Index: compiler/polymorphism.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/polymorphism.m,v
retrieving revision 1.210
diff -u -t -r1.210 polymorphism.m
--- compiler/polymorphism.m	2001/04/29 18:21:32	1.210
+++ compiler/polymorphism.m	2001/05/23 07:19:58
@@ -882,7 +882,7 @@
 
         % Make a map of the locations of the typeclass_infos
         poly_info_get_typeclass_info_map(PolyInfo5, TypeClassInfoMap0),
-        map__det_insert_from_corresponding_lists(TypeClassInfoMap0,
+        map__set_from_corresponding_lists(TypeClassInfoMap0,
                 UnivConstraints, UnivHeadTypeClassInfoVars, TypeClassInfoMap),
         poly_info_set_typeclass_info_map(TypeClassInfoMap,
                 PolyInfo5, PolyInfo).
Index: library/map.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/map.m,v
retrieving revision 1.79
diff -u -t -r1.79 map.m
--- library/map.m	2000/12/13 00:00:40	1.79
+++ library/map.m	2001/05/23 06:20:17
@@ -105,6 +105,19 @@
 
 :- func map__det_insert_from_assoc_list(map(K,V), assoc_list(K, V)) = map(K,V).
 
+        % Apply map__set to key - value pairs from corresponding lists.
+:- pred map__set_from_corresponding_lists(map(K,V), list(K),
+                                                list(V), map(K,V)).
+:- mode map__set_from_corresponding_lists(in, in, in, out) is det.
+
+:- func map__set_from_corresponding_lists(map(K,V), list(K), list(V)) =
+                map(K,V).
+
+:- pred map__set_from_assoc_list(map(K,V), assoc_list(K, V), map(K,V)).
+:- mode map__set_from_assoc_list(in, in, out) is det.
+
+:- func map__set_from_assoc_list(map(K,V), assoc_list(K, V)) = map(K,V).
+
         % Update the value corresponding to a given key
         % Fail if the key doesn't already exist.
 :- pred map__update(map(K,V), K, V, map(K,V)).
@@ -467,6 +480,24 @@
         map__det_insert(Map0, K, V, Map1),
         map__det_insert_from_assoc_list(Map1, KVs, Map).
 
+map__set_from_corresponding_lists(Map0, Ks, Vs, Map) :-
+        ( Ks = [Key | Keys], Vs = [Value | Values] ->
+                map__set(Map0, Key, Value, Map1),
+                map__set_from_corresponding_lists(Map1, Keys,
+                                                        Values, Map)
+        ;
+                Ks = [], Vs = []
+        ->
+                Map = Map0
+        ;
+                error("map__set_from_corresponding_lists - lists do not correspond")
+        ).
+
+map__set_from_assoc_list(Map, [], Map).
+map__set_from_assoc_list(Map0, [K - V | KVs], Map) :-
+        map__set(Map0, K, V, Map1),
+        map__set_from_assoc_list(Map1, KVs, Map).
+
 map__update(Map0, K, V, Map) :-
         tree234__update(Map0, K, V, Map).
 
@@ -773,6 +804,12 @@
 
 map__det_insert_from_assoc_list(M1, AL) = M2 :-
         map__det_insert_from_assoc_list(M1, AL, M2).
+
+map__set_from_corresponding_lists(M1, Ks, Vs) = M2 :-
+        map__set_from_corresponding_lists(M1, Ks, Vs, M2).
+
+map__set_from_assoc_list(M1, AL) = M2 :-
+        map__set_from_assoc_list(M1, AL, M2).
 
 map__update(M1, K, V) = M2 :-
         map__update(M1, K, V, M2).
Index: tests/valid/Mmakefile
===================================================================
RCS file: /home/staff/zs/imp/tests/valid/Mmakefile,v
retrieving revision 1.86
diff -u -t -r1.86 Mmakefile
--- tests/valid/Mmakefile	2001/05/16 17:46:22	1.86
+++ tests/valid/Mmakefile	2001/05/23 07:00:56
@@ -25,6 +25,7 @@
         func_method.m \
         instance_superclass.m \
         instance_unconstrained_tvar.m \
+        repeated_class_constraint.m \
         typeclass_det_warning.m
 
 ADITI_SOURCES= \

New File: tests/valid/repeated_class_constraint.m
===================================================================
% Check that we handle repeated type class constraints on pred declarations

% We check this twice in this module: on the predicate 'p/1', and on the
% class method 'foo/2' which repeats the 'fooable/1' class constraint that 
% gets introduced by the compiler because it is a class method.

:- module repeated_class_constraint.

:- interface.

:- typeclass fooable(T) where
[
    pred foo(T,T) <= fooable(T),
    mode foo(in,out) is det
].

:- pred p(T) <= (fooable(T), fooable(T)).
:- mode p(in) is det.

:- implementation.

p(_).


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


dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If you want to build a ship, don't drum up 
PhD student,                    | people together to collect wood or assign 
Dept. of Comp. Sci. & Soft. Eng.| them tasks and work, but rather teach them 
The University of Melbourne     | to long for the endless immensity of the sea.
Australia                       | -- Antoine de Saint Exupery
--------------------------------------------------------------------------
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