[E-Lang] Operators #5: Additive operators

Mark S. Miller markm@caplet.com
Fri, 06 Apr 2001 20:09:20 -0700


The next Python page 
http://www.python.org/doc/current/ref/sequence-methods.html seems 
irrelevant.  Lemme know if I'm missing something.

The following page http://www.python.org/doc/current/ref/numeric-types.html 
will be dealt with in several successive emails.

This email deals with the additive operators.  It would have us expand

    x + y  ->  x op__add(y)
    x - y  ->  x op__sub(y)
    -x      ->  x op__neg()



General meaning.   For "+", there are two "normal" categories of behavior, and one special exception.


"Closure Behavior" is as defined by the introductory message of this thread:

    For each data type T to which + applies (after coercions), + should 
    be closed over T, such that for any t1 and t2 in T, t1 + t2 should also be
    in T.  For any such type T, there must also be a zero element of that 
    type t0, such that t1 + t0 -> t1 and t0 + t1 -> t1.  + must also be 
    associative.

When members of such a T respond to all of op__add, op__sub, and op__neg, then 

    t1 op__neg() op__neg()  ->  t1
    t1 - t2  ->  t1 + -t2

* integers follow Closure behavior exactly.  The zero element is 0.

* float64s are understood in terms of closure behavior even though they 
violate it.  Floating point addition isn't even associative.  The float64 
operators are defined only in terms of IEEE, and only approximate the above 
definition.

* No other core datatypes are currently expected to exhibit Closure Behavior.



The other "normal" category is "Displacement Behavior", currently 
exemplified only by characters.  In displacement behavior, when t1 and t2 
are both elements of the type, and i is an integer,

    t1 + t2 is an error
    t1 + i  -> t2   iff   t2 - i  ->  t1   iff   t2 - t1 -> i
    If (t1 + i) would be out of range of T, it throws an exception
    t1 + 0 -> t1
    (t1 + i1) + i2  ==  t1 + (i1 + i2), unless (t1 + i1) is out of range.



The special exception is Strings.  For Strings only,

    str op__add(anything)

is equivalent to

    str | E toString(anything)

In other words, it evaluates to the receiving String appended with the 
argument printed to a String.  In previous email I misleadingly referred to 
this as a coercion of the argument to a String.  It isn't.  It's much more 
violent than a coercion normally is in E.  For example, if "s" is a String 
and "c" is a char, "s + c" does what it does in Java.  But "s | c" throws 
an exception, since c doesn't *coerce* to a String.


Of the core data types, these are the only ones that respond to the additive 
operators.  In particular, none of the collections other than String respond 
to these.

        Cheers,
        --MarkM