Tom de Jong, late February - early March 2022.
Refactored slightly on 26 June 2024.

We consider sup-complete dcpos. Of course, every sup-complete poset is a dcpo,
but because the basic object of our domain-theoretic development is a dcpo, the
formalization is structured around dcpos that are additionally sup-complete.

The main point of this file is to show that we can extend families into a
sup-complete dcpo to directed families.

\begin{code}

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

open import MLTT.Spartan
open import UF.FunExt
open import UF.PropTrunc

module DomainTheory.Basics.SupComplete
        (pt : propositional-truncations-exist)
        (fe : Fun-Ext)
        (π“₯ : Universe)
       where

open PropositionalTruncation pt hiding (_∨_)

open import MLTT.List

open import UF.Equiv
open import UF.EquivalenceExamples

open import DomainTheory.Basics.Dcpo pt fe π“₯
open import DomainTheory.Basics.Miscelanea pt fe π“₯
open import DomainTheory.Basics.WayBelow pt fe π“₯

\end{code}

We first define, using a record for convenience, when a dcpo additionally has
all (small) suprema.

\begin{code}

module _
        (𝓓 : DCPO {𝓀} {𝓣})
       where
 record is-sup-complete : π“₯ ⁺ βŠ” 𝓀 βŠ” 𝓣 Μ‡  where
  field
   ⋁ : {I : π“₯ Μ‡ } (Ξ± : I β†’ ⟨ 𝓓 ⟩) β†’ ⟨ 𝓓 ⟩
   ⋁-is-sup : {I : π“₯ Μ‡ } (Ξ± : I β†’ ⟨ 𝓓 ⟩) β†’ is-sup (underlying-order 𝓓) (⋁ Ξ±) Ξ±

  ⋁-is-upperbound : {I : π“₯ Μ‡ } (Ξ± : I β†’ ⟨ 𝓓 ⟩)
                  β†’ is-upperbound (underlying-order 𝓓) (⋁ Ξ±) Ξ±
  ⋁-is-upperbound Ξ± = sup-is-upperbound (underlying-order 𝓓) (⋁-is-sup Ξ±)

  ⋁-is-lowerbound-of-upperbounds : {I : π“₯ Μ‡ } (Ξ± : I β†’ ⟨ 𝓓 ⟩)
                                 β†’ is-lowerbound-of-upperbounds
                                    (underlying-order 𝓓) (⋁ Ξ±) Ξ±
  ⋁-is-lowerbound-of-upperbounds Ξ± =
   sup-is-lowerbound-of-upperbounds (underlying-order 𝓓) (⋁-is-sup Ξ±)

\end{code}

In particular, we get finite joins, which we first define.

\begin{code}

module _
        (𝓓 : DCPO {𝓀} {𝓣})
       where

 ∨-family : (x y : ⟨ 𝓓 ⟩) β†’ πŸ™{π“₯} + πŸ™{π“₯} β†’ ⟨ 𝓓 ⟩
 ∨-family x y (inl _) = x
 ∨-family x y (inr _) = y

 record has-finite-joins : 𝓀 βŠ” 𝓣 βŠ” π“₯ Μ‡  where
  field
   βŠ₯ : ⟨ 𝓓 ⟩
   βŠ₯-is-least : is-least (underlying-order 𝓓) βŠ₯
   _∨_ : ⟨ 𝓓 ⟩ β†’ ⟨ 𝓓 ⟩ β†’ ⟨ 𝓓 ⟩
   ∨-is-sup : (x y : ⟨ 𝓓 ⟩)
            β†’ is-sup (underlying-order 𝓓) (x ∨ y) (∨-family x y)

  infix 100 _∨_

  ∨-is-upperbound₁ : {x y : ⟨ 𝓓 ⟩} β†’ x βŠ‘βŸ¨ 𝓓 ⟩ x ∨ y
  ∨-is-upperbound₁ {x} {y} = pr₁ (∨-is-sup x y) (inl ⋆)

  ∨-is-upperboundβ‚‚ : {x y : ⟨ 𝓓 ⟩} β†’ y βŠ‘βŸ¨ 𝓓 ⟩ x ∨ y
  ∨-is-upperboundβ‚‚ {x} {y} = pr₁ (∨-is-sup x y) (inr ⋆)

  ∨-is-lowerbound-of-upperbounds : {x y z : ⟨ 𝓓 ⟩}
                                 β†’ x βŠ‘βŸ¨ 𝓓 ⟩ z β†’ y βŠ‘βŸ¨ 𝓓 ⟩ z
                                 β†’ x ∨ y βŠ‘βŸ¨ 𝓓 ⟩ z
  ∨-is-lowerbound-of-upperbounds {x} {y} {z} u v = prβ‚‚ (∨-is-sup x y) z Ξ³
   where
    Ξ³ : is-upperbound (underlying-order 𝓓) z (∨-family x y)
    Ξ³ (inl _) = u
    Ξ³ (inr _) = v

