l3lab/ntp-mathlib huggingface dataset:
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    

Proof state:
case intro.intro.intro.intro
α : Type u_1
I B✝ X : Set α
M : IndepMatroid α
B B' : Set α
hB : B ∈ {I | M.Indep I}
hBmax : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B b → (fun x x_1 => x ⊆ x_1) b B
hB' : B' ∈ {I | M.Indep I}
hB'max : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B' b → (fun x x_1 => x ⊆ x_1) b B'
e : α
he : e ∈ B \ B'
hnotmax : B \ {e} ∉ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
f : α
hfB : M.Indep (insert f (B \ {e}))
hf : f ∈ B' ∧ (f ∈ B → f = e)
hfB' : f ∉ B
hnot : ¬(fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) (insert f (B \ {e}))
⊢ False

Next tactic:
obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    

Proof state:
case intro.intro.intro.intro.intro.intro
α : Type u_1
I B✝ X : Set α
M : IndepMatroid α
B B' : Set α
hB : B ∈ {I | M.Indep I}
hBmax : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B b → (fun x x_1 => x ⊆ x_1) b B
hB' : B' ∈ {I | M.Indep I}
hB'max : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B' b → (fun x x_1 => x ⊆ x_1) b B'
e : α
he : e ∈ B \ B'
hnotmax : B \ {e} ∉ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
f : α
hfB : M.Indep (insert f (B \ {e}))
hf : f ∈ B' ∧ (f ∈ B → f = e)
hfB' : f ∉ B
hnot : ¬(fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) (insert f (B \ {e}))
x : α
hxB : x ∈ B \ insert f (B \ {e})
hind : M.Indep (insert x (insert f (B \ {e})))
⊢ False

Next tactic:
simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    

Proof state:
case intro.intro.intro.intro.intro.intro
α : Type u_1
I B✝ X : Set α
M : IndepMatroid α
B B' : Set α
hB : B ∈ {I | M.Indep I}
hBmax : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B b → (fun x x_1 => x ⊆ x_1) b B
hB' : B' ∈ {I | M.Indep I}
hB'max : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B' b → (fun x x_1 => x ⊆ x_1) b B'
e : α
he : e ∈ B \ B'
hnotmax : B \ {e} ∉ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
f : α
hfB : M.Indep (insert f (B \ {e}))
hf : f ∈ B' ∧ (f ∈ B → f = e)
hfB' : f ∉ B
hnot : ¬(fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) (insert f (B \ {e}))
x : α
hind : M.Indep (insert x (insert f (B \ {e})))
hxB : x ∈ B ∧ ¬x = f ∧ (x ∈ B → x = e)
⊢ False

Next tactic:
obtain rfl := hxB.2.2 hxB.1
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    

Proof state:
case intro.intro.intro.intro.intro.intro
α : Type u_1
I B✝ X : Set α
M : IndepMatroid α
B B' : Set α
hB : B ∈ {I | M.Indep I}
hBmax : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B b → (fun x x_1 => x ⊆ x_1) b B
hB' : B' ∈ {I | M.Indep I}
hB'max : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B' b → (fun x x_1 => x ⊆ x_1) b B'
f : α
hfB' : f ∉ B
x : α
he : x ∈ B \ B'
hnotmax : B \ {x} ∉ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hfB : M.Indep (insert f (B \ {x}))
hf : f ∈ B' ∧ (f ∈ B → f = x)
hnot : ¬(fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) (insert f (B \ {x}))
hind : M.Indep (insert x (insert f (B \ {x})))
hxB : x ∈ B ∧ ¬x = f ∧ (x ∈ B → x = x)
⊢ False

Next tactic:
rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    

Proof state:
case intro.intro.intro.intro.intro.intro
α : Type u_1
I B✝ X : Set α
M : IndepMatroid α
B B' : Set α
hB : B ∈ {I | M.Indep I}
hBmax : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B b → (fun x x_1 => x ⊆ x_1) b B
hB' : B' ∈ {I | M.Indep I}
hB'max : ∀ ⦃b : Set α⦄, b ∈ {I | M.Indep I} → (fun x x_1 => x ⊆ x_1) B' b → (fun x x_1 => x ⊆ x_1) b B'
f : α
hfB' : f ∉ B
x : α
he : x ∈ B \ B'
hnotmax : B \ {x} ∉ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hfB : M.Indep (insert f (B \ {x}))
hf : f ∈ B' ∧ (f ∈ B → f = x)
hnot : ¬(fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) (insert f (B \ {x}))
hind : M.Indep (insert f B)
hxB : x ∈ B ∧ ¬x = f ∧ (x ∈ B → x = x)
⊢ False

Next tactic:
exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    

Proof state:
α : Type u_1
I B X : Set α
M : IndepMatroid α
⊢ ∀ X ⊆ M.E,
    ExistsMaximalSubsetProperty (fun x => ∃ B, (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) B ∧ x ⊆ B)
      X

Next tactic:
rintro X hXE I ⟨hB, hB, hIB⟩ hIX
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    rintro X hXE I ⟨hB, hB, hIB⟩ hIX
    

