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