\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Ordered
\import Algebra.Pointed
\import Algebra.StrictlyOrdered
\import Arith.Real
\import Arith.Real.Field
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.Biordered
\import Order.Lattice
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths
\import Paths.Meta
\import Set.Fin
\import Set.Set
\import Topology.Compact
\import Topology.Compact.CompactSupport
\import Topology.CoverSpace
\import Topology.CoverSpace.Urysohn
\import Topology.NormedAbGroup
\import Topology.NormedAbGroup.Real
\import Topology.RatherBelow
\import Topology.TopAbGroup
\import Topology.TopSpace
\import Topology.UniformSpace
\open LatticeAbGroup (abs)

\class UrysohnGroup \extends TopAddGroup, UrysohnCoverSpace {
  -- | Every neighborhood of `0` contains a neighborhood completely below it.
  \lemma neighborhood-<=<sc {V : Set E} (Vo : isOpen V) (V0 : V 0) : ∃ (W : isOpen) (W 0) (W StronglyRatherBelow.<=<sc V)
    => \case cauchy-regular-cover (isUrysohn (cauchy-open.1 Vo V0)) 0 \with {
      | inP (V', inP (U,g,p), c0) => inP (_, interior {_} {V'}, c0, RatherBelow.Scale.<=<-right (\lam c => <=<_<= c idp) $ RatherBelow.Scale.<=<-left p $ g $ s<=<_<= (RatherBelow.<=<sc_<=< p) $ <=<_<= c0 idp)
    }

  -- | A uniform Urysohn function for a neighborhood of `0` contained in a totally bounded set.
  \lemma neighborhood-urysohn {V : Set E} (Vo : isOpen V) (V0 : V 0) {T : Set E} (Ttb : IsTotallyBoundedSet T) (V<=T : V ⊆ T)
    : ∃ (W : isOpen) (W 0) (u : UniformMap \this RealNormed) (IsUrysohnMap W V u)
    => \case neighborhood-<=<sc Vo V0 \with {
      | inP (W,Wo,W0,p) => \case <=<sc-urysohn.1 p \with {
        | inP (u,uh) => inP (W, Wo, W0, totallyBounded-support-uniform properUniform Ttb u \lam {x} nT => \have ux<=0 : u x <= 0 => \lam ux>0 => nT $ V<=T $ uh.4 ux>0 \in <=-antisymmetric ux<=0 (uh.1 x), uh)
      }
    }
}