Proof state:
case intro.intro
α : Type u_1
I✝ B X✝ : Set α
M : IndepMatroid α
X : Set α
hXE : X ⊆ M.E
I hB✝ : Set α
hB : hB✝ ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hIB : I ⊆ hB✝
hIX : I ⊆ X
⊢ Set.Nonempty
    (maximals (fun x x_1 => x ⊆ x_1)
      {Y | (fun x => ∃ B, (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) B ∧ x ⊆ B) Y ∧ I ⊆ Y ∧ Y ⊆ X})

Next tactic:
obtain ⟨J, ⟨hJ, hIJ, hJX⟩, hJmax⟩ := M.indep_maximal X hXE I (M.indep_subset hB.1 hIB) hIX
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    rintro X hXE I ⟨hB, hB, hIB⟩ hIX
    obtain ⟨J, ⟨hJ, hIJ, hJX⟩, hJmax⟩ := M.indep_maximal X hXE I (M.indep_subset hB.1 hIB) hIX
    

Proof state:
case intro.intro.intro.intro.intro.intro
α : Type u_1
I✝ B X✝ : Set α
M : IndepMatroid α
X : Set α
hXE : X ⊆ M.E
I hB✝ : Set α
hB : hB✝ ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hIB : I ⊆ hB✝
hIX : I ⊆ X
J : Set α
hJmax : ∀ ⦃b : Set α⦄, b ∈ {Y | M.Indep Y ∧ I ⊆ Y ∧ Y ⊆ X} → (fun x x_1 => x ⊆ x_1) J b → (fun x x_1 => x ⊆ x_1) b J
hJ : M.Indep J
hIJ : I ⊆ J
hJX : J ⊆ X
⊢ Set.Nonempty
    (maximals (fun x x_1 => x ⊆ x_1)
      {Y | (fun x => ∃ B, (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) B ∧ x ⊆ B) Y ∧ I ⊆ Y ∧ Y ⊆ X})

Next tactic:
obtain ⟨BJ, hBJ⟩ := M.indep_maximal M.E rfl.subset J hJ (M.subset_ground J hJ)
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    rintro X hXE I ⟨hB, hB, hIB⟩ hIX
    obtain ⟨J, ⟨hJ, hIJ, hJX⟩, hJmax⟩ := M.indep_maximal X hXE I (M.indep_subset hB.1 hIB) hIX
    obtain ⟨BJ, hBJ⟩ := M.indep_maximal M.E rfl.subset J hJ (M.subset_ground J hJ)
    

Proof state:
case intro.intro.intro.intro.intro.intro.intro
α : Type u_1
I✝ B X✝ : Set α
M : IndepMatroid α
X : Set α
hXE : X ⊆ M.E
I hB✝ : Set α
hB : hB✝ ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hIB : I ⊆ hB✝
hIX : I ⊆ X
J : Set α
hJmax : ∀ ⦃b : Set α⦄, b ∈ {Y | M.Indep Y ∧ I ⊆ Y ∧ Y ⊆ X} → (fun x x_1 => x ⊆ x_1) J b → (fun x x_1 => x ⊆ x_1) b J
hJ : M.Indep J
hIJ : I ⊆ J
hJX : J ⊆ X
BJ : Set α
hBJ : BJ ∈ maximals (fun x x_1 => x ⊆ x_1) {Y | M.Indep Y ∧ J ⊆ Y ∧ Y ⊆ M.E}
⊢ Set.Nonempty
    (maximals (fun x x_1 => x ⊆ x_1)
      {Y | (fun x => ∃ B, (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) B ∧ x ⊆ B) Y ∧ I ⊆ Y ∧ Y ⊆ X})

Next tactic:
refine' ⟨J, ⟨⟨BJ,_, hBJ.1.2.1⟩ ,hIJ,hJX⟩, _⟩
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    rintro X hXE I ⟨hB, hB, hIB⟩ hIX
    obtain ⟨J, ⟨hJ, hIJ, hJX⟩, hJmax⟩ := M.indep_maximal X hXE I (M.indep_subset hB.1 hIB) hIX
    obtain ⟨BJ, hBJ⟩ := M.indep_maximal M.E rfl.subset J hJ (M.subset_ground J hJ)
    refine' ⟨J, ⟨⟨BJ,_, hBJ.1.2.1⟩ ,hIJ,hJX⟩, _⟩
    · 

Proof state:
case intro.intro.intro.intro.intro.intro.intro.refine'_1
α : Type u_1
I✝ B X✝ : Set α
M : IndepMatroid α
X : Set α
hXE : X ⊆ M.E
I hB✝ : Set α
hB : hB✝ ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hIB : I ⊆ hB✝
hIX : I ⊆ X
J : Set α
hJmax : ∀ ⦃b : Set α⦄, b ∈ {Y | M.Indep Y ∧ I ⊆ Y ∧ Y ⊆ X} → (fun x x_1 => x ⊆ x_1) J b → (fun x x_1 => x ⊆ x_1) b J
hJ : M.Indep J
hIJ : I ⊆ J
hJX : J ⊆ X
BJ : Set α
hBJ : BJ ∈ maximals (fun x x_1 => x ⊆ x_1) {Y | M.Indep Y ∧ J ⊆ Y ∧ Y ⊆ M.E}
⊢ (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) BJ

