\import Algebra.Group
\import Algebra.Group.GroupHom
\import Algebra.Group.SubGroup
\import Algebra.Meta
\import Algebra.Module
\import Algebra.Monoid
\import Algebra.Monoid.MonoidHom
\import Algebra.Ordered
\import Algebra.Ordered.TruncRieszSpace
\import Algebra.QModule
\import Equiv
\import Analysis.Measure.MeasureRing
\import Arith.Rat
\import Arith.Real
\import Arith.Real.Field
\import Arith.Real.UpperReal
\import Function.Meta
\import Logic
\import Logic.Meta
\import Order.Biordered
\import Order.Lattice
\import Order.PartialOrder
\import Paths
\import Paths.Meta
\import Topology.BanachLattice
\import Topology.BanachSpace
\import Topology.MetricSpace
\import Topology.MetricSpace.Complete
\import Topology.NormedAbGroup
\import Topology.NormedAbGroup.Real
\import Topology.CoverSpace.Complete
\import Topology.CoverSpace.CompletionTools
\import Topology.CoverSpace.Product
\import Topology.MetricSpace.ExCompletion (ExMetricCompletion)
\import Topology.NormedAbGroup.ExCompletion
\import Topology.NormedAbGroup.Real.Functions
\import Topology.TopAbGroup.Complete
\import Topology.TopSpace
\import Topology.TopSpace.Product
\import Topology.UniformSpace
\import Topology.UniformSpace.Complete
\import Topology.MetricSpace.UpperReal

\class IntegrationSpace \extends TruncRieszSpace, PseudoNormedAbGroup, ExPseudoNormedRieszSpace, RealPreBanachSpace {
  | integral : E -> Real
  | integral>=0 {a : E} : 0 <= a -> RealAbGroup.zro RealAbGroup.<= integral a
  | integral_+ {a b : E} : integral (a + b) = integral a RealAbGroup.+ integral b
  | norm a => integral (abs a)
  | norm_zro => pmap integral abs_zro *> pmap (\lam x => x)
    (RealAbGroup.cancel-left (integral 0) (inv integral_+ *> pmap integral zro-right *> inv RealAbGroup.zro-right))
  | norm_negative => pmap integral abs_negative
  | norm_+ {x} {y} => transport (_ ExUpperRealAbMonoid.<=) RealAbGroup.+-upper $ Real.<=-upper.1 $
      transport2 (RealAbGroup.<=) RealAbGroup.zro-right
        (inv integral_+ *> pmap integral (simplify : abs (x + y) + (abs x + abs y - abs (x + y)) = abs x + abs y) *> integral_+)
        (RealAbGroup.<=_+ RealAbGroup.<=-refl $ integral>=0 $ to>=0 abs_+)
  | norm_<= {x} {y} |x|<=|y| => Real.<=-upper.1 $ transport2 (RealAbGroup.<=) RealAbGroup.zro-right
      (inv integral_+ *> pmap integral (simplify : abs x + (abs y - abs x) = abs y))
      (RealAbGroup.<=_+ RealAbGroup.<=-refl $ integral>=0 $ to>=0 |x|<=|y|)
  | norm-double {x} => transport (ExUpperRealAbMonoid.<= _) RealAbGroup.+-upper $ Real.<=-upper.1 $ =_<= {RealAbGroup} $
      inv integral_+ *> pmap integral (inv $ pmap abs (pmap (+ x) (inv zro-left)) *> inv (abs_*n {_} {2}) *> pmap (+ _) zro-left)

  \func integral-hom : AddGroupHom \this RealAbGroup integral \cowith
    | func-+ => integral_+

  \lemma integral-mono {a b : E} (a<=b : a <= b) : integral a RealAbGroup.<= integral b
    => transport2 (RealAbGroup.<=) RealAbGroup.zro-right
        (inv integral_+ *> pmap integral (simplify : a + (b - a) = b))
        (gcongr (integral>=0 $ to>=0 a<=b))

  \lemma integral_abs {a : E} : RealAbGroup.abs (integral a) RealAbGroup.<= integral (abs a)
    => RealAbGroup.join-univ (integral-mono abs>=id) $ transport (RealAbGroup.<= _) integral-hom.func-negative $ integral-mono abs>=neg

  \lemma integral_*q {q : Rat} {a : E} : integral (q *q a) = q RealField.* integral a
    => func-*q {_} {_} {integral-hom} *> QModule.fromRatModule_*q

  \lemma integral-split {a b : E} : integral a RealAbGroup.<= integral b RealAbGroup.+ integral (abs (a - b))
    => \have e : integral a = integral b RealAbGroup.+ integral (a - b) => pmap integral (inv (simplify : b + (a - b) = a)) *> integral_+
       \in rewrite e (gcongr (integral-mono abs>=id))

  -- | (R1) holds for `f` if `τ_η f ≤ η e` for a fixed `e` and all `η > 0`.
  \lemma integral-trunc-dominated {f e : E} (h : \Pi {eta : Rat} -> RatField.zro RatField.< eta -> truncAt eta f <= eta *q e)
    {eps : Rat} (eps>0 : RatField.zro RatField.< eps) : ∃ (eta : Rat) (RatField.zro RatField.< eta) (integral (truncAt eta f) RealAbGroup.< Real.fromRat eps)
    => \case (integral e).U-inh \with {
      | inP (M,eM) =>
        \let | M' => M RatField.∨ 1
             | eta => RatField.finv M' RatField.* (eps RatField.* ratio 1 2)
        \in \have | M'>0 : RatField.zro RatField.< M' => RatField.zro<ide <∘l RatField.join-right
                  | eta>0 : RatField.zro RatField.< eta => RatField.<_*_positive_positive (RatField.finv>0 M'>0) linarith
                  | e1 : eta RatField.* M' = eps RatField.* ratio 1 2 => RatField.*-comm *> inv RatField.*-assoc *> pmap (RatField.* _) (RatField.finv-right $ RatField.>_/= M'>0) *> RatField.ide-left
            \in inP (eta, eta>0, dominated-arith (integral-mono (h eta>0)) integral_*q eta>0 (real_<_U.2 eM) e1 eps>0)
    }

  \lemma ldist_abs {a b : E} : ldist (abs a) (abs b) RealAbGroup.<= ldist a b
    => rewrite (lnorm-ldist, lnorm-ldist) $ integral-mono $ abs-univ abs_-left abs_-right

  \lemma integral-metric : NormedAbGroupMap \this RealNormed integral \cowith
    | func-+ => integral_+
    | func-norm => Real.<=-upper.1 integral_abs

  \lemma trunc-metric : MetricMap \this \this trunc \cowith
    | func-dist => rewrite (norm-dist, norm-dist) $ Real.<=-upper.1 $ integral-mono trunc-lipschitz

  \func MeasRing : PseudoMeasureRing \cowith
    | BooleanRing => BoolRing
    | meas a => integral a.1
    | meas_zro => integral-hom.func-zro
    | meas>=0 {x} => integral>=0 (IsBoolean>=0 x.2)
    | meas-disjoint {x} {y} p => pmap integral TruncRieszSpace.BoolRing.join-char
      *> pmap integral (inv (disjoint_pos_+_join (IsBoolean>=0 x.2) (IsBoolean>=0 y.2) (pmap __.1 p))) *> integral_+
    \where {
      \protected \func inc : IsometricMap MeasRing \this __.1 \cowith
        | func-isometry => norm-dist *> inv MeasRing.norm-dist
    }
}
  \where {
    \lemma dominated-arith {t s i : Real} {eta M eps : Rat} (t<=s : t <= s) (s=ei : s = eta RealField.* i) (eta>0 : RatField.zro RatField.< eta)
                           (i<M : i RealAbGroup.< M) (e1 : eta * (M ∨ 1) = eps * ratio 1 2) (eps>0 : RatField.zro RatField.< eps) : t RealAbGroup.< eps
      => \have | ie : i <= Real.fromRat (M ∨ 1) => <=-less i<M <=∘ rat_real_<=.1 join-left
               | p : Real.fromRat eta RealField.* i <= Real.fromRat eta RealField.* Real.fromRat (M ∨ 1) => gcongr
         \in (t<=s <=∘ =_<= s=ei <=∘ p <=∘ =_<= (RealField.*-rat *> pmap Real.fromRat e1)) <∘r rat_real_<.1 linarith
  }

