8745
|
1 |
(*<*)
|
|
2 |
theory natsum = Main:;
|
|
3 |
(*>*)
|
|
4 |
text{*\noindent
|
9792
|
5 |
In particular, there are @{text"case"}-expressions, for example
|
9541
|
6 |
@{term[display]"case n of 0 => 0 | Suc m => m"}
|
8745
|
7 |
primitive recursion, for example
|
|
8 |
*}
|
|
9 |
|
10538
|
10 |
consts sum :: "nat \<Rightarrow> nat";
|
8745
|
11 |
primrec "sum 0 = 0"
|
|
12 |
"sum (Suc n) = Suc n + sum n";
|
|
13 |
|
|
14 |
text{*\noindent
|
|
15 |
and induction, for example
|
|
16 |
*}
|
|
17 |
|
|
18 |
lemma "sum n + sum n = n*(Suc n)";
|
|
19 |
apply(induct_tac n);
|
10171
|
20 |
apply(auto);
|
|
21 |
done
|
8745
|
22 |
|
10538
|
23 |
text{*\newcommand{\mystar}{*%
|
|
24 |
}
|
11456
|
25 |
\index{arithmetic operations!for \protect\isa{nat}}%
|
10538
|
26 |
The usual arithmetic operations \ttindexboldpos{+}{$HOL2arithfun},
|
|
27 |
\ttindexboldpos{-}{$HOL2arithfun}, \ttindexboldpos{\mystar}{$HOL2arithfun},
|
11428
|
28 |
\sdx{div}, \sdx{mod}, \cdx{min} and
|
|
29 |
\cdx{max} are predefined, as are the relations
|
10538
|
30 |
\indexboldpos{\isasymle}{$HOL2arithrel} and
|
10654
|
31 |
\ttindexboldpos{<}{$HOL2arithrel}. As usual, @{prop"m-n = 0"} if
|
|
32 |
@{prop"m<n"}. There is even a least number operation
|
11456
|
33 |
\sdx{LEAST}\@. For example, @{prop"(LEAST n. 1 < n) = 2"}.
|
|
34 |
\REMARK{Isabelle CAN prove it automatically, using \isa{auto intro: Least_equality}.
|
|
35 |
The following needs changing with our new system of numbers.}
|
|
36 |
Note that @{term 1}
|
10538
|
37 |
and @{term 2} are available as abbreviations for the corresponding
|
|
38 |
@{term Suc}-expressions. If you need the full set of numerals,
|
10608
|
39 |
see~\S\ref{sec:numerals}.
|
10538
|
40 |
|
11456
|
41 |
\begin{warn}\index{overloading}
|
11418
|
42 |
The constant \cdx{0} and the operations
|
10538
|
43 |
\ttindexboldpos{+}{$HOL2arithfun}, \ttindexboldpos{-}{$HOL2arithfun},
|
11428
|
44 |
\ttindexboldpos{\mystar}{$HOL2arithfun}, \cdx{min},
|
|
45 |
\cdx{max}, \indexboldpos{\isasymle}{$HOL2arithrel} and
|
10538
|
46 |
\ttindexboldpos{<}{$HOL2arithrel} are overloaded, i.e.\ they are available
|
10978
|
47 |
not just for natural numbers but at other types as well.
|
|
48 |
For example, given the goal @{prop"x+0 = x"},
|
10538
|
49 |
there is nothing to indicate that you are talking about natural numbers.
|
|
50 |
Hence Isabelle can only infer that @{term x} is of some arbitrary type where
|
|
51 |
@{term 0} and @{text"+"} are declared. As a consequence, you will be unable
|
|
52 |
to prove the goal (although it may take you some time to realize what has
|
|
53 |
happened if @{text show_types} is not set). In this particular example,
|
|
54 |
you need to include an explicit type constraint, for example
|
10788
|
55 |
@{text"x+0 = (x::nat)"}. If there is enough contextual information this
|
10538
|
56 |
may not be necessary: @{prop"Suc x = x"} automatically implies
|
|
57 |
@{text"x::nat"} because @{term Suc} is not overloaded.
|
10978
|
58 |
|
|
59 |
For details see \S\ref{sec:numbers} and \S\ref{sec:overloading};
|
|
60 |
Table~\ref{tab:overloading} in the appendix shows the most important overloaded
|
|
61 |
operations.
|
10538
|
62 |
\end{warn}
|
|
63 |
|
11456
|
64 |
Both @{text auto} and @{text simp}
|
|
65 |
(a method introduced below, \S\ref{sec:Simplification}) prove
|
|
66 |
simple arithmetic goals automatically:
|
10538
|
67 |
*}
|
|
68 |
|
|
69 |
lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
|
|
70 |
(*<*)by(auto)(*>*)
|
|
71 |
|
|
72 |
text{*\noindent
|
11458
|
73 |
For efficiency's sake, this built-in prover ignores quantified formulae,
|
|
74 |
logical connectives, and all arithmetic operations apart from addition.
|
|
75 |
In consequence, @{text auto} cannot prove this slightly more complex goal:
|
|
76 |
*}
|
10538
|
77 |
|
11458
|
78 |
lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n";
|
|
79 |
(*<*)by(arith)(*>*)
|
|
80 |
|
|
81 |
text{*\noindent
|
11456
|
82 |
The method \methdx{arith} is more general. It attempts to prove
|
11457
|
83 |
the first subgoal provided it is a quantifier-free \textbf{linear arithmetic}
|
|
84 |
formula. Such formulas may involve the
|
10538
|
85 |
usual logical connectives (@{text"\<not>"}, @{text"\<and>"}, @{text"\<or>"},
|
10971
|
86 |
@{text"\<longrightarrow>"}), the relations @{text"="}, @{text"\<le>"} and @{text"<"},
|
|
87 |
and the operations
|
11456
|
88 |
@{text"+"}, @{text"-"}, @{term min} and @{term max}.
|
10654
|
89 |
For example,
|
10538
|
90 |
*}
|
|
91 |
|
|
92 |
lemma "min i (max j (k*k)) = max (min (k*k) i) (min i (j::nat))";
|
|
93 |
apply(arith)
|
|
94 |
(*<*)done(*>*)
|
|
95 |
|
|
96 |
text{*\noindent
|
|
97 |
succeeds because @{term"k*k"} can be treated as atomic. In contrast,
|
|
98 |
*}
|
|
99 |
|
|
100 |
lemma "n*n = n \<Longrightarrow> n=0 \<or> n=1"
|
|
101 |
(*<*)oops(*>*)
|
|
102 |
|
|
103 |
text{*\noindent
|
11456
|
104 |
is not proved even by @{text arith} because the proof relies
|
10538
|
105 |
on properties of multiplication.
|
|
106 |
|
|
107 |
\begin{warn}
|
|
108 |
The running time of @{text arith} is exponential in the number of occurrences
|
11428
|
109 |
of \ttindexboldpos{-}{$HOL2arithfun}, \cdx{min} and
|
|
110 |
\cdx{max} because they are first eliminated by case distinctions.
|
10538
|
111 |
|
11456
|
112 |
Even for linear arithmetic formulae, \isa{arith} is incomplete. If divisibility plays a
|
10538
|
113 |
role, it may fail to prove a valid formula, for example
|
11456
|
114 |
@{prop"m+m \<noteq> n+n+1"}. Fortunately, such examples are rare.
|
10538
|
115 |
\end{warn}
|
|
116 |
*}
|
|
117 |
|
8745
|
118 |
(*<*)
|
|
119 |
end
|
|
120 |
(*>*)
|