import OAI.Computability.PerfectCompleteness.Construction.LocalReconstruction import OAI.Computability.PerfectCompleteness.Foundations.RecursiveSpaceEquivLemmas import OAI.Computability.PerfectCompleteness.Sampling.CutSamplerKeyLocalityLemmas import OAI.Computability.PerfectCompleteness.Sampling.RecordSeedSamplingLemmas import OAI.Computability.UniqueGames.Foundations.Conditioning import OAI.Computability.UniqueGames.Foundations.SamplingLemmas namespace OAI namespace PerfectCompleteness.CutSamplerReplayTransport open PointwiseSpaces RecursiveSpaces DescendantSpaces RecursiveSampler open CutSamplerKeyLocality open scoped BigOperators Classical noncomputable section universe u v w variable {branch : Nat β†’ Nat} {n m : Nat} (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) theorem squareEquiv_symm_apply {A : Slots branch n β†’ Type u} {B : Slots branch n β†’ Type v} (e : βˆ€ s, A s ≃ B s) (f : squareSpace (space π•œ branch n A)) (y : Assignment B) : ((RecursiveSpaceEquiv.squareEquiv (π•œ := π•œ) e).symm f).val y = f.val (project (fun s => (e s).symm) y) := rfl theorem squareEquiv_symm_eval_of_agree {A : Slots branch n β†’ Type u} {B : Slots branch n β†’ Type v} (e : βˆ€ s, A s ≃ B s) (f : squareSpace (space π•œ branch n A)) (x : Assignment A) (y : Assignment B) (hagree : βˆ€ s, e s (x s) = y s) : ((RecursiveSpaceEquiv.squareEquiv (π•œ := π•œ) e).symm f).val y = f.val x := by rw [squareEquiv_symm_apply] apply congrArg f.val funext s exact (congrArg (e s).symm (hagree s)).symm.trans ((e s).symm_apply_apply (x s)) def transport (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n (m + 1)) β†’ (A : Slots branch n β†’ Type u) β†’ (B : Slots branch n β†’ Type v) β†’ (clean : Fin (branch m) β†’ Prop) β†’ (βˆ€ s, keptLeaf p clean s β†’ A s ≃ B s) β†’ CutSamplerReplay.Tape π•œ repeats p A clean β†’ CutSamplerReplay.Tape π•œ repeats p B clean | _, _, .refl _, _, _, _, e, tape => fun i => (RecursiveSpaceEquiv.squareEquiv (π•œ := π•œ) (fun s => e (i.val, s) i.property)).symm (tape i) | _, _, .step i p, A, B, clean, e, tape => (fun j => (RecursiveSpaceEquiv.squareEquiv (π•œ := π•œ) (fun s => e (j.val, s) (Or.inl j.property))).symm (tape.1 j), fun call => transport π•œ repeats p (childFamily A i) (childFamily B i) clean (fun s hs => e (i, s) (Or.inr hs)) (tape.2 call)) theorem evaluate_transport (p : Path branch n (m + 1)) : βˆ€ (A : Slots branch n β†’ Type u) (B : Slots branch n β†’ Type v) (clean : Fin (branch m) β†’ Prop) (e : βˆ€ s, keptLeaf p clean s β†’ A s ≃ B s) (tape : CutSamplerReplay.Tape π•œ repeats p A clean) (x : Assignment A) (y : Assignment B), (βˆ€ s hs, e s hs (x s) = y s) β†’ CutSamplerReplay.evaluate π•œ repeats p B clean (transport π•œ repeats p A B clean e tape) y = CutSamplerReplay.evaluate π•œ repeats p A clean tape x := by induction n generalizing m with | zero => have h := p.height_le omega | succ n ih => cases p with | refl => intro A B clean e tape x y hagree simp only [CutSamplerReplay.evaluate_refl] apply Finset.sum_congr rfl intro i _ exact squareEquiv_symm_eval_of_agree π•œ (fun s => e (i.val, s) i.property) (tape i) (childRestriction A i.val x) (childRestriction B i.val y) (fun s => hagree (i.val, s) i.property) | step i p => intro A B clean e tape x y hagree simp only [CutSamplerReplay.evaluate_step] apply congrArgβ‚‚ (fun a b : π•œ => a + b) Β· apply Finset.sum_congr rfl intro j _ exact squareEquiv_symm_eval_of_agree π•œ (fun s => e (j.val, s) (Or.inl j.property)) (tape.1 j) (childRestriction A j.val x) (childRestriction B j.val y) (fun s => hagree (j.val, s) (Or.inl j.property)) Β· apply Finset.sum_congr rfl intro h _ apply congrArgβ‚‚ (fun a b : π•œ => a * b) Β· exact ih p (childFamily A i) (childFamily B i) clean (fun s hs => e (i, s) (Or.inr hs)) (tape.2 (h, false)) (childRestriction A i x) (childRestriction B i y) (fun s hs => hagree (i, s) (Or.inr hs)) Β· exact ih p (childFamily A i) (childFamily B i) clean (fun s hs => e (i, s) (Or.inr hs)) (tape.2 (h, true)) (childRestriction A i x) (childRestriction B i y) (fun s hs => hagree (i, s) (Or.inr hs)) end end PerfectCompleteness.CutSamplerReplayTransport section namespace PerfectCompleteness.LocalReconstruction.Recipe open scoped BigOperators variable {π•œ Atom Atom' X J : Type*} [Field π•œ] def map (f : Atom β†’ Atom') : Recipe π•œ Atom β†’ Recipe π•œ Atom' | .constant c => .constant c | .atom a => .atom (f a) | .add r s => .add (map f r) (map f s) | .mul r s => .mul (map f r) (map f s) theorem evaluate_map (f : Atom β†’ Atom') (atoms : Atom' β†’ X β†’ π•œ) (r : Recipe π•œ Atom) : (r.map f).evaluate atoms = r.evaluate (fun a => atoms (f a)) := by induction r with | constant c => rfl | atom a => rfl | add r s hr hs => simp only [map, evaluate, hr, hs] | mul r s hr hs => simp only [map, evaluate, hr, hs] def sumList (f : J β†’ Recipe π•œ Atom) : List J β†’ Recipe π•œ Atom | [] => .constant 0 | j :: js => .add (f j) (sumList f js) theorem evaluate_sumList (f : J β†’ Recipe π•œ Atom) (js : List J) (atoms : Atom β†’ X β†’ π•œ) : (sumList f js).evaluate atoms = (js.map fun j => (f j).evaluate atoms).sum := by induction js with | nil => rfl | cons j js ih => simp only [sumList, evaluate, List.map_cons, List.sum_cons, ih] noncomputable def sumIndexed [Fintype J] (f : J β†’ Recipe π•œ Atom) : Recipe π•œ Atom := sumList f Finset.univ.toList theorem evaluate_sumIndexed [Fintype J] (f : J β†’ Recipe π•œ Atom) (atoms : Atom β†’ X β†’ π•œ) : (sumIndexed f).evaluate atoms = βˆ‘ j, (f j).evaluate atoms := by rw [sumIndexed, evaluate_sumList, Finset.sum_map_toList] end PerfectCompleteness.LocalReconstruction.Recipe namespace PerfectCompleteness.StoppedSamplerRecipe open scoped BigOperators open PointwiseSpaces RecursiveSpaces DescendantSpaces RecursiveSampler open LocalReconstruction noncomputable section universe u w variable {branch : Nat β†’ Nat} {n m : Nat} def ordinaryTag (repeats : Nat β†’ Nat) (i : Fin (branch n)) (p : Path branch n m) (j : OffPath i) : DrawIndex repeats (.step i p) := Sum.inl j def factorTag (repeats : Nat β†’ Nat) (i : Fin (branch n)) (p : Path branch n m) (h : Fin (repeats (n + 1))) (b : Bool) (j : DrawIndex repeats p) : DrawIndex repeats (.step i p) := Sum.inr ((h, b), j) def recipe (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n m) β†’ Recipe π•œ (DrawIndex repeats p) | _, _, .refl _ => .atom () | n + 1, _, .step i p => .add (Recipe.sumIndexed (fun j : OffPath i => .atom (ordinaryTag repeats i p j))) (Recipe.sumIndexed (fun h : Fin (repeats (n + 1)) => .mul ((recipe π•œ repeats p).map (factorTag repeats i p h false)) ((recipe π•œ repeats p).map (factorTag repeats i p h true)))) def atoms (π•œ : Type w) [Field π•œ] (repeats : Nat β†’ Nat) : {n m : Nat} β†’ (p : Path branch n m) β†’ (A : Slots branch n β†’ Type u) β†’ Tape π•œ repeats p A β†’ DrawIndex repeats p β†’ Assignment A β†’ π•œ | _, _, .refl _, _, t, j => (t j).val | _, _, .step i p, A, t, j => match j with | Sum.inl j => pullback π•œ (childRestriction A j.val) (t (Sum.inl j)).val | Sum.inr j => pullback π•œ (childRestriction A i) (atoms π•œ repeats p (childFamily A i) (subTape π•œ repeats i p A t j.1.1 j.1.2) j.2) variable (π•œ : Type w) [Field π•œ] theorem evaluate_recipe (repeats : Nat β†’ Nat) (p : Path branch n m) : βˆ€ (A : Slots branch n β†’ Type u) (t : Tape π•œ repeats p A), (recipe π•œ repeats p).evaluate (atoms π•œ repeats p A t) = (RecursiveSampler.evaluate π•œ repeats p A t).val := by induction p with | refl n => intro A t rfl | @step n m i p ih => intro A t have child (h : Fin (repeats (n + 1))) (b : Bool) : ((recipe π•œ repeats p).map (factorTag repeats i p h b)).evaluate (atoms π•œ repeats (.step i p) A t) = (RecursiveSampler.evaluate π•œ repeats p (childFamily A i) (subTape π•œ repeats i p A t h b)).val ∘ childRestriction A i := by rw [Recipe.evaluate_map] change (recipe π•œ repeats p).evaluate (fun j => atoms π•œ repeats p (childFamily A i) (subTape π•œ repeats i p A t h b) j ∘ childRestriction A i) = _ rw [Recipe.evaluate_pullback, ih] simp only [recipe, Recipe.evaluate, Recipe.evaluate_sumIndexed, child] funext x simp only [RecursiveSampler.evaluate, combine, combineFunction, Finset.sum_apply, Pi.add_apply, Pi.mul_apply, Function.comp_apply, atoms, ordinaryTag, PointwiseSpaces.pullback_apply] theorem evaluate_factors {Z : Type*} (repeats : Nat β†’ Nat) (p : Path branch n m) (A : Slots branch n β†’ Type u) (t : Tape π•œ repeats p A) (other : Assignment A β†’ Z) (known : βˆ€ j, atoms π•œ repeats p A t j ∈ FactoredFunctions.factoringSpace other) : (RecursiveSampler.evaluate π•œ repeats p A t).val ∈ FactoredFunctions.factoringSpace other := by rw [← evaluate_recipe π•œ repeats p A t] exact Recipe.evaluate_factors other (atoms π•œ repeats p A t) known (recipe π•œ repeats p) end end PerfectCompleteness.StoppedSamplerRecipe end namespace PerfectCompleteness.UniformConditioning noncomputable section open scoped BigOperators Classical open UniqueGamesTheorem.Foundations.Games variable {Ξ© Ξ“ : Type*} [Fintype Ξ©] [Fintype Ξ“] [Nonempty Ξ©] [Nonempty Ξ“] theorem uniform_transport (e : Ξ© ≃ Ξ“) : (FiniteDistribution.uniform Ξ©).transport e = FiniteDistribution.uniform Ξ“ := by apply FiniteDistribution.eq_of_weight_eq intro y change 1 / (Fintype.card Ξ© : ℝ) = 1 / (Fintype.card Ξ“ : ℝ) rw [Fintype.card_congr e] theorem uniform_probability_equiv (e : Ξ© ≃ Ξ“) (event : Ξ“ β†’ Bool) : (FiniteDistribution.uniform Ξ©).probability (fun x => event (e x)) = (FiniteDistribution.uniform Ξ“).probability event := by rw [← uniform_transport e, FiniteDistribution.probability_transport] theorem uniform_probability (T : Ξ© β†’ Prop) : (FiniteDistribution.uniform Ξ©).probability (fun x => decide (T x)) = (Fintype.card {x : Ξ© // T x} : ℝ) / (Fintype.card Ξ© : ℝ) := by calc _ = (βˆ‘ x : Ξ©, if T x then (1 : ℝ) else 0) / (Fintype.card Ξ© : ℝ) := by change (βˆ‘ x : Ξ©, if decide (T x) then 1 / (Fintype.card Ξ© : ℝ) else 0) = _ simp only [div_eq_mul_inv, Finset.sum_mul] apply Finset.sum_congr rfl intro x _ by_cases hx : T x <;> simp [hx] _ = _ := by simp only [Fintype.card_subtype, Finset.sum_boole] theorem uniform_probability_pos (T : Ξ© β†’ Prop) [Nonempty {x : Ξ© // T x}] : 0 < (FiniteDistribution.uniform Ξ©).probability (fun x => decide (T x)) := by rw [uniform_probability] exact div_pos (Nat.cast_pos.mpr Fintype.card_pos) (Nat.cast_pos.mpr Fintype.card_pos) omit [Nonempty Ξ©] in theorem uniform_subtype_pushforward_weight (T : Ξ© β†’ Prop) [Nonempty {x : Ξ© // T x}] (x : Ξ©) : ((FiniteDistribution.uniform {x : Ξ© // T x}).pushforward Subtype.val).weight x = if T x then 1 / (Fintype.card {x : Ξ© // T x} : ℝ) else 0 := by change (βˆ‘ y : {x : Ξ© // T x}, if y.val = x then 1 / (Fintype.card {x : Ξ© // T x} : ℝ) else 0) = _ by_cases hx : T x Β· rw [ite_eq_left hx] rw [Finset.sum_eq_single (⟨x, hx⟩ : {x : Ξ© // T x})] Β· simp Β· intro y _ hy have hne : y.val β‰  x := fun heq => hy (Subtype.ext heq) simp [hne] Β· simp Β· rw [ite_eq_right hx] apply Finset.sum_eq_zero intro y _ have hne : y.val β‰  x := fun heq => hx (heq β–Έ y.property) simp [hne] theorem condition_uniform (T : Ξ© β†’ Prop) [Nonempty {x : Ξ© // T x}] (positive : 0 < (FiniteDistribution.uniform Ξ©).probability (fun x => decide (T x))) : (FiniteDistribution.uniform Ξ©).condition (fun x => decide (T x)) positive = (FiniteDistribution.uniform {x : Ξ© // T x}).pushforward Subtype.val := by apply FiniteDistribution.eq_of_weight_eq intro x rw [uniform_subtype_pushforward_weight] change (if decide (T x) then (1 / (Fintype.card Ξ© : ℝ)) / (FiniteDistribution.uniform Ξ©).probability (fun x => decide (T x)) else 0) = _ rw [uniform_probability] have hcard : (Fintype.card Ξ© : ℝ) β‰  0 := Nat.cast_ne_zero.mpr Fintype.card_ne_zero by_cases hx : T x Β· simpa [hx] using div_div_div_cancel_rightβ‚€ hcard (1 : ℝ) (Fintype.card {x : Ξ© // T x} : ℝ) Β· simp [hx] theorem probability_condition_uniform (T : Ξ© β†’ Prop) [Nonempty {x : Ξ© // T x}] (positive : 0 < (FiniteDistribution.uniform Ξ©).probability (fun x => decide (T x))) (event : Ξ© β†’ Bool) : ((FiniteDistribution.uniform Ξ©).condition (fun x => decide (T x)) positive).probability event = (FiniteDistribution.uniform {x : Ξ© // T x}).probability (fun x => event x.val) := by rw [condition_uniform, FiniteDistribution.probability_pushforward] end end PerfectCompleteness.UniformConditioning end OAI