File size: 23,169 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import OAI.Computability.Scheduling.EffectiveFamily

namespace OAI

section

namespace ThreeMachine.Algorithm
open Structure

structure Block (J : Type) where
  length : ℕ
  time : J → ℕ

namespace Block
variable {J : Type} [Fintype J] [DecidableEq J] {r : J → J → Prop}

def Valid (r : J → J → Prop) (W : Finset J) (s : Block J) : Prop :=
  ∃ b : FullBlock r (W : Set J), b.length = s.length ∧ b.time = s.time

def empty : Block J := ⟨0, fun _ => 0⟩
def triple : Block J := ⟨1, fun _ => 1⟩
def append (U : Finset J) (s t : Block J) : Block J :=
  ⟨s.length + t.length, fun x => if x ∈ U then s.time x else s.length + t.time x⟩

omit [Fintype J] [DecidableEq J] in
theorem valid_empty : (empty : Block J).Valid r ∅ := by
  let b : FullBlock r (↑(∅ : Finset J) : Set J) :=
    { length := 0
      time := fun _ => 0
      bounds := by simp
      capacity := by simp
      respects := by simp
      size := by simp }
  exact ⟨b, rfl, rfl⟩

omit [DecidableEq J] in
theorem valid_triple {Z : Finset J} (hc : Z.card = 3)
    (ha : ∀ x ∈ Z, ∀ y ∈ Z, ¬r x y) : (triple : Block J).Valid r Z :=
  ⟨FullBlock.triple ⟨Z,hc⟩ ha, rfl, rfl⟩

theorem valid_append {U V : Finset J} {s t : Block J}
    (hs : s.Valid r U) (ht : t.Valid r V) (hd : Disjoint U V)
    (hb : ∀ x ∈ V, ∀ y ∈ U, ¬r x y) : (append U s t).Valid r (U ∪ V) := by
  obtain ⟨s', hsl, hst⟩ := hs
  obtain ⟨t', htl, htt⟩ := ht
  have hd' : Disjoint (U : Set J) (V : Set J) := Finset.disjoint_coe.mpr hd
  let b := s'.append t' hd' hb
  let c : FullBlock r (↑(U ∪ V) : Set J) :=
    { length := b.length, time := b.time
      bounds := fun x hx => b.bounds x (by simpa using hx)
      capacity := fun k => by simpa using b.capacity k
      respects := fun x hx y hy hxy => b.respects x (by simpa using hx) y (by simpa using hy) hxy
      size := by simpa using b.size }
  refine ⟨c, ?_, ?_⟩
  · change s'.length + t'.length = s.length + t.length
    rw [hsl, htl]
  · funext x
    by_cases hx : x ∈ U <;> simp [c, b, FullBlock.append, append, hx, hsl, hst, htt]

omit [Fintype J] [DecidableEq J] in
theorem Valid.of_eq {U V : Finset J} {s : Block J} (h : s.Valid r U) (he : U = V) :
    s.Valid r V := he ▸ h

end Block

structure State (J : Type) where
  left : Finset J
  middle : Finset J
  right : Finset J
  deriving DecidableEq

namespace State
variable {J : Type} [Fintype J] [DecidableEq J] {r : J → J → Prop}

def candidate (W F Z : Finset J) : State J :=
  ⟨W ∩ F, Z, W \ ((W ∩ F) ∪ Z)⟩

def Compatible (r : J → J → Prop) (W : Finset J) (s : State J) : Prop :=
  (s.left ∪ s.middle) ∪ s.right = W ∧
  Disjoint s.left s.middle ∧ Disjoint s.left s.right ∧ Disjoint s.middle s.right ∧
  s.middle.card = 3 ∧
  (∀ x ∈ s.middle, ∀ y ∈ s.middle, ¬r x y) ∧
  (∀ x ∈ s.middle, ∀ y ∈ s.left, ¬r x y) ∧
  (∀ x ∈ s.right, ∀ y ∈ s.left, ¬r x y) ∧
  (∀ x ∈ s.right, ∀ y ∈ s.middle, ¬r x y)

instance [DecidableRel r] (W : Finset J) (s : State J) : Decidable (s.Compatible r W) :=
  inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _))

def upto (s : State J) : Finset J := s.left ∪ s.middle

