File: evaluator.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 (375 lines) | stat: -rw-r--r-- 14,928 bytes parent folder | download | duplicates (5)
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
(in-package "PROOF-CHECKER-ARRAY")

(include-book "ternary")
(include-book "literal")
(include-book "unique")
(include-book "sets")
(include-book "assignment")
(include-book "clause")
(include-book "all-literals")


;; ===================================================================
;; ======================= LITERAL EVALUATION ========================

(defun evaluate-literal (literal assignment)
  (declare (xargs :guard (and (literalp literal)
                              (assignmentp assignment))))
  (cond
   ((member literal assignment) (true))
   ((member (negate literal) assignment) (false))
   (t (undef))))


;; ===================================================================
;; ========================= EVALUATE-CLAUSE =========================

(defun evaluate-clause (clause assignment)
  (declare (xargs :guard (and (clausep clause)
                              (assignmentp assignment))))
  (if (atom clause)
      (false)
    (let* ((literal (car clause))
           (literal-value (evaluate-literal literal assignment)))
      (if (truep literal-value)
          (true)
        (let* ((remaining-clause (cdr clause))
               (remaining-clause-value (evaluate-clause remaining-clause
                                                        assignment)))
          (cond
           ((truep remaining-clause-value) (true))
           ((undefp literal-value) (undef))
           (t remaining-clause-value)))))))
       
(defthm ternaryp-evaluate-clause
  (ternaryp (evaluate-clause clause assignment)))


;; ===================================================================
;; ======================== EVALUATE-FORMULA =========================

(defun evaluate-formula (formula assignment)
  (declare (xargs :guard (and (formulap formula)
                              (assignmentp assignment))))
  (if (atom formula)
      (true)
    (let* ((clause (car formula))
           (clause-value (evaluate-clause clause assignment)))
      (if (falsep clause-value)
          (false)
        (let* ((remaining-formula (cdr formula))
               (remaining-formula-value (evaluate-formula remaining-formula
                                                          assignment)))
          (cond
           ((falsep remaining-formula-value) (false))
           ((undefp clause-value) (undef))
           (t remaining-formula-value)))))))

(defthm ternaryp-evaluate-formula
  (ternaryp (evaluate-formula formula assignment)))


;; ===================================================================
;; ============================== CONS ===============================

(defthm iff-truep-evaluate-clause-cons
  (iff (truep (evaluate-clause clause (cons literal assignment)))
       (or (member literal clause)
           (truep (evaluate-clause clause assignment)))))

(defthm truep-evaluate-formula-cons
  (equal (truep (evaluate-formula (cons clause formula) assignment))
         (and (truep (evaluate-clause clause assignment))
              (truep (evaluate-formula formula assignment)))))

(defthm not-undefp-evaluate-clause-cons
  (implies (not (undefp (evaluate-clause clause assignment)))
           (not (undefp (evaluate-clause clause (cons literal assignment))))))



;; ===================================================================
;; =================== SET EQUALITY AND EVALUATION ===================

; We can reduce any of these to truep implies truep.  They might be stronger
; than needed.

(defthm set-equalp-implies-equal-evaluate-literal
  (implies (and (set-equalp x y)
                (literalp literal)
                (assignmentp x)
                (assignmentp y))
           (equal (evaluate-literal literal y)
                  (evaluate-literal literal x))))


(defthm set-equalp-implies-equal-evaluate-clause
  (implies (and (set-equalp x y)
                (clausep clause)
                (assignmentp x)
                (assignmentp y))
           (equal (evaluate-clause clause y)
                  (evaluate-clause clause x))))
  ;; :hints (("Goal" :in-theory (disable
  ;; NOT-SET-DIFFERENCE-VARIABLES-IMPLIES-SUBSET-VARIABLESP
  ;; SUBSETP-AND-SET-EQUAL-VARIABLESP-IMPLIES-SUBSETP
  ;; SUBSET-VARIABLESP-AND-SUBSETP-IMPLIES-SUBSETP))))


(defthm set-equalp-implies-equal-evaluate-formula
  (implies (and (set-equalp x y)
                (formulap formula)
                (assignmentp x)
                (assignmentp y))
           (equal (evaluate-formula formula y)
                  (evaluate-formula formula x)))
  :hints (("Goal" :in-theory (disable set-equalp)))) ; want to remove this



