File size: 18,368 Bytes
0dd3af4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib

namespace OAI

namespace SourceActionMonoid
universe u v
variable (V : Type u) (Z : Type v) [Group V] [MulAction V Z]

inductive Generator
  | grp : V → Generator
  | pre : Z → Generator
  | delete : Z → Generator
  | zero : Generator

abbrev Word := FreeMonoid (Generator V Z)

inductive Relation : Word V Z → Word V Z → Prop
  | group_mul (g h : V) : Relation
      (.of (.grp g) * .of (.grp h)) (.of (.grp (g * h)))
  | group_one : Relation (.of (.grp 1)) 1
  | delete_prefix (z : Z) : Relation (.of (.delete z) * .of (.pre z)) 1
  | delete_prefix_ne (y z : Z) (h : y ≠ z) : Relation
      (.of (.delete y) * .of (.pre z)) (.of .zero)
  | group_prefix (g : V) (z : Z) : Relation
      (.of (.grp g) * .of (.pre z)) (.of (.pre (g • z)))
  | delete_group (z : Z) (g : V) : Relation
      (.of (.delete z) * .of (.grp g)) (.of (.delete (g⁻¹ • z)))
  | zero_left (a : Generator V Z) : Relation (.of .zero * .of a) (.of .zero)
  | zero_right (a : Generator V Z) : Relation (.of a * .of .zero) (.of .zero)

abbrev M := PresentedMonoid (Relation V Z)

variable {V Z}

def of (a : Generator V Z) : M V Z := PresentedMonoid.of (Relation V Z) a

def omega : M V Z := of .zero

def prepend (z : Z) : M V Z := of (.pre z)
def delete (z : Z) : M V Z := of (.delete z)

def groupMap : V →* M V Z where
  toFun g := of (.grp g)
  map_one' := PresentedMonoid.mk_eq_mk_of_rel (Relation.group_one (V := V) (Z := Z))
  map_mul' g h := (PresentedMonoid.mk_eq_mk_of_rel (Relation.group_mul (V := V) (Z := Z) g h)).symm

@[simp] lemma of_grp (g : V) : of (.grp g) = groupMap (Z := Z) g := rfl

@[simp] lemma delete_prefix (z : Z) : delete (V := V) z * prepend z = 1 :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.delete_prefix (V := V) (Z := Z) z)

lemma delete_prefix_ne (y z : Z) (h : y ≠ z) :
    delete (V := V) y * prepend z = omega :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.delete_prefix_ne (V := V) (Z := Z) y z h)

lemma group_prefix (g : V) (z : Z) : groupMap g * prepend z = prepend (V := V) (g • z) :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.group_prefix (V := V) (Z := Z) g z)

lemma delete_group (z : Z) (g : V) : delete (V := V) z * groupMap g = delete (g⁻¹ • z) :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.delete_group (V := V) (Z := Z) z g)

@[simp] lemma omega_of (a : Generator V Z) : omega * of a = omega :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.zero_left (V := V) (Z := Z) a)

@[simp] lemma of_omega (a : Generator V Z) : of a * omega = omega :=
  PresentedMonoid.mk_eq_mk_of_rel (Relation.zero_right (V := V) (Z := Z) a)

@[simp] lemma omega_mul (a : M V Z) : omega * a = omega := by
  induction a using PresentedMonoid.inductionOn with | h w =>
    induction w using FreeMonoid.inductionOn' with
    | one => simp
    | of_mul a w ih =>
      rw [map_mul, ← mul_assoc]
      exact (congrArg (· * PresentedMonoid.mk (Relation V Z) w) (omega_of a)).trans ih

@[simp] lemma mul_omega (a : M V Z) : a * omega = omega := by
  induction a using PresentedMonoid.inductionOn with | h w =>
    induction w using FreeMonoid.inductionOn' with
    | one => simp
    | of_mul a w ih =>
      rw [map_mul, mul_assoc, ih]
      exact of_omega a

def prefixes (l : List Z) : M V Z := (l.map prepend).prod

def deletions (l : List Z) : M V Z := (l.reverse.map delete).prod

@[simp] lemma prefixes_nil : prefixes (V := V) ([] : List Z) = 1 := rfl
@[simp] lemma deletions_nil : deletions (V := V) ([] : List Z) = 1 := rfl
@[simp] lemma prefixes_cons (z : Z) (l : List Z) :
    prefixes (V := V) (z :: l) = prepend z * prefixes l := rfl

