File size: 28,126 Bytes
e3e0a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import Mathlib
import OAI.SetTheory.PartitionConsistency.Algebra.DiagramOrder
import OAI.SetTheory.PartitionConsistency.Forcing.Diagrams
import OAI.SetTheory.PartitionConsistency.Algebra.Placements
import OAI.SetTheory.PartitionConsistency.Algebra.PlacementExtensions

namespace OAI

namespace PartitionConsistency
namespace DiagramOrder
lemma pair_lub_iff {α : Type*} [Preorder α] (a b j : α) :
    IsLUB {a, b} j ↔ ∀ z, a ≤ z ∧ b ≤ z ↔ j ≤ z := by
  simp only [isLUB_iff_le_iff, upperBounds, Set.mem_ofPred_eq,
    Set.forall_mem_insert, Set.mem_singleton_iff, forall_eq]
  exact forall_congr' (fun _ => iff_comm)
namespace LeftDivOrder
lemma le_iff_dvd {M : Type*} [LeftCancelMonoid M] [IsConical M]
    (x y : LeftDivOrder M) : x ≤ y ↔ x ∣ y := by
  exact exists_congr (fun _ => eq_comm)
end LeftDivOrder
lemma exists_cone_join {M : Type*} [LeftCancelMonoid M] [IsConical M]
    (hM : HasConditionalJoins (LeftDivOrder M)) (x y a : M)
    (hx : x ∣ a) (hy : y ∣ a) :
    ∃ j : M, ∀ z : M, x ∣ z ∧ y ∣ z ↔ j ∣ z := by
  obtain ⟨j, hj⟩ := hM x y a ((LeftDivOrder.le_iff_dvd _ _).2 hx)
    ((LeftDivOrder.le_iff_dvd _ _).2 hy)
  refine ⟨j, ?_⟩
  intro z
  have hz := (pair_lub_iff (α := LeftDivOrder M) x y j).1 hj z
  rw [LeftDivOrder.le_iff_dvd (M := M) x z,
    LeftDivOrder.le_iff_dvd (M := M) y z,
    LeftDivOrder.le_iff_dvd (M := M) j z] at hz
  exact hz
/-- Cancellation for right divisibility in a left-cancellative monoid. -/
theorem left_mul_dvd_iff {M : Type*} [LeftCancelMonoid M] (a x y : M) :
    a * x ∣ a * y ↔ x ∣ y := by
  constructor
  · rintro ⟨z, hz⟩
    exact ⟨z, mul_left_cancel (hz.trans (mul_assoc a x z))⟩
  · exact mul_dvd_mul_left a

/-- A nonempty inverse image of a principal right cone under left
multiplication is principal. Its initial element is the right quotient
of the conditional join. -/
theorem exists_cone_residual {M : Type*} [LeftCancelMonoid M]
    (hM : ∀ x y a : M, x ∣ a → y ∣ a → ∃ j : M, ∀ b : M, x ∣ b ∧ y ∣ b ↔ j ∣ b)
    (x a c : M) (hc : x ∣ a * c) :
    ∃ r : M, ∀ t : M, x ∣ a * t ↔ r ∣ t := by
  obtain ⟨s, hs⟩ := hM x a (a * c) hc (dvd_mul_right a c)
  obtain ⟨r, rfl⟩ := ((hs s).2 (dvd_refl s)).2
  refine ⟨r, fun t => ?_⟩
  calc
    x ∣ a * t ↔ x ∣ a * t ∧ a ∣ a * t :=
      ⟨fun h => ⟨h, dvd_mul_right _ _⟩, And.left⟩
    _ ↔ a * r ∣ a * t := hs _
    _ ↔ r ∣ t := left_mul_dvd_iff a r t

end DiagramOrder

namespace MergerAlgebra
universe u v

/-- A right action in which each orbit map is injective. This is the
property of the prescribed quotient of copies used in the merger normal
forms. It does not posit the existence of any monoid extension. -/
class RightFreeAction (M : Type u) (D : Type v) [Monoid M] where
  act : D → M → D
  act_one : ∀ d, act d 1 = d
  act_mul : ∀ d x y, act (act d x) y = act d (x * y)
  factor_injective : ∀ d, Function.Injective (act d)

namespace RightFreeAction
variable {M : Type u} {D : Type v} [Monoid M] [RightFreeAction M D]

/-- The right divisibility relation on a right monoid-set. -/
def Reaches (M : Type u) {D : Type v} [Monoid M] [RightFreeAction M D]
    (z w : D) : Prop := ∃ m : M, act z m = w

