--------------------------------------------------------------------------------
title:          The Sierpiล„ski locale
author:         Ayberk Tosun
date-completed: 2024-02-12
dates-updated:  [2024-03-09]
--------------------------------------------------------------------------------

This module contains the definition of the Sierpiล„ski locale as the Scott locale
of the Sierpiล„ski the dcpo.

In the future, other constructions of the Sierpiล„ski locale might be added here.

\begin{code}

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

open import UF.FunExt
open import MLTT.Spartan hiding (๐Ÿš)
open import UF.PropTrunc
open import UF.Subsingletons
open import UF.Size hiding (is-locally-small)

module Locales.Sierpinski.Definition
        (๐“ค  : Universe)
        (pe : Prop-Ext)
        (pt : propositional-truncations-exist)
        (fe : Fun-Ext)
        (sr : Set-Replacement pt) where

open import DomainTheory.BasesAndContinuity.Bases pt fe ๐“ค
open import DomainTheory.BasesAndContinuity.Continuity pt fe ๐“ค
open import DomainTheory.Basics.Dcpo    pt fe ๐“ค renaming (โŸจ_โŸฉ to โŸจ_โŸฉโˆ™)
open import DomainTheory.Basics.Miscelanea pt fe ๐“ค
open import DomainTheory.Basics.Pointed pt fe ๐“ค renaming (โŠฅ to โŠฅโˆ™)
open import DomainTheory.Basics.WayBelow pt fe ๐“ค
open import DomainTheory.Lifting.LiftingSet pt fe ๐“ค pe
open import DomainTheory.Lifting.LiftingSetAlgebraic pt pe fe ๐“ค
open import DomainTheory.Topology.ScottTopology pt fe ๐“ค
open import Lifting.Construction ๐“ค hiding (โŠฅ)
open import Lifting.Miscelanea-PropExt-FunExt ๐“ค pe fe
open import Locales.Frame pt fe hiding (๐Ÿš; is-directed)
open import UF.DiscreteAndSeparated
open import UF.Equiv
open import UF.Subsingletons-FunExt
open import UF.SubtypeClassifier

open Locale
open PropositionalTruncation pt

\end{code}

We first define the Sierpinski dcpo

\begin{code}

๐•Š๐““โบ : DCPO {๐“ค โบ } {๐“ค โบ}
๐•Š๐““โบ = ๐“›-DCPO {X = ๐Ÿ™ {๐“ค}} ๐Ÿ™-is-set

\end{code}

which is locally small and also algebraic:

\begin{code}

๐•Š-is-locally-small : is-locally-small ๐•Š๐““โบ
๐•Š-is-locally-small = ๐“›-is-locally-small {X = ๐Ÿ™ {๐“ค}} ๐Ÿ™-is-set

๐•Š๐““โบ-is-algebraic : is-algebraic-dcpo (๐“›-DCPO {X = ๐Ÿ™ {๐“ค}} ๐Ÿ™-is-set)
๐•Š๐““โบ-is-algebraic = ๐“›-is-algebraic-dcpo ๐Ÿ™-is-set

\end{code}

Unfortunately, we do not have the required machinery for making a locally small
copy of a dcpo from an extrinsic proof that it is locally small. In hindsight,
it would have been easier to work with such extrinsic proofs of local smallness,
but I didn't do this and right now, I don't have the time to migrate my
formalization to this style.

Therefore, I defined the function `๐“›-DCPOโป` which directly gives the locally
small copy of the dcpo in consideration. Instead of working with `๐•Š๐““โบ`, I work
with `๐•Š๐““` instead to circumvent this problem.

\begin{code}

๐•Š๐““ : DCPO {๐“ค โบ} {๐“ค}
๐•Š๐““ = ๐“›-DCPOโป {X = ๐Ÿ™ {๐“ค}} ๐Ÿ™-is-set

\end{code}

These two dcpos are of course order-isomorphic.

\begin{code}

โŠ‘-implies-โŠ‘โบ : (x y : โŸจ ๐•Š๐““ โŸฉโˆ™) โ†’ x โŠ‘โŸจ ๐•Š๐““ โŸฉ y โ†’ x โŠ‘โŸจ ๐•Š๐““โบ โŸฉ y
โŠ‘-implies-โŠ‘โบ x y p q = โŠ‘-to-โŠ‘' p q

โŠ‘โบ-implies-โŠ‘ : (x y : โŸจ ๐•Š๐““ โŸฉโˆ™) โ†’ x โŠ‘โŸจ ๐•Š๐““โบ โŸฉ y โ†’ x โŠ‘โŸจ ๐•Š๐““ โŸฉ y
โŠ‘โบ-implies-โŠ‘ x y p = (ฮป q โ†’ transport is-defined (p q) q) , ฮป _ โ†’ refl

