{- | Completely regular cover spaces and Urysohn functions.
 -
 -   A scale from `V` to `U` is a family of sets `S q`, indexed by rationals, such that `V ⊆ S 0`, `S 1 ⊆ U`,
 -   and `S p s<=< S q` for `0 <= p < q <= 1`. If there is a scale from `V` to `U`, then `V` is completely below `U` ({StronglyRatherBelow.<=<sc}).
 -   A cover space is completely regular ({ScaleRegularCoverSpace}, {IsScaleRegular}) if every Cauchy cover is refined by
 -   the sets completely below its elements.
 -
 -   The main results are:
 -   * {<=<sc-urysohn}: `V <=<sc U` if and only if there is a Urysohn function for `V` and `U` ({IsUrysohn}),
 -     that is, a cover map `u : X -> [0,1]` that equals `1` on `V` and is supported in `U`.
 -   * {scaleRegular-urysohn}: a cover space is completely regular if and only if it has Urysohn functions ({HasUrysohnFunctions}).
 -
 -   A Urysohn function is constructed from a scale as a Dedekind cut: `u x` is the supremum of `1 - q` over `q` with `x ∈ S q`.
 -   Conversely, the sets `{x | (1 - q) / 2 < u x}` form a scale.
 -
 -   Classically, using dependent choice, {StronglyRatherBelow}.`<=<c` implies {StronglyRatherBelow.<=<sc}. Constructively, we use scales instead.
 -}

\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Pointed
\import Arith.Rat
\import Arith.Real
\import Arith.Real.Field
\import Arith.Real.LowerReal
\import Data.Or
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.LinearOrder
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths.Meta
\import Set.Set
\import Topology.CoverSpace
\import Topology.CoverSpace.Complete
\import Topology.NormedAbGroup.Real
\import Topology.NormedAbGroup.Real.Functions
\import Topology.RatherBelow

-- | A cover space is completely regular if every Cauchy cover is refined by the sets completely below its elements.
\type IsUrysohnCoverSpace (X : CoverSpace) : \Prop
  => \Pi {C : Set (Set X)} -> isCauchy C -> isCauchy \lam V => ∃ (U : C) (V StronglyRatherBelow.<=<sc U)

\class UrysohnCoverSpace \extends CompletelyStronglyRegularCoverSpace
  | isUrysohn {C : Set (Set E)} : isCauchy C -> isCauchy \lam V => ∃ (U : C) (V StronglyRatherBelow.<=<sc U)
  | isCompletelyStronglyRegular Cc => cauchy-subset (isUrysohn Cc) \lam (inP (U,CU,p)) => inP $ later (U, CU, RatherBelow.<=<sc_<=<c p)

-- | {u} is a Urysohn function for {V} and {U}: it takes values in `[0,1]`, equals `1` on {V}, and is supported in {U}.
\type IsUrysohnMap {X : CoverSpace} (V U : Set X) (u : CoverMap X RealNormed) : \Prop
  => \Sigma (\Pi (x : X) -> 0 <= u x) (\Pi (x : X) -> u x <= 1) (\Pi {x : X} -> V x -> u x = 1) (\Pi {x : X} -> 0 < u x -> U x)

-- | A cover space has Urysohn functions if every Cauchy cover is refined by the sets that admit a Urysohn function into its elements.
\type HasUrysohnFunctions (X : CoverSpace) : \Prop
  => \Pi {C : Set (Set X)} -> isCauchy C -> isCauchy \lam V => ∃ (U : C) (u : CoverMap X RealNormed) (IsUrysohnMap V U u)

