File: conditional-eqsubst.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (448 lines) | stat: -rw-r--r-- 20,811 bytes parent folder | download | duplicates (8)
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
; Milawa - A Reflective Theorem Prover
; Copyright (C) 2005-2009 Kookamara LLC
;
; Contact:
;
;   Kookamara LLC
;   11410 Windermere Meadows
;   Austin, TX 78759, USA
;   http://www.kookamara.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: Jared Davis <jared@kookamara.com>

(in-package "MILAWA")
(include-book "colors")
(include-book "skeletonp")
(set-verify-guards-eagerness 2)
(set-case-split-limitations nil)
(set-well-founded-relation ord<)
(set-measure-function rank)



;; Conditional Equal Substitution Tactic
;;
;; This tactic allows you to use a conditional equality to split a goal into
;; three goals.  Given an alleged equality of the form:
;;
;;    hypterm -> (equal lhs rhs)
;;
;; We split the current goal, L1 v ... v Ln, into three new subgoals:
;;
;;    (1) (not hypterm) v (equal lhs rhs)      "correctness of substitution"
;;    (2) hypterm v L1 v ... v Ln              "applicability of substitution"
;;    (3) L1/[lhs<-rhs] v ... v Ln[lhs<-rhs]   "post substitution goal"
;;
;; Why is this a sound thing to do?
;;
;;   a. From the proof of (1), we can prove the following for each Li (by the
;;      disjoined replace subterm builder):
;;
;;          (not hypterm) v Li = Li/[lhs<-rhs]
;;
;;   b. From the proof of (3), we can prove (using simple expansion):
;;
;;          (not hypterm) v L1/[lhs<-rhs] v ... v Ln/[lhs<-rhs]
;;
;;   c. From a and b, we can prove (by the disjoined update clause builder):
;;
;;          (not hypterm) v L1 v ... v Ln
;;
;;   d. From c and the proof of (2), we can prove (using cut and contraction):
;;
;;          L1 v ... v Ln
;;
;; Which was our original goal, and hence what we needed to prove.  Together
;; with generalization, this tactic allows us to implement destructor
;; elimination.  For example, suppose we want to prove:
;;
;;   (implies (and (consp x)
;;                 (foo (car x))
;;                 (bar (cdr x)))
;;            (baz x y))
;;
;; Then we will invoke the equal substitution tactic using:
;;
;;    hypterm = (consp x)
;;    lhs     = x
;;    rhs     = (cons (car x) (cdr x))
;;
;; This generates three new goals.  First, to defend the correctness of our
;; substitution, we need to show:
;;
;;    (1) (implies (consp x) (equal x (cons (car x) (cdr x))))
;;
;; This ought to be trivial using the axioms about conses.  Next, we need to
;; show that the substitution is applicable to the current goal.  This is the
;; following implication:
;;
;;    (2) (implies (and (not (consp x))
;;                      (consp x)
;;                      (foo (car x))
;;                      (bar (cdr x)))
;;                 (baz x y))
;;
;; Obviously that's easy to prove since we now have contradictory hyps.  But
;; if (consp x) wasn't one of our assumptions to begin with, we might have had
;; to do some work to prove it.  Finally, we have the more interesting subgoal:
;;
;;    (3) (implies (and (consp (cons (car x) (cdr x)))
;;                      (foo (car (cons (car x) (cdr x))))
;;                      (bar (cdr (cons (car x) (cdr x)))))
;;                 (baz (cons (car x) (cdr x)) y))
;;
;; At this point, (car x) and (cdr x) would need to be generalized, giving us:
;;
;;   (3') (implies (and (consp (cons x1 x2))
;;                      (foo (car (cons x1 x2)))
;;                      (bar (cdr (cons x1 x2))))
;;                 (baz (cons x1 x2) y))
;;
;; And now simple unconditional rewriting should immediately yield:
;;
;;  (3'') (implies (and (foo x1)
;;                      (bar x2))
;;                 (baz (cons x1 x2) y))
;;
;; Which is what I'd expect from car-cdr-elim.