def advance (u : State J) (s t : Block J) : Block J :=
  Block.append u.upto (Block.append u.left s Block.triple) t

def finish (u : State J) (s t : Block J) : Block J := advance u s t

omit [Fintype J] in
theorem Compatible.left_subset {W : Finset J} {u : State J} (h : u.Compatible r W) :
    u.left ⊆ W := by
  rw [← h.1]
  exact fun _ hx => Finset.mem_union_left _ (Finset.mem_union_left _ hx)

omit [Fintype J] in
theorem Compatible.right_subset {W : Finset J} {u : State J} (h : u.Compatible r W) :
    u.right ⊆ W := by
  rw [← h.1]
  exact Finset.subset_union_right

def toSplit {W : Finset J} {u : State J} (h : u.Compatible r W) :
    SplitVertex r (id : Set J → Set J) (W : Set J) where
  left := u.left
  middle := ⟨u.middle, h.2.2.2.2.1⟩
  right := u.right
  partition := by
    intro x
    change x ∈ W ↔ x ∈ u.left ∨ x ∈ u.middle ∨ x ∈ u.right
    rw [← h.1]
    simp only [Finset.mem_union, or_assoc]
  left_middle := Finset.disjoint_coe.mpr h.2.1
  left_right := Finset.disjoint_coe.mpr h.2.2.1
  middle_right := Finset.disjoint_coe.mpr h.2.2.2.1
  antichain := h.2.2.2.2.2.1
  no_middle_left := h.2.2.2.2.2.2.1
  no_right_left := h.2.2.2.2.2.2.2.1
  no_right_middle := h.2.2.2.2.2.2.2.2
  small_left := ⟨u.left, (Described.atom _).mono (by omega),
    (Set.inter_eq_right.mpr h.left_subset).symm⟩
  small_right := ⟨u.right, (Described.atom _).mono (by omega),
    (Set.inter_eq_right.mpr h.right_subset).symm⟩

