-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplify.scm
139 lines (115 loc) · 4.27 KB
/
simplify.scm
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
;; (simplify '(+ 1 2 3 4 0 5)) -> '(+ 1 2 3 4 5)
;; (simplify '(+ (+ 0) (* 0) (+ 0 (* 1 (^ 3 4)) 0 (* 2 (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;; > (simplify '(+ (+ 0) (* 0) (+ 0 (* (* 1) (^ 3 4)) 0 (* 2 (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;; > (simplify '(+ (* (+ 0)) (* 0) (+ 0 (* (* 1) (^ 3 4)) 0 (* 2 (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;; > (simplify '(+ (* (+ 0)) (* 0) (+ 0 (* (* 1) (^ 3 4)) 0 (* (* 2 1) (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;; > (simplify '(* (+ (* (+ 0)) (* 0) (+ 0 (* (* 1) (^ 3 4)) 0 (* (* 2 1) (^ 3 2)) 0 (* 1 (^ 3 0))))))
;;'(+ (^ 3 4) (* 2 (^ 3 2)) 1)
(define (simplify expr)
(cond
((number? expr) expr)
((symbol? expr) expr)
((boolean? expr) expr)
(else
(cond
((is+? expr) (simplify+ expr))
((is^? expr) (simplify^ expr))
((is*? expr) (simplify* expr))
(else expr)))))
;; (simplify+ '(+ 1 2 3 4 0 5)) -> '(+ 1 2 3 4 5)
;;
;; (simplify+ '(+ 1 2 (/ 8 0) 3 4 0 5)) -> '(+ 1 2 (/ 8 0) 3 4 5)
;;
;; (simplify '(+ 0 (+ 0 (* 1 (^ 3 4)) 0 (* 2 (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;; > (simplify '(+ (+ 0) (+ 0 (* 1 (^ 3 4)) 0 (* 2 (^ 3 2)) 0 (* 1 (^ 3 0)))))
;; '(+ (^ 3 4) (* 2 (^ 3 2)) 1)
;;
(define (simplify+ expr)
;;(cond
;;((unary-operation? expr) (simplify (arg expr)))
;; ((binary-operation? expr)
;; (let ((a1-simp (simplify (arg1 expr)))
;; (a2-simp (simplify (arg2 expr))))
;; (cond ((zero-symb? a1-simp) a2-simp)
;; ((zero-symb? a2-simp) a1-simp)
;; (else `(,(operator expr) ,a1-simp ,a2-simp)))))
;;(else ;; n-arity operation
(let* ((a (args expr)) ;; arguments list
(a-simp (map simplify a)) ;; simplified arguments list
(a-not-null ;; not null arguments list
(filter
(lambda (x) (not (zero-symb? x))) ;; simplify .... + 0 + ... + 0 .....
a-simp)))
;;(dv a-not-null)
(cond
((null? a-not-null) 0)
((null? (rest a-not-null)) (first a-not-null))
(else `(,(operator expr) ,@a-not-null)))));;))
;; (simplify+ '(+ 1 x 0 y))
;; '(+ 1 x y)
;; > (simplify- '(- 1 x 0 y))
;; '(- 1 x y)
;; > (simplify- '(- 0 1 x 0 y))
;; '(- 0 1 x y)
;; >
(define (simplify- expr)
(let* ((a (args expr)) ;; arguments list
(a-simp (map simplify a)) ;; simplified arguments list
(a-not-null ;; not null arguments list
(filter
(lambda (x) (not (zero-symb? x))) ;; simplify .... - 0 - ... - 0 .....
a-simp)))
;;(dv a-not-null)
(cond
((null? a-not-null) 0)
((null? (rest a-not-null)) ;; (a1)
(if (zero-symb? (first a-simp)) ;; (- 0 a1)
(list - (first a-not-null)) ;; -> (- a1)
(first a-not-null))) ;; -> a1
((zero-symb? (first a-simp)) ;; (- 0 a1 ...)
`(,(operator expr) 0 ,@a-not-null)) ;; -> (- 0 a1 ...)
(else `(,(operator expr) ,@a-not-null))))) ;; -> (- a1 ...)
(define (simplify* expr)
;;(cond
;;((unary-operation? expr) (simplify (arg expr)))
;; ((binary-operation? expr)
;; (let ((a1-simp (simplify (arg1 expr)))
;; (a2-simp (simplify (arg2 expr))))
;; (cond ((zero-symb? a1-simp) 0)
;; ((zero-symb? a2-simp) 0)
;; ((unity-symb? a1-simp) a2-simp)
;; ((unity-symb? a2-simp) a1-simp)
;; (else `(,(operator expr) ,a1-simp ,a2-simp)))))
;;(else ;; n-arity operation
(let* ((a (args expr)) ;; arguments list
(a-simp (map simplify a)) ;; simplified arguments list
(a-not-one '()));; not one in arguments list
;;(dv a-not-one)
(if (member 0 a-simp)
0 ;; zero multiply somewhere
(begin
(set! a-not-one
(filter
(lambda (x) (not (unity-symb? x))) ;; simplify .... * 1 * ... * 1.....
a-simp))
(cond
((null? a-not-one) 1) ;; only ones
((null? (rest a-not-one)) (first a-not-one)) ;; only one element
(else `(,(operator expr) ,@a-not-one)))))));;))
(define (simplify^ expr)
(let ((a1-simp (simplify (arg1 expr)))
(a2-simp (simplify (arg2 expr))))
(cond ((zero-symb? a1-simp) a1-simp)
((zero-symb? a2-simp) 1)
((eqv? a2-simp 1) a1-simp)
(else `(,(operator expr) ,a1-simp ,a2-simp)))))
;; todo: associate ok + ko ^, n-arity evec + ?
;; > (prefix->infix '(+ a (+ b c)))
;; '(a + (b + c))
;; > (prefix->infix '(+ a b c)))
;; '(a + b + c)