;; ===================================================================
;; ===================== SHORTEN-LONG-ASSIGNMENT =====================

(defun shorten-long-assignment (assignment all-literals)
  (declare (xargs :guard (and (literal-listp assignment)
                              (literal-listp all-literals))))
  (union-variables assignment all-literals))


;; =============== ASSIGNMENTP-SHORTEN-LONG-ASSIGNMENT ===============

(encapsulate
 ()

 (local
  (defthm literal-listp-shorten-long-assignment
    (implies (assignmentp assignment)
             (literal-listp (shorten-long-assignment assignment all-literals)))))
 
 (local
  (defthm unique-literalsp-shorten-long-assignment
    (implies (assignmentp x)
             (unique-literalsp (shorten-long-assignment x y)))))
 
 (local
  (defthm no-conflicting-literalsp-shorten-long-assignment
    (implies (assignmentp x)
             (no-conflicting-literalsp (shorten-long-assignment x y)))))

 (defthm assignmentp-shorten-long-assignment
   (implies (assignmentp x)
            (assignmentp (shorten-long-assignment x y)))
   :hints (("Goal"
            :in-theory (enable assignmentp))))
 )


;; ============ SUBSET-VARIABLESP-SHORTEN-LONG-ASSIGNMENT ============

(defthm subsetp-shorten-long-assignment
  (subsetp (shorten-long-assignment x y) x))

(defthm subset-variablesp-shorten-long-assignment
  (subset-variablesp (shorten-long-assignment x y) y))


;; ========= TRUEP-EVALUATE-FORMULA-SHORTEN-LONG-ASSIGNMENT ==========

(defthm truep-evaluate-literal-shorten-long-assignment
  (implies (and (truep (evaluate-literal literal assignment))
                (member literal all-literals))
           (truep (evaluate-literal literal (shorten-long-assignment
                                             assignment
                                             all-literals)))))

(defthm truep-evaluate-clause-shorten-long-assignment
  (implies (and (truep (evaluate-clause clause assignment))
                (subsetp clause all-literals))
           (truep (evaluate-clause clause (shorten-long-assignment
                                           assignment
                                           all-literals)))))


(defthm subsetp-list-truep-evaluate-formula-shorten-long-assignment
  (implies (and (truep (evaluate-formula formula assignment))
                (subsetp-list formula all-literals))
           (truep (evaluate-formula formula
                                    (shorten-long-assignment
                                     assignment
                                     all-literals))))
  :hints (("Goal"
           :in-theory (disable shorten-long-assignment))))

(defthm truep-evaluate-formula-shorten-long-assignment
  (implies (truep (evaluate-formula formula assignment))
           (truep (evaluate-formula formula
                                    (shorten-long-assignment
                                     assignment
                                     (all-literals formula)))))
  :hints (("Goal" 
           :in-theory (disable shorten-long-assignment))))


;; ============ STRONG-ASSIGNMENT-SHORTEN-LONG-ASSIGNMENT ============

(defthm strong-assignment-shorten-long-assignment
  (implies (and (assignmentp assignment)
                (truep (evaluate-formula formula assignment)))
           (and (assignmentp (shorten-long-assignment
                              assignment
                              (all-literals formula)))
                (truep (evaluate-formula formula 
                                         (shorten-long-assignment
                                          assignment
                                          (all-literals formula))))
                (subset-variablesp (shorten-long-assignment
                                    assignment
                                    (all-literals formula))
                                   (all-literals formula))))
  :hints (("Goal"
           :in-theory (disable shorten-long-assignment))))
                 

;; ===================================================================
;; ===================== EXTEND-SHORT-ASSIGNMENT =====================

; consider switching append order?
(defun extend-short-assignment (assignment all-literals)
  (declare (xargs :guard (and (literal-listp assignment)
                              (literal-listp all-literals))))
  (append (set-difference-variables (remove-conflicting-literals all-literals)
                                    assignment)
          assignment))


;; =============== ASSIGNMENTP-EXTEND-SHORT-ASSIGNMENT ===============