@[refl] theorem reaches_refl (z : D) : Reaches M z z := ⟨1, act_one z⟩

theorem reaches_trans {z w v : D} (hzw : Reaches M z w) (hwv : Reaches M w v) :
    Reaches M z v := by
  obtain ⟨m, hm⟩ := hzw
  obtain ⟨n, hn⟩ := hwv
  exact ⟨m * n, by rw [← act_mul, hm, hn]⟩

/-- Conditional joins expressed as exact intersections of right cones. -/
def HasConeJoins (M : Type u) (D : Type v) [Monoid M] [RightFreeAction M D] : Prop :=
  ∀ z w a : D, Reaches M z a → Reaches M w a →
    ∃ j : D, ∀ b : D, Reaches M z b ∧ Reaches M w b ↔ Reaches M j b

end RightFreeAction

/-- The literal normal forms `x z₁ ... zⱼ` of equation
`eq:mono-merger-forms`; words with no new letter are the old monoid. -/
inductive Word (M : Type u) (D : Type v) where
  | old : M → Word M D
  | snoc : Word M D → D → Word M D

namespace Word
variable {M : Type u} {D : Type v} [Monoid M] [RightFreeAction M D]

/-- Multiplication on the right by an old element acts only on the last
new letter (or on the old coefficient, if no new letter is present). -/
def act : Word M D → M → Word M D
  | old x, y => old (x * y)
  | snoc w z, y => snoc w (RightFreeAction.act z y)

@[simp] theorem act_one (w : Word M D) : act w 1 = w := by
  cases w <;> simp [act, RightFreeAction.act_one]

@[simp] theorem act_mul (w : Word M D) (x y : M) :
    act (act w x) y = act w (x * y) := by
  cases w <;> simp [act, mul_assoc, RightFreeAction.act_mul]

/-- Concatenation, absorbing the leading old coefficient of the second
normal form into the last letter of the first. -/
def product (x : Word M D) : Word M D → Word M D
  | old y => act x y
  | snoc y z => snoc (product x y) z

@[simp] theorem product_act (x y : Word M D) (m : M) :
    product x (act y m) = act (product x y) m := by
  cases y with
  | old n => exact (act_mul x n m).symm
  | snoc w d => rfl

theorem product_assoc (x y z : Word M D) :
    product (product x y) z = product x (product y z) := by
  induction z with
  | old m => exact (product_act x y m).symm
  | snoc z d ih => exact congrArg (fun w => snoc w d) ih

theorem old_one_product (w : Word M D) : product (old 1) w = w := by
  induction w with
  | old x => simp [product, act]
  | snoc w z ih => simp [product, ih]

instance : Monoid (Word M D) where
  mul := product
  one := old 1
  mul_assoc := product_assoc
  one_mul := old_one_product
  mul_one := act_one

@[simp] theorem mul_old (w : Word M D) (x : M) : w * old x = act w x := rfl
@[simp] theorem mul_snoc (w y : Word M D) (z : D) :
    w * snoc y z = snoc (w * y) z := rfl
@[simp] theorem old_mul_old (x y : M) :
    (old x : Word M D) * old y = old (x * y) := rfl
@[simp] theorem snoc_mul_old (w : Word M D) (z : D) (y : M) :
    snoc w z * old y = snoc w (RightFreeAction.act z y) := rfl

/-- Number of new letters, used to distinguish old from genuinely new words. -/
def length : Word M D → ℕ
  | old _ => 0
  | snoc w _ => length w + 1

omit [Monoid M] [RightFreeAction M D] in
@[simp] theorem length_old (x : M) : length (old x : Word M D) = 0 := rfl
omit [Monoid M] [RightFreeAction M D] in
@[simp] theorem length_snoc (w : Word M D) (z : D) :
    length (snoc w z) = length w + 1 := rfl
@[simp] theorem length_act (w : Word M D) (x : M) : length (act w x) = length w := by
  cases w <;> rfl

@[simp] theorem length_mul (x y : Word M D) : length (x * y) = length x + length y := by
  induction y with
  | old m => simp
  | snoc y d ih => simp [ih, Nat.add_assoc]

omit [Monoid M] [RightFreeAction M D] in
theorem length_zero_iff (w : Word M D) : length w = 0 ↔ ∃ m, w = old m := by
  cases w <;> simp

section Cancellation
variable [IsLeftCancelMul M]

theorem act_injective (w : Word M D) : Function.Injective (act w) := by
  cases w with
  | old x =>
      intro y z he
      exact mul_left_cancel (Word.old.inj he)
  | snoc w d =>
      intro y z he
      exact RightFreeAction.factor_injective d (Word.snoc.inj he).2

