Skip to content

Commit b8ea195

Browse files
committed
Fix Myhill-Nerode and golf
1 parent 01d0ad1 commit b8ea195

5 files changed

Lines changed: 125 additions & 178 deletions

File tree

TraceTheory/TraceTheory/Basic.lean

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,30 @@ variable {α : Type*} {I : Independence α}
99
theorem length_eq_of_eqv {u v : List α} (h : TraceEqv I u v) :
1010
u.length = v.length := by
1111
induction h with
12-
| swap _ _ _ =>
13-
rfl
14-
| refl _ =>
15-
rfl
16-
| symm _ ih =>
17-
exact ih.symm
18-
| trans _ _ ih₁ ih₂ =>
19-
exact ih₁.trans ih₂
20-
| compat _ _ ih₁ ih₂ =>
21-
simp [ih₁, ih₂]
12+
| swap _ _ _ => rfl
13+
| refl _ => rfl
14+
| symm _ ih => exact ih.symm
15+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
16+
| compat _ _ ih₁ ih₂ => simp [ih₁, ih₂]
2217

2318
theorem mem_iff_mem {u v : List α} (a : α) (h : TraceEqv I u v) :
2419
(a ∈ u ↔ a ∈ v) := by
2520
induction h with
26-
| swap _ _ _ =>
27-
simp
28-
exact Or.comm
29-
| refl _ =>
30-
exact Eq.to_iff rfl
31-
| symm _ ih =>
32-
exact Iff.symm ih
33-
| trans _ _ ih₁ ih₂ =>
34-
exact ih₁.trans ih₂
35-
| compat _ _ ih₁ ih₂ =>
36-
simp
37-
exact or_congr ih₁ ih₂
21+
| swap _ _ _ => simp [or_comm]
22+
| refl _ => rfl
23+
| symm _ ih => exact ih.symm
24+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
25+
| compat _ _ ih₁ ih₂ => simp [ih₁, ih₂]
3826

3927
/-- The mirror rule. -/
4028
theorem reverse_eqv_of_eqv {u v : List α} (h : TraceEqv I u v) :
4129
TraceEqv I u.reverse v.reverse := by
4230
induction h with
43-
| swap a b h =>
44-
simp
45-
exact TraceEqv.swap b a (I.symm a b h)
46-
| refl u' =>
47-
exact TraceEqv.refl u'.reverse
48-
| symm _ ih =>
49-
exact ih.symm
50-
| trans _ _ ih₁ ih₂ =>
51-
exact ih₁.trans ih₂
52-
| compat _ _ ih₁ ih₂ =>
53-
simp
54-
exact ih₂.compat ih₁
31+
| swap a b h => simp [TraceEqv.swap b a (I.symm a b h)]
32+
| refl u' => apply TraceEqv.refl
33+
| symm _ ih => exact ih.symm
34+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
35+
| compat _ _ ih₁ ih₂ => simp [ih₂.compat ih₁]
5536