sup-complete-dcpo-has-finite-joins : (𝓓 : DCPO {𝓀} {𝓣})
                                   β†’ is-sup-complete 𝓓
                                   β†’ has-finite-joins 𝓓
sup-complete-dcpo-has-finite-joins 𝓓 sc =
 record { βŠ₯ = ⋁ 𝟘-elim ;
          βŠ₯-is-least = Ξ» x β†’ ⋁-is-lowerbound-of-upperbounds 𝟘-elim x 𝟘-induction ;
          _∨_ = Ξ» x y β†’ ⋁ (∨-family 𝓓 x y);
          ∨-is-sup = Ξ» x y β†’ ⋁-is-sup (∨-family 𝓓 x y)
        }
  where
   open is-sup-complete sc

\end{code}

The converse holds as well: if a dcpo has finite joins then it is sup-complete.
This is because, by taking finite joins, we can replace an arbitrary family by
one that is directed and which has the same supremum.
This an important separate fact that we prove now.

\begin{code}

module make-family-directed
        (𝓓 : DCPO {𝓀} {𝓣})
        (𝓓-has-finite-joins : has-finite-joins 𝓓)
       where

 open has-finite-joins 𝓓-has-finite-joins

 module _
         {I : 𝓦 Μ‡ }
         (Ξ± : I β†’ ⟨ 𝓓 ⟩)
        where

  directify : List I β†’ ⟨ 𝓓 ⟩
  directify []      = βŠ₯
  directify (x ∷ l) = α x ∨ directify l

  directify-is-inhabited : βˆ₯ domain directify βˆ₯
  directify-is-inhabited = ∣ [] ∣

  ++-is-upperbound₁ : (l k : List I) β†’ directify l βŠ‘βŸ¨ 𝓓 ⟩ directify (l ++ k)
  ++-is-upperbound₁ []      k = βŠ₯-is-least (directify ([] ++ k))
  ++-is-upperbound₁ (i ∷ l) k =
   ∨-is-lowerbound-of-upperbounds ∨-is-upperbound₁
    (directify l              βŠ‘βŸ¨ 𝓓 ⟩[ ++-is-upperbound₁ l k ]
     directify (l ++ k)       βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperboundβ‚‚ ]
     Ξ± i ∨ directify (l ++ k) ∎⟨ 𝓓 ⟩)

  ++-is-upperboundβ‚‚ : (l k : List I) β†’ directify k βŠ‘βŸ¨ 𝓓 ⟩ directify (l ++ k)
  ++-is-upperboundβ‚‚ []      k = reflexivity 𝓓 (directify k)
  ++-is-upperboundβ‚‚ (i ∷ l) k =
   directify k              βŠ‘βŸ¨ 𝓓 ⟩[ ++-is-upperboundβ‚‚ l k ]
   directify (l ++ k)       βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperboundβ‚‚ ]
   Ξ± i ∨ directify (l ++ k) ∎⟨ 𝓓 ⟩

  ++-is-lowerbound-of-upperbounds : (l k : List I) (x : ⟨ 𝓓 ⟩)
                                  β†’ directify l βŠ‘βŸ¨ 𝓓 ⟩ x
                                  β†’ directify k βŠ‘βŸ¨ 𝓓 ⟩ x
                                  β†’ directify (l ++ k) βŠ‘βŸ¨ 𝓓 ⟩ x
  ++-is-lowerbound-of-upperbounds []      k x  u v = v
  ++-is-lowerbound-of-upperbounds (i ∷ l) k x u v =
   ∨-is-lowerbound-of-upperbounds β¦…1⦆ β¦…2⦆
    where
     β¦…1⦆ = Ξ± i              βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperbound₁ ]
          Ξ± i ∨ directify l βŠ‘βŸ¨ 𝓓 ⟩[ u ]
          x                 ∎⟨ 𝓓 ⟩
     β¦…2⦆ : directify (l ++ k) βŠ‘βŸ¨ 𝓓 ⟩ x
     β¦…2⦆ = ++-is-lowerbound-of-upperbounds l k x β¦…2'⦆ v
      where
       β¦…2'⦆ = directify l      βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperboundβ‚‚ ]
             Ξ± i ∨ directify l βŠ‘βŸ¨ 𝓓 ⟩[ u ]
             x                 ∎⟨ 𝓓 ⟩

  ++-is-binary-sup : (l k : List I)
                   β†’ is-sup (underlying-order 𝓓) (directify (l ++ k))
                            (∨-family 𝓓 (directify l) (directify k))
  ++-is-binary-sup l k = β¦…1⦆ , β¦…2⦆
   where
    β¦…1⦆ : (b : πŸ™ + πŸ™)
        β†’ ∨-family 𝓓 (directify l) (directify k) b βŠ‘βŸ¨ 𝓓 ⟩ directify (l ++ k)
    β¦…1⦆ (inl _) = ++-is-upperbound₁ l k
    β¦…1⦆ (inr _) = ++-is-upperboundβ‚‚ l k
    β¦…2⦆ : is-lowerbound-of-upperbounds (underlying-order 𝓓)
           (directify (l ++ k)) (∨-family 𝓓 (directify l) (directify k))
    β¦…2⦆ x x-ub = ++-is-lowerbound-of-upperbounds l k x
                  (x-ub (inl ⋆)) (x-ub (inr ⋆))

  directify-is-semidirected : is-Semidirected 𝓓 directify
  directify-is-semidirected l k =
   ∣ (l ++ k) , ++-is-upperbound₁ l k , ++-is-upperboundβ‚‚ l k ∣

  directify-is-directed : is-Directed 𝓓 directify
  directify-is-directed = (directify-is-inhabited , directify-is-semidirected)

  directify-upperbound : (x : ⟨ 𝓓 ⟩) β†’ is-upperbound (underlying-order 𝓓) x Ξ±
                       β†’ is-upperbound (underlying-order 𝓓) x directify
  directify-upperbound x x-is-ub []      = βŠ₯-is-least x
  directify-upperbound x x-is-ub (i ∷ l) =
   ∨-is-lowerbound-of-upperbounds (x-is-ub i) (directify-upperbound x x-is-ub l)

  directify-lowerbound-of-upperbounds :
     (x : ⟨ 𝓓 ⟩)
   β†’ is-lowerbound-of-upperbounds (underlying-order 𝓓) x Ξ±
   β†’ is-lowerbound-of-upperbounds (underlying-order 𝓓) x directify
  directify-lowerbound-of-upperbounds x x-is-lb y y-is-ub = x-is-lb y y-is-ub'
   where
    y-is-ub' : (i : I) β†’ Ξ± i βŠ‘βŸ¨ 𝓓 ⟩ y
    y-is-ub' i = Ξ± i             βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperbound₁ ]
                 Ξ± i ∨ βŠ₯         βŠ‘βŸ¨ 𝓓 ⟩[ reflexivity 𝓓 _  ]
                 directify [ i ] βŠ‘βŸ¨ 𝓓 ⟩[ y-is-ub [ i ]    ]
                 y               ∎⟨ 𝓓 ⟩

  directify-sup : (x : ⟨ 𝓓 ⟩) β†’ is-sup (underlying-order 𝓓) x Ξ±
                β†’ is-sup (underlying-order 𝓓) x directify
  directify-sup x (x-is-ub , x-is-lb-of-ubs) =
   ( directify-upperbound x x-is-ub
   , directify-lowerbound-of-upperbounds x x-is-lb-of-ubs)

  directify-sup' : (x : ⟨ 𝓓 ⟩) β†’ is-sup (underlying-order 𝓓) x directify
                 β†’ is-sup (underlying-order 𝓓) x Ξ±
  directify-sup' x (x-is-ub , x-is-lb-of-ubs) =
   ( (Ξ» i β†’ Ξ± i              βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperbound₁ ]
             directify [ i ] βŠ‘βŸ¨ 𝓓 ⟩[ x-is-ub [ i ] ]
             x               ∎⟨ 𝓓 ⟩)
   , (Ξ» y y-is-ub β†’ x-is-lb-of-ubs y (directify-upperbound y y-is-ub)))