-- | A locally totally bounded topological group which is completely regular as a cover space.
\class LocallyCompactUrysohnGroup \extends UrysohnGroup {
  | locallyTB : IsLocallyTotallyBounded \this

  -- | A symmetric totally bounded neighborhood of `0`.
  \lemma tb-neighborhood : ∃ (V : isOpen) (V 0) (\Pi {x : E} -> V x -> V (negative x)) (IsTotallyBoundedSet V)
    => \case topAddGroup-locallyTB-char.1 locallyTB \with {
      | inP (U,Uo,U0,Utb) => \case neighborhood-sym Uo U0 \with {
        | inP (V,Vo,V0,Vs,V<=U) => inP (V, Vo, V0, Vs, totallyBoundedSet-subset V<=U Utb)
      }
    }

  -- | A Urysohn cutoff function.
  \lemma cutoff {V : Set E} (Vo : isOpen V) (V0 : V 0)
    : ∃ (u : CompactCoverMap \this) (W : isOpen) (W 0) (\Pi (x : E) -> 0 <= u x) (\Pi (x : E) -> u x <= 1) (\Pi {x : E} -> W x -> u x = 1) (IsSupported u V)
    => \case tb-neighborhood \with {
      | inP (V1,V1o,V10,_,V1tb) => \case neighborhood-urysohn (open-inter Vo V1o) (V0,V10) V1tb (meet-right : V ∧ V1 ⊆ V1) \with {
        | inP (W,Wo,W0,u,uh) => inP (\new CompactCoverMap \this {
            | CoverMap => u
            | support => inP (_, totallyBoundedSet-subset (meet-right : V ∧ V1 ⊆ V1) V1tb, \lam p => uh.4 (rewrite (RealAbGroup.abs-ofPos (uh.1 _)) in p))
          }, W, Wo, W0, uh.1, uh.2, uh.3, \lam p => (meet-left : V ∧ V1 ⊆ V) (uh.4 (rewrite (RealAbGroup.abs-ofPos (uh.1 _)) in p)))
      }
    }

  -- | A bump function which equals `1` on a neighborhood `K + U` of a totally bounded set.
  \lemma bump {K : Set E} (Ktb : IsTotallyBoundedSet K)
    : ∃ (U : isOpen) (U 0) (h : CompactCoverMap \this) (\Pi (x : E) -> 0 <= h x) (\Pi (x : E) -> h x <= 1) (\Pi {k y : E} -> K k -> U y -> h (k + y) = 1)
    => \case cutoff open-top () \with {
      | inP (u,W,Wo,W0,u>=0,u<=1,uW,_) => \case shrink2 Wo W0 \with {
        | inP (U,Uo,U0,_,UW) => \case addGroup-tb-char.1 Ktb Uo U0 \with {
          | inP (a,ac) =>
            \let | psi => BigSum {CompactCoverTruncRieszSpace \this} (\lam i => CompactMap-translate (a i) u)
                 | psi>=0 (x : E) : 0 <= psi x
                   => rewrite (CompactCoverTruncRieszSpace.BigSum-apply {_} {\lam i => CompactMap-translate (a i) u}) $ RealAbGroup.BigSum_>=0 \lam i => u>=0 _
            \in inP (U, Uo, U0, CompactCoverMap.trunc psi, \lam x => meet-univ (psi>=0 x) zro<=ide, \lam x => meet-right, \lam {k} {y} Kk Uy => \case ac Kk \with {
                  | inP (i,c) =>
                    \have p : 1 <= psi (k + y) => rewrite (CompactCoverTruncRieszSpace.BigSum-apply {_} {\lam i => CompactMap-translate (a i) u}) $ =_<= (inv $ uW $ transport W +-assoc (UW c.1 Uy)) <=∘
                                RealAbGroup.BigSum_>=index {\lam i => u (negative (a i) + (k + y))} (\lam i => u>=0 _) i
                    \in <=-antisymmetric meet-right (meet-univ p <=-refl)
                })
        }
      }
    }

  -- | A partition of unity on a totally bounded set subordinate to translates of a neighborhood.
  \lemma partition {K : Set E} (Ktb : IsTotallyBoundedSet K) {V : Set E} (Vo : isOpen V) (V0 : V 0)
    : ∃ (a : Array E) (h : Array (CompactCoverMap \this) a.len) (\Pi (j : Fin a.len) (x : E) -> 0 <= h j x) (\Pi (j : Fin a.len) -> IsSupported (h j) (\lam x => V (negative (a j) + x)))
        (\Pi (x : E) -> RealAbGroup.BigSum (\lam j => h j x) <= 1) (\Pi {x : E} -> K x -> RealAbGroup.BigSum (\lam j => h j x) = 1)
    => \case cutoff Vo V0 \with {
      | inP (u,W,Wo,W0,u>=0,u<=1,uW,uV) => \case addGroup-tb-char.1 Ktb Wo W0 \with {
        | inP (a,ac) =>
          \let | psi => BigSum {CompactCoverTruncRieszSpace \this} (\lam i => CompactMap-translate (a i) u)
               | D => lipschitz-uniform (CompactMap-ta-uniform psi :: nil) zro<=ide (\lam x => psi x ∨ 1) \lam x y =>
                        transportInv (_ <=) (RealField.ide-left *> RealAbGroup.zro-right) $ RealAbGroup.join-lip <=∘ =_<= (pmap (_ RealAbGroup.+) (pmap abs RealAbGroup.negative-right *> RealAbGroup.abs_zro) *> RealAbGroup.zro-right)
               | D>=1 (x : E) : 1 <= D x => join-right
               | h (j : Fin a.len) => CompactCoverMap.div (CompactMap-translate (a j) u) D D>=1
          \in \have | psi-eq (x : E) : RealAbGroup.BigSum (\lam j => h j x) = psi x * RealField.pinv (D x) (zro<ide <∘l D>=1 x)
                        => inv (RealField.BigSum-rdistr {\lam j => u (negative (a j) + x)}) *> pmap (* RealField.pinv (D x) (zro<ide <∘l D>=1 x)) (inv (CompactCoverTruncRieszSpace.BigSum-apply {_} {\lam i => CompactMap-translate (a i) u} {x}))
                    | D>0 (x : E) => zro<ide <∘l D>=1 x
              \in inP (a, h, \lam j x => RealField.<=_*_positive_positive (u>=0 _) (<=-less (RealField.pinv>0 (D>0 x))),
                       \lam j => IsSupported.bound_*c {E} {CompactMap-translate (a j) u} {h j} (\lam p => uV p) (\lam x => CompactCoverMap.div.div-bound (D>0 x) (D>=1 x) <=∘ =_<= (inv RealField.ide-left)),
                       \lam x => rewrite psi-eq $ RealField.<=_*_positive-left (join-left : psi x <= psi x ∨ 1) (<=-less (RealField.pinv>0 (D>0 x))) <=∘ =_<= (RealField.pinv-right (D>0 x)),
                       \lam {x} Kx => \case ac Kx \with {
                         | inP (i,c) =>
                           \have | p : 1 <= psi x => rewrite (CompactCoverTruncRieszSpace.BigSum-apply {_} {\lam i => CompactMap-translate (a i) u}) $ =_<= (inv $ uW c.1) <=∘
                                       RealAbGroup.BigSum_>=index {\lam i => u (negative (a i) + x)} (\lam i => u>=0 _) i
                                 | q : psi x = D x => <=-antisymmetric join-left (join-univ <=-refl p)
                           \in psi-eq x *> pmap (* _) q *> RealField.pinv-right (D>0 x)
                       })
      }
    }
}