# Theory Real_ZF

theory Real_ZF
imports Int_ZF_IML Ring_ZF_1
(*
This file is a part of IsarMathLib -
a library of formalized mathematics for Isabelle/Isar.

Copyright (C) 2005 - 2009  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.

HIS 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 ‹Construction real numbers - the generic part›

theory Real_ZF imports Int_ZF_IML Ring_ZF_1

begin

text{*The goal of the @{text "Real_ZF"} series of theory files is
to provide a contruction of the set of
real numbers. There are several ways to construct real numbers.
Most common start from the rational numbers and use Dedekind cuts
or Cauchy sequences. @{text "Real_ZF_x.thy"} series formalizes
an alternative
approach that constructs real numbers directly from the group of integers.
Our formalization is mostly based on \cite{Arthan2004}.
Different variants of this contruction are also
described in \cite{Campo2003} and \cite{Street2003}.
I recommend to read these papers, but for the impatient here is a short
description: we take a set of maps $s : Z\rightarrow Z$ such that
the set $\{s(m+n)-s(m)-s(n)\}_{n,m \in Z}$ is finite
($Z$ means the integers here). We call these maps slopes.
Slopes form a group with the natural addition
$(s+r)(n) = s(n)+r(n)$. The maps such that the set $s(Z)$ is finite
(finite range functions) form a subgroup of slopes.
The additive group of real numbers is defined as the
quotient group of slopes by the (sub)group of finite range functions.
The multiplication is defined as the projection of the composition of slopes
into the resulting quotient (coset) space.
*}

subsection{*The definition of real numbers*}

text{*This section contains the construction of the ring of real numbers
as classes of slopes - integer almost homomorphisms. The real definitions
are in @{text "Group_ZF_2"} theory, here we just specialize the definitions
of almost homomorphisms, their equivalence and operations to the
additive group of integers from the general case of abelian groups considered
in @{text "Group_ZF_2"}.*}

text{*The set of slopes is defined as the set of almost homomorphisms
on the additive group of integers.*}

definition

text{*The first operation on slopes (pointwise addition) is a special case
of the first operation on almost homomorphisms.*}

definition

text{*The second operation on slopes (composition) is a special case
of the second operation on almost homomorphisms.*}

definition

text{*Bounded integer maps are functions from integers
to integers that have finite range. They play a role of
zero in the set of real numbers we are constructing.*}

definition
"BoundedIntMaps ≡ FinRangeFunctions(int,int)"

text{*Bounded integer maps form a normal subgroup of slopes.
The equivalence relation on slopes is the (group) quotient
relation defined by this subgroup.
*}

definition
"SlopeEquivalenceRel ≡ QuotientGroupRel(Slopes,SlopeOp1,BoundedIntMaps)"

text{*The set of real numbers is the set of equivalence classes of
slopes.*}

definition
"RealNumbers ≡ Slopes//SlopeEquivalenceRel"

text{*The addition on real numbers is defined as the projection of
pointwise addition of slopes on the quotient. This means that
the additive group of real numbers is the quotient group:
the group of slopes (with pointwise addition) defined by the
normal subgroup of bounded integer maps.*}

definition

text{*Multiplication is defined as the projection of composition
of slopes on the quotient. The fact that it works is probably the
most surprising part of the construction. *}

definition
"RealMultiplication ≡ ProjFun2(Slopes,SlopeEquivalenceRel,SlopeOp2)"

text{*We first show that we can use theorems proven in some proof contexts
(locales). The locale @{text "group1"} requires assumption that we deal with
an abelian group. The next lemma allows to use all theorems proven
in the context called @{text "group1"}.*}

using group1_axioms.intro group1_def Int_ZF_1_T2 by simp

text{*Real numbers form a ring. This is a special case of the theorem
proven in @{text "Ring_ZF_1.thy"}, where we show the same in general for
almost homomorphisms rather than slopes.*}

proof -
let ?FR = "FinRangeFunctions(int,int)"
let ?R = "QuotientGroupRel(?AH,?Op1,?FR)"
let ?A = "ProjFun2(?AH,?R,?Op1)"
let ?M = "ProjFun2(?AH,?R,?Op2)"
have "IsAring(?AH//?R,?A,?M)" using Real_ZF_1_L1 group1.Ring_ZF_1_1_T1
by simp
then show ?thesis using Slopes_def SlopeOp2_def SlopeOp1_def
BoundedIntMaps_def SlopeEquivalenceRel_def RealNumbers_def
qed

text{*We can use theorems proven in @{text "group0"} and @{text "group1"}
contexts applied to the group of real numbers.*}

lemma Real_ZF_1_L2: shows
proof -
have
using Real_ZF_1_T1 IsAring_def by auto
then show
using group1_axioms.intro group0_def group1_def
by auto
qed

text{*Let's define some notation.*}

locale real0 =

fixes real ("ℝ")
defines real_def [simp]: "ℝ ≡ RealNumbers"

fixes ra (infixl "\<ra>" 69)
defines ra_def [simp]: "a\<ra> b ≡ RealAddition⟨a,b⟩"

fixes rminus ("\<rm> _" 72)
defines rminus_def [simp]:"\<rm>a ≡ GroupInv(ℝ,RealAddition)(a)"

fixes rsub (infixl "\<rs>" 69)
defines rsub_def [simp]: "a\<rs>b ≡ a\<ra>(\<rm>b)"

fixes rm (infixl "⋅" 70)
defines rm_def [simp]: "a⋅b ≡ RealMultiplication⟨a,b⟩"

fixes rzero ("𝟬")
defines rzero_def [simp]:

fixes rone ("𝟭")
defines rone_def [simp]:
"𝟭 ≡ TheNeutralElement(RealNumbers,RealMultiplication)"

fixes rtwo ("𝟮")
defines rtwo_def [simp]: "𝟮  ≡ 𝟭\<ra>𝟭"

fixes non_zero ("ℝ⇩0")
defines non_zero_def[simp]: "ℝ⇩0 ≡ ℝ-{𝟬}"

fixes inv ("_¯ " [90] 91)
defines inv_def[simp]:
"a¯ ≡ GroupInv(ℝ⇩0,restrict(RealMultiplication,ℝ⇩0×ℝ⇩0))(a)"

text{*In @{text "real0"} context all theorems proven in the @{text "ring0"},
context are valid.*}

lemma (in real0) Real_ZF_1_L3: shows
using Real_ZF_1_T1 ring0_def ring0.Ring_ZF_1_L1
by auto

text{*Lets try out our notation to see that zero and one are real numbers.*}

lemma (in real0) Real_ZF_1_L4: shows "𝟬∈ℝ"  "𝟭∈ℝ"
using Real_ZF_1_L3 ring0.Ring_ZF_1_L2 by auto

text{*The lemma below lists some properties that
require one real number to state.*}

lemma (in real0) Real_ZF_1_L5: assumes A1: "a∈ℝ"
shows
"(\<rm>a) ∈ ℝ"
"(\<rm>(\<rm>a)) = a"
"a\<ra>𝟬 = a"
"𝟬\<ra>a = a"
"a⋅𝟭 = a"
"𝟭⋅a = a"
"a\<rs>a = 𝟬"
"a\<rs>𝟬 = a"
using assms Real_ZF_1_L3 ring0.Ring_ZF_1_L3 by auto

text{*The lemma below lists some properties that
require two real numbers to state.*}

lemma (in real0) Real_ZF_1_L6: assumes "a∈ℝ"  "b∈ℝ"
shows
"a\<ra>b ∈ ℝ"
"a\<rs>b ∈ ℝ"
"a⋅b ∈ ℝ"
"a\<ra>b = b\<ra>a"
"(\<rm>a)⋅b = \<rm>(a⋅b)"
"a⋅(\<rm>b) = \<rm>(a⋅b)"
using assms Real_ZF_1_L3 ring0.Ring_ZF_1_L4 ring0.Ring_ZF_1_L7
by auto

text{*Multiplication of reals is associative.*}

lemma (in real0) Real_ZF_1_L6A: assumes "a∈ℝ"  "b∈ℝ"  "c∈ℝ"
shows "a⋅(b⋅c) = (a⋅b)⋅c"
using assms Real_ZF_1_L3 ring0.Ring_ZF_1_L11
by simp

text{*Addition is distributive with respect to multiplication.*}

lemma (in real0) Real_ZF_1_L7: assumes "a∈ℝ"  "b∈ℝ"  "c∈ℝ"
shows
"a⋅(b\<ra>c) = a⋅b \<ra> a⋅c"
"(b\<ra>c)⋅a = b⋅a \<ra> c⋅a"
"a⋅(b\<rs>c) = a⋅b \<rs> a⋅c"
"(b\<rs>c)⋅a = b⋅a \<rs> c⋅a"
using assms Real_ZF_1_L3 ring0.ring_oper_distr  ring0.Ring_ZF_1_L8
by auto

text{*A simple rearrangement with four real numbers.*}

lemma (in real0) Real_ZF_1_L7A:
assumes "a∈ℝ"  "b∈ℝ"  "c∈ℝ"  "d∈ℝ"
shows "a\<rs>b \<ra> (c\<rs>d) = a\<ra>c\<rs>b\<rs>d"
using assms Real_ZF_1_L2 group0.group0_4_L8A by simp

text{* @{text "RealAddition"} is defined as the projection of the
first operation on slopes (that is, slope addition) on the quotient
(slopes divided by the "almost equal" relation. The next lemma plays with
definitions to show that this is the same as the operation induced on the
appriopriate quotient group. The names @{text "AH"}, @{text "Op1"}
and @{text "FR"} are used in @{text "group1"} context to denote almost
homomorphisms, the first operation on @{text "AH"} and finite range
functions resp.*}

lemma Real_ZF_1_L8: assumes
"FR = FinRangeFunctions(int,int)"
QuotientGroupOp_def Slopes_def SlopeOp1_def BoundedIntMaps_def
by simp

text{*The symbol @{text "𝟬"} in the @{text "real0"} context is defined
as the neutral element of real addition. The next lemma shows that this
is the same as the neutral element of the appriopriate quotient group.*}

lemma (in real0) Real_ZF_1_L9: assumes
"FR = FinRangeFunctions(int,int)" and
"r = QuotientGroupRel(AH,Op1,FR)"
shows
"TheNeutralElement(AH//r,QuotientGroupOp(AH,Op1,FR)) = 𝟬"
"SlopeEquivalenceRel = r"
using assms Slopes_def Real_ZF_1_L8 RealNumbers_def
SlopeEquivalenceRel_def SlopeOp1_def BoundedIntMaps_def
by auto

text{*Zero is the class of any finite range function.*}

lemma (in real0) Real_ZF_1_L10:
assumes A1: "s ∈ Slopes"
shows "SlopeEquivalenceRel{s} = 𝟬 ⟷ s ∈ BoundedIntMaps"
proof -
let ?FR = "FinRangeFunctions(int,int)"
let ?r = "QuotientGroupRel(?AH,?Op1,?FR)"
let ?e = "TheNeutralElement(?AH//?r,QuotientGroupOp(?AH,?Op1,?FR))"
from A1 have
"s∈?AH"
using Real_ZF_1_L1 Slopes_def
by auto
then have "?r{s} = ?e ⟷ s ∈ ?FR"
using group1.Group_ZF_3_3_L5 by simp
moreover have
"?r = SlopeEquivalenceRel"
"?e = 𝟬"
"?FR = BoundedIntMaps"
using SlopeEquivalenceRel_def Slopes_def SlopeOp1_def
BoundedIntMaps_def Real_ZF_1_L9 by auto
ultimately show ?thesis by simp
qed

text{*We will need a couple of results from @{text "Group_ZF_3.thy"}
The first two that state that the definition
of addition and multiplication of real numbers are consistent,
that is the result
does not depend on the choice of the slopes representing the numbers.
The second one implies that what we call @{text "SlopeEquivalenceRel"}
is actually an equivalence relation on the set of slopes.
We also show that the neutral element of the multiplicative operation on
reals (in short number $1$) is the class of the identity function on
integers.*}

lemma Real_ZF_1_L11: shows
"Congruent2(SlopeEquivalenceRel,SlopeOp1)"
"Congruent2(SlopeEquivalenceRel,SlopeOp2)"
"SlopeEquivalenceRel ⊆ Slopes × Slopes"
"equiv(Slopes, SlopeEquivalenceRel)"
"SlopeEquivalenceRel{id(int)} =
TheNeutralElement(RealNumbers,RealMultiplication)"
"BoundedIntMaps ⊆ Slopes"
proof -
let ?G = "int"
let ?FR = "FinRangeFunctions(int,int)"
let ?R = "QuotientGroupRel(?AH,?Op1,?FR)"
have
"Congruent2(?R,?Op1)"
"Congruent2(?R,?Op2)"
using Real_ZF_1_L1 group1.Group_ZF_3_4_L13A group1.Group_ZF_3_3_L4
by auto
then show
"Congruent2(SlopeEquivalenceRel,SlopeOp1)"
"Congruent2(SlopeEquivalenceRel,SlopeOp2)"
using SlopeEquivalenceRel_def SlopeOp1_def Slopes_def
BoundedIntMaps_def SlopeOp2_def by auto
have "equiv(?AH,?R)"
using Real_ZF_1_L1 group1.Group_ZF_3_3_L3 by simp
then show "equiv(Slopes,SlopeEquivalenceRel)"
using BoundedIntMaps_def SlopeEquivalenceRel_def SlopeOp1_def Slopes_def
by simp
then show "SlopeEquivalenceRel ⊆ Slopes × Slopes"
using equiv_type by simp
have "?R{id(int)} = TheNeutralElement(?AH//?R,ProjFun2(?AH,?R,?Op2))"
using Real_ZF_1_L1 group1.Group_ZF_3_4_T2 by simp
then show  "SlopeEquivalenceRel{id(int)} =
TheNeutralElement(RealNumbers,RealMultiplication)"
using Slopes_def RealNumbers_def
SlopeEquivalenceRel_def SlopeOp1_def BoundedIntMaps_def
RealMultiplication_def SlopeOp2_def
by simp
have "?FR ⊆ ?AH" using Real_ZF_1_L1 group1.Group_ZF_3_3_L1
by simp
then show "BoundedIntMaps ⊆ Slopes"
using BoundedIntMaps_def Slopes_def by simp
qed

text{*A one-side implication of the equivalence from @{text "Real_ZF_1_L10"}:
the class of a bounded integer map is the real zero.*}

lemma (in real0) Real_ZF_1_L11A: assumes "s ∈ BoundedIntMaps"
shows "SlopeEquivalenceRel{s} = 𝟬"
using assms Real_ZF_1_L11 Real_ZF_1_L10 by auto

text{*The next lemma is rephrases the result from @{text "Group_ZF_3.thy"}
that says that the negative (the group inverse with respect to real
addition) of the class of a slope is the class of that slope
composed with the integer additive group inverse. The result and proof is not
very readable as we use mostly generic set theory notation with long names
here. @{text "Real_ZF_1.thy"} contains the same statement written in a more
readable notation: $[-s] = -[s]$.*}

lemma (in real0) Real_ZF_1_L12: assumes A1: "s ∈ Slopes" and
Dr: "r = QuotientGroupRel(Slopes,SlopeOp1,BoundedIntMaps)"
shows "r{GroupInv(int,IntegerAddition) O s} = \<rm>(r{s})"
proof -
let ?G = "int"
let ?FR = "FinRangeFunctions(int,int)"
let ?F = "ProjFun2(Slopes,r,SlopeOp1)"
from A1 Dr have
"group1(?G, ?f)"
"s ∈ AlmostHoms(?G, ?f)"
"r = QuotientGroupRel(
AlmostHoms(?G, ?f), AlHomOp1(?G, ?f), FinRangeFunctions(?G, ?G))"
and "?F = ProjFun2(AlmostHoms(?G, ?f), r, AlHomOp1(?G, ?f))"
using Real_ZF_1_L1 Slopes_def SlopeOp1_def BoundedIntMaps_def
by auto
then have
"r{GroupInv(?G, ?f) O s} =
GroupInv(AlmostHoms(?G, ?f) // r, ?F)(r  {s})"
using group1.Group_ZF_3_3_L6 by simp
with Dr show ?thesis
by simp
qed

text{*Two classes are equal iff the slopes that represent them
are almost equal.*}

lemma Real_ZF_1_L13: assumes "s ∈ Slopes"  "p ∈ Slopes"
and "r = SlopeEquivalenceRel"
shows "r{s} = r{p} ⟷ ⟨s,p⟩ ∈  r"
using assms Real_ZF_1_L11 eq_equiv_class equiv_class_eq
by blast

text{*Identity function on integers is a slope.
Thislemma  concludes the easy part of the construction that follows from
the fact that slope equivalence classes form a ring. It is easy to see
that multiplication of classes of almost homomorphisms is not
commutative in general.
The remaining properties of real numbers, like commutativity of
multiplication and the existence of multiplicative inverses have to be
proven using properties of the group of integers, rather that in general
setting of abelian groups.*}

lemma Real_ZF_1_L14: shows "id(int) ∈ Slopes"
proof -
`