lemma deletions_append (l k : List Z) :
    deletions (V := V) (l ++ k) = deletions k * deletions l := by
  simp [deletions]

@[simp] lemma deletions_cons (z : Z) (l : List Z) :
    deletions (V := V) (z :: l) = deletions l * delete z := by
  simp [deletions]

def normal (l : List Z) (g : V) (r : List Z) : M V Z :=
  prefixes l * groupMap g * deletions r

lemma prepend_normal (z : Z) (l : List Z) (g : V) (r : List Z) :
    prepend z * normal l g r = normal (z :: l) g r := by
  simp only [normal, prefixes_cons, mul_assoc]

lemma group_normal_nil (h g : V) (r : List Z) :
    groupMap h * normal [] g r = normal [] (h * g) r := by
  simp [normal, map_mul, mul_assoc]

lemma group_normal_cons (h : V) (z : Z) (l : List Z) (g : V) (r : List Z) :
    groupMap h * normal (z :: l) g r = normal ((h • z) :: l) g r := by
  simp only [normal, prefixes_cons, ← mul_assoc, group_prefix]

lemma delete_normal_nil (z : Z) (g : V) (r : List Z) :
    delete z * normal [] g r = normal [] 1 (r ++ [g⁻¹ • z]) := by
  simp [normal, ← mul_assoc, delete_group, deletions]

lemma delete_normal_cons (z : Z) (l : List Z) (g : V) (r : List Z) :
    delete z * normal (z :: l) g r = normal l g r := by
  simp only [normal, prefixes_cons, ← mul_assoc, delete_prefix, one_mul]

lemma delete_normal_cons_ne (y z : Z) (h : y ≠ z) (l : List Z) (g : V) (r : List Z) :
    delete y * normal (z :: l) g r = omega := by
  simp only [normal, prefixes_cons, ← mul_assoc, delete_prefix_ne y z h, omega_mul]

theorem normal_exists (a : M V Z) :
    a = omega ∨ ∃ (l : List Z) (g : V) (r : List Z), a = normal l g r := by
  classical
  induction a using PresentedMonoid.inductionOn with | h w =>
    induction w using FreeMonoid.inductionOn' with
    | one =>
      right
      exact ⟨[], 1, [], by simp [normal]⟩
    | of_mul c w ih =>
      rw [map_mul]
      rcases ih with h | ⟨l, g, r, h⟩
      · left
        rw [h]
        exact of_omega c
      · rw [h]
        change of c * normal l g r = omega ∨ _
        cases c with
        | zero => exact Or.inl (omega_mul _)
        | pre z => exact Or.inr ⟨z :: l, g, r, prepend_normal z l g r⟩
        | grp q =>
          cases l with
          | nil => exact Or.inr ⟨[], q * g, r, group_normal_nil q g r⟩
          | cons z l => exact Or.inr ⟨q • z :: l, g, r, group_normal_cons q z l g r⟩
        | delete y =>
          cases l with
          | nil => exact Or.inr ⟨[], 1, r ++ [g⁻¹ • y], delete_normal_nil y g r⟩
          | cons z l =>
            by_cases hyz : y = z
            · subst y
              exact Or.inr ⟨l, g, r, delete_normal_cons z l g r⟩
            · exact Or.inl (delete_normal_cons_ne y z hyz l g r)

abbrev State := List Z × V

def headAction (g : V) : State (V := V) (Z := Z) → State (V := V) (Z := Z)
  | ([], v) => ([], g * v)
  | (z :: zs, v) => (g • z :: zs, v)

noncomputable section
local instance : DecidableEq Z := Classical.decEq _

def generatorAction : Generator V Z → Function.End (Option (State (V := V) (Z := Z)))
  | .grp g => Option.map (headAction g)
  | .pre z => Option.map (fun s => (z :: s.1, s.2))
  | .delete z => fun s => match s with
    | none => none
    | some ([], _) => none
    | some (y :: ys, v) => if z = y then some (ys, v) else none
  | .zero => fun _ => none

@[simp] lemma end_mul_apply {A : Type*} (f g : Function.End A) (a : A) :
    (f * g) a = f (g a) := rfl
@[simp] lemma end_one_apply {A : Type*} (a : A) : (1 : Function.End A) a = a := rfl

