

10 # Simplify(expr_IsList) <-- MapSingle("Simplify",expr);

15 # Simplify(Complex(_r,_i)) <-- Complex(Simplify(r),Simplify(i));

20 # Simplify((_xex) == (_yex)) <-- (Simplify(xex-yex) == 0);

20 # Simplify((_xex) > (_yex)) <-- (Simplify(xex-yex) > 0);
20 # Simplify((_xex) < (_yex)) <-- (Simplify(xex-yex) < 0);
20 # Simplify((_xex) >= (_yex)) <-- (Simplify(xex-yex) >= 0);
20 # Simplify((_xex) <= (_yex)) <-- (Simplify(xex-yex) <= 0);
20 # Simplify((_xex) !== (_yex)) <-- (Simplify(xex-yex) !== 0);

50 # Simplify(_expr) <-- MultiSimp(Eval(expr));


Eliminate(_var,_replace,_function) <-- Simplify(Subst(var,replace)function);
ExpandBrackets(_xx) <-- SimpExpand(SimpImplode(SimpFlatten(xx)));

Function("Flatten",{body,flattenoper})
[
  DoFlatten(body);
];

RuleBase("DoFlatten",{doflattenx});
UnFence("DoFlatten",1);

10 # DoFlatten(_doflattenx)_(Type(doflattenx)=flattenoper) <--
     Apply("Concat",MapSingle("DoFlatten",Tail(Listify(doflattenx))));
20 # DoFlatten(_doflattenx) <-- { doflattenx };


10 # UnFlatten({},_op,_identity) <-- identity;
20 # UnFlatten(list_IsList,_op,_identity) <--
     Apply(op,{Head(list),UnFlatten(Tail(list),op,identity)});


RuleBase("SimpAdd",{x,y});
RuleBase("SimpMul",{x,y});
RuleBase("SimpDiv",{x,y});


10 # SimpFlatten((_x)+(_y)) <-- SimpAdd(SimpFlatten(x),SimpFlatten(y));
10 # SimpFlatten((_x)-(_y)) <-- SimpAdd(SimpFlatten(x),SimpMul(-1,SimpFlatten(y)));
10 # SimpFlatten(    -(_y)) <-- SimpMul(-1,SimpFlatten(y));

10 # SimpFlatten((_x)*(_y)) <-- SimpMul(SimpFlatten(x),SimpFlatten(y));
10 # SimpFlatten((_x)/(_y)) <-- SimpDiv(SimpFlatten(x),SimpFlatten(y));
10 # SimpFlatten((_x)^(n_IsPositiveInteger)) <--
     SimpMul(SimpFlatten(x),SimpFlatten(x^(n-1)));

100 # SimpFlatten(_x) <--
[
  x;
];

10 # SimpExpand(SimpAdd(_x,_y)) <-- SimpExpand(x) + SimpExpand(y);
10 # SimpExpand(SimpMul(_x,_y)) <-- SimpExpand(x) * SimpExpand(y);
10 # SimpExpand(SimpDiv(_x,_y)) <-- SimpExpand(x) / SimpExpand(y);
20 # SimpExpand(_x) <-- x;

/* Distributed multiplication rule */
10 # SimpImplode(SimpMul(SimpAdd(_x,_y),_z)) <--
     SimpImplode(SimpAdd(SimpImplode(SimpMul(x,z)),
                 SimpImplode(SimpMul(y,z))));
10 # SimpImplode(SimpMul(_z,SimpAdd(_x,_y))) <--
     SimpImplode(SimpAdd(SimpImplode(SimpMul(z,x)),
                 SimpImplode(SimpMul(z,y))));
/* Distributed division rule  */
10 # SimpImplode(SimpDiv(SimpAdd(_x,_y),_z)) <--
     SimpImplode(SimpAdd(SimpImplode(SimpDiv(x,z)),
     SimpImplode(SimpDiv(y,z))));



20 # SimpImplode(SimpAdd(_x,_y)) <--
     SimpAdd(SimpImplode(x),SimpImplode(y));
20 # SimpImplode(SimpMul(_x,_y)) <--
     SimpMul(SimpImplode(x),SimpImplode(y));
20 # SimpImplode(SimpDiv(_x,_y)) <--
     SimpDiv(SimpImplode(x),SimpImplode(y));
30 # SimpImplode(_x) <-- x;


