-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterp.lisp
More file actions
9615 lines (8342 loc) · 463 KB
/
interp.lisp
File metadata and controls
9615 lines (8342 loc) · 463 KB
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) 2019 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 <sswords@centtech.com>
(in-package "FGL")
(include-book "bfr-arithmetic")
(include-book "bvar-db-equivs")
(include-book "unify-defs")
(include-book "centaur/meta/bindinglist" :dir :system)
(include-book "syntax-bind")
; (include-book "rewrite-tables")
(include-book "system/f-put-global" :dir :system)
(include-book "std/util/defret-mutual-generate" :dir :system)
(include-book "unify-thms")
(include-book "tools/some-events" :dir :system)
(include-book "primitives-stub")
(include-book "sat-stub")
(include-book "trace")
(include-book "fancy-ev")
(include-book "binder-rules") ;; includes regular rules too
(include-book "congruence-rules")
(local (include-book "tools/trivial-ancestors-check" :dir :system))
(local (include-book "centaur/meta/resolve-flag-cp" :dir :system))
(local (include-book "centaur/meta/urewrite" :dir :system))
(local (include-book "centaur/meta/let-abs" :dir :system))
(local (std::add-default-post-define-hook :fix))
(local (in-theory (e/d (acl2::kwote-lst-redef)
(kwote-lst))))
;; NOTE: All these rules should be superfluous, but they might significantly speed up proofs.
(local (in-theory (disable INTERP-ST-GET-OF-INTERP-ST-FIELD-FIX-KEY-NORMALIZE-CONST)))
(local (defthm interp-st-bfrs-ok-of-update-reclimit
(implies (interp-st-bfrs-ok interp-st)
(interp-st-bfrs-ok (update-interp-st->reclimit reclimit interp-st)))))
(local (defthm interp-st-bfrs-ok-of-update-equiv-contexts
(implies (interp-st-bfrs-ok interp-st)
(interp-st-bfrs-ok (update-interp-st->equiv-contexts equiv-contexts interp-st)))))
(local (defthm interp-st->logicman-of-update-reclimit
(equal (interp-st->logicman (update-interp-st->reclimit reclimit interp-st))
(interp-st->logicman interp-st))))
(local (defthm interp-st->logicman-of-update-equiv-contexts
(equal (interp-st->logicman (update-interp-st->equiv-contexts equiv-contexts interp-st))
(interp-st->logicman interp-st))))
(local (defthm interp-st->logicman-of-update-stack
(equal (interp-st->logicman (update-interp-st->stack equiv-contexts interp-st))
(interp-st->logicman interp-st))))
(local (defthm interp-st->logicman-of-update-errmsg
(equal (interp-st->logicman (update-interp-st->errmsg equiv-contexts interp-st))
(interp-st->logicman interp-st))))
(local (in-theory (disable w)))
(define interp-st-cancel-error ((msg symbolp) interp-st)
:returns new-interp-st
:hooks nil
(if (eq (interp-st->errmsg interp-st) msg)
(update-interp-st->errmsg nil interp-st)
interp-st)
///
(defret interp-st-get-of-<fn>
(implies (not (equal (interp-st-field-fix key) :errmsg))
(equal (interp-st-get key new-interp-st)
(interp-st-get key interp-st))))
(defret <fn>-preserves-error
(implies (not (equal (interp-st->errmsg interp-st) msg))
(equal (interp-st->errmsg new-interp-st)
(interp-st->errmsg interp-st))))
(defret <fn>-something-different
(implies (and msg2 (not (equal msg2 msg)))
(equal (equal (interp-st->errmsg new-interp-st) msg2)
(equal (interp-st->errmsg interp-st) msg2)))))
;; (define fgl-interp-error (msg &key (interp-st 'interp-st)
;; (state 'state))
;; :returns (mv errmsg
;; result
;; new-interp-st
;; new-state)
;; (mv msg nil interp-st state))
;; (defmacro fgl-value (obj)
;; `(mv nil ,obj interp-st state))
;; should we look for equivalence assumptions for this object?
(define fgl-term-obj-p ((x fgl-object-p))
(declare (xargs :guard t))
(fgl-object-case x
:g-ite t
:g-var t
:g-apply t
:otherwise nil))
(define fgl-function-mode-fix! (x)
:guard-hints(("Goal" :in-theory (enable fgl-function-mode-fix)))
:enabled t
(mbe :logic (fgl-function-mode-fix x)
:exec (loghead 6 (ifix x))))
(define g-concretelist-p ((x fgl-objectlist-p))
(if (atom x)
t
(and (fgl-object-case (car x) :g-concrete)
(g-concretelist-p (Cdr x)))))
(define g-concretelist-vals ((x fgl-objectlist-p))
:guard (g-concretelist-p x)
:guard-hints (("goal" :in-theory (enable g-concretelist-p)))
(if (atom x)
nil
(cons (g-concrete->val (car x))
(g-concretelist-vals (cdr x)))))
(define fncall-try-concrete-eval ((fn pseudo-fnsym-p)
(args fgl-objectlist-p)
(dont-concrete-exec)
state)
:returns (mv ok (ans fgl-object-p))
(b* (((when (or dont-concrete-exec
(not (g-concretelist-p args))))
(mv nil nil))
((mv err ans)
(acl2::magic-ev-fncall-logic (pseudo-fnsym-fix fn) (g-concretelist-vals args) state)))
(mv (not err) (g-concrete ans)))
///
(defret fgl-object-bfrlist-of-<fn>
(equal (fgl-object-bfrlist ans) nil))
(local (defthm fgl-objectlist-eval-when-concretelist-p
(implies (g-concretelist-p x)
(equal (fgl-objectlist-eval x env)
(g-concretelist-vals x)))
:hints(("Goal" :in-theory (enable fgl-objectlist-eval fgl-object-eval g-concretelist-p
g-concretelist-vals)))))
(defret eval-of-<fn>
(implies (and ok
(fgl-ev-meta-extract-global-facts :state st)
(equal (w st) (w state)))
(equal (fgl-object-eval ans env)
(fgl-ev (cons (pseudo-fnsym-fix fn) (kwote-lst (fgl-objectlist-eval args env)))
nil)))))
(define interp-st-restore-reclimit ((reclimit natp)
interp-st)
:guard (acl2::nat-equiv reclimit (interp-st->reclimit interp-st))
:inline t
:enabled t
(mbe :logic (update-interp-st->reclimit (lnfix reclimit) interp-st)
:exec interp-st))
(def-b*-binder fgl-interp-recursive-call
:body
`(b* ((interp-recursive-call-reclimit (lnfix (interp-st->reclimit interp-st)))
((mv ,@args interp-st state) . ,forms)
(interp-st (interp-st-restore-reclimit interp-recursive-call-reclimit interp-st)))
,rest-expr))
(define fgl-interp-time$-arg ((arg fgl-object-p) (x pseudo-termp))
(b* ((arg (fgl-object-case arg :g-concrete (and (true-listp arg.val) arg.val) :otherwise nil))
(term-descrip (pseudo-term-case x :fncall x.fn :otherwise (pseudo-term-fix x))))
(if arg
(b* ((msg (nth 3 arg)))
(if msg
arg
(append (take 3 arg)
(list "fGl-interp ~x0: ~st real, ~sc cpu, ~sa bytes~%"
(list term-descrip)))))
(list 0 nil nil "fGl-interp ~x0: ~st real, ~sc cpu, ~sa bytes~%"
(list term-descrip)))))
(local (defthm assoc-when-key
(implies k
(equal (assoc k a)
(hons-assoc-equal k a)))))
(define fgl-interp-match-synp ((x pseudo-termp))
:returns (mv (synp-type symbolp) ;; nil if bad
(untrans-form)
(trans-term pseudo-termp)
(vars))
(b* (((unless (pseudo-term-case x :fncall))
(mv nil nil nil nil))
((pseudo-term-fncall x))
((unless (and (eq x.fn 'synp)
(eql (len x.args) 3)))
(mv nil nil nil nil))
((list vars untrans-form trans-form) x.args)
((unless (and (pseudo-term-case vars :quote)
(pseudo-term-case untrans-form :quote)
(pseudo-term-case trans-form :quote)))
(mv nil nil nil nil))
(vars (acl2::pseudo-term-quote->val vars))
(trans-form (acl2::pseudo-term-quote->val trans-form))
(untrans-form (acl2::pseudo-term-quote->val untrans-form))
((unless (and (consp untrans-form)
(symbolp (car untrans-form))
(pseudo-termp trans-form)))
(mv nil nil nil nil)))
(mv (car untrans-form)
untrans-form
trans-form
vars))
///
(defret fgl-interp-match-synp-implies-eval
(implies synp-type
(equal (fgl-ev x a) t))))
(defmacro fgl-interp-value (&rest args)
`(mv ,@args interp-st state))
(acl2::def-b*-binder fgl-interp-value
:body `(b* (((mv ,@args interp-st state) . ,forms)) ,rest-expr))
(define fgl-interp-syntax-interp ((synp-term pseudo-termp)
(untrans pseudo-termp)
(interp-st interp-st-bfrs-ok)
state)
:returns (mv (ans fgl-object-p)
new-interp-st
new-state)
:prepwork ((local (defthm symbol-alistp-when-fgl-object-bindings-p
(implies (fgl-object-bindings-p x)
(symbol-alistp x))
:hints(("Goal" :in-theory (enable fgl-object-bindings-p))))))
(b* (((unless (and (pseudo-term-case untrans :quote)))
;; We could go ahead and simulate x anyway but this does seem like an error.
(fgl-interp-error :msg (fgl-msg "Bad syntax-interp form: args ~x0."
(list (pseudo-term-fix synp-term)
(pseudo-term-fix untrans)))))
((unless (member-eq 'unequiv (interp-st->equiv-contexts interp-st)))
(fgl-interp-error :msg (fgl-msg "Syntax-interp called not in an unequiv context: args ~x0."
(list (pseudo-term-fix synp-term)
(pseudo-term-fix untrans)))))
(bindings (append (interp-st-minor-bindings interp-st)
(interp-st-bindings interp-st)))
((mv err val interp-st state) (fancy-ev synp-term bindings 100 interp-st state t t))
((when err)
(fgl-interp-error
:msg (fgl-msg "Syntax-interp error evaluating ~x0: ~@1"
(pseudo-term-quote->val untrans)
(if (or (consp err) (stringp err)) err "(no message)"))))
((unless (fgl-bfr-object-p val (interp-st-bfr-state)))
(fgl-interp-error
:msg (fgl-msg "Syntax-bind error: ~x0 evaluted to an illformed symbolic object, saved as debug object."
(pseudo-term-quote->val untrans))
:debug-obj val)))
(fgl-interp-value val))
///
(local (defthm bfrlist-of-interp-st-add-binding
(implies (and (not (member v (major-stack-bfrlist stack)))
(not (member v (fgl-object-bfrlist val))))
(not (member v (major-stack-bfrlist (stack$a-add-binding var val stack)))))
:hints (("goal" :expand ((major-stack-bfrlist stack))
:in-theory (enable stack$a-add-binding major-frame-bfrlist
major-stack-bfrlist)))))
(local (in-theory (disable stack$a-add-binding)))
(local (in-theory (enable bfr-listp-when-not-member-witness)))
(local (Defthm fgl-bfr-object-p-is-fgl-object-p-and-bfr-listp
(equal (fgl-bfr-object-p x)
(and (fgl-object-p x)
(bfr-listp (fgl-object-bfrlist x))))))
(defret interp-st-bfrs-ok-of-<fn>
(implies (interp-st-bfrs-ok interp-st)
(and (interp-st-bfrs-ok new-interp-st)
(implies (equal logicman (interp-st->logicman interp-st))
(lbfr-listp (fgl-object-bfrlist ans) logicman)))))
(local (acl2::use-trivial-ancestors-check))
(defret interp-st-get-of-<fn>
(implies (member (interp-st-field-fix key)
'(:stack :logicman :bvar-db :pathcond :constraint :constraint-db :equiv-contexts :reclimit))
(equal (interp-st-get key new-interp-st)
(interp-st-get key interp-st))))
(defret multivalues-of-<fn>
(equal (list . <values>) <call>))
(defret <fn>-preserves-errmsg
(implies (interp-st->errmsg interp-st)
(equal (interp-st->errmsg new-interp-st)
(interp-st->errmsg interp-st))))
(defret interp-st->errmsg-equal-unreachable-of-<fn>
(implies (not (equal (interp-st->errmsg interp-st) :unreachable))
(not (equal (interp-st->errmsg new-interp-st) :unreachable))))
(defret w-state-of-<fn>
(equal (w new-state) (w state)))
(defret ans-of-<fn>
(implies (and (not (interp-st->errmsg new-interp-st))
(equal contexts (interp-st->equiv-contexts interp-st)))
(equal (fgl-ev-context-fix contexts
(fgl-object-eval ans env logicman))
(fgl-ev-context-fix (interp-st->equiv-contexts interp-st) nil)))))
(define fgl-interp-or-test-equiv-contexts ((contexts equiv-contextsp))
:returns (new-contexts equiv-contextsp)
(cond ((member-eq 'unequiv (equiv-contexts-fix contexts)) '(unequiv))
((member-eq 'iff (equiv-contexts-fix contexts)) '(iff))))
(define fgl-interp-or-test-already-rewrittenp ((contexts equiv-contextsp))
(or (member-eq 'unequiv (equiv-contexts-fix contexts))
(member-eq 'iff (equiv-contexts-fix contexts))))
;; (define interp-st-checkpoint-p (chp interp-st)
;; :enabled t
;; (stobj-let ((pathcond (interp-st->pathcond interp-st))
;; (logicman (interp-st->logicman interp-st)))
;; (ok)
;; (pathcond-checkpoint-p chp (logicman->mode logicman) pathcond)
;; ok))
(define interp-st-pathcond-assume ((test interp-st-bfr-p)
interp-st)
:returns (mv contra
(new-interp-st))
:guard-hints (("goal" :in-theory (enable pathcond-rewind-ok)))
(stobj-let ((logicman (interp-st->logicman interp-st))
(pathcond (interp-st->pathcond interp-st))
(constraint-pathcond (interp-st->constraint interp-st)))
(contra pathcond constraint-pathcond)
;; this is a bit weak... would be better to check against
;; both constraint and pathcond at once somehow
(b* ((constraint-pathcond (pathcond-fix constraint-pathcond))
((unless test)
(mv t pathcond constraint-pathcond))
;; ((mv constraint-implies constraint-pathcond)
;; (logicman-pathcond-implies test constraint-pathcond))
;; ((when (eql constraint-implies 0))
;; (mv t pathcond constraint-pathcond))
((mv contra constraint-pathcond)
(logicman-pathcond-assume test constraint-pathcond))
((when contra)
(mv t pathcond constraint-pathcond))
(constraint-pathcond (pathcond-rewind (logicman->mode logicman) constraint-pathcond))
((mv contra pathcond) (logicman-pathcond-assume test pathcond)))
(mv contra pathcond constraint-pathcond))
(mv contra interp-st))
///
(defret interp-st-get-of-interp-st-pathcond-assume
(implies (and (not (equal (interp-st-field-fix key) :pathcond))
(not (equal (interp-st-field-fix key) :constraint)))
(equal (interp-st-get key new-interp-st)
(interp-st-get key interp-st))))
(defret interp-st->constraint-of-interp-st-pathcond-assume
(equal (interp-st->constraint new-interp-st)
(pathcond-fix (interp-st->constraint interp-st))))
(defret interp-st-bfrs-ok-of-<fn>
(implies (and (interp-st-bfr-p test)
(interp-st-bfrs-ok interp-st))
(interp-st-bfrs-ok new-interp-st)))
(defret pathcond-rewind-of-<fn>
(implies (and (not contra)
(equal mode (logicman->mode (interp-st->logicman interp-st))))
(equal (pathcond-rewind mode (interp-st->pathcond new-interp-st))
(pathcond-fix (interp-st->pathcond interp-st)))))
(defret pathcond-enabledp-of-<fn>
(iff* (nth *pathcond-enabledp* (interp-st->pathcond new-interp-st))
(nth *pathcond-enabledp* (interp-st->pathcond interp-st))))
(defret <fn>-pathcond-when-contra
(implies contra
(pathcond-equiv (interp-st->pathcond new-interp-st)
(interp-st->pathcond interp-st))))
(defret logicman-pathcond-eval-of-<fn>
(implies (and (logicman-pathcond-eval env (interp-st->pathcond interp-st)
(interp-st->logicman interp-st))
(bfr-eval test env (interp-st->logicman interp-st))
(equal logicman (interp-st->logicman new-interp-st)))
(logicman-pathcond-eval env (interp-st->pathcond new-interp-st) logicman)))
(defret interp-st-pathcond-assume-not-contradictionp
(implies (and (logicman-pathcond-eval env (interp-st->pathcond interp-st)
(interp-st->logicman interp-st))
(logicman-pathcond-eval env (interp-st->constraint interp-st)
(interp-st->logicman interp-st))
(bfr-eval test env (interp-st->logicman interp-st)))
(not contra))))
(define interp-st-pathcond-rewind (interp-st)
:guard (stobj-let ((pathcond (interp-st->pathcond interp-st))
(logicman (interp-st->logicman interp-st)))
(ok)
(pathcond-rewind-ok (lbfr-mode) pathcond)
ok)
:returns new-interp-st
:enabled t
(stobj-let ((logicman (interp-st->logicman interp-st))
(pathcond (interp-st->pathcond interp-st)))
(pathcond)
(pathcond-rewind (logicman->mode logicman) pathcond)
interp-st))
(define fgl-apply-match-not ((x fgl-object-p))
:guard (fgl-object-case x :g-apply)
:returns (mv ok
(negated-arg fgl-object-p))
(b* (((unless (mbt (fgl-object-case x :g-apply))) (mv nil nil))
((g-apply x))
(fn x.fn)
(args x.args)
((when (eq fn 'not))
(cond ((eql (len args) 1)
(mv t (fgl-object-fix (car args))))
(t (mv nil nil))))
((when (eq fn 'equal))
(b* (((unless (eql (len args) 2))
(mv nil nil))
((list arg1 arg2) args)
((when (fgl-object-case arg1
:g-concrete (eq arg1.val nil)
:otherwise nil))
(mv t (fgl-object-fix arg2)))
((when (fgl-object-case arg2
:g-concrete (eq arg2.val nil)
:otherwise nil))
(mv t (fgl-object-fix arg1))))
(mv nil nil))))
(mv nil nil))
///
(defret fgl-apply-match-not-correct
(implies ok
(iff (fgl-object-eval negated-arg env logicman)
(not (fgl-object-eval x env logicman))))
:hints(("Goal" :expand ((fgl-objectlist-eval (g-apply->args x) env)
(fgl-objectlist-eval (cdr (g-apply->args x)) env)
(fgl-objectlist-eval (cddr (g-apply->args x)) env))
:in-theory (enable fgl-apply))))
(defret fgl-object-count-of-g-apply-match-not
(implies ok
(< (fgl-object-count negated-arg) (fgl-object-count x)))
:hints(("Goal" :expand ((fgl-object-count x)
(fgl-objectlist-count (g-apply->args x))
(fgl-objectlist-count (cdr (g-apply->args x)))
(fgl-objectlist-count (cddr (g-apply->args x))))))
:rule-classes :linear)
(defret bfrlist-of-fgl-apply-match-not
(implies (not (member v (fgl-object-bfrlist x)))
(not (member v (fgl-object-bfrlist negated-arg))))))
(local
(defthm pseudo-var-list-p-of-append
(implies (and (pseudo-var-list-p x)
(pseudo-var-list-p y))
(pseudo-var-list-p (append x y)))))
(local (in-theory (disable pseudo-termp acl2::magic-ev)))
(define fgl-rewrite-relieve-hyp-synp ((synp-type symbolp)
(form pseudo-termp)
(vars)
(untrans-form)
(interp-st interp-st-bfrs-ok)
state)
:returns (mv successp
new-interp-st
new-state)
:prepwork ((local (defthm alist-keys-of-fgl-object-bindings
(implies (fgl-object-bindings-p x)
(and (pseudo-var-list-p (alist-keys x))
(symbol-listp (alist-keys x))))
:hints(("Goal" :in-theory (enable alist-keys)))))
(local (defthm symbol-alistp-when-fgl-object-bindings-p
(implies (fgl-object-bindings-p x)
(symbol-alistp x))))
(local (Defthm fgl-bfr-object-bindings-p-is-fgl-object-bindings-p-and-bfr-listp
(equal (fgl-bfr-object-bindings-p x)
(and (fgl-object-bindings-p x)
(bfr-listp (fgl-object-bindings-bfrlist x))))
:hints(("Goal" :in-theory (enable fgl-bfr-object-bindings-p-implies-fgl-object-bindings-p)))))
(local (defthm symbol-listp-when-pseudo-var-list-p
(implies (pseudo-var-list-p x)
(symbol-listp x)))))
:hooks ((:fix :omit (synp-type)))
(b* ((bindings (append (interp-st-minor-bindings interp-st)
(interp-st-bindings interp-st)))
(form (pseudo-term-fix form))
((mv err val interp-st state) (fancy-ev form bindings 100 interp-st state t t))
((when err)
(fgl-interp-error
:msg (fgl-msg "Synp error evaluating ~x0 (translated: ~x1): ~x2"
untrans-form form (if (or (consp err) (stringp err)) err "(no message)"))))
((when (eq synp-type 'syntaxp))
(fgl-interp-value val))
;; bind-free...
((unless val)
;; No error -- bind-free evaluated to NIL which means just don't do the rewrite.
(fgl-interp-value nil))
((unless (fgl-bfr-object-bindings-p val (interp-st-bfr-state)))
(fgl-interp-error
:msg (fgl-msg "Bind-free error: ~x0 evaluated to a non-FGL object alist: ~x1" untrans-form val)))
(newly-bound-vars (alist-keys val))
((when (and (symbol-listp vars)
(not (subsetp-eq vars newly-bound-vars))))
(fgl-interp-error
:msg (fgl-msg "Bind-free error: ~x0 evaluated to an alist not ~
containing the required vars ~x1: ~x2" untrans-form val vars)))
;; Consider allowing already-bound variables to be bound in the new
;; bindings, and just omitting them from the final bindings. Might be
;; convenient in case we want to bind a variable in different places for
;; different cases.
((when (intersectp-eq (alist-keys bindings) newly-bound-vars))
(fgl-interp-error
:msg (fgl-msg "Bind-free error: ~x0 evaluated to an alist containing already-bound vars: ~x1" untrans-form val)))
((unless (no-duplicatesp-eq newly-bound-vars))
(fgl-interp-error
:msg (fgl-msg "Bind-free error: ~x0 evaluated to an alist containing duplicate vars: ~x1" untrans-form val)))
(interp-st (interp-st-set-bindings (append val (interp-st-bindings interp-st)) interp-st)))
(fgl-interp-value t))
///
(local
(defthm major-stack-bfrlist-of-stack$a-set-bindings
(implies (and (not (member v (major-stack-bfrlist stack)))
(not (member v (fgl-object-bindings-bfrlist bindings))))
(not (member v (major-stack-bfrlist (stack$a-set-bindings bindings stack)))))
:hints(("Goal" :in-theory (enable stack$a-set-bindings
major-stack-bfrlist
major-frame-bfrlist
minor-frame-bfrlist
minor-stack-bfrlist)
:do-not-induct t))))
(local
(defthm fgl-object-bindings-bfrlist-of-stack$a-bindings-bindings
(implies (not (member v (major-stack-bfrlist stack)))
(not (member v (fgl-object-bindings-bfrlist (stack$a-bindings stack)))))
:hints(("Goal" :in-theory (enable stack$a-bindings
major-stack-bfrlist
major-frame-bfrlist)
:do-not-induct t))))
(local (in-theory (disable stack$a-set-bindings
stack$a-bindings
stack$a-minor-bindings)))
(local (in-theory (enable bfr-listp-when-not-member-witness)))
(defret interp-st-bfrs-ok-of-<fn>
(implies (interp-st-bfrs-ok interp-st)
(interp-st-bfrs-ok new-interp-st)))
(defret interp-st-get-of-<fn>
(implies (member (interp-st-field-fix key)
'(:logicman :bvar-db :pathcond :constraint :constraint-db :equiv-contexts :reclimit))
(equal (interp-st-get key new-interp-st)
(interp-st-get key interp-st))))
(defret multivalues-of-<fn>
(equal (list . <values>)
<call>))
(defret <fn>-preserves-errmsg
(implies (interp-st->errmsg interp-st)
(equal (interp-st->errmsg new-interp-st)
(interp-st->errmsg interp-st))))
(local (acl2::use-trivial-ancestors-check))
(defret interp-st->errmsg-equal-unreachable-of-<fn>
(implies (not (equal (interp-st->errmsg interp-st) :unreachable))
(not (equal (interp-st->errmsg new-interp-st) :unreachable))))
(defret w-state-of-<fn>
(equal (w new-state) (w state))))
;; Used in merge-branches to recognize a branch on which we can unify some
;; function. BOZO We might want to try more than one function for some objects
;; -- e.g. for :g-integer we could do both int and intcons, for concrete values
;; that are conses we could do both concrete and bool, etc. Ideally we'd try
;; all the functions that the argument could unify with.
(define fgl-fncall-object->fn ((x fgl-object-p))
:returns (fn pseudo-fnsym-p) ;; nil if didn't match
(fgl-object-case x
:g-boolean 'bool
:g-integer 'int
:g-concrete 'concrete
:g-apply x.fn
:g-cons 'cons
:g-ite 'if
:otherwise nil))
(defthm fgl-ev-list-of-kwote-lst
(equal (fgl-ev-list (kwote-lst x) a)
(true-list-fix x))
:hints (("goal" :induct (len x))))
(defthm not-quote-when-pseudo-fnsym-p
(implies (pseudo-fnsym-p x)
(not (equal x 'quote))))
(define fgl-object-recognize-merge-fncall ((x fgl-object-p))
;; Note: This is used when we want to merge two calls of the same function,
;; e.g. (if test (foo x y) (foo w z)). We don't want to match a g-boolean
;; to (bool x), for example, because that would lead to an infinite loop where we
;; match (if test (g-boolean b) (g-boolean c)) as
;; (if test (bool (g-boolean b)) (bool (g-boolean c)))
;; then merge the args, i.e. (list (g-boolean b)) (list (g-boolean c))
;; in which when we merge the first arg we get back to
;; (if test (g-boolean b) (g-boolean c)) and get stuck in an infinite loop.
:returns (mv (fn pseudo-fnsym-p :rule-classes (:rewrite (:type-prescription :typed-term fn)))
(args fgl-objectlist-p))
(fgl-object-case x
;; :g-boolean (mv 'bool (list (fgl-object-fix x)))
;; :g-integer (mv 'int (list (fgl-object-fix x)))
;; :g-concrete (mv 'concrete (list (fgl-object-fix x)))
:g-apply (mv x.fn x.args)
:g-cons (mv 'cons (list x.car x.cdr))
:g-concrete (if (consp x.val)
(mv 'cons (list (g-concrete (car x.val))
(g-concrete (cdr x.val))))
(mv nil nil))
:otherwise (mv nil nil))
///
(defret eval-when-fgl-object-recognize-merge-fncall2
(implies (and fn
(equal fn1 fn))
(equal (fgl-ev (cons fn1
(kwote-lst (fgl-objectlist-eval args env)))
a)
(fgl-object-eval x env)))
:hints(("Goal" :in-theory (enable fgl-apply fgl-objectlist-eval
fgl-ev-of-fncall-args))))
(defret bfr-listp-fgl-objectlist-bfrlist-of-<fn>
(implies (bfr-listp (fgl-object-bfrlist x))
(bfr-listp (fgl-objectlist-bfrlist args)))
:hints (("goal" :Expand ((fgl-object-bfrlist x))))))
(defthm logicman-get-of-bfr-ite-bss-fn
(implies (and (not (equal (logicman-field-fix key) :aignet))
(not (equal (logicman-field-fix key) :strash)))
(equal (logicman-get key (mv-nth 1 (bfr-ite-bss-fn c v1 v0 logicman)))
(logicman-get key logicman)))
:hints(("Goal" :in-theory (enable bfr-ite-bss-fn
bfr-ite-bss-fn-aux))))
(define fgl-object-basic-merge ((test lbfr-p)
(then fgl-object-p)
(else fgl-object-p)
(make-ites-p)
&optional
(logicman 'logicman))
:measure (acl2::two-nats-measure (+ (fgl-object-count then)
(fgl-object-count else))
(+ (acl2-count (g-concrete->val then))
(acl2-count (g-concrete->val else))))
:prepwork ((local (include-book "primitive-lemmas"))
(local (defthm-fgl-bfr-object-fix-flag
(defthm fgl-object-count-of-fgl-bfr-object-fix
(equal (fgl-object-count (fgl-bfr-object-fix x))
(fgl-object-count x))
:hints ('(:in-theory (enable fgl-object-count)
:expand ((fgl-bfr-object-fix x))))
:flag fgl-bfr-object-fix)
(defthm fgl-objectlist-count-of-fgl-bfr-objectlist-fix
(equal (fgl-objectlist-count (fgl-bfr-objectlist-fix x))
(fgl-objectlist-count x))
:hints ('(:in-theory (enable fgl-objectlist-count)
:expand ((fgl-bfr-objectlist-fix x))))
:flag fgl-bfr-objectlist-fix)
(defthm fgl-object-alist-count-of-fgl-bfr-object-alist-fix
(equal (fgl-object-alist-count (fgl-bfr-object-alist-fix x))
(fgl-object-alist-count x))
:hints ('(:in-theory (enable fgl-object-alist-count)
:expand ((fgl-bfr-object-alist-fix x)
(fgl-object-alist-count x)
(fgl-object-alist-fix x))))
:flag fgl-bfr-object-alist-fix)))
(local (defthm g-concrete->val-of-fgl-bfr-object-fix
(implies (fgl-object-case x :g-concrete)
(equal (g-concrete->val (fgl-bfr-object-fix x))
(g-concrete->val x)))
:hints(("Goal" :in-theory (enable fgl-bfr-object-fix)))))
(local (defthm fgl-object-kind-of-fgl-bfr-object-fix
(equal (fgl-object-kind (fgl-bfr-object-fix x))
(fgl-object-kind x))
:hints(("Goal" :expand ((fgl-bfr-object-fix x))))))
(local (defthm fgl-object-count-of-gobj-syntactic-list->car
(implies (gobj-syntactic-consp x)
(<= (fgl-object-count (gobj-syntactic-list->car x))
(fgl-object-count x)))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->car gobj-syntactic-consp fgl-object-count)))
:rule-classes :linear))
(local (defthm fgl-object-count-of-gobj-syntactic-list->cdr
(implies (gobj-syntactic-consp x)
(<= (fgl-object-count (gobj-syntactic-list->cdr x))
(fgl-object-count x)))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->cdr gobj-syntactic-consp fgl-object-count)))
:rule-classes :linear))
(local (defthm gobj-syntactic-consp-of-fgl-bfr-object-fix
(equal (gobj-syntactic-consp (fgl-bfr-object-fix x))
(gobj-syntactic-consp x))
:hints(("Goal" :in-theory (enable gobj-syntactic-consp)))))
(local (defthm gobj-syntactic-booleanp-of-fgl-bfr-object-fix
(equal (gobj-syntactic-booleanp (fgl-bfr-object-fix x))
(gobj-syntactic-booleanp x))
:hints(("Goal" :in-theory (enable gobj-syntactic-booleanp)))))
(local (defthm gobj-syntactic-integerp-of-fgl-bfr-object-fix
(equal (gobj-syntactic-integerp (fgl-bfr-object-fix x))
(gobj-syntactic-integerp x))
:hints(("Goal" :in-theory (enable gobj-syntactic-integerp)))))
(local (defthm acl2-count-of-gobj-syntactic-list->car
(implies (and (gobj-syntactic-consp x)
(equal (fgl-object-count (gobj-syntactic-list->car x))
(fgl-object-count x)))
(< (acl2-count (g-concrete->val (fgl-bfr-object-fix (gobj-syntactic-list->car x))))
(acl2-count (g-concrete->val x))))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->car gobj-syntactic-consp fgl-object-count)))
:rule-classes :linear))
(local (defthm acl2-count-of-gobj-syntactic-list->cdr
(implies (and (gobj-syntactic-consp x)
(equal (fgl-object-count (gobj-syntactic-list->cdr x))
(fgl-object-count x)))
(< (acl2-count (g-concrete->val (fgl-bfr-object-fix (gobj-syntactic-list->cdr x))))
(acl2-count (g-concrete->val x))))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->cdr gobj-syntactic-consp fgl-object-count)))
:rule-classes :linear))
(local (defthm gobj-syntactic-list->car-of-fgl-bfr-object-fix
(equal (gobj-syntactic-list->car (fgl-bfr-object-fix x))
(fgl-bfr-object-fix (gobj-syntactic-list->car x)))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->car fgl-bfr-object-fix)
:expand ((fgl-bfr-object-fix x))))))
(local (defthm gobj-syntactic-list->cdr-of-fgl-bfr-object-fix
(equal (gobj-syntactic-list->cdr (fgl-bfr-object-fix x))
(fgl-bfr-object-fix (gobj-syntactic-list->cdr x)))
:hints(("Goal" :in-theory (enable gobj-syntactic-list->cdr fgl-bfr-object-fix)
:expand ((fgl-bfr-object-fix x))))))
(local (defthm gobj-syntactic-integer->bits-of-fgl-bfr-object-fix
(equal (gobj-syntactic-integer->bits (fgl-bfr-object-fix x))
(true-list-fix (bfr-list-fix (gobj-syntactic-integer->bits x))))
:hints(("Goal" :in-theory (enable gobj-syntactic-integer->bits fgl-bfr-object-fix)
:expand ((fgl-bfr-object-fix x))))))
(local (defthm bfr-p-when-booleanp
(implies (booleanp x)
(bfr-p x))
:hints(("Goal" :in-theory (enable booleanp)))))
(local (defthm gobj-syntactic-boolean->bool-of-fgl-bfr-object-fix
(implies (gobj-syntactic-booleanp x)
(equal (gobj-syntactic-boolean->bool (fgl-bfr-object-fix x))
(bfr-fix (gobj-syntactic-boolean->bool x))))
:hints(("Goal" :in-theory (enable gobj-syntactic-boolean->bool
gobj-syntactic-booleanp
fgl-bfr-object-fix)
:expand ((fgl-bfr-object-fix x))))))
(local (defthm equal-of-fgl-object-eval-when-equal-of-fgl-bfr-object-fix
(let ((bfrstate (logicman->bfrstate)))
(implies (equal (fgl-bfr-object-fix x) (fgl-bfr-object-fix y))
(equal (equal (fgl-object-eval x env)
(fgl-object-eval y env))
t)))
:hints (("goal" :use ((:instance fgl-object-eval-of-fgl-bfr-object-fix
(x x))
(:instance fgl-object-eval-of-fgl-bfr-object-fix
(x y)))
:in-theory (disable fgl-object-eval-of-fgl-bfr-object-fix))))))
:verify-guards nil
:returns (mv (okp)
(obj fgl-object-p)
new-logicman)
:guard-hints (("goal" :in-theory (enable bfr-ite-bss-fn)))
:guard (and (fgl-bfr-object-p then (logicman->bfrstate))
(fgl-bfr-object-p else (logicman->bfrstate)))
(b* ((bfrstate (logicman->bfrstate))
(then (fgl-bfr-object-fix then))
(else (fgl-bfr-object-fix else))
((when (equal (fgl-object-fix then)
(fgl-object-fix else)))
(mv t (fgl-bfr-object-fix then) logicman))
((when (and (gobj-syntactic-booleanp then)
(gobj-syntactic-booleanp else)))
(b* (((mv bfr logicman)
(bfr-ite (bfr-fix test)
(gobj-syntactic-boolean->bool then)
(gobj-syntactic-boolean->bool else)
logicman)))
(mv t (mk-g-boolean bfr) logicman)))
((when (and (gobj-syntactic-integerp then)
(gobj-syntactic-integerp else)))
(b* (((mv bfr logicman)
(bfr-ite-bss-fn (bfr-fix test)
(gobj-syntactic-integer->bits then)
(gobj-syntactic-integer->bits else)
logicman)))
(mv t (mk-g-integer bfr) logicman)))
((when (and (gobj-syntactic-consp then)
(gobj-syntactic-consp else)))
(b* ((test (bfr-fix test))
((mv car-ok car logicman)
(fgl-object-basic-merge test
(gobj-syntactic-list->car then)
(gobj-syntactic-list->car else)
make-ites-p
logicman))
((unless car-ok)
(mv nil nil logicman))
((mv cdr-ok cdr logicman)
(fgl-object-basic-merge test
(gobj-syntactic-list->cdr then)
(gobj-syntactic-list->cdr else)
make-ites-p
logicman))
((unless cdr-ok)
(mv nil nil logicman)))
(mv t (mk-g-cons car cdr) logicman))))
(mv make-ites-p
(if make-ites-p (g-ite (mk-g-boolean (bfr-fix test)) then else) nil)
logicman))
///
(local (in-theory (disable bfr-listp-when-not-member-witness
fgl-bfr-object-fix-when-fgl-bfr-object-p
(:d fgl-object-basic-merge))))
;; (defret fgl-bfr-object-p-of-<fn>
;; (fgl-bfr-object-p obj (logicman->bfrstate new-logicman)))
(fty::deffixequiv fgl-object-basic-merge
:hints (("goal" :induct t)
'(:expand ((:free (test else) (fgl-object-basic-merge test then else make-ites-p))
(:free (test then) (fgl-object-basic-merge test then else make-ites-p))))))
(local (defthm bfr-listp-fgl-object-bfrlist-of-fgl-bfr-object-fix
(bfr-listp (fgl-object-bfrlist (fgl-bfr-object-fix x)))
:hints (("goal" :use ((:instance return-type-of-fgl-bfr-object-fix.new-x))
:in-theory (disable return-type-of-fgl-bfr-object-fix.new-x)))))
(defret logicman-extension-p-of-<fn>
(logicman-extension-p new-logicman logicman)
:hints (("goal" :expand (<call>) :induct <call>)))
(defret bfr-nvars-of-<fn>
(equal (bfr-nvars new-logicman)
(bfr-nvars logicman))
:hints (("goal" :expand (<call>) :induct <call>)))
(defret bfr-listp-of-fgl-object-basic-merge
;; (implies (and (lbfr-p test)
;; (lbfr-listp (fgl-object-bfrlist thenval))
;; (lbfr-listp (fgl-object-bfrlist elseval)))
(bfr-listp (fgl-object-bfrlist obj) (logicman->bfrstate new-logicman))
:hints (("goal" :expand (<call>) :induct <call>)
(and stable-under-simplificationp
'(:in-theory (enable bfr-listp-when-not-member-witness)))))
(verify-guards fgl-object-basic-merge-fn
:hints ((and stable-under-simplificationp
'(:in-theory (enable bfr-listp-when-not-member-witness)))))
(defret eval-of-fgl-object-basic-merge
(implies okp
(equal (fgl-object-eval obj env new-logicman)
(if (gobj-bfr-eval test env)
(fgl-object-eval then env logicman)
(fgl-object-eval else env logicman))))
:hints(("Goal" :expand (<call>) :induct <call>
:in-theory (enable gobj-bfr-eval ;; gobj-bfr-list-eval-is-bfr-list-eval
fgl-object-eval-when-gobj-syntactic-consp))))
(local (defthm fgl-bfr-objectlist-of-fgl-bfr-object-fix
(bfr-listp (fgl-object-bfrlist (fgl-bfr-object-fix x bfrstate)) bfrstate)
:hints (("goal" :use ((:instance fgl-bfr-object-p-when-fgl-object-p
(x (fgl-bfr-object-fix x bfrstate))))))))
(deffixequiv fgl-object-basic-merge
:hints (("goal" :induct (fgl-object-basic-merge test then else logicman)
:expand ((:free (then) (fgl-object-basic-merge test then else logicman))
(:free (else) (fgl-object-basic-merge test then else logicman))))))
(defret logicman-get-of-<fn>
(implies (and (not (equal (logicman-field-fix key) :aignet))
(not (equal (logicman-field-fix key) :strash)))
(equal (logicman-get key new-logicman)
(logicman-get key logicman)))
:hints(("Goal" :expand (<call>) :induct <call>))))
(define interp-st-fgl-object-basic-merge ((test interp-st-bfr-p)
(then fgl-object-p)
(else fgl-object-p)
interp-st
state)
:returns (mv (obj fgl-object-p)
new-interp-st
new-state)
:guard (and (interp-st-bfr-listp (fgl-object-bfrlist then) interp-st)
(interp-st-bfr-listp (fgl-object-bfrlist else) interp-st))
(b* ((make-ites (interp-flags->make-ites (interp-st->flags interp-st))))
(stobj-let ((logicman (interp-st->logicman interp-st)))
(okp obj logicman)
(fgl-object-basic-merge test then else make-ites logicman)
(b* (((unless okp)
(fgl-interp-error :msg "If-then-else failed to merge -- see debug obj"
:debug-obj (list :test test
:then (fgl-object-fix then)
:else (fgl-object-fix else)))))
(mv obj interp-st state))))
///
(defret interp-st-bfrs-ok-of-<fn>
(implies (interp-st-bfrs-ok interp-st)
(interp-st-bfrs-ok new-interp-st)))
(defret bfr-object-p-of-<fn>
(lbfr-listp (fgl-object-bfrlist obj) (interp-st->logicman new-interp-st)))
(defret logicman-extension-p-of-<fn>
(logicman-extension-p (interp-st->logicman new-interp-st) (interp-st->logicman interp-st)))
(defret eval-of-interp-st-fgl-object-basic-merge
(implies (not (interp-st->errmsg new-interp-st))
(equal (fgl-object-eval obj env (interp-st->logicman new-interp-st))
(if (gobj-bfr-eval test env (interp-st->logicman interp-st))