AFTD Auto-Formalizing Theoretical Domains

matroid_coloop_detection_log_log

theoremverified

There is a uniform deterministic O(n log log(n+2)) independence-query algorithm to decide if an arbitrary n-element matroid has no coloops. In the hidden full-fiber family this decides the coverage bottleneck, as its holes are exactly its coloops.

Statement

theorem matroid_coloop_detection_log_log : ∃ C : ℕ, ∀ n : ℕ, ∃ T : MatroidIndepQueryTree (Fin n), T.depth ≤ C*n*(1 + Nat.log2 (1 + Nat.log2 (n+2))) ∧ ∀ M : Matroid (Fin n), M.E = Set.univ → ∀ o : Finset (Fin n) → Bool, (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) → (T.eval o = true ↔ ∀ x : Fin n, ¬M.IsColoop x)
Source
Open problem OP-1998 and Theorem 4.1 of https://arxiv.org/abs/2611.23624. Experiment 44437; state/campaigns/matroid-in1302/coloop.md gives the adaptive algorithm and its query analysis.
Verified
29 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 6
  • 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…
  • LinearOrder.toTotalPreorderEvery LinearOrder is a TotalPreorder.
  • TotalPreorderA total preorder: a preorder where ≤ is total (complete). This is weaker than LinearOrder — it does NOT require antisymmetry or…
  • g$g(S,M) = \dim_{\mathbb{F}_p}(S^{\perp}_M) - \dim_{\mathbb{F}_p}(S_M)$.

The proof, explained

