File size: 19,179 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
import OAI.GroupTheory.UniversalGroup.Markers

namespace OAI

universe u v w x

namespace Higman.FreeBasis

section
variable {X : Type*}

noncomputable def erase (S : Set X) : FreeGroup X →* FreeGroup X := by
  classical
  exact FreeGroup.lift (fun x ↦ if x ∈ S then FreeGroup.of x else 1)

open Classical in
@[simp] theorem erase_of (S : Set X) (x : X) : erase S (FreeGroup.of x) =
    if x ∈ S then FreeGroup.of x else 1 := by
  classical
  simp [erase]

abbrev span (S : Set X) : Subgroup (FreeGroup X) := Subgroup.closure (FreeGroup.of '' S)

theorem erase_eq_self_iff (S : Set X) (g : FreeGroup X) : erase S g = g ↔ g ∈ span S := by
  classical
  constructor
  · intro hg
    have hle : (erase S).range ≤ span S := by
      rw [erase,FreeGroup.range_lift_eq_closure]
      apply (Subgroup.closure_le _).mpr
      rintro _ ⟨x,rfl⟩
      by_cases hx : x ∈ S
      · simp only [hx,ite_true]
        exact Subgroup.subset_closure ⟨x,hx,rfl⟩
      · simp only [hx,ite_false]
        exact Subgroup.one_mem _
    exact hle ⟨g,hg⟩
  · have hle : span S ≤ (erase S).eqLocus (MonoidHom.id _) := by
      apply (Subgroup.closure_le _).mpr
      rintro _ ⟨x,hx,rfl⟩
      change erase S (FreeGroup.of x) = FreeGroup.of x
      simp [hx]
    exact fun hg ↦ hle hg

theorem erase_comp (S T : Set X) : (erase S).comp (erase T) = erase (S ∩ T) := by
  classical
  apply FreeGroup.ext_hom
  intro x
  by_cases hxS : x ∈ S <;> by_cases hxT : x ∈ T <;> simp [hxS,hxT]

theorem span_inter (S T : Set X) : span S ⊓ span T = span (S ∩ T) := by
  apply le_antisymm
  · intro g hg
    apply (erase_eq_self_iff _ _).mp
    rw [← erase_comp]
    change erase S (erase T g) = g
    rw [(erase_eq_self_iff T g).mpr hg.2,(erase_eq_self_iff S g).mpr hg.1]
  · exact le_inf (Subgroup.closure_mono (Set.image_mono Set.inter_subset_left))
      (Subgroup.closure_mono (Set.image_mono Set.inter_subset_right))

theorem of_mem_span_iff (S : Set X) (x : X) : FreeGroup.of x ∈ span S ↔ x ∈ S := by
  classical
  constructor
  · intro h
    by_contra hx
    have he := (erase_eq_self_iff S (FreeGroup.of x)).mpr h
    simp only [erase_of,hx,ite_false] at he
    exact FreeGroup.of_ne_one x he.symm
  · intro hx
    exact Subgroup.subset_closure ⟨x,hx,rfl⟩

end

variable {X : Type*} {G : Type*} [Group G]

theorem map_span_inf (d : FreeGroup X →* G) (r : G →* FreeGroup X)
    (hr : r.comp d = MonoidHom.id _) (U V : Set X) (A : Subgroup G)
    (hsupport : A.map r ≤ span U)
    (hletters : ∀ x ∈ U, d (FreeGroup.of x) ∈ A) :
    (span V).map d ⊓ A = (span (V ∩ U)).map d := by
  apply le_antisymm
  · rintro g ⟨⟨w,hw,rfl⟩,hA⟩
    refine ⟨w,?_,rfl⟩
    rw [← span_inter]
    refine ⟨hw,?_⟩
    have hh := hsupport (Subgroup.mem_map.mpr ⟨d w,hA,rfl⟩)
    change w ∈ span U
    simpa only [← MonoidHom.comp_apply,hr,MonoidHom.id_apply] using hh
  · apply le_inf
    · exact Subgroup.map_mono (Subgroup.closure_mono (Set.image_mono Set.inter_subset_left))
    · rw [span,MonoidHom.map_closure]
      apply (Subgroup.closure_le _).mpr
      rintro _ ⟨_,⟨x,hx,rfl⟩,rfl⟩
      exact hletters x hx.2

