-
Notifications
You must be signed in to change notification settings - Fork 5
/
sum-product-power.sls
149 lines (123 loc) · 4.63 KB
/
sum-product-power.sls
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
145
146
147
148
149
#!r6rs
(library (mpl sum-product-power)
(export + * ^
simplify-sum
simplify-product
simplify-power)
(import (rename (rnrs) (+ rnrs:+) (* rnrs:*))
(mpl match)
(dharmalab misc equivalence)
(dharmalab misc list)
(mpl order-relation))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (list-or-null-if-0 x)
(if (equal? x 0)
'()
(list x)))
(define (list-or-null-if-1 x)
(if (equal? x 1)
'()
(list x)))
(define any-are-zero? (any-are (equal-to 0)))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ^
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (raise-to expo)
(lambda (base)
(^ base expo)))
(define (^ v w)
(match (list v w)
((0 w) 0)
((1 w) 1)
((v 0) 1)
((v 1) v)
(((? number?) (? integer?)) (expt v w))
((('^ r s) (? integer?)) (^ r (* s w)))
((('* . vs) (? integer?)) (apply * (map (raise-to w) vs)))
(else `(^ ,v ,w) )))
(define (simplify-power u)
(^ (list-ref u 1)
(list-ref u 2)))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; *
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (merge-products p-elts q-elts)
(match (list p-elts q-elts)
( (() x) x )
( (x ()) x )
( ((p . ps) (q . qs))
(match (simplify-product-rec (list p q))
( () (merge-products ps qs) )
( (x) (cons x (merge-products ps qs)) )
( (? (equal-to (list p q))) (cons p (merge-products ps q-elts)) )
( (? (equal-to (list q p))) (cons q (merge-products p-elts qs)) )) )))
(define (simplify-product-rec elts)
(match elts
( (('* . p-elts) ('* . q-elts)) (merge-products p-elts q-elts) )
( (('* . p-elts) q) (merge-products p-elts (list q)) )
( (p ('* . q-elts)) (merge-products (list p) q-elts) )
( ((? number? p) (? number? q)) (list-or-null-if-1 (rnrs:* p q)) )
( (1 x) (list x) )
( (x 1) (list x) )
( (p q) (cond ((equal? (base p) (base q))
(list-or-null-if-1
(^ (base p)
(+ (exponent p)
(exponent q)))))
((order-relation q p) (list q p))
(else (list p q))) )
( (('* . ps) . qs) (merge-products ps (simplify-product-rec qs)) )
( (x . xs) (merge-products (list x) (simplify-product-rec xs)) )))
(define (simplify-product u)
(match u
( ('* x) x )
( ('* . (? any-are-zero?)) 0 )
( ('* . elts)
(match (simplify-product-rec elts)
( () 1 )
( (x) x )
( xs `(* ,@xs) )) )))
(define (* . elts)
(simplify-product `(* ,@elts)))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; +
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (merge-sums p-elts q-elts)
(match (list p-elts q-elts)
( (() x) x )
( (x ()) x )
( ((p . ps) (q . qs))
(match (simplify-sum-rec (list p q))
( () (merge-sums ps qs) )
( (x) (cons x (merge-sums ps qs)) )
( (? (equal-to (list p q))) (cons p (merge-sums ps q-elts)) )
( (? (equal-to (list q p))) (cons q (merge-sums p-elts qs)) )) )))
(define (simplify-sum-rec elts)
(match elts
( (('+ . p-elts) ('+ . q-elts)) (merge-sums p-elts q-elts) )
( (('+ . p-elts) q) (merge-sums p-elts (list q)) )
( (p ('+ . q-elts)) (merge-sums (list p) q-elts) )
( ((? number? p) (? number? q)) (list-or-null-if-0 (rnrs:+ p q)) )
( (0 x) (list x) )
( (x 0) (list x) )
( (p q) (cond ((equal? (term p) (term q))
(list-or-null-if-0
(* (term p)
(+ (const p)
(const q)))))
((order-relation q p)
(list q p))
(else (list p q))) )
( (('+ . ps) . qs) (merge-sums ps (simplify-sum-rec qs)) )
( (x . xs) (merge-sums (list x) (simplify-sum-rec xs)) )))
(define (simplify-sum u)
(match u
( ('+ x) x )
( ('+ . elts)
(match (simplify-sum-rec elts)
( () 0 )
( (x) x )
( xs `(+ ,@xs) )) )))
(define (+ . elts)
(simplify-sum `(+ ,@elts)))
)