(defund tactic.conditional-eqsubst-okp (x)
  (declare (xargs :guard (tactic.skeletonp x)))
  (let ((goals   (tactic.skeleton->goals x))
        (tacname (tactic.skeleton->tacname x))
        (extras  (tactic.skeleton->extras x))
        (history (tactic.skeleton->history x)))
    (and (equal tacname 'conditional-eqsubst)
         (tuplep 3 extras)
         (let ((hypterm    (first extras))
               (lhs        (second extras))
               (rhs        (third extras))
               (prev-goals (tactic.skeleton->goals history)))
           (and (logic.termp hypterm)
                (logic.termp lhs)
                (logic.termp rhs)
                (consp prev-goals)
                (let ((new-goal1     (first goals))
                      (new-goal2     (second goals))
                      (new-goal3     (third goals))
                      (other-goals   (cdr (cdr (cdr goals))))
                      (original-goal (car prev-goals)))
                  (and (equal new-goal1 (list (logic.function 'not (list hypterm))
                                              (logic.function 'equal (list lhs rhs))))
                       (equal new-goal2 (cons hypterm original-goal))
                       (equal new-goal3 (logic.replace-subterm-list original-goal lhs rhs))
                       (equal other-goals (cdr prev-goals)))))))))

(defund tactic.conditional-eqsubst-env-okp (x atbl)
  (declare (xargs :guard (and (tactic.skeletonp x)
                              (tactic.conditional-eqsubst-okp x)
                              (logic.arity-tablep atbl))
                  :guard-hints (("Goal" :in-theory (enable tactic.conditional-eqsubst-okp)))))
  (let* ((extras  (tactic.skeleton->extras x))
         (hypterm (first extras))
         (lhs     (second extras))
         (rhs     (third extras)))
    (and (logic.term-atblp hypterm atbl)
         (logic.term-atblp lhs atbl)
         (logic.term-atblp rhs atbl))))

(defthm booleanp-of-tactic.conditional-eqsubst-okp
  (equal (booleanp (tactic.conditional-eqsubst-okp x))
         t)
  :hints(("Goal" :in-theory (e/d (tactic.conditional-eqsubst-okp)
                                 ((:executable-counterpart acl2::force))))))

(defthm booleanp-of-tactic.conditional-eqsubst-env-okp
  (equal (booleanp (tactic.conditional-eqsubst-env-okp x atbl))
         t)
  :hints(("Goal" :in-theory (e/d (tactic.conditional-eqsubst-env-okp)
                                 ((:executable-counterpart acl2::force))))))



(defund tactic.conditional-eqsubst-tac (x hypterm lhs rhs)
  ;; Replace occurrences of expr with var, a new variable
  (declare (xargs :guard (and (tactic.skeletonp x)
                              (logic.termp hypterm)
                              (logic.termp lhs)
                              (logic.termp rhs))))
  (let ((goals (tactic.skeleton->goals x)))
    (if (not (consp goals))
        (ACL2::cw "~s0Conditional-eqsubst-tac failure~s1: all clauses have already been proven.~%" *red* *black*)
      (let* ((original-goal (car goals))
             (new-goal1     (list (logic.function 'not (list hypterm))
                                  (logic.function 'equal (list lhs rhs))))
             (new-goal2     (cons hypterm original-goal))
             (new-goal3     (logic.replace-subterm-list original-goal lhs rhs)))
        (tactic.extend-skeleton (cons new-goal1 (cons new-goal2 (cons new-goal3 (cdr goals))))
                                'conditional-eqsubst
                                (list hypterm lhs rhs)
                                x)))))

(defthm forcing-tactic.skeletonp-of-tactic.conditional-eqsubst-tac
  (implies (and (tactic.conditional-eqsubst-tac x hypterm lhs rhs)
                (force (logic.termp hypterm))
                (force (logic.termp lhs))
                (force (logic.termp rhs))
                (force (tactic.skeletonp x)))
           (equal (tactic.skeletonp (tactic.conditional-eqsubst-tac x hypterm lhs rhs))
                  t))
  :hints(("Goal" :in-theory (enable tactic.conditional-eqsubst-tac))))

(defthm forcing-tactic.conditional-eqsubst-okp-of-tactic.conditional-eqsubst-tac
  (implies (and (tactic.conditional-eqsubst-tac x hypterm lhs rhs)
                (force (logic.termp hypterm))
                (force (logic.termp lhs))
                (force (logic.termp rhs))
                (force (tactic.skeletonp x)))
           (equal (tactic.conditional-eqsubst-okp (tactic.conditional-eqsubst-tac x hypterm lhs rhs))
                  t))
  :hints(("Goal" :in-theory (enable tactic.conditional-eqsubst-tac
                                    tactic.conditional-eqsubst-okp))))

(defthm forcing-tactic.conditional-eqsubst-env-okp-of-tactic.conditional-eqsubst-tac
  (implies (and (tactic.conditional-eqsubst-tac x hypterm lhs rhs)
                (force (logic.term-atblp hypterm atbl))
                (force (logic.term-atblp lhs atbl))
                (force (logic.term-atblp rhs atbl))
                (force (tactic.skeletonp x)))
           (equal (tactic.conditional-eqsubst-env-okp (tactic.conditional-eqsubst-tac x hypterm lhs rhs) atbl)
                  t))
  :hints(("Goal" :in-theory (enable tactic.conditional-eqsubst-tac
                                    tactic.conditional-eqsubst-env-okp))))





;; Why is this a sound thing to do?
;;
;;   a. From the proof of (1), we can prove the following for each Li (by the
;;      disjoined replace subterm builder):
;;
;;          (not hypterm) v Li = Li/[lhs<-rhs]
;;
;;   b. From the proof of (3), we can prove (using simple expansion):
;;
;;          (not hypterm) v L1/[lhs<-rhs] v ... v Ln/[lhs<-rhs]
;;
;;   c. From a and b, we can prove (by the disjoined update clause builder):
;;
;;          (not hypterm) v L1 v ... v Ln
;;
;;   d. From c and the proof of (2), we can prove (using cut and contraction):
;;
;;          L1 v ... v Ln

(defderiv tactic.conditional-eqsubst-lemma1
  :derive (v (= (? hyp) nil) (= (? a) (? b)))
  :from   ((proof x (v (!= (not (? hyp)) nil) (!= (equal (? a) (? b)) nil))))
  :proof  (@derive
           ((v (!= (not (? hyp)) nil) (!= (equal (? a) (? b)) nil))   (@given x))
           ((v (!= (not (? hyp)) nil) (= (equal (? a) (? b)) t))      (build.disjoined-equal-t-from-not-nil @-))
           ((v (!= (not (? hyp)) nil) (= (? a) (? b)))                (build.disjoined-pequal-from-equal @-))
           ((v (= (? a) (? b)) (!= (not (? hyp)) nil))                (build.commute-or @-))
           ((v (= (? a) (? b)) (= (? hyp) nil))                       (build.disjoined-pequal-nil-from-negative-lit @-))
           ((v (= (? hyp) nil) (= (? a) (? b)))                       (build.commute-or @-))))

(defund tactic.conditional-eqsubst-bldr (p orig-goal proof1 proof2 proof3 lhs rhs)
  (declare (xargs :guard (and (logic.formulap p)
                              (logic.term-listp orig-goal)
                              (consp orig-goal)
                              (logic.appealp proof1)
                              (logic.appealp proof2)
                              (logic.appealp proof3)
                              (logic.termp lhs)
                              (logic.termp rhs)
                              (equal (logic.conclusion proof1) (logic.por p (logic.pequal lhs rhs)))
                              (equal (logic.conclusion proof2) (logic.por (logic.pnot p) (clause.clause-formula orig-goal)))
                              (equal (logic.conclusion proof3) (clause.clause-formula (logic.replace-subterm-list orig-goal lhs rhs))))))


  (let* (;; P v L1 = L1/[lhs<-rhs]; ...; P v Ln = Ln/[lhs<-rhs]
         (line-1 (build.disjoined-replace-subterm-list orig-goal lhs rhs proof1))
         ;; P v L1/[lhs<-rhs] v ... v Ln/[lhs<-rhs]
         (line-2 (build.expansion P proof3))
         ;; P v L1 v ... v Ln
         (line-3 (clause.disjoined-update-clause-bldr (logic.replace-subterm-list orig-goal lhs rhs) line-2 line-1))
         ;; (L1 v ... v Ln) v (L1 v ... v Ln)
         (line-4 (build.cut line-3 proof2))
         ;; (L1 v ... v Ln)
         (line-5 (build.contraction line-4)))
    line-5))

(defobligations tactic.conditional-eqsubst-bldr
  (build.disjoined-replace-subterm-list
   build.expansion
   clause.disjoined-update-clause-bldr
   build.cut
   build.contraction))

(encapsulate
 ()
 (local (in-theory (enable tactic.conditional-eqsubst-bldr)))

 (defthm tactic.conditional-eqsubst-bldr-under-iff
   (iff (tactic.conditional-eqsubst-bldr p orig-goal proof1 proof2 proof3 lhs rhs)
        t))

 (defthm forcing-logic.appealp-of-tactic.conditional-eqsubst-bldr
   (implies (force (and (logic.formulap p)
                        (logic.term-listp orig-goal)
                        (consp orig-goal)
                        (logic.appealp proof1)
                        (logic.appealp proof2)
                        (logic.appealp proof3)
                        (logic.termp lhs)
                        (logic.termp rhs)
                        (equal (logic.conclusion proof1) (logic.por p (logic.pequal lhs rhs)))
                        (equal (logic.conclusion proof2) (logic.por (logic.pnot p) (clause.clause-formula orig-goal)))
                        (equal (logic.conclusion proof3) (clause.clause-formula (logic.replace-subterm-list orig-goal lhs rhs)))))
            (equal (logic.appealp (tactic.conditional-eqsubst-bldr p orig-goal proof1 proof2 proof3 lhs rhs))
                   t)))

 (defthm forcing-logic.conclusion-of-tactic.conditional-eqsubst-bldr
   (implies (force (and (logic.formulap p)
                        (logic.term-listp orig-goal)
                        (consp orig-goal)
                        (logic.appealp proof1)
                        (logic.appealp proof2)
                        (logic.appealp proof3)
                        (logic.termp lhs)
                        (logic.termp rhs)
                        (equal (logic.conclusion proof1) (logic.por p (logic.pequal lhs rhs)))
                        (equal (logic.conclusion proof2) (logic.por (logic.pnot p) (clause.clause-formula orig-goal)))
                        (equal (logic.conclusion proof3) (clause.clause-formula (logic.replace-subterm-list orig-goal lhs rhs)))))
            (equal (logic.conclusion (tactic.conditional-eqsubst-bldr p orig-goal proof1 proof2 proof3 lhs rhs))
                   (clause.clause-formula orig-goal)))
   :rule-classes ((:rewrite :backchain-limit-lst 0)))

 (defthm@ forcing-logic.proofp-of-tactic.conditional-eqsubst-bldr
   (implies (force (and (logic.formulap p)
                        (logic.term-listp orig-goal)
                        (consp orig-goal)
                        (logic.appealp proof1)
                        (logic.appealp proof2)
                        (logic.appealp proof3)
                        (logic.termp lhs)
                        (logic.termp rhs)
                        (equal (logic.conclusion proof1) (logic.por p (logic.pequal lhs rhs)))
                        (equal (logic.conclusion proof2) (logic.por (logic.pnot p) (clause.clause-formula orig-goal)))
                        (equal (logic.conclusion proof3) (clause.clause-formula (logic.replace-subterm-list orig-goal lhs rhs)))
                        ;; ---
                        (logic.term-list-atblp orig-goal atbl)
                        (logic.proofp proof1 axioms thms atbl)
                        (logic.proofp proof2 axioms thms atbl)
                        (logic.proofp proof3 axioms thms atbl)
                        (@obligations tactic.conditional-eqsubst-bldr)))
            (equal (logic.proofp (tactic.conditional-eqsubst-bldr p orig-goal proof1 proof2 proof3 lhs rhs) axioms thms atbl)
                   t))))



(defund tactic.conditional-eqsubst-compile (x proofs)
  (declare (xargs :guard (and (tactic.skeletonp x)
                              (tactic.conditional-eqsubst-okp x)
                              (logic.appeal-listp proofs)
                              (equal (clause.clause-list-formulas (tactic.skeleton->goals x))
                                     (logic.strip-conclusions proofs)))
                  :verify-guards nil))
  (let* ((extras       (tactic.skeleton->extras x))
         (history      (tactic.skeleton->history x))
         (orig-goal    (car (tactic.skeleton->goals history)))
         (hypterm      (first extras))
         (lhs          (second extras))
         (rhs          (third extras)))
    (cons (tactic.conditional-eqsubst-bldr (logic.pequal hypterm ''nil)
                                           orig-goal
                                           (tactic.conditional-eqsubst-lemma1 (first proofs))
                                           (second proofs)
                                           (third proofs)
                                           lhs rhs)
          (cdr (cdr (cdr proofs))))))

(defobligations tactic.conditional-eqsubst-compile
  (tactic.conditional-eqsubst-lemma1
   tactic.conditional-eqsubst-bldr))

(encapsulate
 ()
 (local (in-theory (enable tactic.conditional-eqsubst-okp
                           tactic.conditional-eqsubst-env-okp
                           tactic.conditional-eqsubst-compile
                           logic.term-formula)))

 (local (defthm crock
          (implies (equal (clause.clause-list-formulas goals) (logic.strip-conclusions proofs))
                   (equal (logic.conclusion (first proofs))
                          (clause.clause-formula (first goals))))))

 (local (defthm crock2
          (implies (equal (clause.clause-list-formulas goals) (logic.strip-conclusions proofs))
                   (equal (logic.conclusion (second proofs))
                          (clause.clause-formula (second goals))))))

 (local (defthm crock3
          (implies (equal (clause.clause-list-formulas goals) (logic.strip-conclusions proofs))
                   (equal (logic.conclusion (third proofs))
                          (clause.clause-formula (third goals))))))

 (local (defthm len-1-when-not-cdr
          (implies (not (cdr x))
                   (equal (equal (len x) 1)
                          (consp x)))
          :rule-classes ((:rewrite :backchain-limit-lst 0))))

 (verify-guards tactic.conditional-eqsubst-compile)

 (defthm forcing-logic.appeal-listp-of-tactic.conditional-eqsubst-compile
   (implies (force (and (tactic.skeletonp x)
                        (tactic.conditional-eqsubst-okp x)
                        (logic.appeal-listp proofs)
                        (equal (clause.clause-list-formulas (tactic.skeleton->goals x))
                               (logic.strip-conclusions proofs))))
            (equal (logic.appeal-listp (tactic.conditional-eqsubst-compile x proofs))
                   t)))

 (defthm forcing-logic.strip-conclusions-of-tactic.conditional-eqsubst-compile
   (implies (force (and (tactic.skeletonp x)
                        (tactic.conditional-eqsubst-okp x)
                        (logic.appeal-listp proofs)
                        (equal (clause.clause-list-formulas (tactic.skeleton->goals x))
                               (logic.strip-conclusions proofs))))
            (equal (logic.strip-conclusions (tactic.conditional-eqsubst-compile x proofs))
                   (clause.clause-list-formulas (tactic.skeleton->goals (tactic.skeleton->history x))))))

 (defthm@ forcing-logic.proof-listp-of-tactic.conditional-eqsubst-compile
   (implies (force (and (tactic.skeletonp x)
                        (tactic.conditional-eqsubst-okp x)
                        (logic.appeal-listp proofs)
                        (equal (clause.clause-list-formulas (tactic.skeleton->goals x))
                               (logic.strip-conclusions proofs))
                        ;; ---
                        (tactic.skeleton-atblp x atbl)
                        (tactic.conditional-eqsubst-env-okp x atbl)
                        (logic.proof-listp proofs axioms thms atbl)
                        (@obligations tactic.conditional-eqsubst-compile)))
            (equal (logic.proof-listp (tactic.conditional-eqsubst-compile x proofs) axioms thms atbl)
                   t))))