File size: 9,509 Bytes
4a56cd6 | 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 | import OAI.Algorithms.SubexponentialSAT.LeanCompat
namespace OAI.Algorithms
/-!
# Boolean chunks and valid finite-field labels
This file implements the encoding in Section 3 of the candidate exposition.
The encoding is an actual recursively defined equivalence, not just an equality
of cardinalities. Primality is unnecessary for this combinatorial part.
-/
namespace SubexponentialSAT.BooleanPacking
open scoped BigOperators
/-- The manuscript's logarithmic chunk-size choice supplies enough labels. -/
theorem capacity_of_le_log {b p : β} (hp : p β 0) (hb : b β€ Nat.log 2 p) :
2 ^ b β€ p :=
Nat.pow_le_of_le_log hp hb
/-- The equivalent `Nat.log2` formulation of the chunk-size bound. -/
theorem capacity_of_le_log2 {b p : β} (hp : p β 0) (hb : b β€ Nat.log2 p) :
2 ^ b β€ p :=
capacity_of_le_log hp (by simpa only [Nat.log2_eq_log_two] using hb)
/-- A chunk of `b` Boolean coordinates. Empty chunks are allowed by the API. -/
abbrev Chunk (b : β) := Fin b β Bool
/-- Splitting a nonempty chunk into its first bit and remaining bits. -/
def splitChunk (b : β) : Chunk (b + 1) β Bool Γ Chunk b where
toFun x := (x 0, fun i => x i.succ)
invFun x := Fin.cases x.1 x.2
left_inv x := by
funext i
refine Fin.cases ?_ (fun j => ?_) i <;> rfl
right_inv x := by
rcases x with β¨head, tailβ©
rfl
/-- A computable encoding of a Boolean chunk by exactly `2^b` labels. -/
def chunkEquiv : (b : β) β Chunk b β Fin (2 ^ b)
| 0 => by simpa only [pow_zero] using Equiv.ofUnique (Chunk 0) (Fin 1)
| b + 1 =>
(splitChunk b).trans <|
(Equiv.prodCongr finTwoEquiv.symm (chunkEquiv b)).trans <|
finProdFinEquiv.trans (finCongr (by simp [pow_succ, Nat.mul_comm]))
/-- The allowed labels for a `b`-bit chunk inside `Fin p`. -/
def ValidLabel (b p : β) := {x : Fin p // x.val < 2 ^ b}
instance (b p : β) : Fintype (ValidLabel b p) :=
inferInstanceAs (Fintype {x : Fin p // x.val < 2 ^ b})
/-- Including the initial segment of length `2^b` into a larger label space. -/
def finValidLabelEquiv {b p : β} (h : 2 ^ b β€ p) :
Fin (2 ^ b) β ValidLabel b p where
toFun x := β¨β¨x.val, lt_of_lt_of_le x.isLt hβ©, x.isLtβ©
invFun x := β¨x.val.val, x.propertyβ©
left_inv _ := rfl
right_inv _ := rfl
/-- Encoding and decoding a chunk using precisely the valid labels. -/
def validLabelEquiv {b p : β} (h : 2 ^ b β€ p) : Chunk b β ValidLabel b p :=
(chunkEquiv b).trans (finValidLabelEquiv h)
/-- The chunk encoding into the complete label space, including invalid labels. -/
def chunkEmbedding {b p : β} (h : 2 ^ b β€ p) : Chunk b βͺ Fin p where
toFun x := (validLabelEquiv h x).val
inj' _ _ heq := (validLabelEquiv h).injective (Subtype.ext heq)
/-- Decoding is defined on valid labels. -/
def decode {b p : β} (h : 2 ^ b β€ p) (x : ValidLabel b p) : Chunk b :=
(validLabelEquiv h).symm x
@[simp] theorem decode_encode {b p : β} (h : 2 ^ b β€ p) (x : Chunk b) :
decode h (validLabelEquiv h x) = x :=
(validLabelEquiv h).symm_apply_apply x
@[simp] theorem encode_decode {b p : β} (h : 2 ^ b β€ p) (x : ValidLabel b p) :
validLabelEquiv h (decode h x) = x :=
(validLabelEquiv h).apply_symm_apply x
theorem chunkEmbedding_valid {b p : β} (h : 2 ^ b β€ p) (x : Chunk b) :
(chunkEmbedding h x).val < 2 ^ b :=
(validLabelEquiv h x).property
theorem mem_range_chunkEmbedding {b p : β} (h : 2 ^ b β€ p) (a : Fin p) :
a β Set.range (chunkEmbedding h) β a.val < 2 ^ b := by
constructor
Β· rintro β¨x, rflβ©
exact chunkEmbedding_valid h x
Β· intro ha
refine β¨decode h β¨a, haβ©, ?_β©
exact congrArg Subtype.val (encode_decode h β¨a, haβ©)
theorem card_chunk (b : β) : Fintype.card (Chunk b) = 2 ^ b := by
calc
_ = Fintype.card (Fin (2 ^ b)) := Fintype.card_congr (chunkEquiv b)
_ = _ := Fintype.card_fin _
theorem card_validLabel {b p : β} (h : 2 ^ b β€ p) :
Fintype.card (ValidLabel b p) = 2 ^ b := by
calc
_ = Fintype.card (Fin (2 ^ b)) := Fintype.card_congr (finValidLabelEquiv h).symm
_ = _ := Fintype.card_fin _
/-- All chunk-label assignments accepted by the unary validity tests. -/
def PackedAssignment {J : Type*} (b : J β β) (p : β) :=
{x : J β Fin p // β j, (x j).val < 2 ^ b j}
instance {J : Type*} [Fintype J] [DecidableEq J] (b : J β β) (p : β) :
Fintype (PackedAssignment b p) :=
inferInstanceAs (Fintype {x : J β Fin p // β j, (x j).val < 2 ^ b j})
/-- Coordinatewise chunk encoding, with validity built into the codomain. -/
def packingEquiv {J : Type*} (b : J β β) {p : β} (h : β j, 2 ^ b j β€ p) :
(β j, Chunk (b j)) β PackedAssignment b p :=
(Equiv.piCongrRight fun j => validLabelEquiv (h j)).trans
Equiv.subtypePiEquivPi.symm
@[simp] theorem packingEquiv_apply {J : Type*} (b : J β β) {p : β}
(h : β j, 2 ^ b j β€ p) (x : β j, Chunk (b j)) (j : J) :
(packingEquiv b h x).val j = chunkEmbedding (h j) (x j) := rfl
@[simp] theorem packingEquiv_symm_apply {J : Type*} (b : J β β) {p : β}
(h : β j, 2 ^ b j β€ p) (x : PackedAssignment b p) (j : J) :
(packingEquiv b h).symm x j = decode (h j) β¨x.val j, x.property jβ© := rfl
/-- An index for each original bit, recording both its chunk and position. -/
abbrev BitIndex {J : Type*} (b : J β β) := Ξ£ j, Fin (b j)
/-- Regrouping Boolean coordinates requires no assignments to be discarded. -/
def regroupEquiv {J : Type*} (b : J β β) :
(BitIndex b β Bool) β (β j, Chunk (b j)) :=
Equiv.piCurry fun _ _ => Bool
/-- Encoding a flat Boolean assignment into accepted chunk labels. -/
def flatPackingEquiv {J : Type*} (b : J β β) {p : β} (h : β j, 2 ^ b j β€ p) :
(BitIndex b β Bool) β PackedAssignment b p :=
(regroupEquiv b).trans (packingEquiv b h)
/-- A concrete partition of the original variable indices transports packing
to the original assignment type. -/
def assignmentPackingEquiv {V J : Type*} (b : J β β) (indices : V β BitIndex b)
{p : β} (h : β j, 2 ^ b j β€ p) :
(V β Bool) β PackedAssignment b p :=
(Equiv.arrowCongr indices (Equiv.refl Bool)).trans (flatPackingEquiv b h)
/-- The satisfying assignments for any predicate correspond under encoding. -/
def satisfyingEquiv {J : Type*} (b : J β β) {p : β} (h : β j, 2 ^ b j β€ p)
(P : (β j, Chunk (b j)) β Prop) :
{x // P x} β {y : PackedAssignment b p // P ((packingEquiv b h).symm y)} :=
(packingEquiv b h).subtypeEquiv (by intro x; simp)
/-- Exact finite model-count preservation, not just satisfiability preservation. -/
theorem count_preserved {J : Type*} [Fintype J] [DecidableEq J] (b : J β β)
{p : β} (h : β j, 2 ^ b j β€ p) (P : (β j, Chunk (b j)) β Prop)
[DecidablePred P] :
Fintype.card {x // P x} =
Fintype.card {y : PackedAssignment b p // P ((packingEquiv b h).symm y)} :=
Fintype.card_congr (satisfyingEquiv b h P)
/-- The exact model-count theorem also applies to any partition of original
Boolean variable indices into chunks. -/
theorem assignment_count_preserved {V J : Type*} [Fintype V] [DecidableEq V]
[Fintype J] [DecidableEq J] (b : J β β) (indices : V β BitIndex b)
{p : β} (h : β j, 2 ^ b j β€ p) (P : (V β Bool) β Prop) [DecidablePred P] :
Fintype.card {x // P x} =
Fintype.card {y : PackedAssignment b p //
P ((assignmentPackingEquiv b indices h).symm y)} := by
apply Fintype.card_congr
exact (assignmentPackingEquiv b indices h).subtypeEquiv (by intro x; simp)
theorem card_packedAssignment {J : Type*} [Fintype J] [DecidableEq J]
(b : J β β) {p : β} (h : β j, 2 ^ b j β€ p) :
Fintype.card (PackedAssignment b p) = 2 ^ (β j, b j) := by
calc
Fintype.card (PackedAssignment b p) = Fintype.card (β j, Chunk (b j)) :=
Fintype.card_congr (packingEquiv b h).symm
_ = β j, Fintype.card (Chunk (b j)) := Fintype.card_pi
_ = β j, 2 ^ b j := by simp only [card_chunk]
_ = 2 ^ (β j, b j) := Finset.prod_pow_eq_pow_sum _ _ _
/-- The finite label equivalence preserves the usual representatives in `ZMod`. -/
@[simp] theorem finEquiv_val {p : β} [NeZero p] (x : Fin p) :
(ZMod.finEquiv p x).val = x.val := by
cases p with
| zero => exact (NeZero.ne (0 : β) rfl).elim
| succ p => rfl
/-- Valid representatives inside `ZMod p`. For prime `p`, these are field labels. -/
def ValidZModLabel (b p : β) := {x : ZMod p // x.val < 2 ^ b}
/-- The same explicit encoding in the residue ring, hence in the prime field. -/
def validZModEquiv {b p : β} [NeZero p] (h : 2 ^ b β€ p) :
Chunk b β ValidZModLabel b p :=
(validLabelEquiv h).trans <|
(ZMod.finEquiv p).toEquiv.subtypeEquiv (by intro x; simp)
/-- Encoding a Boolean chunk as a residue-ring element. -/
def chunkZModEmbedding {b p : β} [NeZero p] (h : 2 ^ b β€ p) :
Chunk b βͺ ZMod p where
toFun x := (validZModEquiv h x).val
inj' _ _ heq := (validZModEquiv h).injective (Subtype.ext heq)
theorem mem_range_chunkZModEmbedding {b p : β} [NeZero p] (h : 2 ^ b β€ p)
(a : ZMod p) : a β Set.range (chunkZModEmbedding h) β a.val < 2 ^ b := by
constructor
Β· rintro β¨x, rflβ©
exact (validZModEquiv h x).property
Β· intro ha
refine β¨(validZModEquiv h).symm β¨a, haβ©, ?_β©
exact congrArg Subtype.val ((validZModEquiv h).apply_symm_apply β¨a, haβ©)
end SubexponentialSAT.BooleanPacking
end OAI.Algorithms
|