\end{code}

Moreover, if each of the Ξ±α΅’'s are compact, then so is every element in the
directified family, because taking finite joins preserves compactness.

\begin{code}

  directify-is-compact : ((i : I) β†’ is-compact 𝓓 (Ξ± i))
                       β†’ (l : List I) β†’ is-compact 𝓓 (directify l)
  directify-is-compact Ξ±s-are-compact []      =
   βŠ₯-is-compact (𝓓 , βŠ₯ , βŠ₯-is-least)
  directify-is-compact αs-are-compact (i ∷ l) =
   binary-join-is-compact 𝓓 ∨-is-upperbound₁ ∨-is-upperboundβ‚‚
    (Ξ» d β†’ ∨-is-lowerbound-of-upperbounds) (Ξ±s-are-compact i) IH
    where
     IH : is-compact 𝓓 (directify l)
     IH = directify-is-compact Ξ±s-are-compact l

\end{code}

When constructing small compact bases for exponentials, it turns out that it is
convenient to consider a variation: we consider the family of elements Ξ±α΅’ less
than some given element x, and the corresponding family of lists l such that
directify l is less than x.

\begin{code}

  module _
          {x : ⟨ 𝓓 ⟩}
         where

   ↓-family : (Ξ£ i κž‰ I , Ξ± i βŠ‘βŸ¨ 𝓓 ⟩ x) β†’ ⟨ 𝓓 ⟩
   ↓-family = Ξ± ∘ pr₁

   directify-↓ : (Ξ£ l κž‰ List I , directify l βŠ‘βŸ¨ 𝓓 ⟩ x) β†’ ⟨ 𝓓 ⟩
   directify-↓ = directify ∘ pr₁

   directify-↓-is-inhabited : βˆ₯ domain directify-↓ βˆ₯
   directify-↓-is-inhabited = ∣ [] , βŠ₯-is-least x ∣

   directify-↓-is-semidirected : is-Semidirected 𝓓 directify-↓
   directify-↓-is-semidirected (l , l-below-x) (k , k-below-x) =
    ∣ ((l ++ k) , ++-is-lowerbound-of-upperbounds l k x l-below-x k-below-x)
                , (++-is-upperbound₁ l k) , (++-is-upperboundβ‚‚ l k) ∣


   directify-↓-is-directed : is-Directed 𝓓 directify-↓
   directify-↓-is-directed =
    (directify-↓-is-inhabited , directify-↓-is-semidirected)

   directify-↓-upperbound : is-upperbound (underlying-order 𝓓) x directify-↓
   directify-↓-upperbound = prβ‚‚

   directify-↓-sup : is-sup (underlying-order 𝓓) x ↓-family
                   β†’ is-sup (underlying-order 𝓓) x directify-↓
   directify-↓-sup (x-ub , x-lb-of-ubs) = (directify-↓-upperbound , Ξ³)
    where
     Ξ³ : is-lowerbound-of-upperbounds (underlying-order 𝓓) x directify-↓
     Ξ³ y y-is-ub = x-lb-of-ubs y claim
      where
       claim : is-upperbound (underlying-order 𝓓) y ↓-family
       claim (i , Ξ±α΅’-below-x) =
        Ξ± i                     βŠ‘βŸ¨ 𝓓 ⟩[ ∨-is-upperbound₁ ]
        directify-↓ ([ i ] , u) βŠ‘βŸ¨ 𝓓 ⟩[ y-is-ub ([ i ] , u) ]
        y                       ∎⟨ 𝓓 ⟩
         where
          u : Ξ± i ∨ βŠ₯ βŠ‘βŸ¨ 𝓓 ⟩ x
          u = ∨-is-lowerbound-of-upperbounds Ξ±α΅’-below-x (βŠ₯-is-least x)

   directify-↓-is-compact : ((i : I) β†’ is-compact 𝓓 (Ξ± i))
                          β†’ (j : domain directify-↓)
                          β†’ is-compact 𝓓 (directify-↓ j)
   directify-↓-is-compact Ξ±s-are-compact j =
    directify-is-compact Ξ±s-are-compact (pr₁ j)

