forked from acl2/acl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdd.lisp
3276 lines (2821 loc) · 132 KB
/
bdd.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ACL2 Version 8.5 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2024, Regents of the University of Texas
; This version of ACL2 is a descendent of ACL2 Version 1.9, Copyright
; (C) 1997 Computational Logic, Inc. See the documentation topic NOTE-2-0.
; This program is free software; you can redistribute it and/or modify
; it under the terms of the LICENSE file distributed with ACL2.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; LICENSE for more details.
; Written by: Matt Kaufmann and J Strother Moore
; email: [email protected] and [email protected]
; Department of Computer Science
; University of Texas at Austin
; Austin, TX 78712 U.S.A.
; Table of contents.
;
; 0. PRELIMINARY MACROS
; I. INTRODUCTION AND DATA TYPES
; II. OP-ALIST
; III. HASH OPERATIONS
; IV. HASH OPERATIONS: QUOTEPS
; V. BDD RULES AND ONE-WAY UNIFIER
; VI. SOME INTERFACE UTILITIES
; VII. MAIN ALGORITHM
; VIII. TOP-LEVEL (INTERFACE) ROUTINES
; IX. COMPILING THIS FILE AND OTHER HELPFUL TIPS
;
; Mx-id-bound is currently 438619, perhaps too low. We could perhaps fix this
; by changing how we deal with close to 16 args in op-hash-index1, and by
; changing 131 in if-hash-index.
(in-package "ACL2")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; 0. PRELIMINARY MACROS ;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro mvf (x &rest rest)
; We often wish to pass back a multiple value such that the first value is a
; fixnum. Efficiency is apparently improved in GCL when that fixnum is not
; "boxed," but instead is treated as a raw C integer. Currently ACL2 provides
; mechanisms for this, but they require that an appropriate THE expression
; surround such a value when it is the first value passed back by MV. (Note
; that there seems to be no way to keep GCL from boxing fixnums in other than
; the first argument position of MV.)
`(mv (the-fixnum ,x) ,@rest))
(defmacro logandf (&rest args)
(xxxjoin-fixnum 'logand args -1))
(defmacro logxorf (&rest args)
(xxxjoin-fixnum 'logxor args 0))
(defmacro logiorf (&rest args)
(xxxjoin-fixnum 'logior args 0))
(defmacro ashf (x y)
(list 'the-fixnum
(list 'ash (list 'the-fixnum x) (list 'the-fixnum y))))
(defmacro mx-id-bound ()
; This bound on mx-id must be such that our calls of +f and *f involving mx-id
; produce fixnums. At this writing the most severe such test is in
; op-hash-index1; see the comment there.
(1- (floor (fixnum-bound) 153)))
(defmacro 1+mx-id (x)
; DILEMMA: Do we let this macro box (1+ x) or not, and if so, when? Here are
; some thoughts on the issue.
; Use this macro to increment mx-id,in order to guarantee that mx-id remains a
; fixnum. X is known to be a nonnegative fixnum; this macro checks that we
; keep it a fixnum by adding 1 to it. It actually checks even more, namely,
; that
`(the-fixnum
(let ((x ,x))
(declare (type #.*fixnum-type* x))
; Should we really include the declaration above? The main reason seems to be
; in order for the incrementing operation below to run fast, but in fact we
; have done several experiments and it seems that the current version of this
; code is optimal for performance. That's a bit surprising, since each mx-id
; gets consed into a list anyhow (a cst), and hence is boxed in GCL (which is
; the only list we are talking about here). So, there wouldn't appear to be
; any particular advantage in wrapping the-fixnum around this form. At any
; rate, the performance issues here seem to be quite minor.
; A typical use of this macro is of the form
; (let ((new-mx-id (1+mx-id mx-id)))
; (declare (type #.*fixnum-type* new-mx-id))
; (let ((new-cst (make-leaf-cst
; new-mx-id
; term
; nil)))
; (mvf new-mx-id
; ...)))
; Note that make-leaf-cst will box new-mx-id -- after all, it is consing
; new-mx-id into a list. The present approach delays this boxing until that
; time, so that we don't have to unbox new-mx-id in the mvf form above. The
; unboxed new-mx-id may actually never benefit from being unboxed, and in fact
; we may want to rework our entire bdd code so that mx-ids are always boxed.
(cond ((< x ,(mx-id-bound))
(1+f x))
(t (the-fixnum ; the-fixnum call may help with proclaiming
(er-hard-val 0 'bdd
"Maximum id for bdds exceeded. Current maximum id is ~x0."
x)))))))
(defmacro bdd-error (mx-id fmt-string fmt-alist bad-cst ttree)
; Perhaps it would be more "natural" to define this macro to return
; `(mvf ,mx-id ,(cons fmt-string ,fmt-alist) ,bad-cst <nil-call-stack> ,ttree)
; since then we can view the result as
; `(mvf ,mx-id ,<msg> ,bad-cst ,call-stack ,ttree)
; However, we would like to have a very fast test for whether the tuple
; returned designates an error. The present approach allows us to test with
; stringp on the second value returned. We take advantage of that in the
; definition of bdd-mv-let.
; Note that the order of the first two values should not be changed: we
; declare mx-id to be a fixnum at some point, and we we want the second
; position to be tested by stringp to see if we have an "error" situation.
; Keep this in sync with bdd-mv-let.
`(mvf ,mx-id ,fmt-string (cons ,fmt-alist ,bad-cst)
; The following nil is really an initial value of the bdd-call-stack that is
; ultimately to be placed in a bddnote. At the time of this writing,
; bdd-mv-let is the only place where we update this stack.
nil
,ttree))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; I. INTRODUCTION AND DATA TYPES ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; In this work we represent terms in what we call the cheap syntax. Such a
; "term" is called a "csterm". Bryant would call it a "node."
; We are interested in normalized IF expressions corresponding to ACL2 terms.
; If x is not itself an IF term, then (IF x y z) is represented by
; `(k ,x ,boolp ,y . ,z)
; where k is a natural number that is uniquely associated with <x,y,z> and
; boolp is t if the term is known to be Boolean. The association between k and
; <x,y,z> is arranged via a "hash table" discussed below. The objective is
; that two canonicalized IF expressions are equal (and therefore represent the
; same term) iff their unique identifiers (cars) are =.
; We also represent "leaf" ACL2 terms, which are generally IF-free, as csts of
; a slightly different sort; see below. (Note that these may have IFs in them
; because certain function symbols are "blocked" -- see bdd-constructors.)
; The list of "leaf" csts arising from variables in the input term, which we
; typically call leaf-cst-list, is passed around unchanged by various of our
; functions. We rely on the op-ht to find csts for other leaves, and to avoid
; re-consing up leaf csts.
; The shapes of csts are as follows. Note that two csts are equal iff their
; cars are =.
; Non-leaf:
; (unique-id tst boolp tbr . fbr) ; informally, represents (if tst tbr fbr)
; Leaf:
; (unique-id term boolp)
; where term is of one of the following forms:
; variable
; quotep
; application of function symbol other than IF to a list of csts
; WARNING: The definition of leafp below relies on the fact that leaf csts are
; exactly those whose final cdr is nil. Do not succumb to the temptation to
; add a new field as the final cdr without taking this into account.
; Note: It is tempting to replace the "term" in the last case by an ACL2 term,
; rather than an application of a function symbol to a list of csts. However,
; the list of csts has presumably already been consed up, so we save the
; re-consing, awaiting the final decoding to build the actual ACL2 term if
; necessary.
; Macros for accessing canonicalized IFs:
(defmacro unique-id (x) `(the-fixnum (car ,x)))
(defmacro tst (x) `(cadr ,x)) ;a cst, not a number; but beware since tst=trm
;and trm is a sort of term
; Note: We found that 95% of the time on one test was being spent inside
; cst-boolp, when we used to keep this information directly in leaf nodes only.
(defmacro cst-boolp (x) `(caddr ,x))
(defmacro tbr (x) `(cadddr ,x))
(defmacro fbr (x) `(cddddr ,x))
(defmacro leafp (x)
`(null (cdddr ,x)))
(defmacro trm (x) `(cadr ,x))
(defun bdd-constructors (wrld)
(declare (xargs :guard
(and (plist-worldp wrld)
(alistp (table-alist 'acl2-defaults-table wrld)))))
(let ((pair (assoc-eq :bdd-constructors (table-alist
'acl2-defaults-table wrld))))
(if pair
(cdr pair)
'(cons))))
(defun make-leaf-cst (unique-id trm boolp)
; We write the definition this way, rather than simply as something like (list*
; unique-id trm boolp ), in order to avoid repeatedly consing up '(t . nil) and
; '(nil . nil).
(if boolp
(list* unique-id trm '(t))
(list* unique-id trm '(nil))))
(defun evg-fn-symb (x)
; This function takes the view that every explicit value can be constructed
; from 0. It returns nil on 0, but, in principle, returns an appropriate
; function symbol otherwise. At this point we choose not to support this idea
; in full, but instead we view cons as the only constructor. We leave the full
; code in place as a comment, in case we choose to support this idea in the
; future.
; (cond ((consp x) 'cons)
; ((symbolp x) 'intern-in-package-of-symbol)
; ((integerp x)
; (cond ((< x 0) 'unary--)
; ((< 0 x) 'binary-+)
; (t nil)))
; ((rationalp x)
; (if (equal (numerator x) 1)
; 'unary-/
; 'binary-*))
; ((complex-rationalp x)
; 'complex)
; ((stringp x) 'coerce)
; ((characterp x) 'char-code)
; (t (er hard 'fn-symb "Unexpected object, ~x0."
; x)))
(cond ((consp x) 'cons)
(t nil)))
(defun bdd-constructor-trm-p (trm bdd-constructors)
(and (consp trm)
(if (fquotep trm)
(member-eq (evg-fn-symb (cadr trm)) bdd-constructors)
(member-eq (car trm) bdd-constructors))))
(defun evg-type (x)
; This function takes the view that every explicit value can be constructed
; from 0. It returns nil on 0, but, in principle, returns an appropriate
; function symbol otherwise. See also evg-fn-symb.
(cond ((consp x) 'consp)
((symbolp x) 'symbol)
((integerp x) 'integer)
((rationalp x) 'rational)
((complex-rationalp x) 'complex-rational)
((stringp x) 'string)
((characterp x) 'character)
(t (er hard 'fn-symb "Unexpected object, ~x0."
x))))
(defun make-if-cst (unique-id tst tbr fbr bdd-constructors)
; The second value returned is always a new cst. The first value is non-nil
; when there is an "error", in which case that value is of the form (fmt-string
; . alist).
(let* ((boolp (and (cst-boolp tbr)
(cst-boolp fbr)))
(new-cst (list* unique-id tst boolp tbr fbr)))
(cond
((or (and (leafp tbr)
(bdd-constructor-trm-p (trm tbr) bdd-constructors))
(and (leafp fbr)
(bdd-constructor-trm-p (trm fbr) bdd-constructors)))
(mv (msg "Attempted to create IF node during BDD processing with ~@0, ~
which would produce a non-BDD term (as defined in :DOC ~
bdd-algorithm). See :DOC show-bdd."
(let ((true-fn (trm tbr))
(false-fn (trm fbr)))
(cond
((and (leafp tbr)
(bdd-constructor-trm-p (trm tbr) bdd-constructors))
(cond
((and (leafp fbr)
(bdd-constructor-trm-p (trm fbr) bdd-constructors))
(msg "true branch with ~#0~[function symbol ~x1~/explicit ~
value of type ~x2~] and false branch with ~
~#3~[function symbol ~x4~/explicit value of type ~
~x5~]"
(if (eq (car true-fn) 'quote) 1 0)
(car true-fn)
(and (eq (car true-fn) 'quote)
(evg-type (cadr true-fn)))
(if (eq (car false-fn) 'quote) 1 0)
(car false-fn)
(and (eq (car false-fn) 'quote)
(evg-type (cadr false-fn)))))
(t (msg "true branch with ~#0~[function symbol ~x1~/explicit ~
value of type ~x2~]"
(if (eq (car true-fn) 'quote) 1 0)
(car true-fn)
(and (eq (car true-fn) 'quote)
(evg-type (cadr true-fn)))))))
(t (msg "false branch with ~#0~[function symbol ~x1~/explicit ~
value of type ~x2~]"
(if (eq (car false-fn) 'quote) 1 0)
(car false-fn)
(and (eq (car false-fn) 'quote)
(evg-type (cadr false-fn))))))))
new-cst))
(t (mv nil new-cst)))))
; We will always represent nil and t as described above. To make this work, we
; must set the initial mx-id to 2, so the next unique id generated is 3.
; It is nearly inconsequential which of t or nil has the smaller id, but we
; find it handy to give t the smaller id, as noted in a comment in the
; definition of combine-op-csts-comm.
(defconst *cst-t* (make-leaf-cst 1 *t* t))
(defconst *cst-nil* (make-leaf-cst 2 *nil* t))
(defmacro cst= (cst1 cst2)
`(= (unique-id ,cst1)
(unique-id ,cst2)))
(defmacro cst-tp (cst)
`(= (unique-id ,cst) 1))
(defmacro cst-nilp (cst)
`(= (unique-id ,cst) 2))
(defmacro cst-varp (cst)
`(< 2 (unique-id ,cst)))
(defun cst-nonnilp (cst)
(and (leafp cst)
(if (quotep (trm cst))
(not (cst-nilp cst))
; Consider other types here besides cons, e.g., that of numbers. We may want
; to pass in a list of functions that have been checked to have type-sets that
; are disjoint from *ts-nil* and variable-free. We would use a member-eq test
; below against such a list. This list of function symbols could be determined
; easily from the list of all function symbols in op-alist.
(ffn-symb-p (trm cst) 'cons))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; II. OP-ALIST ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The goal of this section is to define functions op-alist and op-alist-info.
; See those definitions below for more details. Briefly, these functions
; respectively build and do lookup in a so-called op-alist, which is a list of
; entries that describe function symbols occurring in the term for which we
; want to build a bdd.
(defun bool-mask1 (formals vars rune)
; Formals is the list of formals of a function symbol, and vars is a list of
; variables contained in formals such that every call of that function returns
; either t or nil, assuming that each var in vars is of boolean type. This
; function returns a list in one-one correspondence with formals (but see
; below), replacing a formal by t if it belongs to vars (thus indicating that
; this position's actual might be returned) and nil if not. Rune is a
; type-prescription record, used simply for the final cdr of the list returned
; (after all the t's and nil's have been listed as indicated above).
(cond
((endp formals) rune)
((member-eq (car formals) vars)
(cons t (bool-mask1 (cdr formals) vars rune)))
(t (cons nil (bool-mask1 (cdr formals) vars rune)))))
(defun boolean-term-var (x)
; X is a term. If x is of the form (booleanp v) or something "clearly"
; equivalent to it, return v. Otherwise return nil.
(and (not (variablep x))
(not (fquotep x))
(cond
((and (eq (ffn-symb x) 'booleanp)
(variablep (fargn x 1)))
(fargn x 1))
((eq (ffn-symb x) 'if)
; Check for translated version of (or (equal v t) (equal v nil)) or
; (or (equal v nil) (equal v t)).
(let ((test (fargn x 1))
(tbr (fargn x 2))
(fbr (fargn x 3)))
(and (ffn-symb-p test 'equal)
(let ((v (fargn test 1)))
(and (variablep v)
(let ((b (fargn test 2)))
(and (or (equal b *t*) (equal b *nil*))
(let ((c (if (equal b *t*) *nil* *t*)))
(if (and (equal test tbr)
(equal fbr (fcons-term* 'equal v c)))
v
nil)))))))))
(t nil))))
(defun boolean-hyps-vars (hyps)
; If hyps consists of terms of the form (booleanp v), or perhaps the
; equivalent, then we return a list indices of such v.
(if (endp hyps)
nil
(let ((rst (boolean-hyps-vars (cdr hyps))))
(if (eq rst t)
t
(let ((v (boolean-term-var (car hyps))))
(if v
(cons v rst)
t))))))
(defun first-boolean-type-prescription (type-prescription-list ens formals)
; This function finds the most recent enabled type-prescription rule from the
; given list whose :basic-ts is boolean and :hyps are all of the form (booleanp
; v) or a "clearly" equivalent form, where the :term is of the form (fn ... v
; ...). It returns two values. The first is the :rune of the rule, which is
; non-nil if and only if such a rule is found. If the first value is non-nil,
; then the second value is a "mask" as described in the comment in bool-mask.
(cond
((endp type-prescription-list)
(mv nil nil))
((and (enabled-numep
(access type-prescription (car type-prescription-list) :nume)
ens)
(ts-subsetp
(access type-prescription (car type-prescription-list) :basic-ts)
*ts-boolean*))
(let* ((tp (car type-prescription-list))
(hyps (access type-prescription tp :hyps))
(vars (access type-prescription tp :vars)))
(if hyps
(let ((more-vars (boolean-hyps-vars hyps)))
(if (or (eq more-vars t)
(not (subsetp-eq more-vars formals)))
(first-boolean-type-prescription (cdr type-prescription-list)
ens
formals)
(mv (access type-prescription tp :rune)
(union-eq vars more-vars))))
(mv (access type-prescription tp :rune)
vars))))
(t (first-boolean-type-prescription
(cdr type-prescription-list) ens formals))))
(defun recognizer-rune (recognizer-alist wrld ens)
(cond
((endp recognizer-alist) nil)
((enabled-runep (access recognizer-tuple (car recognizer-alist) :rune)
ens
wrld)
(access recognizer-tuple (car recognizer-alist) :rune))
(t (recognizer-rune (cdr recognizer-alist) wrld ens))))
(defun bool-mask (fn wrld ens)
; Returns a "mask" that is a suitable argument to bool-flg. Thus, this
; function returns either nil or else a mask of the form
; (list* b1 b2 ... bn rune)
; where rune is a type prescription rune and each bi is either t or nil. The
; function bool-flg will check that a given call of fn is boolean, returning
; rune if it can confirm this fact. A bi must be marked t if the conclusion
; that the call of fn is boolean requires a check that the formal corresponding
; to bi is boolean.
; We give special treatment not only to compound recognizers, but also to
; Boolean-valued primitives, since these will not generally have built-in
; type-prescription rules.
(cond
((or (eq fn 'equal) (eq fn '<))
(list* nil nil *fake-rune-for-type-set*))
((eq fn 'not)
; `Not' is so basic that we could probably skip this case, but we might as well
; handle it appropriately.
(list* nil *fake-rune-for-type-set*))
(t
(let ((rune (recognizer-rune (getpropc fn 'recognizer-alist nil wrld)
wrld
ens))
(formals (formals fn wrld)))
(if rune
(bool-mask1 formals nil rune)
(mv-let (rune vars)
; We only consider the most recent type prescription with Boolean base type.
; Some day we might consider somehow combining all such type prescription
; rules.
(first-boolean-type-prescription
(getpropc fn 'type-prescriptions nil wrld)
ens
formals)
(and rune
(bool-mask1 formals vars rune))))))))
(defun commutative-p1 (fn lemmas ens)
; Fn is assumed to have arity 2 in the current ACL2 world.
(cond
((endp lemmas) nil)
(t (if (and (member-eq (access rewrite-rule (car lemmas) :subclass)
'(backchain abbreviation))
(equal (access rewrite-rule (car lemmas) :equiv) 'equal)
(enabled-numep (access rewrite-rule (car lemmas) :nume) ens)
(null (access rewrite-rule (car lemmas) :hyps))
(let ((lhs (access rewrite-rule (car lemmas) :lhs))
(rhs (access rewrite-rule (car lemmas) :rhs)))
(and (or (eq (ffn-symb lhs) fn)
(er hard 'commutative-p1
"We had thought we had a rewrite rule with :lhs ~
being a call of ~x0, but the :lhs is ~x1."
fn lhs))
(ffn-symb-p rhs fn)
(variablep (fargn lhs 1))
(variablep (fargn lhs 2))
(not (eq (fargn lhs 1) (fargn lhs 2)))
(equal (fargn lhs 1) (fargn rhs 2))
(equal (fargn lhs 2) (fargn rhs 1)))))
(access rewrite-rule (car lemmas) :rune)
(commutative-p1 fn (cdr lemmas) ens)))))
(defun find-equivalence-rune (fn rules)
(cond
((endp rules)
nil)
((eq (access congruence-rule (car rules) :equiv) fn)
(let ((rune (access congruence-rule (car rules) :rune)))
(if (eq (car rune) :equivalence)
rune
(find-equivalence-rune fn (cdr rules)))))
(t (find-equivalence-rune fn (cdr rules)))))
(defun equivalence-rune1 (fn congruences)
; For example, if fn is 'iff then congruences may contain:
; (EQUAL ((126 IFF :EQUIVALENCE IFF-IS-AN-EQUIVALENCE))
; ((126 IFF :EQUIVALENCE IFF-IS-AN-EQUIVALENCE)))
; But the two singleton lists above can contain other members too. See the
; Essay on Equivalence, Refinements, and Congruence-based Rewriting.
; See add-equivalence-rule.
(cond
((endp congruences)
(er hard 'equivalence-rune
"Failed to find an equivalence rune for function symbol ~x0."
fn))
(t (let ((x (car congruences)))
(case-match x
(('equal rules1 rules2)
(let ((rune (find-equivalence-rune fn rules1)))
(if (and rune
(equal rune (find-equivalence-rune fn rules2)))
rune
(equivalence-rune1 fn (cdr congruences)))))
(& (equivalence-rune1 fn (cdr congruences))))))))
(defun equivalence-rune (fn wrld)
(declare (xargs :guard (equivalence-relationp fn wrld)))
(cond
((eq fn 'equal)
(fn-rune-nume 'equal nil nil wrld))
(t (equivalence-rune1 fn
(getpropc fn 'congruences
'(:error "See equivalence-rune.")
wrld)))))
(defun commutative-p (fn ens wrld)
; Note that if the value is non-nil, it is a rune justifying the commutativity
; of the given function.
(and (= (arity fn wrld) 2)
(if (equivalence-relationp fn wrld)
(equivalence-rune fn wrld)
(commutative-p1 fn
(getpropc fn 'lemmas nil wrld)
ens))))
; To memoize the various merging operations we will hash on the opcodes.
; Increasing each by a factor of 3 was intended to make it spread out a little
; more, but (at least this has been true at one time) it doesn't have much of
; an effect.
(defun op-alist (fns acc i ens wrld)
; Build a list of entries (op opcode comm-p enabled-executable-counterpartp
; mask). The next index to use is i. Call this as in (op-alist (remove1-eq
; 'if (all-fnnames term)) nil 6 (ens state) (w state)). Keep this in sync with
; op-alist-info.
; Note that if comm-p is non-nil, it is a rune justifying the commutativity of
; the given function. Similarly, if enabled-executable-counterpartp is non-nil
; then it is an :executable-counterpart rune.
(cond
((endp fns) acc)
((> i (mx-id-bound))
(er hard 'bdd
"We are very surprised to see that, apparently, your problem for bdd ~
processing involves ~x0 function symbols! We cannot handle this many ~
function symbols."
(/ i 3)))
(t (op-alist (cdr fns)
(cons (list* (car fns)
i
(commutative-p (car fns) ens wrld)
(and (not (getpropc (car fns) 'constrainedp nil
wrld))
(enabled-xfnp (car fns) ens wrld)
(fn-rune-nume (car fns) nil t wrld))
(bool-mask (car fns) wrld ens))
acc)
(+ 3 i)
ens
wrld))))
(defun op-alist-info (fn op-alist)
; Returns (mv opcode comm-p enabled-exec-p mask). Keep this in sync with
; op-alist.
(cond
((endp op-alist)
(mv (er hard 'op-alist-info
"Function not found: ~x0"
fn)
nil nil nil))
((eq fn (caar op-alist))
(let ((info (cdar op-alist)))
(mv (car info)
(cadr info)
(caddr info)
(cdddr info))))
(t (op-alist-info fn (cdr op-alist)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; III. HASH OPERATIONS ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro if-op-code () 3)
(defmacro hash-size ()
; Do not quote the body of this definition. We want it computed at
; definition/compile time!
; At one time we used a defconst here, but then we realized that we would
; (apparently, at least, in GCL) have to pay the price both of looking up the
; value of that variable and also unboxing it for fixnum operations. Although
; a little experimentation did not yield meaningful evidence that there is
; really an impact on performance, we proceed with a macro.
; WARNING: Do not increase this size too close to (fixnum-bound). See the
; warning in op-hash-index-evg.
(1- (expt 2 15)))
; We have two hash arrays, 'if-ht for assigning unique-ids to csts, and 'op-ht
; for memo-izing the merge operators. In each case the array assigns "buckets"
; to indices.
; Buckets in an if-ht are lists of non-leaf csts.
; Buckets in an op-ht are lists of entries of the form (cst op . args), where
; args is a list of csts. The length of the list is the arity of op.
; Exception: op can be quote, in which case args is a list containing a single
; evg.
(defmacro if-hash-index (x y z)
; Note that (+ 131 2 1) does not exceed 153. See the comment about mx-id-bound
; in op-hash-index1. There is probably nothing sacred about the choices of
; these three numbers 131, 2, and 1, although it seems good that they are
; relatively prime.
`(logandf (+f (*f 131 (unique-id ,x))
(*f 2 (unique-id ,y))
(unique-id ,z))
(hash-size)))
(defun op-hash-index1 (args i acc)
; There should be no more than 16 args before we "turn around", so that the
; multiplier on unique-ids is no more than (1+ (+ 2 3 ... 17)) = 153. (The
; `1+' is because in op-hash-index we add in the op-code as well. Op-codes are
; also bounded by mx-id-bound -- see op-alist -- as are of course unique-ids.)
; See the comment in mx-id-bound.
; If we want to increase the (mx-id-bound), we believe that we could start the
; "turnaround" here earlier. But we have not yet checked this claim carefully.
(declare (type #.*fixnum-type* i acc)
(xargs :measure (acl2-count args)))
(the-fixnum
(cond
((endp args) (if (< acc 0) (- acc) acc))
((or (= (the-fixnum i) 18)
(= (the-fixnum i) -1))
(if (> acc 0)
(op-hash-index1 args -17 acc)
(op-hash-index1 args 2 acc)))
(t (op-hash-index1 (cdr args)
(1+f i)
(+f acc
(*f i
(unique-id (car args)))))))))
(defmacro op-hash-index (op-code args)
`(logandf (+f ,op-code
(op-hash-index1 ,args 2 1))
(hash-size)))
(defmacro op-hash-index-2 (op-code arg1 arg2)
; This special case of op-hash-index is used for commutative operators in
; chk-memo-2.
`(logandf (+f ,op-code
(*f 2 (unique-id ,arg1))
(*f 3 (unique-id ,arg2)))
(hash-size)))
(defmacro op-hash-index-if (arg1 arg2 arg3)
`(logandf (+f (if-op-code)
(*f 2 (unique-id ,arg1))
(*f 3 (unique-id ,arg2))
(*f 4 (unique-id ,arg3)))
(hash-size)))
; Having found the bucket associated with the hash-index, here is how
; we search it.
(defun if-search-bucket (x y z lst)
; Here lst is a list of non-leaf csts.
(cond ((null lst) nil)
((and (cst= x (tst (car lst)))
(cst= y (tbr (car lst)))
(cst= z (fbr (car lst))))
(car lst))
(t (if-search-bucket x y z (cdr lst)))))
(defun cst=-lst (x y)
(cond
((endp x) t)
(t (and (cst= (car x) (car y))
(cst=-lst (cdr x) (cdr y))))))
(defmacro eq-op (x y)
; This test must change if we start allowing LAMBDAs as operators.
`(eq ,x ,y))
(defun op-search-bucket (op args lst)
; Here op is a function symbol and lst is a tail of a bucket from an op-ht.
; Thus, lst is a list of elements of the form (cst op0 . args0), where args0 is
; a list of csts unless op0 is 'quote, which it is not if op0 is op.
(cond ((null lst) nil)
((and (eq-op op
(cadr (car lst)))
(cst=-lst args (cddr (car lst))))
(car (car lst)))
(t (op-search-bucket op args (cdr lst)))))
(defun op-search-bucket-2 (op arg1 arg2 lst)
; This is a version of op-search-bucket for binary functions. This is merely
; an optimization we use for commutative operators, since we know that they are
; binary. We could of course use this for all binary operators, but the point
; here is that for commutative operators we delay consing up the commuted
; argument list until it is necessary. See combine-op-csts-comm.
(cond ((null lst) nil)
((and (eq-op op
(cadr (car lst)))
(let ((args (cddr (car lst))))
(and (cst= arg1 (car args))
(cst= arg2 (cadr args)))))
(car (car lst)))
(t (op-search-bucket-2 op arg1 arg2 (cdr lst)))))
(defun op-search-bucket-if (arg1 arg2 arg3 lst)
; This is a version of op-search-bucket that does not require us to cons up the
; arguments into a list, used in chk-memo-if. This is merely an optimization
; we use since IF is such a common operation.
(cond ((null lst) nil)
((and (eq-op 'if
(cadr (car lst)))
(let ((args (cddr (car lst))))
(and (cst= arg1 (car args))
(cst= arg2 (cadr args))
(cst= arg3 (caddr args)))))
(car (car lst)))
(t (op-search-bucket-if arg1 arg2 arg3 (cdr lst)))))
(defun chk-memo (op-code op args op-ht)
; If <op,arg1,arg2,...> has an entry in op-ht, return 0 and the entry.
; Otherwise, return the hash index for <op-code,arg1,arg2,...> (simply to avoid
; its recomputation) and NIL. Entries are of the form (result op . args). We
; return the hash index as the first value so that we can avoid boxing up a
; fixnum for it in GCL.
(declare (type #.*fixnum-type* op-code))
(the-mv
2
#.*fixnum-type*
(let ((n (op-hash-index op-code args)))
(declare (type #.*fixnum-type* n))
(let ((ans (op-search-bucket op args (aref1 'op-ht op-ht n))))
(cond (ans (mvf 0 ans))
(t (mvf n nil)))))))
(defun chk-memo-2 (op-code op arg1 arg2 op-ht)
; This is merely an optimization of chk-memo for the case where the operator is
; binary, in particularly for commutative operators; see the comment in
; op-search-bucket-2.
(declare (type #.*fixnum-type* op-code))
(the-mv
2
#.*fixnum-type*
(let ((n (op-hash-index-2 op-code arg1 arg2)))
(declare (type #.*fixnum-type* n))
(let ((ans (op-search-bucket-2 op arg1 arg2 (aref1 'op-ht op-ht n))))
(cond (ans (mvf 0 ans))
(t (mvf n nil)))))))
(defun chk-memo-if (arg1 arg2 arg3 op-ht)
; This is merely an optimization of chk-memo for the case where the operator is
; if, which is likely very common. Note that by treating this special case as
; we do, we avoid consing up the list of arguments in some cases; see
; combine-if-csts.
(the-mv
2
#.*fixnum-type*
(let ((n (op-hash-index-if arg1 arg2 arg3)))
(declare (type #.*fixnum-type* n))
(let ((ans (op-search-bucket-if arg1 arg2 arg3 (aref1 'op-ht op-ht n))))
(cond (ans (mvf 0 ans))
(t (mvf n nil)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; IV. HASH OPERATIONS: QUOTEPS ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro half-hash-size ()
(floor (hash-size) 2))
(defmacro fourth-hash-size ()
(floor (hash-size) 4))
(defun op-hash-index-string (index acc string)
(declare (type #.*fixnum-type* index acc))
(the-fixnum
(cond
((= index 0) acc)
(t (let ((index (1- (the-fixnum index))))
(declare (type #.*fixnum-type* index))
(op-hash-index-string
index
(logandf (hash-size)
(+f acc (char-code (char string index))))
string))))))
(defun op-hash-index-evg (evg)
(the-fixnum
(cond
((integerp evg)
(logandf (hash-size) evg))
((rationalp evg)
(logandf (hash-size)
(+ (numerator evg)
(* 17 (denominator evg)))))
((acl2-numberp evg)
(logandf (hash-size)
(+f (op-hash-index-evg (realpart evg))
(op-hash-index-evg (imagpart evg)))))
((characterp evg)
(+f (fourth-hash-size)
(char-code evg)))
((symbolp evg)
(logandf (hash-size)
; WARNING: Here we assume that (* 19 (hash-size)) is a fixnum. We know it is
; because the hash index is at most (hash-size), which is well under
; (fixnum-bound).
(*f 19 (op-hash-index-evg (symbol-name evg)))))
((stringp evg)
(the-fixnum
(op-hash-index-string (the-fixnum! (length evg) 'bdd)
(half-hash-size) evg)))
(t ;cons
(logandf (hash-size)
(+f (op-hash-index-evg (car evg))
(*f 2 (op-hash-index-evg (cdr evg)))))))))
(defun op-search-bucket-quote (evg bucket)
(cond ((null bucket) nil)
((and (eq-op 'quote
(cadr (car bucket)))
(equal evg (caddr (car bucket))))
(car (car bucket)))
(t (op-search-bucket-quote evg (cdr bucket)))))
(defun chk-memo-quotep (term op-ht)
(the-mv
2
#.*fixnum-type*
(let ((n (op-hash-index-evg (cadr term))))
(declare (type #.*fixnum-type* n))
(let ((ans (op-search-bucket-quote
(cadr term)
(aref1 'op-ht op-ht n))))
; One might think that the calls of the-fixnum just below are not necessary,
; but in fact they do appear to produce better compiled code in GCL.
(cond (ans (mvf 0 ans))
(t (mvf n nil)))))))
(defun bdd-quotep (term op-ht mx-id)
(declare (type #.*fixnum-type* mx-id))
(the-mv
3
#.*fixnum-type*
(cond
((equal term *t*)
(mvf mx-id *cst-t* op-ht))
((equal term *nil*)
(mvf mx-id *cst-nil* op-ht))
(t
(mv-let (hash-index ans)
(chk-memo-quotep term op-ht)
(declare (type #.*fixnum-type* hash-index))