File size: 10,548 Bytes
e639140 | 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 | /-
Copyright (c) 2026 Anthropic, PBC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
SPDX-License-Identifier: Apache-2.0
-/
module
public import MainTheoremFormsStatement
public import VariableBound.Paper.Rename.Defs
import Mathlib.SetTheory.Cardinal.Finite
import Mathlib.Data.Fintype.Pi
import Mathlib.Data.List.Dedup
import Mathlib.Logic.Equiv.Prod
/-! # Renaming the variables that occur: the mathematics
For a formula φ over x₀ … x_(N−1) in which n variables occur, `ren φ` is φ with every variable
replaced by its position in the list `occL` of the variables that occur: a formula over x₀ …
x_(n−1).
* `toN_ren`: with natural numbers as variables, it is what the program `renL` computes.
* `isKCNF_ren`, `allOccur_ren`, `length_ren`: it is a k-CNF if φ is, all its variables occur, and it
has as many clauses as φ.
* `numSat_ren`: its number of satisfying assignments is the number of satisfying assignments of φ,
as assignments of the variables that occur.
* `numSat_eq`: the sentence after Corollary 11, numSat φ = 2^(N−n)·numSatOcc φ. `allOccurStmt`: if
all variables occur, the two ways of counting agree. -/
public section
namespace VariableBound.Rename
open Groundwork Groundwork.Prog Groundwork.Decode PaddedDescent.F ClauseBound.Alg MainTheoremForms
open Sat3 (CNF)
/-! ### The list of the variables that occur -/
/-- A number is in `occL ψ` if and only if it is the variable of a literal of a clause of ψ. -/
theorem mem_occL {ψ : Fml} {v : ℕ} : v ∈ occL ψ ↔ ∃ C ∈ ψ, ∃ l ∈ C, l.1 = v := by
simp only [occL, List.mem_dedup, List.mem_flatMap, List.mem_map]
/-- No number is twice in `occL ψ`. -/
theorem nodup_occL (ψ : Fml) : (occL ψ).Nodup := List.nodup_dedup _
variable {N : ℕ} (φ : CNF N)
/-- The variables in the list, for a formula over x₀ … x_(N−1), are those that occur. -/
theorem val_mem_occL {i : Fin N} : i.val ∈ occL (toN φ) ↔ Occurs φ i := by
rw [mem_occL]
constructor
· rintro ⟨C, hC, l, hl, h⟩
obtain ⟨C', hC', rfl⟩ := List.mem_map.1 hC
obtain ⟨l', hl', rfl⟩ := List.mem_map.1 hl
exact ⟨C', hC', l', hl', Fin.ext h⟩
· rintro ⟨C, hC, l, hl, h⟩
exact ⟨_, List.mem_map_of_mem hC, _, List.mem_map_of_mem hl, congrArg Fin.val h⟩
/-- Every number in the list is a variable of φ that occurs. -/
theorem exists_of_mem_occL {v : ℕ} (h : v ∈ occL (toN φ)) : ∃ i : Fin N, i.val = v ∧ Occurs φ i := by
obtain ⟨C, hC, l, hl, rfl⟩ := mem_occL.1 h
obtain ⟨C', hC', rfl⟩ := List.mem_map.1 hC
obtain ⟨l', hl', rfl⟩ := List.mem_map.1 hl
exact ⟨l'.1, rfl, C', hC', l', hl', rfl⟩
/-- The position of a variable that occurs. -/
def pos0 (i : {i : Fin N // Occurs φ i}) : Fin (occL (toN φ)).length :=
⟨(occL (toN φ)).idxOf i.1.val, List.idxOf_lt_length_iff.2 ((val_mem_occL φ).2 i.2)⟩
/-- The position is a bijection from the variables that occur to the positions of the list. -/
theorem pos0_bijective : Function.Bijective (pos0 φ) := by
constructor
· intro i j h
have h' : (occL (toN φ)).idxOf i.1.val = (occL (toN φ)).idxOf j.1.val := congrArg Fin.val h
exact Subtype.ext (Fin.ext ((List.idxOf_inj ((val_mem_occL φ).2 i.2)).1 h'))
· intro j
obtain ⟨i, hi, ho⟩ := exists_of_mem_occL φ (List.getElem_mem j.2)
refine ⟨⟨i, ho⟩, Fin.ext ?_⟩
simp only [pos0, hi]
exact (nodup_occL _).idxOf_getElem j.1 j.2
/-- The length of the list is the number of variables that occur. -/
theorem length_occL : (occL (toN φ)).length = numOcc φ := by
rw [numOcc, Nat.card_congr (Equiv.ofBijective _ (pos0_bijective φ)), Nat.card_eq_fintype_card, Fintype.card_fin]
/-- The renaming: a bijection from the variables that occur to 0, …, n − 1. -/
noncomputable def pos : {i : Fin N // Occurs φ i} ≃ Fin (numOcc φ) :=
(Equiv.ofBijective _ (pos0_bijective φ)).trans (finCongr (length_occL φ))
/-- The new name of a variable is its position in the list. -/
theorem val_pos (i : {i : Fin N // Occurs φ i}) : (pos φ i).val = (occL (toN φ)).idxOf i.1.val := by
simp [pos, pos0]
/-! ### The renamed formula -/
open Classical in
/-- φ with the variables that occur renamed to 0, …, n − 1. -/
noncomputable def ren : CNF (numOcc φ) :=
φ.map fun C => C.filterMap fun l => if h : Occurs φ l.1 then some (pos φ ⟨l.1, h⟩, l.2) else none
/-- With natural numbers as variables, the renamed formula is what the program computes. -/
theorem toN_ren : toN (ren φ) = renL (occL (toN φ)) (toN φ) := by
classical
simp only [toN, ren, renL, List.map_map]
refine List.map_congr_left fun C hC => ?_
simp only [Function.comp_apply, List.map_filterMap, List.map_map]
rw [← List.filterMap_eq_map]
refine List.filterMap_congr fun l hl => ?_
have h : Occurs φ l.1 := ⟨C, hC, l, hl, rfl⟩
simp [h, val_pos, toN]
/-- The renamed formula has as many clauses as φ. -/
theorem length_ren : (ren φ).length = φ.length := by
simp [ren]
/-- Putting the old names back: the inverse of the renaming on clauses all of whose variables are in
the list L. -/
theorem map_back {L : List ℕ} {C : List (ℕ × Bool)} (h : ∀ l ∈ C, l.1 ∈ L) :
(C.map fun l => (L.idxOf l.1, l.2)).map (fun l => (L.getD l.1 0, l.2)) = C := by
rw [List.map_map]
conv_rhs => rw [← List.map_id C]
refine List.map_congr_left fun l hl => ?_
have h1 := List.idxOf_lt_length_iff.2 (h l hl)
simp [List.getElem?_eq_getElem h1]
/-- If φ is a k-CNF, so is the renamed formula. -/
theorem isKCNF_ren {k : ℕ} (h : SatK.IsKCNF k φ) : SatK.IsKCNF k (ren φ) := by
constructor
· intro C' hC'
have h1 : C'.map (fun l => (l.1.val, l.2)) ∈ toN (ren φ) := List.mem_map_of_mem hC'
rw [toN_ren] at h1
obtain ⟨D, hD, hD'⟩ := List.mem_map.1 h1
obtain ⟨C, hC, rfl⟩ := List.mem_map.1 hD
have h2 := congrArg List.length hD'
simp only [List.length_map] at h2
rw [← h2]
exact h.1 C hC
· have h1 : (toN φ).Nodup := by
refine List.Nodup.map (List.map_injective_iff.2 fun a b hab => ?_) h.2
exact Prod.ext (Fin.ext (Prod.ext_iff.1 hab).1) (Prod.ext_iff.1 hab).2
have h2 : (toN (ren φ)).Nodup := by
rw [toN_ren]
refine List.Nodup.map_on (fun C hC D hD hCD => ?_) h1
rw [← map_back (L := occL (toN φ)) (C := C) fun l hl => mem_occL.2 ⟨C, hC, l, hl, rfl⟩,
← map_back (L := occL (toN φ)) (C := D) fun l hl => mem_occL.2 ⟨D, hD, l, hl, rfl⟩, hCD]
exact List.Nodup.of_map _ h2
/-- An assignment a of x₀ … x_(n−1) satisfies the renamed formula if and only if the assignment «a
at the new name» of the variables that occur satisfies φ. -/
theorem sat_ren_iff (a : Fin (numOcc φ) → Bool) :
(∀ C ∈ ren φ, ∃ l ∈ C, a l.1 = l.2) ↔ ∀ C ∈ φ, ∃ l ∈ C, ∃ h : Occurs φ l.1, a (pos φ ⟨l.1, h⟩) = l.2 := by
classical
simp only [ren, List.forall_mem_map, List.mem_filterMap]
refine forall₂_congr fun C hC => ?_
constructor
· rintro ⟨l', ⟨l, hl, hl'⟩, ha⟩
have h : Occurs φ l.1 := ⟨C, hC, l, hl, rfl⟩
rw [dif_pos h, Option.some_inj] at hl'
subst hl'
exact ⟨l, hl, h, ha⟩
· rintro ⟨l, hl, h, ha⟩
exact ⟨_, ⟨l, hl, dif_pos h⟩, ha⟩
/-- All variables of the renamed formula occur. -/
theorem allOccur_ren : SatM.AllOccur (ren φ) := by
classical
intro j
obtain ⟨C, hC, l, hl, h⟩ := ((pos φ).symm j).2
have ho : Occurs φ l.1 := ⟨C, hC, l, hl, rfl⟩
refine ⟨_, List.mem_map_of_mem hC, (pos φ ⟨l.1, ho⟩, l.2), List.mem_filterMap.2 ⟨l, hl, dif_pos ho⟩, ?_⟩
have : (⟨l.1, ho⟩ : {i : Fin N // Occurs φ i}) = (pos φ).symm j := Subtype.ext h
simp [this]
/-- The number of satisfying assignments of the renamed formula is the number of satisfying
assignments of φ, as assignments of the variables that occur. -/
theorem numSat_ren : SatK.numSat (ren φ) = numSatOcc φ := by
rw [SatK.numSat, numSatOcc, ← Fintype.card_subtype, ← Nat.card_eq_fintype_card]
refine Nat.card_congr (((Equiv.arrowCongr (pos φ) (Equiv.refl Bool)).symm).subtypeEquiv fun a => ?_)
rw [sat_ren_iff]
simp [Equiv.arrowCongr]
/-! ### The sentence after Corollary 11 -/
/-- At most N variables occur in a formula over x₀ … x_(N−1). -/
theorem numOcc_le : numOcc φ ≤ N := by
have h := Nat.card_le_card_of_injective (Subtype.val : {i : Fin N // Occurs φ i} → Fin N) Subtype.val_injective
rwa [Nat.card_eq_fintype_card (α := Fin N), Fintype.card_fin] at h
/-- The number of variables that do not occur. -/
theorem card_not_occurs : Nat.card {i : Fin N // ¬ Occurs φ i} = N - numOcc φ := by
classical
rw [numOcc, Nat.card_eq_fintype_card, Nat.card_eq_fintype_card, Fintype.card_subtype_compl, Fintype.card_fin]
/-- The sentence after Corollary 11: for a formula on N variables of which n occur, the number of
satisfying assignments of all N variables is 2^(N−n) times the number of satisfying assignments as
assignments of the n variables that occur. -/
theorem numSat_eq : SatK.numSat φ = 2 ^ (N - numOcc φ) * numSatOcc φ := by
classical
rw [SatK.numSat, numSatOcc, ← Fintype.card_subtype, ← Nat.card_eq_fintype_card, ← card_not_occurs, mul_comm]
have e1 : {a : Fin N → Bool // ∀ C ∈ φ, ∃ l ∈ C, a l.1 = l.2} ≃
{p : ({i : Fin N // Occurs φ i} → Bool) × ({i : Fin N // ¬ Occurs φ i} → Bool) // ∀ C ∈ φ, ∃ l ∈ C, ∃ h : Occurs φ l.1, p.1 ⟨l.1, h⟩ = l.2} :=
(Equiv.piEquivPiSubtypeProd (Occurs φ) fun _ => Bool).subtypeEquiv fun a => by
refine forall₂_congr fun C hC => ⟨?_, ?_⟩
· rintro ⟨l, hl, h⟩
exact ⟨l, hl, ⟨C, hC, l, hl, rfl⟩, h⟩
· rintro ⟨l, hl, _, h⟩
exact ⟨l, hl, h⟩
rw [Nat.card_congr (e1.trans (Equiv.prodSubtypeFstEquivSubtypeProd
(p := fun b : {i : Fin N // Occurs φ i} → Bool => ∀ C ∈ φ, ∃ l ∈ C, ∃ h : Occurs φ l.1, b ⟨l.1, h⟩ = l.2))), Nat.card_prod, Nat.card_fun, Nat.card_eq_fintype_card (α := Bool), Fintype.card_bool]
/-- If all variables occur, n is the number of all variables, and the number of satisfying
assignments, as assignments of the variables that occur, is `SatK.numSat`. -/
theorem allOccurStmt : AllOccurStmt := fun n φ h => by
have h1 : numOcc φ = n := by
rw [numOcc, Nat.card_congr (Equiv.subtypeUnivEquiv (p := Occurs φ) h), Nat.card_eq_fintype_card, Fintype.card_fin]
refine ⟨h1, ?_⟩
rw [numSat_eq φ, h1]
simp
end VariableBound.Rename
|