inconsistent definition of "sort" (and what about stability?)
Subject: inconsistent definition of "sort" (and what about stability?)
From: Oliver Kullmann
Date: Sat, 12 Nov 2011 21:19:55 +0000
Obviously, I am referring to the MAXIMA documentation:
-----------------------------------------------------------
(%i2) ? sort
-- Function: sort (<L>, <P>)
-- Function: sort (<L>)
Sorts a list <L> according to a predicate `P' of two arguments,
such that `<P> (<L>[k], <L>[k + 1])' is `true' for any two
successive elements. The predicate may be specified as the name
of a function or binary infix operator, or as a `lambda'
expression. If specified as the name of an operator, the name is
enclosed in "double quotes".
The sorted list is returned as a new object; the argument <L> is
not modified. To construct the return value, `sort' makes a
shallow copy of the elements of <L>.
If the predicate <P> is not a total order on the elements of <L>,
then `sort' might run to completion without error, but the result
is undefined. `sort' complains if the predicate evaluates to
something other than `true' or `false'.
`sort (<L>)' is equivalent to `sort (<L>, orderlessp)'. That is,
the default sorting order is ascending, as determined by
`orderlessp'. All Maxima atoms and expressions are comparable
under `orderlessp'.
The predicate `ordergreatp' sorts a list in descending order. The
predicate `ordermagnitudep' sorts Maxima numbers, constant symbols
with a numerical value, or expressions which can be evaluated to a
constant by magnitude. All other elements of the list <L> are
sorted by `orderlessp'. The predicate `"<"' allows the ordering
by magnitude too, but does not order completely if elements in the
list <L> are not comparable under `"<"'.
Examples:
(%i1) sort ([11, -17, 29b0, 7.55, 3, -5/2, b + a, 9 * c,
19 - 3 * x]);
5
(%o1) [- 17, - -, 3, 7.55, 11, 2.9b1, b + a, 9 c, 19 - 3 x]
2
(%i2) sort ([11, -17, 29b0, 7.55, 3, -5/2, b + a, 9*c, 19 - 3*x],
ordergreatp);
5
(%o2) [19 - 3 x, 9 c, b + a, 2.9b1, 11, 7.55, 3, - -, - 17]
2
(%i3) sort ([%pi, 3, 4, %e, %gamma]);
(%o3) [3, 4, %e, %gamma, %pi]
(%i4) sort ([%pi, 3, 4, %e, %gamma], "<");
(%o4) [%gamma, %e, 3, %pi, 4]
(%i5) my_list: [[aa,hh,uu], [ee,cc], [zz,xx,mm,cc], [%pi,%e]];
(%o5) [[aa, hh, uu], [ee, cc], [zz, xx, mm, cc], [%pi, %e]]
(%i6) sort (my_list);
(%o6) [[%pi, %e], [aa, hh, uu], [ee, cc], [zz, xx, mm, cc]]
(%i7) sort (my_list, lambda ([a, b], orderlessp (reverse (a),
reverse (b))));
(%o7) [[%pi, %e], [ee, cc], [zz, xx, mm, cc], [aa, hh, uu]]
Order Maxima numbers, constants, and constants expressions by
magnitude, and all other elements in ascending sorting order:
(%i8) sort([%i,1+%i,2*x,minf,inf,%e,sin(1),0,1,2,3,1.0,1.0b0],
ordermagnitudep);
(%o8) [minf, 0, sin(1), 1, 1.0, 1.0b0, 2, %e, 3, inf, %i,
%i + 1, 2 x]
-----------------------------
I must say I find it a bit strange, the tendency to assume that
the question must be wrong in some sense.
On Sat, Nov 12, 2011 at 01:14:19PM -0800, Steve Haflich wrote:
> Oliver Kullmann <O.Kullmann at swansea.ac.uk> wrote:
>
> while trying various Lisp's (not finished yet) a problem showed up which
> we had for a long time, but could ignore, due to just using
> one Lisp, namely what is the definition of "sort"?:
>
> I assume, since you are asking the Maxima list, you are concerned about
> some particular implementation that purports to be an implementation of
> ANSI Common Lisp. But it isn't at all clear to what documentation your
> message refers.
>
I was not speaking about Lisp, but about Maxima.
> ANSI Common Lisp has a well-done specification that covers most of these
> issues. It is available (in ugly pdf, and for a fee) from ANSI, but it
> is also available in a slightly earlier version (essentially the files
> that were submitted to ANSI, just before ANSI slapped their headers and
> footers and copyrights on it -- they are identical in all technical
> specifications) from various places like Franz and Lispworks. (The
> latter is the well-known Hyperspec.)
>
I do not know how Maxima-sort is implemented, and that is a question for
the Maxima-implementers, not for me.
>
> Your specific questions about sort are certainly reasonable questions,
> but you should read the ANS requirements first,
why should I do this?
> and then sEe whethere
> this particular implementation deviates in any significant way. IMO,
> Maxima should be written for the ANS specification except in the rare
> cases where implementation details beoome significant. These cases
> shuld be rare! Dependence on implementation details exposes Maxima to
> future contusions when that implementation changes out from under it.
Exactly! Thus Maxima needs precise specifications of the MAXIMA functions.
Oliver
--
Dr. Oliver Kullmann
Department of Computer Science
College of Science, Swansea University
Faraday Building, Singleton Park
Swansea SA2 8PP, UK
http://cs.swan.ac.uk/~csoliver/