{- | The space `L∞` of bounded measurable elements (Section `L-infinity` of lp-spaces).

  $\mathrm L^\infty(I) = \{F \in \mathrm L^0(I) \mid |F| \le M \text{ for some } M\}$ with the norm
  $\|F\|_\infty = \inf\{M \mid |F| \le M\}$, an upper real that need not be located.

  * {LInftyElem}: an element of `L⁰` with a bound; {linfNorm}: the upper-real norm.
  * {linfMul}: the product {l0Mul} of `L⁰` (Lemma `lem:L0`(2)) restricted to bounded elements.
  * {LInfty}: Proposition `prop:Linfty`(1),(2): `L∞` is a commutative real Banach algebra with unit {l0One}; moreover a
    Stone C*-algebra: $\|F^2\|_\infty \le \|F^2 + G^2\|_\infty$ ({linfNorm-c*-sum}, since squares are nonnegative) and
    $\|F\|_\infty^2 \le \|F^2\|_\infty$ ({linfNorm-c*-square}, since $|F^2| \le q^2$ implies $|F| \le q$).
    Completeness: the inclusion `L∞ → L⁰` is uniformly continuous ({l0-small-bounded}), and the bounds
    $|F_i - F_j| \le \varepsilon$ pass to the limit in `L⁰` since $\{G \mid \tau_\varepsilon|G - F| = |G - F|\}$ is closed.
-}

\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Ordered
\import Algebra.Ordered.RieszSpace
\import Algebra.Ordered.TruncRieszSpace
\import Algebra.Pointed
\import Algebra.QModule
\import Analysis.Integration.FullIntegrationSpace
\import Analysis.Integration.L0
\import Analysis.Integration.L0Product
\import Analysis.Integration.L0Unit
\import Arith.Rat
\import Arith.Real.Root
\import Arith.Real.UpperReal
\import Logic
\import Logic.Meta
\import Order.Biordered
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths
\import Paths.Meta
\import Set.Filter
\import Topology.CoverSpace.Complete
\import Topology.NormedRing
\import Topology.StoneCStarAlgebra
\import Topology.TopRieszSpace.Completion
\import Topology.UniformSpace.Complete
\open LatticeAbGroup
\open RieszSpace
\open QModule
\open AddMonoid
\open TruncRieszSpace

-- | An element of `L⁰` bounded by some `M`.
\record LInftyElem (L : FullIntegrationSpace) (el : L0Space L) (bounded : TruncRieszSpace.IsBounded {L0Space L} el)

-- | $\|F\|_\infty = \inf\{M \mid |F| \le M\}$ as an upper real.
\func linfNorm {L : FullIntegrationSpace} (F : L0Space L) : ExUpperReal \cowith
  | U b => ∃ (M : Rat) (M RatField.< b) (IsBelow (abs F) M)
  | U-closed (inP (M,M<b,p)) b<b' => inP (M, M<b <∘ b<b', p)
  | U-rounded {b} (inP (M,M<b,p)) => inP (RatField.mid M b, inP (M, RatField.mid>left M<b, p), RatField.mid<right M<b)

-- | Two elements of `L∞` have a common positive bound.
\lemma common-bound {L : FullIntegrationSpace} (F G : LInftyElem L)
  : ∃ (a : Rat) (a>0 : RatField.zro RatField.< a) (IsBelow (abs F.el) a) (IsBelow (abs G.el) a)
  => \case F.bounded, G.bounded \with {
    | inP (b,Fb), inP (c,Gc) => inP ((b RatField.∨ c) RatField.+ 1, linarith (RatField.join-left {b} {c}, Fb.1),
                                     IsBelow-level (linarith (RatField.join-left {b} {c})) Fb, IsBelow-level (linarith (RatField.join-right {b} {c})) Gc)
  }

-- | The product of `L∞`: {l0Mul}.
\func linfMul {L : FullIntegrationSpace} (F G : LInftyElem L) : L0Space L
  => l0Mul (F.el, G.el)

-- | $|FG| \le a^2$ if $|F|, |G| \le a$.
\lemma linfMul-bounded {L : FullIntegrationSpace} (F G : LInftyElem L) : TruncRieszSpace.IsBounded (linfMul F G)
  => \case common-bound F G \with {
    | inP (a,a>0,Fa,Ga) => inP (a RatField.* a, l0Mul-bound a>0 a>0 Fa Ga)
  }

