File size: 16,557 Bytes
dd14c26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import OAI.Probability.Localization.FirstHeavy

namespace OAI

open MeasureTheory ProbabilityTheory Set
open scoped BigOperators ENNReal

namespace RepeatedLocalization

namespace FiniteEntropy

instance letterMeasurableSpace (n : ℕ) : MeasurableSpace (Letter n) := ⊤

lemma state_nil {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) :
    state P Q [] = ⟨Q,cellSet Q,false⟩ := rfl

lemma state_append {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (h : Log n) (a : Letter n) :
    state P Q (h++[a]) = update P (state P Q h) a := by
  simp only [state,List.foldl_append,List.foldl_cons,List.foldl_nil]

lemma attemptLetter_none_iff {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (z : Coordinate n) :
    attemptLetter P Q z = none ↔ ¬ ∃ R : Cell n,FirstHeavy P Q R ∧ z ∈ cellSet R := by
  classical
  by_cases h : ∃ R : Cell n,FirstHeavy P Q R ∧ z ∈ cellSet R
  · rw [attemptLetter,dite_eq_left h]
    simp only [reduceCtorEq,h,not_true_eq_false]
  · rw [attemptLetter,dite_eq_right h]
    simp only [h,not_false_eq_true]

lemma attemptLetter_some_iff {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q R : Cell n)
    (z : Coordinate n) : attemptLetter P Q z = some R ↔ FirstHeavy P Q R ∧ z ∈ cellSet R := by
  classical
  constructor
  · intro he
    have hex : ∃ E : Cell n,FirstHeavy P Q E ∧ z ∈ cellSet E := by
      by_contra h
      rw [(attemptLetter_none_iff P Q z).mpr h] at he
      contradiction
    have hc : hex.choose = R := by
      simpa only [attemptLetter,dite_eq_left hex,Option.some.injEq] using he
    exact hc ▸ hex.choose_spec
  · rintro ⟨hR,hzR⟩
    exact (attemptLetter_eq_iff hn P Q (hR.1.1.2 hzR) (some R)).mpr (by simp only [branch,ite_eq_left hR]; exact hzR)

lemma measurableSet_firstHeavy_union {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) :
    MeasurableSet (⋃ R : {R : Cell n // FirstHeavy P Q R},cellSet R.val) :=
  MeasurableSet.iUnion fun R => measurableSet_cellSet R.val

lemma measurable_attemptLetter {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) :
    Measurable (attemptLetter P Q) := by
  classical
  apply measurable_to_countable'
  intro a
  change MeasurableSet {z | attemptLetter P Q z = a}
  cases a with
  | none =>
    have he : {z | attemptLetter P Q z = none} =
        (⋃ R : {R : Cell n // FirstHeavy P Q R},cellSet R.val)ᶜ := by
      ext z
      simp only [Set.mem_ofPred_eq,attemptLetter_none_iff,Set.mem_compl_iff,Set.mem_iUnion]
      apply not_congr
      exact ⟨fun ⟨R,hR,hzR⟩ => ⟨⟨R,hR⟩,hzR⟩,fun ⟨R,hzR⟩ => ⟨R.val,R.property,hzR⟩⟩
    rw [he]
    exact (measurableSet_firstHeavy_union P Q).compl
  | some R =>
    have he : {z | attemptLetter P Q z = some R} = {z | FirstHeavy P Q R ∧ z ∈ cellSet R} := by
      ext z
      exact attemptLetter_some_iff hn P Q R z
    rw [he]
    by_cases hR : FirstHeavy P Q R
    · convert measurableSet_cellSet R using 1
      ext z; simp only [Set.mem_ofPred_eq,hR,true_and]
    · simp only [hR,false_and,Set.ofPred_false,MeasurableSet.empty]

lemma state_event_measurable {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (h : Log n) :
    MeasurableSet (state P Q h).event := by
  induction h using List.reverseRecOn with
  | nil => exact measurableSet_cellSet Q
  | append_singleton h a ih =>
    rw [state_append]
    unfold update
    split
    · exact ih
    · exact ih.inter (measurableSet_branch _ _ _)

lemma state_event_subset_initial {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (h : Log n) :
    (state P Q h).event ⊆ cellSet Q := by
  induction h using List.reverseRecOn with
  | nil => exact Subset.rfl
  | append_singleton h a ih =>
    rw [state_append]
    unfold update
    split
    · exact ih
    · exact Set.inter_subset_left.trans ih

lemma state_event_subset_cell {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (h : Log n) :
    (state P Q h).event ⊆ cellSet (state P Q h).cell := by
  classical
  induction h using List.reverseRecOn with
  | nil => exact Subset.rfl
  | append_singleton h a ih =>
    rw [state_append]
    unfold update
    split
    · exact ih
    · cases a with
      | none => exact Set.inter_subset_left.trans ih
      | some R =>
        simp only [Option.getD_some]
        intro z hz
        by_cases hR : FirstHeavy (ProbabilityTheory.cond P (state P Q h).event) (state P Q h).cell R
        · simpa only [branch,ite_eq_left hR] using hz.2
        · simpa only [branch,ite_eq_right hR,Set.mem_empty_iff_false] using hz.2

lemma state_descendant_of_mem {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n)
    (h : Log n) {z : Coordinate n} (hz : z ∈ (state P Q h).event) :
    Descendant Q (state P Q h).cell := by
  classical
  induction h using List.reverseRecOn with
  | nil => exact Descendant.refl Q
  | append_singleton h a ih =>
    rw [state_append] at hz ⊢
    unfold update at hz ⊢
    by_cases hs : (state P Q h).stopped = true
    · simp only [hs,ite_true] at hz ⊢
      exact ih hz
    · simp only [hs] at hz ⊢
      cases a with
      | none => exact ih hz.1
      | some R =>
        have hR : FirstHeavy (ProbabilityTheory.cond P (state P Q h).event) (state P Q h).cell R := by
          by_contra hR
          simpa only [branch,ite_eq_right hR,Set.mem_empty_iff_false] using hz.2
        exact (ih hz.1).trans hR.1.1

lemma measurable_run {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ) :
    Measurable (run P Q N) := by
  classical
  induction N with
  | zero => exact measurable_const
  | succ N ih =>
    have hf (h : Log n) : Measurable (fun z => if (state P Q h).stopped then h else
        h++[attemptLetter (ProbabilityTheory.cond P (state P Q h).event) (state P Q h).cell z]) := by
      split
      · exact measurable_const
      · exact (measurable_of_countable (fun a : Letter n => h++[a])).comp (measurable_attemptLetter hn _ _)
    let f : Log n × Coordinate n → Log n := fun hz =>
      if (state P Q hz.1).stopped then hz.1 else hz.1++
        [attemptLetter (ProbabilityTheory.cond P (state P Q hz.1).event) (state P Q hz.1).cell hz.2]
    exact (measurable_from_prod_countable_right (f := f) hf).comp (ih.prodMk measurable_id)

lemma run_length_le {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ) (z : Coordinate n) :
    (run P Q N z).length ≤ N := by
  induction N with
  | zero => simp [run]
  | succ N ih =>
    simp only [run]
    split
    · exact ih.trans (Nat.le_succ N)
    · simpa only [List.length_append,List.length_singleton] using Nat.add_le_add_right ih 1

lemma run_length_of_active {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ)
    (z : Coordinate n) (ha : (state P Q (run P Q N z)).stopped = false) :
    (run P Q N z).length = N := by
  induction N with
  | zero => rfl
  | succ N ih =>
    have hprev : (state P Q (run P Q N z)).stopped = false := by
      cases he : (state P Q (run P Q N z)).stopped
      · rfl
      · simp only [run,he,ite_true] at ha
        exact ha
    simp only [run,hprev,Bool.false_eq_true,ite_false,List.length_append,List.length_singleton]
    rw [ih hprev]

lemma run_mem_event {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ)
    {z : Coordinate n} (hz : z ∈ cellSet Q) : z ∈ (state P Q (run P Q N z)).event := by
  induction N with
  | zero => exact hz
  | succ N ih =>
    simp only [run]
    split
    · exact ih
    · rw [state_append]
      unfold update
      split
      · exact ih
      · exact ⟨ih,attemptLetter_mem_branch _ _ (state_event_subset_cell _ _ _ ih)⟩

lemma run_eq_iff_mem {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n)
    (N : ℕ) {z w : Coordinate n} (hz : z ∈ cellSet Q) (hw : w ∈ cellSet Q) :
    run P Q N w = run P Q N z ↔ w ∈ (state P Q (run P Q N z)).event := by
  classical
  induction N generalizing z w with
  | zero => simpa only [run,state_nil,true_iff] using hw
  | succ N ih =>
    cases hzstop : (state P Q (run P Q N z)).stopped with
    | false =>
      cases hwstop : (state P Q (run P Q N w)).stopped with
      | false =>
        simp only [run,hzstop,hwstop,Bool.false_eq_true,ite_false,state_append,update,
          List.append_singleton_inj]
        constructor
        · rintro ⟨hh,ha⟩
          refine ⟨(ih hz hw).mp hh,?_⟩
          have hwm := run_mem_event P Q N hw
          rw [hh] at hwm
          apply (attemptLetter_eq_iff hn _ _ (state_event_subset_cell _ _ _ hwm) _).mp
          simpa only [hh] using ha
        · rintro ⟨hwm,hwa⟩
          have hh := (ih hz hw).mpr hwm
          refine ⟨hh,?_⟩
          rw [hh]
          exact (attemptLetter_eq_iff hn _ _ (state_event_subset_cell _ _ _ hwm) _).mpr hwa
      | true =>
        simp only [run,hzstop,hwstop,Bool.false_eq_true,ite_false,ite_true,state_append,update]
        constructor
        · intro he
          have hl := congrArg List.length he
          rw [List.length_append,List.length_singleton,run_length_of_active P Q N z hzstop] at hl
          have := run_length_le P Q N w
          omega
        · intro hm
          have hh := (ih hz hw).mpr hm.1
          rw [hh,hzstop] at hwstop
          contradiction
    | true =>
      cases hwstop : (state P Q (run P Q N w)).stopped with
      | false =>
        simp only [run,hzstop,hwstop,Bool.false_eq_true,ite_false,ite_true]
        constructor
        · intro he
          have hl := congrArg List.length he
          rw [List.length_append,List.length_singleton,run_length_of_active P Q N w hwstop] at hl
          have := run_length_le P Q N z
          omega
        · intro hm
          have hh := (ih hz hw).mpr hm
          rw [hh,hzstop] at hwstop
          contradiction
      | true => simpa only [run,hzstop,hwstop,ite_true] using ih hz hw

lemma run_fiber_inter_initial {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n)
    (N : ℕ) {z : Coordinate n} (hz : z ∈ cellSet Q) :
    cellSet Q ∩ {w | run P Q N w = run P Q N z} = (state P Q (run P Q N z)).event := by
  ext w
  constructor
  · rintro ⟨hw,he⟩
    exact (run_eq_iff_mem hn P Q N hz hw).mp he
  · intro hm
    have hw := state_event_subset_initial _ _ _ hm
    exact ⟨hw,(run_eq_iff_mem hn P Q N hz hw).mpr hm⟩

lemma run_succ_of_stopped {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ)
    (z : Coordinate n) (h : (state P Q (run P Q N z)).stopped = true) :
    run P Q (N+1) z = run P Q N z := by simp only [run,h,ite_true]

lemma run_add_of_stopped {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) (N M : ℕ)
    (z : Coordinate n) (h : (state P Q (run P Q N z)).stopped = true) :
    run P Q (N+M) z = run P Q N z := by
  induction M with
  | zero => simp
  | succ M ih =>
    rw [Nat.add_succ,run_succ_of_stopped P Q (N+M) z (by rw [ih]; exact h),ih]

lemma run_of_stopped_le {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) {N M : ℕ}
    (z : Coordinate n) (hNM : N ≤ M) (h : (state P Q (run P Q N z)).stopped = true) :
    run P Q M z = run P Q N z := by
  obtain ⟨M,rfl⟩ := Nat.exists_eq_add_of_le hNM
  exact run_add_of_stopped P Q N M z h

lemma attempts_spec {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) {z : Coordinate n}
    (hz : Terminates P Q z) : (state P Q (terminalLog P Q z)).stopped = true := by
  simpa only [terminalLog,attempts,dite_eq_left hz] using Nat.find_spec hz

lemma attempts_min {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) {z : Coordinate n}
    (hz : Terminates P Q z) {N : ℕ} (hN : N < attempts P Q z) :
    (state P Q (run P Q N z)).stopped = false := by
  have hm : ¬ (state P Q (run P Q N z)).stopped = true := Nat.find_min hz (by
    simpa only [attempts,dite_eq_left hz] using hN)
  simpa using hm

lemma attempts_pos {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) {z : Coordinate n}
    (hz : Terminates P Q z) : 0 < attempts P Q z := by
  by_contra h
  have he : attempts P Q z = 0 := by omega
  have hs := attempts_spec P Q hz
  simp [terminalLog,he,run,state_nil] at hs

lemma terminalLog_length {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n) {z : Coordinate n}
    (hz : Terminates P Q z) : (terminalLog P Q z).length = attempts P Q z := by
  obtain ⟨N,hN⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt (attempts_pos P Q hz))
  have hn : (state P Q (run P Q N z)).stopped = false := attempts_min P Q hz (by omega)
  simp only [terminalLog,hN,run,hn,Bool.false_eq_true,ite_false,List.length_append,List.length_singleton]
  rw [run_length_of_active P Q N z hn]

lemma terminalLog_of_stopped {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n)
    {N : ℕ} {z : Coordinate n} (h : (state P Q (run P Q N z)).stopped = true) :
    terminalLog P Q z = run P Q N z := by
  have ht : Terminates P Q z := ⟨N,h⟩
  have hle : attempts P Q z ≤ N := by
    simpa only [attempts,dite_eq_left ht] using Nat.find_min' ht h
  exact (run_of_stopped_le P Q z hle (attempts_spec P Q ht)).symm

lemma measurableSet_stopped {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) (N : ℕ) :
    MeasurableSet {z | (state P Q (run P Q N z)).stopped = true} :=
  ((measurable_of_countable (fun h : Log n => (state P Q h).stopped)).comp
    (measurable_run hn P Q N)) (measurableSet_singleton true)

lemma measurableSet_terminates {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) :
    MeasurableSet {z | Terminates P Q z} := by
  have he : {z | Terminates P Q z} = ⋃ N : ℕ,{z | (state P Q (run P Q N z)).stopped = true} := by
    ext z; simp only [Terminates,Set.mem_ofPred_eq,Set.mem_iUnion]
  rw [he]
  exact MeasurableSet.iUnion (measurableSet_stopped hn P Q)

lemma measurable_attempts {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) :
    Measurable (attempts P Q) := by
  classical
  have hex (z : Coordinate n) : ∃ N : ℕ,(state P Q (run P Q N z)).stopped = true ∨ ¬ Terminates P Q z := by
    by_cases h : Terminates P Q z
    · obtain ⟨N,hN⟩ := h
      exact ⟨N,Or.inl hN⟩
    · exact ⟨0,Or.inr h⟩
  have he (z : Coordinate n) : attempts P Q z = Nat.find (hex z) := by
    by_cases h : Terminates P Q z
    · simp only [attempts,dite_eq_left h]
      apply le_antisymm
      · exact Nat.find_min' h ((Nat.find_spec (hex z)).resolve_right (not_not.mpr h))
      · exact Nat.find_min' (hex z) (Or.inl (Nat.find_spec h))
    · simp only [attempts,dite_eq_right h]
      exact (Nat.eq_zero_of_le_zero (Nat.find_min' (hex z) (show
        (state P Q (run P Q 0 z)).stopped = true ∨ ¬ Terminates P Q z from Or.inr h))).symm
  have hm : Measurable (fun z => Nat.find (hex z)) := Measurable.find (f := fun N (_ : Coordinate n) => N) (p := fun N z =>
      (state P Q (run P Q N z)).stopped = true ∨ ¬ Terminates P Q z)
    (fun N => measurable_const) (fun N => (measurableSet_stopped hn P Q N).union
      (measurableSet_terminates hn P Q).compl) hex
  convert hm using 1
  funext z
  exact he z

lemma measurable_terminalLog {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n) :
    Measurable (terminalLog P Q) := by
  have hm : Measurable (fun Nz : ℕ × Coordinate n => run P Q Nz.1 Nz.2) :=
    measurable_from_prod_countable_right (measurable_run hn P Q)
  exact hm.comp ((measurable_attempts hn P Q).prodMk measurable_id)

lemma terminates_of_terminal_stopped {n : ℕ} (P : Measure (Coordinate n)) (Q : Cell n)
    {z : Coordinate n} (hs : (state P Q (terminalLog P Q z)).stopped = true) : Terminates P Q z :=
  ⟨attempts P Q z,hs⟩

lemma terminalLog_fiber_inter_initial {n : ℕ} (hn : 0 < n) (P : Measure (Coordinate n)) (Q : Cell n)
    {z : Coordinate n} (hz : z ∈ cellSet Q) (ht : Terminates P Q z) :
    cellSet Q ∩ {w | terminalLog P Q w = terminalLog P Q z} =
      (state P Q (terminalLog P Q z)).event := by
  ext w
  constructor
  · rintro ⟨hw,he⟩
    change terminalLog P Q w = terminalLog P Q z at he
    have htw : Terminates P Q w := terminates_of_terminal_stopped P Q (by rw [he]; exact attempts_spec P Q ht)
    have hlen : attempts P Q w = attempts P Q z := by
      rw [← terminalLog_length P Q htw,← terminalLog_length P Q ht,he]
    apply (run_eq_iff_mem hn P Q (attempts P Q z) hz hw).mp
    simpa only [terminalLog,hlen] using he
  · intro hm
    have hw := state_event_subset_initial _ _ _ hm
    have he := (run_eq_iff_mem hn P Q (attempts P Q z) hz hw).mpr hm
    refine ⟨hw,?_⟩
    have hs : (state P Q (run P Q (attempts P Q z) w)).stopped = true := by
      rw [he]; exact attempts_spec P Q ht
    exact (terminalLog_of_stopped P Q hs).trans he

end FiniteEntropy

end RepeatedLocalization

end OAI