/-- Left cancellation is proved from the displayed normal forms. Right
cancellation is neither used nor asserted. -/
theorem product_left_cancel (x : Word M D) {y z : Word M D} (h : x * y = x * z) :
    y = z := by
  induction y generalizing z with
  | old m =>
      cases z with
      | old n => exact congrArg old (act_injective x h)
      | snoc z d =>
          have hl := congrArg length h
          simp only [length_mul, length_old, length_snoc] at hl
          omega
  | snoc y d ih =>
      cases z with
      | old n =>
          have hl := congrArg length h
          simp only [length_mul, length_old, length_snoc] at hl
          omega
      | snoc z e =>
          obtain ⟨h₁, h₂⟩ := Word.snoc.inj h
          exact congrArg₂ snoc (ih h₁) h₂

instance : LeftCancelMonoid (Word M D) where
  mul_left_cancel x _ _ := product_left_cancel x

end Cancellation

/-- Letter count prevents a new word from acquiring an old right multiple.
It also identifies the old right factor, rather than just reflecting order. -/
theorem factors_of_old {x y : Word M D} {a : M} (h : x * y = old a) :
    ∃ b c : M, x = old b ∧ y = old c ∧ b * c = a := by
  have hl := congrArg length h
  simp only [length_mul, length_old, Nat.add_eq_zero_iff] at hl
  obtain ⟨b, rfl⟩ := (length_zero_iff x).1 hl.1
  obtain ⟨c, rfl⟩ := (length_zero_iff y).1 hl.2
  exact ⟨b, c, rfl, rfl, Word.old.inj h⟩

instance [DiagramOrder.IsConical M] : DiagramOrder.IsConical (Word M D) where
  eq_one_of_mul_eq_one h := by
    obtain ⟨b, c, rfl, rfl, hbc⟩ := factors_of_old h
    obtain ⟨rfl, rfl⟩ := DiagramOrder.IsConical.eq_one_of_mul_eq_one hbc
    exact ⟨rfl, rfl⟩

/-- The old monoid embeds by words with no new letters. -/
def oldHom : M →* Word M D where
  toFun := old
  map_one' := rfl
  map_mul' _ _ := rfl

theorem oldHom_injective : Function.Injective (oldHom : M →* Word M D) := fun _ _ h => Word.old.inj h

/-- The leading old coefficient of the normal form. -/
def leading : Word M D → M
  | old x => x
  | snoc w _ => leading w

omit [Monoid M] [RightFreeAction M D] in
@[simp] theorem leading_old (x : M) : leading (old x : Word M D) = x := rfl
omit [Monoid M] [RightFreeAction M D] in
@[simp] theorem leading_snoc (w : Word M D) (z : D) :
    leading (snoc w z) = leading w := rfl

@[simp] theorem leading_old_mul (x : M) (w : Word M D) :
    leading (old x * w) = x * leading w := by
  induction w with
  | old m => rfl
  | snoc w d ih => exact ih

/-- Old divisors are read solely from the old leading coefficient. -/
theorem old_dvd_iff (x : M) (w : Word M D) :
    old x ∣ w ↔ x ∣ leading w := by
  constructor
  · rintro ⟨z, rfl⟩
    exact ⟨leading z, leading_old_mul x z⟩
  · intro h
    induction w with
    | old y =>
        obtain ⟨a, ha⟩ := h
        exact ⟨old a, congrArg old ha⟩
    | snoc w d ih =>
        obtain ⟨q, hq⟩ := ih h
        exact ⟨snoc q d, congrArg (fun w => snoc w d) hq⟩

/-- Comparisons and common-right-multiple calculations on old entries are
unchanged in the extension, including the absence of a common multiple. -/
theorem old_common_multiple_iff (x y : M) :
    (∃ w : Word M D, old x ∣ w ∧ old y ∣ w) ↔ ∃ a : M, x ∣ a ∧ y ∣ a := by
  constructor
  · rintro ⟨w, hx, hy⟩
    exact ⟨leading w, (old_dvd_iff x w).1 hx, (old_dvd_iff y w).1 hy⟩
  · rintro ⟨a, hx, hy⟩
    exact ⟨old a, (old_dvd_iff x (old a)).2 hx, (old_dvd_iff y (old a)).2 hy⟩

/-- The entire right-cone equation for any old conditional join is preserved,
not just comparisons among old elements. -/
theorem preserve_old_cone_intersection (x y j : M)
    (hj : ∀ a : M, x ∣ a ∧ y ∣ a ↔ j ∣ a) (w : Word M D) :
    old x ∣ w ∧ old y ∣ w ↔ old j ∣ w := by
  simpa only [old_dvd_iff] using hj (leading w)

