Skip to content

Commit 25190fa

Browse files
committed
refactor(Data/Seq): mark Seq.recOn with cases_eliminator (#19829)
Mark `Seq.recOn` with `cases_eliminator` to enable using `cases` tactic with sequences.
1 parent aaf3fad commit 25190fa

2 files changed

Lines changed: 57 additions & 73 deletions

File tree

Mathlib/Data/Seq/Parallel.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ theorem terminates_parallel {S : WSeq (Computation α)} {c} (h : c ∈ S) [T : T
157157
have TT : ∀ l', Terminates (corec parallel.aux1 (l', S.tail)) := by
158158
intro
159159
apply IH _ _ _ (Or.inr _) T
160-
rw [a]
161-
cases' S with f al
162-
rfl
160+
rw [a, Seq.get?_tail]
163161
induction' e : Seq.get? S 0 with o
164162
· have D : Seq.destruct S = none := by
165163
dsimp [Seq.destruct]

Mathlib/Data/Seq/Seq.lean

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,16 @@ theorem get?_tail (s : Seq α) (n) : get? (tail s) n = get? s (n + 1) :=
230230
rfl
231231

232232
/-- Recursion principle for sequences, compare with `List.recOn`. -/
233-
def recOn {C : Seq α → Sort v} (s : Seq α) (h1 : C nil) (h2 : ∀ x s, C (cons x s)) :
234-
C s := by
233+
@[cases_eliminator]
234+
def recOn {motive : Seq α → Sort v} (s : Seq α) (nil : motive nil)
235+
(cons : ∀ x s, motive (cons x s)) :
236+
motive s := by
235237
cases' H : destruct s with v
236238
· rw [destruct_eq_nil H]
237-
apply h1
239+
apply nil
238240
· cases' v with a s'
239241
rw [destruct_eq_cons H]
240-
apply h2
242+
apply cons
241243

