math / data /lean /OAI /Computability /PerfectCompleteness /Sampling /CutSamplerKeyLocalityLemmas.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
21.4 kB
import OAI.Computability.PerfectCompleteness.Construction.TreeCanonical
import OAI.Computability.PerfectCompleteness.Foundations.CutTerminalZero
import OAI.Computability.PerfectCompleteness.Repetition.CleanCountingLemmas
import OAI.Computability.PerfectCompleteness.Sampling.CutSamplerLocality
namespace OAI
namespace PerfectCompleteness.CutSamplerKeyLocality
open scoped Classical
open RecursiveSpaces DescendantSpaces TreeSourceSpaces
open CutSamplerLocality
abbrev F2 := ZMod 2
noncomputable section
universe u
variable {branch : Nat β†’ Nat} {n m t : Nat}
def keptLeaf : {n m : Nat} β†’ Path branch n (m + 1) β†’
(Fin (branch m) β†’ Prop) β†’ Slots branch n β†’ Prop
| _, _, .refl _, clean, s => Β¬ clean s.1
| _, _, .step i p, clean, s => s.1 β‰  i ∨ keptLeaf p clean s.2
theorem view_eq_of_keptLeaf (p : Path branch n (m + 1)) :
βˆ€ (A : Slots branch n β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(x y : Assignment A),
(βˆ€ s, keptLeaf p clean s β†’ x s = y s) β†’ view p A clean x = view p A clean y := by
induction n generalizing m with
| zero =>
have h := p.height_le
omega
| succ n ih =>
cases p with
| refl =>
intro A clean x y hagree
funext i s
exact hagree (i.val, s) i.property
| step i p =>
intro A clean x y hagree
apply Prod.ext
Β· funext j s
exact hagree (j.val, s) (Or.inl j.property)
Β· apply ih p (childFamily A i) clean
(childRestriction A i x) (childRestriction A i y)
intro s hs
exact hagree (i, s) (Or.inr hs)
theorem keptLeaf_at_cut (p : Path branch n (m + 1))
(clean : Fin (branch m) β†’ Prop) (i : Fin (branch m)) (s : Slots branch m) :
keptLeaf p clean (p.slotEmbedding (i, s)) ↔ Β¬ clean i := by
induction n generalizing m with
| zero =>
have h := p.height_le
omega
| succ n ih =>
cases p with
| refl => rfl
| step j p =>
change (j β‰  j ∨ keptLeaf p clean (p.slotEmbedding (i, s))) ↔ Β¬ clean i
simpa only [ne_self_iff_false, false_or] using ih p clean i s
def numberedKeep (p : Path branch n (m + 1)) (clean : Fin (branch m) β†’ Prop) :
Fin (TreeCanonical.locationCount branch n t) β†’ Prop :=
fun j => keptLeaf p clean ((TreeCanonical.numbering branch n t).symm j).1
theorem not_numberedKeep_clean (p : Path branch n (m + 1))
(clean : Fin (branch m) β†’ Prop) (i : Fin (branch m)) (hi : clean i)
(s : Slots branch m) (k : Fin t) :
Β¬ numberedKeep p clean
(TreeCanonical.numbering branch n t (p.slotEmbedding (i, s), k)) := by
intro h
have hkeep : keptLeaf p clean (p.slotEmbedding (i, s)) := by
simpa only [numberedKeep, Equiv.symm_apply_apply] using h
exact (keptLeaf_at_cut p clean i s).mp hkeep hi
theorem view_eq_of_numbered_retained (p : Path branch n (m + 1))
(slots : Slots branch n β†’ Fin t β†’ MixedSupport.Slot) (clean : Fin (branch m) β†’ Prop)
(x y : Domain slots)
(hret : CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean)
(TreeCanonical.assignmentEquiv slots x) =
CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean)
(TreeCanonical.assignmentEquiv slots y)) :
view p (LeafDomain slots) clean x = view p (LeafDomain slots) clean y := by
apply view_eq_of_keptLeaf p (LeafDomain slots) clean x y
intro s hs
funext k
let j := TreeCanonical.numbering branch n t (s, k)
have hj : numberedKeep p clean j := by
simpa only [numberedKeep, j, Equiv.symm_apply_apply] using hs
have hvalue := congrFun hret ⟨j, hj⟩
exact Eq.mp (congrArg (fun sk : TreeCanonical.Location branch n t =>
x sk.1 sk.2 = y sk.1 sk.2)
((TreeCanonical.numbering branch n t).symm_apply_apply (s, k))) hvalue
theorem view_eq_of_retained (p : Path branch n (m + 1))
(slots : Slots branch n β†’ Fin t β†’ MixedSupport.Slot) (clean : Fin (branch m) β†’ Prop)
(x y : MixedSupport.Assignment (TreeCanonical.numberedSlots slots))
(hret : CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) x =
CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) y) :
view p (LeafDomain slots) clean ((TreeCanonical.assignmentEquiv slots).symm x) =
view p (LeafDomain slots) clean ((TreeCanonical.assignmentEquiv slots).symm y) := by
apply view_eq_of_numbered_retained p slots clean
simpa only [Equiv.apply_symm_apply] using hret
variable {J : Type*}
(repeats : Nat β†’ Nat) (p : Path branch n (m + 1))
(slots : Slots branch n β†’ Fin t β†’ MixedSupport.Slot) (clean : Fin (branch m) β†’ Prop)
(tapes : J β†’ CutSamplerRefinement.Tape F2 repeats p (LeafDomain slots))
def rowQuery : Domain slots β†’ J β†’ F2 :=
fun x j => (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain slots) (tapes j)).val x
def numberedRows : MixedSupport.Assignment (TreeCanonical.numberedSlots slots) β†’ J β†’ F2 :=
TreeCanonical.numberedFunction slots (rowQuery repeats p slots tapes)
def allRowsZero : Prop :=
βˆ€ j, ZeroAtClean F2 repeats p (LeafDomain slots) clean (tapes j)
theorem numberedRows_constant (hzero : allRowsZero repeats p slots clean tapes)
(x y : MixedSupport.Assignment (TreeCanonical.numberedSlots slots))
(hret : CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) x =
CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) y) :
numberedRows repeats p slots tapes x = numberedRows repeats p slots tapes y := by
funext j
exact evaluate_eq_of_view_eq F2 repeats p (LeafDomain slots) clean (tapes j) (hzero j)
_ _ (view_eq_of_retained p slots clean x y hret)
def retainedRows : CleanKeyErasure.Retained (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) β†’ J β†’ F2 :=
numberedRows repeats p slots tapes ∘
CleanKeyFactorization.fill (TreeCanonical.numberedSlots slots) (numberedKeep p clean)
theorem retainedRows_eq_of_extension (hzero : allRowsZero repeats p slots clean tapes)
(x : CleanKeyErasure.Retained (TreeCanonical.numberedSlots slots) (numberedKeep p clean))
(y : MixedSupport.Assignment (TreeCanonical.numberedSlots slots))
(hy : CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) y = x) :
retainedRows repeats p slots clean tapes x = numberedRows repeats p slots tapes y := by
apply numberedRows_constant repeats p slots clean tapes hzero
exact (CleanKeyFactorization.retain_fill (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) x).trans hy.symm
theorem numberedRows_factor (hzero : allRowsZero repeats p slots clean tapes) :
numberedRows repeats p slots tapes = retainedRows repeats p slots clean tapes ∘
CleanKeyErasure.retain (TreeCanonical.numberedSlots slots) (numberedKeep p clean) := by
funext x
exact (retainedRows_eq_of_extension repeats p slots clean tapes hzero _ x rfl).symm
theorem clean_position_dropped (hzero : allRowsZero repeats p slots clean tapes)
(i : Fin (branch m)) (hi : clean i) (s : Slots branch m) (k : Fin t) :
MixedSupport.keyFields (TreeCanonical.numberedSlots slots) (numberedRows repeats p slots tapes)
(TreeCanonical.numbering branch n t (p.slotEmbedding (i, s), k)) = .dropped :=
CleanKeyErasure.keyFields_dropped_of_retained (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) (numberedRows repeats p slots tapes)
(numberedRows_constant repeats p slots clean tapes hzero) _
(not_numberedKeep_clean p clean i hi s k)
theorem key_eq_retained (hzero : allRowsZero repeats p slots clean tapes)
(side : CanonicalKeys.Side) :
CanonicalKeys.key side (TreeCanonical.numberedSlots slots) (numberedRows repeats p slots tapes) =
CleanKeyFactorization.retainedKey side (numberedKeep p clean)
(CleanKeyFactorization.retainedSlots (TreeCanonical.numberedSlots slots) (numberedKeep p clean))
(retainedRows repeats p slots clean tapes) := by
rw [numberedRows_factor repeats p slots clean tapes hzero]
exact CleanKeyFactorization.key_factor side (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) (retainedRows repeats p slots clean tapes)
theorem label_eq_retained (hzero : allRowsZero repeats p slots clean tapes)
(labeling : KeyStrategy.Strategy (TreeCanonical.locationCount branch n t))
(side : CanonicalKeys.Side) :
(KeyStrategy.label labeling side (TreeCanonical.numberedSlots slots)
(numberedRows repeats p slots tapes)).val =
(labeling (CleanKeyFactorization.retainedQueryKey side (numberedKeep p clean)
(CleanKeyFactorization.retainedSlots (TreeCanonical.numberedSlots slots) (numberedKeep p clean))
(retainedRows repeats p slots clean tapes))).val := by
rw [numberedRows_factor repeats p slots clean tapes hzero]
exact CleanKeyFactorization.label_factor labeling side (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) (retainedRows repeats p slots clean tapes)
theorem response_eq_retained (hzero : allRowsZero repeats p slots clean tapes)
(labeling : KeyStrategy.Strategy (TreeCanonical.locationCount branch n t))
(side : CanonicalKeys.Side) :
KeyStrategy.response labeling side (TreeCanonical.numberedSlots slots)
(numberedRows repeats p slots tapes) =
CleanKeyFactorization.retainedResponse labeling side (numberedKeep p clean)
(CleanKeyFactorization.retainedSlots (TreeCanonical.numberedSlots slots) (numberedKeep p clean))
(retainedRows repeats p slots clean tapes) := by
rw [numberedRows_factor repeats p slots clean tapes hzero]
exact CleanKeyFactorization.response_factor labeling side (TreeCanonical.numberedSlots slots)
(numberedKeep p clean) (retainedRows repeats p slots clean tapes)
end
end PerfectCompleteness.CutSamplerKeyLocality
namespace PerfectCompleteness.CutSamplerReplay
open scoped BigOperators Classical
open RecursiveSpaces DescendantSpaces RecursiveSampler PointwiseSpaces
noncomputable section
universe u w
variable {branch : Nat β†’ Nat} {n m : Nat}
def Tape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) :
{n m : Nat} β†’ (p : Path branch n (m + 1)) β†’ (A : Slots branch n β†’ Type u) β†’
(clean : Fin (branch m) β†’ Prop) β†’ Type (max u w)
| _, m, .refl _, A, clean =>
(i : {i : Fin (branch m) // Β¬ clean i}) β†’
squareSpace (space π•œ branch m (childFamily A i.val))
| n + 1, _, .step i p, A, clean =>
((j : OffPath i) β†’ squareSpace (space π•œ branch n (childFamily A j.val))) Γ—
((call : Fin (repeats (n + 1)) Γ— Bool) β†’ Tape π•œ repeats p (childFamily A i) clean)
def erase (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) :
{n m : Nat} β†’ (p : Path branch n (m + 1)) β†’ (A : Slots branch n β†’ Type u) β†’
(clean : Fin (branch m) β†’ Prop) β†’ CutSamplerRefinement.Tape π•œ repeats p A β†’
Tape π•œ repeats p A clean
| _, _, .refl _, _, _, tape => fun i => tape () i.val
| _, _, .step i p, A, clean, tape =>
(fun j => tape (.inl j), fun call => erase π•œ repeats p (childFamily A i) clean
(CutSamplerLocality.subTape π•œ repeats i p A tape call.1 call.2))
def evaluate (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) :
{n m : Nat} β†’ (p : Path branch n (m + 1)) β†’ (A : Slots branch n β†’ Type u) β†’
(clean : Fin (branch m) β†’ Prop) β†’ Tape π•œ repeats p A clean β†’ Assignment A β†’ π•œ
| _, m, .refl _, A, clean, tape, x =>
βˆ‘ i : {i : Fin (branch m) // Β¬ clean i}, (tape i).val (childRestriction A i.val x)
| n + 1, _, .step i p, A, clean, tape, x =>
(βˆ‘ j : OffPath i, (tape.1 j).val (childRestriction A j.val x)) +
βˆ‘ h : Fin (repeats (n + 1)),
evaluate π•œ repeats p (childFamily A i) clean (tape.2 (h, false)) (childRestriction A i x) *
evaluate π•œ repeats p (childFamily A i) clean (tape.2 (h, true)) (childRestriction A i x)
theorem evaluate_refl (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat)
(A : Slots branch (m + 1) β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : Tape π•œ repeats (.refl (m + 1)) A clean) (x : Assignment A) :
evaluate π•œ repeats (.refl (m + 1)) A clean tape x =
βˆ‘ i : {i : Fin (branch m) // Β¬ clean i}, (tape i).val (childRestriction A i.val x) := rfl
theorem evaluate_step (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat)
(i : Fin (branch n)) (p : Path branch n (m + 1))
(A : Slots branch (n + 1) β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : Tape π•œ repeats (.step i p) A clean) (x : Assignment A) :
evaluate π•œ repeats (.step i p) A clean tape x =
(βˆ‘ j : OffPath i, (tape.1 j).val (childRestriction A j.val x)) +
βˆ‘ h : Fin (repeats (n + 1)),
evaluate π•œ repeats p (childFamily A i) clean (tape.2 (h, false)) (childRestriction A i x) *
evaluate π•œ repeats p (childFamily A i) clean (tape.2 (h, true))
(childRestriction A i x) := rfl
theorem evaluate_erase (π•œ : Type w) [Field π•œ]
(repeats : Nat β†’ Nat) (p : Path branch n (m + 1)) :
βˆ€ (A : Slots branch n β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : CutSamplerRefinement.Tape π•œ repeats p A),
CutSamplerLocality.ZeroAtClean π•œ repeats p A clean tape β†’
βˆ€ x : Assignment A,
evaluate π•œ repeats p A clean (erase π•œ repeats p A clean tape) x =
(CutSamplerRefinement.evaluate π•œ repeats p A tape).val x := by
induction n generalizing m with
| zero =>
have h := p.height_le
omega
| succ n ih =>
cases p with
| refl =>
intro A clean tape hzero x
have heval :
(UniformChildSum.recursiveSum (π•œ := π•œ) A (tape ())).val x =
βˆ‘ i : Fin (branch n), (tape () i).val (childRestriction A i x) := by
have hsum := congrFun
(UniformChildSum.childSum_apply (π•œ := π•œ) (childRestriction A)
(fun i => space π•œ branch n (childFamily A i)) (tape ())) x
simp only [Finset.sum_apply, PointwiseSpaces.pullback_apply] at hsum
refine hsum.trans ?_
refine Finset.sum_congr (ΞΉ := Fin (branch n)) ?_ ?_
Β· ext i
simp only [Finset.mem_univ]
Β· intro i _
rfl
rw [evaluate_refl, CutSamplerLocality.evaluate_refl, heval]
simp only [erase]
rw [← Fintype.sum_subtype_add_sum_subtype clean
(fun i => (tape () i).val (childRestriction A i x))]
have hz : (βˆ‘ i : {i : Fin (branch n) // clean i},
(tape () i.val).val (childRestriction A i.val x)) = 0 := by
apply Finset.sum_eq_zero
intro i _
rw [hzero i.val i.property]
rfl
rw [hz, zero_add]
| step i p =>
intro A clean tape hzero x
rw [evaluate_step, CutSamplerLocality.evaluate_step]
simp only [erase, RecursiveSampler.combine, RecursiveSampler.combineFunction,
Finset.sum_apply, Pi.add_apply, Pi.mul_apply, PointwiseSpaces.pullback_apply]
apply congrArg (fun z =>
(βˆ‘ j : OffPath i, (tape (.inl j)).val (childRestriction A j.val x)) + z)
apply Finset.sum_congr rfl
intro h _
rw [ih p (childFamily A i) clean
(CutSamplerLocality.subTape π•œ repeats i p A tape h false) (hzero h false),
ih p (childFamily A i) clean
(CutSamplerLocality.subTape π•œ repeats i p A tape h true) (hzero h true)]
theorem evaluate_erase_of_split_zero (π•œ : Type w) [Field π•œ]
(repeats : Nat β†’ Nat) (p : Path branch n (m + 1))
(A : Slots branch n β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : CutSamplerRefinement.Tape π•œ repeats p A)
(hzero : βˆ€ terminal i, clean i β†’
(CutTerminalSplit.splitTape π•œ repeats p A tape).1 terminal i = 0)
(x : Assignment A) :
evaluate π•œ repeats p A clean (erase π•œ repeats p A clean tape) x =
(CutSamplerRefinement.evaluate π•œ repeats p A tape).val x :=
evaluate_erase π•œ repeats p A clean tape
((CutTerminalZero.zeroAtClean_iff π•œ repeats p A clean tape).mp hzero) x
end
end PerfectCompleteness.CutSamplerReplay
namespace PerfectCompleteness.CutSamplerReplayGrouping
open RecursiveSpaces DescendantSpaces RecursiveSampler TerminalCalls PointwiseSpaces
noncomputable section
universe u w
variable {branch : Nat β†’ Nat} {n m : Nat}
abbrev RetainedTerminal (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat)
(p : Path branch n (m + 1)) (A : Slots branch n β†’ Type u)
(clean : Fin (branch m) β†’ Prop) :=
TerminalIndex repeats p β†’ (i : {i : Fin (branch m) // Β¬ clean i}) β†’
squareSpace (space π•œ branch m (childFamily (p.family A) i.val))
def exteriorStepIndex (repeats : Nat β†’ Nat) (i : Fin (branch n))
(p : Path branch n (m + 1)) (call : Fin (repeats (n + 1)) Γ— Bool)
(j : CutTerminalSplit.ExteriorIndex repeats p) :
CutTerminalSplit.ExteriorIndex repeats (.step i p) :=
⟨.inr (call, j.val), j.property⟩
def subExterior (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat)
(i : Fin (branch n)) (p : Path branch n (m + 1))
(A : Slots branch (n + 1) β†’ Type u) (call : Fin (repeats (n + 1)) Γ— Bool)
(exterior : CutTerminalSplit.ExteriorTape π•œ repeats (.step i p) A) :
CutTerminalSplit.ExteriorTape π•œ repeats p (childFamily A i) :=
fun j => exterior (exteriorStepIndex repeats i p call j)
theorem subExterior_splitTape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat)
(i : Fin (branch n)) (p : Path branch n (m + 1))
(A : Slots branch (n + 1) β†’ Type u)
(tape : CutSamplerRefinement.Tape π•œ repeats (.step i p) A)
(h : Fin (repeats (n + 1))) (b : Bool) :
subExterior π•œ repeats i p A (h, b)
(CutTerminalSplit.splitTape π•œ repeats (.step i p) A tape).2 =
(CutTerminalSplit.splitTape π•œ repeats p (childFamily A i)
(CutSamplerLocality.subTape π•œ repeats i p A tape h b)).2 := by
funext j
rfl
def assemble (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) :
{n m : Nat} β†’ (p : Path branch n (m + 1)) β†’ (A : Slots branch n β†’ Type u) β†’
(clean : Fin (branch m) β†’ Prop) β†’
CutTerminalSplit.ExteriorTape π•œ repeats p A β†’
RetainedTerminal π•œ repeats p A clean β†’ CutSamplerReplay.Tape π•œ repeats p A clean
| _, _, .refl _, _, _, _, terminal => terminal ()
| _, _, .step i p, A, clean, exterior, terminal =>
(fun j => exterior ⟨.inl j, not_isTerminal_ordinary repeats i p j⟩,
fun call => assemble π•œ repeats p (childFamily A i) clean
(subExterior π•œ repeats i p A call exterior)
(fun location child => terminal (call, location) child))
theorem assemble_splitTape (π•œ : Type w) [Field π•œ]
(repeats : Nat β†’ Nat) (p : Path branch n (m + 1)) :
βˆ€ (A : Slots branch n β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : CutSamplerRefinement.Tape π•œ repeats p A),
assemble π•œ repeats p A clean (CutTerminalSplit.splitTape π•œ repeats p A tape).2
(fun terminal child =>
(CutTerminalSplit.splitTape π•œ repeats p A tape).1 terminal child.val) =
CutSamplerReplay.erase π•œ repeats p A clean tape := by
induction n generalizing m with
| zero =>
have h := p.height_le
omega
| succ n ih =>
cases p with
| refl =>
intro A clean tape
rfl
| step i p =>
intro A clean tape
apply Prod.ext
Β· funext j
rfl
Β· funext call
rcases call with ⟨h, b⟩
change assemble π•œ repeats p (childFamily A i) clean
(subExterior π•œ repeats i p A (h, b)
(CutTerminalSplit.splitTape π•œ repeats (.step i p) A tape).2)
(fun terminal child =>
(CutTerminalSplit.splitTape π•œ repeats (.step i p) A tape).1
((h, b), terminal) child.val) =
CutSamplerReplay.erase π•œ repeats p (childFamily A i) clean
(CutSamplerLocality.subTape π•œ repeats i p A tape h b)
rw [subExterior_splitTape]
simp_rw [CutTerminalZero.splitTape_terminal_step]
exact ih p (childFamily A i) clean
(CutSamplerLocality.subTape π•œ repeats i p A tape h b)
theorem evaluate_assemble_splitTape (π•œ : Type w) [Field π•œ]
(repeats : Nat β†’ Nat) (p : Path branch n (m + 1))
(A : Slots branch n β†’ Type u) (clean : Fin (branch m) β†’ Prop)
(tape : CutSamplerRefinement.Tape π•œ repeats p A)
(hzero : CutSamplerLocality.ZeroAtClean π•œ repeats p A clean tape) (x : Assignment A) :
CutSamplerReplay.evaluate π•œ repeats p A clean
(assemble π•œ repeats p A clean (CutTerminalSplit.splitTape π•œ repeats p A tape).2
(fun terminal child =>
(CutTerminalSplit.splitTape π•œ repeats p A tape).1 terminal child.val)) x =
(CutSamplerRefinement.evaluate π•œ repeats p A tape).val x := by
rw [assemble_splitTape]
exact CutSamplerReplay.evaluate_erase π•œ repeats p A clean tape hzero x
end
end PerfectCompleteness.CutSamplerReplayGrouping
end OAI