/-- Dividing a normal form with one more letter means either dividing its
prefix, or having precisely that prefix and advancing the last new letter. -/
theorem dvd_snoc_iff (x w : Word M D) (d : D) :
    x ∣ snoc w d ↔ x ∣ w ∨ ∃ z, x = snoc w z ∧ RightFreeAction.Reaches M z d := by
  constructor
  · rintro ⟨q, hq⟩
    cases q with
    | old m =>
        cases x with
        | old a => cases hq
        | snoc v z =>
            obtain ⟨hw, hd⟩ := Word.snoc.inj hq
            exact Or.inr ⟨z, congrArg (fun v => snoc v z) hw.symm, m, hd.symm⟩
    | snoc q z => exact Or.inl ⟨q, (Word.snoc.inj hq).1⟩
  · rintro (⟨q, hq⟩ | ⟨z, rfl, m, hm⟩)
    · exact ⟨snoc q d, congrArg (fun v => snoc v d) hq⟩
    · exact ⟨old m, congrArg (snoc w) hm.symm⟩

theorem length_le_of_dvd {x y : Word M D} (h : x ∣ y) : length x ≤ length y := by
  obtain ⟨q, rfl⟩ := h
  simp only [length_mul]
  omega

theorem snoc_not_dvd_prefix (w : Word M D) (z : D) : ¬ snoc w z ∣ w := by
  intro h
  have hl := length_le_of_dvd h
  simp only [length_snoc] at hl
  omega

theorem snoc_not_dvd_old (w : Word M D) (z : D) (m : M) : ¬ snoc w z ∣ old m := by
  intro h
  have hl := length_le_of_dvd h
  simp only [length_snoc, length_old] at hl
  omega

theorem snoc_dvd_snoc_iff (w v : Word M D) (z d : D) :
    snoc w z ∣ snoc v d ↔
      snoc w z ∣ v ∨ w = v ∧ RightFreeAction.Reaches M z d := by
  rw [dvd_snoc_iff]
  constructor
  · rintro (h | ⟨e, he, hea⟩)
    · exact Or.inl h
    · obtain ⟨rfl, rfl⟩ := Word.snoc.inj he
      exact Or.inr ⟨rfl, hea⟩
  · rintro (h | ⟨rfl, hz⟩)
    · exact Or.inl h
    · exact Or.inr ⟨z, rfl, hz⟩

/-- With a fixed prefix, the intersection of two word cones is exactly
controlled by the intersection of the final-letter cones in the right set. -/
theorem snoc_cone_intersection (w : Word M D) (z t j : D)
    (hj : ∀ d : D, RightFreeAction.Reaches M z d ∧ RightFreeAction.Reaches M t d ↔
      RightFreeAction.Reaches M j d) (a : Word M D) :
    snoc w z ∣ a ∧ snoc w t ∣ a ↔ snoc w j ∣ a := by
  induction a with
  | old m => simp only [snoc_not_dvd_old, false_and]
  | snoc a d ih =>
      simp only [snoc_dvd_snoc_iff]
      by_cases hw : w = a
      · subst a
        simp only [snoc_not_dvd_prefix, true_and, false_or, hj]
      · simpa only [hw, false_and, or_false] using ih

/-- Conditional joins for all normal forms, not only for translated copies.
The induction is on the normal form of a common upper bound. -/
theorem exists_cone_join
    (hM : ∀ x y a : M, x ∣ a → y ∣ a → ∃ j : M, ∀ b : M, x ∣ b ∧ y ∣ b ↔ j ∣ b)
    (hD : RightFreeAction.HasConeJoins M D) (x y a : Word M D)
    (hx : x ∣ a) (hy : y ∣ a) :
    ∃ j : Word M D, ∀ b : Word M D, x ∣ b ∧ y ∣ b ↔ j ∣ b := by
  induction a generalizing x y with
  | old m =>
      obtain ⟨qx, hqx⟩ := hx
      obtain ⟨qy, hqy⟩ := hy
      obtain ⟨r, s, rfl, rfl, hxm⟩ := factors_of_old hqx.symm
      obtain ⟨t, u, rfl, rfl, hym⟩ := factors_of_old hqy.symm
      obtain ⟨j, hj⟩ := hM r t m ⟨s, hxm.symm⟩ ⟨u, hym.symm⟩
      exact ⟨old j, preserve_old_cone_intersection r t j hj⟩
  | snoc a d ih =>
      rcases (dvd_snoc_iff x a d).1 hx with hx | ⟨z, rfl, hz⟩
      · rcases (dvd_snoc_iff y a d).1 hy with hy | ⟨t, rfl, ht⟩
        · exact ih x y hx hy
        · have hxy : x ∣ snoc a t := (dvd_snoc_iff x a t).2 (Or.inl hx)
          exact ⟨snoc a t, fun _ => ⟨And.right, fun h => ⟨hxy.trans h, h⟩⟩⟩
      · rcases (dvd_snoc_iff y a d).1 hy with hy | ⟨t, rfl, ht⟩
        · have hyx : y ∣ snoc a z := (dvd_snoc_iff y a z).2 (Or.inl hy)
          exact ⟨snoc a z, fun _ => ⟨And.left, fun h => ⟨h, hyx.trans h⟩⟩⟩
        · obtain ⟨j, hj⟩ := hD z t d hz ht
          exact ⟨snoc a j, snoc_cone_intersection a z t j hj⟩

