File size: 21,059 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import OAI.Computability.Scheduling.CutoffSimplification

namespace OAI

section

namespace ThreeMachine.Structure
namespace Layout
variable {J : Type} [Fintype J]

theorem time_bounds (p : Layout J) (hfull : 3 ∣ Fintype.card J) (x : J) :
    1 ≤ p.time x ∧ p.time x ≤ Fintype.card J / 3 := by
  obtain ⟨T, hT⟩ := hfull
  have hp := (p x).isLt
  dsimp [time]
  omega

noncomputable def slot (p : Layout J) (s : ℕ) : Finset J := by
  classical
  exact Finset.univ.filter (fun x => p.time x = s)

@[simp] theorem mem_slot (p : Layout J) (s : ℕ) (x : J) :
    x ∈ p.slot s ↔ p.time x = s := by
  classical
  simp [slot]

theorem card_slot (p : Layout J) (hfull : 3 ∣ Fintype.card J) (s : ℕ)
    (hs : 1 ≤ s) (hsT : s ≤ Fintype.card J / 3) : (p.slot s).card = 3 := by
  classical
  obtain ⟨T, hT⟩ := hfull
  let i : Fin (Fintype.card J) := ⟨3 * (s - 1), by omega⟩
  let j : Fin (Fintype.card J) := ⟨3 * (s - 1) + 1, by omega⟩
  let k : Fin (Fintype.card J) := ⟨3 * (s - 1) + 2, by omega⟩
  apply Finset.card_eq_three.mpr
  refine ⟨p.symm i, p.symm j, p.symm k, ?_, ?_, ?_, ?_⟩
  · intro he
    have hev := congrArg Fin.val (p.symm.injective he)
    dsimp [i, j] at hev
    omega
  · intro he
    have hev := congrArg Fin.val (p.symm.injective he)
    dsimp [i, k] at hev
    omega
  · intro he
    have hev := congrArg Fin.val (p.symm.injective he)
    dsimp [j, k] at hev
    omega
  · ext x
    simp only [mem_slot, Finset.mem_insert, Finset.mem_singleton]
    have hxi : x = p.symm i ↔ (p x).val = 3 * (s - 1) := by
      rw [Equiv.eq_symm_apply]
      exact Fin.ext_iff
    have hxj : x = p.symm j ↔ (p x).val = 3 * (s - 1) + 1 := by
      rw [Equiv.eq_symm_apply]
      exact Fin.ext_iff
    have hxk : x = p.symm k ↔ (p x).val = 3 * (s - 1) + 2 := by
      rw [Equiv.eq_symm_apply]
      exact Fin.ext_iff
    rw [hxi, hxj, hxk]
    dsimp [time]
    omega

noncomputable def triple (p : Layout J) (hfull : 3 ∣ Fintype.card J)
    (s : ℕ) (hs : 1 ≤ s) (hsT : s ≤ Fintype.card J / 3) : Triple J :=
  ⟨p.slot s, p.card_slot hfull s hs hsT⟩

theorem triple_at (p : Layout J) (hfull : 3 ∣ Fintype.card J)
    (s : ℕ) (hs : 1 ≤ s) (hsT : s ≤ Fintype.card J / 3) :
    (Boundary.actual (p.triple hfull s hs hsT)).At p.time (Fintype.card J / 3) s := by
  exact ⟨hs, hsT, fun x => p.mem_slot s x⟩

def reverse (p : Layout J) : Layout J := p.trans Fin.revPerm

@[simp] theorem reverse_reverse (p : Layout J) : p.reverse.reverse = p := by
  ext x
  simp [reverse]

theorem reverse_time (p : Layout J) (hfull : 3 ∣ Fintype.card J) (x : J) :
    p.reverse.time x = Fintype.card J / 3 + 1 - p.time x := by
  obtain ⟨T, hT⟩ := hfull
  have hp := (p x).isLt
  change (Fintype.card J - ((p x).val + 1)) / 3 + 1 =
    Fintype.card J / 3 + 1 - ((p x).val / 3 + 1)
  omega

