File size: 14,588 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import Mathlib.Data.Fintype.EquivFin
import Mathlib.Data.Fintype.Sum
import OAI.Computability.PerfectCompleteness.Construction.DescendantSpacesLemmas
import OAI.Computability.PerfectCompleteness.Decoding.DependentDecoderSampling
import OAI.Computability.PerfectCompleteness.Sampling.RecursiveSamplerLaws

namespace OAI


namespace PerfectCompleteness.RecordSeedSampling

noncomputable section

open scoped BigOperators Classical
open UniqueGamesTheorem.Foundations.Games

variable {X R : Type*} [Fintype X] [Fintype R] [DecidableEq R]
  {Seed : R → Type*} [∀ r, Fintype (Seed r)]

def freshLaw (P : FiniteDistribution X) (record : X → R)
    (kernel : (r : R) → FiniteDistribution (Seed r)) :
    FiniteDistribution (Σ x : X, Seed (record x)) :=
  CompletionSoundness.sigmaLaw P (fun x => kernel (record x))

theorem probability_eq_table_mean (P : FiniteDistribution X) (record : X → R)
    (kernel : (r : R) → FiniteDistribution (Seed r))
    (event : (x : X) → Seed (record x) → Bool) :
    (freshLaw P record kernel).probability (fun z => event z.1 z.2) =
      (FiniteProduct.law kernel).expectation
        (fun table => P.probability (fun x => event x (table (record x)))) := by
  rw [freshLaw, CompletionSoundness.sigmaLaw_probability]
  calc
    _ = P.expectation (fun x => (FiniteProduct.law kernel).probability
        (fun table => event x (table (record x)))) := by
      apply FiniteDistribution.expectation_congr
      intro x
      have h := FiniteDistribution.probability_pushforward
        (FiniteProduct.law kernel) (fun table => table (record x)) (event x)
      rw [FiniteProduct.eval_pushforward] at h
      exact h
    _ = P.expectation (fun x => (FiniteProduct.law kernel).expectation
        (fun table => if event x (table (record x)) then 1 else 0)) := by
      simp only [FiniteDistribution.probability, FiniteDistribution.expectation,
        mul_ite, mul_one, mul_zero]
    _ = (FiniteProduct.law kernel).expectation (fun table => P.expectation
        (fun x => if event x (table (record x)) then 1 else 0)) :=
      FiniteDistribution.expectation_comm P (FiniteProduct.law kernel) _
    _ = _ := by
      simp only [FiniteDistribution.probability, FiniteDistribution.expectation,
        mul_ite, mul_one, mul_zero]

theorem supported_row (kernel : (r : R) → FiniteDistribution (Seed r))
    (table : (r : R) → Seed r)
    (hs : (FiniteProduct.law kernel).weight table ≠ 0) (r : R) :
    (kernel r).weight (table r) ≠ 0 := by
  intro hr
  exact hs (Finset.prod_eq_zero (Finset.mem_univ r) hr)

theorem probability_supported_guard (P : FiniteDistribution X) (event : X → Bool) :
    P.probability (fun x => decide (P.weight x ≠ 0) && event x) = P.probability event := by
  unfold FiniteDistribution.probability
  apply Finset.sum_congr rfl
  intro x _
  by_cases hx : P.weight x = 0
  · simp only [hx, ne_eq, not_true_eq_false, decide_false, Bool.false_and,
      ite_self]
  · simp only [decide_eq_true hx, Bool.true_and]

theorem probability_le_of_supported_tables (P : FiniteDistribution X) (record : X → R)
    (kernel : (r : R) → FiniteDistribution (Seed r))
    (event : (x : X) → Seed (record x) → Bool) (bound : ℝ)
    (hbound : ∀ table, (FiniteProduct.law kernel).weight table ≠ 0 →
      P.probability (fun x => decide (P.weight x ≠ 0) && event x (table (record x))) ≤ bound) :
    (freshLaw P record kernel).probability (fun z => event z.1 z.2) ≤ bound := by
  rw [probability_eq_table_mean]
  apply DecoderTableAdmissibility.expectation_le_of_support
  intro table hs
  rw [← probability_supported_guard P (fun x => event x (table (record x)))]
  exact hbound table hs