-- | `L∞` is a Stone C*-algebra.
\instance LInfty (L : FullIntegrationSpace) : StoneC*Algebra (LInftyElem L)
  | AbGroup => LInftyGroup L
  | ide => linfOne L
  | * F G => \new LInftyElem L (linfMul F G) (linfMul-bounded F G)
  | ide-left => ext (l0Mul-ide-left _)
  | ide-right => ext (l0Mul-ide-right _)
  | *-assoc => ext (l0Mul-assoc _ _ _)
  | ldistr => ext (l0Mul-ldistr _ _ _)
  | rdistr {x} {y} {z} => ext (l0Mul-comm _ z.el *> l0Mul-ldistr z.el x.el y.el *> pmap2 (+) (l0Mul-comm z.el x.el) (l0Mul-comm z.el y.el))
  | isDivisible a {n} n/=0 => \case a.bounded \with {
    | inP (M,p) => inP (\new LInftyElem L (RatField.finv n *q a.el) (inP (RatField.abs (RatField.finv n) RatField.* M, transportInv (IsBelow __ _) abs_*q (IsBelow_*q RatField.abs>=0 p))),
                       ext (linf-*n {L} {n} *> inv *q_*n *> *q-finv-cancel (natRat/=0 n/=0)))
  }
  | *-comm {F} {G} => ext (l0Mul-comm F.el G.el)
  | norm F => linfNorm F.el
  | norm_zro => linfNorm-zro
  | norm_negative => linfNorm-negative
  | norm_+ => linfNorm-+
  | norm-double => linfNorm-double
  | norm-bounded F => \case F.bounded \with {
    | inP (M,p) => inP (M RatField.+ 1, inP (M, linarith, p))
  }
  | norm-ext p => ext (linfNorm-ext p)
  | norm_*_<= => linfNorm-mul
  | norm_ide_<= {b} b>1 => inP (1, b>1, l0One-below)
  | isCompleteMetric F Fc => linf-complete F Fc
  | c*-sum => linfNorm-c*-sum
  | c*-square => linfNorm-c*-square
  \where {
    -- | The unit of `L∞`.
    \func linfOne (L : FullIntegrationSpace) : LInftyElem L
      => \new LInftyElem L (l0One L) (inP (1, l0One-below))

    -- | $\|0\|_\infty = 0$.
    \lemma linfNorm-zro {L : FullIntegrationSpace} : linfNorm (L0Space L).zro = ExUpperRealAbMonoid.zro
      => <=-antisymmetric (\lam {b} b>0 => inP (0, b>0, transportInv (IsBelow __ 0) abs_zro (IsBelow_zro <=-refl)))
                          (\lam {b} (inP (M,M<b,p)) => p.1 <∘r M<b)

    \lemma linfNorm-negative {L : FullIntegrationSpace} {F : L0Space L} : linfNorm (negative F) = linfNorm F
      => pmap linfNorm' abs_negative
      \where
        \func linfNorm' {L : FullIntegrationSpace} (A : L0Space L) : ExUpperReal \cowith
          | U b => ∃ (M : Rat) (M RatField.< b) (IsBelow A M)
          | U-closed (inP (M,M<b,p)) b<b' => inP (M, M<b <∘ b<b', p)
          | U-rounded {b} (inP (M,M<b,p)) => inP (RatField.mid M b, inP (M, RatField.mid>left M<b, p), RatField.mid<right M<b)

    \lemma linfNorm-+ {L : FullIntegrationSpace} {F G : L0Space L} : linfNorm (F + G) <= linfNorm F ExUpperReal.+ linfNorm G
      => \lam {b} u => \case ExUpperReal.+_U.1 u \with {
        | inP (c, inP (M1,M1<c,p1), d, inP (M2,M2<d,p2), cd<b) => inP (M1 RatField.+ M2, linarith, IsBelow_<= abs_+ (IsBelow_+ p1 p2))
      }

    \lemma linfNorm-double {L : FullIntegrationSpace} {F : L0Space L} : linfNorm F ExUpperReal.+ linfNorm F <= linfNorm (F + F)
      => \lam {b} (inP (M,M<b,p)) =>
          \let | r => M RatField.* ratio 1 2
               | c => (M RatField.+ b) RatField.* ratio 1 4
          \in \have | e : (RatField.zro RatField.+ r) RatField.+ r = M => linarith
                    | pr : IsBelow (abs F) r => IsBelow_*n-div {_} {2} (\case __) (transport2 IsBelow (pmap abs (pmap (`+ F) (inv zro-left)) *> inv (abs_*n {_} {2} {F})) (inv e) p)
                    | rc : r RatField.< c => linarith
                    | Uc : (linfNorm F).U c => inP (r, rc, pr)
          \in ExUpperReal.+_U.2 (inP (c, Uc, c, Uc, (linarith : c RatField.+ c RatField.< b)))

    \lemma linfNorm-ext {L : FullIntegrationSpace} {F : L0Space L} (p : linfNorm F = ExUpperRealAbMonoid.zro) : F = (L0Space L).zro
      => l0-bounded-zero \lam {e} e>0 => \case transportInv (\lam (x : ExUpperReal) => x.U e) p e>0 \with {
        | inP (M,M<e,q) => IsBelow-level (<=-less M<e) q
      }

    -- | $\|FG\|_\infty \le \|F\|_\infty \|G\|_\infty$.
    \lemma linfNorm-mul {L : FullIntegrationSpace} {F G : LInftyElem L} : linfNorm (linfMul F G) <= linfNorm F.el ExUpperReal.* linfNorm G.el
      => \lam {q} u => \case ExUpperReal.*_U.1 u \with {
        | inP (b, inP (M1,M1<b,p1), b>0, c, inP (M2,M2<c,p2), c>0, bc<q) =>
          inP (b RatField.* c, bc<q, l0Mul-bound b>0 c>0 (IsBelow-level (<=-less M1<b) p1) (IsBelow-level (<=-less M2<c) p2))
      }

    -- | $\|F F\|_\infty \le \|F F + G G\|_\infty$.
    \lemma linfNorm-c*-sum {L : FullIntegrationSpace} {F G : LInftyElem L} : linfNorm (linfMul F F) <= linfNorm (linfMul F F + linfMul G G)
      => \lam {d} (inP (M,M<d,p)) => inP (M, M<d, below-sum (l0Mul-square>=0 F.el) (l0Mul-square>=0 G.el) p)
      \where
        \lemma below-sum {L : FullIntegrationSpace} {x y : L0Space L} {M : Rat} (x>=0 : 0 <= x) (y>=0 : 0 <= y) (p : IsBelow (abs (x + y)) M) : IsBelow (abs x) M
          => IsBelow_<= (=_<= (abs-ofPos x>=0) <=∘ (inv zro-right =<= <=_+ <=-refl y>=0) <=∘ =_<= (inv (abs-ofPos (inv zro-left =<= <=_+ x>=0 y>=0)))) p

    -- | $\|F\|_\infty^2 \le \|F F\|_\infty$.
    \lemma linfNorm-c*-square {L : FullIntegrationSpace} {F : LInftyElem L} : linfNorm F.el ExUpperReal.* linfNorm F.el <= linfNorm (linfMul F F)
      => \lam {d} (inP (M,M<d,p)) => \case sqrt-rat p.1 M<d \with {
        | inP (q,r,q>0,q<r,M<qq,rr<d) =>
          \have Ur : (linfNorm F.el).U r => inP (q, q<r, l0Mul-square-below q>0 (IsBelow-level (<=-less M<qq) p))
          \in ExUpperReal.*_U.2 (inP (r, Ur, q>0 <∘ q<r, r, Ur, q>0 <∘ q<r, rr<d))
      }

    -- | The additive group of `L∞`.
    \func LInftyGroup (L : FullIntegrationSpace) : AbGroup (LInftyElem L) \cowith
      | zro => \new LInftyElem L 0 IsBounded_zro
      | + F G => \new LInftyElem L (F.el + G.el) (IsBounded_+ F.bounded G.bounded)
      | zro-left => ext zro-left
      | +-assoc => ext +-assoc
      | +-comm => ext +-comm
      | negative F => \new LInftyElem L (negative F.el) (IsBounded_negative F.bounded)
      | negative-left => ext negative-left

    \lemma linf-*n {L : FullIntegrationSpace} {n : Nat} {F : LInftyElem L} : ((*n) {LInftyGroup L} n F).el = n *n F.el \elim n
      | 0 => idp
      | suc n => pmap (`+ F.el) (linf-*n {L} {n} {F})

    -- | Completeness of `L∞`: a Cauchy filter converges.
    \lemma linf-complete {L : FullIntegrationSpace} (F : ProperFilter (LInftyElem L))
                         (Fc : \Pi {eps : Rat} -> RatField.zro RatField.< eps -> ∃ (x : LInftyElem L) (F (\lam y => (linfNorm (x.el - y.el)).U eps)))
      : ∃ (x : LInftyElem L) ∀ {eps} (RatField.zro RatField.< eps) (F (\lam y => (linfNorm (x.el - y.el)).U eps))
      => \let | G : ProperFilter (L0Space L) => ProperFilter-map (\lam (y : LInftyElem L) => y.el) F
              | G' : CauchyFilter (L0Space L) => \new CauchyFilter (L0Space L) {
                  | ProperFilter => G
                  | isCauchyFilter => (cauchyFilter-uniform-char {L0Space L} {G}).2 \lam Cu => \case (L0Space L).ab-neighborhood-uniform.1 Cu \with {
                    | inP (V,Vo,V0,h) => \case l0-small-bounded Vo V0 \with {
                      | inP (e,e>0,g) => \case Fc e>0 \with {
                        | inP (x,Fx) => \case h x.el \with {
                          | inP (W,CW,hW) => inP (W, CW, filter-mono Fx \lam {y} (inP (M,M<e,p)) => hW (g (IsBelow-level (<=-less M<e) p)))
                        }
                      }
                    }
                  }
                }
              | ball-below {x : LInftyElem L} {e : Rat} (Fx : F (\lam y => (linfNorm (x.el - y.el)).U e)) : F (\lam y => IsBelow (abs (y.el - x.el)) e)
                  => filter-mono Fx \lam {y} (inP (M,M<e,p)) => IsBelow-level (<=-less M<e) (transport (IsBelow __ M) abs_- p)
         \in \case (L0Space L).isComplete (regCF G') \with {
           | inP (x0,h) =>
             \have | lim {c : L0Space L} {e : Rat} (e>0 : RatField.zro RatField.< e) (Fc' : F (\lam y => IsBelow (abs (y.el - c)) e)) : IsBelow (abs (x0 - c)) e
                       => l0-below-closed c e>0 \lam {V} Vo Vx0 => \case isProper (filter-meet (regCF_<= {_} {G'} (h ((L0Space L).open-char.1 Vo Vx0))) Fc') \with {
                            | inP (y, (Vy,py)) => inP (y.el, py, Vy)
                          }
                   | tri {a b c : L0Space L} : abs (a - c) <= abs (a - b) + abs (b - c) => =_<= (pmap abs (sub-split {L0Space L})) <=∘ abs_+
             \in \case Fc RatField.zro<ide \with {
               | inP (x1,F1) => \case x1.bounded \with {
                 | inP (M1,p1) =>
                   \let X0 => \new LInftyElem L x0 (inP (1 RatField.+ M1, IsBelow_<= (=_<= (pmap abs (inv (sub-add {L0Space L} {x0} {x1.el}))) <=∘ abs_+)
                                                                                    (IsBelow_+ (lim RatField.zro<ide (ball-below F1)) p1)))
                   \in inP (X0, \lam {eps} eps>0 =>
                        \let e2 => eps RatField.* ratio 1 2
                        \in \have e2>0 : RatField.zro RatField.< e2 => linarith
                        \in \case Fc e2>0 \with {
                          | inP (x,Fx) => filter-mono Fx \lam {y} (inP (M,M<e2,p)) =>
                              inP (e2 RatField.+ M, linarith, IsBelow_<= tri (IsBelow_+ (lim e2>0 (ball-below Fx)) p))
                        })
               }
             }
         }
      \where {
        \lemma sub-split {A : AbGroup} {a b c : A} : a - c = (a - b) + (b - c)
          => equation.abGroup

        \lemma sub-add {A : AbGroup} {a b : A} : (a - b) + b = a
          => equation.abGroup
      }
  }