for review: new typeclass method syntax

David Glen JEFFERY dgj at cs.mu.oz.au
Wed Jan 7 15:46:30 AEDT 1998


Hi,

Fergus, can you please review this?


Estimated hours taken: 1/2

A few minor changes, including fixing the syntax of a typeclass declaration.
The methods inside a typeclass declaration no longer have a ":-" at the start.

compiler/make_hlds.m:
	Give an error message is a typeclass is multiply defined.
compiler/prog_io.m:
	Export parse_decl, and make the output a maybe_item_and_context so
	that it is more useful to prog_io_typeclass.
compiler/prog_io_typeclass.m:
	Use parse_decl rather than parse_item to parse the class methods. This
	way the ":-" is left off.
compiler/notes/compiler_design.html:
	Document why check_typeclass is the final semantic analysis pass.


Index: compiler/make_hlds.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/make_hlds.m,v
retrieving revision 1.248
diff -u -r1.248 make_hlds.m
--- make_hlds.m	1997/12/22 09:55:55	1.248
+++ make_hlds.m	1998/01/05 03:37:08
@@ -1261,7 +1261,7 @@
 	di, uo) is det.
 
 module_add_instance_defn(Module0, Constraints, Name, Types, Interface, VarSet,
-		Status, _Context, Module) -->
+		Status, Context, Module) -->
 	{ module_info_classes(Module0, Classes) },
 	{ module_info_instances(Module0, Instances0) },
 	{ list__length(Types, ClassArity) },
@@ -1277,8 +1277,16 @@
 			Instances) },
 		{ module_info_set_instances(Module0, Instances, Module) }
 	;
-			% XXX give an error since the class has not been
-			% XXX defined
+		io__stderr_stream(StdErr),
+		io__set_output_stream(StdErr, OldStream),
+		prog_out__write_context(Context),
+		io__write_string("Error: typeclass `"),
+		prog_out__write_sym_name(Name),
+		io__write_char('/'),
+		io__write_int(ClassArity),
+		io__write_string("' multiply defined.\n"),
+		io__set_exit_status(1),
+		io__set_output_stream(OldStream, _),
 		{ Module = Module0 }
 	).
 
Index: compiler/prog_io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io.m,v
retrieving revision 1.167
diff -u -r1.167 prog_io.m
--- prog_io.m	1997/12/19 03:07:55	1.167
+++ prog_io.m	1998/01/07 04:05:03
@@ -102,6 +102,15 @@
 :- pred parse_item(string, varset, term, maybe_item_and_context). 
 :- mode parse_item(in, in, in, out) is det.
 
+	% parse_decl(ModuleName, VarSet, Term, Result)
+	%
+	% parse Term as a declaration. If successful, Result is bound to the
+	% parsed item, otherwise it is bound to an appropriate error message.
+	% Qualify appropriate parts of the item, with ModuleName as the module
+	% name.
+:- pred parse_decl(string, varset, term, maybe_item_and_context).
+:- mode parse_decl(in, in, in, out) is det.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -427,11 +436,10 @@
 
 parse_item(ModuleName, VarSet, Term, Result) :-
  	( %%% some [Decl, DeclContext]
-		Term = term__functor(term__atom(":-"), [Decl], DeclContext)
+		Term = term__functor(term__atom(":-"), [Decl], _DeclContext)
 	->
 		% It's a declaration
-		parse_decl(ModuleName, VarSet, Decl, R),
-		add_context(R, DeclContext, Result)
+		parse_decl(ModuleName, VarSet, Decl, Result)
 	; %%% some [DCG_H, DCG_B, DCG_Context]
 		% It's a DCG clause
 		Term = term__functor(term__atom("-->"), [DCG_H, DCG_B],
@@ -494,18 +502,14 @@
 
 %-----------------------------------------------------------------------------%
 
-	% parse a declaration
-
-:- pred parse_decl(string, varset, term, maybe1(item)).
-:- mode parse_decl(in, in, in, out) is det.
 parse_decl(ModuleName, VarSet, F, Result) :-
 	( 
-		F = term__functor(term__atom(Atom), As, _Context)
+		F = term__functor(term__atom(Atom), As, Context)
 	->
 		(
 			process_decl(ModuleName, VarSet, Atom, As, R)
 		->
-			Result = R
+			add_context(R, Context, Result)
 		;
 			Result = error("unrecognized declaration", F)
 		)
Index: compiler/prog_io_typeclass.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io_typeclass.m,v
retrieving revision 1.1
diff -u -r1.1 prog_io_typeclass.m
--- prog_io_typeclass.m	1997/12/19 03:07:59	1.1
+++ prog_io_typeclass.m	1998/01/07 04:02:05
@@ -151,7 +151,7 @@
 		list__map(lambda([MethodTerm::in, Method::out] is det, 
 			(
 				% Turn the term into an item
-			parse_item(ModuleName, VarSet, MethodTerm, Item),
+			parse_decl(ModuleName, VarSet, MethodTerm, Item),
 				% Turn the item into a class_method
 			item_to_class_method(Item, MethodTerm, Method)
 			)),
Index: compiler/notes/compiler_design.html
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/notes/compiler_design.html,v
retrieving revision 1.13
diff -u -r1.13 compiler_design.html
--- compiler_design.html	1998/01/02 00:11:10	1.13
+++ compiler_design.html	1998/01/05 03:37:34
@@ -313,12 +313,13 @@
 	check_typeclass.m checks that, each instance declaration, that the
 	types, modes and determinism of each predicate/function that is a
 	method of the class is correct (ie. that it matches the typeclass
-	declaration). In this pass, pred_ids and proc_ids are assigned to
-	the methods for each instance. In addition, while checking that the
-	superclasses of a class are satisfied by the instance declaration, a
-	set of constraint_proofs are built up for the superclass constraints.
-	These are used by polymorphism.m when generating the 
-	base_typeclass_info for the instance.
+	declaration). This pass is performed at the end of semantic analysis
+	because it needs mode and determinism information. In this pass,
+	pred_ids and proc_ids are assigned to the methods for each instance. In
+	addition, while checking that the superclasses of a class are satisfied
+	by the instance declaration, a set of constraint_proofs are built up 
+	for the superclass constraints. These are used by polymorphism.m when
+	generating the base_typeclass_info for the instance.
 
 <dt> simplification (simplify.m)
 



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