Theory MetricSpace_ZF

(* 
    This file is a part of IsarMathLib - 
    a library of formalized mathematics written for Isabelle/Isar.

    Copyright (C) 2020 - 2024 Slawomir Kolodynski

    This program is free software; Redistribution and use in source and binary forms, 
    with or without modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation and/or 
   other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *)

section ‹ Metric spaces ›

theory MetricSpace_ZF imports Topology_ZF_1 OrderedLoop_ZF Lattice_ZF UniformSpace_ZF
begin

text‹A metric space is a set on which a distance between points is defined as a function
  $d:X \times X \to [0,\infty)$. With this definition each metric space is a topological space 
  which is paracompact and Hausdorff ($T_2$), hence normal (in fact even perfectly normal).›

subsection‹ Pseudometric - definition and basic properties ›

text‹A metric on $X$ is usually defined as a function $d:X \times X \to [0,\infty)$ that satisfies
  the conditions $d(x,x) = 0$, $d(x, y) = 0 \Rightarrow  x = y$ (identity of indiscernibles), 
  $d(x, y)  = d(y, x)$ (symmetry) and $d(x, y) \le d(x, z) + d(z, y)$ (triangle inequality) 
  for all $x,y \in X$.  Here we are going to be a bit more general and define metric and 
  pseudo-metric as a function valued in an ordered loop. ›

text‹ First we define a pseudo-metric, which has the axioms of a metric, but without the second part
  of the identity of indiscernibles. In our definition IsApseudoMetric› is a predicate on five sets: the function $d$, 
  the set $X$ on which the metric is defined, the loop carrier $G$, the loop operation $A$ 
  and the order $r$ on $G$.›

definition 
  "IsApseudoMetric(d,X,G,A,r)  d:X×X  Nonnegative(G,A,r) 
     (xX. d`x,x = TheNeutralElement(G,A))
     (xX.yX. d`x,y = d`y,x)
     (xX.yX.zX. d`x,z, A`d`x,y,d`y,z  r)"

text‹We add the full axiom of identity of indiscernibles to the definition of a pseudometric
  to get the definition of metric.›

definition 
  "IsAmetric(d,X,G,A,r)  
    IsApseudoMetric(d,X,G,A,r)  (xX.yX. d`x,y = TheNeutralElement(G,A)  x=y)"

text‹A disk is defined as set of points located less than the radius from the center.›

definition "Disk(X,d,r,c,R)  {xX. d`c,x,R  StrictVersion(r)}"

text‹We define metric topology› as consisting of unions of open disks.›

definition
  "MetricTopology(X,L,A,r,d)  {𝒜. 𝒜  Pow(cX. {Disk(X,d,r,c,R). RPositiveSet(L,A,r)})}"

text‹Next we define notation for metric spaces. We will reuse the additive notation defined in 
  the loop1› locale adding only the assumption about $d$ being a pseudometric and notation
  for a disk centered at $c$ with radius $R$.
  Since for many theorems it is sufficient to assume the pseudometric axioms we will
  assume in this context that the sets $d,X,L,A,r$ form a pseudometric raher than a metric.
  In the pmetric_space› context $\tau$ denotes the topology defined by the metric $d$. 
  Analogously to the notation defined in the topology0› context int(A)›,
  cl(A)›, ∂A› will denote the interior, closure and boundary of the set $A$
  with respect to the metric topology. ›

locale pmetric_space =  loop1 +
  fixes d and X 
  assumes pmetricAssum: "IsApseudoMetric(d,X,L,A,r)"
  fixes disk
  defines disk_def [simp]: "disk(c,R)  Disk(X,d,r,c,R)"
  fixes pmettop ("τ") 
  defines pmettop [simp]: "τ  MetricTopology(X,L,A,r,d)"
  fixes interior ("int")
  defines interior_def [simp]: "int(D)  Interior(D,τ)"
  fixes cl
  defines cl_def [simp]: "cl(D)  Closure(D,τ)"
 

text‹ The next lemma shows the definition of the pseudometric in the notation used in the 
  pmetric_space› context.›

lemma (in pmetric_space) pmetric_properties: shows 
  "d: X×X  L+"
  "xX. d`x,x = 𝟬"
  "xX.yX. d`x,y = d`y,x"
  "xX.yX.zX. d`x,z \<lsq> d`x,y \<ra> d`y,z"
  using pmetricAssum unfolding IsApseudoMetric_def by auto