-- | {V} is completely below {U} if and only if there is a Urysohn function for {V} and {U}.
\lemma <=<sc-urysohn {X : CoverSpace} {V U : Set X} : V StronglyRatherBelow.<=<sc U <-> ∃ (u : CoverMap X RealNormed) (IsUrysohnMap V U u)
  => (\lam (inP (S,VS,SU,h)) => scale-urysohn VS SU h, \lam (inP (u,uh)) => urysohn-scale uh)
  \where {
    \lemma scale-urysohn {X : CoverSpace} {V U : Set X} {S : Rat -> Set X} (VS : V ⊆ S 0) (SU : S 1 ⊆ U)
                         (h : \Pi {p q : Rat} -> 0 <= p -> p < q -> q <= 1 -> S p s<=< S q)
      : ∃ (u : CoverMap X RealNormed) (IsUrysohnMap V U u)
      => \have | u>=0 (x : X) : 0 <= scaleReal S h x => \lam ux<0 => \case real_<_U.1 ux<0 \with {
                 | byLeft h10 => linarith
                 | byRight (inP (q,q<=1,c,_)) => linarith
               }
               | u<=1 (x : X) : scaleReal S h x <= 1 => \lam ux>1 => \case real_<_L.1 ux>1 \with {
                 | byLeft h10 => linarith
                 | byRight (inP (q,q>=0,c,_)) => linarith
               }
         \in inP (real-cut-coverMap (scaleReal S h) u>=0 u<=1 (cut-cover h), (u>=0, u<=1,
               \lam {x} Vx => <=-antisymmetric (u<=1 x) \lam ux<1 => \case real_<_U.1 ux<1 \with {
                 | byLeft h11 => linarith
                 | byRight (inP (q,q<=1,c,nS)) => nS $ s<=<_<= (h <=-refl linarith q<=1) (VS Vx)
               },
               \lam {x} ux>0 => \case real_<_L.1 ux>0 \with {
                 | byLeft h00 => absurd linarith
                 | byRight (inP (q,q>=0,c,Sq)) => SU $ s<=<_<= (h q>=0 linarith <=-refl) Sq
               }))
      \where {
        -- | `u x > a`, where `u x` is the supremum of `1 - q` over `q` with `x ∈ S q`.
        \func lowerCut {X : \Set} (S : Rat -> Set X) (x : X) (a : Rat) : \Prop
          => (a < 0) || ∃ (q : Rat) (0 <= q) (q + a < 1) (S q x)

        -- | `u x < b`.
        \func upperCut {X : \Set} (S : Rat -> Set X) (x : X) (b : Rat) : \Prop
          => (1 < b) || ∃ (q : Rat) (q <= 1) (1 < q + b) (Not (S q x))

        \lemma cut-cover {X : PrecoverSpace} {S : Rat -> Set X} (h : \Pi {p q : Rat} -> 0 <= p -> p < q -> q <= 1 -> S p s<=< S q) {a b : Rat} (a<b : a < b)
          : isCauchy \lam W => (W ⊆ \lam x => lowerCut S x a) || (W ⊆ \lam x => upperCut S x b)
          => \case LinearOrder.dec<_<= a 0, LinearOrder.dec<_<= 1 b \with {
            | inl a<0, _ => top-cauchy $ later $ byLeft \lam _ => byLeft a<0
            | _, inl b>1 => top-cauchy $ later $ byRight \lam _ => byLeft b>1
            | inr a>=0, inr b<=1 => cauchy-subset (unfolds in h {1 - (a + b + b) * ratio 1 3} {1 - (a + a + b) * ratio 1 3} linarith linarith linarith) $ later \case __ \with {
              | byLeft p => byRight $ rewrite p \lam {x} nS => byRight $ inP (_, linarith, linarith, nS)
              | byRight p => byLeft $ rewrite p \lam {x} Sx => byRight $ inP (_, linarith, linarith, Sx)
            }
          }

        -- | The real number defined by a scale at a point.
        \func scaleReal {X : PrecoverSpace} (S : Rat -> Set X) (h : \Pi {p q : Rat} -> 0 <= p -> p < q -> q <= 1 -> S p s<=< S q) (x : X) : Real \cowith
          | L => lowerCut S x
          | L-inh => inP (-1, byLeft linarith)
          | L-closed p a'<a => \case p \with {
            | byLeft a<0 => byLeft linarith
            | byRight (inP (q,q>=0,c,Sq)) => byRight $ inP (q, q>=0, linarith, Sq)
          }
          | L-rounded {a} p => \case p \with {
            | byLeft a<0 => inP (a * ratio 1 2, byLeft linarith, linarith)
            | byRight (inP (q,q>=0,c,Sq)) => inP ((a + (1 - q)) * ratio 1 2, byRight $ inP (q, q>=0, linarith, Sq), linarith)
          }
          | U => upperCut S x
          | U-inh => inP (2, byLeft linarith)
          | U-closed p b<b' => \case p \with {
            | byLeft b>1 => byLeft linarith
            | byRight (inP (q,q<=1,c,nS)) => byRight $ inP (q, q<=1, linarith, nS)
          }
          | U-rounded {b} p => \case p \with {
            | byLeft b>1 => inP ((1 + b) * ratio 1 2, byLeft linarith, linarith)
            | byRight (inP (q,q<=1,c,nS)) => inP ((b + (1 - q)) * ratio 1 2, byRight $ inP (q, q<=1, linarith, nS), linarith)
          }
          | LU-disjoint p p' => \case p, p' \with {
            | byLeft a<0, byLeft a>1 => linarith
            | byLeft a<0, byRight (inP (q,q<=1,c,_)) => linarith
            | byRight (inP (q,q>=0,c,_)), byLeft a>1 => linarith
            | byRight (inP (q,q>=0,c,Sq)), byRight (inP (q',q'<=1,c',nS)) => nS $ s<=<_<= (h q>=0 linarith q'<=1) Sq
          }
          | LU-located a<b => \case cauchy-cover (cut-cover h a<b) x \with {
            | inP (W, byLeft g, Wx) => byLeft (g Wx)
            | inP (W, byRight g, Wx) => byRight (g Wx)
          }
      }

    \lemma urysohn-scale {X : CoverSpace} {V U : Set X} {u : CoverMap X RealNormed} (uh : IsUrysohnMap V U u) : V StronglyRatherBelow.<=<sc U
      => inP (\lam q x => (u x).L ((1 - q) * ratio 1 2),
              \lam {x} Vx => rewrite (uh.3 Vx) $ real_<_L.1 $ rat_real_<.1 linarith,
              \lam {x} ux => uh.4 $ real_<_L.2 $ LowerReal.L_<= ux linarith,
              \lam {p} {q} p>=0 p<q q<=1 => unfolds $ cauchy-refine (coverMap-cut u {(1 - q) * ratio 1 2} {(1 - p) * ratio 1 2} linarith) $ later \case __ \with {
                | byLeft g => inP (_, byRight idp, g)
                | byRight g => inP (_, byLeft idp, \lam {x} Wx Tpx => (u x).LU-disjoint Tpx (g Wx))
              })
  }

-- | A cover space is completely regular if and only if it has Urysohn functions.
\lemma scaleRegular-urysohn {X : CoverSpace} : IsUrysohnCoverSpace X <-> HasUrysohnFunctions X
  => (\lam r Cc => cauchy-subset (r Cc) \lam (inP (U,CU,p)) => TruncP.map (<=<sc-urysohn.1 p) \lam s => later (U, CU, s.1, s.2),
      \lam r Cc => cauchy-subset (r Cc) \lam (inP (U,CU,u,uh)) => inP $ later (U, CU, <=<sc-urysohn.2 $ inP (u,uh)))