(encapsulate
 ()
 
 (local
  (defthm literal-listp-extend-short-assignment
    (implies (and (assignmentp assignment)
                  (literal-listp all-literals))
             (literal-listp (extend-short-assignment assignment
                                                     all-literals)))))

 (local
  (defthm unique-literalsp-extend-short-assignment
    (implies (and (assignmentp assignment)
                  (unique-literalsp all-literals))
             (unique-literalsp (extend-short-assignment assignment
                                                        all-literals)))))

 (local
  (defthm no-conflicting-literalsp-extend-short-assignment
    (implies (assignmentp assignment)
             (no-conflicting-literalsp (extend-short-assignment
                                        assignment
                                        all-literals)))))

 (defthm assignmentp-extend-short-assignment
   (implies (and (assignmentp assignment)
                 (literal-listp all-literals)
                 (unique-literalsp all-literals))
            (assignmentp (extend-short-assignment assignment all-literals))))
 )


;; ============ SUBSET-VARIABLESP-EXTEND-SHORT-ASSIGNMENT ============

(defthm subset-variablesp-extend-short-assignment
  (implies (literal-listp all-literals)
           (subset-variablesp all-literals
                              (extend-short-assignment assignment
                                                       all-literals))))       
       
(defthm subset-variablesp-extend-short-assignment2
  (implies (subset-variablesp assignment all-literals)
           (subset-variablesp (extend-short-assignment assignment
                                                       all-literals)
                              all-literals)))


;; ========= TRUEP-EVALUATE-FORMULA-EXTEND-SHORT-ASSIGNMENT ==========

(defthm truep-evaluate-literal-extend-long-assignment
  (implies (and (truep (evaluate-literal literal assignment))
                (member literal all-literals))
           (truep (evaluate-literal literal (extend-short-assignment
                                             assignment
                                             all-literals)))))

(defthm truep-evaluate-clause-extend-short-assignment
  (implies (and (truep (evaluate-clause clause assignment))
                (subsetp clause all-literals))
           (truep (evaluate-clause clause (extend-short-assignment
                                           assignment
                                           all-literals)))))

(defthm subsetp-list-truep-evaluate-formula-extend-short-assignment
  (implies (and (truep (evaluate-formula formula assignment))
                (subsetp-list formula all-literals))
           (truep (evaluate-formula formula (extend-short-assignment
                                             assignment
                                             all-literals))))
  :hints (("Goal"
           :in-theory (disable extend-short-assignment))))

(defthm truep-evaluate-formula-extend-short-assignment
  (implies (and (assignmentp assignment)
                (truep (evaluate-formula formula assignment)))
           (truep (evaluate-formula formula (extend-short-assignment
                                             assignment
                                             (all-literals formula)))))
  :hints (("Goal"
           :in-theory (disable extend-short-assignment))))


;; ============ STRONG-ASSIGNMENT-EXTEND-SHORT-ASSIGNMENT ============

(defthm strong-assignment-extend-short-assignment
  (implies (and (formulap formula)
                (assignmentp assignment)
                (truep (evaluate-formula formula assignment)))
           (and (assignmentp (extend-short-assignment
                              assignment
                              (all-literals formula)))
                (truep (evaluate-formula formula (extend-short-assignment
                                                  assignment
                                                  (all-literals formula))))
                (subset-variablesp (all-literals formula)
                                   (extend-short-assignment
                                    assignment
                                    (all-literals formula)))))
  :hints (("Goal"
           :in-theory (disable extend-short-assignment))))


;; ===================================================================
;; ============== SATISFYING-IMPLIES-STRONG-SATISFYING ===============

(defun-sk exists-strong-satisfying-assignment (formula)
  (exists assignment (and (assignmentp assignment)
                          (truep (evaluate-formula formula assignment))
                          (set-equal-variablesp (all-literals formula)
                                                assignment))))
(in-theory (disable exists-strong-satisfying-assignment
                    exists-strong-satisfying-assignment-suff))


(defthm satisfying-assignment-implies-exists-strong-satisfying-assignment
  (implies (and (formulap formula)
                (assignmentp assignment)
                (truep (evaluate-formula formula assignment)))
           (exists-strong-satisfying-assignment formula))
  :hints (("Goal"
           :in-theory (disable extend-short-assignment
                               shorten-long-assignment
                               )
           :use ((:instance exists-strong-satisfying-assignment-suff
                            (assignment (extend-short-assignment
                                         (shorten-long-assignment
                                          assignment
                                          (all-literals formula))
                                         (all-literals formula))))))))