The theorem shows that testing whether an n-element matroid has no coloops can be solved deterministically with O(n log log(n+2)) independence queries. By matroid duality, coloops in M are loops in M*, and independence of Q in M is equivalent to spanning of E \ Q in M*, translating independence queries into dual spanning queries. The decision problem is solved by a recursive split-and-peel algorithm that splits the ground set when the basis frame is small and peels an independent subset using divide-and-conquer block search when the frame is large, achieving the optimal O(n log log n) complexity when parameters are balanced.

  1. Sequential composition of query programs preserves semantics and adds query costs. automation

    By structural induction on the program p. A return step costs 0 and evaluates immediately; a query step evaluates the queried branch and adds 1 to the cost, matching the definition of cost additivity via linear arithmetic.

    lines 23–33

        (p.bind f).val σ = (f (p.val σ)).val σ := by
      induction p with
      | ret b => rfl
      | ask T k ih => simp only [bind, val, ih]
    
    theorem cost_bind (σ : Finset α → Bool) (p : Prog α β) (f : β → Prog α γ) :
        (p.bind f).cost σ = p.cost σ + (f (p.val σ)).cost σ := by
      induction p with
      | ret b => simp [bind, cost, val]
      | ask T k ih => simp only [bind, cost, val, ih]; omega
    
  2. Basic rank properties hold, and relative independence transfers to subsets.

    The empty set has rank 0 by the cardinality axiom. Subadditivity ρ(X ∪ Y) ≤ ρ(X) + |Y| follows from submodularity and the cardinality bound. If K is independent relative to Y (i.e. ρ(Y ∪ K) = ρ(Y) + |K|), then submodularity shows K is independent over any subset Z ⊆ Y, and partitioning Y ∪ K = (Y ∪ K') ∪ (K \ K') shows any sub-collection K' ⊆ K is also independent over Y.

    lines 50–77

    uses Finset.card_sdiff_add_card_eq_card

    theorem r_empty : ρ.r ∅ = 0 := by have := ρ.card ∅; simpa using this
    theorem le_R (X : Finset α) : ρ.r X ≤ ρ.R := ρ.mono (Finset.subset_univ X)
    theorem union_le (X Y : Finset α) : ρ.r (X ∪ Y) ≤ ρ.r X + ρ.r Y := by
      have := ρ.sub X Y; omega
    theorem union_le_card (X Y : Finset α) : ρ.r (X ∪ Y) ≤ ρ.r X + Y.card := by
      have := ρ.union_le X Y; have := ρ.card Y; omega
    
    theorem indep_over_sub {Y K Z : Finset α} (hZ : Z ⊆ Y) (h : ρ.r (Y ∪ K) = ρ.r Y + K.card) :
        ρ.r (Z ∪ K) = ρ.r Z + K.card := by
      have h1 := ρ.sub (Z ∪ K) Y
      have e1 : Z ∪ K ∪ Y = Y ∪ K := by
        ext x; simp only [Finset.mem_union]; have := @hZ x; tauto
      have e2 : ρ.r Z ≤ ρ.r ((Z ∪ K) ∩ Y) := ρ.mono (by
        intro x hx; simp only [Finset.mem_inter, Finset.mem_union]; exact ⟨Or.inl hx, hZ hx⟩)
      rw [e1] at h1
      have := ρ.union_le_card Z K
      omega
    
    theorem indep_over_subK {Y K K' : Finset α} (hK : K' ⊆ K) (h : ρ.r (Y ∪ K) = ρ.r Y + K.card) :
        ρ.r (Y ∪ K') = ρ.r Y + K'.card := by
      have e : Y ∪ K = (Y ∪ K') ∪ (K \ K') := by
        ext x; simp only [Finset.mem_union, Finset.mem_sdiff]; have := @hK x; tauto
      have h1 := ρ.union_le_card (Y ∪ K') (K \ K')
      rw [← e] at h1
      have h2 := Finset.card_sdiff_add_card_eq_card hK
      have h3 := ρ.union_le_card Y K'
      omega
    
  3. A spanning set whose elements are all essential is independent relative to the contracted set.

    We prove by induction on finite subsets K' ⊆ K that ρ(Q) + |K'| ≤ ρ(Q ∪ K'). For the induction step, submodularity on Q ∪ (insert a s) and Q ∪ (K \ {a}) combined with the assumption that deleting any single element drops the rank strictly below R implies the rank increases by at least 1 at each insertion.

    lines 78–119

    uses Finset.card_insert_of_notMem

    theorem indep_of_ess {Q K : Finset α} (hs : ρ.r (Q ∪ K) = ρ.R)
        (he : ∀ f ∈ K, ρ.r (Q ∪ K.erase f) < ρ.R) : ρ.r (Q ∪ K) = ρ.r Q + K.card := by
      have key : ∀ K' : Finset α, K' ⊆ K → ρ.r Q + K'.card ≤ ρ.r (Q ∪ K') := by
        intro K'
        induction K' using Finset.induction_on with
        | empty => intro _; simp
        | insert a s ha ih =>
          intro hsub
          have haK : a ∈ K := hsub (Finset.mem_insert_self a s)
          have hsK : s ⊆ K.erase a := by
            intro x hx; rw [Finset.mem_erase]
            exact ⟨fun h => ha (h ▸ hx), hsub (Finset.mem_insert_of_mem hx)⟩
          have ih' := ih (fun x hx => Finset.mem_of_mem_erase (hsK hx))
          have h1 := ρ.sub (Q ∪ insert a s) (Q ∪ K.erase a)
          have e1 : Q ∪ insert a s ∪ (Q ∪ K.erase a) = Q ∪ K := by
            ext x; simp only [Finset.mem_union, Finset.mem_insert, Finset.mem_erase]
            constructor
            · rintro ((h | h | h) | h | h)
              · exact Or.inl h
              · exact Or.inr (h ▸ haK)
              · exact Or.inr (hsub (Finset.mem_insert_of_mem h))
              · exact Or.inl h
              · exact Or.inr h.2
            · rintro (h | h)
              · exact Or.inl (Or.inl h)
              · by_cases hx : x = a
                · exact Or.inl (Or.inr (Or.inl hx))
                · exact Or.inr (Or.inr ⟨hx, h⟩)
          have e2 : ρ.r (Q ∪ s) ≤ ρ.r ((Q ∪ insert a s) ∩ (Q ∪ K.erase a)) := ρ.mono (by
            intro x hx
            simp only [Finset.mem_inter, Finset.mem_union, Finset.mem_insert] at hx ⊢
            rcases hx with hx | hx
            · exact ⟨Or.inl hx, Or.inl hx⟩
            · exact ⟨Or.inr (Or.inr hx), Or.inr (hsK hx)⟩)
          rw [e1, hs] at h1
          have h3 := he a haK
          rw [Finset.card_insert_of_notMem ha]
          omega
      have := key K le_rfl
      have := ρ.union_le_card Q K
      omega
    
  4. Greedy shrinking extracts an essential spanning frame in queries equal to the list length.

    Induction on the candidate list fs. For each element f, we query whether Q ∪ (K \ {f}) still spans. If it spans, f is discarded; otherwise f is kept. Both branches maintain the invariant that all non-candidate elements are essential, requiring exactly |fs| queries in total.

    lines 126–158

    uses Finset.erase_subset

    theorem shrink_spec (Q : Finset α) : ∀ (fs : List α) (K : Finset α), ρ.r (Q ∪ K) = ρ.R →
        (∀ g ∈ K, g ∉ fs → ρ.r (Q ∪ K.erase g) < ρ.R) →
        (shrinkP Q fs K).val ρ.span ⊆ K ∧ ρ.r (Q ∪ (shrinkP Q fs K).val ρ.span) = ρ.R ∧
        (∀ g ∈ (shrinkP Q fs K).val ρ.span,
          ρ.r (Q ∪ ((shrinkP Q fs K).val ρ.span).erase g) < ρ.R) ∧
        (shrinkP Q fs K).cost ρ.span = fs.length := by
      intro fs
      induction fs with
      | nil => intro K h1 h2; simp only [shrinkP, val, cost, List.length_nil]; exact ⟨le_rfl, h1, fun g hg => h2 g hg (by simp), trivial⟩
      | cons f fs ih =>
        intro K h1 h2
        by_cases hb : ρ.r (Q ∪ K.erase f) = ρ.R
        · have hsp : ρ.span (Q ∪ K.erase f) = true := decide_eq_true hb
          simp only [shrinkP, val, cost, hsp, if_true, List.length_cons]
          obtain ⟨a, b, c, d⟩ := ih (K.erase f) hb (by
            intro g hg hgn
            have hgf : g ≠ f := (Finset.mem_erase.mp hg).1
            have hgK := (Finset.mem_erase.mp hg).2
            have := h2 g hgK (by simp [hgf, hgn])
            have hm : ρ.r (Q ∪ (K.erase f).erase g) ≤ ρ.r (Q ∪ K.erase g) :=
              ρ.mono (Finset.union_subset_union le_rfl (by
                intro x hx; simp only [Finset.mem_erase] at hx ⊢; exact ⟨hx.1, hx.2.2⟩))
            omega)
          exact ⟨a.trans (Finset.erase_subset _ _), b, c, by omega⟩
        · have hsp : ρ.span (Q ∪ K.erase f) = false := decide_eq_false hb
          simp only [shrinkP, val, cost, hsp, Bool.false_eq_true, if_false, List.length_cons]
          obtain ⟨a, b, c, d⟩ := ih K h1 (by
            intro g hg hgn
            by_cases hgf : g = f
            · subst hgf; have := ρ.le_R (Q ∪ K.erase g); omega
            · exact h2 g hg (by simp [hgf, hgn]))
          exact ⟨a, b, c, by omega⟩
    
  5. Divide-and-conquer block deletion finds essential elements of a block of size ≤ 2^h in at most 2(h+1)k + 1 queries.

    By strong induction on the block length. If removing the entire block preserves spanning, it is discarded in 1 query. If not and the block has length 1, the single element is essential. Otherwise, the block is split into two halves of size ≤ 2^(h-1) and each is visited recursively. The costs sum to 2h k_1 + 1 + 2h k_2 + 1 + 1 ≤ 2(h+1)(k_1 + k_2) + 1 by disjointness of the retained sets.

    lines 169–280

    uses Finset.card_union_of_disjoint

    termination_by block.length
    decreasing_by
      all_goals simp only [List.length_take, List.length_drop, List.length_cons]
      · omega
      · have : bs.length ≠ 0 := by simpa using h
        omega
    
    theorem visit_spec (P : Finset α) : ∀ (m : ℕ) (block : List α), block.length = m →
        ∀ (h : ℕ) (kept : Finset α), block.Nodup → block.length ≤ 2 ^ h → ρ.r (kept ∪ P) = ρ.R →
        kept \ block.toFinset ⊆ (visitP P block kept).val ρ.span ∧
        (visitP P block kept).val ρ.span ⊆ kept ∧
        ρ.r ((visitP P block kept).val ρ.span ∪ P) = ρ.R ∧
        (∀ d ∈ (visitP P block kept).val ρ.span, d ∈ block →
          ρ.r (((visitP P block kept).val ρ.span).erase d ∪ P) < ρ.R) ∧
        (visitP P block kept).cost ρ.span ≤
          2 * (h + 1) * (block.toFinset ∩ (visitP P block kept).val ρ.span).card + 1 := by
      intro m
      induction m using Nat.strong_induction_on with
      | _ m IH =>
      intro block hm h kept hnd hlen hkept
      match block, hm, hnd, hlen with
      | [], _, _, _ =>
        simp only [visitP, val, cost, List.toFinset_nil, Finset.sdiff_empty, List.not_mem_nil]
        exact ⟨le_rfl, le_rfl, hkept, fun _ _ h => h.elim, by omega⟩
      | b :: bs, hm, hnd, hlen =>
        by_cases ht : ρ.r ((kept \ (b :: bs).toFinset) ∪ P) = ρ.R
        · have hsp : ρ.span ((kept \ (b :: bs).toFinset) ∪ P) = true := decide_eq_true ht
          rw [visitP]; simp only [val, cost, hsp, if_true]
          refine ⟨le_rfl, Finset.sdiff_subset, ht, ?_, by omega⟩
          intro d hd hdb
          exact absurd (List.mem_toFinset.mpr hdb) (Finset.mem_sdiff.mp hd).2
        · have hsp : ρ.span ((kept \ (b :: bs).toFinset) ∪ P) = false := decide_eq_false ht
          have hlt : ρ.r ((kept \ (b :: bs).toFinset) ∪ P) < ρ.R := by
            have := ρ.le_R ((kept \ (b :: bs).toFinset) ∪ P); omega
          by_cases hbs : bs = []
          · subst hbs
            rw [visitP]; simp only [val, cost, hsp, Bool.false_eq_true, if_false, dif_pos]
            refine ⟨Finset.sdiff_subset, le_rfl, hkept, ?_, by omega⟩
            intro d hd hdb
            simp only [List.mem_singleton] at hdb; subst hdb
            have : kept.erase d = kept \ [d].toFinset := by ext x; simp [and_comm]
            rw [this]; exact hlt
          · rw [visitP]; simp only [val, cost, hsp, Bool.false_eq_true, if_false, dif_neg hbs]
            rw [val_bind, cost_bind]
            set l := b :: bs with hl
            set q := (bs.length + 1) / 2
            have hlen' : l.length = bs.length + 1 := by simp [hl]
            have hbs1 : bs.length ≠ 0 := by simpa using hbs
            obtain ⟨h', rfl⟩ : ∃ h', h = h' + 1 := by
              cases h with
              | zero => simp at hlen; omega
              | succ h' => exact ⟨h', rfl⟩
            have hpow : 2 ^ (h' + 1) = 2 * 2 ^ h' := by ring
            set A := l.take q
            set B := l.drop q
            have hAB : A ++ B = l := List.take_append_drop q l
            have hndAB : (A ++ B).Nodup := hAB ▸ hnd
            have hAnd : A.Nodup := (List.nodup_append.mp hndAB).1
            have hBnd : B.Nodup := (List.nodup_append.mp hndAB).2.1
            have hdisj : ∀ x ∈ A, x ∉ B := fun x hx hx' =>
              (List.nodup_append.mp hndAB).2.2 x hx x hx' rfl
            have hAlen : A.length ≤ 2 ^ h' := by simp [A, List.length_take]; omega
            have hBlen : B.length ≤ 2 ^ h' := by simp [B, List.length_drop]; omega
            have hAm : A.length < m := by simp [A, List.length_take]; omega
            have hBm : B.length < m := by simp [B, List.length_drop]; omega
            obtain ⟨a1, a2, a3, a4, a5⟩ := IH _ hAm A rfl h' kept hAnd hAlen hkept
            set k1 := (visitP P A kept).val ρ.span
            obtain ⟨b1, b2, b3, b4, b5⟩ := IH _ hBm B rfl h' k1 hBnd hBlen a3
            set k2 := (visitP P B k1).val ρ.span
            have hlset : l.toFinset = A.toFinset ∪ B.toFinset := by
              rw [← hAB, List.toFinset_append]
            refine ⟨?_, b2.trans a2, b3, ?_, ?_⟩
            · intro x hx
              rw [hlset, Finset.mem_sdiff, Finset.mem_union, not_or] at hx
              exact b1 (Finset.mem_sdiff.mpr ⟨a1 (Finset.mem_sdiff.mpr ⟨hx.1, hx.2.1⟩), hx.2.2⟩)
            · intro d hd hdl
              rw [← hAB, List.mem_append] at hdl
              rcases hdl with hdA | hdB
              · have := a4 d (b2 hd) hdA
                have hm2 : ρ.r (k2.erase d ∪ P) ≤ ρ.r (k1.erase d ∪ P) :=
                  ρ.mono (Finset.union_subset_union (Finset.erase_subset_erase d b2) le_rfl)
                omega
              · exact b4 d hd hdB
            · have eA : A.toFinset ∩ k1 = A.toFinset ∩ k2 := by
                ext x; simp only [Finset.mem_inter, List.mem_toFinset]
                constructor
                · rintro ⟨hxA, hx1⟩
                  exact ⟨hxA, b1 (Finset.mem_sdiff.mpr ⟨hx1, List.mem_toFinset.not.mpr (hdisj x hxA)⟩)⟩
                · rintro ⟨hxA, hx2⟩; exact ⟨hxA, b2 hx2⟩
              have eL : l.toFinset ∩ k2 = (A.toFinset ∩ k2) ∪ (B.toFinset ∩ k2) := by
                rw [hlset, Finset.union_inter_distrib_right]
              have hdj : Disjoint (A.toFinset ∩ k2) (B.toFinset ∩ k2) := by
                rw [Finset.disjoint_left]; intro x hx hx'
                exact hdisj x (List.mem_toFinset.mp (Finset.mem_inter.mp hx).1)
                  (List.mem_toFinset.mp (Finset.mem_inter.mp hx').1)
              have ecard : (l.toFinset ∩ k2).card = (A.toFinset ∩ k2).card + (B.toFinset ∩ k2).card := by
                rw [eL, Finset.card_union_of_disjoint hdj]
              have hpos : 1 ≤ (l.toFinset ∩ k2).card := by
                by_contra hc
                have hempty : l.toFinset ∩ k2 = ∅ := Finset.card_eq_zero.mp (by omega)
                have hsub : k2 ⊆ kept \ l.toFinset := by
                  intro x hx
                  refine Finset.mem_sdiff.mpr ⟨a2 (b2 hx), fun hxl => ?_⟩
                  have : x ∈ l.toFinset ∩ k2 := Finset.mem_inter.mpr ⟨hxl, hx⟩
                  rw [hempty] at this; exact absurd this (Finset.notMem_empty x)
                have := ρ.mono (Finset.union_subset_union hsub (le_refl P))
                omega
              rw [eA] at a5
              rw [ecard]
              rw [ecard] at hpos
              nlinarith
    
  6. Chunked extraction of independent elements using visitP runs in at most 2(h+1)k + w queries.

    By strong induction on list length. The list is partitioned into chunks of size c ≤ 2^h. On each chunk of size c, visitP is executed, and extractP recurses on the remainder with a reduced budget w - 1. Summing the query costs yields 2(h+1)k + w.

    lines 286–372

    uses List.take_append_drop

    termination_by l.length
    decreasing_by
      simp only [List.length_drop]
      have h1 : l ≠ [] := fun e => h (Or.inl e)
      have h2 : c ≠ 0 := fun e => h (Or.inr e)
      have : l.length ≠ 0 := by simpa using h1
      omega
    
    theorem extract_spec (P : Finset α) (c h : ℕ) (hc : 1 ≤ c) (hch : c ≤ 2 ^ h) :
        ∀ (m : ℕ) (l : List α), l.length = m → ∀ (kept : Finset α) (w : ℕ), l.Nodup →
        l.length ≤ c * w → ρ.r (kept ∪ P) = ρ.R →
        kept \ l.toFinset ⊆ (extractP P c l kept).val ρ.span ∧
        (extractP P c l kept).val ρ.span ⊆ kept ∧
        ρ.r ((extractP P c l kept).val ρ.span ∪ P) = ρ.R ∧
        (∀ d ∈ (extractP P c l kept).val ρ.span, d ∈ l →
          ρ.r (((extractP P c l kept).val ρ.span).erase d ∪ P) < ρ.R) ∧
        (extractP P c l kept).cost ρ.span ≤
          2 * (h + 1) * (l.toFinset ∩ (extractP P c l kept).val ρ.span).card + w := by
      intro m
      induction m using Nat.strong_induction_on with
      | _ m IH =>
      intro l hm kept w hnd hlen hkept
      by_cases hl : l = []
      · subst hl
        rw [extractP]; simp only [true_or, dif_pos, val, cost, List.toFinset_nil, Finset.sdiff_empty,
          List.not_mem_nil]
        exact ⟨le_rfl, le_rfl, hkept, fun _ _ h => h.elim, by omega⟩
      · have hc0 : c ≠ 0 := by omega
        have hne : ¬ (l = [] ∨ c = 0) := by tauto
        rw [extractP]; simp only [dif_neg hne]
        rw [val_bind, cost_bind]
        set A := l.take c
        set B := l.drop c
        have hAB : A ++ B = l := List.take_append_drop c l
        have hndAB : (A ++ B).Nodup := hAB ▸ hnd
        have hAnd : A.Nodup := (List.nodup_append.mp hndAB).1
        have hBnd : B.Nodup := (List.nodup_append.mp hndAB).2.1
        have hdisj : ∀ x ∈ A, x ∉ B := fun x hx hx' =>
          (List.nodup_append.mp hndAB).2.2 x hx x hx' rfl
        have hl0 : l.length ≠ 0 := by simpa using hl
        have hw : 1 ≤ w := by
          rcases Nat.eq_zero_or_pos w with h0 | h0
          · subst h0; rw [Nat.mul_zero] at hlen; omega
          · exact h0
        have hAlen : A.length ≤ 2 ^ h := (List.length_take_le c l).trans hch
        have hBlen : B.length ≤ c * (w - 1) := by
          simp only [B, List.length_drop]
          have : c * (w - 1) = c * w - c := by rw [Nat.mul_sub_one]
          omega
        have hBm : B.length < m := by simp [B, List.length_drop]; omega
        obtain ⟨a1, a2, a3, a4, a5⟩ := ρ.visit_spec P A.length A rfl h kept hAnd hAlen hkept
        set k1 := (visitP P A kept).val ρ.span
        obtain ⟨b1, b2, b3, b4, b5⟩ := IH _ hBm B rfl k1 (w - 1) hBnd hBlen a3
        set k2 := (extractP P c B k1).val ρ.span
        have hlset : l.toFinset = A.toFinset ∪ B.toFinset := by
          rw [← hAB, List.toFinset_append]
        refine ⟨?_, b2.trans a2, b3, ?_, ?_⟩
        · intro x hx
          rw [hlset, Finset.mem_sdiff, Finset.mem_union, not_or] at hx
          exact b1 (Finset.mem_sdiff.mpr ⟨a1 (Finset.mem_sdiff.mpr ⟨hx.1, hx.2.1⟩), hx.2.2⟩)
        · intro d hd hdl
          rw [← hAB, List.mem_append] at hdl
          rcases hdl with hdA | hdB
          · have := a4 d (b2 hd) hdA
            have hm2 : ρ.r (k2.erase d ∪ P) ≤ ρ.r (k1.erase d ∪ P) :=
              ρ.mono (Finset.union_subset_union (Finset.erase_subset_erase d b2) le_rfl)
            omega
          · exact b4 d hd hdB
        · have eA : A.toFinset ∩ k1 = A.toFinset ∩ k2 := by
            ext x; simp only [Finset.mem_inter, List.mem_toFinset]
            constructor
            · rintro ⟨hxA, hx1⟩
              exact ⟨hxA, b1 (Finset.mem_sdiff.mpr ⟨hx1, List.mem_toFinset.not.mpr (hdisj x hxA)⟩)⟩
            · rintro ⟨hxA, hx2⟩; exact ⟨hxA, b2 hx2⟩
          have eL : l.toFinset ∩ k2 = (A.toFinset ∩ k2) ∪ (B.toFinset ∩ k2) := by
            rw [hlset, Finset.union_inter_distrib_right]
          have hdj : Disjoint (A.toFinset ∩ k2) (B.toFinset ∩ k2) := by
            rw [Finset.disjoint_left]; intro x hx hx'
            exact hdisj x (List.mem_toFinset.mp (Finset.mem_inter.mp hx).1)
              (List.mem_toFinset.mp (Finset.mem_inter.mp hx').1)
          have ecard : (l.toFinset ∩ k2).card = (A.toFinset ∩ k2).card + (B.toFinset ∩ k2).card := by
            rw [eL, Finset.card_union_of_disjoint hdj]
          rw [eA] at a5
          rw [ecard]
          have hw1 : w - 1 + 1 = w := Nat.sub_add_cancel hw
          nlinarith
    
  7. Any element in a relatively independent set is not a loop. automation

    If ρ(P ∪ D) = ρ(P) + |D|, then restricting to the singleton {d} ⊆ D gives ρ(P ∪ {d}) = ρ(P) + 1. Since ρ(P ∪ {d}) ≤ ρ(P) + ρ({d}), we have ρ({d}) ≥ 1 ≠ 0.

    lines 411–416

    theorem nonloop_of_indep {P D : Finset α} (h : ρ.r (P ∪ D) = ρ.r P + D.card) {d : α} (hd : d ∈ D) :
        ρ.r {d} ≠ 0 := by
      have := ρ.indep_over_subK (Finset.singleton_subset_iff.mpr hd) h
      have := ρ.union_le P {d}
      simp at *; omega
    
  8. Restricting to a child block with a shrunk frame preserves the algorithmic invariant.

    Shrinking the frame F against A ∪ P yields an essential subset K ⊆ F such that (A ∪ P) ∪ K spans, and K is independent over P. Contracting K reduces the frame to F \ K of size |F| - |K|, and loops in A over P ∪ K are already loops over P, satisfying the invariant Inv at query cost |F|.

    lines 418–473

    uses Finset.card_sdiff_of_subset, List.toFinset_card_of_nodup

    theorem child_spec (P : Finset α) (A F : List α) (hA : A.Nodup) (hF : F.Nodup)
        (hdis : Disjoint A.toFinset P) (hloop : ∀ x ∈ A, ρ.r (P ∪ {x}) = ρ.r P → ρ.r {x} = 0)
        (hFs : ρ.r (F.toFinset ∪ P) = ρ.R) (hFr : ρ.r P + F.length = ρ.R) :
        ρ.Inv (P ∪ (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span) A
          (F.filter (fun x => decide (x ∉ (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span))) ∧
        (shrinkP (A.toFinset ∪ P) F F.toFinset).cost ρ.span = F.length := by
      have hQF : ρ.r ((A.toFinset ∪ P) ∪ F.toFinset) = ρ.R := by
        have h1 := ρ.mono (show F.toFinset ∪ P ⊆ (A.toFinset ∪ P) ∪ F.toFinset by
          intro x; simp only [Finset.mem_union]; tauto)
        have := ρ.le_R ((A.toFinset ∪ P) ∪ F.toFinset); omega
      obtain ⟨s1, s2, s3, s4⟩ := ρ.shrink_spec (A.toFinset ∪ P) F F.toFinset hQF
        (fun g hg hgn => absurd (List.mem_toFinset.mp hg) hgn)
      set K := (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span
      have hind := ρ.indep_of_ess s2 s3
      have hPK : ρ.r (P ∪ K) = ρ.r P + K.card :=
        ρ.indep_over_sub (show P ⊆ A.toFinset ∪ P from Finset.subset_union_right) hind
      have hAK : ∀ x ∈ A, x ∉ K := by
        intro x hxA hxK
        have := s3 x hxK
        have e : A.toFinset ∪ P ∪ K.erase x = A.toFinset ∪ P ∪ K := by
          ext y; simp only [Finset.mem_union, Finset.mem_erase, List.mem_toFinset]
          constructor
          · rintro ((h | h) | h); exacts [Or.inl (Or.inl h), Or.inl (Or.inr h), Or.inr h.2]
          · rintro ((h | h) | h)
            · exact Or.inl (Or.inl h)
            · exact Or.inl (Or.inr h)
            · by_cases hy : y = x
              · subst hy; exact Or.inl (Or.inl hxA)
              · exact Or.inr ⟨hy, h⟩
        rw [e] at this; omega
      have hFK : (F.filter (fun x => decide (x ∉ K))).toFinset = F.toFinset \ K := by
        ext x; simp
      have hlen : (F.filter (fun x => decide (x ∉ K))).length = F.length - K.card := by
        rw [← List.toFinset_card_of_nodup (hF.filter _), hFK, Finset.card_sdiff_of_subset s1,
          List.toFinset_card_of_nodup hF]
      have hKF : K.card ≤ F.length := by
        rw [← List.toFinset_card_of_nodup hF]; exact Finset.card_le_card s1
      refine ⟨⟨hA, hF.filter _, ?_, ?_, ?_, ?_, ?_⟩, s4⟩
      · rw [Finset.disjoint_union_right]; refine ⟨hdis, ?_⟩
        rw [Finset.disjoint_left]; intro x hx hxK; exact hAK x (List.mem_toFinset.mp hx) hxK
      · rw [← Finset.union_assoc]; exact s2
      · rw [hFK]
        have h1 := ρ.mono (show F.toFinset ∪ P ⊆ (F.toFinset \ K) ∪ (P ∪ K) by
          intro x; simp only [Finset.mem_union, Finset.mem_sdiff]; tauto)
        have := ρ.le_R ((F.toFinset \ K) ∪ (P ∪ K)); omega
      · rw [hlen, hPK]; omega
      · intro x hx hxr
        have hZ : P ∪ {x} ⊆ A.toFinset ∪ P := by
          intro y; simp only [Finset.mem_union, Finset.mem_singleton, List.mem_toFinset]
          rintro (h | h); exacts [Or.inr h, Or.inl (h ▸ hx)]
        have h2 := ρ.indep_over_sub hZ hind
        have e : P ∪ {x} ∪ K = P ∪ K ∪ {x} := by
          ext y; simp only [Finset.mem_union, Finset.mem_singleton]; tauto
        rw [e, hxr, hPK] at h2
        exact hloop x hx (by omega)
    
  9. The split-and-peel recursion decides loop-freeness within the scaled cost bound.

    Induction on fuel. When |F| = 0, elements of S are loops. When |S| ≤ |F|, all elements are non-loops. In the split phase (|S| > L |F|), S is split into halves, each requiring |F| ≤ |S|/L queries to shrink the frame, and the recursive costs combine to maintain the potential A*L*|S| + 2*|S|*j. In the peel phase (|S| ≤ L |F|), extractP finds an independent set of size |F| in O(|F| log c + |S|/c) queries, certifying those elements as non-loops and recursing on the remaining |S| - |F| elements.

    lines 474–655

    theorem walk_spec (L c h A : ℕ) (hL : 1 ≤ L) (hLc : L ≤ c) (hc : c = 2 ^ h) (hA : A = 2 * h + 4) :
        ∀ (fuel : ℕ) (P : Finset α) (S F : List α), ρ.Inv P S F → S.length < fuel →
        (walkP L c fuel P S F).val ρ.span = decide (∀ x ∈ S, ρ.r {x} ≠ 0) ∧
        ∀ j, S.length ≤ 2 ^ j →
          L * (walkP L c fuel P S F).cost ρ.span ≤ A * L * S.length + 2 * S.length * j := by
      intro fuel
      induction fuel with
      | zero => intro P S F _ h; omega
      | succ fuel ih =>
      intro P S F hinv hfuel
      obtain ⟨hS, hF, hdis, hSs, hFs, hFr, hloop⟩ := hinv
      by_cases hk0 : F.length = 0
      · simp only [walkP, hk0, if_true, val, cost]
        refine ⟨?_, fun j _ => by simp⟩
        have hRP : ρ.R = ρ.r P := by omega
        cases S with
        | nil => simp
        | cons y ys =>
          have : ρ.r {y} = 0 := by
            apply hloop y (by simp)
            have := ρ.mono (show P ⊆ P ∪ {y} from Finset.subset_union_left)
            have := ρ.le_R (P ∪ {y}); omega
          simp only [reduceCtorEq, decide_false, List.mem_cons, forall_eq_or_imp, ne_eq]
          simp [this]
      · by_cases hkt : S.length ≤ F.length
        · simp only [walkP, hk0, if_false, hkt, if_true, val, cost]
          have hSc : ρ.r (P ∪ S.toFinset) = ρ.r P + S.toFinset.card := by
            have := ρ.union_le_card P S.toFinset
            have e : P ∪ S.toFinset = S.toFinset ∪ P := Finset.union_comm _ _
            rw [List.toFinset_card_of_nodup hS] at this ⊢
            rw [e] at this ⊢; omega
          refine ⟨(decide_eq_true ?_).symm, fun j _ => by simp⟩
          intro x hx
          exact ρ.nonloop_of_indep hSc (List.mem_toFinset.mpr hx)
        · have hsubtf : ∀ A : List α, (∀ x ∈ A, x ∈ S) → A.toFinset ⊆ S.toFinset := by
            intro A hA x hx; rw [List.mem_toFinset] at hx ⊢; exact hA x hx
          have hk1 : 1 ≤ F.length := by omega
          by_cases hsplit : L * F.length < S.length
          · simp only [walkP, hk0, hkt, if_false, hsplit, if_true]
            rw [val_bind, cost_bind, val_bind, cost_bind]
            set t := S.length with ht
            set A1 := S.take (t / 2) with hA1
            set A2 := S.drop (t / 2) with hA2
            have hLk : L ≤ L * F.length := Nat.le_mul_of_pos_right L hk1
            have ht2 : 2 ≤ t := by omega
            have hl1 : A1.length = t / 2 := by rw [hA1, List.length_take]; omega
            have hl2 : A2.length = t - t / 2 := by rw [hA2, List.length_drop]
            have hmem : ∀ x, x ∈ S ↔ x ∈ A1 ∨ x ∈ A2 := by
              intro x; rw [← List.mem_append, List.take_append_drop]
            have hA1n : A1.Nodup := hS.sublist (List.take_sublist _ _)
            have hA2n : A2.Nodup := hS.sublist (List.drop_sublist _ _)
            have hA1S : ∀ x ∈ A1, x ∈ S := fun x hx => (hmem x).mpr (Or.inl hx)
            have hA2S : ∀ x ∈ A2, x ∈ S := fun x hx => (hmem x).mpr (Or.inr hx)
            obtain ⟨inv1, c1⟩ := ρ.child_spec P A1 F hA1n hF
              (Finset.disjoint_of_subset_left (hsubtf A1 hA1S) hdis)
              (fun x hx => hloop x (hA1S x hx)) hFs hFr
            obtain ⟨v1, cb1⟩ := ih _ A1 _ inv1 (by omega)
            rw [c1]
            by_cases hb1 : ∀ x ∈ A1, ρ.r {x} ≠ 0
            · have hb1' : decide (∀ x ∈ A1, ρ.r {x} ≠ 0) = true := decide_eq_true hb1
              simp only [v1, hb1', if_true]
              rw [val_bind, cost_bind]
              obtain ⟨inv2, c2⟩ := ρ.child_spec P A2 F hA2n hF
                (Finset.disjoint_of_subset_left (hsubtf A2 hA2S) hdis)
                (fun x hx => hloop x (hA2S x hx)) hFs hFr
              obtain ⟨v2, cb2⟩ := ih _ A2 _ inv2 (by omega)
              rw [c2, v2]
              refine ⟨?_, ?_⟩
              · apply decide_eq_decide.mpr
                constructor
                · intro H x hx
                  rcases (hmem x).mp hx with h1 | h2
                  · exact hb1 x h1
                  · exact H x h2
                · intro H x hx; exact H x (hA2S x hx)
              · intro j hj
                obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := by
                  cases j with
                  | zero => simp at hj; omega
                  | succ j' => exact ⟨j', rfl⟩
                have hp : 2 ^ (j' + 1) = 2 * 2 ^ j' := by ring
                have e1 := cb1 j' (by omega)
                have e2 := cb2 j' (by omega)
                rw [hl1] at e1; rw [hl2] at e2
                have ide : ∀ a b : ℕ, a + b = t → A * L * t = A * L * a + A * L * b := by
                  intro a b hab; rw [← hab]; ring
                have ide2 : ∀ a b : ℕ, a + b = t →
                    2 * t * (j' + 1) = 2 * a * j' + 2 * b * j' + 2 * t := by
                  intro a b hab; rw [← hab]; ring
                have i1 := ide (t / 2) (t - t / 2) (by omega)
                have i2 := ide2 (t / 2) (t - t / 2) (by omega)
                have i3 : L * cost ρ.span (shrinkP (A2.toFinset ∪ P) F F.toFinset) = L * F.length := by
                  rw [c2]
                generalize ha : t / 2 = a at e1 e2 i1 i2
                simp only [Nat.mul_add]
                linarith
            · have hb1' : decide (∀ x ∈ A1, ρ.r {x} ≠ 0) = false := decide_eq_false hb1
              simp only [v1, hb1', Bool.false_eq_true, if_false, val, cost]
              refine ⟨?_, ?_⟩
              · apply (decide_eq_false _).symm
                intro H; exact hb1 (fun x hx => H x (hA1S x hx))
              · intro j hj
                obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := by
                  cases j with
                  | zero => simp at hj; omega
                  | succ j' => exact ⟨j', rfl⟩
                have hp : 2 ^ (j' + 1) = 2 * 2 ^ j' := by ring
                have e1 := cb1 j' (by omega)
                rw [hl1] at e1
                have hAL : A * L * (t / 2) ≤ A * L * t := Nat.mul_le_mul_left _ (by omega)
                have h3 : 2 * (t / 2) * j' ≤ 2 * t * j' := Nat.mul_le_mul_right _ (by omega)
                have h4 : 2 * t * (j' + 1) = 2 * t * j' + 2 * t := by ring
                simp only [Nat.mul_add]
                omega
          · simp only [walkP, hk0, hkt, if_false, hsplit]
            rw [val_bind, cost_bind, val_bind, cost_bind]
            set t := S.length with ht
            set k := F.length with hk
            have htc : t ≤ c * k := by
              have := Nat.mul_le_mul_right k hLc; omega
            obtain ⟨e1, e2, e3, e4, e5⟩ := ρ.extract_spec P c h (by rw [hc]; exact Nat.one_le_two_pow)
              (by rw [hc]) t S rfl S.toFinset k hS htc hSs
            set D := (extractP P c S S.toFinset).val ρ.span with hD
            have hPD : ρ.r (P ∪ D) = ρ.r P + D.card := by
              apply ρ.indep_of_ess
              · rw [Finset.union_comm]; exact e3
              · intro f hf
                rw [Finset.union_comm]
                exact e4 f hf (List.mem_toFinset.mp (e2 hf))
            have hDk : D.card = k := by
              have := Finset.union_comm P D ▸ e3; omega
            set Dl := S.filter (fun x => decide (x ∈ D)) with hDl
            set rest := S.filter (fun x => decide (x ∉ D)) with hrest
            have hDlt : Dl.toFinset = D := by
              ext x; simp only [hDl, List.toFinset_filter, Finset.mem_filter, List.mem_toFinset,
                decide_eq_true_eq]
              constructor
              · exact fun h => h.2
              · exact fun h => ⟨List.mem_toFinset.mp (e2 h), h⟩
            have hDln : Dl.Nodup := hS.filter _
            have hrn : rest.Nodup := hS.filter _
            have hDll : Dl.length = k := by
              rw [← List.toFinset_card_of_nodup hDln, hDlt, hDk]
            have hrt : rest.toFinset = S.toFinset \ D := by
              ext x; simp [hrest, and_comm]
            have hrl : rest.length = t - k := by
              rw [← List.toFinset_card_of_nodup hrn, hrt, Finset.card_sdiff_of_subset e2,
                List.toFinset_card_of_nodup hS, hDk]
            have hrS : ∀ x ∈ rest, x ∈ S := fun x hx => (List.mem_filter.mp hx).1
            obtain ⟨inv1, c1⟩ := ρ.child_spec P rest Dl hrn hDln
              (Finset.disjoint_of_subset_left (hsubtf rest hrS) hdis)
              (fun x hx => hloop x (hrS x hx)) (by rw [hDlt]; exact e3)
              (by rw [hDll]; exact hFr)
            obtain ⟨v1, cb1⟩ := ih _ rest _ inv1 (by omega)
            rw [v1, c1, hDll]
            refine ⟨?_, ?_⟩
            · apply decide_eq_decide.mpr
              constructor
              · intro H x hx
                by_cases hxD : x ∈ D
                · exact ρ.nonloop_of_indep hPD hxD
                · exact H x (List.mem_filter.mpr ⟨hx, by simpa using hxD⟩)
              · intro H x hx; exact H x (hrS x hx)
            · intro j hj
              have f1 := cb1 j (by omega)
              rw [hrl] at f1
              have hSD : (S.toFinset ∩ D).card = k := by
                rw [Finset.inter_eq_right.mpr e2, hDk]
              rw [hSD] at e5
              have f2 : L * cost ρ.span (extractP P c S S.toFinset) ≤ L * (2 * (h + 1) * k + k) :=
                Nat.mul_le_mul_left L e5
              have ide : ∀ u : ℕ, u + k = t → A * L * t = A * L * u + L * (2 * (h + 1) * k + k) + L * k := by
                intro u hu; rw [← hu, hA]; ring
              have ide2 : ∀ u : ℕ, u + k = t → 2 * t * j = 2 * u * j + 2 * k * j := by
                intro u hu; rw [← hu]; ring
              have i1 := ide (t - k) (by omega)
              have i2 := ide2 (t - k) (by omega)
              generalize t - k = u at f1 i1 i2
              have i3 : 0 ≤ 2 * k * j := Nat.zero_le _
              simp only [Nat.mul_add]
              linarith
    
  10. Truncation preserves the output when cost is within budget, and toTree produces a bounded-depth tree. automation

    Induction on the bound b confirms that truncating at depth b ≥ cost(p) does not alter the execution trace. Induction on the tree structure confirms that toTree converts queries to complementary independent-set queries and that its depth is at most b.

    lines 669–713

    theorem val_trunc {α : Type} (σ : Finset α → Bool) :
        ∀ (b : ℕ) (p : Prog α Bool), p.cost σ ≤ b → (trunc b p).val σ = p.val σ := by
      intro b
      induction b with
      | zero =>
        intro p hp
        cases p with
        | ret x => rfl
        | ask T k => simp [cost] at hp
      | succ b ih =>
        intro p hp
        cases p with
        | ret x => rfl
        | ask T k =>
          simp only [trunc, val]
          exact ih _ (by simp only [cost] at hp; omega)
    
    def toTree {n : ℕ} : Prog (Fin n) Bool → MatroidIndepQueryTree (Fin n)
      | .ret b => .leaf b
      | .ask T k => .query (Finset.univ \ T) (toTree (k false)) (toTree (k true))
    
    theorem eval_toTree {n : ℕ} (o : Finset (Fin n) → Bool) (p : Prog (Fin n) Bool) :
        (toTree p).eval o = p.val (fun T => o (Finset.univ \ T)) := by
      induction p with
      | ret b => rfl
      | ask T k ih =>
        simp only [toTree, MatroidIndepQueryTree.eval, val]
        cases h : o (Finset.univ \ T) <;> simp [ih]
    
    theorem depth_toTree_trunc {n : ℕ} :
        ∀ (b : ℕ) (p : Prog (Fin n) Bool), (toTree (trunc b p)).depth ≤ b := by
      intro b
      induction b with
      | zero =>
        intro p; cases p with
        | ret x => simp [trunc, toTree, MatroidIndepQueryTree.depth]
        | ask T k => simp [trunc, toTree, MatroidIndepQueryTree.depth]
      | succ b ih =>
        intro p; cases p with
        | ret x => simp [trunc, toTree, MatroidIndepQueryTree.depth]
        | ask T k =>
          simp only [trunc, toTree, MatroidIndepQueryTree.depth]
          have := ih (k false); have := ih (k true); omega
    
    end Prog
  11. The top-level rank algorithm mainP correctly tests loop-freeness within the target query bound.

    The algorithm shrinks the universal set to obtain an initial frame F of size ρ(univ) in n queries, satisfying the invariant Inv with P = ∅. Applying walk_spec with fuel n + 1 yields correctness and the overall cost bound L*n + A*L*n + 2*n*j.

    lines 721–754

    uses List.mem_finRange

    theorem main_spec {n : ℕ} (ρ : RankFn (Fin n)) (L c h A : ℕ) (hL : 1 ≤ L) (hLc : L ≤ c)
        (hc : c = 2 ^ h) (hA : A = 2 * h + 4) (j : ℕ) (hj : n ≤ 2 ^ j) :
        (mainP n L c).val ρ.span = decide (∀ x, ρ.r {x} ≠ 0) ∧
        L * (mainP n L c).cost ρ.span ≤ L * n + A * L * n + 2 * n * j := by
      unfold mainP
      rw [val_bind, cost_bind]
      have huniv : (List.finRange n).toFinset = Finset.univ := by
        ext x; simp [List.mem_finRange]
      obtain ⟨s1, s2, s3, s4⟩ := ρ.shrink_spec ∅ (List.finRange n) Finset.univ
        (by simp [R]) (fun g _ hg => absurd (List.mem_finRange g) hg)
      set K := (shrinkP ∅ (List.finRange n) Finset.univ).val ρ.span
      have hind := ρ.indep_of_ess s2 s3
      rw [ρ.r_empty] at hind
      set Fl := (List.finRange n).filter (fun x => decide (x ∈ K))
      have hFln : Fl.Nodup := (List.nodup_finRange n).filter _
      have hFlt : Fl.toFinset = K := by ext x; simp [Fl, List.mem_finRange]
      have hFll : Fl.length = K.card := by rw [← List.toFinset_card_of_nodup hFln, hFlt]
      have inv : ρ.Inv ∅ (List.finRange n) Fl := by
        refine ⟨List.nodup_finRange n, hFln, by simp, ?_, ?_, ?_, ?_⟩
        · rw [huniv]; simp [R]
        · rw [hFlt]; simpa using s2
        · rw [ρ.r_empty, hFll]; simp only [Finset.empty_union] at hind s2; omega
        · intro x _ hx; simpa [ρ.r_empty] using hx
      obtain ⟨v, cb⟩ := ρ.walk_spec L c h A hL hLc hc hA (n + 1) ∅ _ _ inv (by simp)
      rw [v, s4]
      refine ⟨?_, ?_⟩
      · apply decide_eq_decide.mpr
        simp [List.mem_finRange]
      · have := cb j (by simpa using hj)
        simp only [List.length_finRange] at this
        rw [List.length_finRange, Nat.mul_add]
        omega
    
    end RankFn
  12. The dual rank of a matroid satisfies the RankFn axioms. automation

    The dual matroid rank M✶.eRk on finite subsets is finite, so it can be coerced to ℕ. Its monotonicity, submodularity, and cardinality bound follow from the corresponding properties of the matroid rank function.

    lines 760–788

    theorem dual_eRk_ne_top (X : Finset (Fin n)) : M✶.eRk (X : Set (Fin n)) ≠ ⊤ := by
      have h1 := M✶.eRk_le_encard (X : Set (Fin n))
      rw [Set.encard_coe_eq_coe_finsetCard] at h1
      exact ne_top_of_le_ne_top (ENat.natCast_ne_top _) h1
    
    theorem dual_eRk_coe (X : Finset (Fin n)) :
        M✶.eRk (X : Set (Fin n)) = ((M✶.eRk (X : Set (Fin n))).toNat : ℕ∞) :=
      (ENat.natCast_toNat (dual_eRk_ne_top M X)).symm
    
    noncomputable def dualRank : RankFn (Fin n) where
      r X := (M✶.eRk (X : Set (Fin n))).toNat
      mono := by
        intro X Y hXY
        exact ENat.toNat_le_toNat (M✶.eRk_mono (Finset.coe_subset.mpr hXY)) (dual_eRk_ne_top M Y)
      sub := by
        intro X Y
        have h := M✶.eRk_inter_add_eRk_union_le (X : Set (Fin n)) (Y : Set (Fin n))
        rw [← Finset.coe_inter, ← Finset.coe_union, dual_eRk_coe M (X ∩ Y), dual_eRk_coe M (X ∪ Y),
          dual_eRk_coe M X, dual_eRk_coe M Y] at h
        have h' : (M✶.eRk ↑(X ∩ Y)).toNat + (M✶.eRk ↑(X ∪ Y)).toNat ≤
            (M✶.eRk ↑X).toNat + (M✶.eRk ↑Y).toNat := by exact_mod_cast h
        omega
      card := by
        intro X
        have h1 := M✶.eRk_le_encard (X : Set (Fin n))
        rw [Set.encard_coe_eq_coe_finsetCard, dual_eRk_coe M X] at h1
        exact_mod_cast h1
    
  13. Matroid independence queries correspond to dual spanning queries, and dual non-loops correspond to non-coloops.

    A subset Q is independent in M if and only if Q is coindependent in M✶, which holds if and only if its complement univ \ Q spans M✶. Moreover, an element x is a coloop of M if and only if x is a loop of M✶, which is equivalent to dual rank ρ({x}) = 0.

    lines 789–824

    uses Matroid.isLoop_iff, Matroid.spanning_iff_compl_coindep

    theorem dualRank_span (hE : M.E = Set.univ) (o : Finset (Fin n) → Bool)
        (ho : ∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) :
        (fun T => o (Finset.univ \ T)) = (dualRank M).span := by
      funext T
      apply Bool.eq_iff_iff.mpr
      rw [ho, RankFn.span, decide_eq_true_iff]
      have hE' : M✶.E = Set.univ := by rw [Matroid.dual_ground, hE]
      have hT : (T : Set (Fin n)) ⊆ M✶.E := by rw [hE']; exact Set.subset_univ _
      rw [Finset.coe_sdiff, Finset.coe_univ, ← Matroid.dual_coindep_iff, ← hE',
        ← Matroid.spanning_iff_compl_coindep hT, Matroid.spanning_iff_eRk_le hT,
        Matroid.eRank_def, hE']
      show _ ↔ (M✶.eRk (T : Set (Fin n))).toNat = (M✶.eRk ((Finset.univ : Finset (Fin n)) : Set (Fin n))).toNat
      rw [Finset.coe_univ]
      have h1 := M✶.eRk_mono (Set.subset_univ (T : Set (Fin n)))
      have h2 := dual_eRk_coe M T
      have h3 := dual_eRk_coe M Finset.univ
      rw [Finset.coe_univ] at h3
      constructor
      · intro h
        have : M✶.eRk (T : Set (Fin n)) = M✶.eRk Set.univ := le_antisymm h1 h
        rw [this]
      · intro h
        rw [h2, h3, h]
    
    theorem dualRank_loop (hE : M.E = Set.univ) (x : Fin n) :
        (dualRank M).r {x} ≠ 0 ↔ ¬ M.IsColoop x := by
      have hE' : M✶.E = Set.univ := by rw [Matroid.dual_ground, hE]
      show (M✶.eRk ((({x} : Finset (Fin n))) : Set (Fin n))).toNat ≠ 0 ↔ ¬ M✶.IsLoop x
      rw [Finset.coe_singleton]
      have hne := dual_eRk_ne_top M {x}
      rw [Finset.coe_singleton] at hne
      rw [Ne, ENat.toNat_eq_zero, or_iff_left hne,
        Matroid.eRk_eq_zero_iff (by rw [hE']; exact Set.subset_univ _), Set.singleton_subset_iff,
        ← Matroid.isLoop_iff]
    
    end Bridge
  14. Setting parameters balances the split and peel phases, yielding an O(n log log n) query tree.

    Choosing j = ⌊log2(n+2)⌋ + 1, h = ⌊log2 j⌋ + 1, c = 2^h, and L = j yields scaled cost j * (2h + 7) * n. Dividing by j bounds the actual query cost by (2h + 7)n ≤ 9n(1 + log2 log2(n+2)). Truncating at this depth gives the query tree T.

    lines 826–853

    uses Nat.lt_log2_self

    theorem coloop_detection_main : ∃ C : ℕ, ∀ n : ℕ, ∃ T : MatroidIndepQueryTree (Fin n),
        T.depth ≤ C*n*(1 + Nat.log2 (1 + Nat.log2 (n+2))) ∧ ∀ M : Matroid (Fin n), M.E = Set.univ →
        ∀ o : Finset (Fin n) → Bool, (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) →
        (T.eval o = true ↔ ∀ x : Fin n, ¬M.IsColoop x) := by
      refine ⟨9, fun n => ?_⟩
      set j := Nat.log2 (n + 2) + 1 with hj
      set h := Nat.log2 j + 1 with hh
      set B := 9 * n * (1 + Nat.log2 (1 + Nat.log2 (n + 2))) with hB
      refine ⟨Prog.toTree (Prog.trunc B (RankFn.mainP n j (2 ^ h))), Prog.depth_toTree_trunc _ _, ?_⟩
      intro M hE o ho
      rw [Prog.eval_toTree, dualRank_span M hE o ho]
      have hjn : n ≤ 2 ^ j := by have h0 : n + 2 < 2 ^ j := Nat.lt_log2_self; omega
      have hjc : j ≤ 2 ^ h := by have h0 : j < 2 ^ h := Nat.lt_log2_self; omega
      obtain ⟨v, cb⟩ := RankFn.main_spec (dualRank M) j (2 ^ h) h (2 * h + 4) (by omega) hjc rfl rfl
        j hjn
      have hcost : (RankFn.mainP n j (2 ^ h)).cost (dualRank M).span ≤ B := by
        have e : j * n + (2 * h + 4) * j * n + 2 * n * j = j * ((2 * h + 7) * n) := by ring
        rw [e] at cb
        have := Nat.le_of_mul_le_mul_left cb (by omega : 0 < j)
        have hj' : 1 + Nat.log2 (n + 2) = j := by omega
        rw [hB, hj']
        have : (2 * h + 7) * n ≤ 9 * n * (1 + Nat.log2 j) := by
          rw [hh]; nlinarith
        omega
      rw [Prog.val_trunc _ _ _ hcost, v, decide_eq_true_iff]
      exact forall_congr' fun x => dualRank_loop M hE x
    
    end ColoopLogLog
  15. The main theorem follows directly from coloop_detection_main.

    The theorem is an exact restatement of coloop_detection_main, which provides the constant C = 9 and the query tree.

    lines 855–856

    uses ColoopLogLog.coloop_detection_main

    theorem matroid_coloop_detection_log_log : ∃ C : ℕ, ∀ n : ℕ, ∃ T : MatroidIndepQueryTree (Fin n), T.depth ≤ C*n*(1 + Nat.log2 (1 + Nat.log2 (n+2))) ∧ ∀ M : Matroid (Fin n), M.E = Set.univ → ∀ o : Finset (Fin n) → Bool, (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) → (T.eval o = true ↔ ∀ x : Fin n, ¬M.IsColoop x) := by
      exact ColoopLogLog.coloop_detection_main
ColoopLogLog.coloop_detection_main
There exists a constant C such that for every n, a query tree of depth at most C * n * (1 + log2(1 + log2(n+2))) decides whether an n-element matroid has no coloops.
Finset.card_insert_of_notMem
Inserting an element into a finite set that does not already contain it increases its cardinality by 1.
Finset.card_sdiff_add_card_eq_card
For a subset s ⊆ t, the cardinality of the difference t \ s plus the cardinality of s equals the cardinality of t.
Finset.card_sdiff_of_subset
For a subset s ⊆ t, the cardinality of the difference t \ s equals |t| - |s|.
Finset.card_union_of_disjoint
The cardinality of the union of two disjoint finite sets is the sum of their cardinalities.
Finset.erase_subset
Removing an element from a finite set yields a subset of the original set.
List.mem_finRange
Every element of Fin n belongs to List.finRange n.
List.take_append_drop
Concatenating the first i elements of a list with the list after dropping i elements yields the original list.
List.toFinset_card_of_nodup
Converting a duplicate-free list to a finite set preserves its length as cardinality.
Matroid.isLoop_iff
An element e is a loop of matroid M if and only if e belongs to the set of loops of M.
Matroid.spanning_iff_compl_coindep
A subset S spans matroid M if and only if its complement M.E \ S is coindependent in M.
Nat.lt_log2_self
For every natural number n, n is strictly less than 2^(log2(n) + 1).

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

Lean’s trace was cut to the first 400 of 764 tactics.

namespace ColoopLogLoginductive Prog (α β : Type) where  | ret : β → Prog α β  | ask : Finset α → (Bool → Prog α β) → Prog α βnamespace Progvariable {α β γ : Type}def bind : Prog α β → (β → Prog α γ) → Prog α γ  | ret b, f => f b  | ask T k, f => ask T (fun x => bind (k x) f)def val (σ : Finset α → Bool) : Prog α β → β  | ret b => b  | ask T k => val σ (k (σ T))def cost (σ : Finset α → Bool) : Prog α β → ℕ  | ret _ => 0  | ask T k => 1 + cost σ (k (σ T))theorem val_bind (σ : Finset α → Bool) (p : Prog α β) (f : β → Prog α γ) :    (p.bind f).val σ = (f (p.val σ)).val σ := by  induction p with  | ret b => rfl  | ask T k ih => simp only [bind, val, ih]theorem cost_bind (σ : Finset α → Bool) (p : Prog α β) (f : β → Prog α γ) :    (p.bind f).cost σ = p.cost σ + (f (p.val σ)).cost σ := by  induction p with  | ret b => simp [bind, cost, val]  | ask T k ih => simp only [bind, cost, val, ih]; omegaend Progopen Progstructure RankFn (α : Type) [DecidableEq α] [Fintype α] where  r : Finset α → ℕ  mono : ∀ {X Y : Finset α}, X ⊆ Y → r X ≤ r Y  sub : ∀ X Y : Finset α, r (X ∪ Y) + r (X ∩ Y) ≤ r X + r Y  card : ∀ X : Finset α, r X ≤ X.cardnamespace RankFnvariable {α : Type} [DecidableEq α] [Fintype α] (ρ : RankFn α)def R : ℕ := ρ.r Finset.univdef span (T : Finset α) : Bool := decide (ρ.r T = ρ.R)theorem r_empty : ρ.r ∅ = 0 := by have := ρ.card ∅; simpa using thistheorem le_R (X : Finset α) : ρ.r X ≤ ρ.R := ρ.mono (Finset.subset_univ X)theorem union_le (X Y : Finset α) : ρ.r (X ∪ Y) ≤ ρ.r X + ρ.r Y := by  have := ρ.sub X Y; omegatheorem union_le_card (X Y : Finset α) : ρ.r (X ∪ Y) ≤ ρ.r X + Y.card := by  have := ρ.union_le X Y; have := ρ.card Y; omegatheorem indep_over_sub {Y K Z : Finset α} (hZ : Z ⊆ Y) (h : ρ.r (Y ∪ K) = ρ.r Y + K.card) :    ρ.r (Z ∪ K) = ρ.r Z + K.card := by  have h1 := ρ.sub (Z ∪ K) Y  have e1 : Z ∪ K ∪ Y = Y ∪ K := by    ext x; simp only [Finset.mem_union]; have := @hZ x; tauto  have e2 : ρ.r Z ≤ ρ.r ((Z ∪ K) ∩ Y) := ρ.mono (by    intro x hx; simp only [Finset.mem_inter, Finset.mem_union]; exact ⟨Or.inl hx, hZ hx⟩)  rw [e1] at h1  have := ρ.union_le_card Z K  omegatheorem indep_over_subK {Y K K' : Finset α} (hK : K' ⊆ K) (h : ρ.r (Y ∪ K) = ρ.r Y + K.card) :    ρ.r (Y ∪ K') = ρ.r Y + K'.card := by  have e : Y ∪ K = (Y ∪ K') ∪ (K \ K') := by    ext x; simp only [Finset.mem_union, Finset.mem_sdiff]; have := @hK x; tauto  have h1 := ρ.union_le_card (Y ∪ K') (K \ K')  rw [← e] at h1  have h2 := Finset.card_sdiff_add_card_eq_card hK  have h3 := ρ.union_le_card Y K'  omegatheorem indep_of_ess {Q K : Finset α} (hs : ρ.r (Q ∪ K) = ρ.R)    (he : ∀ f ∈ K, ρ.r (Q ∪ K.erase f) < ρ.R) : ρ.r (Q ∪ K) = ρ.r Q + K.card := by  have key : ∀ K' : Finset α, K' ⊆ K → ρ.r Q + K'.card ≤ ρ.r (Q ∪ K') := by    intro K'    induction K' using Finset.induction_on with    | empty => intro _; simp    | insert a s ha ih =>      intro hsub      have haK : a ∈ K := hsub (Finset.mem_insert_self a s)      have hsK : s ⊆ K.erase a := by        intro x hx; rw [Finset.mem_erase]        exact ⟨fun h => ha (h ▸ hx), hsub (Finset.mem_insert_of_mem hx)⟩      have ih' := ih (fun x hx => Finset.mem_of_mem_erase (hsK hx))      have h1 := ρ.sub (Q ∪ insert a s) (Q ∪ K.erase a)      have e1 : Q ∪ insert a s ∪ (Q ∪ K.erase a) = Q ∪ K := by        ext x; simp only [Finset.mem_union, Finset.mem_insert, Finset.mem_erase]        constructor        · rintro ((h | h | h) | h | h)          · exact Or.inl h          · exact Or.inr (h ▸ haK)          · exact Or.inr (hsub (Finset.mem_insert_of_mem h))          · exact Or.inl h          · exact Or.inr h.2        · rintro (h | h)          · exact Or.inl (Or.inl h)          · by_cases hx : x = a            · exact Or.inl (Or.inr (Or.inl hx))            · exact Or.inr (Or.inr ⟨hx, h⟩)      have e2 : ρ.r (Q ∪ s) ≤ ρ.r ((Q ∪ insert a s) ∩ (Q ∪ K.erase a)) := ρ.mono (by        intro x hx        simp only [Finset.mem_inter, Finset.mem_union, Finset.mem_insert] at hx ⊢        rcases hx with hx | hx        · exact ⟨Or.inl hx, Or.inl hx⟩        · exact ⟨Or.inr (Or.inr hx), Or.inr (hsK hx)⟩)      rw [e1, hs] at h1      have h3 := he a haK      rw [Finset.card_insert_of_notMem ha]      omega  have := key K le_rfl  have := ρ.union_le_card Q K  omega/-! ## Shrinking a frame -/def shrinkP (Q : Finset α) : List α → Finset α → Prog α (Finset α)  | [], K => .ret K  | f :: fs, K => .ask (Q ∪ K.erase f) (fun b => shrinkP Q fs (if b then K.erase f else K))theorem shrink_spec (Q : Finset α) : ∀ (fs : List α) (K : Finset α), ρ.r (Q ∪ K) = ρ.R →    (∀ g ∈ K, g ∉ fs → ρ.r (Q ∪ K.erase g) < ρ.R) →    (shrinkP Q fs K).val ρ.span ⊆ K ∧ ρ.r (Q ∪ (shrinkP Q fs K).val ρ.span) = ρ.R ∧    (∀ g ∈ (shrinkP Q fs K).val ρ.span,      ρ.r (Q ∪ ((shrinkP Q fs K).val ρ.span).erase g) < ρ.R) ∧    (shrinkP Q fs K).cost ρ.span = fs.length := by  intro fs  induction fs with  | nil => intro K h1 h2; simp only [shrinkP, val, cost, List.length_nil]; exact ⟨le_rfl, h1, fun g hg => h2 g hg (by simp), trivial⟩  | cons f fs ih =>    intro K h1 h2    by_cases hb : ρ.r (Q ∪ K.erase f) = ρ.R    · have hsp : ρ.span (Q ∪ K.erase f) = true := decide_eq_true hb      simp only [shrinkP, val, cost, hsp, if_true, List.length_cons]      obtain ⟨a, b, c, d⟩ := ih (K.erase f) hb (by        intro g hg hgn        have hgf : g ≠ f := (Finset.mem_erase.mp hg).1        have hgK := (Finset.mem_erase.mp hg).2        have := h2 g hgK (by simp [hgf, hgn])        have hm : ρ.r (Q ∪ (K.erase f).erase g) ≤ ρ.r (Q ∪ K.erase g) :=          ρ.mono (Finset.union_subset_union le_rfl (by            intro x hx; simp only [Finset.mem_erase] at hx ⊢; exact ⟨hx.1, hx.2.2⟩))        omega)      exact ⟨a.trans (Finset.erase_subset _ _), b, c, by omega⟩    · have hsp : ρ.span (Q ∪ K.erase f) = false := decide_eq_false hb      simp only [shrinkP, val, cost, hsp, Bool.false_eq_true, if_false, List.length_cons]      obtain ⟨a, b, c, d⟩ := ih K h1 (by        intro g hg hgn        by_cases hgf : g = f        · subst hgf; have := ρ.le_R (Q ∪ K.erase g); omega        · exact h2 g hg (by simp [hgf, hgn]))      exact ⟨a, b, c, by omega⟩/-! ## Block deletion search -/def visitP (P : Finset α) (block : List α) (kept : Finset α) : Prog α (Finset α) :=  match block with  | [] => .ret kept  | b :: bs => .ask ((kept \ (b :: bs).toFinset) ∪ P) (fun res =>      if res then .ret (kept \ (b :: bs).toFinset)      else if h : bs = [] then .ret kept      else (visitP P ((b :: bs).take ((bs.length + 1) / 2)) kept).bind        (fun k1 => visitP P ((b :: bs).drop ((bs.length + 1) / 2)) k1))termination_by block.lengthdecreasing_by  all_goals simp only [List.length_take, List.length_drop, List.length_cons]  · omega  · have : bs.length ≠ 0 := by simpa using h    omegatheorem visit_spec (P : Finset α) : ∀ (m : ℕ) (block : List α), block.length = m →    ∀ (h : ℕ) (kept : Finset α), block.Nodup → block.length ≤ 2 ^ h → ρ.r (kept ∪ P) = ρ.R →    kept \ block.toFinset ⊆ (visitP P block kept).val ρ.span ∧    (visitP P block kept).val ρ.span ⊆ kept ∧    ρ.r ((visitP P block kept).val ρ.span ∪ P) = ρ.R ∧    (∀ d ∈ (visitP P block kept).val ρ.span, d ∈ block →      ρ.r (((visitP P block kept).val ρ.span).erase d ∪ P) < ρ.R) ∧    (visitP P block kept).cost ρ.span ≤      2 * (h + 1) * (block.toFinset ∩ (visitP P block kept).val ρ.span).card + 1 := by  intro m  induction m using Nat.strong_induction_on with  | _ m IH =>  intro block hm h kept hnd hlen hkept  match block, hm, hnd, hlen with  | [], _, _, _ =>    simp only [visitP, val, cost, List.toFinset_nil, Finset.sdiff_empty, List.not_mem_nil]    exact ⟨le_rfl, le_rfl, hkept, fun _ _ h => h.elim, by omega⟩  | b :: bs, hm, hnd, hlen =>    by_cases ht : ρ.r ((kept \ (b :: bs).toFinset) ∪ P) = ρ.R    · have hsp : ρ.span ((kept \ (b :: bs).toFinset) ∪ P) = true := decide_eq_true ht      rw [visitP]; simp only [val, cost, hsp, if_true]      refine ⟨le_rfl, Finset.sdiff_subset, ht, ?_, by omega⟩      intro d hd hdb      exact absurd (List.mem_toFinset.mpr hdb) (Finset.mem_sdiff.mp hd).2    · have hsp : ρ.span ((kept \ (b :: bs).toFinset) ∪ P) = false := decide_eq_false ht      have hlt : ρ.r ((kept \ (b :: bs).toFinset) ∪ P) < ρ.R := by        have := ρ.le_R ((kept \ (b :: bs).toFinset) ∪ P); omega      by_cases hbs : bs = []      · subst hbs        rw [visitP]; simp only [val, cost, hsp, Bool.false_eq_true, if_false, dif_pos]        refine ⟨Finset.sdiff_subset, le_rfl, hkept, ?_, by omega⟩        intro d hd hdb        simp only [List.mem_singleton] at hdb; subst hdb        have : kept.erase d = kept \ [d].toFinset := by ext x; simp [and_comm]        rw [this]; exact hlt      · rw [visitP]; simp only [val, cost, hsp, Bool.false_eq_true, if_false, dif_neg hbs]        rw [val_bind, cost_bind]        set l := b :: bs with hl        set q := (bs.length + 1) / 2        have hlen' : l.length = bs.length + 1 := by simp [hl]        have hbs1 : bs.length ≠ 0 := by simpa using hbs        obtain ⟨h', rfl⟩ : ∃ h', h = h' + 1 := by          cases h with          | zero => simp at hlen; omega          | succ h' => exact ⟨h', rfl⟩        have hpow : 2 ^ (h' + 1) = 2 * 2 ^ h' := by ring        set A := l.take q        set B := l.drop q        have hAB : A ++ B = l := List.take_append_drop q l        have hndAB : (A ++ B).Nodup := hAB ▸ hnd        have hAnd : A.Nodup := (List.nodup_append.mp hndAB).1        have hBnd : B.Nodup := (List.nodup_append.mp hndAB).2.1        have hdisj : ∀ x ∈ A, x ∉ B := fun x hx hx' =>          (List.nodup_append.mp hndAB).2.2 x hx x hx' rfl        have hAlen : A.length ≤ 2 ^ h' := by simp [A, List.length_take]; omega        have hBlen : B.length ≤ 2 ^ h' := by simp [B, List.length_drop]; omega        have hAm : A.length < m := by simp [A, List.length_take]; omega        have hBm : B.length < m := by simp [B, List.length_drop]; omega        obtain ⟨a1, a2, a3, a4, a5⟩ := IH _ hAm A rfl h' kept hAnd hAlen hkept        set k1 := (visitP P A kept).val ρ.span        obtain ⟨b1, b2, b3, b4, b5⟩ := IH _ hBm B rfl h' k1 hBnd hBlen a3        set k2 := (visitP P B k1).val ρ.span        have hlset : l.toFinset = A.toFinset ∪ B.toFinset := by          rw [← hAB, List.toFinset_append]        refine ⟨?_, b2.trans a2, b3, ?_, ?_⟩        · intro x hx          rw [hlset, Finset.mem_sdiff, Finset.mem_union, not_or] at hx          exact b1 (Finset.mem_sdiff.mpr ⟨a1 (Finset.mem_sdiff.mpr ⟨hx.1, hx.2.1⟩), hx.2.2⟩)        · intro d hd hdl          rw [← hAB, List.mem_append] at hdl          rcases hdl with hdA | hdB          · have := a4 d (b2 hd) hdA            have hm2 : ρ.r (k2.erase d ∪ P) ≤ ρ.r (k1.erase d ∪ P) :=              ρ.mono (Finset.union_subset_union (Finset.erase_subset_erase d b2) le_rfl)            omega          · exact b4 d hd hdB        · have eA : A.toFinset ∩ k1 = A.toFinset ∩ k2 := by            ext x; simp only [Finset.mem_inter, List.mem_toFinset]            constructor            · rintro ⟨hxA, hx1⟩              exact ⟨hxA, b1 (Finset.mem_sdiff.mpr ⟨hx1, List.mem_toFinset.not.mpr (hdisj x hxA)⟩)⟩            · rintro ⟨hxA, hx2⟩; exact ⟨hxA, b2 hx2⟩          have eL : l.toFinset ∩ k2 = (A.toFinset ∩ k2) ∪ (B.toFinset ∩ k2) := by            rw [hlset, Finset.union_inter_distrib_right]          have hdj : Disjoint (A.toFinset ∩ k2) (B.toFinset ∩ k2) := by            rw [Finset.disjoint_left]; intro x hx hx'            exact hdisj x (List.mem_toFinset.mp (Finset.mem_inter.mp hx).1)              (List.mem_toFinset.mp (Finset.mem_inter.mp hx').1)          have ecard : (l.toFinset ∩ k2).card = (A.toFinset ∩ k2).card + (B.toFinset ∩ k2).card := by            rw [eL, Finset.card_union_of_disjoint hdj]          have hpos : 1 ≤ (l.toFinset ∩ k2).card := by            by_contra hc            have hempty : l.toFinset ∩ k2 = ∅ := Finset.card_eq_zero.mp (by omega)            have hsub : k2 ⊆ kept \ l.toFinset := by              intro x hx              refine Finset.mem_sdiff.mpr ⟨a2 (b2 hx), fun hxl => ?_⟩              have : x ∈ l.toFinset ∩ k2 := Finset.mem_inter.mpr ⟨hxl, hx⟩              rw [hempty] at this; exact absurd this (Finset.notMem_empty x)            have := ρ.mono (Finset.union_subset_union hsub (le_refl P))            omega          rw [eA] at a5          rw [ecard]          rw [ecard] at hpos          nlinarith/-! ## Extraction in chunks -/def extractP (P : Finset α) (c : ℕ) (l : List α) (kept : Finset α) : Prog α (Finset α) :=  if h : l = [] ∨ c = 0 then .ret kept  else (visitP P (l.take c) kept).bind (fun k1 => extractP P c (l.drop c) k1)termination_by l.lengthdecreasing_by  simp only [List.length_drop]  have h1 : l ≠ [] := fun e => h (Or.inl e)  have h2 : c ≠ 0 := fun e => h (Or.inr e)  have : l.length ≠ 0 := by simpa using h1  omegatheorem extract_spec (P : Finset α) (c h : ℕ) (hc : 1 ≤ c) (hch : c ≤ 2 ^ h) :    ∀ (m : ℕ) (l : List α), l.length = m → ∀ (kept : Finset α) (w : ℕ), l.Nodup →    l.length ≤ c * w → ρ.r (kept ∪ P) = ρ.R →    kept \ l.toFinset ⊆ (extractP P c l kept).val ρ.span ∧    (extractP P c l kept).val ρ.span ⊆ kept ∧    ρ.r ((extractP P c l kept).val ρ.span ∪ P) = ρ.R ∧    (∀ d ∈ (extractP P c l kept).val ρ.span, d ∈ l →      ρ.r (((extractP P c l kept).val ρ.span).erase d ∪ P) < ρ.R) ∧    (extractP P c l kept).cost ρ.span ≤      2 * (h + 1) * (l.toFinset ∩ (extractP P c l kept).val ρ.span).card + w := by  intro m  induction m using Nat.strong_induction_on with  | _ m IH =>  intro l hm kept w hnd hlen hkept  by_cases hl : l = []  · subst hl    rw [extractP]; simp only [true_or, dif_pos, val, cost, List.toFinset_nil, Finset.sdiff_empty,      List.not_mem_nil]    exact ⟨le_rfl, le_rfl, hkept, fun _ _ h => h.elim, by omega⟩  · have hc0 : c ≠ 0 := by omega    have hne : ¬ (l = [] ∨ c = 0) := by tauto    rw [extractP]; simp only [dif_neg hne]    rw [val_bind, cost_bind]    set A := l.take c    set B := l.drop c    have hAB : A ++ B = l := List.take_append_drop c l    have hndAB : (A ++ B).Nodup := hAB ▸ hnd    have hAnd : A.Nodup := (List.nodup_append.mp hndAB).1    have hBnd : B.Nodup := (List.nodup_append.mp hndAB).2.1    have hdisj : ∀ x ∈ A, x ∉ B := fun x hx hx' =>      (List.nodup_append.mp hndAB).2.2 x hx x hx' rfl    have hl0 : l.length ≠ 0 := by simpa using hl    have hw : 1 ≤ w := by      rcases Nat.eq_zero_or_pos w with h0 | h0      · subst h0; rw [Nat.mul_zero] at hlen; omega      · exact h0    have hAlen : A.length ≤ 2 ^ h := (List.length_take_le c l).trans hch    have hBlen : B.length ≤ c * (w - 1) := by      simp only [B, List.length_drop]      have : c * (w - 1) = c * w - c := by rw [Nat.mul_sub_one]      omega    have hBm : B.length < m := by simp [B, List.length_drop]; omega    obtain ⟨a1, a2, a3, a4, a5⟩ := ρ.visit_spec P A.length A rfl h kept hAnd hAlen hkept    set k1 := (visitP P A kept).val ρ.span    obtain ⟨b1, b2, b3, b4, b5⟩ := IH _ hBm B rfl k1 (w - 1) hBnd hBlen a3    set k2 := (extractP P c B k1).val ρ.span    have hlset : l.toFinset = A.toFinset ∪ B.toFinset := by      rw [← hAB, List.toFinset_append]    refine ⟨?_, b2.trans a2, b3, ?_, ?_⟩    · intro x hx      rw [hlset, Finset.mem_sdiff, Finset.mem_union, not_or] at hx      exact b1 (Finset.mem_sdiff.mpr ⟨a1 (Finset.mem_sdiff.mpr ⟨hx.1, hx.2.1⟩), hx.2.2⟩)    · intro d hd hdl      rw [← hAB, List.mem_append] at hdl      rcases hdl with hdA | hdB      · have := a4 d (b2 hd) hdA        have hm2 : ρ.r (k2.erase d ∪ P) ≤ ρ.r (k1.erase d ∪ P) :=          ρ.mono (Finset.union_subset_union (Finset.erase_subset_erase d b2) le_rfl)        omega      · exact b4 d hd hdB    · have eA : A.toFinset ∩ k1 = A.toFinset ∩ k2 := by        ext x; simp only [Finset.mem_inter, List.mem_toFinset]        constructor        · rintro ⟨hxA, hx1⟩          exact ⟨hxA, b1 (Finset.mem_sdiff.mpr ⟨hx1, List.mem_toFinset.not.mpr (hdisj x hxA)⟩)⟩        · rintro ⟨hxA, hx2⟩; exact ⟨hxA, b2 hx2⟩      have eL : l.toFinset ∩ k2 = (A.toFinset ∩ k2) ∪ (B.toFinset ∩ k2) := by        rw [hlset, Finset.union_inter_distrib_right]      have hdj : Disjoint (A.toFinset ∩ k2) (B.toFinset ∩ k2) := by        rw [Finset.disjoint_left]; intro x hx hx'        exact hdisj x (List.mem_toFinset.mp (Finset.mem_inter.mp hx).1)          (List.mem_toFinset.mp (Finset.mem_inter.mp hx').1)      have ecard : (l.toFinset ∩ k2).card = (A.toFinset ∩ k2).card + (B.toFinset ∩ k2).card := by        rw [eL, Finset.card_union_of_disjoint hdj]      rw [eA] at a5      rw [ecard]      have hw1 : w - 1 + 1 = w := Nat.sub_add_cancel hw      nlinarithend RankFnend ColoopLogLognamespace ColoopLogLogopen Prognamespace RankFnvariable {α : Type} [DecidableEq α] [Fintype α] (ρ : RankFn α)/-! ## The split-and-peel recursion -/def walkP (L c : ℕ) : ℕ → Finset α → List α → List α → Prog α Bool  | 0, _, _, _ => .ret false  | fuel + 1, P, S, F =>    if F.length = 0 then .ret (decide (S = []))    else if S.length ≤ F.length then .ret true    else if L * F.length < S.length then      (shrinkP ((S.take (S.length / 2)).toFinset ∪ P) F F.toFinset).bind fun K1 =>      (walkP L c fuel (P ∪ K1) (S.take (S.length / 2)) (F.filter (fun x => decide (x ∉ K1)))).bind        fun b1 =>      if b1 then        (shrinkP ((S.drop (S.length / 2)).toFinset ∪ P) F F.toFinset).bind fun K2 =>        walkP L c fuel (P ∪ K2) (S.drop (S.length / 2)) (F.filter (fun x => decide (x ∉ K2)))      else .ret false    else      (extractP P c S S.toFinset).bind fun D =>      (shrinkP ((S.filter (fun x => decide (x ∉ D))).toFinset ∪ P)          (S.filter (fun x => decide (x ∈ D))) (S.filter (fun x => decide (x ∈ D))).toFinset).bind        fun K =>      walkP L c fuel (P ∪ K) (S.filter (fun x => decide (x ∉ D)))        ((S.filter (fun x => decide (x ∈ D))).filter (fun x => decide (x ∉ K)))/-- The invariant of a call: `S` spans the contraction by `P`, `F` is a basis of it, andcontracting `P` creates no new loops in `S`. -/def Inv (P : Finset α) (S F : List α) : Prop :=  S.Nodup ∧ F.Nodup ∧ Disjoint S.toFinset P ∧ ρ.r (S.toFinset ∪ P) = ρ.R ∧  ρ.r (F.toFinset ∪ P) = ρ.R ∧ ρ.r P + F.length = ρ.R ∧  (∀ x ∈ S, ρ.r (P ∪ {x}) = ρ.r P → ρ.r {x} = 0)theorem nonloop_of_indep {P D : Finset α} (h : ρ.r (P ∪ D) = ρ.r P + D.card) {d : α} (hd : d ∈ D) :    ρ.r {d} ≠ 0 := by  have := ρ.indep_over_subK (Finset.singleton_subset_iff.mpr hd) h  have := ρ.union_le P {d}  simp at *; omega/-- Restricting to a child block `A` with a frame `F`. -/theorem child_spec (P : Finset α) (A F : List α) (hA : A.Nodup) (hF : F.Nodup)    (hdis : Disjoint A.toFinset P) (hloop : ∀ x ∈ A, ρ.r (P ∪ {x}) = ρ.r P → ρ.r {x} = 0)    (hFs : ρ.r (F.toFinset ∪ P) = ρ.R) (hFr : ρ.r P + F.length = ρ.R) :    ρ.Inv (P ∪ (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span) A      (F.filter (fun x => decide (x ∉ (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span))) ∧    (shrinkP (A.toFinset ∪ P) F F.toFinset).cost ρ.span = F.length := by  have hQF : ρ.r ((A.toFinset ∪ P) ∪ F.toFinset) = ρ.R := by    have h1 := ρ.mono (show F.toFinset ∪ P ⊆ (A.toFinset ∪ P) ∪ F.toFinset by      intro x; simp only [Finset.mem_union]; tauto)    have := ρ.le_R ((A.toFinset ∪ P) ∪ F.toFinset); omega  obtain ⟨s1, s2, s3, s4⟩ := ρ.shrink_spec (A.toFinset ∪ P) F F.toFinset hQF    (fun g hg hgn => absurd (List.mem_toFinset.mp hg) hgn)  set K := (shrinkP (A.toFinset ∪ P) F F.toFinset).val ρ.span  have hind := ρ.indep_of_ess s2 s3  have hPK : ρ.r (P ∪ K) = ρ.r P + K.card :=    ρ.indep_over_sub (show P ⊆ A.toFinset ∪ P from Finset.subset_union_right) hind  have hAK : ∀ x ∈ A, x ∉ K := by    intro x hxA hxK    have := s3 x hxK    have e : A.toFinset ∪ P ∪ K.erase x = A.toFinset ∪ P ∪ K := by      ext y; simp only [Finset.mem_union, Finset.mem_erase, List.mem_toFinset]      constructor      · rintro ((h | h) | h); exacts [Or.inl (Or.inl h), Or.inl (Or.inr h), Or.inr h.2]      · rintro ((h | h) | h)        · exact Or.inl (Or.inl h)        · exact Or.inl (Or.inr h)        · by_cases hy : y = x          · subst hy; exact Or.inl (Or.inl hxA)          · exact Or.inr ⟨hy, h⟩    rw [e] at this; omega  have hFK : (F.filter (fun x => decide (x ∉ K))).toFinset = F.toFinset \ K := by    ext x; simp  have hlen : (F.filter (fun x => decide (x ∉ K))).length = F.length - K.card := by    rw [← List.toFinset_card_of_nodup (hF.filter _), hFK, Finset.card_sdiff_of_subset s1,      List.toFinset_card_of_nodup hF]  have hKF : K.card ≤ F.length := by    rw [← List.toFinset_card_of_nodup hF]; exact Finset.card_le_card s1  refine ⟨⟨hA, hF.filter _, ?_, ?_, ?_, ?_, ?_⟩, s4⟩  · rw [Finset.disjoint_union_right]; refine ⟨hdis, ?_⟩    rw [Finset.disjoint_left]; intro x hx hxK; exact hAK x (List.mem_toFinset.mp hx) hxK  · rw [← Finset.union_assoc]; exact s2  · rw [hFK]    have h1 := ρ.mono (show F.toFinset ∪ P ⊆ (F.toFinset \ K) ∪ (P ∪ K) by      intro x; simp only [Finset.mem_union, Finset.mem_sdiff]; tauto)    have := ρ.le_R ((F.toFinset \ K) ∪ (P ∪ K)); omega  · rw [hlen, hPK]; omega  · intro x hx hxr    have hZ : P ∪ {x} ⊆ A.toFinset ∪ P := by      intro y; simp only [Finset.mem_union, Finset.mem_singleton, List.mem_toFinset]      rintro (h | h); exacts [Or.inr h, Or.inl (h ▸ hx)]    have h2 := ρ.indep_over_sub hZ hind    have e : P ∪ {x} ∪ K = P ∪ K ∪ {x} := by      ext y; simp only [Finset.mem_union, Finset.mem_singleton]; tauto    rw [e, hxr, hPK] at h2    exact hloop x hx (by omega)theorem walk_spec (L c h A : ℕ) (hL : 1 ≤ L) (hLc : L ≤ c) (hc : c = 2 ^ h) (hA : A = 2 * h + 4) :    ∀ (fuel : ℕ) (P : Finset α) (S F : List α), ρ.Inv P S F → S.length < fuel →    (walkP L c fuel P S F).val ρ.span = decide (∀ x ∈ S, ρ.r {x} ≠ 0) ∧    ∀ j, S.length ≤ 2 ^ j →      L * (walkP L c fuel P S F).cost ρ.span ≤ A * L * S.length + 2 * S.length * j := by  intro fuel  induction fuel with  | zero => intro P S F _ h; omega  | succ fuel ih =>  intro P S F hinv hfuel  obtain ⟨hS, hF, hdis, hSs, hFs, hFr, hloop⟩ := hinv  by_cases hk0 : F.length = 0  · simp only [walkP, hk0, if_true, val, cost]    refine ⟨?_, fun j _ => by simp⟩    have hRP : ρ.R = ρ.r P := by omega    cases S with    | nil => simp    | cons y ys =>      have : ρ.r {y} = 0 := by        apply hloop y (by simp)        have := ρ.mono (show P ⊆ P ∪ {y} from Finset.subset_union_left)        have := ρ.le_R (P ∪ {y}); omega      simp only [reduceCtorEq, decide_false, List.mem_cons, forall_eq_or_imp, ne_eq]      simp [this]  · by_cases hkt : S.length ≤ F.length    · simp only [walkP, hk0, if_false, hkt, if_true, val, cost]      have hSc : ρ.r (P ∪ S.toFinset) = ρ.r P + S.toFinset.card := by        have := ρ.union_le_card P S.toFinset        have e : P ∪ S.toFinset = S.toFinset ∪ P := Finset.union_comm _ _        rw [List.toFinset_card_of_nodup hS] at this ⊢        rw [e] at this ⊢; omega      refine ⟨(decide_eq_true ?_).symm, fun j _ => by simp⟩      intro x hx      exact ρ.nonloop_of_indep hSc (List.mem_toFinset.mpr hx)    · have hsubtf : ∀ A : List α, (∀ x ∈ A, x ∈ S) → A.toFinset ⊆ S.toFinset := by        intro A hA x hx; rw [List.mem_toFinset] at hx ⊢; exact hA x hx      have hk1 : 1 ≤ F.length := by omega      by_cases hsplit : L * F.length < S.length      · simp only [walkP, hk0, hkt, if_false, hsplit, if_true]        rw [val_bind, cost_bind, val_bind, cost_bind]        set t := S.length with ht        set A1 := S.take (t / 2) with hA1        set A2 := S.drop (t / 2) with hA2        have hLk : L ≤ L * F.length := Nat.le_mul_of_pos_right L hk1        have ht2 : 2 ≤ t := by omega        have hl1 : A1.length = t / 2 := by rw [hA1, List.length_take]; omega        have hl2 : A2.length = t - t / 2 := by rw [hA2, List.length_drop]        have hmem : ∀ x, x ∈ S ↔ x ∈ A1 ∨ x ∈ A2 := by          intro x; rw [← List.mem_append, List.take_append_drop]        have hA1n : A1.Nodup := hS.sublist (List.take_sublist _ _)        have hA2n : A2.Nodup := hS.sublist (List.drop_sublist _ _)        have hA1S : ∀ x ∈ A1, x ∈ S := fun x hx => (hmem x).mpr (Or.inl hx)        have hA2S : ∀ x ∈ A2, x ∈ S := fun x hx => (hmem x).mpr (Or.inr hx)        obtain ⟨inv1, c1⟩ := ρ.child_spec P A1 F hA1n hF          (Finset.disjoint_of_subset_left (hsubtf A1 hA1S) hdis)          (fun x hx => hloop x (hA1S x hx)) hFs hFr        obtain ⟨v1, cb1⟩ := ih _ A1 _ inv1 (by omega)        rw [c1]        by_cases hb1 : ∀ x ∈ A1, ρ.r {x} ≠ 0        · have hb1' : decide (∀ x ∈ A1, ρ.r {x} ≠ 0) = true := decide_eq_true hb1          simp only [v1, hb1', if_true]          rw [val_bind, cost_bind]          obtain ⟨inv2, c2⟩ := ρ.child_spec P A2 F hA2n hF            (Finset.disjoint_of_subset_left (hsubtf A2 hA2S) hdis)            (fun x hx => hloop x (hA2S x hx)) hFs hFr          obtain ⟨v2, cb2⟩ := ih _ A2 _ inv2 (by omega)          rw [c2, v2]          refine ⟨?_, ?_⟩          · apply decide_eq_decide.mpr            constructor            · intro H x hx              rcases (hmem x).mp hx with h1 | h2              · exact hb1 x h1              · exact H x h2            · intro H x hx; exact H x (hA2S x hx)          · intro j hj            obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := by              cases j with              | zero => simp at hj; omega              | succ j' => exact ⟨j', rfl⟩            have hp : 2 ^ (j' + 1) = 2 * 2 ^ j' := by ring            have e1 := cb1 j' (by omega)            have e2 := cb2 j' (by omega)            rw [hl1] at e1; rw [hl2] at e2            have ide : ∀ a b : ℕ, a + b = t → A * L * t = A * L * a + A * L * b := by              intro a b hab; rw [← hab]; ring            have ide2 : ∀ a b : ℕ, a + b = t →                2 * t * (j' + 1) = 2 * a * j' + 2 * b * j' + 2 * t := by              intro a b hab; rw [← hab]; ring            have i1 := ide (t / 2) (t - t / 2) (by omega)            have i2 := ide2 (t / 2) (t - t / 2) (by omega)            have i3 : L * cost ρ.span (shrinkP (A2.toFinset ∪ P) F F.toFinset) = L * F.length := by              rw [c2]            generalize ha : t / 2 = a at e1 e2 i1 i2            simp only [Nat.mul_add]            linarith        · have hb1' : decide (∀ x ∈ A1, ρ.r {x} ≠ 0) = false := decide_eq_false hb1          simp only [v1, hb1', Bool.false_eq_true, if_false, val, cost]          refine ⟨?_, ?_⟩          · apply (decide_eq_false _).symm            intro H; exact hb1 (fun x hx => H x (hA1S x hx))          · intro j hj            obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := by              cases j with              | zero => simp at hj; omega              | succ j' => exact ⟨j', rfl⟩            have hp : 2 ^ (j' + 1) = 2 * 2 ^ j' := by ring            have e1 := cb1 j' (by omega)            rw [hl1] at e1            have hAL : A * L * (t / 2) ≤ A * L * t := Nat.mul_le_mul_left _ (by omega)            have h3 : 2 * (t / 2) * j' ≤ 2 * t * j' := Nat.mul_le_mul_right _ (by omega)            have h4 : 2 * t * (j' + 1) = 2 * t * j' + 2 * t := by ring            simp only [Nat.mul_add]            omega      · simp only [walkP, hk0, hkt, if_false, hsplit]        rw [val_bind, cost_bind, val_bind, cost_bind]        set t := S.length with ht        set k := F.length with hk        have htc : t ≤ c * k := by          have := Nat.mul_le_mul_right k hLc; omega        obtain ⟨e1, e2, e3, e4, e5⟩ := ρ.extract_spec P c h (by rw [hc]; exact Nat.one_le_two_pow)          (by rw [hc]) t S rfl S.toFinset k hS htc hSs        set D := (extractP P c S S.toFinset).val ρ.span with hD        have hPD : ρ.r (P ∪ D) = ρ.r P + D.card := by          apply ρ.indep_of_ess          · rw [Finset.union_comm]; exact e3          · intro f hf            rw [Finset.union_comm]            exact e4 f hf (List.mem_toFinset.mp (e2 hf))        have hDk : D.card = k := by          have := Finset.union_comm P D ▸ e3; omega        set Dl := S.filter (fun x => decide (x ∈ D)) with hDl        set rest := S.filter (fun x => decide (x ∉ D)) with hrest        have hDlt : Dl.toFinset = D := by          ext x; simp only [hDl, List.toFinset_filter, Finset.mem_filter, List.mem_toFinset,            decide_eq_true_eq]          constructor          · exact fun h => h.2          · exact fun h => ⟨List.mem_toFinset.mp (e2 h), h⟩        have hDln : Dl.Nodup := hS.filter _        have hrn : rest.Nodup := hS.filter _        have hDll : Dl.length = k := by          rw [← List.toFinset_card_of_nodup hDln, hDlt, hDk]        have hrt : rest.toFinset = S.toFinset \ D := by          ext x; simp [hrest, and_comm]        have hrl : rest.length = t - k := by          rw [← List.toFinset_card_of_nodup hrn, hrt, Finset.card_sdiff_of_subset e2,            List.toFinset_card_of_nodup hS, hDk]        have hrS : ∀ x ∈ rest, x ∈ S := fun x hx => (List.mem_filter.mp hx).1        obtain ⟨inv1, c1⟩ := ρ.child_spec P rest Dl hrn hDln          (Finset.disjoint_of_subset_left (hsubtf rest hrS) hdis)          (fun x hx => hloop x (hrS x hx)) (by rw [hDlt]; exact e3)          (by rw [hDll]; exact hFr)        obtain ⟨v1, cb1⟩ := ih _ rest _ inv1 (by omega)        rw [v1, c1, hDll]        refine ⟨?_, ?_⟩        · apply decide_eq_decide.mpr          constructor          · intro H x hx            by_cases hxD : x ∈ D            · exact ρ.nonloop_of_indep hPD hxD            · exact H x (List.mem_filter.mpr ⟨hx, by simpa using hxD⟩)          · intro H x hx; exact H x (hrS x hx)        · intro j hj          have f1 := cb1 j (by omega)          rw [hrl] at f1          have hSD : (S.toFinset ∩ D).card = k := by            rw [Finset.inter_eq_right.mpr e2, hDk]          rw [hSD] at e5          have f2 : L * cost ρ.span (extractP P c S S.toFinset) ≤ L * (2 * (h + 1) * k + k) :=            Nat.mul_le_mul_left L e5          have ide : ∀ u : ℕ, u + k = t → A * L * t = A * L * u + L * (2 * (h + 1) * k + k) + L * k := by            intro u hu; rw [← hu, hA]; ring          have ide2 : ∀ u : ℕ, u + k = t → 2 * t * j = 2 * u * j + 2 * k * j := by            intro u hu; rw [← hu]; ring          have i1 := ide (t - k) (by omega)          have i2 := ide2 (t - k) (by omega)          generalize t - k = u at f1 i1 i2          have i3 : 0 ≤ 2 * k * j := Nat.zero_le _          simp only [Nat.mul_add]          linarithend RankFnend ColoopLogLognamespace ColoopLogLogopen Prognamespace Progdef trunc {α : Type} : ℕ → Prog α Bool → Prog α Bool  | _, .ret b => .ret b  | 0, .ask _ _ => .ret false  | b + 1, .ask T k => .ask T (fun x => trunc b (k x))theorem val_trunc {α : Type} (σ : Finset α → Bool) :    ∀ (b : ℕ) (p : Prog α Bool), p.cost σ ≤ b → (trunc b p).val σ = p.val σ := by  intro b  induction b with  | zero =>    intro p hp    cases p with    | ret x => rfl    | ask T k => simp [cost] at hp  | succ b ih =>    intro p hp    cases p with    | ret x => rfl    | ask T k =>      simp only [trunc, val]      exact ih _ (by simp only [cost] at hp; omega)def toTree {n : ℕ} : Prog (Fin n) Bool → MatroidIndepQueryTree (Fin n)  | .ret b => .leaf b  | .ask T k => .query (Finset.univ \ T) (toTree (k false)) (toTree (k true))theorem eval_toTree {n : ℕ} (o : Finset (Fin n) → Bool) (p : Prog (Fin n) Bool) :    (toTree p).eval o = p.val (fun T => o (Finset.univ \ T)) := by  induction p with  | ret b => rfl  | ask T k ih =>    simp only [toTree, MatroidIndepQueryTree.eval, val]    cases h : o (Finset.univ \ T) <;> simp [ih]theorem depth_toTree_trunc {n : ℕ} :    ∀ (b : ℕ) (p : Prog (Fin n) Bool), (toTree (trunc b p)).depth ≤ b := by  intro b  induction b with  | zero =>    intro p; cases p with    | ret x => simp [trunc, toTree, MatroidIndepQueryTree.depth]    | ask T k => simp [trunc, toTree, MatroidIndepQueryTree.depth]  | succ b ih =>    intro p; cases p with    | ret x => simp [trunc, toTree, MatroidIndepQueryTree.depth]    | ask T k =>      simp only [trunc, toTree, MatroidIndepQueryTree.depth]      have := ih (k false); have := ih (k true); omegaend Prognamespace RankFndef mainP (n L c : ℕ) : Prog (Fin n) Bool :=  (shrinkP ∅ (List.finRange n) Finset.univ).bind fun K =>    walkP L c (n + 1) ∅ (List.finRange n) ((List.finRange n).filter (fun x => decide (x ∈ K)))theorem main_spec {n : ℕ} (ρ : RankFn (Fin n)) (L c h A : ℕ) (hL : 1 ≤ L) (hLc : L ≤ c)    (hc : c = 2 ^ h) (hA : A = 2 * h + 4) (j : ℕ) (hj : n ≤ 2 ^ j) :    (mainP n L c).val ρ.span = decide (∀ x, ρ.r {x} ≠ 0) ∧    L * (mainP n L c).cost ρ.span ≤ L * n + A * L * n + 2 * n * j := by  unfold mainP  rw [val_bind, cost_bind]  have huniv : (List.finRange n).toFinset = Finset.univ := by    ext x; simp [List.mem_finRange]  obtain ⟨s1, s2, s3, s4⟩ := ρ.shrink_spec ∅ (List.finRange n) Finset.univ    (by simp [R]) (fun g _ hg => absurd (List.mem_finRange g) hg)  set K := (shrinkP ∅ (List.finRange n) Finset.univ).val ρ.span  have hind := ρ.indep_of_ess s2 s3  rw [ρ.r_empty] at hind  set Fl := (List.finRange n).filter (fun x => decide (x ∈ K))  have hFln : Fl.Nodup := (List.nodup_finRange n).filter _  have hFlt : Fl.toFinset = K := by ext x; simp [Fl, List.mem_finRange]  have hFll : Fl.length = K.card := by rw [← List.toFinset_card_of_nodup hFln, hFlt]  have inv : ρ.Inv ∅ (List.finRange n) Fl := by    refine ⟨List.nodup_finRange n, hFln, by simp, ?_, ?_, ?_, ?_⟩    · rw [huniv]; simp [R]    · rw [hFlt]; simpa using s2    · rw [ρ.r_empty, hFll]; simp only [Finset.empty_union] at hind s2; omega    · intro x _ hx; simpa [ρ.r_empty] using hx  obtain ⟨v, cb⟩ := ρ.walk_spec L c h A hL hLc hc hA (n + 1) ∅ _ _ inv (by simp)  rw [v, s4]  refine ⟨?_, ?_⟩  · apply decide_eq_decide.mpr    simp [List.mem_finRange]  · have := cb j (by simpa using hj)    simp only [List.length_finRange] at this    rw [List.length_finRange, Nat.mul_add]    omegaend RankFn/-! ## The dual rank function of a matroid -/section Bridgevariable {n : ℕ} (M : Matroid (Fin n))theorem dual_eRk_ne_top (X : Finset (Fin n)) : M✶.eRk (X : Set (Fin n)) ≠ ⊤ := by  have h1 := M✶.eRk_le_encard (X : Set (Fin n))  rw [Set.encard_coe_eq_coe_finsetCard] at h1  exact ne_top_of_le_ne_top (ENat.natCast_ne_top _) h1theorem dual_eRk_coe (X : Finset (Fin n)) :    M✶.eRk (X : Set (Fin n)) = ((M✶.eRk (X : Set (Fin n))).toNat : ℕ∞) :=  (ENat.natCast_toNat (dual_eRk_ne_top M X)).symmnoncomputable def dualRank : RankFn (Fin n) where  r X := (M✶.eRk (X : Set (Fin n))).toNat  mono := by    intro X Y hXY    exact ENat.toNat_le_toNat (M✶.eRk_mono (Finset.coe_subset.mpr hXY)) (dual_eRk_ne_top M Y)  sub := by    intro X Y    have h := M✶.eRk_inter_add_eRk_union_le (X : Set (Fin n)) (Y : Set (Fin n))    rw [← Finset.coe_inter, ← Finset.coe_union, dual_eRk_coe M (X ∩ Y), dual_eRk_coe M (X ∪ Y),      dual_eRk_coe M X, dual_eRk_coe M Y] at h    have h' : (M✶.eRk ↑(X ∩ Y)).toNat + (M✶.eRk ↑(X ∪ Y)).toNat ≤        (M✶.eRk ↑X).toNat + (M✶.eRk ↑Y).toNat := by exact_mod_cast h    omega  card := by    intro X    have h1 := M✶.eRk_le_encard (X : Set (Fin n))    rw [Set.encard_coe_eq_coe_finsetCard, dual_eRk_coe M X] at h1    exact_mod_cast h1theorem dualRank_span (hE : M.E = Set.univ) (o : Finset (Fin n) → Bool)    (ho : ∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) :    (fun T => o (Finset.univ \ T)) = (dualRank M).span := by  funext T  apply Bool.eq_iff_iff.mpr  rw [ho, RankFn.span, decide_eq_true_iff]  have hE' : M✶.E = Set.univ := by rw [Matroid.dual_ground, hE]  have hT : (T : Set (Fin n)) ⊆ M✶.E := by rw [hE']; exact Set.subset_univ _  rw [Finset.coe_sdiff, Finset.coe_univ, ← Matroid.dual_coindep_iff, ← hE',    ← Matroid.spanning_iff_compl_coindep hT, Matroid.spanning_iff_eRk_le hT,    Matroid.eRank_def, hE']  show _ ↔ (M✶.eRk (T : Set (Fin n))).toNat = (M✶.eRk ((Finset.univ : Finset (Fin n)) : Set (Fin n))).toNat  rw [Finset.coe_univ]  have h1 := M✶.eRk_mono (Set.subset_univ (T : Set (Fin n)))  have h2 := dual_eRk_coe M T  have h3 := dual_eRk_coe M Finset.univ  rw [Finset.coe_univ] at h3  constructor  · intro h    have : M✶.eRk (T : Set (Fin n)) = M✶.eRk Set.univ := le_antisymm h1 h    rw [this]  · intro h    rw [h2, h3, h]theorem dualRank_loop (hE : M.E = Set.univ) (x : Fin n) :    (dualRank M).r {x} ≠ 0 ↔ ¬ M.IsColoop x := by  have hE' : M✶.E = Set.univ := by rw [Matroid.dual_ground, hE]  show (M✶.eRk ((({x} : Finset (Fin n))) : Set (Fin n))).toNat ≠ 0 ↔ ¬ M✶.IsLoop x  rw [Finset.coe_singleton]  have hne := dual_eRk_ne_top M {x}  rw [Finset.coe_singleton] at hne  rw [Ne, ENat.toNat_eq_zero, or_iff_left hne,    Matroid.eRk_eq_zero_iff (by rw [hE']; exact Set.subset_univ _), Set.singleton_subset_iff,    ← Matroid.isLoop_iff]end Bridgetheorem coloop_detection_main : ∃ C : ℕ, ∀ n : ℕ, ∃ T : MatroidIndepQueryTree (Fin n),    T.depth ≤ C*n*(1 + Nat.log2 (1 + Nat.log2 (n+2))) ∧ ∀ M : Matroid (Fin n), M.E = Set.univ →    ∀ o : Finset (Fin n) → Bool, (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) →    (T.eval o = true ↔ ∀ x : Fin n, ¬M.IsColoop x) := by  refine ⟨9, fun n => ?_⟩  set j := Nat.log2 (n + 2) + 1 with hj  set h := Nat.log2 j + 1 with hh  set B := 9 * n * (1 + Nat.log2 (1 + Nat.log2 (n + 2))) with hB  refine ⟨Prog.toTree (Prog.trunc B (RankFn.mainP n j (2 ^ h))), Prog.depth_toTree_trunc _ _, ?_⟩  intro M hE o ho  rw [Prog.eval_toTree, dualRank_span M hE o ho]  have hjn : n ≤ 2 ^ j := by have h0 : n + 2 < 2 ^ j := Nat.lt_log2_self; omega  have hjc : j ≤ 2 ^ h := by have h0 : j < 2 ^ h := Nat.lt_log2_self; omega  obtain ⟨v, cb⟩ := RankFn.main_spec (dualRank M) j (2 ^ h) h (2 * h + 4) (by omega) hjc rfl rfl    j hjn  have hcost : (RankFn.mainP n j (2 ^ h)).cost (dualRank M).span ≤ B := by    have e : j * n + (2 * h + 4) * j * n + 2 * n * j = j * ((2 * h + 7) * n) := by ring    rw [e] at cb    have := Nat.le_of_mul_le_mul_left cb (by omega : 0 < j)    have hj' : 1 + Nat.log2 (n + 2) = j := by omega    rw [hB, hj']    have : (2 * h + 7) * n ≤ 9 * n * (1 + Nat.log2 j) := by      rw [hh]; nlinarith    omega  rw [Prog.val_trunc _ _ _ hcost, v, decide_eq_true_iff]  exact forall_congr' fun x => dualRank_loop M hE xend ColoopLogLogtheorem matroid_coloop_detection_log_log : ∃ C : ℕ, ∀ n : ℕ, ∃ T : MatroidIndepQueryTree (Fin n), T.depth ≤ C*n*(1 + Nat.log2 (1 + Nat.log2 (n+2))) ∧ ∀ M : Matroid (Fin n), M.E = Set.univ → ∀ o : Finset (Fin n) → Bool, (∀ Q, o Q = true ↔ M.Indep (Q : Set (Fin n))) → (T.eval o = true ↔ ∀ x : Fin n, ¬M.IsColoop x) := by  exact ColoopLogLog.coloop_detection_main
See it in the graphDiscuss this resultChallenge it