File: formula-compiler.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 (368 lines) | stat: -rw-r--r-- 16,400 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
; 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 "equal")
(include-book "if")
(set-verify-guards-eagerness 2)
(set-case-split-limitations nil)
(set-well-founded-relation ord<)
(set-measure-function rank)


(dd.open "formula-compiler.tex")

;; Formula Compiler.
;;
;; We introduce a compiler which translates formulas into terms.  This is very
;; useful because it means we can focus on term manipulation rather than trying
;; to deal with formulas and terms.  The compliation is done by recursively
;; applying the following transformations everywhere throughout the formula:
;;
;;   compile(x = y) == (equal x y)
;;   compile(~F)    == (if compile(F) nil t)
;;   compile(F v G) == (if compile(F) t compile(G))
;;
;; Now, if we want to prove some formula, A-formula, it suffices to compile it
;; into A-term, and then prove A-term != nil.  Then, we can just use the
;; following derivation:
;;
;;  1. A-formula v A-term = nil        Compile-Formula Builder, Property #3
;;  2. A-term = nil v A-formula        Commute Or
;;  3. A-term != nil                   Given
;;  4. A-formula                       Modus Ponens 2; 3,2
;;
;; Q.E.D.

(defund logic.compile-formula (x)
  (declare (xargs :guard (logic.formulap x)
                  :verify-guards nil))
  (cond ((equal (logic.fmtype x) 'por*)
         (logic.function 'if
                (list (logic.compile-formula (logic.vlhs x))
                      ''t
                      (logic.compile-formula (logic.vrhs x)))))
        ((equal (logic.fmtype x) 'pnot*)
         (logic.function 'if
                (list (logic.compile-formula (logic.~arg x))
                      ''nil
                      ''t)))
        ((equal (logic.fmtype x) 'pequal*)
         (logic.function 'equal (list (logic.=lhs x) (logic.=rhs x))))
        (t nil)))

(defthm forcing-logic.termp-of-logic.compile-formula
  (implies (force (logic.formulap x))
           (equal (logic.termp (logic.compile-formula x))
                  t))
  :hints(("Goal" :in-theory (enable logic.compile-formula))))

(defthm forcing-logic.term-atblp-of-logic.compile-formula
  (implies (force (and (logic.formulap x)
                       (logic.formula-atblp x atbl)
                       (equal (cdr (lookup 'if atbl)) 3)
                       (equal (cdr (lookup 'equal atbl)) 2)))
           (equal (logic.term-atblp (logic.compile-formula x) atbl)
                  t))
  :hints(("Goal" :in-theory (enable logic.compile-formula))))

(verify-guards logic.compile-formula)




(defderiv build.compile-formula-lemma-1
  :derive (v (v B C) (= (if (? p) t (? q)) nil))
  :from ((proof x (v B (= (? p) nil)))
         (proof y (v C (= (? q) nil))))
  :proof (@derive
          ((v B (= (? p) nil))                          (@given x))
          ((v B (= (if (? p) t (? q)) (? q)))           (build.disjoined-if-when-nil @- (@term t) (@term (? q))))
          ((v (v B C) (= (if (? p) t (? q)) (? q)))     (build.multi-assoc-expansion @- (@formulas B C)) *1)
          ((v C (= (? q) nil))                          (@given y))
          ((v (v B C) (= (? q) nil))                    (build.multi-assoc-expansion @- (@formulas B C)))
          ((v (v B C) (= (if (? p) t (? q)) nil))       (build.disjoined-transitivity-of-pequal *1 @-)))
  :minatbl ((if . 3)))

(defderiv build.compile-formula-lemma-2
  :derive (v (! (v B C)) (= (if (? p) t (? q)) t))
  :from ((proof x (v (! B) (= (? p) t)))
         (proof y (v (! C) (= (? q) t))))
  :proof (@derive
          ((v (! B) (= (? p) t))                     (@given x))
          ((v (! B) (!= (? p) nil))                  (build.disjoined-not-nil-from-t @-))
          ((v (! B) (= (if (? p) t (? q)) t))        (build.disjoined-if-when-not-nil @- (@term t) (@term (? q))) *1)
          ((v (! C) (= (? q) t))                     (@given y))
          ((v (! C) (= t (? q)))                     (build.disjoined-commute-pequal @-))
          ((v (! C) (= (if (? p) t (? q)) t))        (build.disjoined-if-when-same @- (@term (? p))))
          ((v (! (v B C)) (= (if (? p) t (? q)) t))  (build.merge-implications *1 @-)))
  :minatbl ((if . 3)))

(defund@ build.compile-formula (a)
  (declare (xargs :guard (logic.formulap a)
                  :verify-guards nil))
  ;; We simultaneously derive the following four formulas.
  ;;
  ;;   1. ~A v (logic.compile-formula A) = t
  ;;   2. A v (logic.compile-formula A) = nil
  ;;
  ;; The resulting proofs are returned in a list.
  (cond ((equal (logic.fmtype a) 'por*)
         ;; Let A = B v C.
         ;; Let D = (logic.compile-formula B).
         ;; Let E = (logic.compile-formula C).
         ;; Now (logic.compile-formula A) is (if D t E).
         (let* ((subproofs-b (build.compile-formula (logic.vlhs a)))
                (subproofs-c (build.compile-formula (logic.vrhs a)))
                ;; Sub-B1: ~B v D = t
                (sub-b1      (first subproofs-b))
                ;; Sub-B2: B v D = nil
                (sub-b2      (second subproofs-b))
                ;; Sub-C1: ~C v E = t
                (sub-c1      (first subproofs-c))
                ;; Sub-C2: ~C v E = nil
                (sub-c2      (second subproofs-c))
                ;; Goal1: ~(B v C) v (if D t E) = t
                (goal-1 (@derive ((v (! B) (= (? d) t))                        (@given sub-b1))
                                 ((v (! C) (= (? e) t))                        (@given sub-c1))
                                 ((v (! (v B C)) (= (if (? d) t (? e)) t))     (build.compile-formula-lemma-2 @-- @-))))
                ;; Goal2: (B v C) v (if D t E) = nil
                (goal-2 (@derive ((v B (= (? d) nil))                          (@given sub-b2))
                                 ((v C (= (? e) nil))                          (@given sub-c2))
                                 ((v (v B C) (= (if (? d) t (? e)) nil))       (build.compile-formula-lemma-1 @-- @-)))))

           (list goal-1 goal-2)))

        ((equal (logic.fmtype a) 'pnot*)
         ;; Let A = ~B.
         ;; Let C = (logic.compile-formula B).
         ;; Now (logic.compile-formula A) is (if C nil t).
         (let* ((subproofs (build.compile-formula (logic.~arg a)))
                ;; Sub1: ~B v C = t
                (sub1      (first subproofs))
                ;; Sub2: B v C = nil
                (sub2      (second subproofs))
                ;; Goal1: ~~B v (if C nil t) = t
                (goal-1 (@derive ((v B (= (? c) nil))                          (@given sub2))
                                 ((v B (= (if (? c) nil t) t))                 (build.disjoined-if-when-nil @- (@term nil) (@term t)))
                                 ((v (! (! B)) (= (if (? c) nil t) t))         (build.lhs-insert-neg-neg @-))))
                ;; Goal2: ~B v (if C nil t) = nil
                (goal-2 (@derive ((v (! B) (= (? c) t))                        (@given sub1))
                                 ((v (! B) (!= (? c) nil))                     (build.disjoined-not-nil-from-t @-))
                                 ((v (! B) (= (if (? c) nil t) nil))           (build.disjoined-if-when-not-nil @- (@term nil) (@term t))))))
           (list goal-1 goal-2)))


        ((equal (logic.fmtype a) 'pequal*)
         ;; Now (logic.compile-formula A) is (equal lhs rhs).
         (let* ((left   (logic.=lhs a))
                (right  (logic.=rhs a))
                (goal-1 (@derive ((v (!= x y) (= (equal x y) t))                     (build.axiom (axiom-equal-when-same)))
                                 ((v (!= (? l) (? r)) (= (equal (? l) (? r)) t))     (build.instantiation @- (list (cons 'x left) (cons 'y right))))))
                (goal-2 (@derive ((v (= x y) (= (equal x y) nil))                    (build.axiom (axiom-equal-when-diff)))
                                 ((v (= (? l) (? r)) (= (equal (? l) (? r)) nil))    (build.instantiation @- (list (cons 'x left) (cons 'y right)))))))
           (list goal-1 goal-2)))

        (t nil)))



(defobligations build.compile-formula
  (build.instantiation
   build.lhs-insert-neg-neg
   build.disjoined-if-when-nil
   build.disjoined-if-when-not-nil
   build.compile-formula-lemma-1
   build.compile-formula-lemma-2
   build.disjoined-not-nil-from-t
   build.disjoined-not-t-from-nil
   build.disjoined-not-nil-from-t
   build.disjoined-t-from-not-nil)
  :extra-axioms ((axiom-equal-when-same)
                 (axiom-equal-when-diff)))


(encapsulate
 ()
 (local (defthm lemma
          (implies (logic.formulap x)
                   (let ((result (logic.compile-formula x))
                         (proofs (build.compile-formula x)))
                     (and (logic.appealp (first proofs))
                          (logic.appealp (second proofs))
                          (equal (logic.conclusion (first proofs)) (logic.por (logic.pnot x) (logic.pequal result ''t)))
                          (equal (logic.conclusion (second proofs)) (logic.por x (logic.pequal result ''nil)))
                          )))
          :rule-classes nil
          :hints(("Goal"
                  :induct (logic.compile-formula x)
                  :in-theory (enable logic.compile-formula
                                     build.compile-formula
                                     axiom-equal-when-same
                                     axiom-equal-when-diff)))))

 (defthm forcing-logic.appealp-of-first-of-build.compile-formula
   (implies (force (logic.formulap x))
            (equal (logic.appealp (first (build.compile-formula x)))
                   t))
   :hints(("Goal" :use ((:instance lemma)))))

 (defthm forcing-logic.appealp-of-second-of-build.compile-formula
   (implies (force (logic.formulap x))
            (equal (logic.appealp (second (build.compile-formula x)))
                   t))
   :hints(("Goal" :use ((:instance lemma)))))

 (defthm forcing-logic.conclusion-of-first-of-build.compile-formula
   (implies (force (logic.formulap x))
            (equal (logic.conclusion (first (build.compile-formula x)))
                   (logic.por (logic.pnot x) (logic.pequal (logic.compile-formula x) ''t))))
   :rule-classes ((:rewrite :backchain-limit-lst 0))
   :hints(("Goal" :use ((:instance lemma)))))

 (defthm forcing-logic.conclusion-of-second-of-build.compile-formula
   (implies (force (logic.formulap x))
            (equal (logic.conclusion (second (build.compile-formula x)))
                   (logic.por x (logic.pequal (logic.compile-formula x) ''nil))))
   :rule-classes ((:rewrite :backchain-limit-lst 0))
   :hints(("Goal" :use ((:instance lemma))))))



(encapsulate
 ()
 (local (defthm@ lemma
          (implies (and (logic.formulap x)
                        (logic.formula-atblp x atbl)
                        (equal (cdr (lookup 'equal atbl)) 2)
                        (equal (cdr (lookup 'if atbl)) 3)
                        (@obligations build.compile-formula))
                   (and (logic.proofp (first (build.compile-formula x)) axioms thms atbl)
                        (logic.proofp (second (build.compile-formula x)) axioms thms atbl)))
          :hints(("Goal" :in-theory (enable build.compile-formula
                                            logic.compile-formula
                                            axiom-equal-when-same
                                            axiom-equal-when-diff)))))

 (defthm@ forcing-logic.proofp-of-first-of-build.compile-formula
   (implies (force (and (logic.formulap x)
                        (logic.formula-atblp x atbl)
                        (equal (cdr (lookup 'equal atbl)) 2)
                        (equal (cdr (lookup 'if atbl)) 3)
                        (@obligations build.compile-formula)))
            (equal (logic.proofp (first (build.compile-formula x)) axioms thms atbl)
                   t))
   :hints(("Goal" :use ((:instance lemma)))))

 (defthm@ forcing-logic.proofp-of-second-of-build.compile-formula
   (implies (force (and (logic.formulap x)
                        (logic.formula-atblp x atbl)
                        (equal (cdr (lookup 'equal atbl)) 2)
                        (equal (cdr (lookup 'if atbl)) 3)
                        (@obligations build.compile-formula)))
            (equal (logic.proofp (second (build.compile-formula x)) axioms thms atbl)
                   t))
   :hints(("Goal" :use ((:instance lemma))))))


(verify-guards build.compile-formula
               :hints(("Goal" :in-theory (enable axiom-equal-when-same
                                                 axiom-equal-when-diff))))


(defprojection
  :list (logic.compile-formula-list x)
  :element (logic.compile-formula x)
  :guard (logic.formula-listp x)
  :nil-preservingp t)

(defthm forcing-logic.term-listp-of-logic.compile-formula-list
  (implies (force (logic.formula-listp x))
           (equal (logic.term-listp (logic.compile-formula-list x))
                  t))
  :hints(("Goal" :induct (cdr-induction x))))

(defthm forcing-logic.term-list-atblp-of-logic.compile-formula-list
  (implies (force (and (logic.formula-listp x)
                       (logic.formula-list-atblp x atbl)
                       (equal (cdr (lookup 'if atbl)) 3)
                       (equal (cdr (lookup 'equal atbl)) 2)))
           (equal (logic.term-list-atblp (logic.compile-formula-list x) atbl)
                  t))
  :hints(("Goal" :induct (cdr-induction x))))



(defund build.compile-formula-list-comm-2 (x)
  (declare (xargs :guard (logic.formula-listp x)))
  (if (consp x)
      (cons (build.commute-or (second (build.compile-formula (car x))))
            (build.compile-formula-list-comm-2 (cdr x)))
    nil))

(defobligations build.compile-formula-list-comm-2
  (build.commute-or
   build.compile-formula))

(encapsulate
 ()
 (local (in-theory (enable build.compile-formula-list-comm-2)))

 (defthm len-of-build.compile-formula-list-comm-2
   (equal (len (build.compile-formula-list-comm-2 x))
          (len x)))

 (defthm logic.appeal-listp-of-build.compile-formula-list-comm-2
   (implies (force (logic.formula-listp x))
            (equal (logic.appeal-listp (build.compile-formula-list-comm-2 x))
                   t)))

 (defthm logic.strip-conclusions-of-logic.compile-formula-list-bldr3
   (implies (force (logic.formula-listp x))
            (equal (logic.strip-conclusions (build.compile-formula-list-comm-2 x))
                   (logic.por-list
                    (logic.pequal-list (logic.compile-formula-list x)
                                       (repeat ''nil (len x)))
                    x))))

 (defthm@ logic.proof-listp-of-build.compile-formula-list-comm-2
   (implies (force (and (logic.formula-listp x)
                        ;; ---
                        (logic.formula-list-atblp x atbl)
                        (equal (cdr (lookup 'equal atbl)) 2)
                        (equal (cdr (lookup 'if atbl)) 3)
                        (@obligations build.compile-formula-list-comm-2)
                        ))
            (equal (logic.proof-listp (build.compile-formula-list-comm-2 x) axioms thms atbl)
                   t))))

(dd.close)