doc-src/TutorialI/Types/numerics.tex
 author paulson Wed Feb 21 15:21:15 2001 +0100 (2001-02-21) changeset 11174 96a533d300a6 parent 11161 166f7d87b37f child 11216 279004936bb0 permissions -rw-r--r--
revisions in response to comments by Tobias
 paulson@10794  1 % $Id$  paulson@11174  2 Until now, our numerical examples have used the type of \textbf{natural  paulson@11174  3 numbers},  paulson@10594  4 \isa{nat}. This is a recursive datatype generated by the constructors  paulson@10594  5 zero and successor, so it works well with inductive proofs and primitive  paulson@11174  6 recursive function definitions. HOL also provides the type  paulson@10794  7 \isa{int} of \textbf{integers}, which lack induction but support true  paulson@11174  8 subtraction. The integers are preferable to the natural numbers for reasoning about  paulson@11174  9 complicated arithmetic expressions, even for some expressions whose  paulson@11174  10 value is non-negative. The logic HOL-Real also has the type  paulson@11174  11 \isa{real} of real numbers. Isabelle has no subtyping, so the numeric  paulson@11174  12 types are distinct and there are functions to convert between them.  paulson@11174  13 Fortunately most numeric operations are overloaded: the same symbol can be  paulson@11174  14 used at all numeric types. Table~\ref{tab:overloading} in the appendix  paulson@11174  15 shows the most important operations, together with the priorities of the  paulson@11174  16 infix symbols.  paulson@10594  17 paulson@10594  18 paulson@10594  19 Many theorems involving numeric types can be proved automatically by  paulson@10594  20 Isabelle's arithmetic decision procedure, the method  paulson@10594  21 \isa{arith}. Linear arithmetic comprises addition, subtraction  paulson@10594  22 and multiplication by constant factors; subterms involving other operators  paulson@10594  23 are regarded as variables. The procedure can be slow, especially if the  paulson@10594  24 subgoal to be proved involves subtraction over type \isa{nat}, which  paulson@10594  25 causes case splits.  paulson@10594  26 paulson@10594  27 The simplifier reduces arithmetic expressions in other  paulson@10594  28 ways, such as dividing through by common factors. For problems that lie  paulson@10881  29 outside the scope of automation, HOL provides hundreds of  paulson@10594  30 theorems about multiplication, division, etc., that can be brought to  paulson@10881  31 bear. You can locate them using Proof General's Find  paulson@10881  32 button. A few lemmas are given below to show what  paulson@10794  33 is available.  paulson@10594  34 paulson@10594  35 \subsection{Numeric Literals}  nipkow@10779  36 \label{sec:numerals}  paulson@10594  37 paulson@10594  38 Literals are available for the types of natural numbers, integers  paulson@10594  39 and reals and denote integer values of arbitrary size.  paulson@10594  40 They begin  paulson@10594  41 with a number sign (\isa{\#}), have an optional minus sign (\isa{-}) and  paulson@10594  42 then one or more decimal digits. Examples are \isa{\#0}, \isa{\#-3}  paulson@10594  43 and \isa{\#441223334678}.  paulson@10594  44 paulson@10594  45 Literals look like constants, but they abbreviate  paulson@10594  46 terms, representing the number in a two's complement binary notation.  paulson@10794  47 Isabelle performs arithmetic on literals by rewriting rather  paulson@10594  48 than using the hardware arithmetic. In most cases arithmetic  paulson@10594  49 is fast enough, even for large numbers. The arithmetic operations  paulson@10794  50 provided for literals include addition, subtraction, multiplication,  paulson@10794  51 integer division and remainder. Fractions of literals (expressed using  paulson@10794  52 division) are reduced to lowest terms.  paulson@10594  53 paulson@10794  54 \begin{warn}  paulson@10794  55 The arithmetic operators are  paulson@10594  56 overloaded, so you must be careful to ensure that each numeric  paulson@10594  57 expression refers to a specific type, if necessary by inserting  paulson@10594  58 type constraints. Here is an example of what can go wrong:  paulson@10794  59 \par  paulson@10594  60 \begin{isabelle}  paulson@10594  61 \isacommand{lemma}\ "\#2\ *\ m\ =\ m\ +\ m"  paulson@10594  62 \end{isabelle}  paulson@10594  63 %  paulson@10594  64 Carefully observe how Isabelle displays the subgoal:  paulson@10594  65 \begin{isabelle}  paulson@10594  66 \ 1.\ (\#2::'a)\ *\ m\ =\ m\ +\ m  paulson@10594  67 \end{isabelle}  paulson@10594  68 The type \isa{'a} given for the literal \isa{\#2} warns us that no numeric  paulson@10594  69 type has been specified. The problem is underspecified. Given a type  paulson@10594  70 constraint such as \isa{nat}, \isa{int} or \isa{real}, it becomes trivial.  paulson@10794  71 \end{warn}  paulson@10794  72 paulson@10881  73 \begin{warn}  paulson@10881  74 Numeric literals are not constructors and therefore must not be used in  paulson@10881  75 patterns. For example, this declaration is rejected:  paulson@10881  76 \begin{isabelle}  paulson@10881  77 \isacommand{recdef}\ h\ "\isacharbraceleft \isacharbraceright "\isanewline  nipkow@11148  78 "h\ \#3\ =\ \#2"\isanewline  nipkow@11148  79 "h\ i\ \ =\ i"  paulson@10881  80 \end{isabelle}  paulson@10881  81 paulson@10881  82 You should use a conditional expression instead:  paulson@10881  83 \begin{isabelle}  paulson@10881  84 "h\ i\ =\ (if\ i\ =\ \#3\ then\ \#2\ else\ i)"  paulson@10881  85 \end{isabelle}  paulson@10881  86 \end{warn}  paulson@10881  87 paulson@10594  88 paulson@10594  89 paulson@10594  90 \subsection{The type of natural numbers, {\tt\slshape nat}}  paulson@10594  91 paulson@10594  92 This type requires no introduction: we have been using it from the  paulson@10794  93 beginning. Hundreds of theorems about the natural numbers are  paulson@10594  94 proved in the theories \isa{Nat}, \isa{NatArith} and \isa{Divides}. Only  paulson@10594  95 in exceptional circumstances should you resort to induction.  paulson@10594  96 paulson@10594  97 \subsubsection{Literals}  paulson@10594  98 The notational options for the natural numbers can be confusing. The  paulson@10594  99 constant \isa{0} is overloaded to serve as the neutral value  paulson@10594  100 in a variety of additive types. The symbols \isa{1} and \isa{2} are  paulson@10594  101 not constants but abbreviations for \isa{Suc 0} and \isa{Suc(Suc 0)},  paulson@10594  102 respectively. The literals \isa{\#0}, \isa{\#1} and \isa{\#2} are  paulson@10794  103 syntactically different from \isa{0}, \isa{1} and \isa{2}. You will  paulson@10594  104 sometimes prefer one notation to the other. Literals are obviously  paulson@10794  105 necessary to express large values, while \isa{0} and \isa{Suc} are needed  paulson@10794  106 in order to match many theorems, including the rewrite rules for primitive  paulson@10794  107 recursive functions. The following default simplification rules replace  paulson@10794  108 small literals by zero and successor:  paulson@10594  109 \begin{isabelle}  paulson@10594  110 \#0\ =\ 0  paulson@10594  111 \rulename{numeral_0_eq_0}\isanewline  paulson@10594  112 \#1\ =\ 1  paulson@10594  113 \rulename{numeral_1_eq_1}\isanewline  paulson@10594  114 \#2\ +\ n\ =\ Suc\ (Suc\ n)  paulson@10594  115 \rulename{add_2_eq_Suc}\isanewline  paulson@10594  116 n\ +\ \#2\ =\ Suc\ (Suc\ n)  paulson@10594  117 \rulename{add_2_eq_Suc'}  paulson@10594  118 \end{isabelle}  paulson@10594  119 In special circumstances, you may wish to remove or reorient  paulson@10594  120 these rules.  paulson@10594  121 paulson@10594  122 \subsubsection{Typical lemmas}  paulson@10594  123 Inequalities involving addition and subtraction alone can be proved  paulson@10594  124 automatically. Lemmas such as these can be used to prove inequalities  paulson@10594  125 involving multiplication and division:  paulson@10594  126 \begin{isabelle}  paulson@10594  127 \isasymlbrakk i\ \isasymle \ j;\ k\ \isasymle \ l\isasymrbrakk \ \isasymLongrightarrow \ i\ *\ k\ \isasymle \ j\ *\ l%  paulson@10594  128 \rulename{mult_le_mono}\isanewline  paulson@10594  129 \isasymlbrakk i\ <\ j;\ 0\ <\ k\isasymrbrakk \ \isasymLongrightarrow \ i\  paulson@10594  130 *\ k\ <\ j\ *\ k%  paulson@10594  131 \rulename{mult_less_mono1}\isanewline  paulson@10594  132 m\ \isasymle \ n\ \isasymLongrightarrow \ m\ div\ k\ \isasymle \ n\ div\ k%  paulson@10594  133 \rulename{div_le_mono}  paulson@10594  134 \end{isabelle}  paulson@10594  135 %  paulson@10594  136 Various distributive laws concerning multiplication are available:  paulson@10594  137 \begin{isabelle}  paulson@10594  138 (m\ +\ n)\ *\ k\ =\ m\ *\ k\ +\ n\ *\ k%  paulson@10594  139 \rulename{add_mult_distrib}\isanewline  paulson@10594  140 (m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%  paulson@10594  141 \rulename{diff_mult_distrib}\isanewline  paulson@10594  142 (m\ mod\ n)\ *\ k\ =\ (m\ *\ k)\ mod\ (n\ *\ k)  paulson@10594  143 \rulename{mod_mult_distrib}  paulson@10594  144 \end{isabelle}  paulson@10594  145 paulson@10594  146 \subsubsection{Division}  paulson@10881  147 The infix operators \isa{div} and \isa{mod} are overloaded.  paulson@10881  148 Isabelle/HOL provides the basic facts about quotient and remainder  paulson@10881  149 on the natural numbers:  paulson@10594  150 \begin{isabelle}  paulson@10594  151 m\ mod\ n\ =\ (if\ m\ <\ n\ then\ m\ else\ (m\ -\ n)\ mod\ n)  paulson@10594  152 \rulename{mod_if}\isanewline  paulson@10594  153 m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%  paulson@10594  154 \rulename{mod_div_equality}  paulson@10594  155 \end{isabelle}  paulson@10594  156 paulson@10594  157 Many less obvious facts about quotient and remainder are also provided.  paulson@10594  158 Here is a selection:  paulson@10594  159 \begin{isabelle}  paulson@10594  160 a\ *\ b\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%  paulson@10594  161 \rulename{div_mult1_eq}\isanewline  paulson@10594  162 a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%  paulson@10594  163 \rulename{mod_mult1_eq}\isanewline  paulson@10594  164 a\ div\ (b*c)\ =\ a\ div\ b\ div\ c%  paulson@10594  165 \rulename{div_mult2_eq}\isanewline  paulson@10594  166 a\ mod\ (b*c)\ =\ b * (a\ div\ b\ mod\ c)\ +\ a\ mod\ b%  paulson@10594  167 \rulename{mod_mult2_eq}\isanewline  paulson@10594  168 0\ <\ c\ \isasymLongrightarrow \ (c\ *\ a)\ div\ (c\ *\ b)\ =\ a\ div\ b%  paulson@10594  169 \rulename{div_mult_mult1}  paulson@10594  170 \end{isabelle}  paulson@10594  171 paulson@10594  172 Surprisingly few of these results depend upon the  paulson@10794  173 divisors' being nonzero. That is because division by  paulson@10794  174 zero yields zero:  paulson@10594  175 \begin{isabelle}  paulson@10594  176 a\ div\ 0\ =\ 0  paulson@10594  177 \rulename{DIVISION_BY_ZERO_DIV}\isanewline  paulson@10594  178 a\ mod\ 0\ =\ a%  paulson@10594  179 \rulename{DIVISION_BY_ZERO_MOD}  paulson@10594  180 \end{isabelle}  paulson@10594  181 As a concession to convention, these equations are not installed as default  paulson@11174  182 simplification rules. In \isa{div_mult_mult1} above, one of  nipkow@11161  183 the two divisors (namely~\isa{c}) must still be nonzero.  paulson@10594  184 paulson@10594  185 The \textbf{divides} relation has the standard definition, which  paulson@10594  186 is overloaded over all numeric types:  paulson@10594  187 \begin{isabelle}  paulson@10594  188 m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k  paulson@10594  189 \rulename{dvd_def}  paulson@10594  190 \end{isabelle}  paulson@10594  191 %  paulson@10594  192 Section~\ref{sec:proving-euclid} discusses proofs involving this  paulson@10594  193 relation. Here are some of the facts proved about it:  paulson@10594  194 \begin{isabelle}  paulson@10594  195 \isasymlbrakk m\ dvd\ n;\ n\ dvd\ m\isasymrbrakk \ \isasymLongrightarrow \ m\ =\ n%  paulson@10594  196 \rulename{dvd_anti_sym}\isanewline  paulson@10594  197 \isasymlbrakk k\ dvd\ m;\ k\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ k\ dvd\ (m\ +\ n)  paulson@10594  198 \rulename{dvd_add}  paulson@10594  199 \end{isabelle}  paulson@10594  200 paulson@10594  201 \subsubsection{Simplifier tricks}  paulson@10594  202 The rule \isa{diff_mult_distrib} shown above is one of the few facts  paulson@10594  203 about \isa{m\ -\ n} that is not subject to  paulson@10594  204 the condition \isa{n\ \isasymle \ m}. Natural number subtraction has few  paulson@10794  205 nice properties; often you should remove it by simplifying with this split  paulson@10794  206 rule:  paulson@10594  207 \begin{isabelle}  paulson@10594  208 `P(a-b)\ =\ ((a