Next tactic:
exact ⟨hBJ.1.1, fun B' hB' hBJB' ↦ hBJ.2 ⟨hB',hBJ.1.2.1.trans hBJB',
        M.subset_ground _ hB'⟩ hBJB'⟩
/-- An `M : IndepMatroid α` gives a `Matroid α` whose bases are the maximal `M`-independent sets. -/
@[simps] protected def matroid (M : IndepMatroid α) : Matroid α where
  E := M.E
  Base := (· ∈ maximals (· ⊆ ·) {I | M.Indep I})
  exists_base := by
    obtain ⟨B, ⟨hB,-,-⟩, hB₁⟩ :=
      M.indep_maximal M.E rfl.subset ∅ M.indep_empty (empty_subset _)
    exact ⟨B, ⟨hB, fun B' hB' h' ↦ hB₁ ⟨hB', empty_subset _,M.subset_ground B' hB'⟩ h'⟩⟩
  base_exchange := by
    rintro B B' ⟨hB, hBmax⟩ ⟨hB',hB'max⟩ e he
    have hnotmax : B \ {e} ∉ maximals (· ⊆ ·) {I | M.Indep I}
    { simp only [mem_maximals_setOf_iff, diff_singleton_subset_iff, not_and, not_forall,
        exists_prop, exists_and_left]
      exact fun _ ↦ ⟨B, hB, subset_insert _ _, by simpa using he.1⟩ }

    obtain ⟨f,hf,hfB⟩ := M.indep_aug (M.indep_subset hB (diff_subset B {e})) hnotmax ⟨hB',hB'max⟩
    simp only [mem_diff, mem_singleton_iff, not_and, not_not] at hf

    have hfB' : f ∉ B := by (intro hfB; obtain rfl := hf.2 hfB; exact he.2 hf.1)

    refine' ⟨f, ⟨hf.1, hfB'⟩, by_contra (fun hnot ↦ _)⟩
    obtain ⟨x,hxB, hind⟩ :=  M.indep_aug hfB hnot ⟨hB, hBmax⟩
    simp only [mem_diff, mem_insert_iff, mem_singleton_iff, not_or, not_and, not_not] at hxB
    obtain rfl := hxB.2.2 hxB.1
    rw [insert_comm, insert_diff_singleton, insert_eq_of_mem he.1] at hind
    exact not_mem_subset (hBmax hind (subset_insert _ _)) hfB' (mem_insert _ _)
  maximality := by
    rintro X hXE I ⟨hB, hB, hIB⟩ hIX
    obtain ⟨J, ⟨hJ, hIJ, hJX⟩, hJmax⟩ := M.indep_maximal X hXE I (M.indep_subset hB.1 hIB) hIX
    obtain ⟨BJ, hBJ⟩ := M.indep_maximal M.E rfl.subset J hJ (M.subset_ground J hJ)
    refine' ⟨J, ⟨⟨BJ,_, hBJ.1.2.1⟩ ,hIJ,hJX⟩, _⟩
    · exact ⟨hBJ.1.1, fun B' hB' hBJB' ↦ hBJ.2 ⟨hB',hBJ.1.2.1.trans hBJB',
        M.subset_ground _ hB'⟩ hBJB'⟩
    

Proof state:
case intro.intro.intro.intro.intro.intro.intro.refine'_2
α : Type u_1
I✝ B X✝ : Set α
M : IndepMatroid α
X : Set α
hXE : X ⊆ M.E
I hB✝ : Set α
hB : hB✝ ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}
hIB : I ⊆ hB✝
hIX : I ⊆ X
J : Set α
hJmax : ∀ ⦃b : Set α⦄, b ∈ {Y | M.Indep Y ∧ I ⊆ Y ∧ Y ⊆ X} → (fun x x_1 => x ⊆ x_1) J b → (fun x x_1 => x ⊆ x_1) b J
hJ : M.Indep J
hIJ : I ⊆ J
hJX : J ⊆ X
BJ : Set α
hBJ : BJ ∈ maximals (fun x x_1 => x ⊆ x_1) {Y | M.Indep Y ∧ J ⊆ Y ∧ Y ⊆ M.E}
⊢ ∀ ⦃b : Set α⦄,
    b ∈
        {Y |
          (fun x => ∃ B, (fun x => x ∈ maximals (fun x x_1 => x ⊆ x_1) {I | M.Indep I}) B ∧ x ⊆ B) Y ∧ I ⊆ Y ∧ Y ⊆ X} →
      (fun x x_1 => x ⊆ x_1) J b → (fun x x_1 => x ⊆ x_1) b J

Next tactic:
simp only [maximals, mem_setOf_eq, and_imp, forall_exists_index]