theorem reverse_respects {p : Layout J} {r : J → J → Prop}
    (hfull : 3 ∣ Fintype.card J) (hp : p.Respects r) :
    p.reverse.Respects (fun x y => r y x) := by
  intro x y hyx
  rw [p.reverse_time hfull, p.reverse_time hfull]
  have hxy := hp y x hyx
  have hx := p.time_bounds hfull x
  have hy := p.time_bounds hfull y
  omega

theorem reverse_full {p : Layout J} {r : J → J → Prop} (hp : p.Full r) :
    p.reverse.Full (fun x y => r y x) := ⟨hp.1, reverse_respects hp.1 hp.2⟩

theorem FixedOutside.reverse {p q : Layout J} {W : Set J}
    (h : p.FixedOutside q W) : p.reverse.FixedOutside q.reverse W := by
  intro x hx
  simp [Layout.reverse, h x hx]

end Layout

namespace Boundary
variable {J : Type} [Fintype J]

theorem at_reverse {p : Layout J} (hfull : 3 ∣ Fintype.card J)
    {B : Boundary J} {s : ℕ} (h : B.At p.time (Fintype.card J / 3) s) :
    B.reverse.At p.reverse.time (Fintype.card J / 3) (Fintype.card J / 3 + 1 - s) := by
  cases B with
  | left => dsimp [At] at h; subst s; rfl
  | right => dsimp [At] at h; subst s; simp [reverse, At]
  | actual Z =>
      dsimp only [At] at h
      refine ⟨by omega, by omega, ?_⟩
      intro x
      rw [h.2.2, p.reverse_time hfull]
      have hx := p.time_bounds hfull x
      omega

end Boundary

end ThreeMachine.Structure

namespace ThreeMachine.Structure
section IntervalHelpers
variable {J : Type} [Fintype J]

theorem Layout.FixedOutside.window {p q : Layout J} {W : Set J} {a b : ℕ}
    (h : p.FixedOutside q W) (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time a b) :
    ∀ x, x ∈ W ↔ x ∈ Window q.time a b := by
  intro x
  constructor
  · exact h.time_range (fun z hz => (hW z).mp hz)
  · intro hx
    by_contra hn
    have he := h.time hn
    apply hn
    apply (hW x).mpr
    change a < q.time x ∧ q.time x < b at hx
    change a < p.time x ∧ p.time x < b
    simpa only [he] using hx

theorem Layout.FixedOutside.boundary {p q : Layout J} {W : Set J} {a b s T : ℕ}
    {B : Boundary J} (h : p.FixedOutside q W)
    (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time a b)
    (hs : s ≤ a ∨ b ≤ s) (hB : B.At p.time T s) : B.At q.time T s := by
  cases B with
  | left => exact hB
  | right => exact hB
  | actual Z =>
      refine ⟨hB.1, hB.2.1, ?_⟩
      intro x
      by_cases hx : x ∈ W
      · have hp := (hW x).mp hx
        have hq := (h.window hW x).mp hx
        have hnp : p.time x ≠ s := by rcases hs with hs | hs <;> dsimp [Window] at hp <;> omega
        have hnq : q.time x ≠ s := by rcases hs with hs | hs <;> dsimp [Window] at hq <;> omega
        rw [hB.2.2]
        exact iff_of_false hnp hnq
      · rw [hB.2.2, h.time hx]

theorem PastInvariant.reorder {r : J → J → Prop} {p q : Layout J}
    {K : GlobalList J} {A : Boundary J} {a b : ℕ} {W : Set J}
    (hpast : PastInvariant r p.time K A a) (hfix : p.FixedOutside q W)
    (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time a b) :
    PastInvariant r q.time K A a := by
  intro x hx htime
  have hnW : x ∉ W := by
    intro hw
    have h := ((hfix.window hW) x).mp hw
    exact (not_lt_of_ge htime) h.1
  have ht : p.time x = q.time x := (hfix.time hnW).symm
  exact hpast x hx (ht ▸ htime)