lemma generatorAction_relations (a b : Word V Z) (h : Relation V Z a b) :
    FreeMonoid.lift generatorAction a = FreeMonoid.lift generatorAction b := by
  cases h with
  | group_mul g h =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    funext s
    simp only [end_mul_apply]
    cases s with
    | none => rfl
    | some p =>
      rcases p with ⟨l, v⟩
      cases l <;> simp [generatorAction, headAction, mul_assoc, mul_smul]
  | group_one =>
    simp only [FreeMonoid.lift_eval_of, map_one]
    funext s
    simp only [end_one_apply]
    cases s with
    | none => rfl
    | some p =>
      rcases p with ⟨l, v⟩
      cases l <;> simp [generatorAction, headAction]
  | delete_prefix z =>
    simp only [map_mul, FreeMonoid.lift_eval_of, map_one]
    funext s
    simp only [end_mul_apply, end_one_apply]
    cases s <;> simp [generatorAction]
  | delete_prefix_ne y z h =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    funext s
    simp only [end_mul_apply]
    cases s <;> simp [generatorAction, h]
  | group_prefix g z =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    funext s
    simp only [end_mul_apply]
    cases s <;> simp [generatorAction, headAction]
  | delete_group z g =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    funext s
    simp only [end_mul_apply]
    cases s with
    | none => rfl
    | some p =>
      rcases p with ⟨l, v⟩
      cases l with
      | nil => rfl
      | cons y ys =>
        have hi : z = g • y ↔ g⁻¹ • z = y := by
          constructor
          · intro h
            rw [h, inv_smul_smul]
          · intro h
            rw [← h, smul_inv_smul]
        simp only [generatorAction, Option.map_some, headAction]
        simp only [hi]
  | zero_left a =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    rfl
  | zero_right a =>
    simp only [map_mul, FreeMonoid.lift_eval_of]
    funext s
    simp only [end_mul_apply]
    cases a <;> rfl

def representation : M V Z →* Function.End (Option (State (V := V) (Z := Z))) :=
  PresentedMonoid.lift generatorAction generatorAction_relations

@[simp] lemma representation_of (a : Generator V Z) :
    representation (of a) = generatorAction a := rfl

@[simp] lemma representation_omega (s : Option (State (V := V) (Z := Z))) :
    representation omega s = none := rfl

@[simp] lemma representation_group (g : V) (s : Option (State (V := V) (Z := Z))) :
    representation (groupMap (Z := Z) g) s = s.map (headAction g) := rfl

@[simp] lemma representation_prepend (z : Z) (s : Option (State (V := V) (Z := Z))) :
    representation (prepend (V := V) z) s = s.map (fun p => (z :: p.1, p.2)) := rfl

def strip : List Z → List Z → Option (List Z)
  | [], w => some w
  | _ :: _, [] => none
  | z :: zs, y :: ys => if z = y then strip zs ys else none

@[simp] lemma strip_append (r l : List Z) : strip r (r ++ l) = some l := by
  induction r with
  | nil => rfl
  | cons z r ih => simpa [strip] using ih

lemma strip_eq_some_iff (r k t : List Z) : strip r k = some t ↔ k = r ++ t := by
  induction r generalizing k with
  | nil => simp [strip]
  | cons z r ih =>
    cases k with
    | nil => simp [strip]
    | cons y k =>
      by_cases h : z = y
      · subst y
        simp [strip, ih]
      · simp [strip, h, Ne.symm h]

@[simp] lemma representation_prefixes (l : List Z)
    (s : Option (State (V := V) (Z := Z))) :
    representation (prefixes (V := V) l) s = s.map (fun p => (l ++ p.1, p.2)) := by
  induction l with
  | nil => simp
  | cons z l ih =>
    simp only [prefixes_cons, map_mul, end_mul_apply, ih, representation_prepend,
      Option.map_map]
    rfl

@[simp] lemma representation_delete_none (z : Z) :
    representation (delete (V := V) z) none = none := rfl

@[simp] lemma representation_delete_nil (z : Z) (v : V) :
    representation (delete (V := V) z) (some ([], v)) = none := rfl

@[simp] lemma representation_delete_cons (z y : Z) (k : List Z) (v : V) :
    representation (delete (V := V) z) (some (y :: k, v)) =
      if z = y then some (k, v) else none := rfl