theorem valid_advance {W : Finset J} {u v : State J} {s t : Block J}
    (hu : u.Compatible r W) (hv : v.Compatible r W) (hs : s.Valid r u.left)
    (hsub : u.upto ⊆ v.left) (ht : t.Valid r (v.left \ u.upto)) :
    (u.advance s t).Valid r v.left := by
  have hs' := Block.valid_append hs
    (Block.valid_triple hu.2.2.2.2.1 hu.2.2.2.2.2.1) hu.2.1 hu.2.2.2.2.2.2.1
  have hd : Disjoint u.upto (v.left \ u.upto) :=
    Finset.disjoint_left.mpr (fun _ hx hy => (Finset.mem_sdiff.mp hy).2 hx)
  have hb : ∀ x ∈ v.left \ u.upto, ∀ y ∈ u.upto, ¬r x y := by
    intro x hx y hy
    have hxr : x ∈ u.right := by
      have hxW := hv.left_subset (Finset.mem_sdiff.mp hx).1
      rw [← hu.1] at hxW
      rcases Finset.mem_union.mp hxW with hp | hr
      · exact ((Finset.mem_sdiff.mp hx).2 hp).elim
      · exact hr
    rcases Finset.mem_union.mp hy with hy | hy
    · exact hu.2.2.2.2.2.2.2.1 x hxr y hy
    · exact hu.2.2.2.2.2.2.2.2 x hxr y hy
  have he : u.upto ∪ (v.left \ u.upto) = v.left := Finset.union_sdiff_of_subset hsub
  exact (Block.valid_append hs' ht hd hb).of_eq he

theorem valid_finish {W : Finset J} {u : State J} {s t : Block J}
    (hu : u.Compatible r W) (hs : s.Valid r u.left) (ht : t.Valid r u.right) :
    (u.finish s t).Valid r W := by
  have hs' := Block.valid_append hs
    (Block.valid_triple hu.2.2.2.2.1 hu.2.2.2.2.2.1) hu.2.1 hu.2.2.2.2.2.2.1
  have hd : Disjoint u.upto u.right := Finset.disjoint_union_left.mpr ⟨hu.2.2.1,hu.2.2.2.1⟩
  have hb : ∀ x ∈ u.right, ∀ y ∈ u.upto, ¬r x y := by
    intro x hx y hy
    exact (Finset.mem_union.mp hy).elim
      (hu.2.2.2.2.2.2.2.1 x hx y) (hu.2.2.2.2.2.2.2.2 x hx y)
  exact (Block.valid_append hs' ht hd hb).of_eq hu.1

omit [Fintype J] in
theorem left_card_strict {W : Finset J} {u v : State J}
    (hu : u.Compatible r W) (hsub : u.upto ⊆ v.left) : u.left.card < v.left.card := by
  have he : u.upto.card = u.left.card + 3 := by
    rw [upto, Finset.card_union_of_disjoint hu.2.1, hu.2.2.2.2.1]
  have := Finset.card_le_card hsub
  omega

end State
end ThreeMachine.Algorithm

namespace ThreeMachine.Algorithm

def lookup {K V : Type} [DecidableEq K] : List (K × V) → K → Option V
  | [], _ => none
  | (a,b)::xs, k => if a = k then some b else lookup xs k

def Has {K V : Type} [DecidableEq K] (tab : List (K × V)) (k : K) : Prop :=
  ∃ v, lookup tab k = some v

theorem lookup_mem {K V : Type} [DecidableEq K] {tab : List (K × V)} {k : K} {v : V}
    (h : lookup tab k = some v) : (k,v) ∈ tab := by
  induction tab with
  | nil => simp [lookup] at h
  | cons a xs ih =>
    simp only [lookup] at h
    split_ifs at h with he
    · subst k
      have he' : a.2 = v := Option.some.inj h
      rw [← he']
      exact List.mem_cons_self
    · exact List.mem_cons_of_mem _ (ih h)

theorem has_iff_mem {K V : Type} [DecidableEq K] (tab : List (K × V)) (k : K) :
    Has tab k ↔ ∃ v, (k,v) ∈ tab := by
  constructor
  · rintro ⟨v,hv⟩
    exact ⟨v, lookup_mem hv⟩
  · induction tab with
    | nil => simp
    | cons a xs ih =>
      rintro ⟨v,hv⟩
      by_cases he : a.1 = k
      · exact ⟨a.2, by simp [lookup, he]⟩
      · rw [List.mem_cons] at hv
        rcases hv with hv | hv
        · exact (he (congrArg Prod.fst hv).symm).elim
        · obtain ⟨v',hv'⟩ := ih ⟨v,hv⟩
          exact ⟨v', by simp only [lookup, ite_eq_right he, hv']⟩

def firstResult {K V : Type} (f : K → Option V) : List K → Option V
  | [] => none
  | k::ks => match f k with
    | some v => some v
    | none => firstResult f ks

theorem firstResult_some {K V : Type} {f : K → Option V} {ks : List K} {v : V}
    (h : firstResult f ks = some v) : ∃ k ∈ ks, f k = some v := by
  induction ks with
  | nil => simp [firstResult] at h
  | cons k ks ih =>
    cases hf : f k with
    | none =>
      obtain ⟨k',hk',he⟩ := ih (by simpa only [firstResult, hf] using h)
      exact ⟨k', List.mem_cons_of_mem _ hk',he⟩
    | some u =>
      have he : u = v := Option.some.inj (by simpa only [firstResult, hf] using h)
      exact ⟨k,List.mem_cons_self,he ▸ hf⟩

theorem firstResult_has {K V : Type} (f : K → Option V) (ks : List K) :
    (∃ v, firstResult f ks = some v) ↔ ∃ k ∈ ks, ∃ v, f k = some v := by
  constructor
  · rintro ⟨v,hv⟩
    obtain ⟨k,hk,hv⟩ := firstResult_some hv
    exact ⟨k,hk,v,hv⟩
  · induction ks with
    | nil => simp
    | cons k ks ih =>
      rintro ⟨k',hk',v,hv⟩
      cases hf : f k with
      | some u => exact ⟨u, by simp [firstResult,hf]⟩
      | none =>
        have hk : k' ∈ ks := by
          rcases List.mem_cons.mp hk' with he | he
          · subst k'
            rw [hf] at hv
            contradiction
          · exact he
        obtain ⟨u,hu⟩ := ih ⟨k',hk,v,hv⟩
        exact ⟨u, by simpa only [firstResult,hf] using hu⟩

def tableOf {K V : Type} (ks : List K) (f : K → Option V) : List (K × V) :=
  ks.filterMap (fun k => (f k).map (fun v => (k,v)))

theorem mem_tableOf {K V : Type} (ks : List K) (f : K → Option V) (k : K) (v : V) :
    (k,v) ∈ tableOf ks f ↔ k ∈ ks ∧ f k = some v := by
  rw [tableOf,List.mem_filterMap]
  constructor
  · rintro ⟨a,ha,he⟩
    cases hf : f a with
    | none => simp [hf] at he
    | some b =>
      have hab : (a,b) = (k,v) := Option.some.inj (by simpa [hf] using he)
      cases hab
      exact ⟨ha,hf⟩
  · rintro ⟨hk,hv⟩
    exact ⟨k,hk, by simp [hv]⟩

theorem has_tableOf {K V : Type} [DecidableEq K] (ks : List K) (f : K → Option V) (k : K) :
    Has (tableOf ks f) k ↔ k ∈ ks ∧ ∃ v, f k = some v := by
  rw [has_iff_mem]
  simp only [mem_tableOf, exists_and_left]

inductive Walk {K : Type} (r : K → K → Prop) : ℕ → K → K → Prop
  | refl (a : K) : Walk r 0 a a
  | tail {n a b c} : Walk r n a b → r b c → Walk r (n+1) a c

namespace Walk
variable {K : Type} {r : K → K → Prop}

theorem of_path {a b : K} (h : Relation.ReflTransGen r a b) : ∃ n, Walk r n a b := by
  induction h with
  | refl => exact ⟨0,.refl _⟩
  | tail _ hs ih => obtain ⟨n,hn⟩ := ih; exact ⟨n+1,.tail hn hs⟩

theorem rank_bound {ρ : K → ℕ} (hr : ∀ a b, r a b → ρ a < ρ b)
    {n a b} (h : Walk r n a b) : n + ρ a ≤ ρ b := by
  induction h with
  | refl => omega
  | tail _ hs ih => have := hr _ _ hs; omega

end Walk
end ThreeMachine.Algorithm

namespace ThreeMachine.Algorithm
open Structure
section DynamicProgram
variable {J : Type} [Fintype J] [DecidableEq J] (r : J → J → Prop) [DecidableRel r]

def states (family triples : List (Finset J)) (W : Finset J) : List (State J) :=
  (family.flatMap (fun F => triples.map (State.candidate W F))).filter (fun s => s.Compatible r W)

def tryAdvance (tab : List (Finset J × Block J)) (u : State J × Block J)
    (v : State J) : Option (Block J) :=
  if u.1.upto ⊆ v.left then (lookup tab (v.left \ u.1.upto)).map (u.1.advance u.2) else none

def expand (tab : List (Finset J × Block J)) (sts : List (State J))
    (marks : List (State J × Block J)) : List (State J × Block J) :=
  tableOf sts (fun v => match lookup marks v with
    | some s => some s
    | none => firstResult (fun u => tryAdvance tab u v) marks)

def markings (tab : List (Finset J × Block J)) (sts : List (State J)) :
    ℕ → List (State J × Block J)
  | 0 => tableOf sts (fun v => lookup tab v.left)
  | k+1 => expand tab sts (markings tab sts k)

def solve (tab : List (Finset J × Block J)) (family triples : List (Finset J))
    (W : Finset J) : Option (Block J) :=
  if W = ∅ then some Block.empty else
    firstResult (fun v => (lookup tab v.1.right).map (v.1.finish v.2))
      (markings tab (states r family triples W) (Fintype.card J))

def layers (family triples : List (Finset J)) : ℕ → List (Finset J × Block J)
  | 0 => [(∅, Block.empty)]
  | k+1 => tableOf family (solve r (layers family triples k) family triples)

def TableGood (tab : List (Finset J × Block J)) : Prop :=
  ∀ W s, (W,s) ∈ tab → s.Valid r W

def MarksGood (W : Finset J) (marks : List (State J × Block J)) : Prop :=
  ∀ u s, (u,s) ∈ marks → u.Compatible r W ∧ s.Valid r u.left

theorem states_compatible {family triples : List (Finset J)} {W : Finset J} {u : State J}
    (h : u ∈ states r family triples W) : u.Compatible r W := by
  exact of_decide_eq_true (List.mem_filter.mp h).2

omit [DecidableRel r] in
theorem markings_good {tab : List (Finset J × Block J)} {sts : List (State J)} {W : Finset J}
    (htab : TableGood r tab) (hsts : ∀ u ∈ sts, u.Compatible r W) (k : ℕ) :
    MarksGood r W (markings tab sts k) := by
  induction k with
  | zero =>
    intro u s h
    obtain ⟨hu, hs⟩ := (mem_tableOf sts _ u s).mp h
    exact ⟨hsts u hu, htab _ _ (lookup_mem hs)⟩
  | succ k ih =>
    intro v s h
    obtain ⟨hv, hs⟩ := (mem_tableOf sts _ v s).mp h
    refine ⟨hsts v hv, ?_⟩
    cases he : lookup (markings tab sts k) v with
    | some b =>
      have hb : b = s := Option.some.inj (by simpa only [he] using hs)
      exact hb ▸ (ih v b (lookup_mem he)).2
    | none =>
      obtain ⟨⟨u,b⟩, hu, hh⟩ := firstResult_some (by simpa only [he] using hs)
      simp only [tryAdvance] at hh
      split_ifs at hh with hsub
      · cases hg : lookup tab (v.left \ u.upto) with
        | none => simp [hg] at hh
        | some t =>
          have hes : u.advance b t = s := Option.some.inj (by simpa [hg] using hh)
          exact hes ▸ State.valid_advance (ih u b hu).1 (hsts v hv)
            (ih u b hu).2 hsub (htab _ _ (lookup_mem hg))

theorem solve_good {tab : List (Finset J × Block J)} (htab : TableGood r tab)
    (family triples : List (Finset J)) {W : Finset J} {s : Block J}
    (h : solve r tab family triples W = some s) : s.Valid r W := by
  simp only [solve] at h
  split_ifs at h with he
  · subst W
    have hs : Block.empty = s := Option.some.inj h
    exact hs ▸ Block.valid_empty
  · obtain ⟨⟨u,b⟩,hu,hs⟩ := firstResult_some h
    have hg := markings_good r htab (fun v hv => states_compatible r hv) (Fintype.card J) u b hu
    cases ht : lookup tab u.right with
    | none => simp [ht] at hs
    | some t =>
      have hes : u.finish b t = s := Option.some.inj (by simpa [ht] using hs)
      exact hes ▸ State.valid_finish hg.1 hg.2 (htab _ _ (lookup_mem ht))

theorem layers_good (family triples : List (Finset J)) (k : ℕ) :
    TableGood r (layers r family triples k) := by
  induction k with
  | zero =>
    intro W s h
    have he : (W,s) = (∅,Block.empty) := by simpa only [layers, List.mem_singleton] using h
    cases he
    exact Block.valid_empty
  | succ k ih =>
    intro W s h
    exact solve_good r ih family triples ((mem_tableOf family _ W s).mp h).2

omit [Fintype J] [DecidableRel r] in
theorem markings_stay {tab : List (Finset J × Block J)} {sts : List (State J)}
    {v : State J} (hv : v ∈ sts) {k : ℕ} (h : Has (markings tab sts k) v) :
    Has (markings tab sts (k+1)) v := by
  obtain ⟨s,hs⟩ := h
  apply (has_tableOf sts _ v).mpr
  exact ⟨hv,s,by simp only [hs]⟩

omit [Fintype J] [DecidableRel r] in
theorem markings_mono {tab : List (Finset J × Block J)} {sts : List (State J)}
    {v : State J} (hv : v ∈ sts) {k l : ℕ} (h : Has (markings tab sts k) v) (hkl : k ≤ l) :
    Has (markings tab sts l) v := by
  obtain ⟨d,rfl⟩ := Nat.exists_eq_add_of_le hkl
  clear hkl
  induction d with
  | zero => exact h
  | succ d ih => exact markings_stay hv ih

omit [Fintype J] [DecidableRel r] in
theorem markings_step {tab : List (Finset J × Block J)} {sts : List (State J)}
    {u v : State J} (hv : v ∈ sts) (hsub : u.upto ⊆ v.left)
    (hgap : Has tab (v.left \ u.upto)) {k : ℕ} (hu : Has (markings tab sts k) u) :
    Has (markings tab sts (k+1)) v := by
  apply (has_tableOf sts _ v).mpr
  refine ⟨hv, ?_⟩
  cases he : lookup (markings tab sts k) v with
  | some b => exact ⟨b,rfl⟩
  | none =>
    apply (firstResult_has _ _).mpr
    obtain ⟨s,hs⟩ := hu
    obtain ⟨t,ht⟩ := hgap
    exact ⟨(u,s),lookup_mem hs,u.advance s t,by simp [tryAdvance,hsub,ht]⟩

def Arc (tab : List (Finset J × Block J)) (sts : List (State J)) (u v : State J) : Prop :=
  u ∈ sts ∧ v ∈ sts ∧ u.upto ⊆ v.left ∧ Has tab (v.left \ u.upto)

omit [Fintype J] [DecidableRel r] in
theorem markings_walk {tab : List (Finset J × Block J)} {sts : List (State J)}
    {u v : State J} {k : ℕ} (hu : u ∈ sts) (hinit : Has tab u.left)
    (h : Walk (Arc tab sts) k u v) : Has (markings tab sts k) v := by
  induction h with
  | refl => exact (has_tableOf sts _ _).mpr ⟨hu,hinit⟩
  | tail _ hs ih => exact markings_step hs.2.1 hs.2.2.1 hs.2.2.2 (ih hu hinit)

theorem solve_complete_path {tab : List (Finset J × Block J)} {family triples : List (Finset J)}
    {W : Finset J} {u v : State J} (hu : u ∈ states r family triples W)
    (hv : v ∈ states r family triples W) (hinit : Has tab u.left) (hfinal : Has tab v.right)
    (hpath : Relation.ReflTransGen (Arc tab (states r family triples W)) u v) :
    ∃ s, solve r tab family triples W = some s := by
  by_cases he : W = ∅
  · exact ⟨Block.empty, by simp [solve,he]⟩
  obtain ⟨k,hk⟩ := Walk.of_path hpath
  have hbound : k ≤ Fintype.card J := by
    have hb := hk.rank_bound (ρ := fun s => s.left.card) (fun a b hh =>
      State.left_card_strict (states_compatible r hh.1) hh.2.2.1)
    have hc := Finset.card_le_univ v.left
    omega
  have hm := markings_mono hv (markings_walk hu hinit hk) hbound
  obtain ⟨s,hs⟩ := hm
  obtain ⟨t,ht⟩ := hfinal
  simp only [solve,ite_eq_right he]
  apply (firstResult_has _ _).mpr
  exact ⟨(v,s),lookup_mem hs,v.finish s t,by simp [ht]⟩

end DynamicProgram
end ThreeMachine.Algorithm

namespace ThreeMachine.Algorithm
open Structure
open scoped Classical
section EraseSplits
variable {J A : Type} [Fintype J] [DecidableEq J] {r : J → J → Prop}
    {atoms : A → Set J} {W : Set J}

omit [Fintype J] [DecidableEq J] in
@[ext] theorem State.ext {u v : State J} (hL : u.left = v.left)
    (hM : u.middle = v.middle) (hR : u.right = v.right) : u = v := by
  cases u
  cases v
  cases hL
  cases hM
  cases hR
  rfl

noncomputable def State.erase (v : SplitVertex r atoms W) : State J :=
  ⟨v.left.toFinset, v.middle.val, v.right.toFinset⟩

theorem State.erase_compatible (v : SplitVertex r atoms W) :
    (erase v).Compatible r W.toFinset := by
  classical
  refine ⟨?_, ?_, ?_, ?_, v.middle.property, ?_, ?_, ?_, ?_⟩
  · ext x
    simpa only [erase, Finset.mem_union, Set.mem_toFinset, or_assoc] using (v.partition x).symm
  · apply Finset.disjoint_left.mpr
    intro x hx hy
    exact Set.disjoint_left.mp v.left_middle (Set.mem_toFinset.mp hx) hy
  · apply Finset.disjoint_left.mpr
    intro x hx hy
    exact Set.disjoint_left.mp v.left_right (Set.mem_toFinset.mp hx) (Set.mem_toFinset.mp hy)
  · apply Finset.disjoint_left.mpr
    intro x hx hy
    exact Set.disjoint_left.mp v.middle_right hx (Set.mem_toFinset.mp hy)
  · exact v.antichain
  · intro x hx y hy
    exact v.no_middle_left x hx y (Set.mem_toFinset.mp hy)
  · intro x hx y hy
    exact v.no_right_left x (Set.mem_toFinset.mp hx) y (Set.mem_toFinset.mp hy)
  · intro x hx y hy
    exact v.no_right_middle x (Set.mem_toFinset.mp hx) y hy

theorem State.erase_candidate (v : SplitVertex r atoms W) {F : Set J} (hF : v.left = W ∩ F) :
    candidate W.toFinset F.toFinset v.middle.val = erase v := by
  classical
  apply State.ext
  · ext x
    change x ∈ W.toFinset ∩ F.toFinset ↔ x ∈ v.left.toFinset
    simp only [Finset.mem_inter, Set.mem_toFinset,hF,Set.mem_inter_iff]
  · rfl
  · ext x
    change x ∈ W.toFinset \ ((W.toFinset ∩ F.toFinset) ∪ v.middle.val) ↔ x ∈ v.right.toFinset
    simp only [Finset.mem_sdiff, Finset.mem_inter, Finset.mem_union, Set.mem_toFinset]
    rw [v.right_eq]
    simp only [Set.mem_sdiff, Set.mem_union, Finset.mem_coe, hF, Set.mem_inter_iff]

theorem State.erase_mem_states [DecidableRel r] (family triples : List (Finset J))
    (hfamily : ∀ F, Described atoms F 10000 → F.toFinset ∈ family)
    (htriples : ∀ Z : Triple J, Z.val ∈ triples) (v : SplitVertex r atoms W) :
    erase v ∈ states r family triples W.toFinset := by
  classical
  obtain ⟨F,hF,he⟩ := v.small_left
  apply List.mem_filter.mpr
  refine ⟨List.mem_flatMap.mpr ⟨F.toFinset,hfamily F hF,
    List.mem_map.mpr ⟨v.middle.val,htriples v.middle, erase_candidate v he⟩⟩, ?_⟩
  exact decide_eq_true (erase_compatible v)

theorem State.erase_inclusion {u v : SplitVertex r atoms W}
    (h : u.left ∪ u.middle.val ⊆ v.left) : (erase u).upto ⊆ (erase v).left := by
  classical
  intro x hx
  apply Set.mem_toFinset.mpr
  apply h
  rcases Finset.mem_union.mp hx with hx | hx
  · exact Or.inl (Set.mem_toFinset.mp hx)
  · exact Or.inr hx

theorem State.erase_gap (u v : SplitVertex r atoms W) :
    (v.left \ (u.left ∪ u.middle.val)).toFinset = (erase v).left \ (erase u).upto := by
  classical
  ext x
  simp only [Set.mem_toFinset, Set.mem_sdiff, Set.mem_union, Finset.mem_coe,
    erase, upto, Finset.mem_sdiff, Finset.mem_union]

end EraseSplits

section Completeness
variable {J A : Type} [Fintype J] [DecidableEq J] (r : J → J → Prop) [DecidableRel r]
    (atoms : A → Set J) (family triples : List (Finset J))
    (hfamily : ∀ F, Described atoms F 10000 → F.toFinset ∈ family)
    (htriples : ∀ Z : Triple J, Z.val ∈ triples)

include hfamily htriples in
theorem layers_complete (k : ℕ) {W : Set J} (h : AcceptedAt r atoms k W) :
    Has (layers r family triples k) W.toFinset := by
  classical
  induction k generalizing W with
  | zero =>
    have he : W = ∅ := h.2
    subst W
    refine ⟨Block.empty, ?_⟩
    simp [layers, lookup]
  | succ k ih =>
    apply (has_tableOf family _ W.toFinset).mpr
    refine ⟨hfamily W h.1, ?_⟩
    rcases h.2 with he | ⟨u,v,hu,hp,hv⟩
    · subst W
      exact ⟨Block.empty,by simp [solve]⟩
    · have hstate (s : SplitVertex r atoms W) := State.erase_mem_states family triples hfamily htriples s
      apply solve_complete_path r (hstate u) (hstate v) (ih hu) (ih hv)
      clear hv
      induction hp with
      | refl => exact .refl
      | @tail v w _ hs ihp =>
        refine ihp.tail ⟨hstate v,hstate w,State.erase_inclusion hs.1,?_⟩
        rw [← State.erase_gap]
        simpa only [← Set.toFinite_toFinset] using ih hs.2

end Completeness
end ThreeMachine.Algorithm

end

end OAI