def SwapInvariant [DecidableEq J] (r : J → J → Prop) (p : Layout J)
    (W : Set J) (K : GlobalList J) (rank : J → ℕ) (A : Boundary J) (a : ℕ) : Prop :=
  ∀ q : Layout J, p.FixedOutside q W → q.Respects r →
    ∀ x ∈ W, x ∉ A.desc r → ∀ y, q.time y = a → y ∈ K.Qual →
      K.KeyLT rank x y → ¬(q.exchange x y).Respects r

theorem SwapInvariant.reorder [DecidableEq J] {r : J → J → Prop} {p q : Layout J}
    {W : Set J} {K : GlobalList J} {rank : J → ℕ} {A : Boundary J} {a : ℕ}
    (h : SwapInvariant r p W K rank A a) (hfix : p.FixedOutside q W) :
    SwapInvariant r q W K rank A a := by
  intro u hqu hu x hx hnx y hy hyq hkey
  exact h u (hfix.trans hqu) hu x hx hnx y hy hyq hkey

theorem ideal_has_separator_interval {r : J → J → Prop} {S : Set J}
    (p : Layout J) (hp : p.Full r) {W : Finset J} {a b : ℕ}
    (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time a b) (hne : W.Nonempty)
    (htrans : ∀ ⦃x y z⦄, r x y → r y z → r x z)
    (hideal : IdealOn W r S) :
    ∃ q : Layout J, p.FixedOutside q (W : Set J) ∧ q.Full r ∧
      ∃ t, a < t ∧ t < b ∧ Separator W r q.time S t := by
  classical
  let priority := fun x : J => if x ∈ S then (p x).val else Fintype.card J + (p x).val
  have hprefix : PrefixOn W priority S := by
    intro x _ hx y _ hy
    simp only [priority, ite_eq_left hx, ite_eq_right hy]
    have := (p x).isLt
    omega
  obtain ⟨q, hfix, hq⟩ := exists_normalizedOn p W r priority hp.2
  have hWq : ∀ x, x ∈ W ↔ x ∈ Window q.time a b := hfix.window hW
  obtain ⟨x, hx, hmin⟩ := W.exists_min_image q.time hne
  have hclosed : SlotClosed W q.time := by
    intro z hz y he
    apply (hWq y).mpr
    have hzt := (hWq z).mp hz
    change a < q.time z ∧ q.time z < b at hzt
    change a < q.time y ∧ q.time y < b
    simpa only [he] using hzt
  have hbefore : EarlierHigh W r q.time S (q.time x) := by
    intro z hz ht _
    have := hmin z hz
    omega
  obtain ⟨t, _hst, hpath, hsep⟩ := walk_to_separator htrans hq.1 hclosed
    (Or.inl ⟨x, hx, rfl⟩) hideal hprefix
    (normalizedOn_local_obstruction hq hWq) hbefore
  have ht : ∃ z ∈ W, q.time z = t := by
    rcases hpath.endpoint with he | ⟨z, hz, hzt⟩
    · exact ⟨x, hx, he.symm⟩
    · exact ⟨z, hz, hzt⟩
  obtain ⟨z, hz, hzt⟩ := ht
  have hztBounds := (hWq z).mp hz
  change a < q.time z ∧ q.time z < b at hztBounds
  refine ⟨q, hfix, ⟨hp.1, hq.1⟩, t, ?_, ?_, hsep⟩ <;> omega

end IntervalHelpers
end ThreeMachine.Structure

namespace ThreeMachine.Structure
section BoundaryWalk
variable {J : Type} [Fintype J]

theorem exception_global_ready {r : J → J → Prop} {p : Layout J}
    {E : Finset J} {a v : ℕ} {S : Set J} {x : J}
    (htrans : ∀ ⦃u z w⦄, r u z → r z w → r u w) (hp : p.Respects r)
    (hE : ∀ z, z ∈ E ↔ a < p.time z ∧ p.time z < v)
    (hideal : IdealOn E r S) (hx : Exception E r p.time S a x) :
    ∀ u, r u x → p.time u < a := by
  intro u hux
  have htx := hp u x hux
  have hxt := (hE x).mp hx.mem
  by_contra hn
  have hau : a ≤ p.time u := by omega
  rcases hau.eq_or_lt with he | hl
  · exact hx.not_desc ⟨u, he.symm, hux⟩
  · have hu : u ∈ E := (hE u).mpr ⟨hl, htx.trans hxt.2⟩
    have := exception_ready htrans hp hideal hx u hu hux
    omega