end
end PerfectCompleteness.RecordSeedSampling



namespace PerfectCompleteness.RecursiveSampler

open PointwiseSpaces RecursiveSpaces DescendantSpaces
open UniqueGamesTheorem.Foundations.Games
open scoped BigOperators

universe u w

variable {branch : Nat → Nat} {n m : Nat}

abbrev OffPath (i : Fin (branch n)) := {j : Fin (branch n) // j ≠ i}

def DrawIndex (repeats : Nat → Nat) : {n m : Nat} → Path branch n m → Type
  | _, _, .refl _ => Unit
  | n + 1, _, .step i p =>
      OffPath i ⊕ ((Fin (repeats (n + 1)) × Bool) × DrawIndex repeats p)

noncomputable instance drawIndexFintype (repeats : Nat → Nat) (p : Path branch n m) :
    Fintype (DrawIndex repeats p) := by
  induction p with
  | refl n => exact inferInstanceAs (Fintype Unit)
  | @step n m i p ih =>
      letI : Fintype (DrawIndex repeats p) := ih
      exact inferInstanceAs (Fintype
        (OffPath i ⊕ ((Fin (repeats (n + 1)) × Bool) × DrawIndex repeats p)))

noncomputable instance drawIndexDecidableEq (repeats : Nat → Nat) (p : Path branch n m) :
    DecidableEq (DrawIndex repeats p) := by
  induction p with
  | refl n => exact inferInstanceAs (DecidableEq Unit)
  | @step n m i p ih =>
      letI : DecidableEq (DrawIndex repeats p) := ih
      exact inferInstanceAs (DecidableEq
        (OffPath i ⊕ ((Fin (repeats (n + 1)) × Bool) × DrawIndex repeats p)))

variable (𝕜 : Type w) [Field 𝕜]

def DrawSpace (𝕜 : 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)
  | n, _, .refl _, A, _ => space 𝕜 branch n A
  | n + 1, _, .step i p, A, j =>
      match j with
      | Sum.inl j => squareSpace (space 𝕜 branch n (childFamily A j.val))
      | Sum.inr j => DrawSpace 𝕜 repeats p (childFamily A i) j.2

abbrev Tape (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) :=
  (j : DrawIndex repeats p) → DrawSpace 𝕜 repeats p A j

def zeroDraw (𝕜 : Type w) [Field 𝕜] (repeats : Nat → Nat) :
    {n m : Nat} → (p : Path branch n m) → (A : Slots branch n → Type u) →
      (j : DrawIndex repeats p) → DrawSpace 𝕜 repeats p A j
  | n, _, .refl _, A, _ => (0 : space 𝕜 branch n A)
  | 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 => zeroDraw 𝕜 repeats p (childFamily A i) j.2

def zeroTape (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) : Tape 𝕜 repeats p A :=
  zeroDraw 𝕜 repeats p A

instance drawSpaceNonempty (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) (j : DrawIndex repeats p) :
    Nonempty (DrawSpace 𝕜 repeats p A j) :=
  ⟨zeroDraw 𝕜 repeats p A j⟩

instance tapeNonempty (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) : Nonempty (Tape 𝕜 repeats p A) :=
  ⟨zeroTape 𝕜 repeats p A⟩

theorem drawSpace_finite [Finite 𝕜] (repeats : Nat → Nat) (p : Path branch n m) :
    ∀ (A : Slots branch n → Type u), (∀ s, Finite (A s)) →
      ∀ j : DrawIndex repeats p, Finite (DrawSpace 𝕜 repeats p A j) := by
  induction p with
  | refl n =>
      intro A hA j
      let : ∀ s, Finite (A s) := hA
      change Finite (space 𝕜 branch n A)
      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 drawSpaceFinite [Finite 𝕜] (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) [∀ s, Finite (A s)] (j : DrawIndex repeats p) :
    Finite (DrawSpace 𝕜 repeats p A j) :=
  drawSpace_finite 𝕜 repeats p A (fun _ => inferInstance) j

noncomputable instance drawSpaceFintype [Finite 𝕜] (repeats : Nat → Nat)
    (p : Path branch n m) (A : Slots branch n → Type u) [∀ s, Finite (A s)]
    (j : DrawIndex repeats p) : Fintype (DrawSpace 𝕜 repeats p A j) :=
  Fintype.ofFinite _

def subTape (repeats : Nat → Nat) (i : Fin (branch n)) (p : Path branch n m)
    (A : Slots branch (n + 1) → Type u) (t : Tape 𝕜 repeats (.step i p) A)
    (h : Fin (repeats (n + 1))) (b : Bool) :
    Tape 𝕜 repeats p (childFamily A i) :=
  fun j => t (.inr ((h, b), j))

theorem recursive_locations_distinct (repeats : Nat → Nat)
    (i : Fin (branch n)) (p : Path branch n m)
    (h h' : Fin (repeats (n + 1))) (b b' : Bool)
    (j j' : DrawIndex repeats p) (hne : (h, b) ≠ (h', b')) :
    (Sum.inr ((h, b), j) : DrawIndex repeats (.step i p)) ≠
      Sum.inr ((h', b'), j') := by
  intro heq
  exact hne (congrArg Prod.fst (Sum.inr.inj heq))

noncomputable section

def combineFunction (A : Slots branch (n + 1) → Type u) (i : Fin (branch n))
    (r : Nat)
    (ordinary : (j : OffPath i) → squareSpace (space 𝕜 branch n (childFamily A j.val)))
    (left right : Fin r → space 𝕜 branch n (childFamily A i)) : Assignment A → 𝕜 :=
  (∑ j : OffPath i, pullback 𝕜 (childRestriction A j.val) (ordinary j).val) +
    ∑ h : Fin r, pullback 𝕜 (childRestriction A i) ((left h).val * (right h).val)

theorem combineFunction_mem (A : Slots branch (n + 1) → Type u)
    (i : Fin (branch n)) (r : Nat)
    (ordinary : (j : OffPath i) → squareSpace (space 𝕜 branch n (childFamily A j.val)))
    (left right : Fin r → space 𝕜 branch n (childFamily A i)) :
    combineFunction 𝕜 A i r ordinary left right ∈ space 𝕜 branch (n + 1) A := by
  apply (space 𝕜 branch (n + 1) A).add_mem
  · apply (space 𝕜 branch (n + 1) A).sum_mem
    intro j _
    exact child_square_le_space A j.val (Submodule.mem_map_of_mem (ordinary j).property)
  · apply (space 𝕜 branch (n + 1) A).sum_mem
    intro h _
    exact child_square_le_space A i (Submodule.mem_map_of_mem
      (mul_mem_squareSpace _ (left h).property (right h).property))

def combine (A : Slots branch (n + 1) → Type u) (i : Fin (branch n)) (r : Nat)
    (ordinary : (j : OffPath i) → squareSpace (space 𝕜 branch n (childFamily A j.val)))
    (left right : Fin r → space 𝕜 branch n (childFamily A i)) :
    space 𝕜 branch (n + 1) A :=
  ⟨combineFunction 𝕜 A i r ordinary left right,
    combineFunction_mem 𝕜 A i r ordinary left right⟩

@[simp] theorem combine_zero (A : Slots branch (n + 1) → Type u)
    (i : Fin (branch n)) (r : Nat) :
    combine 𝕜 A i r (fun _ => 0) (fun _ => 0) (fun _ => 0) = 0 := by
  apply Subtype.ext
  funext x
  simp [combine, combineFunction, PointwiseSpaces.pullback]

def evaluate (𝕜 : Type w) [Field 𝕜] (repeats : Nat → Nat) :
    {n m : Nat} → (p : Path branch n m) → (A : Slots branch n → Type u) →
      Tape 𝕜 repeats p A → space 𝕜 branch n A
  | _, _, .refl _, _, t => t ()
  | n + 1, _, .step i p, A, t =>
      combine 𝕜 A i (repeats (n + 1)) (fun j => t (.inl j))
        (fun h => evaluate 𝕜 repeats p (childFamily A i)
          (subTape 𝕜 repeats i p A t h false))
        (fun h => evaluate 𝕜 repeats p (childFamily A i)
          (subTape 𝕜 repeats i p A t h true))

@[simp] theorem evaluate_refl (repeats : Nat → Nat)
    (A : Slots branch n → Type u) (t : Tape 𝕜 repeats (.refl n) A) :
    evaluate 𝕜 repeats (.refl n) A t = t () := rfl

theorem evaluate_step (repeats : Nat → Nat) (i : Fin (branch n))
    (p : Path branch n m) (A : Slots branch (n + 1) → Type u)
    (t : Tape 𝕜 repeats (.step i p) A) :
    evaluate 𝕜 repeats (.step i p) A t =
      combine 𝕜 A i (repeats (n + 1)) (fun j => t (.inl j))
        (fun h => evaluate 𝕜 repeats p (childFamily A i)
          (subTape 𝕜 repeats i p A t h false))
        (fun h => evaluate 𝕜 repeats p (childFamily A i)
          (subTape 𝕜 repeats i p A t h true)) := rfl

theorem evaluate_mem (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) (t : Tape 𝕜 repeats p A) :
    (evaluate 𝕜 repeats p A t).val ∈ space 𝕜 branch n A :=
  (evaluate 𝕜 repeats p A t).property

@[simp] theorem evaluate_zeroTape (repeats : Nat → Nat) (p : Path branch n m) :
    ∀ (A : Slots branch n → Type u),
      evaluate 𝕜 repeats p A (zeroTape 𝕜 repeats p A) = 0 := by
  induction p with
  | refl n =>
      intro A
      rfl
  | @step n m i p ih =>
      intro A
      change combine 𝕜 A i (repeats (n + 1)) (fun _ => 0)
        (fun _ => evaluate 𝕜 repeats p (childFamily A i)
          (zeroTape 𝕜 repeats p (childFamily A i)))
        (fun _ => evaluate 𝕜 repeats p (childFamily A i)
          (zeroTape 𝕜 repeats p (childFamily A i))) = 0
      simp only [ih, combine_zero]

variable [Finite 𝕜]

def tapeLaw (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 (DrawSpace 𝕜 repeats p A j))

theorem tapeLaw_eq_uniform (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) [∀ s, Finite (A s)] :
    tapeLaw 𝕜 repeats p A = FiniteDistribution.uniform (Tape 𝕜 repeats p A) :=
  UniformLinearImage.law_uniform

theorem tapeLaw_marginal (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) [∀ s, Finite (A s)] (j : DrawIndex repeats p) :
    (tapeLaw 𝕜 repeats p A).pushforward (fun t => t j) =
      FiniteDistribution.uniform (DrawSpace 𝕜 repeats p A j) :=
  FiniteProduct.eval_pushforward _ j

def law (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) [∀ s, Finite (A s)]
    [Fintype (space 𝕜 branch n A)] : FiniteDistribution (space 𝕜 branch n A) :=
  (tapeLaw 𝕜 repeats p A).pushforward (evaluate 𝕜 repeats p A)

theorem law_refl_uniform (repeats : Nat → Nat) (A : Slots branch n → Type u)
    [∀ s, Finite (A s)] [out : Fintype (space 𝕜 branch n A)] :
    law 𝕜 repeats (.refl n) A = FiniteDistribution.uniform (space 𝕜 branch n A) := by
  have hout : out = drawSpaceFintype 𝕜 repeats (.refl n) A () := Subsingleton.elim _ _
  cases hout
  exact tapeLaw_marginal 𝕜 repeats (.refl n) A ()

theorem probability_law (repeats : Nat → Nat) (p : Path branch n m)
    (A : Slots branch n → Type u) [∀ s, Finite (A s)]
    [Fintype (space 𝕜 branch n A)] (event : space 𝕜 branch n A → Bool) :
    (law 𝕜 repeats p A).probability event =
      (tapeLaw 𝕜 repeats p A).probability (fun t => event (evaluate 𝕜 repeats p A t)) :=
  FiniteDistribution.probability_pushforward _ _ _

end
end PerfectCompleteness.RecursiveSampler

end OAI