Debian Codegraph @

benchmarks.larceny/src/sboyer.scm

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