-- Mathlib/.lake/packages/lean4/src/lean/Init/Internal/Order/Lemmas.lean
theorem monotone_findM?
    {m : Type → Type} [Monad m] [∀ α, PartialOrder (m α)] [MonoBind m] {α : Type}
    (f : γ → α → m Bool) (xs : Array α) (hmono : monotone f) :
    monotone (fun x => xs.findM? (f x))

Proof state:
case hmono₁
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x =>
    forIn xs ⟨none, PUnit.unit⟩ fun a r =>
      let r := r.snd;
      do
      let __do_lift ← f x a
      if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
        else do
          pure PUnit.unit
          pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Tactic:
apply monotone_forIn

Result:
case hmono₁.hmono
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x a r =>
    let r := r.snd;
    do
    let __do_lift ← f x a
    if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
      else do
        pure PUnit.unit
        pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Proof state:
case hmono₁.hmono.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
⊢ monotone fun x r =>
    let r := r.snd;
    do
    let __do_lift ← f x y
    if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
      else do
        pure PUnit.unit
        pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Tactic:
apply monotone_of_monotone_apply

Result:
case hmono₁.hmono.h.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
⊢ ∀ (y_1 : MProd (Option (Option α)) PUnit),
    monotone fun x =>
      let r := y_1.snd;
      do
      let __do_lift ← f x y
      if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
        else do
          pure PUnit.unit
          pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Proof state:
case hmono₁.hmono.h.h.hmono₂
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
r : MProd (Option (Option α)) PUnit
⊢ monotone fun x __do_lift =>
    if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
    else do
      pure PUnit.unit
      pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Tactic:
apply monotone_const

Result:
no goals

Proof state:
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x => Array.findM? (f x) xs

Tactic:
unfold Array.findM?

Result:
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x => do
    let r ←
      forIn xs ⟨none, PUnit.unit⟩ fun a r =>
          let r := r.snd;
          do
          let __do_lift ← f x a
          if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
            else do
              pure PUnit.unit
              pure (ForInStep.yield ⟨none, PUnit.unit⟩)
    let __do_jp : PUnit → m (Option α) := fun y => pure none
    match r.fst with
      | none => do
        let y ← pure PUnit.unit
        __do_jp y
      | some a => pure a

Proof state:
case hmono₁.hmono.h.h.hmono₁.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
r : MProd (Option (Option α)) PUnit
⊢ monotone f

Tactic:
apply hmono

Result:
no goals

Proof state:
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x => do
    let r ←
      forIn xs ⟨none, PUnit.unit⟩ fun a r =>
          let r := r.snd;
          do
          let __do_lift ← f x a
          if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
            else do
              pure PUnit.unit
              pure (ForInStep.yield ⟨none, PUnit.unit⟩)
    let __do_jp : PUnit → m (Option α) := fun y => pure none
    match r.fst with
      | none => do
        let y ← pure PUnit.unit
        __do_jp y
      | some a => pure a

Tactic:
apply monotone_bind

Result:
case hmono₁
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x =>
    forIn xs ⟨none, PUnit.unit⟩ fun a r =>
      let r := r.snd;
      do
      let __do_lift ← f x a
      if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
        else do
          pure PUnit.unit
          pure (ForInStep.yield ⟨none, PUnit.unit⟩)

case hmono₂
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x r =>
    let __do_jp := fun y => pure none;
    match r.fst with
    | none => do
      let y ← pure PUnit.unit
      __do_jp y
    | some a => pure a

Proof state:
case hmono₁.hmono
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ monotone fun x a r =>
    let r := r.snd;
    do
    let __do_lift ← f x a
    if __do_lift = true then pure (ForInStep.done ⟨some (some a), PUnit.unit⟩)
      else do
        pure PUnit.unit
        pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Tactic:
apply monotone_of_monotone_apply

Result:
case hmono₁.hmono.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
⊢ ∀ (y : α),
    monotone fun x r =>
      let r := r.snd;
      do
      let __do_lift ← f x y
      if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
        else do
          pure PUnit.unit
          pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Proof state:
case hmono₁.hmono.h.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
⊢ ∀ (y_1 : MProd (Option (Option α)) PUnit),
    monotone fun x =>
      let r := y_1.snd;
      do
      let __do_lift ← f x y
      if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
        else do
          pure PUnit.unit
          pure (ForInStep.yield ⟨none, PUnit.unit⟩)

Tactic:
intro r

Result:
case hmono₁.hmono.h.h
γ : Type w
inst✝³ : PartialOrder γ
m : Type → Type
inst✝² : Monad m
inst✝¹ : (α : Type) → PartialOrder (m α)
inst✝ : MonoBind m
α : Type
f : γ → α → m Bool
xs : Array α
hmono : monotone f
y : α
r : MProd (Option (Option α)) PUnit
⊢ monotone fun x =>
    let r := r.snd;
    do
    let __do_lift ← f x y
    if __do_lift = true then pure (ForInStep.done ⟨some (some y), PUnit.unit⟩)
      else do
        pure PUnit.unit
        pure (ForInStep.yield ⟨none, PUnit.unit⟩)