end Higman.FreeBasis

namespace Higman.DecoratedMarker
open Monoid Coprod
variable {G : Type u} [Group G] {T : Type v} [Group T] {Q : Type w}

theorem eq_inclusion_of_right_eq_one (x : Base G T Q) (hx : (test x).right = 1) :
    x = inclusion (test x).left := by
  apply (decomposition (G := G) (T := T) (Q := Q)).injective
  change test x = test (inclusion (test x).left)
  rw [← MonoidHom.comp_apply,test_comp]
  apply SemidirectProduct.ext
  · rfl
  · exact hx

end Higman.DecoratedMarker

namespace Higman.MarkerEmbedding

section
open Monoid Coprod DecoratedMarker
variable {G : Type u} [Group G] {D : Type v} [Group D]
    {T : Type w} [Group T] {P Q : Type*}

@[simp] theorem test_embedding (f : D →* T) (o : P → T) (q : P → Q)
    (w : P → G) (x : Base G D P) :
    test (embedding f o q w x) =
      SemidirectProduct.map (kernelMap f o q w) f (kernelMap_shift f o q w) (test x) := by
  change (test.comp inverse) _ = _
  rw [test_comp_inverse]
  rfl

@[simp] theorem embedding_inclusion (f : D →* T) (o : P → T) (q : P → Q)
    (w : P → G) (x : Kernel G D P) :
    embedding f o q w (inclusion x) = inclusion (kernelMap f o q w x) := by
  simp only [embedding,MonoidHom.comp_apply]
  rw [← MonoidHom.comp_apply (test : Base G D P →* _),test_comp]
  simp

