[erlang-questions] map elements in defined order
Richard A. O'Keefe
ok@REDACTED
Mon Oct 30 01:09:18 CET 2017
On 28/10/17 2:26 AM, Sverker Eriksson wrote:
> Yes. All Erlang terms, including maps, have a globally defined,
> implementation independent, total order.
>
> The reason, for this "surprising" order of maps, is the alternative of
> using the normal arithmetic order for keys is much worse.
>
> If we want 1 and 1.0 to be different keys (which we do as we use
> matching (=:=) to lookup keys)
> then we need to order 1 and 1.0, and normal arithmetic term ordering
> does not (1 == 1.0).
The fundamental mistake here is confusing *arithmetic* ordering
with *term* ordering. In *term* ordering, if X and Y are
behaviourally distinguishable then either X < Y or X > Y should
be true. Since integers and floats are behaviourally
distinguishable,
1> X = 1 bsl 53.
9007199254740992
2> Y = float(X).
9007199254740992.0
3> X == Y.
true
4> X+1 == Y+1.
false
it follows that either X < Y or Y > X should be true in
*term* (but not *arithmetic*) order.
Arithmetic order in Erlang has some seriously weird issues, like
you can find numbers X Y such that X - Y is 0.0 but X == Y is false.
>
>
> It has been discussed (more and less serious) to expose this
> "map-key-order" with operators like :<, :>, =:<, >:=.
Surely we deserve *some* sane ordering in Erlang?
Prolog had two sets of comparison operators: term ordering and
arithmetic ordering. There were *reasons* for that.
More information about the erlang-questions
mailing list