-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathlpr_arrayPackingProgScript.sml
212 lines (185 loc) · 6.37 KB
/
lpr_arrayPackingProgScript.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
(*
This builds a proof checker specialized to the
packing chromatic number bounds
*)
open preamble basis lpr_composeProgTheory UnsafeProofTheory lprTheory lpr_listTheory lpr_parsingTheory HashtableProofTheory lpr_arrayProgTheory lpr_arrayParsingProgTheory packingTheory;
val _ = new_theory "lpr_arrayPackingProg"
val _ = temp_delsimps ["NORMEQ_CONV"] (*"*)
val _ = diminish_srw_ss ["ABBREV"]
val _ = set_trace "BasicProvers.var_eq_old" 1
val _ = translation_extends"lpr_arrayParsingProg";
val xlet_autop = xlet_auto >- (TRY( xcon) >> xsimpl)
val usage_string = ‘
Input: <r = radius> <k = total colors> <c = center color, 1 ≤ c ≤ k>
Outputs a direct encoding CNF whose unsatisfiability implies a lower bound of k+1 for the packing chromatic number of the plane.
’
fun drop_until p [] = []
| drop_until p (x::xs) = if p x then x::xs else drop_until p xs;
val usage_string_tm =
usage_string |> hd |> (fn QUOTE s => s) |> explode
|> drop_until (fn c => c = #"\n") |> tl |> implode
|> stringSyntax.fromMLstring;
Definition usage_string_def:
usage_string = strlit ^usage_string_tm
End
val r = translate usage_string_def;
(* Make sure the inputs are valid *)
Definition parse_numbers_def:
parse_numbers r k c =
case (fromNatString r,fromNatString k,fromNatString c) of
(SOME r, SOME k, SOME c) =>
if 1 ≤ c ∧ c ≤ k then SOME (r,k,c) else NONE
| _ => NONE
End
val r = translate parse_numbers_def;
(* NOTE: still written in the old style since this doesn't actually use the UNSAT checker *)
(* 3 arg *)
val check_unsat_3 = (append_prog o process_topdecs) `
fun check_unsat_3 enc r k c =
case parse_numbers r k c of
Some (r,(k,c)) =>
TextIO.print_list (print_dimacs (enc r k c))
| None =>
TextIO.output TextIO.stdErr usage_string`
Definition check_unsat_3_sem_def:
check_unsat_3_sem fs enc r k c err =
case parse_numbers r k c of
SOME (r,k,c) =>
add_stdout fs (concat (print_dimacs (enc r k c)))
| NONE => add_stderr fs err
End
Theorem check_unsat_3_spec:
STRING_TYPE r rv ∧
STRING_TYPE k kv ∧
STRING_TYPE c cv ∧
(NUM --> NUM --> NUM -->
LIST_TYPE (LIST_TYPE INT)) enc encv
⇒
app (p:'ffi ffi_proj) ^(fetch_v"check_unsat_3"(get_ml_prog_state()))
[encv; rv; kv; cv]
(STDIO fs)
(POSTv uv. &UNIT_TYPE () uv *
SEP_EXISTS err. STDIO (check_unsat_3_sem fs enc r k c err))
Proof
rw[]>>
xcf "check_unsat_3" (get_ml_prog_state ())>>
rpt xlet_autop>>
Cases_on`parse_numbers r k c`>>fs[OPTION_TYPE_def]
>- (
xmatch>>
xapp_spec output_stderr_spec \\ xsimpl>>
qexists_tac`emp`>>
qexists_tac `usage_string` >> simp [theorem "usage_string_v_thm"] >>
qexists_tac`fs`>>xsimpl>>
simp[check_unsat_3_sem_def]>>
rw[]>>qexists_tac`usage_string`>>xsimpl)>>
PairCases_on`x`>>fs[PAIR_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
xapp_spec print_list_spec>>xsimpl>>
asm_exists_tac>>xsimpl>>
simp[check_unsat_3_sem_def]>>
qexists_tac`emp`>>qexists_tac`fs`>>xsimpl
QED
val res = translate balanced_mapTheory.lookup_def;
val res = translate balanced_mapTheory.singleton_def;
val res = translate balanced_mapTheory.ratio_def;
val res = translate balanced_mapTheory.size_def;
val res = translate balanced_mapTheory.delta_def;
val res = translate balanced_mapTheory.balanceL_def;
val res = translate balanced_mapTheory.balanceR_def;
val res = translate balanced_mapTheory.insert_def;
val res = translate balanced_mapTheory.empty_def;
val res = translate remap_var_def;
val res = translate remap_lit_def;
val res = translate remap_clause_def;
val res = translate remap_fml_def;
val res = translate cmp_pair_def;
val res = translate cmp_num_def;
val res = translate cmp_int_def;
val res = translate cmp_nii_def;
val res = translate cmp_nii_def;
val res = translate remap_nii_def;
val res = translate packingTheory.fix_col_def;
val res = translate packingTheory.in_ball_def;
val res = translate packingTheory.vertices_def;
val res = translate packingTheory.fix_cols_def;
val res = translate (packingTheory.restrict_col_def |> REWRITE_RULE [GSYM sub_check_def]);
val res = translate packingTheory.restrict_cols_def;
val res = translate packingTheory.full_restrict_def;
val res = translate packingTheory.encode_def;
val res = translate full_encode_def;
val main = (append_prog o process_topdecs) `
fun main u =
case CommandLine.arguments () of
[r,k,c] => check_unsat_3 full_encode r k c
| _ => TextIO.output TextIO.stdErr usage_string`
Definition main_sem_def:
main_sem cl fs err =
case TL cl of
[r;k;c] => check_unsat_3_sem fs full_encode r k c err
| _ => add_stderr fs err
End
Theorem main_spec:
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"main"(get_ml_prog_state()))
[Conv NONE []]
(COMMANDLINE cl * STDIO fs)
(POSTv uv. &UNIT_TYPE () uv *
COMMANDLINE cl * SEP_EXISTS err. STDIO (main_sem cl fs err))
Proof
rw[]>>
xcf"main"(get_ml_prog_state())>>
reverse(Cases_on`wfcl cl`) >- (fs[COMMANDLINE_def] \\ xpull)>>
rpt xlet_autop >>
Cases_on `cl` >- fs[wfcl_def] >>
simp[main_sem_def]>>
every_case_tac>>fs[LIST_TYPE_def]>>xmatch>>
qmatch_asmsub_abbrev_tac`wfcl cl` >>
TRY (
xapp_spec output_stderr_spec \\ xsimpl>>
qexists_tac`COMMANDLINE cl`>>xsimpl>>
qexists_tac `usage_string` >> simp [theorem "usage_string_v_thm"] >>
qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`usage_string`>>xsimpl)
>- (
xapp>>xsimpl>>
qexists_tac`COMMANDLINE cl`>>xsimpl>>
rpt(asm_exists_tac>> simp[])>>
qexists_tac`fs`>>
qexists_tac`full_encode`>>simp [theorem "full_encode_v_thm"] >>
rw[]>>xsimpl>>
qexists_tac`x`>>xsimpl)
QED
Theorem main_whole_prog_spec2:
hasFreeFD fs ⇒
whole_prog_spec2 main_v cl fs NONE (λfs'. ∃err. fs' = main_sem cl fs err)
Proof
rw[basis_ffiTheory.whole_prog_spec2_def]
\\ match_mp_tac (MP_CANON (DISCH_ALL (MATCH_MP app_wgframe (UNDISCH main_spec))))
\\ xsimpl
\\ rw[PULL_EXISTS]
\\ qexists_tac`main_sem cl fs x`
\\ qexists_tac`x`
\\ xsimpl
\\ rw[main_sem_def,check_unsat_3_sem_def]
\\ every_case_tac
\\ simp[GSYM add_stdo_with_numchars,with_same_numchars]
QED
local
val name = "main"
val (sem_thm,prog_tm) =
whole_prog_thm (get_ml_prog_state()) name (UNDISCH main_whole_prog_spec2)
Definition main_prog_def:
main_prog = ^prog_tm
End
in
Theorem main_semantics =
sem_thm
|> REWRITE_RULE[GSYM main_prog_def]
|> DISCH_ALL
|> SIMP_RULE(srw_ss())[GSYM CONJ_ASSOC,AND_IMP_INTRO];
end
val _ = export_theory();