-- | A top-level version of {IntegrationSpace.integral-mono} for {gcongr}.
\attr gcongr
\lemma integral-mono_gcongr {E : IntegrationSpace} {a b : E} (a<=b : a <= b) : E.integral a RealAbGroup.<= E.integral b
  => E.integral-mono a<=b

-- | An integration space is regular if `I(τ_η f) → 0` as `η → 0⁺` and `I(τ_T f) → I(f)` as `T → ∞`, for `f ≥ 0`.
\class RegularIntegrationSpace \extends IntegrationSpace {
  | integral-trunc-small {f : E} : 0 <= f -> \Pi {eps : Rat} -> RatField.zro RatField.< eps
    -> ∃ (eta : Rat) (RatField.zro RatField.< eta) (integral (truncAt eta f) RealAbGroup.< Real.fromRat eps)
  | integral-trunc-large {f : E} : 0 <= f -> \Pi {eps : Rat} -> RatField.zro RatField.< eps
    -> ∃ (T : Rat) (RatField.zro RatField.< T) (integral f RealAbGroup.< integral (truncAt T f) RealAbGroup.+ Real.fromRat eps)
}

-- | A regular integration space whose elements are bounded; (R2) holds automatically.
\class BoundedIntegrationSpace \extends BoundedTruncRieszSpace, RegularIntegrationSpace {
  | integral-trunc-large {f} f>=0 {eps} eps>0 => \case trunc-bounded f \with {
    | inP (M,fM) =>
      \have | T>0 : RatField.zro RatField.< M RatField.∨ 1 => RatField.zro<ide <∘l RatField.join-right
            | e : truncAt (M RatField.∨ 1) f = f => <=-antisymmetric (truncAt-bound T>0)
              (truncAt-greatest T>0 (IsBelow-level RatField.join-left (IsBelow_<= (=_<= (inv (abs-ofPos f>=0))) fM)) <=-refl)
      \in inP (M RatField.∨ 1, T>0, rewrite e $ transport (RealAbGroup.< _) RealAbGroup.zro-right $ RealAbGroup.<_+-right (integral f) (rat_real_<.1 eps>0))
  }
}

\instance RatIntegrationSpace : BoundedIntegrationSpace Rat
  | TruncRieszSpace => RatTrunc
  | integral x => x
  | integral>=0 p => rat_real_<=.1 p
  | integral_+ => inv RealAbGroup.+-rat
  | trunc-bounded a => inP (RatTrunc.abs a, (RatTrunc.abs>=0, inP (1, <=-less RatField.zro<ide, <=-antisymmetric RatField.meet-left (RatField.meet-univ <=-refl <=-refl), =_<= (inv rat_*q))))
  | trunc-archimedean p => rat_archimedean \lam n => transport (RatField.<= 1) (p n) RatField.meet-right
  | integral-trunc-small {f} _ {eps} eps>0 => inP (eps * ratio 1 2, linarith, rat_real_<.1 $ truncAt-le linarith <∘r linarith)
  \where {
    \func RatTrunc : TruncRieszSpace Rat \cowith
      | PosetAbGroup => RatField
      | join => RatField.join
      | join-left => join-left
      | join-right => join-right
      | join-univ => join-univ
      | isDivisible a {n} n/=0 => inP (RatField.finv n * a, inv RatField.natCoef_*_*n *> inv *-assoc *> pmap (* a) (RatField.finv-right $ natRat/=0 n/=0) *> ide-left)
      | trunc x => x ∧ 1
      | trunc_zro>=0 => <=-refl
      | trunc-bound => meet-left
      | trunc-assoc => meet-univ (RatField.negative_<= join-left <=∘ =_<= RatField.negative-isInv) (RatField.negative_<= join-right <=∘ =_<= RatField.negative-isInv <=∘ meet-right)

    \lemma rat_*q {c : Rat} : c RatTrunc.*q 1 = c
      => func-*q {RatTrunc} {QModule.fromRatModule (RingLModule RatField)} {\new AddGroupHom { | func x => x | func-+ => idp }}
        *> QModule.fromRatModule_*q *> RatField.ide-right

    \lemma truncAt-le {eta : Rat} (eta>0 : RatField.zro RatField.< eta) {f : Rat} : RatTrunc.truncAt eta f <= eta
      => RatTrunc.<=_*q-right (<=-less eta>0) meet-right <=∘ =_<= rat_*q
  }

