Theory MetricSpace_ZF

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

    Copyright (C) 2020,2021 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
begin

textA 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 

textA 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)"

textWe 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)"

textA 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)}"

textNext 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.

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)"


text The next lemma shows the definition of the pseudometric in the notation used in the 
  metric_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

textThe values of the metric are 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
  
textThe 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

textIf 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
  
textA technical lemma that allows us to shorten some proofs: 

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

textIf a point $x$ is inside a disk $B$ and $m\leq R-d(c,x)$ 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 If we assume that the order on the group makes the positive set a meet semi-lattice (i.e.
  every two-element subset of $G_+$ has a greatest lower bound) then 
  the collection of disks centered at points of the space and with radii in the positive set 
  of the group satisfies the base condition. The meet semi-lattice assumption can be weakened 
  to "each two-element subset of $G_+$ has a lower bound in $G_+$", but we don't do that here. 

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

textDisks 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) = 0"
proof -
  { assume "disk(x,rx)disk(y,ry)  0"
    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

textUnions of disks form a topology, hence (pseudo)metric spaces are topological spaces. 

theorem (in pmetric_space) pmetric_is_top: 
  assumes  "r {down-directs} L+" 
  defines "B  cX. {disk(c,R). RL+}" 
  defines "T  {A. A  Pow(B)}"
  shows "T {is a topology}"  "B {is a base for} T"  "T = X"
proof -
  from assms  show  "T {is a topology}"  "B {is a base for} T" 
    using disks_form_base Top_1_2_T1 by auto
  then have "T = B" using Top_1_2_L5 by simp
  moreover have "B = X"
  proof
    from assms(2) show "B  X" using disk_definition by auto
    { fix x assume "xX"
      from assms(1) obtain R where "RL+" unfolding DownDirects_def by blast
      with assms(2) xX have "x  B" using center_in_disk by auto
    } thus "X  B" by auto
  qed 
  ultimately show "T = X" by simp
qed

textTo 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"

textIn 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

textDistance 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

textAn ordered loop valued metric space is $T_2$ (i.e. Hausdorff).

theorem (in metric_space) metric_space_T2:
    assumes "r {down-directs} L+"
    defines "B  cX. {disk(c,R). RL+}" 
    defines "T  {A. A  Pow(B)}"
    shows "T {is T2}"
proof -
  { fix x y assume "xT" "yT" "xy"
    from assms have "BT" using pmetric_is_top(2) base_sets_open by auto
    moreover have "UB. VB. xU  yV  UV = 0"
    proof -
      let ?R = "d`x,y"
      from assms have "T = X" using pmetric_is_top(3) by simp
      with xT yT have "xX" "yX" by auto
      with xy have "?RL+" using dist_pos by simp
      with assms(2) xX yX have "disk(x,?R)  B" and "disk(y,?R)  B"
        by auto
      { assume "disk(x,?R)  disk(y,?R) = 0"
        moreover from assms(2) 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 "UB. VB. xU  yV  UV = 0" by auto
      }
      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) 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 assms(2) 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 "UB. VB. xU  yV  UV = 0" by auto
      }
      ultimately show ?thesis by auto
    qed
    ultimately have "UT. VT. xU  yV  UV = 0" by auto
  } then show ?thesis unfolding isT2_def by simp
qed

end