src/Doc/How_to_Prove_it/How_to_Prove_it.thy
author wenzelm
Mon, 25 Mar 2019 17:21:26 +0100
changeset 69981 3dced198b9ec
parent 69597 ff784d5a5bfb
permissions -rw-r--r--
more strict AFP properties;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     1
(*<*)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     2
theory How_to_Prove_it
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     3
imports Complex_Main
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     4
begin
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     5
(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
     6
text\<open>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
     7
\chapter{\<^theory>\<open>Main\<close>}
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     8
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
     9
\section{Natural numbers}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    10
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    11
%Tobias Nipkow
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    12
\paragraph{Induction rules}~\\
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    13
In addition to structural induction there is the induction rule
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    14
@{thm[source] less_induct}:
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    15
\begin{quote}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    16
@{thm less_induct}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    17
\end{quote}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    18
This is often called ``complete induction''. It is applied like this:
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    19
\begin{quote}
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    20
(\<open>induction n rule: less_induct\<close>)
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    21
\end{quote}
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    22
In fact, it is not restricted to \<^typ>\<open>nat\<close> but works for any wellfounded
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    23
order \<open><\<close>.
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    24
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    25
There are many more special induction rules. You can find all of them
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    26
via the Find button (in Isabelle/jedit) with the following search criteria:
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    27
\begin{quote}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    28
\texttt{name: Nat name: induct}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    29
\end{quote}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    30
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    31
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    32
\paragraph{How to convert numerals into \<^const>\<open>Suc\<close> terms}~\\
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    33
Solution: simplify with the lemma @{thm[source] numeral_eq_Suc}.
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    34
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    35
\noindent
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    36
Example:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
    37
\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    38
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    39
lemma fixes x :: int shows "x ^ 3 = x * x * x"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    40
by (simp add: numeral_eq_Suc)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    41
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    42
text\<open>This is a typical situation: function ``\<open>^\<close>'' is defined
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    43
by pattern matching on \<^const>\<open>Suc\<close> but is applied to a numeral.
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    44
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    45
Note: simplification with @{thm[source] numeral_eq_Suc} will convert all numerals.
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    46
One can be more specific with the lemmas @{thm [source] numeral_2_eq_2}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    47
(@{thm numeral_2_eq_2}) and @{thm[source] numeral_3_eq_3} (@{thm numeral_3_eq_3}).
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    48
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    49
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    50
\section{Lists}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    51
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    52
%Tobias Nipkow
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    53
\paragraph{Induction rules}~\\
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    54
In addition to structural induction there are a few more induction rules
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    55
that come in handy at times:
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    56
\begin{itemize}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    57
\item
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    58
Structural induction where the new element is appended to the end
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    59
of the list (@{thm[source] rev_induct}):
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    60
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    61
@{thm rev_induct}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    62
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    63
\item Induction on the length of a list (@{thm [source] length_induct}):
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    64
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    65
@{thm length_induct}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    66
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    67
\item Simultaneous induction on two lists of the same length (@{thm [source] list_induct2}):
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    68
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    69
@{thm[display,margin=60] list_induct2}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    70
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    71
\end{itemize}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    72
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    73
%Tobias Nipkow
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    74
\section{Algebraic simplification}
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    75
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    76
On the numeric types \<^typ>\<open>nat\<close>, \<^typ>\<open>int\<close> and \<^typ>\<open>real\<close>,
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    77
proof method \<open>simp\<close> and friends can deal with a limited amount of linear
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    78
arithmetic (no multiplication except by numerals) and method \<open>arith\<close> can
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    79
handle full linear arithmetic (on \<^typ>\<open>nat\<close>, \<^typ>\<open>int\<close> including quantifiers).
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    80
But what to do when proper multiplication is involved?
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    81
At this point it can be helpful to simplify with the lemma list
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    82
@{thm [source] algebra_simps}. Examples:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
    83
\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    84
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    85
lemma fixes x :: int
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    86
  shows "(x + y) * (y - z) = (y - z) * x + y * (y-z)"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    87
by(simp add: algebra_simps)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    88
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    89
lemma fixes x :: "'a :: comm_ring"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    90
  shows "(x + y) * (y - z) = (y - z) * x + y * (y-z)"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    91
by(simp add: algebra_simps)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    92
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
    93
text\<open>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    94
Rewriting with @{thm[source] algebra_simps} has the following effect:
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    95
terms are rewritten into a normal form by multiplying out,
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    96
rearranging sums and products into some canonical order.
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
    97
In the above lemma the normal form will be something like
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    98
\<^term>\<open>x*y + y*y - x*z - y*z\<close>.
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    99
This works for concrete types like \<^typ>\<open>int\<close> as well as for classes like
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   100
\<^class>\<open>comm_ring\<close> (commutative rings). For some classes (e.g.\ \<^class>\<open>ring\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   101
and \<^class>\<open>comm_ring\<close>) this yields a decision procedure for equality.
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   102
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   103
Additional function and predicate symbols are not a problem either:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   104
\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   105
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   106
lemma fixes f :: "int \<Rightarrow> int" shows "2 * f(x*y) - f(y*x) < f(y*x) + 1"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   107
by(simp add: algebra_simps)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   108
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   109
text\<open>Here @{thm[source]algebra_simps} merely has the effect of rewriting
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   110
\<^term>\<open>y*x\<close> to \<^term>\<open>x*y\<close> (or the other way around). This yields
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   111
a problem of the form \<^prop>\<open>2*t - t < t + (1::int)\<close> and we are back in the
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   112
realm of linear arithmetic.
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   113
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   114
Because @{thm[source]algebra_simps} multiplies out, terms can explode.
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   115
If one merely wants to bring sums or products into a canonical order
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   116
it suffices to rewrite with @{thm [source] ac_simps}:\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   117
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   118
lemma fixes f :: "int \<Rightarrow> int" shows "f(x*y*z) - f(z*x*y) = 0"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 56820
diff changeset
   119
by(simp add: ac_simps)
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   120
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   121
text\<open>The lemmas @{thm[source]algebra_simps} take care of addition, subtraction
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   122
and multiplication (algebraic structures up to rings) but ignore division (fields).
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   123
The lemmas @{thm[source]field_simps} also deal with division:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   124
\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   125
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   126
lemma fixes x :: real shows "x+z \<noteq> 0 \<Longrightarrow> 1 + y/(x+z) = (x+y+z)/(x+z)"
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   127
by(simp add: field_simps)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   128
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   129
text\<open>Warning: @{thm[source]field_simps} can blow up your terms
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67399
diff changeset
   130
beyond recognition.\<close>
56820
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   131
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   132
(*<*)
7fbed439b8d3 new documentation: How to Prove it
nipkow
parents:
diff changeset
   133
end
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 57514
diff changeset
   134
(*>*)