doc-src/TutorialI/Types/numerics.tex
author wenzelm
Wed, 03 Jan 2001 21:24:29 +0100
changeset 10774 4de3a0d3ae28
parent 10654 458068404143
child 10777 a5a6255748c3
permissions -rw-r--r--
recdef_tc;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10594
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     1
Our examples until now have used the type of \textbf{natural numbers},
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     2
\isa{nat}.  This is a recursive datatype generated by the constructors
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     3
zero  and successor, so it works well with inductive proofs and primitive
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     4
recursive function definitions. Isabelle/HOL also has the type \isa{int}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     5
of \textbf{integers}, which gives up induction in exchange  for proper subtraction.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     6
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     7
The integers are preferable to the natural  numbers for reasoning about
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     8
complicated arithmetic expressions. For  example, a termination proof
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
     9
typically involves an integer metric  that is shown to decrease at each
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    10
loop iteration. Even if the  metric cannot become negative, proofs about it
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    11
are usually easier  if the integers are used rather than the natural
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    12
numbers. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    13
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    14
The logic Isabelle/HOL-Real also has the type \isa{real} of real numbers
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    15
and even the type \isa{hypreal} of non-standard reals. These
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    16
\textbf{hyperreals} include  infinitesimals, which represent infinitely
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    17
small and infinitely  large quantities; they greatly facilitate proofs
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    18
about limits,  differentiation and integration.  Isabelle has no subtyping, 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    19
so the numeric types are distinct and there are 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    20
functions to convert between them. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    21
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    22
Many theorems involving numeric types can be proved automatically by
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    23
Isabelle's arithmetic decision procedure, the method
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    24
\isa{arith}.  Linear arithmetic comprises addition, subtraction
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    25
and multiplication by constant factors; subterms involving other operators
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    26
are regarded as variables.  The procedure can be slow, especially if the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    27
subgoal to be proved involves subtraction over type \isa{nat}, which 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    28
causes case splits.  
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    29
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    30
The simplifier reduces arithmetic expressions in other
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    31
ways, such as dividing through by common factors.  For problems that lie
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    32
outside the scope of automation, the library has hundreds of
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    33
theorems about multiplication, division, etc., that can be brought to
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    34
bear.  You can find find them by browsing the library.  Some
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    35
useful lemmas are shown below.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    36
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    37
\subsection{Numeric Literals}
10608
620647438780 *** empty log message ***
nipkow
parents: 10594
diff changeset
    38
