src/Doc/Tutorial/document/numerics.tex
changeset 68635 8094b853a92f
parent 64242 93c6f0da5c70
equal deleted inserted replaced
68634:db0980691ef4 68635:8094b853a92f
     9 recursive function definitions.  HOL also provides the type
     9 recursive function definitions.  HOL also provides the type
    10 \isa{int} of \textbf{integers}, which lack induction but support true
    10 \isa{int} of \textbf{integers}, which lack induction but support true
    11 subtraction.  With subtraction, arithmetic reasoning is easier, which makes
    11 subtraction.  With subtraction, arithmetic reasoning is easier, which makes
    12 the integers preferable to the natural numbers for
    12 the integers preferable to the natural numbers for
    13 complicated arithmetic expressions, even if they are non-negative.  There are also the types
    13 complicated arithmetic expressions, even if they are non-negative.  There are also the types
    14 \isa{rat}, \isa{real} and \isa{complex}: the rational, real and complex numbers.  Isabelle has no 
    14 \isa{rat}, \isa{real} and \isa{complex}: the rational, real and complex numbers.  Isabelle has no
    15 subtyping,  so the numeric
    15 subtyping,  so the numeric
    16 types are distinct and there are functions to convert between them.
    16 types are distinct and there are functions to convert between them.
    17 Most numeric operations are overloaded: the same symbol can be
    17 Most numeric operations are overloaded: the same symbol can be
    18 used at all numeric types. Table~\ref{tab:overloading} in the appendix
    18 used at all numeric types. Table~\ref{tab:overloading} in the appendix
    19 shows the most important operations, together with the priorities of the
    19 shows the most important operations, together with the priorities of the
    20 infix symbols. Algebraic properties are organized using type classes
    20 infix symbols. Algebraic properties are organized using type classes
    21 around algebraic concepts such as rings and fields;
    21 around algebraic concepts such as rings and fields;
    22 a property such as the commutativity of addition is a single theorem 
    22 a property such as the commutativity of addition is a single theorem
    23 (\isa{add.commute}) that applies to all numeric types.
    23 (\isa{add.commute}) that applies to all numeric types.
    24 
    24 
    25 \index{linear arithmetic}%
    25 \index{linear arithmetic}%
    26 Many theorems involving numeric types can be proved automatically by
    26 Many theorems involving numeric types can be proved automatically by
    27 Isabelle's arithmetic decision procedure, the method
    27 Isabelle's arithmetic decision procedure, the method
    28 \methdx{arith}.  Linear arithmetic comprises addition, subtraction
    28 \methdx{arith}.  Linear arithmetic comprises addition, subtraction
    29 and multiplication by constant factors; subterms involving other operators
    29 and multiplication by constant factors; subterms involving other operators
    30 are regarded as variables.  The procedure can be slow, especially if the
    30 are regarded as variables.  The procedure can be slow, especially if the
    31 subgoal to be proved involves subtraction over type \isa{nat}, which 
    31 subgoal to be proved involves subtraction over type \isa{nat}, which
    32 causes case splits.  On types \isa{nat} and \isa{int}, \methdx{arith}
    32 causes case splits.  On types \isa{nat} and \isa{int}, \methdx{arith}
    33 can deal with quantifiers---this is known as Presburger arithmetic---whereas on type \isa{real} it cannot.
    33 can deal with quantifiers---this is known as Presburger arithmetic---whereas on type \isa{real} it cannot.
    34 
    34 
    35 The simplifier reduces arithmetic expressions in other
    35 The simplifier reduces arithmetic expressions in other
    36 ways, such as dividing through by common factors.  For problems that lie
    36 ways, such as dividing through by common factors.  For problems that lie
    49 literals, which consist of one or more decimal digits optionally preceeded by a minus sign (\isa{-}).  Examples are \isa{2}, \isa{-3} and
    49 literals, which consist of one or more decimal digits optionally preceeded by a minus sign (\isa{-}).  Examples are \isa{2}, \isa{-3} and
    50 \isa{441223334678}.  Literals are available for the types of natural
    50 \isa{441223334678}.  Literals are available for the types of natural
    51 numbers, integers, rationals, reals, etc.; they denote integer values of
    51 numbers, integers, rationals, reals, etc.; they denote integer values of
    52 arbitrary size.
    52 arbitrary size.
    53 
    53 
    54 Literals look like constants, but they abbreviate 
    54 Literals look like constants, but they abbreviate
    55 terms representing the number in a two's complement binary notation. 
    55 terms representing the number in a two's complement binary notation.
    56 Isabelle performs arithmetic on literals by rewriting rather 
    56 Isabelle performs arithmetic on literals by rewriting rather
    57 than using the hardware arithmetic. In most cases arithmetic 
    57 than using the hardware arithmetic. In most cases arithmetic
    58 is fast enough, even for numbers in the millions. The arithmetic operations 
    58 is fast enough, even for numbers in the millions. The arithmetic operations
    59 provided for literals include addition, subtraction, multiplication, 
    59 provided for literals include addition, subtraction, multiplication,
    60 integer division and remainder.  Fractions of literals (expressed using
    60 integer division and remainder.  Fractions of literals (expressed using
    61 division) are reduced to lowest terms.
    61 division) are reduced to lowest terms.
    62 
    62 
    63 \begin{warn}\index{overloading!and arithmetic}
    63 \begin{warn}\index{overloading!and arithmetic}
    64 The arithmetic operators are 
    64 The arithmetic operators are
    65 overloaded, so you must be careful to ensure that each numeric 
    65 overloaded, so you must be careful to ensure that each numeric
    66 expression refers to a specific type, if necessary by inserting 
    66 expression refers to a specific type, if necessary by inserting
    67 type constraints.  Here is an example of what can go wrong:
    67 type constraints.  Here is an example of what can go wrong:
    68 \par
    68 \par
    69 \begin{isabelle}
    69 \begin{isabelle}
    70 \isacommand{lemma}\ "2\ *\ m\ =\ m\ +\ m"
    70 \isacommand{lemma}\ "2\ *\ m\ =\ m\ +\ m"
    71 \end{isabelle}
    71 \end{isabelle}
    78 type has been specified.  The problem is underspecified.  Given a type
    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.
    79 constraint such as \isa{nat}, \isa{int} or \isa{real}, it becomes trivial.
    80 \end{warn}
    80 \end{warn}
    81 
    81 
    82 \begin{warn}
    82 \begin{warn}
    83 \index{function@\isacommand {function} (command)!and numeric literals}  
    83 \index{function@\isacommand {function} (command)!and numeric literals}
    84 Numeric literals are not constructors and therefore
    84 Numeric literals are not constructors and therefore
    85 must not be used in patterns.  For example, this declaration is
    85 must not be used in patterns.  For example, this declaration is
    86 rejected:
    86 rejected:
    87 \begin{isabelle}
    87 \begin{isabelle}
    88 \isacommand{function}\ h\ \isakeyword{where}\isanewline
    88 \isacommand{function}\ h\ \isakeyword{where}\isanewline
   101 \subsection{The Type of Natural Numbers, {\tt\slshape nat}}
   101 \subsection{The Type of Natural Numbers, {\tt\slshape nat}}
   102 
   102 
   103 \index{natural numbers|(}\index{*nat (type)|(}%
   103 \index{natural numbers|(}\index{*nat (type)|(}%
   104 This type requires no introduction: we have been using it from the
   104 This type requires no introduction: we have been using it from the
   105 beginning.  Hundreds of theorems about the natural numbers are
   105 beginning.  Hundreds of theorems about the natural numbers are
   106 proved in the theories \isa{Nat} and \isa{Divides}.  
   106 proved in the theories \isa{Nat} and \isa{Divides}.
   107 Basic properties of addition and multiplication are available through the
   107 Basic properties of addition and multiplication are available through the
   108 axiomatic type class for semirings (\S\ref{sec:numeric-classes}).
   108 axiomatic type class for semirings (\S\ref{sec:numeric-classes}).
   109 
   109 
   110 \subsubsection{Literals}
   110 \subsubsection{Literals}
   111 \index{numeric literals!for type \protect\isa{nat}}%
   111 \index{numeric literals!for type \protect\isa{nat}}%
   121 better than nested \isa{Suc}s at expressing large values.  But many theorems,
   121 better than nested \isa{Suc}s at expressing large values.  But many theorems,
   122 including the rewrite rules for primitive recursive functions, can only be
   122 including the rewrite rules for primitive recursive functions, can only be
   123 applied to terms of the form \isa{Suc\ $n$}.
   123 applied to terms of the form \isa{Suc\ $n$}.
   124 
   124 
   125 The following default  simplification rules replace
   125 The following default  simplification rules replace
   126 small literals by zero and successor: 
   126 small literals by zero and successor:
   127 \begin{isabelle}
   127 \begin{isabelle}
   128 2\ +\ n\ =\ Suc\ (Suc\ n)
   128 2\ +\ n\ =\ Suc\ (Suc\ n)
   129 \rulename{add_2_eq_Suc}\isanewline
   129 \rulename{add_2_eq_Suc}\isanewline
   130 n\ +\ 2\ =\ Suc\ (Suc\ n)
   130 n\ +\ 2\ =\ Suc\ (Suc\ n)
   131 \rulename{add_2_eq_Suc'}
   131 \rulename{add_2_eq_Suc'}
   144 \rulename{mod_if}\isanewline
   144 \rulename{mod_if}\isanewline
   145 m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%
   145 m\ div\ n\ *\ n\ +\ m\ mod\ n\ =\ m%
   146 \rulenamedx{div_mult_mod_eq}
   146 \rulenamedx{div_mult_mod_eq}
   147 \end{isabelle}
   147 \end{isabelle}
   148 
   148 
   149 Many less obvious facts about quotient and remainder are also provided. 
   149 Many less obvious facts about quotient and remainder are also provided.
   150 Here is a selection:
   150 Here is a selection:
   151 \begin{isabelle}
   151 \begin{isabelle}
   152 a\ *\ b\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
   152 a\ *\ b\ div\ c\ =\ a\ *\ (b\ div\ c)\ +\ a\ *\ (b\ mod\ c)\ div\ c%
   153 \rulename{div_mult1_eq}\isanewline
   153 \rulename{div_mult1_eq}\isanewline
   154 a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
   154 a\ *\ b\ mod\ c\ =\ a\ *\ (b\ mod\ c)\ mod\ c%
   179 In \isa{div_mult_mult1} above, one of
   179 In \isa{div_mult_mult1} above, one of
   180 the two divisors (namely~\isa{c}) must still be nonzero.
   180 the two divisors (namely~\isa{c}) must still be nonzero.
   181 
   181 
   182 The \textbf{divides} relation\index{divides relation}
   182 The \textbf{divides} relation\index{divides relation}
   183 has the standard definition, which
   183 has the standard definition, which
   184 is overloaded over all numeric types: 
   184 is overloaded over all numeric types:
   185 \begin{isabelle}
   185 \begin{isabelle}
   186 m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k
   186 m\ dvd\ n\ \isasymequiv\ {\isasymexists}k.\ n\ =\ m\ *\ k
   187 \rulenamedx{dvd_def}
   187 \rulenamedx{dvd_def}
   188 \end{isabelle}
   188 \end{isabelle}
   189 %
   189 %
   196 \rulenamedx{dvd_add}
   196 \rulenamedx{dvd_add}
   197 \end{isabelle}
   197 \end{isabelle}
   198 
   198 
   199 \subsubsection{Subtraction}
   199 \subsubsection{Subtraction}
   200 
   200 
   201 There are no negative natural numbers, so \isa{m\ -\ n} equals zero unless 
   201 There are no negative natural numbers, so \isa{m\ -\ n} equals zero unless
   202 \isa{m} exceeds~\isa{n}. The following is one of the few facts
   202 \isa{m} exceeds~\isa{n}. The following is one of the few facts
   203 about \isa{m\ -\ n} that is not subject to
   203 about \isa{m\ -\ n} that is not subject to
   204 the condition \isa{n\ \isasymle \  m}. 
   204 the condition \isa{n\ \isasymle \  m}.
   205 \begin{isabelle}
   205 \begin{isabelle}
   206 (m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%
   206 (m\ -\ n)\ *\ k\ =\ m\ *\ k\ -\ n\ *\ k%
   207 \rulenamedx{diff_mult_distrib}
   207 \rulenamedx{diff_mult_distrib}
   208 \end{isabelle}
   208 \end{isabelle}
   209 Natural number subtraction has few
   209 Natural number subtraction has few
   232 
   232 
   233 
   233 
   234 \subsection{The Type of Integers, {\tt\slshape int}}
   234 \subsection{The Type of Integers, {\tt\slshape int}}
   235 
   235 
   236 \index{integers|(}\index{*int (type)|(}%
   236 \index{integers|(}\index{*int (type)|(}%
   237 Reasoning methods for the integers resemble those for the natural numbers, 
   237 Reasoning methods for the integers resemble those for the natural numbers,
   238 but induction and
   238 but induction and
   239 the constant \isa{Suc} are not available.  HOL provides many lemmas for
   239 the constant \isa{Suc} are not available.  HOL provides many lemmas for
   240 proving inequalities involving integer multiplication and division, similar
   240 proving inequalities involving integer multiplication and division, similar
   241 to those shown above for type~\isa{nat}. The laws of addition, subtraction
   241 to those shown above for type~\isa{nat}. The laws of addition, subtraction
   242 and multiplication are available through the axiomatic type class for rings
   242 and multiplication are available through the axiomatic type class for rings
   243 (\S\ref{sec:numeric-classes}).
   243 (\S\ref{sec:numeric-classes}).
   244 
   244 
   245 The \rmindex{absolute value} function \cdx{abs} is overloaded, and is 
   245 The \rmindex{absolute value} function \cdx{abs} is overloaded, and is
   246 defined for all types that involve negative numbers, including the integers.
   246 defined for all types that involve negative numbers, including the integers.
   247 The \isa{arith} method can prove facts about \isa{abs} automatically, 
   247 The \isa{arith} method can prove facts about \isa{abs} automatically,
   248 though as it does so by case analysis, the cost can be exponential.
   248 though as it does so by case analysis, the cost can be exponential.
   249 \begin{isabelle}
   249 \begin{isabelle}
   250 \isacommand{lemma}\ "abs\ (x+y)\ \isasymle \ abs\ x\ +\ abs\ (y\ ::\ int)"\isanewline
   250 \isacommand{lemma}\ "abs\ (x+y)\ \isasymle \ abs\ x\ +\ abs\ (y\ ::\ int)"\isanewline
   251 \isacommand{by}\ arith
   251 \isacommand{by}\ arith
   252 \end{isabelle}
   252 \end{isabelle}
   269 treats signed operands using the same rules as for multiplication.
   269 treats signed operands using the same rules as for multiplication.
   270 Many facts about quotients and remainders are provided:
   270 Many facts about quotients and remainders are provided:
   271 \begin{isabelle}
   271 \begin{isabelle}
   272 (a\ +\ b)\ div\ c\ =\isanewline
   272 (a\ +\ b)\ div\ c\ =\isanewline
   273 a\ div\ c\ +\ b\ div\ c\ +\ (a\ mod\ c\ +\ b\ mod\ c)\ div\ c%
   273 a\ div\ c\ +\ b\ div\ c\ +\ (a\ mod\ c\ +\ b\ mod\ c)\ div\ c%
   274 \rulename{zdiv_zadd1_eq}
   274 \rulename{div_add1_eq}
   275 \par\smallskip
   275 \par\smallskip
   276 (a\ +\ b)\ mod\ c\ =\ (a\ mod\ c\ +\ b\ mod\ c)\ mod\ c%
   276 (a\ +\ b)\ mod\ c\ =\ (a\ mod\ c\ +\ b\ mod\ c)\ mod\ c%
   277 \rulename{mod_add_eq}
   277 \rulename{mod_add_eq}
   278 \end{isabelle}
   278 \end{isabelle}
   279 
   279 
   318 \label{sec:real}
   318 \label{sec:real}
   319 
   319 
   320 \index{rational numbers|(}\index{*rat (type)|(}%
   320 \index{rational numbers|(}\index{*rat (type)|(}%
   321 \index{real numbers|(}\index{*real (type)|(}%
   321 \index{real numbers|(}\index{*real (type)|(}%
   322 \index{complex numbers|(}\index{*complex (type)|(}%
   322 \index{complex numbers|(}\index{*complex (type)|(}%
   323 These types provide true division, the overloaded operator \isa{/}, 
   323 These types provide true division, the overloaded operator \isa{/},
   324 which differs from the operator \isa{div} of the 
   324 which differs from the operator \isa{div} of the
   325 natural numbers and integers. The rationals and reals are 
   325 natural numbers and integers. The rationals and reals are
   326 \textbf{dense}: between every two distinct numbers lies another.
   326 \textbf{dense}: between every two distinct numbers lies another.
   327 This property follows from the division laws, since if $x\not=y$ then $(x+y)/2$ lies between them:
   327 This property follows from the division laws, since if $x\not=y$ then $(x+y)/2$ lies between them:
   328 \begin{isabelle}
   328 \begin{isabelle}
   329 a\ <\ b\ \isasymLongrightarrow \ \isasymexists r.\ a\ <\ r\ \isasymand \ r\ <\ b%
   329 a\ <\ b\ \isasymLongrightarrow \ \isasymexists r.\ a\ <\ r\ \isasymand \ r\ <\ b%
   330 \rulename{dense}
   330 \rulename{dense}
   332 
   332 
   333 The real numbers are, moreover, \textbf{complete}: every set of reals that
   333 The real numbers are, moreover, \textbf{complete}: every set of reals that
   334 is bounded above has a least upper bound.  Completeness distinguishes the
   334 is bounded above has a least upper bound.  Completeness distinguishes the
   335 reals from the rationals, for which the set $\{x\mid x^2<2\}$ has no least
   335 reals from the rationals, for which the set $\{x\mid x^2<2\}$ has no least
   336 upper bound.  (It could only be $\surd2$, which is irrational.) The
   336 upper bound.  (It could only be $\surd2$, which is irrational.) The
   337 formalization of completeness, which is complicated, 
   337 formalization of completeness, which is complicated,
   338 can be found in theory \texttt{RComplete}.
   338 can be found in theory \texttt{RComplete}.
   339 
   339 
   340 Numeric literals\index{numeric literals!for type \protect\isa{real}}
   340 Numeric literals\index{numeric literals!for type \protect\isa{real}}
   341 for type \isa{real} have the same syntax as those for type
   341 for type \isa{real} have the same syntax as those for type
   342 \isa{int} and only express integral values.  Fractions expressed
   342 \isa{int} and only express integral values.  Fractions expressed
   355 numbers.  Base your theory upon theory \thydx{Complex_Main}, not the
   355 numbers.  Base your theory upon theory \thydx{Complex_Main}, not the
   356 usual \isa{Main}.%
   356 usual \isa{Main}.%
   357 \end{warn}
   357 \end{warn}
   358 
   358 
   359 Available in the logic HOL-NSA is the
   359 Available in the logic HOL-NSA is the
   360 theory \isa{Hyperreal}, which define the type \tydx{hypreal} of 
   360 theory \isa{Hyperreal}, which define the type \tydx{hypreal} of
   361 \rmindex{non-standard reals}.  These
   361 \rmindex{non-standard reals}.  These
   362 \textbf{hyperreals} include infinitesimals, which represent infinitely
   362 \textbf{hyperreals} include infinitesimals, which represent infinitely
   363 small and infinitely large quantities; they facilitate proofs
   363 small and infinitely large quantities; they facilitate proofs
   364 about limits, differentiation and integration~\cite{fleuriot-jcm}.  The
   364 about limits, differentiation and integration~\cite{fleuriot-jcm}.  The
   365 development defines an infinitely large number, \isa{omega} and an
   365 development defines an infinitely large number, \isa{omega} and an
   366 infinitely small positive number, \isa{epsilon}.  The 
   366 infinitely small positive number, \isa{epsilon}.  The
   367 relation $x\approx y$ means ``$x$ is infinitely close to~$y$.''
   367 relation $x\approx y$ means ``$x$ is infinitely close to~$y$.''
   368 Theory \isa{Hyperreal} also defines transcendental functions such as sine,
   368 Theory \isa{Hyperreal} also defines transcendental functions such as sine,
   369 cosine, exponential and logarithm --- even the versions for type
   369 cosine, exponential and logarithm --- even the versions for type
   370 \isa{real}, because they are defined using nonstandard limits.%
   370 \isa{real}, because they are defined using nonstandard limits.%
   371 \index{rational numbers|)}\index{*rat (type)|)}%
   371 \index{rational numbers|)}\index{*rat (type)|)}%
   379 Hundreds of basic properties are proved in the theory \isa{Ring_and_Field}.
   379 Hundreds of basic properties are proved in the theory \isa{Ring_and_Field}.
   380 These lemmas are available (as simprules if they were declared as such)
   380 These lemmas are available (as simprules if they were declared as such)
   381 for all numeric types satisfying the necessary axioms. The theory defines
   381 for all numeric types satisfying the necessary axioms. The theory defines
   382 dozens of type classes, such as the following:
   382 dozens of type classes, such as the following:
   383 \begin{itemize}
   383 \begin{itemize}
   384 \item 
   384 \item
   385 \tcdx{semiring} and \tcdx{ordered_semiring}: a \emph{semiring}
   385 \tcdx{semiring} and \tcdx{ordered_semiring}: a \emph{semiring}
   386 provides the associative operators \isa{+} and~\isa{*}, with \isa{0} and~\isa{1}
   386 provides the associative operators \isa{+} and~\isa{*}, with \isa{0} and~\isa{1}
   387 as their respective identities. The operators enjoy the usual distributive law,
   387 as their respective identities. The operators enjoy the usual distributive law,
   388 and addition (\isa{+}) is also commutative.
   388 and addition (\isa{+}) is also commutative.
   389 An \emph{ordered semiring} is also linearly
   389 An \emph{ordered semiring} is also linearly
   390 ordered, with addition and multiplication respecting the ordering. Type \isa{nat} is an ordered semiring.
   390 ordered, with addition and multiplication respecting the ordering. Type \isa{nat} is an ordered semiring.
   391 \item 
   391 \item
   392 \tcdx{ring} and \tcdx{ordered_ring}: a \emph{ring} extends a semiring
   392 \tcdx{ring} and \tcdx{ordered_ring}: a \emph{ring} extends a semiring
   393 with unary minus (the additive inverse) and subtraction (both
   393 with unary minus (the additive inverse) and subtraction (both
   394 denoted~\isa{-}). An \emph{ordered ring} includes the absolute value
   394 denoted~\isa{-}). An \emph{ordered ring} includes the absolute value
   395 function, \cdx{abs}. Type \isa{int} is an ordered ring.
   395 function, \cdx{abs}. Type \isa{int} is an ordered ring.
   396 \item 
   396 \item
   397 \tcdx{field} and \tcdx{ordered_field}: a field extends a ring with the
   397 \tcdx{field} and \tcdx{ordered_field}: a field extends a ring with the
   398 multiplicative inverse (called simply \cdx{inverse} and division~(\isa{/})).
   398 multiplicative inverse (called simply \cdx{inverse} and division~(\isa{/})).
   399 An ordered field is based on an ordered ring. Type \isa{complex} is a field, while type \isa{real} is an ordered field.
   399 An ordered field is based on an ordered ring. Type \isa{complex} is a field, while type \isa{real} is an ordered field.
   400 \item 
   400 \item
   401 \tcdx{division_by_zero} includes all types where \isa{inverse 0 = 0}
   401 \tcdx{division_by_zero} includes all types where \isa{inverse 0 = 0}
   402 and \isa{a / 0 = 0}. These include all of Isabelle's standard numeric types.
   402 and \isa{a / 0 = 0}. These include all of Isabelle's standard numeric types.
   403 However, the basic properties of fields are derived without assuming
   403 However, the basic properties of fields are derived without assuming
   404 division by zero.
   404 division by zero.
   405 \end{itemize}
   405 \end{itemize}
   433 (c\ =\ (0::'a::ring_no_zero_divisors)\ \isasymor\ a\ =\ b)
   433 (c\ =\ (0::'a::ring_no_zero_divisors)\ \isasymor\ a\ =\ b)
   434 \end{isabelle}
   434 \end{isabelle}
   435 
   435 
   436 
   436 
   437 \subsubsection{Simplifying with the AC-Laws}
   437 \subsubsection{Simplifying with the AC-Laws}
   438 Suppose that two expressions are equal, differing only in 
   438 Suppose that two expressions are equal, differing only in
   439 associativity and commutativity of addition.  Simplifying with the
   439 associativity and commutativity of addition.  Simplifying with the
   440 following equations sorts the terms and groups them to the right, making
   440 following equations sorts the terms and groups them to the right, making
   441 the two expressions identical.
   441 the two expressions identical.
   442 \begin{isabelle}
   442 \begin{isabelle}
   443 a\ +\ b\ +\ c\ =\ a\ +\ (b\ +\ c)
   443 a\ +\ b\ +\ c\ =\ a\ +\ (b\ +\ c)
   445 a\ +\ b\ =\ b\ +\ a%
   445 a\ +\ b\ =\ b\ +\ a%
   446 \rulenamedx{add.commute}\isanewline
   446 \rulenamedx{add.commute}\isanewline
   447 a\ +\ (b\ +\ c)\ =\ b\ +\ (a\ +\ c)
   447 a\ +\ (b\ +\ c)\ =\ b\ +\ (a\ +\ c)
   448 \rulename{add.left_commute}
   448 \rulename{add.left_commute}
   449 \end{isabelle}
   449 \end{isabelle}
   450 The name \isa{ac_simps}\index{*ac_simps (theorems)} 
   450 The name \isa{ac_simps}\index{*ac_simps (theorems)}
   451 refers to the list of all three theorems; similarly
   451 refers to the list of all three theorems; similarly
   452 there is \isa{ac_simps}.\index{*ac_simps (theorems)} 
   452 there is \isa{ac_simps}.\index{*ac_simps (theorems)}
   453 They are all proved for semirings and therefore hold for all numeric types.
   453 They are all proved for semirings and therefore hold for all numeric types.
   454 
   454 
   455 Here is an example of the sorting effect.  Start
   455 Here is an example of the sorting effect.  Start
   456 with this goal, which involves type \isa{nat}.
   456 with this goal, which involves type \isa{nat}.
   457 \begin{isabelle}
   457 \begin{isabelle}
   504 \end{isabelle}
   504 \end{isabelle}
   505 
   505 
   506 
   506 
   507 \subsubsection{Absolute Value}
   507 \subsubsection{Absolute Value}
   508 
   508 
   509 The \rmindex{absolute value} function \cdx{abs} is available for all 
   509 The \rmindex{absolute value} function \cdx{abs} is available for all
   510 ordered rings, including types \isa{int}, \isa{rat} and \isa{real}.
   510 ordered rings, including types \isa{int}, \isa{rat} and \isa{real}.
   511 It satisfies many properties,
   511 It satisfies many properties,
   512 such as the following:
   512 such as the following:
   513 \begin{isabelle}
   513 \begin{isabelle}
   514 \isasymbar x\ *\ y\isasymbar \ =\ \isasymbar x\isasymbar \ *\ \isasymbar y\isasymbar 
   514 \isasymbar x\ *\ y\isasymbar \ =\ \isasymbar x\isasymbar \ *\ \isasymbar y\isasymbar
   515 \rulename{abs_mult}\isanewline
   515 \rulename{abs_mult}\isanewline
   516 (\isasymbar a\isasymbar \ \isasymle \ b)\ =\ (a\ \isasymle \ b\ \isasymand \ -\ a\ \isasymle \ b)
   516 (\isasymbar a\isasymbar \ \isasymle \ b)\ =\ (a\ \isasymle \ b\ \isasymand \ -\ a\ \isasymle \ b)
   517 \rulename{abs_le_iff}\isanewline
   517 \rulename{abs_le_iff}\isanewline
   518 \isasymbar a\ +\ b\isasymbar \ \isasymle \ \isasymbar a\isasymbar \ +\ \isasymbar b\isasymbar 
   518 \isasymbar a\ +\ b\isasymbar \ \isasymle \ \isasymbar a\isasymbar \ +\ \isasymbar b\isasymbar
   519 \rulename{abs_triangle_ineq}
   519 \rulename{abs_triangle_ineq}
   520 \end{isabelle}
   520 \end{isabelle}
   521 
   521 
   522 \begin{warn}
   522 \begin{warn}
   523 The absolute value bars shown above cannot be typed on a keyboard.  They
   523 The absolute value bars shown above cannot be typed on a keyboard.  They
   526 \end{warn}
   526 \end{warn}
   527 
   527 
   528 
   528 
   529 \subsubsection{Raising to a Power}
   529 \subsubsection{Raising to a Power}
   530 
   530 
   531 Another type class, \tcdx{ordered\_idom}, specifies rings that also have 
   531 Another type class, \tcdx{ordered\_idom}, specifies rings that also have
   532 exponentation to a natural number power, defined using the obvious primitive
   532 exponentation to a natural number power, defined using the obvious primitive
   533 recursion. Theory \thydx{Power} proves various theorems, such as the 
   533 recursion. Theory \thydx{Power} proves various theorems, such as the
   534 following.
   534 following.
   535 \begin{isabelle}
   535 \begin{isabelle}
   536 a\ \isacharcircum \ (m\ +\ n)\ =\ a\ \isacharcircum \ m\ *\ a\ \isacharcircum \ n%
   536 a\ \isacharcircum \ (m\ +\ n)\ =\ a\ \isacharcircum \ m\ *\ a\ \isacharcircum \ n%
   537 \rulename{power_add}\isanewline
   537 \rulename{power_add}\isanewline
   538 a\ \isacharcircum \ (m\ *\ n)\ =\ (a\ \isacharcircum \ m)\ \isacharcircum \ n%
   538 a\ \isacharcircum \ (m\ *\ n)\ =\ (a\ \isacharcircum \ m)\ \isacharcircum \ n%