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