text‹The values of the metric are in the in the nonnegative set of the loop, 
  hence in the loop.›

lemma (in pmetric_space) pmetric_loop_valued: assumes "xX" "yX"
  shows "d`x,y  L+" "d`x,y  L"
proof -
  from assms show "d`x,y  L+" using pmetric_properties(1) apply_funtype
    by simp
  then show "d`x,y  L" using Nonnegative_def by auto
qed
  
text‹The definition of the disk in the notation used in the pmetric_space› context:›

lemma (in pmetric_space) disk_definition: shows "disk(c,R) = {xX. d`c,x \<ls> R}"
proof -
  have "disk(c,R) = Disk(X,d,r,c,R)" by simp
  then have "disk(c,R) = {xX. d`c,x,R  StrictVersion(r)}" 
    unfolding Disk_def by simp
  moreover have "xX. d`c,x,R  StrictVersion(r)  d`c,x \<ls> R"
    using def_of_strict_ver by simp
  ultimately show ?thesis by auto
qed

text‹If the radius is positive then the center is in disk.›

lemma (in pmetric_space) center_in_disk: assumes "cX" and "RL+" shows "c  disk(c,R)"
  using pmetricAssum assms IsApseudoMetric_def PositiveSet_def disk_definition by simp
  
text‹A technical lemma that allows us to shorten some proofs: if $c$ is an element
  of $X$ and $x$ is in disk with center $c$ and radius $R$ then $R$ is a positive element of 
  $L$ and $-d(x,y)+R$ is in the set of positive elements of the loop. ›

lemma (in pmetric_space) radius_in_loop: assumes "cX" and "x  disk(c,R)"
  shows "RL" "𝟬\<ls>R" "RL+" "(\<rm>d`c,x \<ad> R)  L+"
proof -
  from assms(2) have "xX" and "d`c,x \<ls> R" using disk_definition by auto
  with assms(1) show "𝟬\<ls>R" using pmetric_properties(1) apply_funtype 
      nonneg_definition loop_strict_ord_trans by blast
  then show "RL" and "RL+" using posset_definition PositiveSet_def by auto
  from d`c,x \<ls> R show "(\<rm>d`c,x \<ad> R)  L+"
    using ls_other_side(2) by simp
qed

text‹If a point $x$ is inside a disk $B$ and $m\leq -d\langle c,x\rangle + R$ then the disk centered 
  at the point $x$ and with radius $m$ is contained in the disk $B$. ›

lemma (in pmetric_space) disk_in_disk: 
  assumes "cX"  and "x  disk(c,R)" and "m \<lsq> (\<rm>d`c,x \<ad> R)"
  shows "disk(x,m)  disk(c,R)"
proof
  fix y assume "y  disk(x,m)"
  then have "d`x,y \<ls> m" using disk_definition by simp
  from assms(1,2) y  disk(x,m) have "RL" "xX" "yX" 
    using radius_in_loop(1) disk_definition by auto
  with assms(1) have "d`c,y \<lsq> d`c,x \<ra> d`x,y" using pmetric_properties(4) by simp
  from assms(1) xX have "d`c,x  L" 
    using pmetric_properties(1) apply_funtype nonneg_subset by auto
  with d`x,y \<ls> m assms(3) have "d`c,x \<ra> d`x,y \<ls> d`c,x \<ra> (\<rm>d`c,x \<ad> R)"
    using loop_strict_ord_trans1 strict_ord_trans_inv(2) by blast
  with d`c,x  L RL d`c,y \<lsq> d`c,x \<ra> d`x,y yX show "y  disk(c,R)"
    using lrdiv_props(6) loop_strict_ord_trans disk_definition by simp
qed

text‹A special case of disk_in_disk› where we set $m = -d\langle c,x\rangle + R$: 
  if $x$ is an element of a disk with center $c\in X$
  and radius $R$ then this disk contains the disk centered at $x$ and with radius 
  $-d\langle c,x\rangle + R$. ›

lemma (in pmetric_space) disk_in_disk1: 
  assumes "cX"  and "x  disk(c,R)"
  shows "disk(x,\<rm>d`c,x \<ad> R)  disk(c,R)"