\end{code}

The proposition `๐Ÿ˜` is the bottom element of this dcpo, meaning it can be
made into a pointed dcpo:

\begin{code}

๐•Š๐““โŠฅ : DCPOโŠฅ {๐“ค โบ} {๐“ค}
๐•Š๐““โŠฅ = ๐•Š๐““ , (๐Ÿ˜ , (ฮป ()) , ๐Ÿ˜-is-prop) , ฮป _ โ†’ (ฮป ()) , ฮป ()

\end{code}

The proposition `๐Ÿ™` is the top element of this dcpo.

\begin{code}

๐Ÿ™-is-top : (x : โŸจ ๐•Š๐““ โŸฉโˆ™) โ†’ x โŠ‘โŸจ ๐•Š๐““ โŸฉ ฮท โ‹†
๐Ÿ™-is-top (P , q) = (ฮป _ โ†’ โ‹†) , ฮป _ โ†’ refl

\end{code}

Furthermore, the dcpo `๐•Š๐““` is compact.

\begin{code}

๐•Š๐““-is-compact : is-compact ๐•Š๐““ (ฮท โ‹†)
๐•Š๐““-is-compact I ฮฑ (โˆฃiโˆฃ , upโป) pโป =
 โˆฅโˆฅ-rec โˆƒ-is-prop โ€  (ฮทs-are-compact ๐Ÿ™-is-set โ‹† I ฮฑ ฮด p)
  where
   open is-locally-small ๐•Š-is-locally-small

   up : is-semidirected (underlying-order ๐•Š๐““โบ) ฮฑ
   up i j = โˆฅโˆฅ-rec โˆƒ-is-prop โ€  (upโป i j)
    where
     โ€  : ฮฃ k ๊ž‰ I , (ฮฑ i โŠ‘โŸจ ๐•Š๐““  โŸฉ ฮฑ k) ร— (ฮฑ j โŠ‘โŸจ ๐•Š๐““  โŸฉ ฮฑ k)
       โ†’ โˆƒ k ๊ž‰ I , (ฮฑ i โŠ‘โŸจ ๐•Š๐““โบ โŸฉ ฮฑ k) ร— (ฮฑ j โŠ‘โŸจ ๐•Š๐““โบ โŸฉ ฮฑ k)
     โ€  (k , p , q) = โˆฃ k , โŠ‘-implies-โŠ‘โบ (ฮฑ i) (ฮฑ k) p  , โŠ‘-implies-โŠ‘โบ (ฮฑ j) (ฮฑ k) q โˆฃ

   ฮด : is-directed (underlying-order ๐•Š๐““โบ) ฮฑ
   ฮด = โˆฃiโˆฃ , up

   p : ฮท โ‹† โŠ‘โŸจ ๐•Š๐““โบ โŸฉ (โˆ (๐“›-DCPO ๐Ÿ™-is-set) ฮด)
   p = โŠ‘-to-โŠ‘' (prโ‚ pโป , ฮป _ โ†’ refl)

   โ€  : ฮฃ i ๊ž‰ I , underlying-order (๐“›-DCPO ๐Ÿ™-is-set) (ฮท โ‹†) (ฮฑ i)
     โ†’ โˆƒ i ๊ž‰ I , ฮท โ‹† โŠ‘โŸจ ๐•Š๐““ โŸฉ (ฮฑ i)
   โ€  (i , q) = โˆฃ i , โŠ‘โบ-implies-โŠ‘ (ฮท โ‹†) (ฮฑ i) q โˆฃ

\end{code}

We define a function for mapping inhabitants of the Sierpiล„ski dcpo to the type
of propositions:

\begin{code}

to-ฮฉ : โŸจ ๐•Š๐““ โŸฉโˆ™ โ†’ ฮฉ ๐“ค
to-ฮฉ (P , _ , h) = P , h

\end{code}

Conversely, we define a function mapping every proposition `P : ฮฉ ๐“ค` to the
carrier set of the Sierpiล„ski dcpo.

\begin{code}

to-๐•Š๐““ : ฮฉ ๐“ค โ†’  โŸจ ๐•Š๐““ โŸฉโˆ™
to-๐•Š๐““ (P , h) = P , (ฮป _ โ†’ โ‹†) , h

\end{code}

It is obvious that these form an equivalence.

\begin{code}