end Word
end MergerAlgebra

namespace DiagramForcing.OverlapPrescription
universe u
variable {M : Type u} [LeftCancelMonoid M] {n : ℕ}

/-- Right multiplication respects the pair identifications themselves. No
extra transitive closure is taken. -/
theorem rel_mul (P : OverlapPrescription M n) {z w : Fin n × M}
    (h : P.Rel z w) (m : M) :
    P.Rel (z.1, z.2 * m) (w.1, w.2 * m) := by
  classical
  obtain ⟨i, x⟩ := z
  obtain ⟨j, y⟩ := w
  by_cases hij : i = j
  · subst j
    simp only [rel_same_copy] at h ⊢
    exact congrArg (· * m) h
  · simp only [Rel, hij, ↓reduceIte] at h ⊢
    cases ho : P.start i j with
    | none => simp only [ho] at h
    | some ab =>
        obtain ⟨a, b⟩ := ab
        rw [ho] at h
        obtain ⟨t, rfl, rfl⟩ := h
        exact ⟨t * m, mul_assoc _ _ _, mul_assoc _ _ _⟩

/-- The quotient of the finitely many prescribed copies. Validity, rather
than an equivalence closure, provides its setoid. -/
def copySetoid (P : OverlapPrescription M n) (hP : P.Valid) : Setoid (Fin n × M) :=
  ⟨P.Rel, hP⟩

def Copies (P : OverlapPrescription M n) (hP : P.Valid) := Quotient (P.copySetoid hP)

def copy (P : OverlapPrescription M n) (hP : P.Valid) (i : Fin n) (x : M) : P.Copies hP :=
  Quotient.mk (P.copySetoid hP) (i, x)

@[simp] theorem copy_eq_iff (P : OverlapPrescription M n) (hP : P.Valid)
    (i j : Fin n) (x y : M) :
    P.copy hP i x = P.copy hP j y ↔ P.Rel (i, x) (j, y) := Quotient.eq

theorem copy_injective (P : OverlapPrescription M n) (hP : P.Valid) (i : Fin n) :
    Function.Injective (P.copy hP i) := by
  intro x y he
  exact (P.rel_same_copy i x y).1 ((P.copy_eq_iff hP i i x y).1 he)

def copiesAct (P : OverlapPrescription M n) (hP : P.Valid) (z : P.Copies hP) (m : M) :
    P.Copies hP :=
  Quotient.map (fun z : Fin n × M => (z.1, z.2 * m)) (fun _ _ h => P.rel_mul h m) z

@[simp] theorem copiesAct_copy (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x m : M) :
    P.copiesAct hP (P.copy hP i x) m = P.copy hP i (x * m) := rfl

/-- The injective orbit maps in the prescribed quotient follow from left
cancellation within a single original copy. -/
instance copiesRightFreeAction (P : OverlapPrescription M n) (hP : P.Valid) :
    MergerAlgebra.RightFreeAction M (P.Copies hP) where
  act := P.copiesAct hP
  act_one := by
    intro z
    induction z using Quotient.inductionOn with
    | h z =>
        change P.copy hP z.1 (z.2 * 1) = P.copy hP z.1 z.2
        rw [mul_one]
  act_mul := by
    intro z x y
    induction z using Quotient.inductionOn with
    | h z =>
        change P.copy hP z.1 ((z.2 * x) * y) = P.copy hP z.1 (z.2 * (x * y))
        rw [mul_assoc]
  factor_injective := by
    intro z
    induction z using Quotient.inductionOn with
    | h z =>
        intro x y he
        exact mul_left_cancel (P.copy_injective hP z.1 he)

open MergerAlgebra.RightFreeAction

