doc-src/TutorialI/Types/numerics.tex
author haftmann
Wed, 17 Jun 2009 11:00:14 +0200
changeset 31682 358cdcdf56d2
parent 31678 752f23a37240
child 33750 0a0d6d79d984
permissions -rw-r--r--
corrected some issues
paulson@11389
     1
paulson@11389
     2
\section{Numbers}
paulson@11389
     3
\label{sec:numbers}
paulson@11389
     4
paulson@11494
     5
\index{numbers|(}%
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@14400
    12
subtraction.  With subtraction, arithmetic reasoning is easier, which makes
paulson@14400
    13
the integers preferable to the natural numbers for
haftmann@31678
    14
complicated arithmetic expressions, even if they are non-negative.  There are also the types
paulson@14400
    15
\isa{rat}, \isa{real} and \isa{complex}: the rational, real and complex numbers.  Isabelle has no 
paulson@13979
    16
subtyping,  so the numeric
paulson@13979
    17
types are distinct and there are functions to convert between them.
paulson@14400
    18
Most numeric operations are overloaded: the same symbol can be
paulson@11174
    19
used at all numeric types. Table~\ref{tab:overloading} in the appendix
paulson@11174
    20
shows the most important operations, together with the priorities of the
paulson@14400
    21
infix symbols. Algebraic properties are organized using type classes
paulson@14400
    22
around algebraic concepts such as rings and fields;
paulson@14400
    23
a property such as the commutativity of addition is a single theorem 
paulson@14400
    24
(\isa{add_commute}) that applies to all numeric types.
paulson@10594
    25
paulson@11416
    26
\index{linear arithmetic}%
paulson@10594
    27
Many theorems involving numeric types can be proved automatically by
paulson@10594
    28
Isabelle's arithmetic decision procedure, the method
paulson@11416
    29
\methdx{arith}.  Linear arithmetic comprises addition, subtraction
paulson@10594
    30
and multiplication by constant factors; subterms involving other operators
paulson@10594
    31
are regarded as variables.  The procedure can be slow, especially if the
paulson@10594
    32
subgoal to be proved involves subtraction over type \isa{nat}, which 
nipkow@13996
    33
causes case splits.  On types \isa{nat} and \isa{int}, \methdx{arith}
paulson@14400
    34
can deal with quantifiers---this is known as Presburger arithmetic---whereas on type \isa{real} it cannot.
paulson@10594
    35
paulson@10594
    36
The simplifier reduces arithmetic expressions in other
paulson@10594
    37
ways, such as dividing through by common factors.  For problems that lie
paulson@10881
    38
outside the scope of automation, HOL provides hundreds of
paulson@10594
    39
theorems about multiplication, division, etc., that can be brought to
paulson@10881
    40
bear.  You can locate them using Proof General's Find
paulson@10881
    41
button.  A few lemmas are given below to show what
paulson@10794
    42
is available.
paulson@10594
    43
paulson@10594
    44
\subsection{Numeric Literals}
nipkow@10779
    45
\label{sec:numerals}
paulson@10594
    46
paulson@11416
    47
\index{numeric literals|(}%
paulson@12156
    48
The constants \cdx{0} and \cdx{1} are overloaded.  They denote zero and one,
paulson@12156
    49
respectively, for all numeric types.  Other values are expressed by numeric
wenzelm@21243
    50
literals, which consist of one or more decimal digits optionally preceeded by a minus sign (\isa{-}).  Examples are \isa{2}, \isa{-3} and
wenzelm@21243
    51
\isa{441223334678}.  Literals are available for the types of natural
wenzelm@21243
    52
numbers, integers, rationals, reals, etc.; they denote integer values of
wenzelm@21243
    53
arbitrary size.
paulson@10594
    54
paulson@10594
    55
Literals look like constants, but they abbreviate 
paulson@12156
    56
terms representing the number in a two's complement binary notation. 
paulson@10794
    57
Isabelle performs arithmetic on literals by rewriting rather 
paulson@10594
    58
than using the hardware arithmetic. In most cases arithmetic 
paulson@14400
    59
is fast enough, even for numbers in the millions. The arithmetic operations 
paulson@10794
    60
provided for literals include addition, subtraction, multiplication, 
paulson@10794
    61
integer division and remainder.  Fractions of literals (expressed using
paulson@10794
    62
division) are reduced to lowest terms.
paulson@10594
    63
paulson@11416
    64
\begin{warn}\index{overloading!and arithmetic}
paulson@10794
    65
The arithmetic operators are 
paulson@10594
    66
overloaded, so you must be careful to ensure that each numeric 
paulson@10594
    67
expression refers to a specific type, if necessary by inserting 
paulson@10594
    68
type constraints.  Here is an example of what can go wrong:
paulson@10794
    69
\par
paulson@10594
    70
\begin{isabelle}
paulson@12156
    71
\isacommand{lemma}\ "2\ *\ m\ =\ m\ +\ m"
paulson@10594
    72
\end{isabelle}
paulson@10594
    73
%
paulson@10594
    74
Carefully observe how Isabelle displays the subgoal:
paulson@10594
    75
\begin{isabelle}
paulson@12156
    76
\ 1.\ (2::'a)\ *\ m\ =\ m\ +\ m
paulson@10594
    77
\end{isabelle}
paulson@12156
    78
The type \isa{'a} given for the literal \isa{2} warns us that no numeric
paulson@10594
    79
type has been specified.  The problem is underspecified.  Given a type
paulson@10594
    80
constraint such as \isa{nat}, \isa{int} or \isa{real}, it becomes trivial.
paulson@10794
    81
\end{warn}
paulson@10794
    82
paulson@10881
    83
\begin{warn}
haftmann@31682
    84
\index{function@\isacommand {function} (command)!and numeric literals}  
paulson@11416
    85
Numeric literals are not constructors and therefore
paulson@11416
    86
must not be used in patterns.  For example, this declaration is
paulson@11416
    87
rejected:
paulson@10881
    88
\begin{isabelle}
haftmann@31682
    89
\isacommand{function}\ h\ \isakeyword{where}\isanewline
paulson@12156
    90
"h\ 3\ =\ 2"\isanewline
haftmann@31682
    91
\isacharbar "h\ i\ \ =\ i"
paulson@10881
    92
\end{isabelle}
paulson@10881
    93
paulson@10881
    94
You should use a conditional expression instead:
paulson@10881
    95
\begin{isabelle}
paulson@12156
    96
"h\ i\ =\ (if\ i\ =\ 3\ then\ 2\ else\ i)"
paulson@10881
    97
\end{isabelle}
paulson@11416
    98
\index{numeric literals|)}
paulson@10881
    99
\end{warn}
paulson@10881
   100
paulson@10594
   101
nipkow@11216
   102
\subsection{The Type of Natural Numbers, {\tt\slshape nat}}
paulson@10594
   103
paulson@11416
   104
\index{natural numbers|(}\index{*nat (type)|(}%
paulson@10594
   105
This type requires no introduction: we have been using it from the
paulson@10794
   106
beginning.  Hundreds of theorems about the natural numbers are
wenzelm@21243
   107
proved in the theories \isa{Nat} and \isa{Divides}.  
paulson@14400
   108
Basic properties of addition and multiplication are available through the
haftmann@31678
   109
axiomatic type class for semirings (\S\ref{sec:numeric-classes}).
paulson@10594
   110
paulson@10594
   111
\subsubsection{Literals}
paulson@11416
   112
\index{numeric literals!for type \protect\isa{nat}}%
paulson@12156
   113
The notational options for the natural  numbers are confusing.  Recall that an
paulson@12156
   114
overloaded constant can be defined independently for each type; the definition
paulson@12156
   115
of \cdx{1} for type \isa{nat} is
paulson@12156
   116
\begin{isabelle}
paulson@12156
   117
1\ \isasymequiv\ Suc\ 0
paulson@12156
   118
\rulename{One_nat_def}
paulson@12156
   119
\end{isabelle}
paulson@12156
   120
This is installed as a simplification rule, so the simplifier will replace
paulson@12156
   121
every occurrence of \isa{1::nat} by \isa{Suc\ 0}.  Literals are obviously
paulson@12156
   122
better than nested \isa{Suc}s at expressing large values.  But many theorems,
paulson@12156
   123
including the rewrite rules for primitive recursive functions, can only be
paulson@12156
   124
applied to terms of the form \isa{Suc\ $n$}.
paulson@12156
   125
paulson@12156
   126
The following default  simplification rules replace
paulson@10794
   127
small literals by zero and successor: 
paulson@10594
   128
\begin{isabelle}
paulson@12156
   129
2\ +\ n\ =\ Suc\ (Suc\ n)
paulson@10594
   130
\rulename{add_2_eq_Suc}\isanewline
paulson@12156
   131
n\ +\ 2\ =\ Suc\ (Suc\ n)
paulson@10594
   132
\rulename{add_2_eq_Suc'}
paulson@10594
   133
\end{isabelle}
paulson@12156
   134
It is less easy to transform \isa{100} into \isa{Suc\ 99} (for example), and
paulson@12156
   135
the simplifier will normally reverse this transformation.  Novices should
paulson@12156
   136
express natural numbers using \isa{0} and \isa{Suc} only.
paulson@10594
   137
paulson@10594
   138
\subsubsection{Division}
paulson@11416
   139
\index{division!for type \protect\isa{nat}}%
paulson@10881
   140
The infix operators \isa{div} and \isa{mod} are overloaded.
paulson@10881
   141
Isabelle/HOL provides the basic facts about quotient and remainder
paulson@10881
   142
on the natural numbers:
paulson@10594
   143
\begin{isabelle}
paulson@10594
   144
m\ mod\ n\ =\ (if\ m\ <\ n\ then\ m\ else\ (m\ -\ n)\ mod\ n)
paulson@10594
   145
\rulename{mod_if}\isanewline
paulson@10594
   146
m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%
paulson@11416
   147
\rulenamedx{mod_div_equality}
paulson@10594
   148
\end{isabelle}
paulson@10594
   149
paulson@10594
   150
Many less obvious facts about quotient and remainder are also provided. 
paulson@10594
   151
Here is a selection:
paulson@10594
   152
\begin{isabelle}
paulson@10594
   153
a\ *\ b\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
paulson@10594
   154
\rulename{div_mult1_eq}\isanewline
paulson@10594
   155
a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
nipkow@30208
   156
\rulename{mod_mult_right_eq}\isanewline
paulson@10594
   157
a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
paulson@10594
   158
\rulename{div_mult2_eq}\isanewline
paulson@10594
   159
a\ mod\ (b*c)\ =\ b * (a\ div\ b\ mod\ c)\ +\ a\ mod\ b%
paulson@10594
   160
\rulename{mod_mult2_eq}\isanewline
paulson@10594
   161
0\ <\ c\ \isasymLongrightarrow \ (c\ *\ a)\ div\ (c\ *\ b)\ =\ a\ div\ b%
paulson@14400
   162
\rulename{div_mult_mult1}\isanewline
paulson@14400
   163
(m\ mod\ n)\ *\ k\ =\ (m\ *\ k)\ mod\ (n\ *\ k)
paulson@14400
   164
\rulenamedx{mod_mult_distrib}\isanewline
paulson@14400
   165
m\ \isasymle \ n\ \isasymLongrightarrow \ m\ div\ k\ \isasymle \ n\ div\ k%
paulson@14400
   166
\rulename{div_le_mono}
paulson@10594
   167
\end{isabelle}
paulson@10594
   168
paulson@10594
   169
Surprisingly few of these results depend upon the
paulson@11416
   170
divisors' being nonzero.
paulson@11416
   171
\index{division!by zero}%
paulson@11416
   172
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@14400
   180
In \isa{div_mult_mult1} above, one of
nipkow@11161
   181
the two divisors (namely~\isa{c}) must still be nonzero.
paulson@10594
   182
paulson@11416
   183
The \textbf{divides} relation\index{divides relation}
paulson@11416
   184
has the standard definition, which
paulson@10594
   185
is overloaded over all numeric types: 
paulson@10594
   186
\begin{isabelle}
paulson@10594
   187
m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k
paulson@11416
   188
\rulenamedx{dvd_def}
paulson@10594
   189
\end{isabelle}
paulson@10594
   190
%
paulson@10594
   191
Section~\ref{sec:proving-euclid} discusses proofs involving this
paulson@10594
   192
relation.  Here are some of the facts proved about it:
paulson@10594
   193
\begin{isabelle}
paulson@10594
   194
\isasymlbrakk m\ dvd\ n;\ n\ dvd\ m\isasymrbrakk \ \isasymLongrightarrow \ m\ =\ n%
paulson@11416
   195
\rulenamedx{dvd_anti_sym}\isanewline
paulson@10594
   196
\isasymlbrakk k\ dvd\ m;\ k\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ k\ dvd\ (m\ +\ n)
paulson@11416
   197
\rulenamedx{dvd_add}
paulson@10594
   198
\end{isabelle}
paulson@10594
   199
paulson@14400
   200
\subsubsection{Subtraction}
paulson@14400
   201
paulson@14400
   202
There are no negative natural numbers, so \isa{m\ -\ n} equals zero unless 
paulson@14400
   203
\isa{m} exceeds~\isa{n}. The following is one of the few facts
paulson@10594
   204
about \isa{m\ -\ n} that is not subject to
paulson@14400
   205
the condition \isa{n\ \isasymle \  m}. 
paulson@14400
   206
\begin{isabelle}
paulson@14400
   207
(m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%
paulson@14400
   208
\rulenamedx{diff_mult_distrib}
paulson@14400
   209
\end{isabelle}
paulson@14400
   210
Natural number subtraction has few
paulson@10794
   211
nice properties; often you should remove it by simplifying with this split
paulson@14400
   212
rule.
paulson@10594
   213
\begin{isabelle}
paulson@10594
   214
P(a-b)\ =\ ((a<b\ \isasymlongrightarrow \ P\
paulson@10594
   215
0)\ \isasymand \ (\isasymforall d.\ a\ =\ b+d\ \isasymlongrightarrow \ P\
paulson@10594
   216
d))
paulson@10594
   217
\rulename{nat_diff_split}
paulson@10594
   218
\end{isabelle}
paulson@14400
   219
For example, splitting helps to prove the following fact.
paulson@10594
   220
\begin{isabelle}
paulson@12156
   221
\isacommand{lemma}\ "(n\ -\ 2)\ *\ (n\ +\ 2)\ =\ n\ *\ n\ -\ (4::nat)"\isanewline
paulson@12156
   222
\isacommand{apply}\ (simp\ split:\ nat_diff_split,\ clarify)\isanewline
paulson@12156
   223
\ 1.\ \isasymAnd d.\ \isasymlbrakk n\ <\ 2;\ n\ *\ n\ =\ 4\ +\ d\isasymrbrakk \ \isasymLongrightarrow \ d\ =\ 0
paulson@12156
   224
\end{isabelle}
paulson@12156
   225
The result lies outside the scope of linear arithmetic, but
paulson@12156
   226
 it is easily found
paulson@12156
   227
if we explicitly split \isa{n<2} as \isa{n=0} or \isa{n=1}:
paulson@12156
   228
\begin{isabelle}
paulson@12156
   229
\isacommand{apply}\ (subgoal_tac\ "n=0\ |\ n=1",\ force,\ arith)\isanewline
paulson@10594
   230
\isacommand{done}
paulson@14400
   231
\end{isabelle}%%%%%%
paulson@11416
   232
\index{natural numbers|)}\index{*nat (type)|)}
paulson@11416
   233
paulson@10594
   234
nipkow@11216
   235
\subsection{The Type of Integers, {\tt\slshape int}}
paulson@10594
   236
paulson@11416
   237
\index{integers|(}\index{*int (type)|(}%
paulson@14400
   238
Reasoning methods for the integers resemble those for the natural numbers, 
wenzelm@21243
   239
but induction and
wenzelm@21243
   240
the constant \isa{Suc} are not available.  HOL provides many lemmas for
wenzelm@21243
   241
proving inequalities involving integer multiplication and division, similar
wenzelm@21243
   242
to those shown above for type~\isa{nat}. The laws of addition, subtraction
wenzelm@21243
   243
and multiplication are available through the axiomatic type class for rings
haftmann@31678
   244
(\S\ref{sec:numeric-classes}).
paulson@10594
   245
paulson@14400
   246
The \rmindex{absolute value} function \cdx{abs} is overloaded, and is 
paulson@14400
   247
defined for all types that involve negative numbers, including the integers.
paulson@10881
   248
The \isa{arith} method can prove facts about \isa{abs} automatically, 
paulson@10881
   249
though as it does so by case analysis, the cost can be exponential.
paulson@10881
   250
\begin{isabelle}
paulson@11174
   251
\isacommand{lemma}\ "abs\ (x+y)\ \isasymle \ abs\ x\ +\ abs\ (y\ ::\ int)"\isanewline
paulson@10881
   252
\isacommand{by}\ arith
paulson@10881
   253
\end{isabelle}
paulson@10794
   254
paulson@11416
   255
For division and remainder,\index{division!by negative numbers}
paulson@11416
   256
the treatment of negative divisors follows
paulson@10794
   257
mathematical practice: the sign of the remainder follows that
paulson@10594
   258
of the divisor:
paulson@10594
   259
\begin{isabelle}
paulson@12156
   260
0\ <\ b\ \isasymLongrightarrow \ 0\ \isasymle \ a\ mod\ b%
paulson@10594
   261
\rulename{pos_mod_sign}\isanewline
paulson@12156
   262
0\ <\ b\ \isasymLongrightarrow \ a\ mod\ b\ <\ b%
paulson@10594
   263
\rulename{pos_mod_bound}\isanewline
paulson@12156
   264
b\ <\ 0\ \isasymLongrightarrow \ a\ mod\ b\ \isasymle \ 0
paulson@10594
   265
\rulename{neg_mod_sign}\isanewline
paulson@12156
   266
b\ <\ 0\ \isasymLongrightarrow \ b\ <\ a\ mod\ b%
paulson@10594
   267
\rulename{neg_mod_bound}
paulson@10594
   268
\end{isabelle}
paulson@10594
   269
ML treats negative divisors in the same way, but most computer hardware
paulson@10594
   270
treats signed operands using the same rules as for multiplication.
paulson@10794
   271
Many facts about quotients and remainders are provided:
paulson@10594
   272
\begin{isabelle}
paulson@10594
   273
(a\ +\ b)\ div\ c\ =\isanewline
paulson@10594
   274
a\ div\ c\ +\ b\ div\ c\ +\ (a\ mod\ c\ +\ b\ mod\ c)\ div\ c%
paulson@10594
   275
\rulename{zdiv_zadd1_eq}
paulson@10594
   276
\par\smallskip
paulson@10594
   277
(a\ +\ b)\ mod\ c\ =\ (a\ mod\ c\ +\ b\ mod\ c)\ mod\ c%
nipkow@30208
   278
\rulename{mod_add_eq}
paulson@10594
   279
\end{isabelle}
paulson@10594
   280
paulson@10594
   281
\begin{isabelle}
paulson@10594
   282
(a\ *\ b)\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
paulson@10594
   283
\rulename{zdiv_zmult1_eq}\isanewline
paulson@10594
   284
(a\ *\ b)\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
paulson@10594
   285
\rulename{zmod_zmult1_eq}
paulson@10594
   286
\end{isabelle}
paulson@10594
   287
paulson@10594
   288
\begin{isabelle}
paulson@12156
   289
0\ <\ c\ \isasymLongrightarrow \ a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
paulson@10594
   290
\rulename{zdiv_zmult2_eq}\isanewline
paulson@12156
   291
0\ <\ c\ \isasymLongrightarrow \ a\ mod\ (b*c)\ =\ b*(a\ div\ b\ mod\
paulson@10594
   292
c)\ +\ a\ mod\ b%
paulson@10594
   293
\rulename{zmod_zmult2_eq}
paulson@10594
   294
\end{isabelle}
paulson@10594
   295
The last two differ from their natural number analogues by requiring
paulson@10594
   296
\isa{c} to be positive.  Since division by zero yields zero, we could allow
paulson@10594
   297
\isa{c} to be zero.  However, \isa{c} cannot be negative: a counterexample
paulson@10594
   298
is
paulson@10594
   299
$\isa{a} = 7$, $\isa{b} = 2$ and $\isa{c} = -3$, when the left-hand side of
paulson@14400
   300
\isa{zdiv_zmult2_eq} is $-2$ while the right-hand side is~$-1$.
paulson@14400
   301
The prefix~\isa{z} in many theorem names recalls the use of $\mathbb{Z}$ to
paulson@14400
   302
denote the set of integers.%
paulson@11416
   303
\index{integers|)}\index{*int (type)|)}
paulson@10594
   304
paulson@13979
   305
Induction is less important for integers than it is for the natural numbers, but it can be valuable if the range of integers has a lower or upper bound.  There are four rules for integer induction, corresponding to the possible relations of the bound ($\geq$, $>$, $\leq$ and $<$):
paulson@13750
   306
\begin{isabelle}
paulson@13750
   307
\isasymlbrakk k\ \isasymle \ i;\ P\ k;\ \isasymAnd i.\ \isasymlbrakk k\ \isasymle \ i;\ P\ i\isasymrbrakk \ \isasymLongrightarrow \ P(i+1)\isasymrbrakk \ \isasymLongrightarrow \ P\ i%
paulson@13750
   308
\rulename{int_ge_induct}\isanewline
paulson@13750
   309
\isasymlbrakk k\ <\ i;\ P(k+1);\ \isasymAnd i.\ \isasymlbrakk k\ <\ i;\ P\ i\isasymrbrakk \ \isasymLongrightarrow \ P(i+1)\isasymrbrakk \ \isasymLongrightarrow \ P\ i%
paulson@13750
   310
\rulename{int_gr_induct}\isanewline
paulson@13750
   311
\isasymlbrakk i\ \isasymle \ k;\ P\ k;\ \isasymAnd i.\ \isasymlbrakk i\ \isasymle \ k;\ P\ i\isasymrbrakk \ \isasymLongrightarrow \ P(i-1)\isasymrbrakk \ \isasymLongrightarrow \ P\ i%
paulson@13750
   312
\rulename{int_le_induct}\isanewline
paulson@13750
   313
\isasymlbrakk i\ <\ k;\ P(k-1);\ \isasymAnd i.\ \isasymlbrakk i\ <\ k;\ P\ i\isasymrbrakk \ \isasymLongrightarrow \ P(i-1)\isasymrbrakk \ \isasymLongrightarrow \ P\ i%
paulson@13750
   314
\rulename{int_less_induct}
paulson@13750
   315
\end{isabelle}
paulson@13750
   316
paulson@10594
   317
paulson@14400
   318
\subsection{The Types of Rational, Real and Complex Numbers}
nipkow@16359
   319
\label{sec:real}
paulson@10594
   320
paulson@14400
   321
\index{rational numbers|(}\index{*rat (type)|(}%
paulson@11416
   322
\index{real numbers|(}\index{*real (type)|(}%
paulson@14400
   323
\index{complex numbers|(}\index{*complex (type)|(}%
paulson@14400
   324
These types provide true division, the overloaded operator \isa{/}, 
paulson@14400
   325
which differs from the operator \isa{div} of the 
paulson@14400
   326
natural numbers and integers. The rationals and reals are 
paulson@14400
   327
\textbf{dense}: between every two distinct numbers lies another.
paulson@14400
   328
This property follows from the division laws, since if $x\not=y$ then $(x+y)/2$ lies between them:
paulson@10777
   329
\begin{isabelle}
paulson@14400
   330
a\ <\ b\ \isasymLongrightarrow \ \isasymexists r.\ a\ <\ r\ \isasymand \ r\ <\ b%
paulson@14295
   331
\rulename{dense}
paulson@10777
   332
\end{isabelle}
paulson@10777
   333
wenzelm@21243
   334
The real numbers are, moreover, \textbf{complete}: every set of reals that
wenzelm@21243
   335
is bounded above has a least upper bound.  Completeness distinguishes the
wenzelm@21243
   336
reals from the rationals, for which the set $\{x\mid x^2<2\}$ has no least
wenzelm@27093
   337
upper bound.  (It could only be $\surd2$, which is irrational.) The
wenzelm@21243
   338
formalization of completeness, which is complicated, 
haftmann@31678
   339
can be found in theory \texttt{RComplete}.
paulson@14400
   340
paulson@14400
   341
Numeric literals\index{numeric literals!for type \protect\isa{real}}
paulson@14400
   342
for type \isa{real} have the same syntax as those for type
paulson@14400
   343
\isa{int} and only express integral values.  Fractions expressed
paulson@14400
   344
using the division operator are automatically simplified to lowest terms:
paulson@14400
   345
\begin{isabelle}
paulson@14400
   346
\ 1.\ P\ ((3\ /\ 4)\ *\ (8\ /\ 15))\isanewline
paulson@14400
   347
\isacommand{apply} simp\isanewline
paulson@14400
   348
\ 1.\ P\ (2\ /\ 5)
paulson@14400
   349
\end{isabelle}
paulson@14400
   350
Exponentiation can express floating-point values such as
paulson@14400
   351
\isa{2 * 10\isacharcircum6}, but at present no special simplification
paulson@14400
   352
is performed.
paulson@14400
   353
paulson@14400
   354
\begin{warn}
haftmann@31678
   355
Types \isa{rat}, \isa{real} and \isa{complex} are provided by theory HOL-Complex, which is
haftmann@31678
   356
Main extended with a definitional development of the rational, real and complex
nipkow@16359
   357
numbers.  Base your theory upon theory \thydx{Complex_Main}, not the
haftmann@31678
   358
usual \isa{Main}.%
paulson@14400
   359
\end{warn}
paulson@14400
   360
haftmann@31678
   361
Available in the logic HOL-NSA is the
paulson@14400
   362
theory \isa{Hyperreal}, which define the type \tydx{hypreal} of 
paulson@14400
   363
\rmindex{non-standard reals}.  These
paulson@14400
   364
\textbf{hyperreals} include infinitesimals, which represent infinitely
paulson@14400
   365
small and infinitely large quantities; they facilitate proofs
paulson@14400
   366
about limits, differentiation and integration~\cite{fleuriot-jcm}.  The
paulson@14400
   367
development defines an infinitely large number, \isa{omega} and an
paulson@14400
   368
infinitely small positive number, \isa{epsilon}.  The 
paulson@14400
   369
relation $x\approx y$ means ``$x$ is infinitely close to~$y$.''
paulson@14400
   370
Theory \isa{Hyperreal} also defines transcendental functions such as sine,
paulson@14400
   371
cosine, exponential and logarithm --- even the versions for type
paulson@14400
   372
\isa{real}, because they are defined using nonstandard limits.%
paulson@14400
   373
\index{rational numbers|)}\index{*rat (type)|)}%
paulson@14400
   374
\index{real numbers|)}\index{*real (type)|)}%
paulson@14400
   375
\index{complex numbers|)}\index{*complex (type)|)}
paulson@14400
   376
paulson@14400
   377
haftmann@31678
   378
\subsection{The Numeric Type Classes}\label{sec:numeric-classes}
paulson@14400
   379
paulson@14400
   380
Isabelle/HOL organises its numeric theories using axiomatic type classes.
paulson@14400
   381
Hundreds of basic properties are proved in the theory \isa{Ring_and_Field}.
paulson@14400
   382
These lemmas are available (as simprules if they were declared as such)
paulson@14400
   383
for all numeric types satisfying the necessary axioms. The theory defines
paulson@23525
   384
dozens of type classes, such as the following:
paulson@14400
   385
\begin{itemize}
paulson@14400
   386
\item 
wenzelm@21243
   387
\tcdx{semiring} and \tcdx{ordered_semiring}: a \emph{semiring}
paulson@23525
   388
provides the associative operators \isa{+} and~\isa{*}, with \isa{0} and~\isa{1}
paulson@23525
   389
as their respective identities. The operators enjoy the usual distributive law,
paulson@23525
   390
and addition (\isa{+}) is also commutative.
paulson@23525
   391
An \emph{ordered semiring} is also linearly
wenzelm@21243
   392
ordered, with addition and multiplication respecting the ordering. Type \isa{nat} is an ordered semiring.
paulson@14400
   393
\item 
wenzelm@21243
   394
\tcdx{ring} and \tcdx{ordered_ring}: a \emph{ring} extends a semiring
wenzelm@21243
   395
with unary minus (the additive inverse) and subtraction (both
wenzelm@21243
   396
denoted~\isa{-}). An \emph{ordered ring} includes the absolute value
wenzelm@21243
   397
function, \cdx{abs}. Type \isa{int} is an ordered ring.
paulson@14400
   398
\item 
wenzelm@21243
   399
\tcdx{field} and \tcdx{ordered_field}: a field extends a ring with the
wenzelm@27093
   400
multiplicative inverse (called simply \cdx{inverse} and division~(\isa{/})).
wenzelm@21243
   401
An ordered field is based on an ordered ring. Type \isa{complex} is a field, while type \isa{real} is an ordered field.
paulson@14400
   402
\item 
paulson@14400
   403
\tcdx{division_by_zero} includes all types where \isa{inverse 0 = 0}
paulson@14400
   404
and \isa{a / 0 = 0}. These include all of Isabelle's standard numeric types.
paulson@14400
   405
However, the basic properties of fields are derived without assuming
wenzelm@21243
   406
division by zero.
wenzelm@21243
   407
\end{itemize}
paulson@14400
   408
paulson@23525
   409
Hundreds of basic lemmas are proved, each of which
wenzelm@21243
   410
holds for all types in the corresponding type class. In most
paulson@23525
   411
cases, it is obvious whether a property is valid for a particular type. No
paulson@23525
   412
abstract properties involving subtraction hold for type \isa{nat};
paulson@23525
   413
instead, theorems such as
paulson@23525
   414
\isa{diff_mult_distrib} are proved specifically about subtraction on
wenzelm@21243
   415
type~\isa{nat}. All abstract properties involving division require a field.
wenzelm@21243
   416
Obviously, all properties involving orderings required an ordered
wenzelm@21243
   417
structure.
paulson@14400
   418
paulson@23504
   419
The class \tcdx{ring_no_zero_divisors} of rings without zero divisors satisfies a number of natural cancellation laws, the first of which characterizes this class:
paulson@14400
   420
\begin{isabelle}
paulson@14400
   421
(a\ *\ b\ =\ (0::'a))\ =\ (a\ =\ (0::'a)\ \isasymor \ b\ =\ (0::'a))
paulson@14400
   422
\rulename{mult_eq_0_iff}\isanewline
paulson@14400
   423
(a\ *\ c\ =\ b\ *\ c)\ =\ (c\ =\ (0::'a)\ \isasymor \ a\ =\ b)
paulson@14400
   424
\rulename{mult_cancel_right}
paulson@14400
   425
\end{isabelle}
nipkow@16412
   426
\begin{pgnote}
nipkow@16523
   427
Setting the flag \pgmenu{Isabelle} $>$ \pgmenu{Settings} $>$
nipkow@16523
   428
\pgmenu{Show Sorts} will display the type classes of all type variables.
nipkow@16412
   429
\end{pgnote}
nipkow@16412
   430
\noindent
paulson@23504
   431
Here is how the theorem \isa{mult_cancel_left} appears with the flag set.
paulson@14400
   432
\begin{isabelle}
paulson@23504
   433
((c::'a::ring_no_zero_divisors)\ *\ (a::'a::ring_no_zero_divisors) =\isanewline
paulson@23504
   434
\ c\ *\ (b::'a::ring_no_zero_divisors))\ =\isanewline
paulson@23504
   435
(c\ =\ (0::'a::ring_no_zero_divisors)\ \isasymor\ a\ =\ b)
paulson@14400
   436
\end{isabelle}
paulson@14400
   437
paulson@14400
   438
paulson@14400
   439
\subsubsection{Simplifying with the AC-Laws}
paulson@14400
   440
Suppose that two expressions are equal, differing only in 
paulson@14400
   441
associativity and commutativity of addition.  Simplifying with the
paulson@14400
   442
following equations sorts the terms and groups them to the right, making
paulson@14400
   443
the two expressions identical.
paulson@14400
   444
\begin{isabelle}
paulson@14400
   445
a\ +\ b\ +\ c\ =\ a\ +\ (b\ +\ c)
paulson@14400
   446
\rulenamedx{add_assoc}\isanewline
paulson@14400
   447
a\ +\ b\ =\ b\ +\ a%
paulson@14400
   448
\rulenamedx{add_commute}\isanewline
paulson@14400
   449
a\ +\ (b\ +\ c)\ =\ b\ +\ (a\ +\ c)
paulson@14400
   450
\rulename{add_left_commute}
paulson@14400
   451
\end{isabelle}
paulson@14400
   452
The name \isa{add_ac}\index{*add_ac (theorems)} 
paulson@14400
   453
refers to the list of all three theorems; similarly
paulson@14400
   454
there is \isa{mult_ac}.\index{*mult_ac (theorems)} 
paulson@14400
   455
They are all proved for semirings and therefore hold for all numeric types.
paulson@14400
   456
paulson@14400
   457
Here is an example of the sorting effect.  Start
paulson@14400
   458
with this goal, which involves type \isa{nat}.
paulson@14400
   459
\begin{isabelle}
paulson@14400
   460
\ 1.\ Suc\ (i\ +\ j\ *\ l\ *\ k\ +\ m\ *\ n)\ =\
paulson@14400
   461
f\ (n\ *\ m\ +\ i\ +\ k\ *\ j\ *\ l)
paulson@14400
   462
\end{isabelle}
paulson@14400
   463
%
paulson@14400
   464
Simplify using  \isa{add_ac} and \isa{mult_ac}.
paulson@14400
   465
\begin{isabelle}
paulson@14400
   466
\isacommand{apply}\ (simp\ add:\ add_ac\ mult_ac)
paulson@14400
   467
\end{isabelle}
paulson@14400
   468
%
paulson@14400
   469
Here is the resulting subgoal.
paulson@14400
   470
\begin{isabelle}
paulson@14400
   471
\ 1.\ Suc\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))\
paulson@14400
   472
=\ f\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))%
paulson@14400
   473
\end{isabelle}
paulson@14400
   474
paulson@14400
   475
paulson@14400
   476
\subsubsection{Division Laws for Fields}
paulson@14400
   477
paulson@10777
   478
Here is a selection of rules about the division operator.  The following
paulson@10777
   479
are installed as default simplification rules in order to express
paulson@10777
   480
combinations of products and quotients as rational expressions:
paulson@10777
   481
\begin{isabelle}
paulson@14288
   482
a\ *\ (b\ /\ c)\ =\ a\ *\ b\ /\ c
paulson@14288
   483
\rulename{times_divide_eq_right}\isanewline
paulson@14288
   484
b\ /\ c\ *\ a\ =\ b\ *\ a\ /\ c
paulson@14288
   485
\rulename{times_divide_eq_left}\isanewline
paulson@14288
   486
a\ /\ (b\ /\ c)\ =\ a\ *\ c\ /\ b
paulson@14288
   487
\rulename{divide_divide_eq_right}\isanewline
paulson@14288
   488
a\ /\ b\ /\ c\ =\ a\ /\ (b\ *\ c)
paulson@14288
   489
\rulename{divide_divide_eq_left}
paulson@10777
   490
\end{isabelle}
paulson@10777
   491
paulson@10777
   492
Signs are extracted from quotients in the hope that complementary terms can
paulson@10777
   493
then be cancelled:
paulson@10777
   494
\begin{isabelle}
paulson@14295
   495
-\ (a\ /\ b)\ =\ -\ a\ /\ b
paulson@14295
   496
\rulename{minus_divide_left}\isanewline
paulson@14295
   497
-\ (a\ /\ b)\ =\ a\ /\ -\ b
paulson@14295
   498
\rulename{minus_divide_right}
paulson@10777
   499
\end{isabelle}
paulson@10777
   500
paulson@10777
   501
The following distributive law is available, but it is not installed as a
paulson@10777
   502
simplification rule.
paulson@10777
   503
\begin{isabelle}
paulson@14295
   504
(a\ +\ b)\ /\ c\ =\ a\ /\ c\ +\ b\ /\ c%
paulson@14295
   505
\rulename{add_divide_distrib}
paulson@10777
   506
\end{isabelle}
paulson@10777
   507
paulson@10594
   508
paulson@14400
   509
\subsubsection{Absolute Value}
paulson@14400
   510
paulson@14400
   511
The \rmindex{absolute value} function \cdx{abs} is available for all 
paulson@14400
   512
ordered rings, including types \isa{int}, \isa{rat} and \isa{real}.
paulson@14400
   513
It satisfies many properties,
paulson@14400
   514
such as the following:
paulson@10777
   515
\begin{isabelle}
paulson@14400
   516
\isasymbar x\ *\ y\isasymbar \ =\ \isasymbar x\isasymbar \ *\ \isasymbar y\isasymbar 
paulson@14400
   517
\rulename{abs_mult}\isanewline
paulson@14400
   518
(\isasymbar a\isasymbar \ \isasymle \ b)\ =\ (a\ \isasymle \ b\ \isasymand \ -\ a\ \isasymle \ b)
paulson@14400
   519
\rulename{abs_le_iff}\isanewline
paulson@14400
   520
\isasymbar a\ +\ b\isasymbar \ \isasymle \ \isasymbar a\isasymbar \ +\ \isasymbar b\isasymbar 
paulson@14400
   521
\rulename{abs_triangle_ineq}
paulson@10777
   522
\end{isabelle}
paulson@10777
   523
paulson@10881
   524
\begin{warn}
paulson@14400
   525
The absolute value bars shown above cannot be typed on a keyboard.  They
paulson@14400
   526
can be entered using the X-symbol package.  In \textsc{ascii}, type \isa{abs x} to
paulson@14400
   527
get \isa{\isasymbar x\isasymbar}.
paulson@10881
   528
\end{warn}
paulson@10777
   529
paulson@14400
   530
paulson@14400
   531
\subsubsection{Raising to a Power}
paulson@14400
   532
haftmann@31682
   533
Another type class, \tcdx{ordered\_idom}, specifies rings that also have 
paulson@14400
   534
exponentation to a natural number power, defined using the obvious primitive
paulson@14400
   535
recursion. Theory \thydx{Power} proves various theorems, such as the 
paulson@14400
   536
following.
paulson@14400
   537
\begin{isabelle}
paulson@14400
   538
a\ \isacharcircum \ (m\ +\ n)\ =\ a\ \isacharcircum \ m\ *\ a\ \isacharcircum \ n%
paulson@14400
   539
\rulename{power_add}\isanewline
paulson@14400
   540
a\ \isacharcircum \ (m\ *\ n)\ =\ (a\ \isacharcircum \ m)\ \isacharcircum \ n%
paulson@14400
   541
\rulename{power_mult}\isanewline
paulson@14400
   542
\isasymbar a\ \isacharcircum \ n\isasymbar \ =\ \isasymbar a\isasymbar \ \isacharcircum \ n%
paulson@14400
   543
\rulename{power_abs}
paulson@14400
   544
\end{isabelle}%%%%%%%%%%%%%%%%%%%%%%%%%
nipkow@13996
   545
\index{numbers|)}