import OAI.Computability.PerfectCompleteness.Repetition.CleanConditioning import OAI.Computability.PerfectCompleteness.Sampling.RecordSeedSamplingLemmas import OAI.Computability.PerfectCompleteness.Sampling.UniformChildSum import OAI.Computability.UniqueGames.Foundations.SamplingLemmas namespace OAI section namespace PerfectCompleteness.CutSamplerRefinement open PointwiseSpaces RecursiveSpaces DescendantSpaces RecursiveSampler open UniqueGamesTheorem.Foundations.Games open scoped Classical noncomputable section universe u w variable {branch : Nat β†’ Nat} {n m : Nat} def RefinedSpace (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n m) β†’ (A : Slots branch n β†’ Type u) β†’ DrawIndex repeats p β†’ Type (max u w) | 0, _, .refl _, A, _ => space π•œ branch 0 A | n + 1, _, .refl _, A, _ => (i : Fin (branch n)) β†’ squareSpace (space π•œ branch n (childFamily A i)) | n + 1, _, .step i p, A, j => match j with | Sum.inl j => squareSpace (space π•œ branch n (childFamily A j.val)) | Sum.inr j => RefinedSpace π•œ repeats p (childFamily A i) j.2 def zeroFactor (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n m) β†’ (A : Slots branch n β†’ Type u) β†’ (j : DrawIndex repeats p) β†’ RefinedSpace π•œ repeats p A j | 0, _, .refl _, A, _ => (0 : space π•œ branch 0 A) | n + 1, _, .refl _, A, _ => fun i => (0 : squareSpace (space π•œ branch n (childFamily A i))) | n + 1, _, .step i p, A, j => match j with | Sum.inl j => (0 : squareSpace (space π•œ branch n (childFamily A j.val))) | Sum.inr j => zeroFactor π•œ repeats p (childFamily A i) j.2 instance refinedNonempty (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) (j : DrawIndex repeats p) : Nonempty (RefinedSpace π•œ repeats p A j) := ⟨zeroFactor π•œ repeats p A j⟩ theorem refined_finite (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (A : Slots branch n β†’ Type u), (βˆ€ s, Finite (A s)) β†’ βˆ€ j : DrawIndex repeats p, Finite (RefinedSpace π•œ repeats p A j) := by induction p with | refl n => intro A hA j let : βˆ€ s, Finite (A s) := hA cases n with | zero => change Finite (space π•œ branch 0 A) infer_instance | succ n => change Finite ((i : Fin (branch n)) β†’ squareSpace (space π•œ branch n (childFamily A i))) let : βˆ€ i : Fin (branch n), Finite (squareSpace (space π•œ branch n (childFamily A i))) := fun i => by let : βˆ€ s, Finite ((childFamily A i) s) := fun s => hA (i, s) infer_instance infer_instance | @step n m i p ih => intro A hA j cases j with | inl j => let : βˆ€ s, Finite ((childFamily A j.val) s) := fun s => hA (j.val, s) change Finite (squareSpace (space π•œ branch n (childFamily A j.val))) infer_instance | inr j => exact ih (childFamily A i) (fun s => hA (i, s)) j.2 instance refinedFinite (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] (j : DrawIndex repeats p) : Finite (RefinedSpace π•œ repeats p A j) := refined_finite π•œ repeats p A (fun _ => inferInstance) j instance refinedFintype (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] (j : DrawIndex repeats p) : Fintype (RefinedSpace π•œ repeats p A j) := Fintype.ofFinite _ def collapseFactor (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n m) β†’ (A : Slots branch n β†’ Type u) β†’ (j : DrawIndex repeats p) β†’ RefinedSpace π•œ repeats p A j β†’ DrawSpace π•œ repeats p A j | 0, _, .refl _, _, _, x => x | _ + 1, _, .refl _, A, _, x => UniformChildSum.recursiveSum A x | _ + 1, _, .step i p, A, j, x => match j with | Sum.inl _ => x | Sum.inr j => collapseFactor π•œ repeats p (childFamily A i) j.2 x abbrev Tape (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) := (j : DrawIndex repeats p) β†’ RefinedSpace π•œ repeats p A j def collapse (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) : Tape π•œ repeats p A β†’ RecursiveSampler.Tape π•œ repeats p A := fun x j => collapseFactor π•œ repeats p A j (x j) def tapeLaw (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] : FiniteDistribution (Tape π•œ repeats p A) := FiniteProduct.law (fun j => FiniteDistribution.uniform (RefinedSpace π•œ repeats p A j)) private theorem pushforward_identity {Ξ© : Type*} [Fintype Ξ©] (ΞΌ : FiniteDistribution Ξ©) : ΞΌ.pushforward (fun x => x) = ΞΌ := by change ΞΌ.pushforward (Equiv.refl Ξ©) = ΞΌ rw [FiniteDistribution.pushforward_equiv] exact FiniteDistribution.eq_of_weight_eq (fun _ => rfl) theorem collapseFactor_uniform (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] (j : DrawIndex repeats p), (FiniteDistribution.uniform (RefinedSpace π•œ repeats p A j)).pushforward (collapseFactor π•œ repeats p A j) = FiniteDistribution.uniform (DrawSpace π•œ repeats p A j) := by induction p with | refl n => intro A hA j cases n with | zero => let : βˆ€ s, Finite (A s) := hA let : Fintype (space π•œ branch 0 A) := Fintype.ofFinite _ change (FiniteDistribution.uniform (space π•œ branch 0 A)).pushforward (fun x => x) = FiniteDistribution.uniform (space π•œ branch 0 A) exact pushforward_identity _ | succ n => let : Fintype ((i : Fin (branch n)) β†’ squareSpace (space π•œ branch n (childFamily A i))) := refinedFintype π•œ repeats (.refl (n + 1)) A j let : Fintype (space π•œ branch (n + 1) A) := RecursiveSampler.drawSpaceFintype π•œ repeats (.refl (n + 1)) A j exact UniformLinearImage.uniform_pushforward_linearMap (UniformChildSum.recursiveSum (π•œ := π•œ) A) (UniformChildSum.recursiveSum_surjective (π•œ := π•œ) A) | @step n m i p ih => intro A hA j cases j with | inl j => let : βˆ€ s, Finite ((childFamily A j.val) s) := fun s => hA (j.val, s) let : Fintype (squareSpace (space π•œ branch n (childFamily A j.val))) := Fintype.ofFinite _ change (FiniteDistribution.uniform (squareSpace (space π•œ branch n (childFamily A j.val)))).pushforward (fun x => x) = FiniteDistribution.uniform (squareSpace (space π•œ branch n (childFamily A j.val))) apply FiniteDistribution.eq_of_weight_eq intro x simp [FiniteDistribution.pushforward] | inr j => let : βˆ€ s, Finite ((childFamily A i) s) := fun s => hA (i, s) exact ih (childFamily A i) j.2 theorem collapse_tapeLaw (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] : (tapeLaw π•œ repeats p A).pushforward (collapse π•œ repeats p A) = RecursiveSampler.tapeLaw π•œ repeats p A := by unfold tapeLaw collapse rw [CleanConditioning.pushforward_law] simp_rw [collapseFactor_uniform] rfl def evaluate (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) (x : Tape π•œ repeats p A) : space π•œ branch n A := RecursiveSampler.evaluate π•œ repeats p A (collapse π•œ repeats p A x) theorem evaluate_law (π•œ : Type w) [Field π•œ] [Finite π•œ] (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) [βˆ€ s, Finite (A s)] [Fintype (space π•œ branch n A)] : (tapeLaw π•œ repeats p A).pushforward (evaluate π•œ repeats p A) = RecursiveSampler.law π•œ repeats p A := by change (tapeLaw π•œ repeats p A).pushforward ((RecursiveSampler.evaluate π•œ repeats p A) ∘ collapse π•œ repeats p A) = _ calc _ = ((tapeLaw π•œ repeats p A).pushforward (collapse π•œ repeats p A)).pushforward (RecursiveSampler.evaluate π•œ repeats p A) := (FiniteDistribution.pushforward_comp (tapeLaw π•œ repeats p A) (collapse π•œ repeats p A) (RecursiveSampler.evaluate π•œ repeats p A)).symm _ = _ := by rw [collapse_tapeLaw] rfl end end PerfectCompleteness.CutSamplerRefinement end end OAI