Martin Escardo, 1-7 November 2023.

We prove the main results of [1] and [2] about automorphisms of Ξ©.

[1] Peter T. Johnstone. Automorphisms of Ξ©. Algebra Universalis,
    9 (1979) 1-7.
    https://doi.org/10.1007/BF02488012

[2] Peter Freyd. Choice and well-ordering.  Annals of Pure and Applied
    Logic 35 (1987) 149-166.
    https://doi.org/10.1016/0168-0072(87)90060-1
    https://core.ac.uk/download/pdf/81927529.pdf

\begin{code}

{-# OPTIONS --safe --without-K #-}

open import Groups.Type
open import MLTT.Spartan
open import UF.Embeddings
open import UF.Equiv hiding (_β‰…_)
open import UF.Equiv-FunExt
open import UF.FunExt
open import UF.Logic
open import UF.Sets
open import UF.Sets-Properties
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import UF.SubtypeClassifier hiding (Ξ©)
open import UF.SubtypeClassifier-Properties

module Higgs.AutomorphismsOfOmega
        {𝓀 : Universe}
        (fe : Fun-Ext)
        (pe : propext 𝓀)
       where

private
 𝓀⁺ = 𝓀 ⁺

open import Higgs.InvolutionTheorem fe pe
open import Higgs.Rigidity fe pe

open Conjunction
open Implication fe
open Universal fe

can-recover-automorphism-from-its-value-at-⊀
 : (𝕗 : Aut Ξ©)
   (p : Ξ©)
 β†’ ⌜ 𝕗 ⌝ p = (p ⇔ ⌜ 𝕗 ⌝ ⊀)
can-recover-automorphism-from-its-value-at-⊀ 𝕗@(f , _) p =
 Ξ©-ext' pe fe
  ((f p = ⊀)       β‰ƒβŸ¨ Ξ©-automorphism-swap-≃ 𝕗 ⟩
   (f ⊀ = p)       β‰ƒβŸ¨ ≃-sym (⇔-equiv-to-= pe (f ⊀) p) ⟩
   ((f ⊀ ⇔ p) = ⊀) β‰ƒβŸ¨ transport-≃ (_= ⊀) (⇔-sym pe (f ⊀) p) ⟩
   ((p ⇔ f ⊀) = ⊀) β– )

\end{code}

The Higgs object ℍ as defined by Johnstone in [1].

\begin{code}

is-widespread : Ξ© β†’ 𝓀⁺ Μ‡
is-widespread r = (p : Ξ©) β†’ ((p ⇔ r) ⇔ r) = p

being-widespread-is-prop : (r : Ξ©) β†’ is-prop (is-widespread r)
being-widespread-is-prop r = Ξ -is-prop fe (Ξ» p β†’ Ξ©-is-set fe pe)

ℍ : 𝓀⁺ Μ‡
ℍ = Ξ£ r κž‰ Ξ© , is-widespread r

βŸͺ_⟫ : ℍ β†’ Ξ©
βŸͺ r , _ ⟫ = r

βŸͺ_⟫-is-widespread : (x : ℍ) β†’ is-widespread βŸͺ x ⟫
βŸͺ _ , i ⟫-is-widespread = i

ℍ-is-set : is-set ℍ
ℍ-is-set = subsets-of-sets-are-sets
            Ξ©
            is-widespread
            (Ξ©-is-set fe pe)
            (Ξ» {r} β†’ being-widespread-is-prop r)

to-ℍ-= : (r s : Ξ©) {i : is-widespread r} {j : is-widespread s}
       β†’ r = s
       β†’ (r , i) =[ ℍ ] (s , j)
to-ℍ-= r s {i} {j} = to-subtype-= being-widespread-is-prop

to-ℍ-=' : (x@(r , i) y@(s , j) : ℍ)
         β†’ (r holds ↔ s holds)
         β†’ x = y
to-ℍ-=' (r , i) (s , j) (f , g) = to-ℍ-= r s (Ξ©-extensionality pe fe f g)

\end{code}

The equality of the Higgs object has values in 𝓀⁺, but is equivalent
to an equality with values in 𝓀 and hence in Ξ©.

\begin{code}

_=ₕ_ : ℍ β†’ ℍ β†’ Ξ©
(p , p-is-ws) =ₕ (q , q-is-ws) = p ⇔ q

infix 4 _=ₕ_

=ₕ-agrees-with-= : (x y : ℍ) β†’ ((x =ₕ y) holds) ≃ (x = y)
=ₕ-agrees-with-= x@(p , p-is-ws) y@(q , q-is-ws)
 = logically-equivalent-props-are-equivalent
    (holds-is-prop (x =ₕ y))
    ℍ-is-set
    (to-ℍ-=' x y)
    (Ξ» (e : x = y) β†’ idtofun _ _ (ap (_holds ∘ βŸͺ_⟫) e) ,
                      idtofun _ _ (ap (_holds ∘ βŸͺ_⟫) (e ⁻¹)))

Ξ©-automorphisms-are-⇔-embeddings : (𝕗 : Aut Ξ©)
                                   (p q : Ξ©)
                                 β†’ (p ⇔ q) = (⌜ 𝕗 ⌝ p ⇔ ⌜ 𝕗 ⌝ q)
Ξ©-automorphisms-are-⇔-embeddings 𝕗@(f , f-is-equiv) p q =
 Ξ©-ext' pe fe
  (((p ⇔ q) = ⊀)     β‰ƒβŸ¨ I ⟩
   (p = q)           β‰ƒβŸ¨ II ⟩
   (f p = f q)       β‰ƒβŸ¨ III ⟩
   ((f p ⇔ f q) = ⊀) β– )
  where
   I   = ⇔-equiv-to-= pe p q
   II  = embedding-criterion-converse' f (equivs-are-embeddings' 𝕗) p q
   III = ≃-sym (⇔-equiv-to-= pe (f p) (f q))

eval-at-⊀-is-widespread : (𝕗 : Aut Ξ©) β†’ is-widespread (eval-at-⊀ 𝕗)
eval-at-⊀-is-widespread 𝕗@(f , _) p = II
 where
  I = p ⇔ ⊀           =⟨ Iβ‚€ ⟩
      f p ⇔ f ⊀       =⟨ I₁ ⟩
      (p ⇔ f ⊀) ⇔ f ⊀ ∎
   where
    Iβ‚€ = Ξ©-automorphisms-are-⇔-embeddings 𝕗 p ⊀
    I₁ = ap (_⇔ f ⊀) (can-recover-automorphism-from-its-value-at-⊀ 𝕗 p)

  II : ((p ⇔ f ⊀) ⇔ f ⊀) = p
  II = transport (_= p) I (⊀-⇔-neutral pe p)

Aut-Ξ©-to-ℍ : Aut Ξ© β†’ ℍ
Aut-Ξ©-to-ℍ 𝕗 = eval-at-⊀ 𝕗 , eval-at-⊀-is-widespread 𝕗

ℍ-to-Aut-Ξ© : ℍ β†’ Aut Ξ©
ℍ-to-Aut-Ξ© (r , i) = (Ξ» p β†’ p ⇔ r) , involutions-are-equivs _ i

Ξ·-ℍ : ℍ-to-Aut-Ξ© ∘ Aut-Ξ©-to-ℍ ∼ id
Ξ·-ℍ 𝕗@(f , f-is-equiv) = to-≃-= fe h
 where
  h : (Ξ» p β†’ p ⇔ f ⊀) ∼ f
  h p = (can-recover-automorphism-from-its-value-at-⊀ 𝕗 p)⁻¹

Ξ΅-ℍ : Aut-Ξ©-to-ℍ ∘ ℍ-to-Aut-Ξ© ∼ id
Ξ΅-ℍ (r , i) = to-ℍ-= (⊀ ⇔ r) r (⊀-⇔-neutral' pe r)

ℍ-to-Aut-Ξ©-is-equiv : is-equiv ℍ-to-Aut-Ξ©
ℍ-to-Aut-Ξ©-is-equiv = qinvs-are-equivs ℍ-to-Aut-Ξ© (Aut-Ξ©-to-ℍ , Ξ΅-ℍ , Ξ·-ℍ)

Aut-Ξ©-to-ℍ-is-equiv : is-equiv Aut-Ξ©-to-ℍ
Aut-Ξ©-to-ℍ-is-equiv = inverses-are-equivs ℍ-to-Aut-Ξ© ℍ-to-Aut-Ξ©-is-equiv

opaque
 ℍ-to-Aut-Ξ©-equivalence : ℍ ≃ Aut Ξ©
 ℍ-to-Aut-Ξ©-equivalence = ℍ-to-Aut-Ξ© , ℍ-to-Aut-Ξ©-is-equiv

\end{code}

The type Aut Ξ© is a group under composition, the symmetric group on Ξ©,
where the neutral element is the identity automorphism and the inverse
of any element is itself.  That is, Aut Ξ© is a boolean group, or a
group of order 2. We now show that the group structure on ℍ induced by
the above equivalence is given by logical equivalence _⇔_ with neutral
element ⊀.

\begin{code}

open import Groups.Type
open import Groups.Symmetric fe

Ξ©β‚› : Group 𝓀⁺
Ξ©β‚› = symmetric-group Ξ© (Ξ©-is-set fe pe)

opaque
 𝓗-construction : Ξ£ s κž‰ Group-structure ℍ , is-hom (ℍ , s) Ξ©β‚› ℍ-to-Aut-Ξ©
 𝓗-construction = transport-Group-structure Ξ©β‚› ℍ ℍ-to-Aut-Ξ© ℍ-to-Aut-Ξ©-is-equiv

𝓗 : Group 𝓀⁺
𝓗 = ℍ , pr₁ 𝓗-construction

𝓗-to-Ξ©β‚›-isomorphism : 𝓗 β‰… Ξ©β‚›
𝓗-to-Ξ©β‚›-isomorphism = ℍ-to-Aut-Ξ© , ℍ-to-Aut-Ξ©-is-equiv , prβ‚‚ 𝓗-construction

π“š-isomorphism-explicitly : (x : ℍ) (p : Ξ©)
                         β†’ ⌜ ℍ-to-Aut-Ξ© x ⌝ p = (p ⇔ βŸͺ x ⟫)
π“š-isomorphism-explicitly x p = refl

\end{code}

The unit of 𝓗 is ⊀ and its multiplication is logical equivalence.

\begin{code}

opaque
 unfolding 𝓗-construction

 𝓗-unit : βŸͺ unit 𝓗 ⟫ = ⊀
 𝓗-unit = refl

 𝓗-multiplication : (x y : ℍ) β†’ βŸͺ x ·⟨ 𝓗 ⟩ y ⟫ = (βŸͺ x ⟫ ⇔ βŸͺ y ⟫)
 𝓗-multiplication x y =
  βŸͺ x ·⟨ 𝓗 ⟩ y ⟫     =⟨refl⟩
  (⊀ ⇔ βŸͺ x ⟫) ⇔ βŸͺ y ⟫ =⟨ ap (_⇔ βŸͺ y ⟫) (⊀-⇔-neutral' pe βŸͺ x ⟫) ⟩
  βŸͺ x ⟫ ⇔ βŸͺ y ⟫       ∎

 corollary-⊀ : is-widespread ⊀
 corollary-⊀ = βŸͺ unit 𝓗 ⟫-is-widespread

π•₯ : ℍ
π•₯ = ⊀ , corollary-⊀

corollary-⇔ : (r s : Ξ©)
            β†’ is-widespread r
            β†’ is-widespread s
            β†’ is-widespread (r ⇔ s)
corollary-⇔ r s i j = II
 where
  x y : ℍ
  x = (r , i)
  y = (s , j)

  I : βŸͺ x ·⟨ 𝓗 ⟩ y ⟫ = (r ⇔ s)
  I = 𝓗-multiplication x y

  II : is-widespread (r ⇔ s)
  II = transport is-widespread I βŸͺ x ·⟨ 𝓗 ⟩ y ⟫-is-widespread

corollary-⇔-assoc : (r s t : Ξ©)
                  β†’ is-widespread r
                  β†’ is-widespread s
                  β†’ is-widespread t
                  β†’ (r ⇔ s) ⇔ t = r ⇔ (s ⇔ t)
corollary-⇔-assoc r s t i j k = I
 where
  _Β·_ : ℍ β†’ ℍ β†’ ℍ
  x Β· y = x ·⟨ 𝓗 ⟩ y

  x y z : ℍ
  x = (r , i)
  y = (s , j)
  z = (t , k)

  I =  (r ⇔ s) ⇔ t             =⟨refl⟩
       (βŸͺ x ⟫ ⇔ βŸͺ y ⟫) ⇔ βŸͺ z ⟫ =⟨ ap (_⇔ βŸͺ z ⟫) ((𝓗-multiplication _ _)⁻¹) ⟩
       βŸͺ x Β· y ⟫ ⇔ βŸͺ z ⟫       =⟨ (𝓗-multiplication _ _)⁻¹ ⟩
       βŸͺ (x Β· y) Β· z ⟫         =⟨ ap βŸͺ_⟫ (assoc 𝓗 x y z) ⟩
       βŸͺ x Β· (y Β· z) ⟫         =⟨ 𝓗-multiplication _ _ ⟩
       βŸͺ x ⟫ ⇔ βŸͺ y Β· z ⟫       =⟨ ap (βŸͺ x ⟫ ⇔_) (𝓗-multiplication _ _) ⟩
       βŸͺ x ⟫ ⇔ (βŸͺ y ⟫ ⇔ βŸͺ z ⟫) =⟨refl⟩
       r ⇔ (s ⇔ t)             ∎

\end{code}

Alternative characterization of the widespread property, as stated in
Johnstone's Elephant.

\begin{code}

open import UF.PropTrunc

module _ (pt : propositional-truncations-exist) where

 open PropositionalTruncation pt hiding (_∨_ ; ∨-elim)
 open Disjunction pt

 is-widespread' : Ξ© β†’ 𝓀⁺ Μ‡
 is-widespread' r = (p : Ξ©) β†’ (p ∨ (p β‡’ r)) holds

 widespread'-gives-widespread : (r : Ξ©)
                              β†’ is-widespread' r
                              β†’ is-widespread  r
 widespread'-gives-widespread r w' = w
  where
   I : (p : Ξ©)
     β†’ (p holds + (p holds β†’ r holds))
     β†’ ((p ⇔ r) ⇔ r) = p
   I p (inl h) =
    (p ⇔ r) ⇔ r =⟨ ap (Ξ» - β†’ (- ⇔ r) ⇔ r) Iβ‚€ ⟩
    (⊀ ⇔ r) ⇔ r =⟨ ap (_⇔ r) (⊀-⇔-neutral' pe r) ⟩
    r ⇔ r       =⟨ ⇔-refl pe r ⟩
    ⊀           =⟨ Iβ‚€ ⁻¹ ⟩
    p           ∎
     where
      Iβ‚€ : p = ⊀
      Iβ‚€ = holds-gives-equal-⊀ pe fe p h

   I p (inr f) = Ξ©-ext pe fe I₁ Iβ‚‚
    where
     have-f : (p β‡’ r) holds
     have-f = f

     I₁ : (p ⇔ r) ⇔ r = ⊀ β†’ p = ⊀
     I₁ e₁ =
      p     =⟨ Iβ‚„ ⟩
      r     =⟨ (⇔-gives-= pe _ _ e₁)⁻¹ ⟩
      p ⇔ r =⟨ =-gives-⇔ pe _ _ Iβ‚„ ⟩
      ⊀     ∎
       where
        Iβ‚‚ : r β‡’ (p ⇔ r) = ⊀
        Iβ‚‚ = ∧-elim-R pe fe _ _ e₁

        I₃ : (r β‡’ (p ⇔ r)) holds
        I₃ = equal-⊀-gives-holds _ Iβ‚‚

        g : (r β‡’ p) holds
        g r-holds = ∧-Elim-R (p β‡’ r) (r β‡’ p) (I₃ r-holds) r-holds

        Iβ‚„ : p = r
        Iβ‚„ = Ξ©-extensionality pe fe f g

     Iβ‚‚ : p = ⊀ β†’ (p ⇔ r) ⇔ r = ⊀
     Iβ‚‚ eβ‚‚ =
      (p ⇔ r) ⇔ r =⟨ ap (Ξ» - β†’ (- ⇔ r) ⇔ r) eβ‚‚ ⟩
      (⊀ ⇔ r) ⇔ r =⟨ ap (_⇔ r) (⊀-⇔-neutral' pe r) ⟩
      r ⇔ r       =⟨ ⇔-refl pe r ⟩
      ⊀           ∎

   w : is-widespread r
   w p = βˆ₯βˆ₯-rec (Ξ©-is-set fe pe) (I p) (w' p)

\end{code}

TODO. Write the above proof purely equationally. In order to do this,
first formulate and prove the equational definition of Heyting algebra
in other modules. Or to begin with, for simplicity, just prove in
UF.Logic that Ξ© satisfies the equations that define a lattice to be a
Heyting algebra.

\begin{code}

 widespread-gives-widespread' : (r : Ξ©)
                              β†’ is-widespread  r
                              β†’ is-widespread' r
 widespread-gives-widespread' r@(R , R-is-prop) w = IV
  where
   have-w : (p : Ξ©) β†’ ((p ⇔ r) ⇔ r) = p
   have-w = w

   module _ (p@(P , P-is-prop) : Ξ©) where

    P' : 𝓀 Μ‡
    P' = βˆ₯ P + (P β†’ R) βˆ₯

    I : ((P' ↔ R) ↔ R) ↔ P'
    I = equal-⊀-gives-holds _ (=-gives-⇔ pe _ _ (w (P' , βˆ₯βˆ₯-is-prop)))

    II : (P' β†’ R) β†’ R
    II f = f ∣ inr (Ξ» (Ο€ : P) β†’ f ∣ inl Ο€ ∣) ∣

    III : P'
    III = lr-implication I
           ((Ξ» (e : P' ↔ R) β†’ II (lr-implication e)) ,
            (Ξ» (ρ : R) β†’ (Ξ» (_ : P') β†’ ρ) ,
                         (Ξ» (_ : R) β†’ ∣ inr (Ξ» (_ : P) β†’ ρ) ∣)))

    IV : (p ∨ (p β‡’ r)) holds
    IV = III

\end{code}