1-- Well-orders: least elements, initial segments below a point, restriction2-- to a subset, and the rigidity of order embeddings3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Set.WellOrder9import Order1011-- The initial segment of A below a.12def segment (A r a : Set) : Set:= { x ∈ A | relates(r, x, a) }1314-- The order r restricted to the subset B.15def restrictOrder (r B : Set) : Set:= r ∩ (B × B)1617theorem segment.subset (A r a : Set)18shows segment(A, r, a) ⊆ A19proof20intros x hx21simp [segment.membership] at hx22exact hx.left2324theorem restrictOrder.relates (r B x y : Set)25shows relates(restrictOrder(r, B), x, y) ↔ relates(r, x, y) ∧ x ∈ B ∧ y ∈ B26proof27constructor28 . intro h29have member : (x, y) ∈ restrictOrder(r, B) := h30simp [restrictOrder, intersection.membership] at member31have coordinates := product.pair_membership.forward with member.right32constructor33 . exact member.left34 . exact coordinates35 . intro h36have pairIn : (x, y) ∈ B × B := product.pair with h.right.left, h.right.right37have inR : (x, y) ∈ r := h.left38have member : (x, y) ∈ restrictOrder(r, B) bysimp [restrictOrder, intersection.membership, inR, pairIn]39exact member4041-- Least elements4243theorem WellOrder.least (A r S : Set)44requires order : WellOrder(A, r)45requires hS : S ⊆ A46requires nonempty : S ≠∅47shows∃ m ∈ S, ∀ x ∈ S, x ≠ m → relates(r, m, x)48proof49have founded := WellOrder.well_founded with order50have bounded : subset(S, A)51intros x hx52exact hS with x, hx53obtain⟨m, hm, minimal⟩:= founded with S, ⟨bounded, nonempty⟩54have connected := StrictLinearOrder.connected with (WellOrder.strict_linear with order)55witness m56constructor57 . exact hm58 . intros x hx different59have differentBack : m ≠ x60intro same61apply different62rw same63rfl64havecases:= connected with m, (hS with m, hm), x, (hS with x, hx), differentBack65casescaseswith below | above66 . exact below67 . exfalso68exact minimal with x, hx, above6970theorem WellOrder.least_unique (A r S m n : Set)71requires order : WellOrder(A, r)72requires hS : S ⊆ A73requires hm : m ∈ S74requires hn : n ∈ S75requires leastM : ∀ x ∈ S, x ≠ m → relates(r, m, x)76requires leastN : ∀ x ∈ S, x ≠ n → relates(r, n, x)77shows m = n78proof79by_contra different80have differentBack : n ≠ m81intro same82apply different83rw same84rfl85have below := leastM with n, hn, differentBack86have above := leastN with m, hm, different87have irreflexive := StrictLinearOrder.irreflexive with (WellOrder.strict_linear with order)88have transitive := StrictLinearOrder.transitive with (WellOrder.strict_linear with order)89have loop := transitive with m, (hS with m, hm), n, (hS with n, hn), m, (hS with m, hm), below, above90exact irreflexive with m, (hS with m, hm), loop9192-- Initial segments below a point9394theorem segment.initial (A r a : Set)95requires order : StrictOrder(A, r)96requires ha : a ∈ A97shows InitialSegment(A, r, segment(A, r, a))98proof99have transitive := StrictOrder.transitive with order100constructor101 . exact segment.subset at A, r, a102 . intros x hx y hy below103simp [segment.membership] at hx104simp [segment.membership]105constructor106 . exact hy107 . exact transitive with y, hy, x, hx.left, a, ha, below, hx.right108109theorem segment.absent (A r a : Set)110requires order : StrictOrder(A, r)111requires ha : a ∈ A112shows¬(a ∈ segment(A, r, a))113proof114intro h115simp [segment.membership] at h116have irreflexive := StrictOrder.irreflexive with order117exact irreflexive with a, ha, h.right118119theorem segment.proper (A r a : Set)120requires order : StrictOrder(A, r)121requires ha : a ∈ A122shows ProperInitialSegment(A, r, segment(A, r, a))123proof124constructor125 . exact segment.initial with order, ha126 . intro equal127have absent := segment.absent with order, ha128rw equal at absent129exact absent with ha130131theorem segment.monotone (A r a b : Set)132requires order : StrictOrder(A, r)133requires ha : a ∈ A134requires hb : b ∈ A135requires below : relates(r, a, b)136shows segment(A, r, a) ⊆ segment(A, r, b)137proof138have transitive := StrictOrder.transitive with order139intros x hx140simp [segment.membership] at hx141simp [segment.membership]142constructor143 . exact hx.left144 . exact transitive with x, hx.left, a, ha, b, hb, hx.right, below145146theorem segment.injective (A r a b : Set)147requires order : StrictLinearOrder(A, r)148requires ha : a ∈ A149requires hb : b ∈ A150requires equal : segment(A, r, a) = segment(A, r, b)151shows a = b152proof153by_contra different154have connected := StrictLinearOrder.connected with order155have strict := StrictLinearOrder.strict with order156havecases:= connected with a, ha, b, hb, different157casescaseswith below | above158 . have member : a ∈ segment(A, r, b) bysimp [segment.membership, ha, below]159rw← equal at member160exact segment.absent with strict, ha, member161 . have member : b ∈ segment(A, r, a) bysimp [segment.membership, hb, above]162rw equal at member163exact segment.absent with strict, hb, member164165theorem InitialSegment.cases (A r I : Set)166requires order : WellOrder(A, r)167requires initial : InitialSegment(A, r, I)168shows I = A ∨∃ a ∈ A, I = segment(A, r, a)169proof170have included := InitialSegment.subset with initial171have strict := StrictLinearOrder.strict with (WellOrder.strict_linear with order)172have connected := StrictLinearOrder.connected with (WellOrder.strict_linear with order)173have irreflexive := StrictOrder.irreflexive with strict174havesplit : I = A ∨ I ≠ A bytauto175casessplitwith whole | proper176 . left177exact whole178 . right179have outside : A ∖ I ≠∅180intro empty181apply proper182apply Subset.antisymmetric183 . exact included184 . exact difference.eq_empty_iff.forward with empty185obtain⟨a, haOut, leastA⟩:= WellOrder.least with order, (difference.subset_left at A, I), outside186simp [difference.membership] at haOut187witness a188constructor189 . exact haOut.left190 . ext x191constructor192 . intro hx193have hxA : x ∈ A := included with x, hx194simp [segment.membership]195constructor196 . exact hxA197 . have different : x ≠ a198intro same199rw same at hx200exact haOut.right with hx201havecases:= connected with x, hxA, a, haOut.left, different202casescaseswith below | above203 . exact below204 . exfalso205exact haOut.right with (initial.right with x, hx, a, haOut.left, above)206 . intro hx207simp [segment.membership] at hx208by_contra absent209have inOutside : x ∈ A ∖ I bysimp [difference.membership, hx.left, absent]210have different : x ≠ a211intro same212rw same at hx213exact irreflexive with a, haOut.left, hx.right214have above := leastA with x, inOutside, different215have transitive := StrictOrder.transitive with strict216have loop := transitive with x, hx.left, a, haOut.left, x, hx.left, hx.right, above217exact irreflexive with x, hx.left, loop218219-- Restriction to a subset220221theorem restrictOrder.relation (r B : Set)222shows RelationBetween(restrictOrder(r, B), B, B)223proof224intros p hp225simp [restrictOrder, intersection.membership] at hp226exact hp.right227228theorem restrictOrder.well_order (A r B : Set)229requires order : WellOrder(A, r)230requires hB : B ⊆ A231shows WellOrder(B, restrictOrder(r, B))232proof233have strict := StrictLinearOrder.strict with (WellOrder.strict_linear with order)234have connected := StrictLinearOrder.connected with (WellOrder.strict_linear with order)235have irreflexive := StrictOrder.irreflexive with strict236have transitive := StrictOrder.transitive with strict237constructor238 . constructor239 . constructor240 . exact restrictOrder.relation at r, B241 . constructor242 . intros x hx loop243have related := restrictOrder.relates.forward with loop244exact irreflexive with x, (hB with x, hx), related.left245 . intros x hx y hy z hz first second246have firstR := restrictOrder.relates.forward with first247have secondR := restrictOrder.relates.forward with second248apply restrictOrder.relates.backward249constructor250 . exact transitive with x, (hB with x, hx), y, (hB with y, hy), z, (hB with z, hz), firstR.left, secondR.left251 . exact⟨hx, hz⟩252 . intros x hx y hy different253havecases:= connected with x, (hB with x, hx), y, (hB with y, hy), different254casescaseswith below | above255 . left256apply restrictOrder.relates.backward257exact⟨below, hx, hy⟩258 . right259apply restrictOrder.relates.backward260exact⟨above, hy, hx⟩261 . intros S hS262have included : S ⊆ A263intros x hx264exact hB with x, (hS.left with x, hx)265obtain⟨m, hm, leastM⟩:= WellOrder.least with order, included, hS.right266witness m267constructor268 . exact hm269 . intros y hy below270have related := restrictOrder.relates.forward with below271have different : y ≠ m272intro same273rw same at related274exact irreflexive with m, (included with m, hm), related.left275have above := leastM with y, hy, different276have loop := transitive with m, (included with m, hm), y, (included with y, hy), m, (included with m, hm), above, related.left277exact irreflexive with m, (included with m, hm), loop278279-- Rigidity of embeddings280281-- The points an embedding moves down.282def descents (f A r : Set) : Set:= { x ∈ A | relates(r, f(x), x) }283284theorem OrderEmbedding.not_below (f A r : Set)285requires order : WellOrder(A, r)286requires embedding : OrderEmbedding(f, A, r, A, r)287shows∀ x ∈ A, ¬(relates(r, f(x), x))288proof289have function := Injective.function with embedding.left290have strict := StrictLinearOrder.strict with (WellOrder.strict_linear with order)291have irreflexive := StrictOrder.irreflexive with strict292by_contra some293have descending : descents(f, A, r) ≠∅294intro empty295apply some296intros x hx below297have member : x ∈ descents(f, A, r) bysimp [descents.membership, hx, below]298rw empty at member299exactset.empty with member300have descendingSubset : descents(f, A, r) ⊆ A301intros x hx302simp [descents.membership] at hx303exact hx.left304obtain⟨m, hm, leastM⟩:= WellOrder.least with order, descendingSubset, descending305simp [descents.membership] at hm306have valueA := FunctionBetween.apply_closed with function, hm.left307have related := FunctionBetween.relates_apply with function, hm.left308have relatedValue := FunctionBetween.relates_apply with function, valueA309have preserved := embedding.right with f(m), valueA, m, hm.left, f(f(m)), (FunctionBetween.apply_closed with function, valueA), f(m), valueA, relatedValue, related310have lower := preserved.forward with hm.right311have inDescending : f(m) ∈ descents(f, A, r) bysimp [descents.membership, valueA, lower]312have different : f(m) ≠ m313intro same314rw same at hm315exact irreflexive with m, hm.left, hm.right316have above := leastM with f(m), inDescending, different317have transitive := StrictOrder.transitive with strict318have loop := transitive with m, hm.left, f(m), valueA, m, hm.left, above, hm.right319exact irreflexive with m, hm.left, loop320