ฮฉ-equivalent-to-๐•Š : ฮฉ ๐“ค โ‰ƒ โŸจ ๐•Š๐““ โŸฉโˆ™
ฮฉ-equivalent-to-๐•Š = to-๐•Š๐““ , ((to-ฮฉ , โ€ ) , (to-ฮฉ , โ€ก))
 where
  ฯˆ : {A : ๐“ค  ฬ‡} โ†’ is-prop (A โ†’ ๐Ÿ™)
  ฯˆ = ฮ -is-prop fe (ฮป _ โ†’ ๐Ÿ™-is-prop)

  ฯ‘ : {A : ๐“ค  ฬ‡} โ†’ is-prop (is-prop A)
  ฯ‘ = being-prop-is-prop fe

  โ€  : (to-๐•Š๐““ โˆ˜ to-ฮฉ) โˆผ id
  โ€  (P , f , h) = to-subtype-๏ผ (ฮป _ โ†’ ร—-is-prop ฯˆ ฯ‘) refl

  โ€ก : to-ฮฉ โˆ˜ to-๐•Š๐““ โˆผ id
  โ€ก (P , h) = to-subtype-๏ผ (ฮป _ โ†’ ฯ‘) refl

\end{code}

For convenience we define abbreviations for the copies of `โŠค` and `โŠฅ` in `๐•Š๐““`.

\begin{code}

โŠคโ‚› : โŸจ ๐•Š๐““ โŸฉโˆ™
โŠคโ‚› = to-๐•Š๐““ โŠค

โŠฅโ‚› : โŸจ ๐•Š๐““ โŸฉโˆ™
โŠฅโ‚› = to-๐•Š๐““ โŠฅ

\end{code}

We now proceed to the definition of the Sierpiล„ski locale.

First, we show that `๐•Š๐““` has a specified small compact basis.

\begin{code}

open import Locales.ScottLocale.Definition pt fe ๐“ค
open import Locales.ScottLocale.ScottLocalesOfAlgebraicDcpos pt fe ๐“ค

hscb : has-specified-small-compact-basis ๐•Š๐““
hscb = (๐Ÿ™ {๐“ค} + ๐Ÿ™ {๐“ค}) , ฮฒ , ฯƒ
 where
  ฮฒ : ๐Ÿ™ + ๐Ÿ™ โ†’ โŸจ ๐•Š๐““ โŸฉโˆ™
  ฮฒ (inl โ‹†) = โŠฅโ‚›
  ฮฒ (inr โ‹†) = โŠคโ‚›

  ฮฒ-is-compact : (b : ๐Ÿ™ + ๐Ÿ™) โ†’ is-compact ๐•Š๐““ (ฮฒ b)
  ฮฒ-is-compact (inl โ‹†) = โŠฅ-is-compact ๐•Š๐““โŠฅ
  ฮฒ-is-compact (inr โ‹†) = ๐•Š๐““-is-compact

  ฮฒ-is-upward-directed : (x : โŸจ ๐•Š๐““ โŸฉโˆ™)
                       โ†’ is-semidirected (underlying-order ๐•Š๐““) (โ†“-inclusion ๐•Š๐““ ฮฒ x)
  ฮฒ-is-upward-directed x (inl โ‹† , p) (z , q)  = let
                                                 u = (z , q)
                                                 rโ‚ = reflexivity ๐•Š๐““ (ฮฒ (inl โ‹†))
                                                 rโ‚‚ = reflexivity ๐•Š๐““ (ฮฒ z)
                                                in
                                                 โˆฃ u , โŠฅ-is-least ๐•Š๐““โŠฅ (ฮฒ z) , rโ‚‚ โˆฃ
  ฮฒ-is-upward-directed x (inr โ‹† , pโ‚) (z , q) = let
                                                 rโ‚ = reflexivity ๐•Š๐““ (ฮฒ (inr โ‹†))
                                                 rโ‚‚ = reflexivity ๐•Š๐““ (ฮฒ (inr โ‹†))
                                                in
                                                 โˆฃ (inr โ‹† , pโ‚) , rโ‚ , ๐Ÿ™-is-top (ฮฒ z) โˆฃ

  covering : (x : โŸจ ๐•Š๐““ โŸฉโˆ™) โ†’ is-sup (underlying-order ๐•Š๐““) x (โ†“-inclusion ๐•Š๐““ ฮฒ x)
  covering ๐’ซ@(P , f , h) = prโ‚‚ , โ€ 
   where
    โ€  : is-lowerbound-of-upperbounds (underlying-order ๐•Š๐““) (P , f , h) (โ†“-inclusion ๐•Š๐““ ฮฒ (P , f , h))
    โ€  ๐’ซโ€ฒ@(Pโ€ฒ , fโ€ฒ , hโ€ฒ) ฯ… = โ€ก
     where
      โ™  : P โ†’ ๐’ซ โŠ‘โŸจ ๐•Š๐““ โŸฉ ๐’ซโ€ฒ
      โ™  p = transport (ฮป - โ†’ - โŠ‘โŸจ ๐•Š๐““ โŸฉ ๐’ซโ€ฒ) eq (ฯ… (inr โ‹† , q))
       where
        q : ฮฒ (inr โ‹†) โŠ‘โŸจ ๐•Š๐““ โŸฉ ๐’ซ
        q = (ฮป _ โ†’ p) , ฮป _ โ†’ ๐Ÿ™-is-prop โ‹† (f p)

        eq : ฮฒ (inr โ‹†) ๏ผ ๐’ซ
        eq = antisymmetry ๐•Š๐““ (ฮฒ (inr โ‹†)) ๐’ซ q (๐Ÿ™-is-top ๐’ซ)

      โ€ก : underlying-order ๐•Š๐““ (P , f , h) ๐’ซโ€ฒ
      โ€ก = (ฮป p โ†’ prโ‚ (โ™  p) p) , ฮป p โ†’ ๐Ÿ™-is-prop โ‹† (f p)

  ฯƒ : is-small-compact-basis ๐•Š๐““ ฮฒ
  ฯƒ = record
       { basis-is-compact = ฮฒ-is-compact
       ; โŠ‘แดฎ-is-small = ฮป x b โ†’ (ฮฒ b โŠ‘โŸจ ๐•Š๐““ โŸฉ x) , โ‰ƒ-refl (ฮฒ b โŠ‘โŸจ ๐•Š๐““ โŸฉ x)
       ; โ†“แดฎ-is-directed = ฮป x โ†’ โˆฃ inl โ‹† , โŠฅ-is-least ๐•Š๐““โŠฅ x โˆฃ , ฮฒ-is-upward-directed x
       ; โ†“แดฎ-is-sup = covering
       }

