-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogicman.lisp
3453 lines (2952 loc) · 147 KB
/
logicman.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
; FGL - A Symbolic Simulation Framework for ACL2
; Copyright (C) 2018 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; Original author: Sol Swords <[email protected]>
(in-package "FGL")
;; Critpath: include aignet/semantics and move logic-building functions to separate book
(include-book "centaur/aignet/construction" :dir :system)
;; Critpath: factor out sat-lits stobj definition from aignet/cnf and include that + ipasir-logic instead
(include-book "centaur/aignet/ipasir" :dir :system)
;; (include-book "bvar-db")
(include-book "bfr")
(include-book "arith-base")
(include-book "syntax-bind")
;; (include-book "pathcond-stobj")
(include-book "defapply")
(include-book "std/stobjs/nicestobj" :dir :system)
(include-book "clause-processors/meta-extract-user" :dir :system)
(local (include-book "theory"))
(local (include-book "tools/trivial-ancestors-check" :dir :system))
(local (include-book "std/util/termhints" :dir :system))
(acl2::defstobj-clone u32arr aignet::u32arr :pkg fgl-fgl)
(acl2::defstobj-clone ipasir ipasir::ipasir :pkg fgl-fgl)
(acl2::defstobj-clone sat-lits aignet::sat-lits :pkg fgl-fgl)
(acl2::defstobj-clone strash aignet::strash :pkg fgl-fgl)
(stobjs::defnicestobj logicman
(aignet :type aignet)
(strash :type strash)
(ipasir :type (array ipasir (0)) :resizable t)
(sat-lits :type (array sat-lits (0)) :resizable t)
(mode :type (satisfies bfr-mode-p) :pred bfr-mode-p :fix bfr-mode-fix :initially 0)
(aignet-refcounts :type u32arr)
(refcounts-index :type (integer 0 *) :pred natp :fix nfix :initially 0))
(local (in-theory (disable aignet::aignetp
aignet::strashp
ipasir::ipasirp
aignet::sat-litsp
aignet::u32arrp)))
(defthmd logicman->ipasiri-of-resize
(equal (logicman->ipasiri n (resize-logicman->ipasir size logicman))
(cond ((and (< (nfix n) (logicman->ipasir-length logicman))
(< (nfix n) (nfix size)))
(logicman->ipasiri n logicman))
((< (nfix n) (nfix size))
(ipasir::create-ipasir))
(t nil)))
:hints(("Goal" :in-theory (enable logicman->ipasiri
resize-logicman->ipasir
logicman->ipasir-length))))
(defthmd logicman->sat-litsi-of-resize
(equal (logicman->sat-litsi n (resize-logicman->sat-lits size logicman))
(cond ((and (< (nfix n) (logicman->sat-lits-length logicman))
(< (nfix n) (nfix size)))
(logicman->sat-litsi n logicman))
((< (nfix n) (nfix size))
(aignet::create-sat-lits))
(t nil)))
:hints(("Goal" :in-theory (enable logicman->sat-litsi
resize-logicman->sat-lits
logicman->sat-lits-length))))
;; (defconst *logicman-fields*
;; '((aignet :type aignet::aignet)
;; (strash :type aignet::strash)
;; (ipasir :type ipasir::ipasir)
;; (sat-lits :type aignet::sat-lits)
;; ;; (pathcond :type pathcond)
;; ;; (bvar-db :type bvar-db)
;; ;; (prof :type interp-profiler)
;; (mode :type (satisfies bfr-mode-p) :initially 0)
;; (aignet-refcounts :type aignet::aignet-refcounts)
;; ;; refcounts are up to date up to this aignet ID
;; (refcounts-index :type (integer 0 *) :initially 0)))
;; (local (defun logicman-fields-to-templates (fields)
;; (declare (xargs :mode :program))
;; (if (atom fields)
;; nil
;; (cons (acl2::make-tmplsubst :atoms `((<field> . ,(caar fields))
;; (:<field> . ,(intern$ (symbol-name (caar fields)) "KEYWORD"))
;; (:<fieldcase> . ,(if (atom (cdr fields))
;; t
;; (intern$ (symbol-name (caar fields)) "KEYWORD")))
;; (<type> . ,(third (car fields)))
;; (<rest> . ,(cdddr (car fields))))
;; :strs `(("<FIELD>" . ,(symbol-name (caar fields))))
;; :pkg-sym 'fgl::foo)
;; (logicman-fields-to-templates (cdr fields))))))
;; (make-event
;; `(defconst *logicman-field-templates*
;; ',(logicman-fields-to-templates *logicman-fields*)))
;; (make-event
;; (acl2::template-subst
;; '(defstobj logicman
;; (:@proj fields (logicman-><field> :type <type> . <rest>)))
;; :subsubsts `((fields . ,*logicman-field-templates*))))
;; (defthm logicmanp-implies-aignetp
;; (implies (logicmanp logicman)
;; (aignet::node-listp (logicman->aignet logicman))))
;; (defthm logicmanp-implies-bfr-mode-p
;; (implies (logicmanp logicman)
;; (bfr-mode-p (logicman->mode logicman))))
;; (defthm logicmanp-implies-pathcondp
;; (implies (logicmanp logicman)
;; (pathcondp (logicman->pathcond logicman))))
;; (in-theory (disable logicmanp))
;; (make-event
;; (acl2::template-subst
;; '(std::defenum logicman-field-p ((:@proj fields :<field>)))
;; :subsubsts `((fields . ,*logicman-field-templates*))))
;; (make-event
;; (acl2::template-subst
;; '(define logicman-get ((key logicman-field-p) &optional (logicman 'logicman))
;; ;; bozo define doesn't correctly support :non-executable t with macro args
;; (declare (xargs :non-executable t))
;; :no-function t
;; :prepwork ((local (in-theory (enable logicman-field-fix))))
;; (prog2$ (acl2::throw-nonexec-error 'logicman-get-fn (list key logicman))
;; (case key
;; (:@proj fields (:<fieldcase> (logicman-><field> logicman))))))
;; :subsubsts `((fields . ,*logicman-field-templates*))))
;; (make-event
;; (acl2::template-subst
;; '(defsection logicman-field-basics
;; (local (in-theory (enable logicman-get
;; logicman-field-fix)))
;; (:@append fields
;; (def-updater-independence-thm logicman-><field>-updater-independence
;; (implies (equal (logicman-get :<field> new)
;; (logicman-get :<field> old))
;; (equal (logicman-><field> new)
;; (logicman-><field> old))))
;; (defthm update-logicman-><field>-preserves-others
;; (implies (not (equal (logicman-field-fix i) :<field>))
;; (equal (logicman-get i (update-logicman-><field> x logicman))
;; (logicman-get i logicman))))
;; (defthm update-logicman-><field>-self-preserves-logicman
;; (implies (logicmanp logicman)
;; (equal (update-logicman-><field>
;; (logicman-><field> logicman)
;; logicman)
;; logicman))
;; :hints(("Goal" :in-theory (enable logicmanp
;; aignet::redundant-update-nth))))
;; (defthm logicman-><field>-of-update-logicman-><field>
;; (equal (logicman-><field> (update-logicman-><field> x logicman))
;; x)))
;; (:@proj fields
;; (in-theory (disable logicman-><field>
;; update-logicman-><field>)))
;; (local (in-theory (disable logicman-get
;; logicman-field-fix)))
;; ;; test:
;; (local
;; (defthm logicman-test-updater-independence
;; (b* ((logicman1 (update-logicman->strash strash logicman))
;; (logicman2 (update-logicman->ipasir ipasir logicman)))
;; (and (equal (logicman->mode logicman2) (logicman->mode logicman))
;; (equal (logicman->mode logicman1) (logicman->mode logicman)))))))
;; :subsubsts `((fields . ,*logicman-field-templates*))))
(define sat-lits-init (sat-lits)
:enabled t
:guard-hints (("goal" :in-theory (enable aignet::resize-aignet->sat
aignet::create-sat-lits
aignet::sat-litsp
update-nth nth len
resize-list)
:do-not-induct t))
:prepwork ((local (defthm equal-of-plus-const
(implies (and (syntaxp (and (quotep c1) (quotep c2)))
(acl2-numberp c2))
(equal (equal (+ c1 x) c2)
(equal (fix x) (- c2 (fix c1)))))))
(local (defthm len-equal-0
(equal (equal (len x) 0)
(not (consp x))))))
(mbe :logic (non-exec (aignet::create-sat-lits))
:exec (b* ((sat-lits (aignet::update-sat-next-var$ 1 sat-lits))
(sat-lits (aignet::resize-aignet->sat 0 sat-lits))
(sat-lits (aignet::resize-sat->aignet 1 sat-lits)))
(aignet::update-sat->aigneti 0 0 sat-lits))))
(define ipasir-maybe-release (ipasir)
:returns (new-ipasir)
(if (eq (ipasir::ipasir-get-status ipasir) :undef)
ipasir
(ipasir-release ipasir))
///
(defret status-of-ipasir-maybe-release
(equal (ipasir::ipasir$a->status new-ipasir) :undef)))
(fty::deffixcong acl2::nat-equiv equal (logicman->ipasiri n logicman) n
:hints(("Goal" :in-theory (enable logicman->ipasiri))))
(fty::deffixcong acl2::nat-equiv equal (update-logicman->ipasiri n val logicman) n
:hints(("Goal" :in-theory (enable update-logicman->ipasiri))))
(fty::deffixcong acl2::nat-equiv equal (logicman->sat-litsi n logicman) n
:hints(("Goal" :in-theory (enable logicman->sat-litsi))))
(fty::deffixcong acl2::nat-equiv equal (update-logicman->sat-litsi n val logicman) n
:hints(("Goal" :in-theory (enable update-logicman->sat-litsi))))
(define logicman-release-ipasirs ((n natp) logicman)
:measure (nfix (- (logicman->ipasir-length logicman) (nfix n)))
:guard (<= n (logicman->ipasir-length logicman))
:returns (new-logicman)
(if (mbe :logic (zp (- (logicman->ipasir-length logicman) (nfix n)))
:exec (eql n (logicman->ipasir-length logicman)))
logicman
(stobj-let ((ipasir (logicman->ipasiri n logicman)))
(ipasir)
(ipasir-maybe-release ipasir)
(logicman-release-ipasirs (1+ (lnfix n)) logicman)))
///
(defret logicman-get-of-<fn>
(implies (not (equal (logicman-field-fix k) :ipasir))
(equal (logicman-get k new-logicman)
(logicman-get k logicman)))))
(define logicman-init (&key
(logicman 'logicman)
((max-ins natp) '0)
((max-nodes posp) '1))
:returns new-logicman
:enabled t
:prepwork ((local (defthm equal-of-plus-const
(implies (and (syntaxp (and (quotep c1) (quotep c2)))
(acl2-numberp c2))
(equal (equal (+ c1 x) c2)
(equal (fix x) (- c2 (fix c1)))))))
(local (defthm len-equal-const
(implies (syntaxp (quotep n))
(equal (equal (len x) n)
(if (zp n)
(and (not (consp x))
(equal n 0))
(and (consp x)
(equal (len (cdr x)) (+ -1 n))))))))
(local (in-theory (e/d* (logicman-defs
aignet::strashp
update-nth len)
(cons-equal))))
(local (defthmd update-nth-same
(equal (update-nth n val (update-nth n val1 x))
(update-nth n val x))
:hints(("Goal" :in-theory (enable update-nth)))))
(local (defthm update-nth-of-logicman-release-ipasirs
(equal (update-nth *logicman->ipasiri* val (logicman-release-ipasirs n logicman))
(update-nth *logicman->ipasiri* val logicman))
:hints(("Goal" :in-theory (enable logicman-release-ipasirs
update-nth-same))))))
:guard-hints (("goal" :do-not-induct t))
(mbe :logic (non-exec (create-logicman))
:exec (stobj-let ((aignet (logicman->aignet logicman))
(strash (logicman->strash logicman))
(u32arr (logicman->aignet-refcounts logicman)))
(aignet strash u32arr)
(b* ((aignet (aignet::aignet-init 0 0 max-ins max-nodes aignet))
(strash (strashtab-clear strash))
(u32arr (resize-u32 0 u32arr)))
(mv aignet strash u32arr))
(b* ((logicman (update-logicman->mode 0 logicman))
(logicman (logicman-release-ipasirs 0 logicman))
(logicman (resize-logicman->ipasir 0 logicman))
(logicman (resize-logicman->sat-lits 0 logicman)))
(update-logicman->refcounts-index 0 logicman)))))
(defmacro lbfr-mode (&optional (logicman 'logicman))
`(logicman->mode ,logicman))
(defsection lbfr-case
:parents (bfr)
:short "Choose behavior based on the current @(see bfr) mode of a logicman."
:long "<p>Same as @(see bfr-mode-case), but implicitly uses the bfr mode
setting of a logicman stobj. If no logicman is given as the first
argument (i.e., the first argument is a keyword), the variable named
@('logicman') is implicitly used.</p>"
(defun lbfr-case-fn (args)
(if (keywordp (car args))
`(bfr-mode-case (lbfr-mode) . ,args)
`(bfr-mode-case (lbfr-mode ,(car args)) . ,(cdr args))))
(defmacro lbfr-case (&rest args)
(lbfr-case-fn args)))
(defsection lbfr-mode-is
:parents (bfr)
:short "Check the current bfr-mode of a logicman stobj"
:long "<p>Same as @(see bfr-mode-is), but gets the bfr mode object from a
logicman stobj. If no logicman argument is supplied, the variable named
@('logicman') is implicitly used.</p>"
(defun lbfr-mode-is-fn (key lman)
`(bfr-mode-is ,key (lbfr-mode ,lman)))
(defmacro lbfr-mode-is (key &optional (logicman 'logicman))
(lbfr-mode-is-fn key logicman)))
(define logicman->bfrstate (&optional (logicman 'logicman))
:returns (bfrstate bfrstate-p)
(b* ((bfr-mode (lbfr-mode)))
(bfr-mode-case
:aignet (stobj-let ((aignet (logicman->aignet logicman)))
(bfrstate)
(bfrstate bfr-mode (1- (aignet::num-fanins aignet)))
bfrstate)
:otherwise (stobj-let ((aignet (logicman->aignet logicman)))
(bfrstate)
(bfrstate bfr-mode (aignet::num-ins aignet))
bfrstate)))
///
(def-updater-independence-thm logicman->bfrstate-updater-independence
(implies (and (equal (logicman->aignet new)
(logicman->aignet old))
(equal (logicman->mode new)
(logicman->mode old)))
(equal (logicman->bfrstate new)
(logicman->bfrstate old))))
(defret bfrstate->mode-of-logicman->bfrstate
(equal (bfrstate->mode bfrstate)
(bfr-mode-fix (lbfr-mode))))
(defret bfrstate->bound-of-logicman->bfrstate-aignet
(implies (lbfr-mode-is :aignet)
(equal (bfrstate->bound bfrstate)
(aignet::fanin-count (logicman->aignet logicman)))))
(defthm aignet-litp-of-bfr->aignet-lit
(b* ((bfrstate (logicman->bfrstate)))
(implies (lbfr-mode-is :aignet)
(aignet::aignet-litp (bfr->aignet-lit x)
(logicman->aignet logicman))))
:hints(("Goal" :in-theory (enable logicman->bfrstate
bfr->aignet-lit
bfr-fix
aignet-lit->bfr
bounded-lit-fix
aignet::aignet-idp)))))
;; Include pathcond? parametrization hyp?
(define logicman-extension-p (new old)
:non-executable t
:verify-guards nil
(and (aignet::aignet-extension-p (logicman->aignet new)
(logicman->aignet old))
(equal (aignet::num-regs (logicman->aignet new))
(aignet::num-regs (logicman->aignet old)))
;; (bvar-db-extension-p (logicman->bvar-db new)
;; (logicman->bvar-db old))
(equal (logicman->mode new) (logicman->mode old))
;; (equal (logicman->ipasir new) (logicman->ipasir old))
;; (equal (logicman->sat-lits new) (logicman->sat-lits old))
;; (equal (logicman->aignet-refcounts new) (logicman->aignet-refcounts old))
;; (equal (logicman->refcounts-index new) (logicman->refcounts-index old))
)
///
;; (def-updater-independence-thm logicman-extension-p-updater-independence-1
;; (implies (and (equal (logicman-get :aignet new)
;; (logicman-get :aignet old))
;; ;; (equal (logicman-get :bvar-db new)
;; ;; (logicman-get :bvar-db old))
;; (equal (logicman-get :mode new)
;; (logicman-get :mode old))
;; (logicman-extension-p old other))
;; (logicman-extension-p new other)))
;; (def-updater-independence-thm logicman-extension-p-updater-independence-2
;; (implies (and (equal (logicman-get :aignet new)
;; (logicman-get :aignet old))
;; ;; (equal (logicman-get :bvar-db new)
;; ;; (logicman-get :bvar-db old))
;; (equal (logicman-get :mode new)
;; (logicman-get :mode old))
;; (logicman-extension-p other old))
;; (logicman-extension-p other new)))
;; (def-updater-independence-thm logicman-extension-p-updater-independence-2
;; (implies (and (equal (logicman-get :aignet new)
;; (logicman-get :aignet old))
;; ;; (equal (logicman-get :bvar-db new)
;; ;; (logicman-get :bvar-db old))
;; (equal (logicman-get :mode new)
;; (logicman-get :mode old))
;; (logicman-extension-p other old))
;; (logicman-extension-p other new)))
;; (in-theory (disable logicman-extension-p-updater-independence-1
;; logicman-extension-p-updater-independence-2))
(defmacro bind-logicman-extension (new old-var)
`(and (bind-free (acl2::prev-stobj-binding ,new ',old-var mfc state))
(logicman-extension-p ,new ,old-var)))
(defthm logicman-extension-self
(logicman-extension-p x x))
(def-updater-independence-thm logicman-extension-of-update-1
(implies (and (equal (logicman->aignet new)
(logicman->aignet old))
(equal (logicman->mode new)
(logicman->mode old)))
(equal (logicman-extension-p new logicman)
(logicman-extension-p old logicman))))
(def-updater-independence-thm logicman-extension-of-update-2
(implies (and (equal (logicman->aignet new)
(logicman->aignet old))
(equal (logicman->mode new)
(logicman->mode old)))
(equal (logicman-extension-p logicman new)
(logicman-extension-p logicman old))))
(defthm aignet-extension-when-logicman-extension
(implies (logicman-extension-p new old)
(aignet::aignet-extension-p (logicman->aignet new)
(logicman->aignet old))))
(defthm logicman-extension-p-transitive
(implies (and (bind-logicman-extension x y)
(logicman-extension-p y z))
(logicman-extension-p x z)))
;; (defthm logicman-extension-when-same
;; (implies (and (equal (logicman-get :aignet new)
;; (logicman-get :aignet old))
;; ;; (equal (logicman-get :bvar-db new)
;; ;; (logicman-get :bvar-db old))
;; (equal (logicman-get :mode new)
;; (logicman-get :mode old)))
;; (logicman-extension-p new old)))
(defthm logicman->mode-of-extension
(implies (bind-logicman-extension x y)
(equal (logicman->mode x)
(logicman->mode y))))
(defthm bfrstate>=-when-logicman-extension
(implies (logicman-extension-p new old)
(bfrstate>= (logicman->bfrstate new)
(logicman->bfrstate old)))
:hints(("Goal" :in-theory (enable logicman->bfrstate
bfrstate>=))))
(local (in-theory (disable bfrstate>=-when-logicman-extension
logicman-extension-p)))
(defthm bfr-p-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(bfr-p x (logicman->bfrstate old)))
(bfr-p x (logicman->bfrstate new)))
:hints (("goal" :use bfrstate>=-when-logicman-extension)))
(defthm bfr-fix-when-bfr-p-extension
(implies (and (bind-logicman-extension new old)
(bfr-p x (logicman->bfrstate old)))
(equal (bfr-fix x (logicman->bfrstate new)) x))
:hints (("Goal" :use ((:instance bfr-p-when-logicman-extension))
:in-theory (disable bfr-p-when-logicman-extension))))
(defthm bfr->aignet-lit-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(bfr-p x (logicman->bfrstate old)))
(equal (bfr->aignet-lit x (logicman->bfrstate new))
(bfr->aignet-lit x (logicman->bfrstate old))))
:hints(("Goal" :in-theory (enable bfr->aignet-lit))))
(defthm bfr-listp-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(bfr-listp x (logicman->bfrstate old)))
(bfr-listp x (logicman->bfrstate new)))
:hints (("goal" :use bfrstate>=-when-logicman-extension)))
(defthm fgl-bfr-object-p-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(fgl-bfr-object-p x (logicman->bfrstate old)))
(fgl-bfr-object-p x (logicman->bfrstate new)))
:hints (("goal" :use bfrstate>=-when-logicman-extension
:in-theory (disable fgl-bfr-object-p-when-fgl-object-p))))
(defthm fgl-bfr-objectlist-p-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(fgl-bfr-objectlist-p x (logicman->bfrstate old)))
(fgl-bfr-objectlist-p x (logicman->bfrstate new)))
:hints (("goal" :use bfrstate>=-when-logicman-extension
:in-theory (disable fgl-bfr-objectlist-p-when-fgl-objectlist-p))))
(defthm fgl-bfr-object-alist-p-when-logicman-extension
(implies (and (bind-logicman-extension new old)
(fgl-bfr-object-alist-p x (logicman->bfrstate old)))
(fgl-bfr-object-alist-p x (logicman->bfrstate new)))
:hints (("goal" :use bfrstate>=-when-logicman-extension
:in-theory (disable fgl-bfr-object-alist-p-when-fgl-object-alist-p)))))
;; (define bfr-p (x &optional (logicman 'logicman))
;; (bfr-case
;; :aig (aig-p x)
;; :bdd (acl2::ubddp x)
;; :aignet
;; (or (booleanp x)
;; (and (satlink::litp x)
;; (not (eql x 0))
;; (not (eql x 1))
;; (stobj-let
;; ((aignet (logicman->aignet logicman)))
;; (ans)
;; (aignet::fanin-litp x aignet)
;; ans))))
;; ///
;; (defthm bfr-p-of-constants
;; (and (bfr-p t)
;; (bfr-p nil)))
;; (def-updater-independence-thm bfr-p-updater-independence
;; (implies (and (equal (logicman-get :aignet new)
;; (logicman-get :aignet old))
;; (equal (logicman-get :mode new)
;; (logicman-get :mode old)))
;; (equal (bfr-p x new) (bfr-p x old))))
;; (defthm bfr-p-when-logicman-extension
;; (implies (and (bind-logicman-extension new old)
;; (bfr-p x old))
;; (bfr-p x new))
;; :hints(("Goal" :in-theory (enable logicman-extension-p)))))
;; (std::deflist bfr-listp$ (x logicman)
;; (bfr-p x)
;; :true-listp t
;; ///
;; (defthm bfr-listp-when-logicman-extension
;; (implies (and (bind-logicman-extension new old)
;; (bfr-listp$ x old))
;; (bfr-listp$ x new))))
;; (defmacro bfr-listp (x &optional (logicman 'logicman))
;; `(bfr-listp$ ,x ,logicman))
;; (add-macro-alias bfr-listp bfr-listp$)
;; (define aignet-lit->bfr ((x satlink::litp) &optional (logicman 'logicman))
;; :guard (stobj-let ((aignet (logicman->aignet logicman)))
;; (ans)
;; (aignet::fanin-litp x aignet)
;; ans)
;; :returns (bfr bfr-p :hyp (eq (lbfr-mode) :aignet)
;; :hints(("Goal" :in-theory (enable bfr-p))))
;; (b* ((x (mbe :logic (non-exec (aignet::aignet-lit-fix x (logicman->aignet logicman)))
;; :exec x)))
;; (case x
;; (0 nil)
;; (1 t)
;; (t x)))
;; ///
;; (defthm aignet-lit->bfr-of-logicman-extension
;; (implies (and (bind-logicman-extension new old)
;; (aignet::aignet-litp x (logicman->aignet old)))
;; (equal (aignet-lit->bfr x new)
;; (aignet-lit->bfr x old)))
;; :hints(("Goal" :in-theory (enable logicman-extension-p)))))
;; (define bfr-fix ((x bfr-p) &optional (logicman 'logicman))
;; :returns (new-x bfr-p)
;; :prepwork ((local (in-theory (enable bfr-p aignet-lit->bfr))))
;; (mbe :logic (bfr-case
;; :aig (aig-fix x)
;; :bdd (acl2::ubdd-fix x)
;; :aignet
;; (if (booleanp x)
;; x
;; (aignet-lit->bfr x)))
;; :exec x)
;; ///
;; (std::defret bfr-fix-when-bfr-p
;; (implies (bfr-p x)
;; (equal new-x x))))
;; (define bfr-list-fix ((x bfr-listp) &optional (logicman 'logicman))
;; :returns (new-x bfr-listp)
;; (if (atom x)
;; nil
;; (cons (bfr-fix (car x))
;; (bfr-list-fix (cdr x))))
;; ///
;; (defret bfr-list-fix-when-bfr-listp
;; (implies (bfr-listp x)
;; (equal (bfr-list-fix x) x)))
;; (defret car-of-bfr-list-fix
;; (implies (consp x)
;; (equal (car (bfr-list-fix x))
;; (bfr-fix (car x)))))
;; (defret cdr-of-bfr-list-fix
;; (implies (consp x)
;; (equal (cdr (bfr-list-fix x))
;; (bfr-list-fix (cdr x)))))
;; (defret consp-of-bfr-list-fix
;; (equal (consp (bfr-list-fix x))
;; (consp x)))
;; (defret len-of-bfr-list-fix
;; (equal (len (bfr-list-fix x))
;; (len x))))
;; (define bfr->aignet-lit ((x bfr-p) &optional (logicman 'logicman))
;; :guard (eq (lbfr-mode) :aignet)
;; :returns (lit (implies (eq (lbfr-mode) :aignet)
;; (aignet::aignet-litp lit (logicman->aignet logicman))))
;; :prepwork ((local (in-theory (enable bfr-fix aignet-lit->bfr bfr-p))))
;; (b* ((x (bfr-fix x)))
;; (case x
;; ((nil) 0)
;; ((t) 1)
;; (t (satlink::lit-fix x))))
;; ///
;; (defthm bfr->aignet-lit-of-logicman-extension
;; (implies (and (bind-logicman-extension new old)
;; (bfr-p x old))
;; (equal (bfr->aignet-lit x new)
;; (bfr->aignet-lit x old))))
;; (defthm bfr->aignet-lit-of-aignet-lit->bfr
;; (implies (equal (lbfr-mode) :aignet)
;; (equal (bfr->aignet-lit (aignet-lit->bfr x))
;; (aignet::aignet-lit-fix x (logicman->aignet logicman))))
;; :hints(("Goal" :in-theory (enable aignet-lit->bfr))))
;; (defthm aignet-lit->bfr-of-bfr->aignet-lit
;; (implies (equal (lbfr-mode) :aignet)
;; (equal (aignet-lit->bfr (bfr->aignet-lit x))
;; (bfr-fix x)))
;; :hints(("Goal" :in-theory (enable aignet-lit->bfr bfr-fix))))
;; (defthm bfr->aignet-lit-of-bfr-fix
;; (equal (bfr->aignet-lit (bfr-fix x))
;; (bfr->aignet-lit x))
;; :hints(("Goal" :in-theory (enable bfr-fix)))))
(define bools-to-bits ((x true-listp))
:returns (bits aignet::bit-listp)
(if (atom x)
nil
(cons (bool->bit (car x))
(bools-to-bits (cdr x))))
///
(std::defret len-of-<fn>
(equal (len bits) (len x)))
(std::defret nth-of-<fn>
(acl2::bit-equiv (nth n bits) (bool->bit (nth n x)))
:hints(("Goal" :in-theory (enable nth)))))
(define list-to-bits-aux ((n natp)
(x true-listp)
(acl2::bitarr))
:guard (<= (+ (nfix n) (len x)) (acl2::bits-length acl2::bitarr))
:returns (new-bitarr)
(b* (((when (atom x)) (mbe :logic (non-exec (aignet::bit-list-fix acl2::bitarr))
:exec acl2::bitarr))
(acl2::bitarr (acl2::set-bit (lnfix n) (bool->bit (car x)) acl2::bitarr)))
(list-to-bits-aux (1+ (lnfix n)) (cdr x) acl2::bitarr))
///
(std::defret len-of-<fn>
(implies (<= (+ (nfix n) (len x)) (len acl2::bitarr))
(equal (len new-bitarr) (len acl2::bitarr))))
;; (std::defret nth-of-<fn>
;; (implies (<= (+ (nfix n) (len x)) (len acl2::bitarr))
;; (equal (nth k new-bitarr)
;; (if (and (<= (nfix n) (nfix k))
;; (< (nfix k) (+ (nfix n) (len x))))
;; (bool->bit (nth (- (nfix k) (nfix n)) x))
;; (nth k (aignet::bit-list-fix acl2::bitarr)))))
;; :hints(("Goal" :in-theory (enable* acl2::arith-equiv-forwarding))))
(local (defthm nthcdr-greater-of-update-nth
(implies (< (nfix m) (nfix n))
(equal (nthcdr n (update-nth m val x))
(nthcdr n x)))
:hints(("Goal" :in-theory (enable update-nth)))))
(local (defthm bit-list-fix-of-update-nth
(implies (<= (nfix n) (len x))
(equal (aignet::bit-list-fix (update-nth n v x))
(update-nth n (bfix v) (aignet::bit-list-fix x))))
:hints(("Goal" :in-theory (enable update-nth)))))
(local (defthm append-take-nthcdr
(implies (<= (nfix n) (len x))
(equal (append (take n x) (nthcdr n x))
x))))
(local (defthm take-of-update-nth
(equal (take (+ 1 (nfix n)) (update-nth n v x))
(append (take n x) (list v)))
:hints(("Goal" :in-theory (enable update-nth)))))
(std::defret list-to-bits-aux-redef
(implies (<= (+ (nfix n) (len x)) (len acl2::bitarr))
(equal new-bitarr
(append (take n (aignet::bit-list-fix acl2::bitarr))
(bools-to-bits x)
(nthcdr (+ (nfix n) (len x)) (aignet::bit-list-fix acl2::bitarr)))))
:hints(("Goal" :in-theory (e/d (bools-to-bits)
(acl2::append-of-cons
take))
:induct (list-to-bits-aux n x acl2::bitarr)))))
;;(local (include-book "std/lists/resize-list" :dir :system))
(define list-to-bits ((x true-listp)
(acl2::bitarr))
:enabled t
:prepwork ((local (defthm nthcdr-of-nil
(equal (nthcdr n nil) nil)))
(local (defthm nthcdr-by-len
(implies (and (>= (nfix n) (len x))
(true-listp x))
(equal (nthcdr n x) nil))
:hints (("goal" :induct (nthcdr n x)))))
(local (in-theory (disable nthcdr aignet::bit-list-fix-of-cons resize-list))))
(mbe :logic (non-exec (bools-to-bits x))
:exec (b* ((acl2::bitarr (acl2::resize-bits (len x) acl2::bitarr)))
(list-to-bits-aux 0 x acl2::bitarr))))
(define alist-to-bitarr-aux ((n natp)
(alist)
(acl2::bitarr))
:measure (nfix (- (acl2::bits-length acl2::bitarr) (nfix n)))
:guard (<= n (acl2::bits-length acl2::bitarr))
:returns (new-bitarr)
(b* (((when (mbe :logic (zp (- (acl2::bits-length acl2::bitarr) (nfix n)))
:exec (eql n (acl2::bits-length acl2::bitarr))))
acl2::bitarr)
(acl2::bitarr (acl2::set-bit n (bool->bit (cdr (hons-get (lnfix n) alist))) acl2::bitarr)))
(alist-to-bitarr-aux (1+ (lnfix n)) alist acl2::bitarr))
///
(defret len-of-alist-to-bitarr-aux
(equal (len new-bitarr) (len acl2::bitarr)))
(defret nth-of-alist-to-bitarr-aux
(equal (nth k new-bitarr)
(if (and (<= (nfix n) (nfix k))
(< (nfix k) (len acl2::bitarr)))
(bool->bit (cdr (hons-assoc-equal (nfix k) alist)))
(nth k acl2::bitarr))))
(defret true-listp-of-<fn>
(implies (true-listp acl2::bitarr)
(true-listp new-bitarr))))
(define alist-to-bitarr ((max natp)
(alist)
(acl2::bitarr))
:returns (new-bitarr)
(b* ((acl2::bitarr (acl2::resize-bits max acl2::bitarr)))
(alist-to-bitarr-aux 0 alist acl2::bitarr))
///
(defret len-of-<fn>
(equal (len new-bitarr) (nfix max)))
(defret nth-of-<fn>
(acl2::bit-equiv (nth k new-bitarr)
(bool->bit
(and (< (nfix k) (nfix max))
(cdr (hons-assoc-equal (nfix k) alist))))))
(defthm alist-to-bitarr-normalize
(implies (syntaxp (not (equal bitarr ''nil)))
(equal (alist-to-bitarr max alist bitarr)
(alist-to-bitarr max alist nil)))
:hints ((acl2::equal-by-nths-hint)))
(local (in-theory (disable alist-to-bitarr)))
#!aignet
(defthm id-eval-of-alist-to-bitarr-aignet-extension
(implies (and (syntaxp (not (equal new old)))
(aignet-extension-p new old)
;; (aignet-idp x old)
)
(equal (id-eval x (fgl::alist-to-bitarr (stype-count :pi new) env bitarr) regvals old)
(id-eval x (fgl::alist-to-bitarr (stype-count :pi old) env bitarr) regvals old)))
:hints (("goal" :induct (id-eval-ind x old)
:expand ((:free (invals regvals)
(id-eval x invals regvals old)))
:in-theory (enable lit-eval eval-and-of-lits eval-xor-of-lits))))
#!aignet
(defthm lit-eval-of-alist-to-bitarr-aignet-extension
(implies (and (syntaxp (not (equal new old)))
(aignet-extension-p new old)
;; (aignet-litp x old)
)
(equal (lit-eval x (fgl::alist-to-bitarr (stype-count :pi new) env bitarr) regvals old)
(lit-eval x (fgl::alist-to-bitarr (stype-count :pi old) env bitarr) regvals old)))
:hints (("goal"
:expand ((:free (invals regvals)
(lit-eval x invals regvals old)))
:in-theory (enable aignet-idp)))))
(defmacro lbfr-p (x &optional (logicman 'logicman))
`(bfr-p ,x (logicman->bfrstate ,logicman)))
(defmacro lbfr-listp (x &optional (logicman 'logicman))
`(bfr-listp ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-p (x &optional (logicman 'logicman))
`(fgl-bfr-object-p ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-objectlist-p (x &optional (logicman 'logicman))
`(fgl-bfr-objectlist-p ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-alist-p (x &optional (logicman 'logicman))
`(fgl-bfr-object-alist-p ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-bindings-p (x &optional (logicman 'logicman))
`(fgl-bfr-object-bindings-p ,x (logicman->bfrstate ,logicman)))
(defmacro lbfr-fix (x &optional (logicman 'logicman))
`(bfr-fix ,x (logicman->bfrstate ,logicman)))
(defmacro lbfr-list-fix (x &optional (logicman 'logicman))
`(bfr-list-fix ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-fix (x &optional (logicman 'logicman))
`(fgl-bfr-object-fix ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-objectlist-fix (x &optional (logicman 'logicman))
`(fgl-bfr-objectlist-fix ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-alist-fix (x &optional (logicman 'logicman))
`(fgl-bfr-object-alist-fix ,x (logicman->bfrstate ,logicman)))
(defmacro lgl-bfr-object-bindings-fix (x &optional (logicman 'logicman))
`(fgl-bfr-object-bindings-fix ,x (logicman->bfrstate ,logicman)))
(define bfr-eval ((x lbfr-p "A Boolean function object")
(env "The (Boolean) evaluation environment")
&optional ((logicman "The logic manager") 'logicman))
:parents (bfr)
:short "Evaluate a BFR under an appropriate BDD/AIG environment."
:long "<p>@('Bfr-eval') is the evaluator for Boolean function objects in FGL.</p>
<p>Boolean function objects in FGL may be <see topic='@(url acl2::ubdds)'>UBDDs</see>,
<see topic='@(url acl2::aig)'>hons-AIGs</see>, or <see topic='@(url acl2::aignet)'>
aignet</see> literals (represented as natural numbers). The @(see bfr-mode) of
the logic manager (accessed as @('(logicman->mode logicman)')) determines how
the objects are interpreted. Furthermore, in the aignet mode, literals must be
evaluated relative to an aignet, which is also stored in the logic manager as a
nested stobj, accessed using @('(logicman->aignet logicman)').</p>
<p>The @('env') input determines the values of Boolean variables, but its form
also depends on the BFR mode. In UBDD mode, it is an ordered list of Boolean
values, where @('(nth n env)') is the value of Boolean variable @('n').
Otherwise, it is a fast alist mapping natural numbers to Boolean values, and
the value of the nth Boolean variable is @('(cdr (hons-get n env))'). In
aignet mode, the Boolean variable numbers map to primary inputs of the AIG --
registers are not used.</p>"
:returns (bool t)
:prepwork (;; (local (include-book "std/lists/nth" :dir :system))
(local (defthm alist-to-bits-of-nil-under-bits-equiv
(aignet::bits-equiv (alist-to-bitarr max nil bitarr)
nil)
:hints(("Goal" :in-theory (enable aignet::bits-equiv)))))
)
:guard-hints ((and stable-under-simplificationp
'(:in-theory (enable aignet::aignet-idp bfr-p ;; logicman->bfrstate
))))
(b* ((bfrstate (logicman->bfrstate)))
(bfrstate-case
:bdd (acl2::eval-bdd (bfr-fix x) env)
:aig (acl2::aig-eval (bfr-fix x) env)
:aignet (mbe :logic (non-exec
(acl2::bit->bool
(aignet::lit-eval (bfr->aignet-lit x)
(alist-to-bitarr (aignet::num-ins (logicman->aignet logicman)) env nil)
nil
(logicman->aignet logicman))))
:Exec
(b* ((x (bfr->aignet-lit x))
((acl2::local-stobjs aignet::invals aignet::regvals)
(mv ans aignet::invals aignet::regvals)))
(stobj-let
((aignet (logicman->aignet logicman)))
(ans aignet::invals aignet::regvals)
(b* ((aignet::invals (alist-to-bitarr (aignet::num-ins aignet) env aignet::invals))
(aignet::regvals (alist-to-bitarr (aignet::num-regs aignet) nil aignet::regvals)))
(mv (acl2::bit->bool
(aignet::lit-eval x aignet::invals aignet::regvals aignet::aignet))
aignet::invals aignet::regvals))
(mv ans aignet::invals aignet::regvals))))))
///
(defthm bfr-eval-consts
(and (equal (bfr-eval t env) t)
(equal (bfr-eval nil env) nil))
:hints(("Goal" :in-theory (enable bfr->aignet-lit))))
(defthm bfr-eval-of-bfr-fix
(b* ((bfrstate (logicman->bfrstate logicman)))
(equal (bfr-eval (bfr-fix x) env)
(bfr-eval x env)))
:hints((acl2::use-termhint
(b* ((bfrstate (logicman->bfrstate old-logicman)))
(bfrstate-case
:aignet '(:in-theory (enable aignet::lit-eval-of-aignet-lit-fix))
:otherwise '(:in-theory (enable bfr-fix ;; logicman->bfrstate
))))))
:otf-flg t)
(defthm bfr-eval-of-logicman-extension
(implies (and (bind-logicman-extension new old)
(lbfr-p x old))
(equal (bfr-eval x env new)
(bfr-eval x env old)))
:hints((acl2::use-termhint
(b* ((bfrstate (logicman->bfrstate old)))
(bfrstate-case
:aignet (acl2::termhint-seq '(:in-theory (enable aignet::lit-eval-of-aignet-lit-fix))
'(:in-theory (enable logicman-extension-p)))
:otherwise '(:in-theory (enable bfr-fix ;; logicman->bfrstate
))))))))
(define bfr-list-eval ((x lbfr-listp) env &optional (logicman 'logicman))
:returns bools
(if (atom x)