doc-src/TutorialI/Misc/natsum.thy
author bulwahn
Mon, 22 Mar 2010 08:30:12 +0100
changeset 35876 ac44e2312f0a
parent 27168 9a9cc62932d9
permissions -rw-r--r--
only adding lifted arguments to item net in the function flattening; correcting indentation; removing dead code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     1
(*<*)
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
     2
theory natsum imports Main begin
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     3
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     4
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
     5
In particular, there are @{text"case"}-expressions, for example
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
     6
@{term[display]"case n of 0 => 0 | Suc m => m"}
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     7
primitive recursion, for example
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     8
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     9
27015
f8537d69f514 *** empty log message ***
nipkow
parents: 16768
diff changeset
    10
primrec sum :: "nat \<Rightarrow> nat" where
f8537d69f514 *** empty log message ***
nipkow
parents: 16768
diff changeset
    11
"sum 0 = 0" |
f8537d69f514 *** empty log message ***
nipkow
parents: 16768
diff changeset
    12
"sum (Suc n) = Suc n + sum n"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    13
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    14
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    15
and induction, for example
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    16
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    17
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    18
lemma "sum n + sum n = n*(Suc n)"
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    19
apply(induct_tac n)
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    20
apply(auto)
10171
59d6633835fa *** empty log message ***
nipkow
parents: 9834
diff changeset
    21
