Repository navigation
Expand file tree
/
Copy pathdimacsScript.sml
More file actions
153 lines (135 loc) · 3.76 KB
/
Copy pathdimacsScript.sml
File metadata and controls
153 lines (135 loc) · 3.76 KB
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
(*
Syntax and semantics of CNF in the DIMACS format
*)
Theory dimacs
Ancestors
misc cnf syntax_helper input_lex mlstring mlint
Libs
preamble
(* This file provides a surface syntax and semantics that closely resembles
the meaning of CNF DIMACS files.
Clauses, literals and their semantics are inherited from cnf; the DIMACS
header, comment stripping and body engine from input_lex; literal
print/parse from syntax_helper. *)
(***
A parser and printer for CNF in CakeML
***)
Definition parse_cnf_toks_def:
parse_cnf_toks tokss = parse_dimacs_toks_gen parse_lits tokss
End
Definition parse_cnf_def:
parse_cnf strs =
let tokss = MAP toks strs in
case parse_cnf_toks tokss of
NONE => NONE
| SOME (nvars, nclauses, ls) => SOME ls
End
(* CNF printer *)
(* The largest variable occurring in cs *)
Definition max_cnf_def:
max_cnf cs = max_list 0 (MAP (max_list 0 o MAP var_lit) cs)
End
(* The header declares v as the variable count *)
Definition print_cnf_def:
print_cnf v cs =
print_header_line v (LENGTH cs) ::
MAP (print_lits #"\n") cs
End
(***
Round trip: parsing the printed formula returns it unchanged, when the
declared variable count bounds every variable
***)
Theorem max_cnf_clause:
MEM c cs ∧ MEM l c ⇒
var_lit l ≤ max_cnf cs
Proof
rw[max_cnf_def]>>
irule le_max_list>>
simp[MEM_MAP,PULL_EXISTS]>>
irule_at Any le_max_list>>
simp[MEM_MAP,PULL_EXISTS]>>
metis_tac[LESS_EQ_REFL]
QED
Theorem LIST_REL_parse_lits_print[local]:
EVERY (EVERY nz_lit) cs ∧
EVERY (EVERY (λl. var_lit l ≤ v)) cs ⇒
LIST_REL (λs c. parse_lits v s = SOME c)
(MAP toks (MAP (print_lits #"\n") cs)) cs
Proof
strip_tac>>
simp[LIST_REL_MAP1,LIST_REL_EL_EQN]>>
rw[]>>
DEP_REWRITE_TAC[parse_lits_print_lits]>>
gvs[EVERY_EL]
QED
Theorem parse_cnf_toks_print_cnf_toks:
EVERY (EVERY nz_lit) cs ∧
EVERY (EVERY (λl. var_lit l ≤ v)) cs
⇒
∃mv cl.
parse_cnf_toks (MAP toks (print_cnf v cs)) = SOME (mv,cl,cs)
Proof
strip_tac>>
simp[parse_cnf_toks_def,parse_dimacs_toks_gen_def,print_cnf_def]>>
qmatch_goalsub_abbrev_tac`print_header_line a b`>>
simp[Once toks_def]>>
assume_tac print_header_line_first>>fs[]>>
pop_assum sym_sub_tac>>
`tokenize «p» = INL «p»` by EVAL_TAC>>
simp[keep_line_def]>>
simp[GSYM toks_def,parse_header_line_print_header_line]>>
simp[FILTER_keep_line_print_lits]>>
simp[Abbr`b`]>>
qmatch_goalsub_abbrev_tac`parse_body_gen _ _ ss []`>>
`LIST_REL (λs c. parse_lits a s = SOME c) ss cs` by
simp[Abbr`ss`,LIST_REL_parse_lits_print]>>
drule parse_body_gen_LIST_REL>>
disch_then(qspec_then`[]` mp_tac)>>
simp[]
QED
Theorem parse_cnf_print_cnf:
EVERY (EVERY nz_lit) cs ∧
EVERY (EVERY (λl. var_lit l ≤ v)) cs
⇒
parse_cnf (print_cnf v cs) = SOME cs
Proof
rw[parse_cnf_def]>>
assume_tac parse_cnf_toks_print_cnf_toks>>
gvs[]
QED
(***
Everything the parser accepts uses non-zero literals
***)
Theorem parse_cnf_toks_nz_lit:
parse_cnf_toks tokss = SOME (v,n,cs) ⇒
EVERY (EVERY nz_lit) cs
Proof
strip_tac>>
gvs[parse_cnf_toks_def,parse_dimacs_toks_gen_def,AllCaseEqs()]>>
drule parse_body_gen_EVERY>>
disch_then match_mp_tac>>
simp[]>>
metis_tac[parse_lits_nz_lit]
QED
Theorem parse_cnf_nz_lit:
parse_cnf ls = SOME cs ⇒
EVERY (EVERY nz_lit) cs
Proof
strip_tac>>gvs[parse_cnf_def,AllCaseEqs()]>>
metis_tac[parse_cnf_toks_nz_lit]
QED
(***
Every literal the parser accepts is bounded by the declared variable count
***)
Theorem parse_cnf_toks_bound:
parse_cnf_toks tokss = SOME (v,n,cs) ⇒
EVERY (EVERY (λl. var_lit l ≤ v)) cs
Proof
strip_tac>>
gvs[parse_cnf_toks_def,parse_dimacs_toks_gen_def,AllCaseEqs()]>>
drule parse_body_gen_EVERY>>
disch_then match_mp_tac>>
simp[]>>
rw[]>>
gvs[parse_lits_def,AllCaseEqs(),check_maxvar_def]
QED