diff: fixes to typeclass pretty-printing

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 16 16:53:14 AEDT 1998


Estimated hours taken: 0.5

compiler/mercury_to_mercury.m:
	Tidy up the output of typeclass and instance declarations.
	Previously there were a number of minor problems (instance
	declarations not implemented, unnecessary parentheses,
	newlines in wrong places).

tests/misc_test/Mmakefile:
tests/misc_test/pretty_print.m:
tests/misc_test/pretty_print.exp:
	Add a test case for pretty-printing.

cvs diff  compiler/mercury_to_mercury.m tests/misc_tests/Mmakefile tests/misc_tests/pretty_print_test.exp tests/misc_tests/pretty_print_test.m
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.130
diff -u -r1.130 mercury_to_mercury.m
--- mercury_to_mercury.m	1998/01/29 13:31:41	1.130
+++ mercury_to_mercury.m	1998/02/16 05:40:38
@@ -374,7 +374,7 @@
 
 	output_class_methods(Methods),
 	
-	io__write_string("].\n").
+	io__write_string("\n].\n").
 mercury_output_item(instance(Constraints, ClassName, Types, Methods, 
 		VarSet), _) --> 
 	io__write_string(":- instance "),
@@ -401,7 +401,7 @@
 
 	output_instance_methods(Methods),
 	
-	io__write_string("].\n").
+	io__write_string("\n].\n").
 
 %-----------------------------------------------------------------------------%
 :- pred output_class_constraints(list(class_constraint), varset, 
@@ -430,52 +430,53 @@
 :- mode output_class_method(in, di, uo) is det.
 
 output_class_method(Method) -->
-	io__write_string("\t("),
+	io__write_string("\t"),
 	(
 		{ Method = pred(VarSet, Name, TypesAndModes, Detism, 
 			_Condition, ClassContext, Context) },
 		mercury_output_pred_decl(VarSet, Name, TypesAndModes, Detism,
-			pure, ClassContext, Context, "", "),\n(", "\n")
+			pure, ClassContext, Context, "", ",\n\t", "")
 	;
 		{ Method = func(VarSet, Name, TypesAndModes, TypeAndMode, 
 			Detism, _Condition, ClassContext, Context) },
 		mercury_output_func_decl(VarSet, Name, TypesAndModes,
 			TypeAndMode, Detism, pure, ClassContext, Context, 
-			"", "),\n(", "\n")
+			"", ",\n\t", "")
 	;
 		{ Method = pred_mode(VarSet, Name, Modes, Detism, 
 			_Condition, Context) },
 		mercury_output_pred_mode_decl_2(VarSet, Name, Modes, Detism,
-			Context, "", "\n")
+			Context, "", "")
 	;
 		{ Method = func_mode(VarSet, Name, Modes, Mode, 
 			Detism, _Condition, Context) },
 		mercury_output_func_mode_decl_2(VarSet, Name, Modes, 
-			Mode, Detism, Context, "", "\n")
-	),
-	io__write_char(')').
+			Mode, Detism, Context, "", "")
+	).
 
 :- pred output_instance_methods(instance_interface, io__state, io__state).
 :- mode output_instance_methods(in, di, uo) is det.
 
 output_instance_methods(Methods) -->
-	{ OutputMethod = lambda([Method::in, IO0::di, IO::uo] is det,
-		(
-			(
-				Method = func_instance(Name1, Name2, Arity),
-				io__write_string("func((", IO0, IO1)
-			;
-				Method = pred_instance(Name1, Name2, Arity),
-				io__write_string("pred((", IO0, IO1)
-			),
-			mercury_output_bracketed_sym_name(Name1, IO1, IO2),
-			io__write_string(")/", IO2, IO3),
-			io__write_int(Arity, IO3, IO4),
-			io__write_string(") is ", IO4, IO5),
-			mercury_output_bracketed_sym_name(Name2, IO5, IO)
-		)
-	) },
-	io__write_list(Methods, ",\n", OutputMethod).
+	io__write_list(Methods, ",\n", output_instance_method).
+
+:- pred output_instance_method(instance_method, io__state, io__state).
+:- mode output_instance_method(in, di, uo) is det.
+
+output_instance_method(Method) -->
+	io__write_char('\t'),
+	(
+		{ Method = func_instance(Name1, Name2, Arity) },
+		io__write_string("func(")
+	;
+		{ Method = pred_instance(Name1, Name2, Arity) },
+		io__write_string("pred(")
+	),
+	mercury_output_bracketed_sym_name(Name1),
+	io__write_string("/"),
+	io__write_int(Arity),
+	io__write_string(") is "),
+	mercury_output_bracketed_sym_name(Name2).
 
 %-----------------------------------------------------------------------------%
 
Index: tests/misc_tests/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/misc_tests/Mmakefile,v
retrieving revision 1.2
diff -u -r1.2 Mmakefile
--- Mmakefile	1998/01/25 08:44:30	1.2
+++ Mmakefile	1998/02/16 05:47:04
@@ -13,9 +13,13 @@
 	debugger_test debugger_test.m < debugger_test.inp \
 		> debugger_test.out 2>&1
 
