Debian Codegraph @

benchmarks/rnrs-benchmarks/nboyer.ss

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