Debian Codegraph @

benchmarks.larceny/src/nboyer.scm

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