proof -
  from assms(2) have "RL" and "d`c,x  L"
    using disk_definition less_members by auto
  with assms show ?thesis using left_right_sub_closed(1) loop_ord_refl disk_in_disk
    by simp
qed

text‹Assuming that two disks have the same center, closed disk with smaller radius
  in contained in the (open) disk with a larger radius. ›

lemma (in pmetric_space) disk_radius_strict_mono:
  assumes "r1 \<ls> r2" 
  shows "{yX. d`x,y \<lsq> r1}  disk(x,r2)"
  using assms loop_strict_ord_trans disk_definition by auto

text‹ If we assume that the loop's order relation down-directs $L_+$ then
  the collection of disks centered at points of the space and with radii in the positive set 
  of the loop satisfies the base condition. The property that an order relation "down-directs"
  a set is defined in Order_ZF› and means that every two-element subset of the set 
  has a lower bound in that set. ›

lemma (in pmetric_space) disks_form_base: 
  assumes "r {down-directs} L+"
  defines "B  cX. {disk(c,R). RL+}"
  shows "B {satisfies the base condition}"
proof -
  { fix U V assume "UB" "VB"
    fix x assume "xUV"
    have "WB. xW  WUV"
    proof -
      from assms(2) UB VB obtain cU  cV RU  RV 
        where "cU  X" "RU  L+" "cV  X" "RV  L+" "U = disk(cU,RU)" "V = disk(cV,RV)"
        by auto
      with xUV have "x  disk(cU,RU)" and "x  disk(cV,RV)" by auto
      then have "xX" "d`cU,x \<ls> RU" "d`cV,x \<ls> RV" using disk_definition by auto
      let ?mU = "\<rm> d`cU,x \<ad> RU"
      let ?mV = "\<rm> d`cV,x \<ad> RV"
      from cUX xdisk(cU,RU) cVX xdisk(cV,RV) have "?mUL+" and "?mVL+" 
        using radius_in_loop(4) by auto
      with assms(1) obtain m where "m  L+" "m \<lsq> ?mU" "m \<lsq> ?mV"
        unfolding DownDirects_def by auto
      let ?W = "disk(x,m)"
      from m  L+ m \<lsq> ?mU m \<lsq> ?mV 
        cU  X x  disk(cU,RU) cV  X x  disk(cV,RV) 
        U = disk(cU,RU) V = disk(cV,RV)
        have "?W  UV" using disk_in_disk by blast
      moreover from assms(2) xX m  L+ have "?W  B" and "x?W" using center_in_disk
        by auto
      ultimately show ?thesis by auto
    qed      
  } then show ?thesis unfolding SatisfiesBaseCondition_def by auto
qed

text‹Disks centered at points farther away than the sum of radii do not overlap. ›

lemma (in pmetric_space) far_disks:
  assumes "xX" "yX"  "rx\<ra>ry \<lsq> d`x,y"
  shows "disk(x,rx)disk(y,ry) = "
proof -
  { assume "disk(x,rx)disk(y,ry)  "
    then obtain z where "z  disk(x,rx)disk(y,ry)" by auto
    then have "zX" and "d`x,z \<ra> d`y,z \<ls> rx\<ra>ry"
      using disk_definition add_ineq_strict by auto
    moreover from assms(1,2) zX have "d`x,y \<lsq> d`x,z \<ra> d`y,z"
      using pmetric_properties(3,4) by auto
    ultimately have "d`x,y \<ls> rx\<ra>ry" using loop_strict_ord_trans 
      by simp
    with assms(3) have False using loop_strict_ord_trans by auto
  } thus ?thesis by auto
qed

text‹ If we have a loop element that is smaller than the distance between two points, then
  we can separate these points with disks.›

lemma (in pmetric_space) disjoint_disks:
  assumes "xX" "yX" "rx\<ls>d`x,y"
  shows "(\<rm>rx\<ad>(d`x,y))  L+" and "disk(x,rx)disk(y,\<rm>rx\<ad>(d`x,y)) = 0"
proof -
  from assms(3) show "(\<rm>rx\<ad>(d`x,y))  L+"
    using ls_other_side posset_definition1 by simp
  from assms(1,2,3) have "rxL" and "d`x,y  L" 
    using less_members(1) pmetric_loop_valued(2) by auto
  then have "rx\<ra>(\<rm>rx\<ad>(d`x,y)) = d`x,y" using lrdiv_props(6) by simp
  with assms(1,2) d`x,y  L show "disk(x,rx)disk(y,\<rm>rx\<ad>(d`x,y)) = 0"
    using loop_ord_refl far_disks by simp
