[m-rev.] for review: clarify polymorphic mode examples

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Jun 4 16:47:01 AEST 2007


Estimated hours taken: 3
Branches: main

Improve documentation of polymorphic modes and include more
tests in the test suite to the check parsing of declarations that
contain them.

doc/reference_manual.texi:
 	Indicate where the parentheses must be placed in order to resolve
 	operator precedence issues with predicate mode declarations that
 	contain inst constraints.  Also indicate where the determinism
 	component must occur in such declarations w.r.t the inst
 	constraints.

 	Add determinism components to the mode declarations that are used
 	as example of constrained polymorphic insts.

library/bitmap.m:
 	Unrelated change: conform to our coding standard w.r.t comment
 	positioning in the standard library.

tests/valid/Mmakefile:
tests/valid/constr_inst_syntax.m:
 	Further test cases for constrained polymorphic modes; in
 	particular check that parsing of declarations containing
 	both type class and inst constraints works.

Julien.

Index: doc/reference_manual.texi
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.394
diff -u -r1.394 reference_manual.texi
--- doc/reference_manual.texi	1 Jun 2007 12:54:12 -0000	1.394
+++ doc/reference_manual.texi	4 Jun 2007 06:45:09 -0000
@@ -2854,7 +2854,7 @@
  For example, in the mode declaration
  @example
  	:- mode append(in(list_skel(I =< ground)), in(list_skel(I =< ground)),
-	        out(list_skel(I =< ground))).
+	        out(list_skel(I =< ground))) is det.
  @end example
  @noindent
  @code{I} is an inst parameter which is constrained to be ground. 
@@ -2864,7 +2864,7 @@
  If the mode of append had been simply
  @example
  	:- mode append(in(list_skel(ground)), in(list_skel(ground)),
-	        out(list_skel(ground))).
+	        out(list_skel(ground))) is det.
  @end example
  @noindent
  then we would only have been able to infer an inst of @samp{list_skel(ground)}
@@ -2879,15 +2879,20 @@
  following a @samp{<=}.
  E.g.@: the above example could have been written as
  @example
-	:- mode append(in(list_skel(I)), in(list_skel(I)), out(list_skel(I)))
-	              <= I =< ground.
+	:- (mode append(in(list_skel(I)), in(list_skel(I)),
+                out(list_skel(I))) is det) <= I =< ground.
  @end example

+Note that in the current Mercury implementation this syntax requires
+parentheses around the @samp{mode(@dots{}) is @var{Det}} part of the
+declaration.
+
  Also, if the constraint on an inst parameter is @samp{ground} then it
  is not necessary to give the constraint in the declaration.
  The example can be further shortened to
  @example
-	:- mode append(in(list_skel(I)), in(list_skel(I)), out(list_skel(I))).
+	:- mode append(in(list_skel(I)), in(list_skel(I)), out(list_skel(I)))
+                is det.
  @end example


Index: library/bitmap.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/bitmap.m,v
retrieving revision 1.20
diff -u -r1.20 bitmap.m
--- library/bitmap.m	30 May 2007 02:47:07 -0000	1.20
+++ library/bitmap.m	4 Jun 2007 06:45:09 -0000
@@ -78,6 +78,7 @@
  :- mode new(in, in) = bitmap_uo is det.

      % Same as new(N, no).
+    %
  :- func new(num_bits) = bitmap.
  :- mode new(in) = bitmap_uo is det.

@@ -333,6 +334,7 @@

      % Convert a bitmap to a string of `1' and `0' characters, where
      % the bytes are separated by `.'.
+    %
  :- func to_byte_string(bitmap) = string.
  %:- mode to_byte_string(bitmap_ui) = out is det.
  :- mode to_byte_string(in) = out is det.
Index: tests/valid/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/valid/Mmakefile,v
retrieving revision 1.188
diff -u -r1.188 Mmakefile
--- tests/valid/Mmakefile	30 May 2007 23:44:17 -0000	1.188
+++ tests/valid/Mmakefile	4 Jun 2007 06:45:09 -0000
@@ -64,6 +64,7 @@
  	common_struct_bug \
  	complicated_unify \
  	compl_unify_bug \
+	constr_inst_syntax \
  	constrained_poly_bound_arg \
  	constrained_poly_insts \
  	constraint_prop_bug \
Index: tests/valid/constr_inst_syntax.m
===================================================================
RCS file: tests/valid/constr_inst_syntax.m
diff -N tests/valid/constr_inst_syntax.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/valid/constr_inst_syntax.m	4 Jun 2007 06:45:09 -0000
@@ -0,0 +1,60 @@
+:- module constr_inst_syntax.
+:- interface.
+
+:- typeclass foo(T) where [].
+:- typeclass bar(T) where [].
+:- instance bar(int).
+:- type list(T) ---> [] ; [ T | list(T) ].
+:- inst list_skel(I) ---> [] ; [ I | list_skel(I) ].
+
+	% The example from the reference manual.
+	%
+:- pred append(list(T), list(T), list(T)). 
+:- (mode append(in(list_skel(I)), in(list_skel(I)),
+	out(list_skel(I))) is det) <= I =< ground.
+
+	% Check that inst constraints can be combined with
+	% typeclass constraints in predmode decls.
+	%
+:- pred predmode_1(T::in(I), T::in(I), T::out(I))
+	is det <= (I =< any, foo(T)).
+
+	% A more complicated example using inst and type class
+	% constraints.
+	%
+:- some [U]
+   (impure pred predmode_2(T::in(I), U::out, T::out(I)) is det => bar(U))
+   <= (foo(T), I =< any).
+
+	% Multiple inst constraints.
+	%
+:- pred multi_inst_constrs(T, T, U, U).
+:- (mode multi_inst_constrs(in(I), out(I), in(J), out(J)) is det)
+	<= (I =< ground, J =< any).
+
+	% Inst constraints and functions.
+	%
+:- func id(T) = T.
+:- (mode id(in(I)) = out(I) is det) <= I =< ground.
+
+:- func id2(T::in(I), U::in(J)) = (T::out(I)) is det
+	<= (foo(T), I =< ground, J =< any).
+
+:- implementation.
+
+:- instance bar(int) where [].
+
+append([], Zs, Zs).
+append([ X | Xs ], Ys, [ X | Zs ]) :-
+	append(Xs, Ys, Zs).
+
+predmode_1(X, _, X).
+
+multi_inst_constrs(A, A, B, B).
+
+predmode_2(X, 3, X) :-
+	impure impure_true.
+
+id(X) = X.
+
+id2(X, _) = X.

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list