[m-rev.] for review: user-defined comparison

Simon Taylor stayl at cs.mu.OZ.AU
Thu Feb 13 00:48:05 AEDT 2003


On 12-Feb-2003, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 12-Feb-2003, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> > +A comparison predicate can also be supplied.
> > +
> > + at example
> > +:- type set(T) ---> set(list(T))
> > +        where equality is set_equals, comparison is set_compare.
> > +
> > +:- pred set_compare(comparison_result::uo, set(T)::in, set(T)::in) is det.
> > +set_compare(promise_only_solution(set_compare_2(Set1, Set2)), Set1, Set2).
> > +
> > +:- pred set_compare_2(set(T)::in, set(T)::in,
> > +                comparison_result::uo) is cc_multi.
> > +set_compare_2(set(List1), set(List2), Result) :-
> > +        compare(Result, list__sort(List1), list__sort(List2)).
> > + at end example
> 
> Here the code uses "compare" without defining it.
> What's "compare"?
> 
> (The code also uses `list__sort', but since that name is module-qualified,
> it's easier for the reader to guess that this is a predicate from
> another module, perhaps from the Mercury standard library.
> With `compare', there is no such clue.)

I've added the qualifier.

> > +A type declaration for a type @samp{foo(T1, @dots{}, TN)} may contain a
> > + at samp{where comparison is @var{comparepred}} specification only it declares
> 
> s/only it/only if it/

Done.

> > +The semantics [...] are as follows:
> ...
> > +Any comparisons of type @var{T} are computed using the specified predicate
> > + at var{comparepred}.
> 
> Any comparisons?  What's a comparison?  When do they occur?
> 
> I think this needs to be spelt out a lot more clearly.
> 
> Remember that the reader will probably read the language reference manual
> before the library reference manual, so you can't assume that the reader
> knows anything about the Mercury standard library.

--- reference_manual.texi	2003/02/12 07:38:42	1.2
+++ reference_manual.texi	2003/02/12 13:46:16
@@ -3390,13 +3390,14 @@
 :- type set(T) ---> set(list(T))
         where equality is set_equals, comparison is set_compare.
 
-:- pred set_compare(comparison_result::uo, set(T)::in, set(T)::in) is det.
+:- pred set_compare(builtin__comparison_result::uo,
+                set(T)::in, set(T)::in) is det.
 set_compare(promise_only_solution(set_compare_2(Set1, Set2)), Set1, Set2).
 
 :- pred set_compare_2(set(T)::in, set(T)::in,
-                comparison_result::uo) is cc_multi.
+                builtin__comparison_result::uo) is cc_multi.
 set_compare_2(set(List1), set(List2), Result) :-
-        compare(Result, list__sort(List1), list__sort(List2)).
+        builtin__compare(Result, list__sort(List1), list__sort(List2)).
 @end example
 
 If a comparison predicate is supplied and the unification predicate
@@ -3414,8 +3415,8 @@
 exception of type @samp{require__software_error} when called.
 
 A type declaration for a type @samp{foo(T1, @dots{}, TN)} may contain a
- at samp{where equality is @var{equalitypred}} specification only it declares
-a discriminated union type or a foreign type
+ at samp{where equality is @var{equalitypred}} specification only if it
+declares a discriminated union type or a foreign type
 (@pxref{Using foreign types from Mercury}) and the
 following conditions are satisfied:
 
@@ -3449,45 +3450,6 @@
 
 @end itemize
 
-A type declaration for a type @samp{foo(T1, @dots{}, TN)} may contain a
- at samp{where comparison is @var{comparepred}} specification only it declares
-a discriminated union type or a foreign type
-(@pxref{Using foreign types from Mercury}) and the
-following conditions are satisfied:
-
- at itemize @bullet
- at item
- at var{comparepred} must be the name of a predicate with signature
- at example
-:- pred @var{comparepred}(comparison_result::uo, foo(T1, @dots{}, TN)::in,
-                foo(T1, @dots{}, TN)::in) is det.
- at end example
-
-As with equality predicates, it is legal for the type, mode and
-determinism to be more permissive.
-
- at item
-The comparison predicate must also be ``pure'' (@pxref{Impurity}).
-
- at item
-The relation
- at example
-compare_eq(X, Y) :- @var{comparepred}((=), X, Y).
- at end example
-must be an equivalence relation; that is, it must be symmetric,
-reflexive, and transitive.  The compiler is not required to check this.
-
- at item
-The relations
- at example
-compare_leq(X, Y) :- @var{comparepred}(R, X, Y), (R = (=) ; R = (<)).
-compare_geq(X, Y) :- @var{comparepred}(R, X, Y), (R = (=) ; R = (>)).
- at end example
-must be total order relations: that is they must be antisymmetric,
-reflexive and transitive.  The compiler is not required to check this.
-
- at end itemize
-
 Types with user-defined equality can only be used in limited ways.
 Because there multiple representations for the same abstract
 value, any attempt to examine the representation of such a value
@@ -3524,11 +3486,51 @@
 Any @samp{(in, in)} unifications for type @var{T} are computed using the
 specified predicate @var{equalitypred}.
 
+ at end itemize
+
+A type declaration for a type @samp{foo(T1, @dots{}, TN)} may contain a
+ at samp{where comparison is @var{comparepred}} specification only it declares
+a discriminated union type or a foreign type
+(@pxref{Using foreign types from Mercury}) and the
+following conditions are satisfied:
+
+ at itemize @bullet
 @item
-Any comparisons of type @var{T} are computed using the specified predicate
- at var{comparepred}.
+ at var{comparepred} must be the name of a predicate with signature
+ at example
+:- pred @var{comparepred}(builtin__comparison_result::uo,
+                foo(T1, @dots{}, TN)::in, foo(T1, @dots{}, TN)::in) is det.
+ at end example
+
+As with equality predicates, it is legal for the type, mode and
+determinism to be more permissive.
+
+ at item
+The comparison predicate must also be ``pure'' (@pxref{Impurity}).
+
+ at item
+The relation
+ at example
+compare_eq(X, Y) :- @var{comparepred}((=), X, Y).
+ at end example
+must be an equivalence relation; that is, it must be symmetric,
+reflexive, and transitive.  The compiler is not required to check this.
+
+ at item
+The relations
+ at example
+compare_leq(X, Y) :- @var{comparepred}(R, X, Y), (R = (=) ; R = (<)).
+compare_geq(X, Y) :- @var{comparepred}(R, X, Y), (R = (=) ; R = (>)).
+ at end example
+must be total order relations: that is they must be antisymmetric,
+reflexive and transitive.  The compiler is not required to check this.
 
 @end itemize
+
+For each type for which the declaration has a
+ at samp{where comparison is @var{comparepred}} specification,
+any calls to @samp{builtin__compare/3} with arguments of that type
+are evaluated as if they were calls to @var{comparepred}.
 
 @node Higher-order
 @chapter Higher-order programming
--------------------------------------------------------------------------
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