@[simp] lemma representation_deletions_none (r : List Z) :
    representation (deletions (V := V) r) none = none := by
  induction r with
  | nil => rfl
  | cons z r ih =>
    simpa only [deletions_cons, map_mul, end_mul_apply, representation_delete_none] using ih

lemma representation_deletions (r : List Z) (k : List Z) (v : V) :
    representation (deletions (V := V) r) (some (k, v)) =
      (strip r k).map (fun t => (t, v)) := by
  induction r generalizing k with
  | nil => rfl
  | cons z r ih =>
    rw [deletions_cons, map_mul, end_mul_apply]
    cases k with
    | nil => simp [strip]
    | cons y k =>
      by_cases h : z = y
      · subst y
        simpa [strip] using ih k
      · simp [strip, h]

@[simp] lemma strip_self (r : List Z) : strip r r = some [] := by
  simpa only [List.append_nil] using strip_append r []

lemma representation_normal (l : List Z) (g : V) (r k : List Z) (v : V) :
    representation (normal l g r) (some (k, v)) =
      (strip r k).map (fun t =>
        let p := headAction g (t, v)
        (l ++ p.1, p.2)) := by
  simp only [normal, map_mul, end_mul_apply, representation_deletions,
    representation_group, representation_prefixes, Option.map_map]
  rfl

@[simp] lemma representation_normal_at (l : List Z) (g : V) (r : List Z) :
    representation (normal l g r) (some (r, 1)) = some (l, g) := by
  simp only [representation_normal, strip_self, Option.map_some, headAction,
    List.append_nil, mul_one]

lemma normal_ne_zero (l : List Z) (g : V) (r : List Z) : normal l g r ≠ omega := by
  intro h
  have e := congrArg (fun a : M V Z => representation a (some (r, 1))) h
  simp only [representation_normal_at, representation_omega, reduceCtorEq] at e

lemma normal_domain_prefix (l : List Z) (g : V) (r k : List Z) (v : V)
    (h : representation (normal l g r) (some (k, v)) ≠ none) : r <+: k := by
  cases hs : strip r k with
  | none => simp only [representation_normal, hs, Option.map_none, ne_eq, not_true_eq_false] at h
  | some t => exact ⟨t, ((strip_eq_some_iff _ _ _).mp hs).symm⟩

theorem normal_eq_parameters {l l' : List Z} {g g' : V} {r r' : List Z}
    (h : normal l g r = normal l' g' r') : l = l' ∧ g = g' ∧ r = r' := by
  have hr : r <+: r' := normal_domain_prefix l g r r' 1 (by
    rw [h, representation_normal_at]
    exact Option.some_ne_none _)
  have hr' : r' <+: r := normal_domain_prefix l' g' r' r 1 (by
    rw [← h, representation_normal_at]
    exact Option.some_ne_none _)
  have heq : r = r' := hr.eq_of_length (Nat.le_antisymm hr.length_le hr'.length_le)
  subst r'
  have e := congrArg (fun a : M V Z => representation a (some (r, 1))) h
  simp only [representation_normal_at, Option.some.injEq, Prod.mk.injEq] at e
  exact ⟨e.1, e.2, rfl⟩