qed

text‹The definition of metric topology written in notation of pmetric_space› context:›

lemma (in pmetric_space) metric_top_def_alt:
  defines "B  cX. {disk(c,R). RL+}"
  shows "τ = {A. A  Pow(B)}"
proof -
  from assms have "MetricTopology(X,L,A,r,d) =  {A. A  Pow(B)}"
    unfolding MetricTopology_def by simp
  thus ?thesis by simp
qed

text‹If the order of the loop down-directs its set of positive elements 
  then the metric topology defined as collection of unions of (open) disks is indeed a topology.
  Recall that in the pmetric_space› context $\tau$ denotes the metric topology. ›

theorem (in pmetric_space) pmetric_is_top: 
  assumes  "r {down-directs} L+"  
  shows "τ {is a topology}"
  using assms disks_form_base Top_1_2_T1 metric_top_def_alt by simp

text‹If $r$ down-directs $L_+$ then the collection of open disks is a base for
  the metric topology.›

theorem (in pmetric_space) disks_are_base:
  assumes  "r {down-directs} L+" 
  defines "B  cX. {disk(c,R). RL+}"
  shows "B {is a base for} τ"
  using assms disks_form_base Top_1_2_T1 metric_top_def_alt by simp

text‹If $r$ down-directs $L_+$ then $X$ is the carrier of metric topology.›

theorem (in pmetric_space) metric_top_carrier: 
  assumes  "r {down-directs} L+" shows "τ = X"
proof -
  let ?B = "cX. {disk(c,R). RL+}"
  from assms have "τ = ?B"
    using disks_are_base Top_1_2_L5 by simp
  moreover have "?B = X"
  proof
    show "?B  X" using disk_definition by auto
    from assms show "X  ?B" unfolding DownDirects_def using center_in_disk 
      by blast
  qed 
  ultimately show "τ = X" by simp
qed

text‹Under the assumption that $r$ down-directs $L_+$ the propositions proven
  in the topology0› context can be used in the pmetric_space› context.›

lemma (in pmetric_space) topology0_valid_in_pmetric_space:
  assumes  "r {down-directs} L+" 
  shows "topology0(τ)" 
  using assms pmetric_is_top unfolding topology0_def by simp

text‹If $r$ down-directs $L_+$ then disks are open in the metric topology.›

lemma (in pmetric_space) disks_open: 
  assumes "cX" "RL+" "r {down-directs} L+"
  shows "disk(c,R)  τ"
  using assms base_sets_open disks_are_base(1) pmetric_is_top 
    by blast

text‹To define the metric_space› locale we take the pmetric_space› and add 
  the assumption of identity of indiscernibles.›

locale metric_space =  pmetric_space +
  assumes ident_indisc: "xX. yX. d`x,y=𝟬  x=y"

text‹In the metric_space› locale $d$ is a metric.›

lemma (in metric_space) d_metric: shows "IsAmetric(d,X,L,A,r)"
  using pmetricAssum ident_indisc unfolding IsAmetric_def by simp

text‹Distance of different points is greater than zero. ›

lemma (in metric_space) dist_pos: assumes "xX" "yX" "xy"
  shows "𝟬\<ls>d`x,y" "d`x,y  L+"
proof -
  from assms(1,2) have "d`x,y  L+" 
    using pmetric_properties(1) apply_funtype by simp  
  then have "𝟬 \<lsq> d`x,y" using Nonnegative_def by auto
  with assms show "d`x,y  L+" and "𝟬\<ls>d`x,y" 
    using ident_indisc posset_definition posset_definition1 by auto
qed

text‹If $r$ down-directs $L_+$ then the ordered loop valued metric space is $T_2$ (i.e. Hausdorff).›

theorem (in metric_space) metric_space_T2:
    assumes "r {down-directs} L+" 
    shows "τ {is T2}"
