Theorem: Nat.Subtype.ofNat_surjective_aux
File: Mathlib/Logic/Denumerable.lean

tactic: set t : List s :=
  ((List.range x).filter fun y => y ∈ s).pmap
    (fun (y : ℕ) (hy : y ∈ s) => ⟨y, hy⟩)
    (by intros a ha; simpa using (List.mem_filter.mp ha).2) with ht
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: have hmt : ∀ {y : s}, y ∈ t ↔ y < ⟨x, hx⟩ := by
  simp [List.mem_filter, Subtype.ext_iff_val, ht]
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: have wf : ∀ m : s, List.maximum t = m → ↑m < x := fun m hmax => by
  simpa using hmt.mp (List.maximum_mem hmax)
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: cases' hmax : List.maximum t with m
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: cases' ofNat_surjective_aux m.2 with a ha
state_before: case some
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: refine ⟨a + 1, le_antisymm ?_ ?_⟩ <;> rw [ofNat]
state_before: case some.intro
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: intros a ha
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
⊢ ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s

tactic: simpa using (List.mem_filter.mp ha).2
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
a : ℕ
ha : a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x)
⊢ a ∈ s

tactic: simp [List.mem_filter, Subtype.ext_iff_val, ht]
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
⊢ ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }

tactic: simpa using hmt.mp (List.maximum_mem hmax)
state_before: α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
m : ↑s
hmax : List.maximum t = ↑m
⊢ ↑m < x

tactic: refine ⟨0, le_antisymm bot_le (le_of_not_gt fun h => List.not_mem_nil (⊥ : s) ?_)⟩
state_before: case none
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
hmax : List.maximum t = none
⊢ ∃ n, ofNat s n = { val := x✝, property := hx }

tactic: rwa [← List.maximum_eq_none.1 hmax, hmt]
state_before: case none
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
hmax : List.maximum t = none
h : { val := x✝, property := hx } > ofNat s 0
⊢ ⊥ ∈ []

tactic: refine succ_le_of_lt ?_
state_before: case some.intro.refine_1
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
⊢ succ (ofNat s a) ≤ { val := x✝, property := hx }

tactic: rw [ha]
state_before: case some.intro.refine_1
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
⊢ ofNat s a < { val := x✝, property := hx }

tactic: exact wf _ hmax
state_before: case some.intro.refine_1
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
⊢ { val := ↑m, property := (_ : ↑m ∈ s) } < { val := x✝, property := hx }

tactic: refine le_succ_of_forall_lt_le fun z hz => ?_
state_before: case some.intro.refine_2
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
⊢ { val := x✝, property := hx } ≤ succ (ofNat s a)

tactic: rw [ha]
state_before: case some.intro.refine_2
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
z : ↑s
hz : z < { val := x✝, property := hx }
⊢ z ≤ ofNat s a

tactic: cases m
state_before: case some.intro.refine_2
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
hmax : List.maximum t = some m
a : ℕ
ha : ofNat s a = { val := ↑m, property := (_ : ↑m ∈ s) }
z : ↑s
hz : z < { val := x✝, property := hx }
⊢ z ≤ { val := ↑m, property := (_ : ↑m ∈ s) }

tactic: exact List.le_maximum_of_mem (hmt.2 hz) hmax
state_before: case some.intro.refine_2.mk
α : Type ?u.49351
β : Type ?u.49354
s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
a : ℕ
z : ↑s
hz : z < { val := x✝, property := hx }
val✝ : ℕ
property✝ : val✝ ∈ s
hmax : List.maximum t = some { val := val✝, property := property✝ }
ha :
  ofNat s a =
    { val := ↑{ val := val✝, property := property✝ }, property := (_ : ↑{ val := val✝, property := property✝ } ∈ s) }
⊢ z ≤ { val := ↑{ val := val✝, property := property✝ }, property := (_ : ↑{ val := val✝, property := property✝ } ∈ s) }

tactic: tauto
state_before: s : Set ℕ
inst✝¹ : Infinite ↑s
inst✝ : DecidablePred fun x => x ∈ s
x✝ : ℕ
a✝ :
  ∀ (y : ℕ),
    (invImage (fun a => sizeOf a) instWellFoundedRelation).1 y x✝ →
      ∀ (hx : y ∈ s), ∃ n, ofNat s n = { val := y, property := hx }
x : ℕ := x✝
hx : x✝ ∈ s
t : List ↑s :=
  List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
    (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
ht :
  t =
    List.pmap (fun y hy => { val := y, property := hy }) (List.filter (fun y => decide (y ∈ s)) (List.range x))
      (_ : ∀ (a : ℕ), a ∈ List.filter (fun y => decide (y ∈ s)) (List.range x) → a ∈ s)
hmt : ∀ {y : ↑s}, y ∈ t ↔ y < { val := x, property := hx }
wf : ∀ (m : ↑s), List.maximum t = ↑m → ↑m < x
m : ↑s
h✝ hmax : List.maximum t = some m
⊢ (invImage (fun a => sizeOf a) instWellFoundedRelation).1 (↑m) x✝