-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcliqueScript.sml
387 lines (359 loc) · 9.16 KB
/
cliqueScript.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
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
(*
Formalization of the max clique problem
*)
open preamble pbcTheory graph_basicTheory pbc_normaliseTheory;
val _ = new_theory "clique";
Definition is_clique_def:
is_clique vs (v,e) ⇔
vs ⊆ count v ∧
(∀x y. x ∈ vs ∧ y ∈ vs ∧ x ≠ y ⇒
is_edge e x y)
End
Definition max_clique_size_def:
max_clique_size g =
MAX_SET ({CARD vs | is_clique vs g})
End
Theorem CARD_clique_bound:
is_clique vs g ⇒
CARD vs ≤ FST g
Proof
Cases_on`g`>>rw[is_clique_def]>>
(drule_at Any) CARD_SUBSET>>
simp[]
QED
Definition mk_constraint_def:
mk_constraint e x y =
if y ≤ x ∨ is_edge e x y then []
else
[(GreaterEqual,[(1,Neg x);(1,Neg y)], 1):num pbc]
End
(* Encoding *)
Definition encode_def:
encode (v,e) =
FLAT (GENLIST (λx.
FLAT (GENLIST (λy.
mk_constraint e x y) v)) v):num pbc list
End
(* Objective is to minimize the number of negated variables *)
Definition clique_obj_def:
clique_obj v =
SOME((GENLIST (λb. (1, Neg b)) v), 0)
: ((num lin_term # int) option)
End
Theorem iSUM_SNOC:
∀ls.
iSUM (SNOC x ls) = x + iSUM ls
Proof
Induct>>rw[iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem iSUM_GENLIST_inter:
iSUM (GENLIST (λb. b2i (b ∉ w)) v) =
iSUM (GENLIST (λb. b2i (b ∉ (w ∩ count v))) v)
Proof
AP_TERM_TAC>>
fs[GENLIST_FUN_EQ]
QED
Theorem iSUM_GENLIST_eq_k:
∀vp vs.
vs ⊆ count vp ⇒
iSUM (GENLIST (λb. b2i (b ∉ vs)) vp) = &vp - &CARD vs
Proof
Induct>>rw[iSUM_def]>>
reverse (Cases_on`vp ∈ vs`>>fs[GENLIST,iSUM_SNOC])
>- (
first_x_assum(qspec_then`vs` mp_tac)>>
impl_tac>- (
fs[SUBSET_DEF]>>
rw[]>>
first_x_assum drule>>fs[prim_recTheory.LESS_THM]>>
metis_tac[])>>
rw[]>>
intLib.ARITH_TAC)>>
first_x_assum(qspecl_then[`vs DIFF{vp}`] mp_tac)>>
impl_tac>- (
fs[SUBSET_DEF]>>rw[]>>
first_x_assum drule>>fs[prim_recTheory.LESS_THM])>>
rw[]>>
`(GENLIST (λb. b2i (b ∉ vs ∨ b = vp)) vp) =
(GENLIST (λb. b2i (b ∉ vs)) vp)` by
(match_mp_tac GENLIST_CONG>>fs[])>>
gvs[] >>
`FINITE vs` by (
match_mp_tac SUBSET_FINITE_I>>
qexists_tac`count (SUC vp)`>>
fs[SUBSET_DEF])>>
`CARD (vs DIFF {vp}) = CARD vs - 1` by
(DEP_REWRITE_TAC[CARD_DIFF]>>
`vs ∩ {vp} = {vp}` by
(simp[EXTENSION]>>metis_tac[])>>
simp[])>>
rw[]>>
`CARD vs > 0` by
(Cases_on`vs`>>rw[]>>gvs[EXTENSION])>>
Cases_on`CARD vs`>>fs[]>>
intLib.ARITH_TAC
QED
Theorem MEM_if:
MEM x (if P then A else B) ⇔
if P then MEM x A else MEM x B
Proof
rw[]
QED
Theorem b2i_rewrite[simp]:
1 − b2i (vs b) = b2i (b ∉ vs)
Proof
Cases_on`vs b`>>fs[IN_APP]
QED
Theorem encode_correct:
good_graph (v,e) ∧
encode (v,e) = constraints ⇒
(
(∃vs.
is_clique vs (v,e) ∧
CARD vs = k) ⇔
(∃w.
pbc$satisfies w (set constraints) ∧
eval_obj (clique_obj v) w = &v - &k)
)
Proof
rw[EQ_IMP_THM]
>- (
qexists_tac`vs`>>
rw[encode_def]
>- (
simp[satisfies_def,MEM_FLAT,MEM_GENLIST,PULL_EXISTS,mk_constraint_def]>>
rw[]>>
gvs[satisfies_pbc_def,eval_lin_term_def,is_clique_def,iSUM_def]>>
`¬(x ∈ vs ∧ y ∈ vs)` by
(CCONTR_TAC>>fs[]>>
first_x_assum(qspecl_then[`x`,`y`] mp_tac)>>
gvs[])>>
fs[]
>- (Cases_on`y ∈ vs`>>fs[])
>- (Cases_on`x ∈ vs`>>fs[]))
>- (
simp[eval_obj_def,clique_obj_def,eval_lin_term_def,MAP_GENLIST,o_DEF]>>
simp[]>>
MATCH_MP_TAC iSUM_GENLIST_eq_k>>
fs[is_clique_def]))
>- (
qexists_tac`w ∩ count v`>>
rw[is_clique_def]
>- (
fs[satisfies_def,encode_def,MEM_FLAT,PULL_EXISTS,MEM_GENLIST,mk_constraint_def]>>
wlog_tac `x < y` [`x`,`y`]
>- (
first_x_assum(qspecl_then[`y`,`x`] mp_tac)>>
fs[good_graph_def,is_edge_thm])>>
fs[MEM_if]>>
CCONTR_TAC>>
res_tac>>fs[satisfies_pbc_def,eval_lin_term_def,IN_APP]>>
ntac 2 (last_x_assum kall_tac)>>
rfs[iSUM_def])>>
pop_assum mp_tac>>
simp[eval_obj_def,clique_obj_def,eval_lin_term_def,MAP_GENLIST,o_DEF]>>
REWRITE_TAC [Once iSUM_GENLIST_inter]>>
DEP_REWRITE_TAC [iSUM_GENLIST_eq_k]>>
simp[]>>
intLib.ARITH_TAC)
QED
(* Encode the variables as strings
and normalize to ≥ only *)
Definition enc_string_def:
(enc_string x =
concat [strlit"x";toString (x+1)])
End
Theorem enc_string_INJ:
INJ enc_string UNIV UNIV
Proof
rw [INJ_DEF]
\\ fs [enc_string_def]
\\ fs [mlstringTheory.concat_def]
\\ every_case_tac \\ gvs []
\\ pop_assum sym_sub_tac
\\ fs [mlintTheory.num_to_str_11]
QED
Definition full_encode_def:
full_encode g =
(map_obj enc_string
(clique_obj (FST g)),
MAP (map_pbc enc_string) (encode g))
End
(* Convert minimization to maximization and add default 0 lb *)
Definition conv_concl_def:
(conv_concl n (OBounds lbi ubi) =
let ubg =
case lbi of NONE => 0 (* Dummy impossible value *)
| SOME lb =>
if lb ≤ 0 then n else n - Num lb in
let lbg =
case ubi of NONE => (0:num)
| SOME ub => (n - Num (ABS ub)) in
SOME (lbg,ubg)) ∧
(conv_concl _ _ = NONE)
End
Theorem is_clique_exists:
is_clique {} g
Proof
Cases_on`g`>>
simp[is_clique_def]
QED
Theorem is_clique_CARD:
is_clique vs g ⇒
CARD vs ≤ FST g
Proof
Cases_on`g`>>
rw[is_clique_def]>>
`FINITE (count q)` by
fs[]>>
drule CARD_SUBSET>>
disch_then drule>>
simp[]
QED
Theorem b2i_geq_zero[simp]:
b2i b ≥ 0
Proof
Cases_on`b`>>
simp[]
QED
Theorem iSUM_zero:
(∀x. MEM x ls ⇒ x ≥ 0) ⇒
iSUM ls ≥ 0
Proof
Induct_on`ls`>> rw[iSUM_def]>>
fs[]>>
first_x_assum(qspec_then`h` assume_tac)>>
fs[]>>
intLib.ARITH_TAC
QED
Theorem iSUM_sub_b2i_geq_0':
(∀x. MEM x ls ⇒ ∃y. x = 1 - b2i y) ⇒
iSUM ls ≤ &(LENGTH ls)
Proof
Induct_on`ls`>>fs[iSUM_def]>>rw[]>>
first_assum(qspec_then`h` assume_tac)>>fs[]>>
Cases_on`y`>>fs[ADD1]>>
intLib.ARITH_TAC
QED
Theorem eval_obj_clique_obj_bounds:
eval_obj (clique_obj q) w ≥ 0 ∧
eval_obj (clique_obj q) w ≤ &q
Proof
fs[clique_obj_def,eval_obj_def,eval_lin_term_def]>>
CONJ_ASM1_TAC
>- (
match_mp_tac iSUM_zero>>
simp[MEM_MAP,MEM_GENLIST,PULL_EXISTS])>>
qmatch_goalsub_abbrev_tac`iSUM ls`>>
`q = LENGTH ls` by
fs[Abbr`ls`]>>
rw[]>>
DEP_REWRITE_TAC[iSUM_sub_b2i_geq_0']>>
simp[Abbr`ls`,MEM_MAP,MEM_GENLIST,PULL_EXISTS]>>rw[]>>
qexists_tac`b ∈ w`>>
Cases_on` b ∈ w`>>fs[IN_APP]
QED
Theorem full_encode_sem_concl:
good_graph g ∧
full_encode g = (obj,pbf) ∧
sem_concl (set pbf) obj concl ∧
conv_concl (FST g) concl = SOME (lbg, ubg) ⇒
(∀vs. is_clique vs g ⇒ CARD vs ≤ ubg) ∧
(∃vs. is_clique vs g ∧ lbg ≤ CARD vs)
Proof
strip_tac>>
gvs[full_encode_def]>>
qpat_x_assum`sem_concl _ _ _` mp_tac>>
simp[LIST_TO_SET_MAP]>>
DEP_REWRITE_TAC[GSYM concl_INJ_iff]>>
CONJ_TAC >- (
assume_tac enc_string_INJ>>
drule INJ_SUBSET>>
disch_then match_mp_tac>>
simp[])>>
Cases_on`concl`>>fs[conv_concl_def]>>
rename1`OBounds lbi ubi`>>
simp[sem_concl_def]>>
Cases_on`g`>>
drule encode_correct>>
rw[]
>- ( (* Lower bound optimization *)
Cases_on`lbi`>>fs[unsatisfiable_def,satisfiable_def]
>- (
(* the formula is always satisfiable, so INF lower bound
is impossible *)
`F` by
metis_tac[is_clique_exists])>>
fs[SF DNF_ss,EQ_IMP_THM]>>
first_x_assum drule>> strip_tac>>
drule is_clique_CARD>>simp[]>>rw[]>>
first_x_assum drule_all>>rw[]>>
intLib.ARITH_TAC)>>
(* Upper bound optimization *)
Cases_on`ubi`>>
fs[SF DNF_ss,EQ_IMP_THM]
>-
metis_tac[is_clique_exists]>>
`eval_obj (clique_obj q) w ≥ 0` by
(fs[clique_obj_def,eval_obj_def,eval_lin_term_def]>>
match_mp_tac iSUM_zero>>
simp[MEM_MAP,MEM_GENLIST,PULL_EXISTS])>>
first_x_assum drule>>
disch_then(qspec_then`q - Num (eval_obj (clique_obj q) w)` mp_tac)>>
impl_tac >- (
DEP_REWRITE_TAC[GSYM integerTheory.INT_SUB]>>
mp_tac eval_obj_clique_obj_bounds>>
intLib.ARITH_TAC)>>
rw[]>>
asm_exists_tac>>simp[]>>
intLib.ARITH_TAC
QED
Theorem MAX_SET_eq_intro:
FINITE s ∧
(∀x. x ∈ s ⇒ x ≤ n) ∧
n ∈ s ⇒
MAX_SET s = n
Proof
rw[]>>
DEEP_INTRO_TAC MAX_SET_ELIM>>
simp[]>>
rw[]>>
fs[]>>
res_tac>>fs[]
QED
Theorem full_encode_sem_concl_check:
good_graph g ∧
full_encode g = (obj,pbf) ∧
sem_concl (set pbf) obj concl ∧
conv_concl (FST g) concl = SOME (mc,mc) ⇒
max_clique_size g = mc
Proof
rw[]>>
drule_all full_encode_sem_concl>>
simp[max_clique_size_def]>>
rw[]>>
match_mp_tac MAX_SET_eq_intro>>
CONJ_TAC >- (
`FINITE (count (FST g + 1))` by fs[]>>
drule_then match_mp_tac SUBSET_FINITE>>
rw[SUBSET_DEF]>>
drule CARD_clique_bound>>
simp[])>>
rw[]
>-
metis_tac[]>>
first_assum drule>>
strip_tac>>
first_x_assum (irule_at Any)>>
fs[]
QED
Theorem full_encode_eq =
full_encode_def
|> SIMP_RULE (srw_ss()) [FORALL_PROD,encode_def]
|> SIMP_RULE (srw_ss()) [mk_constraint_def]
|> SIMP_RULE (srw_ss()) [MAP_FLAT,MAP_GENLIST,MAP_APPEND,o_DEF,MAP_MAP_o,pbc_ge_def,map_pbc_def,FLAT_FLAT,FLAT_MAP_SING,map_lit_def,MAP_if]
|> SIMP_RULE (srw_ss()) [FLAT_GENLIST_FOLDN,FOLDN_APPEND_op]
|> PURE_ONCE_REWRITE_RULE [APPEND_OP_DEF]
|> SIMP_RULE (srw_ss()) [if_APPEND];
val _ = export_theory();