AFTD Auto-Formalizing Theoretical Domains

matroid_laminar_connectedness_query_linear

theoremverifiedoriginal

For each n there is one deterministic independence-query tree of depth at most 4n+1 deciding connectedness for every laminar matroid on n elements. The promise uses the circuit characterization of laminar matroids: for intersecting circuits one closure contains the other. The tree uses only independence bits and receives no laminar presentation.

Statement

theorem matroid_laminar_connectedness_query_linear (n : ℕ) :
    ∃ T : MatroidIndepQueryTree (Fin n), T.depth ≤ 4*n+1 ∧
      ∀ M : Matroid (Fin n), M.E = Set.univ →
        (∀ C D, M.IsCircuit C → M.IsCircuit D → (C ∩ D).Nonempty →
          M.closure C ⊆ M.closure D ∨ M.closure D ⊆ M.closure C) →
        ∀ o : Finset (Fin n) → Bool,
          (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) →
          (T.eval o = true ↔ matroid_connected M)
Related work
Open problem OP-1998 and Theorem 4.1 of https://arxiv.org/abs/2611.23624. The classical intersecting-circuit characterization appears in https://arxiv.org/abs/1606.08354. Experiment 44434 enumerates labeled laminar matroids through six elements.
Verified
28 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 8
  • not_matroid_connected_of_fund_circuit_separated_finiteIf M is a matroid with finite ground set, B is a base, and A is a non-empty proper subset of B such that every e in E \ B whose…
  • matroid_connectedA matroid M on a type α is connected if for all elements e and f of its ground set M.E, either e = f or there is a circuit C of M that…
  • MatroidIndepQueryTreeA deterministic independence-query algorithm on a ground set α, as a rooted binary decision tree: a leaf leaf b outputs the Boolean b;…
  • MatroidIndepQueryTree.evalThe Boolean output of an independence-query decision tree T on a ground set α when the queries are answered by an oracle o : Finset α →…
  • MatroidIndepQueryTree.depthThe depth (height) of an independence-query decision tree on a ground set α, a natural number defined by recursion on the tree: a leaf…
  • not_matroid_connected_of_not_indep_singletonA matroid with a loop e (the set {e} is dependent) and another ground element f ≠ e is not connected.
  • matroid_connected_of_subsingletonA matroid whose ground set has at most one element is connected.
  • FThe prime field $\mathrm{GF}(p)$, realized as $\mathbb{Z}/p\mathbb{Z}$ via ZMod.

Read back from the Lean

For every natural number $n$, there exists an independence query tree $T$ on ground set Fin n such that: 1. The depth of $T$ is at most $4n + 1$; 2. For every matroid $M$ on Fin n, if the ground set $M.E$ is the entire universe Set.univ, and if for any two circuits $C, D$ of $M$ with non-empty intersection, either the closure of $C$ is contained in the closure of $D$ or the closure of $D$ is contained in the closure of $C$ (i.e., $M$ is circuit-laminar), then for any oracle $o : \mathrm{Finset}(\mathrm{Fin}\ n) \to \mathrm{Bool}$ that correctly decides independence in $M$ (i.e., $o(Q) = \mathrm{true} \iff Q$ is independent in $M$ for all $Q$), the evaluation of $T$ on $o$ returns $\mathrm{true}$ if and only if $M$ is connected (matroid_connected M).

Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.

The proof, explained

