[mercury-users] tuple comparison

Ralph Becket rafe at cs.mu.OZ.AU
Tue Mar 11 10:49:52 AEDT 2003


Simon Taylor, Monday, 10 March 2003:
> On 10-Mar-2003, Michael Day <mikeday at yeslogic.com> wrote:
> > Is a comparison of two tuples of ints always equivalent to comparing each 
> > int in turn, from left to right? ie:
> > 
> > compare(<, {1,2,3}, {4,5,6}),
> > compare(>, {4,5,6}, {4,4,4}),
> > compare(=, {1,2,3}, {1,2,3})
> 
> Yes.

The aspects of compare/3 that can be relied upon should be documented in
the reference manual:

compare(R, X, Y) :-

	     if X `with_type` int then R is determined by the usual
	integer ordering

	else if X `with_type` char then R is determined by the character
	encoding of the target platform

	else if X `with_type` string then R is determined as if by
	compare(R, string.to_char_list(X), string.to_char_list(Y))

	else if X `with_type` float then <<this can be subtle>>

	else if X `with_type` T and T has user-defined comparison, then
	R is determined by the user-defined comparison predicate for T

	else if X `with_type` T and T is higher order or foreign then an
	exception is thrown

	otherwise if X = F(A1, ..., AM) and Y = G(B1, ..., BN) then
	     if compare(RArity, M, N) and
	        RArity \= (=) then R = RArity
	else if compare(RFunctor, F as string, G as string) and
	        RFunctor \= (=) then R = RFunctor
	else if compare(R1, A1, B1) and
	        R1 \= (=) then R = R1
	else if compare(R2, A2, B2) and
	        R2 \= (=) then R = R2
	...
	else if compare(Rn, An, Bn) and
	        R = Rn

I'm too busy with the book to write this up properly - any takers?

	Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list