done
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    22
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    23
text{*\newcommand{\mystar}{*%
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    24
}
11456
7eb63f63e6c6 revisions and indexing
paulson
parents: 11428
diff changeset
    25
\index{arithmetic operations!for \protect\isa{nat}}%
15364
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    26
The arithmetic operations \isadxboldpos{+}{$HOL2arithfun},
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    27
\isadxboldpos{-}{$HOL2arithfun}, \isadxboldpos{\mystar}{$HOL2arithfun},
11428
332347b9b942 tidying the index
paulson
parents: 11418
diff changeset
    28
\sdx{div}, \sdx{mod}, \cdx{min} and
332347b9b942 tidying the index
paulson
parents: 11418
diff changeset
    29
\cdx{max} are predefined, as are the relations
15364
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    30
\isadxboldpos{\isasymle}{$HOL2arithrel} and
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    31
\isadxboldpos{<}{$HOL2arithrel}. As usual, @{prop"m-n = (0::nat)"} if
10654
458068404143 *** empty log message ***
nipkow
parents: 10608
diff changeset
    32
@{prop"m<n"}. There is even a least number operation
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    33
\sdx{LEAST}\@.  For example, @{prop"(LEAST n. 0 < n) = Suc 0"}.
11456
7eb63f63e6c6 revisions and indexing
paulson
parents: 11428
diff changeset
    34
\begin{warn}\index{overloading}
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    35
  The constants \cdx{0} and \cdx{1} and the operations
15364
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    36
  \isadxboldpos{+}{$HOL2arithfun}, \isadxboldpos{-}{$HOL2arithfun},
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    37
  \isadxboldpos{\mystar}{$HOL2arithfun}, \cdx{min},
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    38
  \cdx{max}, \isadxboldpos{\isasymle}{$HOL2arithrel} and
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    39
  \isadxboldpos{<}{$HOL2arithrel} are overloaded: they are available
12329
8743e8305611 minor textual tweaks
paulson
parents: 12327
diff changeset
    40
  not just for natural numbers but for other types as well.
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    41
  For example, given the goal @{text"x + 0 = x"}, there is nothing to indicate
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    42
  that you are talking about natural numbers. Hence Isabelle can only infer
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    43
  that @{term x} is of some arbitrary type where @{text 0} and @{text"+"} are
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    44
  declared. As a consequence, you will be unable to prove the
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    45
  goal. To alert you to such pitfalls, Isabelle flags numerals without a
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    46
  fixed type in its output: @{prop"x+0 = x"}. (In the absence of a numeral,
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    47
  it may take you some time to realize what has happened if \pgmenu{Show
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    48
  Types} is not set).  In this particular example, you need to include
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    49
  an explicit type constraint, for example @{text"x+0 = (x::nat)"}. If there
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    50
  is enough contextual information this may not be necessary: @{prop"Suc x =
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    51
  x"} automatically implies @{text"x::nat"} because @{term Suc} is not
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    52
  overloaded.
10978
5eebea8f359f *** empty log message ***
nipkow
parents: 10971
diff changeset
    53
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    54
  For details on overloading see \S\ref{sec:overloading}.
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    55
  Table~\ref{tab:overloading} in the appendix shows the most important
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    56
  overloaded operations.
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    57
\end{warn}
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    58
\begin{warn}
15364
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    59
  The symbols \isadxboldpos{>}{$HOL2arithrel} and
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    60
  \isadxboldpos{\isasymge}{$HOL2arithrel} are merely syntax: @{text"x > y"}
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    61
  stands for @{prop"y < x"} and similary for @{text"\<ge>"} and
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    62
  @{text"\<le>"}.
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    63
\end{warn}
0c3891c3528f *** empty log message ***
nipkow
parents: 13996
diff changeset
    64
\begin{warn}
12329
8743e8305611 minor textual tweaks
paulson
parents: 12327
diff changeset
    65
  Constant @{text"1::nat"} is defined to equal @{term"Suc 0"}. This definition
12327
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    66
  (see \S\ref{sec:ConstDefinitions}) is unfolded automatically by some
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    67
  tactics (like @{text auto}, @{text simp} and @{text arith}) but not by
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    68
  others (especially the single step tactics in Chapter~\ref{chap:rules}).
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    69
  If you need the full set of numerals, see~\S\ref{sec:numerals}.
5a4d78204492 *** empty log message ***
nipkow
parents: 11711
diff changeset
    70
  \emph{Novices are advised to stick to @{term"0::nat"} and @{term Suc}.}
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    71
\end{warn}
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    72
11456
7eb63f63e6c6 revisions and indexing
paulson
parents: 11428
diff changeset
    73
Both @{text auto} and @{text simp}
7eb63f63e6c6 revisions and indexing
paulson
parents: 11428
diff changeset
    74
(a method introduced below, \S\ref{sec:Simplification}) prove 
7eb63f63e6c6 revisions and indexing
paulson
parents: 11428
diff changeset
    75
simple arithmetic goals automatically:
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    76
*}
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    77
11711
ecdfd237ffee fixed numerals;
wenzelm
parents: 11458
diff changeset
    78
lemma "\<lbrakk> \<not> m < n; m < n + (1::nat) \<rbrakk> \<Longrightarrow> m = n"
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    79
(*<*)by(auto)(*>*)
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    80
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    81
text{*\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11457
diff changeset
    82
For efficiency's sake, this built-in prover ignores quantified formulae,
16768
37636be4cbd1 small text mod
nipkow
parents: 16523
diff changeset
    83
many logical connectives, and all arithmetic operations apart from addition.
13181
dc393bbee6ce *** empty log message ***
nipkow
parents: 12699
diff changeset
    84
In consequence, @{text auto} and @{text simp} cannot prove this slightly more complex goal:
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11457
diff changeset
    85
*}
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    86
13181
dc393bbee6ce *** empty log message ***
nipkow
parents: 12699
diff changeset
    87
lemma "m \<noteq> (n::nat) \<Longrightarrow> m < n \<or> n < m"
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11457
diff changeset
    88
(*<*)by(arith)(*>*)
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11457
diff changeset
    89
13996
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    90
text{*\noindent The method \methdx{arith} is more general.  It attempts to
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    91
prove the first subgoal provided it is a \textbf{linear arithmetic} formula.
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    92
Such formulas may involve the usual logical connectives (@{text"\<not>"},
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    93
@{text"\<and>"}, @{text"\<or>"}, @{text"\<longrightarrow>"}, @{text"="},
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    94
@{text"\<forall>"}, @{text"\<exists>"}), the relations @{text"="},
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    95
@{text"\<le>"} and @{text"<"}, and the operations @{text"+"}, @{text"-"},
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
    96
@{term min} and @{term max}.  For example, *}
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    97
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16417
diff changeset
    98
lemma "min i (max j (k*k)) = max (min (k*k) i) (min i (j::nat))"
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
    99
apply(arith)
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   100
(*<*)done(*>*)
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   101
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   102
text{*\noindent
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   103
succeeds because @{term"k*k"} can be treated as atomic. In contrast,
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   104
*}
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   105
27168
9a9cc62932d9 lemma modified
nipkow
parents: 27015
diff changeset
   106
lemma "n*n = n+1 \<Longrightarrow> n=0"
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   107
(*<*)oops(*>*)
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   108
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   109
text{*\noindent
27168
9a9cc62932d9 lemma modified
nipkow
parents: 27015
diff changeset
   110
is not proved by @{text arith} because the proof relies 
13996
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   111
on properties of multiplication. Only multiplication by numerals (which is
27168
9a9cc62932d9 lemma modified
nipkow
parents: 27015
diff changeset
   112
the same as iterated addition) is taken into account.
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   113
13996
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   114
\begin{warn} The running time of @{text arith} is exponential in the number
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   115
  of occurrences of \ttindexboldpos{-}{$HOL2arithfun}, \cdx{min} and
11428
332347b9b942 tidying the index
paulson
parents: 11418
diff changeset
   116
  \cdx{max} because they are first eliminated by case distinctions.
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   117
13996
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   118
If @{text k} is a numeral, \sdx{div}~@{text k}, \sdx{mod}~@{text k} and
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   119
@{text k}~\sdx{dvd} are also supported, where the former two are eliminated
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   120
by case distinctions, again blowing up the running time.
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   121
16768
37636be4cbd1 small text mod
nipkow
parents: 16523
diff changeset
   122
If the formula involves quantifiers, @{text arith} may take
13996
a994b92ab1ea *** empty log message ***
nipkow
parents: 13181
diff changeset
   123
super-exponential time and space.
10538
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   124
\end{warn}
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   125
*}
d1bf9ca9008d *** empty log message ***
nipkow
parents: 10171
diff changeset
   126
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   127
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   128
end
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   129
(*>*)