|
6 | 6 | "../syntax/syntax.rkt") |
7 | 7 |
|
8 | 8 | (provide *rules* |
9 | | - *sound-rules* |
10 | 9 | (struct-out rule) |
11 | | - add-unsound) |
| 10 | + add-sound) |
12 | 11 |
|
13 | 12 | ;; A rule represents "find-and-replacing" `input` by `output`. Both |
14 | 13 | ;; are patterns, meaning that symbols represent pattern variables. |
|
22 | 21 | (define (rule-enabled? rule) |
23 | 22 | (ormap (curry flag-set? 'rules) (rule-tags rule))) |
24 | 23 |
|
25 | | -(define (rule-sound? rule) |
26 | | - (set-member? (rule-tags rule) 'sound)) |
27 | | - |
28 | 24 | (define (*rules*) |
29 | 25 | (filter rule-enabled? *all-rules*)) |
30 | 26 |
|
31 | | -(define (*sound-rules*) |
32 | | - (filter (conjoin rule-enabled? rule-sound?) *all-rules*)) |
33 | | - |
34 | | -(define (add-unsound expr) |
| 27 | +(define (add-sound expr) |
35 | 28 | (match expr |
36 | | - [(list op args ...) (cons (sym-append "unsound-" op) (map add-unsound args))] |
| 29 | + [(list (and (or '/ 'pow 'log) op) args ...) |
| 30 | + `(,(sym-append "sound-" op) ,@(map add-sound args) ,(gensym))] |
| 31 | + [(list op args ...) (cons op (map add-sound expr))] |
37 | 32 | [_ expr])) |
38 | 33 |
|
39 | | -(define-syntax define-rule |
40 | | - (syntax-rules () |
41 | | - [(define-rule rname group input output) |
42 | | - (set! *all-rules* (cons (rule 'rname 'input 'output '(group sound)) *all-rules*))] |
43 | | - [(define-rule rname group input output #:unsound) |
44 | | - (set! *all-rules* (cons (rule 'rname 'input (add-unsound 'output) '(group)) *all-rules*))])) |
| 34 | +(define-syntax-rule (define-rule rname group input output) |
| 35 | + (set! *all-rules* (cons (rule 'rname 'input 'output '(group)) *all-rules*))) |
45 | 36 |
|
46 | 37 | (define-syntax-rule (define-rules group |
47 | 38 | [rname input output flags ...] ...) |
|
154 | 145 | (define-rules arithmetic |
155 | 146 | [mult-flip (/ a b) (* a (/ 1 b))] |
156 | 147 | [mult-flip-rev (* a (/ 1 b)) (/ a b)] |
157 | | - [div-flip (/ a b) (/ 1 (/ b a)) #:unsound] ; unsound @ a = 0, b != 0 |
| 148 | + [div-flip (/ a b) (sound-/ 1 (sound-/ b a 0) (/ a b))] |
158 | 149 | [div-flip-rev (/ 1 (/ b a)) (/ a b)]) |
159 | 150 |
|
160 | 151 | ; Fractions |
161 | 152 | (define-rules arithmetic |
162 | | - [sum-to-mult (+ a b) (* (+ 1 (/ b a)) a) #:unsound] ; unsound @ a = 0, b = 1 |
| 153 | + #;[sum-to-mult (+ a b) (* (+ 1 (/ b a)) a) #:unsound] ; unsound @ a = 0, b = 1 |
163 | 154 | [sum-to-mult-rev (* (+ 1 (/ b a)) a) (+ a b)] |
164 | | - [sub-to-mult (- a b) (* (- 1 (/ b a)) a) #:unsound] ; unsound @ a = 0, b = 1 |
| 155 | + #;[sub-to-mult (- a b) (* (- 1 (/ b a)) a) #:unsound] ; unsound @ a = 0, b = 1 |
165 | 156 | [sub-to-mult-rev (* (- 1 (/ b a)) a) (- a b)] |
166 | 157 | [add-to-fraction (+ c (/ b a)) (/ (+ (* c a) b) a)] |
167 | 158 | [add-to-fraction-rev (/ (+ (* c a) b) a) (+ c (/ b a))] |
|
170 | 161 | [common-denominator (+ (/ a b) (/ c d)) (/ (+ (* a d) (* c b)) (* b d))]) |
171 | 162 |
|
172 | 163 | (define-rules polynomials |
173 | | - [sqr-pow (pow a b) (* (pow a (/ b 2)) (pow a (/ b 2))) #:unsound] ; unsound @ a = -1, b = 1 |
174 | | - [flip-+ (+ a b) (/ (- (* a a) (* b b)) (- a b)) #:unsound] ; unsound @ a = b = 1 |
175 | | - [flip-- (- a b) (/ (- (* a a) (* b b)) (+ a b)) #:unsound]) ; unsound @ a = -1, b = 1 |
| 164 | + #;[sqr-pow (pow a b) (* (pow a (/ b 2)) (pow a (/ b 2))) #:unsound] ; unsound @ a = -1, b = 1 |
| 165 | + [flip-+ (+ a b) (sound-/ (- (* a a) (* b b)) (- a b) (+ a b))] |
| 166 | + [flip-- (- a b) (sound-/ (- (* a a) (* b b)) (+ a b) (- a b))]) |
176 | 167 |
|
177 | 168 | ; Difference of cubes |
178 | 169 | (define-rules polynomials |
|
181 | 172 | [difference-cubes-rev (* (+ (* a a) (+ (* b b) (* a b))) (- a b)) (- (pow a 3) (pow b 3))] |
182 | 173 | [sum-cubes-rev (* (+ (* a a) (- (* b b) (* a b))) (+ a b)) (+ (pow a 3) (pow b 3))]) |
183 | 174 |
|
184 | | -(define-rules polynomials ; unsound @ a = b = 0 |
185 | | - [flip3-+ (+ a b) (/ (+ (pow a 3) (pow b 3)) (+ (* a a) (- (* b b) (* a b)))) #:unsound] |
186 | | - [flip3-- (- a b) (/ (- (pow a 3) (pow b 3)) (+ (* a a) (+ (* b b) (* a b)))) #:unsound]) |
| 175 | +(define-rules polynomials |
| 176 | + [flip3-+ (+ a b) (sound-/ (+ (pow a 3) (pow b 3)) (+ (* a a) (- (* b b) (* a b))) (+ a b))] |
| 177 | + [flip3-- (- a b) (sound-/ (- (pow a 3) (pow b 3)) (+ (* a a) (+ (* b b) (* a b))) (- a b))]) |
187 | 178 |
|
188 | 179 | ; Dealing with fractions |
189 | 180 | (define-rules fractions |
|
245 | 236 | [sqrt-undiv (/ (sqrt x) (sqrt y)) (sqrt (/ x y))]) |
246 | 237 |
|
247 | 238 | (define-rules arithmetic |
248 | | - [sqrt-prod (sqrt (* x y)) (* (sqrt x) (sqrt y)) #:unsound] ; unsound @ x = y = -1 |
249 | | - [sqrt-div (sqrt (/ x y)) (/ (sqrt x) (sqrt y)) #:unsound] ; unsound @ x = y = -1 |
250 | | - [add-sqr-sqrt x (* (sqrt x) (sqrt x)) #:unsound]) ; unsound @ x = -1 |
| 239 | + [sqrt-prod (sqrt (* x y)) (* (sqrt (fabs x)) (sqrt (fabs y)))] |
| 240 | + [sqrt-div (sqrt (/ x y)) (/ (sqrt (fabs x)) (sqrt (fabs y)))] |
| 241 | + [add-sqr-sqrt x (copysign (* (sqrt (fabs x)) (sqrt (fabs x))) x)]) |
251 | 242 |
|
252 | 243 | ; Cubing |
253 | 244 | (define-rules arithmetic |
|
289 | 280 | ; Exponentials |
290 | 281 | (define-rules exponents |
291 | 282 | [add-log-exp x (log (exp x))] |
292 | | - [add-exp-log x (exp (log x)) #:unsound] ; unsound @ x = 0 |
| 283 | + #;[add-exp-log x (exp (log x)) #:unsound] ; unsound @ x = -1 |
293 | 284 | [rem-exp-log (exp (log x)) x] |
294 | 285 | [rem-log-exp (log (exp x)) x]) |
295 | 286 |
|
|
348 | 339 | [pow-div (/ (pow a b) (pow a c)) (pow a (- b c))]) |
349 | 340 |
|
350 | 341 | (define-rules exponents |
351 | | - [pow-plus-rev (pow a (+ b 1)) (* (pow a b) a) #:unsound] ; unsound @ a = 0, b = -1/2 |
352 | | - [pow-neg (pow a (neg b)) (/ 1 (pow a b)) #:unsound]) ; unsound @ a = 0, b = -1 |
| 342 | + [pow-plus-rev (pow a (+ b 1)) (* (sound-pow a b 1) a)] |
| 343 | + [pow-neg (pow a (neg b)) (sound-/ 1 (sound-pow a b 0) 0)]) |
353 | 344 |
|
354 | 345 | (define-rules exponents |
355 | | - [pow-to-exp (pow a b) (exp (* (log a) b)) #:unsound] ; unsound @ a = -1, b = 1 |
356 | | - [pow-add (pow a (+ b c)) (* (pow a b) (pow a c)) #:unsound] ; unsound @ a = -1, b = c = 1/2 |
357 | | - [pow-sub (pow a (- b c)) (/ (pow a b) (pow a c)) #:unsound] ; unsound @ a = -1, b = c = 1/2 |
| 346 | + #;[pow-to-exp (pow a b) (exp (* (log a) b)) #:unsound] ; unsound @ a = -1, b = 1 |
| 347 | + #;[pow-add (pow a (+ b c)) (* (pow a b) (pow a c)) #:unsound] ; unsound @ a = -1, b = c = 1/2 |
| 348 | + #;[pow-sub (pow a (- b c)) (/ (pow a b) (pow a c)) #:unsound] ; unsound @ a = -1, b = c = 1/2 |
| 349 | + #; |
358 | 350 | [unpow-prod-down (pow (* b c) a) (* (pow b a) (pow c a)) #:unsound]) ; unsound @ a = 1/2, b = c = -1 |
359 | 351 |
|
360 | 352 | ; Logarithms |
|
364 | 356 | [log-pow-rev (* b (log a)) (log (pow a b))]) |
365 | 357 |
|
366 | 358 | (define-rules exponents |
367 | | - [log-prod (log (* a b)) (+ (log a) (log b)) #:unsound] ; unsound @ a = b = -1 |
368 | | - [log-div (log (/ a b)) (- (log a) (log b)) #:unsound] ; unsound @ a = b = -1 |
369 | | - [log-pow (log (pow a b)) (* b (log a)) #:unsound]) ; unsound @ a = -1, b = 2 |
| 359 | + [log-prod (log (* a b)) (+ (log (fabs a)) (log (fabs b)))] |
| 360 | + [log-div (log (/ a b)) (- (log (fabs a)) (log (fabs b)))] |
| 361 | + [log-pow (log (pow a b)) (* b (sound-log (fabs a) 0))]) |
370 | 362 |
|
371 | 363 | (define-rules exponents |
372 | 364 | [sum-log (+ (log a) (log b)) (log (* a b))] |
|
0 commit comments