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