proof -
  let ?B = "cX. {disk(c,R). RL+}"
  { fix x y assume "xτ" "yτ" "xy"
    from assms have "?Bτ" using metric_top_def_alt by auto
    have "U?B. V?B. xU  yV  UV = "
    proof -
      let ?R = "d`x,y" 
      from assms have "τ = X" using metric_top_carrier by simp
      with xτ have "xX" by blast
      from τ = X yτ have "yX" by blast
      with xy xX have "?RL+" using dist_pos by simp
      with xX yX have "disk(x,?R)  ?B" and "disk(y,?R)  ?B"
        by auto
      { assume "disk(x,?R)  disk(y,?R) = "
        moreover from xX yX ?RL+ have 
            "disk(x,?R)?B" "disk(y,?R)?B" "xdisk(x,?R)" "ydisk(y,?R)"
          using center_in_disk by auto
        ultimately have "U?B. V?B. xU  yV  UV = 0" by blast
      }
      moreover
      { assume "disk(x,?R)  disk(y,?R)  0"
        then obtain z where "z  disk(x,?R)" and "z  disk(y,?R)" 
          by auto
        then have "d`x,z \<ls> ?R" using disk_definition by simp
        then have "𝟬 \<ls> \<rm>d`x,z\<ad>?R" using ls_other_side(1) by simp
        let ?r = "\<rm>d`x,z\<ad>?R"
        have "?r\<ls>?R"
        proof -
          from z  disk(y,?R) xX yX have "zX" "xz"
            using disk_definition pmetric_properties(3) by auto
          with xX yX zX show ?thesis
            using pmetric_loop_valued dist_pos(1) add_subtract_pos(2) by simp 
        qed
        with xX yX have "disk(x,?r)disk(y,\<rm>?r\<ad>?R) = 0"
          by (rule disjoint_disks)
        moreover 
        from 𝟬\<ls>?r ?r\<ls>?R have "?rL+" "(\<rm>?r\<ad>?R)  L+"
          using ls_other_side posset_definition1 by auto
        with xX yX have 
            "disk(x,?r)?B" "disk(y,\<rm>?r\<ad>(d`x,y))?B" and
            "xdisk(x,?r)" "ydisk(y,\<rm>?r\<ad>(d`x,y))"
          using center_in_disk by auto
        ultimately have "U?B. V?B. xU  yV  UV = 0" by blast
      }
      ultimately show ?thesis by auto
    qed
    with ?Bτ have "Uτ. Vτ. xU  yV  UV = " by (rule exist2_subset)
  } then show ?thesis unfolding isT2_def by simp
qed

subsection‹Uniform structures on metric spaces›

text‹Each pseudometric space with pseudometric $d:X\times X\rightarrow L$ 
  supports a natural uniform structure, defined as supersets of the collection
  of inverse images $U_c = d^{-1}([0,c])$, where $c>0$.  ›

text‹In the following definition $X$ is the underlying space, $L$ is the loop (carrier),
  $A$ is the loop operation, $r$ is an order relation compatible with $A$,
  and $d$ is a pseudometric on $X$, valued in the ordered loop $L$.
  With this we define the uniform gauge as the collection of inverse images
  of the closed intervals $[0,c]$ as $c$ varies of the set of positive elements of $L$.›

definition
  "UniformGauge(X,L,A,r,d)  {d-``({bNonnegative(L,A,r). b,c  r}). cPositiveSet(L,A,r)}"

text‹In the pmetric_space› context we will write UniformGauge(X,L,A,r,d)› as 𝔅›. ›

abbreviation (in pmetric_space) gauge ("𝔅") where "𝔅  UniformGauge(X,L,A,r,d)"

