math / data /lean /OAI /Algorithms /SubexponentialSAT /Subexp /AssignmentEnumeration.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
19.7 kB
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.Subexp.LocalExpectation
import OAI.Algorithms.SubexponentialSAT.Subexp.CoefficientSearch
namespace OAI.Algorithms
/-!
# Explicit enumerations for the conditional-expectation assignment
Words are enumerated by a finite recursion over the alphabet `List.finRange r`.
Seed indices are enumerated from supplied factor and vertex lists. Numeric
codes use `idxOf`, and inverse lookups use the same lists. No arbitrary finite
equivalence or choice operator occurs in these executable definitions.
-/
namespace SubexponentialSAT.Subexp.AssignmentEnumeration
open SubexponentialSAT.MulticolorTypes SubexponentialSAT.SubexpTypeVariance SubexponentialSAT.SlotAssignment
section Tuples
variable {Ξ± : Type*}
/-- Distinct alphabet entries and distinct tails give distinct tuples. -/
theorem tuples_nodup (alphabet : List Ξ±) (hn : alphabet.Nodup) (Q : β„•) :
(CoefficientSearch.tuples alphabet Q).Nodup := by
induction Q with
| zero => simp [CoefficientSearch.tuples]
| succ Q ih =>
rw [CoefficientSearch.tuples, List.nodup_flatMap]
refine ⟨fun a _ => ih.map (by
intro x y h
exact (Fin.cons_inj.mp h).2), ?_⟩
apply hn.imp
intro a b hab f hfa hfb
obtain ⟨x, _, hxf⟩ := List.mem_map.mp hfa
obtain ⟨y, _, hyf⟩ := List.mem_map.mp hfb
exact hab (Fin.cons_inj.mp (hxf.trans hyf.symm)).1
end Tuples
/-- The complete word alphabet, in the recursion's explicit lexicographic order. -/
def wordAlphabet (r Q : β„•) : List (Word r Q) :=
CoefficientSearch.tuples (List.finRange r) Q
@[simp] theorem wordAlphabet_length (r Q : β„•) :
(wordAlphabet r Q).length = r ^ Q := by
simp [wordAlphabet]
theorem wordAlphabet_nodup (r Q : β„•) : (wordAlphabet r Q).Nodup :=
tuples_nodup _ (List.nodup_finRange r) Q
theorem mem_wordAlphabet (r Q : β„•) (w : Word r Q) : w ∈ wordAlphabet r Q :=
CoefficientSearch.mem_tuples _ (fun _ => List.mem_finRange _) Q w
/-- Explicit default for a nonempty color alphabet. -/
def zeroWord (r Q : β„•) (hr : 0 < r) : Word r Q := fun _ => ⟨0, hr⟩
theorem wordAlphabet_ne_nil (r Q : β„•) (hr : 0 < r) : wordAlphabet r Q β‰  [] :=
List.ne_nil_of_mem (mem_wordAlphabet r Q (zeroWord r Q hr))
/-- Head and tail supplied to the existing sequential choice procedure. -/
def firstWord (r Q : β„•) (hr : 0 < r) : Word r Q :=
(wordAlphabet r Q).headD (zeroWord r Q hr)
def remainingWords (r Q : β„•) : List (Word r Q) := (wordAlphabet r Q).tail
theorem firstWord_cons_remaining (r Q : β„•) (hr : 0 < r) :
firstWord r Q hr :: remainingWords r Q = wordAlphabet r Q := by
have hne := wordAlphabet_ne_nil r Q hr
unfold firstWord remainingWords
cases h : wordAlphabet r Q with
| nil => exact False.elim (hne h)
| cons w ws => rfl
theorem choices_nodup (r Q : β„•) (hr : 0 < r) :
(firstWord r Q hr :: remainingWords r Q).Nodup := by
rw [firstWord_cons_remaining]
exact wordAlphabet_nodup r Q
theorem choices_complete (r Q : β„•) (hr : 0 < r) (w : Word r Q) :
w ∈ firstWord r Q hr :: remainingWords r Q := by
rw [firstWord_cons_remaining]
exact mem_wordAlphabet r Q w
@[simp] theorem choices_length (r Q : β„•) (hr : 0 < r) :
(firstWord r Q hr :: remainingWords r Q).length = r ^ Q := by
rw [firstWord_cons_remaining, wordAlphabet_length]
/-- Numeric word codes are exactly the positions used by `indexCode`. -/
def wordCode (r Q : β„•) (w : Word r Q) : β„• :=
SubexponentialSAT.LocalExpectation.indexCode (wordAlphabet r Q) w
theorem wordCode_lt (r Q : β„•) (w : Word r Q) : wordCode r Q w < r ^ Q := by
have h := List.idxOf_lt_length_iff.mpr (mem_wordAlphabet r Q w)
simpa only [wordCode, SubexponentialSAT.LocalExpectation.indexCode, wordAlphabet_length] using h
def wordCodeFin (r Q : β„•) (w : Word r Q) : Fin (r ^ Q) :=
⟨wordCode r Q w, wordCode_lt r Q w⟩
/-- Bounded word lookup in the actual enumeration. -/
def wordAt (r Q : β„•) (j : Fin (r ^ Q)) : Word r Q :=
(wordAlphabet r Q).get ⟨j.val, by simpa only [wordAlphabet_length] using j.isLt⟩
/-- Partial lookup is available when a caller holds only a natural number. -/
def wordAt? (r Q n : β„•) : Option (Word r Q) := (wordAlphabet r Q)[n]?
@[simp] theorem wordAt_wordCode (r Q : β„•) (w : Word r Q) :
wordAt r Q (wordCodeFin r Q w) = w := by
simp [wordAt, wordCodeFin, wordCode, SubexponentialSAT.LocalExpectation.indexCode]
@[simp] theorem wordCode_wordAt (r Q : β„•) (j : Fin (r ^ Q)) :
wordCodeFin r Q (wordAt r Q j) = j := by
apply Fin.ext
exact (wordAlphabet_nodup r Q).idxOf_getElem j.val _
@[simp] theorem wordAt?_wordCode (r Q : β„•) (w : Word r Q) :
wordAt? r Q (wordCode r Q w) = some w :=
List.getElem?_idxOf (mem_wordAlphabet r Q w)
/-- An explicit index equivalence whose two functions are lookup and `idxOf`. -/
def wordIndexEquiv (r Q : β„•) : Fin (r ^ Q) ≃ Word r Q where
toFun := wordAt r Q
invFun := wordCodeFin r Q
left_inv := wordCode_wordAt r Q
right_inv := wordAt_wordCode r Q
/-- Explicitly enumerate a head word and all slot words. -/
def universalAlphabet (r Q Ξ” : β„•) : List (UniversalType r Q Ξ”) :=
((wordAlphabet r Q).product (CoefficientSearch.tuples (wordAlphabet r Q) Ξ”)).map
(equivProd r Q Ξ”).symm
theorem universalAlphabet_nodup (r Q Ξ” : β„•) : (universalAlphabet r Q Ξ”).Nodup :=
((wordAlphabet_nodup r Q).product
(tuples_nodup _ (wordAlphabet_nodup r Q) Ξ”)).map (equivProd r Q Ξ”).symm.injective
theorem mem_universalAlphabet (r Q Ξ” : β„•) (u : UniversalType r Q Ξ”) :
u ∈ universalAlphabet r Q Ξ” := by
apply List.mem_map.mpr
refine ⟨(u.head, u.slot), List.mem_product.mpr ⟨mem_wordAlphabet r Q u.head, ?_⟩, rfl⟩
exact CoefficientSearch.mem_tuples _ (mem_wordAlphabet r Q) Ξ” u.slot
@[simp] theorem universalAlphabet_length (r Q Ξ” : β„•) :
(universalAlphabet r Q Ξ”).length = r ^ (Q * (Ξ” + 1)) := by
simp only [universalAlphabet, List.length_map]
have hp : ((wordAlphabet r Q).product
(CoefficientSearch.tuples (wordAlphabet r Q) Ξ”)).length =
(wordAlphabet r Q).length *
(CoefficientSearch.tuples (wordAlphabet r Q) Ξ”).length := List.length_product _ _
rw [hp, CoefficientSearch.tuples_length, wordAlphabet_length, pow_mul, pow_succ]
exact Nat.mul_comm _ _
section SumLists
variable {Ξ± Ξ² : Type*}
/-- List the left summand followed by the right summand. -/
def sumList (as : List Ξ±) (bs : List Ξ²) : List (Ξ± βŠ• Ξ²) :=
as.map Sum.inl ++ bs.map Sum.inr
@[simp] theorem sumList_length (as : List Ξ±) (bs : List Ξ²) :
(sumList as bs).length = as.length + bs.length := by simp [sumList]
theorem sumList_nodup (as : List Ξ±) (bs : List Ξ²)
(ha : as.Nodup) (hb : bs.Nodup) : (sumList as bs).Nodup := by
apply List.Nodup.append (ha.map Sum.inl_injective) (hb.map Sum.inr_injective)
intro x hx hy
obtain ⟨a, _, rfl⟩ := List.mem_map.mp hx
obtain ⟨b, _, h⟩ := List.mem_map.mp hy
cases h
theorem sumList_complete (as : List Ξ±) (bs : List Ξ²)
(ha : βˆ€ a, a ∈ as) (hb : βˆ€ b, b ∈ bs) (x : Ξ± βŠ• Ξ²) : x ∈ sumList as bs := by
cases x <;> simp [sumList, ha, hb]
end SumLists
section Seeds
variable {V E : Type*}
/-- Factors, vertex heads, then all vertex/slot padding words. -/
def seedIndices (vertices : List V) (factors : List E) (Ξ” : β„•) :
List (SeedIndex V E Ξ”) :=
sumList factors (sumList vertices (vertices.product (List.finRange Ξ”)))
theorem seedIndices_nodup (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : vertices.Nodup) (he : factors.Nodup) :
(seedIndices vertices factors Ξ”).Nodup :=
sumList_nodup _ _ he
(sumList_nodup _ _ hv (hv.product (List.nodup_finRange Ξ”)))
theorem mem_seedIndices (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
s ∈ seedIndices vertices factors Ξ” := by
apply sumList_complete _ _ he
apply sumList_complete _ _ hv
intro x
exact List.mem_product.mpr ⟨hv x.1, List.mem_finRange x.2⟩
@[simp] theorem seedIndices_length (vertices : List V) (factors : List E) (Ξ” : β„•) :
(seedIndices vertices factors Ξ”).length =
factors.length + (Ξ” + 1) * vertices.length := by
simp only [seedIndices, sumList_length]
have hp : (vertices.product (List.finRange Ξ”)).length =
vertices.length * (List.finRange Ξ”).length := List.length_product _ _
rw [hp, List.length_finRange]
ring
theorem enumeration_length [Fintype V] [DecidableEq V]
(vertices : List V) (hn : vertices.Nodup) (hc : βˆ€ v, v ∈ vertices) :
vertices.length = Fintype.card V := by
rw [← List.toFinset_card_of_nodup hn]
have h : vertices.toFinset = Finset.univ := by ext v; simp [hc v]
rw [h, Finset.card_univ]
theorem seedIndices_length_card [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]
(vertices : List V) (factors : List E) (Ξ” : β„•)
(hvn : vertices.Nodup) (hvc : βˆ€ v, v ∈ vertices)
(hen : factors.Nodup) (hec : βˆ€ e, e ∈ factors) :
(seedIndices vertices factors Ξ”).length =
Fintype.card E + (Ξ” + 1) * Fintype.card V := by
rw [seedIndices_length, enumeration_length vertices hvn hvc,
enumeration_length factors hen hec]
variable [DecidableEq V] [DecidableEq E]
def seedCode (vertices : List V) (factors : List E) (Ξ” : β„•)
(s : SeedIndex V E Ξ”) : β„• :=
SubexponentialSAT.LocalExpectation.indexCode (seedIndices vertices factors Ξ”) s
def seedAt? (vertices : List V) (factors : List E) (Ξ” n : β„•) :
Option (SeedIndex V E Ξ”) := (seedIndices vertices factors Ξ”)[n]?
theorem seedAt?_seedCode (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
seedAt? vertices factors Ξ” (seedCode vertices factors Ξ” s) = some s := by
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
exact List.getElem?_idxOf (mem_seedIndices vertices factors Ξ” hv he s)
theorem seedCode_lt (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
seedCode vertices factors Ξ” s < factors.length + (Ξ” + 1) * vertices.length := by
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
have h := List.idxOf_lt_length_iff.mpr (mem_seedIndices vertices factors Ξ” hv he s)
simpa only [seedCode, SubexponentialSAT.LocalExpectation.indexCode, seedIndices_length] using h
def seedCodeFin (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
Fin (factors.length + (Ξ” + 1) * vertices.length) :=
⟨seedCode vertices factors Ξ” s, seedCode_lt vertices factors Ξ” hv he s⟩
def seedAt (vertices : List V) (factors : List E) (Ξ” : β„•)
(n : Fin (factors.length + (Ξ” + 1) * vertices.length)) : SeedIndex V E Ξ” :=
(seedIndices vertices factors Ξ”).get
⟨n.val, by simpa only [seedIndices_length] using n.isLt⟩
@[simp] theorem seedAt_seedCode (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
seedAt vertices factors Ξ” (seedCodeFin vertices factors Ξ” hv he s) = s := by
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
simp [seedAt, seedCodeFin, seedCode, SubexponentialSAT.LocalExpectation.indexCode]
@[simp] theorem seedCode_seedAt (vertices : List V) (factors : List E) (Ξ” : β„•)
(hvn : vertices.Nodup) (hen : factors.Nodup)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors)
(n : Fin (factors.length + (Ξ” + 1) * vertices.length)) :
seedCodeFin vertices factors Ξ” hv he (seedAt vertices factors Ξ” n) = n := by
apply Fin.ext
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
exact (seedIndices_nodup vertices factors Ξ” hvn hen).idxOf_getElem n.val _
/-- Both directions execute the supplied-list lookup and position search. -/
def seedIndexEquiv (vertices : List V) (factors : List E) (Ξ” : β„•)
(hvn : vertices.Nodup) (hen : factors.Nodup)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) :
Fin (factors.length + (Ξ” + 1) * vertices.length) ≃ SeedIndex V E Ξ” where
toFun := seedAt vertices factors Ξ”
invFun := seedCodeFin vertices factors Ξ” hv he
left_inv := seedCode_seedAt vertices factors Ξ” hvn hen hv he
right_inv := seedAt_seedCode vertices factors Ξ” hv he
theorem seedCode_injective (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) :
Function.Injective (seedCode vertices factors Ξ”) := by
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
intro s t h
exact (List.idxOf_inj (mem_seedIndices vertices factors Ξ” hv he s)).mp h
theorem seedCodes_eq_range (vertices : List V) (factors : List E) (Ξ” : β„•)
(hv : vertices.Nodup) (he : factors.Nodup) :
(seedIndices vertices factors Ξ”).map (seedCode vertices factors Ξ”) =
List.range (factors.length + (Ξ” + 1) * vertices.length) := by
let _ : BEq (SeedIndex V E Ξ”) := instBEqOfDecidableEq
apply List.ext_getElem
Β· simp only [List.length_map, seedIndices_length, List.length_range]
Β· intro n hn₁ hnβ‚‚
simp only [List.getElem_map, List.getElem_range, seedCode,
SubexponentialSAT.LocalExpectation.indexCode]
exact (seedIndices_nodup vertices factors Ξ” hv he).idxOf_getElem n (by simpa using hn₁)
end Seeds
section ChoiceDecoding
variable {I W : Type*}
/-- The search stores chosen values newest first; numerical coordinates use
the reverse list, so coordinate zero is the first choice. -/
theorem decode_eq_reverse_getD (default : W) (values : List W) (n : β„•) :
SubexponentialSAT.LocalExpectation.decode default values n = values.reverse.getD n default := by
induction values with
| nil => rfl
| cons w values ih =>
simp only [SubexponentialSAT.LocalExpectation.decode, List.reverse_cons]
by_cases heq : n = values.length
Β· subst n
simp only [Function.update_self]
rw [List.getD_append_right _ _ _ _ (by simp)]
simp
Β· rw [Function.update_of_ne heq, ih]
by_cases hlt : n < values.length
Β· rw [List.getD_append _ _ _ _ (by simpa using hlt)]
Β· have hge : values.length + 1 ≀ n := by omega
rw [List.getD_eq_default _ _ (by simpa using Nat.le_of_succ_le hge),
List.getD_eq_default _ _ (by simpa using hge)]
/-- Serialize a total assignment in exactly the search's reversed-list format. -/
def encodeChoices (indices : List I) (assignment : I β†’ W) : List W :=
(indices.map assignment).reverse
@[simp] theorem encodeChoices_length (indices : List I) (assignment : I β†’ W) :
(encodeChoices indices assignment).length = indices.length := by
simp [encodeChoices]
theorem decode_encodeChoices [DecidableEq I] (default : W)
(indices : List I) (assignment : I β†’ W) (i : I) (hi : i ∈ indices) :
SubexponentialSAT.LocalExpectation.decode default (encodeChoices indices assignment)
(SubexponentialSAT.LocalExpectation.indexCode indices i) = assignment i := by
rw [decode_eq_reverse_getD]
simp only [encodeChoices, List.reverse_reverse, List.getD_eq_getElem?_getD,
List.getElem?_map, SubexponentialSAT.LocalExpectation.indexCode, List.getElem?_idxOf hi]
rfl
theorem assignWithEvaluator_length (a : W) (tail : List W)
(evaluate : β„• β†’ List W β†’ β„š) (n : β„•) (fixed : List W) :
(SubexponentialSAT.ConditionalExpectation.assignWithEvaluator a tail evaluate n fixed).length =
n + fixed.length := by
induction n generalizing fixed with
| zero => simp [SubexponentialSAT.ConditionalExpectation.assignWithEvaluator]
| succ n ih =>
simp only [SubexponentialSAT.ConditionalExpectation.assignWithEvaluator, ih, List.length_cons]
omega
end ChoiceDecoding
section ComputedAssignment
variable {V E : Type*} [DecidableEq V] [DecidableEq E] [Fintype V] [Fintype E]
variable (P : SubexponentialSAT.Factor.Pattern V E) {r Q Ξ” : β„•} [NeZero r]
(slots : SlotMap P Ξ”)
/-- The existing assignment algorithm with its word and seed enumerations
constructed explicitly from the input vertex and factor lists. -/
def enumeratedAssignment (vertices : List V) (factors : List E) :
Assignment V E r Q Ξ” :=
LocalExpectation.computedAssignment P slots
(firstWord r Q (Nat.pos_of_neZero r)) (remainingWords r Q) vertices
(seedIndices vertices factors Ξ”)
/-- The actual reverse-chronological choices made by that algorithm. -/
def assignmentValues (vertices : List V) (factors : List E) : List (Word r Q) :=
SubexponentialSAT.LocalExpectation.localAssign
(firstWord r Q (Nat.pos_of_neZero r)) (remainingWords r Q)
(vertices.product vertices)
(LocalExpectation.numberedPairSupport P slots (seedIndices vertices factors Ξ”))
(LocalExpectation.numberedPairTerm P slots (seedIndices vertices factors Ξ”))
(LocalExpectation.pairOffset (V := V) (r := r) (Q := Q) (Ξ” := Ξ”))
(firstWord r Q (Nat.pos_of_neZero r)) (seedIndices vertices factors Ξ”).length
@[simp] theorem assignmentValues_length (vertices : List V) (factors : List E) :
(assignmentValues (r := r) (Q := Q) P slots vertices factors).length =
factors.length + (Ξ” + 1) * vertices.length := by
simp only [assignmentValues, SubexponentialSAT.LocalExpectation.localAssign,
assignWithEvaluator_length, List.length_nil, Nat.add_zero, seedIndices_length]
theorem enumeratedAssignment_apply (vertices : List V) (factors : List E)
(s : SeedIndex V E Ξ”) :
enumeratedAssignment (r := r) (Q := Q) P slots vertices factors s =
SubexponentialSAT.LocalExpectation.decode (firstWord r Q (Nat.pos_of_neZero r))
(assignmentValues P slots vertices factors) (seedCode vertices factors Ξ” s) := rfl
/-- The assigned word is found at precisely its seed code in the chronological
choice list. Every seed lookup is in range. -/
theorem assignmentValues_lookup (vertices : List V) (factors : List E)
(hv : βˆ€ v, v ∈ vertices) (he : βˆ€ e, e ∈ factors) (s : SeedIndex V E Ξ”) :
((assignmentValues (r := r) (Q := Q) P slots vertices factors).reverse)[seedCode vertices factors Ξ” s]? =
some (enumeratedAssignment P slots vertices factors s) := by
have hlt : seedCode vertices factors Ξ” s <
(assignmentValues (r := r) (Q := Q) P slots vertices factors).reverse.length := by
simpa only [List.length_reverse, assignmentValues_length] using
seedCode_lt vertices factors Ξ” hv he s
rw [enumeratedAssignment_apply, decode_eq_reverse_getD,
List.getD_eq_getElem _ _ hlt, List.getElem?_eq_getElem hlt]
/-- The deviation theorem now requires only the actual input lists; alphabet
and seed completeness are discharged by the explicit constructions above. -/
theorem enumeratedAssignment_deviation_le (vertices : List V) (factors : List E)
(hvn : vertices.Nodup) (hvc : βˆ€ v, v ∈ vertices)
(hen : factors.Nodup) (hec : βˆ€ e, e ∈ factors) {k : β„•} (hP : P.Bounded k Ξ”) :
LocalExpectation.pairObjective P slots
(enumeratedAssignment (r := r) (Q := Q) P slots vertices factors) ≀
(Fintype.card (UniversalType r Q Ξ”) : β„š) *
(1 + (Ξ” * k : β„•)) * (Fintype.card V : β„š) :=
LocalExpectation.computedAssignment_deviation_le P slots
(firstWord r Q (Nat.pos_of_neZero r)) (remainingWords r Q)
(choices_nodup r Q _) (choices_complete r Q _)
vertices hvn hvc (seedIndices vertices factors Ξ”)
(seedIndices_nodup vertices factors Ξ” hvn hen)
(mem_seedIndices vertices factors Ξ” hvc hec) hP
end ComputedAssignment
end SubexponentialSAT.Subexp.AssignmentEnumeration
end OAI.Algorithms