I want the expression (x-x) to be simplified to 0.
type aexpr =
| CstI of int
| Var of string
| Add of aexpr * aexpr
| Sub of aexpr * aexpr
| Mul of aexpr * aexpr;;
let rec simplify expr =
match expr with
| Add(CstI(n1), CstI(n2)) ->CstI(n1 + n2)
| Sub(CstI(n1), CstI(n2)) ->CstI(n1 - n2)
| Mul(CstI(n1), CstI(n2)) ->CstI(n1 * n2)
| Add(e, CstI(0)) -> simplify e
| Add(CstI(0), e) -> simplify e
| Sub(CstI(0), e) -> simplify e
| Sub(e, CstI(0)) -> simplify e
| Sub(Var(x1), Var(x2)) when x1.Equals(x2) -> CstI(0) // <- not working
| Mul(CstI(0), e) -> CstI(0)
| Mul(e, CstI(0)) -> CstI(0)
| Mul(CstI(1), e) -> simplify e
| Mul(e, CstI(1)) -> simplify e
| _ -> expr;;
This does not do it. I do not see what I am doing wrong. Hope you can help me :)
Edit: It compiles fine but it does not do anything. Ie.
let e = Mul(CstI(1), Add(CstI(4), Sub(Var("x"), Var("x"))));;
In f# interactive:
let result = simplify e;;
val result : aexpr = Add (CstI 4,Sub (Var "x",Var "x"))
Result should be CstI 4