| 1 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2 |
; File: sboyer.sch |
| 3 |
; Description: The Boyer benchmark |
| 4 |
; Author: Bob Boyer |
| 5 |
; Created: 5-Apr-85 |
| 6 |
; Modified: 10-Apr-85 14:52:20 (Bob Shaw) |
| 7 |
; 22-Jul-87 (Will Clinger) |
| 8 |
; 2-Jul-88 (Will Clinger -- distinguished #f and the empty list) |
| 9 |
; 13-Feb-97 (Will Clinger -- fixed bugs in unifier and rules, |
| 10 |
; rewrote to eliminate property lists, and added |
| 11 |
; a scaling parameter suggested by Bob Boyer) |
| 12 |
; 19-Mar-99 (Will Clinger -- cleaned up comments) |
| 13 |
; Language: Scheme |
| 14 |
; Status: Public Domain |
| 15 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 16 |
|
| 17 |
;;; SBOYER -- Logic programming benchmark, originally written by Bob Boyer. |
| 18 |
;;; Much less CONS-intensive than NBOYER because it uses Henry Baker's |
| 19 |
;;; "sharing cons". |
| 20 |
|
| 21 |
; Note: The version of this benchmark that appears in Dick Gabriel's book |
| 22 |
; contained several bugs that are corrected here. These bugs are discussed |
| 23 |
; by Henry Baker, "The Boyer Benchmark Meets Linear Logic", ACM SIGPLAN Lisp |
| 24 |
; Pointers 6(4), October-December 1993, pages 3-10. The fixed bugs are: |
| 25 |
; |
| 26 |
; The benchmark now returns a boolean result. |
| 27 |
; FALSEP and TRUEP use TERM-MEMBER? rather than MEMV (which is called MEMBER |
| 28 |
; in Common Lisp) |
| 29 |
; ONE-WAY-UNIFY1 now treats numbers correctly |
| 30 |
; ONE-WAY-UNIFY1-LST now treats empty lists correctly |
| 31 |
; Rule 19 has been corrected (this rule was not touched by the original |
| 32 |
; benchmark, but is used by this version) |
| 33 |
; Rules 84 and 101 have been corrected (but these rules are never touched |
| 34 |
; by the benchmark) |
| 35 |
; |
| 36 |
; According to Baker, these bug fixes make the benchmark 10-25% slower. |
| 37 |
; Please do not compare the timings from this benchmark against those of |
| 38 |
; the original benchmark. |
| 39 |
; |
| 40 |
; This version of the benchmark also prints the number of rewrites as a sanity |
| 41 |
; check, because it is too easy for a buggy version to return the correct |
| 42 |
; boolean result. The correct number of rewrites is |
| 43 |
; |
| 44 |
; n rewrites peak live storage (approximate, in bytes) |
| 45 |
; 0 95024 |
| 46 |
; 1 591777 |
| 47 |
; 2 1813975 |
| 48 |
; 3 5375678 |
| 49 |
; 4 16445406 |
| 50 |
; 5 51507739 |
| 51 |
|
| 52 |
; Sboyer is a 2-phase benchmark. |
| 53 |
; The first phase attaches lemmas to symbols. This phase is not timed, |
| 54 |
; but it accounts for very little of the runtime anyway. |
| 55 |
; The second phase creates the test problem, and tests to see |
| 56 |
; whether it is implied by the lemmas. |
| 57 |
|
| 58 |
(library (rnrs-benchmarks sboyer) |
| 59 |
(export main) |
| 60 |
(import (rnrs) (rnrs-benchmarks)) |
| 61 |
|
| 62 |
(define (main . args) |
| 63 |
(let ((n (if (null? args) 0 (car args)))) |
| 64 |
(setup-boyer) |
| 65 |
(run-benchmark |
| 66 |
(string-append "sboyer" |
| 67 |
(number->string n)) |
| 68 |
sboyer-iters |
| 69 |
(lambda (rewrites) |
| 70 |
(and (number? rewrites) |
| 71 |
(case n |
| 72 |
((0) (= rewrites 95024)) |
| 73 |
((1) (= rewrites 591777)) |
| 74 |
((2) (= rewrites 1813975)) |
| 75 |
((3) (= rewrites 5375678)) |
| 76 |
((4) (= rewrites 16445406)) |
| 77 |
((5) (= rewrites 51507739)) |
| 78 |
; If it works for n <= 5, assume it works. |
| 79 |
(else #t)))) |
| 80 |
(lambda (alist term n) (lambda () (test-boyer alist term n))) |
| 81 |
(quote ((x f (plus (plus a b) |
| 82 |
(plus c (zero)))) |
| 83 |
(y f (times (times a b) |
| 84 |
(plus c d))) |
| 85 |
(z f (reverse (append (append a b) |
| 86 |
(nil)))) |
| 87 |
(u equal (plus a b) |
| 88 |
(difference x y)) |
| 89 |
(w lessp (remainder a b) |
| 90 |
(member a (length b))))) |
| 91 |
(quote (implies (and (implies x y) |
| 92 |
(and (implies y z) |
| 93 |
(and (implies z u) |
| 94 |
(implies u w)))) |
| 95 |
(implies x w))) |
| 96 |
n))) |
| 97 |
|
| 98 |
(define (setup-boyer) #t) ; assigned below |
| 99 |
(define (test-boyer) #t) ; assigned below |
| 100 |
|
| 101 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 102 |
; |
| 103 |
; The first phase. |
| 104 |
; |
| 105 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 106 |
|
| 107 |
; In the original benchmark, it stored a list of lemmas on the |
| 108 |
; property lists of symbols. |
| 109 |
; In the new benchmark, it maintains an association list of |
| 110 |
; symbols and symbol-records, and stores the list of lemmas |
| 111 |
; within the symbol-records. |
| 112 |
|
| 113 |
(let () |
| 114 |
|
| 115 |
(define (setup) |
| 116 |
(add-lemma-lst |
| 117 |
(quote ((equal (compile form) |
| 118 |
(reverse (codegen (optimize form) |
| 119 |
(nil)))) |
| 120 |
(equal (eqp x y) |
| 121 |
(equal (fix x) |
| 122 |
(fix y))) |
| 123 |
(equal (greaterp x y) |
| 124 |
(lessp y x)) |
| 125 |
(equal (lesseqp x y) |
| 126 |
(not (lessp y x))) |
| 127 |
(equal (greatereqp x y) |
| 128 |
(not (lessp x y))) |
| 129 |
(equal (boolean x) |
| 130 |
(or (equal x (t)) |
| 131 |
(equal x (f)))) |
| 132 |
(equal (iff x y) |
| 133 |
(and (implies x y) |
| 134 |
(implies y x))) |
| 135 |
(equal (even1 x) |
| 136 |
(if (zerop x) |
| 137 |
(t) |
| 138 |
(odd (_1- x)))) |
| 139 |
(equal (countps- l pred) |
| 140 |
(countps-loop l pred (zero))) |
| 141 |
(equal (fact- i) |
| 142 |
(fact-loop i 1)) |
| 143 |
(equal (reverse- x) |
| 144 |
(reverse-loop x (nil))) |
| 145 |
(equal (divides x y) |
| 146 |
(zerop (remainder y x))) |
| 147 |
(equal (assume-true var alist) |
| 148 |
(cons (cons var (t)) |
| 149 |
alist)) |
| 150 |
(equal (assume-false var alist) |
| 151 |
(cons (cons var (f)) |
| 152 |
alist)) |
| 153 |
(equal (tautology-checker x) |
| 154 |
(tautologyp (normalize x) |
| 155 |
(nil))) |
| 156 |
(equal (falsify x) |
| 157 |
(falsify1 (normalize x) |
| 158 |
(nil))) |
| 159 |
(equal (prime x) |
| 160 |
(and (not (zerop x)) |
| 161 |
(not (equal x (add1 (zero)))) |
| 162 |
(prime1 x (_1- x)))) |
| 163 |
(equal (and p q) |
| 164 |
(if p (if q (t) |
| 165 |
(f)) |
| 166 |
(f))) |
| 167 |
(equal (or p q) |
| 168 |
(if p (t) |
| 169 |
(if q (t) |
| 170 |
(f)))) |
| 171 |
(equal (not p) |
| 172 |
(if p (f) |
| 173 |
(t))) |
| 174 |
(equal (implies p q) |
| 175 |
(if p (if q (t) |
| 176 |
(f)) |
| 177 |
(t))) |
| 178 |
(equal (fix x) |
| 179 |
(if (numberp x) |
| 180 |
x |
| 181 |
(zero))) |
| 182 |
(equal (if (if a b c) |
| 183 |
d e) |
| 184 |
(if a (if b d e) |
| 185 |
(if c d e))) |
| 186 |
(equal (zerop x) |
| 187 |
(or (equal x (zero)) |
| 188 |
(not (numberp x)))) |
| 189 |
(equal (plus (plus x y) |
| 190 |
z) |
| 191 |
(plus x (plus y z))) |
| 192 |
(equal (equal (plus a b) |
| 193 |
(zero)) |
| 194 |
(and (zerop a) |
| 195 |
(zerop b))) |
| 196 |
(equal (difference x x) |
| 197 |
(zero)) |
| 198 |
(equal (equal (plus a b) |
| 199 |
(plus a c)) |
| 200 |
(equal (fix b) |
| 201 |
(fix c))) |
| 202 |
(equal (equal (zero) |
| 203 |
(difference x y)) |
| 204 |
(not (lessp y x))) |
| 205 |
(equal (equal x (difference x y)) |
| 206 |
(and (numberp x) |
| 207 |
(or (equal x (zero)) |
| 208 |
(zerop y)))) |
| 209 |
(equal (meaning (plus-tree (append x y)) |
| 210 |
a) |
| 211 |
(plus (meaning (plus-tree x) |
| 212 |
a) |
| 213 |
(meaning (plus-tree y) |
| 214 |
a))) |
| 215 |
(equal (meaning (plus-tree (plus-fringe x)) |
| 216 |
a) |
| 217 |
(fix (meaning x a))) |
| 218 |
(equal (append (append x y) |
| 219 |
z) |
| 220 |
(append x (append y z))) |
| 221 |
(equal (reverse (append a b)) |
| 222 |
(append (reverse b) |
| 223 |
(reverse a))) |
| 224 |
(equal (times x (plus y z)) |
| 225 |
(plus (times x y) |
| 226 |
(times x z))) |
| 227 |
(equal (times (times x y) |
| 228 |
z) |
| 229 |
(times x (times y z))) |
| 230 |
(equal (equal (times x y) |
| 231 |
(zero)) |
| 232 |
(or (zerop x) |
| 233 |
(zerop y))) |
| 234 |
(equal (exec (append x y) |
| 235 |
pds envrn) |
| 236 |
(exec y (exec x pds envrn) |
| 237 |
envrn)) |
| 238 |
(equal (mc-flatten x y) |
| 239 |
(append (flatten x) |
| 240 |
y)) |
| 241 |
(equal (member x (append a b)) |
| 242 |
(or (member x a) |
| 243 |
(member x b))) |
| 244 |
(equal (member x (reverse y)) |
| 245 |
(member x y)) |
| 246 |
(equal (length (reverse x)) |
| 247 |
(length x)) |
| 248 |
(equal (member a (intersect b c)) |
| 249 |
(and (member a b) |
| 250 |
(member a c))) |
| 251 |
(equal (nth (zero) |
| 252 |
i) |
| 253 |
(zero)) |
| 254 |
(equal (exp i (plus j k)) |
| 255 |
(times (exp i j) |
| 256 |
(exp i k))) |
| 257 |
(equal (exp i (times j k)) |
| 258 |
(exp (exp i j) |
| 259 |
k)) |
| 260 |
(equal (reverse-loop x y) |
| 261 |
(append (reverse x) |
| 262 |
y)) |
| 263 |
(equal (reverse-loop x (nil)) |
| 264 |
(reverse x)) |
| 265 |
(equal (count-list z (sort-lp x y)) |
| 266 |
(plus (count-list z x) |
| 267 |
(count-list z y))) |
| 268 |
(equal (equal (append a b) |
| 269 |
(append a c)) |
| 270 |
(equal b c)) |
| 271 |
(equal (plus (remainder x y) |
| 272 |
(times y (quotient x y))) |
| 273 |
(fix x)) |
| 274 |
(equal (power-eval (big-plus1 l i base) |
| 275 |
base) |
| 276 |
(plus (power-eval l base) |
| 277 |
i)) |
| 278 |
(equal (power-eval (big-plus x y i base) |
| 279 |
base) |
| 280 |
(plus i (plus (power-eval x base) |
| 281 |
(power-eval y base)))) |
| 282 |
(equal (remainder y 1) |
| 283 |
(zero)) |
| 284 |
(equal (lessp (remainder x y) |
| 285 |
y) |
| 286 |
(not (zerop y))) |
| 287 |
(equal (remainder x x) |
| 288 |
(zero)) |
| 289 |
(equal (lessp (quotient i j) |
| 290 |
i) |
| 291 |
(and (not (zerop i)) |
| 292 |
(or (zerop j) |
| 293 |
(not (equal j 1))))) |
| 294 |
(equal (lessp (remainder x y) |
| 295 |
x) |
| 296 |
(and (not (zerop y)) |
| 297 |
(not (zerop x)) |
| 298 |
(not (lessp x y)))) |
| 299 |
(equal (power-eval (power-rep i base) |
| 300 |
base) |
| 301 |
(fix i)) |
| 302 |
(equal (power-eval (big-plus (power-rep i base) |
| 303 |
(power-rep j base) |
| 304 |
(zero) |
| 305 |
base) |
| 306 |
base) |
| 307 |
(plus i j)) |
| 308 |
(equal (gcd x y) |
| 309 |
(gcd y x)) |
| 310 |
(equal (nth (append a b) |
| 311 |
i) |
| 312 |
(append (nth a i) |
| 313 |
(nth b (difference i (length a))))) |
| 314 |
(equal (difference (plus x y) |
| 315 |
x) |
| 316 |
(fix y)) |
| 317 |
(equal (difference (plus y x) |
| 318 |
x) |
| 319 |
(fix y)) |
| 320 |
(equal (difference (plus x y) |
| 321 |
(plus x z)) |
| 322 |
(difference y z)) |
| 323 |
(equal (times x (difference c w)) |
| 324 |
(difference (times c x) |
| 325 |
(times w x))) |
| 326 |
(equal (remainder (times x z) |
| 327 |
z) |
| 328 |
(zero)) |
| 329 |
(equal (difference (plus b (plus a c)) |
| 330 |
a) |
| 331 |
(plus b c)) |
| 332 |
(equal (difference (add1 (plus y z)) |
| 333 |
z) |
| 334 |
(add1 y)) |
| 335 |
(equal (lessp (plus x y) |
| 336 |
(plus x z)) |
| 337 |
(lessp y z)) |
| 338 |
(equal (lessp (times x z) |
| 339 |
(times y z)) |
| 340 |
(and (not (zerop z)) |
| 341 |
(lessp x y))) |
| 342 |
(equal (lessp y (plus x y)) |
| 343 |
(not (zerop x))) |
| 344 |
(equal (gcd (times x z) |
| 345 |
(times y z)) |
| 346 |
(times z (gcd x y))) |
| 347 |
(equal (value (normalize x) |
| 348 |
a) |
| 349 |
(value x a)) |
| 350 |
(equal (equal (flatten x) |
| 351 |
(cons y (nil))) |
| 352 |
(and (nlistp x) |
| 353 |
(equal x y))) |
| 354 |
(equal (listp (gopher x)) |
| 355 |
(listp x)) |
| 356 |
(equal (samefringe x y) |
| 357 |
(equal (flatten x) |
| 358 |
(flatten y))) |
| 359 |
(equal (equal (greatest-factor x y) |
| 360 |
(zero)) |
| 361 |
(and (or (zerop y) |
| 362 |
(equal y 1)) |
| 363 |
(equal x (zero)))) |
| 364 |
(equal (equal (greatest-factor x y) |
| 365 |
1) |
| 366 |
(equal x 1)) |
| 367 |
(equal (numberp (greatest-factor x y)) |
| 368 |
(not (and (or (zerop y) |
| 369 |
(equal y 1)) |
| 370 |
(not (numberp x))))) |
| 371 |
(equal (times-list (append x y)) |
| 372 |
(times (times-list x) |
| 373 |
(times-list y))) |
| 374 |
(equal (prime-list (append x y)) |
| 375 |
(and (prime-list x) |
| 376 |
(prime-list y))) |
| 377 |
(equal (equal z (times w z)) |
| 378 |
(and (numberp z) |
| 379 |
(or (equal z (zero)) |
| 380 |
(equal w 1)))) |
| 381 |
(equal (greatereqp x y) |
| 382 |
(not (lessp x y))) |
| 383 |
(equal (equal x (times x y)) |
| 384 |
(or (equal x (zero)) |
| 385 |
(and (numberp x) |
| 386 |
(equal y 1)))) |
| 387 |
(equal (remainder (times y x) |
| 388 |
y) |
| 389 |
(zero)) |
| 390 |
(equal (equal (times a b) |
| 391 |
1) |
| 392 |
(and (not (equal a (zero))) |
| 393 |
(not (equal b (zero))) |
| 394 |
(numberp a) |
| 395 |
(numberp b) |
| 396 |
(equal (_1- a) |
| 397 |
(zero)) |
| 398 |
(equal (_1- b) |
| 399 |
(zero)))) |
| 400 |
(equal (lessp (length (delete x l)) |
| 401 |
(length l)) |
| 402 |
(member x l)) |
| 403 |
(equal (sort2 (delete x l)) |
| 404 |
(delete x (sort2 l))) |
| 405 |
(equal (dsort x) |
| 406 |
(sort2 x)) |
| 407 |
(equal (length (cons x1 |
| 408 |
(cons x2 |
| 409 |
(cons x3 (cons x4 |
| 410 |
(cons x5 |
| 411 |
(cons x6 x7))))))) |
| 412 |
(plus 6 (length x7))) |
| 413 |
(equal (difference (add1 (add1 x)) |
| 414 |
2) |
| 415 |
(fix x)) |
| 416 |
(equal (quotient (plus x (plus x y)) |
| 417 |
2) |
| 418 |
(plus x (quotient y 2))) |
| 419 |
(equal (sigma (zero) |
| 420 |
i) |
| 421 |
(quotient (times i (add1 i)) |
| 422 |
2)) |
| 423 |
(equal (plus x (add1 y)) |
| 424 |
(if (numberp y) |
| 425 |
(add1 (plus x y)) |
| 426 |
(add1 x))) |
| 427 |
(equal (equal (difference x y) |
| 428 |
(difference z y)) |
| 429 |
(if (lessp x y) |
| 430 |
(not (lessp y z)) |
| 431 |
(if (lessp z y) |
| 432 |
(not (lessp y x)) |
| 433 |
(equal (fix x) |
| 434 |
(fix z))))) |
| 435 |
(equal (meaning (plus-tree (delete x y)) |
| 436 |
a) |
| 437 |
(if (member x y) |
| 438 |
(difference (meaning (plus-tree y) |
| 439 |
a) |
| 440 |
(meaning x a)) |
| 441 |
(meaning (plus-tree y) |
| 442 |
a))) |
| 443 |
(equal (times x (add1 y)) |
| 444 |
(if (numberp y) |
| 445 |
(plus x (times x y)) |
| 446 |
(fix x))) |
| 447 |
(equal (nth (nil) |
| 448 |
i) |
| 449 |
(if (zerop i) |
| 450 |
(nil) |
| 451 |
(zero))) |
| 452 |
(equal (last (append a b)) |
| 453 |
(if (listp b) |
| 454 |
(last b) |
| 455 |
(if (listp a) |
| 456 |
(cons (car (last a)) |
| 457 |
b) |
| 458 |
b))) |
| 459 |
(equal (equal (lessp x y) |
| 460 |
z) |
| 461 |
(if (lessp x y) |
| 462 |
(equal (t) z) |
| 463 |
(equal (f) z))) |
| 464 |
(equal (assignment x (append a b)) |
| 465 |
(if (assignedp x a) |
| 466 |
(assignment x a) |
| 467 |
(assignment x b))) |
| 468 |
(equal (car (gopher x)) |
| 469 |
(if (listp x) |
| 470 |
(car (flatten x)) |
| 471 |
(zero))) |
| 472 |
(equal (flatten (cdr (gopher x))) |
| 473 |
(if (listp x) |
| 474 |
(cdr (flatten x)) |
| 475 |
(cons (zero) |
| 476 |
(nil)))) |
| 477 |
(equal (quotient (times y x) |
| 478 |
y) |
| 479 |
(if (zerop y) |
| 480 |
(zero) |
| 481 |
(fix x))) |
| 482 |
(equal (get j (set i val mem)) |
| 483 |
(if (eqp j i) |
| 484 |
val |
| 485 |
(get j mem))))))) |
| 486 |
|
| 487 |
(define (add-lemma-lst lst) |
| 488 |
(cond ((null? lst) |
| 489 |
#t) |
| 490 |
(else (add-lemma (car lst)) |
| 491 |
(add-lemma-lst (cdr lst))))) |
| 492 |
|
| 493 |
(define (add-lemma term) |
| 494 |
(cond ((and (pair? term) |
| 495 |
(eq? (car term) |
| 496 |
(quote equal)) |
| 497 |
(pair? (cadr term))) |
| 498 |
(put (car (cadr term)) |
| 499 |
(quote lemmas) |
| 500 |
(cons |
| 501 |
(translate-term term) |
| 502 |
(get (car (cadr term)) (quote lemmas))))) |
| 503 |
(else (fatal-error "ADD-LEMMA did not like term: " term)))) |
| 504 |
|
| 505 |
; Translates a term by replacing its constructor symbols by symbol-records. |
| 506 |
|
| 507 |
(define (translate-term term) |
| 508 |
(cond ((not (pair? term)) |
| 509 |
term) |
| 510 |
(else (cons (symbol->symbol-record (car term)) |
| 511 |
(translate-args (cdr term)))))) |
| 512 |
|
| 513 |
(define (translate-args lst) |
| 514 |
(cond ((null? lst) |
| 515 |
'()) |
| 516 |
(else (cons (translate-term (car lst)) |
| 517 |
(translate-args (cdr lst)))))) |
| 518 |
|
| 519 |
; For debugging only, so the use of MAP does not change |
| 520 |
; the first-order character of the benchmark. |
| 521 |
|
| 522 |
(define (untranslate-term term) |
| 523 |
(cond ((not (pair? term)) |
| 524 |
term) |
| 525 |
(else (cons (get-name (car term)) |
| 526 |
(map untranslate-term (cdr term)))))) |
| 527 |
|
| 528 |
; A symbol-record is represented as a vector with two fields: |
| 529 |
; the symbol (for debugging) and |
| 530 |
; the list of lemmas associated with the symbol. |
| 531 |
|
| 532 |
(define (put sym property value) |
| 533 |
(put-lemmas! (symbol->symbol-record sym) value)) |
| 534 |
|
| 535 |
(define (get sym property) |
| 536 |
(get-lemmas (symbol->symbol-record sym))) |
| 537 |
|
| 538 |
(define (symbol->symbol-record sym) |
| 539 |
(let ((x (assq sym *symbol-records-alist*))) |
| 540 |
(if x |
| 541 |
(cdr x) |
| 542 |
(let ((r (make-symbol-record sym))) |
| 543 |
(set! *symbol-records-alist* |
| 544 |
(cons (cons sym r) |
| 545 |
*symbol-records-alist*)) |
| 546 |
r)))) |
| 547 |
|
| 548 |
; Association list of symbols and symbol-records. |
| 549 |
|
| 550 |
(define *symbol-records-alist* '()) |
| 551 |
|
| 552 |
; A symbol-record is represented as a vector with two fields: |
| 553 |
; the symbol (for debugging) and |
| 554 |
; the list of lemmas associated with the symbol. |
| 555 |
|
| 556 |
(define (make-symbol-record sym) |
| 557 |
(vector sym '())) |
| 558 |
|
| 559 |
(define (put-lemmas! symbol-record lemmas) |
| 560 |
(vector-set! symbol-record 1 lemmas)) |
| 561 |
|
| 562 |
(define (get-lemmas symbol-record) |
| 563 |
(vector-ref symbol-record 1)) |
| 564 |
|
| 565 |
(define (get-name symbol-record) |
| 566 |
(vector-ref symbol-record 0)) |
| 567 |
|
| 568 |
(define (symbol-record-equal? r1 r2) |
| 569 |
(eq? r1 r2)) |
| 570 |
|
| 571 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 572 |
; |
| 573 |
; The second phase. |
| 574 |
; |
| 575 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 576 |
|
| 577 |
(define (test alist term n) |
| 578 |
(let ((term |
| 579 |
(apply-subst |
| 580 |
(translate-alist alist) |
| 581 |
(translate-term |
| 582 |
(do ((term term (list 'or term '(f))) |
| 583 |
(n n (- n 1))) |
| 584 |
((zero? n) term)))))) |
| 585 |
(tautp term))) |
| 586 |
|
| 587 |
(define (translate-alist alist) |
| 588 |
(cond ((null? alist) |
| 589 |
'()) |
| 590 |
(else (cons (cons (caar alist) |
| 591 |
(translate-term (cdar alist))) |
| 592 |
(translate-alist (cdr alist)))))) |
| 593 |
|
| 594 |
(define (apply-subst alist term) |
| 595 |
(cond ((not (pair? term)) |
| 596 |
(let ((temp-temp (assq term alist))) |
| 597 |
(if temp-temp |
| 598 |
(cdr temp-temp) |
| 599 |
term))) |
| 600 |
(else (cons (car term) |
| 601 |
(apply-subst-lst alist (cdr term)))))) |
| 602 |
|
| 603 |
(define (apply-subst-lst alist lst) |
| 604 |
(cond ((null? lst) |
| 605 |
'()) |
| 606 |
(else (cons (apply-subst alist (car lst)) |
| 607 |
(apply-subst-lst alist (cdr lst)))))) |
| 608 |
|
| 609 |
(define (tautp x) |
| 610 |
(tautologyp (rewrite x) |
| 611 |
'() '())) |
| 612 |
|
| 613 |
(define (tautologyp x true-lst false-lst) |
| 614 |
(cond ((truep x true-lst) |
| 615 |
#t) |
| 616 |
((falsep x false-lst) |
| 617 |
#f) |
| 618 |
((not (pair? x)) |
| 619 |
#f) |
| 620 |
((eq? (car x) if-constructor) |
| 621 |
(cond ((truep (cadr x) |
| 622 |
true-lst) |
| 623 |
(tautologyp (caddr x) |
| 624 |
true-lst false-lst)) |
| 625 |
((falsep (cadr x) |
| 626 |
false-lst) |
| 627 |
(tautologyp (cadddr x) |
| 628 |
true-lst false-lst)) |
| 629 |
(else (and (tautologyp (caddr x) |
| 630 |
(cons (cadr x) |
| 631 |
true-lst) |
| 632 |
false-lst) |
| 633 |
(tautologyp (cadddr x) |
| 634 |
true-lst |
| 635 |
(cons (cadr x) |
| 636 |
false-lst)))))) |
| 637 |
(else #f))) |
| 638 |
|
| 639 |
(define if-constructor '*) ; becomes (symbol->symbol-record 'if) |
| 640 |
|
| 641 |
(define rewrite-count 0) ; sanity check |
| 642 |
|
| 643 |
; The next procedure is Henry Baker's sharing CONS, which avoids |
| 644 |
; allocation if the result is already in hand. |
| 645 |
; The REWRITE and REWRITE-ARGS procedures have been modified to |
| 646 |
; use SCONS instead of CONS. |
| 647 |
|
| 648 |
(define (scons x y original) |
| 649 |
(if (and (eq? x (car original)) |
| 650 |
(eq? y (cdr original))) |
| 651 |
original |
| 652 |
(cons x y))) |
| 653 |
|
| 654 |
(define (rewrite term) |
| 655 |
(set! rewrite-count (+ rewrite-count 1)) |
| 656 |
(cond ((not (pair? term)) |
| 657 |
term) |
| 658 |
(else (rewrite-with-lemmas (scons (car term) |
| 659 |
(rewrite-args (cdr term)) |
| 660 |
term) |
| 661 |
(get-lemmas (car term)))))) |
| 662 |
|
| 663 |
(define (rewrite-args lst) |
| 664 |
(cond ((null? lst) |
| 665 |
'()) |
| 666 |
(else (scons (rewrite (car lst)) |
| 667 |
(rewrite-args (cdr lst)) |
| 668 |
lst)))) |
| 669 |
|
| 670 |
(define (rewrite-with-lemmas term lst) |
| 671 |
(cond ((null? lst) |
| 672 |
term) |
| 673 |
((one-way-unify term (cadr (car lst))) |
| 674 |
(rewrite (apply-subst unify-subst (caddr (car lst))))) |
| 675 |
(else (rewrite-with-lemmas term (cdr lst))))) |
| 676 |
|
| 677 |
(define unify-subst '*) |
| 678 |
|
| 679 |
(define (one-way-unify term1 term2) |
| 680 |
(begin (set! unify-subst '()) |
| 681 |
(one-way-unify1 term1 term2))) |
| 682 |
|
| 683 |
(define (one-way-unify1 term1 term2) |
| 684 |
(cond ((not (pair? term2)) |
| 685 |
(let ((temp-temp (assq term2 unify-subst))) |
| 686 |
(cond (temp-temp |
| 687 |
(term-equal? term1 (cdr temp-temp))) |
| 688 |
((number? term2) ; This bug fix makes |
| 689 |
(equal? term1 term2)) ; nboyer 10-25% slower! |
| 690 |
(else |
| 691 |
(set! unify-subst (cons (cons term2 term1) |
| 692 |
unify-subst)) |
| 693 |
#t)))) |
| 694 |
((not (pair? term1)) |
| 695 |
#f) |
| 696 |
((eq? (car term1) |
| 697 |
(car term2)) |
| 698 |
(one-way-unify1-lst (cdr term1) |
| 699 |
(cdr term2))) |
| 700 |
(else #f))) |
| 701 |
|
| 702 |
(define (one-way-unify1-lst lst1 lst2) |
| 703 |
(cond ((null? lst1) |
| 704 |
(null? lst2)) |
| 705 |
((null? lst2) |
| 706 |
#f) |
| 707 |
((one-way-unify1 (car lst1) |
| 708 |
(car lst2)) |
| 709 |
(one-way-unify1-lst (cdr lst1) |
| 710 |
(cdr lst2))) |
| 711 |
(else #f))) |
| 712 |
|
| 713 |
(define (falsep x lst) |
| 714 |
(or (term-equal? x false-term) |
| 715 |
(term-member? x lst))) |
| 716 |
|
| 717 |
(define (truep x lst) |
| 718 |
(or (term-equal? x true-term) |
| 719 |
(term-member? x lst))) |
| 720 |
|
| 721 |
(define false-term '*) ; becomes (translate-term '(f)) |
| 722 |
(define true-term '*) ; becomes (translate-term '(t)) |
| 723 |
|
| 724 |
; The next two procedures were in the original benchmark |
| 725 |
; but were never used. |
| 726 |
|
| 727 |
(define (trans-of-implies n) |
| 728 |
(translate-term |
| 729 |
(list (quote implies) |
| 730 |
(trans-of-implies1 n) |
| 731 |
(list (quote implies) |
| 732 |
0 n)))) |
| 733 |
|
| 734 |
(define (trans-of-implies1 n) |
| 735 |
(cond ((equal? n 1) |
| 736 |
(list (quote implies) |
| 737 |
0 1)) |
| 738 |
(else (list (quote and) |
| 739 |
(list (quote implies) |
| 740 |
(- n 1) |
| 741 |
n) |
| 742 |
(trans-of-implies1 (- n 1)))))) |
| 743 |
|
| 744 |
; Translated terms can be circular structures, which can't be |
| 745 |
; compared using Scheme's equal? and member procedures, so we |
| 746 |
; use these instead. |
| 747 |
|
| 748 |
(define (term-equal? x y) |
| 749 |
(cond ((pair? x) |
| 750 |
(and (pair? y) |
| 751 |
(symbol-record-equal? (car x) (car y)) |
| 752 |
(term-args-equal? (cdr x) (cdr y)))) |
| 753 |
(else (equal? x y)))) |
| 754 |
|
| 755 |
(define (term-args-equal? lst1 lst2) |
| 756 |
(cond ((null? lst1) |
| 757 |
(null? lst2)) |
| 758 |
((null? lst2) |
| 759 |
#f) |
| 760 |
((term-equal? (car lst1) (car lst2)) |
| 761 |
(term-args-equal? (cdr lst1) (cdr lst2))) |
| 762 |
(else #f))) |
| 763 |
|
| 764 |
(define (term-member? x lst) |
| 765 |
(cond ((null? lst) |
| 766 |
#f) |
| 767 |
((term-equal? x (car lst)) |
| 768 |
#t) |
| 769 |
(else (term-member? x (cdr lst))))) |
| 770 |
|
| 771 |
(set! setup-boyer |
| 772 |
(lambda () |
| 773 |
(set! *symbol-records-alist* '()) |
| 774 |
(set! if-constructor (symbol->symbol-record 'if)) |
| 775 |
(set! false-term (translate-term '(f))) |
| 776 |
(set! true-term (translate-term '(t))) |
| 777 |
(setup))) |
| 778 |
|
| 779 |
(set! test-boyer |
| 780 |
(lambda (alist term n) |
| 781 |
(set! rewrite-count 0) |
| 782 |
(let ((answer (test alist term n))) |
| 783 |
; (write rewrite-count) |
| 784 |
; (display " rewrites") |
| 785 |
; (newline) |
| 786 |
(if answer |
| 787 |
rewrite-count |
| 788 |
#f)))))) |