forked from acl2/acl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof-builder-a.lisp
1695 lines (1493 loc) · 68 KB
/
proof-builder-a.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.
(in-package "ACL2")
; PC globals are those that can be changed from inside the proof-builder's
; interactive loop, and whose values we want saved. Note that state-stack can
; also be changed outside the interactive loop (by use of :instructions), so we
; need to be careful. We'll manage this by keeping state-stack as a PC global,
; updating pc-output upon entry to reflect the latest value of state-stack.
(defmacro pc-value (sym)
(cond ((eq sym 'ss-alist)
'(f-get-global 'pc-ss-alist state))
(t `(cdr (assoc-eq ',sym
(f-get-global 'pc-output state))))))
(defmacro pc-assign (key val)
(cond ((eq key 'ss-alist)
`(f-put-global 'pc-ss-alist ,val state))
(t `(f-put-global
'pc-output
(put-assoc-eq ',key ,val
(f-get-global 'pc-output state))
state))))
(defun initialize-pc-acl2 (state)
(er-progn
(assign pc-output nil)
(pprogn
(pc-assign ss-alist nil)
(pc-assign old-ss nil)
(pc-assign state-stack nil)
(pc-assign next-pc-enabled-array-suffix 0)
(pc-assign pc-depth 0) ; for the proof-builder-cl-proc clause-processor
(assign in-verify-flg nil))))
(defmacro state-stack ()
'(pc-value state-stack))
(defmacro old-ss ()
'(pc-value old-ss))
; The entries in ss-alist are of the form (name state-stack . old-ss).
(defmacro ss-alist ()
'(pc-value ss-alist))
(defrec pc-info
((print-macroexpansion-flg . print-prompt-and-instr-flg)
.
(prompt . prompt-depth-prefix))
nil)
(defmacro pc-print-prompt-and-instr-flg ()
'(access pc-info (f-get-global 'pc-info state) :print-prompt-and-instr-flg))
(defmacro pc-print-macroexpansion-flg ()
'(access pc-info (f-get-global 'pc-info state) :print-macroexpansion-flg))
(defmacro pc-prompt ()
'(access pc-info (f-get-global 'pc-info state) :prompt))
(defmacro pc-prompt-depth-prefix ()
'(access pc-info (f-get-global 'pc-info state) :prompt-depth-prefix))
; We will maintain an invariant that there are no unproved goals hanging around
; in the pc-state. Moreover, for simplicity, we leave it up to each command to
; ensure that no newly-created goal has a conclusion with a non-NIL explicit
; value. The function remove-proved-goal-from-pc-state will be applied to
; remove the current goal if it has been proved.
; The pc-ens component of the state is either an enabled structure or else is
; NIL, which indicates that we should use the global enabled structure.
(defrec pc-state
(instruction
(goals . abbreviations)
local-tag-tree
pc-ens
.
tag-tree)
nil)
(defconst *pc-state-fields-for-primitives*
'(instruction goals abbreviations tag-tree local-tag-tree pc-ens))
(defmacro instruction (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:instruction))
(defmacro goals (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:goals))
(defmacro abbreviations (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:abbreviations))
(defmacro local-tag-tree (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:local-tag-tree))
(defmacro pc-ens (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:pc-ens))
(defmacro tag-tree (&optional state-stack-supplied-p)
`(access pc-state
(car ,(if state-stack-supplied-p
'state-stack
'(state-stack)))
:tag-tree))
; A state-stack is a list of goal records. The goal contains explicit hyps,
; and also (via current-addr) implicit if-term governors. Depends-on is the
; first suffix available for subgoals of the current goal; so, (goal-name . n)
; has been used at some point for exactly those positive integers n for which n
; < depends-on.
(defrec goal
(conc depends-on
(hyps . current-addr)
goal-name)
t)
(defconst *goal-fields*
'(conc hyps current-addr goal-name depends-on))
(defmacro conc (&optional ss-supplied-p)
`(access goal (car (goals ,ss-supplied-p)) :conc))
(defmacro hyps (&optional ss-supplied-p)
`(access goal (car (goals ,ss-supplied-p)) :hyps))
(defmacro current-addr (&optional ss-supplied-p)
`(access goal (car (goals ,ss-supplied-p)) :current-addr))
(defmacro goal-name (&optional ss-supplied-p)
`(access goal (car (goals ,ss-supplied-p)) :goal-name))
(defmacro depends-on (&optional ss-supplied-p)
`(access goal (car (goals ,ss-supplied-p)) :depends-on))
(defmacro make-official-pc-command (sym)
`(intern-in-package-of-symbol (symbol-name ,sym)
'acl2-pc::acl2-pkg-witness))
(defun intern-in-keyword-package (sym)
(declare (xargs :guard (symbolp sym)))
(intern (symbol-name sym) "KEYWORD"))
(defun make-pretty-pc-command (x)
(declare (xargs :guard (symbolp x)))
;; Returns the user-and-stored version of the command x.
(intern-in-keyword-package x))
(defun make-pretty-pc-instr (instr)
(declare (xargs :guard (or (symbolp instr)
(and (consp instr)
(symbolp (car instr))))))
(if (atom instr)
(make-pretty-pc-command instr)
(if (null (cdr instr))
(make-pretty-pc-command (car instr))
(cons (make-pretty-pc-command (car instr))
(cdr instr)))))
(defmacro change-pc-state (pc-s &rest args)
(list* 'change 'pc-state pc-s args))
(defun make-official-pc-instr (instr)
; This function always returns a syntactically legal instruction, i.e., a true
; list whose car is a symbol in the ACL2-PC package
(if (consp instr)
(if (and (symbolp (car instr))
(true-listp (cdr instr)))
(cons (make-official-pc-command (car instr)) (cdr instr))
(list (make-official-pc-command 'illegal) instr))
(if (symbolp instr)
(list (make-official-pc-command instr))
(if (and (integerp instr)
(> instr 0))
(list (make-official-pc-command 'dv) instr)
(list (make-official-pc-command 'illegal) instr)))))
(defun check-formals-length (formals args fn ctx state)
(declare (xargs :guard (and (symbol-listp formals)
(true-listp args))))
(let ((max-length (if (member-eq '&rest formals)
'infinity
(length (remove '&optional formals))))
(min-length (let ((k (max (length (member-eq '&rest formals))
(length (member-eq '&optional formals)))))
(- (length formals) k)))
(n (length args)))
(if (and (<= min-length n)
(or (eq max-length 'infinity)
(<= n max-length)))
(value t)
(if (equal min-length max-length)
(er soft ctx
"Wrong number of arguments in argument list ~x0 to ~x1. There should ~
be ~n2 argument~#3~[s~/~/s~] to ~x1."
args fn min-length (zero-one-or-more min-length))
(if (equal max-length 'infinity)
(er soft ctx
"Wrong number of arguments in argument list ~x0 to ~x1. There should ~
be at least ~n2 argument~#3~[s~/~/s~] to ~x1."
args fn min-length (min min-length 2))
(er soft ctx
"Wrong number of arguments in argument list ~x0 to ~x1. There should ~
be between ~n2 and ~n3 arguments to ~x1."
args fn min-length max-length))))))
(defun check-&optional-and-&rest (formals state)
(cond
((not (true-listp formals))
(er soft 'check-&optional-and-&rest
"The formals are supposed to be a true list, but they are ~x0."
formals))
;; &optional can only occur at most once
((member-eq '&optional (cdr (member-eq '&optional formals)))
(er soft 'check-&optional-and-&rest
"The &optional keywords occurs more than once in ~x0."
formals))
;; &rest can only occur next to the end
(t (let ((r-formals (reverse formals)))
(if (or (eq (car r-formals) '&optional)
(eq (car r-formals) '&rest))
(er soft 'check-&optional-and-&rest
"The &optional and &rest keywords may not occur as the last element of ~
the formals list, ~x0."
formals)
(if (member-eq '&rest (cddr r-formals))
(er soft 'check-&optional-and-&rest
"The &rest keyword may not occur except as the next-to-last ~
member of the formals list, which is not the case for ~x0."
formals)
(value t)))))))
(defun make-let-pairs-from-formals (formals arg)
;; e.g. (make-let-pairs-from-formals '(a b c) 'x) =
;; ((a (car x)) (b (car (cdr x))) (c (car (cdr (cdr x)))))
(if (consp formals)
(if (eq (car formals) '&optional)
(make-let-pairs-from-formals (cdr formals) arg)
(if (eq (car formals) '&rest)
(list (list (cadr formals) arg))
(cons (list (car formals) (list 'car arg))
(make-let-pairs-from-formals (cdr formals) (list 'cdr arg)))))
nil))
;; The following are like all-vars, but heuristic in that they deal with untranslated forms.
(mutual-recursion
(defun all-symbols (form)
(cond
((symbolp form)
(list form))
((atom form)
nil)
((eq (car form) (quote quote))
nil)
(t
;; used to have just (all-symbols-list (cdr form)) below, but
;; then (cond (current-addr ...) ...) messed up
(union-eq (all-symbols (car form))
(all-symbols-list (cdr form))))))
(defun all-symbols-list (x)
(if (consp x)
(union-eq (all-symbols (car x))
(all-symbols-list (cdr x)))
nil))
)
(defun make-access-bindings (record-name record fields)
(if (consp fields)
(cons `(,(car fields) (access ,record-name ,record ,(intern-in-keyword-package (car fields))))
(make-access-bindings record-name record (cdr fields)))
nil))
(defun let-form-for-pc-state-vars (form)
(let ((vars (all-symbols form)))
(let* ((goal-vars
(intersection-eq *goal-fields* vars))
(pc-state-vars
(if goal-vars
(intersection-eq *pc-state-fields-for-primitives* (cons 'goals vars))
(intersection-eq *pc-state-fields-for-primitives* vars))))
`(let ,(make-access-bindings 'pc-state 'pc-state pc-state-vars)
(let ,(make-access-bindings 'goal '(car goals) goal-vars)
,form)))))
(defun check-field-names (formals ctx state)
(let ((bad-formals (intersection-eq formals
(append *goal-fields* *pc-state-fields-for-primitives*))))
(if bad-formals
(er soft ctx
"It is illegal to use names of pc-state or goal fields as formals to~
define commands with ~x0, in this case ~&1."
ctx bad-formals)
(value t))))
(defmacro print-no-change (&optional str alist (col '0))
`(print-no-change-fn ,str ,alist ,col state))
(defmacro print-no-change2 (&rest args)
`(pprogn ,(cons 'print-no-change args)
(mv nil state)))
(defun print-no-change-fn (str alist col state)
(declare (xargs :guard (or (stringp str)
(null str))))
(io? proof-builder nil state
(col alist str)
(mv-let (col state)
(let ((channel (proofs-co state)))
(mv-let (col state)
(fmt1 "~|*** NO CHANGE ***" nil col channel state nil)
(if str
(mv-let (col state)
(fmt1 " -- " nil col channel state nil)
(mv-let (col state)
(fmt1 str alist col channel state
(term-evisc-tuple nil state))
(fmt1 "~|" nil col channel state nil)))
(fmt1 "~|" nil col channel state nil))))
(declare (ignore col))
state)))
(defmacro maybe-update-instruction (instr pc-state-and-state)
`(mv-let (pc-state state)
,pc-state-and-state
(mv (and pc-state ; in case the instruction failed!
(if (access pc-state pc-state :instruction)
pc-state
(change-pc-state pc-state :instruction (make-pretty-pc-instr ,instr))))
state)))
(defun pc-primitive-defun-form (raw-name name formals doc body)
`(defun ,name (args state)
;; notice that args aren't ignored, since even if they're nil, they're
;; used for arity checking
,@(and doc (list doc))
(mv-let
;; can't use er-progn because we return (mv nil state) for errors
(erp v state)
(check-formals-length ',formals args ',raw-name ',name state)
(declare (ignore v))
(if erp
(mv nil state)
(let ((pc-state
(change pc-state
(car (state-stack))
:instruction nil))
,@(make-let-pairs-from-formals formals 'args))
;; in case we have (declare (ignore pc-state))
,@(butlast body 1)
(maybe-update-instruction
(cons ',raw-name args)
,(let-form-for-pc-state-vars (car (last body)))))))))
(defun pc-command-table-guard (key val wrld)
; We wrap the pc-command-table guard into this function so that we can redefine
; it when modifying the ACL2 system.
(and (function-symbolp key wrld)
(or (eq val 'macro)
(eq val 'atomic-macro)
(eq val 'meta)
(and (eq val 'primitive)
(global-val 'boot-strap-flg wrld)))))
(table pc-command-table nil nil
:guard
; Since there isn't any documentation particularlly relevant to this table, we
; avoid using set-table-guard here.
; Before adding this table guard after Version_4.3, we were able to certify the
; following book.
; (in-package "ACL2")
; (program)
; (set-state-ok t)
; (define-pc-primitive foo (&rest rest-args)
; (declare (ignore rest-args))
; (mv (change-pc-state pc-state :goals (cdr goals))
; state))
; (logic)
; (defthm bug
; nil
; :instructions (:foo)
; :rule-classes nil)
(pc-command-table-guard key val world))
(defmacro add-pc-command (name command-type)
`(table pc-command-table ',name ,command-type))
(defmacro pc-command-type (name)
`(cdr (assoc-equal ,name (table-alist 'pc-command-table (w state)))))
(defmacro print-no-change3 (&optional str alist (col '0))
`(pprogn (print-no-change-fn ,str ,alist ,col state)
(value nil)))
(defun add-pc-command-1 (name command-type state)
(table-fn
'pc-command-table
`(',name ',command-type)
state
(list 'table 'pc-command-table (list 'quote name) (list 'quote command-type))))
(defun toggle-pc-macro-fn (name new-tp state)
(let ((tp (pc-command-type name)))
(if (null tp)
(print-no-change3 "The command ~x0 is not a proof-builder command."
(list (cons #\0 name)))
(case tp
(macro (if (or (null new-tp) (equal (symbol-name new-tp) "ATOMIC-MACRO"))
(add-pc-command-1 name 'atomic-macro state)
(if (equal (symbol-name new-tp) "MACRO")
(print-no-change3 "~x0 is already a non-atomic macro."
(list (cons #\0 name)))
(print-no-change3 "You can't change a proof-builder macro ~
to have type ~x0."
(list (cons #\0 new-tp))))))
(atomic-macro (if (or (null new-tp) (equal (symbol-name new-tp) "MACRO"))
(add-pc-command-1 name 'macro state)
(if (equal (symbol-name new-tp) "ATOMIC-MACRO")
(print-no-change3 "~x0 is already an atomic macro."
(list (cons #\0 name)))
(print-no-change3 "You can't change a proof-builder atomic macro ~
to have type ~x0."
(list (cons #\0 new-tp))))))
(otherwise (print-no-change3 "You can't change the type of a proof-builder ~x0 command."
(list (cons #\0 tp))))))))
(defun pc-meta-or-macro-defun (raw-name name formals doc body)
`(defun ,name (args state)
;; notice that args aren't ignored, since even if they're nil, they're
;; used for arity checking
(declare (xargs :mode :program :stobjs state))
,@(and doc (list doc))
(er-progn
(check-formals-length ',formals args ',raw-name ',name state)
(let ((state-stack (state-stack))
,@(make-let-pairs-from-formals formals 'args))
;; in case we have a doc-string and/or declare forms
,@(butlast body 1)
(let ((very-silly-copy-of-state-stack state-stack))
; This silly trick ensures that we don't have to declare state-stack ignored.
(declare (ignore very-silly-copy-of-state-stack))
,(car (last body)))))))
(defun goal-names (goals)
(if (consp goals)
(cons (access goal (car goals) :goal-name)
(goal-names (cdr goals)))
nil))
(defun instructions-of-state-stack (ss acc)
(if (consp ss)
(instructions-of-state-stack
(cdr ss)
(cons (access pc-state (car ss) :instruction)
acc))
;; at the end we cdr the accumulator to get rid of the `start' instruction
(cdr acc)))
(defmacro fms0 (str &optional alist col (evisc-tuple 'nil evisc-tuple-p))
;; This should only be called when the cursor is on the left margin, or when
;; a fresh line or new line indicator starts the string, unless col is
;; supplied.
`(mv-let (new-col state)
(fmt1 ,str ,alist
,(or col
0)
(proofs-co state)
state
,(if evisc-tuple-p evisc-tuple '(term-evisc-tuple nil state)))
(declare (ignore new-col))
state))
(defmacro with-output-forced (output-chan signature code)
; Use this to force output to output-chan after executing the give code. See
; print-pc-prompt and print-prompt for examples that make the usage pretty
; obvious.
(cond ((or (not (true-listp signature))
(member-eq output-chan signature))
(er hard 'with-output-forced
"Ill-formed call: ~x0"
`(with-output-forced ,output-chan ,signature ,code)))
(t
#+acl2-loop-only
code
#-acl2-loop-only
`(mv-let ,signature
,code
#-acl2-loop-only
(progn (force-output (get-output-stream-from-channel ,output-chan))
(mv ,@signature))
#+acl2-loop-only
(mv ,@signature)))))
(defun print-pc-prompt (state)
; This function does not print a new line before or after the prompt. It
; assumes that we're in column 0.
(io? proof-builder nil (mv col state)
()
(let ((chan (proofs-co state)))
(with-output-forced
chan
(col state)
(fmt1 (pc-prompt) nil 0 chan state nil)))
:default-bindings ((col 0))))
(defun pc-macroexpand (raw-instr state)
; We assume that instr has already been "parsed", so that it's a list whose car
; is in the ACL2-PC package. This function repeatedly expands instr until we
; have an answer. At one time we intended not to allow state to be returned by
; macroexpansion, but now we want to take a more general view that all kinds of
; what used to be called "help" commands are implemented by macro commands.
; Notice that unlike Lisp macros, the global Lisp state is available for the
; expansion. Hence we can query the ACL2 database etc.
; Moreover, we can modify state, and in particular set state globals but with
; the same protection as we have during make-event: the use of
; protect-system-state-globals. That macro is invoked by the call below of
; xtrans-eval.
(let ((instr (make-official-pc-instr raw-instr)))
; Notice that instr is syntactically valid, i.e. is a true-listp headed by a
; symbol in the acl2-pc package -- even if raw-instr isn't of this form.
(if (member-eq (pc-command-type (car instr)) '(macro atomic-macro))
(er-let* ((val (xtrans-eval (list (car instr)
(list 'quote (cdr instr))
'state)
nil t t
'pc-macroexpand
state t)))
(pc-macroexpand val state))
; So, now we have an instruction that is primitive or meta.
(value instr))))
(defun find-goal (name goals)
(if (consp goals)
(if (equal name (access goal (car goals) :goal-name))
(car goals)
(find-goal name (cdr goals)))
nil))
(defun print-all-goals-proved-message (state)
(io? proof-builder nil state
nil
(pprogn
(print-no-change "There are no unproved goals!")
(if (f-get-global 'in-verify-flg state)
(fms0 "You may wish to exit.~%")
state))))
(defmacro when-goals (form)
`(if (goals t)
,form
(print-all-goals-proved-message state)))
(defmacro when-goals-trip (form)
`(if (goals t)
,form
(pprogn (print-all-goals-proved-message state)
(value 'skip))))
(defun current-immediate-deps (goal-name goal-names)
;; Returns all names in goal-names that are immediate dependents of goal-name.
(if (consp goal-names)
(if (and (consp (car goal-names))
(equal goal-name (caar goal-names)))
(cons (car goal-names)
(current-immediate-deps goal-name (cdr goal-names)))
(current-immediate-deps goal-name (cdr goal-names)))
nil))
(defun goal-dependent-p (parent name)
;; says whether parent is a proper ancestor of name
(if (consp name)
(if (equal parent (car name))
t
(goal-dependent-p parent (car name)))
nil))
(defun current-all-deps (goal-name goal-names)
;; Returns all names in goal-names that are proper dependents (not necessarily
;; immediate) of goal-name.
(if (consp goal-names)
(if (goal-dependent-p goal-name (car goal-names))
(cons (car goal-names)
(current-immediate-deps goal-name (cdr goal-names)))
(current-immediate-deps goal-name (cdr goal-names)))
nil))
(defun maybe-print-proved-goal-message (goal old-goals goals state)
; Here goal is a goal in the existing pc-state and goals is the goals in the
; new pc-state. old-goals is the goals in the existing pc-state.
; Warning: This function should be called under (io? proof-builder ...).
(let* ((name (access goal goal :goal-name))
(new-names (goal-names goals))
(names (set-difference-equal new-names (goal-names old-goals))))
(pprogn (if names
(fms0 "~|~%Creating ~n0 new ~#1~[~/goal~/goals~]: ~&2.~%"
(list (cons #\0 (length names))
(cons #\1 (zero-one-or-more (length names)))
(cons #\2 names))
0 nil)
state)
(if (find-goal name goals)
state
(let ((unproved-deps (current-all-deps name new-names)))
(if unproved-deps
(fms0 "~|~%The proof of the current goal, ~x0, has been ~
completed. However, the following subgoals remain ~
to be proved:~%~ ~ ~&1.~%Now proving ~x2.~%"
(list (cons #\0 name)
(cons #\1 unproved-deps)
(cons #\2 (access goal (car goals)
:goal-name)))
0 nil)
(if goals
(fms0 "~|~%The proof of the current goal, ~x0, has been ~
completed, as have all of its subgoals.~%Now proving ~x1.~%"
(list (cons #\0 name)
(cons #\1 (access goal (car goals)
:goal-name)))
0 nil)
(pprogn
(fms0 "~|*!*!*!*!*!*!* All goals have been proved! ~
*!*!*!*!*!*!*~%")
(if (f-get-global 'in-verify-flg state)
(fms0 "You may wish to exit.~%")
state)))))))))
(defun accumulate-ttree-in-pc-state (pc-state state)
(er-let* ((ttree (accumulate-ttree-and-step-limit-into-state
(access pc-state pc-state :tag-tree)
:skip
state)))
(value (change-pc-state pc-state :tag-tree ttree))))
(defun pc-process-assumptions (pc-ens ttree wrld state)
; Like process-assumptions, but returns (mv clauses known-assumptions ttree
; state).
(let ((n (count-assumptions ttree)))
(pprogn
(cond
((< n 101)
state)
(t
(io? prove nil state
(n)
(fms "~%Note: processing ~x0 forced hypotheses which we now ~
collect)~%"
(list (cons #\0 n))
(proofs-co state) state nil))))
(mv-let
(n0 assns pairs ttree1)
(extract-and-clausify-assumptions nil ttree nil pc-ens wrld
(splitter-output))
(cond
((= n0 0)
(mv nil nil ttree state))
(t
(mv (strip-cdrs pairs) assns ttree1 state)))))))
(defun make-implication (assumptions concl)
(cond
(assumptions
(fcons-term* (quote implies) (conjoin assumptions) concl))
(t concl)))
(defun cl-set-to-implications (cl-set)
(if (null cl-set)
nil
(cons (make-implication (butlast (car cl-set) 1)
(car (last (car cl-set))))
(cl-set-to-implications (cdr cl-set)))))
(defun known-assumptions (type-alist assns)
; Here assns is a list of cleaned-up assumptions. We want to collect those
; assumptions whose hypotheses are clearly true under the given type-alist.
; There seems to be no point in trying to add the ones that don't have this
; property, since they'd only introduce case splits. In fact, though, probably
; most of the assumptions we encounter will have this property.
(cond
((null assns)
nil)
((dumb-type-alist-implicationp type-alist
(access assumption (car assns) :type-alist))
(cons (access assumption (car assns) :term)
(known-assumptions type-alist (cdr assns))))
(t (known-assumptions type-alist (cdr assns)))))
(defun add-assumptions-to-top-goal
(goal-unproved-p known-assumptions forced-goals remaining-goals)
(if forced-goals
(if goal-unproved-p
(cons (if known-assumptions
(if forced-goals
(change goal (car remaining-goals)
:hyps
(append (access goal (car remaining-goals) :hyps)
known-assumptions)
:depends-on (+ (access goal
(car remaining-goals)
:depends-on)
(length forced-goals)))
(change goal (car remaining-goals)
:hyps
(append (access goal (car remaining-goals) :hyps)
known-assumptions)))
(car remaining-goals))
(append forced-goals (cdr remaining-goals)))
(append forced-goals remaining-goals))
; Otherwise, we assume that since forced-goals is nil, assns is nil.
; This saves us the cons above.
remaining-goals))
(defun unproved-goals (pc-state)
(let ((goals (access pc-state pc-state :goals)))
(if (and goals
(equal (access goal (car goals) :conc)
*t*))
(cdr goals)
goals)))
(defun make-pc-ens (pc-ens state)
(if (null pc-ens)
(ens state)
pc-ens))
(defun initial-rcnst-from-ens (ens wrld state splitter-output)
(make-rcnst ens wrld state
:splitter-output splitter-output
; We need the :force-info to be non-nil for the call of
; resume-suspended-assumption-rewriting in pc-single-step-primitive. We set it
; to t so that forcing is unrestricted. Proof-builder calls to the prover
; won't be hurt by this, because simplify-clause sets :force-info itself.
:force-info t))
(defun make-new-goals-fixed-hyps (termlist hyps goal-name start-index)
;; similar to make-new-goals
(if (consp termlist)
(cons (make goal
:conc (car termlist)
:hyps hyps
:current-addr nil
:goal-name (cons goal-name start-index)
:depends-on 1)
(make-new-goals-fixed-hyps (cdr termlist) hyps goal-name
(1+ start-index)))
nil))
(defun pc-single-step-primitive (instr state)
(state-global-let*
((guard-checking-on nil)) ; see the Essay on Guard Checking
(let* ((goals (goals))
(wrld (w state))
(old-tag-tree (tag-tree)))
(cond
((null goals)
(pprogn (print-all-goals-proved-message state)
(mv nil nil state)))
(t
(mv-let
(erp stobjs-out/vals state)
(trans-eval-default-warning (list (car instr)
(list 'quote (cdr instr))
'state)
'pc-single-step state t)
(let ((vals (cdr stobjs-out/vals)))
; Vals is (x replaced-state), where x is a pc-state or nil.
(cond
(erp
(pprogn (print-no-change
; We used to say "Very odd" here, but it is perfectly natural to get such an
; error if there is an rdepth-error.
"An error occurred in executing ~X01."
(list (cons #\0 instr)
(cons #\1 (abbrev-evisc-tuple state))))
(mv 'pc-single-step-error-primitive nil state)))
(t
(assert$
(equal (car stobjs-out/vals) '(nil state))
(cond
((car vals) ;so, there is a new state
(let ((pc-ens (make-pc-ens (pc-ens) state)))
(mv-let
(step-limit bad-ass ttree)
(resume-suspended-assumption-rewriting
(access pc-state (car vals) :local-tag-tree)
nil ;ancestors
nil ;gstack
nil ;simplify-clause-pot-lst
(initial-rcnst-from-ens pc-ens
wrld
state
(splitter-output))
wrld
state
(initial-step-limit wrld state))
(declare (ignore step-limit))
(cond
(bad-ass
(pprogn
(let ((assumnote
; Is the assumnotes field always non-empty?
(car (access assumption bad-ass :assumnotes))))
(print-no-change
"When applying the rune ~x0 to the target ~x1, a ~
hypothesis of the form (~x2 ...) or (~x3 ...) was ~
later found to be false."
(list (cons #\0 (access assumnote assumnote :rune))
(cons #\1 (access assumnote assumnote :target))
(cons #\2 'force)
(cons #\3 'case-split))))
(mv nil nil state)))
(t
(let* ((returned-pc-state (car vals))
(remaining-goals (unproved-goals returned-pc-state))
(goal-name (goal-name)) ; original goal-name
(goal-unproved-p
(and remaining-goals
(equal goal-name
(access goal (car remaining-goals)
:goal-name))))
(hyps (hyps)) ; original hyps
(returned-goal
(let* ((goals (access pc-state returned-pc-state
:goals)))
(and goals
(equal goal-name
(access goal (car goals) :goal-name))
(car goals))))
(depends-on
(cond (returned-goal (access goal returned-goal
:depends-on))
(t ; goal has disappeared; use old depends-on
(depends-on)))))
(mv-let
(cl-set assns ttree state)
(pc-process-assumptions pc-ens ttree wrld state)
(mv-let
(contradictionp hyps-type-alist ttree0)
(cond ((and assns goal-unproved-p)
(type-alist-clause (dumb-negate-lit-lst hyps)
nil nil nil pc-ens wrld nil
nil))
(t ; else don't bother
(mv nil nil nil)))
(cond
(contradictionp
(er-let*
((new-pc-state
(let ((local-ttree (cons-tag-trees ttree ttree0)))
(accumulate-ttree-in-pc-state
(change-pc-state
(car vals)
:goals
(cdr goals)
:tag-tree
(cons-tag-trees local-ttree old-tag-tree)
:local-tag-tree
local-ttree)
state))))
(pprogn (io? proof-builder nil state
(instr goal-name)
(fms0 "~|AHA! A contradiction has ~
been discovered in the ~
hypotheses of goal ~x0 in the ~
course of executing ~
instruction ~x1, in the ~
process of preparing to deal ~
with forced assumptions.~|"
(list (cons #\0 goal-name)
(cons #\1 instr))
0 nil))
(io? proof-builder nil state
(goals)
(maybe-print-proved-goal-message
(car goals) goals (cdr goals) state))
(pc-assign state-stack
(cons new-pc-state
(state-stack)))
(value new-pc-state))))
(t
(let* ((termlist
(cl-set-to-implications cl-set))
(forced-goals
(make-new-goals-fixed-hyps
termlist hyps goal-name depends-on))
(new-goals
(add-assumptions-to-top-goal
goal-unproved-p
(known-assumptions hyps-type-alist assns)
forced-goals
remaining-goals))
(pc-state-1
(change-pc-state (car vals)
:goals new-goals
:tag-tree
(cons-tag-trees
ttree old-tag-tree)
:local-tag-tree ttree)))
(er-let* ((new-pc-state
(accumulate-ttree-in-pc-state
pc-state-1
state)))
(pprogn
(cond
(forced-goals
(io? proof-builder nil state
(forced-goals)
(fms0
"~|~%NOTE (forcing): Creating ~
~n0 new ~#1~[~/goal~/goals~] ~
due to FORCE or CASE-SPLIT ~
hypotheses of rules.~%"
(list
(cons #\0 (length forced-goals))
(cons #\1
(zero-one-or-more
(length forced-goals)))))))
(t state))
(io? proof-builder nil state
(new-goals goals)
(maybe-print-proved-goal-message