1-- Maximal extensions in families closed under nonempty chain unions.2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Set.MaximalPrinciple8import Zorn910def extensions (F I : Set) : Set:= {J ∈ F | I ⊆ J}1112theorem inclusionChain.nested (F S : Set)13requires chain : Chain(F, inclusionOrder(F), S)14shows∀ C ∈ S, ∀ D ∈ S, C ⊆ D ∨ D ⊆ C15proof16rcases chain with⟨bounded, comparable⟩17intros C hC D hD18have memberC := bounded with C, hC19have memberD := bounded with D, hD20have directions := comparable with C, hC, D, hD21have forward := inclusionOrder.relates at F, C, D with*22have backward := inclusionOrder.relates at F, D, C with*23cases directions with cd | dc24 . left25exact forward.forward with cd26 . right27exact backward.forward with dc2829-- Only nonempty unions are required; I supplies the empty-chain bound.30theorem maximal_extension (F I : Set)31requires seed : I ∈ F32requires closed : ∀ S, Nonempty(S) → S ⊆ F →33 (∀ C ∈ S, ∀ D ∈ S, C ⊆ D ∨ D ⊆ C) →⋃(S) ∈ F34shows∃ M ∈ F, I ⊆ M ∧ (∀ J ∈ F, M ⊆ J → J = M)35proof36haveself:= Subset.reflexive at I37have seedMember : I ∈ extensions(F, I) := extensions.membership.backward with⟨seed, self⟩38have condition : ZornCondition(extensions(F, I), inclusionOrder(extensions(F, I)))39constructor40 . exact inclusionOrder.partial at extensions(F, I)41 . intros S chain42have subset := Chain.subset with chain43have nested := inclusionChain.nested with chain44have alternatives : Nonempty(S) ∨¬Nonempty(S) bytauto45cases alternatives with inhabited | absent46 . have subsetF : S ⊆ F47intros J hJ48have member := subset with J, hJ49obtain⟨inFamily, extendsJ⟩:= extensions.membership.forward with member50exact inFamily51have unionF := closed with S, inhabited, subsetF, nested52have extends : I ⊆⋃(S)53obtain⟨seedChain, hJ⟩:= inhabited54have member := subset with seedChain, hJ55obtain⟨inFamily, contains⟩:= extensions.membership.forward with member56intros x hx57have inJ := contains with x, hx58exactset.union.backward with⟨seedChain, hJ, inJ⟩59have unionMember : ⋃(S) ∈ extensions(F, I) := extensions.membership.backward with⟨unionF, extends⟩60witness⋃(S)61refine⟨unionMember, subset, ?_⟩62intros J hJ63have member := subset with J, hJ64apply inclusionOrder.relates.backward with member, unionMember65intros x hx66exactset.union.backward with⟨J, hJ, hx⟩67 . witness I68refine⟨seedMember, subset, ?_⟩69intros J hJ70exfalso71exact absent with⟨J, hJ⟩72obtain⟨M, memberM, greatest⟩:= Zorn.theorem with condition73obtain⟨inF, extends⟩:= extensions.membership.forward with memberM74refine⟨M, inF, extends, ?_⟩75intros J hJ contains76have extendsJ := Subset.transitive with extends, contains77have memberJ : J ∈ extensions(F, I) := extensions.membership.backward with⟨hJ, extendsJ⟩78have edge := inclusionOrder.relates.backward with memberM, memberJ, contains79exact greatest with J, memberJ, edge8081theorem Chain.extends_maximal (A r I : Set)82requires initial : Chain(A, r, I)83shows∃ M, Chain(A, r, M) ∧ I ⊆ M ∧84 (∀ J, Chain(A, r, J) → M ⊆ J → J = M)85proof86have seed := chains.contains with initial87have closed : ∀ S, Nonempty(S) → S ⊆ chains(A, r) →88 (∀ C ∈ S, ∀ D ∈ S, C ⊆ D ∨ D ⊆ C) →⋃(S) ∈ chains(A, r)89intros S inhabited subset nested90have family : ChainOfChains(A, r, S)91constructor92 . intros C hC93have member := subset with C, hC94obtain⟨boundedC, chainC⟩:= chains.membership.forward with member95exact chainC96 . exact nested97have unionChain := Chain.union with family98exact chains.contains with unionChain99obtain⟨M, member, extends, maximal⟩:= maximal_extension with seed, closed100obtain⟨boundedM, chainM⟩:= chains.membership.forward with member101witness M102constructor103 . exact chainM104 . constructor105 . exact extends106 . intros J chainJ contains107have memberJ := chains.contains with chainJ108exact maximal with J, memberJ, contains109110theorem Zorn.above (A r a : Set)111requires condition : ZornCondition(A, r)112requires start : a ∈ A113shows∃ m, Maximal(A, r, m) ∧ relates(r, a, m)114proof115have order := ZornCondition.order with condition116have singletonChain := Chain.singleton with order, start117obtain⟨C, chain, extends, maximalChain⟩:= Chain.extends_maximal with singletonChain118rcases condition with⟨partial, bounds⟩119obtain⟨m, bound⟩:= bounds with C, chain120have extended := Chain.adjoin_upper with order, chain, bound121have subset := union2.subset_left at C, {m}122have equal := maximalChain with C ∪ {m}, extended, subset123have mIn : m ∈ C124haveself:= singleton.self at m125have member := union2.right at m, C, {m} withself126 rwa equal at member127rcases bound with⟨mInA, subsetC, upper⟩128have aIn : a ∈ C byexact extends with a, singleton.self at a129have above := upper with a, aIn130have maximal : Maximal(A, r, m)131constructor132 . exact mInA133 . intros x hx mx134have transitive := IsPartialOrder.transitive with order135have boundX : UpperBound(A, r, C, x)136constructor137 . exact hx138 . constructor139 . exact subsetC140 . intros y hy141have ym := upper with y, hy142have yInA : y ∈ A byexact subsetC with y, hy143exact transitive with y, yInA, m, mInA, x, hx, ym, mx144have chainX := Chain.adjoin_upper with order, chain, boundX145have subsetX := union2.subset_left at C, {x}146have equalX := maximalChain with C ∪ {x}, chainX, subsetX147have xIn : x ∈ C148haveself:= singleton.self at x149have member := union2.right at x, C, {x} withself150 rwa equalX at member151have xm := upper with x, xIn152have antisymmetric := IsPartialOrder.antisymmetric with order153exact antisymmetric with x, hx, m, mInA, xm, mx154refine⟨m, maximal, above⟩155