1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(datatype Math
(Diff Math Math)
(Integral Math Math)
(Add Math Math)
(Sub Math Math)
(Mul Math Math)
(Div Math Math)
(Pow Math Math)
(Ln Math)
(Sqrt Math)
(Sin Math)
(Cos Math)
(Const i64)
(Var String))
(relation MathU (Math))
(rule ((= e (Diff x y))) ((MathU e)))
(rule ((= e (Integral x y))) ((MathU e)))
(rule ((= e (Add x y))) ((MathU e)))
(rule ((= e (Sub x y))) ((MathU e)))
(rule ((= e (Mul x y))) ((MathU e)))
(rule ((= e (Div x y))) ((MathU e)))
(rule ((= e (Pow x y))) ((MathU e)))
(rule ((= e (Ln x))) ((MathU e)))
(rule ((= e (Sqrt x))) ((MathU e)))
(rule ((= e (Sin x))) ((MathU e)))
(rule ((= e (Cos x))) ((MathU e)))
(rule ((= e (Const x))) ((MathU e)))
(rule ((= e (Var x))) ((MathU e)))
(relation evals-to (Math i64))
(rule ((= e (Const c))) ((evals-to e c)))
(rule ((= e (Add a b)) (evals-to a va) (evals-to b vb))
((evals-to e (+ va vb))))
(rule ((= e (Sub a b)) (evals-to a va) (evals-to b vb))
((evals-to e (- va vb))))
(rule ((= e (Mul a b)) (evals-to a va) (evals-to b vb))
((evals-to e (* va vb))))
(rule ((= e (Div a b)) (evals-to a va) (evals-to b vb) (!= vb 0))
((evals-to e (/ va vb))))
(rule ((evals-to x vx)) ((union x (Const vx))))
(relation is-const (Math))
(rule ((evals-to a va)) ((is-const a)))
(relation is-sym (Math))
(rule ((= e (Var s))) ((is-sym e)))
(relation is-not-zero (Math))
(rule ((evals-to x vx)
(!= vx 0))
((is-not-zero x)))
(relation is-const-or-distinct-var-demand (Math Math))
(relation is-const-or-distinct-var (Math Math))
(rule ((is-const-or-distinct-var-demand v w)
(is-const v))
((is-const-or-distinct-var v w)))
(rule ((is-const-or-distinct-var-demand v w)
(= v (Var vv))
(= w (Var vw))
(!= vv vw))
((is-const-or-distinct-var v w)))
(rewrite (Add a b) (Add b a))
(rewrite (Mul a b) (Mul b a))
(rewrite (Add a (Add b c)) (Add (Add a b) c))
(rewrite (Mul a (Mul b c)) (Mul (Mul a b) c))
(rewrite (Sub a b) (Add a (Mul (Const -1) b)))
(rewrite (Div a b) (Mul a (Pow b (Const -1))) :when ((is-not-zero b)))
(rewrite (Add a (Const 0)) a)
(rewrite (Mul a (Const 0)) (Const 0))
(rewrite (Mul a (Const 1)) a)
;; NOTE: these two rules are different from math.rs, as math.rs does pruning
(rule ((MathU a) (!= a (Const 0))) ((union a (Add a (Const 0)))))
(rule ((MathU a) (!= a (Const 1))) ((union a (Mul a (Const 1)))))
(rewrite (Sub a a) (Const 0))
(rewrite (Div a a) (Const 1) :when ((is-not-zero a)))
(rewrite (Mul a (Add b c)) (Add (Mul a b) (Mul a c)))
(rewrite (Add (Mul a b) (Mul a c)) (Mul a (Add b c)))
(rewrite (Mul (Pow a b) (Pow a c)) (Pow a (Add b c)))
(rewrite (Pow x (Const 0)) (Const 1) :when ((is-not-zero x)))
(rewrite (Pow x (Const 1)) x)
(rewrite (Pow x (Const 2)) (Mul x x))
(rewrite (Pow x (Const -1)) (Div (Const 1) x) :when ((is-not-zero x)))
(rewrite (Mul x (Div (Const 1) x)) (Const 1) :when ((is-not-zero x)))
(rewrite (Diff x x) (Const 1) :when ((is-sym x)))
(rule ((= e (Diff x c))
(is-sym x))
((is-const-or-distinct-var-demand c x)))
(rewrite (Diff x c) (Const 0) :when ((is-sym x) (is-const-or-distinct-var c x)))
(rewrite (Diff x (Add a b)) (Add (Diff x a) (Diff x b)))
(rewrite (Diff x (Mul a b)) (Add (Mul a (Diff x b)) (Mul b (Diff x a))))
(rewrite (Diff x (Sin x)) (Cos x))
(rewrite (Diff x (Cos x)) (Mul (Const -1) (Sin x)))
(rewrite (Diff x (Ln x)) (Div (Const 1) x) :when ((is-not-zero x)))
(rewrite (Diff x (Pow f g))
(Mul (Pow f g)
(Add (Mul (Diff x f) (Div g f))
(Mul (Diff x g) (Ln f))))
:when ((is-not-zero f)
(is-not-zero g)))
(rewrite (Integral (Const 1) x) x)
(rewrite (Integral (Pow x c) x)
(Div (Pow x (Add c (Const 1))) (Add c (Const 1)))
:when ((is-const c)))
(rewrite (Integral (Cos x) x) (Sin x))
(rewrite (Integral (Sin x) x) (Mul (Const -1) (Cos x)))
(rewrite (Integral (Add f g) x) (Add (Integral f x) (Integral g x)))
(rewrite (Integral (Sub f g) x) (Sub (Integral f x) (Integral g x)))
(rewrite (Integral (Mul a b) x)
(Sub (Mul a (Integral b x))
(Integral (Mul (Diff x a) (Integral b x)) x)))
(let $start-expr2 (Add (Const 1)
(Sub (Var "a")
(Mul (Sub (Const 2)
(Const 1))
(Var "a")))))
(run 6)
(let $end-expr2 (Const 1))
(check (= $start-expr2 $end-expr2))
(extract $start-expr2)