omit [Fintype J] in

theorem advance_carry_descendant {r : J → J → Prop} {time priority : J → ℕ}
    {E : Finset J} {H S : Set J} {a v s : ℕ} {x y : J}
    (htrans : ∀ ⦃u z w⦄, r u z → r z w → r u w)
    (hrespect : ∀ u z, r u z → time u < time z)
    (hE : ∀ z, z ∈ E ↔ a < time z ∧ time z < v)
    (hH : GlobalUpset r H) (hhigh : ∀ z ∈ E, z ∈ H → z ∉ S)
    (hideal : IdealOn E r S) (hprefix : PrefixOn E priority S)
    (hnorm : LocalObstruction E r time priority)
    (has : a ≤ s) (hx : Exception E r time S s x)
    (hfirst : s = a → ∀ z ∈ H, time z = a → ∃ u ∈ E, r z u ∧ time u ≤ time x)
    (hyH : y ∈ H) (hys : time y = s) :
    ∃ z ∈ E, time z = time x ∧ r y z ∧ z ∈ H := by
  have hready := exception_ready htrans hrespect hideal hx
  have hforce : ∀ z ∈ E, z ∉ S → s ≤ time z → time z < time x →
      ∃ u ∈ E, r z u ∧ time u ≤ time x := by
    intro z hz hnS hsz hzx
    exact hnorm x hx.mem z hz hzx (hprefix x hx.mem hx.low z hz hnS)
      (fun u hu hux => (hready u hu hux).trans_le hsz)
  have hsucc : ∃ u ∈ E, r y u ∧ time u ≤ time x := by
    by_cases hsa : s = a
    · exact hfirst hsa y hyH (hys.trans hsa)
    · have hyE : y ∈ E := (hE y).mpr
        ⟨by omega, by have := (hE x).mp hx.mem; have := hx.later; omega⟩
      exact hforce y hyE (hhigh y hyE hyH) (by omega) (by have := hx.later; omega)
  obtain ⟨u, hu, hyu, hut⟩ := hsucc
  have huH := hH hyu hyH
  have hsu : s ≤ time u := by have := hrespect y u hyu; omega
  obtain ⟨z, hz, hzt, huz, _hnS⟩ := trace_to_slot htrans hrespect hideal hforce
    u hu (hhigh u hu huH) hsu hut
  have hyz : r y z := by
    rcases huz with e | hur
    · simpa [e] using hyu
    · exact htrans hyu hur
  exact ⟨z, hz, hzt, hyz, hH hyz hyH⟩

theorem first_boundary_force [DecidableEq J]
    {r : J → J → Prop} {p : Layout J} {E : Finset J} {a v : ℕ}
    {S H : Set J} {x : J}
    (htrans : ∀ ⦃u z w⦄, r u z → r z w → r u w) (hp : p.Respects r)
    (hE : ∀ z, z ∈ E ↔ a < p.time z ∧ p.time z < v)
    (hideal : IdealOn E r S) (hx : Exception E r p.time S a x)
    (hno : ∀ y ∈ H, p.time y = a → ¬(p.exchange x y).Respects r) :
    ∀ y ∈ H, p.time y = a → ∃ u ∈ E, r y u ∧ p.time u ≤ p.time x := by
  have hready : ∀ u, r u x → p.time u < a :=
    exception_global_ready htrans hp hE hideal hx
  intro y hy hya
  by_contra hn
  have hsucc : ∀ u, r y u → p.time x < p.time u := by
    intro u hyu
    by_contra hntu
    have hxv := ((hE x).mp hx.mem).2
    have hyut := hp y u hyu
    have huE : u ∈ E := (hE u).mpr ⟨by omega, by omega⟩
    exact hn ⟨u, huE, hyu, by omega⟩
  have hyx : p.time y < p.time x := by have := hx.later; omega
  exact hno y hy hya (respects_exchange hp x y hyx (fun u hur => by
    have := hready u hur; omega) hsucc)