@[simp] theorem reaches_copy_iff (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x : M) (z : P.Copies hP) :
    Reaches M (P.copy hP i x) z ↔ ∃ m, P.copy hP i (x * m) = z := Iff.rfl

@[simp] theorem reaches_same_copy_iff (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x y : M) :
    Reaches M (P.copy hP i x) (P.copy hP i y) ↔ x ∣ y := by
  rw [reaches_copy_iff]
  constructor
  · rintro ⟨m, hm⟩
    exact ⟨m, (P.copy_injective hP i hm).symm⟩
  · rintro ⟨m, rfl⟩
    exact ⟨m, rfl⟩

theorem same_copy_cone_intersection (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x y j : M) (hj : ∀ a : M, x ∣ a ∧ y ∣ a ↔ j ∣ a)
    (z : P.Copies hP) :
    Reaches M (P.copy hP i x) z ∧ Reaches M (P.copy hP i y) z ↔
      Reaches M (P.copy hP i j) z := by
  constructor
  · rintro ⟨⟨a, ha⟩, ⟨b, hb⟩⟩
    change P.copy hP i (x * a) = z at ha
    change P.copy hP i (y * b) = z at hb
    have hxy : x * a = y * b := P.copy_injective hP i (ha.trans hb.symm)
    obtain ⟨c, hc⟩ := (hj (x * a)).1 ⟨dvd_mul_right _ _, b, hxy⟩
    exact ⟨c, by change P.copy hP i (j * c) = z; rw [← hc, ha]⟩
  · intro h
    have hu := (hj j).2 (dvd_refl j)
    exact ⟨reaches_trans ((P.reaches_same_copy_iff hP i x j).2 hu.1) h,
      reaches_trans ((P.reaches_same_copy_iff hP i y j).2 hu.2) h⟩

/-- Intersections from different copies must pass through exactly the
specified overlap: validity ensures that the quotient adds no other equality. -/
theorem overlap_cone_intersection (P : OverlapPrescription M n) (hP : P.Valid)
    {i j : Fin n} (hij : i ≠ j) {a b : M} (hab : P.start i j = some (a, b))
    (x y : M) (z : P.Copies hP) :
    Reaches M (P.copy hP i x) z ∧ Reaches M (P.copy hP j y) z ↔
      ∃ t : M, x ∣ a * t ∧ y ∣ b * t ∧ z = P.copy hP i (a * t) := by
  constructor
  · rintro ⟨⟨m, hm⟩, ⟨k, hk⟩⟩
    change P.copy hP i (x * m) = z at hm
    change P.copy hP j (y * k) = z at hk
    have he := (P.copy_eq_iff hP i j (x * m) (y * k)).1 (hm.trans hk.symm)
    simp only [Rel, hij, ↓reduceIte, hab] at he
    obtain ⟨t, ht, ht'⟩ := he
    exact ⟨t, ⟨m, ht.symm⟩, ⟨k, ht'.symm⟩, hm.symm.trans (congrArg _ ht)⟩
  · rintro ⟨t, ⟨m, hm⟩, ⟨k, hk⟩, rfl⟩
    have he : P.copy hP i (a * t) = P.copy hP j (b * t) := by
      apply (P.copy_eq_iff hP i j _ _).2
      simp only [Rel, hij, ↓reduceIte, hab]
      exact ⟨t, rfl, rfl⟩
    exact ⟨⟨m, congrArg (P.copy hP i) hm.symm⟩,
      ⟨k, (congrArg (P.copy hP j) hk.symm).trans he.symm⟩⟩