text‹In notation defined in the pmetric_space› context we can write the uniform gauge
  as $\{ d^{-1}(\{c\in L^+: c\leq b\}: b \in L_+ \}$.  ›

lemma (in pmetric_space) uniform_gauge_def_alt: 
  shows "𝔅 = {d-``({cL+. c\<lsq>b}). bL+}"
  unfolding UniformGauge_def by simp

text‹Members of the uniform gauge are subsets of $X\times X$ i.e. relations on $X$. ›

lemma (in pmetric_space) uniform_gauge_relations: 
  assumes "B𝔅" shows "BX×X"
  using assms uniform_gauge_def_alt pmetric_properties(1) func1_1_L3
  by force

text‹If the distance between two points of $X$ is less or equal $b$, then
  this pair of points is in $d^{-1}([0,b])$. ›

lemma (in pmetric_space) gauge_members: 
  assumes "xX" "yX" "d`x,y \<lsq> b"
  shows "x,y  d-``({cL+. c\<lsq>b})"
  using assms pmetric_properties(1) apply_funtype func1_1_L15
  by simp

text‹Suppose $b\in L^+$ (i.e. b is an element of the loop that is greater than the neutral element)
  and $x\in X$. Then the image of the singleton set $\{ x\}$ by the relation 
  $B=\{ d^{-1}(\{c\in L^+: c\leq b\}$ is the set $\{ y\in X:d\langle x,y\rangle  \leq b\}$,
  i.e. the closed disk with center $x$ and radius $b$. Hence the the image $B\{ x\}$ contains
  the open disk with center $x$ and radius $b$. ›

lemma (in pmetric_space) disk_in_gauge: 
  assumes "bL+" "xX" 
  defines "B  d-``({cL+. c\<lsq>b})"
  shows "B``{x} = {yX. d`x,y \<lsq> b}" and "disk(x,b)  B``{x}"
proof -
  from assms(1,3) have "BX×X" 
    using uniform_gauge_def_alt uniform_gauge_relations by auto
  with assms(2,3) show "B``{x} = {yX. d`x,y \<lsq> b}"
    using pmetric_properties(1) func1_1_L15 by force
  then show "disk(x,b)  B``{x}" using disk_definition by auto
qed

text‹Gauges corresponding to larger elements of the loop are larger. ›

lemma (in pmetric_space) uniform_gauge_mono: 
  assumes "b1\<lsq>b2" shows "d-``({cL+. c\<lsq>b1})  d-``({cL+. c\<lsq>b2})" 
  using ordLoopAssum assms vimage_mono1 
  unfolding IsAnOrdLoop_def IsPartOrder_def trans_def by auto

text‹For any two sets of the form $d^{-1}([0,b])$ we can find a third one that is contained
  in both. ›

lemma (in pmetric_space) gauge_1st_cond: 
  assumes "r {down-directs} L+" "B1𝔅" "B2𝔅"
  shows "B3𝔅. B3B1B2" 
proof -
  from assms(2,3) obtain b1 b2 where "b1L+" "b2L+" and 
    I: "B1 = d-``({cL+. c\<lsq>b1})" "B2 = d-``({cL+. c\<lsq>b2})"
    using uniform_gauge_def_alt by auto
  from assms(1) b1L+ b2L+ obtain b3 where "b3L+" "b3\<lsq>b1" "b3\<lsq>b2"
    unfolding DownDirects_def by auto
  from I b3\<lsq>b1 b3\<lsq>b2 have "d-``({cL+. c\<lsq>b3})  B1B2"
    using uniform_gauge_mono by blast
  with b3L+ show ?thesis using uniform_gauge_def_alt 
    by auto
qed

text‹Sets of the form $d^{-1}([0,b])$ contain the diagonal. ›

lemma (in pmetric_space) gauge_2nd_cond: assumes "B𝔅" shows "id(X)B"
proof
  fix p assume "pid(X)"
  then obtain x where "xX" and "p=x,x" by auto
  then have "pX×X" and "d`(p) = 𝟬" using pmetric_properties(2) by simp_all
  from assms obtain b where "bL+" and "B = d-``({cL+. c\<lsq>b})"
    using uniform_gauge_def_alt by auto
  with pX×X d`(p) = 𝟬 show "pB"
    using posset_definition1 loop_zero_nonneg pmetric_properties(1) func1_1_L15
    by simp
qed

text‹Sets of the form $d^{-1}([0,b])$ are symmetric.›

lemma (in pmetric_space) gauge_symmetric: 
  assumes "B𝔅" shows "B = converse(B)"
proof -
  from assms obtain b where "B = d-``({cL+. c\<lsq>b})"
    using uniform_gauge_def_alt by auto
  with pmetricAssum show ?thesis unfolding IsApseudoMetric_def
    using symm_vimage_symm by auto
qed

text‹A set of the form $d^{-1}([0,b])$ contains a symmetric set of this form.›

corollary (in pmetric_space) gauge_3rd_cond: 
  assumes "B1𝔅" shows "B2𝔅. B2  converse(B1)"
  using assms gauge_symmetric by auto

text‹The collection of sets of the form $d^{-1}([0,b])$ for $b\in L_+$ 
  is contained of the powerset of $X\times X$.›

lemma (in pmetric_space) gauge_5thCond: shows "𝔅Pow(X×X)"
  using uniform_gauge_def_alt pmetric_properties(1) func1_1_L3 by force

text‹If the set of positive values is non-empty, then there are sets
  of the form $d^{-1}([0,b])$ for $b>0$.›

lemma (in pmetric_space) gauge_6thCond: 
  assumes "L+" shows "𝔅"  using assms uniform_gauge_def_alt by simp

text‹The remaining 4th condition for the sets of the form $d^{-1}([0,b])$
  to be a uniform base (or a fundamental system of entourages) cannot be proven
  without additional assumptions in the context of ordered loop valued metrics. 
  To see that consider the example
  of natural numbers with the metric $d\langle x,y \rangle = |x-y|$, where we think
  of $d$ as valued in the nonnegative set of ordered group of integers.
  Now take the set $B_1 = d^{-1}([0,1]) = d^{-1}(\{ 0,1\} )$. Then the set $B_1 \circ B_1$ 
  is strictly larger than $B_1$, but there is no smaller set $B_2$ we can take so that
  $B_2 \circ B_2 \subseteq B_1$. 
  One condition that is sufficient is that for every $b_1 >0$ there is a $b_2 >0$
  such that $b_2 + b_2 \leq b_1 $. I have not found a standard name for this property, for now
  we will use the name IsHalfable›. ›

definition
  "IsHalfable(L,A,r)  b1PositiveSet(L,A,r). b2PositiveSet(L,A,r). A`b2,b2,b1  r"

text‹The property of halfability written in the notation used in the pmetric_space› context.›

lemma (in pmetric_space) is_halfable_def_alt: 
  assumes "IsHalfable(L,A,r)" "b1L+"
  shows "b2L+. b2\<ra>b2 \<lsq> b1"
  using assms unfolding IsHalfable_def by simp

text‹If the loop order is halfable then for every set $B_1$ of the form $d^{-1}([0,b_1])$ 
  for some $b_1>0$ we can find another one $B_2 = d^{-1}([0,b_2])$ such that $B_2$ 
  composed with itself is contained in $B_1$.›

lemma (in pmetric_space) gauge_4thCond: 
  assumes "IsHalfable(L,A,r)" "B1𝔅" shows "B2𝔅.B2𝔅. B2 O B2  B1"
proof -
  from assms(2) obtain b1 where "b1L+" and "B1 = d-``({cL+. c\<lsq>b1})"
    using uniform_gauge_def_alt by auto
  from assms(1) b1L+ obtain b2 where "b2L+" and "b2\<ra>b2 \<lsq> b1"
    using is_halfable_def_alt by auto
  let ?B2 = "d-``({cL+. c\<lsq>b2})"
  from b2L+ have "?B2𝔅" unfolding UniformGauge_def by auto
  { fix p assume "p  ?B2 O ?B2" 
    with ?B2𝔅 obtain x y where "xX" "yX" and "p=x,y"
      using gauge_5thCond by blast
    from p  ?B2 O ?B2 p=x,y obtain z where 
      "x,z  ?B2" and "z,y  ?B2"
      using rel_compdef by auto
    with ?B2𝔅 have "zX" using gauge_5thCond by auto
    from x,z  ?B2 z,y  ?B2 have "d`x,z \<ra> d`z,y \<lsq> b2\<ra> b2"
      using pmetric_properties(1) func1_1_L15 add_ineq by simp
    with b2\<ra>b2 \<lsq> b1 have "d`x,z \<ra> d`z,y \<lsq> b1"
      using loop_ord_trans by simp
    with xX yX zX p=x,y B1 = d-``({cL+. c\<lsq>b1}) have "pB1"
      using pmetric_properties(4) loop_ord_trans gauge_members by blast      
  } hence "?B2 O ?B2  B1" by auto
  with ?B2𝔅 show ?thesis by auto
qed

text‹If $X$ and $L_+$ are not empty, the order relation $r$
  down-directs $L_+$, and the loop order is halfable, then $\mathfrak{B}$
  (which in the pmetric_space› context is an abbreviation for 
  $\{ d^{-1}(\{c\in L^+: c\leq b\}: b \in L_+ \}$)
  is a fundamental system of entourages, hence its supersets 
  form a uniformity on $X$ and hence those supersets define a topology on $X$.›

theorem (in pmetric_space) metric_gauge_base: 
  assumes "X" "L+" "r {down-directs} L+" "IsHalfable(L,A,r)"
  shows 
    "𝔅 {is a uniform base on} X"
    "Supersets(X×X,𝔅) {is a uniformity on} X"
    "UniformTopology(Supersets(X×X,𝔅),X) {is a topology}"
    "UniformTopology(Supersets(X×X,𝔅),X) = X"
  using assms gauge_1st_cond gauge_2nd_cond gauge_3rd_cond 
    gauge_4thCond gauge_5thCond gauge_6thCond uniformity_base_is_base
    uniform_top_is_top
  unfolding IsUniformityBaseOn_def by simp_all

text‹At this point we know that a pseudometric induces two topologies: one consisting of unions
  of open disks (the metric topology) and second one being the uniform topology derived 
  from the uniformity generated the fundamental system of entourages (the base uniformity) 
  of the sets of the form $d^{-1}([0,b])$ for $b>0$.  
  The next theorem states that if $X$ and $L_+$ are not empty, $r$ down-directs $L_+$,
  and the loop order is halfable, then these two topologies are in fact the same. 
  Recall that in the pmetric_space› context $\tau$ denotes the metric topology. ›

theorem (in pmetric_space) metric_top_is_uniform_top:
  assumes "X" "L+" "r {down-directs} L+" "IsHalfable(L,A,r)"
  shows "τ = UniformTopology(Supersets(X×X,𝔅),X)"
proof
  let  = "Supersets(X×X,𝔅)"
  from assms have " {is a uniformity on} X" using metric_gauge_base
    by simp
  let ?T = "UniformTopology(,X)"
  { fix U assume "U?T"
    then have "UPow(X)" and I: "xU. U{V``{x}. V}"
      unfolding UniformTopology_def by auto
    { fix x assume "xU"
      with I obtain A where "A" and "U = A``{x}"
        by auto
      from xU U?T have "x?T" by auto
      with assms have "xX" using metric_gauge_base(4) by simp
      from A obtain B where "B𝔅" and "BA"
        unfolding Supersets_def by auto
      from B𝔅 obtain b where "bL+" and "B = d-``({cL+. c\<lsq>b})"
        using uniform_gauge_def_alt by auto
      with xX BA U = A``{x} have "disk(x,b)  U"
        using disk_in_gauge(2) by blast
      with assms(3) xX bL+ have "Vτ. xV  VU"
        using disks_open center_in_disk by force
    } with assms(3) have "Uτ"
      using topology0_valid_in_pmetric_space topology0.open_neigh_open
        by simp
  } thus "?T  τ" by auto
  let ?𝒟 = "cX. {disk(c,R). RL+}"
  { fix U assume "U  ?𝒟"
    then obtain c b where "cX" "bL+" "U = disk(c,b)"
      by blast
    { fix x assume "xU"
      let ?b1 = "\<rm>d`c,x \<ad> b"
      from xU cX U = disk(c,b) have 
        "xX" "xdisk(c,b)" "disk(x,?b1)  U" "?b1  L+"
        using disk_in_disk1 disk_definition radius_in_loop(4) by simp_all
      with assms(4) obtain b2 where "b2L+" and "b2\<ra>b2 \<lsq> ?b1"
        using is_halfable_def_alt by auto
      let ?D = "{yX. d`x,y \<lsq> b2}"
      from b2L+ b2\<ra>b2 \<lsq> ?b1 have "?D  disk(x,?b1)"
        using posset_definition1 positive_subset add_subtract_pos(3) 
          loop_strict_ord_trans1 disk_radius_strict_mono by blast 
      let ?B = "d-``({cL+. c\<lsq>b2})"
      from b2L+ have "?B𝔅" using uniform_gauge_def_alt by auto
      then have "?B" using uniform_gauge_relations superset_gen 
        by simp
      from b2L+ xX ?D  disk(x,?b1) disk(x,?b1)  U have "?B``{x}  U"
        using disk_in_gauge(1) by auto
      with ?B have "W. W``{x}  U" by auto
    } with U = disk(c,b)  {is a uniformity on} X have "U  ?T"
      using disk_definition uniftop_def_alt1 by auto
  } hence "?𝒟  ?T" by auto
  with assms show "τ?T"
    using disks_are_base(1) metric_gauge_base(3) base_smallest_top
    by simp
qed

end