end BoundaryWalk
end ThreeMachine.Structure

namespace ThreeMachine.Structure
section VaryingWalk
variable {J : Type}

inductive CutoffWalk (E : Finset J) (r : J → J → Prop) (time priority : J → ℕ)
    (Sfinal : Set J) (a : ℕ) : ℕ → Prop
  | refl : CutoffWalk E r time priority Sfinal a a
  | step {s : ℕ} {x : J} {U : Set J}
      (prev : CutoffWalk E r time priority Sfinal a s)
      (ideal : IdealOn E r U) (isPrefix : PrefixOn E priority U)
      (below : ∀ z ∈ E, z ∈ U → z ∈ Sfinal)
      (exception : Exception E r time U s x) :
      CutoffWalk E r time priority Sfinal a (time x)

theorem CutoffWalk.le {E : Finset J} {r : J → J → Prop} {time priority : J → ℕ}
    {S : Set J} {a t : ℕ} (h : CutoffWalk E r time priority S a t) : a ≤ t := by
  induction h with
  | refl => exact le_rfl
  | step _ _ _ _ hx ih => exact ih.trans hx.later.le

theorem CutoffWalk.mono {E : Finset J} {r : J → J → Prop} {time priority : J → ℕ}
    {S S' : Set J} {a t : ℕ} (h : CutoffWalk E r time priority S a t)
    (hsub : ∀ z ∈ E, z ∈ S → z ∈ S') : CutoffWalk E r time priority S' a t := by
  induction h with
  | refl => exact .refl
  | step _ hi hp hb hx ih => exact .step ih hi hp (fun z hz hu => hsub z hz (hb z hz hu)) hx

theorem CutoffWalk.trans_advance {E : Finset J} {r : J → J → Prop}
    {time priority : J → ℕ} {S U : Set J} {a s t : ℕ}
    (h : CutoffWalk E r time priority S a s) (hp : AdvancePath E r time U s t)
    (hi : IdealOn E r U) (hprefix : PrefixOn E priority U)
    (hbelow : ∀ z ∈ E, z ∈ U → z ∈ S) :
    CutoffWalk E r time priority S a t := by
  induction hp with
  | refl => exact h
  | step _ hx ih => exact .step ih hi hprefix hbelow hx

theorem CutoffWalk.carry {E : Finset J} {r : J → J → Prop} {time priority : J → ℕ}
    {S H : Set J} {a v t : ℕ}
    (htrans : ∀ ⦃u z w⦄, r u z → r z w → r u w)
    (hrespect : ∀ u z, r u z → time u < time z)
    (hE : ∀ z, z ∈ E ↔ a < time z ∧ time z < v)
    (hH : GlobalUpset r H) (hhigh : ∀ z ∈ E, z ∈ H → z ∉ S)
    (hnorm : LocalObstruction E r time priority)
    (hfirst : ∀ U, IdealOn E r U → PrefixOn E priority U →
      (∀ z ∈ E, z ∈ U → z ∈ S) → ∀ x, Exception E r time U a x →
      ∀ y ∈ H, time y = a → ∃ u ∈ E, r y u ∧ time u ≤ time x)
    (hwalk : CutoffWalk E r time priority S a t) :
    ∀ y ∈ H, time y = a → ∃ z, time z = t ∧ (z = y ∨ r y z) ∧ z ∈ H := by
  induction hwalk with
  | refl => exact fun y hy ht => ⟨y, ht, Or.inl rfl, hy⟩
  | @step s x U prev hi hp hb hx ih =>
    intro y hy hyt
    obtain ⟨z, hzt, hyz, hzH⟩ := ih y hy hyt
    obtain ⟨w, _hw, hwt, hzw, hwH⟩ := advance_carry_descendant htrans hrespect hE hH
      (fun z hz hzH hzU => hhigh z hz hzH (hb z hz hzU)) hi hp hnorm prev.le hx
      (fun hsa => hfirst U hi hp hb x (by simpa only [hsa] using hx)) hzH hzt
    refine ⟨w, hwt, Or.inr ?_, hwH⟩
    rcases hyz with rfl | hyz
    · exact hzw
    · exact htrans hyz hzw

end VaryingWalk

section PastUpdate
variable {J : Type} [Fintype J]

omit [Fintype J] in

theorem Boundary.weakPred_witness {r : J → J → Prop} {time : J → ℕ}
    {T a : ℕ} {A : Boundary J} {x : J}
    (hA : A.At time T a) (ha : a ≤ T) (hx : x ∈ A.weakPred r) :
    ∃ y, time y = a ∧ (x = y ∨ r x y) := by
  cases A with
  | left => exact False.elim hx
  | right => simp only [Boundary.At] at hA; omega
  | actual Z =>
    rcases hx with ⟨y, hy, hxy⟩ | hx
    · exact ⟨y, (hA.2.2 y).mp hy, Or.inr hxy⟩
    · exact ⟨x, (hA.2.2 x).mp hx, Or.inl rfl⟩

theorem past_update {r : J → J → Prop} {p : Layout J} {priority : J → ℕ}
    {E : Finset J} {a v ap : ℕ} {A A' : Boundary J} {K K' : GlobalList J}
    {S H : Set J}
    (hp : p.Full r) (htrans : ∀ ⦃u z w⦄, r u z → r z w → r u w)
    (hA : A.At p.time (Fintype.card J / 3) a)
    (hA' : A'.At p.time (Fintype.card J / 3) ap)
    (ha : a ≤ Fintype.card J / 3) (hap : ap < v)
    (hE : ∀ z, z ∈ E ↔ a < p.time z ∧ p.time z < v)
    (hparent : PastInvariant r p.time K A a)
    (hH : GlobalUpset r H) (hHO : H ⊆ K.Qual)
    (hearly : ∀ z ∈ K'.Qual, p.time z ≤ a → z ∈ H)
    (hhigh : ∀ z ∈ E, z ∈ K'.Qual → z ∉ S)
    (hHhigh : ∀ z ∈ E, z ∈ H → z ∉ S)
    (hsep : A'.Separates r p.time E S ap)
    (hnorm : LocalObstruction E r p.time priority)
    (hfirst : ∀ U, IdealOn E r U → PrefixOn E priority U →
      (∀ z ∈ E, z ∈ U → z ∈ S) → ∀ x, Exception E r p.time U a x →
      ∀ y ∈ H, p.time y = a → ∃ u ∈ E, r y u ∧ p.time u ≤ p.time x)
    (hwalk : CutoffWalk E r p.time priority S a ap) :
    PastInvariant r p.time K' A' ap := by
  intro z hz hzt
  by_cases hae : p.time z ≤ a
  · have hzH := hearly z hz hae
    obtain ⟨y, hya, hzy⟩ := Boundary.weakPred_witness hA ha (hparent z (hHO hzH) hae)
    have hyH : y ∈ H := by
      rcases hzy with rfl | hzy
      · exact hzH
      · exact hH hzy hzH
    obtain ⟨w, hwt, hyw, _hwH⟩ := hwalk.carry htrans hp.2 hE hH hHhigh hnorm hfirst y hyH hya
    have hwmem : w ∈ A'.weakPred r :=
      (Boundary.mem_weak_at_time (p.time_bounds hp.1) hA' hwt).1
    rcases hzy with rfl | hzy
    · rcases hyw with rfl | hyw
      · exact hwmem
      · exact Boundary.weakPred_downset htrans A' hyw hwmem
    · have hzw : r z w := by
        rcases hyw with rfl | hyw
        · exact hzy
        · exact htrans hzy hyw
      exact Boundary.weakPred_downset htrans A' hzw hwmem
  · have hzE := (hE z).mpr ⟨by omega, by omega⟩
    rcases hzt.eq_or_lt with he | hl
    · exact (Boundary.mem_weak_at_time (p.time_bounds hp.1) hA' he).1
    · exact A'.pred_subset_weakPred r (hsep.1 z hzE hl (hhigh z hzE hz))

end PastUpdate
end ThreeMachine.Structure

namespace ThreeMachine.Structure
section ChildSwaps
variable {J : Type} [Fintype J] [DecidableEq J]

omit [DecidableEq J] in
theorem Layout.FixedOutside.earlier_positions {p q : Layout J} {W : Set J} {a b : ℕ}
    (hfix : p.FixedOutside q W) (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time a b)
    {y : J} (hy : p.time y ≤ a) : ∀ j, j ≤ p y → q.symm j = p.symm j := by
  intro j hj
  have hn : p.symm j ∉ W := by
    intro hmem
    have ht := ((hW _).mp hmem).1
    dsimp [Layout.time] at ht hy
    have hjv : j.val ≤ (p y).val := hj
    simp only [p.apply_symm_apply] at ht
    omega
  apply q.injective
  rw [q.apply_symm_apply, hfix _ hn, p.apply_symm_apply]

theorem swap_from_normalized {r : J → J → Prop} {p : Layout J}
    {E W : Set J} {a b ap bp : ℕ} {A' : Boundary J}
    {K : GlobalList J} {rank priority : J → ℕ}
    (hE : ∀ x, x ∈ E ↔ x ∈ Window p.time a b)
    (hW : ∀ x, x ∈ W ↔ x ∈ Window p.time ap bp)
    (ha : a < ap) (hb : bp ≤ b) (hab : ap < bp)
    (hnorm : p.NormalizedOn E r priority)
    (hqual : ∀ x ∈ W, x ∉ A'.desc r → x ∈ K.Qual)
    (hkeys : ∀ x ∈ W, x ∈ K.Qual → ∀ y, p.time y = ap → y ∈ K.Qual →
      K.KeyLT rank x y → priority x < priority y) :
    SwapInvariant r p W K rank A' ap := by
  intro q hfix _hq x hx hnx y hy hyq hkey hswap
  have hWx := (hfix.window hW x).mp hx
  have hnWy : y ∉ W := by
    intro hw
    have ht := ((hfix.window hW) y).mp hw
    exact (lt_irrefl ap) (hy ▸ ht.1)
  have hpya : p.time y = ap := (hfix.time hnWy).symm.trans hy
  have hWE : W ⊆ E := by
    intro z hz
    have ht := (hW z).mp hz
    exact (hE z).mpr ⟨ha.trans ht.1, ht.2.trans_le hb⟩
  have hyE : y ∈ E := (hE y).mpr ⟨by omega, by omega⟩
  exact normalizedOn_prefix_no_exchange hnorm (hfix.mono hWE) (hWE hx) hyE
    (hfix.earlier_positions hW hpya.le) (by have := hWx.1; omega)
    (hkeys x hx (hqual x hx hnx) y hpya hyq hkey) hswap

theorem swap_inherited {r : J → J → Prop} {p : Layout J}
    {W W' : Set J} {K K' : GlobalList J} {rank : J → ℕ} {A : Boundary J} {a : ℕ}
    (hparent : SwapInvariant r p W K rank A a) (hsub : W' ⊆ W)
    (hqual : ∀ y ∈ K'.Qual, y ∈ K.Qual)
    (hkeys : ∀ x ∈ W', x ∉ A.desc r → ∀ y ∈ K'.Qual,
      K'.KeyLT rank x y → K.KeyLT rank x y) :
    SwapInvariant r p W' K' rank A a := by
  intro q hfix hq x hx hnx y hy hyq hkey
  exact hparent q (hfix.mono hsub) hq x (hsub hx) hnx y hy
    (hqual y hyq) (hkeys x hx hnx y hyq hkey)

theorem swap_prune {r : J → J → Prop} {p : Layout J}
    {W : Set J} {K : GlobalList J} {rank : J → ℕ} {A : Boundary J} {a : ℕ}
    (hparent : SwapInvariant r p W K rank A a) :
    SwapInvariant r p W (K.prune W) rank A a := by
  intro q hfix hq x hx hnx y hy hyq hkey
  exact hparent q hfix hq x hx hnx y hy (K.prune_qual_subset W hyq)
    (GlobalList.prune_key hx hkey)

end ChildSwaps
end ThreeMachine.Structure

end

end OAI