%mathpiper,def="+;SumListSide"

/* Addition */

100 # + x_  <-- x;

50 # x_Number? + y_Number? <-- AddN(x,y);

100 # 0 + x_    <-- x;
100 # x_ + 0    <-- x;
//100 # x_ + x_   <-- 2*x;
100 # x_ + n_Constant?*(x_)   <-- (n+1)*x;
100 # n_Constant?*(x_) + x_   <-- (n+1)*x;
//100 # Sinh(x_)+Cosh(x_)                <-- Exp(x);

101 # x_ + - y_ <-- x-y;
101 # x_ + (- y_)/(z_) <-- x-(y/z);
101 # (- y_)/(z_) + x_  <-- x-(y/z);
101 # (- x_) + y_ <-- y-x;
102 # x_ + y_NegativeNumber? <-- x-(-y);
102 # x_ + y_NegativeNumber? * z_ <-- x-((-y)*z);
102 # x_ + (y_NegativeNumber?)/(z_) <-- x-((-y)/z);
102 # (y_NegativeNumber?)/(z_) + x_  <-- x-((-y)/z);

102 # (x_NegativeNumber?) + y_ <-- y-(-x);
// fractions
150 # n1_ / d_ + n2_ / d_ <-- (n1+n2)/d;

//200 # (x_Number? + y_)::Not?(Number?(y)) <-- y+x;
//200 # ((y_ + x_Number?) + z_)::Not?(Number?(y) |? Number?(z)) <-- (y+z)+x;
//200 # ((x_Number? + y_) + z_Number?)::Not?(Number?(y)) <-- y+(x+z);
//200 # ((x_ + y_Number?) + z_Number?)::Not?(Number?(x)) <-- x+(y+z);

// fractions
210 # x_Number? + (y_Number? / z_Number?) <--(x*z+y)/z;
210 # (y_Number? / z_Number?) + x_Number? <--(x*z+y)/z;
210 # (x_Number? / v_Number?) + (y_Number? / z_Number?) <--(x*z+y*v)/(v*z);


//  220 # + x_List?          <-- MapSingle("+",x);        // this rule is never active

220 # (xlist_List? + ylist_List?)::(Length(xlist)=?Length(ylist)) <-- Map("+",[xlist,ylist]);

SumListSide(x_, y_List?) <--
{
    Local(i,result);

    Check(y !=? [], "An arithmetic operation can't be done with an empty list.");

    result:=[];

    For(i:=1,i<=?Length(y),i++)
    { 
        Insert!(result,i,x + y[i]); 
    }

    result;
}

240 # (x_List? + y_)::Not?(List?(y)) <-- SumListSide(y,x);
241 # (x_ + y_List?)::Not?(List?(x)) <-- SumListSide(x,y);

250 # z_Infinity? + Complex(x_,y_) <-- Complex(x+z,y);
250 # Complex(x_,y_) + z_Infinity? <-- Complex(x+z,y);

251 # z_Infinity? + x_ <-- z;
251 # x_ + z_Infinity? <-- z;


250 # Undefined + y_ <-- Undefined;
250 # x_ + Undefined <-- Undefined;

%/mathpiper





%mathpiper_docs,name="+",categories="Operators"
*CMD + --- arithmetic addition
*STD
*CALL
        x+y
        +x

*PARMS

{x} and {y} -- objects for which arithmetic addition is defined

*DESC

The addition operators can work on integers,
rational numbers, complex numbers, vectors, matrices and lists.

These operators are implemented in the standard math library (as opposed
to being built-in). This means that they can be extended by the user.

*E.G.

In> 2+3
Result: 5
%/mathpiper_docs