\label{sec:numerals}
10594
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    39
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    40
Literals are available for the types of natural numbers, integers 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    41
and reals and denote integer values of arbitrary size. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    42
\REMARK{hypreal?}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    43
They begin 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    44
with a number sign (\isa{\#}), have an optional minus sign (\isa{-}) and 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    45
then one or more decimal digits. Examples are \isa{\#0}, \isa{\#-3} 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    46
and \isa{\#441223334678}.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    47
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    48
Literals look like constants, but they abbreviate 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    49
terms, representing the number in a two's complement binary notation. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    50
Isabelle performs arithmetic on literals by rewriting, rather 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    51
than using the hardware arithmetic. In most cases arithmetic 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    52
is fast enough, even for large numbers. The arithmetic operations 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    53
provided for literals are addition, subtraction, multiplication, 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    54
integer division and remainder. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    55
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    56
\emph{Beware}: the arithmetic operators are 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    57
overloaded, so you must be careful to ensure that each numeric 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    58
expression refers to a specific type, if necessary by inserting 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    59
type constraints.  Here is an example of what can go wrong:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    60
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    61
\isacommand{lemma}\ "\#2\ *\ m\ =\ m\ +\ m"
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    62
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    63
%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    64
Carefully observe how Isabelle displays the subgoal:
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
\ 1.\ (\#2::'a)\ *\ 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
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
    69
type has been specified.  The problem is underspecified.  Given a type
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    70
constraint such as \isa{nat}, \isa{int} or \isa{real}, it becomes trivial.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    71
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    72
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    73
\subsection{The type of natural numbers, {\tt\slshape nat}}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    74
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    75
This type requires no introduction: we have been using it from the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    76
start.  Hundreds of useful lemmas about arithmetic on type \isa{nat} are
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    77
proved in the theories \isa{Nat}, \isa{NatArith} and \isa{Divides}.  Only
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    78
in exceptional circumstances should you resort to induction.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    79
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    80
\subsubsection{Literals}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    81
The notational options for the natural numbers can be confusing. The 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    82
constant \isa{0} is overloaded to serve as the neutral value 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    83
in a variety of additive types. The symbols \isa{1} and \isa{2} are 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    84
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
    85
respectively. The literals \isa{\#0}, \isa{\#1} and \isa{\#2}  are
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    86
entirely different from \isa{0}, \isa{1} and \isa{2}. You  will
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    87
sometimes prefer one notation to the other. Literals are  obviously
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    88
necessary to express large values, while \isa{0} and \isa{Suc}  are
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    89
needed in order to match many theorems, including the rewrite  rules for
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    90
primitive recursive functions. The following default  simplification rules
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    91
replace small literals by zero and successor: 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    92
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    93
\#0\ =\ 0
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    94
\rulename{numeral_0_eq_0}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    95
\#1\ =\ 1
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    96
\rulename{numeral_1_eq_1}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    97
\#2\ +\ n\ =\ Suc\ (Suc\ n)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    98
\rulename{add_2_eq_Suc}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
    99
n\ +\ \#2\ =\ Suc\ (Suc\ n)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   100
\rulename{add_2_eq_Suc'}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   101
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   102
In special circumstances, you may wish to remove or reorient 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   103
these rules. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   104
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   105
\subsubsection{Typical lemmas}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   106
Inequalities involving addition and subtraction alone can be proved
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   107
automatically.  Lemmas such as these can be used to prove inequalities
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   108
involving multiplication and division:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   109
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   110
\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
   111
\rulename{mult_le_mono}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   112
\isasymlbrakk i\ <\ j;\ 0\ <\ k\isasymrbrakk \ \isasymLongrightarrow \ i\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   113
*\ k\ <\ j\ *\ k%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   114
\rulename{mult_less_mono1}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   115
m\ \isasymle \ n\ \isasymLongrightarrow \ m\ div\ k\ \isasymle \ n\ div\ k%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   116
\rulename{div_le_mono}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   117
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   118
%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   119
Various distributive laws concerning multiplication are available:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   120
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   121
(m\ +\ n)\ *\ k\ =\ m\ *\ k\ +\ n\ *\ k%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   122
\rulename{add_mult_distrib}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   123
(m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   124
\rulename{diff_mult_distrib}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   125
(m\ mod\ n)\ *\ k\ =\ (m\ *\ k)\ mod\ (n\ *\ k)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   126
\rulename{mod_mult_distrib}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   127
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   128
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   129
\subsubsection{Division}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   130
The library contains the basic facts about quotient and remainder
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   131
(including the analogous equation, \isa{div_if}):
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   132
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   133
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
   134
\rulename{mod_if}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   135
m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   136
\rulename{mod_div_equality}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   137
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   138
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   139
Many less obvious facts about quotient and remainder are also provided. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   140
Here is a selection:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   141
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   142
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
   143
\rulename{div_mult1_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   144
a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   145
\rulename{mod_mult1_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   146
a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   147
\rulename{div_mult2_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   148
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
   149
\rulename{mod_mult2_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   150
0\ <\ c\ \isasymLongrightarrow \ (c\ *\ a)\ div\ (c\ *\ b)\ =\ a\ div\ b%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   151
\rulename{div_mult_mult1}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   152
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   153
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   154
Surprisingly few of these results depend upon the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   155
divisors' being nonzero.  Isabelle/HOL defines division by zero:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   156
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   157
a\ div\ 0\ =\ 0
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   158
\rulename{DIVISION_BY_ZERO_DIV}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   159
a\ mod\ 0\ =\ a%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   160
\rulename{DIVISION_BY_ZERO_MOD}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   161
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   162
As a concession to convention, these equations are not installed as default
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   163
simplification rules but are merely used to remove nonzero-divisor
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   164
hypotheses by case analysis.  In \isa{div_mult_mult1} above, one of
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   165
the two divisors (namely~\isa{c}) must be still be nonzero.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   166
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   167
The \textbf{divides} relation has the standard definition, which
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   168
is overloaded over all numeric types: 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   169
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   170
m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   171
\rulename{dvd_def}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   172
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   173
%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   174
Section~\ref{sec:proving-euclid} discusses proofs involving this
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   175
relation.  Here are some of the facts proved about it:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   176
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   177
\isasymlbrakk m\ dvd\ n;\ n\ dvd\ m\isasymrbrakk \ \isasymLongrightarrow \ m\ =\ n%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   178
\rulename{dvd_anti_sym}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   179
\isasymlbrakk k\ dvd\ m;\ k\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ k\ dvd\ (m\ +\ n)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   180
\rulename{dvd_add}
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
\subsubsection{Simplifier tricks}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   184
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
   185
about \isa{m\ -\ n} that is not subject to
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   186
the condition \isa{n\ \isasymle \  m}.  Natural number subtraction has few
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   187
nice properties; often it is best to remove it from a subgoal
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   188
using this split rule:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   189
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   190
P(a-b)\ =\ ((a<b\ \isasymlongrightarrow \ P\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   191
0)\ \isasymand \ (\isasymforall d.\ a\ =\ b+d\ \isasymlongrightarrow \ P\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   192
d))
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   193
\rulename{nat_diff_split}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   194
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   195
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
   196
linear arithmetic:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   197
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   198
\isacommand{lemma}\ "(n-1)*(n+1)\ =\ n*n\ -\ 1"\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   199
\isacommand{apply}\ (simp\ split:\ nat_diff_split)\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   200
\isacommand{done}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   201
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   202
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   203
Suppose that two expressions are equal, differing only in 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   204
associativity and commutativity of addition.  Simplifying with the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   205
following equations sorts the terms and groups them to the right, making
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   206
the two expressions identical:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   207
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   208
m\ +\ n\ +\ k\ =\ m\ +\ (n\ +\ k)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   209
\rulename{add_assoc}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   210
m\ +\ n\ =\ n\ +\ m%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   211
\rulename{add_commute}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   212
x\ +\ (y\ +\ z)\ =\ y\ +\ (x\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   213
+\ z)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   214
\rulename{add_left_commute}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   215
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   216
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
   217
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
   218
with this goal:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   219
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   220
\ 1.\ Suc\ (i\ +\ j\ *\ l\ *\ k\ +\ m\ *\ n)\ =\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   221
f\ (n\ *\ m\ +\ i\ +\ k\ *\ j\ *\ l)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   222
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   223
%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   224
Simplify using  \isa{add_ac} and \isa{mult_ac}:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   225
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   226
\isacommand{apply}\ (simp\ add:\ add_ac\ mult_ac)
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   227
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   228
%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   229
Here is the resulting subgoal:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   230
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   231
\ 1.\ Suc\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   232
=\ f\ (i\ +\ (m\ *\ n\ +\ j\ *\ (k\ *\ l)))%
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
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   236
\subsection{The type of integers, {\tt\slshape int}}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   237
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   238
Reasoning methods resemble those for the natural numbers, but
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   239
induction and the constant \isa{Suc} are not available.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   240
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   241
Concerning simplifier tricks, we have no need to eliminate subtraction (it
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   242
is well-behaved), but the simplifier can sort the operands of integer
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   243
operators.  The name \isa{zadd_ac} refers to the associativity and
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   244
commutativity theorems for integer addition, while \isa{zmult_ac} has the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   245
analogous theorems for multiplication.  The prefix~\isa{z} in many theorem
10654
458068404143 *** empty log message ***
nipkow
parents: 10608
diff changeset
   246
names recalls the use of $Z$ to denote the set of integers.
10594
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   247
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   248
For division and remainder, the treatment of negative divisors follows
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   249
traditional mathematical practice: the sign of the remainder follows that
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   250
of the divisor:
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
\#0\ <\ b\ \isasymLongrightarrow \ \#0\ \isasymle \ a\ mod\ b%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   253
\rulename{pos_mod_sign}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   254
\#0\ <\ b\ \isasymLongrightarrow \ a\ mod\ b\ <\ b%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   255
\rulename{pos_mod_bound}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   256
b\ <\ \#0\ \isasymLongrightarrow \ a\ mod\ b\ \isasymle \ \#0
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   257
\rulename{neg_mod_sign}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   258
b\ <\ \#0\ \isasymLongrightarrow \ b\ <\ a\ mod\ b%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   259
\rulename{neg_mod_bound}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   260
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   261
ML treats negative divisors in the same way, but most computer hardware
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   262
treats signed operands using the same rules as for multiplication.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   263
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   264
The library provides many lemmas for proving inequalities involving integer
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   265
multiplication and division, similar to those shown above for
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   266
type~\isa{nat}.  The absolute value function \isa{abs} is
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   267
defined for the integers; we have for example the obvious law
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   268
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   269
\isasymbar x\ *\ y\isasymbar \ =\ \isasymbar x\isasymbar \ *\ \isasymbar y\isasymbar 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   270
\rulename{abs_mult}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   271
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   272
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   273
Again, many facts about quotients and remainders are provided:
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   274
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   275
(a\ +\ b)\ div\ c\ =\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   276
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
   277
\rulename{zdiv_zadd1_eq}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   278
\par\smallskip
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   279
(a\ +\ b)\ mod\ c\ =\ (a\ mod\ c\ +\ b\ mod\ c)\ mod\ c%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   280
\rulename{zmod_zadd1_eq}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   281
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   282
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   283
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   284
(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
   285
\rulename{zdiv_zmult1_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   286
(a\ *\ b)\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   287
\rulename{zmod_zmult1_eq}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   288
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   289
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   290
\begin{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   291
\#0\ <\ c\ \isasymLongrightarrow \ a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   292
\rulename{zdiv_zmult2_eq}\isanewline
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   293
\#0\ <\ c\ \isasymLongrightarrow \ a\ mod\ (b*c)\ =\ b*(a\ div\ b\ mod\
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   294
c)\ +\ a\ mod\ b%
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   295
\rulename{zmod_zmult2_eq}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   296
\end{isabelle}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   297
The last two differ from their natural number analogues by requiring
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   298
\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
   299
\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
   300
is
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   301
$\isa{a} = 7$, $\isa{b} = 2$ and $\isa{c} = -3$, when the left-hand side of
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   302
\isa{zdiv_zmult2_eq} is $-2$ while the right-hand side is $-1$.
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   303
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   304
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   305
\subsection{The type of real numbers, {\tt\slshape real}}
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   306
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   307
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
   308
addition and multiplication.  The name \isa{real_add_ac} refers to the
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   309
associativity and commutativity theorems for addition; similarly
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   310
\isa{real_mult_ac} contains those properties for multiplication. 
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   311
6330bc4b6fe4 nat and int sections but no real
paulson
parents:
diff changeset
   312
\textbf{To be written.  Inverse, abs, theorems about density, etc.?}