[m-rev.] for review: deconstruct by functor number

Simon Taylor staylr at gmail.com
Fri Jan 5 13:02:24 AEDT 2007


On 02-Jan-2007, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> >library/construct.m:
> >library/term.m:
> >browser/term_rep.m:
> >	Add a function get_functor_lex, which returns the lexicographic
> >	functor number given an ordinal functor number.
> >
> >	Add equivalence types to make it clearer which ordering is
> >	being used by which functor numbers.
> >
> >	Remove a C-ism: num_functors now fails rather than returning -1
> >	for types without functors.
> 
> I suggest also add det_num_functors that does the same thing except
> that it throws an exception rather than failing.
 
Done.
 
> >Index: NEWS
> >===================================================================
> >RCS file: /home/mercury1/repository/mercury/NEWS,v
> >retrieving revision 1.433
> >diff -u -u -r1.433 NEWS
> >--- NEWS	21 Dec 2006 03:00:48 -0000	1.433
> >+++ NEWS	28 Dec 2006 12:45:54 -0000
> >@@ -40,6 +40,16 @@
> >* The type software_error/0 has been moved from the require module into
> >  the exception module.
> >
> >+* construct.num_functors now fails rather than returning -1 for types
> >+  with no functors.
> 
> Append the arity to num_functors.

Done.

Ralph Becket wrote:
> Taking this line, the documentation for num_functors should include this
> justification.  Julien and I spent about ten minutes trying to work this
> out from the source code, but couldn't find an explanation.

Done.

Simon.


diff -u NEWS NEWS
--- NEWS
+++ NEWS
@@ -43,11 +43,12 @@
 * The type software_error/0 has been moved from the require module into
   the exception module.
 
-* construct.num_functors now fails rather than returning -1 for types
-  with no functors.
+* construct.num_functors/1 now fails rather than returning -1 for types
+  with no functors.  There is a new function construct.det_num_functors/1
+  which aborts for types with no functors.
 
 * We have added predicates deconstruct.functor_number/3 and
-  deconstruct.deconstruct_du/4 which return functor numbers suiable
+  deconstruct.deconstruct_du/4 which return functor numbers suitable
   for use by construct.construct, rather than functor strings.
 
 * We have added a function construct.get_functor_lex/2 which converts
diff -u library/construct.m library/construct.m
--- library/construct.m
+++ library/construct.m
@@ -35,11 +35,17 @@
     % num_functors(TypeInfo).
     %
     % Returns the number of different functors for the top-level
-    % type constructor of the type specified by TypeInfo, or -1
+    % type constructor of the type specified by TypeInfo, or fail
     % if the type is not a discriminated union type.
     %
+    % deconstruct.functor_number/3, deconstruct.deconstruct_du/5
+    % and the semidet predicates and functions in this module will
+    % only succeed for types for which num_functors/1 succeeds.
+    %
 :- func num_functors(type_desc) = int is semidet.
 
+:- func det_num_functors(type_desc) = int.
+
     % get_functor(Type, FunctorNumber, FunctorName, Arity, ArgTypes).
     %
 
@@ -109,6 +115,8 @@
 
 :- implementation.
 
+:- import_module require.
+
 % For use by the Java and IL backends.
 %
 :- use_module rtti_implementation.
@@ -120,6 +128,13 @@
 
 ").
 
+det_num_functors(TypeInfo) =
+    ( if N = num_functors(TypeInfo)
+      then N
+      else func_error(
+                "construct.det_num_functors: type does not have functors")
+    ).
+
 :- pragma foreign_proc("C",
     num_functors(TypeInfo::in) = (Functors::out),
     [will_not_call_mercury, thread_safe, promise_pure],
only in patch2:
--- tests/valid/agc_unbound_typevars2.m	22 Mar 2006 02:56:44 -0000	1.2
+++ tests/valid/agc_unbound_typevars2.m	3 Jan 2007 01:21:57 -0000
@@ -44,5 +44,8 @@
 
 test_all(_T) -->
 	{ TypeInfo = type_of(poly_one([2399.3])) },
-	{ N = num_functors(TypeInfo) },
-	io__write_int(N).
+	( { N = num_functors(TypeInfo) } ->
+		io__write_int(N)
+	;
+		io__write_string("no functors")
+	).
only in patch2:
--- tests/valid/agc_unbound_typevars.m	22 Mar 2006 02:56:44 -0000	1.3
+++ tests/valid/agc_unbound_typevars.m	3 Jan 2007 01:20:50 -0000
@@ -32,7 +32,11 @@
 	TypeInfo = type_of([]), 
 	map__init(Map),
 	TypeInfo2 = type_of(Map), 
-	N = num_functors(TypeInfo),
-	M = num_functors(TypeInfo2),
-	X = N + M.
-
+	(
+		N = num_functors(TypeInfo),
+		M = num_functors(TypeInfo2)
+	->
+		X = N + M
+	;
+		X = -1
+	).
only in patch2:
--- extras/quickcheck/qcheck.m	30 Mar 2006 01:21:18 -0000	1.6
+++ extras/quickcheck/qcheck.m	2 Jan 2007 12:05:09 -0000
@@ -176,9 +176,9 @@
 	% If all tests pass, quickcheck will print a summary of the
 	% tests executed.
 	%
-	% Quickcheck makes the assumption that the distincation between
+	% Quickcheck makes the assumption that the distinction between
 	% a function and a discriminated union is that num_functors(FuncType) 
-	% is -1, while num_functors(UnionType) is not -1.
+	% fails for a function.
 	% As a result of this assumption, the function should not be called
 	% where qcheck needs to generate a type that is not supported by the
 	% default/custom generators.
@@ -796,7 +796,7 @@
 		   ;	Datatype = type_of("String") ->
 				Temp = rand_string(RS0, RS),
 				Univ = univ(Temp)
-		   ;	num_functors(Datatype) = -1 ->
+		   ;	\+ num_functors(Datatype) = _ ->
                                 Univ = rand_function(Datatype, RS0, RS)
 		   ; 
 		    		Univ = rand_union(Datatype, Frequencys, 
@@ -888,9 +888,9 @@
 		    [], RS0, RS),
 		string__from_char_list(Charlist,X).
 
-	% generate disciminated union
+	% generate discriminated union
 rand_union(Datatype, Frequencys, GF, UserGenerators, RS0, RS) = Univ :-
-		NumFunctors = num_functors(Datatype),
+		NumFunctors = det_num_functors(Datatype),
 		TempFreq = get_freq(Datatype, NumFunctors, 0, Frequencys, GF),
         	rnd__irange(0, freq_base(TempFreq) - 1, Selector, RS0, RS1),
 		{ Branch, SubBranch } = select_branch(Selector, 0, 0, TempFreq),  
--------------------------------------------------------------------------
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