Skip to content

Commit ca087f5

Browse files
committed
Add refactor tests and rename some symbols
1 parent 3150d81 commit ca087f5

5 files changed

Lines changed: 154 additions & 42 deletions

File tree

TraceTheory/TraceTheory/Basic.lean

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,8 @@ inductive CommuteOnce (I : Independence α) : List α → List α → Prop
6969
| mk : ∀ (x y : List α) (a b : α), I.rel a b →
7070
CommuteOnce I (x ++ [a, b] ++ y) (x ++ [b, a] ++ y)
7171

72-
instance : Coe α (FreeMonoid α) := ⟨FreeMonoid.of⟩
73-
74-
inductive SwapOnce (I : Independence α) : FreeMonoid α → FreeMonoid α → Prop
75-
| swap (a b : α) : I.rel a b → SwapOnce I (↑a * ↑b) (↑b * ↑a)
76-
77-
def traceCon (I : Independence α) : Con (FreeMonoid α) := conGen (SwapOnce I)
78-
79-
def TraceMonoid' (I : Independence α) := (traceCon I).Quotient
80-
8172
variable {I : Independence α}
8273

83-
instance : Monoid (TraceMonoid' I) := (traceCon I).monoid
84-
8574
lemma eqvGen_append_right {u v w : List α}
8675
(h : Relation.EqvGen (CommuteOnce I) u v) :
8776
Relation.EqvGen (CommuteOnce I) (u ++ w) (v ++ w) := by
@@ -660,18 +649,18 @@ theorem projection_lemma {u v : List α} [DecidableEq α] (D : Dependence α) :
660649

661650
/-- The setoid structure on strings defined by the trace equivalence relation `TraceEqv I`.
662651
This serves as the basis for constructing the quotient type `Trace`. -/
663-
def traceSetoid (I : Independence α) : Setoid (List α) where
652+
def TraceSetoid (I : Independence α) : Setoid (List α) where
664653
r := TraceEqv I
665654
iseqv := Equivalence.mk TraceEqv.refl TraceEqv.symm TraceEqv.trans
666655

667656
/-- The type of Mazurkiewicz traces, defined as the quotient of the free monoid
668657
by the trace equivalence relation induced by `I`. -/
669-
def Trace (I : Independence α) := Quotient (traceSetoid I)
658+
def TraceMonoid (I : Independence α) := Quotient (TraceSetoid I)
670659

671660
/-- Composition (concatenation) of traces. This is induced by the concatenation
672661
operation on the underlying strings. -/
673662
@[simp]
674-
def mul : Trace I -> Trace I -> Trace I := by
663+
def mul : TraceMonoid I -> TraceMonoid I -> TraceMonoid I := by
675664
exact Quotient.lift₂
676665
(fun w₁ w₂ => ⟦w₁ ++ w₂⟧)
677666
(by
@@ -681,9 +670,9 @@ def mul : Trace I -> Trace I -> Trace I := by
681670
)
682671

683672
/-- The empty trace, represented by the equivalence class of the empty list. -/
684-
def one := Quotient.mk (traceSetoid I) []
673+
def one := Quotient.mk (TraceSetoid I) []
685674

686-
instance : Monoid (Trace I) where
675+
instance : Monoid (TraceMonoid I) where
687676
mul := mul
688677
one := one
689678
mul_assoc := by
@@ -708,7 +697,7 @@ instance : Monoid (Trace I) where
708697
variable (I) in
709698
/-- The prefix relation on traces. A trace `t₁` is a prefix of `t₂` if there exists
710699
a trace `⟦w⟧` such that `t₁` concatenated with `⟦w⟧` equals `t₂`. -/
711-
def isPrefix (t₁ t₂ : Trace I) := ∃ w, mul t₁ ⟦w⟧ = t₂
700+
def isPrefix (t₁ t₂ : TraceMonoid I) := ∃ w, mul t₁ ⟦w⟧ = t₂
712701

713702
theorem exists_gcp {u v w : List α} [DecidableEq α]
714703
(hu : isPrefix I ⟦u⟧ ⟦w⟧) (hv : isPrefix I ⟦v⟧ ⟦w⟧) :
@@ -840,6 +829,26 @@ instance : Monoid (List α) where
840829

841830
variable [DecidableEq α]
842831

832+
instance : CancelMonoid (TraceMonoid I) where
833+
mul_left_cancel := by
834+
intro a b c heq
835+
induction a using Quotient.inductionOn with | h a =>
836+
induction b using Quotient.inductionOn with | h b =>
837+
induction c using Quotient.inductionOn with | h c =>
838+
apply Quotient.sound
839+
simp only at heq
840+
have heqv := by simpa using Quotient.exact heq
841+
exact append_cancel_left heqv
842+
mul_right_cancel := by
843+
intro a b c heq
844+
induction a using Quotient.inductionOn with | h a =>
845+
induction b using Quotient.inductionOn with | h b =>
846+
induction c using Quotient.inductionOn with | h c =>
847+
apply Quotient.sound
848+
simp only at heq
849+
have heqv := by simpa using Quotient.exact heq
850+
exact append_cancel_right heqv
851+
843852
variable (I) in
844853
/-- A dependence morphism is any homomophism from the free monoid of strings onto another monoid
845854
such that:
@@ -870,16 +879,16 @@ theorem DependenceMorphism.map_append {I M} [Monoid M]
870879
ϕ.toFun.map_mul u v
871880

872881
/-- The natural homomorphism from the free monoid `List α` to the trace monoid `Trace I`. -/
873-
def mk' : List α →* Trace I where
874-
toFun := Quotient.mk (traceSetoid I)
882+
def mk' : List α →* TraceMonoid I where
883+
toFun := Quotient.mk (TraceSetoid I)
875884
map_one' := rfl
876885
map_mul' := by
877886
intro w₁ w₂
878887
rfl
879888

880889
/-- The natural homomorphism from the free monoid of strings to the trace monoid
881890
is a dependence morphism. -/
882-
def traceDependenceMorphism : DependenceMorphism I (Trace I) where
891+
def traceDependenceMorphism : DependenceMorphism I (TraceMonoid I) where
883892
toFun := mk'
884893
A1 := by
885894
intro w hw

TraceTheory/TraceTheory/DependenceGraph.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def dependenceGraphDependenceMorphism [DecidableEq α] :
932932
-- Theorem (1.4.8)
933933
/-- The trace monoid and `GraphMonoid` are isomorphic. -/
934934
noncomputable def traceMonoidIsoGraphMonoid [DecidableEq α] :
935-
Trace (inducedIndependence D) ≃* GraphMonoid D := by
935+
TraceMonoid (inducedIndependence D) ≃* GraphMonoid D := by
936936
apply dependenceMorphismIso
937937
traceDependenceMorphism
938938
Quotient.mk_surjective

TraceTheory/TraceTheory/History.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def historyDependenceMorphism (S : Fin n → Finset α) (h_cover : ∀ a, ∃ i,
193193
/-- The trace monoid and `HistoryMonoid` are isomorphic. -/
194194
noncomputable def traceMonoidIsoHistoryMonoid
195195
(S : Fin n → Finset α) (h_cover : ∀ a, ∃ i, a ∈ S i) :
196-
Trace (inducedIndependence (SigmaDependence S h_cover)) ≃* HistoryMonoid S := by
196+
TraceMonoid (inducedIndependence (SigmaDependence S h_cover)) ≃* HistoryMonoid S := by
197197
apply dependenceMorphismIso
198198
traceDependenceMorphism
199199
Quotient.mk_surjective

TraceTheory/TraceTheory/Language.lean

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace TraceTheory
88

99
section LexNf
1010

11-
variable {α : Type} [LinearOrder α]
11+
variable {α : Type*} [LinearOrder α]
1212

1313
/-- Lexicographic Normal Form.
1414
A word x is in normal form if it is minimal among all words equivalent to it. -/
1515
def IsLexNf (I : Independence α) (x : List α) : Prop :=
16-
∀ w, TraceEquiv I x w → x ≤ w
16+
∀ w, TraceEqv I x w → x ≤ w
1717

1818
/-- The condition to be in Lexicographic Normal Form.
1919
For all factorizations x = ybuaz, where (a, b) ∈ I, and a < b,
@@ -34,18 +34,18 @@ lemma factorCondition_of_lexNf (I : Independence α) (x : List α) (h : IsLexNf
3434
use y ++ [a] ++ [b] ++ u ++ z
3535
rw [hx]
3636
constructor
37-
· have h_comm_au : TraceEquiv I ([a] ++ u) (u ++ [a]) := by
37+
· have h_comm_au : TraceEqv I ([a] ++ u) (u ++ [a]) := by
3838
apply comm_append_of_indep
3939
intro c hc
4040
simp at hc
4141
rw [hc]
4242
exact h
43-
apply TraceEquiv.compat _ (TraceEquiv.refl z)
43+
apply TraceEqv.compat _ (TraceEqv.refl z)
4444
simp only [List.append_assoc]
45-
apply TraceEquiv.compat (TraceEquiv.refl y)
46-
apply TraceEquiv.trans (TraceEquiv.compat (TraceEquiv.refl [b]) (TraceEquiv.symm h_comm_au))
45+
apply TraceEqv.compat (TraceEqv.refl y)
46+
apply TraceEqv.trans (TraceEqv.compat (TraceEqv.refl [b]) (TraceEqv.symm h_comm_au))
4747
simp only [← List.append_assoc]
48-
exact TraceEquiv.compat (TraceEquiv.swap b a (I.symm a b h_indep)) (TraceEquiv.refl u)
48+
exact TraceEqv.compat (TraceEqv.swap b a (I.symm a b h_indep)) (TraceEqv.refl u)
4949
· simp
5050
apply List.append_left_lt
5151
apply List.cons_lt_cons_iff.mpr
@@ -54,7 +54,7 @@ lemma factorCondition_of_lexNf (I : Independence α) (x : List α) (h : IsLexNf
5454

5555
-- TODO: Move somewhere else?
5656
lemma indep_and_exists_of_equiv_of_head_ne {a b : α} {w x : List α}
57-
(I : Independence α) (h : TraceEquiv I ([a] ++ w) ([b] ++ x)) (hne : a ≠ b) :
57+
(I : Independence α) (h : TraceEqv I ([a] ++ w) ([b] ++ x)) (hne : a ≠ b) :
5858
I.rel a b ∧ ∃ u v, x = u ++ [a] ++ v ∧ Independent I [a] u := by
5959
have h_rev := reverse_equiv_of_equiv h
6060
simp at h_rev
@@ -179,15 +179,15 @@ variable {α : Type} {I : Independence α}
179179

180180
/-- The `Language` of all strings trace equivalent to strings in language `X`. -/
181181
def traceClosure (I : Independence α) (X : Language α) : Language α :=
182-
{ y | ∃ x ∈ X, TraceEquiv I x y }
182+
{ y | ∃ x ∈ X, TraceEqv I x y }
183183

184184
/-- A language is `I`-closed if its trace closure under `I` is equal to itself. -/
185185
def IsClosed (I : Independence α) (X : Language α) : Prop :=
186186
traceClosure I X = X
187187

188188
theorem traceClosure.le_closure {X : Language α} : X ≤ traceClosure I X := by
189189
intro x hx
190-
exact ⟨x, hx, TraceEquiv.refl x⟩
190+
exact ⟨x, hx, TraceEqv.refl x⟩
191191

192192
theorem traceClosure.mono {X Y : Language α} (h : X ≤ Y) :
193193
traceClosure I X ≤ traceClosure I Y := by
@@ -200,7 +200,7 @@ theorem traceClosure.idem {X : Language α} :
200200
apply le_antisymm
201201
· intro x hx
202202
rcases hx with ⟨y, ⟨z, hz, heqv_zy⟩, heqv_yx⟩
203-
exact ⟨z, hz, TraceEquiv.trans heqv_zy heqv_yx⟩
203+
exact ⟨z, hz, TraceEqv.trans heqv_zy heqv_yx⟩
204204
· apply mono
205205
apply le_closure
206206

@@ -209,8 +209,8 @@ def IsValidFactorization
209209
(I : Independence α) (X : Language α) (x y : List α) (xs ys : List (List α)) : Prop :=
210210
xs.length = ys.length ∧
211211
(List.zipWith (· ++ ·) xs ys).flatten ∈ X ∧
212-
TraceEquiv I x xs.flatten ∧
213-
TraceEquiv I y ys.flatten ∧
212+
TraceEqv I x xs.flatten ∧
213+
TraceEqv I y ys.flatten ∧
214214
∀ i : ℕ, ∀ (h : i + 1 < xs.length),
215215
Independent I (xs[i]'(Nat.lt_of_succ_lt h)) ((ys.drop i).flatten)
216216

TraceTheory/TraceTheory/Refactor.lean

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Mathlib.Algebra.FreeMonoid.Basic
22
import Mathlib.GroupTheory.Congruence.Basic
33
import TraceTheory.List
44

5+
open FreeMonoid
6+
57
namespace TraceTheory
68

79
variable {α : Type*}
@@ -32,22 +34,123 @@ def inducedIndependence {α : Type*} (D : Dependence α) : Independence α where
3234

3335
instance : Coe α (FreeMonoid α) := ⟨FreeMonoid.of⟩
3436

37+
attribute [coe] FreeMonoid.of
38+
3539
/-- The binary relation $~$ such that $u~v$ if and only if there exists strings $x,y$ and symbols
36-
$a,b$ such that $u=xaby$ and $v=xbay$. -/
40+
$a,b$ such that $aIb$, $u=xaby$ and $v=xbay$. -/
3741
inductive SwapOnce (I : Independence α) : FreeMonoid α → FreeMonoid α → Prop
3842
| swap (a b : α) : I.rel a b → SwapOnce I (↑a * ↑b) (↑b * ↑a)
3943

40-
/-- Trace equivalence as the least congruence containing $~$. -/
41-
def traceCon (I : Independence α) : Con (FreeMonoid α) := conGen (SwapOnce I)
44+
/-- Trace equivalence defined as the least congruence containing $~$. -/
45+
def TraceEqv (I : Independence α) : Con (FreeMonoid α) := conGen (SwapOnce I)
4246

43-
/-- The trace monoid. -/
44-
def TraceMonoid (I : Independence α) := (traceCon I).Quotient
47+
/-- The trace monoid defined as the quotient of the free monoid by the trace equivalence relation
48+
induced by `I`. -/
49+
def TraceMonoid (I : Independence α) := (TraceEqv I).Quotient
4550

4651
variable {I : Independence α}
4752

48-
instance : Monoid (TraceMonoid I) := (traceCon I).monoid
53+
instance : Monoid (TraceMonoid I) := (TraceEqv I).monoid
54+
55+
theorem length_eq_of_eqv {u v : FreeMonoid α} (h : TraceEqv I u v) : u.length = v.length := by
56+
induction h with
57+
| of _ _ h_swap => cases h_swap; simp
58+
| refl w => rfl
59+
| symm _ ih => simp [ih]
60+
| trans _ _ ih₁ ih₂ => simp [ih₁, ih₂]
61+
| mul _ _ ih₁ ih₂ => simp [ih₁, ih₂]
62+
63+
theorem mem_iff_mem {u v : FreeMonoid α} (a : α) (h : TraceEqv I u v) : a ∈ u ↔ a ∈ v := by
64+
induction h with
65+
| of _ _ h_swap => cases h_swap; simp [mem_mul, mem_of, Or.comm]
66+
| refl w => rfl
67+
| symm _ ih => simp [ih]
68+
| trans _ _ ih₁ ih₂ => simp [ih₁, ih₂]
69+
| mul _ _ ih₁ ih₂ => simp [ih₁, ih₂]
70+
71+
theorem rev_eqv_of_eqv {u v : FreeMonoid α} (h : TraceEqv I u v) :
72+
TraceEqv I u.reverse v.reverse := by
73+
induction h with
74+
| of _ _ h_swap =>
75+
cases h_swap with
76+
| swap a b h_indep =>
77+
simp only [reverse_mul]
78+
apply ConGen.Rel.of
79+
exact SwapOnce.swap b a (I.symm _ _ h_indep)
80+
| refl _ => apply (TraceEqv I).refl
81+
| symm _ ih => exact ih.symm
82+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
83+
| mul _ _ ih₁ ih₂ =>
84+
simp only [reverse_mul]
85+
exact ih₂.mul ih₁
86+
87+
theorem proj_eqv_of_eqv {u v : FreeMonoid α} {S : Finset α} [DecidableEq α] (h : TraceEqv I u v) :
88+
TraceEqv I (proj S u) (proj S v) := by
89+
sorry
90+
91+
theorem of_mul_cancel_left {a : α} {u v : FreeMonoid α} [DecidableEq α]
92+
(h : TraceEqv I (↑a * u) (↑a * v)) :
93+
TraceEqv I u v := by
94+
sorry
4995

50-
theorem length_eq_of_con {u v : FreeMonoid α} (h : traceCon I u v) : u.length = v.length := by
96+
theorem of_mul_cancel_right {a : α} {u v : FreeMonoid α} [DecidableEq α]
97+
(h : TraceEqv I (u * ↑a) (v * ↑a)) :
98+
TraceEqv I u v := by
5199
sorry
52100

101+
theorem mul_cancel_left {w u v : FreeMonoid α} [DecidableEq α] (h : TraceEqv I (w * u) (w * v)) :
102+
TraceEqv I u v := by
103+
induction w using recOn with
104+
| h0 => exact h
105+
| ih _ _ ih =>
106+
rw [mul_assoc, mul_assoc] at h
107+
exact ih (of_mul_cancel_left h)
108+
109+
theorem mul_cancel_right {w u v : FreeMonoid α} [DecidableEq α] (h : TraceEqv I (u * w) (v * w)) :
110+
TraceEqv I u v := by
111+
induction w using List.reverseRecOn with
112+
| nil =>
113+
change TraceEqv I (u * 1) (v * 1) at h
114+
simp_all [mul_one]
115+
| append_singleton w' a ih =>
116+
change (TraceEqv I) (u * (↑w' * ↑a)) (v * (↑w' * ↑a)) at h
117+
rw [← mul_assoc, ← mul_assoc] at h
118+
exact ih (of_mul_cancel_right h)
119+
120+
instance [DecidableEq α] : CancelMonoid (TraceMonoid I) where
121+
mul_left_cancel := by
122+
intro a b c heq
123+
induction a using Quotient.inductionOn with | h a =>
124+
induction b using Quotient.inductionOn with | h b =>
125+
induction c using Quotient.inductionOn with | h c =>
126+
apply Quotient.sound
127+
simp only at heq
128+
have heqv := by simpa using Quotient.exact heq
129+
exact mul_cancel_left heqv
130+
mul_right_cancel := by
131+
intro a b c heq
132+
induction a using Quotient.inductionOn with | h a =>
133+
induction b using Quotient.inductionOn with | h b =>
134+
induction c using Quotient.inductionOn with | h c =>
135+
apply Quotient.sound
136+
simp only at heq
137+
have heqv := by simpa using Quotient.exact heq
138+
exact mul_cancel_right heqv
139+
140+
inductive SwapOnce' (I : Independence α) : List α → List α → Prop
141+
| swap (a b : α) : I.rel a b → SwapOnce' I [a, b] [b, a]
142+
143+
instance : Monoid (List α) where
144+
one := []
145+
mul := List.append
146+
mul_assoc := List.append_assoc
147+
mul_one := List.append_nil
148+
one_mul := List.nil_append
149+
150+
def TraceEqv' (I : Independence α) : Con (List α) := conGen (SwapOnce' I)
151+
152+
def TraceMonoid' (I : Independence α) := (TraceEqv' I).Quotient
153+
154+
instance : Monoid (TraceMonoid' I) := (TraceEqv' I).monoid
155+
53156
end TraceTheory

0 commit comments

Comments
 (0)