File size: 18,112 Bytes
2fca478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Geometry.PeriodicTiling.Dependence
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.Data.ZMod.Basic
import Mathlib.Tactic.Abel

namespace OAI

universe uι uM uN uA uB uH uC

noncomputable section

namespace PeriodicTilingThree

open scoped BigOperators

section CycleCombinatorics

variable {ι : Type uι} [Fintype ι]
variable {M : ι → Type uM} {N : ι → Type uN}
variable [∀ i, AddCommGroup (M i)] [∀ i, AddCommGroup (N i)]
variable {t : ℕ} [NeZero t]

def cycleExtend (cycle : ZMod t ↪ ι) (v : ∀ j, M (cycle j)) : ∀ i, M i := by
  classical
  exact ∑ j : ZMod t, Pi.single (cycle j) (v j)

@[simp] theorem cycleExtend_apply
    {ι : Type uι} [Fintype ι]
    {M : ι → Type uM} [∀ i, AddCommGroup (M i)] {t : ℕ} [NeZero t]
    (cycle : ZMod t ↪ ι)
    (v : ∀ j, M (cycle j)) (j : ZMod t) :
    cycleExtend cycle v (cycle j) = v j := by
  classical
  rw [cycleExtend, Finset.sum_apply, Finset.sum_eq_single j]
  · simp
  · intro l _ hlj
    have hne : cycle l ≠ cycle j := fun h => hlj (cycle.injective h)
    simp [Ne.symm hne]
  · simp

theorem cycleExtend_eq_zero
    {ι : Type uι} [Fintype ι]
    {M : ι → Type uM} [∀ i, AddCommGroup (M i)] {t : ℕ} [NeZero t]
    (cycle : ZMod t ↪ ι)
    (v : ∀ j, M (cycle j)) {i : ι} (hi : ∀ j, cycle j ≠ i) :
    cycleExtend cycle v i = 0 := by
  classical
  rw [cycleExtend, Finset.sum_apply]
  apply Finset.sum_eq_zero
  intro j _
  simp [Ne.symm (hi j)]

def cycleShift (cycle : ZMod t ↪ ι) (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j) (e : ∀ i, N i) : ∀ i, M i :=
  cycleExtend cycle (fun j => (d j (e (cycle (j - 1))) : M (cycle j)))

@[simp] theorem cycleShift_apply
    {ι : Type uι} [Fintype ι]
    {M : ι → Type uM} {N : ι → Type uN}
    [∀ i, AddCommGroup (M i)] [∀ i, AddCommGroup (N i)] {t : ℕ} [NeZero t]
    (cycle : ZMod t ↪ ι)
    (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j) (e : ∀ i, N i) (j : ZMod t) :
    cycleShift cycle U d e (cycle j) = (d j (e (cycle (j - 1))) : M (cycle j)) :=
  cycleExtend_apply cycle _ j

theorem cycleShift_eq_zero
    {ι : Type uι} [Fintype ι]
    {M : ι → Type uM} {N : ι → Type uN}
    [∀ i, AddCommGroup (M i)] [∀ i, AddCommGroup (N i)] {t : ℕ} [NeZero t]
    (cycle : ZMod t ↪ ι)
    (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j) (e : ∀ i, N i)
    {i : ι} (hi : ∀ j, cycle j ≠ i) : cycleShift cycle U d e i = 0 :=
  cycleExtend_eq_zero cycle _ hi

def cycleMap (cycle : ZMod t ↪ ι) (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j)
    (g : ∀ i, M i → N i) (k : ∀ i, M i) (e : ∀ i, N i) : ∀ i, N i :=
  fun i => e i + g i (k i - cycleShift cycle U d e i)

@[simp] theorem cycleMap_apply_cycle (cycle : ZMod t ↪ ι)
    (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j)
    (g : ∀ i, M i → N i) (k : ∀ i, M i) (e : ∀ i, N i) (j : ZMod t) :
    cycleMap cycle U d g k e (cycle j) =
      e (cycle j) + g (cycle j) (k (cycle j) - d j (e (cycle (j - 1)))) := by
  simp only [cycleMap, cycleShift_apply]

