-- Mathlib/.lake/packages/batteries/Batteries/Data/String/Lemmas.lean
theorem findAux_of_valid (p) : ∀ l m r,
    findAux ⟨l ++ m ++ r⟩ p ⟨utf8Len l + utf8Len m⟩ ⟨utf8Len l⟩ =
    ⟨utf8Len l + utf8Len (m.takeWhile (!p ·))⟩
  | l, [], r => by unfold findAux List.takeWhile; simp
  | l, c::m, r => by
    unfold findAux List.takeWhile
    rw [dif_pos (by exact Nat.lt_add_of_pos_right add_utf8Size_pos)]
    have h1 := get_of_valid l (c::m++r); have h2 := next_of_valid l c (m++r)
    simp only [List.cons_append, Char.reduceDefault, List.headD_cons] at h1 h2
    simp only [List.append_assoc, List.cons_append, h1, utf8Len_cons, h2]
    cases p c
    · simp only [Bool.false_eq_true, ↓reduceIte, Bool.not_false, utf8Len_cons]
      have foo := findAux_of_valid p (l++[c]) m r
      simp only [List.append_assoc, List.singleton_append, List.cons_append, utf8Len_append,
        utf8Len_cons, utf8Len_nil, Nat.zero_add, List.nil_append] at foo
      rw [Nat.add_right_comm, Nat.add_assoc] at foo
      rw [foo, Nat.add_right_comm, Nat.add_assoc]
    · simp

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ (c :: m ++ r) }.get { byteIdx := utf8Len l } = (c :: m ++ r).headD default
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then { byteIdx := utf8Len l }
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.findAux p { byteIdx := utf8Len l + utf8Len (c :: m) }
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !p c with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Tactic:
have h2 := next_of_valid l c (m++r)

Result:
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ (c :: m ++ r) }.get { byteIdx := utf8Len l } = (c :: m ++ r).headD default
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then { byteIdx := utf8Len l }
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.findAux p { byteIdx := utf8Len l + utf8Len (c :: m) }
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !p c with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ (c :: m ++ r) }.get { byteIdx := utf8Len l } = (c :: m ++ r).headD default
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then { byteIdx := utf8Len l }
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.findAux p { byteIdx := utf8Len l + utf8Len (c :: m) }
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !p c with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Tactic:
simp only [List.cons_append, Char.reduceDefault, List.headD_cons] at h1 h2

Result:
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ c :: (m ++ r) }.get { byteIdx := utf8Len l } = c
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then { byteIdx := utf8Len l }
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.findAux p { byteIdx := utf8Len l + utf8Len (c :: m) }
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !p c with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ c :: (m ++ r) }.get { byteIdx := utf8Len l } = c
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if p c = true then { byteIdx := utf8Len l }
    else
      { data := l ++ c :: (m ++ r) }.findAux p { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) }
        { byteIdx := utf8Len l + c.utf8Size }) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !p c with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Tactic:
cases p c

Result:
case false
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ c :: (m ++ r) }.get { byteIdx := utf8Len l } = c
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if false = true then { byteIdx := utf8Len l }
    else
      { data := l ++ c :: (m ++ r) }.findAux p { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) }
        { byteIdx := utf8Len l + c.utf8Size }) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !false with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

case true
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ c :: (m ++ r) }.get { byteIdx := utf8Len l } = c
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
⊢ (if true = true then { byteIdx := utf8Len l }
    else
      { data := l ++ c :: (m ++ r) }.findAux p { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) }
        { byteIdx := utf8Len l + c.utf8Size }) =
    {
      byteIdx :=
        utf8Len l +
          utf8Len
            (match !true with
            | true => c :: List.takeWhile (fun x => !p x) m
            | false => []) }

Proof state:
case false
p : Char → Bool
l : List Char
c : Char
m r : List Char
h1 : { data := l ++ c :: (m ++ r) }.get { byteIdx := utf8Len l } = c
h2 : { data := l ++ c :: (m ++ r) }.next { byteIdx := utf8Len l } = { byteIdx := utf8Len l + c.utf8Size }
foo :
  { data := l ++ c :: (m ++ r) }.findAux p { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) }
      { byteIdx := utf8Len l + c.utf8Size } =
    { byteIdx := utf8Len l + c.utf8Size + utf8Len (List.takeWhile (fun x => !p x) m) }
⊢ { data := l ++ c :: (m ++ r) }.findAux p { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) }
      { byteIdx := utf8Len l + c.utf8Size } =
    { byteIdx := utf8Len l + (utf8Len (List.takeWhile (fun x => !p x) m) + c.utf8Size) }

Tactic:
rw [foo, Nat.add_right_comm, Nat.add_assoc]

Result:
no goals