theorem kernel_intersection (f : D →* T) (hf : Function.Injective f)
    (o : P → T) (q : P → Q) (w : P → G) (R : Subgroup (Kernel G T Q)) :
    R.map inclusion ⊓ (embedding f o q w).range =
      (R ⊓ (kernelMap f o q w).range).map inclusion := by
  apply le_antisymm
  · rintro _ ⟨⟨a,ha,rfl⟩,⟨x,hx⟩⟩
    have hxright : (test x).right = 1 := by
      apply hf
      have he := congrArg (fun y : Base G T Q ↦ (test y).right) hx
      simp only [test_embedding,SemidirectProduct.map_right] at he
      rw [← MonoidHom.comp_apply,test_comp] at he
      simpa only [SemidirectProduct.right_inl,map_one] using he
    have hx' := eq_inclusion_of_right_eq_one x hxright
    refine ⟨a,⟨ha,?_⟩,rfl⟩
    refine ⟨(test x).left,?_⟩
    apply inclusion_injective
    rw [← embedding_inclusion,← hx']
    exact hx
  · rintro _ ⟨a,⟨ha,⟨x,hx⟩⟩,rfl⟩
    refine ⟨⟨a,ha,rfl⟩,inclusion x,?_⟩
    rw [embedding_inclusion,hx]

lemma projection_kernelMap (f : D →* T) (o : P → T) (q : P → Q) (w : P → G) :
    (Coprod.snd : Kernel G T Q →* FreeGroup (T × Q)).comp (kernelMap f o q w) =
      (FreeGroup.map (index f o q)).comp (Coprod.snd : Kernel G D P →* _) := by
  apply Coprod.hom_ext
  · apply MonoidHom.ext
    intro g
    simp
  · apply FreeGroup.ext_hom
    intro x
    simp

lemma range_projection_support (f : D →* T) (o : P → T) (q : P → Q) (w : P → G) :
    (kernelMap f o q w).range.map Coprod.snd ≤ FreeBasis.span (Set.range (index f o q)) := by
  rintro _ ⟨_,⟨x,rfl⟩,rfl⟩
  have he := DFunLike.congr_fun (projection_kernelMap f o q w) x
  change Coprod.snd (kernelMap f o q w x) ∈ FreeBasis.span (Set.range (index f o q))
  rw [show Coprod.snd (kernelMap f o q w x) = FreeGroup.map (index f o q) (Coprod.snd x) from he]
  have hm : FreeGroup.map (index f o q) (Coprod.snd x) ∈ (FreeGroup.map (index f o q)).range :=
    ⟨Coprod.snd x,rfl⟩
  simpa only [FreeGroup.range_map,FreeBasis.span] using hm

lemma decorated_letter_mem_range (f : D →* T) (o : P → T) (q : P → Q) (w : P → G)
    (v : T × Q → G) (x : T × Q) (hx : x ∈ Set.range (index f o q)) :
    DecoratedFreeBasis.inclusion v (FreeGroup.of x) ∈ (kernelMap f o q w).range := by
  obtain ⟨⟨d,p⟩,rfl⟩ := hx
  refine ⟨inl (v (index f o q (d,p)) * (w p)⁻¹) * inr (FreeGroup.of (d,p)),?_⟩
  simp [mul_assoc]

theorem decorated_kernel_intersection (f : D →* T) (o : P → T) (q : P → Q)
    (w : P → G) (v : T × Q → G) (V : Set (T × Q)) :
    (FreeBasis.span V).map (DecoratedFreeBasis.inclusion v) ⊓ (kernelMap f o q w).range =
      (FreeBasis.span (V ∩ Set.range (index f o q))).map (DecoratedFreeBasis.inclusion v) :=
  FreeBasis.map_span_inf (DecoratedFreeBasis.inclusion v) Coprod.snd
    (DecoratedFreeBasis.projection_comp v) (Set.range (index f o q)) V _
    (range_projection_support f o q w) (decorated_letter_mem_range f o q w v)

def decoratedSubgroup (v : T × Q → G) (V : Set (T × Q)) : Subgroup (Base G T Q) :=
  (FreeBasis.span V).map (inclusion.comp (DecoratedFreeBasis.inclusion v))

theorem decorated_intersection (f : D →* T) (hf : Function.Injective f)
    (o : P → T) (q : P → Q) (w : P → G) (v : T × Q → G) (V : Set (T × Q)) :
    decoratedSubgroup v V ⊓ (embedding f o q w).range =
      decoratedSubgroup v (V ∩ Set.range (index f o q)) := by
  rw [decoratedSubgroup,← Subgroup.map_map,kernel_intersection f hf o q w,
    decorated_kernel_intersection,Subgroup.map_map]
  rfl

theorem decoratedSubgroup_eq_closure (v : T × Q → G) (V : Set (T × Q)) :
    decoratedSubgroup v V =
      Subgroup.closure ((fun x : T × Q ↦ coefficient (v x) * marker x.1 x.2) '' V) := by
  rw [decoratedSubgroup,FreeBasis.span,MonoidHom.map_closure,Set.image_image]
  congr 1

end

open DecoratedMarker
variable {G : Type u} [Group G] {T : Type v} [Group T] {Q : Type w}
  {D : Type*} [Group D] {P : Type*}

def pullDecoration (f : D →* T) (o : P → T) (q : P → Q) (w : P → G)
    (v : T × Q → G) (x : D × P) : G := v (index f o q x) * (w x.2)⁻¹

@[simp] lemma embedding_decorated (f : D →* T) (o : P → T) (q : P → Q) (w : P → G)
    (v : T × Q → G) (x : D × P) :
    embedding f o q w (coefficient (pullDecoration f o q w v x) * marker x.1 x.2) =
      coefficient (v (index f o q x)) * marker (index f o q x).1 (index f o q x).2 := by
  rw [map_mul,embedding_coefficient,embedding_marker,← mul_assoc,← map_mul]
  simp [pullDecoration,index]

lemma map_decoratedSubgroup (f : D →* T) (o : P → T) (q : P → Q) (w : P → G)
    (v : T × Q → G) (W : Set (D × P)) :
    (decoratedSubgroup (pullDecoration f o q w v) W).map (embedding f o q w) =
      decoratedSubgroup v (index f o q '' W) := by
  rw [decoratedSubgroup_eq_closure,MonoidHom.map_closure,decoratedSubgroup_eq_closure]
  simp only [Set.image_image,embedding_decorated]

theorem comap_decoratedSubgroup [Nonempty P] (f : D →* T) (hf : Function.Injective f)
    (o : P → T) (q : P → Q) (w : P → G) (hi : Function.Injective (index f o q))
    (v : T × Q → G) (V : Set (T × Q)) :
    (decoratedSubgroup v V).comap (embedding f o q w) =
      decoratedSubgroup (pullDecoration f o q w v) (index f o q ⁻¹' V) := by
  apply Subgroup.map_injective (embedding_injective f hf o q w hi)
  rw [Subgroup.map_comap_eq,inf_comm,decorated_intersection f hf,
    map_decoratedSubgroup,Set.image_preimage_eq_inter_range]

end Higman.MarkerEmbedding

namespace Higman.PartialIso
variable {H : Type u} [Group H] {G : Type u} [Group G]

noncomputable def ofEmbeddings (i j : H →* G) (hi : Function.Injective i)
    (hj : Function.Injective j) : PartialIso G where
  source := i.range
  target := j.range
  equiv := (MonoidHom.ofInjective hi).symm.trans (MonoidHom.ofInjective hj)

@[simp] theorem ofEmbeddings_apply (i j : H →* G) (hi : Function.Injective i)
    (hj : Function.Injective j) (x : H) :
    ((ofEmbeddings i j hi hj).equiv (MonoidHom.ofInjective hi x) : G) = j x := by
  change j ((MonoidHom.ofInjective hi).symm ((MonoidHom.ofInjective hi) x)) = j x
  rw [MulEquiv.symm_apply_apply]

lemma ofEmbeddings_invariant_iff (i j : H →* G) (hi : Function.Injective i)
    (hj : Function.Injective j) (R : Subgroup G) :
    (ofEmbeddings i j hi hj).Invariant R ↔ R.comap i = R.comap j := by
  constructor
  · intro h
    ext x
    simpa only [ofEmbeddings_apply,MonoidHom.ofInjective_apply,Subgroup.mem_comap] using
      (h (MonoidHom.ofInjective hi x)).symm
  · intro h a
    obtain ⟨x,rfl⟩ := (MonoidHom.ofInjective hi).surjective a
    rw [ofEmbeddings_apply]
    change x ∈ R.comap j ↔ x ∈ R.comap i
    rw [h]

end Higman.PartialIso
namespace Higman.MarkerMachine
open DecoratedMarker MarkerEmbedding

structure Edge (G T : Type u) [Group G] [Group T] (Q : Type u) where
  Domain : Type u
  [domainGroup : Group Domain]
  inputLinear : Domain →* T
  outputLinear : Domain →* T
  input_injective : Function.Injective inputLinear
  output_injective : Function.Injective outputLinear
  inputOffset : T
  outputOffset : T
  inputState : Q
  outputState : Q
  weight : G

attribute [instance] Edge.domainGroup
variable {G T Q : Type u} [Group G] [Group T]

namespace Edge
variable (e : Edge G T Q)

def input (d : e.Domain) : T × Q := (e.inputLinear d * e.inputOffset,e.inputState)
def output (d : e.Domain) : T × Q := (e.outputLinear d * e.outputOffset,e.outputState)

def inputMap : Base G e.Domain Unit →* Base G T Q :=
  embedding e.inputLinear (fun _ ↦ e.inputOffset) (fun _ ↦ e.inputState) (fun _ ↦ 1)
def outputMap : Base G e.Domain Unit →* Base G T Q :=
  embedding e.outputLinear (fun _ ↦ e.outputOffset) (fun _ ↦ e.outputState) (fun _ ↦ e.weight)

lemma input_index_injective : Function.Injective
    (index e.inputLinear (fun _ : Unit ↦ e.inputOffset) (fun _ ↦ e.inputState)) :=
  index_injective e.inputLinear e.input_injective _ _ (Function.injective_of_subsingleton _)
lemma output_index_injective : Function.Injective
    (index e.outputLinear (fun _ : Unit ↦ e.outputOffset) (fun _ ↦ e.outputState)) :=
  index_injective e.outputLinear e.output_injective _ _ (Function.injective_of_subsingleton _)

lemma inputMap_injective : Function.Injective e.inputMap :=
  embedding_injective e.inputLinear e.input_injective _ _ _ e.input_index_injective
lemma outputMap_injective : Function.Injective e.outputMap :=
  embedding_injective e.outputLinear e.output_injective _ _ _ e.output_index_injective

noncomputable def partialIso : PartialIso (Base G T Q) :=
  PartialIso.ofEmbeddings e.inputMap e.outputMap e.inputMap_injective e.outputMap_injective

lemma inputMap_decorated (v : G) (d : e.Domain) :
    e.inputMap (coefficient v * marker d ()) =
      coefficient v * marker (e.input d).1 (e.input d).2 := by
  simp [inputMap,input]

lemma outputMap_decorated (v : G) (d : e.Domain) :
    e.outputMap (coefficient v * marker d ()) =
      coefficient (v * e.weight) * marker (e.output d).1 (e.output d).2 := by
  simp [outputMap,output,map_mul,mul_assoc]

end Edge
end Higman.MarkerMachine

namespace Higman.MarkerEmbedding
open DecoratedMarker
variable {G : Type u} [Group G] {T : Type v} [Group T] {Q : Type w}

lemma decoratedSubgroup_congr {v w : T × Q → G} {V : Set (T × Q)}
    (h : ∀ x ∈ V, v x = w x) : decoratedSubgroup v V = decoratedSubgroup w V := by
  rw [decoratedSubgroup_eq_closure,decoratedSubgroup_eq_closure]
  congr 1
  apply Set.image_congr
  intro x hx
  rw [h x hx]

end Higman.MarkerEmbedding
namespace Higman.MarkerMachine.Edge
open DecoratedMarker MarkerEmbedding
variable {G T Q : Type u} [Group G] [Group T] (e : Edge G T Q)

theorem invariant (v : T × Q → G) (V : Set (T × Q))
    (hV : ∀ d, e.input d ∈ V ↔ e.output d ∈ V)
    (hv : ∀ d, e.input d ∈ V → v (e.output d) = v (e.input d) * e.weight) :
    e.partialIso.Invariant (decoratedSubgroup v V) := by
  rw [partialIso,PartialIso.ofEmbeddings_invariant_iff]
  unfold inputMap outputMap
  rw [comap_decoratedSubgroup e.inputLinear e.input_injective _ _ _ e.input_index_injective,
    comap_decoratedSubgroup e.outputLinear e.output_injective _ _ _ e.output_index_injective]
  have he : index e.inputLinear (fun _ : Unit ↦ e.inputOffset) (fun _ ↦ e.inputState) ⁻¹' V =
      index e.outputLinear (fun _ : Unit ↦ e.outputOffset) (fun _ ↦ e.outputState) ⁻¹' V := by
    ext x
    exact hV x.1
  rw [← he]
  apply decoratedSubgroup_congr
  rintro ⟨d,p⟩ hd
  change v (e.input d) * (1 : G)⁻¹ = v (e.output d) * e.weight⁻¹
  change e.input d ∈ V at hd
  rw [hv d hd]
  simp

lemma invariant_decorated_iff (R : Subgroup (Base G T Q)) (hR : e.partialIso.Invariant R)
    (a : G) (d : e.Domain) :
    coefficient (a * e.weight) * marker (e.output d).1 (e.output d).2 ∈ R ↔
      coefficient a * marker (e.input d).1 (e.input d).2 ∈ R := by
  have hh := hR (MonoidHom.ofInjective e.inputMap_injective (coefficient a * marker d ()))
  change ((PartialIso.ofEmbeddings e.inputMap e.outputMap _ _).equiv
    (MonoidHom.ofInjective e.inputMap_injective (coefficient a * marker d ())) :
    Base G T Q) ∈ R ↔ _ at hh
  simpa only [PartialIso.ofEmbeddings_apply,MonoidHom.ofInjective_apply,
    inputMap_decorated,outputMap_decorated] using hh

lemma source_fg [Group.IsFinitelyPresented G] [Group.IsFinitelyPresented e.Domain] :
    e.partialIso.source.FG := by
  have : Group.IsFinitelyPresented (G × e.Domain) := finitelyPresented_prod
  obtain ⟨n,p,hp,_⟩ := Group.IsFinitelyPresented.out (G := Base G e.Domain Unit)
  have : Group.FG (Base G e.Domain Unit) := Group.fg_of_surjective hp
  exact (Group.fg_iff_subgroup_fg e.inputMap.range).mp inferInstance

end Higman.MarkerMachine.Edge

namespace Higman.MarkerMachine
open DecoratedMarker MarkerEmbedding
variable {G T Q : Type u} [Group G] [Group T]

inductive Reach (es : List (Edge G T Q)) (seed : T × Q) : T × Q → Prop
  | start : Reach es seed seed
  | forward (e : Edge G T Q) (he : e ∈ es) (d : e.Domain) :
      Reach es seed (e.input d) → Reach es seed (e.output d)
  | backward (e : Edge G T Q) (he : e ∈ es) (d : e.Domain) :
      Reach es seed (e.output d) → Reach es seed (e.input d)

lemma reach_iff (es : List (Edge G T Q)) (seed : T × Q) (e : Edge G T Q) (he : e ∈ es)
    (d : e.Domain) : Reach es seed (e.input d) ↔ Reach es seed (e.output d) :=
  ⟨Reach.forward e he d,Reach.backward e he d⟩

def word (v : T × Q → G) (c : T × Q) : Base G T Q :=
  coefficient (v c) * marker c.1 c.2

theorem orbit_eq (es : List (Edge G T Q)) (seed : T × Q) (v : T × Q → G)
    (hv : ∀ e ∈ es, ∀ d, Reach es seed (e.input d) →
      v (e.output d) = v (e.input d) * e.weight) :
    Orbit.subgroup (es.map Edge.partialIso) {word v seed} =
      decoratedSubgroup v {c | Reach es seed c} := by
  apply le_antisymm
  · apply Orbit.minimal
    · rintro x (rfl : x = word v seed)
      rw [decoratedSubgroup_eq_closure]
      exact Subgroup.subset_closure ⟨seed,Reach.start,rfl⟩
    · intro p hp
      obtain ⟨e,he,rfl⟩ := List.mem_map.mp hp
      exact e.invariant v _ (reach_iff es seed e he) (hv e he)
  · rw [decoratedSubgroup_eq_closure]
    apply (Subgroup.closure_le _).mpr
    rintro _ ⟨c,hc,rfl⟩
    change word v c ∈ Orbit.subgroup (es.map Edge.partialIso) {word v seed}
    induction hc with
    | start => exact Orbit.subset _ _ (Set.mem_singleton _)
    | forward e he d hr ih =>
      have hinv := Orbit.invariant (es.map Edge.partialIso) {word v seed} e.partialIso
        (List.mem_map.mpr ⟨e,he,rfl⟩)
      change coefficient (v (e.output d)) * marker (e.output d).1 (e.output d).2 ∈ _
      rw [hv e he d hr]
      exact (e.invariant_decorated_iff _ hinv (v (e.input d)) d).mpr ih
    | backward e he d hr ih =>
      have hinv := Orbit.invariant (es.map Edge.partialIso) {word v seed} e.partialIso
        (List.mem_map.mpr ⟨e,he,rfl⟩)
      apply (e.invariant_decorated_iff _ hinv (v (e.input d)) d).mp
      rw [← hv e he d (Reach.backward e he d hr)]
      exact ih

theorem benign [Group.IsFinitelyPresented G] [Group.IsFinitelyPresented T] [Finite Q]
    (es : List (Edge G T Q)) (hD : ∀ e ∈ es, Group.IsFinitelyPresented e.Domain)
    (seed : T × Q) (v : T × Q → G)
    (hv : ∀ e ∈ es, ∀ d, Reach es seed (e.input d) →
      v (e.output d) = v (e.input d) * e.weight) :
    IsBenign (decoratedSubgroup v {c | Reach es seed c}) := by
  have : Group.IsFinitelyPresented (G × T) := finitelyPresented_prod
  rw [← orbit_eq es seed v hv]
  apply Orbit.benign _ _ _ (Set.finite_singleton _)
  intro p hp
  obtain ⟨e,he,rfl⟩ := List.mem_map.mp hp
  have := hD e he
  exact e.source_fg

end Higman.MarkerMachine

end OAI