import OAI.Computability.PerfectCompleteness.Foundations.HierarchicalFrozenTablesLemmas import OAI.Computability.PerfectCompleteness.Sampling.BucketUniformLemmas namespace OAI section namespace PerfectCompleteness.SamplerComposition open PointwiseSpaces RecursiveSpaces DescendantSpaces RecursiveSampler noncomputable section universe u w variable {branch : Nat β†’ Nat} {n m k : Nat} def ExpansionSpace (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m k : Nat} β†’ (p : Path branch n m) β†’ (q : Path branch m k) β†’ (A : Slots branch n β†’ Type u) β†’ DrawIndex repeats p β†’ Type (max u w) | _, _, _, .refl _, q, A, _ => RecursiveSampler.Tape π•œ repeats q A | n + 1, _, _, .step i p, q, A, j => match j with | Sum.inl j => squareSpace (space π•œ branch n (childFamily A j.val)) | Sum.inr j => ExpansionSpace π•œ repeats p q (childFamily A i) j.2 abbrev ExpandedTape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) := (j : DrawIndex repeats p) β†’ ExpansionSpace π•œ repeats p q A j def zeroFactor (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m k : Nat} β†’ (p : Path branch n m) β†’ (q : Path branch m k) β†’ (A : Slots branch n β†’ Type u) β†’ (j : DrawIndex repeats p) β†’ ExpansionSpace π•œ repeats p q A j | _, _, _, .refl _, q, A, _ => zeroTape π•œ repeats q A | n + 1, _, _, .step i p, q, A, j => match j with | Sum.inl j => (0 : squareSpace (space π•œ branch n (childFamily A j.val))) | Sum.inr j => zeroFactor π•œ repeats p q (childFamily A i) j.2 def zeroExpandedTape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) : ExpandedTape π•œ repeats p q A := zeroFactor π•œ repeats p q A def expandedSubTape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (i : Fin (branch n)) (p : Path branch n m) (q : Path branch m k) (A : Slots branch (n + 1) β†’ Type u) (t : ExpandedTape π•œ repeats (.step i p) q A) (h : Fin (repeats (n + 1))) (b : Bool) : ExpandedTape π•œ repeats p q (childFamily A i) := fun j => t (.inr ((h, b), j)) def expand (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m k : Nat} β†’ (p : Path branch n m) β†’ (q : Path branch m k) β†’ (A : Slots branch n β†’ Type u) β†’ RecursiveSampler.Tape π•œ repeats (p.append q) A β†’ ExpandedTape π•œ repeats p q A | _, _, _, .refl _, _, _, t => fun _ => t | _n + 1, _, _, .step i p, q, A, t => fun j => match j with | Sum.inl j => t (.inl j) | Sum.inr j => expand π•œ repeats p q (childFamily A i) (subTape π•œ repeats i (p.append q) A t j.1.1 j.1.2) j.2 def flatten (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m k : Nat} β†’ (p : Path branch n m) β†’ (q : Path branch m k) β†’ (A : Slots branch n β†’ Type u) β†’ ExpandedTape π•œ repeats p q A β†’ RecursiveSampler.Tape π•œ repeats (p.append q) A | _, _, _, .refl _, _, _, t => t () | _n + 1, _, _, .step i p, q, A, t => fun j => match j with | Sum.inl j => t (.inl j) | Sum.inr j => flatten π•œ repeats p q (childFamily A i) (expandedSubTape π•œ repeats i p q A t j.1.1 j.1.2) j.2 theorem flatten_expand (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (q : Path branch m k) (A : Slots branch n β†’ Type u) (t : RecursiveSampler.Tape π•œ repeats (p.append q) A), flatten π•œ repeats p q A (expand π•œ repeats p q A t) = t := by induction p with | refl n => intro q A t; rfl | @step n m i p ih => intro q A t funext j cases j with | inl j => rfl | inr j => exact congrFun (ih q (childFamily A i) (subTape π•œ repeats i (p.append q) A t j.1.1 j.1.2)) j.2 theorem expand_flatten (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (q : Path branch m k) (A : Slots branch n β†’ Type u) (t : ExpandedTape π•œ repeats p q A), expand π•œ repeats p q A (flatten π•œ repeats p q A t) = t := by induction p with | refl n => intro q A t funext j cases j rfl | @step n m i p ih => intro q A t funext j cases j with | inl j => rfl | inr j => exact congrFun (ih q (childFamily A i) (expandedSubTape π•œ repeats i p q A t j.1.1 j.1.2)) j.2 def expandEquiv (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) : RecursiveSampler.Tape π•œ repeats (p.append q) A ≃ ExpandedTape π•œ repeats p q A where toFun := expand π•œ repeats p q A invFun := flatten π•œ repeats p q A left_inv := flatten_expand π•œ repeats p q A right_inv := expand_flatten π•œ repeats p q A def collapseFactor (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m k : Nat} β†’ (p : Path branch n m) β†’ (q : Path branch m k) β†’ (A : Slots branch n β†’ Type u) β†’ (j : DrawIndex repeats p) β†’ ExpansionSpace π•œ repeats p q A j β†’ DrawSpace π•œ repeats p A j | _, _, _, .refl _, q, A, _ => RecursiveSampler.evaluate π•œ repeats q A | _, _, _, .step i p, q, A, j => match j with | Sum.inl _ => fun t => t | Sum.inr j => collapseFactor π•œ repeats p q (childFamily A i) j.2 def collapseExpanded (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) : ExpandedTape π•œ repeats p q A β†’ RecursiveSampler.Tape π•œ repeats p A := fun t j => collapseFactor π•œ repeats p q A j (t j) def collapseSuffix (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) : RecursiveSampler.Tape π•œ repeats (p.append q) A β†’ RecursiveSampler.Tape π•œ repeats p A := collapseExpanded π•œ repeats p q A ∘ expandEquiv π•œ repeats p q A theorem evaluate_collapseExpanded (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (q : Path branch m k) (A : Slots branch n β†’ Type u) (t : ExpandedTape π•œ repeats p q A), RecursiveSampler.evaluate π•œ repeats p A (collapseExpanded π•œ repeats p q A t) = RecursiveSampler.evaluate π•œ repeats (p.append q) A (flatten π•œ repeats p q A t) := by induction p with | refl n => intro q A t; rfl | @step n m i p ih => intro q A t have hl := funext fun h : Fin (repeats (n + 1)) => ih q (childFamily A i) (expandedSubTape π•œ repeats i p q A t h false) have hr := funext fun h : Fin (repeats (n + 1)) => ih q (childFamily A i) (expandedSubTape π•œ repeats i p q A t h true) exact congrArgβ‚‚ (fun (left right : Fin (repeats (n + 1)) β†’ space π•œ branch n (childFamily A i)) => combine π•œ A i (repeats (n + 1)) (fun j => t (.inl j)) left right) hl hr theorem evaluate_collapseSuffix (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (q : Path branch m k) (A : Slots branch n β†’ Type u) (t : RecursiveSampler.Tape π•œ repeats (p.append q) A) : RecursiveSampler.evaluate π•œ repeats p A (collapseSuffix π•œ repeats p q A t) = RecursiveSampler.evaluate π•œ repeats (p.append q) A t := (evaluate_collapseExpanded π•œ repeats p q A (expand π•œ repeats p q A t)).trans (congrArg (RecursiveSampler.evaluate π•œ repeats (p.append q) A) (flatten_expand π•œ repeats p q A t)) end end PerfectCompleteness.SamplerComposition end end OAI