theorem forall_of_cyclic_step {Q : ZMod t → Prop} (j₀ : ZMod t) (h₀ : Q j₀)
    (hstep : ∀ j, Q j → Q (j + 1)) : ∀ j, Q j := by
  have hnat : ∀ n : ℕ, Q (j₀ + (n : ZMod t)) := by
    intro n
    induction n with
    | zero => simpa using h₀
    | succ n ih => simpa only [Nat.cast_succ, add_assoc] using hstep _ ih
  intro j
  have h := hnat (j - j₀).val
  simpa only [ZMod.natCast_zmod_val, add_sub_cancel] using h

theorem cycleMap_injective_of_inactive (cycle : ZMod t ↪ ι)
    (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j)
    (g : ∀ i, M i → N i) (k : ∀ i, M i)
    (j₀ : ZMod t) (hinactive : ¬ DigitActive (g (cycle j₀)) (U j₀)) :
    Function.Injective (cycleMap cycle U d g k) := by
  classical
  have hagree {w w' : M (cycle j₀)} (hw : w - w' ∈ U j₀) :
      g (cycle j₀) w = g (cycle j₀) w' := by
    by_contra hne
    exact hinactive ⟨w, w', hw, hne⟩
  have hconstant (e : ∀ i, N i) :
      g (cycle j₀) (k (cycle j₀) - d j₀ (e (cycle (j₀ - 1)))) =
        g (cycle j₀) (k (cycle j₀)) := by
    apply hagree
    have heq : (k (cycle j₀) - (d j₀ (e (cycle (j₀ - 1))) : M (cycle j₀))) -
        k (cycle j₀) = -((d j₀ (e (cycle (j₀ - 1)))) : M (cycle j₀)) := by abel
    rw [heq]
    exact (U j₀).neg_mem (d j₀ (e (cycle (j₀ - 1)))).property
  intro e e' he
  have hstart : e (cycle j₀) = e' (cycle j₀) := by
    have h := congrFun he (cycle j₀)
    rw [cycleMap_apply_cycle, cycleMap_apply_cycle, hconstant, hconstant] at h
    exact add_right_cancel h
  have hstep (j : ZMod t) (hj : e (cycle j) = e' (cycle j)) :
      e (cycle (j + 1)) = e' (cycle (j + 1)) := by
    have h := congrFun he (cycle (j + 1))
    simp only [cycleMap_apply_cycle] at h
    have hprev : e (cycle (j + 1 - 1)) = e' (cycle (j + 1 - 1)) := by
      exact Eq.mpr (congrArg (fun l : ZMod t => e (cycle l) = e' (cycle l))
        (add_sub_cancel_right j 1)) hj
    rw [hprev] at h
    exact add_right_cancel h
  have hon := forall_of_cyclic_step j₀ hstart hstep
  funext i
  by_cases hi : ∃ j, cycle j = i
  · obtain ⟨j, rfl⟩ := hi
    exact hon j
  · have hoff : ∀ j, cycle j ≠ i := by simpa only [not_exists] using hi
    have h := congrFun he i
    simp only [cycleMap, cycleShift_eq_zero cycle U d e hoff,
      cycleShift_eq_zero cycle U d e' hoff, sub_zero] at h
    exact add_right_cancel h

theorem cycleMap_bijective_of_inactive [∀ i, Fintype (N i)]
    (cycle : ZMod t ↪ ι) (U : ∀ j, AddSubgroup (M (cycle j)))
    (d : ∀ j, N (cycle (j - 1)) → U j)
    (g : ∀ i, M i → N i) (k : ∀ i, M i)
    (j₀ : ZMod t) (hinactive : ¬ DigitActive (g (cycle j₀)) (U j₀)) :
    Function.Bijective (cycleMap cycle U d g k) :=
  (cycleMap_injective_of_inactive cycle U d g k j₀ hinactive).bijective_of_finite

theorem cycleMap_collision_of_all_active (cycle : ZMod t ↪ ι)
    (U : ∀ j, AddSubgroup (M (cycle j))) (g : ∀ i, M i → N i)
    (hactive : ∀ j, DigitActive (g (cycle j)) (U j)) :
    ∃ (k : ∀ i, M i) (d : ∀ j, N (cycle (j - 1)) → U j)
      (ε : ∀ i, N i), ε ≠ 0 ∧
      cycleMap cycle U d g k 0 = cycleMap cycle U d g k ε := by
  classical
  choose w w' hmem hne using hactive
  let ε₀ : ∀ j, N (cycle j) := fun j => g (cycle j) (w j) - g (cycle j) (w' j)
  have hε₀ (j : ZMod t) : ε₀ j ≠ 0 := sub_ne_zero.mpr (hne j)
  let ε : ∀ i, N i := cycleExtend cycle ε₀
  let k : ∀ i, M i := cycleExtend cycle w
  let d : ∀ j, N (cycle (j - 1)) → U j := fun j v =>
    if v = ε₀ (j - 1) then ⟨w j - w' j, hmem j⟩ else 0
  have hd₀ (j : ZMod t) : d j 0 = 0 := by
    simp only [d, ite_eq_right (Ne.symm (hε₀ (j - 1)))]
  have hdε (j : ZMod t) : d j (ε (cycle (j - 1))) = ⟨w j - w' j, hmem j⟩ := by
    simp only [d, ε, cycleExtend_apply, ite_eq_left rfl]
  refine ⟨k, d, ε, ?_, ?_⟩
  · intro hz
    have h := congrFun hz (cycle 0)
    apply hε₀ 0
    simpa only [ε, cycleExtend_apply, Pi.zero_apply] using h
  · funext i
    by_cases hi : ∃ j, cycle j = i
    · obtain ⟨j, rfl⟩ := hi
      calc
        cycleMap cycle U d g k 0 (cycle j) = g (cycle j) (w j) := by
          simp only [cycleMap_apply_cycle, Pi.zero_apply, hd₀, ZeroMemClass.coe_zero,
            sub_zero, zero_add, k, cycleExtend_apply]
        _ = ε₀ j + g (cycle j) (w' j) := (sub_add_cancel _ _).symm
        _ = cycleMap cycle U d g k ε (cycle j) := by
          rw [cycleMap_apply_cycle, hdε]
          simp only [k, ε, cycleExtend_apply, sub_sub_cancel]
    · have hoff : ∀ j, cycle j ≠ i := by simpa only [not_exists] using hi
      simp only [cycleMap, Pi.zero_apply, ε, cycleExtend_eq_zero cycle ε₀ hoff,
        cycleShift_eq_zero cycle U d 0 hoff,
        cycleShift_eq_zero cycle U d (cycleExtend cycle ε₀) hoff]

theorem forall_cycleMap_bijective_iff [∀ i, Fintype (N i)]
    (cycle : ZMod t ↪ ι) (U : ∀ j, AddSubgroup (M (cycle j)))
    (g : ∀ i, M i → N i) :
    (∀ (d : ∀ j, N (cycle (j - 1)) → U j) (k : ∀ i, M i),
      Function.Bijective (cycleMap cycle U d g k)) ↔
      ∃ j, ¬ DigitActive (g (cycle j)) (U j) := by
  constructor
  · intro hall
    by_contra hnone
    have hactive : ∀ j, DigitActive (g (cycle j)) (U j) := by
      simpa only [not_exists, not_not] using hnone
    obtain ⟨k, d, ε, hε, hcollision⟩ :=
      cycleMap_collision_of_all_active cycle U g hactive
    exact hε ((hall d k).1 hcollision).symm
  · rintro ⟨j, hj⟩ d k
    exact cycleMap_bijective_of_inactive cycle U d g k j hj

end CycleCombinatorics

theorem existsUnique_fixed_comp_iff {A : Type uA} {B : Type uB} (f : A → B) (g : B → A) :
    (∃! a, g (f a) = a) ↔ ∃! b, f (g b) = b := by
  constructor
  · rintro ⟨a, ha, hu⟩
    refine ⟨f a, ?_, ?_⟩
    · exact congrArg f ha
    · intro b hb
      have hg : g b = a := hu _ (congrArg g hb)
      calc
        b = f (g b) := hb.symm
        _ = f a := congrArg f hg
  · rintro ⟨b, hb, hu⟩
    refine ⟨g b, ?_, ?_⟩
    · exact congrArg g hb
    · intro a ha
      have hf : f a = b := hu _ (congrArg f ha)
      calc
        a = g (f a) := ha.symm
        _ = g b := congrArg g hf

theorem tiles_offset_iff_permutation
    {H : Type uH} {B : Type uB} {C : Type uC} [AddCommGroup H] [AddCommGroup B] [AddCommGroup C]
    (q : H →+ B) (ψ : H →+ C) (s : B → H) (hs : ∀ b, q (s b) = b)
    (hjoint : Function.Surjective (fun y => (q y, ψ y)))
    (Δ : C → B) (F : Finset H)
    (hF : ∀ y, y ∈ F ↔ q y = Δ (ψ y)) :
    Tiles F (Set.range s) ↔
      ∀ b, Function.Bijective (fun e => e + ψ (s (b - Δ e))) := by
  have hinj : Function.Injective s := by
    intro b b' h
    simpa only [hs] using congrArg q h
  have hmem (y : H) (b : B) : y - s b ∈ F ↔
      q y - Δ (ψ y - ψ (s b)) = b := by
    rw [hF, map_sub, hs, map_sub]
    simp only [sub_eq_iff_eq_add]
    exact ⟨fun h => h.trans (add_comm _ _), fun h => h.trans (add_comm _ _)⟩
  have hfibre (y : H) : (∃! b, y - s b ∈ F) ↔
      ∃! e, e + ψ (s (q y - Δ e)) = ψ y := by
    calc
      _ ↔ ∃! b, q y - Δ (ψ y - ψ (s b)) = b := existsUnique_congr (hmem y)
      _ ↔ ∃! e, ψ y - ψ (s (q y - Δ e)) = e :=
        existsUnique_fixed_comp_iff (fun b => ψ y - ψ (s b)) (fun e => q y - Δ e)
      _ ↔ _ := existsUnique_congr (fun e => by rw [sub_eq_iff_eq_add]; exact eq_comm)
  rw [tiles_range_iff_unique_base s hinj F]
  constructor
  · intro h b
    apply (Function.bijective_iff_existsUnique _).mpr
    intro c
    obtain ⟨y, hy⟩ := hjoint (b, c)
    have hq : q y = b := congrArg Prod.fst hy
    have hψ : ψ y = c := congrArg Prod.snd hy
    simpa only [hq, hψ] using (hfibre y).mp (h y)
  · intro h y
    exact (hfibre y).mpr ((h (q y)).existsUnique (ψ y))

section ConcreteTiles

variable {p : ℕ} [NeZero p] (E : EncodingParameters p)

def usefulProjection : Ambient E →+ Useful E where
  toFun v := fun i => (v.2.2 i).2
  map_zero' := rfl
  map_add' _ _ := rfl

@[simp] theorem usefulProjection_apply (v : Ambient E) (i : Channel p) :
    usefulProjection E v i = (v.2.2 i).2 := rfl

@[simp] theorem usefulProjection_point (o : GraphOutputs E) (b : Base E) :
    usefulProjection E (point o b) = o.c b := rfl

theorem q0_usefulProjection_surjective :
    Function.Surjective (fun y : Ambient E => (q0 E y, usefulProjection E y)) := by
  rintro ⟨b, c⟩
  let o : GraphOutputs E := ⟨fun _ => c, fun _ => 0, fun _ => 0⟩
  refine ⟨point o b, ?_⟩
  exact Prod.ext (q0_point o b) rfl

private theorem offset_horizontal_zero (D : Useful E → Low E)
    (v : {v : Ambient E // q0 E v = (0, D (usefulProjection E v))}) :
    (v : Ambient E).1 = 0 := congrArg Prod.fst v.property

noncomputable instance offsetFintype (D : Useful E → Low E) :
    Fintype {v : Ambient E // q0 E v = (0, D (usefulProjection E v))} :=
  Fintype.ofInjective (fun v => (v : Ambient E).2) (by
    intro v w h
    apply Subtype.ext
    exact Prod.ext ((offset_horizontal_zero E D v).trans
      (offset_horizontal_zero E D w).symm) h)

def offsetTile (D : Useful E → Low E) : Finset (Ambient E) :=
  (Finset.univ : Finset {v : Ambient E // q0 E v = (0, D (usefulProjection E v))}).map
    ⟨Subtype.val, Subtype.val_injective⟩

@[simp] theorem mem_offsetTile (D : Useful E → Low E) (v : Ambient E) :
    v ∈ offsetTile E D ↔ q0 E v = (0, D (usefulProjection E v)) := by
  constructor
  · intro h
    obtain ⟨w, _, rfl⟩ := Finset.mem_map.mp h
    exact w.property
  · intro h
    exact Finset.mem_map.mpr ⟨⟨v, h⟩, Finset.mem_univ _, rfl⟩

theorem offsetTile_nonempty (D : Useful E → Low E) : (offsetTile E D).Nonempty := by
  let o : GraphOutputs E := ⟨fun _ => 0, fun _ => 0, fun _ => 0⟩
  refine ⟨point o (0, D 0), (mem_offsetTile E D _).mpr ?_⟩
  exact q0_point o (0, D 0)

theorem offsetTile_iff (D : Useful E → Low E) (o : GraphOutputs E) :
    Tiles (offsetTile E D) (graph o) ↔
      ∀ x k, Function.Bijective (fun e : Useful E => e + o.c (x, k - D e)) := by
  have h := tiles_offset_iff_permutation (q0 E) (usefulProjection E) (point o)
    (q0_point o) (q0_usefulProjection_surjective E) (fun e => (0, D e))
    (offsetTile E D) (mem_offsetTile E D)
  change Tiles (offsetTile E D) (graph o) ↔ _ at h
  constructor
  · intro htile x k
    have hb := h.mp htile (x, k)
    change Function.Bijective (fun e : Useful E => e + o.c (x - 0, k - D e)) at hb
    simpa only [sub_zero] using hb
  · intro hmap
    apply h.mpr
    rintro ⟨x, k⟩
    change Function.Bijective (fun e : Useful E => e + o.c (x - 0, k - D e))
    simpa only [sub_zero] using hmap x k

variable {t : ℕ} [NeZero t]

def cycleTile (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j))
    (d : ∀ j, P E (cycle (j - 1)) → digitSubgroup E (cycle j) (labels j)) :
    Finset (Ambient E) :=
  offsetTile E (cycleShift cycle (fun j => digitSubgroup E (cycle j) (labels j)) d)

theorem cycleTile_nonempty (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j))
    (d : ∀ j, P E (cycle (j - 1)) → digitSubgroup E (cycle j) (labels j)) :
    (cycleTile E cycle labels d).Nonempty := offsetTile_nonempty E _

theorem cycleTile_iff (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j))
    (d : ∀ j, P E (cycle (j - 1)) → digitSubgroup E (cycle j) (labels j))
    (o : GraphOutputs E) (g : ∀ i, Plane → K E i → P E i)
    (hfactor : ∀ x k i, o.c (x, k) i = g i x (k i)) :
    Tiles (cycleTile E cycle labels d) (graph o) ↔
      ∀ x k, Function.Bijective
        (cycleMap cycle (fun j => digitSubgroup E (cycle j) (labels j)) d
          (fun i => g i x) k) := by
  let U := fun j => digitSubgroup E (cycle j) (labels j)
  have hm (x : Plane) (k : Low E) :
      (fun e : Useful E => e + o.c (x, k - cycleShift cycle U d e)) =
        cycleMap cycle U d (fun i => g i x) k := by
    funext e i
    change e i + o.c (x, k - cycleShift cycle U d e) i =
      e i + g i x (k i - cycleShift cycle U d e i)
    exact congrArg (fun v => e i + v) (hfactor x (k - cycleShift cycle U d e) i)
  simpa only [cycleTile, hm] using offsetTile_iff E (cycleShift cycle U d) o

theorem all_cycleTile_iff (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j))
    (o : GraphOutputs E) (g : ∀ i, Plane → K E i → P E i)
    (hfactor : ∀ x k i, o.c (x, k) i = g i x (k i)) :
    (∀ d : ∀ j, P E (cycle (j - 1)) → digitSubgroup E (cycle j) (labels j),
      Tiles (cycleTile E cycle labels d) (graph o)) ↔
      ∀ x, ∃ j, ¬ DigitActive (g (cycle j) x) (digitSubgroup E (cycle j) (labels j)) := by
  constructor
  · intro h x
    apply (forall_cycleMap_bijective_iff cycle
      (fun j => digitSubgroup E (cycle j) (labels j)) (fun i => g i x)).mp
    intro d k
    exact (cycleTile_iff E cycle labels d o g hfactor).mp (h d) x k
  · intro h d
    apply (cycleTile_iff E cycle labels d o g hfactor).mpr
    intro x k
    obtain ⟨j, hj⟩ := h x
    exact cycleMap_bijective_of_inactive cycle
      (fun j => digitSubgroup E (cycle j) (labels j)) d (fun i => g i x) k j hj

def cycleTiles (cycle : ZMod t ↪ Channel p) (labels : ∀ j, Label p (cycle j)) :
    Finset (Finset (Ambient E)) := by
  classical
  exact Finset.univ.image (cycleTile E cycle labels)

theorem cycleTiles_nonempty (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j)) : (cycleTiles E cycle labels).Nonempty := by
  classical
  exact ⟨cycleTile E cycle labels (fun _ _ => 0),
    Finset.mem_image.mpr ⟨(fun _ _ => 0), Finset.mem_univ _, rfl⟩⟩

theorem nonempty_of_mem_cycleTiles (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j)) {F : Finset (Ambient E)}
    (hF : F ∈ cycleTiles E cycle labels) : F.Nonempty := by
  classical
  obtain ⟨d, _, rfl⟩ := Finset.mem_image.mp hF
  exact cycleTile_nonempty E cycle labels d

theorem forall_mem_cycleTiles_iff (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j)) (A : Set (Ambient E)) :
    (∀ F ∈ cycleTiles E cycle labels, Tiles F A) ↔
      ∀ d : ∀ j, P E (cycle (j - 1)) → digitSubgroup E (cycle j) (labels j),
        Tiles (cycleTile E cycle labels d) A := by
  classical
  simp only [cycleTiles, Finset.mem_image, Finset.mem_univ, true_and]
  constructor
  · intro h d
    exact h _ ⟨d, rfl⟩
  · intro h F hF
    obtain ⟨d, rfl⟩ := hF
    exact h d

theorem cycle_exclusion_iff (cycle : ZMod t ↪ Channel p)
    (labels : ∀ j, Label p (cycle j)) (o : GraphOutputs E)
    (hdep : HasDependence E o) :
    (∀ F ∈ cycleTiles E cycle labels, Tiles F (graph o)) ↔
      ∀ x, ∃ j, ¬ Active E o (cycle j) x (labels j) := by
  rw [forall_mem_cycleTiles_iff]
  exact all_cycleTile_iff E cycle labels o (usefulAt E o) (usefulAt_factor E hdep)

end ConcreteTiles

end PeriodicTilingThree

end

end OAI