[m-rev.] for review: functional dependencies (1/3)

Mark Brown mark at cs.mu.OZ.AU
Wed Apr 20 22:41:27 AEST 2005


On 20-Apr-2005, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 20-Apr-2005, Mark Anthony BROWN <mark at cs.mu.OZ.AU> wrote:
> > Implement functional dependencies for the typeclass system.
> 
> That is a huge piece of work you've done there.  Great job!

Thanks!

> 
> > +++ NEWS	19 Apr 2005 14:32:41 -0000
> > @@ -1,6 +1,10 @@
> >  NEWS since the 0.12.0 fork:
> >  ----------------------------------
> >  
> > +Changes to the Mercury language:
> > +* We have added support for functional dependencies to the typeclass system.
> 
> It would be a good idea to add a pointer there to the appropriate
> section of the language reference manual which documents the new feature.

Done.

> 
> > +	% In the HLDS, functional dependencies are represented using
> > +	% argument positions (counting from 1) rather than type variables.
> > +	% We know that there will be a one-one correspondence since
> > +	% typeclass parameters must be distinct variables, and using
> > +	% argument positions is more efficient.
> > +	%
> > +:- type hlds_class_fundeps == list(hlds_class_fundep).
> > +:- type hlds_class_fundep
> > +	--->	fundep(
> > +			domain		:: set(int),
> > +			range		:: set(int)
> > +		).
> 
> You might want to consider using
> 
> 	:- type hlds_class_argpos == int.
> 
> to further document in the code what is already documented in the comment.
> It's not especially helpful here, but maybe there are other places in
> the code that use 'int' which could instead use this type synonym and
> hence be hyperlinked (via vim/emacs tags) to the definition of this type
> synonym and hence to the comment above.
> 

Good idea.

I've also fixed what would become a dangling pointer in the log message, once
it was committed.  Relative diff follows.

Cheers,
Mark.

--- log7	2005-04-20 22:05:11.000000000 +1000
+++ log	2005-04-20 22:05:21.000000000 +1000
@@ -16,7 +16,7 @@
 for the moment because the diff will be easier to review that way.  Moving
 to the new module will also remove the problem of one particular function
 being implemented in both typecheck and hlds_data, which is flagged by an XXX
-below.
+in the code.
 
 XXX the check for consistency of instances is not yet complete.  We check all
 visible instances, but not instances that are only present at link time.  We
diff -u NEWS NEWS
--- NEWS	19 Apr 2005 14:32:41 -0000
+++ NEWS	20 Apr 2005 12:08:22 -0000
@@ -3,6 +3,8 @@
 
 Changes to the Mercury language:
 * We have added support for functional dependencies to the typeclass system.
+  See the "Type classes" section of the Mercury Language Refence Manual for
+  details.
 
 Changes to the Mercury standard library:
 * We have added string.word_wrap/2.
diff -u compiler/check_typeclass.m compiler/check_typeclass.m
--- compiler/check_typeclass.m	19 Apr 2005 15:12:48 -0000
+++ compiler/check_typeclass.m	20 Apr 2005 12:17:12 -0000
@@ -1479,12 +1479,13 @@
 	io__set_exit_status(1, !IO).
 
 	% XXX this is duplicated in typecheck
-:- func restrict_list_elements(set(int), list(T)) = list(T).
+:- func restrict_list_elements(set(hlds_class_argpos), list(T)) = list(T).
 
 restrict_list_elements(Elements, List) =
         restrict_list_elements_2(Elements, 1, List).
 
-:- func restrict_list_elements_2(set(int), int, list(T)) = list(T).
+:- func restrict_list_elements_2(set(hlds_class_argpos), int, list(T)) =
+	list(T).
 
 restrict_list_elements_2(_, _, []) = [].
 restrict_list_elements_2(Elements, Index, [X | Xs]) =
diff -u compiler/hlds_data.m compiler/hlds_data.m
--- compiler/hlds_data.m	13 Apr 2005 08:44:55 -0000
+++ compiler/hlds_data.m	20 Apr 2005 12:10:26 -0000
@@ -833,10 +833,12 @@
 :- type hlds_class_fundeps == list(hlds_class_fundep).
 :- type hlds_class_fundep
 	--->	fundep(
-			domain		:: set(int),
-			range		:: set(int)
+			domain		:: set(hlds_class_argpos),
+			range		:: set(hlds_class_argpos)
 		).
 