5637
/-- The projection rule. -/
5738
theorem proj_eqv_of_eqv {u v : List α} {S : Finset α} [DecidableEq α] (h : TraceEqv I u v) :
@@ -64,15 +45,10 @@ theorem proj_eqv_of_eqv {u v : List α} {S : Finset α} [DecidableEq α] (h : Tr
6445
· apply TraceEqv.refl
6546
· apply TraceEqv.refl
6647
· apply TraceEqv.refl
67-
| refl _ =>
68-
apply TraceEqv.refl
69-
| symm _ ih =>
70-
exact ih.symm
71-
| trans _ _ ih₁ ih₂ =>
72-
exact ih₁.trans ih₂
73-
| compat _ _ ih₁ ih₂ =>
74-
simp
75-
exact ih₁.compat ih₂
48+
| refl _ => apply TraceEqv.refl
49+
| symm _ ih => exact ih.symm
50+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
51+
| compat _ _ ih₁ ih₂ => simp [ih₁.compat ih₂]
7652

7753
theorem proj_eq_of_eqv {u v : List α} [DecidableEq α]
7854
(D : Dependence α) (h : TraceEqv (inducedIndependence D) u v)
@@ -94,15 +70,10 @@ theorem proj_eq_of_eqv {u v : List α} [DecidableEq α]
9470
· simp [filter, ha', hb']
9571
· simp [filter, ha', hb']
9672
· simp [filter, ha', hb']
97-
| refl _ =>
98-
rfl
99-
| symm _ ih =>
100-
exact ih.symm
101-
| trans _ _ ih₁ ih₂ =>
102-
exact ih₁.trans ih₂
103-
| compat _ _ ih₁ ih₂ =>
104-
simp [proj_append]
105-
rw [ih₁, ih₂]
73+
| refl _ => rfl
74+
| symm _ ih => exact ih.symm
75+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
76+
| compat _ _ ih₁ ih₂ => simp [ih₁, ih₂]
10677

10778
theorem cancelRight_congr {u v : List α} [DecidableEq α] (a : α) (h : TraceEqv I u v) :
10879
TraceEqv I (u ÷ a) (v ÷ a) := by
@@ -118,19 +89,14 @@ theorem cancelRight_congr {u v : List α} [DecidableEq α] (a : α) (h : TraceEq
11889
· simp [cancelRight, hab, hac]
11990
apply TraceEqv.swap
12091
exact h_indep
121-
| refl _ =>
122-
apply TraceEqv.refl
123-
| symm _ ih =>
124-
exact ih.symm
125-
| trans _ _ ih₁ ih₂ =>
126-
exact ih₁.trans ih₂
92+
| refl _ => apply TraceEqv.refl
93+
| symm _ ih => exact ih.symm
94+
| trans _ _ ih₁ ih₂ => exact ih₁.trans ih₂
12795
| compat t₁ t₂ ih₁ ih₂ =>
12896
expose_names
12997
by_cases h_mem : a ∈ w₃
130-
· simp [h_mem, (mem_iff_mem a t₂).mp h_mem]
131-
exact t₁.compat ih₂
132-
· simp [h_mem, (mem_iff_mem a t₂).mpr.mt h_mem]
133-
exact ih₁.compat t₂
98+
· simp [h_mem, (mem_iff_mem a t₂).mp h_mem, t₁.compat ih₂]
99+
· simp [h_mem, (mem_iff_mem a t₂).mpr.mt h_mem, ih₁.compat t₂]
134100

135101
theorem erase_congr {u v : List α} [DecidableEq α] (a : α) (h : TraceEqv I u v) :
136102
TraceEqv I (u.erase a) (v.erase a) := by
@@ -163,6 +129,22 @@ theorem append_cancel_middle {l r u v : List α} [DecidableEq α]
163129
TraceEqv I u v :=
164130
append_cancel_left (append_cancel_right h)
165131

132+
instance [DecidableEq α] : CancelMonoid (Trace I) where
133+
mul_left_cancel := by
134+
intro t₁ t₂ t₃
135+
refine Quotient.inductionOn₃ t₁ t₂ t₃ (fun w₁ w₂ w₃ => ?_)
136+
intro heq
137+
apply Quotient.sound
138+
simp only at heq
139+
exact append_cancel_left (Quotient.exact heq)
140+
mul_right_cancel := by
141+
intro t₁ t₂ t₃
142+
refine Quotient.inductionOn₃ t₁ t₂ t₃ (fun w₁ w₂ w₃ => ?_)
143+
intro heq
144+
apply Quotient.sound
145+
simp only at heq
146+
exact append_cancel_right (Quotient.exact heq)
147+
166148
lemma eqv_length_eq_two {a b : α} {w : List α} (h : TraceEqv I [a, b] w) :
167149
w = [a, b] ∨ w = [b, a] := by
168150
rcases length_eq_two.mp (length_eq_of_eqv h).symm with ⟨c, d, rfl⟩
@@ -185,13 +167,11 @@ lemma eqv_singletons {a b : α} (h : TraceEqv I [a] [b]) : a = b := by
185167
generalize hu : ([a] : List α) = u at h
186168
generalize hv : ([b] : List α) = v at h
187169
induction h generalizing a b with
188-
| swap _ _ _ =>
189-
cases hu
170+
| swap _ _ _ => cases hu
190171
| refl _ =>
191172
subst hv
192173
injection hu
193-
| symm _ ih =>
194-
exact (ih hv hu).symm
174+
| symm _ ih => exact (ih hv hu).symm
195175
| trans t₁ t₂ _ _ =>
196176
have ht₁ := mem_iff_mem a t₁
197177
have ht₂ := mem_iff_mem a t₂
@@ -287,8 +267,7 @@ theorem indep_of_comm_singleton {u v w : List α} {a : α} [DecidableEq α]
287267
(h : TraceEqv I (u ++ [a] ++ v) (w ++ [a])) (h_mem : a ∉ v) :
288268
Independent I [a] v := by
289269
induction v using reverseRecOn generalizing w with
290-
| nil =>
291-
simp
270+
| nil => simp
292271
| append_singleton x b ih =>
293272
intro a' ha' b' hb'
294273
simp at ha' hb' h_mem
@@ -308,8 +287,7 @@ theorem indep_of_comm_singleton {u v w : List α} {a : α} [DecidableEq α]
308287
theorem comm_singleton_of_indep {w : List α} {a : α} (h : Independent I [a] w) :
309288
TraceEqv I (w ++ [a]) ([a] ++ w) := by
310289
induction w using reverseRecOn with
311-
| nil =>
312-
apply TraceEqv.refl
290+
| nil => apply TraceEqv.refl
313291
| append_singleton w' b ih =>
314292
simp at h
315293
have haw' : Independent I [a] w' := by

TraceTheory/TraceTheory/Defs.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def mul : Trace I -> Trace I -> Trace I :=
117117
/-- The empty trace, represented by the equivalence class of the empty list. -/
118118
def one := Quotient.mk (TraceSetoid I) []
119119

120-
instance : CancelMonoid (Trace I) where
120+
instance : Monoid (Trace I) where
121121
mul := mul
122122
one := one
123123
mul_assoc := by
@@ -138,8 +138,6 @@ instance : CancelMonoid (Trace I) where
138138
apply Quotient.sound
139139
rw [List.append_nil]
140140
exact TraceEqv.refl _
141-
mul_left_cancel := sorry
142-
mul_right_cancel := sorry
143141

144142
-- TODO better name
145143
/-- The natural homomorphism from the free monoid `List α` to the `TraceMonoid I`. -/

TraceTheory/TraceTheory/DependenceGraph.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import TraceTheory.DependenceMorphism
88

99
open TraceTheory
1010

11-
variable {α : Type} {D : Dependence α}
11+
variable {α : Type*} {D : Dependence α}
1212

1313
variable (D) in
1414
/-- A `DependenceGraph` over `D` is a triple $\gamma=(V,R,\varphi)$ where $V$ is a finite set,

TraceTheory/TraceTheory/List.lean

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ lemma append_singleton_cancelRight {w : List α} {a b : α} :
3838
theorem append_cancelRight {u v : List α} {a : α} :
3939
(u ++ v) ÷ a = if a ∈ v then u ++ (v ÷ a) else (u ÷ a) ++ v := by
4040
induction v using List.reverseRecOn with
41-
| nil =>
42-
simp
41+
| nil => simp
4342
| append_singleton v' b ih =>
4443
rw [← append_assoc, append_singleton_cancelRight]
4544
by_cases heq : b = a <;> by_cases h_mem : a ∈ v'
@@ -55,8 +54,7 @@ theorem singleton_cancelRight {a : α} : [a] ÷ a = [] := by
5554
theorem proj_cancelRight {S : Finset α} {w : List α} {a : α} :
5655
proj S (w ÷ a) = if a ∈ S then proj S w ÷ a else proj S w := by
5756
induction w using List.reverseRecOn with
58-
| nil =>
59-
simp [proj]
57+
| nil => simp [proj]
6058
| append_singleton w' b ih =>
6159
split_ifs with ha <;> by_cases heq : b = a
6260
· simp [heq, ha, proj]
@@ -70,14 +68,13 @@ theorem proj_cancelRight {S : Finset α} {w : List α} {a : α} :
7068
theorem rightmost_occurrence {w : List α} {a : α} (h : a ∈ w) :
7169
∃ w' w'', w = w' ++ [a] ++ w'' ∧ a ∉ w'' := by
7270
induction w using List.reverseRecOn with
73-
| nil =>
74-
contradiction
71+
| nil => contradiction
7572
| append_singleton v b ih =>
7673
by_cases hab : a = b
7774
· use v, []
7875
simp [hab]
79-
· simp [hab] at h
80-
have ⟨w', w'', h_concat, h_in⟩ := ih h
76+
· simp only [mem_append, mem_cons, hab, not_mem_nil, or_self, or_false] at h
77+
rcases ih h with ⟨w', w'', h_concat, h_in⟩
8178
use w', w'' ++ [b]
8279
simp [h_concat, h_in, hab]
8380

@@ -90,8 +87,8 @@ theorem leftmost_occurrence {w : List α} {a : α} (h : a ∈ w) :
9087
by_cases hab : a = b
9188
· use [], v
9289
simp [hab]
93-
· simp [hab] at h
94-
have ⟨w', w'', h_concat, h_in⟩ := ih h
90+
· simp only [mem_cons, hab, false_or] at h
91+
rcases ih h with ⟨w', w'', h_concat, h_in⟩
9592
use [b] ++ w', w''
9693
simp [h_concat, h_in, hab]
9794

@@ -103,20 +100,18 @@ theorem exists_decomp_of_lt_of_len_eq {w x : List α} [LinearOrder α]
103100
x = p ++ [b] ++ x' ∧
104101
a < b := by
105102
induction w generalizing x with
106-
| nil =>
107-
cases x <;> contradiction
103+
| nil => cases x <;> contradiction
108104
| cons a w' ih =>
109105
cases x with
110-
| nil =>
111-
contradiction
106+
| nil => contradiction
112107
| cons b x' =>
113108
cases hlt with
114109
| rel hab =>
115110
use [], a, b, w', x'
116111
exact ⟨rfl, rfl, hab⟩
117112
| cons h_lex =>
118113
simp at h_len
119-
have ⟨p', a', b', w'', x'', hw', hx', hlt'⟩ := ih (List.lex_lt.mp h_lex) h_len
114+
rcases ih (List.lex_lt.mp h_lex) h_len with ⟨p', a', b', w'', x'', hw', hx', hlt'⟩
120115
use a :: p', a', b', w'', x''
121116
simp [hw', hx', hlt']
122117

0 commit comments

Comments
 (0)