| 1 |
;;; BOYER -- Logic programming benchmark, originally written by Bob Boyer. |
| 2 |
;;; Fairly CONS intensive. |
| 3 |
|
| 4 |
(library (rnrs-benchmarks boyer) |
| 5 |
(export main) |
| 6 |
(import (rnrs) |
| 7 |
(rnrs mutable-pairs) |
| 8 |
(rnrs-benchmarks)) |
| 9 |
|
| 10 |
(define (lookup key table) |
| 11 |
(let loop ((x table)) |
| 12 |
(if (null? x) |
| 13 |
#f |
| 14 |
(let ((pair (car x))) |
| 15 |
(if (eq? (car pair) key) |
| 16 |
pair |
| 17 |
(loop (cdr x))))))) |
| 18 |
|
| 19 |
(define properties '()) |
| 20 |
|
| 21 |
(define (get key1 key2) |
| 22 |
(let ((x (lookup key1 properties))) |
| 23 |
(if x |
| 24 |
(let ((y (lookup key2 (cdr x)))) |
| 25 |
(if y |
| 26 |
(cdr y) |
| 27 |
#f)) |
| 28 |
#f))) |
| 29 |
|
| 30 |
(define (put key1 key2 val) |
| 31 |
(let ((x (lookup key1 properties))) |
| 32 |
(if x |
| 33 |
(let ((y (lookup key2 (cdr x)))) |
| 34 |
(if y |
| 35 |
(set-cdr! y val) |
| 36 |
(set-cdr! x (cons (cons key2 val) (cdr x))))) |
| 37 |
(set! properties |
| 38 |
(cons (list key1 (cons key2 val)) properties))))) |
| 39 |
|
| 40 |
(define unify-subst '()) |
| 41 |
|
| 42 |
(define (add-lemma term) |
| 43 |
(cond ((and (pair? term) |
| 44 |
(eq? (car term) |
| 45 |
(quote equal)) |
| 46 |
(pair? (cadr term))) |
| 47 |
(put (car (cadr term)) |
| 48 |
(quote lemmas) |
| 49 |
(cons term (get (car (cadr term)) (quote lemmas))))) |
| 50 |
(else (fatal-error "ADD-LEMMA did not like term: " term)))) |
| 51 |
|
| 52 |
(define (add-lemma-lst lst) |
| 53 |
(cond ((null? lst) |
| 54 |
#t) |
| 55 |
(else (add-lemma (car lst)) |
| 56 |
(add-lemma-lst (cdr lst))))) |
| 57 |
|
| 58 |
(define (apply-subst alist term) |
| 59 |
(cond ((not (pair? term)) |
| 60 |
(cond ((assq term alist) => cdr) |
| 61 |
(else term))) |
| 62 |
(else (cons (car term) |
| 63 |
(apply-subst-lst alist (cdr term)))))) |
| 64 |
|
| 65 |
(define (apply-subst-lst alist lst) |
| 66 |
(cond ((null? lst) |
| 67 |
'()) |
| 68 |
(else (cons (apply-subst alist (car lst)) |
| 69 |
(apply-subst-lst alist (cdr lst)))))) |
| 70 |
|
| 71 |
(define (falsep x lst) |
| 72 |
(or (equal? x (quote (f))) |
| 73 |
(member x lst))) |
| 74 |
|
| 75 |
(define (one-way-unify term1 term2) |
| 76 |
(begin (set! unify-subst '()) |
| 77 |
(one-way-unify1 term1 term2))) |
| 78 |
|
| 79 |
(define (one-way-unify1 term1 term2) |
| 80 |
(cond ((not (pair? term2)) |
| 81 |
(cond ((assq term2 unify-subst) => |
| 82 |
(lambda (x) (equal? term1 (cdr x)))) |
| 83 |
(else (set! unify-subst (cons (cons term2 term1) |
| 84 |
unify-subst)) |
| 85 |
#t))) |
| 86 |
((not (pair? term1)) |
| 87 |
#f) |
| 88 |
((eq? (car term1) |
| 89 |
(car term2)) |
| 90 |
(one-way-unify1-lst (cdr term1) |
| 91 |
(cdr term2))) |
| 92 |
(else #f))) |
| 93 |
|
| 94 |
(define (one-way-unify1-lst lst1 lst2) |
| 95 |
(cond ((null? lst1) |
| 96 |
#t) |
| 97 |
((one-way-unify1 (car lst1) |
| 98 |
(car lst2)) |
| 99 |
(one-way-unify1-lst (cdr lst1) |
| 100 |
(cdr lst2))) |
| 101 |
(else #f))) |
| 102 |
|
| 103 |
(define (rewrite term) |
| 104 |
(cond ((not (pair? term)) |
| 105 |
term) |
| 106 |
(else (rewrite-with-lemmas (cons (car term) |
| 107 |
(rewrite-args (cdr term))) |
| 108 |
(get (car term) |
| 109 |
(quote lemmas)))))) |
| 110 |
|
| 111 |
(define (rewrite-args lst) |
| 112 |
(cond ((null? lst) |
| 113 |
'()) |
| 114 |
(else (cons (rewrite (car lst)) |
| 115 |
(rewrite-args (cdr lst)))))) |
| 116 |
|
| 117 |
(define (rewrite-with-lemmas term lst) |
| 118 |
(cond ((or (not lst) (null? lst)) |
| 119 |
term) |
| 120 |
((one-way-unify term (cadr (car lst))) |
| 121 |
(rewrite (apply-subst unify-subst (caddr (car lst))))) |
| 122 |
(else (rewrite-with-lemmas term (cdr lst))))) |
| 123 |
|
| 124 |
(define (setup) |
| 125 |
(add-lemma-lst |
| 126 |
(quote ((equal (compile form) |
| 127 |
(reverse (codegen (optimize form) |
| 128 |
(nil)))) |
| 129 |
(equal (eqp x y) |
| 130 |
(equal (fix x) |
| 131 |
(fix y))) |
| 132 |
(equal (greaterp x y) |
| 133 |
(lessp y x)) |
| 134 |
(equal (lesseqp x y) |
| 135 |
(not (lessp y x))) |
| 136 |
(equal (greatereqp x y) |
| 137 |
(not (lessp x y))) |
| 138 |
(equal (boolean x) |
| 139 |
(or (equal x (t)) |
| 140 |
(equal x (f)))) |
| 141 |
(equal (iff x y) |
| 142 |
(and (implies x y) |
| 143 |
(implies y x))) |
| 144 |
(equal (even1 x) |
| 145 |
(if (zerop x) |
| 146 |
(t) |
| 147 |
(odd (_1- x)))) |
| 148 |
(equal (countps- l pred) |
| 149 |
(countps-loop l pred (zero))) |
| 150 |
(equal (fact- i) |
| 151 |
(fact-loop i 1)) |
| 152 |
(equal (reverse- x) |
| 153 |
(reverse-loop x (nil))) |
| 154 |
(equal (divides x y) |
| 155 |
(zerop (remainder y x))) |
| 156 |
(equal (assume-true var alist) |
| 157 |
(cons (cons var (t)) |
| 158 |
alist)) |
| 159 |
(equal (assume-false var alist) |
| 160 |
(cons (cons var (f)) |
| 161 |
alist)) |
| 162 |
(equal (tautology-checker x) |
| 163 |
(tautologyp (normalize x) |
| 164 |
(nil))) |
| 165 |
(equal (falsify x) |
| 166 |
(falsify1 (normalize x) |
| 167 |
(nil))) |
| 168 |
(equal (prime x) |
| 169 |
(and (not (zerop x)) |
| 170 |
(not (equal x (add1 (zero)))) |
| 171 |
(prime1 x (_1- x)))) |
| 172 |
(equal (and p q) |
| 173 |
(if p (if q (t) |
| 174 |
(f)) |
| 175 |
(f))) |
| 176 |
(equal (or p q) |
| 177 |
(if p (t) |
| 178 |
(if q (t) |
| 179 |
(f)) |
| 180 |
(f))) |
| 181 |
(equal (not p) |
| 182 |
(if p (f) |
| 183 |
(t))) |
| 184 |
(equal (implies p q) |
| 185 |
(if p (if q (t) |
| 186 |
(f)) |
| 187 |
(t))) |
| 188 |
(equal (fix x) |
| 189 |
(if (numberp x) |
| 190 |
x |
| 191 |
(zero))) |
| 192 |
(equal (if (if a b c) |
| 193 |
d e) |
| 194 |
(if a (if b d e) |
| 195 |
(if c d e))) |
| 196 |
(equal (zerop x) |
| 197 |
(or (equal x (zero)) |
| 198 |
(not (numberp x)))) |
| 199 |
(equal (plus (plus x y) |
| 200 |
z) |
| 201 |
(plus x (plus y z))) |
| 202 |
(equal (equal (plus a b) |
| 203 |
(zero)) |
| 204 |
(and (zerop a) |
| 205 |
(zerop b))) |
| 206 |
(equal (difference x x) |
| 207 |
(zero)) |
| 208 |
(equal (equal (plus a b) |
| 209 |
(plus a c)) |
| 210 |
(equal (fix b) |
| 211 |
(fix c))) |
| 212 |
(equal (equal (zero) |
| 213 |
(difference x y)) |
| 214 |
(not (lessp y x))) |
| 215 |
(equal (equal x (difference x y)) |
| 216 |
(and (numberp x) |
| 217 |
(or (equal x (zero)) |
| 218 |
(zerop y)))) |
| 219 |
(equal (meaning (plus-tree (append x y)) |
| 220 |
a) |
| 221 |
(plus (meaning (plus-tree x) |
| 222 |
a) |
| 223 |
(meaning (plus-tree y) |
| 224 |
a))) |
| 225 |
(equal (meaning (plus-tree (plus-fringe x)) |
| 226 |
a) |
| 227 |
(fix (meaning x a))) |
| 228 |
(equal (append (append x y) |
| 229 |
z) |
| 230 |
(append x (append y z))) |
| 231 |
(equal (reverse (append a b)) |
| 232 |
(append (reverse b) |
| 233 |
(reverse a))) |
| 234 |
(equal (times x (plus y z)) |
| 235 |
(plus (times x y) |
| 236 |
(times x z))) |
| 237 |
(equal (times (times x y) |
| 238 |
z) |
| 239 |
(times x (times y z))) |
| 240 |
(equal (equal (times x y) |
| 241 |
(zero)) |
| 242 |
(or (zerop x) |
| 243 |
(zerop y))) |
| 244 |
(equal (exec (append x y) |
| 245 |
pds envrn) |
| 246 |
(exec y (exec x pds envrn) |
| 247 |
envrn)) |
| 248 |
(equal (mc-flatten x y) |
| 249 |
(append (flatten x) |
| 250 |
y)) |
| 251 |
(equal (member x (append a b)) |
| 252 |
(or (member x a) |
| 253 |
(member x b))) |
| 254 |
(equal (member x (reverse y)) |
| 255 |
(member x y)) |
| 256 |
(equal (length (reverse x)) |
| 257 |
(length x)) |
| 258 |
(equal (member a (intersect b c)) |
| 259 |
(and (member a b) |
| 260 |
(member a c))) |
| 261 |
(equal (nth (zero) |
| 262 |
i) |
| 263 |
(zero)) |
| 264 |
(equal (exp i (plus j k)) |
| 265 |
(times (exp i j) |
| 266 |
(exp i k))) |
| 267 |
(equal (exp i (times j k)) |
| 268 |
(exp (exp i j) |
| 269 |
k)) |
| 270 |
(equal (reverse-loop x y) |
| 271 |
(append (reverse x) |
| 272 |
y)) |
| 273 |
(equal (reverse-loop x (nil)) |
| 274 |
(reverse x)) |
| 275 |
(equal (count-list z (sort-lp x y)) |
| 276 |
(plus (count-list z x) |
| 277 |
(count-list z y))) |
| 278 |
(equal (equal (append a b) |
| 279 |
(append a c)) |
| 280 |
(equal b c)) |
| 281 |
(equal (plus (remainder x y) |
| 282 |
(times y (quotient x y))) |
| 283 |
(fix x)) |
| 284 |
(equal (power-eval (big-plus1 l i base) |
| 285 |
base) |
| 286 |
(plus (power-eval l base) |
| 287 |
i)) |
| 288 |
(equal (power-eval (big-plus x y i base) |
| 289 |
base) |
| 290 |
(plus i (plus (power-eval x base) |
| 291 |
(power-eval y base)))) |
| 292 |
(equal (remainder y 1) |
| 293 |
(zero)) |
| 294 |
(equal (lessp (remainder x y) |
| 295 |
y) |
| 296 |
(not (zerop y))) |
| 297 |
(equal (remainder x x) |
| 298 |
(zero)) |
| 299 |
(equal (lessp (quotient i j) |
| 300 |
i) |
| 301 |
(and (not (zerop i)) |
| 302 |
(or (zerop j) |
| 303 |
(not (equal j 1))))) |
| 304 |
(equal (lessp (remainder x y) |
| 305 |
x) |
| 306 |
(and (not (zerop y)) |
| 307 |
(not (zerop x)) |
| 308 |
(not (lessp x y)))) |
| 309 |
(equal (power-eval (power-rep i base) |
| 310 |
base) |
| 311 |
(fix i)) |
| 312 |
(equal (power-eval (big-plus (power-rep i base) |
| 313 |
(power-rep j base) |
| 314 |
(zero) |
| 315 |
base) |
| 316 |
base) |
| 317 |
(plus i j)) |
| 318 |
(equal (gcd x y) |
| 319 |
(gcd y x)) |
| 320 |
(equal (nth (append a b) |
| 321 |
i) |
| 322 |
(append (nth a i) |
| 323 |
(nth b (difference i (length a))))) |
| 324 |
(equal (difference (plus x y) |
| 325 |
x) |
| 326 |
(fix y)) |
| 327 |
(equal (difference (plus y x) |
| 328 |
x) |
| 329 |
(fix y)) |
| 330 |
(equal (difference (plus x y) |
| 331 |
(plus x z)) |
| 332 |
(difference y z)) |
| 333 |
(equal (times x (difference c w)) |
| 334 |
(difference (times c x) |
| 335 |
(times w x))) |
| 336 |
(equal (remainder (times x z) |
| 337 |
z) |
| 338 |
(zero)) |
| 339 |
(equal (difference (plus b (plus a c)) |
| 340 |
a) |
| 341 |
(plus b c)) |
| 342 |
(equal (difference (add1 (plus y z)) |
| 343 |
z) |
| 344 |
(add1 y)) |
| 345 |
(equal (lessp (plus x y) |
| 346 |
(plus x z)) |
| 347 |
(lessp y z)) |
| 348 |
(equal (lessp (times x z) |
| 349 |
(times y z)) |
| 350 |
(and (not (zerop z)) |
| 351 |
(lessp x y))) |
| 352 |
(equal (lessp y (plus x y)) |
| 353 |
(not (zerop x))) |
| 354 |
(equal (gcd (times x z) |
| 355 |
(times y z)) |
| 356 |
(times z (gcd x y))) |
| 357 |
(equal (value (normalize x) |
| 358 |
a) |
| 359 |
(value x a)) |
| 360 |
(equal (equal (flatten x) |
| 361 |
(cons y (nil))) |
| 362 |
(and (nlistp x) |
| 363 |
(equal x y))) |
| 364 |
(equal (listp (gopher x)) |
| 365 |
(listp x)) |
| 366 |
(equal (samefringe x y) |
| 367 |
(equal (flatten x) |
| 368 |
(flatten y))) |
| 369 |
(equal (equal (greatest-factor x y) |
| 370 |
(zero)) |
| 371 |
(and (or (zerop y) |
| 372 |
(equal y 1)) |
| 373 |
(equal x (zero)))) |
| 374 |
(equal (equal (greatest-factor x y) |
| 375 |
1) |
| 376 |
(equal x 1)) |
| 377 |
(equal (numberp (greatest-factor x y)) |
| 378 |
(not (and (or (zerop y) |
| 379 |
(equal y 1)) |
| 380 |
(not (numberp x))))) |
| 381 |
(equal (times-list (append x y)) |
| 382 |
(times (times-list x) |
| 383 |
(times-list y))) |
| 384 |
(equal (prime-list (append x y)) |
| 385 |
(and (prime-list x) |
| 386 |
(prime-list y))) |
| 387 |
(equal (equal z (times w z)) |
| 388 |
(and (numberp z) |
| 389 |
(or (equal z (zero)) |
| 390 |
(equal w 1)))) |
| 391 |
(equal (greatereqpr x y) |
| 392 |
(not (lessp x y))) |
| 393 |
(equal (equal x (times x y)) |
| 394 |
(or (equal x (zero)) |
| 395 |
(and (numberp x) |
| 396 |
(equal y 1)))) |
| 397 |
(equal (remainder (times y x) |
| 398 |
y) |
| 399 |
(zero)) |
| 400 |
(equal (equal (times a b) |
| 401 |
1) |
| 402 |
(and (not (equal a (zero))) |
| 403 |
(not (equal b (zero))) |
| 404 |
(numberp a) |
| 405 |
(numberp b) |
| 406 |
(equal (_1- a) |
| 407 |
(zero)) |
| 408 |
(equal (_1- b) |
| 409 |
(zero)))) |
| 410 |
(equal (lessp (length (delete x l)) |
| 411 |
(length l)) |
| 412 |
(member x l)) |
| 413 |
(equal (sort2 (delete x l)) |
| 414 |
(delete x (sort2 l))) |
| 415 |
(equal (dsort x) |
| 416 |
(sort2 x)) |
| 417 |
(equal (length (cons x1 |
| 418 |
(cons x2 |
| 419 |
(cons x3 (cons x4 |
| 420 |
(cons x5 |
| 421 |
(cons x6 x7))))))) |
| 422 |
(plus 6 (length x7))) |
| 423 |
(equal (difference (add1 (add1 x)) |
| 424 |
2) |
| 425 |
(fix x)) |
| 426 |
(equal (quotient (plus x (plus x y)) |
| 427 |
2) |
| 428 |
(plus x (quotient y 2))) |
| 429 |
(equal (sigma (zero) |
| 430 |
i) |
| 431 |
(quotient (times i (add1 i)) |
| 432 |
2)) |
| 433 |
(equal (plus x (add1 y)) |
| 434 |
(if (numberp y) |
| 435 |
(add1 (plus x y)) |
| 436 |
(add1 x))) |
| 437 |
(equal (equal (difference x y) |
| 438 |
(difference z y)) |
| 439 |
(if (lessp x y) |
| 440 |
(not (lessp y z)) |
| 441 |
(if (lessp z y) |
| 442 |
(not (lessp y x)) |
| 443 |
(equal (fix x) |
| 444 |
(fix z))))) |
| 445 |
(equal (meaning (plus-tree (delete x y)) |
| 446 |
a) |
| 447 |
(if (member x y) |
| 448 |
(difference (meaning (plus-tree y) |
| 449 |
a) |
| 450 |
(meaning x a)) |
| 451 |
(meaning (plus-tree y) |
| 452 |
a))) |
| 453 |
(equal (times x (add1 y)) |
| 454 |
(if (numberp y) |
| 455 |
(plus x (times x y)) |
| 456 |
(fix x))) |
| 457 |
(equal (nth (nil) |
| 458 |
i) |
| 459 |
(if (zerop i) |
| 460 |
(nil) |
| 461 |
(zero))) |
| 462 |
(equal (last (append a b)) |
| 463 |
(if (listp b) |
| 464 |
(last b) |
| 465 |
(if (listp a) |
| 466 |
(cons (car (last a)) |
| 467 |
b) |
| 468 |
b))) |
| 469 |
(equal (equal (lessp x y) |
| 470 |
z) |
| 471 |
(if (lessp x y) |
| 472 |
(equal t z) |
| 473 |
(equal f z))) |
| 474 |
(equal (assignment x (append a b)) |
| 475 |
(if (assignedp x a) |
| 476 |
(assignment x a) |
| 477 |
(assignment x b))) |
| 478 |
(equal (car (gopher x)) |
| 479 |
(if (listp x) |
| 480 |
(car (flatten x)) |
| 481 |
(zero))) |
| 482 |
(equal (flatten (cdr (gopher x))) |
| 483 |
(if (listp x) |
| 484 |
(cdr (flatten x)) |
| 485 |
(cons (zero) |
| 486 |
(nil)))) |
| 487 |
(equal (quotient (times y x) |
| 488 |
y) |
| 489 |
(if (zerop y) |
| 490 |
(zero) |
| 491 |
(fix x))) |
| 492 |
(equal (get j (set i val mem)) |
| 493 |
(if (eqp j i) |
| 494 |
val |
| 495 |
(get j mem))))))) |
| 496 |
|
| 497 |
(define (tautologyp x true-lst false-lst) |
| 498 |
(cond ((truep x true-lst) |
| 499 |
#t) |
| 500 |
((falsep x false-lst) |
| 501 |
#f) |
| 502 |
((not (pair? x)) |
| 503 |
#f) |
| 504 |
((eq? (car x) |
| 505 |
(quote if)) |
| 506 |
(cond ((truep (cadr x) |
| 507 |
true-lst) |
| 508 |
(tautologyp (caddr x) |
| 509 |
true-lst false-lst)) |
| 510 |
((falsep (cadr x) |
| 511 |
false-lst) |
| 512 |
(tautologyp (cadddr x) |
| 513 |
true-lst false-lst)) |
| 514 |
(else (and (tautologyp (caddr x) |
| 515 |
(cons (cadr x) |
| 516 |
true-lst) |
| 517 |
false-lst) |
| 518 |
(tautologyp (cadddr x) |
| 519 |
true-lst |
| 520 |
(cons (cadr x) |
| 521 |
false-lst)))))) |
| 522 |
(else #f))) |
| 523 |
|
| 524 |
(define (tautp x) |
| 525 |
(tautologyp (rewrite x) |
| 526 |
'() '())) |
| 527 |
|
| 528 |
(define (test alist term) |
| 529 |
(tautp |
| 530 |
(apply-subst alist term))) |
| 531 |
|
| 532 |
(define (trans-of-implies n) |
| 533 |
(list (quote implies) |
| 534 |
(trans-of-implies1 n) |
| 535 |
(list (quote implies) |
| 536 |
0 n))) |
| 537 |
|
| 538 |
(define (trans-of-implies1 n) |
| 539 |
(cond ((equal? n 1) |
| 540 |
(list (quote implies) |
| 541 |
0 1)) |
| 542 |
(else (list (quote and) |
| 543 |
(list (quote implies) |
| 544 |
(- n 1) |
| 545 |
n) |
| 546 |
(trans-of-implies1 (- n 1)))))) |
| 547 |
|
| 548 |
(define (truep x lst) |
| 549 |
(or (equal? x (quote (t))) |
| 550 |
(member x lst))) |
| 551 |
|
| 552 |
|
| 553 |
(define (main . args) |
| 554 |
(run-benchmark |
| 555 |
"boyer" |
| 556 |
boyer-iters |
| 557 |
(lambda (result) (equal? result #t)) |
| 558 |
(lambda (alist term) (lambda () (test alist term))) |
| 559 |
(quote ((x f (plus (plus a b) |
| 560 |
(plus c (zero)))) |
| 561 |
(y f (times (times a b) |
| 562 |
(plus c d))) |
| 563 |
(z f (reverse (append (append a b) |
| 564 |
(nil)))) |
| 565 |
(u equal (plus a b) |
| 566 |
(difference x y)) |
| 567 |
(w lessp (remainder a b) |
| 568 |
(member a (length b))))) |
| 569 |
(quote (implies (and (implies x y) |
| 570 |
(and (implies y z) |
| 571 |
(and (implies z u) |
| 572 |
(implies u w)))) |
| 573 |
(implies x w))))) |
| 574 |
|
| 575 |
(setup)) |