Theorem: exists_increasing_or_nonincreasing_subseq'
File: Mathlib/Order/OrderIsoNat.lean

tactic: let bad : Set ℕ := { m | ∀ n, m < n → ¬r (f m) (f n) }
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: by_cases hbad : Infinite bad
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: haveI := hbad
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Infinite ↑bad
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: refine' ⟨Nat.orderEmbeddingOfSet bad, Or.intro_right _ fun m n mn => _⟩
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad this : Infinite ↑bad
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: have h := @Set.mem_range_self _ _ ↑(Nat.orderEmbeddingOfSet bad) m
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad this : Infinite ↑bad
m n : ℕ
mn : m < n
⊢ ¬r (f (↑(Nat.orderEmbeddingOfSet bad) m)) (f (↑(Nat.orderEmbeddingOfSet bad) n))

tactic: rw [Nat.orderEmbeddingOfSet_range bad] at h
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad this : Infinite ↑bad
m n : ℕ
mn : m < n
h : ↑(Nat.orderEmbeddingOfSet bad) m ∈ Set.range ↑(Nat.orderEmbeddingOfSet bad)
⊢ ¬r (f (↑(Nat.orderEmbeddingOfSet bad) m)) (f (↑(Nat.orderEmbeddingOfSet bad) n))

tactic: exact h _ ((OrderEmbedding.lt_iff_lt _).2 mn)
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad this : Infinite ↑bad
m n : ℕ
mn : m < n
h : ↑(Nat.orderEmbeddingOfSet bad) m ∈ bad
⊢ ¬r (f (↑(Nat.orderEmbeddingOfSet bad) m)) (f (↑(Nat.orderEmbeddingOfSet bad) n))

tactic: rw [Set.infinite_coe_iff, Set.Infinite, not_not] at hbad
state_before: case neg
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : ¬Infinite ↑bad
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: have h : ∀ n : ℕ, ∃ n' : ℕ, n < n' ∧ r (f (n + m)) (f (n' + m)) := by
  intro n
  have h := hm _ (le_add_of_nonneg_left n.zero_le)
  simp only [exists_prop, not_not, Set.mem_setOf_eq, not_forall] at h
  obtain ⟨n', hn1, hn2⟩ := h
  obtain ⟨x, hpos, rfl⟩ := exists_pos_add_of_lt hn1
  refine' ⟨n + x, add_lt_add_left hpos n, _⟩
  rw [add_assoc, add_comm x m, ← add_assoc]
  exact hn2
state_before: case neg.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: let g' : ℕ → ℕ := @Nat.rec (fun _ => ℕ) m fun n gn => Nat.find (h gn)
state_before: case neg.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
h : ∀ (n : ℕ), ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: exact
  ⟨(RelEmbedding.natLt (fun n => g' n + m) fun n =>
        Nat.add_lt_add_right (Nat.find_spec (h (g' n))).1 m).orderEmbeddingOfLTEmbedding,
    Or.intro_left _ fun n => (Nat.find_spec (h (g' n))).2⟩
state_before: case neg.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
h : ∀ (n : ℕ), ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))
g' : ℕ → ℕ := Nat.rec m fun n gn => Nat.find (_ : ∃ n', gn < n' ∧ r (f (gn + m)) (f (n' + m)))
⊢ ∃ g, (∀ (n : ℕ), r (f (↑g n)) (f (↑g (n + 1)))) ∨ ∀ (m n : ℕ), m < n → ¬r (f (↑g m)) (f (↑g n))

tactic: by_cases he : hbad.toFinset.Nonempty
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
⊢ ∃ m, ∀ (n : ℕ), m ≤ n → ¬n ∈ bad

tactic: refine'
  ⟨(hbad.toFinset.max' he).succ, fun n hn nbad =>
    Nat.not_succ_le_self _
      (hn.trans (hbad.toFinset.le_max' n (hbad.mem_toFinset.2 nbad)))⟩
state_before: case pos
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
he : Finset.Nonempty (Set.Finite.toFinset hbad)
⊢ ∃ m, ∀ (n : ℕ), m ≤ n → ¬n ∈ bad

tactic: exact ⟨0, fun n _ nbad => he ⟨n, hbad.mem_toFinset.2 nbad⟩⟩
state_before: case neg
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
he : ¬Finset.Nonempty (Set.Finite.toFinset hbad)
⊢ ∃ m, ∀ (n : ℕ), m ≤ n → ¬n ∈ bad

tactic: intro n
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
⊢ ∀ (n : ℕ), ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: have h := hm _ (le_add_of_nonneg_left n.zero_le)
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n : ℕ
⊢ ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: simp only [exists_prop, not_not, Set.mem_setOf_eq, not_forall] at h
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n : ℕ
h : ¬n + m ∈ bad
⊢ ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: obtain ⟨n', hn1, hn2⟩ := h
state_before: α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n : ℕ
h : ∃ x, n + m < x ∧ r (f (n + m)) (f x)
⊢ ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: obtain ⟨x, hpos, rfl⟩ := exists_pos_add_of_lt hn1
state_before: case intro.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n n' : ℕ
hn1 : n + m < n'
hn2 : r (f (n + m)) (f n')
⊢ ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: refine' ⟨n + x, add_lt_add_left hpos n, _⟩
state_before: case intro.intro.intro.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n x : ℕ
hpos : 0 < x
hn1 : n + m < n + m + x
hn2 : r (f (n + m)) (f (n + m + x))
⊢ ∃ n', n < n' ∧ r (f (n + m)) (f (n' + m))

tactic: rw [add_assoc, add_comm x m, ← add_assoc]
state_before: case intro.intro.intro.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n x : ℕ
hpos : 0 < x
hn1 : n + m < n + m + x
hn2 : r (f (n + m)) (f (n + m + x))
⊢ r (f (n + m)) (f (n + x + m))

tactic: exact hn2
state_before: case intro.intro.intro.intro
α : Type ?u.41863
r : α → α → Prop
f : ℕ → α
bad : Set ℕ := {m | ∀ (n : ℕ), m < n → ¬r (f m) (f n)}
hbad : Set.Finite bad
m : ℕ
hm : ∀ (n : ℕ), m ≤ n → ¬n ∈ bad
n x : ℕ
hpos : 0 < x
hn1 : n + m < n + m + x
hn2 : r (f (n + m)) (f (n + m + x))
⊢ r (f (n + m)) (f (n + m + x))