242244
theorem mem_rec_on {C : Seq α → Prop} {a s} (M : a ∈ s)
243245
(h1 : ∀ b s', a = b ∨ C s' → C (cons b s')) : C s := by
@@ -251,10 +253,9 @@ theorem mem_rec_on {C : Seq α → Prop} {a s} (M : a ∈ s)
251253
rw [TH]
252254
apply h1 _ _ (Or.inl rfl)
253255
-- Porting note: had to reshuffle `intro`
254-
revert e; apply s.recOn _ fun b s' => _
255-
· intro e; injection e
256-
· intro b s' e
257-
have h_eq : (cons b s').val (Nat.succ k) = s'.val k := by cases s'; rfl
256+
cases' s with b s'
257+
· injection e
258+
· have h_eq : (cons b s').val (Nat.succ k) = s'.val k := by cases s' using Subtype.recOn; rfl
258259
rw [h_eq] at e
259260
apply h1 _ _ (Or.inr (IH e))
260261

@@ -331,20 +332,21 @@ theorem eq_of_bisim (bisim : IsBisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s
331332
match t₁, t₂, e with
332333
| _, _, ⟨s, s', rfl, rfl, r⟩ => by
333334
suffices head s = head s' ∧ R (tail s) (tail s') from
334-
And.imp id (fun r => ⟨tail s, tail s', by cases s; rfl, by cases s'; rfl, r⟩) this
335+
And.imp id (fun r => ⟨tail s, tail s', by cases s using Subtype.recOn; rfl,
336+
by cases s' using Subtype.recOn; rfl, r⟩) this
335337
have := bisim r; revert r this
336-
apply recOn s _ _ <;> apply recOn s' _ _
338+
cases' s with x s <;> cases' s' with x' s'
337339
· intro r _
338340
constructor
339341
· rfl
340342
· assumption
341-
· intro x s _ this
343+
· intro _ this
342344
rw [destruct_nil, destruct_cons] at this
343345
exact False.elim this
344-
· intro x s _ this
346+
· intro _ this
345347
rw [destruct_nil, destruct_cons] at this
346348
exact False.elim this
347-
· intro x s x' s' _ this
349+
· intro _ this
348350
rw [destruct_cons, destruct_cons] at this
349351
rw [head_cons, head_cons, tail_cons, tail_cons]
350352
cases' this with h1 h2
@@ -568,10 +570,10 @@ def toListOrStream (s : Seq α) [Decidable s.Terminates] : List α ⊕ Stream'
568570
theorem nil_append (s : Seq α) : append nil s = s := by
569571
apply coinduction2; intro s
570572
dsimp [append]; rw [corec_eq]
571-
dsimp [append]; apply recOn s _ _
573+
dsimp [append]
574+
cases' s with x s
572575
· trivial
573-
· intro x s
574-
rw [destruct_cons]
576+
· rw [destruct_cons]
575577
dsimp
576578
exact ⟨rfl, s, rfl, rfl⟩
577579

@@ -718,10 +720,9 @@ theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) :
718720
@[simp]
719721
theorem append_nil (s : Seq α) : append s nil = s := by
720722
apply coinduction2 s; intro s
721-
apply recOn s _ _
723+
cases' s with x s
722724
· trivial
723-
· intro x s
724-
rw [cons_append, destruct_cons, destruct_cons]
725+
· rw [cons_append, destruct_cons, destruct_cons]
725726
dsimp
726727
exact ⟨rfl, s, rfl, rfl⟩
727728

@@ -732,15 +733,12 @@ theorem append_assoc (s t u : Seq α) : append (append s t) u = append s (append
732733
exact
733734
match s1, s2, h with
734735
| _, _, ⟨s, t, u, rfl, rfl⟩ => by
735-
apply recOn s <;> simp
736-
· apply recOn t <;> simp
737-
· apply recOn u <;> simp
738-
· intro _ u
739-
refine ⟨nil, nil, u, ?_, ?_⟩ <;> simp
740-
· intro _ t
741-
refine ⟨nil, t, u, ?_, ?_⟩ <;> simp
742-
· intro _ s
743-
exact ⟨s, t, u, rfl, rfl⟩
736+
cases' s with _ s <;> simp
737+
· cases' t with _ t <;> simp
738+
· cases' u with _ u <;> simp
739+
· refine ⟨nil, nil, u, ?_, ?_⟩ <;> simp
740+
· refine ⟨nil, t, u, ?_, ?_⟩ <;> simp
741+
· exact ⟨s, t, u, rfl, rfl⟩
744742
· exact ⟨s, t, u, rfl, rfl⟩
745743

746744
@[simp]
@@ -776,12 +774,10 @@ theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s)
776774
exact
777775
match s1, s2, h with
778776
| _, _, ⟨s, t, rfl, rfl⟩ => by
779-
apply recOn s <;> simp
780-
· apply recOn t <;> simp
781-
· intro _ t
782-
refine ⟨nil, t, ?_, ?_⟩ <;> simp
783-
· intro _ s
784-
exact ⟨s, t, rfl, rfl⟩
777+
cases' s with _ s <;> simp
778+
· cases' t with _ t <;> simp
779+
· refine ⟨nil, t, ?_, ?_⟩ <;> simp
780+
· exact ⟨s, t, rfl, rfl⟩
785781

786782
@[simp]
787783
theorem map_get? (f : α → β) : ∀ s n, get? (map f s) n = (get? s n).map f
@@ -835,15 +831,13 @@ theorem join_cons (a : α) (s S) : join (cons (a, s) S) = cons a (append s (join
835831
exact
836832
match s1, s2, h with
837833
| s, _, Or.inl <| Eq.refl s => by
838-
apply recOn s; · trivial
839-
· intro x s
840-
rw [destruct_cons]
834+
cases' s with x s; · trivial
835+
· rw [destruct_cons]
841836
exact ⟨rfl, Or.inl rfl⟩
842837
| _, _, Or.inr ⟨a, s, S, rfl, rfl⟩ => by
843-
apply recOn s
838+
cases' s with x s
844839
· simp [join_cons_cons, join_cons_nil]
845-
· intro x s
846-
simpa [join_cons_cons, join_cons_nil] using Or.inr ⟨x, s, S, rfl, rfl⟩
840+
· simpa [join_cons_cons, join_cons_nil] using Or.inr ⟨x, s, S, rfl, rfl⟩
847841

848842
@[simp]
849843
theorem join_append (S T : Seq (Seq1 α)) : join (append S T) = append (join S) (join T) := by
@@ -854,18 +848,15 @@ theorem join_append (S T : Seq (Seq1 α)) : join (append S T) = append (join S)
854848
exact
855849
match s1, s2, h with
856850
| _, _, ⟨s, S, T, rfl, rfl⟩ => by
857-
apply recOn s <;> simp
858-
· apply recOn S <;> simp
859-
· apply recOn T
851+
cases' s with _ s <;> simp
852+
· cases' S with s S <;> simp
853+
· cases' T with s T
860854
· simp
861-
· intro s T
862-
cases' s with a s; simp only [join_cons, destruct_cons, true_and]
855+
· cases' s with a s; simp only [join_cons, destruct_cons, true_and]
863856
refine ⟨s, nil, T, ?_, ?_⟩ <;> simp
864-
· intro s S
865-
cases' s with a s
857+
· cases' s with a s
866858
simpa using ⟨s, S, T, rfl, rfl⟩
867-
· intro _ s
868-
exact ⟨s, S, T, rfl, rfl⟩
859+
· exact ⟨s, S, T, rfl, rfl⟩
869860
· refine ⟨nil, S, T, ?_, ?_⟩ <;> simp
870861

871862
@[simp]
@@ -920,11 +911,11 @@ theorem of_mem_append {s₁ s₂ : Seq α} {a : α} (h : a ∈ append s₁ s₂)
920911
generalize e : append s₁ s₂ = ss; intro h; revert s₁
921912
apply mem_rec_on h _
922913
intro b s' o s₁
923-
apply s₁.recOn _ fun c t₁ => _
914+
cases' s₁ with c t₁
924915
· intro m _
925916
apply Or.inr
926917
simpa using m
927-
· intro c t₁ m e
918+
· intro m e
928919
have this := congr_arg destruct e
929920
cases' show a = c ∨ a ∈ append t₁ s₂ by simpa using m with e' m
930921
· rw [e']
@@ -1002,7 +993,7 @@ def bind (s : Seq1 α) (f : α → Seq1 β) : Seq1 β :=
1002993

1003994
@[simp]
1004995
theorem join_map_ret (s : Seq α) : Seq.join (Seq.map ret s) = s := by
1005-
apply coinduction2 s; intro s; apply recOn s <;> simp [ret]
996+
apply coinduction2 s; intro s; cases s <;> simp [ret]
1006997

1007998
@[simp]
1008999
theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s
@@ -1016,7 +1007,7 @@ theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s
10161007
theorem ret_bind (a : α) (f : α → Seq1 β) : bind (ret a) f = f a := by
10171008
simp only [bind, map, ret.eq_1, map_nil]
10181009
cases' f a with a s
1019-
apply recOn s <;> intros <;> simp
1010+
cases s <;> simp
10201011

10211012
@[simp]
10221013
theorem map_join' (f : α → β) (S) : Seq.map f (Seq.join S) = Seq.join (Seq.map (map f) S) := by
@@ -1028,18 +1019,16 @@ theorem map_join' (f : α → β) (S) : Seq.map f (Seq.join S) = Seq.join (Seq.m
10281019
exact
10291020
match s1, s2, h with
10301021
| _, _, ⟨s, S, rfl, rfl⟩ => by
1031-
apply recOn s <;> simp
1032-
· apply recOn S <;> simp
1033-
· intro x S
1034-
cases' x with a s
1022+
cases' s with _ s <;> simp
1023+
· cases' S with x S <;> simp
1024+
· cases' x with a s
10351025
simpa [map] using ⟨_, _, rfl, rfl⟩
1036-
· intro _ s
1037-
exact ⟨s, S, rfl, rfl⟩
1026+
· exact ⟨s, S, rfl, rfl⟩
10381027
· refine ⟨nil, S, ?_, ?_⟩ <;> simp
10391028

10401029
@[simp]
10411030
theorem map_join (f : α → β) : ∀ S, map f (join S) = join (map (map f) S)
1042-
| ((a, s), S) => by apply recOn s <;> intros <;> simp [map]
1031+
| ((a, s), S) => by cases s <;> simp [map]
10431032

10441033
@[simp]
10451034
theorem join_join (SS : Seq (Seq1 (Seq1 α))) :
@@ -1052,17 +1041,14 @@ theorem join_join (SS : Seq (Seq1 (Seq1 α))) :
10521041
exact
10531042
match s1, s2, h with
10541043
| _, _, ⟨s, SS, rfl, rfl⟩ => by
1055-
apply recOn s <;> simp
1056-
· apply recOn SS <;> simp
1057-
· intro S SS
1058-
cases' S with s S; cases' s with x s
1044+
cases' s with _ s <;> simp
1045+
· cases' SS with S SS <;> simp
1046+
· cases' S with s S; cases' s with x s
10591047
simp only [Seq.join_cons, join_append, destruct_cons]
1060-
apply recOn s <;> simp
1048+
cases' s with x s <;> simp
10611049
· exact ⟨_, _, rfl, rfl⟩
1062-
· intro x s
1063-
refine ⟨Seq.cons x (append s (Seq.join S)), SS, ?_, ?_⟩ <;> simp
1064-
· intro _ s
1065-
exact ⟨s, SS, rfl, rfl⟩
1050+
· refine ⟨Seq.cons x (append s (Seq.join S)), SS, ?_, ?_⟩ <;> simp
1051+
· exact ⟨s, SS, rfl, rfl⟩
10661052
· refine ⟨nil, SS, ?_, ?_⟩ <;> simp
10671053

10681054
@[simp]
@@ -1080,7 +1066,7 @@ theorem bind_assoc (s : Seq1 α) (f : α → Seq1 β) (g : β → Seq1 γ) :
10801066
-- give names to variables.
10811067
induction' s using recOn with x s_1 <;> induction' S using recOn with x_1 s_2 <;> simp
10821068
· cases' x_1 with x t
1083-
apply recOn t <;> intros <;> simp
1069+
cases t <;> simp
10841070
· cases' x_1 with y t; simp
10851071

10861072
instance monad : Monad Seq1 where

0 commit comments

Comments
 (0)