/-- Conditional joins in the actual prescribed quotient of copies.
The sequential joins and their right quotients are constructed using only
old conditional joins; failing any one of them means there was no common
upper bound in the first place. -/
theorem copies_hasConeJoins (P : OverlapPrescription M n) (hP : P.Valid)
    (hM : ∀ x y a : M, x ∣ a → y ∣ a → ∃ j : M, ∀ b : M, x ∣ b ∧ y ∣ b ↔ j ∣ b) :
    HasConeJoins M (P.Copies hP) := by
  classical
  intro z w c hz hw
  obtain ⟨⟨i, x⟩, rfl⟩ := Quotient.exists_rep z
  obtain ⟨⟨j, y⟩, rfl⟩ := Quotient.exists_rep w
  change Reaches M (P.copy hP i x) c at hz
  change Reaches M (P.copy hP j y) c at hw
  by_cases hij : i = j
  · subst j
    obtain ⟨u, hu⟩ := (P.reaches_copy_iff hP i x c).1 hz
    obtain ⟨v, hv⟩ := (P.reaches_copy_iff hP i y c).1 hw
    have hxy : x * u = y * v := P.copy_injective hP i (hu.trans hv.symm)
    obtain ⟨k, hk⟩ := hM x y (x * u) (dvd_mul_right _ _) ⟨v, hxy⟩
    exact ⟨P.copy hP i k, P.same_copy_cone_intersection hP i x y k hk⟩
  · obtain ⟨u, hu⟩ := (P.reaches_copy_iff hP i x c).1 hz
    obtain ⟨v, hv⟩ := (P.reaches_copy_iff hP j y c).1 hw
    have he := (P.copy_eq_iff hP i j (x * u) (y * v)).1 (hu.trans hv.symm)
    simp only [Rel, hij, ↓reduceIte] at he
    cases ho : P.start i j with
    | none => simp only [ho] at he
    | some ab =>
        obtain ⟨a, b⟩ := ab
        rw [ho] at he
        obtain ⟨t₀, hxt, hyt⟩ := he
        obtain ⟨r, hr⟩ := DiagramOrder.exists_cone_residual hM x a t₀ ⟨u, hxt.symm⟩
        obtain ⟨s, hs⟩ := DiagramOrder.exists_cone_residual hM y b t₀ ⟨v, hyt.symm⟩
        obtain ⟨k, hk⟩ := hM r s t₀ ((hr t₀).1 ⟨u, hxt.symm⟩)
          ((hs t₀).1 ⟨v, hyt.symm⟩)
        have hcoeff : ∀ t, x ∣ a * t ∧ y ∣ b * t ↔ k ∣ t := fun t => by
          rw [hr t, hs t, hk t]
        refine ⟨P.copy hP i (a * k), fun z => ?_⟩
        change Reaches M (P.copy hP i x) z ∧ Reaches M (P.copy hP j y) z ↔
          Reaches M (P.copy hP i (a * k)) z
        rw [P.overlap_cone_intersection hP hij ho]
        constructor
        · rintro ⟨t, hxt, hyt, rfl⟩
          obtain ⟨m, rfl⟩ := (hcoeff t).1 ⟨hxt, hyt⟩
          exact ⟨m, congrArg (P.copy hP i) (mul_assoc a k m)⟩
        · rintro ⟨m, hm⟩
          change P.copy hP i ((a * k) * m) = z at hm
          have ht := (hcoeff (k * m)).2 (dvd_mul_right _ _)
          exact ⟨k * m, ht.1, ht.2, hm.symm.trans (congrArg _ (mul_assoc a k m))⟩

/-- The extension is constructed, not postulated: its elements are the
normal forms over the actual prescribed quotient. -/
abbrev Merger (P : OverlapPrescription M n) (hP : P.Valid) :=
  MergerAlgebra.Word M (P.Copies hP)

/-- The new roots which merge the original copies. -/
def root (P : OverlapPrescription M n) (hP : P.Valid) (i : Fin n) : P.Merger hP :=
  .snoc (.old 1) (P.copy hP i 1)

@[simp] theorem root_mul_old (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x : M) :
    P.root hP i * .old x = .snoc (.old 1) (P.copy hP i x) := by
  change MergerAlgebra.Word.snoc (.old 1) (P.copy hP i (1 * x)) = _
  rw [one_mul]

/-- Exact realization of the given valid overlap prescription by left
translations. The right-hand side is the original pair relation, not its
transitive closure; no extra overlap is introduced. -/
theorem root_mul_eq_iff (P : OverlapPrescription M n) (hP : P.Valid)
    (z w : Fin n × M) :
    P.root hP z.1 * .old z.2 = P.root hP w.1 * .old w.2 ↔ P.Rel z w := by
  simp only [root_mul_old, MergerAlgebra.Word.snoc.injEq, true_and, copy_eq_iff]

/-- New roots are genuinely new elements, even if an overlap identifies
two of them with one another. -/
theorem root_not_old (P : OverlapPrescription M n) (hP : P.Valid)
    (i : Fin n) (x : M) : P.root hP i ≠ .old x := by
  simp [root]

