Theory CertCheck_CNF_XOR
section ‹ ApproxMC certification for CNF-XOR ›
text ‹
This concretely instantiates the locales with a syntax and
semantics for CNF-XOR, giving us a certificate checker for
approximate counting in this theory.
›
theory CertCheck_CNF_XOR imports
ApproxMCAnalysis
CertCheck
HOL.String "HOL-Library.Code_Target_Numeral"
Show.Show_Real
begin
text ‹
This follows CryptoMiniSAT's CNF-XOR formula syntax.
A clause is a list of literals (one of which must be satisfied).
An XOR constraint has the form $l_1 + l_2 + \dots + l_n = 1$ where addition is taken over $F_2$.
Syntactically, they are specified by the list of LHS literals.
Variables are natural numbers (in practice, variable 0 is never used)
›
datatype lit = Pos nat | Neg nat
type_synonym clause = "lit list"
type_synonym cmsxor = "lit list"
type_synonym fml = "clause list × cmsxor list"
type_synonym assignment = "nat ⇒ bool"
definition sat_lit :: "assignment ⇒ lit ⇒ bool" where
"sat_lit w l = (case l of Pos x ⇒ w x | Neg x ⇒ ¬w x)"
definition sat_clause :: "assignment ⇒ clause ⇒ bool" where
"sat_clause w C = (∃l ∈ set C. sat_lit w l)"
definition sat_cmsxor :: "assignment ⇒ cmsxor ⇒ bool" where
"sat_cmsxor w C = odd ((sum_list (map (of_bool ∘ (sat_lit w)) C))::nat)"
definition sat_fml :: "assignment ⇒ fml ⇒ bool"
where
"sat_fml w f = (
(∀C ∈ set (fst f). sat_clause w C) ∧
(∀C ∈ set (snd f). sat_cmsxor w C))"
definition sols :: "fml ⇒ assignment set"
where "sols f = {w. sat_fml w f}"
lemma sat_fml_cons[simp]:
shows
"sat_fml w (FC, x # FX) ⟷
sat_fml w (FC,FX) ∧ sat_cmsxor w x"
"sat_fml w (c # FC, FX) ⟷
sat_fml w (FC,FX) ∧ sat_clause w c"
unfolding sat_fml_def by auto
fun enc_xor :: "nat xor ⇒ fml ⇒ fml"
where
"enc_xor (x,b) (FC,FX) = (
if b then (FC, map Pos x # FX)
else
case x of
[] ⇒ (FC,FX)
| (v#vs) ⇒ (FC, (Neg v # map Pos vs) # FX))"
lemma sols_enc_xor:
shows "sols (enc_xor (x,b) (FC,FX)) =
sols (FC,FX) ∩ {ω. satisfies_xorL (x,b) ω}"
unfolding sols_def
by (cases x; auto simp add: satisfies_xorL_def sat_cmsxor_def o_def sat_lit_def list.case_eq_if)
definition check_sol :: "fml ⇒ (nat ⇒ bool) ⇒ bool"
where "check_sol fml w = (
list_all (list_ex (sat_lit w)) (fst fml) ∧
list_all (sat_cmsxor w) (snd fml))"
definition ban_sol :: "(nat × bool) list ⇒ fml ⇒ fml"
where "ban_sol vs fml =
((map (λ(v,b). if b then Neg v else Pos v) vs)#fst fml, snd fml)"
lemma check_sol_sol:
shows "w ∈ sols F ⟷
check_sol F w"
unfolding check_sol_def sols_def sat_fml_def
apply clarsimp
by (metis Ball_set_list_all Bex_set_list_ex sat_clause_def)
lemma ban_sat_clause:
shows "sat_clause w (map (λ(v, b). if b then Neg v else Pos v) vs) ⟷
map w (map fst vs) ≠ map snd vs"
unfolding sat_clause_def
by (force simp add: sat_lit_def split: if_splits)
lemma sols_ban_sol:
shows"sols (ban_sol vs F) =
sols F ∩
{ω. map ω (map fst vs) ≠ map snd vs}"
unfolding ban_sol_def sols_def
by (auto simp add: ban_sat_clause)