

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);

// conditionals
25 # Simplify(if (_a) _b) <-- "if" @ {Simplify(a), Simplify(b)};
25 # Simplify(_a else _b) <-- "else" @ {Simplify(a), Simplify(b)};

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


Eliminate(_var,_replace,_function) <-- Simplify(Subst(var,replace)function);
//ExpandBrackets(_xx) <-- SimpExpand(SimpImplode(SimpFlatten(xx)));
ExpandBrackets(x) := NormalForm(MM(x));

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;


//////////////////////////////////////////////////
/// ExpandFrac --- normalize rational functions (no simplification)
//////////////////////////////////////////////////

5 # ExpandFrac(expr_IsList) <-- MapSingle("ExpandFrac", expr);

// expression does not contain fractions
10 # ExpandFrac(_expr)_Not(HasFuncSome(expr, "/", {Atom("+"), Atom("-"), *, /, ^})) <-- expr;
15 # ExpandFrac(a_IsRationalOrNumber) <-- a;
20 # ExpandFrac(_expr) <-- ExpandFrac'combine(GetNumerDenom(expr));

ExpandFrac'combine({_a, _b}) <-- a/b;

/// GetNumerDenom(x) returns a pair of expressions representing normalized numerator and denominator; GetNumerDenom(x, a) multiplies the numerator by the number a
GetNumerDenom(_expr, _a) <-- GetNumerDenom(expr)*{a,1};

// on expressions that are not fractions, we return unit denominator
10 # GetNumerDenom(_expr)_Not(HasFuncSome(expr, "/", {Atom("+"), Atom("-"), *, /, ^})) <-- {expr, 1};
// rational numbers are not simplified
15 # GetNumerDenom(a_IsRationalOrNumber) <-- {a, 1};
// arithmetic
20 # GetNumerDenom(_a + _b) <-- ExpandFrac'add(GetNumerDenom(a), GetNumerDenom(b));
20 # GetNumerDenom(_a - _b) <-- ExpandFrac'add(GetNumerDenom(a), GetNumerDenom(b, -1));
20 # GetNumerDenom(- _a) <-- GetNumerDenom(a, -1);
20 # GetNumerDenom(+ _a) <-- GetNumerDenom(a);
20 # GetNumerDenom(_a * _b) <-- ExpandFrac'multiply(GetNumerDenom(a), GetNumerDenom(b));
20 # GetNumerDenom(_a / _b) <-- ExpandFrac'divide(GetNumerDenom(a), GetNumerDenom(b));
// integer powers
20 # GetNumerDenom(_a ^ b_IsInteger)_(b > 1) <-- ExpandFrac'multiply(GetNumerDenom(a), GetNumerDenom(a^(b-1)));
20 # GetNumerDenom(_a ^ b_IsInteger)_(b < -1) <-- ExpandFrac'divide(GetNumerDenom(1), GetNumerDenom(a^(-b)));
20 # GetNumerDenom(_a ^ b_IsInteger)_(b = -1) <-- ExpandFrac'divide(GetNumerDenom(1), GetNumerDenom(a));
// non-integer powers are not considered to be rational functions
25 # GetNumerDenom(_a ^ _b) <-- {a^b, 1};

// arithmetic on fractions; not doing any simplification here, whereas we might want to
ExpandFrac'add({_a, _b}, {_c, _d}) <-- {a*d+b*c, b*d};
ExpandFrac'multiply({_a, _b}, {_c, _d}) <-- {a*c, b*d};
ExpandFrac'divide({_a, _b}, {_c, _d}) <-- {a*d, b*c};

