-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof-checker-b.lisp
4839 lines (4457 loc) · 205 KB
/
proof-checker-b.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 6.4 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2014, 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")
(defmacro install-new-pc-meta-or-macro (command-type raw-name name formals doc body)
`(progn ,(pc-meta-or-macro-defun raw-name name formals doc body)
(add-pc-command ,name ',command-type)))
(defun define-pc-meta-or-macro-fn (command-type raw-name formals body)
(let ((name (make-official-pc-command raw-name)) )
(mv-let
(doc body)
(remove-doc
(case command-type
(meta " (meta)")
(macro " (macro)")
(atomic-macro " (atomic macro)")
(otherwise ""))
body)
`(install-new-pc-meta-or-macro ,command-type ,raw-name ,name
,formals ,doc ,body))))
(defmacro define-pc-meta (raw-name formals &rest body)
(define-pc-meta-or-macro-fn 'meta raw-name formals body))
(defmacro define-pc-macro (raw-name formals &rest body)
(define-pc-meta-or-macro-fn 'macro raw-name formals body))
(defmacro define-pc-atomic-macro (raw-name formals &rest body)
(define-pc-meta-or-macro-fn 'atomic-macro raw-name formals body))
(defmacro toggle-pc-macro (name &optional new-tp)
(declare (xargs :guard (and (symbolp new-tp)
(or (null new-tp)
(member-equal (symbol-name new-tp)
'("MACRO" "ATOMIC-MACRO"))))))
`(toggle-pc-macro-fn ',(make-official-pc-command name) ',new-tp state))
(defmacro define-pc-primitive (raw-name formals &rest body)
; Define-pc-primitive defines a new primitive for the proof-checker. That
; primitive is always a function returning (mv pc-state state), where the
; (pc-value state-stack) has not been changed for state.
; Primitive command definitions should never look at the instruction field of
; the current state; see pc-primitive-defun-form.
; We generally rely in pc-single-step-primitive on the following property: a
; primitive leaves the top goal on the top of the :goals stack of the pc-state,
; adjusted as necessary, with its depends-on field reflecting all new subgoals
; added to that stack. However, if the top goal is proved and no forced
; hypotheses are stored in the tag tree (see pc-single-step-primitive), then we
; may drop a proved goal.
(let ((name (make-official-pc-command raw-name)))
(mv-let
(doc body)
(remove-doc " (primitive)" body)
`(progn
,(pc-primitive-defun-form raw-name name formals doc body)
(add-pc-command ,name 'primitive)))))
(define-pc-primitive comment (&rest x)
(declare (ignore x))
(mv pc-state state))
(defun non-bounded-nums (nums lower upper)
(declare (xargs :guard (and (rationalp lower)
(rationalp upper)
(true-listp nums))))
(if (consp nums)
(if (and (integerp (car nums))
(<= lower (car nums))
(<= (car nums) upper))
(non-bounded-nums (cdr nums) lower upper)
(cons (car nums)
(non-bounded-nums (cdr nums) lower upper)))
nil))
(defun delete-by-position (lst current-index nums)
(declare (xargs :guard (and (true-listp nums)
(integerp current-index))))
(if (consp lst)
(if (member current-index nums)
(delete-by-position (cdr lst) (1+ current-index) nums)
(cons (car lst)
(delete-by-position (cdr lst) (1+ current-index) nums)))
nil))
(define-pc-primitive drop (&rest nums)
(if nums
(let ((bad-nums (non-bounded-nums nums 1 (length hyps))))
(if bad-nums
(print-no-change2 "The following are not in-range hypothesis numbers: ~&0."
(list (cons #\0 bad-nums)))
(mv (change-pc-state
pc-state
:goals
(cons (change goal (car goals)
:hyps (delete-by-position hyps 1 nums))
(cdr goals)))
state)))
(if hyps
(mv (change-pc-state
pc-state
:goals
(cons (change goal (car goals)
:hyps nil)
(cdr goals)))
state)
(print-no-change2 "There are no hypotheses to drop!"))))
(define-pc-meta lisp (form)
(cond ((not (f-get-global 'in-verify-flg state))
(er soft 'acl2-pc::lisp
"You may only invoke the proof-checker LISP command when ~
you are inside the interactive loop."))
((and (symbolp form)
(or (eq form t)
(eq form nil)
(keywordp form)))
(value form))
(t
(mv-let (erp stobjs-out/vals state)
(trans-eval form :lisp state t)
(let ((stobjs-out (car stobjs-out/vals))
(vals (cdr stobjs-out/vals)))
(if (equal stobjs-out *error-triple-sig*)
(mv (or erp (car vals)) (cadr vals) state)
(mv erp vals state)))))))
(define-pc-primitive fail-primitive ()
(declare (ignore pc-state))
(mv nil state))
(define-pc-macro fail (&optional hard)
(if hard
(value '(lisp (mv hard nil state)))
(value 'fail-primitive)))
(define-pc-macro illegal (instr)
(pprogn (print-no-change "Illegal interactive instruction, ~x0.~% An instruction must be a ~
symbol or a proper list headed by a symbol."
(list (cons #\0 instr)))
(value :fail)))
(defun chk-assumption-free-ttree-1 (ttree ctx)
;; Same as chk-assumption-free-ttree, but returns a value.
(cond ((tagged-objectsp 'assumption ttree)
(er hard ctx
"The 'assumption ~x0 was found in the final ttree!"
(car (tagged-objects 'assumption ttree))))
((tagged-objectsp 'fc-derivation ttree)
(er hard ctx
"The 'fc-derivation ~x0 was found in the final ttree!"
(car (tagged-objects 'fc-derivation ttree))))
(t t)))
(defun put-cdr-assoc-query-id (id val alist)
(cond ((atom alist) (cons (cons id val) alist))
((eq id (caar alist)) (cons (cons id val) (cdr alist)))
(t (cons (car alist)
(put-cdr-assoc-query-id id val (cdr alist))))))
(defun set-query-val (id val state)
;; If val is 'toggle, then a NIL default is changed to T and every
;; other default is changed to NIL. Otherwise, VAL is the new default.
(let ((alist (ld-query-control-alist state)))
(set-ld-query-control-alist
(put-cdr-assoc-query-id
id
(if (eq val 'toggle)
(not (cdr-assoc-query-id id alist))
val)
alist)
state)))
(defmacro query-on-exit (&optional (val 'toggle))
`(set-query-val 'acl2-pc::exit ',val state))
(defun replay-query (state)
;; Returns a state-stack, T or NIL. A T value means we should replay instructions
;; in order to create the state-stack. A value of NIL means that we should exit
;; without creating the event (by making the state-stack nil).
;; In fact, the only time we return other than the current
;; state-stack is if we're inside verify and
;; either the query flag is off or the response is other than "Y".
(acl2-query 'acl2-pc::exit
'("~%Do you want to submit this event? Possible replies are:~%~
Y (Yes), R (yes and Replay commands), N (No, but exit), A (Abort exiting).~|~ "
:y :y :r :r :n :n :a :a)
nil state))
(define-pc-meta exit (&optional event-name rule-classes do-it-flg)
; We allow (exit .. nil ..) to indicate that information is to be picked up
; from the initial pc-state.
(if (not (f-get-global 'in-verify-flg state))
(er soft 'acl2-pc::exit
"You may not invoke the EXIT command unless inside the ~
interactive loop.")
(if args ; so it's not just a command to exit
(let* ((event-name-and-types-and-raw-term
(event-name-and-types-and-raw-term state-stack))
(event-name
(or event-name
(car event-name-and-types-and-raw-term)))
(instructions (instructions-of-state-stack state-stack nil)))
(er-let* ((event-name
(if event-name
(value event-name)
(pprogn (io? proof-checker nil state
nil
(fms0 "Please supply an event name (or :A to ~
abort)~%>> "))
(state-global-let*
((infixp nil))
(read-object *standard-oi* state))))))
(if (eq event-name :a)
(pprogn (io? proof-checker nil state
nil
(fms0 "~|Exit aborted.~%"))
(mv nil nil state))
(if (null (goals t))
(let* ((rule-classes (if (consp (cdr args))
rule-classes
(if (and (consp args)
(eq (car args) nil))
(cadr event-name-and-types-and-raw-term)
'(:rewrite))))
(event-form `(defthm ,event-name
,(caddr event-name-and-types-and-raw-term)
,@(if (equal rule-classes '(:rewrite))
nil
(list :rule-classes rule-classes))
:instructions ,instructions)))
(mv-let (erp stobjs-out/vals state)
(pprogn
(print-pc-defthm event-form state)
(mv-let (erp ans state)
(cond (do-it-flg (value :y))
((eq event-name t) (value :n))
(t (replay-query state)))
(declare (ignore erp))
(case ans
(:y (trans-eval event-form
'acl2-pc::exit
state
t))
(:r (pprogn (state-from-instructions
(caddr event-form)
event-name
rule-classes
instructions
'(signal value)
state)
(trans-eval event-form
'acl2-pc::exit
state
t)))
(:a (mv t '(nil . t) state))
(otherwise (mv t '(nil . nil) state)))))
; We assume here that if DEFTHM returns without error, then it succeeds.
(if (or erp (null (car stobjs-out/vals)))
(if (eq (cdr stobjs-out/vals) t)
(pprogn (io? proof-checker nil state
nil
(fms0 "~|Exit aborted.~%"))
(mv nil nil state))
(mv *pc-complete-signal* nil state))
(mv *pc-complete-signal* event-name state))))
; Otherwise, we have an incomplete proof.
(pprogn (io? proof-checker nil state
(instructions event-name-and-types-and-raw-term
state-stack)
(fms0 "~%Not exiting, as there remain unproved ~
goals: ~&0.~%The original goal is:~%~ ~ ~ ~
~ ~y1~| Here is the current instruction ~
list, starting with the first:~%~ ~ ~ ~ ~
~y2~|"
(list (cons #\0 (goal-names (goals t)))
(cons #\1 (caddr event-name-and-types-and-raw-term))
(cons #\2 instructions))))
(mv nil nil state))))))
(pprogn (io? proof-checker nil state
nil
(fms0 "~|Exiting....~%"))
(mv *pc-complete-signal* nil state)))))
(define-pc-meta undo (&optional n)
(if (and args
(not (and (integerp n)
(< 0 n))))
(pprogn (print-no-change
"The optional argument to undo must be a positive integer.")
(mv nil nil state))
(let ((m (min (or n 1) (1- (length state-stack)))))
(if (null (cdr state-stack))
(pprogn (print-no-change "Already at the start.")
(mv nil nil state))
(pprogn (pc-assign old-ss state-stack)
(io? proof-checker nil state
(state-stack m)
(fms0 "~|Undoing: ~y0~|"
(list (cons #\0
(access pc-state
(car (nthcdr (1- m) state-stack))
:instruction)))))
(pc-assign state-stack
(nthcdr m state-stack))
(if (consp (cdr (state-stack)))
state
(io? proof-checker nil state
nil
(fms0 "Back to the start.~%")))
(mv nil t state))))))
(define-pc-meta restore ()
(let ((old-ss (pc-value old-ss)))
(if (null old-ss)
(pprogn (io? proof-checker nil state
nil
(fms0 "~%Nothing to restore from!~%"))
(mv nil nil state))
(let ((saved-ss state-stack))
(pprogn (pc-assign state-stack old-ss)
(pc-assign old-ss saved-ss)
(mv nil t state))))))
(defun print-commands (indexed-instrs state)
(if (null indexed-instrs)
state
(if (null (caar indexed-instrs))
(io? proof-checker nil state
(indexed-instrs)
(fms0 (car (cdar indexed-instrs))
(cdr (cdar indexed-instrs))))
(pprogn (io? proof-checker nil state
(indexed-instrs)
(fms0 "~|~x0. ~y1~|"
(list (cons #\0 (caar indexed-instrs))
(cons #\1 (cdar indexed-instrs)))))
(print-commands (cdr indexed-instrs) state)))))
(defun make-pretty-start-instr (state-stack)
(let* ((triple (event-name-and-types-and-raw-term state-stack))
(name (car triple))
(types (cadr triple)))
(if name
(list "~|[started with (~x0 ~x1 ...)]~%"
(cons #\0 name)
(cons #\1 types))
(list "~|<< no event name specified at start >>~%"))))
(defun raw-indexed-instrs (start-index finish-index state-stack)
(declare (xargs :guard (and (integerp start-index)
(integerp finish-index)
(<= start-index finish-index)
(true-listp state-stack)
;; It's tempting to add the following guard, but
;; since state-stack keeps shrinking, it can get violated
;; on recursive calls.
;; (<= finish-index (length state-stack))
)))
(if (< start-index finish-index)
(cons (cons start-index (access pc-state (car state-stack) :instruction))
(raw-indexed-instrs (1+ start-index) finish-index (cdr state-stack)))
(if (cdr state-stack)
(list (cons start-index (access pc-state (car state-stack) :instruction)))
(list (cons nil (make-pretty-start-instr state-stack))))))
(define-pc-macro sequence-no-restore (instr-list)
(value `(sequence ,instr-list nil nil nil nil t)))
(define-pc-macro skip ()
(value '(sequence-no-restore nil)))
(defmacro define-pc-help (name args &rest body)
`(define-pc-macro ,name ,args ,@(butlast body 1)
(pprogn ,(car (last body))
(value 'skip))))
(defun evisc-indexed-instrs-1 (name rev-indexed-instrs)
(if (consp rev-indexed-instrs)
(let ((instr (cdr (car rev-indexed-instrs))))
(case-match instr
((comm ':end x . &)
(if (and (eq comm (make-pretty-pc-command :comment))
(equal x name))
rev-indexed-instrs
(evisc-indexed-instrs-1 name (cdr rev-indexed-instrs))))
(& (evisc-indexed-instrs-1 name (cdr rev-indexed-instrs)))))
nil))
(defun evisc-indexed-instrs-rec (rev-indexed-instrs)
(if (consp rev-indexed-instrs)
(let ((instr (cdr (car rev-indexed-instrs)))
(evisc-cdr (evisc-indexed-instrs-rec (cdr rev-indexed-instrs))))
(case-match instr
((comm ':begin name . &)
(if (eq comm (make-pretty-pc-command :comment))
(let ((rst (evisc-indexed-instrs-1 name evisc-cdr)))
(if rst
(cons (cons (car (car rev-indexed-instrs))
(cons "***HIDING***" instr))
(cdr rst))
(cons (car rev-indexed-instrs)
evisc-cdr)))
(cons (car rev-indexed-instrs)
evisc-cdr)))
(& (cons (car rev-indexed-instrs)
evisc-cdr))))
nil))
(defun mark-unfinished-instrs (indexed-instrs)
;; any "begin" in here was not matched with an "end"
(if (consp indexed-instrs)
(let ((instr (cdr (car indexed-instrs))))
(case-match instr
((comm ':begin & . &)
(if (eq comm (make-pretty-pc-command :comment))
(cons (cons (car (car indexed-instrs))
(cons "***UNFINISHED***" instr))
(mark-unfinished-instrs (cdr indexed-instrs)))
(cons (car indexed-instrs)
(mark-unfinished-instrs (cdr indexed-instrs)))))
(& (cons (car indexed-instrs)
(mark-unfinished-instrs (cdr indexed-instrs))))))
nil))
(defun evisc-indexed-instrs (indexed-instrs)
;; for now, returns a new state stack in which we drop bookends
;; (comment (begin <name>) ...)
;; (comment (end <name>) ...)
(mark-unfinished-instrs (reverse (evisc-indexed-instrs-rec (reverse indexed-instrs)))))
(define-pc-help commands (&optional n evisc-p)
(if (and n (not (and (integerp n) (> n 0))))
(io? proof-checker nil state
(n)
(fms0 "*** The first optional argument to the COMMANDS command must ~
be a positive integer, but ~x0 is not.~|"
(list (cons #\0 n))))
(let* ((indexed-instrs (raw-indexed-instrs 1
(if n
(min n (length state-stack))
(length state-stack))
state-stack)))
(print-commands (if evisc-p (evisc-indexed-instrs indexed-instrs) indexed-instrs)
state))))
(define-pc-macro comm (&optional n)
(value (list 'commands n t)))
(defun promote-guts (pc-state goals hyps x y no-flatten-flg)
(change-pc-state
pc-state
:goals
(cons (change goal (car goals)
:hyps (append hyps
(if no-flatten-flg
(list x)
(flatten-ands-in-lit x)))
:conc y)
(cdr goals))))
(define-pc-primitive promote (&optional do-not-flatten-flag)
(if current-addr
(print-no-change2 "You must be at the top ~
of the goal in order to promote the ~
antecedents of an implication. Try TOP first.")
(case-match conc
(('implies x y)
(mv (promote-guts pc-state goals hyps x y do-not-flatten-flag) state))
(('if x y *t*)
(mv (promote-guts pc-state goals hyps x y do-not-flatten-flag) state))
(& (print-no-change2 "The goal must be of the form ~x0 or ~x1."
(list (cons #\0 '(IMPLIES P Q))
(cons #\1 '(IF P Q T))))))))
(defun remove-by-indices (m indices lst)
;; (declare (xargs :guard (null (non-bounded-nums indices m (length lst)))))
;; this was ok for the original entry, but it's not preserved
(if (consp lst)
(if (member-equal m indices)
(remove-by-indices (1+ m) indices (cdr lst))
(cons (car lst) (remove-by-indices (1+ m) indices (cdr lst))))
nil))
;;; **** Should improve the following so that if form outputs a state or
;;; does return just one result, then fms0 isn't even called but instead
;;; an appropriate error message is printed.
(define-pc-macro print (form &optional without-evisc)
; NOTE: The saved-output mechanism described in the Essay on Saved-output won't
; work here, because there is no call of io?. We can't call io? because form
; is arbitrary and hence we cannot check its variables.
(let ((print-form `(fms0 "~|~y0~|" (list (cons #\0 ,form)))))
(value `(lisp ,(if without-evisc
`(without-evisc ,print-form)
print-form)))))
(defun bounded-integer-listp (i j lst)
;; If i is a non-integer, then it's -infinity.
;; If j is a non-integer, then it's +infinity.
(if (consp lst)
(and (integerp (car lst))
(if (integerp i)
(if (integerp j)
(and (<= i (car lst))
(<= (car lst) j))
(<= i (car lst)))
(<= (car lst) j)))
(null lst)))
(defun fetch-term-and-cl (term addr cl)
;; Returns the subterm of TERM at address ADDR paired with a list
;; containing the tests governing that occurrence of the subterm plus
;; the literals of the input CL. However, if CL is T then we simply
;; return (mv nil t) (see also below).
;; I've assumed that the address is a list of positive integers. If
;; the address is not valid for diving into TERM according to ADDR,
;; then we return (mv nil t). Notice that ADDR is expected to be in
;; the correct order, while CL is in reverse order and the extension
;; of CL returned in the second position is also in reverse order.
;; For the funny contrapositive subcase of IMPLIES, note that
;; (implies (implies (and u (not x)) (equal y1 y2))
;; (implies u (equal (implies y1 x) (implies y2 x))))
;; is a tautology. However, the corresponding fact does not hold in
;; general for IF; it depends on x being boolean.
(declare (xargs :guard (bounded-integer-listp 1 'infinity addr)))
(cond ((eq cl t)
(mv nil t))
((null addr)
(mv term cl))
((or (variablep term) (fquotep term))
;; can't dive any further
(mv nil t))
((and (integerp (car addr))
(< 0 (car addr))
(< (car addr) (length term)))
(case-match term
(('if t1 t2 t3)
(cond ((= 1 (car addr))
(fetch-term-and-cl t1 (cdr addr) cl))
((= 2 (car addr))
(fetch-term-and-cl t2 (cdr addr) (cons t1 cl)))
(t (fetch-term-and-cl t3 (cdr addr) (cons (dumb-negate-lit t1) cl)))))
(('implies t1 t2)
(cond ((= 1 (car addr))
(fetch-term-and-cl t1 (cdr addr) (cons (dumb-negate-lit t2) cl)))
(t
(fetch-term-and-cl t2 (cdr addr) (cons t1 cl)))))
(& (fetch-term-and-cl (nth (1- (car addr)) (fargs term)) (cdr addr) cl))))
(t
(mv nil t))))
(defun fetch-term (term addr)
;; causes hard error when appropriate
(mv-let (term cl)
(fetch-term-and-cl term addr nil)
(if (eq cl t)
(er hard 'fetch-term
"FETCH-TERM-AND-CL did not find a subterm of ~x0 at address ~x1."
term addr)
term)))
(defun governors (term addr)
(mv-let (term cl)
(fetch-term-and-cl term addr nil)
(declare (ignore term))
;; note that cl could be T rather than a list of governors
cl))
;;;;;;!!!!!!! I should generalize the following to arbitrary equivalence stuff.
(defun term-id-iff (term address iff-flg)
;; The property we want is that if one substitutes an equivalent subterm
;; of TERM at the given address (equivalent modulo the flag returned by
;; this function, that is), then the resulting term is equivalent modulo
;; the IFF-FLG argument to the original TERM. We assume that address is
;; a valid address for term. (*** This should really be a guard.)
(if (null address)
iff-flg
;; so, the term is a function application
(term-id-iff (nth (car address) term)
(cdr address)
(cond ((eq (ffn-symb term) (quote if))
(if (= (car address) 1)
t
iff-flg))
((member-eq (ffn-symb term) (quote (implies iff not)))
t)
(t
nil)))))
;; The way abbreviations will work is as follows. For input, an
;; abbreviation variable is to be thought of as a placeholder for
;; literal substitution (*before* translation!). It was tempting to
;; think of abbreviation variables as standing for something else only
;; when they're in variable position, but the problem with that
;; approach is that we can't tell about the position until we've done
;; the translation (consider macro calls that look at the first
;; character, say, for example). On a pragmatic (implementation)
;; level, it's hard to see how to implement a translator that
;; substitutes for abbreviation variables only when they're in
;; variable position, except by modifying translate. On the other
;; hand, for untranslation the specification is only that
;; (trans (untrans x)) = x, where here translation is with respect
;; to abbreviations. Notice though that this spec messes things
;; up, because if x is (quote &v) then untrans of that is still
;; (quote &v) but then trans would remove the &v, if we use sublis
;; to deal with abbreviations.
;; So, I think I'll implement abbreviations as follows. There will
;; be a new "macro":
;; (defmacro ? (x)
;; (cdr (assoc-eq x (abbreviations))))
;; Notice however that (abbreviations) generates a reference to
;; state, which isn't compatible with ? being a macro. So, I'll
;; stub it out:
(defmacro ? (x)
`(?-fn ',x))
(defstub ?-fn (x)
t)
;; Now, translation will be followed by an appropriate substitution.
;; For convenience, abbreviations will be turned into an alist whose
;; pairs are of the form ((&-fn 'var) . term).
(defun abbreviations-alist (abbreviations)
(if (consp abbreviations)
(cons (cons (fcons-term* '?-fn (kwote (caar abbreviations)))
(cdar abbreviations))
(abbreviations-alist (cdr abbreviations)))
nil))
(mutual-recursion
(defun chk-?s (term ctx state)
;; There shouldn't be any ?-fns in term.
(cond
((or (variablep term) (fquotep term))
(value nil))
((eq (ffn-symb term) '?-fn)
(case-match term
((& ('quote var))
(if (variablep var)
(er soft ctx "The variable ~x0 is not among the current abbreviations."
var)
(er soft ctx "Expected a variable in place of ~x0."
var)))
(& (value (er hard ctx "Bad call of ?-FN, ~x0. ?-FN must be called on the quotation of ~
a variable."
term)))))
((flambdap (ffn-symb term))
(er-progn (chk-?s (lambda-body (ffn-symb term)) ctx state)
(chk-?s-lst (fargs term) ctx state)))
(t (chk-?s-lst (fargs term) ctx state))))
(defun chk-?s-lst (term-lst ctx state)
(if (consp term-lst)
(er-progn (chk-?s (car term-lst) ctx state)
(chk-?s-lst (cdr term-lst) ctx state))
(value nil)))
)
(defun remove-?s (term abbreviations-alist ctx state)
(let ((newterm (sublis-expr abbreviations-alist term)))
(er-progn (chk-?s newterm ctx state)
(value newterm))))
(defun translate-abb (x abbreviations ctx state)
(mv-let
(erp term state)
(translate x t
; Since we only use this function in a logical context, we set
; logic-modep to t.
t t ctx (w state) state)
(if erp
(mv erp term state)
(remove-?s term (abbreviations-alist abbreviations) ctx state))))
(defmacro trans0 (x &optional abbreviations ctx)
`(translate-abb ,x ,abbreviations ,(or ctx ''trans0) state))
(defun p-body (conc current-addr abbreviations state)
(io? proof-checker nil state
(abbreviations current-addr conc)
(fms0 "~|~y0~|"
(list (cons #\0 (untrans0 (fetch-term conc current-addr)
(term-id-iff conc current-addr t)
abbreviations))))))
(define-pc-help p ()
(when-goals
(p-body (conc t) (current-addr t) (abbreviations t) state)))
(define-pc-help pp ()
(when-goals
(io? proof-checker nil state
(state-stack)
(fms0 "~|~y0~|"
(list (cons #\0 (fetch-term (conc t) (current-addr t))))))))
(defun take-by-indices (m indices lst)
;; (declare (xargs :guard (null (non-bounded-nums indices m (length lst)))))
;; this was ok for the original entry, but it's not preserved
(if (consp lst)
(if (member-equal m indices)
(cons (car lst) (take-by-indices (1+ m) indices (cdr lst)))
(take-by-indices (1+ m) indices (cdr lst)))
nil))
(defun print-hyps (indexed-hyps ndigits abbreviations state)
(declare (xargs :guard (and (eqlable-alistp indexed-hyps)
(integerp ndigits)
(> ndigits 0))))
(if (null indexed-hyps)
state
(pprogn (io? proof-checker nil state
(abbreviations ndigits indexed-hyps)
(fms0 "~c0. ~y1~|"
(list (cons #\0 (cons (caar indexed-hyps) ndigits))
(cons #\1 (untrans0 (cdar indexed-hyps) t abbreviations)))))
(print-hyps (cdr indexed-hyps) ndigits abbreviations state))))
(defun some-> (lst n)
;; says whether some element of lst exceeds n
(declare (xargs :guard (and (rational-listp lst)
(rationalp n))))
(if lst
(or (> (car lst) n)
(some-> (cdr lst) n))
nil))
(defun print-hyps-top (indexed-hyps abbreviations state)
(declare (xargs :guard (eqlable-alistp indexed-hyps)))
(if (null indexed-hyps)
(io? proof-checker nil state
nil
(fms0 "~|There are no top-level hypotheses.~|"))
(print-hyps indexed-hyps (if (some-> (strip-cars indexed-hyps) 9) 2 1)
abbreviations state)))
(defun print-governors-top (indexed-hyps abbreviations state)
(declare (xargs :guard (eqlable-alistp indexed-hyps)))
(if (null indexed-hyps)
(io? proof-checker nil state
nil
(fms0 "~|There are no governors.~|"))
(print-hyps indexed-hyps (if (some-> (strip-cars indexed-hyps) 9) 2 1)
abbreviations state)))
(defun pair-indices (seed indices lst)
;; Returns a list of indices paired with the corresponding (1-based) element of
;; lst when in range. Seed is a starting integer; we do things this way
;; because we want the result sorted (and hence want to recurse on lst).
(declare (xargs :guard (and (integerp seed)
(true-listp lst)
(bounded-integer-listp 1 (length lst) indices))))
(if lst
(let ((rest-lst
(pair-indices (1+ seed) indices (cdr lst))))
(if (member seed indices)
(cons (cons seed (car lst))
rest-lst)
rest-lst))
nil))
(define-pc-macro hyps (&optional hyps-indices govs-indices)
(when-goals-trip
(let* ((hyps (hyps t))
(len-hyps (length hyps))
(govs (and govs-indices;; for efficiency
(governors (conc t) (current-addr t))))
(len-govs (length govs))
(abbs (abbreviations t))
(hyps-indices (or hyps-indices
(null args))))
(cond
((not (or (eq hyps-indices t) (bounded-integer-listp 1 len-hyps hyps-indices)))
(pprogn
(io? proof-checker nil state
(len-hyps hyps-indices)
(fms0 "~|Bad hypothesis-list argument to HYPS, ~X0n. The ~
hypothesis-list argument should either be T or should be a ~
list of integers between 1 and the number of top-level ~
hypotheses, ~x1.~%"
(list (cons #\0 hyps-indices)
(cons #\n nil)
(cons #\1 len-hyps))))
(value :fail)))
((not (or (eq govs-indices t) (bounded-integer-listp 1 len-govs govs-indices)))
(pprogn
(io? proof-checker nil state
(len-govs govs-indices)
(fms0 "~|Bad governors-list argument to HYPS,~% ~X0n.~%The ~
governors-list argument should either be T or should be a ~
list of integers between 1 and the number of top-level ~
governors, ~x1."
(list (cons #\0 govs-indices)
(cons #\n nil)
(cons #\1 len-govs))))
(value :fail)))
((and (null hyps-indices) (null govs-indices))
(pprogn
(io? proof-checker nil state
nil
(fms0 "~|You have specified no printing of either hypotheses or ~
governors! Perhaps you should read the documentation for ~
the HYPS command.~|"))
(value :fail)))
(t
(let ((hyps-to-print
(if (eq hyps-indices t)
(count-off 1 hyps)
(pair-indices 1 hyps-indices hyps)))
(govs-to-print
(if (eq govs-indices t)
(count-off 1 govs)
(pair-indices 1 govs-indices govs))))
(pprogn
(if hyps-indices
(pprogn
(if (eq hyps-indices t)
(io? proof-checker nil state
nil
(fms0 "~|*** Top-level hypotheses:~|"))
(io? proof-checker nil state
nil
(fms0 "~|*** Specified top-level hypotheses:~|")))
(print-hyps-top hyps-to-print abbs state))
state)
(if govs-indices
(pprogn
(if (eq govs-indices t)
(io? proof-checker nil state
nil
(fms0 "~|~%*** Governors:~|"))
(io? proof-checker nil state
nil
(fms0 "~|~%*** Specified governors:~|")))
(print-governors-top govs-to-print abbs state))
state)
(value 'skip))))))))
(define-pc-primitive demote (&rest rest-args)
(cond
(current-addr
(print-no-change2 "You must be at the top of the conclusion in order to ~
demote hypotheses. Try TOP first."))
((null hyps)
(print-no-change2 "There are no top-level hypotheses."))
(t
(let ((badindices (non-bounded-nums rest-args 1 (length hyps))))
(if badindices
(print-no-change2 "The arguments to DEMOTE ~
must be indices of active top-level hypotheses, ~
but the following are not: ~&0."
(list (cons #\0 badindices)))
(mv (change-pc-state
pc-state
:goals
(cons (change goal (car goals)
:hyps (if rest-args
(remove-by-indices 1 rest-args hyps)
nil)
:conc (make-implication
(if rest-args
(take-by-indices 1 rest-args hyps)
hyps)
conc))
(cdr goals)))
state))))))
(defun pair-keywords (keywords lst)
(declare (xargs :guard (and (all-keywords-p keywords)
(keyword-value-listp lst))))
;; returns (mv alist rst)
(if (consp keywords)
(mv-let (alist rst)
(pair-keywords (cdr keywords) lst)
(let ((tail (assoc-keyword (car keywords) rst)))
(if tail
(mv (cons (cons (car tail) (cadr tail)) alist)
;; could use a remove1 version of the following, but who cares?
(remove-keyword (car keywords) rst))
(mv alist rst))))
(mv nil lst)))
(defun null-pool (pool)
(cond
((null pool) t)
((eq (access pool-element (car pool) :tag) 'being-proved-by-induction)
(null-pool (cdr pool)))
(t nil)))
(defun initial-pspv (term displayed-goal otf-flg ens wrld splitter-output)
(change prove-spec-var *empty-prove-spec-var*
:rewrite-constant
(initial-rcnst-from-ens ens wrld splitter-output)
:user-supplied-term term
:displayed-goal displayed-goal
:otf-flg otf-flg))
(defun pc-prove (term displayed-goal hints otf-flg ens wrld ctx state)
; This is exactly the same as the ACL2 PROVE function, except that we allow
; :bye objects in the tag-tree, there is no checking of the load mode, and the
; warning above.
(prog2$
(initialize-brr-stack state)
(er-let* ((ttree
(let ((pspv (initial-pspv term displayed-goal otf-flg ens wrld
(splitter-output)))
(clauses (list (list term))))
(if (f-get-global 'in-verify-flg state) ;interactive
(state-global-let*
((saved-output-p t)
(saved-output-token-lst :all))
(pprogn (f-put-global 'saved-output-reversed nil state)
(prove-loop clauses pspv hints ens wrld ctx state)))
(prove-loop clauses pspv hints ens wrld ctx state)))))
(er-progn
(chk-assumption-free-ttree ttree ctx state)
(value ttree)))))
(defun sublis-equal (alist tree)
(declare (xargs :guard (alistp alist)))
(let ((pair (assoc-equal tree alist)))
(if pair
(cdr pair)
(if (atom tree)
tree
(cons (sublis-equal alist (car tree))
(sublis-equal alist (cdr tree)))))))
(defun abbreviations-alist-? (abbreviations)
;; Same as abbreviations-alist, except that we assume that we
;; haven't translated yet, and hence we use ? instead of ?-fn
;; and we don't quote the variable.
(if (consp abbreviations)
(cons (cons (fcons-term* '? (caar abbreviations))
(cdar abbreviations))
(abbreviations-alist-? (cdr abbreviations)))
nil))
(defun find-?-fn (x)
;; x is not necessarily a term. Heuristically though it's useful
;; to be able to find all (?-fn var) subexpressions of x.
(if (atom x)
nil
(if (eq (car x) '?-fn)
(list (cadr x))
(union-equal (find-?-fn (car x))
(find-?-fn (cdr x))))))
(defun unproved-pc-prove-clauses (ttree)
(reverse-strip-cdrs (tagged-objects :bye ttree) nil))
(defun prover-call (comm term-to-prove rest-args pc-state state)
;; We assume that the :otf-flg and :hints "hints" are locally inside
;; a variable called rest-args, which in fact are the arguments to the
;; instruction being processed.
;; Returns an error triple (mv erp-flg ttree state).
(declare (xargs :guard (keywordp comm)))