+pretty_print_test.out: pretty_print_test.ugly
+	cp pretty_print_test.ugly pretty_print_test.out
+
 #-----------------------------------------------------------------------------#
 
 PROGS=	debugger_test
+OTHERS= mdemangle_test pretty_print_test
 
 MCFLAGS-debugger_test = --generate-trace
 
@@ -23,8 +27,8 @@
 
 DEPS=	$(PROGS:%=%.dep)
 DEPENDS=$(PROGS:%=%.depend)
-OUTS=	$(PROGS:%=%.out) mdemangle_test.out
-RESS=	$(PROGS:%=%.res) mdemangle_test.res
+OUTS=	$(PROGS:%=%.out) $(OTHERS:%=%.out)
+RESS=	$(PROGS:%=%.res) $(OTHERS:%=%.res)
 MODS=	$(PROGS:%=%.mod)
 
 #-----------------------------------------------------------------------------#
Index: tests/misc_tests/pretty_print_test.exp
===================================================================
RCS file: pretty_print_test.exp
diff -N pretty_print_test.exp
--- /dev/null	Mon Feb 16 16:51:12 1998
+++ pretty_print_test.exp	Mon Feb 16 16:49:50 1998
@@ -0,0 +1,38 @@
+:- module pretty_print_test.
+:- interface.
+:- import_module io.
+:- pred pretty_print_test:main(io__state, io__state).
+:- mode pretty_print_test:main(di, uo) is det.
+:- implementation.
+:- type (pretty_print_test:foobar)
+	--->	pretty_print_test:foo
+	;	pretty_print_test:bar(int)
+	;	pretty_print_test:baz(int, int).
+:- type (pretty_print_test:cont(T))
+	--->	pretty_print_test:foo
+	;	pretty_print_test:cont(T, cont(T)).
+:- type (pretty_print_test:eq(T1, T2)) == foobar.
+pretty_print_test:main(DCG_0, DCG_2) :-
+	io:write_int(type_num(42), DCG_0, DCG_1),
+	io:nl(DCG_1, DCG_2).
+:- typeclass pretty_print_test:numbered_type(T) where [
+	func pretty_print_test:type_num(T) = int,
+	func pretty_print_test:type_num_2(T) = int,
+	pred pretty_print_test:type_num_3(T, int),
+	mode pretty_print_test:type_num_3(in, out),
+	mode pretty_print_test:type_num_3(out, in),
+	pred pretty_print_test:type_num_4(T, int),
+	mode pretty_print_test:type_num_4(in, out)
+].
+:- instance (numbered_type(int)) where [
+	func(type_num/1) is foo_type_num,
+	func(type_num_2/1) is foo_type_num,
+	pred(type_num_3/2) is foo_type_num_p,
+	pred(type_num_4/2) is foo_type_num_p
+].
+:- func pretty_print_test:foo_type_num(int) = int.
+pretty_print_test:foo_type_num(V_1) = 42.
+:- pred pretty_print_test:foo_type_num_p(int, int).
+:- mode pretty_print_test:foo_type_num_p(in, out) is det.
+:- mode pretty_print_test:foo_type_num_p(out, in) is det.
+pretty_print_test:foo_type_num_p(V_1, 42).
Index: tests/misc_tests/pretty_print_test.m
===================================================================
RCS file: pretty_print_test.m
diff -N pretty_print_test.m
--- /dev/null	Mon Feb 16 16:51:12 1998
+++ pretty_print_test.m	Mon Feb 16 16:49:26 1998
@@ -0,0 +1,39 @@
+% XXX we should also test use of operators
+
+:- module pretty_print_test.
+:- interface.
+:- import_module io.
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- type foobar ---> foo ; bar(int) ; baz(int, int).
+:- type cont(T) ---> foo ; cont(T, cont(T)).
+:- type eq(T1, T2) == foobar.
+
+main --> io__write_int(type_num(42)), io__nl.
+
+:- typeclass numbered_type(T) where [
+        func type_num(T) = int,
+        func type_num_2(T) = int,
+        pred type_num_3(T, int),
+        mode type_num_3(in, out),
+        mode type_num_3(out, in),
+        pred type_num_4(T::in, int::out)
+].
+
+:- instance numbered_type(int) where [
+        func(type_num/1) is foo_type_num,
+        func(type_num_2/1) is foo_type_num,
+        pred(type_num_3/2) is foo_type_num_p,
+        pred(type_num_4/2) is foo_type_num_p
+].
+
+:- func foo_type_num(int) = int.
+foo_type_num(_) = 42.
+
+:- pred foo_type_num_p(int, int).
+:- mode foo_type_num_p(in, out) is det.
+:- mode foo_type_num_p(out, in) is det.
+foo_type_num_p(_, 42).
+
-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list