@[simp] theorem normal_eq_iff {l l' : List Z} {g g' : V} {r r' : List Z} :
    normal l g r = normal l' g' r' ↔ l = l' ∧ g = g' ∧ r = r' := by
  constructor
  · exact normal_eq_parameters
  · rintro ⟨rfl, rfl, rfl⟩
    rfl

theorem unique_normal_form (a : M V Z) (ha : a ≠ omega) :
    ∃! p : List Z × V × List Z, a = normal p.1 p.2.1 p.2.2 := by
  obtain h | ⟨l, g, r, h⟩ := normal_exists a
  · exact (ha h).elim
  · refine ⟨(l, g, r), h, ?_⟩
    rintro ⟨l', g', r'⟩ h'
    obtain ⟨rfl, rfl, rfl⟩ := normal_eq_parameters (h'.symm.trans h)
    rfl

theorem groupMap_injective : Function.Injective (groupMap (V := V) (Z := Z)) := by
  intro g h heq
  have he : normal ([] : List Z) g [] = normal [] h [] := by
    simpa only [normal, prefixes_nil, deletions_nil, one_mul, mul_one] using heq
  exact (normal_eq_parameters he).2.1

def groupUnits : V →* (M V Z)ˣ := (groupMap (V := V) (Z := Z)).toHomUnits

@[simp] lemma groupUnits_val (g : V) :
    (groupUnits (Z := Z) g).val = groupMap (Z := Z) g := rfl

theorem groupUnits_injective : Function.Injective (groupUnits (V := V) (Z := Z)) := by
  intro g h heq
  exact groupMap_injective (congrArg Units.val heq)

end

namespace Algorithm
variable [DecidableEq Z]

abbrev Form := Option (List Z × V × List Z)

def eval : Form (V := V) (Z := Z) → M V Z
  | none => omega
  | some (l, g, r) => normal l g r

def step : Generator V Z → Form (V := V) (Z := Z) → Form (V := V) (Z := Z)
  | _, none => none
  | .zero, _ => none
  | .pre z, some (l, g, r) => some (z :: l, g, r)
  | .grp h, some ([], g, r) => some ([], h * g, r)
  | .grp h, some (z :: l, g, r) => some (h • z :: l, g, r)
  | .delete y, some ([], g, r) => some ([], 1, r ++ [g⁻¹ • y])
  | .delete y, some (z :: l, g, r) => if y = z then some (l, g, r) else none

lemma eval_step (c : Generator V Z) (f : Form (V := V) (Z := Z)) :
    eval (step c f) = of c * eval f := by
  cases f with
  | none => exact (of_omega c).symm
  | some p =>
    rcases p with ⟨l, g, r⟩
    cases c with
    | zero => exact (omega_mul _).symm
    | pre z => exact (prepend_normal z l g r).symm
    | grp h =>
      cases l with
      | nil => exact (group_normal_nil h g r).symm
      | cons z l => exact (group_normal_cons h z l g r).symm
    | delete y =>
      cases l with
      | nil => exact (delete_normal_nil y g r).symm
      | cons z l =>
        by_cases h : y = z
        · subst y
          change eval (if z = z then some (l, g, r) else none) =
            delete z * normal (z :: l) g r
          rw [ite_eq_left rfl]
          exact (delete_normal_cons z l g r).symm
        · change eval (if y = z then some (l, g, r) else none) =
            delete y * normal (z :: l) g r
          rw [ite_eq_right h]
          exact (delete_normal_cons_ne y z h l g r).symm

def normalize : List (Generator V Z) → Form (V := V) (Z := Z)
  | [] => some ([], 1, [])
  | c :: w => step c (normalize w)

theorem normalize_correct (w : List (Generator V Z)) :
    eval (normalize w) = PresentedMonoid.mk (Relation V Z) (FreeMonoid.ofList w) := by
  induction w with
  | nil => simp [normalize, eval, normal]
  | cons c w ih =>
    simp only [normalize, eval_step, ih, FreeMonoid.ofList_cons, map_mul]
    rfl

omit [DecidableEq Z] in
lemma eval_injective : Function.Injective (eval (V := V) (Z := Z)) := by
  intro a b hab
  cases a with
  | none =>
    cases b with
    | none => rfl
    | some p => exact (normal_ne_zero p.1 p.2.1 p.2.2 hab.symm).elim
  | some p =>
    rcases p with ⟨l, g, r⟩
    cases b with
    | none => exact (normal_ne_zero l g r hab).elim
    | some p =>
      rcases p with ⟨l', g', r'⟩
      obtain ⟨rfl, rfl, rfl⟩ := normal_eq_parameters hab
      rfl

theorem word_eq_iff (a b : List (Generator V Z)) :
    PresentedMonoid.mk (Relation V Z) (FreeMonoid.ofList a) =
        PresentedMonoid.mk (Relation V Z) (FreeMonoid.ofList b) ↔ normalize a = normalize b := by
  rw [← normalize_correct, ← normalize_correct]
  exact eval_injective.eq_iff

def equalWords [DecidableEq V] (a b : List (Generator V Z)) : Bool :=
  decide (normalize a = normalize b)

theorem equalWords_correct [DecidableEq V] (a b : List (Generator V Z)) :
    equalWords a b = true ↔
      PresentedMonoid.mk (Relation V Z) (FreeMonoid.ofList a) =
        PresentedMonoid.mk (Relation V Z) (FreeMonoid.ofList b) := by
  simp only [equalWords, decide_eq_true_eq, ← word_eq_iff]

end Algorithm
end SourceActionMonoid

end OAI