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