|
| 1 | +/- |
| 2 | +Copyright (c) 2024 Lean FRO, LLC. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Scott Morrison |
| 5 | +-/ |
| 6 | +prelude |
| 7 | +import Init.Data.List.Lemmas |
| 8 | +import Std.Sat.CNF.Literal |
| 9 | + |
| 10 | +namespace Std |
| 11 | +namespace Sat |
| 12 | + |
| 13 | +/-- |
| 14 | +A clause in a CNF. |
| 15 | +
|
| 16 | +The literal `(i, b)` is satisfied is the assignment to `i` agrees with `b`. |
| 17 | +-/ |
| 18 | +abbrev CNF.Clause (α : Type u) : Type u := List (Literal α) |
| 19 | + |
| 20 | +/-- |
| 21 | +A CNF formula. |
| 22 | +
|
| 23 | +Literals are identified by members of `α`. |
| 24 | +-/ |
| 25 | +abbrev CNF (α : Type u) : Type u := List (CNF.Clause α) |
| 26 | + |
| 27 | +namespace CNF |
| 28 | + |
| 29 | +/-- |
| 30 | +Evaluating a `Clause` with respect to an assignment `f`. |
| 31 | +-/ |
| 32 | +def Clause.eval (f : α → Bool) (c : Clause α) : Bool := c.any fun (i, n) => f i == n |
| 33 | + |
| 34 | +@[simp] theorem Clause.eval_nil (f : α → Bool) : Clause.eval f [] = false := rfl |
| 35 | +@[simp] theorem Clause.eval_succ (f : α → Bool) : |
| 36 | + Clause.eval f (i :: c) = (f i.1 == i.2 || Clause.eval f c) := rfl |
| 37 | + |
| 38 | +/-- |
| 39 | +Evaluating a `CNF` formula with respect to an assignment `f`. |
| 40 | +-/ |
| 41 | +def eval (f : α → Bool) (g : CNF α) : Bool := g.all fun c => c.eval f |
| 42 | + |
| 43 | +@[simp] theorem eval_nil (f : α → Bool) : eval f [] = true := rfl |
| 44 | +@[simp] theorem eval_succ (f : α → Bool) : eval f (c :: g) = (c.eval f && eval f g) := rfl |
| 45 | + |
| 46 | +@[simp] theorem eval_append (f : α → Bool) (g h : CNF α) : |
| 47 | + eval f (g ++ h) = (eval f g && eval f h) := List.all_append |
| 48 | + |
| 49 | +instance : HSat α (Clause α) where |
| 50 | + eval assign clause := Clause.eval assign clause |
| 51 | + |
| 52 | +instance : HSat α (CNF α) where |
| 53 | + eval assign cnf := eval assign cnf |
| 54 | + |
| 55 | +@[simp] theorem unsat_nil_iff_false : unsatisfiable α ([] : CNF α) ↔ False := |
| 56 | + ⟨fun h => by simp [unsatisfiable, (· ⊨ ·)] at h, by simp⟩ |
| 57 | + |
| 58 | +@[simp] theorem sat_nil {assign : α → Bool} : assign ⊨ ([] : CNF α) ↔ True := by |
| 59 | + simp [(· ⊨ ·)] |
| 60 | + |
| 61 | +@[simp] theorem unsat_nil_cons {g : CNF α} : unsatisfiable α ([] :: g) ↔ True := by |
| 62 | + simp [unsatisfiable, (· ⊨ ·)] |
| 63 | + |
| 64 | +namespace Clause |
| 65 | + |
| 66 | +/-- |
| 67 | +Literal `a` occurs in `Clause` `c`. |
| 68 | +-/ |
| 69 | +def mem (a : α) (c : Clause α) : Prop := (a, false) ∈ c ∨ (a, true) ∈ c |
| 70 | + |
| 71 | +instance {a : α} {c : Clause α} [DecidableEq α] : Decidable (mem a c) := |
| 72 | + inferInstanceAs <| Decidable (_ ∨ _) |
| 73 | + |
| 74 | +@[simp] theorem not_mem_nil {a : α} : mem a ([] : Clause α) ↔ False := by simp [mem] |
| 75 | +@[simp] theorem mem_cons {a : α} : mem a (i :: c : Clause α) ↔ (a = i.1 ∨ mem a c) := by |
| 76 | + rcases i with ⟨b, (_|_)⟩ |
| 77 | + · simp [mem, or_assoc] |
| 78 | + · simp [mem] |
| 79 | + rw [or_left_comm] |
| 80 | + |
| 81 | +theorem mem_of (h : (a, b) ∈ c) : mem a c := by |
| 82 | + cases b |
| 83 | + · left; exact h |
| 84 | + · right; exact h |
| 85 | + |
| 86 | +theorem eval_congr (f g : α → Bool) (c : Clause α) (w : ∀ i, mem i c → f i = g i) : |
| 87 | + eval f c = eval g c := by |
| 88 | + induction c |
| 89 | + case nil => rfl |
| 90 | + case cons i c ih => |
| 91 | + simp only [eval_succ] |
| 92 | + rw [ih, w] |
| 93 | + · rcases i with ⟨b, (_|_)⟩ <;> simp [mem] |
| 94 | + · intro j h |
| 95 | + apply w |
| 96 | + rcases h with h | h |
| 97 | + · left |
| 98 | + apply List.mem_cons_of_mem _ h |
| 99 | + · right |
| 100 | + apply List.mem_cons_of_mem _ h |
| 101 | + |
| 102 | +end Clause |
| 103 | + |
| 104 | +/-- |
| 105 | +Literal `a` occurs in `CNF` formula `g`. |
| 106 | +-/ |
| 107 | +def mem (a : α) (g : CNF α) : Prop := ∃ c, c ∈ g ∧ c.mem a |
| 108 | + |
| 109 | +instance {a : α} {g : CNF α} [DecidableEq α] : Decidable (mem a g) := |
| 110 | + inferInstanceAs <| Decidable (∃ _, _) |
| 111 | + |
| 112 | +theorem any_nonEmpty_iff_exists_mem {g : CNF α} : |
| 113 | + (List.any g fun c => !List.isEmpty c) = true ↔ ∃ a, mem a g := by |
| 114 | + simp only [List.any_eq_true, Bool.not_eq_true', List.isEmpty_false_iff_exists_mem, mem, |
| 115 | + Clause.mem] |
| 116 | + constructor |
| 117 | + . intro h |
| 118 | + rcases h with ⟨clause, ⟨hclause1, hclause2⟩⟩ |
| 119 | + rcases hclause2 with ⟨lit, hlit⟩ |
| 120 | + exists lit.fst, clause |
| 121 | + constructor |
| 122 | + . assumption |
| 123 | + . rcases lit with ⟨_, ⟨_ | _⟩⟩ <;> simp_all |
| 124 | + . intro h |
| 125 | + rcases h with ⟨lit, clause, ⟨hclause1, hclause2⟩⟩ |
| 126 | + exists clause |
| 127 | + constructor |
| 128 | + . assumption |
| 129 | + . cases hclause2 with |
| 130 | + | inl hl => exact Exists.intro _ hl |
| 131 | + | inr hr => exact Exists.intro _ hr |
| 132 | + |
| 133 | +@[simp] theorem not_mem_cons : (¬ ∃ a, mem a g) ↔ ∃ n, g = List.replicate n [] := by |
| 134 | + simp only [← any_nonEmpty_iff_exists_mem] |
| 135 | + simp only [List.any_eq_true, Bool.not_eq_true', not_exists, not_and, Bool.not_eq_false] |
| 136 | + induction g with |
| 137 | + | nil => |
| 138 | + simp only [List.not_mem_nil, List.isEmpty_iff, false_implies, forall_const, true_iff] |
| 139 | + exact ⟨0, rfl⟩ |
| 140 | + | cons c g ih => |
| 141 | + simp_all [ih, List.isEmpty_iff] |
| 142 | + constructor |
| 143 | + · rintro ⟨rfl, n, rfl⟩ |
| 144 | + exact ⟨n+1, rfl⟩ |
| 145 | + · rintro ⟨n, h⟩ |
| 146 | + cases n |
| 147 | + · simp at h |
| 148 | + · simp_all only [List.replicate, List.cons.injEq, true_and] |
| 149 | + exact ⟨_, rfl⟩ |
| 150 | + |
| 151 | +instance {g : CNF α} [DecidableEq α] : Decidable (∃ a, mem a g) := |
| 152 | + decidable_of_iff (g.any fun c => !c.isEmpty) any_nonEmpty_iff_exists_mem |
| 153 | + |
| 154 | +@[simp] theorem not_mem_nil {a : α} : mem a ([] : CNF α) ↔ False := by simp [mem] |
| 155 | +@[simp] theorem mem_cons {a : α} {i} {c : CNF α} : |
| 156 | + mem a (i :: c : CNF α) ↔ (Clause.mem a i ∨ mem a c) := by simp [mem] |
| 157 | + |
| 158 | +theorem mem_of (h : c ∈ g) (w : Clause.mem a c) : mem a g := by |
| 159 | + apply Exists.intro c |
| 160 | + constructor <;> assumption |
| 161 | + |
| 162 | +@[simp] theorem mem_append {a : α} {x y : CNF α} : mem a (x ++ y) ↔ mem a x ∨ mem a y := by |
| 163 | + simp [mem, List.mem_append] |
| 164 | + constructor |
| 165 | + · rintro ⟨c, (mx | my), mc⟩ |
| 166 | + · left |
| 167 | + exact ⟨c, mx, mc⟩ |
| 168 | + · right |
| 169 | + exact ⟨c, my, mc⟩ |
| 170 | + · rintro (⟨c, mx, mc⟩ | ⟨c, my, mc⟩) |
| 171 | + · exact ⟨c, Or.inl mx, mc⟩ |
| 172 | + · exact ⟨c, Or.inr my, mc⟩ |
| 173 | + |
| 174 | +theorem eval_congr (f g : α → Bool) (x : CNF α) (w : ∀ i, mem i x → f i = g i) : |
| 175 | + eval f x = eval g x := by |
| 176 | + induction x |
| 177 | + case nil => rfl |
| 178 | + case cons c x ih => |
| 179 | + simp only [eval_succ] |
| 180 | + rw [ih, Clause.eval_congr] <;> |
| 181 | + · intro i h |
| 182 | + apply w |
| 183 | + simp [h] |
| 184 | + |
| 185 | +end CNF |
| 186 | + |
| 187 | +end Sat |
| 188 | +end Std |
0 commit comments