src/Doc/Prog_Prove/Isar.thy
author wenzelm
Tue, 16 Jan 2018 09:30:00 +0100
changeset 67443 3abf6a722518
parent 67406 23307fd33906
child 67613 ce654b0e6d69
permissions -rw-r--r--
standardized towards new-style formal comments: isabelle update_comments;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     1
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     2
theory Isar
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     3
imports LaTeXsugar
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     4
begin
52059
2f970c7f722b proper option quick_and_dirty;
wenzelm
parents: 51445
diff changeset
     5
declare [[quick_and_dirty]]
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     6
(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
     7
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     8
Apply-scripts are unreadable and hard to maintain. The language of choice
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     9
for larger proofs is \concept{Isar}. The two key features of Isar are:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    10
\begin{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    11
\item It is structured, not linear.
56989
nipkow
parents: 56451
diff changeset
    12
\item It is readable without its being run because
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    13
you need to state what you are proving at any given point.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    14
\end{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    15
Whereas apply-scripts are like assembly language programs, Isar proofs
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    16
are like structured programs with comments. A typical Isar proof looks like this:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
    17
\<close>text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    18
\begin{tabular}{@ {}l}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    19
\isacom{proof}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    20
\quad\isacom{assume} @{text"\""}$\mathit{formula}_0$@{text"\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    21
\quad\isacom{have} @{text"\""}$\mathit{formula}_1$@{text"\""} \quad\isacom{by} @{text simp}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    22
\quad\vdots\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    23
\quad\isacom{have} @{text"\""}$\mathit{formula}_n$@{text"\""} \quad\isacom{by} @{text blast}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    24
\quad\isacom{show} @{text"\""}$\mathit{formula}_{n+1}$@{text"\""} \quad\isacom{by} @{text \<dots>}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    25
\isacom{qed}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    26
\end{tabular}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
    27
\<close>text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    28
It proves $\mathit{formula}_0 \Longrightarrow \mathit{formula}_{n+1}$
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
    29
(provided each proof step succeeds).
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    30
The intermediate \isacom{have} statements are merely stepping stones
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    31
on the way towards the \isacom{show} statement that proves the actual
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    32
goal. In more detail, this is the Isar core syntax:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    33
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    34
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    35
\begin{tabular}{@ {}lcl@ {}}
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
    36
\textit{proof} &=& \indexed{\isacom{by}}{by} \textit{method}\\
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
    37
      &$\mid$& \indexed{\isacom{proof}}{proof} [\textit{method}] \ \textit{step}$^*$ \ \indexed{\isacom{qed}}{qed}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    38
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    39
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    40
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    41
\begin{tabular}{@ {}lcl@ {}}
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
    42
\textit{step} &=& \indexed{\isacom{fix}}{fix} \textit{variables} \\
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
    43
      &$\mid$& \indexed{\isacom{assume}}{assume} \textit{proposition} \\
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
    44
      &$\mid$& [\indexed{\isacom{from}}{from} \textit{fact}$^+$] (\indexed{\isacom{have}}{have} $\mid$ \indexed{\isacom{show}}{show}) \ \textit{proposition} \ \textit{proof}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    45
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    46
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    47
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    48
\begin{tabular}{@ {}lcl@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    49
\textit{proposition} &=& [\textit{name}:] @{text"\""}\textit{formula}@{text"\""}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    50
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    51
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    52
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    53
\begin{tabular}{@ {}lcl@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    54
\textit{fact} &=& \textit{name} \ $\mid$ \ \dots
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    55
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    56
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    57
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    58
\noindent A proof can either be an atomic \isacom{by} with a single proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    59
method which must finish off the statement being proved, for example @{text
56989
nipkow
parents: 56451
diff changeset
    60
auto},  or it can be a \isacom{proof}--\isacom{qed} block of multiple
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    61
steps. Such a block can optionally begin with a proof method that indicates
58504
nipkow
parents: 58502
diff changeset
    62
how to start off the proof, e.g., \mbox{@{text"(induction xs)"}}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    63
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    64
A step either assumes a proposition or states a proposition
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    65
together with its proof. The optional \isacom{from} clause
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    66
indicates which facts are to be used in the proof.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    67
Intermediate propositions are stated with \isacom{have}, the overall goal
56989
nipkow
parents: 56451
diff changeset
    68
is stated with \isacom{show}. A step can also introduce new local variables with
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    69
\isacom{fix}. Logically, \isacom{fix} introduces @{text"\<And>"}-quantified
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    70
variables, \isacom{assume} introduces the assumption of an implication
56989
nipkow
parents: 56451
diff changeset
    71
(@{text"\<Longrightarrow>"}) and \isacom{have}/\isacom{show} introduce the conclusion.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    72
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    73
Propositions are optionally named formulas. These names can be referred to in
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    74
later \isacom{from} clauses. In the simplest case, a fact is such a name.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    75
But facts can also be composed with @{text OF} and @{text of} as shown in
58602
ab56811d76c6 tuned spaces
nipkow
parents: 58522
diff changeset
    76
\autoref{sec:forward-proof} --- hence the \dots\ in the above grammar.  Note
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    77
that assumptions, intermediate \isacom{have} statements and global lemmas all
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    78
have the same status and are thus collectively referred to as
55317
834a84553e02 started index
nipkow
parents: 54839
diff changeset
    79
\conceptidx{facts}{fact}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    80
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    81
Fact names can stand for whole lists of facts. For example, if @{text f} is
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    82
defined by command \isacom{fun}, @{text"f.simps"} refers to the whole list of
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    83
recursion equations defining @{text f}. Individual facts can be selected by
56989
nipkow
parents: 56451
diff changeset
    84
writing @{text"f.simps(2)"}, whole sublists by writing @{text"f.simps(2-4)"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    85
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    86
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
    87
\section{Isar by Example}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    88
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
    89
We show a number of proofs of Cantor's theorem that a function from a set to
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    90
its powerset cannot be surjective, illustrating various features of Isar. The
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    91
constant @{const surj} is predefined.
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
    92
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    93
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    94
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    95
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    96
  assume 0: "surj f"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    97
  from 0 have 1: "\<forall>A. \<exists>a. A = f a" by(simp add: surj_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    98
  from 1 have 2: "\<exists>a. {x. x \<notin> f x} = f a" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    99
  from 2 show "False" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   100
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   101
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   102
text\<open>
56989
nipkow
parents: 56451
diff changeset
   103
The \isacom{proof} command lacks an explicit method by which to perform
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   104
the proof. In such cases Isabelle tries to use some standard introduction
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   105
rule, in the above case for @{text"\<not>"}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   106
\[
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   107
\inferrule{
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   108
\mbox{@{thm (prem 1) notI}}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   109
{\mbox{@{thm (concl) notI}}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   110
\]
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   111
In order to prove @{prop"~ P"}, assume @{text P} and show @{text False}.
64852
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   112
Thus we may assume \mbox{\noquotes{@{prop [source] "surj f"}}}. The proof shows that names of propositions
58602
ab56811d76c6 tuned spaces
nipkow
parents: 58522
diff changeset
   113
may be (single!) digits --- meaningful names are hard to invent and are often
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   114
not necessary. Both \isacom{have} steps are obvious. The second one introduces
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   115
the diagonal set @{term"{x. x \<notin> f x}"}, the key idea in the proof.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   116
If you wonder why @{text 2} directly implies @{text False}: from @{text 2}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   117
it follows that @{prop"a \<notin> f a \<longleftrightarrow> a \<in> f a"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   118
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   119
\subsection{\indexed{@{text this}}{this}, \indexed{\isacom{then}}{then}, \indexed{\isacom{hence}}{hence} and \indexed{\isacom{thus}}{thus}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   120
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   121
Labels should be avoided. They interrupt the flow of the reader who has to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   122
scan the context for the point where the label was introduced. Ideally, the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   123
proof is a linear flow, where the output of one step becomes the input of the
58605
nipkow
parents: 58602
diff changeset
   124
next step, piping the previously proved fact into the next proof, like
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   125
in a UNIX pipe. In such cases the predefined name @{text this} can be used
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   126
to refer to the proposition proved in the previous step. This allows us to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   127
eliminate all labels from our proof (we suppress the \isacom{lemma} statement):
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   128
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   129
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   130
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   131
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   132
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   133
  assume "surj f"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   134
  from this have "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   135
  from this show "False" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   136
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   137
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   138
text\<open>We have also taken the opportunity to compress the two \isacom{have}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   139
steps into one.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   140
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   141
To compact the text further, Isar has a few convenient abbreviations:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   142
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   143
54839
nipkow
parents: 54577
diff changeset
   144
\begin{tabular}{r@ {\quad=\quad}l}
nipkow
parents: 54577
diff changeset
   145
\isacom{then} & \isacom{from} @{text this}\\
nipkow
parents: 54577
diff changeset
   146
\isacom{thus} & \isacom{then} \isacom{show}\\
nipkow
parents: 54577
diff changeset
   147
\isacom{hence} & \isacom{then} \isacom{have}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   148
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   149
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   150
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   151
\noindent
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   152
With the help of these abbreviations the proof becomes
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   153
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   154
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   155
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   156
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   157
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   158
  assume "surj f"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   159
  hence "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   160
  thus "False" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   161
qed
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   162
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   163
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   164
There are two further linguistic variations:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   165
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   166
54839
nipkow
parents: 54577
diff changeset
   167
\begin{tabular}{r@ {\quad=\quad}l}
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   168
(\isacom{have}$\mid$\isacom{show}) \ \textit{prop} \ \indexed{\isacom{using}}{using} \ \textit{facts}
54839
nipkow
parents: 54577
diff changeset
   169
&
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   170
\isacom{from} \ \textit{facts} \ (\isacom{have}$\mid$\isacom{show}) \ \textit{prop}\\
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   171
\indexed{\isacom{with}}{with} \ \textit{facts} & \isacom{from} \ \textit{facts} \isa{this}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   172
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   173
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   174
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   175
\noindent The \isacom{using} idiom de-emphasizes the used facts by moving them
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   176
behind the proposition.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   177
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   178
\subsection{Structured Lemma Statements: \indexed{\isacom{fixes}}{fixes}, \indexed{\isacom{assumes}}{assumes}, \indexed{\isacom{shows}}{shows}}
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   179
\index{lemma@\isacom{lemma}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   180
Lemmas can also be stated in a more structured fashion. To demonstrate this
64852
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   181
feature with Cantor's theorem, we rephrase \noquotes{@{prop[source]"\<not> surj f"}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   182
a little:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   183
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   184
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   185
lemma
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   186
  fixes f :: "'a \<Rightarrow> 'a set"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   187
  assumes s: "surj f"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   188
  shows "False"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   189
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   190
txt\<open>The optional \isacom{fixes} part allows you to state the types of
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   191
variables up front rather than by decorating one of their occurrences in the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   192
formula with a type constraint. The key advantage of the structured format is
47306
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
   193
the \isacom{assumes} part that allows you to name each assumption; multiple
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
   194
assumptions can be separated by \isacom{and}. The
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   195
\isacom{shows} part gives the goal. The actual theorem that will come out of
64852
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   196
the proof is \noquotes{@{prop[source]"surj f \<Longrightarrow> False"}}, but during the proof the assumption
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   197
\noquotes{@{prop[source]"surj f"}} is available under the name @{text s} like any other fact.
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   198
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   199
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   200
proof -
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   201
  have "\<exists> a. {x. x \<notin> f x} = f a" using s
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   202
    by(auto simp: surj_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   203
  thus "False" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   204
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   205
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   206
text\<open>
56312
nipkow
parents: 55417
diff changeset
   207
\begin{warn}
nipkow
parents: 55417
diff changeset
   208
Note the hyphen after the \isacom{proof} command.
56989
nipkow
parents: 56451
diff changeset
   209
It is the null method that does nothing to the goal. Leaving it out would be asking
58602
ab56811d76c6 tuned spaces
nipkow
parents: 58522
diff changeset
   210
Isabelle to try some suitable introduction rule on the goal @{const False} --- but
56312
nipkow
parents: 55417
diff changeset
   211
there is no such rule and \isacom{proof} would fail.
nipkow
parents: 55417
diff changeset
   212
\end{warn}
64852
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   213
In the \isacom{have} step the assumption \noquotes{@{prop[source]"surj f"}} is now
f3504bc69ea3 fix problems because of "surj" input abbreviation; tuned
nipkow
parents: 61517
diff changeset
   214
referenced by its name @{text s}. The duplication of \noquotes{@{prop[source]"surj f"}} in the
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   215
above proofs (once in the statement of the lemma, once in its proof) has been
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   216
eliminated.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   217
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
   218
Stating a lemma with \isacom{assumes}-\isacom{shows} implicitly introduces the
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   219
name \indexed{@{text assms}}{assms} that stands for the list of all assumptions. You can refer
58521
nipkow
parents: 58504
diff changeset
   220
to individual assumptions by @{text"assms(1)"}, @{text"assms(2)"}, etc.,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   221
thus obviating the need to name them individually.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   222
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   223
\section{Proof Patterns}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   224
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   225
We show a number of important basic proof patterns. Many of them arise from
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   226
the rules of natural deduction that are applied by \isacom{proof} by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   227
default. The patterns are phrased in terms of \isacom{show} but work for
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   228
\isacom{have} and \isacom{lemma}, too.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   229
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   230
\ifsem\else
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   231
\subsection{Logic}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   232
\fi
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   233
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   234
We start with two forms of \concept{case analysis}:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   235
starting from a formula @{text P} we have the two cases @{text P} and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   236
@{prop"~P"}, and starting from a fact @{prop"P \<or> Q"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   237
we have the two cases @{text P} and @{text Q}:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   238
\<close>text_raw\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   239
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   240
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   241
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   242
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   243
(*<*)lemma "R" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   244
show "R"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   245
proof cases
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   246
  assume "P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   247
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   248
  show "R" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   249
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   250
  assume "\<not> P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   251
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   252
  show "R" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   253
qed(*<*)oops(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   254
text_raw \<open>}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   255
\end{minipage}\index{cases@@{text cases}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   256
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   257
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   258
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   259
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   260
(*<*)lemma "R" proof-(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   261
have "P \<or> Q" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   262
then show "R"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   263
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   264
  assume "P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   265
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   266
  show "R" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   267
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   268
  assume "Q"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   269
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   270
  show "R" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   271
qed(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   272
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   273
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   274
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   275
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   276
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   277
\begin{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   278
How to prove a logical equivalence:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   279
\end{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   280
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   281
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   282
(*<*)lemma "P\<longleftrightarrow>Q" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   283
show "P \<longleftrightarrow> Q"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   284
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   285
  assume "P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   286
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   287
  show "Q" (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   288
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   289
  assume "Q"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   290
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   291
  show "P" (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   292
qed(*<*)qed(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   293
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   294
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   295
\begin{isamarkuptext}%
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   296
Proofs by contradiction (@{thm[source] ccontr} stands for ``classical contradiction''):
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   297
\end{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   298
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   299
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   300
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   301
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   302
(*<*)lemma "\<not> P" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   303
show "\<not> P"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   304
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   305
  assume "P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   306
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   307
  show "False" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   308
qed(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   309
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   310
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   311
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   312
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   313
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   314
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   315
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   316
(*<*)lemma "P" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   317
show "P"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   318
proof (rule ccontr)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   319
  assume "\<not>P"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   320
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   321
  show "False" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   322
qed(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   323
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   324
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   325
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   326
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   327
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   328
\begin{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   329
How to prove quantified formulas:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   330
\end{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   331
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   332
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   333
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   334
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   335
(*<*)lemma "ALL x. P x" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   336
show "\<forall>x. P(x)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   337
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   338
  fix x
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   339
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   340
  show "P(x)" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   341
qed(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   342
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   343
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   344
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   345
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   346
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   347
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   348
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   349
(*<*)lemma "EX x. P(x)" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   350
show "\<exists>x. P(x)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   351
proof
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   352
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   353
  show "P(witness)" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   354
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   355
(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   356
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   357
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   358
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   359
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   360
\medskip
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   361
\begin{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   362
In the proof of \noquotes{@{prop[source]"\<forall>x. P(x)"}},
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   363
the step \indexed{\isacom{fix}}{fix}~@{text x} introduces a locally fixed variable @{text x}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   364
into the subproof, the proverbial ``arbitrary but fixed value''.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   365
Instead of @{text x} we could have chosen any name in the subproof.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   366
In the proof of \noquotes{@{prop[source]"\<exists>x. P(x)"}},
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   367
@{text witness} is some arbitrary
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   368
term for which we can prove that it satisfies @{text P}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   369
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   370
How to reason forward from \noquotes{@{prop[source] "\<exists>x. P(x)"}}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   371
\end{isamarkuptext}%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   372
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   373
(*<*)lemma True proof- assume 1: "EX x. P x"(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   374
have "\<exists>x. P(x)" (*<*)by(rule 1)(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   375
then obtain x where p: "P(x)" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   376
(*<*)oops(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   377
text\<open>
55389
nipkow
parents: 55361
diff changeset
   378
After the \indexed{\isacom{obtain}}{obtain} step, @{text x} (we could have chosen any name)
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   379
is a fixed local
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   380
variable, and @{text p} is the name of the fact
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   381
\noquotes{@{prop[source] "P(x)"}}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   382
This pattern works for one or more @{text x}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   383
As an example of the \isacom{obtain} command, here is the proof of
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   384
Cantor's theorem in more detail:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   385
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   386
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   387
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   388
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   389
  assume "surj f"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   390
  hence  "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   391
  then obtain a where  "{x. x \<notin> f x} = f a"  by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   392
  hence  "a \<notin> f a \<longleftrightarrow> a \<in> f a"  by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   393
  thus "False" by blast
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   394
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   395
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   396
text_raw\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   397
\begin{isamarkuptext}%
47306
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
   398
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
   399
Finally, how to prove set equality and subset relationship:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   400
\end{isamarkuptext}%
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   401
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   402
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   403
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   404
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   405
(*<*)lemma "A = (B::'a set)" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   406
show "A = B"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   407
proof
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   408
  show "A \<subseteq> B" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   409
next
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   410
  show "B \<subseteq> A" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   411
qed(*<*)qed(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   412
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   413
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   414
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   415
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   416
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   417
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   418
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   419
(*<*)lemma "A <= (B::'a set)" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   420
show "A \<subseteq> B"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   421
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   422
  fix x
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   423
  assume "x \<in> A"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   424
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   425
  show "x \<in> B" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   426
qed(*<*)qed(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   427
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   428
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   429
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   430
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   431
\begin{isamarkuptext}%
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   432
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   433
\ifsem\else
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   434
\subsection{Chains of (In)Equations}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   435
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   436
In textbooks, chains of equations (and inequations) are often displayed like this:
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   437
\begin{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   438
\begin{tabular}{@ {}l@ {\qquad}l@ {}}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   439
$t_1 = t_2$ & \isamath{\,\langle\mathit{justification}\rangle}\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   440
$\phantom{t_1} = t_3$ & \isamath{\,\langle\mathit{justification}\rangle}\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   441
\quad $\vdots$\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   442
$\phantom{t_1} = t_n$ & \isamath{\,\langle\mathit{justification}\rangle}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   443
\end{tabular}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   444
\end{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   445
The Isar equivalent is this:
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   446
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   447
\begin{samepage}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   448
\begin{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   449
\isakeyword{have} \<open>"t\<^sub>1 = t\<^sub>2"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   450
\isakeyword{also have} \<open>"... = t\<^sub>3"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   451
\quad $\vdots$\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   452
\isakeyword{also have} \<open>"... = t\<^sub>n"\<close> \isasymproof \\
65349
nipkow
parents: 65348
diff changeset
   453
\isakeyword{finally show} \<open>"t\<^sub>1 = t\<^sub>n"\<close>\ \texttt{.}
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   454
\end{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   455
\end{samepage}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   456
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   457
\noindent
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   458
The ``\<open>...\<close>'' and ``\<open>.\<close>'' deserve some explanation:
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   459
\begin{description}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   460
\item[``\<open>...\<close>''] is literally three dots. It is the name of an unknown that Isar
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   461
automatically instantiates with the right-hand side of the previous equation.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   462
In general, if \<open>this\<close> is the theorem @{term "p t\<^sub>1 t\<^sub>2"} then ``\<open>...\<close>''
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   463
stands for \<open>t\<^sub>2\<close>.
65349
nipkow
parents: 65348
diff changeset
   464
\item[``\<open>.\<close>''] (a single dot) is a proof method that solves a goal by one of the
65352
nipkow
parents: 65349
diff changeset
   465
assumptions. This works here because the result of \isakeyword{finally}
65349
nipkow
parents: 65348
diff changeset
   466
is the theorem \mbox{\<open>t\<^sub>1 = t\<^sub>n\<close>},
nipkow
parents: 65348
diff changeset
   467
\isakeyword{show} \<open>"t\<^sub>1 = t\<^sub>n"\<close> states the theorem explicitly,
nipkow
parents: 65348
diff changeset
   468
and ``\<open>.\<close>'' proves the theorem with the result of \isakeyword{finally}.
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   469
\end{description}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   470
The above proof template also works for arbitrary mixtures of \<open>=\<close>, \<open>\<le>\<close> and \<open><\<close>,
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   471
for example:
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   472
\begin{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   473
\isakeyword{have} \<open>"t\<^sub>1 < t\<^sub>2"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   474
\isakeyword{also have} \<open>"... = t\<^sub>3"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   475
\quad $\vdots$\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   476
\isakeyword{also have} \<open>"... \<le> t\<^sub>n"\<close> \isasymproof \\
65349
nipkow
parents: 65348
diff changeset
   477
\isakeyword{finally show} \<open>"t\<^sub>1 < t\<^sub>n"\<close>\ \texttt{.}
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   478
\end{quote}
65349
nipkow
parents: 65348
diff changeset
   479
The relation symbol in the \isakeyword{finally} step needs to be the most precise one
nipkow
parents: 65348
diff changeset
   480
possible. In the example above, you must not write \<open>t\<^sub>1 \<le> t\<^sub>n\<close> instead of \mbox{\<open>t\<^sub>1 < t\<^sub>n\<close>}.
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   481
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   482
\begin{warn}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   483
Isabelle only supports \<open>=\<close>, \<open>\<le>\<close> and \<open><\<close> but not \<open>\<ge>\<close> and \<open>>\<close>
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   484
in (in)equation chains (by default).
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   485
\end{warn}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   486
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   487
If you want to go beyond merely using the above proof patterns and want to
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   488
understand what \isakeyword{also} and \isakeyword{finally} mean, read on.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   489
There is an Isar theorem variable called \<open>calculation\<close>, similar to \<open>this\<close>.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   490
When the first \isakeyword{also} in a chain is encountered, Isabelle sets
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   491
\<open>calculation := this\<close>. In each subsequent \isakeyword{also} step,
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   492
Isabelle composes the theorems \<open>calculation\<close> and \<open>this\<close> (i.e.\ the two previous
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   493
(in)equalities) using some predefined set of rules including transitivity
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   494
of \<open>=\<close>, \<open>\<le>\<close> and \<open><\<close> but also mixed rules like @{prop"\<lbrakk> x \<le> y; y < z \<rbrakk> \<Longrightarrow> x < z"}.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   495
The result of this composition is assigned to \<open>calculation\<close>. Consider
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   496
\begin{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   497
\isakeyword{have} \<open>"t\<^sub>1 \<le> t\<^sub>2"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   498
\isakeyword{also} \isakeyword{have} \<open>"... < t\<^sub>3"\<close> \isasymproof\\
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   499
\isakeyword{also} \isakeyword{have} \<open>"... = t\<^sub>4"\<close> \isasymproof\\
65349
nipkow
parents: 65348
diff changeset
   500
\isakeyword{finally show} \<open>"t\<^sub>1 < t\<^sub>4"\<close>\ \texttt{.}
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   501
\end{quote}
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   502
After the first \isakeyword{also}, \<open>calculation\<close> is \<open>"t\<^sub>1 \<le> t\<^sub>2"\<close>,
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   503
and after the second \isakeyword{also}, \<open>calculation\<close> is \<open>"t\<^sub>1 < t\<^sub>3"\<close>.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   504
The command \isakeyword{finally} is short for \isakeyword{also from} \<open>calculation\<close>.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   505
Therefore the \isakeyword{also} hidden in \isakeyword{finally} sets \<open>calculation\<close>
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   506
to \<open>t\<^sub>1 < t\<^sub>4\<close> and the final ``\texttt{.}'' succeeds.
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   507
67299
ba52a058942f prefer formal citations;
wenzelm
parents: 67039
diff changeset
   508
For more information on this style of proof see @{cite "BauerW-TPHOLs01"}.
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   509
\fi
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   510
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   511
\section{Streamlining Proofs}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   512
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   513
\subsection{Pattern Matching and Quotations}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   514
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   515
In the proof patterns shown above, formulas are often duplicated.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   516
This can make the text harder to read, write and maintain. Pattern matching
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   517
is an abbreviation mechanism to avoid such duplication. Writing
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   518
\begin{quote}
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   519
\isacom{show} \ \textit{formula} @{text"("}\indexed{\isacom{is}}{is} \textit{pattern}@{text")"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   520
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   521
matches the pattern against the formula, thus instantiating the unknowns in
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   522
the pattern for later use. As an example, consider the proof pattern for
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   523
@{text"\<longleftrightarrow>"}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   524
\end{isamarkuptext}%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   525
\<close>
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   526
(*<*)lemma "formula\<^sub>1 \<longleftrightarrow> formula\<^sub>2" proof-(*>*)
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   527
show "formula\<^sub>1 \<longleftrightarrow> formula\<^sub>2" (is "?L \<longleftrightarrow> ?R")
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   528
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   529
  assume "?L"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   530
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   531
  show "?R" (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   532
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   533
  assume "?R"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   534
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   535
  show "?L" (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   536
qed(*<*)qed(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   537
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   538
text\<open>Instead of duplicating @{text"formula\<^sub>i"} in the text, we introduce
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   539
the two abbreviations @{text"?L"} and @{text"?R"} by pattern matching.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   540
Pattern matching works wherever a formula is stated, in particular
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   541
with \isacom{have} and \isacom{lemma}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   542
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   543
The unknown \indexed{@{text"?thesis"}}{thesis} is implicitly matched against any goal stated by
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   544
\isacom{lemma} or \isacom{show}. Here is a typical example:\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   545
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   546
lemma "formula"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   547
proof -
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   548
  text_raw\<open>\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   549
  show ?thesis (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   550
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   551
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   552
text\<open>
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   553
Unknowns can also be instantiated with \indexed{\isacom{let}}{let} commands
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   554
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   555
\isacom{let} @{text"?t"} = @{text"\""}\textit{some-big-term}@{text"\""}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   556
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   557
Later proof steps can refer to @{text"?t"}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   558
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   559
\isacom{have} @{text"\""}\dots @{text"?t"} \dots@{text"\""}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   560
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   561
\begin{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   562
Names of facts are introduced with @{text"name:"} and refer to proved
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   563
theorems. Unknowns @{text"?X"} refer to terms or formulas.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   564
\end{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   565
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   566
Although abbreviations shorten the text, the reader needs to remember what
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   567
they stand for. Similarly for names of facts. Names like @{text 1}, @{text 2}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   568
and @{text 3} are not helpful and should only be used in short proofs. For
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
   569
longer proofs, descriptive names are better. But look at this example:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   570
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   571
\isacom{have} \ @{text"x_gr_0: \"x > 0\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   572
$\vdots$\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   573
\isacom{from} @{text "x_gr_0"} \dots
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   574
\end{quote}
56989
nipkow
parents: 56451
diff changeset
   575
The name is longer than the fact it stands for! Short facts do not need names;
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   576
one can refer to them easily by quoting them:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   577
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   578
\isacom{have} \ @{text"\"x > 0\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   579
$\vdots$\\
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   580
\isacom{from} @{text "`x>0`"} \dots\index{$IMP053@@{text"`...`"}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   581
\end{quote}
55317
834a84553e02 started index
nipkow
parents: 54839
diff changeset
   582
Note that the quotes around @{text"x>0"} are \conceptnoidx{back quotes}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   583
They refer to the fact not by name but by value.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   584
55359
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   585
\subsection{\indexed{\isacom{moreover}}{moreover}}
2d8222c76020 indexed document
nipkow
parents: 55317
diff changeset
   586
\index{ultimately@\isacom{ultimately}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   587
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   588
Sometimes one needs a number of facts to enable some deduction. Of course
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   589
one can name these facts individually, as shown on the right,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   590
but one can also combine them with \isacom{moreover}, as shown on the left:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   591
\<close>text_raw\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   592
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   593
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   594
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   595
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   596
(*<*)lemma "P" proof-(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   597
have "P\<^sub>1" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   598
moreover have "P\<^sub>2" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   599
moreover
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   600
text_raw\<open>\\$\vdots$\\\hspace{-1.4ex}\<close>(*<*)have "True" ..(*>*)
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   601
moreover have "P\<^sub>n" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   602
ultimately have "P"  (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   603
(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   604
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   605
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   606
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   607
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   608
\qquad
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   609
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   610
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   611
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   612
(*<*)lemma "P" proof-(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   613
have lab\<^sub>1: "P\<^sub>1" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   614
have lab\<^sub>2: "P\<^sub>2" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   615
text_raw\<open>\\$\vdots$\\\hspace{-1.4ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   616
have lab\<^sub>n: "P\<^sub>n" (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   617
from lab\<^sub>1 lab\<^sub>2 text_raw\<open>\ $\dots$\\\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   618
have "P"  (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   619
(*<*)oops(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   620
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   621
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   622
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   623
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   624
\begin{isamarkuptext}%
65348
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   625
The \isacom{moreover} version is no shorter but expresses the structure
b5ce7100ddc8 added chains of (in)equations
nipkow
parents: 64852
diff changeset
   626
a bit more clearly and avoids new names.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   627
67039
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   628
\subsection{Local Lemmas}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   629
56989
nipkow
parents: 56451
diff changeset
   630
Sometimes one would like to prove some lemma locally within a proof,
nipkow
parents: 56451
diff changeset
   631
a lemma that shares the current context of assumptions but that
58502
nipkow
parents: 58486
diff changeset
   632
has its own assumptions and is generalized over its locally fixed
67039
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   633
variables at the end. This is simply an extension of the basic
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   634
\indexed{\isacom{have}}{have} construct:
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   635
\begin{quote}
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   636
\indexed{\isacom{have}}{have} @{text"B"}\
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   637
 \indexed{\isacom{if}}{if} \<open>name:\<close> @{text"A\<^sub>1 \<dots> A\<^sub>m"}\
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   638
 \indexed{\isacom{for}}{for} @{text"x\<^sub>1 \<dots> x\<^sub>n"}\\
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   639
\isasymproof
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   640
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   641
proves @{text"\<lbrakk> A\<^sub>1; \<dots> ; A\<^sub>m \<rbrakk> \<Longrightarrow> B"}
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   642
where all @{text"x\<^sub>i"} have been replaced by unknowns @{text"?x\<^sub>i"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   643
As an example we prove a simple fact about divisibility on integers.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   644
The definition of @{text "dvd"} is @{thm dvd_def}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   645
\end{isamarkuptext}%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   646
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   647
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   648
lemma fixes a b :: int assumes "b dvd (a+b)" shows "b dvd a"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   649
proof -
67039
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   650
  have "\<exists>k'. a = b*k'" if asm: "a+b = b*k" for k
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   651
  proof
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   652
    show "a = b*(k - 1)" using asm by(simp add: algebra_simps)
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   653
  qed
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   654
  then show ?thesis using assms by(auto simp add: dvd_def)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   655
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   656
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   657
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   658
54436
nipkow
parents: 54292
diff changeset
   659
\subsection*{Exercises}
52706
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   660
52661
a3b04f0ab6a4 added exercise
nipkow
parents: 52593
diff changeset
   661
\exercise
a3b04f0ab6a4 added exercise
nipkow
parents: 52593
diff changeset
   662
Give a readable, structured proof of the following lemma:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   663
\<close>
54218
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   664
lemma assumes T: "\<forall>x y. T x y \<or> T y x"
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   665
  and A: "\<forall>x y. A x y \<and> A y x \<longrightarrow> x = y"
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   666
  and TA: "\<forall>x y. T x y \<longrightarrow> A x y" and "A x y"
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   667
  shows "T x y"
52661
a3b04f0ab6a4 added exercise
nipkow
parents: 52593
diff changeset
   668
(*<*)oops(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   669
text\<open>
52661
a3b04f0ab6a4 added exercise
nipkow
parents: 52593
diff changeset
   670
\endexercise
a3b04f0ab6a4 added exercise
nipkow
parents: 52593
diff changeset
   671
52706
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   672
\exercise
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   673
Give a readable, structured proof of the following lemma:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   674
\<close>
67039
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   675
lemma "\<exists>ys zs. xs = ys @ zs \<and>
690b4b334889 Replaced Raw Proof Blocks by Local Lemmas
nipkow
parents: 65437
diff changeset
   676
            (length ys = length zs \<or> length ys = length zs + 1)"
52706
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   677
(*<*)oops(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   678
text\<open>
52706
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   679
Hint: There are predefined functions @{const_typ take} and @{const_typ drop}
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   680
such that @{text"take k [x\<^sub>1,\<dots>] = [x\<^sub>1,\<dots>,x\<^sub>k]"} and
54218
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   681
@{text"drop k [x\<^sub>1,\<dots>] = [x\<^bsub>k+1\<^esub>,\<dots>]"}. Let sledgehammer find and apply
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   682
the relevant @{const take} and @{const drop} lemmas for you.
52706
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   683
\endexercise
5f86b30badd9 added exerciese
nipkow
parents: 52661
diff changeset
   684
54218
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
   685
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   686
\section{Case Analysis and Induction}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   687
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   688
\subsection{Datatype Case Analysis}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   689
\index{case analysis|(}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   690
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   691
We have seen case analysis on formulas. Now we want to distinguish
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   692
which form some term takes: is it @{text 0} or of the form @{term"Suc n"},
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   693
is it @{term"[]"} or of the form @{term"x#xs"}, etc. Here is a typical example
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   694
proof by case analysis on the form of @{text xs}:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   695
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   696
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   697
lemma "length(tl xs) = length xs - 1"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   698
proof (cases xs)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   699
  assume "xs = []"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   700
  thus ?thesis by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   701
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   702
  fix y ys assume "xs = y#ys"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   703
  thus ?thesis by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   704
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   705
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   706
text\<open>\index{cases@@{text"cases"}|(}Function @{text tl} (''tail'') is defined by @{thm list.sel(2)} and
55417
01fbfb60c33e adapted to 'xxx_{case,rec}' renaming, to new theorem names, and to new variable names in theorems
blanchet
parents: 55389
diff changeset
   707
@{thm list.sel(3)}. Note that the result type of @{const length} is @{typ nat}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   708
and @{prop"0 - 1 = (0::nat)"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   709
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   710
This proof pattern works for any term @{text t} whose type is a datatype.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   711
The goal has to be proved for each constructor @{text C}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   712
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   713
\isacom{fix} \ @{text"x\<^sub>1 \<dots> x\<^sub>n"} \isacom{assume} @{text"\"t = C x\<^sub>1 \<dots> x\<^sub>n\""}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   714
\end{quote}\index{case@\isacom{case}|(}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   715
Each case can be written in a more compact form by means of the \isacom{case}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   716
command:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   717
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   718
\isacom{case} @{text "(C x\<^sub>1 \<dots> x\<^sub>n)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   719
\end{quote}
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
   720
This is equivalent to the explicit \isacom{fix}-\isacom{assume} line
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   721
but also gives the assumption @{text"\"t = C x\<^sub>1 \<dots> x\<^sub>n\""} a name: @{text C},
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   722
like the constructor.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   723
Here is the \isacom{case} version of the proof above:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   724
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   725
(*<*)lemma "length(tl xs) = length xs - 1"(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   726
proof (cases xs)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   727
  case Nil
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   728
  thus ?thesis by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   729
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   730
  case (Cons y ys)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   731
  thus ?thesis by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   732
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   733
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   734
text\<open>Remember that @{text Nil} and @{text Cons} are the alphanumeric names
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   735
for @{text"[]"} and @{text"#"}. The names of the assumptions
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   736
are not used because they are directly piped (via \isacom{thus})
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   737
into the proof of the claim.
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   738
\index{case analysis|)}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   739
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   740
\subsection{Structural Induction}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   741
\index{induction|(}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   742
\index{structural induction|(}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   743
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   744
We illustrate structural induction with an example based on natural numbers:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   745
the sum (@{text"\<Sum>"}) of the first @{text n} natural numbers
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   746
(@{text"{0..n::nat}"}) is equal to \mbox{@{term"n*(n+1) div 2::nat"}}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   747
Never mind the details, just focus on the pattern:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   748
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   749
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   750
lemma "\<Sum>{0..n::nat} = n*(n+1) div 2"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   751
proof (induction n)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   752
  show "\<Sum>{0..0::nat} = 0*(0+1) div 2" by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   753
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   754
  fix n assume "\<Sum>{0..n::nat} = n*(n+1) div 2"
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
   755
  thus "\<Sum>{0..Suc n} = Suc n*(Suc n+1) div 2" by simp
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   756
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   757
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   758
text\<open>Except for the rewrite steps, everything is explicitly given. This
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   759
makes the proof easily readable, but the duplication means it is tedious to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   760
write and maintain. Here is how pattern
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   761
matching can completely avoid any duplication:\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   762
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   763
lemma "\<Sum>{0..n::nat} = n*(n+1) div 2" (is "?P n")
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   764
proof (induction n)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   765
  show "?P 0" by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   766
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   767
  fix n assume "?P n"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   768
  thus "?P(Suc n)" by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   769
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   770
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   771
text\<open>The first line introduces an abbreviation @{text"?P n"} for the goal.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   772
Pattern matching @{text"?P n"} with the goal instantiates @{text"?P"} to the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   773
function @{term"\<lambda>n. \<Sum>{0..n::nat} = n*(n+1) div 2"}.  Now the proposition to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   774
be proved in the base case can be written as @{text"?P 0"}, the induction
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   775
hypothesis as @{text"?P n"}, and the conclusion of the induction step as
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   776
@{text"?P(Suc n)"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   777
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   778
Induction also provides the \isacom{case} idiom that abbreviates
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   779
the \isacom{fix}-\isacom{assume} step. The above proof becomes
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   780
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   781
(*<*)lemma "\<Sum>{0..n::nat} = n*(n+1) div 2"(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   782
proof (induction n)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   783
  case 0
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   784
  show ?case by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   785
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   786
  case (Suc n)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   787
  thus ?case by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   788
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   789
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   790
text\<open>
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   791
The unknown @{text"?case"}\index{case?@@{text"?case"}|(} is set in each case to the required
58504
nipkow
parents: 58502
diff changeset
   792
claim, i.e., @{text"?P 0"} and \mbox{@{text"?P(Suc n)"}} in the above proof,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   793
without requiring the user to define a @{text "?P"}. The general
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   794
pattern for induction over @{typ nat} is shown on the left-hand side:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   795
\<close>text_raw\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   796
\begin{tabular}{@ {}ll@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   797
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   798
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   799
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   800
(*<*)lemma "P(n::nat)" proof -(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   801
show "P(n)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   802
proof (induction n)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   803
  case 0
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   804
  text_raw\<open>\\\mbox{}\ \ $\vdots$\\\mbox{}\hspace{-1ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   805
  show ?case (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   806
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   807
  case (Suc n)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   808
  text_raw\<open>\\\mbox{}\ \ $\vdots$\\\mbox{}\hspace{-1ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   809
  show ?case (*<*)sorry(*>*) text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   810
qed(*<*)qed(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   811
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   812
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   813
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   814
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   815
\begin{minipage}[t]{.4\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   816
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   817
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   818
\isacom{let} @{text"?case = \"P(0)\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   819
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   820
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   821
~\\[1ex]
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   822
\isacom{fix} @{text n} \isacom{assume} @{text"Suc: \"P(n)\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   823
\isacom{let} @{text"?case = \"P(Suc n)\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   824
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   825
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   826
\medskip
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   827
\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   828
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   829
On the right side you can see what the \isacom{case} command
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   830
on the left stands for.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   831
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   832
In case the goal is an implication, induction does one more thing: the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   833
proposition to be proved in each case is not the whole implication but only
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   834
its conclusion; the premises of the implication are immediately made
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   835
assumptions of that case. That is, if in the above proof we replace
49837
nipkow
parents: 48985
diff changeset
   836
\isacom{show}~@{text"\"P(n)\""} by
nipkow
parents: 48985
diff changeset
   837
\mbox{\isacom{show}~@{text"\"A(n) \<Longrightarrow> P(n)\""}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   838
then \isacom{case}~@{text 0} stands for
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   839
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   840
\isacom{assume} \ @{text"0: \"A(0)\""}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   841
\isacom{let} @{text"?case = \"P(0)\""}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   842
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   843
and \isacom{case}~@{text"(Suc n)"} stands for
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   844
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   845
\isacom{fix} @{text n}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   846
\isacom{assume} @{text"Suc:"}
47306
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
   847
  \begin{tabular}[t]{l}@{text"\"A(n) \<Longrightarrow> P(n)\""}\\@{text"\"A(Suc n)\""}\end{tabular}\\
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   848
\isacom{let} @{text"?case = \"P(Suc n)\""}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   849
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   850
The list of assumptions @{text Suc} is actually subdivided
56989
nipkow
parents: 56451
diff changeset
   851
into @{text"Suc.IH"}, the induction hypotheses (here @{text"A(n) \<Longrightarrow> P(n)"}),
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   852
and @{text"Suc.prems"}, the premises of the goal being proved
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   853
(here @{text"A(Suc n)"}).
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   854
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   855
Induction works for any datatype.
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   856
Proving a goal @{text"\<lbrakk> A\<^sub>1(x); \<dots>; A\<^sub>k(x) \<rbrakk> \<Longrightarrow> P(x)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   857
by induction on @{text x} generates a proof obligation for each constructor
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   858
@{text C} of the datatype. The command \isacom{case}~@{text"(C x\<^sub>1 \<dots> x\<^sub>n)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   859
performs the following steps:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   860
\begin{enumerate}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   861
\item \isacom{fix} @{text"x\<^sub>1 \<dots> x\<^sub>n"}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   862
\item \isacom{assume} the induction hypotheses (calling them @{text C.IH}\index{IH@@{text".IH"}})
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   863
 and the premises \mbox{@{text"A\<^sub>i(C x\<^sub>1 \<dots> x\<^sub>n)"}} (calling them @{text"C.prems"}\index{prems@@{text".prems"}})
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   864
 and calling the whole list @{text C}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   865
\item \isacom{let} @{text"?case = \"P(C x\<^sub>1 \<dots> x\<^sub>n)\""}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   866
\end{enumerate}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   867
\index{structural induction|)}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   868
65437
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   869
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   870
\ifsem\else
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   871
\subsection{Computation Induction}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   872
\index{rule induction}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   873
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   874
In \autoref{sec:recursive-funs} we introduced computation induction and
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   875
its realization in Isabelle: the definition
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   876
of a recursive function \<open>f\<close> via \isacom{fun} proves the corresponding computation
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   877
induction rule called \<open>f.induct\<close>. Induction with this rule looks like in
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   878
\autoref{sec:recursive-funs}, but now with \isacom{proof} instead of \isacom{apply}:
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   879
\begin{quote}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   880
\isacom{proof} (\<open>induction x\<^sub>1 \<dots> x\<^sub>k rule: f.induct\<close>)
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   881
\end{quote}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   882
Just as for structural induction, this creates several cases, one for each
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   883
defining equation for \<open>f\<close>. By default (if the equations have not been named
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   884
by the user), the cases are numbered. That is, they are started by
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   885
\begin{quote}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   886
\isacom{case} (\<open>i x y ...\<close>)
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   887
\end{quote}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   888
where \<open>i = 1,...,n\<close>, \<open>n\<close> is the number of equations defining \<open>f\<close>,
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   889
and \<open>x y ...\<close> are the variables in equation \<open>i\<close>. Note the following:
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   890
\begin{itemize}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   891
\item
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   892
Although \<open>i\<close> is an Isar name, \<open>i.IH\<close> (or similar) is not. You need
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   893
double quotes: "\<open>i.IH\<close>". When indexing the name, write "\<open>i.IH\<close>"(1),
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   894
not "\<open>i.IH\<close>(1)".
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   895
\item
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   896
If defining equations for \<open>f\<close> overlap, \isacom{fun} instantiates them to make
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   897
them nonoverlapping. This means that one user-provided equation may lead to
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   898
several equations and thus to several cases in the induction rule.
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   899
These have names of the form "\<open>i_j\<close>", where \<open>i\<close> is the number of the original
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   900
equation and the system-generated \<open>j\<close> indicates the subcase.
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   901
\end{itemize}
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   902
In Isabelle/jEdit, the \<open>induction\<close> proof method displays a proof skeleton
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   903
with all \isacom{case}s. This is particularly useful for computation induction
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   904
and the following rule induction.
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   905
\fi
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   906
b8fc7e2e1b35 doc for comp. ind.
nipkow
parents: 65352
diff changeset
   907
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
   908
\subsection{Rule Induction}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
   909
\index{rule induction|(}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   910
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   911
Recall the inductive and recursive definitions of even numbers in
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   912
\autoref{sec:inductive-defs}:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   913
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   914
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   915
inductive ev :: "nat \<Rightarrow> bool" where
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   916
ev0: "ev 0" |
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   917
evSS: "ev n \<Longrightarrow> ev(Suc(Suc n))"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   918
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   919
fun evn :: "nat \<Rightarrow> bool" where
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   920
"evn 0 = True" |
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   921
"evn (Suc 0) = False" |
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   922
"evn (Suc(Suc n)) = evn n"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   923
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   924
text\<open>We recast the proof of @{prop"ev n \<Longrightarrow> evn n"} in Isar. The
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   925
left column shows the actual proof text, the right column shows
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   926
the implicit effect of the two \isacom{case} commands:\<close>text_raw\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   927
\begin{tabular}{@ {}l@ {\qquad}l@ {}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   928
\begin{minipage}[t]{.5\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   929
\isa{%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   930
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   931
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   932
lemma "ev n \<Longrightarrow> evn n"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   933
proof(induction rule: ev.induct)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   934
  case ev0
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   935
  show ?case by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   936
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   937
  case evSS
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   938
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   939
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   940
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   941
  thus ?case by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   942
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   943
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   944
text_raw \<open>}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   945
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   946
&
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   947
\begin{minipage}[t]{.5\textwidth}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   948
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   949
~\\
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   950
\isacom{let} @{text"?case = \"evn 0\""}\\
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   951
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   952
~\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   953
\isacom{fix} @{text n}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   954
\isacom{assume} @{text"evSS:"}
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   955
  \begin{tabular}[t]{l} @{text"\"ev n\""}\\@{text"\"evn n\""}\end{tabular}\\
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   956
\isacom{let} @{text"?case = \"evn(Suc(Suc n))\""}\\
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   957
\end{minipage}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   958
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   959
\medskip
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   960
\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   961
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   962
The proof resembles structural induction, but the induction rule is given
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   963
explicitly and the names of the cases are the names of the rules in the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   964
inductive definition.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   965
Let us examine the two assumptions named @{thm[source]evSS}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   966
@{prop "ev n"} is the premise of rule @{thm[source]evSS}, which we may assume
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   967
because we are in the case where that rule was used; @{prop"evn n"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   968
is the induction hypothesis.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   969
\begin{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   970
Because each \isacom{case} command introduces a list of assumptions
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   971
named like the case name, which is the name of a rule of the inductive
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   972
definition, those rules now need to be accessed with a qualified name, here
58522
nipkow
parents: 58521
diff changeset
   973
@{thm[source] ev.ev0} and @{thm[source] ev.evSS}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   974
\end{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   975
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   976
In the case @{thm[source]evSS} of the proof above we have pretended that the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   977
system fixes a variable @{text n}.  But unless the user provides the name
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   978
@{text n}, the system will just invent its own name that cannot be referred
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   979
to.  In the above proof, we do not need to refer to it, hence we do not give
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   980
it a specific name. In case one needs to refer to it one writes
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   981
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   982
\isacom{case} @{text"(evSS m)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   983
\end{quote}
58605
nipkow
parents: 58602
diff changeset
   984
like \isacom{case}~@{text"(Suc n)"} in earlier structural inductions.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   985
The name @{text m} is an arbitrary choice. As a result,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   986
case @{thm[source] evSS} is derived from a renamed version of
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   987
rule @{thm[source] evSS}: @{text"ev m \<Longrightarrow> ev(Suc(Suc m))"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   988
Here is an example with a (contrived) intermediate step that refers to @{text m}:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   989
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   990
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   991
lemma "ev n \<Longrightarrow> evn n"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   992
proof(induction rule: ev.induct)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   993
  case ev0 show ?case by simp
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   994
next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   995
  case (evSS m)
61429
63fb7a68a12c even -> evn to avoid clash with global even
nipkow
parents: 61022
diff changeset
   996
  have "evn(Suc(Suc m)) = evn m" by simp
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
   997
  thus ?case using \<open>evn m\<close> by blast
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   998
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   999
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1000
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1001
\indent
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1002
In general, let @{text I} be a (for simplicity unary) inductively defined
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1003
predicate and let the rules in the definition of @{text I}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1004
be called @{text "rule\<^sub>1"}, \dots, @{text "rule\<^sub>n"}. A proof by rule
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1005
induction follows this pattern:\index{inductionrule@@{text"induction ... rule:"}}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1006
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1007
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1008
(*<*)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1009
inductive I where rule\<^sub>1: "I()" |  rule\<^sub>2: "I()" |  rule\<^sub>n: "I()"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1010
lemma "I x \<Longrightarrow> P x" proof-(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1011
show "I x \<Longrightarrow> P x"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1012
proof(induction rule: I.induct)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1013
  case rule\<^sub>1
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1014
  text_raw\<open>\\[-.4ex]\mbox{}\ \ $\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1015
  show ?case (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1016
next
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1017
  text_raw\<open>\\[-.4ex]$\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1018
(*<*)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1019
  case rule\<^sub>2
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1020
  show ?case sorry
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1021
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1022
next
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1023
  case rule\<^sub>n
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1024
  text_raw\<open>\\[-.4ex]\mbox{}\ \ $\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}\<close>
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1025
  show ?case (*<*)sorry(*>*)text_raw\<open>\ \isasymproof\\\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1026
qed(*<*)qed(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1027
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1028
text\<open>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1029
One can provide explicit variable names by writing
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1030
\isacom{case}~@{text"(rule\<^sub>i x\<^sub>1 \<dots> x\<^sub>k)"}, thus renaming the first @{text k}
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1031
free variables in rule @{text i} to @{text"x\<^sub>1 \<dots> x\<^sub>k"},
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1032
going through rule @{text i} from left to right.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1033
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
  1034
\subsection{Assumption Naming}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1035
\label{sec:assm-naming}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1036
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1037
In any induction, \isacom{case}~@{text name} sets up a list of assumptions
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1038
also called @{text name}, which is subdivided into three parts:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1039
\begin{description}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1040
\item[@{text name.IH}]\index{IH@@{text".IH"}} contains the induction hypotheses.
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1041
\item[@{text name.hyps}]\index{hyps@@{text".hyps"}} contains all the other hypotheses of this case in the
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1042
induction rule. For rule inductions these are the hypotheses of rule
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1043
@{text name}, for structural inductions these are empty.
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1044
\item[@{text name.prems}]\index{prems@@{text".prems"}} contains the (suitably instantiated) premises
58504
nipkow
parents: 58502
diff changeset
  1045
of the statement being proved, i.e., the @{text A\<^sub>i} when
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
  1046
proving @{text"\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1047
\end{description}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1048
\begin{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1049
Proof method @{text induct} differs from @{text induction}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1050
only in this naming policy: @{text induct} does not distinguish
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1051
@{text IH} from @{text hyps} but subsumes @{text IH} under @{text hyps}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1052
\end{warn}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1053
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1054
More complicated inductive proofs than the ones we have seen so far
58602
ab56811d76c6 tuned spaces
nipkow
parents: 58522
diff changeset
  1055
often need to refer to specific assumptions --- just @{text name} or even
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1056
@{text name.prems} and @{text name.IH} can be too unspecific.
58504
nipkow
parents: 58502
diff changeset
  1057
This is where the indexing of fact lists comes in handy, e.g.,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1058
@{text"name.IH(2)"} or @{text"name.prems(1-2)"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1059
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
  1060
\subsection{Rule Inversion}
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
  1061
\label{sec:rule-inversion}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1062
\index{rule inversion|(}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1063
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
  1064
Rule inversion is case analysis of which rule could have been used to
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1065
derive some fact. The name \conceptnoidx{rule inversion} emphasizes that we are
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1066
reasoning backwards: by which rules could some given fact have been proved?
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1067
For the inductive definition of @{const ev}, rule inversion can be summarized
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1068
like this:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1069
@{prop[display]"ev n \<Longrightarrow> n = 0 \<or> (EX k. n = Suc(Suc k) \<and> ev k)"}
47711
c1cca2a052e4 doc update
nipkow
parents: 47704
diff changeset
  1070
The realisation in Isabelle is a case analysis.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1071
A simple example is the proof that @{prop"ev n \<Longrightarrow> ev (n - 2)"}. We
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1072
already went through the details informally in \autoref{sec:Logic:even}. This
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1073
is the Isar proof:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1074
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1075
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1076
notepad
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1077
begin fix n
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1078
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1079
  assume "ev n"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1080
  from this have "ev(n - 2)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1081
  proof cases
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1082
    case ev0 thus "ev(n - 2)" by (simp add: ev.ev0)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1083
  next
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1084
    case (evSS k) thus "ev(n - 2)" by (simp add: ev.evSS)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1085
  qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1086
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1087
end
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1088
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1089
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1090
text\<open>The key point here is that a case analysis over some inductively
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1091
defined predicate is triggered by piping the given fact
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1092
(here: \isacom{from}~@{text this}) into a proof by @{text cases}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1093
Let us examine the assumptions available in each case. In case @{text ev0}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1094
we have @{text"n = 0"} and in case @{text evSS} we have @{prop"n = Suc(Suc k)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1095
and @{prop"ev k"}. In each case the assumptions are available under the name
56989
nipkow
parents: 56451
diff changeset
  1096
of the case; there is no fine-grained naming schema like there is for induction.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1097
47704
8b4cd98f944e doc update
nipkow
parents: 47306
diff changeset
  1098
Sometimes some rules could not have been used to derive the given fact
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1099
because constructors clash. As an extreme example consider
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1100
rule inversion applied to @{prop"ev(Suc 0)"}: neither rule @{text ev0} nor
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1101
rule @{text evSS} can yield @{prop"ev(Suc 0)"} because @{text"Suc 0"} unifies
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1102
neither with @{text 0} nor with @{term"Suc(Suc n)"}. Impossible cases do not
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1103
have to be proved. Hence we can prove anything from @{prop"ev(Suc 0)"}:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1104
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1105
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1106
notepad begin fix P
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1107
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1108
  assume "ev(Suc 0)" then have P by cases
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1109
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1110
end
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1111
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1112
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1113
text\<open>That is, @{prop"ev(Suc 0)"} is simply not provable:\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1114
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1115
lemma "\<not> ev(Suc 0)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1116
proof
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1117
  assume "ev(Suc 0)" then show False by cases
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1118
qed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1119
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1120
text\<open>Normally not all cases will be impossible. As a simple exercise,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1121
prove that \mbox{@{prop"\<not> ev(Suc(Suc(Suc 0)))"}.}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1122
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52059
diff changeset
  1123
\subsection{Advanced Rule Induction}
51445
nipkow
parents: 51443
diff changeset
  1124
\label{sec:advanced-rule-induction}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1125
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1126
So far, rule induction was always applied to goals of the form @{text"I x y z \<Longrightarrow> \<dots>"}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1127
where @{text I} is some inductively defined predicate and @{text x}, @{text y}, @{text z}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1128
are variables. In some rare situations one needs to deal with an assumption where
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1129
not all arguments @{text r}, @{text s}, @{text t} are variables:
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1130
\begin{isabelle}
61517
6cf5215afe8c adapted to 436b7fe89cdc
nipkow
parents: 61429
diff changeset
  1131
\isacom{lemma} @{text"\"I r s t \<Longrightarrow> \<dots>\""}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1132
\end{isabelle}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1133
Applying the standard form of
54577
nipkow
parents: 54436
diff changeset
  1134
rule induction in such a situation will lead to strange and typically unprovable goals.
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1135
We can easily reduce this situation to the standard one by introducing
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1136
new variables @{text x}, @{text y}, @{text z} and reformulating the goal like this:
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1137
\begin{isabelle}
61517
6cf5215afe8c adapted to 436b7fe89cdc
nipkow
parents: 61429
diff changeset
  1138
\isacom{lemma} @{text"\"I x y z \<Longrightarrow> x = r \<Longrightarrow> y = s \<Longrightarrow> z = t \<Longrightarrow> \<dots>\""}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1139
\end{isabelle}
56989
nipkow
parents: 56451
diff changeset
  1140
Standard rule induction will work fine now, provided the free variables in
58502
nipkow
parents: 58486
diff changeset
  1141
@{text r}, @{text s}, @{text t} are generalized via @{text"arbitrary"}.
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1142
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1143
However, induction can do the above transformation for us, behind the curtains, so we never
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1144
need to see the expanded version of the lemma. This is what we need to write:
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1145
\begin{isabelle}
61517
6cf5215afe8c adapted to 436b7fe89cdc
nipkow
parents: 61429
diff changeset
  1146
\isacom{lemma} @{text"\"I r s t \<Longrightarrow> \<dots>\""}\isanewline
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1147
\isacom{proof}@{text"(induction \"r\" \"s\" \"t\" arbitrary: \<dots> rule: I.induct)"}\index{inductionrule@@{text"induction ... rule:"}}\index{arbitrary@@{text"arbitrary:"}}
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1148
\end{isabelle}
58605
nipkow
parents: 58602
diff changeset
  1149
Like for rule inversion, cases that are impossible because of constructor clashes
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1150
will not show up at all. Here is a concrete example:\<close>
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1151
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1152
lemma "ev (Suc m) \<Longrightarrow> \<not> ev m"
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1153
proof(induction "Suc m" arbitrary: m rule: ev.induct)
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1154
  fix n assume IH: "\<And>m. n = Suc m \<Longrightarrow> \<not> ev m"
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1155
  show "\<not> ev (Suc n)"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
  1156
  proof \<comment> \<open>contradiction\<close>
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1157
    assume "ev(Suc n)"
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1158
    thus False
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67406
diff changeset
  1159
    proof cases \<comment> \<open>rule inversion\<close>
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1160
      fix k assume "n = Suc k" "ev k"
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1161
      thus False using IH by auto
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1162
    qed
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1163
  qed
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1164
qed
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1165
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1166
text\<open>
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1167
Remarks:
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1168
\begin{itemize}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1169
\item 
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1170
Instead of the \isacom{case} and @{text ?case} magic we have spelled all formulas out.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1171
This is merely for greater clarity.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1172
\item
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1173
We only need to deal with one case because the @{thm[source] ev0} case is impossible.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1174
\item
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1175
The form of the @{text IH} shows us that internally the lemma was expanded as explained
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1176
above: \noquotes{@{prop[source]"ev x \<Longrightarrow> x = Suc m \<Longrightarrow> \<not> ev m"}}.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1177
\item
61013
nipkow
parents: 61012
diff changeset
  1178
The goal @{prop"\<not> ev (Suc n)"} may surprise. The expanded version of the lemma
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1179
would suggest that we have a \isacom{fix} @{text m} \isacom{assume} @{prop"Suc(Suc n) = Suc m"}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1180
and need to show @{prop"\<not> ev m"}. What happened is that Isabelle immediately
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1181
simplified @{prop"Suc(Suc n) = Suc m"} to @{prop"Suc n = m"} and could then eliminate
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1182
@{text m}. Beware of such nice surprises with this advanced form of induction.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1183
\end{itemize}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1184
\begin{warn}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1185
This advanced form of induction does not support the @{text IH}
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1186
naming schema explained in \autoref{sec:assm-naming}:
56989
nipkow
parents: 56451
diff changeset
  1187
the induction hypotheses are instead found under the name @{text hyps},
nipkow
parents: 56451
diff changeset
  1188
as they are for the simpler
51443
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1189
@{text induct} method.
4edb82207c5c added advanced rule induction subsection
nipkow
parents: 49837
diff changeset
  1190
\end{warn}
55361
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1191
\index{induction|)}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1192
\index{cases@@{text"cases"}|)}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1193
\index{case@\isacom{case}|)}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1194
\index{case?@@{text"?case"}|)}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1195
\index{rule induction|)}
d459a63ca42f more indexing
nipkow
parents: 55359
diff changeset
  1196
\index{rule inversion|)}
54218
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
  1197
54436
nipkow
parents: 54292
diff changeset
  1198
\subsection*{Exercises}
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
  1199
54232
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1200
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1201
\exercise
54292
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1202
Give a structured proof by rule inversion:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1203
\<close>
54232
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1204
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1205
lemma assumes a: "ev(Suc(Suc n))" shows "ev n"
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1206
(*<*)oops(*>*)
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1207
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1208
text\<open>
54232
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1209
\endexercise
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1210
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
  1211
\begin{exercise}
54232
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1212
Give a structured proof of @{prop "\<not> ev(Suc(Suc(Suc 0)))"}
e039a9b9700d more exercises
nipkow
parents: 54218
diff changeset
  1213
by rule inversions. If there are no cases to be proved you can close
61013
nipkow
parents: 61012
diff changeset
  1214
a proof immediately with \isacom{qed}.
54218
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
  1215
\end{exercise}
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
  1216
07c0c121a8dc more exercises
nipkow
parents: 53015
diff changeset
  1217
\begin{exercise}
54292
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1218
Recall predicate @{text star} from \autoref{sec:star} and @{text iter}
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1219
from Exercise~\ref{exe:iter}. Prove @{prop "iter r n x y \<Longrightarrow> star r x y"}
56989
nipkow
parents: 56451
diff changeset
  1220
in a structured style; do not just sledgehammer each case of the
54292
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1221
required induction.
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1222
\end{exercise}
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1223
ce4a17b2e373 more exercises
nipkow
parents: 54232
diff changeset
  1224
\begin{exercise}
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
  1225
Define a recursive function @{text "elems ::"} @{typ"'a list \<Rightarrow> 'a set"}
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
  1226
and prove @{prop "x : elems xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> elems ys"}.
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
  1227
\end{exercise}
61012
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1228
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1229
\begin{exercise}
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1230
Extend Exercise~\ref{exe:cfg} with a function that checks if some
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1231
\mbox{@{text "alpha list"}} is a balanced
61022
1c4ae64636bb tuned spacing
nipkow
parents: 61021
diff changeset
  1232
string of parentheses. More precisely, define a \mbox{recursive} function
61012
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1233
@{text "balanced :: nat \<Rightarrow> alpha list \<Rightarrow> bool"} such that @{term"balanced n w"}
61022
1c4ae64636bb tuned spacing
nipkow
parents: 61021
diff changeset
  1234
is true iff (informally) @{text"S (a\<^sup>n @ w)"}. Formally, prove that
1c4ae64636bb tuned spacing
nipkow
parents: 61021
diff changeset
  1235
@{prop "balanced n w \<longleftrightarrow> S (replicate n a @ w)"} where
61012
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1236
@{const replicate} @{text"::"} @{typ"nat \<Rightarrow> 'a \<Rightarrow> 'a list"} is predefined
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1237
and @{term"replicate n x"} yields the list @{text"[x, \<dots>, x]"} of length @{text n}.
40a0a4077126 nex exercise
nipkow
parents: 58999
diff changeset
  1238
\end{exercise}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67299
diff changeset
  1239
\<close>
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1240
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1241
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1242
end
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
  1243
(*>*)