-
Notifications
You must be signed in to change notification settings - Fork 86
/
old_boolExpToCnfScript.sml
309 lines (273 loc) · 8.41 KB
/
old_boolExpToCnfScript.sml
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
(*
Encoding from Boolean expressions to CNF.
*)
open preamble miscTheory ASCIInumbersTheory cnfTheory;
val _ = new_theory "old_boolExpToCnf";
(* ----------------------- Types --------------------------------------- *)
Datatype:
nnf =
NnfTrue
| NnfFalse
| NnfLit literal
| NnfAnd nnf nnf
| NnfOr nnf nnf
End
Datatype:
noImp =
NoImpTrue
| NoImpFalse
| NoImpLit literal
| NoImpNot noImp
| NoImpAnd noImp noImp
| NoImpOr noImp noImp
End
Datatype:
boolExp =
True
| False
| Lit literal
| Not boolExp
| And boolExp boolExp
| Or boolExp boolExp
| Impl boolExp boolExp
| Iff boolExp boolExp
End
(* ----------------------- Satisfiability ------------------------------ *)
Definition eval_nnf_def:
(eval_nnf (w: assignment) NnfTrue = T) ∧
(eval_nnf w NnfFalse = F) ∧
(eval_nnf w (NnfLit l) = eval_literal w l) ∧
(eval_nnf w (NnfAnd nnf1 nnf2) =
(eval_nnf w nnf1 ∧ eval_nnf w nnf2)) ∧
(eval_nnf w (NnfOr nnf1 nnf2) =
(eval_nnf w nnf1 ∨ eval_nnf w nnf2))
End
Definition eval_noImp_def:
(eval_noImp (w: assignment) NoImpTrue = T) ∧
(eval_noImp w NoImpFalse = F) ∧
(eval_noImp w (NoImpLit l) =
eval_literal w l) ∧
(eval_noImp w (NoImpNot b) =
¬ (eval_noImp w b)) ∧
(eval_noImp w (NoImpAnd b1 b2) =
(eval_noImp w b1 ∧ eval_noImp w b2)) ∧
(eval_noImp w (NoImpOr b1 b2) =
(eval_noImp w b1 ∨ eval_noImp w b2))
End
Definition eval_boolExp_def:
(eval_boolExp (w: assignment) True = T) ∧
(eval_boolExp w False = F) ∧
(eval_boolExp w (Lit l) = eval_literal w l) ∧
(eval_boolExp w (Not b) = ¬ (eval_boolExp w b)) ∧
(eval_boolExp w (And b1 b2) =
(eval_boolExp w b1 ∧ eval_boolExp w b2)) ∧
(eval_boolExp w (Or b1 b2) =
(eval_boolExp w b1 ∨ eval_boolExp w b2)) ∧
(eval_boolExp w (Impl b1 b2) =
(eval_boolExp w b1 ⇒ eval_boolExp w b2)) ∧
(eval_boolExp w (Iff b1 b2) =
(eval_boolExp w b1 ⇔ eval_boolExp w b2))
End
Definition unsat_boolExp_def:
unsat_boolExp b = ∀w. ¬ eval_boolExp w b
End
(* ----------------- Simplification functions -------------------------- *)
Definition distr_def:
(distr CnfEmpty _ = CnfEmpty) ∧
(distr _ CnfEmpty = CnfEmpty) ∧
(distr (CnfAnd a b) c = CnfAnd (distr a c) (distr b c)) ∧
(distr a (CnfAnd b c) = CnfAnd (distr a b) (distr a c)) ∧
(distr (CnfClause a) (CnfClause b) = CnfClause (ClauseOr a b))
End
Definition nnf_to_cnf_def:
(nnf_to_cnf NnfTrue = CnfEmpty) ∧
(nnf_to_cnf NnfFalse = CnfClause ClauseEmpty) ∧
(nnf_to_cnf (NnfLit l) = CnfClause (ClauseLit l)) ∧
(nnf_to_cnf (NnfAnd a b) = CnfAnd (nnf_to_cnf a) (nnf_to_cnf b)) ∧
(nnf_to_cnf (NnfOr a b) = distr (nnf_to_cnf a) (nnf_to_cnf b))
End
Definition noImp_to_nnf_def:
(noImp_to_nnf NoImpTrue = NnfTrue) ∧
(noImp_to_nnf NoImpFalse = NnfFalse) ∧
(noImp_to_nnf (NoImpLit l) = NnfLit l) ∧
(noImp_to_nnf (NoImpNot NoImpTrue) = NnfFalse) ∧
(noImp_to_nnf (NoImpNot NoImpFalse) = NnfTrue) ∧
(noImp_to_nnf (NoImpNot (NoImpLit l)) =
NnfLit (negate_literal l)) ∧
(noImp_to_nnf (NoImpNot (NoImpNot b)) = noImp_to_nnf b) ∧
(noImp_to_nnf (NoImpNot (NoImpAnd b1 b2)) =
NnfOr
(noImp_to_nnf (NoImpNot b1))
(noImp_to_nnf (NoImpNot b2))) ∧
(noImp_to_nnf (NoImpNot (NoImpOr b1 b2)) =
NnfAnd
(noImp_to_nnf (NoImpNot b1))
(noImp_to_nnf (NoImpNot b2))) ∧
(noImp_to_nnf (NoImpAnd b1 b2) =
NnfAnd (noImp_to_nnf b1) (noImp_to_nnf b2)) ∧
(noImp_to_nnf (NoImpOr b1 b2) =
NnfOr (noImp_to_nnf b1) (noImp_to_nnf b2))
End
Definition boolExp_to_noImp_def:
(boolExp_to_noImp True = NoImpTrue) ∧
(boolExp_to_noImp False = NoImpFalse) ∧
(boolExp_to_noImp (Lit l) = NoImpLit l) ∧
(boolExp_to_noImp (Not b) =
case (boolExp_to_noImp b) of
NoImpTrue => NoImpFalse
| NoImpFalse => NoImpTrue
| b' => (NoImpNot b')) ∧
(boolExp_to_noImp (And b1 b2) =
case (boolExp_to_noImp b1, boolExp_to_noImp b2) of
(NoImpFalse, _) => NoImpFalse
| (NoImpTrue, b2') => b2'
| (_, NoImpFalse) => NoImpFalse
| (b1', NoImpTrue) => b1'
| (b1', b2') => (NoImpAnd b1' b2')) ∧
(boolExp_to_noImp (Or b1 b2) =
case (boolExp_to_noImp b1, boolExp_to_noImp b2) of
(NoImpTrue, _) => NoImpTrue
| (NoImpFalse, b2') => b2'
| (_, NoImpTrue) => NoImpTrue
| (b1', NoImpFalse) => b1'
| (b1', b2') => (NoImpOr b1' b2')) ∧
(boolExp_to_noImp (Impl b1 b2) =
case (boolExp_to_noImp b1, boolExp_to_noImp b2) of
(NoImpFalse, _) => NoImpTrue
| (NoImpTrue, b2') => b2'
| (_, NoImpTrue) => NoImpTrue
| (b1', NoImpFalse) => (NoImpNot b1')
| (b1', b2') => (NoImpOr (NoImpNot b1') b2')) ∧
(boolExp_to_noImp (Iff b1 b2) =
case (boolExp_to_noImp b1, boolExp_to_noImp b2) of
(NoImpTrue, NoImpTrue) => NoImpTrue
| (NoImpFalse, NoImpFalse) => NoImpTrue
| (NoImpTrue, NoImpFalse) => NoImpFalse
| (NoImpFalse, NoImpTrue) => NoImpFalse
| (NoImpTrue, b2') => b2'
| (NoImpFalse, b2') => (NoImpNot b2')
| (b1', NoImpTrue) => b1'
| (b1', NoImpFalse) => (NoImpNot b1')
| (b1', b2') => NoImpOr
(NoImpAnd b1' b2')
(NoImpAnd (NoImpNot b1') (NoImpNot b2')))
End
Definition boolExp_to_cnf_def:
boolExp_to_cnf b = nnf_to_cnf (noImp_to_nnf (boolExp_to_noImp b))
End
(* ----------------------- Theorems ------------------------------------ *)
Theorem distr_preserves_sat:
∀ b1 b2.
eval_cnf w (distr b1 b2) ⇔
(eval_cnf w b1 ∨ eval_cnf w b2)
Proof
ho_match_mp_tac distr_ind
>> rpt strip_tac
>> rw[distr_def, eval_cnf_def, eval_clause_def]
>> metis_tac[]
QED
Theorem nnf_to_cnf_preserves_sat:
eval_nnf w b = eval_cnf w (nnf_to_cnf b)
Proof
Induct_on ‘b’
>> simp[eval_nnf_def, nnf_to_cnf_def,
eval_cnf_def, eval_clause_def,
distr_preserves_sat]
QED
Theorem negate_literal_thm:
eval_literal w (negate_literal l) ⇔ ¬ eval_literal w l
Proof
Cases_on ‘l’
>> rw[negate_literal_def, eval_literal_def]
QED
Theorem noImpNot_thm:
∀ b.
eval_nnf w (noImp_to_nnf (NoImpNot b)) ⇔
¬ eval_nnf w (noImp_to_nnf b)
Proof
Induct
>> rw[noImp_to_nnf_def, eval_nnf_def, negate_literal_thm]
QED
Theorem noImp_to_nnf_preserves_sat:
eval_noImp w b = eval_nnf w (noImp_to_nnf b)
Proof
Induct_on ‘b’
>> rw[eval_noImp_def,
noImp_to_nnf_def,
eval_nnf_def,
noImpNot_thm]
QED
Theorem boolExp_to_noImp_preserves_sat:
eval_boolExp w b = eval_noImp w (boolExp_to_noImp b)
Proof
Induct_on ‘b’
>> Cases_on‘boolExp_to_noImp b’
>> Cases_on‘boolExp_to_noImp b'’
>> rw[eval_boolExp_def,
boolExp_to_noImp_def,
eval_noImp_def]
>> metis_tac[]
QED
Theorem boolExp_to_cnf_preserves_sat:
eval_boolExp w b = eval_cnf w (boolExp_to_cnf b)
Proof
rw[boolExp_to_noImp_preserves_sat,
noImp_to_nnf_preserves_sat,
nnf_to_cnf_preserves_sat,
boolExp_to_cnf_def]
QED
(* --------------------- Pretty printing ------------------------- *)
Definition lit_to_str_def:
lit_to_str (INL l) = "b" ++ num_to_dec_string l ∧
lit_to_str (INR l) = "~b" ++ num_to_dec_string l
End
Definition clause_to_str_def:
clause_to_str ClauseEmpty = "False" ∧
clause_to_str (ClauseLit l) = lit_to_str l ∧
clause_to_str (ClauseOr b1 b2) =
clause_to_str b1 ++ " \\/ " ++ clause_to_str b2
End
Definition cnf_to_str_def:
cnf_to_str CnfEmpty = "True" ∧
cnf_to_str (CnfClause c) = clause_to_str c ∧
cnf_to_str (CnfAnd b1 b2) =
let b1_str =
case b1 of
(CnfClause (ClauseOr _ _)) => "(" ++ cnf_to_str b1 ++ ")"
| _ => cnf_to_str b1
in let b2_str =
case b2 of
(CnfClause (ClauseOr _ _)) => "(" ++ cnf_to_str b2 ++ ")"
| _ => cnf_to_str b2
in b1_str ++ " /\\ " ++ b2_str
End
Theorem example1 =
EVAL “cnf_to_str
(boolExp_to_cnf
(And
(Not (And
(Lit (INL 2))
(Lit (INR 1))))
(Or
(Lit (INL 0))
(Lit (INR 1)))))”;
Theorem example2 =
EVAL “cnf_to_str
(boolExp_to_cnf
(And
(Lit (INL 0))
(And
(Lit (INL 2))
(And
(Lit (INR 1))
(Lit (INR 3))))))”;
Theorem example3 =
EVAL “(boolExp_to_cnf
(Or
(Lit (INL 0))
(Or
(Lit (INL 2))
(Or
(Lit (INR 1))
(Lit (INR 3))))))”;
val _ = export_theory();