Skip to content

Commit 6bbd26f

Browse files
committed
feat: add splitOnP_append_cons (#31825)
This PR adds a lemma for `splitOnP`, allowing expressions where a `splitOnP` on a concatenation to be better simplified. Also golfs a relevant consequence with the new lemma. Related to google-deepmind/formal-conjectures#1120
1 parent 28539d1 commit 6bbd26f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Mathlib/Data/List/SplitOn.lean

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,22 @@ theorem splitOnP_eq_single (h : ∀ x ∈ xs, ¬p x) : xs.splitOnP p = [xs] := b
7676
simp only [splitOnP_cons, h hd mem_cons_self, if_false, Bool.false_eq_true,
7777
modifyHead_cons, ih <| forall_mem_of_forall_mem_cons h]
7878

79+
/-- When a list of the form `[...xs, sep, ...as]` is split at the `sep` element satisfying `p`,
80+
the result is the concatenation of `splitOnP` called on `xs` and `as` -/
81+
theorem splitOnP_append_cons (xs as : List α) (sep : α) (hsep : p sep) :
82+
(xs ++ sep :: as).splitOnP p = List.splitOnP p xs ++ List.splitOnP p as := by
83+
induction xs with
84+
| nil => simp [hsep]
85+
| cons hd tl ih =>
86+
obtain ⟨hd1, tl1, h1'⟩ := List.exists_cons_of_ne_nil (List.splitOnP_ne_nil p tl)
87+
by_cases hPh : p hd <;> simp [*]
88+
7989
/-- When a list of the form `[...xs, sep, ...as]` is split on `p`, the first element is `xs`,
8090
assuming no element in `xs` satisfies `p` but `sep` does satisfy `p` -/
81-
theorem splitOnP_first (h : ∀ x ∈ xs, ¬p x) (sep : α) (hsep : p sep) (as : List α) :
91+
theorem splitOnP_first (h : ∀ x ∈ xs, ¬p x) (sep : α) (hsep : p sep = true) (as : List α) :
8292
(xs ++ sep :: as).splitOnP p = xs :: as.splitOnP p := by
83-
induction xs with
84-
| nil => simp [hsep]
85-
| cons hd tl ih => simp [h hd _, ih <| forall_mem_of_forall_mem_cons h]
93+
rw [splitOnP_append_cons p xs as sep hsep, splitOnP_eq_single p xs h]
94+
rfl
8695

8796
/-- `intercalate [x]` is the left inverse of `splitOn x` -/
8897
theorem intercalate_splitOn (x : α) [DecidableEq α] : [x].intercalate (xs.splitOn x) = xs := by

0 commit comments

Comments
 (0)