\end{code}

Finally if the dcpo is locally small, then the family directify-↓ can be indexed
by a small type (provided the original family was indexed by a small type).

\begin{code}

 module _
         (𝓓-is-locally-small : is-locally-small 𝓓)
         {I : π“₯ Μ‡ }
         (Ξ± : I β†’ ⟨ 𝓓 ⟩)
        where

  open is-locally-small 𝓓-is-locally-small

  directify-↓-small : (x : ⟨ 𝓓 ⟩) β†’ (Ξ£ l κž‰ List I , directify Ξ± l βŠ‘β‚› x) β†’ ⟨ 𝓓 ⟩
  directify-↓-small x = directify Ξ± ∘ pr₁

  module _
          {x : ⟨ 𝓓 ⟩}
         where

   directify-↓-small-≃ : domain (directify-↓ Ξ±) ≃ domain (directify-↓-small x)
   directify-↓-small-≃ =
    Ξ£-cong (Ξ» l β†’ ≃-sym βŠ‘β‚›-≃-βŠ‘)

   directify-↓-small-sup : is-sup (underlying-order 𝓓) x (↓-family Ξ±)
                         β†’ is-sup (underlying-order 𝓓) x (directify-↓-small x)
   directify-↓-small-sup x-is-sup =
    reindexed-family-sup 𝓓 directify-↓-small-≃
     (directify-↓ Ξ±) x (directify-↓-sup Ξ± x-is-sup)

   directify-↓-small-is-directed : is-Directed 𝓓 (directify-↓-small x)
   directify-↓-small-is-directed =
    reindexed-family-is-directed 𝓓 directify-↓-small-≃
     (directify-↓ Ξ±) (directify-↓-is-directed Ξ±)

