-- Mathlib/.lake/packages/batteries/Batteries/Data/String/Lemmas.lean
theorem revFindAux_of_valid (p) : ∀ l r,
    revFindAux ⟨l.reverse ++ r⟩ p ⟨utf8Len l⟩ = (l.dropWhile (!p ·)).tail?.map (⟨utf8Len ·⟩)
  | [], r => by unfold revFindAux List.dropWhile; simp
  | c::l, r => by
    unfold revFindAux List.dropWhile
    rw [dif_neg (by exact Pos.ne_of_gt add_utf8Size_pos)]
    have h1 := get_of_valid l.reverse (c::r); have h2 := prev_of_valid l.reverse c r
    simp only [utf8Len_reverse, Char.reduceDefault, List.headD_cons] at h1 h2
    simp only [List.reverse_cons, List.append_assoc, List.singleton_append, utf8Len_cons, h2, h1]
    cases p c <;> simp only [Bool.false_eq_true, ↓reduceIte, Bool.not_false, Bool.not_true,
      List.tail?_cons, Option.map_some']
    exact revFindAux_of_valid p l (c::r)

Proof state:
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l.reverse } = (c :: r).headD default
h2 :
  { data := l.reverse ++ c :: r }.prev { byteIdx := utf8Len l.reverse + c.utf8Size } = { byteIdx := utf8Len l.reverse }
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

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

Result:
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l } = c
h2 : { data := l.reverse ++ c :: r }.prev { byteIdx := utf8Len l + c.utf8Size } = { byteIdx := utf8Len l }
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Proof state:
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l.reverse } = (c :: r).headD default
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Tactic:
have h2 := prev_of_valid l.reverse c r

Result:
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l.reverse } = (c :: r).headD default
h2 :
  { data := l.reverse ++ c :: r }.prev { byteIdx := utf8Len l.reverse + c.utf8Size } = { byteIdx := utf8Len l.reverse }
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Proof state:
p : Char → Bool
c : Char
l r : List Char
⊢ { data := (c :: l).reverse ++ r }.revFindAux p { byteIdx := utf8Len (c :: l) } =
    Option.map (fun x => { byteIdx := utf8Len x }) (List.dropWhile (fun x => !p x) (c :: l)).tail?

Tactic:
unfold revFindAux List.dropWhile

Result:
p : Char → Bool
c : Char
l r : List Char
⊢ (if h : { byteIdx := utf8Len (c :: l) } = 0 then none
    else
      let_fun this := ⋯;
      let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
      if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
      else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Proof state:
p : Char → Bool
r : List Char
⊢ (if h : { byteIdx := utf8Len [] } = 0 then none
    else
      let_fun this := ⋯;
      let pos := { data := [].reverse ++ r }.prev { byteIdx := utf8Len [] };
      if p ({ data := [].reverse ++ r }.get pos) = true then some pos
      else { data := [].reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x }) [].tail?

Tactic:
simp

Result:
no goals

Proof state:
p : Char → Bool
c : Char
l r : List Char
⊢ ¬{ byteIdx := utf8Len (c :: l) } = 0

Tactic:
exact Pos.ne_of_gt add_utf8Size_pos

Result:
no goals

Proof state:
p : Char → Bool
c : Char
l r : List Char
⊢ (if h : { byteIdx := utf8Len (c :: l) } = 0 then none
    else
      let_fun this := ⋯;
      let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
      if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
      else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Tactic:
rw [dif_neg (by exact Pos.ne_of_gt add_utf8Size_pos)]

Result:
p : Char → Bool
c : Char
l r : List Char
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Proof state:
p : Char → Bool
c : Char
l r : List Char
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Tactic:
have h1 := get_of_valid l.reverse (c::r)

Result:
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l.reverse } = (c :: r).headD default
⊢ (let_fun this := ⋯;
    let pos := { data := (c :: l).reverse ++ r }.prev { byteIdx := utf8Len (c :: l) };
    if p ({ data := (c :: l).reverse ++ r }.get pos) = true then some pos
    else { data := (c :: l).reverse ++ r }.revFindAux p pos) =
    Option.map (fun x => { byteIdx := utf8Len x })
      (match !p c with
        | true => List.dropWhile (fun x => !p x) l
        | false => c :: l).tail?

Proof state:
case false
p : Char → Bool
c : Char
l r : List Char
h1 : { data := l.reverse ++ c :: r }.get { byteIdx := utf8Len l } = c
h2 : { data := l.reverse ++ c :: r }.prev { byteIdx := utf8Len l + c.utf8Size } = { byteIdx := utf8Len l }
⊢ { data := l.reverse ++ c :: r }.revFindAux p { byteIdx := utf8Len l } =
    Option.map (fun x => { byteIdx := utf8Len x }) (List.dropWhile (fun x => !p x) l).tail?

Tactic:
exact revFindAux_of_valid p l (c::r)

Result:
no goals