doc-src/TutorialI/Types/numerics.tex
author paulson
Fri Jan 12 16:11:55 2001 +0100 (2001-01-12)
changeset 10881 03f06372230b
parent 10794 65d18005d802
child 10978 5eebea8f359f
permissions -rw-r--r--
abs and other small changes
paulson@10794
     1
% $Id$
paulson@10794
     2
Until now, our numerical have used the type of \textbf{natural numbers},
paulson@10594
     3
\isa{nat}.  This is a recursive datatype generated by the constructors
paulson@10594
     4
zero  and successor, so it works well with inductive proofs and primitive
paulson@10794
     5
recursive function definitions. Isabelle/HOL also provides the type
paulson@10794
     6
\isa{int} of \textbf{integers}, which lack induction but support true
paulson@10881
     7
subtraction. The logic HOL-Real also has the type \isa{real} of real
paulson@10777
     8
numbers.  Isabelle has no subtyping,  so the numeric types are distinct and
paulson@10777
     9
there are  functions to convert between them. 
paulson@10594
    10
paulson@10594
    11
The integers are preferable to the natural  numbers for reasoning about
paulson@10594
    12
complicated arithmetic expressions. For  example, a termination proof
paulson@10594
    13
typically involves an integer metric  that is shown to decrease at each
paulson@10794
    14
loop iteration. Even if the  metric cannot become negative, proofs 
paulson@10794
    15
may be easier if you use the integers instead of the natural
paulson@10594
    16
numbers. 
paulson@10594
    17
paulson@10594
    18
Many theorems involving numeric types can be proved automatically by
paulson@10594
    19
Isabelle's arithmetic decision procedure, the method
paulson@10594
    20
\isa{arith}.  Linear arithmetic comprises addition, subtraction
paulson@10594
    21
and multiplication by constant factors; subterms involving other operators
paulson@10594
    22
are regarded as variables.  The procedure can be slow, especially if the
paulson@10594
    23
subgoal to be proved involves subtraction over type \isa{nat}, which 
paulson@10594
    24
causes case splits.  
paulson@10594
    25
paulson@10594
    26
The simplifier reduces arithmetic expressions in other
paulson@10594
    27
ways, such as dividing through by common factors.  For problems that lie
paulson@10881
    28
outside the scope of automation, HOL provides hundreds of
paulson@10594
    29
theorems about multiplication, division, etc., that can be brought to
paulson@10881
    30
bear.  You can locate them using Proof General's Find
paulson@10881
    31
button.  A few lemmas are given below to show what
paulson@10794
    32
is available.
paulson@10594
    33
paulson@10594
    34
\subsection{Numeric Literals}
nipkow@10779
    35
\label{sec:numerals}
paulson@10594
    36
paulson@10594
    37
Literals are available for the types of natural numbers, integers 
paulson@10594
    38
and reals and denote integer values of arbitrary size. 
paulson@10594
    39
They begin 
paulson@10594
    40