+:- type hlds_class_argpos == int.
+
 :- type hlds_class_interface	==	list(hlds_class_proc).
 :- type hlds_class_proc
 	---> 	hlds_class_proc(
diff -u compiler/hlds_out.m compiler/hlds_out.m
--- compiler/hlds_out.m	11 Apr 2005 12:33:21 -0000
+++ compiler/hlds_out.m	20 Apr 2005 12:18:36 -0000
@@ -3471,7 +3471,8 @@
 	io.write_string(" >> ", !IO),
 	hlds_output_fundep_2(Range, !IO).
 
-:- pred hlds_output_fundep_2(set(int)::in, io::di, io::uo) is det.
+:- pred hlds_output_fundep_2(set(hlds_class_argpos)::in, io::di, io::uo)
+	is det.
 
 hlds_output_fundep_2(ArgNumSet, !IO) :-
 	ArgNumList = set.to_sorted_list(ArgNumSet),
diff -u compiler/intermod.m compiler/intermod.m
--- compiler/intermod.m	10 Apr 2005 15:17:56 -0000
+++ compiler/intermod.m	20 Apr 2005 12:19:00 -0000
@@ -1464,7 +1464,8 @@
 	Domain = unmake_hlds_class_fundep_2(TVars, Domain0),
 	Range = unmake_hlds_class_fundep_2(TVars, Range0).
 
-:- func unmake_hlds_class_fundep_2(list(tvar), set(int)) = list(tvar).
+:- func unmake_hlds_class_fundep_2(list(tvar), set(hlds_class_argpos)) =
+	list(tvar).
 
 unmake_hlds_class_fundep_2(TVars, Set) = solutions(P) :-
 	P = (pred(TVar::out) is nondet :-
diff -u compiler/make_hlds.m compiler/make_hlds.m
--- compiler/make_hlds.m	15 Apr 2005 15:46:09 -0000
+++ compiler/make_hlds.m	20 Apr 2005 12:19:50 -0000
@@ -3562,14 +3562,14 @@
 	Domain = make_hlds_fundep_2(TVars, Domain0),
 	Range = make_hlds_fundep_2(TVars, Range0).
 
-:- func make_hlds_fundep_2(list(tvar), list(tvar)) = set(int).
+:- func make_hlds_fundep_2(list(tvar), list(tvar)) = set(hlds_class_argpos).
 
 make_hlds_fundep_2(TVars, List) = list.foldl(Func, List, set.init) :-
 	Func = (func(TVar, Set0) = set.insert(Set0, N) :-
 		N = get_list_index(TVars, 1, TVar)
 	).
 
-:- func get_list_index(list(T), int, T) = int.
+:- func get_list_index(list(T), hlds_class_argpos, T) = hlds_class_argpos.
 
 get_list_index([], _, _) = _ :-
 	error("get_list_index: element not found").
diff -u compiler/typecheck.m compiler/typecheck.m
--- compiler/typecheck.m	20 Apr 2005 04:04:50 -0000
+++ compiler/typecheck.m	20 Apr 2005 12:21:09 -0000
@@ -4480,8 +4480,8 @@
 	% For each index in the set, check that the types in the corresponding
 	% positions in the lists are identical.
 	%
-:- pred lists_match_on_elements(set(int)::in, list(type)::in, list(type)::in)
-	is semidet.
+:- pred lists_match_on_elements(set(hlds_class_argpos)::in, list(type)::in,
+	list(type)::in) is semidet.
 
 lists_match_on_elements(Elements, TypesA, TypesB) :-
 	RTypesA = restrict_list_elements(Elements, TypesA),
@@ -4491,8 +4491,9 @@
 	% For each index in the set, unify the types in the corresponding
 	% positions in the lists and add to the current bindings.
 	%
-:- pred unify_on_elements(set(int)::in, list(type)::in, list(type)::in,
-	head_type_params::in, tsubst::in, tsubst::out) is semidet.
+:- pred unify_on_elements(set(hlds_class_argpos)::in, list(type)::in,
+	list(type)::in, head_type_params::in, tsubst::in, tsubst::out)
+	is semidet.
 
 unify_on_elements(Elements, TypesA, TypesB, HeadTypeParams, !Bindings) :-
 	RTypesA = restrict_list_elements(Elements, TypesA),
@@ -4502,8 +4503,8 @@
 	% Analogous to type_list_subsumes except that it only checks those
 	% elements of the list specified by the set of indices.
 	%
-:- pred subsumes_on_elements(set(int)::in, list(type)::in, list(type)::in,
-	tsubst::out) is semidet.
+:- pred subsumes_on_elements(set(hlds_class_argpos)::in, list(type)::in,
+	list(type)::in, tsubst::out) is semidet.
 
 subsumes_on_elements(Elements, TypesA, TypesB, Subst) :-
 	RTypesA = restrict_list_elements(Elements, TypesA),
@@ -4512,12 +4513,13 @@
 	map__init(Subst0),
 	type_unify_list(RTypesA, RTypesB, RTypesBVars, Subst0, Subst).
 
-:- func restrict_list_elements(set(int), list(T)) = list(T).
+:- func restrict_list_elements(set(hlds_class_argpos), list(T)) = list(T).
 
 restrict_list_elements(Elements, List) =
 	restrict_list_elements_2(Elements, 1, List).
 
-:- func restrict_list_elements_2(set(int), int, list(T)) = list(T).
+:- func restrict_list_elements_2(set(hlds_class_argpos), hlds_class_argpos,
+	list(T)) = list(T).
 
 restrict_list_elements_2(_, _, []) = [].
 restrict_list_elements_2(Elements, Index, [X | Xs]) =
--------------------------------------------------------------------------
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