test summary of the proof that is long enough to avoid being flagged as a placeholder. It describes the overall strategy and depth.

  1. c1

    e1

    lines 8–37

      classical
      by_cases hxC : x ∈ C
      · exact ⟨C,hC,he,hxC⟩
      let I := C \ {e}
      let D := M.fundCircuit x I
      have hI : M.Indep I := hC.sdiff_singleton_indep he
      have hD : M.IsCircuit D := hI.fundCircuit_isCircuit
        (by simpa [I,hC.closure_sdiff_singleton_eq] using hx) (fun h => hxC h.1)
      have hb : ¬ D ⊆ {x} := fun h => hD.not_indep (hi.subset h)
      rw [not_subset] at hb
      obtain ⟨b,hbD,hbx⟩ := hb
      have hbC : b ∈ C := by
        rcases M.fundCircuit_subset_insert x I hbD with h | h
        · exact (hbx h).elim
        · exact h.1
      let J := C \ {b}
      let F := M.fundCircuit x J
      have hJ : M.Indep J := hC.sdiff_singleton_indep hbC
      have hF : M.IsCircuit F := hJ.fundCircuit_isCircuit
        (by simpa [J,hC.closure_sdiff_singleton_eq] using hx) (fun h => hxC h.1)
      refine ⟨F,hF,?_,M.mem_fundCircuit x J⟩
      by_contra heF
      have hFI : F ⊆ insert x I := by
        intro y hy
        rcases M.fundCircuit_subset_insert x J hy with h | h
        · exact Or.inl h
        · exact Or.inr ⟨h.1,fun hye => heF (hye ▸ hy)⟩
      have hFD : F = D := hF.eq_fundCircuit_of_subset hI hFI
      have hmem := M.fundCircuit_subset_insert x J (hFD.symm ▸ hbD)
      exact hmem.elim hbx (fun h => h.2 rfl)
  2. c2

    e2

    lines 42–50

      have hC := hB.fundCircuit_isCircuit he hn
      apply Subset.antisymm _ (inter_subset_inter_left B (M.subset_closure _ hC.subset_ground))
      rintro b ⟨hbc,hb⟩
      refine ⟨?_,hb⟩
      by_contra hnC
      rw [← hC.closure_sdiff_singleton_eq e, M.fundCircuit_sdiff_eq_inter hn] at hbc
      exact hB.indep.notMem_closure_sdiff_of_mem hb
        (M.closure_subset_closure (show M.fundCircuit e B ∩ B ⊆ B \ {b} from
          fun y hy => ⟨hy.2, fun h => hnC (h ▸ hy.1)⟩) hbc)
  3. c3

    e3

    lines 55–61

      simp only [Matroid.isColoop_iff_forall_mem_isBase,not_forall,exists_prop] at hc
      obtain ⟨D,hD,hbd⟩ := hc
      obtain ⟨e,he,hX⟩ := hB.exchange hD ⟨hb,hbd⟩
      have heE := hD.subset_ground he.1
      have heb : e ≠ b := fun h => he.2 (h ▸ hb)
      refine ⟨e,⟨heE,he.2⟩,(hB.indep.mem_fundCircuit_iff (by rwa [hB.closure_eq]) he.2).mpr ?_⟩
      simpa [← insert_sdiff_singleton_comm heb] using hX.indep
  4. c4

    e4

    lines 72–150

      classical
      intro supp rows
      have hfull (e : Fin n) : e ∈ M.E := he ▸ trivial
      haveI : Nontrivial (Fin n) := Fin.nontrivial_iff_two_le.mpr hn
      have hS (x y : Fin n) (hx : x ∉ B) (hy : y ∉ B)
          (le : M.closure (M.fundCircuit x B) ⊆ M.closure (M.fundCircuit y B)) :
          supp x ⊆ supp y := by
        intro b hb
        obtain ⟨hbB,hbX⟩ := Finset.mem_filter.mp hb
        have hb' : b ∈ M.closure (M.fundCircuit x B) ∩ (B : Set (Fin n)) := by
          rw [nested_support hB (hfull x) hx]; exact ⟨hbX,hbB⟩
        have hb'' : b ∈ M.closure (M.fundCircuit y B) ∩ (B : Set (Fin n)) := ⟨le hb'.1,hbB⟩
        rw [nested_support hB (hfull y) hy] at hb''
        exact Finset.mem_filter.mpr ⟨hbB,hb''.1⟩
      have compare (e f b : Fin n) (heB : e ∉ B) (hfB : f ∉ B) (hb : b ∈ supp e) (hb' : b ∈ supp f) :
          supp e ⊆ supp f ∨ supp f ⊆ supp e := by
        exact (hlam _ _ (hB.fundCircuit_isCircuit (hfull e) heB) (hB.fundCircuit_isCircuit (hfull f) hfB)
          ⟨b,(Finset.mem_filter.mp hb).2,(Finset.mem_filter.mp hb').2⟩).imp
            (hS e f heB hfB) (hS f e hfB heB)
      by_cases hne : rows.Nonempty
      swap
      · refine ⟨fun hconn => ?_,fun _ => ⟨a,ha,fun e he => (hne ⟨e,he⟩).elim⟩⟩
        obtain ⟨z,hza⟩ := exists_ne a
        have hncol : ¬M.IsColoop a := by
          rcases hconn a (hfull a) z (hfull z) with heq | ⟨C,hC,hac,_⟩
          · exact (hza heq.symm).elim
          · exact fun h => (Matroid.isColoop_iff_forall_notMem_isCircuit (hfull a)).mp h hC hac
        obtain ⟨e,⟨_,heB⟩,hae⟩ := nested_covered hB ha hncol
        exact (hne ⟨e,Finset.mem_filter.mpr ⟨Finset.mem_compl.mpr heB,Finset.mem_filter.mpr ⟨ha,hae⟩⟩⟩).elim
      obtain ⟨r,hr,hmax⟩ := Finset.exists_max_image rows (fun e => (supp e).card) hne
      have hr' := Finset.mem_filter.mp hr
      have hrB := Finset.mem_compl.mp hr'.1
      have maximal (f : Fin n) (hf : f ∈ rows) : supp f ⊆ supp r := by
        have hf' := Finset.mem_filter.mp hf
        rcases compare f r a (Finset.mem_compl.mp hf'.1) hrB hf'.2 hr'.2 with le | le
        · exact le
        · exact (Finset.eq_of_subset_of_card_le le (hmax f hf)).ge
      have hsub : supp r ⊆ B := Finset.filter_subset _ _
      constructor
      · intro hconn
        refine ⟨r,hr,?_⟩
        by_contra! hnfull
        obtain ⟨b,hb,hbr⟩ := hnfull
        have hproper : (supp r : Set (Fin n)) ⊂ (B : Set (Fin n)) :=
          Set.ssubset_iff_subset_ne.mpr ⟨hsub,fun h => hbr (show b ∈ (supp r : Set (Fin n)) from h ▸ hb)⟩
        apply (not_matroid_connected_of_fund_circuit_separated_finite M M.E.toFinite hB
          (A := (supp r : Set (Fin n))) ⟨a,hr'.2⟩ hproper ?_) hconn
        intro f hf hmeet x hx
        obtain ⟨c,hcf,hcr⟩ := hmeet
        have hSf : c ∈ supp f := Finset.mem_filter.mpr ⟨hsub hcr,hcf⟩
        have hle : supp f ⊆ supp r := by
          rcases compare f r c hf.2 hrB hSf hcr with le | le
          · exact le
          · exact maximal f (Finset.mem_filter.mpr ⟨Finset.mem_compl.mpr hf.2, le hr'.2⟩)
        rcases M.fundCircuit_subset_insert f (B : Set (Fin n)) hx with hx' | hx'
        · exact Or.inl hx'
        · exact Or.inr (hle (Finset.mem_filter.mpr ⟨hx',hx⟩))
      · intro hdisc
        by_contra! hncol
        have hallB (b : Fin n) (hb : b ∈ B) : b ∈ supp r := by
          obtain ⟨e,he',hbe⟩ := hncol b hb
          exact maximal e he' hbe
        let C := M.fundCircuit r (B : Set (Fin n))
        have hC : M.IsCircuit C := hB.fundCircuit_isCircuit (hfull r) hrB
        have hBC : (B : Set (Fin n)) ⊆ C := fun b hb => (Finset.mem_filter.mp (hallB b hb)).2
        have circ (x : Fin n) : ∃ D, M.IsCircuit D ∧ a ∈ D ∧ x ∈ D :=
          matroid_nonloop_common_circuit_in_circuit_closure hC (hBC ha)
            (M.closure_subset_closure hBC (hB.closure_eq.symm ▸ hfull x)) (hi x (hfull x))
        apply hdisc
        intro x hx y hy
        right
        obtain ⟨D,hD,haD,hxD⟩ := circ x
        obtain ⟨F,hF,haF,hyF⟩ := circ y
        rcases hlam D F hD hF ⟨a,haD,haF⟩ with le | le
        · obtain ⟨G,hG,hyG,hxG⟩ := matroid_nonloop_common_circuit_in_circuit_closure hF hyF
            (le (M.subset_closure _ hD.subset_ground hxD)) (hi x hx)
          exact ⟨G,hG,hxG,hyG⟩
        · exact matroid_nonloop_common_circuit_in_circuit_closure hD hxD
            (le (M.subset_closure _ hF.subset_ground hyF)) (hi y hy)
  5. c5

    e5

    lines 161–170

      induction t generalizing rs cs with
      | zero => rfl
      | succ t ih =>
        cases cs with
        | nil => simp [nested_scan,MatroidIndepQueryTree.depth]
        | cons c cs =>
          cases rs with
          | nil => simp [nested_scan,MatroidIndepQueryTree.depth]
          | cons r rs => simp [nested_scan,MatroidIndepQueryTree.depth, Nat.add_comm 1,
              max_le_iff,ih]
  6. c6

    e6

    lines 176–203

      induction t generalizing rs cs with
      | zero => omega
      | succ t ih =>
        cases cs with
        | nil => simp [nested_scan,MatroidIndepQueryTree.eval]
        | cons c cs =>
          cases rs with
          | nil => simp [nested_scan,MatroidIndepQueryTree.eval]
          | cons r rs =>
            simp only [List.length_cons] at hlen
            have ihN := ih rs (c::cs) (by simp only [List.length_cons]; omega)
            have ihY := ih (r::rs) cs (by simp only [List.length_cons]; omega)
            simp only [nested_scan,MatroidIndepQueryTree.eval]
            split_ifs with ha
            · constructor
              · rintro ⟨s,hs,hu⟩
                exact ihY.1 ⟨s,hs,fun z hz => hu z (by simp [hz])⟩
              · rintro ⟨d,hd,hz⟩
                apply ihY.2
                exact ⟨d, (List.mem_cons.mp hd).resolve_left
                  (by rintro rfl; exact hz r (by simp) ha), hz⟩
            · constructor
              · rintro ⟨s,hs,hu⟩
                apply ihN.1
                exact ⟨s,(List.mem_cons.mp hs).resolve_left
                  (by rintro rfl; exact ha (hu c (by simp))), hu⟩
              · rintro ⟨d,hd,hz⟩
                exact ihN.2 ⟨d,hd,fun s hs => hz s (by simp [hs])⟩
  7. c7

    e7

    lines 213–219

      induction xs generalizing I with
      | nil => simpa [nested_basis] using hk I
      | cons x xs ih =>
        simp only [nested_basis,MatroidIndepQueryTree.depth,List.length_cons]
        have := ih I
        have := ih (insert x I)
        omega
  8. c8

    e8

    lines 228–252

      induction xs generalizing I with
      | nil =>
        apply hk _ (hI.isBase_of_ground_subset_closure ?_)
        intro z _
        exact (hcov z).resolve_left (by simp)
      | cons x xs ih =>
        simp only [nested_basis,MatroidIndepQueryTree.eval]
        split_ifs with h
        · apply ih _ ((ho _).mp h)
          intro y
          rcases hcov y with hmem | hcl
          · rcases List.mem_cons.mp hmem with rfl | hmem
            · right; apply M.subset_closure _ ((ho _).mp h).subset_ground; simp
            · exact Or.inl hmem
          · exact Or.inr (M.closure_subset_closure (by simp) hcl)
        · apply ih _ hI
          intro y
          rcases hcov y with hmem | hcl
          · rcases List.mem_cons.mp hmem with rfl | hmem
            · right
              apply hI.mem_closure_iff.mpr (Or.inl ?_)
              apply M.dep_of_not_indep _ (he ▸ subset_univ _)
              simpa using mt (ho (insert y I)).mpr h
            · exact Or.inl hmem
          · exact Or.inr hcl
  9. c9

    e9

    lines 260–262

      induction xs with
      | nil => simp [nested_loops]
      | cons x xs ih => simp [nested_loops,MatroidIndepQueryTree.depth,ih]; omega
  10. c10

    e10

    lines 266–271

        (nested_loops k xs).eval o = true ↔ (∀ x ∈ xs, o {x} = true) ∧ k.eval o = true := by
      induction xs with
      | nil => simp [nested_loops]
      | cons x xs ih =>
        simp only [nested_loops,MatroidIndepQueryTree.eval]
        split_ifs <;> simp_all
  11. c11

    e11

    lines 281–287

      induction es generalizing kept with
      | nil => simpa [laminar_filter] using hk kept
      | cons e es ih =>
        simp only [laminar_filter,MatroidIndepQueryTree.depth,List.length_cons]
        have := ih kept
        have := ih (e::kept)
        omega
  12. c12

    e12

    lines 293–296

      | nil => simp [laminar_filter]
      | cons e es ih =>
        simp only [laminar_filter,MatroidIndepQueryTree.eval]
        cases h : o (q e) <;> simp [h,ih]
  13. c13

    e13

    lines 306–376

      classical
      by_cases hn : 2 ≤ n
      swap
      · refine ⟨.leaf true,by simp [MatroidIndepQueryTree.depth],fun M _ _ o _ => ?_⟩
        haveI : Subsingleton (Fin n) := Fin.subsingleton_iff_le_one.mpr (by omega)
        simp [MatroidIndepQueryTree.eval,matroid_connected_of_subsingleton M (fun _ _ _ _ => Subsingleton.elim _ _)]
      haveI : NeZero n := ⟨by omega⟩
      let all := (Finset.univ : Finset (Fin n)).toList
      let q (B : Finset (Fin n)) (e b : Fin n) := (insert e B).erase b
      let anchor (B : Finset (Fin n)) := if h : B.Nonempty then B.min' h else 0
      let scan (B : Finset (Fin n)) (rs : List (Fin n)) := nested_scan (q B) (n+1) rs B.toList
      let k (B : Finset (Fin n)) := laminar_filter (fun e => q B e (anchor B)) (scan B) [] Bᶜ.toList
      have ha : all.length = n := by simp [all]
      have hk (B) : (k B).depth ≤ n+(n+1) := by
        have h := laminar_filter_depth (fun e => q B e (anchor B)) (scan B) (n+1)
          (fun _ => nested_scan_depth _ _ _ _) Bᶜ.toList []
        exact h.trans (Nat.add_le_add_right (by simpa using Finset.card_le_univ Bᶜ) _)
      refine ⟨nested_loops (nested_basis k ∅ all) all,?_,?_⟩
      · rw [nested_loops_depth,ha]
        have h := nested_basis_depth k _ hk all ∅
        omega
      intro M hE hlam o ho
      have he (x : Fin n) : x ∈ M.E := hE ▸ trivial
      rw [nested_loops_eval]
      by_cases hloop : ∀ x : Fin n, o {x} = true
      swap
      · obtain ⟨x,hx⟩ := not_forall.mp hloop
        haveI : Nontrivial (Fin n) := Fin.nontrivial_iff_two_le.mpr hn
        obtain ⟨y,hy⟩ := exists_ne x
        have hdis := not_matroid_connected_of_not_indep_singleton M x y (he x) (he y) hy.symm
          (by simpa using mt (ho {x}).mpr hx)
        simp_all [all]
      have hsingle (x) (_ : x ∈ M.E) : M.Indep {x} := by simpa using (ho {x}).mp (hloop x)
      rw [and_iff_right (fun x _ => hloop x)]
      apply nested_basis_eval hE ho k _ all ∅ (by simp) (by simp [all])
      intro B hB
      have hne : B.Nonempty := by
        by_contra! hempty
        have hmax := hB.eq_of_subset_indep (hsingle 0 (he 0)) (show (B : Set (Fin n)) ⊆ {0} by simp [hempty])
        simpa [hempty] using hmax
      have haB : anchor B ∈ B := by simp [anchor,hne,Finset.min'_mem]
      have hside := laminar_side hn hE hB haB hlam hsingle
      let rs := ((Bᶜ.toList.filter (fun e => o (q B e (anchor B)))).reverse)
      have hor (e b : Fin n) (heB : e ∈ Bᶜ) : o (q B e b) = true ↔ b ∈ M.fundCircuit e B := by
        rw [ho,hB.indep.mem_fundCircuit_iff (by rw [hB.closure_eq]; exact he e) (Finset.mem_compl.mp heB)]
        simp [q]
      have hrows (e : Fin n) : e ∈ rs ↔ e ∈ Bᶜ.filter
          (fun f => anchor B ∈ B.filter (fun b => b ∈ M.fundCircuit f B)) := by
        simp only [rs,List.mem_reverse,List.mem_filter,Finset.mem_toList,Finset.mem_filter]
        exact and_congr_right (fun h => (hor e (anchor B) h).trans (and_iff_right haB).symm)
      have hlen' : rs.length ≤ Bᶜ.card := by
        simpa [rs] using (List.length_filter_le (p := fun e => o (q B e (anchor B))) Bᶜ.toList)
      have hlen : rs.length + B.toList.length < n+1 := by
        have hc := Finset.card_compl_add_card B
        simp only [Fintype.card_fin] at hc
        simp only [Finset.length_toList]; omega
      have hscan := nested_scan_correct (q B) o (n+1) rs B.toList hlen
      change (laminar_filter _ _ _ _).eval o = true ↔ _
      rw [laminar_filter_eval]
      simp only [List.append_nil]
      constructor
      · contrapose
        intro hdis
        obtain ⟨b,hb,hnone⟩ := hside.2 hdis
        exact hscan.2 ⟨b,by simpa using hb, fun e hem ho' =>
          hnone e ((hrows _).mp hem) (Finset.mem_filter.mpr ⟨hb,
            (hor e b (Finset.mem_filter.mp ((hrows _).mp hem)).1).mp ho'⟩)⟩
      · intro hcon
        obtain ⟨e,hem,hall⟩ := hside.1 hcon
        exact hscan.1 ⟨e,(hrows _).mpr hem,fun b hb => (hor e b (Finset.mem_filter.mp hem).1).mpr
          (Finset.mem_filter.mp (hall b (by simpa using hb))).2⟩

Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.

Lean source view module on GitHub

open Setopen scoped Classicalprivate theorem matroid_nonloop_common_circuit_in_circuit_closure {α : Type*} {M : Matroid α}    {C : Set α} {e x : α} (hC : M.IsCircuit C) (he : e ∈ C)    (hx : x ∈ M.closure C) (hi : M.Indep {x}) :    ∃ D, M.IsCircuit D ∧ e ∈ D ∧ x ∈ D := by  classical  by_cases hxC : x ∈ C  · exact ⟨C,hC,he,hxC⟩  let I := C \ {e}  let D := M.fundCircuit x I  have hI : M.Indep I := hC.sdiff_singleton_indep he  have hD : M.IsCircuit D := hI.fundCircuit_isCircuit    (by simpa [I,hC.closure_sdiff_singleton_eq] using hx) (fun h => hxC h.1)  have hb : ¬ D ⊆ {x} := fun h => hD.not_indep (hi.subset h)  rw [not_subset] at hb  obtain ⟨b,hbD,hbx⟩ := hb  have hbC : b ∈ C := by    rcases M.fundCircuit_subset_insert x I hbD with h | h    · exact (hbx h).elim    · exact h.1  let J := C \ {b}  let F := M.fundCircuit x J  have hJ : M.Indep J := hC.sdiff_singleton_indep hbC  have hF : M.IsCircuit F := hJ.fundCircuit_isCircuit    (by simpa [J,hC.closure_sdiff_singleton_eq] using hx) (fun h => hxC h.1)  refine ⟨F,hF,?_,M.mem_fundCircuit x J⟩  by_contra heF  have hFI : F ⊆ insert x I := by    intro y hy    rcases M.fundCircuit_subset_insert x J hy with h | h    · exact Or.inl h    · exact Or.inr ⟨h.1,fun hye => heF (hye ▸ hy)⟩  have hFD : F = D := hF.eq_fundCircuit_of_subset hI hFI  have hmem := M.fundCircuit_subset_insert x J (hFD.symm ▸ hbD)  exact hmem.elim hbx (fun h => h.2 rfl)private theorem nested_support {α : Type*} {M : Matroid α} {B : Set α}    (hB : M.IsBase B) {e : α} (he : e ∈ M.E) (hn : e ∉ B) :    M.closure (M.fundCircuit e B) ∩ B = M.fundCircuit e B ∩ B := by  have hC := hB.fundCircuit_isCircuit he hn  apply Subset.antisymm _ (inter_subset_inter_left B (M.subset_closure _ hC.subset_ground))  rintro b ⟨hbc,hb⟩  refine ⟨?_,hb⟩  by_contra hnC  rw [← hC.closure_sdiff_singleton_eq e, M.fundCircuit_sdiff_eq_inter hn] at hbc  exact hB.indep.notMem_closure_sdiff_of_mem hb    (M.closure_subset_closure (show M.fundCircuit e B ∩ B ⊆ B \ {b} from      fun y hy => ⟨hy.2, fun h => hnC (h ▸ hy.1)⟩) hbc)private theorem nested_covered {α : Type*} {M : Matroid α} {B : Set α} (hB : M.IsBase B)    {b : α} (hb : b ∈ B) (hc : ¬M.IsColoop b) :    ∃ e ∈ M.E \ B, b ∈ M.fundCircuit e B := by  simp only [Matroid.isColoop_iff_forall_mem_isBase,not_forall,exists_prop] at hc  obtain ⟨D,hD,hbd⟩ := hc  obtain ⟨e,he,hX⟩ := hB.exchange hD ⟨hb,hbd⟩  have heE := hD.subset_ground he.1  have heb : e ≠ b := fun h => he.2 (h ▸ hb)  refine ⟨e,⟨heE,he.2⟩,(hB.indep.mem_fundCircuit_iff (by rwa [hB.closure_eq]) he.2).mpr ?_⟩  simpa [← insert_sdiff_singleton_comm heb] using hX.indepprivate theorem laminar_side {n : ℕ} (hn : 2 ≤ n) {M : Matroid (Fin n)} (he : M.E = univ)    {B : Finset (Fin n)} (hB : M.IsBase (B : Set (Fin n))) {a : Fin n} (ha : a ∈ B)    (hlam : ∀ C D, M.IsCircuit C → M.IsCircuit D → (C ∩ D).Nonempty →      M.closure C ⊆ M.closure D ∨ M.closure D ⊆ M.closure C)    (hi : ∀ x ∈ M.E, M.Indep {x}) :    let supp (e : Fin n) := B.filter (fun b => b ∈ M.fundCircuit e B)    let rows := Bᶜ.filter (fun e => a ∈ supp e)    (matroid_connected M → ∃ e ∈ rows, ∀ b ∈ B, b ∈ supp e) ∧    (¬ matroid_connected M → ∃ b ∈ B, ∀ e ∈ rows, b ∉ supp e) := by  classical  intro supp rows  have hfull (e : Fin n) : e ∈ M.E := he ▸ trivial  haveI : Nontrivial (Fin n) := Fin.nontrivial_iff_two_le.mpr hn  have hS (x y : Fin n) (hx : x ∉ B) (hy : y ∉ B)      (le : M.closure (M.fundCircuit x B) ⊆ M.closure (M.fundCircuit y B)) :      supp x ⊆ supp y := by    intro b hb    obtain ⟨hbB,hbX⟩ := Finset.mem_filter.mp hb    have hb' : b ∈ M.closure (M.fundCircuit x B) ∩ (B : Set (Fin n)) := by      rw [nested_support hB (hfull x) hx]; exact ⟨hbX,hbB⟩    have hb'' : b ∈ M.closure (M.fundCircuit y B) ∩ (B : Set (Fin n)) := ⟨le hb'.1,hbB⟩    rw [nested_support hB (hfull y) hy] at hb''    exact Finset.mem_filter.mpr ⟨hbB,hb''.1⟩  have compare (e f b : Fin n) (heB : e ∉ B) (hfB : f ∉ B) (hb : b ∈ supp e) (hb' : b ∈ supp f) :      supp e ⊆ supp f ∨ supp f ⊆ supp e := by    exact (hlam _ _ (hB.fundCircuit_isCircuit (hfull e) heB) (hB.fundCircuit_isCircuit (hfull f) hfB)      ⟨b,(Finset.mem_filter.mp hb).2,(Finset.mem_filter.mp hb').2⟩).imp        (hS e f heB hfB) (hS f e hfB heB)  by_cases hne : rows.Nonempty  swap  · refine ⟨fun hconn => ?_,fun _ => ⟨a,ha,fun e he => (hne ⟨e,he⟩).elim⟩⟩    obtain ⟨z,hza⟩ := exists_ne a    have hncol : ¬M.IsColoop a := by      rcases hconn a (hfull a) z (hfull z) with heq | ⟨C,hC,hac,_⟩      · exact (hza heq.symm).elim      · exact fun h => (Matroid.isColoop_iff_forall_notMem_isCircuit (hfull a)).mp h hC hac    obtain ⟨e,⟨_,heB⟩,hae⟩ := nested_covered hB ha hncol    exact (hne ⟨e,Finset.mem_filter.mpr ⟨Finset.mem_compl.mpr heB,Finset.mem_filter.mpr ⟨ha,hae⟩⟩⟩).elim  obtain ⟨r,hr,hmax⟩ := Finset.exists_max_image rows (fun e => (supp e).card) hne  have hr' := Finset.mem_filter.mp hr  have hrB := Finset.mem_compl.mp hr'.1  have maximal (f : Fin n) (hf : f ∈ rows) : supp f ⊆ supp r := by    have hf' := Finset.mem_filter.mp hf    rcases compare f r a (Finset.mem_compl.mp hf'.1) hrB hf'.2 hr'.2 with le | le    · exact le    · exact (Finset.eq_of_subset_of_card_le le (hmax f hf)).ge  have hsub : supp r ⊆ B := Finset.filter_subset _ _  constructor  · intro hconn    refine ⟨r,hr,?_⟩    by_contra! hnfull    obtain ⟨b,hb,hbr⟩ := hnfull    have hproper : (supp r : Set (Fin n)) ⊂ (B : Set (Fin n)) :=      Set.ssubset_iff_subset_ne.mpr ⟨hsub,fun h => hbr (show b ∈ (supp r : Set (Fin n)) from h ▸ hb)⟩    apply (not_matroid_connected_of_fund_circuit_separated_finite M M.E.toFinite hB      (A := (supp r : Set (Fin n))) ⟨a,hr'.2⟩ hproper ?_) hconn    intro f hf hmeet x hx    obtain ⟨c,hcf,hcr⟩ := hmeet    have hSf : c ∈ supp f := Finset.mem_filter.mpr ⟨hsub hcr,hcf⟩    have hle : supp f ⊆ supp r := by      rcases compare f r c hf.2 hrB hSf hcr with le | le      · exact le      · exact maximal f (Finset.mem_filter.mpr ⟨Finset.mem_compl.mpr hf.2, le hr'.2⟩)    rcases M.fundCircuit_subset_insert f (B : Set (Fin n)) hx with hx' | hx'    · exact Or.inl hx'    · exact Or.inr (hle (Finset.mem_filter.mpr ⟨hx',hx⟩))  · intro hdisc    by_contra! hncol    have hallB (b : Fin n) (hb : b ∈ B) : b ∈ supp r := by      obtain ⟨e,he',hbe⟩ := hncol b hb      exact maximal e he' hbe    let C := M.fundCircuit r (B : Set (Fin n))    have hC : M.IsCircuit C := hB.fundCircuit_isCircuit (hfull r) hrB    have hBC : (B : Set (Fin n)) ⊆ C := fun b hb => (Finset.mem_filter.mp (hallB b hb)).2    have circ (x : Fin n) : ∃ D, M.IsCircuit D ∧ a ∈ D ∧ x ∈ D :=      matroid_nonloop_common_circuit_in_circuit_closure hC (hBC ha)        (M.closure_subset_closure hBC (hB.closure_eq.symm ▸ hfull x)) (hi x (hfull x))    apply hdisc    intro x hx y hy    right    obtain ⟨D,hD,haD,hxD⟩ := circ x    obtain ⟨F,hF,haF,hyF⟩ := circ y    rcases hlam D F hD hF ⟨a,haD,haF⟩ with le | le    · obtain ⟨G,hG,hyG,hxG⟩ := matroid_nonloop_common_circuit_in_circuit_closure hF hyF        (le (M.subset_closure _ hD.subset_ground hxD)) (hi x hx)      exact ⟨G,hG,hxG,hyG⟩    · exact matroid_nonloop_common_circuit_in_circuit_closure hD hxD        (le (M.subset_closure _ hF.subset_ground hyF)) (hi y hy)private def nested_scan {α : Type*} (q : α → α → Finset α) :    ℕ → List α → List α → MatroidIndepQueryTree α  | 0, _, _ => .leaf false  | _+1, _, [] => .leaf true  | _+1, [], _::_ => .leaf false  | t+1, r::rs, c::cs => .query (q r c) (nested_scan q t rs (c::cs)) (nested_scan q t (r::rs) cs)private theorem nested_scan_depth {α : Type*} (q : α → α → Finset α) (t : ℕ)    (rs cs : List α) : (nested_scan q t rs cs).depth ≤ t := by  induction t generalizing rs cs with  | zero => rfl  | succ t ih =>    cases cs with    | nil => simp [nested_scan,MatroidIndepQueryTree.depth]    | cons c cs =>      cases rs with      | nil => simp [nested_scan,MatroidIndepQueryTree.depth]      | cons r rs => simp [nested_scan,MatroidIndepQueryTree.depth, Nat.add_comm 1,          max_le_iff,ih]private theorem nested_scan_correct {α : Type*} (q : α → α → Finset α)    (o : Finset α → Bool) (t : ℕ) (rs cs : List α) (hlen : rs.length + cs.length < t) :    ((∃ r ∈ rs, ∀ c ∈ cs, o (q r c) = true) → (nested_scan q t rs cs).eval o = true) ∧    ((∃ c ∈ cs, ∀ r ∈ rs, o (q r c) ≠ true) → (nested_scan q t rs cs).eval o ≠ true) := by  induction t generalizing rs cs with  | zero => omega  | succ t ih =>    cases cs with    | nil => simp [nested_scan,MatroidIndepQueryTree.eval]    | cons c cs =>      cases rs with      | nil => simp [nested_scan,MatroidIndepQueryTree.eval]      | cons r rs =>        simp only [List.length_cons] at hlen        have ihN := ih rs (c::cs) (by simp only [List.length_cons]; omega)        have ihY := ih (r::rs) cs (by simp only [List.length_cons]; omega)        simp only [nested_scan,MatroidIndepQueryTree.eval]        split_ifs with ha        · constructor          · rintro ⟨s,hs,hu⟩            exact ihY.1 ⟨s,hs,fun z hz => hu z (by simp [hz])⟩          · rintro ⟨d,hd,hz⟩            apply ihY.2            exact ⟨d, (List.mem_cons.mp hd).resolve_left              (by rintro rfl; exact hz r (by simp) ha), hz⟩        · constructor          · rintro ⟨s,hs,hu⟩            apply ihN.1            exact ⟨s,(List.mem_cons.mp hs).resolve_left              (by rintro rfl; exact ha (hu c (by simp))), hu⟩          · rintro ⟨d,hd,hz⟩            exact ihN.2 ⟨d,hd,fun s hs => hz s (by simp [hs])⟩private def nested_basis {α : Type*} [DecidableEq α] (k : Finset α → MatroidIndepQueryTree α)    (I : Finset α) : List α → MatroidIndepQueryTree α  | [] => k I  | x::xs => .query (insert x I) (nested_basis k I xs) (nested_basis k (insert x I) xs)private theorem nested_basis_depth {α : Type*} [DecidableEq α] (k : Finset α → MatroidIndepQueryTree α)    (d : ℕ) (hk : ∀ I, (k I).depth ≤ d) (xs : List α) (I : Finset α) :    (nested_basis k I xs).depth ≤ xs.length + d := by  induction xs generalizing I with  | nil => simpa [nested_basis] using hk I  | cons x xs ih =>    simp only [nested_basis,MatroidIndepQueryTree.depth,List.length_cons]    have := ih I    have := ih (insert x I)    omegaprivate theorem nested_basis_eval {α : Type*} [DecidableEq α] {M : Matroid α}    (he : M.E = univ) {o : Finset α → Bool} (ho : ∀ Q, o Q = true ↔ M.Indep (Q : Set α))    (k : Finset α → MatroidIndepQueryTree α) {p : Prop}    (hk : ∀ B : Finset α, M.IsBase (B : Set α) → ((k B).eval o = true ↔ p))    (xs : List α) (I : Finset α) (hI : M.Indep (I : Set α))    (hcov : ∀ x, x ∈ xs ∨ x ∈ M.closure (I : Set α)) :    (nested_basis k I xs).eval o = true ↔ p := by  induction xs generalizing I with  | nil =>    apply hk _ (hI.isBase_of_ground_subset_closure ?_)    intro z _    exact (hcov z).resolve_left (by simp)  | cons x xs ih =>    simp only [nested_basis,MatroidIndepQueryTree.eval]    split_ifs with h    · apply ih _ ((ho _).mp h)      intro y      rcases hcov y with hmem | hcl      · rcases List.mem_cons.mp hmem with rfl | hmem        · right; apply M.subset_closure _ ((ho _).mp h).subset_ground; simp        · exact Or.inl hmem      · exact Or.inr (M.closure_subset_closure (by simp) hcl)    · apply ih _ hI      intro y      rcases hcov y with hmem | hcl      · rcases List.mem_cons.mp hmem with rfl | hmem        · right          apply hI.mem_closure_iff.mpr (Or.inl ?_)          apply M.dep_of_not_indep _ (he ▸ subset_univ _)          simpa using mt (ho (insert y I)).mpr h        · exact Or.inl hmem      · exact Or.inr hclprivate def nested_loops {α : Type*} (k : MatroidIndepQueryTree α) : List α → MatroidIndepQueryTree α  | [] => k  | x::xs => .query {x} (.leaf false) (nested_loops k xs)private theorem nested_loops_depth {α : Type*} (k : MatroidIndepQueryTree α) (xs : List α) :    (nested_loops k xs).depth = xs.length + k.depth := by  induction xs with  | nil => simp [nested_loops]  | cons x xs ih => simp [nested_loops,MatroidIndepQueryTree.depth,ih]; omegaprivate theorem nested_loops_eval {α : Type*} (k : MatroidIndepQueryTree α) (xs : List α)    (o : Finset α → Bool) :    (nested_loops k xs).eval o = true ↔ (∀ x ∈ xs, o {x} = true) ∧ k.eval o = true := by  induction xs with  | nil => simp [nested_loops]  | cons x xs ih =>    simp only [nested_loops,MatroidIndepQueryTree.eval]    split_ifs <;> simp_allprivate def laminar_filter {α : Type*} (q : α → Finset α) (k : List α → MatroidIndepQueryTree α)    (kept : List α) : List α → MatroidIndepQueryTree α  | [] => k kept  | e::es => .query (q e) (laminar_filter q k kept es) (laminar_filter q k (e::kept) es)private theorem laminar_filter_depth {α : Type*} (q : α → Finset α)    (k : List α → MatroidIndepQueryTree α) (d : ℕ) (hk : ∀ ys, (k ys).depth ≤ d)    (es kept : List α) : (laminar_filter q k kept es).depth ≤ es.length + d := by  induction es generalizing kept with  | nil => simpa [laminar_filter] using hk kept  | cons e es ih =>    simp only [laminar_filter,MatroidIndepQueryTree.depth,List.length_cons]    have := ih kept    have := ih (e::kept)    omegaprivate theorem laminar_filter_eval {α : Type*} (q : α → Finset α)    (k : List α → MatroidIndepQueryTree α) (o : Finset α → Bool) (es kept : List α) :    (laminar_filter q k kept es).eval o = (k ((es.filter (fun e => o (q e))).reverse ++ kept)).eval o := by  induction es generalizing kept with  | nil => simp [laminar_filter]  | cons e es ih =>    simp only [laminar_filter,MatroidIndepQueryTree.eval]    cases h : o (q e) <;> simp [h,ih]theorem matroid_laminar_connectedness_query_linear (n : ℕ) :    ∃ T : MatroidIndepQueryTree (Fin n), T.depth ≤ 4*n+1 ∧      ∀ M : Matroid (Fin n), M.E = Set.univ →        (∀ C D, M.IsCircuit C → M.IsCircuit D → (C ∩ D).Nonempty →          M.closure C ⊆ M.closure D ∨ M.closure D ⊆ M.closure C) →        ∀ o : Finset (Fin n) → Bool,          (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) →          (T.eval o = true ↔ matroid_connected M) := by  classical  by_cases hn : 2 ≤ n  swap  · refine ⟨.leaf true,by simp [MatroidIndepQueryTree.depth],fun M _ _ o _ => ?_⟩    haveI : Subsingleton (Fin n) := Fin.subsingleton_iff_le_one.mpr (by omega)    simp [MatroidIndepQueryTree.eval,matroid_connected_of_subsingleton M (fun _ _ _ _ => Subsingleton.elim _ _)]  haveI : NeZero n := ⟨by omega⟩  let all := (Finset.univ : Finset (Fin n)).toList  let q (B : Finset (Fin n)) (e b : Fin n) := (insert e B).erase b  let anchor (B : Finset (Fin n)) := if h : B.Nonempty then B.min' h else 0  let scan (B : Finset (Fin n)) (rs : List (Fin n)) := nested_scan (q B) (n+1) rs B.toList  let k (B : Finset (Fin n)) := laminar_filter (fun e => q B e (anchor B)) (scan B) [] Bᶜ.toList  have ha : all.length = n := by simp [all]  have hk (B) : (k B).depth ≤ n+(n+1) := by    have h := laminar_filter_depth (fun e => q B e (anchor B)) (scan B) (n+1)      (fun _ => nested_scan_depth _ _ _ _) Bᶜ.toList []    exact h.trans (Nat.add_le_add_right (by simpa using Finset.card_le_univ Bᶜ) _)  refine ⟨nested_loops (nested_basis k ∅ all) all,?_,?_⟩  · rw [nested_loops_depth,ha]    have h := nested_basis_depth k _ hk all ∅    omega  intro M hE hlam o ho  have he (x : Fin n) : x ∈ M.E := hE ▸ trivial  rw [nested_loops_eval]  by_cases hloop : ∀ x : Fin n, o {x} = true  swap  · obtain ⟨x,hx⟩ := not_forall.mp hloop    haveI : Nontrivial (Fin n) := Fin.nontrivial_iff_two_le.mpr hn    obtain ⟨y,hy⟩ := exists_ne x    have hdis := not_matroid_connected_of_not_indep_singleton M x y (he x) (he y) hy.symm      (by simpa using mt (ho {x}).mpr hx)    simp_all [all]  have hsingle (x) (_ : x ∈ M.E) : M.Indep {x} := by simpa using (ho {x}).mp (hloop x)  rw [and_iff_right (fun x _ => hloop x)]  apply nested_basis_eval hE ho k _ all ∅ (by simp) (by simp [all])  intro B hB  have hne : B.Nonempty := by    by_contra! hempty    have hmax := hB.eq_of_subset_indep (hsingle 0 (he 0)) (show (B : Set (Fin n)) ⊆ {0} by simp [hempty])    simpa [hempty] using hmax  have haB : anchor B ∈ B := by simp [anchor,hne,Finset.min'_mem]  have hside := laminar_side hn hE hB haB hlam hsingle  let rs := ((Bᶜ.toList.filter (fun e => o (q B e (anchor B)))).reverse)  have hor (e b : Fin n) (heB : e ∈ Bᶜ) : o (q B e b) = true ↔ b ∈ M.fundCircuit e B := by    rw [ho,hB.indep.mem_fundCircuit_iff (by rw [hB.closure_eq]; exact he e) (Finset.mem_compl.mp heB)]    simp [q]  have hrows (e : Fin n) : e ∈ rs ↔ e ∈ Bᶜ.filter      (fun f => anchor B ∈ B.filter (fun b => b ∈ M.fundCircuit f B)) := by    simp only [rs,List.mem_reverse,List.mem_filter,Finset.mem_toList,Finset.mem_filter]    exact and_congr_right (fun h => (hor e (anchor B) h).trans (and_iff_right haB).symm)  have hlen' : rs.length ≤ Bᶜ.card := by    simpa [rs] using (List.length_filter_le (p := fun e => o (q B e (anchor B))) Bᶜ.toList)  have hlen : rs.length + B.toList.length < n+1 := by    have hc := Finset.card_compl_add_card B    simp only [Fintype.card_fin] at hc    simp only [Finset.length_toList]; omega  have hscan := nested_scan_correct (q B) o (n+1) rs B.toList hlen  change (laminar_filter _ _ _ _).eval o = true ↔ _  rw [laminar_filter_eval]  simp only [List.append_nil]  constructor  · contrapose    intro hdis    obtain ⟨b,hb,hnone⟩ := hside.2 hdis    exact hscan.2 ⟨b,by simpa using hb, fun e hem ho' =>      hnone e ((hrows _).mp hem) (Finset.mem_filter.mpr ⟨hb,        (hor e b (Finset.mem_filter.mp ((hrows _).mp hem)).1).mp ho'⟩)⟩  · intro hcon    obtain ⟨e,hem,hall⟩ := hside.1 hcon    exact hscan.1 ⟨e,(hrows _).mpr hem,fun b hb => (hor e b (Finset.mem_filter.mp hem).1).mpr      (Finset.mem_filter.mp (hall b (by simpa using hb))).2⟩
See it in the graphDiscuss this resultChallenge it