\instance RealIntegrationSpace : BoundedIntegrationSpace Real
  | TruncRieszSpace => RealTrunc
  | integral x => x
  | integral>=0 p => p
  | integral_+ => idp
  | trunc-bounded a => \case (RealTrunc.abs a).U-inh \with {
    | inP (M,aM) => inP (M, (rat_real_<=.2 $ <=-less $ RealTrunc.abs>=0 <∘r real_<_U.2 aM, inP (1, <=-less RealAbGroup.zro<ide, <=-antisymmetric RealAbGroup.meet-left (RealAbGroup.meet-univ <=-refl <=-refl), <=-less (real_<_U.2 aM) <=∘ =_<= (inv real_*q))))
  }
  | trunc-archimedean p => real_archimedean \lam n => transport (RealAbGroup.<= 1) (p n) RealAbGroup.meet-right
  | integral-trunc-small {f} _ {eps} eps>0 => inP (eps * ratio 1 2, linarith, truncAt-le linarith <∘r rat_real_<.1 linarith)
  \where {
    \func RealTrunc : TruncRieszSpace Real \cowith
      | PosetAbGroup => RealAbGroup
      | join => RealAbGroup.join
      | join-left => join-left
      | join-right => join-right
      | join-univ => join-univ
      | isDivisible a {n} n/=0 => inP (RatField.finv n RealField.* a, inv RealField.natCoef_*_*n *> inv RealField.*-assoc *> pmap (RealField.* a) (pmap (RealField.* _) (inv RealField.coefMap_natCoef) *> RealField.*-rat *> pmap Real.fromRat (RatField.finv-right $ natRat/=0 n/=0)) *> RealField.ide-left)
      | trunc x => x ∧ 1
      | trunc_zro>=0 => meet-univ <=-refl (RealAbGroup.<=-less RealAbGroup.zro<ide)
      | trunc-bound => meet-left
      | trunc-assoc => meet-univ (RealAbGroup.negative_<= join-left <=∘ =_<= RealAbGroup.negative-isInv) (RealAbGroup.negative_<= join-right <=∘ =_<= RealAbGroup.negative-isInv <=∘ meet-right)

    \lemma real_*q {c : Rat} : c RealTrunc.*q 1 = Real.fromRat c
      => func-*q {RealTrunc} {_} {\new AddGroupHom RealTrunc RealAbGroup { | func x => x | func-+ => idp }} *> QModule.fromRatModule_*q *> RealField.ide-right

    \lemma truncAt-le {eta : Rat} (eta>0 : RatField.zro RatField.< eta) {f : Real} : RealTrunc.truncAt eta f <= Real.fromRat eta
      => RealTrunc.<=_*q-right (<=-less eta>0) meet-right <=∘ =_<= real_*q
  }

\record IntegrationSpaceHom \extends TruncRieszSpaceHom, NormedIsometricMap {
  \override Dom : IntegrationSpace
  \override Cod : IntegrationSpace

  | func-integral {x : Dom} : integral (func x) = integral x
  | func-norm-isometry {x} => Real.=-upper.1 (pmap integral (inv func-abs) *> func-integral)

  \lemma func-boolean {x : Dom} (xb : IsBoolean x) : IsBoolean (func x)
    => pmap trunc (inv func-+) *> inv func-trunc *> pmap func xb

  \func MeasRingHom : MeasureRingHom (MeasRing {Dom}) (MeasRing {Cod}) \cowith
    | func a => (func a.1, func-boolean a.2)
    | func-+ => ext $ func-abs *> pmap LatticeAbGroup.abs func-minus
    | func-* => ext func-meet
    | func-meas => func-integral
} \where {
  \open TruncRieszSpace
  \open IntegrationSpace

  \lemma isometry-denseEmbedding {X Y : IntegrationSpace} {f : IntegrationSpaceHom X Y} (fd : f.IsDense) : f.IsDenseEmbedding
    => (fd, f.embedding->coverEmbedding (f.dense->uniformEmbedding fd).2)

  \func id {X : IntegrationSpace} : IntegrationSpaceHom X X \cowith
    | TruncRieszSpaceHom => TruncRieszSpaceHom.id
    | func-integral => idp

  \func \fixl 8 compose \alias \infixl 8 ∘ {X Y Z : IntegrationSpace} (g : IntegrationSpaceHom Y Z) (f : IntegrationSpaceHom X Y) : IntegrationSpaceHom X Z \cowith
    | TruncRieszSpaceHom => g TruncRieszSpaceHom.∘ f
    | func-integral => func-integral *> func-integral
}

\class CompleteIntegrationSpace \extends IntegrationSpace, RieszLSpace, CompleteNormedAbGroup {
  | norm_+_>= {x} {y} x>=0 y>=0 => =_<= {RealAbGroup} $ inv $ pmap integral (abs-ofPos $ transport (<= _) zro-left $ <=_+ x>=0 y>=0) *> integral_+ *> pmap2 (RealAbGroup.+) (pmap integral $ inv $ abs-ofPos x>=0) (pmap integral $ inv $ abs-ofPos y>=0)

  \func CompleteMeasRing : CompleteMeasureRing \cowith
    | PseudoMeasureRing => MeasRing
    | meas-ext {x} p => ext $ norm-ext $ pmap integral (abs-ofPos (IsBoolean>=0 x.2)) *> p
    | isStronglyComplete => metric-stronglyComplete $ isometric-closed-complete IntegrationSpace.MeasRing.inc $
        transport IsClosed (ext \lam y => ext (\lam b => inP ((y,b), idp), \lam (inP (x,p)) => transport IsBoolean p x.2)) IsBoolean-closed

  \lemma IsBoolean-closed : IsClosed IsBoolean
    => \lam xl => equalizer-closed (trunc-metric ContMap.∘ +-cont ContMap.∘ ProductTopSpace.tuple ContMap.id ContMap.id) ContMap.id
        \lam Vo Vx => TruncP.map (xl Vo Vx) \lam s => (s.1, s.2, s.3)
}