๐•Š๐““-is-structurally-algebraic : structurally-algebraic ๐•Š๐““
๐•Š๐““-is-structurally-algebraic =
 structurally-algebraic-if-specified-small-compact-basis ๐•Š๐““ hscb

\end{code}

Using this compact basis, we define the Sierpiล„ski locale as the Scott locale of
`๐•Š๐““`.

\begin{code}

open ScottLocaleConstruction ๐•Š๐““ hscb pe

๐•Š : Locale (๐“ค โบ) ๐“ค ๐“ค
๐•Š = ScottLocale

\end{code}

Added on 2024-03-08.

There are three important opens of the Sierpiล„ski locale.

````````````````````````````````````````````````````````````````````````````````
    ฮฉ
    |
   {โŠคโ‚›}
    |
    โˆ…
````````````````````````````````````````````````````````````````````````````````

The top and bottom one are the full subset and the empty subset of `ฮฉ`. We now
define the singleton open lying in the middle. We call this Scott open `truth`.

We first define the subset `โŸจ ๐•Š๐““ โŸฉ โ†’ ฮฉ` underlying this map, which is in fact
just the identity map since given a proposition `P`, `P ๏ผ โŠค` iff `P` holds.

\begin{code}

open DefnOfScottLocale ๐•Š๐““ ๐“ค pe

truthโ‚€ : โŸจ ๐•Š๐““ โŸฉโˆ™ โ†’ ฮฉ ๐“ค
truthโ‚€ (P , _ , i) = (P , i)

\end{code}

We now package this subset up with the proof that it is Scott open.

\begin{code}

open DefnOfScottTopology ๐•Š๐““ ๐“ค

truthโ‚€-is-upward-closed : is-upwards-closed truthโ‚€ holds
truthโ‚€-is-upward-closed U V u (ฯ† , _) = ฯ† u

truthแตฃ : ๐’ชโ‚›แดฟ
truthแตฃ =
 record
  { pred                              = truthโ‚€
  ; pred-is-upwards-closed            = ฯ…
  ; pred-is-inaccessible-by-dir-joins = ฮน
  }
  where
   ฯ… : is-upwards-closed truthโ‚€ holds
   ฯ… U V u (ฯ† , _) = ฯ† u

   ฮน : is-inaccessible-by-directed-joins truthโ‚€ holds
   ฮน U ฮผ = ฮผ

truth : โŸจ ๐’ช ๐•Š โŸฉ
truth = from-๐’ชโ‚›แดฟ truthแตฃ

\end{code}