-- Mathlib/.lake/packages/batteries/Batteries/Data/String/Lemmas.lean
theorem anyAux_of_valid (p : Char → Bool) : ∀ l m r,
    anyAux ⟨l ++ m ++ r⟩ ⟨utf8Len l + utf8Len m⟩ p ⟨utf8Len l⟩ = m.any p
  | l, [], r => by unfold anyAux; simp
  | l, c::m, r => by
    unfold anyAux
    rw [dif_pos (by exact Nat.lt_add_of_pos_right add_utf8Size_pos)]
    simp only [List.append_assoc, List.cons_append, get_of_valid l (c :: (m ++ r)),
      Char.reduceDefault, List.headD_cons, utf8Len_cons, next_of_valid l c (m ++ r),
      Bool.if_true_left, Bool.decide_eq_true, List.any_cons]
    cases p c <;> simp
    simpa [← Nat.add_assoc, Nat.add_right_comm] using anyAux_of_valid p (l++[c]) m r

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then true
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.anyAux { byteIdx := utf8Len l + utf8Len (c :: m) } p
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    (c :: m).any p

Tactic:
simp only [List.append_assoc, List.cons_append, get_of_valid l (c :: (m ++ r)),
  Char.reduceDefault, List.headD_cons, utf8Len_cons, next_of_valid l c (m ++ r),
  Bool.if_true_left, Bool.decide_eq_true, List.any_cons]

Result:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (p c ||
      { data := l ++ c :: (m ++ r) }.anyAux { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) } p
        { byteIdx := utf8Len l + c.utf8Size }) =
    (p c || m.any p)

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ { byteIdx := utf8Len l } < { byteIdx := utf8Len l + utf8Len (c :: m) }

Tactic:
exact Nat.lt_add_of_pos_right add_utf8Size_pos

Result:
no goals

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (if h : { byteIdx := utf8Len l } < { byteIdx := utf8Len l + utf8Len (c :: m) } then
      if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then true
      else
        let_fun this := ⋯;
        { data := l ++ c :: m ++ r }.anyAux { byteIdx := utf8Len l + utf8Len (c :: m) } p
          ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })
    else false) =
    (c :: m).any p

Tactic:
rw [dif_pos (by exact Nat.lt_add_of_pos_right add_utf8Size_pos)]

Result:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then true
    else
      let_fun this := ⋯;
      { data := l ++ c :: m ++ r }.anyAux { byteIdx := utf8Len l + utf8Len (c :: m) } p
        ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })) =
    (c :: m).any p

Proof state:
p : Char → Bool
l r : List Char
⊢ { data := l ++ [] ++ r }.anyAux { byteIdx := utf8Len l + utf8Len [] } p { byteIdx := utf8Len l } = [].any p

Tactic:
unfold anyAux

Result:
p : Char → Bool
l r : List Char
⊢ (if h : { byteIdx := utf8Len l } < { byteIdx := utf8Len l + utf8Len [] } then
      if p ({ data := l ++ [] ++ r }.get { byteIdx := utf8Len l }) = true then true
      else
        let_fun this := ⋯;
        { data := l ++ [] ++ r }.anyAux { byteIdx := utf8Len l + utf8Len [] } p
          ({ data := l ++ [] ++ r }.next { byteIdx := utf8Len l })
    else false) =
    [].any p

Proof state:
p : Char → Bool
l r : List Char
⊢ (if h : { byteIdx := utf8Len l } < { byteIdx := utf8Len l + utf8Len [] } then
      if p ({ data := l ++ [] ++ r }.get { byteIdx := utf8Len l }) = true then true
      else
        let_fun this := ⋯;
        { data := l ++ [] ++ r }.anyAux { byteIdx := utf8Len l + utf8Len [] } p
          ({ data := l ++ [] ++ r }.next { byteIdx := utf8Len l })
    else false) =
    [].any p

Tactic:
simp

Result:
no goals

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ { data := l ++ c :: m ++ r }.anyAux { byteIdx := utf8Len l + utf8Len (c :: m) } p { byteIdx := utf8Len l } =
    (c :: m).any p

Tactic:
unfold anyAux

Result:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (if h : { byteIdx := utf8Len l } < { byteIdx := utf8Len l + utf8Len (c :: m) } then
      if p ({ data := l ++ c :: m ++ r }.get { byteIdx := utf8Len l }) = true then true
      else
        let_fun this := ⋯;
        { data := l ++ c :: m ++ r }.anyAux { byteIdx := utf8Len l + utf8Len (c :: m) } p
          ({ data := l ++ c :: m ++ r }.next { byteIdx := utf8Len l })
    else false) =
    (c :: m).any p

Proof state:
case false
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ { data := l ++ c :: (m ++ r) }.anyAux { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) } p
      { byteIdx := utf8Len l + c.utf8Size } =
    m.any p

Tactic:
simpa [← Nat.add_assoc, Nat.add_right_comm] using anyAux_of_valid p (l++[c]) m r

Result:
no goals

Proof state:
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ (p c ||
      { data := l ++ c :: (m ++ r) }.anyAux { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) } p
        { byteIdx := utf8Len l + c.utf8Size }) =
    (p c || m.any p)

Tactic:
cases p c <;> simp

Result:
case false
p : Char → Bool
l : List Char
c : Char
m r : List Char
⊢ { data := l ++ c :: (m ++ r) }.anyAux { byteIdx := utf8Len l + (utf8Len m + c.utf8Size) } p
      { byteIdx := utf8Len l + c.utf8Size } =
    m.any p