\end{code}

As announced, we get that dcpos with finite joins are sup-complete.

\begin{code}

dcpo-with-finite-joins-is-sup-complete : (𝓓 : DCPO {𝓀} {𝓣})
                                       β†’ has-finite-joins 𝓓
                                       β†’ is-sup-complete 𝓓
dcpo-with-finite-joins-is-sup-complete 𝓓 h =
 record {
  ⋁ = sup ;
  ⋁-is-sup = Ξ» Ξ± β†’ directify-sup' 𝓓 h Ξ± (sup Ξ±)
                                  (∐-is-sup 𝓓 (directify-is-directed 𝓓 h Ξ±))
 }
  where
   open has-finite-joins h
   open make-family-directed
   sup : {I : π“₯ Μ‡} β†’ (I β†’ ⟨ 𝓓 ⟩) β†’ ⟨ 𝓓 ⟩
   sup {I} Ξ± = ∐ 𝓓 (directify-is-directed 𝓓 h Ξ±)

\end{code}

Finally, we re-export the directify constructions via this module for use in
other files.

\begin{code}

module sup-complete-dcpo
        (𝓓 : DCPO {𝓀} {𝓣'})
        (𝓓-is-sup-complete : is-sup-complete 𝓓)
       where

 open is-sup-complete 𝓓-is-sup-complete
 open make-family-directed
       𝓓 (sup-complete-dcpo-has-finite-joins 𝓓 𝓓-is-sup-complete)
      public

\end{code}