1-- Transport of well-orders along injections, without Choice.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 Passmore67-- Copyright (c) 2026 Grant Passmore. Apache License 2.0.8module Grace.Cumulus.Set.OrderTransport9import Order1011def transportedOrder (f A B r : Set) : Set:=12 {p ∈ B × B |∃ x ∈ A, ∃ y ∈ A, relates(r, x, y) ∧ p = (f[x], f[y])}1314theorem transportedOrder.decode (f A B r u v : Set)15requires related : relates(transportedOrder(f, A, B, r), u, v)16shows∃ x ∈ A, ∃ y ∈ A, relates(r, x, y) ∧ u = f[x] ∧ v = f[y]17proof18unfold relates at related19obtain⟨bounded, x, hx, y, hy, edge, shape⟩:= transportedOrder.membership.forward with related20obtain⟨left, right⟩:= orderedPair.injective with shape21refine⟨x, hx, y, hy, edge, left, right⟩2223theorem transportedOrder.edge (f A B r x y : Set)24requires function : f : A → B25requires hx : x ∈ A26requires hy : y ∈ A27requires related : relates(r, x, y)28shows relates(transportedOrder(f, A, B, r), f[x], f[y])29proof30haveleft:= FunctionBetween.apply_closed at f, A, B, x with*31haveright:= FunctionBetween.apply_closed at f, A, B, y with*32have pair := product.pair withleft, right33unfold relates34exact transportedOrder.membership.backward with⟨pair, x, hx, y, hy, related, rfl⟩3536theorem transportedOrder.reflect (f A B r x y : Set)37requires injective : InjectiveBetween(f, A, B)38requires hx : x ∈ A39requires hy : y ∈ A40requires related : relates(transportedOrder(f, A, B, r), f[x], f[y])41shows relates(r, x, y)42proof43obtain⟨a, ha, b, hb, edge, left, right⟩:= transportedOrder.decode with related44have sameA := Injective.apply_cancel with injective, hx, ha, left45have sameB := Injective.apply_cancel with injective, hy, hb, right46rw sameA47 rwa sameB4849theorem transportedOrder.relation (f A B r : Set)50requires function : f : A → B51shows RelationBetween(transportedOrder(f, A, B, r), functionImage(f, A, B), functionImage(f, A, B))52proof53intros p member54obtain⟨bounded, x, hx, y, hy, edge, shape⟩:= transportedOrder.membership.forward with member55haveleft:= functionImage.value with hx56haveright:= functionImage.value with hy57rw shape58exact product.pair withleft, right5960theorem transportedOrder.strict_linear (f A B r : Set)61requires injective : InjectiveBetween(f, A, B)62requires order : StrictLinearOrder(A, r)63shows StrictLinearOrder(functionImage(f, A, B), transportedOrder(f, A, B, r))64proof65have function := Injective.function with injective66have irreflexive := StrictLinearOrder.irreflexive with order67have transitive := StrictLinearOrder.transitive with order68have connected := StrictLinearOrder.connected with order69constructor70constructor71 . exact transportedOrder.relation at f, A, B, r with function72 . constructor73intros u hu self74obtain⟨bound, x, hx, value⟩:= functionImage.membership.forward with hu75rw value atself76have original := transportedOrder.reflect at f, A, B, r, x, x with*77exact irreflexive with x, hx, original78intros u hu v hv w hw uv vw79obtain⟨bu, a, ha, uValue⟩:= functionImage.membership.forward with hu80obtain⟨bv, b, hb, vValue⟩:= functionImage.membership.forward with hv81obtain⟨bw, c, hc, wValue⟩:= functionImage.membership.forward with hw82rw [uValue, vValue] at uv83rw [vValue, wValue] at vw84have ab := transportedOrder.reflect at f, A, B, r, a, b with*85have bc := transportedOrder.reflect at f, A, B, r, b, c with*86have ac := transitive with a, ha, b, hb, c, hc, ab, bc87rw [uValue, wValue]88exact transportedOrder.edge at f, A, B, r, a, c with*89intros u hu v hv distinct90obtain⟨bu2, a2, ha2, uValue2⟩:= functionImage.membership.forward with hu91obtain⟨bv2, b2, hb2, vValue2⟩:= functionImage.membership.forward with hv92have different : a2 ≠ b2 bysimp93have alternatives := connected with a2, ha2, b2, hb2, different94rw [uValue2, vValue2]95cases alternatives with ab2 | ba296 . left97exact transportedOrder.edge at f, A, B, r, a2, b2 with*98 . right99exact transportedOrder.edge at f, A, B, r, b2, a2 with*100101theorem transportedOrder.well_founded (f A B r : Set)102requires injective : InjectiveBetween(f, A, B)103requires founded : wellFoundedOn(r, A)104shows wellFoundedOn(transportedOrder(f, A, B, r), functionImage(f, A, B))105proof106intros S conditions107rcases conditions with⟨subsetS, nonemptyS⟩108obtain⟨u, hu⟩:= Nonempty.of_ne_empty with nonemptyS109have inImage := subsetS with u, hu110obtain⟨bounded, x, hx, value⟩:= functionImage.membership.forward with inImage111rw value at hu112have preimageNonempty : Nonempty(functionPreimage(f, A, S)) :=⟨x, (functionPreimage.membership.backward with⟨hx, hu⟩)⟩113have preimageNe := Nonempty.ne_empty with preimageNonempty114have preimageSubset : subset(functionPreimage(f, A, S), A) bysimp [functionPreimage, subset]115obtain⟨m, hm, minimal⟩:= founded with functionPreimage(f, A, S), ⟨preimageSubset, preimageNe⟩116obtain⟨mInA, imageInS⟩:= functionPreimage.membership.forward with hm117refine⟨f[m], imageInS, ?_⟩118intros v hv edge119have vInImage := subsetS with v, hv120obtain⟨vBound, y, hy, vValue⟩:= functionImage.membership.forward with vInImage121rw vValue at hv122rw vValue at edge123have yPreimage : y ∈ functionPreimage(f, A, S) := functionPreimage.membership.backward with⟨hy, hv⟩124have original := transportedOrder.reflect with*125exact minimal with y, yPreimage, original126127theorem transportedOrder.well_order (f A B r : Set)128requires injective : InjectiveBetween(f, A, B)129requires order : WellOrder(A, r)130shows WellOrder(functionImage(f, A, B), transportedOrder(f, A, B, r))131proof132have strict := WellOrder.strict_linear with order133have founded := WellOrder.well_founded with order134constructor135 . exact transportedOrder.strict_linear with*136 . exact transportedOrder.well_founded with*137