1-- Exchanging two elements of a set, 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 Passmore67module Grace.Cumulus.Set.Transposition8import Involution910defunique exchangedValue (I a b x : Set) (y ∈ I) :=11 (x = a ∧ y = b) ∨ (x = b ∧ y = a) ∨ (x ≠ a ∧ x ≠ b ∧ y = x)12requires ha : a ∈ I13requires hb : b ∈ I14requires member : x ∈ I15proof16have alternatives : x = a ∨ x = b ∨ (x ≠ a ∧ x ≠ b)17tauto18cases alternatives with atFirst | remaining19 . refine⟨b, hb, ?_⟩20settle21 . cases remaining with atSecond | outside22 . refine⟨a, ha, ?_⟩23settle24 . refine⟨x, member, ?_⟩25settle26uniqueness27intros u hu firstValue v hv secondValue28cases firstValue with atFirst | remaining29 . cases secondValue with atFirstAgain | other30 . cc31 . cases other with atSecond | outside32 . cc33 . cc34 . cases remaining with atSecond | outside35 . cases secondValue with atFirst | other36 . cc37 . cases other with atSecondAgain | outsideAgain38 . cc39 . cc40 . cases secondValue with atFirst | other41 . cc42 . cases other with atSecond | outsideAgain43 . cc44 . cc4546defgraph transposition (I a b : Set) (x ∈ I) : I := exchangedValue(I, a, b, x)47requires ha : a ∈ I48requires hb : b ∈ I49proof50have selected := exchangedValue.selected at I, a, b, x with*51exact selected.left5253section Exchanges54variable (I a b : Set)55hypothesis ha : a ∈ I56hypothesis hb : b ∈ I5758theorem transposition.first59shows transposition(I, a, b)(a) = b60proof61have selected := exchangedValue.selected at I, a, b, a with*62have value := transposition.value at I, a, b, a with*63have alternatives := selected.right64cases alternatives with atFirst | remaining65 . cc66 . cases remaining with atSecond | outside67 . cc68 . cc6970theorem transposition.second71shows transposition(I, a, b)(b) = a72proof73have selected := exchangedValue.selected at I, a, b, b with*74have value := transposition.value at I, a, b, b with*75have alternatives := selected.right76cases alternatives with atFirst | remaining77 . cc78 . cases remaining with atSecond | outside79 . cc80 . cc8182theorem transposition.other (x : Set)83requires member : x ∈ I84requires notFirst : x ≠ a85requires notSecond : x ≠ b86shows transposition(I, a, b)(x) = x87proof88have selected := exchangedValue.selected at I, a, b, x with*89have value := transposition.value at I, a, b, x with*90have alternatives := selected.right91cases alternatives with atFirst | remaining92 . cc93 . cases remaining with atSecond | outside94 . cc95 . cc9697theorem transposition.involution98shows InvolutionOn(transposition(I, a, b), I)99proof100have function := transposition.function at I, a, b with*101have atFirst := transposition.first at I, a, b with*102have atSecond := transposition.second at I, a, b with*103unfold InvolutionOn104refine⟨function, ?_⟩105intros x member106have alternatives : x = a ∨ x = b ∨ (x ≠ a ∧ x ≠ b) bytauto107cases alternatives with same | remaining108 . cc109 . cases remaining with same | outside110 . cc111 . have unchanged := transposition.other with ha, hb, member, outside.left, outside.right112cc113114theorem transposition.bijective115shows BijectiveBetween(transposition(I, a, b), I, I)116proof117simp [transposition.involution, InvolutionOn.bijective]118119theorem transposition.symmetric120shows transposition(I, a, b) = transposition(I, b, a)121proof122have firstFunction := transposition.function at I, a, b with*123have secondFunction := transposition.function at I, b, a with*124have firstA := transposition.first at I, a, b with*125have firstB := transposition.second at I, a, b with*126have secondA := transposition.second at I, b, a with*127have secondB := transposition.first at I, b, a with*128apply FunctionBetween.ext with firstFunction, secondFunction129intros i member130have alternatives : i = a ∨ i = b ∨ (i ≠ a ∧ i ≠ b) bytauto131cases alternatives with atA | rest132 . cc133 . cases rest with atB | outside134 . cc135 . have first := transposition.other with ha, hb, member, outside.left, outside.right136have second := transposition.other with hb, ha, member, outside.right, outside.left137cc138139end Exchanges140-- Conjugating (a b) by (b c) gives (a c).141theorem transposition.conjugate (I a b c i : Set)142requires ha : a ∈ I143requires hb : b ∈ I144requires hc : c ∈ I145requires hi : i ∈ I146requires ab : a ≠ b147requires ac : a ≠ c148requires bc : b ≠ c149shows transposition(I, b, c)(transposition(I, a, b)(transposition(I, b, c)(i))) = transposition(I, a, c)(i)150proof151have ba : b ≠ a bysimp152have ca : c ≠ a bysimp153have cb : c ≠ b bysimp154have bcFirst := transposition.first at I, b, c with*155have bcSecond := transposition.second at I, b, c with*156have bcOther := transposition.other at I, b, c, a with*157have abFirst := transposition.first at I, a, b with*158have abSecond := transposition.second at I, a, b with*159have abOther := transposition.other at I, a, b, c with*160have acFirst := transposition.first at I, a, c with*161have acSecond := transposition.second at I, a, c with*162have acOther := transposition.other at I, a, c, b with*163have alternatives : i = a ∨ i = b ∨ i = c ∨ (i ≠ a ∧ i ≠ b ∧ i ≠ c) bytauto164cases alternatives with first | rest165 . cc166 . cases rest with second | restAgain167 . cc168 . cases restAgain with third | outside169 . cc170 . have outsideBC := transposition.other with hb, hc, hi, outside.right.left, outside.right.right171have outsideAB := transposition.other with ha, hb, hi, outside.left, outside.right.left172have outsideAC := transposition.other with ha, hc, hi, outside.left, outside.right.right173cc174