/-- All conditional joins in the constructed finite-copy merger. -/
theorem merger_hasConditionalJoins [DiagramOrder.IsConical M]
    (P : OverlapPrescription M n) (hP : P.Valid)
    (hM : DiagramOrder.HasConditionalJoins (DiagramOrder.LeftDivOrder M)) :
    DiagramOrder.HasConditionalJoins (DiagramOrder.LeftDivOrder (P.Merger hP)) := by
  intro x y a hx hy
  have hc := DiagramOrder.exists_cone_join hM
  obtain ⟨j, hj⟩ := MergerAlgebra.Word.exists_cone_join hc
    (P.copies_hasConeJoins hP hc) x y a
    ((DiagramOrder.LeftDivOrder.le_iff_dvd (M := P.Merger hP) x a).1 hx)
    ((DiagramOrder.LeftDivOrder.le_iff_dvd (M := P.Merger hP) y a).1 hy)
  refine ⟨j, (DiagramOrder.pair_lub_iff (α := DiagramOrder.LeftDivOrder (P.Merger hP)) x y j).2 (fun z => ?_)⟩
  change (∃ q, x * q = z) ∧ (∃ q, y * q = z) ↔ ∃ q, j * q = z
  constructor
  · rintro ⟨⟨q, hq⟩, ⟨r, hr⟩⟩
    obtain ⟨s, hs⟩ := (hj z).1 ⟨⟨q, hq.symm⟩, ⟨r, hr.symm⟩⟩
    exact ⟨s, hs.symm⟩
  · rintro ⟨s, hs⟩
    obtain ⟨⟨q, hq⟩, ⟨r, hr⟩⟩ := (hj z).2 ⟨s, hs.symm⟩
    exact ⟨⟨q, hq.symm⟩, ⟨r, hr.symm⟩⟩

end DiagramForcing.OverlapPrescription
end PartitionConsistency

namespace PartitionConsistency.MergerAlgebra.Word
universe u
variable {M D : Type u}

/-- Finite coding of a normal form; only the existing coefficient and
finitely many quotient letters are needed. -/
def letters : Word M D → List D
  | old _ => []
  | snoc w z => z :: letters w

/-- The normal-form encoding is injective, so no presentation-sized
collection beyond finite strings is required. -/
theorem code_injective : Function.Injective (fun w : Word M D => (leading w, letters w)) := by
  intro w
  induction w with
  | old m =>
      intro v hv
      cases v with
      | old n => exact congrArg old (Prod.mk.inj hv).1
      | snoc v z =>
          have he := (Prod.mk.inj hv).2
          simp [letters] at he
  | snoc w z ih =>
      intro v hv
      cases v with
      | old n =>
          have he := (Prod.mk.inj hv).2
          simp [letters] at he
      | snoc v d =>
          have hzv := List.cons.inj (Prod.mk.inj hv).2
          exact congrArg₂ snoc (ih (Prod.ext (Prod.mk.inj hv).1 hzv.2)) hzv.1

/-- The scheduling bound for the normal forms. -/
theorem cardinal_le {μ : Cardinal.{u}} (hω : Cardinal.aleph0 ≤ μ)
    (hM : Cardinal.mk M ≤ μ) (hD : Cardinal.mk D ≤ μ) :
    Cardinal.mk (Word M D) ≤ μ := by
  calc
    Cardinal.mk (Word M D) ≤ Cardinal.mk (M × List D) :=
      Cardinal.mk_le_of_injective code_injective
    _ = Cardinal.mk M * Cardinal.mk (List D) := by simp only [Cardinal.mk_prod, Cardinal.lift_id]
    _ ≤ max (max (Cardinal.mk M) (Cardinal.mk (List D))) Cardinal.aleph0 :=
      Cardinal.mul_le_max _ _
    _ ≤ μ := max_le (max_le hM ((Cardinal.mk_list_le_max D).trans (max_le hω hD))) hω

end PartitionConsistency.MergerAlgebra.Word

namespace PartitionConsistency.DiagramForcing.OverlapPrescription
universe u
variable {M : Type u} [LeftCancelMonoid M] {n : ℕ}

/-- A finite-copy merger preserves the source's infinite cardinal bound. -/
theorem merger_cardinal_le (P : OverlapPrescription M n) (hP : P.Valid)
    {μ : Cardinal.{u}} (hω : Cardinal.aleph0 ≤ μ) (hM : Cardinal.mk M ≤ μ) :
    Cardinal.mk (P.Merger hP) ≤ μ := by
  apply MergerAlgebra.Word.cardinal_le hω hM
  calc
    Cardinal.mk (P.Copies hP) ≤ Cardinal.mk (Fin n × M) := Cardinal.mk_quotient_le
    _ = n * Cardinal.mk M := by simp [Cardinal.mk_prod]
    _ ≤ max (max (n : Cardinal) (Cardinal.mk M)) Cardinal.aleph0 := Cardinal.mul_le_max _ _
    _ ≤ μ := max_le (max_le ((Cardinal.natCast_lt_aleph0 (n := n)).le.trans hω) hM) hω

end PartitionConsistency.DiagramForcing.OverlapPrescription

end OAI