Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Union-find: exact mutable union contract

Proved
UnionFindAnalysis.union_contract

by lunjia · Oct 10, 2026 · Mathlib 0df444a (Lean v4.33.1)

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

A represented union-find object admits a terminating UnionFind.union.Call for any two in-range vertices a,ba,ba,b. The Boolean result is true exactly when their representatives differed in the initial model. The theorem exposes witnesses for the two traversed paths, the two compression results and rank link, the corresponding final model, exact clock increments, and storage preservation outside the parent/rank arrays.

If the two path lengths are ppp and qqq, unionCosts p q charges p+q+2p+q+2p+q+2 uf_find ticks, p+qp+qp+q uf_compress ticks and one uf_union tick; every other clock has increment zero. This is the exact operational basis for the later amortized theorem.

The precondition Rep heap layout model connects an allocated object and distinct allocated parent/rank arrays to a valid forest on Fin n. Validity includes representative consistency, strictly increasing rank along nonroot parent links, and the root-mass rank bound. It does not assume a particular operation history. Vertex arguments belong to Fin n; no such argument exists when n=0n=0n=0.

<!-- algo2lean-provenance-v0.1.4 -->

Algo2Lean provenance and reproduction. These definitions target Algo2Lean v0.1.4, using the compiler source at public commit 0f79d5e4242e6477531c9ee4410409f5b1c92f76, and Lean 4.33.1. The versioned compiler location is Algo2Lean v0.1.4. Compile all supplied .algo files and imports to reproduce the raw generated definitions. Release publication and exact website parity require their separately recorded checks; this metadata alone does not assert them. Prove2Me imports and declaration packaging are mechanically adapted, with original hashes, compiler-reported source ranges, and adaptations recorded in transplant provenance. Exact reproduction does not prove the compiler correct.

Preamble
import Definitions.Def_Algo_union_find_Analysis
import Theorems.Thm_Algo2Lean_UnionFind_compress_valid
import Theorems.Thm_Algo2Lean_UnionFind_link_valid

set_option autoImplicit true
set_option backward.isDefEq.respectTransparency false
set_option backward.defeqAttrib.useBackward true

set_option autoImplicit false
Formal statement
theorem UnionFindAnalysis.union_contract :
    ∀ {n : Nat} (heap : Algo2LeanGenerated.Heap) (layout : UnionFindAnalysis.Layout)
      (model : Algo2Lean.UnionFind.State n),
      UnionFindAnalysis.Rep heap layout model →
        ∀ (a b : Fin n),
          Exists.{1} fun first =>
            Exists.{1} fun second =>
              Exists.{1} fun after =>
                Algo2Lean.UnionFind.Find model a (model.representative a) first ∧
                  Algo2Lean.UnionFind.Find (UnionFindAnalysis.findResult model a first) b
                      ((UnionFindAnalysis.findResult model a first).representative b) second ∧
                    Algo.union_find.UnionFind.union.Call heap
                        (Prod.mk.{0, 0} layout.object
                          (Prod.mk.{0, 0} (↑(↑a : Nat) : Int) (↑(↑b : Nat) : Int)))
                        after
                        (Decidable.decide (Ne.{1} (model.representative a) (model.representative b))) ∧
                      UnionFindAnalysis.Rep after layout
                          (UnionFindAnalysis.unionResult model a b first second) ∧
                        UnionFindAnalysis.Storage heap after
                            (List.cons.{0} layout.parent (List.cons.{0} layout.rank List.nil.{0})) ∧
                          heap.Increments after (UnionFindAnalysis.unionCosts first second) := by
  sorry
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/kruskal/analysis/union_find.lean#L538-L581

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me