\class CompleteRegularIntegrationSpace \extends RegularIntegrationSpace, CompleteIntegrationSpace {
  -- | The subgroup of bounded elements.
  \func boundedSub : SubAddGroup \this \cowith
    | contains => IsBounded
    | contains_zro => IsBounded_zro
    | contains_+ => IsBounded_+
    | contains_negative => IsBounded_negative

  -- | The bounded part `X_b = {a | |a| ≤ M·1 for some M}` as an integration space.
  \func boundedBase : IntegrationSpace \cowith
    | AbGroup => IAbGroup boundedSub
    | <= a b => a.1 <= b.1
    | <=-refl => <=-refl
    | <=-transitive => <=-transitive
    | <=-antisymmetric p q => ext (<=-antisymmetric p q)
    | <=_+ => <=_+
    | join a b => (a.1 ∨ b.1, IsBounded_join a.2 b.2)
    | join-left => join-left
    | join-right => join-right
    | join-univ => join-univ
    | isDivisible a {n} n/=0 => inP ((RatField.finv n *q a.1, IsBounded_*q (RatField.finv>=0 fromNat_>=0) a.2),
        ext $ AddMonoidHom.func-*n {IAddGroup.embed} *> inv *q_*n *> inv *q-assoc *> pmap (*q a.1) (RatField.finv-right $ natRat/=0 n/=0) *> ide_*q)
    | trunc a => (trunc a.1, IsBounded_trunc a.2)
    | trunc_zro>=0 => trunc_zro>=0
    | trunc-bound => trunc-bound
    | trunc-assoc => trunc-assoc
    | integral a => integral a.1
    | integral>=0 => integral>=0
    | integral_+ => integral_+

  \func baseInc : IntegrationSpaceHom boundedBase \this \cowith
    | func a => a.1
    | func-+ => idp
    | func-<= p => p
    | func-join>= => <=-refl
    | func-trunc => idp
    | func-integral => idp

  -- | `IsBelow` in {E} lifts to the bounded part.
  \lemma below-lift {a : boundedBase} {r : Rat} (p : IsBelow a.1 r) : TruncRieszSpace.IsBelow {boundedBase} a r
    => (p.1, \case p.2 \with {
      | inP (u,u>=0,uU,au) => inP ((u, IsUnderUnit-bounded u>=0 uU), u>=0, (ext uU : TruncRieszSpace.trunc {boundedBase} (u, IsUnderUnit-bounded u>=0 uU) = (u, IsUnderUnit-bounded u>=0 uU)), au <=∘ =_<= (inv (func-*q {_} {_} {baseInc})))
    })

  -- | An element whose multiples are all under the unit is non-positive.
  \lemma infinitesimal<=0 {a : E} (h : \Pi (n : Nat) -> IsUnderUnit (n *n a)) : a <= 0
    => \have | hp (n : Nat) : IsUnderUnit (n *n part+ a) => pmap trunc (inv part+_*n) *> trunc_join *> pmap2 (∨) (h n) trunc_zro *> part+_*n
             | below {eta : Rat} (eta>0 : RatField.zro RatField.< eta) : IsBelow (part+ a) eta
                => \have N/=0 : (rat_natBound (RatField.finv eta)).1 /= 0
                          => \lam N=0 => RatField.<-irreflexive (RatField.<-transitive RatField.zro<ide (transport (\lam (k : Nat) => 1 RatField.< k RatField.*n eta) N=0 (rat_*n-unbounded eta>0)))
                   \in IsBelow_*n-div N/=0 $ IsBelow-level (<=-less (rat_*n-unbounded eta>0)) (IsUnderUnit-below (*n_>=0 join-right) (hp _))
             | small {d : Rat} (d>0 : RatField.zro RatField.< d) : integral (part+ a) RealAbGroup.< Real.fromRat d
                => \case integral-trunc-small {_} {part+ a} join-right d>0 \with {
                  | inP (eta,eta>0,h') => transport (\lam z => integral z RealAbGroup.< _) (<=-antisymmetric (truncAt-bound eta>0) (truncAt-greatest eta>0 (below eta>0) <=-refl)) h'
                }
             | I=0 : integral (part+ a) = RealAbGroup.zro => RealAbGroup.<=-antisymmetric (real_<=-eps \lam d>0 => transportInv (_ RealAbGroup.<) RealAbGroup.zro-left (small d>0)) (integral>=0 join-right)
       \in join-left <=∘ =_<= (norm-ext (pmap integral (abs-ofPos join-right) *> I=0))

  -- | The bounded part `X_b` of a complete regular integration space is a bounded integration space.
  \func boundedSpace : BoundedIntegrationSpace \cowith
    | IntegrationSpace => boundedBase
    | trunc-bounded a => \case a.2 \with {
      | inP (M,aM) => inP (M, below-lift aM)
    }
    | trunc-archimedean {a} p => infinitesimal<=0 \lam n => inv (pmap trunc (AddMonoidHom.func-*n {baseInc})) *> pmap __.1 (p n) *> AddMonoidHom.func-*n {baseInc}
    | integral-trunc-small {f} f>=0 {eps} eps>0 => \case integral-trunc-small {_} {f.1} f>=0 eps>0 \with {
      | inP (eta,eta>0,h) => inP (eta, eta>0, transportInv (RealAbGroup.< _) (pmap integral (TruncRieszSpaceHom.func-truncAt {baseInc})) h)
    }

  -- | The inclusion of the bounded part.
  \func boundedInc : IntegrationSpaceHom boundedSpace \this \cowith
    | func a => a.1
    | func-+ => idp
    | func-<= p => p
    | func-join>= => <=-refl
    | func-trunc => idp
    | func-integral => idp

  -- | A homomorphism from a bounded integration space lands in the bounded part.
  \func bounded-lift {L : BoundedIntegrationSpace} (f : IntegrationSpaceHom L \this) : IntegrationSpaceHom L boundedSpace \cowith
    | func x => (f x, f.func-IsBounded (L.trunc-bounded x))
    | func-+ => ext f.func-+
    | func-<= p => f.func-<= p
    | func-join>= => f.func-join>=
    | func-trunc => ext f.func-trunc
    | func-integral => f.func-integral

  -- | The bounded part is dense.
  \lemma boundedInc-dense : boundedInc.IsDense
    => \lam {y} Uo Uy => \case dist_open.1 Uo Uy \with {
      | inP (eps,eps>0,sub) => \case integral-trunc-large {_} {part+ y} join-right (half>0 eps>0), integral-trunc-large {_} {part- y} join-right (half>0 eps>0) \with {
        | inP (T,T>0,h1), inP (T',T'>0,h2) =>
          \let | t => truncAt T (part+ y)
               | t' => truncAt T' (part- y)
          \in \have | le : abs (y - (t - t')) <= (part+ y - t) + (part- y - t')
                        => =_<= (pmap abs (pmap (__ - _) (inv parts_-) *> equation.abGroup)) <=∘ abs_+ <=∘ =_<= (pmap2 (+) (abs-ofPos (to>=0 (truncAt-bound T>0))) (abs_negative *> abs-ofPos (to>=0 (truncAt-bound T'>0))))
                    | le' => RealAbGroup.<=-transitive (integral-mono le) (=_<= {RealAbGroup} (integral_+ *> pmap2 (RealAbGroup.+) integral-hom.func-minus integral-hom.func-minus))
             \in inP (t - t', inP ((t - t', IsBounded_+ (IsBounded-truncAt T>0 join-right) (IsBounded_negative (IsBounded-truncAt T'>0 join-right))), idp),
                      sub $ real_<_U.1 $ transportInv (RealAbGroup.< _) (lnorm-ldist {\this} {y} {t - t'}) $ dense-arith le' h1 h2)
      }
    }
}
  \where {
    \lemma half>0 {eps : Rat} (eps>0 : RatField.zro RatField.< eps) : RatField.zro RatField.< eps * ratio 1 2
      => positivity

    \lemma dense-arith {d s1 t1 s2 t2 : Real} {eps : Rat} (le : d <= (s1 RealAbGroup.+ RealAbGroup.negative t1) RealAbGroup.+ (s2 RealAbGroup.+ RealAbGroup.negative t2))
                       (h1 : s1 RealAbGroup.< t1 RealAbGroup.+ Real.fromRat (eps * ratio 1 2)) (h2 : s2 RealAbGroup.< t2 RealAbGroup.+ Real.fromRat (eps * ratio 1 2)) : d RealAbGroup.< Real.fromRat eps
      => \have e : Real.fromRat eps = Real.fromRat (eps * ratio 1 2) RealAbGroup.+ Real.fromRat (eps * ratio 1 2)
                => pmap Real.fromRat (RatField.<=-antisymmetric linarith linarith) *> inv RealAbGroup.+-rat
         \in linarith
  }

-- | (R1) and (R2) transfer along a dense homomorphism from a regular integration space.
\func dense-regular {X : RegularIntegrationSpace} {Z : IntegrationSpace} (f : IntegrationSpaceHom X Z) (fd : f.IsDense) : RegularIntegrationSpace \cowith
  | IntegrationSpace => Z
  | integral-trunc-small {F} F>=0 {eps} eps>0 => \case approx fd F>=0 (third>0 eps>0) \with {
    | inP (x,x>=0,Fx) => \case X.integral-trunc-small x>=0 (third>0 eps>0) \with {
      | inP (eta,eta>0,h) =>
        \have | t1 => Z.integral-split {Z.truncAt eta F} {Z.truncAt eta (f x)}
                | t2 : Z.integral (Z.abs (Z.truncAt eta F - Z.truncAt eta (f x))) RealAbGroup.<= Z.integral (Z.abs (F - f x)) => Z.integral-mono (Z.truncAt-lipschitz eta>0)
                | t3 : Z.integral (Z.truncAt eta (f x)) = X.integral (X.truncAt eta x) => pmap Z.integral (inv f.func-truncAt) *> f.func-integral
        \in inP (eta, eta>0, third-arith1 eps>0 t1 t2 (rewrite t3 h) Fx)
    }
  }
  | integral-trunc-large {F} F>=0 {eps} eps>0 => \case approx fd F>=0 (third>0 eps>0) \with {
    | inP (x,x>=0,Fx) => \case X.integral-trunc-large x>=0 (third>0 eps>0) \with {
      | inP (T,T>0,h) =>
        \have | t1 => Z.integral-split {F} {f x}
                | t2 => Z.integral-split {Z.truncAt T (f x)} {Z.truncAt T F}
                | t3 : Z.integral (Z.abs (Z.truncAt T (f x) - Z.truncAt T F)) RealAbGroup.<= Z.integral (Z.abs (F - f x)) => Z.integral-mono (Z.truncAt-lipschitz T>0 <=∘ =_<= Z.abs_-)
                | t4 : Z.integral (Z.truncAt T (f x)) = X.integral (X.truncAt T x) => pmap Z.integral (inv f.func-truncAt) *> f.func-integral
        \in inP (T, T>0, third-arith2 t1 (transport2 (\lam u v => u RealAbGroup.< v RealAbGroup.+ _) (inv f.func-integral) (inv t4) h) t2 t3 Fx)
    }
  }
  \where {
    \lemma third>0 {eps : Rat} (eps>0 : RatField.zro RatField.< eps) : RatField.zro RatField.< eps * ratio 1 3
      => linarith

    -- | A positive element of {Z} is approximated by images of positive elements.
    \lemma approx {X Z : IntegrationSpace} {f : IntegrationSpaceHom X Z} (fd : f.IsDense) {F : Z} (F>=0 : Z.zro <= F) {d : Rat} (d>0 : RatField.zro RatField.< d)
      : ∃ (x : X) (X.zro <= x) (Z.integral (Z.abs (F - f x)) RealAbGroup.< Real.fromRat d)
      => \case fd {F} {OBall d F} OBall-open (OBall-center d>0) \with {
        | inP (z, inP (y,p), h) =>
          \have | e : f (X.part+ y) = Z.part+ (f y) => f.func-join *> pmap (_ ∨) f.func-zro
                  | n : Z.integral (Z.abs (F - f y)) RealAbGroup.< Real.fromRat d => transport (RealAbGroup.< _) (lnorm-ldist {Z} {F} {f y}) $ real_<_U.2 $ transportInv (OBall d F) p h
                  | l : Z.abs (F - f (X.part+ y)) <= Z.abs (F - f y)
                      => =_<= (pmap (\lam w => Z.abs (w - _)) (inv (Z.join_<= F>=0) *> Z.join-comm) *> pmap (\lam w => Z.abs (Z.part+ F - w)) e) <=∘ Z.join-lip
                          <=∘ =_<= (pmap (_ +) (pmap Z.abs Z.negative-right *> Z.abs_zro) *> Z.zro-right)
          \in inP (X.part+ y, X.join-right, Z.integral-mono l <∘r n)
      }

    \lemma thirds {eps : Rat} : Real.fromRat eps = Real.fromRat (eps * ratio 1 3) RealAbGroup.+ Real.fromRat (eps * ratio 1 3) RealAbGroup.+ Real.fromRat (eps * ratio 1 3)
      => pmap Real.fromRat (RatField.<=-antisymmetric linarith linarith) *> inv (pmap (RealAbGroup.+ _) RealAbGroup.+-rat *> RealAbGroup.+-rat)

    \lemma third-arith1 {a b c d : Real} {eps : Rat} (eps>0 : RatField.zro RatField.< eps) (t1 : a RealAbGroup.<= b RealAbGroup.+ c) (t2 : c RealAbGroup.<= d)
                        (h : b RealAbGroup.< Real.fromRat (eps * ratio 1 3)) (Fx : d RealAbGroup.< Real.fromRat (eps * ratio 1 3)) : a RealAbGroup.< Real.fromRat eps
      => \have | e => thirds {eps}
               | p : RealAbGroup.zro RealAbGroup.< Real.fromRat (eps * ratio 1 3) => rat_real_<.1 (third>0 eps>0)
         \in linarith

    \lemma third-arith2 {a b c d e g : Real} {eps : Rat} (t1 : a RealAbGroup.<= b RealAbGroup.+ c) (h : b RealAbGroup.< d RealAbGroup.+ Real.fromRat (eps * ratio 1 3))
                        (t2 : d RealAbGroup.<= e RealAbGroup.+ g) (t3 : g RealAbGroup.<= c) (Fx : c RealAbGroup.< Real.fromRat (eps * ratio 1 3)) : a RealAbGroup.< e RealAbGroup.+ Real.fromRat eps
      => \have e' => thirds {eps}
         \in linarith
  }

\instance RealCompleteIntegrationSpace : CompleteRegularIntegrationSpace Real
  | RegularIntegrationSpace => RealIntegrationSpace
  | norm-ext => RealNormed.norm-ext
  | isStronglyComplete => RealNormed.isStronglyComplete

\instance IntegrationSpaceCompletion (X : IntegrationSpace) : CompleteIntegrationSpace
  | IntegrationSpace => integration
  | norm-ext p => (ExNormedAbGroupCompletion X).norm-ext (norm-eq *> p)
  | isStronglyComplete => metric-stronglyComplete {integration} (Completion X).isComplete
  \where {
    \open ProductCoverSpace
    \open CoverMap
    \open TopAbGroupCompletion

    \type \infix 4 <= (F G : RegularCauchyFilter X) => join-func F G = G

    \func integration : IntegrationSpace (RegularCauchyFilter X) \cowith
      | TopAbGroup => TopAbGroupCompletion X
      | isDivisible a {n} n/=0 => inP (lift1 (X.*q-right-uniform {RatField.finv n}) a, inv *n-cover-char *> unique1 (*n-cover n ∘ lift1 X.*q-right-uniform) id
          \lam x => pmap (*n-cover n) lift1-char *> *n-cover-point *> pmap pointCF (inv X.*q_*n *> inv X.*q-assoc *> pmap (\lam q => q X.*q x) (RatField.finv-right $ natRat/=0 n/=0) *> X.ide_*q))
      | <= => <=
      | <=-refl => join-idemp
      | <=-transitive => <=-trans
      | <=-antisymmetric p q => inv q *> join-comm *> p
      | <=_+ p q => <=-trans (join-+-right *> pmap (+-func __ _) p) (join-+-left *> pmap (+-func _) q)
      | join => join-func
      | join-left => inv join-assoc *> pmap (join-func __ _) join-idemp
      | join-right {F} {G} => inv join-assoc *> pmap (join-func __ _) join-comm *> join-assoc *> pmap (join-func F) join-idemp
      | join-univ p q => join-assoc *> pmap (join-func _) q *> p
      | trunc => trunc-cover
      | trunc_zro>=0 => pmap (join-func _) trunc-char *> join-char *> pmap pointCF (X.join_<= X.trunc_zro>=0) *> inv trunc-char
      | trunc-bound => unique1 (join-cover ∘ tuple trunc-cover id) id
          \lam x => pmap (join-func __ _) trunc-char *> join-char *> pmap pointCF (X.join_<= X.trunc-bound)
      | trunc-assoc => unique2 (join-cover ∘ tuple (meet-cover ∘ prod id trunc-cover) (trunc-cover ∘ proj1)) (trunc-cover ∘ proj1)
          \lam x y => pmap2 join-func (pmap (meet-func _) trunc-char *> meet-char) trunc-char *> join-char *> pmap pointCF (X.join_<= X.trunc-assoc) *> inv trunc-char
      | integral => integral-cover
      | integral>=0 {F} p => transport (RealAbGroup.zro RealAbGroup.<=) (pmap integral-cover p) $ transport (RealAbGroup.zro RealAbGroup.<=) (integral-abs-pos (join-cover ∘ tuple (const (pointCF X.zro)) id) (\lam x => transportInv (RealAbGroup.zro RealAbGroup.<=) (pmap integral-cover join-char *> integral-char) (X.integral>=0 join-left)) {F}) RealAbGroup.abs>=0
      | integral_+ {F} {G} => dense-lift-unique (prod completion completion) (ProductTopSpace.prod.isDense completion.isDenseEmbedding.1 completion.isDenseEmbedding.1)
          (integral-cover ∘ +-cover) (RealNormed.+-cont ContMap.∘ prod integral-cover integral-cover) (\lam s => pmap integral-cover +-char *> integral-char *> X.integral_+ *> inv (pmap2 (RealAbGroup.+) integral-char integral-char)) (F,G)
      | dist-uniform {C} => transport (\lam (d : RegularCauchyFilter X -> RegularCauchyFilter X -> ExUpperReal) => isUniform {UniformCompletion X} C <-> ∃ (eps : Rat) (0 RatField.< eps) ∀ x ∃ (U : C) ∀ {y} ((d x y).U eps -> U y)) {(ExMetricCompletion X).dist}
          (ext \lam F G => norm-dist {ExNormedAbGroupCompletion X} *> norm-eq) ((ExMetricCompletion X).dist-uniform {C})

    \sfunc join-cover : CoverMap (Completion X `ProductCoverSpace` Completion X) (Completion X)
      => lift2 X.join-uniform

    \func join-func (F G : RegularCauchyFilter X) => join-cover (F,G)

    \lemma join-char {x y : X} : join-cover (pointCF x, pointCF y) = pointCF (x ∨ y)
      => rewrite (\peval join-cover) lift2-char

    \func meet-cover : CoverMap (Completion X `ProductCoverSpace` Completion X) (Completion X)
      => negative-cover ∘ join-cover ∘ prod negative-cover negative-cover

    \func meet-func (F G : RegularCauchyFilter X) => meet-cover (F,G)

    \lemma meet-char {x y : X} : meet-func (pointCF x) (pointCF y) = pointCF (x ∧ y)
      => pmap negative-cover (pmap2 join-func negative-char negative-char *> join-char) *> negative-char

    \func abs-cover : CoverMap (Completion X) (Completion X)
      => join-cover ∘ tuple id negative-cover

    \lemma abs-char {x : X} : abs-cover (pointCF x) = pointCF (X.abs x)
      => pmap (join-func _) negative-char *> join-char

    \sfunc trunc-cover : CoverMap (Completion X) (Completion X)
      => lift1 X.trunc-metric

    \lemma trunc-char {x : X} : trunc-cover (pointCF x) = pointCF (X.trunc x)
      => rewrite (\peval trunc-cover) lift1-char

    \sfunc integral-cover : CoverMap (Completion X) RealNormed
      => dense-lift completion completion.isDenseEmbedding X.integral-metric

    \lemma integral-char {x : X} : integral-cover (pointCF x) = X.integral x
      => rewrite (\peval integral-cover) (dense-lift-char completion.isDenseEmbedding x)

    \func *n-cover (n : Nat) : CoverMap (Completion X) (Completion X) \elim n
      | 0 => const (pointCF X.zro)
      | suc n => +-cover ∘ tuple (*n-cover n) id

    \lemma *n-cover-char {n : Nat} {F : RegularCauchyFilter X} : *n-cover n F = n AddMonoid.*n {TopAbGroupCompletion X} F \elim n
      | 0 => idp
      | suc n => pmap (+-func __ F) *n-cover-char

    \lemma *n-cover-point {n : Nat} {x : X} : *n-cover n (pointCF x) = pointCF (n X.*n x) \elim n
      | 0 => idp
      | suc n => pmap (+-func __ _) *n-cover-point *> +-char

    \lemma join-idemp {F : RegularCauchyFilter X} : join-func F F = F
      => unique1 (join-cover ∘ tuple id id) id \lam x => join-char *> pmap pointCF X.join-idemp

    \lemma join-comm {F G : RegularCauchyFilter X} : join-func F G = join-func G F
      => unique2 join-cover (join-cover ∘ tuple proj2 proj1) \lam x y => join-char *> pmap pointCF X.join-comm *> inv join-char

    \lemma join-assoc {F G H : RegularCauchyFilter X} : join-func (join-func F G) H = join-func F (join-func G H)
      => unique3 (join-cover ∘ prod join-cover id) (join-cover ∘ tuple (proj1 ∘ proj1) (join-cover ∘ prod proj2 id))
          \lam x y z => pmap (join-func __ _) join-char *> join-char *> pmap pointCF X.join-assoc *> inv (pmap (join-func _) join-char *> join-char)

    \lemma join-+-left {F G H : RegularCauchyFilter X} : join-func (+-func F G) (+-func F H) = +-func F (join-func G H)
      => unique3 (join-cover ∘ tuple (+-cover ∘ proj1) (+-cover ∘ tuple (proj1 ∘ proj1) proj2)) (+-cover ∘ tuple (proj1 ∘ proj1) (join-cover ∘ prod proj2 id))
          \lam x y z => pmap2 join-func +-char +-char *> join-char *> pmap pointCF (inv X.join_+-left) *> inv (pmap (+-func _) join-char *> +-char)

    \lemma join-+-right {F G H : RegularCauchyFilter X} : join-func (+-func F H) (+-func G H) = +-func (join-func F G) H
      => unique3 (join-cover ∘ tuple (+-cover ∘ tuple (proj1 ∘ proj1) proj2) (+-cover ∘ tuple (proj2 ∘ proj1) proj2)) (+-cover ∘ prod join-cover id)
          \lam x y z => pmap2 join-func +-char +-char *> join-char *> pmap pointCF (inv X.join_+-right) *> inv (pmap (+-func __ _) join-char *> +-char)

    \lemma <=-trans {F G H : RegularCauchyFilter X} (p : F <= G) (q : G <= H) : F <= H
      => pmap (join-func F) (inv q) *> inv join-assoc *> pmap (join-func __ H) p *> q

    \lemma integral-abs-pos (c : CoverMap (Completion X) (Completion X)) (p : \Pi (x : X) -> RealAbGroup.zro RealAbGroup.<= integral-cover (c (pointCF x))) {F : RegularCauchyFilter X}
      : RealAbGroup.abs (integral-cover (c F)) = integral-cover (c F)
      => dense-lift-unique completion completion.isDenseEmbedding.1 (lnorm-metric {RealNormed} ∘ integral-cover ∘ c) (integral-cover ∘ c) (\lam x => RealAbGroup.abs-ofPos (p x)) F

    \lemma norm-eq {F : RegularCauchyFilter X} : (ExNormedAbGroupCompletion X).norm F = {ExUpperReal} integral-cover (abs-cover F)
      => completion-lift-unique {X} {ExUpperRealMetric} (norm-metric-map {ExNormedAbGroupCompletion X}) (norm-metric-map {RealNormed} ∘ integral-cover ∘ abs-cover)
          (\lam x => completion-exNormed-isometry.func-norm-isometry *> Real.=-upper.1 (inv $ pmap (\lam z => RealAbGroup.abs (integral-cover z)) abs-char *> pmap RealAbGroup.abs integral-char *> RealAbGroup.abs-ofPos (X.integral>=0 X.abs>=0))) F
        *> Real.=-upper.1 (integral-abs-pos abs-cover \lam x => transportInv (RealAbGroup.zro RealAbGroup.<=) (pmap integral-cover abs-char *> integral-char) (X.integral>=0 X.abs>=0))
  }

\func integrationCompletion {X : IntegrationSpace} : NormedIsometricMap X (IntegrationSpaceCompletion X) \cowith
  | UniformMap => uniform-completion
  | func-+ => inv TopAbGroupCompletion.+-char
  | func-norm-isometry => Real.=-upper.1 $ pmap IntegrationSpaceCompletion.integral-cover IntegrationSpaceCompletion.abs-char *> IntegrationSpaceCompletion.integral-char
  \where {
    \lemma func-join {x y : X} : integrationCompletion (x ∨ y) = integrationCompletion x ∨ integrationCompletion y
      => inv IntegrationSpaceCompletion.join-char

    \lemma func-trunc {x : X} : integrationCompletion (X.trunc x) = trunc (integrationCompletion x)
      => inv IntegrationSpaceCompletion.trunc-char

    \lemma func-integral {x : X} : integral (integrationCompletion x) = X.integral x
      => IntegrationSpaceCompletion.integral-char

    \lemma func-<= {x y : X} (x<=y : x <= y) : integrationCompletion x <= integrationCompletion y
      => IntegrationSpaceCompletion.join-char *> pmap pointCF (X.join_<= x<=y)

    \protected \func inc : IntegrationSpaceHom X (IntegrationSpaceCompletion X) \cowith
      | AddGroupHom => integrationCompletion
      | func-<= => func-<=
      | func-join>= => =_<= func-join
      | func-trunc => func-trunc
      | func-integral => func-integral

    \lemma inc-dense : inc.IsDense
      => completion.isDenseEmbedding.1
  }

-- | The completion of a regular integration space is regular.
\func RegularIntegrationSpaceCompletion (X : RegularIntegrationSpace) : CompleteRegularIntegrationSpace \cowith
  | CompleteIntegrationSpace => IntegrationSpaceCompletion X
  | integral-trunc-small => RegularIntegrationSpace.integral-trunc-small {dense-regular integrationCompletion.inc integrationCompletion.inc-dense}
  | integral-trunc-large => RegularIntegrationSpace.integral-trunc-large {dense-regular integrationCompletion.inc integrationCompletion.inc-dense}

-- | Extension of a homomorphism along a dense homomorphism into a complete integration space.
\func dense-integration-lift {X Y : IntegrationSpace} {Z : CompleteIntegrationSpace} (f : IntegrationSpaceHom X Y) (fd : f.IsDense) (g : IntegrationSpaceHom X Z) : IntegrationSpaceHom Y Z \cowith
  | AddGroupHom => lift f fd g
  | func-<= p => transport (_ <=) (inv (lift-join f fd g) *> pmap (lift f fd g) (Y.join_<= p)) join-left
  | func-join>= => =_<= (lift-join f fd g)
  | func-trunc {y} => dense-lift-unique f fd (lift f fd g ContMap.∘ Y.trunc-metric) (Z.trunc-metric ContMap.∘ lift f fd g)
      (\lam x => pmap (lift f fd g) (inv f.func-trunc) *> char f fd g *> g.func-trunc *> pmap Z.trunc (inv (char f fd g))) y
  | func-integral {y} => dense-lift-unique f fd (Z.integral-metric ContMap.∘ lift f fd g) Y.integral-metric
      (\lam x => pmap Z.integral (char f fd g) *> g.func-integral *> inv f.func-integral) y
  \where {
    \func lift {X Y : IntegrationSpace} {Z : CompleteIntegrationSpace} (f : IntegrationSpaceHom X Y) (fd : f.IsDense) (g : IntegrationSpaceHom X Z) : NormedAbGroupMap Y Z
      => dense-normed-lift f fd g

    \lemma char {X Y : IntegrationSpace} {Z : CompleteIntegrationSpace} (f : IntegrationSpaceHom X Y) (fd : f.IsDense) (g : IntegrationSpaceHom X Z) {x : X} : lift f fd g (f x) = g x
      => dense-normed-lift.char f fd g

    \lemma lift-join {X Y : IntegrationSpace} {Z : CompleteIntegrationSpace} (f : IntegrationSpaceHom X Y) (fd : f.IsDense) (g : IntegrationSpaceHom X Z) {y y' : Y}
      : lift f fd g (y ∨ y') = lift f fd g y ∨ lift f fd g y'
      => dense-lift-unique (ProductCoverSpace.prod f f) (ProductTopSpace.prod.isDense {_} {_} {_} {_} {f} {f} fd fd)
          (lift f fd g ContMap.∘ Y.join-uniform) (Z.join-uniform ContMap.∘ ProductCoverSpace.prod (lift f fd g) (lift f fd g))
          (\lam s => pmap (lift f fd g) (inv f.func-join) *> char f fd g *> g.func-join *> inv (pmap2 (∨) (char f fd g) (char f fd g))) (y,y')

    \lemma unique {X Y : IntegrationSpace} {Z : CompleteIntegrationSpace} {f : IntegrationSpaceHom X Y} (fd : f.IsDense) (h h' : IntegrationSpaceHom Y Z)
                   (p : \Pi (x : X) -> h (f x) = h' (f x)) (y : Y) : h y = h' y
      => dense-lift-unique f fd h h' p y
  }

-- | The universal property of the completion: a homomorphism into a complete integration space extends uniquely to the completion.
\sfunc integrationCompletion-lift {X : IntegrationSpace} {Z : CompleteIntegrationSpace} (g : IntegrationSpaceHom X Z) : IntegrationSpaceHom (IntegrationSpaceCompletion X) Z
  => dense-integration-lift integrationCompletion.inc integrationCompletion.inc-dense g
  \where {
    \lemma char {X : IntegrationSpace} {Z : CompleteIntegrationSpace} {g : IntegrationSpaceHom X Z} {x : X} : integrationCompletion-lift g (integrationCompletion x) = g x
      => pmap (\lam (h : IntegrationSpaceHom (IntegrationSpaceCompletion X) Z) => h (integrationCompletion x)) (\peval integrationCompletion-lift g) *> dense-integration-lift.char integrationCompletion.inc integrationCompletion.inc-dense g

    \lemma unique {X : IntegrationSpace} {Z : CompleteIntegrationSpace} (h h' : IntegrationSpaceHom (IntegrationSpaceCompletion X) Z)
                   (p : \Pi (x : X) -> h (integrationCompletion x) = h' (integrationCompletion x)) (F : IntegrationSpaceCompletion X) : h F = h' F
      => dense-integration-lift.unique integrationCompletion.inc-dense h h' p F
  }

-- | The homomorphism of completions induced by a homomorphism of integration spaces.
\sfunc integrationCompletion-map {X Y : IntegrationSpace} (f : IntegrationSpaceHom X Y) : IntegrationSpaceHom (IntegrationSpaceCompletion X) (IntegrationSpaceCompletion Y)
  => integrationCompletion-lift (integrationCompletion.inc IntegrationSpaceHom.∘ f)
  \where {
    \protected \lemma char {X Y : IntegrationSpace} {f : IntegrationSpaceHom X Y} {x : X} : integrationCompletion-map f (integrationCompletion x) = integrationCompletion (f x)
      => pmap (\lam (h : IntegrationSpaceHom (IntegrationSpaceCompletion X) (IntegrationSpaceCompletion Y)) => h (integrationCompletion x)) (\peval integrationCompletion-map f) *> integrationCompletion-lift.char
  }

-- | A homomorphism of integration spaces with dense image induces an isomorphism of completions.
\lemma integrationCompletion-map-equiv {X Y : IntegrationSpace} {f : IntegrationSpaceHom X Y} (fd : f.IsDense) : IsEquiv (integrationCompletion-map f)
  => transportInv (\lam (h : IntegrationSpaceHom (IntegrationSpaceCompletion X) (IntegrationSpaceCompletion Y)) => IsEquiv h) ((\peval integrationCompletion-map f) *> (\peval integrationCompletion-lift (integrationCompletion.inc IntegrationSpaceHom.∘ f))) (lift1-equiv (IntegrationSpaceHom.isometry-denseEmbedding fd))