1-- The order on decompositions: T precedes U when their partition sequences2-- and chosen colours agree below some stage at which U is strictly coarser3-- or chooses a larger colour. A finite family of decompositions into trees4-- and a rest has a maximal member.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.GraphTheory.TreePacking.Order11import LastColour1213-- The least natural of the empty set is ∅, so leastNatural(S) is a natural14-- for every set S of naturals, inhabited or not.15theorem leastNatural.empty16shows leastNatural(∅) =∅17proof18unfold leastNatural19have none : leastNatural.candidates(∅) =∅20ext n21constructor22 . intro member23exfalso24exactset.empty with member.right.left25 . intro member26exfalso27exactset.empty with member28rw none29exact union.empty3031theorem leastNatural.natural (S : Set)32requires naturals : S ⊆ ℕ33showsNat(leastNatural(S))34proof35 by_cases empty : S =∅36 . rw [empty, leastNatural.empty, ← nat.zero]37exact Naturals.constructor.zero38 . have inhabited := Nonempty.of_ne_empty with empty39have member := leastNatural.member with inhabited, naturals40exact nat.omega.backward with (naturals with leastNatural(S), member)4142-- A term named by a variable, to shorten long proofs.43theorem exists_eq (x : Set)44shows∃ c, c = x45proof46witness x47rfl4849-- The colour chosen at a stage: the least disconnected colour, or k once50-- every colour is internally connected, as the least of the disconnected51-- colours with k adjoined.52def stageColour (leftEnd rightEnd T P : Set, k : Nat) : Set:=53 leastNatural(disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k})5455theorem stageColour.subset (leftEnd rightEnd T P : Set, k : Nat)56shows disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k} ⊆ ℕ57proof58intros i member59simpat member60have subset := disconnectedColour.subset at leftEnd, rightEnd, T, P, k61rcases member with colour | same62 . exact subset with i, colour63 . rw same64exact nat.omega.forward at k with*6566theorem stageColour.inhabited (leftEnd rightEnd T P : Set, k : Nat)67shows Nonempty(disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k})68proof69have member : k ∈ disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k}70bysimp71exact⟨k, member⟩7273theorem stageColour.natural (leftEnd rightEnd T P : Set, k : Nat)74showsNat(stageColour(leftEnd, rightEnd, T, P, k))75proof76unfold stageColour77exact leastNatural.natural with stageColour.subset7879theorem stageColour.member (leftEnd rightEnd T P : Set, k : Nat)80shows stageColour(leftEnd, rightEnd, T, P, k) ∈81 disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k}82proof83unfold stageColour84exact leastNatural.member with stageColour.inhabited, stageColour.subset8586theorem stageColour.least (leftEnd rightEnd T P x : Set, k : Nat)87requires member : x ∈ disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k}88shows stageColour(leftEnd, rightEnd, T, P, k) ≤ x89proof90have subset := stageColour.subset at leftEnd, rightEnd, T, P, k91have xNat := nat.omega.backward with (subset with x, member)92unfold stageColour93exact leastNatural.least with stageColour.inhabited, subset, member9495-- The chosen colour is k exactly when every colour is internally connected,96-- and otherwise the least disconnected colour.97theorem stageColour.of_none (leftEnd rightEnd T P : Set, k : Nat)98requires none : disconnectedColour(leftEnd, rightEnd, T, P, k) =∅99shows stageColour(leftEnd, rightEnd, T, P, k) = k100proof101have member := stageColour.member at leftEnd, rightEnd, T, P, k102rw none at member103simpat member104exact member105106theorem stageColour.of_some (leftEnd rightEnd T P : Set, k : Nat)107requires some : disconnectedColour(leftEnd, rightEnd, T, P, k) ≠∅108shows stageColour(leftEnd, rightEnd, T, P, k) =109 leastNatural(disconnectedColour(leftEnd, rightEnd, T, P, k))110proof111have inhabited := Nonempty.of_ne_empty with some112have subsetColours := disconnectedColour.subset at leftEnd, rightEnd, T, P, k113have chosen := leastNatural.member with inhabited, subsetColours114have chosenNat := leastNatural.natural with subsetColours115have stageNat := stageColour.natural at leftEnd, rightEnd, T, P, k116have chosenIn :117 leastNatural(disconnectedColour(leftEnd, rightEnd, T, P, k)) ∈118 disconnectedColour(leftEnd, rightEnd, T, P, k) ∪ {k} bysimp [chosen]119have below := stageColour.least with chosenIn120have member := stageColour.member at leftEnd, rightEnd, T, P, k121simpat member122have above : leastNatural(disconnectedColour(leftEnd, rightEnd, T, P, k)) ≤123 stageColour(leftEnd, rightEnd, T, P, k)124rcases member with inColours | isK125 . exact leastNatural.least with inhabited, subsetColours, inColours126 . rw isK127have less := Less.of_membership128at leastNatural(disconnectedColour(leftEnd, rightEnd, T, P, k)), k129with chosen.left130arith131exact LessEqual.antisymmetric with below, above132133-- The order on decompositions, as in the paper: T precedes T' when the134-- partition sequences and the chosen colours agree below some stage j, and135-- at j the partition of T' is strictly coarser or the colour of T' is larger.136predicate Precedes (V leftEnd rightEnd T U : Set, k : Nat) :=137∃ j ∈ ℕ, (∀ i ∈ j,138 partitionSequence(V, leftEnd, rightEnd, T, k, i) =139 partitionSequence(V, leftEnd, rightEnd, U, k, i) ∧140 stageColour(leftEnd, rightEnd, T,141 partitionSequence(V, leftEnd, rightEnd, T, k, i), k) =142 stageColour(leftEnd, rightEnd, U,143 partitionSequence(V, leftEnd, rightEnd, U, k, i), k)) ∧144 ((Refines(partitionSequence(V, leftEnd, rightEnd, T, k, j),145 partitionSequence(V, leftEnd, rightEnd, U, k, j)) ∧146 partitionSequence(V, leftEnd, rightEnd, T, k, j) ≠147 partitionSequence(V, leftEnd, rightEnd, U, k, j)) ∨148 (partitionSequence(V, leftEnd, rightEnd, T, k, j) =149 partitionSequence(V, leftEnd, rightEnd, U, k, j) ∧150 stageColour(leftEnd, rightEnd, T,151 partitionSequence(V, leftEnd, rightEnd, T, k, j), k) <152 stageColour(leftEnd, rightEnd, U,153 partitionSequence(V, leftEnd, rightEnd, U, k, j), k)))154155-- The order is strict: no decomposition precedes itself, since at the stage156-- j the partition would differ from itself or the colour be below itself.157theorem Precedes.irreflexive (V leftEnd rightEnd T : Set, k : Nat)158shows¬Precedes(V, leftEnd, rightEnd, T, T, k)159proof160intro precedes161obtain⟨j, _, _, step⟩:= precedes162rcases step with⟨_, different⟩|⟨_, less⟩163 . exact different withrfl164 . have natural := stageColour.natural at leftEnd, rightEnd, T,165 partitionSequence(V, leftEnd, rightEnd, T, k, j), k166exact Less.irreflexive with natural, less167168-- Agreement of partitions and chosen colours below a stage composes, up to169-- the earlier of the two stages.170theorem Precedes.agree_below (V leftEnd rightEnd T U W : Set, k j l n : Nat)171requires first : ∀ i ∈ j,172 partitionSequence(V, leftEnd, rightEnd, T, k, i) =173 partitionSequence(V, leftEnd, rightEnd, U, k, i) ∧174 stageColour(leftEnd, rightEnd, T,175 partitionSequence(V, leftEnd, rightEnd, T, k, i), k) =176 stageColour(leftEnd, rightEnd, U,177 partitionSequence(V, leftEnd, rightEnd, U, k, i), k)178requires second : ∀ i ∈ l,179 partitionSequence(V, leftEnd, rightEnd, U, k, i) =180 partitionSequence(V, leftEnd, rightEnd, W, k, i) ∧181 stageColour(leftEnd, rightEnd, U,182 partitionSequence(V, leftEnd, rightEnd, U, k, i), k) =183 stageColour(leftEnd, rightEnd, W,184 partitionSequence(V, leftEnd, rightEnd, W, k, i), k)185requires boundJ : n ≤ j186requires boundL : n ≤ l187shows∀ i ∈ n,188 partitionSequence(V, leftEnd, rightEnd, T, k, i) =189 partitionSequence(V, leftEnd, rightEnd, W, k, i) ∧190 stageColour(leftEnd, rightEnd, T,191 partitionSequence(V, leftEnd, rightEnd, T, k, i), k) =192 stageColour(leftEnd, rightEnd, W,193 partitionSequence(V, leftEnd, rightEnd, W, k, i), k)194proof195intros i member196have iNat := natural.member at n with i, member197have less := Less.of_membership at i, n with member198have belowJ : i < j byarith199have belowL : i < l byarith200obtain⟨sameTU, coloursTU⟩:=201 first with i, (membership.of_Less at i, j with*, belowJ)202obtain⟨sameUW, coloursUW⟩:=203 second with i, (membership.of_Less at i, l with*, belowL)204constructor205 . rw [sameTU, sameUW]206 . rw [coloursTU, coloursUW]207208-- Transitivity: the stage of the composed comparison is the earlier of the209-- two stages; at an equal stage the two partition drops compose by a strict210-- count, and the two colour drops by the order of ℕ.211theorem Precedes.transitive (V E leftEnd rightEnd T U W : Set, k : Nat)212requires graph : Graph(V, E, leftEnd, rightEnd)213requires finiteVertices : Finite(V)214requires nonempty : V ≠∅215requires decompositionT : Decomposition(E, T, k)216requires decompositionU : Decomposition(E, U, k)217requires decompositionW : Decomposition(E, W, k)218requires first : Precedes(V, leftEnd, rightEnd, T, U, k)219requires second : Precedes(V, leftEnd, rightEnd, U, W, k)220shows Precedes(V, leftEnd, rightEnd, T, W, k)221proof222obtain⟨j, jNatural, agreeTU, stepTU⟩:= first223obtain⟨l, lNatural, agreeUW, stepUW⟩:= second224have jNat := nat.omega.backward with jNatural225have lNat := nat.omega.backward with lNatural226have order := Less.trichotomy at j, l with*227rcases order with before | same | after228 . -- The first comparison decides, since U and W still agree at stage j.229have inL := membership.of_Less at j, l with*, before230obtain⟨sameUW, coloursUW⟩:= agreeUW with j, inL231have below : j ≤ l byarith232refine⟨j, jNatural, (Precedes.agree_below with233 agreeTU, agreeUW, (LessEqual.reflexive at j), below), ?_⟩234rw [coloursUW, sameUW] at stepTU235exact stepTU236 . -- Equal stages: the drops compose.237rwunder same at agreeTU238rw same at stepTU239refine⟨l, lNatural, (Precedes.agree_below with agreeTU, agreeUW,240 (LessEqual.reflexive at l), (LessEqual.reflexive at l)), ?_⟩241have partitionT : Partition(V,242 partitionSequence(V, leftEnd, rightEnd, T, k, l)) :=243 partitionSequence.partition with graph, nonempty, decompositionT244have partitionU : Partition(V,245 partitionSequence(V, leftEnd, rightEnd, U, k, l)) :=246 partitionSequence.partition with graph, nonempty, decompositionU247have partitionW : Partition(V,248 partitionSequence(V, leftEnd, rightEnd, W, k, l)) :=249 partitionSequence.partition with graph, nonempty, decompositionW250rcases stepTU with⟨refinesTU, differentTU⟩|⟨sameTU, lessTU⟩251 . rcases stepUW with⟨refinesUW, differentUW⟩|⟨sameUW, lessUW⟩252 . -- Two strict refinements: the class counts strictly increase twice.253left254refine⟨(Refines.transitive with refinesTU, refinesUW), ?_⟩255have dropTU := Refines.cardinality_lt with256 finiteVertices, partitionU, partitionT, refinesTU, differentTU257have dropUW := Refines.cardinality_lt with258 finiteVertices, partitionW, partitionU, refinesUW, differentUW259have finiteU := Partition.finite with finiteVertices, partitionU260have finiteW := Partition.finite with finiteVertices, partitionW261intro sameTW262rw sameTW at dropTU263arith264 . left265rw sameUW at refinesTU, differentTU266exact⟨refinesTU, differentTU⟩267 . rcases stepUW with⟨refinesUW, differentUW⟩|⟨sameUW, lessUW⟩268 . left269rw← sameTU at refinesUW, differentUW270exact⟨refinesUW, differentUW⟩271 . right272have naturalT := stageColour.natural at leftEnd, rightEnd, T,273 partitionSequence(V, leftEnd, rightEnd, T, k, l), k274have naturalU := stageColour.natural at leftEnd, rightEnd, U,275 partitionSequence(V, leftEnd, rightEnd, U, k, l), k276have naturalW := stageColour.natural at leftEnd, rightEnd, W,277 partitionSequence(V, leftEnd, rightEnd, W, k, l), k278constructor279 . rw [sameTU, sameUW]280 . exact Less.transitive with naturalT, naturalU, naturalW,281 lessTU, lessUW282 . -- The second comparison decides, since T and U still agree at stage l.283have inJ := membership.of_Less at l, j with*, after284obtain⟨sameTU, coloursTU⟩:= agreeTU with l, inJ285have below : l ≤ j byarith286refine⟨l, lNatural, (Precedes.agree_below with287 agreeTU, agreeUW, below, (LessEqual.reflexive at l)), ?_⟩288rw [← coloursTU, ← sameTU] at stepUW289exact stepUW290291-- A nonempty finite family of decompositions has a maximal member: adjoin292-- the members one at a time, keeping the old maximal element unless the new293-- one lies above it.294theorem Precedes.exists_maximal (V E leftEnd rightEnd S : Set, k : Nat)295requires graph : Graph(V, E, leftEnd, rightEnd)296requires finiteVertices : Finite(V)297requires nonempty : V ≠∅298requires finite : Finite(S)299requires members : ∀ T ∈ S, Decomposition(E, T, k)300shows S ≠∅→301∃ T ∈ S, ∀ U ∈ S, ¬Precedes(V, leftEnd, rightEnd, T, U, k)302proof303induction finite with G a304| empty =>305intro inhabited306exfalso307exact inhabited withrfl308| insert using ih =>309intro _310have aIn : a ∈ G ∪ {a} bysimp311have decompositionA := members with a, aMember312 by_cases emptyG : G =∅313 . refine⟨a, aIn, ?_⟩314intros U member above315rw emptyG at member316simpat member317rw member at above318exact Precedes.irreflexive with above319 . obtain⟨M, inG, maximal⟩:= ih with emptyG320have decompositionM := members with M, (GSubset with M, inG)321 by_cases better : Precedes(V, leftEnd, rightEnd, M, a, k)322 . -- a is maximal: a member above a would lie above M.323refine⟨a, aIn, ?_⟩324intros U member above325simpat member326rcases member with inG2 | same327 . have decompositionU := members with U, (GSubset with U, inG2)328have chain := Precedes.transitive with graph, finiteVertices,329 nonempty, decompositionM, decompositionA, decompositionU,330 better, above331exact maximal with U, inG2, chain332 . rw same at above333exact Precedes.irreflexive with above334 . have mIn : M ∈ G ∪ {a} bysimp [inG]335refine⟨M, mIn, ?_⟩336intros U member above337simpat member338rcases member with inG2 | same339 . exact maximal with U, inG2, above340 . rw same at above341exact better with above342343-- The decompositions into succ(k) colours whose first k colours are trees:344-- a finite family, as a set of subsets of succ(k) × 𝒫(E).345def treeDecompositions (V E leftEnd rightEnd : Set, k : Nat) : Set:=346 { T ∈𝒫(succ(k) ×𝒫(E)) | Decomposition(E, T, succ(k)) ∧347 (∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i))) }348349theorem treeDecompositions.finite (V E leftEnd rightEnd : Set, k : Nat)350requires finiteEdges : Finite(E)351shows Finite(treeDecompositions(V, E, leftEnd, rightEnd, k))352proof353have subset : treeDecompositions(V, E, leftEnd, rightEnd, k) ⊆354𝒫(succ(k) ×𝒫(E))355intros T member356exact member.left357have finiteIndex : Finite(succ(k)) := Finite.natural at succ(k)358have finiteProduct := Finite.product with359 finiteIndex, (Finite.powerset with finiteEdges)360exact Finite.subset with (Finite.powerset with finiteProduct), subset361362-- The order is a strict partial order on the finitely many decompositions,363-- so a maximal one exists among those whose first k colours are trees.364theorem Precedes.maximal (V E leftEnd rightEnd : Set, k : Nat)365requires graph : Graph(V, E, leftEnd, rightEnd)366requires finiteVertices : Finite(V)367requires finiteEdges : Finite(E)368requires nonempty : V ≠∅369requires some : ∃ T, Decomposition(E, T, succ(k)) ∧370 (∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i)))371shows∃ T, Decomposition(E, T, succ(k)) ∧372 (∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, T(i))) ∧373¬(∃ U, Decomposition(E, U, succ(k)) ∧374 (∀ i ∈ k, SpanningTree(V, leftEnd, rightEnd, U(i))) ∧375 Precedes(V, leftEnd, rightEnd, T, U, succ(k)))376proof377obtain⟨T₀, decomposition₀, trees₀⟩:= some378have finiteFamily := treeDecompositions.finite379at V, E, leftEnd, rightEnd, k with finiteEdges380have members : ∀ T ∈ treeDecompositions(V, E, leftEnd, rightEnd, k),381 Decomposition(E, T, succ(k))382intros T member383exact member.right.left384have inhabited : treeDecompositions(V, E, leftEnd, rightEnd, k) ≠∅385apply Nonempty.ne_empty386witness T₀387have relation : T₀ ⊆ succ(k) ×𝒫(E) := decomposition₀.left.left388exact⟨(powerset.membership_iff.backward with relation),389 decomposition₀, trees₀⟩390have step := Precedes.exists_maximal with391 graph, finiteVertices, nonempty, finiteFamily, members392obtain⟨T, member, maximal⟩:= step with inhabited393obtain⟨_, decomposition, trees⟩:= member394refine⟨T, decomposition, trees, ?_⟩395intro worse396obtain⟨U, decompositionU, treesU, precedes⟩:= worse397have relation : U ⊆ succ(k) ×𝒫(E) := decompositionU.left.left398have memberU : U ∈ treeDecompositions(V, E, leftEnd, rightEnd, k) :=399⟨(powerset.membership_iff.backward with relation),400 decompositionU, treesU⟩401exact maximal with U, memberU, precedes402