with a number sign (\isa{\#}), have an optional minus sign (\isa{-}) and 
paulson@10594
    41
then one or more decimal digits. Examples are \isa{\#0}, \isa{\#-3} 
paulson@10594
    42
and \isa{\#441223334678}.
paulson@10594
    43
paulson@10594
    44
Literals look like constants, but they abbreviate 
paulson@10594
    45
terms, representing the number in a two's complement binary notation. 
paulson@10794
    46
Isabelle performs arithmetic on literals by rewriting rather 
paulson@10594
    47
than using the hardware arithmetic. In most cases arithmetic 
paulson@10594
    48
is fast enough, even for large numbers. The arithmetic operations 
paulson@10794
    49
provided for literals include addition, subtraction, multiplication, 
paulson@10794
    50
integer division and remainder.  Fractions of literals (expressed using
paulson@10794
    51
division) are reduced to lowest terms.
paulson@10594
    52
paulson@10794
    53
\begin{warn}
paulson@10794
    54
The arithmetic operators are 
paulson@10594
    55
overloaded, so you must be careful to ensure that each numeric 
paulson@10594
    56
expression refers to a specific type, if necessary by inserting 
paulson@10594
    57
type constraints.  Here is an example of what can go wrong:
paulson@10794
    58
\par
paulson@10594
    59
\begin{isabelle}
paulson@10594
    60
\isacommand{lemma}\ "\#2\ *\ m\ =\ m\ +\ m"
paulson@10594
    61
\end{isabelle}
paulson@10594
    62
%
paulson@10594
    63
Carefully observe how Isabelle displays the subgoal:
paulson@10594
    64
\begin{isabelle}
paulson@10594
    65
\ 1.\ (\#2::'a)\ *\ m\ =\ m\ +\ m
paulson@10594
    66
\end{isabelle}
paulson@10594
    67
The type \isa{'a} given for the literal \isa{\#2} warns us that no numeric
paulson@10594
    68
type has been specified.  The problem is underspecified.  Given a type
paulson@10594
    69
constraint such as \isa{nat}, \isa{int} or \isa{real}, it becomes trivial.
paulson@10794
    70
\end{warn}
paulson@10794
    71
paulson@10881
    72
\begin{warn}
paulson@10881
    73
Numeric literals are not constructors and therefore must not be used in
paulson@10881
    74
patterns.  For example, this declaration is rejected:
paulson@10881
    75
\begin{isabelle}
paulson@10881
    76
\isacommand{recdef}\ h\ "\isacharbraceleft \isacharbraceright "\isanewline
paulson@10881
    77
h\ \#3\ =\ \#2\isanewline
paulson@10881
    78
h\ i\ \ =\ i
paulson@10881
    79
\end{isabelle}
paulson@10881
    80
paulson@10881
    81
You should use a conditional expression instead:
paulson@10881
    82
\begin{isabelle}
paulson@10881
    83
"h\ i\ =\ (if\ i\ =\ \#3\ then\ \#2\ else\ i)"
paulson@10881
    84
\end{isabelle}
paulson@10881
    85
\end{warn}
paulson@10881
    86
paulson@10594
    87
paulson@10594
    88
paulson@10594
    89
\subsection{The type of natural numbers, {\tt\slshape nat}}
paulson@10594
    90
paulson@10594
    91
This type requires no introduction: we have been using it from the
paulson@10794
    92
beginning.  Hundreds of theorems about the natural numbers are
paulson@10594
    93
proved in the theories \isa{Nat}, \isa{NatArith} and \isa{Divides}.  Only
paulson@10594
    94
in exceptional circumstances should you resort to induction.
paulson@10594
    95
paulson@10594
    96
\subsubsection{Literals}
paulson@10594
    97
The notational options for the natural numbers can be confusing. The 
paulson@10594
    98
constant \isa{0} is overloaded to serve as the neutral value 
paulson@10594
    99
in a variety of additive types. The symbols \isa{1} and \isa{2} are 
paulson@10594
   100
not constants but abbreviations for \isa{Suc 0} and \isa{Suc(Suc 0)},
paulson@10594
   101
respectively. The literals \isa{\#0}, \isa{\#1} and \isa{\#2}  are
paulson@10794
   102
syntactically different from \isa{0}, \isa{1} and \isa{2}. You  will
paulson@10594
   103
sometimes prefer one notation to the other. Literals are  obviously
paulson@10794
   104
necessary to express large values, while \isa{0} and \isa{Suc}  are needed
paulson@10794
   105
in order to match many theorems, including the rewrite  rules for primitive
paulson@10794
   106
recursive functions. The following default  simplification rules replace
paulson@10794
   107
small literals by zero and successor: 
paulson@10594
   108
\begin{isabelle}
paulson@10594
   109
\#0\ =\ 0
paulson@10594
   110
\rulename{numeral_0_eq_0}\isanewline
paulson@10594
   111
\#1\ =\ 1
paulson@10594
   112
\rulename{numeral_1_eq_1}\isanewline
paulson@10594
   113
\#2\ +\ n\ =\ Suc\ (Suc\ n)
paulson@10594
   114
\rulename{add_2_eq_Suc}\isanewline
paulson@10594
   115
n\ +\ \#2\ =\ Suc\ (Suc\ n)
paulson@10594
   116
\rulename{add_2_eq_Suc'}
paulson@10594
   117
\end{isabelle}
paulson@10594
   118
In special circumstances, you may wish to remove or reorient 
paulson@10594
   119
these rules. 
paulson@10594
   120
paulson@10594
   121
\subsubsection{Typical lemmas}
paulson@10594
   122
Inequalities involving addition and subtraction alone can be proved
paulson@10594
   123
automatically.  Lemmas such as these can be used to prove inequalities
paulson@10594
   124
involving multiplication and division:
paulson@10594
   125
\begin{isabelle}
paulson@10594
   126
\isasymlbrakk i\ \isasymle \ j;\ k\ \isasymle \ l\isasymrbrakk \ \isasymLongrightarrow \ i\ *\ k\ \isasymle \ j\ *\ l%
paulson@10594
   127
\rulename{mult_le_mono}\isanewline
paulson@10594
   128
\isasymlbrakk i\ <\ j;\ 0\ <\ k\isasymrbrakk \ \isasymLongrightarrow \ i\
paulson@10594
   129
*\ k\ <\ j\ *\ k%
paulson@10594
   130
\rulename{mult_less_mono1}\isanewline
paulson@10594
   131
m\ \isasymle \ n\ \isasymLongrightarrow \ m\ div\ k\ \isasymle \ n\ div\ k%
paulson@10594
   132
\rulename{div_le_mono}
paulson@10594
   133
\end{isabelle}
paulson@10594
   134
%
paulson@10594
   135
Various distributive laws concerning multiplication are available:
paulson@10594
   136
\begin{isabelle}
paulson@10594
   137
(m\ +\ n)\ *\ k\ =\ m\ *\ k\ +\ n\ *\ k%
paulson@10594
   138
\rulename{add_mult_distrib}\isanewline
paulson@10594
   139
(m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%
paulson@10594
   140
\rulename{diff_mult_distrib}\isanewline
paulson@10594
   141
(m\ mod\ n)\ *\ k\ =\ (m\ *\ k)\ mod\ (n\ *\ k)
paulson@10594
   142
\rulename{mod_mult_distrib}
paulson@10594
   143
\end{isabelle}
paulson@10594
   144
paulson@10594
   145
\subsubsection{Division}
paulson@10881
   146
The infix operators \isa{div} and \isa{mod} are overloaded.
paulson@10881
   147
Isabelle/HOL provides the basic facts about quotient and remainder
paulson@10881
   148
on the natural numbers:
paulson@10594
   149
\begin{isabelle}
paulson@10594
   150
m\ mod\ n\ =\ (if\ m\ <\ n\ then\ m\ else\ (m\ -\ n)\ mod\ n)
paulson@10594
   151
\rulename{mod_if}\isanewline
paulson@10594
   152
m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%
paulson@10594
   153
\rulename{mod_div_equality}
paulson@10594
   154
\end{isabelle}
paulson@10594
   155
paulson@10594
   156
Many less obvious facts about quotient and remainder are also provided. 
paulson@10594
   157
Here is a selection:
paulson@10594
   158
\begin{isabelle}
paulson@10594
   159
a\ *\ b\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
paulson@10594
   160
\rulename{div_mult1_eq}\isanewline
paulson@10594
   161
a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
paulson@10594
   162
\rulename{mod_mult1_eq}\isanewline
paulson@10594
   163
a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
paulson@10594
   164
\rulename{div_mult2_eq}\isanewline
paulson@10594
   165
a\ mod\ (b*c)\ =\ b * (a\ div\ b\ mod\ c)\ +\ a\ mod\ b%
paulson@10594
   166
\rulename{mod_mult2_eq}\isanewline
paulson@10594
   167
0\ <\ c\ \isasymLongrightarrow \ (c\ *\ a)\ div\ (c\ *\ b)\ =\ a\ div\ b%
paulson@10594
   168
\rulename{div_mult_mult1}
paulson@10594
   169
\end{isabelle}
paulson@10594
   170
paulson@10594
   171
Surprisingly few of these results depend upon the
paulson@10794
   172
divisors' being nonzero.  That is because division by
paulson@10794
   173
zero yields zero:
paulson@10594
   174
\begin{isabelle}
paulson@10594
   175
a\ div\ 0\ =\ 0
paulson@10594
   176
\rulename{DIVISION_BY_ZERO_DIV}\isanewline
paulson@10594
   177
a\ mod\ 0\ =\ a%
paulson@10594
   178
\rulename{DIVISION_BY_ZERO_MOD}
paulson@10594
   179
\end{isabelle}
paulson@10594
   180
As a concession to convention, these equations are not installed as default
paulson@10594
   181
simplification rules but are merely used to remove nonzero-divisor
paulson@10594
   182
hypotheses by case analysis.  In \isa{div_mult_mult1} above, one of
paulson@10594
   183
the two divisors (namely~\isa{c}) must be still be nonzero.
paulson@10594
   184
paulson@10594
   185
The \textbf{divides} relation has the standard definition, which
paulson@10594
   186
is overloaded over all numeric types: 
paulson@10594
   187
\begin{isabelle}
paulson@10594
   188
m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k
paulson@10594
   189
\rulename{dvd_def}
paulson@10594
   190
\end{isabelle}
paulson@10594
   191
%
paulson@10594
   192
Section~\ref{sec:proving-euclid} discusses proofs involving this
paulson@10594
   193
relation.  Here are some of the facts proved about it:
paulson@10594
   194
\begin{isabelle}
paulson@10594
   195
\isasymlbrakk m\ dvd\ n;\ n\ dvd\ m\isasymrbrakk \ \isasymLongrightarrow \ m\ =\ n%
paulson@10594
   196
\rulename{dvd_anti_sym}\isanewline
paulson@10594
   197
\isasymlbrakk k\ dvd\ m;\ k\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ k\ dvd\ (m\ +\ n)
paulson@10594
   198
\rulename{dvd_add}
paulson@10594
   199
\end{isabelle}
paulson@10594
   200
paulson@10594
   201
\subsubsection{Simplifier tricks}
paulson@10594
   202
The rule \isa{diff_mult_distrib} shown above is one of the few facts
paulson@10594
   203
about \isa{m\ -\ n} that is not subject to
paulson@10594
   204
the condition \isa{n\ \isasymle \  m}.  Natural number subtraction has few
paulson@10794
   205
nice properties; often you should remove it by simplifying with this split
paulson@10794
   206
rule:
paulson@10594
   207
\begin{isabelle}
paulson@10594
   208
P(a-b)\ =\ ((a<b\ \isasymlongrightarrow \ P\
paulson@10594
   209
0)\ \isasymand \ (\isasymforall d.\ a\ =\ b+d\ \isasymlongrightarrow \ P\
paulson@10594
   210
d))
paulson@10594
   211
\rulename{nat_diff_split}
paulson@10594
   212
\end{isabelle}
paulson@10594
   213
For example, it proves the following fact, which lies outside the scope of
paulson@10594
   214
linear arithmetic:
paulson@10594
   215
\begin{isabelle}
paulson@10594
   216
\isacommand{lemma}\ "(n-1)*(n+1)\ =\ n*n\ -\ 1"\isanewline
paulson@10594
   217
\isacommand{apply}\ (simp\ split:\ nat_diff_split)\isanewline
paulson@10594
   218
\isacommand{done}
paulson@10594
   219
\end{isabelle}
paulson@10594
   220
paulson@10594
   221
Suppose that two expressions are equal, differing only in 
paulson@10594
   222
associativity and commutativity of addition.  Simplifying with the
paulson@10594
   223
following equations sorts the terms and groups them to the right, making
paulson@10594
   224
the two expressions identical:
paulson@10594
   225
\begin{isabelle}
paulson@10594
   226
m\ +\ n\ +\ k\ =\ m\ +\ (n\ +\ k)
paulson@10594
   227
\rulename{add_assoc}\isanewline
paulson@10594
   228
m\ +\ n\ =\ n\ +\ m%
paulson@10594
   229
\rulename{add_commute}\isanewline
paulson@10594
   230
x\ +\ (y\ +\ z)\ =\ y\ +\ (x\
paulson@10594
   231
+\ z)
paulson@10594
   232
\rulename{add_left_commute}
paulson@10594
   233
\end{isabelle}
paulson@10594
   234
The name \isa{add_ac} refers to the list of all three theorems, similarly
paulson@10594
   235
there is \isa{mult_ac}.  Here is an example of the sorting effect.  Start
paulson@10594
   236
with this goal:
paulson@10594
   237
\begin{isabelle}
paulson@10594
   238
\ 1.\ Suc\ (i\ +\ j\ *\ l\ *\ k\ +\ m\ *\ n)\ =\
paulson@10594
   239
f\ (n\ *\ m\ +\ i\ +\ k\ *\ j\ *\ l)
paulson@10594
   240
\end{isabelle}
paulson@10594
   241
%
paulson@10594
   242
Simplify using  \isa{add_ac} and \isa{mult_ac}:
paulson@10594
   243
\begin{isabelle}
paulson@10594
   244
\isacommand{apply}\ (simp\ add:\ add_ac\ mult_ac)
paulson@10594
   245
\end{isabelle}
paulson@10594
   246
%
paulson@10594
   247
Here is the resulting subgoal:
paulson@10594
   248
\begin{isabelle}
paulson@10594
   249
\ 1.\ Suc\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))\
paulson@10594
   250
=\ f\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))%
paulson@10594
   251
\end{isabelle}
paulson@10594
   252
paulson@10594
   253
paulson@10594
   254
\subsection{The type of integers, {\tt\slshape int}}
paulson@10594
   255
paulson@10794
   256
Reasoning methods resemble those for the natural numbers, but induction and
paulson@10881
   257
the constant \isa{Suc} are not available.  HOL provides many lemmas
paulson@10794
   258
for proving inequalities involving integer multiplication and division,
paulson@10794
   259
similar to those shown above for type~\isa{nat}.  
paulson@10794
   260
paulson@10794
   261
The absolute value function \isa{abs} is overloaded for the numeric types.
paulson@10794
   262
It is defined for the integers; we have for example the obvious law
paulson@10794
   263
\begin{isabelle}
paulson@10794
   264
\isasymbar x\ *\ y\isasymbar \ =\ \isasymbar x\isasymbar \ *\ \isasymbar y\isasymbar 
paulson@10794
   265
\rulename{abs_mult}
paulson@10794
   266
\end{isabelle}
paulson@10594
   267
paulson@10794
   268
\begin{warn}
paulson@10794
   269
The absolute value bars shown above cannot be typed on a keyboard.  They
paulson@10794
   270
can be entered using the X-symbol package.  In ASCII, type \isa{abs x} to
paulson@10794
   271
get \isa{\isasymbar x\isasymbar}.
paulson@10794
   272
\end{warn}
paulson@10794
   273
paulson@10881
   274
The \isa{arith} method can prove facts about \isa{abs} automatically, 
paulson@10881
   275
though as it does so by case analysis, the cost can be exponential.
paulson@10881
   276
\begin{isabelle}
paulson@10881
   277
\isacommand{lemma}\ "\isasymlbrakk abs\ x\ <\ a;\ abs\ y\ <\ b\isasymrbrakk \ \isasymLongrightarrow \ abs\ x\ +\ abs\ y\ <\ (a\ +\ b\ ::\ int)"\isanewline
paulson@10881
   278
\isacommand{by}\ arith
paulson@10881
   279
\end{isabelle}
paulson@10794
   280
paulson@10794
   281
Concerning simplifier tricks, we have no need to eliminate subtraction: it
paulson@10794
   282
is well-behaved.  As with the natural numbers, the simplifier can sort the
paulson@10794
   283
operands of sums and products.  The name \isa{zadd_ac} refers to the
paulson@10794
   284
associativity and commutativity theorems for integer addition, while
paulson@10794
   285
\isa{zmult_ac} has the analogous theorems for multiplication.  The
paulson@10794
   286
prefix~\isa{z} in many theorem names recalls the use of $\mathbb{Z}$ to
paulson@10794
   287
denote the set of integers.
paulson@10594
   288
paulson@10594
   289
For division and remainder, the treatment of negative divisors follows
paulson@10794
   290
mathematical practice: the sign of the remainder follows that
paulson@10594
   291
of the divisor:
paulson@10594
   292
\begin{isabelle}
paulson@10594
   293
\#0\ <\ b\ \isasymLongrightarrow \ \#0\ \isasymle \ a\ mod\ b%
paulson@10594
   294
\rulename{pos_mod_sign}\isanewline
paulson@10594
   295
\#0\ <\ b\ \isasymLongrightarrow \ a\ mod\ b\ <\ b%
paulson@10594
   296
\rulename{pos_mod_bound}\isanewline
paulson@10594
   297
b\ <\ \#0\ \isasymLongrightarrow \ a\ mod\ b\ \isasymle \ \#0
paulson@10594
   298
\rulename{neg_mod_sign}\isanewline
paulson@10594
   299
b\ <\ \#0\ \isasymLongrightarrow \ b\ <\ a\ mod\ b%
paulson@10594
   300
\rulename{neg_mod_bound}
paulson@10594
   301
\end{isabelle}
paulson@10594
   302
ML treats negative divisors in the same way, but most computer hardware
paulson@10594
   303
treats signed operands using the same rules as for multiplication.
paulson@10794
   304
Many facts about quotients and remainders are provided:
paulson@10594
   305
\begin{isabelle}
paulson@10594
   306
(a\ +\ b)\ div\ c\ =\isanewline
paulson@10594
   307
a\ div\ c\ +\ b\ div\ c\ +\ (a\ mod\ c\ +\ b\ mod\ c)\ div\ c%
paulson@10594
   308
\rulename{zdiv_zadd1_eq}
paulson@10594
   309
\par\smallskip
paulson@10594
   310
(a\ +\ b)\ mod\ c\ =\ (a\ mod\ c\ +\ b\ mod\ c)\ mod\ c%
paulson@10594
   311
\rulename{zmod_zadd1_eq}
paulson@10594
   312
\end{isabelle}
paulson@10594
   313
paulson@10594
   314
\begin{isabelle}
paulson@10594
   315
(a\ *\ b)\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
paulson@10594
   316
\rulename{zdiv_zmult1_eq}\isanewline
paulson@10594
   317
(a\ *\ b)\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
paulson@10594
   318
\rulename{zmod_zmult1_eq}
paulson@10594
   319
\end{isabelle}
paulson@10594
   320
paulson@10594
   321
\begin{isabelle}
paulson@10594
   322
\#0\ <\ c\ \isasymLongrightarrow \ a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
paulson@10594
   323
\rulename{zdiv_zmult2_eq}\isanewline
paulson@10594
   324
\#0\ <\ c\ \isasymLongrightarrow \ a\ mod\ (b*c)\ =\ b*(a\ div\ b\ mod\
paulson@10594
   325
c)\ +\ a\ mod\ b%
paulson@10594
   326
\rulename{zmod_zmult2_eq}
paulson@10594
   327
\end{isabelle}
paulson@10594
   328
The last two differ from their natural number analogues by requiring
paulson@10594
   329
\isa{c} to be positive.  Since division by zero yields zero, we could allow
paulson@10594
   330
\isa{c} to be zero.  However, \isa{c} cannot be negative: a counterexample
paulson@10594
   331
is
paulson@10594
   332
$\isa{a} = 7$, $\isa{b} = 2$ and $\isa{c} = -3$, when the left-hand side of
paulson@10881
   333
\isa{zdiv_zmult2_eq} is $-2$ while the right-hand side is~$-1$.
paulson@10594
   334
paulson@10594
   335
paulson@10594
   336
\subsection{The type of real numbers, {\tt\slshape real}}
paulson@10594
   337
paulson@10777
   338
The real numbers enjoy two significant properties that the integers lack. 
paulson@10777
   339
They are
paulson@10777
   340
\textbf{dense}: between every two distinct real numbers there lies another.
paulson@10777
   341
This property follows from the division laws, since if $x<y$ then between
paulson@10777
   342
them lies $(x+y)/2$.  The second property is that they are
paulson@10777
   343
\textbf{complete}: every set of reals that is bounded above has a least
paulson@10777
   344
upper bound.  Completeness distinguishes the reals from the rationals, for
paulson@10777
   345
which the set $\{x\mid x^2<2\}$ has no least upper bound.  (It could only be
paulson@10777
   346
$\surd2$, which is irrational.)
paulson@10794
   347
The formalization of completeness is complicated; rather than
paulson@10777
   348
reproducing it here, we refer you to the theory \texttt{RComplete} in
paulson@10777
   349
directory \texttt{Real}.
paulson@10794
   350
Density, however, is trivial to express:
paulson@10777
   351
\begin{isabelle}
paulson@10777
   352
x\ <\ y\ \isasymLongrightarrow \ \isasymexists r.\ x\ <\ r\ \isasymand \ r\ <\ y%
paulson@10777
   353
\rulename{real_dense}
paulson@10777
   354
\end{isabelle}
paulson@10777
   355
paulson@10777
   356
Here is a selection of rules about the division operator.  The following
paulson@10777
   357
are installed as default simplification rules in order to express
paulson@10777
   358
combinations of products and quotients as rational expressions:
paulson@10777
   359
\begin{isabelle}
paulson@10777
   360
x\ *\ (y\ /\ z)\ =\ x\ *\ y\ /\ z%
paulson@10777
   361
\rulename{real_times_divide1_eq}\isanewline
paulson@10777
   362
y\ /\ z\ *\ x\ =\ y\ *\ x\ /\ z%
paulson@10777
   363
\rulename{real_times_divide2_eq}\isanewline
paulson@10777
   364
x\ /\ (y\ /\ z)\ =\ x\ *\ z\ /\ y%
paulson@10777
   365
\rulename{real_divide_divide1_eq}\isanewline
paulson@10777
   366
x\ /\ y\ /\ z\ =\ x\ /\ (y\ *\ z)
paulson@10777
   367
\rulename{real_divide_divide2_eq}
paulson@10777
   368
\end{isabelle}
paulson@10777
   369
paulson@10777
   370
Signs are extracted from quotients in the hope that complementary terms can
paulson@10777
   371
then be cancelled:
paulson@10777
   372
\begin{isabelle}
paulson@10777
   373
-\ x\ /\ y\ =\ -\ (x\ /\ y)
paulson@10777
   374
\rulename{real_minus_divide_eq}\isanewline
paulson@10777
   375
x\ /\ -\ y\ =\ -\ (x\ /\ y)
paulson@10777
   376
\rulename{real_divide_minus_eq}
paulson@10777
   377
\end{isabelle}
paulson@10777
   378
paulson@10777
   379
The following distributive law is available, but it is not installed as a
paulson@10777
   380
simplification rule.
paulson@10777
   381
\begin{isabelle}
paulson@10777
   382
(x\ +\ y)\ /\ z\ =\ x\ /\ z\ +\ y\ /\ z%
paulson@10777
   383
\rulename{real_add_divide_distrib}
paulson@10777
   384
\end{isabelle}
paulson@10777
   385
paulson@10594
   386
As with the other numeric types, the simplifier can sort the operands of
paulson@10594
   387
addition and multiplication.  The name \isa{real_add_ac} refers to the
paulson@10777
   388
associativity and commutativity theorems for addition, while similarly
paulson@10594
   389
\isa{real_mult_ac} contains those properties for multiplication. 
paulson@10594
   390
paulson@10777
   391
The absolute value function \isa{abs} is
paulson@10777
   392
defined for the reals, along with many theorems such as this one about
paulson@10777
   393
exponentiation:
paulson@10777
   394
\begin{isabelle}
paulson@10777
   395
\isasymbar r\isasymbar \ \isacharcircum \ n\ =\ \isasymbar r\ \isacharcircum \ n\isasymbar 
paulson@10777
   396
\rulename{realpow_abs}
paulson@10777
   397
\end{isabelle}
paulson@10777
   398
paulson@10881
   399
\begin{warn}
paulson@10881
   400
Type \isa{real} is only available in the logic HOL-Real, which
paulson@10777
   401
is  HOL extended with the rather substantial development of the real
paulson@10777
   402
numbers.  Base your theory upon theory \isa{Real}, not the usual \isa{Main}.
paulson@10881
   403
\end{warn}
paulson@10777
   404
paulson@10777
   405
Also distributed with Isabelle is HOL-Hyperreal,
paulson@10777
   406
whose theory \isa{Hyperreal} defines the type \isa{hypreal} of non-standard
paulson@10777
   407
reals.  These
paulson@10777
   408
\textbf{hyperreals} include infinitesimals, which represent infinitely
paulson@10777
   409
small and infinitely large quantities; they facilitate proofs
paulson@10794
   410
about limits, differentiation and integration~\cite{fleuriot-jcm}.  The
paulson@10794
   411
development defines an infinitely large number, \isa{omega} and an
paulson@10881
   412
infinitely small positive number, \isa{epsilon}.  The 
paulson@10881
   413
relation $x\approx y$ means ``$x$ is infinitely close to~$y$''.