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