doc-src/TutorialI/Rules/rules.tex
author wenzelm
Sun, 30 Dec 2007 23:07:27 +0100
changeset 25748 55a458a31e37
parent 25264 7007bc8ddae4
child 27167 a99747ccba87
permissions -rw-r--r--
added PROMPT message;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10792
78dfc5904eea a few extra brackets
paulson
parents: 10596
diff changeset
     1
% $Id$
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
     2
%!TEX root = ../tutorial.tex
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     3
\chapter{The Rules of the Game}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     4
\label{chap:rules}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     5
 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
     6
This chapter outlines the concepts and techniques that underlie reasoning
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
     7
in Isabelle.  Until now, we have proved everything using only induction and
13439
2f98365f57a8 *** empty log message ***
nipkow
parents: 12815
diff changeset
     8
simplification, but any serious verification project requires more elaborate
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
     9
forms of inference.  The chapter also introduces the fundamentals of
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    10
predicate logic.  The first examples in this chapter will consist of
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    11
detailed, low-level proof steps.  Later, we shall see how to automate such
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    12
reasoning using the methods
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    13
\isa{blast},
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    14
\isa{auto} and others.  Backward or goal-directed proof is our usual style,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    15
but the chapter also introduces forward reasoning, where one theorem is
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    16
transformed to yield another.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    17
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
    18
\section{Natural Deduction}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    19
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    20
\index{natural deduction|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    21
In Isabelle, proofs are constructed using inference rules. The 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    22
most familiar inference rule is probably \emph{modus ponens}:%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    23
\index{modus ponens@\emph{modus ponens}} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    24
\[ \infer{Q}{P\imp Q & P} \]
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    25
This rule says that from $P\imp Q$ and $P$ we may infer~$Q$.  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    26
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    27
\textbf{Natural deduction} is an attempt to formalize logic in a way 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    28
that mirrors human reasoning patterns. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    29
For each logical symbol (say, $\conj$), there 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    30
are two kinds of rules: \textbf{introduction} and \textbf{elimination} rules. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    31
The introduction rules allow us to infer this symbol (say, to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    32
infer conjunctions). The elimination rules allow us to deduce 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    33
consequences from this symbol. Ideally each rule should mention 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    34
one symbol only.  For predicate logic this can be 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    35
done, but when users define their own concepts they typically 
11255
ca546b170471 *** empty log message ***
paulson
parents: 11234
diff changeset
    36
have to refer to other symbols as well.  It is best not to be dogmatic.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    37
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    38
Natural deduction generally deserves its name.  It is easy to use.  Each
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    39
proof step consists of identifying the outermost symbol of a formula and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    40
applying the corresponding rule.  It creates new subgoals in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    41
an obvious way from parts of the chosen formula.  Expanding the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    42
definitions of constants can blow up the goal enormously.  Deriving natural
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    43
deduction rules for such constants lets us reason in terms of their key
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    44
properties, which might otherwise be obscured by the technicalities of its
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    45
definition.  Natural deduction rules also lend themselves to automation.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    46
Isabelle's
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    47
\textbf{classical reasoner} accepts any suitable  collection of natural deduction
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    48
rules and uses them to search for proofs automatically.  Isabelle is designed around
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    49
natural deduction and many of its tools use the terminology of introduction
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    50
and elimination rules.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    51
\index{natural deduction|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    52
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    53
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
    54
\section{Introduction Rules}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    55
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    56
\index{introduction rules|(}%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
    57
An introduction rule tells us when we can infer a formula 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    58
containing a specific logical symbol. For example, the conjunction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    59
introduction rule says that if we have $P$ and if we have $Q$ then 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    60
we have $P\conj Q$. In a mathematics text, it is typically shown 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    61
like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    62
\[  \infer{P\conj Q}{P & Q} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    63
The rule introduces the conjunction
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
    64
symbol~($\conj$) in its conclusion.  In Isabelle proofs we
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    65
mainly  reason backwards.  When we apply this rule, the subgoal already has
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    66
the form of a conjunction; the proof step makes this conjunction symbol
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    67
disappear. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    68
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    69
In Isabelle notation, the rule looks like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    70
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
    71
\isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P\ \isasymand\ ?Q\rulenamedx{conjI}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    72
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    73
Carefully examine the syntax.  The premises appear to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    74
left of the arrow and the conclusion to the right.  The premises (if 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    75
more than one) are grouped using the fat brackets.  The question marks
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    76
indicate \textbf{schematic variables} (also called
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
    77
\textbf{unknowns}):\index{unknowns|bold} they may
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    78
be replaced by arbitrary formulas.  If we use the rule backwards, Isabelle
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    79
tries to unify the current subgoal with the conclusion of the rule, which
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    80
has the form \isa{?P\ \isasymand\ ?Q}.  (Unification is discussed below,
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
    81
{\S}\ref{sec:unification}.)  If successful,
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    82
it yields new subgoals given by the formulas assigned to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    83
\isa{?P} and \isa{?Q}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    84
12333
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
    85
The following trivial proof illustrates how rules work.  It also introduces a
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
    86
style of indentation.  If a command adds a new subgoal, then the next
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
    87
command's indentation is increased by one space; if it proves a subgoal, then
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
    88
the indentation is reduced.  This provides the reader with hints about the
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
    89
subgoal structure.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    90
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
    91
\isacommand{lemma}\ conj_rule:\ "\isasymlbrakk P;\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    92
Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\
10301
paulson
parents: 10295
diff changeset
    93
(Q\ \isasymand\ P)"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    94
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    95
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    96
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    97
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    98
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    99
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   100
At the start, Isabelle presents 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   101
us with the assumptions (\isa{P} and~\isa{Q}) and with the goal to be proved,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   102
\isa{P\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   103
(Q\ \isasymand\ P)}.  We are working backwards, so when we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   104
apply conjunction introduction, the rule removes the outermost occurrence
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   105
of the \isa{\isasymand} symbol.  To apply a  rule to a subgoal, we apply
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   106
the proof method \isa{rule} --- here with \isa{conjI}, the  conjunction
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   107
introduction rule. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   108
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   109
%\isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ Q\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   110
%\isasymand\ P\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   111
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   112
\ 2.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\ \isasymand\ P
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   113
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   114
Isabelle leaves two new subgoals: the two halves of the original conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   115
The first is simply \isa{P}, which is trivial, since \isa{P} is among 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   116
the assumptions.  We can apply the \methdx{assumption} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   117
method, which proves a subgoal by finding a matching assumption.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   118
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   119
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   120
Q\ \isasymand\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   121
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   122
We are left with the subgoal of proving  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   123
\isa{Q\ \isasymand\ P} from the assumptions \isa{P} and~\isa{Q}.  We apply
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   124
\isa{rule conjI} again. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   125
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   126
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\isanewline
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   127
\ 2.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   128
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   129
We are left with two new subgoals, \isa{Q} and~\isa{P}, each of which can be proved
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   130
using the \isa{assumption} method.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   131
\index{introduction rules|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   132
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   133
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   134
\section{Elimination Rules}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   135
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   136
\index{elimination rules|(}%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   137
Elimination rules work in the opposite direction from introduction 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   138
rules. In the case of conjunction, there are two such rules. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   139
From $P\conj Q$ we infer $P$. also, from $P\conj Q$  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   140
we infer $Q$:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   141
\[ \infer{P}{P\conj Q} \qquad \infer{Q}{P\conj Q}  \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   142
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   143
Now consider disjunction. There are two introduction rules, which resemble inverted forms of the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   144
conjunction elimination rules:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   145
\[ \infer{P\disj Q}{P} \qquad \infer{P\disj Q}{Q}  \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   146
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   147
What is the disjunction elimination rule?  The situation is rather different from 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   148
conjunction.  From $P\disj Q$ we cannot conclude  that $P$ is true and we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   149
cannot conclude that $Q$ is true; there are no direct
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   150
elimination rules of the sort that we have seen for conjunction.  Instead,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   151
there is an elimination  rule that works indirectly.  If we are trying  to prove
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   152
something else, say $R$, and we know that $P\disj Q$ holds,  then we have to consider
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   153
two cases.  We can assume that $P$ is true  and prove $R$ and then assume that $Q$ is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   154
true and prove $R$ a second  time.  Here we see a fundamental concept used in natural
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   155
deduction:  that of the \textbf{assumptions}. We have to prove $R$ twice, under
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   156
different assumptions.  The assumptions are local to these subproofs and are visible 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   157
nowhere else. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   158
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   159
In a logic text, the disjunction elimination rule might be shown 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   160
like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   161
\[ \infer{R}{P\disj Q & \infer*{R}{[P]} & \infer*{R}{[Q]}} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   162
The assumptions $[P]$ and $[Q]$ are bracketed 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   163
to emphasize that they are local to their subproofs.  In Isabelle 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   164
notation, the already-familiar \isa{\isasymLongrightarrow} syntax serves the
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   165
same  purpose:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   166
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   167
\isasymlbrakk?P\ \isasymor\ ?Q;\ ?P\ \isasymLongrightarrow\ ?R;\ ?Q\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulenamedx{disjE}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   168
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   169
When we use this sort of elimination rule backwards, it produces 
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   170
a case split.  (We have seen this before, in proofs by induction.)  The following  proof
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   171
illustrates the use of disjunction elimination.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   172
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
   173
\isacommand{lemma}\ disj_swap:\ "P\ \isasymor\ Q\ 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   174
\isasymLongrightarrow\ Q\ \isasymor\ P"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   175
\isacommand{apply}\ (erule\ disjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   176
\ \isacommand{apply}\ (rule\ disjI2)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   177
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   178
\isacommand{apply}\ (rule\ disjI1)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   179
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   180
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   181
We assume \isa{P\ \isasymor\ Q} and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   182
must prove \isa{Q\ \isasymor\ P}\@.  Our first step uses the disjunction
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   183
elimination rule, \isa{disjE}\@.  We invoke it using \methdx{erule}, a
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   184
method designed to work with elimination rules.  It looks for an assumption that
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   185
matches the rule's first premise.  It deletes the matching assumption,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   186
regards the first premise as proved and returns subgoals corresponding to
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   187
the remaining premises.  When we apply \isa{erule} to \isa{disjE}, only two
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   188
subgoals result.  This is better than applying it using \isa{rule}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   189
to get three subgoals, then proving the first by assumption: the other
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   190
subgoals would have the redundant assumption 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   191
\hbox{\isa{P\ \isasymor\ Q}}.
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   192
Most of the time, \isa{erule} is  the best way to use elimination rules, since it
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   193
replaces an assumption by its subformulas; only rarely does the original
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   194
assumption remain useful.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   195
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   196
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   197
%P\ \isasymor\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   198
\ 1.\ P\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   199
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   200
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   201
These are the two subgoals returned by \isa{erule}.  The first assumes
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   202
\isa{P} and the  second assumes \isa{Q}.  Tackling the first subgoal, we
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   203
need to  show \isa{Q\ \isasymor\ P}\@.  The second introduction rule
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   204
(\isa{disjI2}) can reduce this  to \isa{P}, which matches the assumption.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   205
So, we apply the
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   206
\isa{rule}  method with \isa{disjI2} \ldots
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   207
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   208
\ 1.\ P\ \isasymLongrightarrow\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   209
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   210
\end{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   211
\ldots and finish off with the \isa{assumption} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   212
method.  We are left with the other subgoal, which 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   213
assumes \isa{Q}.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   214
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   215
\ 1.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   216
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   217
Its proof is similar, using the introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   218
rule \isa{disjI1}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   219
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   220
The result of this proof is a new inference rule \isa{disj_swap}, which is neither 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   221
an introduction nor an elimination rule, but which might 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   222
be useful.  We can use it to replace any goal of the form $Q\disj P$
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   223
by a one of the form $P\disj Q$.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   224
\index{elimination rules|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   225
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   226
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   227
\section{Destruction Rules: Some Examples}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   228
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   229
\index{destruction rules|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   230
Now let us examine the analogous proof for conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   231
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
   232
\isacommand{lemma}\ conj_swap:\ "P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   233
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   234
\ \isacommand{apply}\ (drule\ conjunct2)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   235
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   236
\isacommand{apply}\ (drule\ conjunct1)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   237
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   238
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   239
Recall that the conjunction elimination rules --- whose Isabelle names are 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   240
\isa{conjunct1} and \isa{conjunct2} --- simply return the first or second half
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   241
of a conjunction.  Rules of this sort (where the conclusion is a subformula of a
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   242
premise) are called \textbf{destruction} rules because they take apart and destroy
10978
5eebea8f359f *** empty log message ***
nipkow
parents: 10971
diff changeset
   243
a premise.%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   244
\footnote{This Isabelle terminology has no counterpart in standard logic texts, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   245
although the distinction between the two forms of elimination rule is well known. 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   246
Girard \cite[page 74]{girard89},\index{Girard, Jean-Yves|fnote}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   247
for example, writes ``The elimination rules 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   248
[for $\disj$ and $\exists$] are very
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   249
bad.  What is catastrophic about them is the parasitic presence of a formula [$R$]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   250
which has no structural link with the formula which is eliminated.''}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   251
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   252
The first proof step applies conjunction introduction, leaving 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   253
two subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   254
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   255
%P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   256
\ 1.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   257
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   258
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   259
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   260
To invoke the elimination rule, we apply a new method, \isa{drule}. 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   261
Think of the \isa{d} as standing for \textbf{destruction} (or \textbf{direct}, if
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   262
you prefer).   Applying the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   263
second conjunction rule using \isa{drule} replaces the assumption 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   264
\isa{P\ \isasymand\ Q} by \isa{Q}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   265
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   266
\ 1.\ Q\ \isasymLongrightarrow\ Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   267
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   268
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   269
The resulting subgoal can be proved by applying \isa{assumption}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   270
The other subgoal is similarly proved, using the \isa{conjunct1} rule and the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   271
\isa{assumption} method.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   272
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   273
Choosing among the methods \isa{rule}, \isa{erule} and \isa{drule} is up to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   274
you.  Isabelle does not attempt to work out whether a rule 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   275
is an introduction rule or an elimination rule.  The 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   276
method determines how the rule will be interpreted. Many rules 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   277
can be used in more than one way.  For example, \isa{disj_swap} can 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   278
be applied to assumptions as well as to goals; it replaces any
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   279
assumption of the form
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   280
$P\disj Q$ by a one of the form $Q\disj P$.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   281
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   282
Destruction rules are simpler in form than indirect rules such as \isa{disjE},
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   283
but they can be inconvenient.  Each of the conjunction rules discards half 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   284
of the formula, when usually we want to take both parts of the conjunction as new
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   285
assumptions.  The easiest way to do so is by using an 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   286
alternative conjunction elimination rule that resembles \isa{disjE}\@.  It is
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   287
seldom, if ever, seen in logic books.  In Isabelle syntax it looks like this: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   288
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   289
\isasymlbrakk?P\ \isasymand\ ?Q;\ \isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulenamedx{conjE}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   290
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   291
\index{destruction rules|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   292
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   293
\begin{exercise}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   294
Use the rule \isa{conjE} to shorten the proof above. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   295
\end{exercise}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   296
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   297
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   298
\section{Implication}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   299
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   300
\index{implication|(}%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   301
At the start of this chapter, we saw the rule \emph{modus ponens}.  It is, in fact,
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   302
a destruction rule. The matching introduction rule looks like this 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   303
in Isabelle: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   304
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   305
(?P\ \isasymLongrightarrow\ ?Q)\ \isasymLongrightarrow\ ?P\
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   306
\isasymlongrightarrow\ ?Q\rulenamedx{impI}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   307
\end{isabelle}
12535
wenzelm
parents: 12408
diff changeset
   308
And this is \emph{modus ponens}\index{modus ponens@\emph{modus ponens}}:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   309
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   310
\isasymlbrakk?P\ \isasymlongrightarrow\ ?Q;\ ?P\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   311
\isasymLongrightarrow\ ?Q
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   312
\rulenamedx{mp}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   313
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   314
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   315
Here is a proof using the implication rules.  This 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   316
lemma performs a sort of uncurrying, replacing the two antecedents 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   317
of a nested implication by a conjunction.  The proof illustrates
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   318
how assumptions work.  At each proof step, the subgoals inherit the previous
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   319
assumptions, perhaps with additions or deletions.  Rules such as
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   320
\isa{impI} and \isa{disjE} add assumptions, while applying \isa{erule} or
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   321
\isa{drule} deletes the matching assumption.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   322
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   323
\isacommand{lemma}\ imp_uncurry:\
10301
paulson
parents: 10295
diff changeset
   324
"P\ \isasymlongrightarrow\ (Q\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   325
\isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   326
\isasymand\ Q\ \isasymlongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   327
R"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   328
\isacommand{apply}\ (rule\ impI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   329
\isacommand{apply}\ (erule\ conjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   330
\isacommand{apply}\ (drule\ mp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   331
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   332
\isacommand{apply}\ (drule\ mp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   333
\ \ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   334
\ \isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   335
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   336
First, we state the lemma and apply implication introduction (\isa{rule impI}), 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   337
which moves the conjunction to the assumptions. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   338
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   339
%P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R\ \isasymLongrightarrow\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   340
%\isasymand\ Q\ \isasymlongrightarrow\ R\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   341
\ 1.\ \isasymlbrakk P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P\ \isasymand\ Q\isasymrbrakk\ \isasymLongrightarrow\ R
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   342
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   343
Next, we apply conjunction elimination (\isa{erule conjE}), which splits this
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   344
conjunction into two  parts. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   345
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   346
\ 1.\ \isasymlbrakk P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P;\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   347
Q\isasymrbrakk\ \isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   348
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   349
Now, we work on the assumption \isa{P\ \isasymlongrightarrow\ (Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   350
\isasymlongrightarrow\ R)}, where the parentheses have been inserted for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   351
clarity.  The nested implication requires two applications of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   352
\textit{modus ponens}: \isa{drule mp}.  The first use  yields the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   353
implication \isa{Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   354
\isasymlongrightarrow\ R}, but first we must prove the extra subgoal 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   355
\isa{P}, which we do by assumption. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   356
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   357
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   358
\ 2.\ \isasymlbrakk P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\ \isasymLongrightarrow\ R
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   359
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   360
Repeating these steps for \isa{Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   361
\isasymlongrightarrow\ R} yields the conclusion we seek, namely~\isa{R}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   362
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   363
\ 1.\ \isasymlbrakk P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   364
\isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   365
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   366
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   367
The symbols \isa{\isasymLongrightarrow} and \isa{\isasymlongrightarrow}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   368
both stand for implication, but they differ in many respects.  Isabelle
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   369
uses \isa{\isasymLongrightarrow} to express inference rules; the symbol is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   370
built-in and Isabelle's inference mechanisms treat it specially.  On the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   371
other hand, \isa{\isasymlongrightarrow} is just one of the many connectives
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   372
available in higher-order logic.  We reason about it using inference rules
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   373
such as \isa{impI} and \isa{mp}, just as we reason about the other
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   374
connectives.  You will have to use \isa{\isasymlongrightarrow} in any
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   375
context that requires a formula of higher-order logic.  Use
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   376
\isa{\isasymLongrightarrow} to separate a theorem's preconditions from its
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   377
conclusion.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   378
\index{implication|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   379
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   380
\medskip
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   381
\index{by@\isacommand{by} (command)|(}%
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   382
The \isacommand{by} command is useful for proofs like these that use
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   383
\isa{assumption} heavily.  It executes an
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   384
\isacommand{apply} command, then tries to prove all remaining subgoals using
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   385
\isa{assumption}.  Since (if successful) it ends the proof, it also replaces the 
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   386
\isacommand{done} symbol.  For example, the proof above can be shortened:
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   387
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   388
\isacommand{lemma}\ imp_uncurry:\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   389
"P\ \isasymlongrightarrow\ (Q\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   390
\isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   391
\isasymand\ Q\ \isasymlongrightarrow\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   392
R"\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   393
\isacommand{apply}\ (rule\ impI)\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   394
\isacommand{apply}\ (erule\ conjE)\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   395
\isacommand{apply}\ (drule\ mp)\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   396
\ \isacommand{apply}\ assumption\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   397
\isacommand{by}\ (drule\ mp)
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   398
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   399
We could use \isacommand{by} to replace the final \isacommand{apply} and
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   400
\isacommand{done} in any proof, but typically we use it
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   401
to eliminate calls to \isa{assumption}.  It is also a nice way of expressing a
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   402
one-line proof.%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   403
\index{by@\isacommand{by} (command)|)}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   404
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   405
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   406
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   407
\section{Negation}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   408
 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   409
\index{negation|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   410
Negation causes surprising complexity in proofs.  Its natural 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   411
deduction rules are straightforward, but additional rules seem 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   412
necessary in order to handle negated assumptions gracefully.  This section
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   413
also illustrates the \isa{intro} method: a convenient way of
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   414
applying introduction rules.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   415
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   416
Negation introduction deduces $\lnot P$ if assuming $P$ leads to a 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   417
contradiction. Negation elimination deduces any formula in the 
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   418
presence of $\lnot P$ together with~$P$: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   419
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   420
(?P\ \isasymLongrightarrow\ False)\ \isasymLongrightarrow\ \isasymnot\ ?P%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   421
\rulenamedx{notI}\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   422
\isasymlbrakk{\isasymnot}\ ?P;\ ?P\isasymrbrakk\ \isasymLongrightarrow\ ?R%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   423
\rulenamedx{notE}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   424
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   425
%
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   426
Classical logic allows us to assume $\lnot P$ 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   427
when attempting to prove~$P$: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   428
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   429
(\isasymnot\ ?P\ \isasymLongrightarrow\ ?P)\ \isasymLongrightarrow\ ?P%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   430
\rulenamedx{classical}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   431
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   432
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   433
\index{contrapositives|(}%
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   434
The implications $P\imp Q$ and $\lnot Q\imp\lnot P$ are logically
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   435
equivalent, and each is called the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   436
\textbf{contrapositive} of the other.  Four further rules support
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   437
reasoning about contrapositives.  They differ in the placement of the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   438
negation symbols: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   439
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   440
\isasymlbrakk?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ \isasymnot\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   441
\rulename{contrapos_pp}\isanewline
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   442
\isasymlbrakk?Q;\ ?P\ \isasymLongrightarrow\ \isasymnot\ ?Q\isasymrbrakk\ \isasymLongrightarrow\
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   443
\isasymnot\ ?P%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   444
\rulename{contrapos_pn}\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   445
\isasymlbrakk{\isasymnot}\ ?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   446
\rulename{contrapos_np}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   447
\isasymlbrakk{\isasymnot}\ ?Q;\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ \isasymnot\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   448
\rulename{contrapos_nn}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   449
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   450
%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   451
These rules are typically applied using the \isa{erule} method, where 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   452
their effect is to form a contrapositive from an 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   453
assumption and the goal's conclusion.%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   454
\index{contrapositives|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   455
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   456
The most important of these is \isa{contrapos_np}.  It is useful
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   457
for applying introduction rules to negated assumptions.  For instance, 
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   458
the assumption $\lnot(P\imp Q)$ is equivalent to the conclusion $P\imp Q$ and we 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   459
might want to use conjunction introduction on it. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   460
Before we can do so, we must move that assumption so that it 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   461
becomes the conclusion. The following proof demonstrates this 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   462
technique: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   463
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   464
\isacommand{lemma}\ "\isasymlbrakk{\isasymnot}(P{\isasymlongrightarrow}Q);\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   465
\isasymnot(R{\isasymlongrightarrow}Q)\isasymrbrakk\ \isasymLongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   466
R"\isanewline
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   467
\isacommand{apply}\ (erule_tac\ Q = "R{\isasymlongrightarrow}Q"\ \isakeyword{in}\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   468
contrapos_np)\isanewline
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   469
\isacommand{apply}\ (intro\ impI)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   470
\isacommand{by}\ (erule\ notE)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   471
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   472
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   473
There are two negated assumptions and we need to exchange the conclusion with the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   474
second one.  The method \isa{erule contrapos_np} would select the first assumption,
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   475
which we do not want.  So we specify the desired assumption explicitly
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   476
using a new method, \isa{erule_tac}.  This is the resulting subgoal: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   477
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   478
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   479
R\isasymrbrakk\ \isasymLongrightarrow\ R\ \isasymlongrightarrow\ Q%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   480
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   481
The former conclusion, namely \isa{R}, now appears negated among the assumptions,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   482
while the negated formula \isa{R\ \isasymlongrightarrow\ Q} becomes the new
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   483
conclusion.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   484
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   485
We can now apply introduction rules.  We use the \methdx{intro} method, which
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   486
repeatedly applies the given introduction rules.  Here its effect is equivalent
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   487
to \isa{rule impI}.
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   488
\begin{isabelle}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   489
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\ R;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   490
R\isasymrbrakk\ \isasymLongrightarrow\ Q%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   491
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   492
We can see a contradiction in the form of assumptions \isa{\isasymnot\ R}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   493
and~\isa{R}, which suggests using negation elimination.  If applied on its own,
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   494
\isa{notE} will select the first negated assumption, which is useless.  
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   495
Instead, we invoke the rule using the
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   496
\isa{by} command.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   497
Now when Isabelle selects the first assumption, it tries to prove \isa{P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   498
\isasymlongrightarrow\ Q} and fails; it then backtracks, finds the 
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   499
assumption \isa{\isasymnot~R} and finally proves \isa{R} by assumption.  That
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   500
concludes the proof.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   501
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   502
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   503
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   504
The following example may be skipped on a first reading.  It involves a
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   505
peculiar but important rule, a form of disjunction introduction:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   506
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   507
(\isasymnot \ ?Q\ \isasymLongrightarrow \ ?P)\ \isasymLongrightarrow \ ?P\ \isasymor \ ?Q%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   508
\rulenamedx{disjCI}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   509
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   510
This rule combines the effects of \isa{disjI1} and \isa{disjI2}.  Its great
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   511
advantage is that we can remove the disjunction symbol without deciding
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   512
which disjunction to prove.  This treatment of disjunction is standard in sequent
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   513
and tableau calculi.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   514
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   515
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   516
\isacommand{lemma}\ "(P\ \isasymor\ Q)\ \isasymand\ R\
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   517
\isasymLongrightarrow\ P\ \isasymor\ (Q\ \isasymand\ R)"\isanewline
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   518
\isacommand{apply}\ (intro\ disjCI\ conjI)\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   519
\isacommand{apply}\ (elim\ conjE\ disjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   520
\ \isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   521
\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   522
\isacommand{by}\ (erule\ contrapos_np,\ rule\ conjI)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   523
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   524
%
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   525
The first proof step uses \isa{intro} to apply
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   526
the introduction rules \isa{disjCI} and \isa{conjI}.  The resulting
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
   527
subgoal has the negative assumption 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   528
\hbox{\isa{\isasymnot(Q\ \isasymand\ R)}}.  
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   529
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   530
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   531
\ 1.\ \isasymlbrakk(P\ \isasymor\ Q)\ \isasymand\ R;\ \isasymnot\ (Q\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   532
R)\isasymrbrakk\ \isasymLongrightarrow\ P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   533
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   534
Next we apply the \isa{elim} method, which repeatedly applies 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   535
elimination rules; here, the elimination rules given 
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   536
in the command.  One of the subgoals is trivial (\isa{\isacommand{apply} assumption}),
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   537
leaving us with one other:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   538
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   539
\ 1.\ \isasymlbrakk{\isasymnot}\ (Q\ \isasymand\ R);\ R;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   540
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   541
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   542
Now we must move the formula \isa{Q\ \isasymand\ R} to be the conclusion.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   543
combination 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   544
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   545
\ \ \ \ \ (erule\ contrapos_np,\ rule\ conjI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   546
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   547
is robust: the \isa{conjI} forces the \isa{erule} to select a
10301
paulson
parents: 10295
diff changeset
   548
conjunction.  The two subgoals are the ones we would expect from applying
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   549
conjunction introduction to
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
   550
\isa{Q~\isasymand~R}:  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   551
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   552
\ 1.\ \isasymlbrakk R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   553
Q\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   554
\ 2.\ \isasymlbrakk R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\ R%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   555
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   556
They are proved by assumption, which is implicit in the \isacommand{by}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   557
command.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   558
\index{negation|)}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   559
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   560
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   561
\section{Interlude: the Basic Methods for Rules}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   562
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   563
We have seen examples of many tactics that operate on individual rules.  It
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   564
may be helpful to review how they work given an arbitrary rule such as this:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   565
\[ \infer{Q}{P@1 & \ldots & P@n} \]
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   566
Below, we refer to $P@1$ as the \bfindex{major premise}.  This concept
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   567
applies only to elimination and destruction rules.  These rules act upon an
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   568
instance of their major premise, typically to replace it by subformulas of itself.
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   569
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   570
Suppose that the rule above is called~\isa{R}\@.  Here are the basic rule
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   571
methods, most of which we have already seen:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   572
\begin{itemize}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   573
\item 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   574
Method \isa{rule\ R} unifies~$Q$ with the current subgoal, replacing it
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   575
by $n$ new subgoals: instances of $P@1$, \ldots,~$P@n$. 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   576
This is backward reasoning and is appropriate for introduction rules.
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   577
\item 
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   578
Method \isa{erule\ R} unifies~$Q$ with the current subgoal and
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   579
simultaneously unifies $P@1$ with some assumption.  The subgoal is 
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   580
replaced by the $n-1$ new subgoals of proving
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   581
instances of $P@2$,
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   582
\ldots,~$P@n$, with the matching assumption deleted.  It is appropriate for
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   583
elimination rules.  The method
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   584
\isa{(rule\ R,\ assumption)} is similar, but it does not delete an
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   585
assumption.
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   586
\item 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   587
Method \isa{drule\ R} unifies $P@1$ with some assumption, which it
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   588
then deletes.  The subgoal is 
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   589
replaced by the $n-1$ new subgoals of proving $P@2$, \ldots,~$P@n$; an
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   590
$n$th subgoal is like the original one but has an additional assumption: an
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   591
instance of~$Q$.  It is appropriate for destruction rules. 
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   592
\item 
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   593
Method \isa{frule\ R} is like \isa{drule\ R} except that the matching
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
   594
assumption is not deleted.  (See {\S}\ref{sec:frule} below.)
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   595
\end{itemize}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   596
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   597
Other methods apply a rule while constraining some of its
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   598
variables.  The typical form is
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   599
\begin{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   600
\ \ \ \ \ \methdx{rule_tac}\ $v@1$ = $t@1$ \isakeyword{and} \ldots \isakeyword{and}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   601
$v@k$ =
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   602
$t@k$ \isakeyword{in} R
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   603
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   604
This method behaves like \isa{rule R}, while instantiating the variables
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   605
$v@1$, \ldots,
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   606
$v@k$ as specified.  We similarly have \methdx{erule_tac}, \methdx{drule_tac} and
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   607
\methdx{frule_tac}.  These methods also let us specify which subgoal to
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   608
operate on.  By default it is the first subgoal, as with nearly all
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   609
methods, but we can specify that rule \isa{R} should be applied to subgoal
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   610
number~$i$:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   611
\begin{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   612
\ \ \ \ \ rule_tac\ [$i$] R
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   613
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   614
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   615
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   616
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   617
\section{Unification and Substitution}\label{sec:unification}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   618
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   619
\index{unification|(}%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   620
As we have seen, Isabelle rules involve schematic variables, which begin with
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   621
a question mark and act as
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   622
placeholders for terms.  \textbf{Unification} --- well known to Prolog programmers --- is the act of
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   623
making two terms identical, possibly replacing their schematic variables by
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   624
terms.  The simplest case is when the two terms are already the same. Next
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   625
simplest is \textbf{pattern-matching}, which replaces variables in only one of the
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   626
terms.  The
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   627
\isa{rule} method typically  matches the rule's conclusion
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   628
against the current subgoal.  The
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   629
\isa{assumption} method matches the current subgoal's conclusion
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   630
against each of its assumptions.   Unification can instantiate variables in both terms; the \isa{rule} method can do this if the goal
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   631
itself contains schematic variables.  Other occurrences of the variables in
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   632
the rule or proof state are updated at the same time.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   633
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   634
Schematic variables in goals represent unknown terms.  Given a goal such
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   635
as $\exists x.\,P$, they let us proceed with a proof.  They can be 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   636
filled in later, sometimes in stages and often automatically. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   637
16359
nipkow
parents: 15952
diff changeset
   638
\begin{pgnote}
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16412
diff changeset
   639
If unification fails when you think it should succeed, try setting the Proof General flag \pgmenu{Isabelle} $>$ \pgmenu{Settings} $>$
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16412
diff changeset
   640
\pgmenu{Trace Unification},
16359
nipkow
parents: 15952
diff changeset
   641
which makes Isabelle show the cause of unification failures (in Proof
16523
f8a734dc0fbc *** empty log message ***
nipkow
parents: 16412
diff changeset
   642
General's \pgmenu{Trace} buffer).
16359
nipkow
parents: 15952
diff changeset
   643
\end{pgnote}
16412
50eab0183aea *** empty log message ***
nipkow
parents: 16410
diff changeset
   644
\noindent
16359
nipkow
parents: 15952
diff changeset
   645
For example, suppose we are trying to prove this subgoal by assumption:
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   646
\begin{isabelle}
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   647
\ 1.\ P\ (a,\ f\ (b,\ g\ (e,\ a),\ b),\ a)\ \isasymLongrightarrow \ P\ (a,\ f\ (b,\ g\ (c,\ a),\ b),\ a)
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   648
\end{isabelle}
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   649
The \isa{assumption} method having failed, we try again with the flag set:
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   650
\begin{isabelle}
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   651
\isacommand{apply} assumption
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   652
\end{isabelle}
16412
50eab0183aea *** empty log message ***
nipkow
parents: 16410
diff changeset
   653
In this trivial case, the output clearly shows that \isa{e} clashes with \isa{c}:
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   654
\begin{isabelle}
16412
50eab0183aea *** empty log message ***
nipkow
parents: 16410
diff changeset
   655
Clash: e =/= c
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   656
\end{isabelle}
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   657
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   658
Isabelle uses
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   659
\textbf{higher-order} unification, which works in the
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   660
typed $\lambda$-calculus.  The procedure requires search and is potentially
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   661
undecidable.  For our purposes, however, the differences from ordinary
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   662
unification are straightforward.  It handles bound variables
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   663
correctly, avoiding capture.  The two terms
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   664
\isa{{\isasymlambda}x.\ f(x,z)} and \isa{{\isasymlambda}y.\ f(y,z)} are
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   665
trivially unifiable because they differ only by a bound variable renaming.  The two terms \isa{{\isasymlambda}x.\ ?P} and
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   666
\isa{{\isasymlambda}x.\ t x}  are not unifiable; replacing \isa{?P} by
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   667
\isa{t x} is forbidden because the free occurrence of~\isa{x} would become
13751
ac6a9c2f9fb2 trace_unify_fail
paulson
parents: 13439
diff changeset
   668
bound.  Unfortunately, even if \isa{trace_unify_fail} is set, Isabelle displays no information about this type of failure.
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   669
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   670
\begin{warn}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   671
Higher-order unification sometimes must invent
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   672
$\lambda$-terms to replace function  variables,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   673
which can lead to a combinatorial explosion. However,  Isabelle proofs tend
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   674
to involve easy cases where there are few possibilities for the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   675
$\lambda$-term being constructed. In the easiest case, the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   676
function variable is applied only to bound variables, 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   677
as when we try to unify \isa{{\isasymlambda}x\ y.\ f(?h x y)} and
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   678
\isa{{\isasymlambda}x\ y.\ f(x+y+a)}.  The only solution is to replace
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   679
\isa{?h} by \isa{{\isasymlambda}x\ y.\ x+y+a}.  Such cases admit at most
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   680
one unifier, like ordinary unification.  A harder case is
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   681
unifying \isa{?h a} with~\isa{a+b}; it admits two solutions for \isa{?h},
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   682
namely \isa{{\isasymlambda}x.~a+b} and \isa{{\isasymlambda}x.~x+b}. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   683
Unifying \isa{?h a} with~\isa{a+a+b} admits four solutions; their number is
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   684
exponential in the number of occurrences of~\isa{a} in the second term.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   685
\end{warn}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   686
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   687
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   688
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   689
\subsection{Substitution and the {\tt\slshape subst} Method}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   690
\label{sec:subst}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   691
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   692
\index{substitution|(}%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   693
Isabelle also uses function variables to express \textbf{substitution}. 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   694
A typical substitution rule allows us to replace one term by 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   695
another if we know that two terms are equal. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   696
\[ \infer{P[t/x]}{s=t & P[s/x]} \]
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   697
The rule uses a notation for substitution: $P[t/x]$ is the result of
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   698
replacing $x$ by~$t$ in~$P$.  The rule only substitutes in the positions
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   699
designated by~$x$.  For example, it can
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   700
derive symmetry of equality from reflexivity.  Using $x=s$ for~$P$
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   701
replaces just the first $s$ in $s=s$ by~$t$:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   702
\[ \infer{t=s}{s=t & \infer{s=s}{}} \]
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   703
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   704
The Isabelle version of the substitution rule looks like this: 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   705
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   706
\isasymlbrakk?t\ =\ ?s;\ ?P\ ?s\isasymrbrakk\ \isasymLongrightarrow\ ?P\
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   707
?t
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   708
\rulenamedx{ssubst}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   709
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   710
Crucially, \isa{?P} is a function 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   711
variable.  It can be replaced by a $\lambda$-term 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   712
with one bound variable, whose occurrences identify the places 
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   713
in which $s$ will be replaced by~$t$.  The proof above requires \isa{?P}
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   714
to be replaced by \isa{{\isasymlambda}x.~x=s}; the second premise will then
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   715
be \isa{s=s} and the conclusion will be \isa{t=s}.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   716
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   717
The \isa{simp} method also replaces equals by equals, but the substitution
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   718
rule gives us more control.  Consider this proof: 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   719
\begin{isabelle}
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   720
\isacommand{lemma}\
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   721
"\isasymlbrakk x\ =\ f\ x;\ odd(f\ x)\isasymrbrakk\ \isasymLongrightarrow\
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   722
odd\ x"\isanewline
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   723
\isacommand{by}\ (erule\ ssubst)
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   724
\end{isabelle}
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   725
%
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   726
The assumption \isa{x\ =\ f\ x}, if used for rewriting, would loop, 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   727
replacing \isa{x} by \isa{f x} and then by
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   728
\isa{f(f x)} and so forth. (Here \isa{simp} 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   729
would see the danger and would re-orient the equality, but in more complicated
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   730
cases it can be fooled.) When we apply the substitution rule,  
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   731
Isabelle replaces every
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   732
\isa{x} in the subgoal by \isa{f x} just once. It cannot loop.  The
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   733
resulting subgoal is trivial by assumption, so the \isacommand{by} command
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   734
proves it implicitly. 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   735
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   736
We are using the \isa{erule} method in a novel way. Hitherto, 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   737
the conclusion of the rule was just a variable such as~\isa{?R}, but it may
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   738
be any term. The conclusion is unified with the subgoal just as 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   739
it would be with the \isa{rule} method. At the same time \isa{erule} looks 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   740
for an assumption that matches the rule's first premise, as usual.  With
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   741
\isa{ssubst} the effect is to find, use and delete an equality 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   742
assumption.
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   743
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   744
The \methdx{subst} method performs individual substitutions. In simple cases,
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   745
it closely resembles a use of the substitution rule.  Suppose a
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   746
proof has reached this point:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   747
\begin{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   748
\ 1.\ \isasymlbrakk P\ x\ y\ z;\ Suc\ x\ <\ y\isasymrbrakk \ \isasymLongrightarrow \ f\ z\ =\ x\ *\ y%
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   749
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   750
Now we wish to apply a commutative law:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   751
\begin{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   752
?m\ *\ ?n\ =\ ?n\ *\ ?m%
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   753
\rulename{mult_commute}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   754
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   755
Isabelle rejects our first attempt:
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   756
\begin{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   757
apply (simp add: mult_commute)
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   758
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   759
The simplifier notices the danger of looping and refuses to apply the
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   760
rule.%
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   761
\footnote{More precisely, it only applies such a rule if the new term is
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   762
smaller under a specified ordering; here, \isa{x\ *\ y}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   763
is already smaller than
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   764
\isa{y\ *\ x}.}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   765
%
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   766
The \isa{subst} method applies \isa{mult_commute} exactly once.  
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   767
\begin{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   768
\isacommand{apply}\ (subst\ mult_commute)\isanewline
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   769
\ 1.\ \isasymlbrakk P\ x\ y\ z;\ Suc\ x\ <\ y\isasymrbrakk \
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   770
\isasymLongrightarrow \ f\ z\ =\ y\ *\ x%
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   771
\end{isabelle}
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   772
As we wanted, \isa{x\ *\ y} has become \isa{y\ *\ x}.
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   773
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   774
\medskip
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   775
This use of the \methdx{subst} method has the same effect as the command
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   776
\begin{isabelle}
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   777
\isacommand{apply}\ (rule\ mult_commute [THEN ssubst])
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   778
\end{isabelle}
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   779
The attribute \isa{THEN}, which combines two rules, is described in 
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   780
{\S}\ref{sec:THEN} below. The \methdx{subst} method is more powerful than
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   781
applying the substitution rule. It can perform substitutions in a subgoal's
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   782
assumptions. Moreover, if the subgoal contains more than one occurrence of
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
   783
the left-hand side of the equality, the \methdx{subst} method lets us specify which occurrence should be replaced.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   784
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   785
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   786
\subsection{Unification and Its Pitfalls}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   787
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   788
Higher-order unification can be tricky.  Here is an example, which you may
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   789
want to skip on your first reading:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   790
\begin{isabelle}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   791
\isacommand{lemma}\ "\isasymlbrakk x\ =\
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   792
f\ x;\ triple\ (f\ x)\ (f\ x)\ x\isasymrbrakk\
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   793
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   794
\isacommand{apply}\ (erule\ ssubst)\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   795
\isacommand{back}\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   796
\isacommand{back}\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   797
\isacommand{back}\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   798
\isacommand{back}\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   799
\isacommand{apply}\ assumption\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   800
\isacommand{done}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   801
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   802
%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   803
By default, Isabelle tries to substitute for all the 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   804
occurrences.  Applying \isa{erule\ ssubst} yields this subgoal:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   805
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   806
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ (f\ x)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   807
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   808
The substitution should have been done in the first two occurrences 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   809
of~\isa{x} only. Isabelle has gone too far. The \commdx{back}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   810
command allows us to reject this possibility and demand a new one: 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   811
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   812
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ (f\ x)\ (f\ x)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   813
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   814
%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   815
Now Isabelle has left the first occurrence of~\isa{x} alone. That is 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   816
promising but it is not the desired combination. So we use \isacommand{back} 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   817
again:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   818
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   819
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ x\ (f\ x)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   820
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   821
%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   822
This also is wrong, so we use \isacommand{back} again: 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   823
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   824
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ x\ (f\ x)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   825
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   826
%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   827
And this one is wrong too. Looking carefully at the series 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   828
of alternatives, we see a binary countdown with reversed bits: 111,
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   829
011, 101, 001.  Invoke \isacommand{back} again: 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   830
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   831
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ x%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   832
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   833
At last, we have the right combination!  This goal follows by assumption.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   834
\index{unification|)}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   835
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   836
\medskip
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   837
This example shows that unification can do strange things with
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   838
function variables.  We were forced to select the right unifier using the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   839
\isacommand{back} command.  That is all right during exploration, but \isacommand{back}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   840
should never appear in the final version of a proof.  You can eliminate the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   841
need for \isacommand{back} by giving Isabelle less freedom when you apply a rule.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   842
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   843
One way to constrain the inference is by joining two methods in a 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   844
\isacommand{apply} command. Isabelle  applies the first method and then the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   845
second. If the second method  fails then Isabelle automatically backtracks.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   846
This process continues until  the first method produces an output that the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   847
second method can  use. We get a one-line proof of our example: 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   848
\begin{isabelle}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   849
\isacommand{lemma}\ "\isasymlbrakk x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\isasymrbrakk\
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   850
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   851
\isacommand{apply}\ (erule\ ssubst,\ assumption)\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   852
\isacommand{done}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   853
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   854
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   855
\noindent
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   856
The \isacommand{by} command works too, since it backtracks when
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   857
proving subgoals by assumption:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   858
\begin{isabelle}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   859
\isacommand{lemma}\ "\isasymlbrakk x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\isasymrbrakk\
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   860
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   861
\isacommand{by}\ (erule\ ssubst)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   862
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   863
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   864
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   865
The most general way to constrain unification is 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   866
by instantiating variables in the rule.  The method \isa{rule_tac} is
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   867
similar to \isa{rule}, but it
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   868
makes some of the rule's variables  denote specified terms.  
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   869
Also available are {\isa{drule_tac}}  and \isa{erule_tac}.  Here we need
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   870
\isa{erule_tac} since above we used \isa{erule}.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   871
\begin{isabelle}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   872
\isacommand{lemma}\ "\isasymlbrakk x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\isasymrbrakk\ \isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
   873
\isacommand{by}\ (erule_tac\ P = "\isasymlambda u.\ triple\ u\ u\ x"\ 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   874
\isakeyword{in}\ ssubst)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   875
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   876
%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   877
To specify a desired substitution 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   878
requires instantiating the variable \isa{?P} with a $\lambda$-expression. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   879
The bound variable occurrences in \isa{{\isasymlambda}u.\ P\ u\
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   880
u\ x} indicate that the first two arguments have to be substituted, leaving
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   881
the third unchanged.  With this instantiation, backtracking is neither necessary
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   882
nor possible.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   883
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   884
An alternative to \isa{rule_tac} is to use \isa{rule} with a theorem
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   885
modified using~\isa{of}, described in
12540
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
   886
{\S}\ref{sec:forward} below.   But \isa{rule_tac}, unlike \isa{of}, can 
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
   887
express instantiations that refer to 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   888
\isasymAnd-bound variables in the current subgoal.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   889
\index{substitution|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   890
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   891
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   892
\section{Quantifiers}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   893
11411
c315dda16748 indexing
paulson
parents: 11406
diff changeset
   894
\index{quantifiers!universal|(}%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   895
Quantifiers require formalizing syntactic substitution and the notion of 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   896
arbitrary value.  Consider the universal quantifier.  In a logic
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   897
book, its introduction  rule looks like this: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   898
\[ \infer{\forall x.\,P}{P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   899
Typically, a proviso written in English says that $x$ must not
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   900
occur in the assumptions.  This proviso guarantees that $x$ can be regarded as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   901
arbitrary, since it has not been assumed to satisfy any special conditions. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   902
Isabelle's  underlying formalism, called the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   903
\bfindex{meta-logic}, eliminates the  need for English.  It provides its own
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   904
universal quantifier (\isasymAnd) to express the notion of an arbitrary value.  We
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   905
have already seen  another symbol of the meta-logic, namely
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   906
\isa\isasymLongrightarrow, which expresses  inference rules and the treatment of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   907
assumptions. The only other  symbol in the meta-logic is \isa\isasymequiv, which
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   908
can be used to define constants.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   909
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   910
\subsection{The Universal Introduction Rule}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   911
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   912
Returning to the universal quantifier, we find that having a similar quantifier
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   913
as part of the meta-logic makes the introduction rule trivial to express:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   914
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   915
(\isasymAnd x.\ ?P\ x)\ \isasymLongrightarrow\ {\isasymforall}x.\ ?P\ x\rulenamedx{allI}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   916
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   917
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   918
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   919
The following trivial proof demonstrates how the universal introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   920
rule works. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   921
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   922
\isacommand{lemma}\ "{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   923
\isacommand{apply}\ (rule\ allI)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   924
\isacommand{by}\ (rule\ impI)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   925
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   926
The first step invokes the rule by applying the method \isa{rule allI}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   927
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   928
\ 1.\ \isasymAnd x.\ P\ x\ \isasymlongrightarrow\ P\ x
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   929
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   930
Note  that the resulting proof state has a bound variable,
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   931
namely~\isa{x}.  The rule has replaced the universal quantifier of
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   932
higher-order  logic by Isabelle's meta-level quantifier.  Our goal is to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   933
prove
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   934
\isa{P\ x\ \isasymlongrightarrow\ P\ x} for arbitrary~\isa{x}; it is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   935
an implication, so we apply the corresponding introduction rule (\isa{impI}). 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   936
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
   937
\ 1.\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow\ P\ x
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   938
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   939
This last subgoal is implicitly proved by assumption. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   940
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   941
\subsection{The Universal Elimination Rule}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   942
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   943
Now consider universal elimination. In a logic text, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   944
the rule looks like this: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   945
\[ \infer{P[t/x]}{\forall x.\,P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   946
The conclusion is $P$ with $t$ substituted for the variable~$x$.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   947
Isabelle expresses substitution using a function variable: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   948
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   949
{\isasymforall}x.\ ?P\ x\ \isasymLongrightarrow\ ?P\ ?x\rulenamedx{spec}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   950
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   951
This destruction rule takes a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   952
universally quantified formula and removes the quantifier, replacing 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   953
the bound variable \isa{x} by the schematic variable \isa{?x}.  Recall that a
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   954
schematic variable starts with a question mark and acts as a
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   955
placeholder: it can be replaced by any term.  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   956
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   957
The universal elimination rule is also
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   958
available in the standard elimination format.  Like \isa{conjE}, it never
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   959
appears in logic books:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   960
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   961
\isasymlbrakk \isasymforall x.\ ?P\ x;\ ?P\ ?x\ \isasymLongrightarrow \ ?R\isasymrbrakk \ \isasymLongrightarrow \ ?R%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
   962
\rulenamedx{allE}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   963
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   964
The methods \isa{drule~spec} and \isa{erule~allE} do precisely the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   965
same inference.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   966
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   967
To see how $\forall$-elimination works, let us derive a rule about reducing 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   968
the scope of a universal quantifier.  In mathematical notation we write
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   969
\[ \infer{P\imp\forall x.\,Q}{\forall x.\,P\imp Q} \]
10978
5eebea8f359f *** empty log message ***
nipkow
parents: 10971
diff changeset
   970
with the proviso ``$x$ not free in~$P$.''  Isabelle's treatment of
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   971
substitution makes the proviso unnecessary.  The conclusion is expressed as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   972
\isa{P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   973
\isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)}. No substitution for the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   974
variable \isa{P} can introduce a dependence upon~\isa{x}: that would be a
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   975
bound variable capture.  Let us walk through the proof.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   976
\begin{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   977
\isacommand{lemma}\ "(\isasymforall x.\ P\ \isasymlongrightarrow \ Q\ x)\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   978
\isasymLongrightarrow \ P\ \isasymlongrightarrow \ (\isasymforall x.\ Q\
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   979
x)"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   980
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   981
First we apply implies introduction (\isa{impI}), 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   982
which moves the \isa{P} from the conclusion to the assumptions. Then 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   983
we apply universal introduction (\isa{allI}).  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   984
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   985
\isacommand{apply}\ (rule\ impI,\ rule\ allI)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   986
\ 1.\ \isasymAnd x.\ \isasymlbrakk{\isasymforall}x.\ P\ \isasymlongrightarrow\ Q\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
   987
x;\ P\isasymrbrakk\ \isasymLongrightarrow\ Q\ x
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   988
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   989
As before, it replaces the HOL 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   990
quantifier by a meta-level quantifier, producing a subgoal that 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   991
binds the variable~\isa{x}.  The leading bound variables
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   992
(here \isa{x}) and the assumptions (here \isa{{\isasymforall}x.\ P\
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
   993
\isasymlongrightarrow\ Q\ x} and \isa{P}) form the \textbf{context} for the
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   994
conclusion, here \isa{Q\ x}.  Subgoals inherit the context,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   995
although assumptions can be added or deleted (as we saw
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
   996
earlier), while rules such as \isa{allI} add bound variables.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   997
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   998
Now, to reason from the universally quantified 
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
   999
assumption, we apply the elimination rule using the \isa{drule} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1000
method.  This rule is called \isa{spec} because it specializes a universal formula
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1001
to a particular term.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1002
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1003
\isacommand{apply}\ (drule\ spec)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1004
\ 1.\ \isasymAnd x.\ \isasymlbrakk P;\ P\ \isasymlongrightarrow\ Q\ (?x2\
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1005
x)\isasymrbrakk\ \isasymLongrightarrow\ Q\ x
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1006
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1007
Observe how the context has changed.  The quantified formula is gone,
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1008
replaced by a new assumption derived from its body.  We have
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1009
removed the quantifier and replaced the bound variable
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1010
by the curious term 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1011
\isa{?x2~x}.  This term is a placeholder: it may become any term that can be
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1012
built from~\isa{x}.  (Formally, \isa{?x2} is an unknown of function type, applied
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1013
to the argument~\isa{x}.)  This new assumption is an implication, so we can  use
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1014
\emph{modus ponens} on it, which concludes the proof. 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1015
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1016
\isacommand{by}\ (drule\ mp)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1017
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1018
Let us take a closer look at this last step.  \emph{Modus ponens} yields
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1019
two subgoals: one where we prove the antecedent (in this case \isa{P}) and
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1020
one where we may assume the consequent.  Both of these subgoals are proved
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1021
by the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1022
\isa{assumption} method, which is implicit in the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1023
\isacommand{by} command.  Replacing the \isacommand{by} command by 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1024
\isa{\isacommand{apply} (drule\ mp, assumption)} would have left one last
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1025
subgoal:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1026
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1027
\ 1.\ \isasymAnd x.\ \isasymlbrakk P;\ Q\ (?x2\ x)\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1028
\isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1029
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1030
The consequent is \isa{Q} applied to that placeholder.  It may be replaced by any
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1031
term built from~\isa{x}, and here 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1032
it should simply be~\isa{x}.  The assumption need not
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1033
be identical to the conclusion, provided the two formulas are unifiable.%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1034
\index{quantifiers!universal|)}  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1035
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1036
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1037
\subsection{The Existential Quantifier}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1038
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1039
\index{quantifiers!existential|(}%
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1040
The concepts just presented also apply
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1041
to the existential quantifier, whose introduction rule looks like this in
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1042
Isabelle: 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1043
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1044
?P\ ?x\ \isasymLongrightarrow\ {\isasymexists}x.\ ?P\ x\rulenamedx{exI}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1045
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1046
If we can exhibit some $x$ such that $P(x)$ is true, then $\exists x.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1047
P(x)$ is also true.  It is a dual of the universal elimination rule, and
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1048
logic texts present it using the same notation for substitution.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1049
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1050
The existential
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1051
elimination rule looks like this
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1052
in a logic text: 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1053
\[ \infer{Q}{\exists x.\,P & \infer*{Q}{[P]}} \]
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1054
%
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1055
It looks like this in Isabelle: 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1056
\begin{isabelle}
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1057
\isasymlbrakk{\isasymexists}x.\ ?P\ x;\ \isasymAnd x.\ ?P\ x\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?Q\rulenamedx{exE}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1058
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1059
%
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1060
Given an existentially quantified theorem and some
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1061
formula $Q$ to prove, it creates a new assumption by removing the quantifier.  As with
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1062
the universal introduction  rule, the textbook version imposes a proviso on the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1063
quantified variable, which Isabelle expresses using its meta-logic.  It is
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1064
enough to have a universal quantifier in the meta-logic; we do not need an existential
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1065
quantifier to be built in as well.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1066
 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1067
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1068
\begin{exercise}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1069
Prove the lemma
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1070
\[ \exists x.\, P\conj Q(x)\Imp P\conj(\exists x.\, Q(x)). \]
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1071
\emph{Hint}: the proof is similar 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1072
to the one just above for the universal quantifier. 
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1073
\end{exercise}
11411
c315dda16748 indexing
paulson
parents: 11406
diff changeset
  1074
\index{quantifiers!existential|)}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1075
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1076
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1077
\subsection{Renaming an Assumption: {\tt\slshape rename_tac}}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1078
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1079
\index{assumptions!renaming|(}\index{*rename_tac (method)|(}%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1080
When you apply a rule such as \isa{allI}, the quantified variable
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1081
becomes a new bound variable of the new subgoal.  Isabelle tries to avoid
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1082
changing its name, but sometimes it has to choose a new name in order to
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1083
avoid a clash.  The result may not be ideal:
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1084
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1085
\isacommand{lemma}\ "x\ <\ y\ \isasymLongrightarrow \ \isasymforall x\ y.\ P\ x\
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1086
(f\ y)"\isanewline
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
  1087
\isacommand{apply}\ (intro allI)\isanewline
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1088
\ 1.\ \isasymAnd xa\ ya.\ x\ <\ y\ \isasymLongrightarrow \ P\ xa\ (f\ ya)
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1089
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1090
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1091
The names \isa{x} and \isa{y} were already in use, so the new bound variables are
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1092
called \isa{xa} and~\isa{ya}.  You can rename them by invoking \isa{rename_tac}:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1093
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1094
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1095
\isacommand{apply}\ (rename_tac\ v\ w)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1096
\ 1.\ \isasymAnd v\ w.\ x\ <\ y\ \isasymLongrightarrow \ P\ v\ (f\ w)
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1097
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1098
Recall that \isa{rule_tac}\index{*rule_tac (method)!and renaming} 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1099
instantiates a
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1100
theorem with specified terms.  These terms may involve the goal's bound
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1101
variables, but beware of referring to  variables
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1102
like~\isa{xa}.  A future change to your theories could change the set of names
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1103
produced at top level, so that \isa{xa} changes to~\isa{xb} or reverts to~\isa{x}.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1104
It is safer to rename automatically-generated variables before mentioning them.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1105
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1106
If the subgoal has more bound variables than there are names given to
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1107
\isa{rename_tac}, the rightmost ones are renamed.%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1108
\index{assumptions!renaming|)}\index{*rename_tac (method)|)}
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1109
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1110
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  1111
\subsection{Reusing an Assumption: {\tt\slshape frule}}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
  1112
\label{sec:frule}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1113
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1114
\index{assumptions!reusing|(}\index{*frule (method)|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1115
Note that \isa{drule spec} removes the universal quantifier and --- as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1116
usual with elimination rules --- discards the original formula.  Sometimes, a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1117
universal formula has to be kept so that it can be used again.  Then we use a new
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1118
method: \isa{frule}.  It acts like \isa{drule} but copies rather than replaces
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1119
the selected assumption.  The \isa{f} is for \emph{forward}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1120
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1121
In this example, going from \isa{P\ a} to \isa{P(h(h~a))}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1122
requires two uses of the quantified assumption, one for each~\isa{h}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1123
in~\isa{h(h~a)}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1124
\begin{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1125
\isacommand{lemma}\ "\isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x);
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1126
\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P(h\ (h\ a))"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1127
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1128
%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1129
Examine the subgoal left by \isa{frule}:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1130
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1131
\isacommand{apply}\ (frule\ spec)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1132
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x);\ P\ a;\ P\ ?x\ \isasymlongrightarrow\ P\ (h\ ?x)\isasymrbrakk\ \isasymLongrightarrow\ P\ (h\ (h\ a))
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1133
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1134
It is what \isa{drule} would have left except that the quantified
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1135
assumption is still present.  Next we apply \isa{mp} to the
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1136
implication and the assumption~\isa{P\ a}:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1137
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1138
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1139
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x);\ P\ a;\ P\ (h\ a)\isasymrbrakk\ \isasymLongrightarrow\ P\ (h\ (h\ a))
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1140
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1141
%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1142
We have created the assumption \isa{P(h\ a)}, which is progress.  To
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1143
continue the proof, we apply \isa{spec} again.  We shall not need it
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1144
again, so we can use
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1145
\isa{drule}.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1146
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1147
\isacommand{apply}\ (drule\ spec)\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1148
\ 1.\ \isasymlbrakk P\ a;\ P\ (h\ a);\ P\ ?x2\ 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1149
\isasymlongrightarrow \ P\ (h\ ?x2)\isasymrbrakk \ \isasymLongrightarrow \
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1150
P\ (h\ (h\ a))
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1151
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1152
%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1153
The new assumption bridges the gap between \isa{P(h\ a)} and \isa{P(h(h\ a))}.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1154
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1155
\isacommand{by}\ (drule\ mp)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1156
\end{isabelle}
10854
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1157
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1158
\medskip
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1159
\emph{A final remark}.  Replacing this \isacommand{by} command with
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1160
\begin{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1161
\isacommand{apply}\ (drule\ mp,\ assumption)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1162
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1163
would not work: it would add a second copy of \isa{P(h~a)} instead
10854
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1164
of the desired assumption, \isa{P(h(h~a))}.  The \isacommand{by}
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1165
command forces Isabelle to backtrack until it finds the correct one.
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1166
Alternatively, we could have used the \isacommand{apply} command and bundled the
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1167
\isa{drule mp} with \emph{two} calls of \isa{assumption}.  Or, of course,
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1168
we could have given the entire proof to \isa{auto}.%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1169
\index{assumptions!reusing|)}\index{*frule (method)|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1170
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1171
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1172
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1173
\subsection{Instantiating a Quantifier Explicitly}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1174
\index{quantifiers!instantiating}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1175
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1176
We can prove a theorem of the form $\exists x.\,P\, x$ by exhibiting a
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1177
suitable term~$t$ such that $P\,t$ is true.  Dually, we can use an
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1178
assumption of the form $\forall x.\,P\, x$ to generate a new assumption $P\,t$ for
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1179
a suitable term~$t$.  In many cases, 
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1180
Isabelle makes the correct choice automatically, constructing the term by
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1181
unification.  In other cases, the required term is not obvious and we must
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1182
specify it ourselves.  Suitable methods are \isa{rule_tac}, \isa{drule_tac}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1183
and \isa{erule_tac}.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1184
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
  1185
We have seen (just above, {\S}\ref{sec:frule}) a proof of this lemma:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1186
\begin{isabelle}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1187
\isacommand{lemma}\ "\isasymlbrakk \isasymforall x.\ P\ x\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1188
\isasymlongrightarrow \ P\ (h\ x);\ P\ a\isasymrbrakk \
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1189
\isasymLongrightarrow \ P(h\ (h\ a))"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1190
\end{isabelle}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1191
We had reached this subgoal:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1192
\begin{isabelle}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1193
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1194
x);\ P\ a;\ P\ (h\ a)\isasymrbrakk\ \isasymLongrightarrow\ P\ (h\ (h\ a))
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1195
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1196
%
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1197
The proof requires instantiating the quantified assumption with the
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1198
term~\isa{h~a}.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1199
\begin{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1200
\isacommand{apply}\ (drule_tac\ x\ =\ "h\ a"\ \isakeyword{in}\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1201
spec)\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1202
\ 1.\ \isasymlbrakk P\ a;\ P\ (h\ a);\ P\ (h\ a)\ \isasymlongrightarrow \
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1203
P\ (h\ (h\ a))\isasymrbrakk \ \isasymLongrightarrow \ P\ (h\ (h\ a))
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1204
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1205
We have forced the desired instantiation.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1206
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1207
\medskip
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1208
Existential formulas can be instantiated too.  The next example uses the 
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1209
\textbf{divides} relation\index{divides relation}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1210
of number theory: 
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1211
\begin{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1212
?m\ dvd\ ?n\ \isasymequiv\ {\isasymexists}k.\ ?n\ =\ ?m\ *\ k
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1213
\rulename{dvd_def}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1214
\end{isabelle}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1215
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1216
Let us prove that multiplication of natural numbers is monotone with
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1217
respect to the divides relation:
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1218
\begin{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1219
\isacommand{lemma}\ mult_dvd_mono:\ "{\isasymlbrakk}i\ dvd\ m;\ j\ dvd\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1220
n\isasymrbrakk\ \isasymLongrightarrow\ i*j\ dvd\ (m*n\ ::\ nat)"\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1221
\isacommand{apply}\ (simp\ add:\ dvd_def)
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1222
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1223
%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1224
Unfolding the definition of divides has left this subgoal:
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1225
\begin{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1226
\ 1.\ \isasymlbrakk \isasymexists k.\ m\ =\ i\ *\ k;\ \isasymexists k.\ n\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1227
=\ j\ *\ k\isasymrbrakk \ \isasymLongrightarrow \ \isasymexists k.\ m\ *\
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1228
n\ =\ i\ *\ j\ *\ k
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1229
\end{isabelle}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1230
%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1231
Next, we eliminate the two existential quantifiers in the assumptions:
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1232
\begin{isabelle}
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1233
\isacommand{apply}\ (erule\ exE)\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1234
\ 1.\ \isasymAnd k.\ \isasymlbrakk \isasymexists k.\ n\ =\ j\ *\ k;\ m\ =\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1235
i\ *\ k\isasymrbrakk \ \isasymLongrightarrow \ \isasymexists k.\ m\ *\ n\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1236
=\ i\ *\ j\ *\ k%
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1237
\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1238
\isacommand{apply}\ (erule\ exE)
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1239
\isanewline
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1240
\ 1.\ \isasymAnd k\ ka.\ \isasymlbrakk m\ =\ i\ *\ k;\ n\ =\ j\ *\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1241
ka\isasymrbrakk \ \isasymLongrightarrow \ \isasymexists k.\ m\ *\ n\ =\ i\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1242
*\ j\ *\ k
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1243
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1244
%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1245
The term needed to instantiate the remaining quantifier is~\isa{k*ka}.  But
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1246
\isa{ka} is an automatically-generated name.  As noted above, references to
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1247
such variable names makes a proof less resilient to future changes.  So,
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1248
first we rename the most recent variable to~\isa{l}:
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1249
\begin{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1250
\isacommand{apply}\ (rename_tac\ l)\isanewline
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1251
\ 1.\ \isasymAnd k\ l.\ \isasymlbrakk m\ =\ i\ *\ k;\ n\ =\ j\ *\ l\isasymrbrakk \
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1252
\isasymLongrightarrow \ \isasymexists k.\ m\ *\ n\ =\ i\ *\ j\ *\ k%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1253
\end{isabelle}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1254
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1255
We instantiate the quantifier with~\isa{k*l}:
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1256
\begin{isabelle}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1257
\isacommand{apply}\ (rule_tac\ x="k*l"\ \isakeyword{in}\ exI)\ \isanewline
11234
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1258
\ 1.\ \isasymAnd k\ ka.\ \isasymlbrakk m\ =\ i\ *\ k;\ n\ =\ j\ *\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1259
ka\isasymrbrakk \ \isasymLongrightarrow \ m\ *\ n\ =\ i\
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1260
*\ j\ *\ (k\ *\ ka)
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1261
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1262
%
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1263
The rest is automatic, by arithmetic.
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1264
\begin{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1265
\isacommand{apply}\ simp\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1266
\isacommand{done}\isanewline
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1267
\end{isabelle}
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1268
6902638af59e quantifier instantiation
paulson
parents: 11179
diff changeset
  1269
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1270
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1271
\section{Description Operators}
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1272
\label{sec:SOME}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1273
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1274
\index{description operators|(}%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1275
HOL provides two description operators.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1276
A \textbf{definite description} formalizes the word ``the,'' as in
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1277
``the greatest divisior of~$n$.''
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1278
It returns an arbitrary value unless the formula has a unique solution.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1279
An \textbf{indefinite description} formalizes the word ``some,'' as in
12815
wenzelm
parents: 12540
diff changeset
  1280
``some member of~$S$.''  It differs from a definite description in not
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1281
requiring the solution to be unique: it uses the axiom of choice to pick any
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1282
solution. 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1283
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1284
\begin{warn}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1285
Description operators can be hard to reason about.  Novices
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1286
should try to avoid them.  Fortunately, descriptions are seldom required.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1287
\end{warn}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1288
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1289
\subsection{Definite Descriptions}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1290
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1291
\index{descriptions!definite}%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1292
A definite description is traditionally written $\iota x.  P(x)$.  It denotes
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1293
the $x$ such that $P(x)$ is true, provided there exists a unique such~$x$;
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1294
otherwise, it returns an arbitrary value of the expected type.
12540
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  1295
Isabelle uses \sdx{THE} for the Greek letter~$\iota$.  
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  1296
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  1297
%(The traditional notation could be provided, but it is not legible on screen.)
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1298
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1299
We reason using this rule, where \isa{a} is the unique solution:
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1300
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1301
\isasymlbrakk P\ a;\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow \ x\ =\ a\isasymrbrakk \ 
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1302
\isasymLongrightarrow \ (THE\ x.\ P\ x)\ =\ a%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1303
\rulenamedx{the_equality}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1304
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1305
For instance, we can define the
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1306
cardinality of a finite set~$A$ to be that
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1307
$n$ such that $A$ is in one-to-one correspondence with $\{1,\ldots,n\}$.  We can then
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1308
prove that the cardinality of the empty set is zero (since $n=0$ satisfies the
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1309
description) and proceed to prove other facts.
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1310
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1311
A more challenging example illustrates how Isabelle/HOL defines the least number
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1312
operator, which denotes the least \isa{x} satisfying~\isa{P}:%
11428
332347b9b942 tidying the index
paulson
parents: 11417
diff changeset
  1313
\index{least number operator|see{\protect\isa{LEAST}}}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1314
\begin{isabelle}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1315
(LEAST\ x.\ P\ x)\ = (THE\ x.\ P\ x\ \isasymand \ (\isasymforall y.\
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
  1316
P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y))
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1317
\end{isabelle}
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
  1318
%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1319
Let us prove the analogue of \isa{the_equality} for \sdx{LEAST}\@.
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1320
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1321
\isacommand{theorem}\ Least_equality:\isanewline
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
  1322
\ \ \ \ \ "\isasymlbrakk P\ (k::nat);\ \ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ \isasymle \ x\isasymrbrakk \ \isasymLongrightarrow \ (LEAST\ x.\ P\ x)\ =\ k"\isanewline
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1323
\isacommand{apply}\ (simp\ add:\ Least_def)\isanewline
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1324
\isanewline
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1325
\ 1.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ \isasymle \ x\isasymrbrakk \isanewline
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1326
\isaindent{\ 1.\ }\isasymLongrightarrow \ (THE\ x.\ P\ x\ \isasymand \ (\isasymforall y.\ P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y))\ =\ k%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1327
\end{isabelle}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1328
The first step has merely unfolded the definition.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1329
\begin{isabelle}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1330
\isacommand{apply}\ (rule\ the_equality)\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1331
\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1332
\ 1.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1333
\isasymle \ x\isasymrbrakk \ \isasymLongrightarrow \ P\ k\ \isasymand \
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1334
(\isasymforall y.\ P\ y\ \isasymlongrightarrow \ k\ \isasymle \ y)\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1335
\ 2.\ \isasymAnd x.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ \isasymle \ x;\ P\ x\ \isasymand \ (\isasymforall y.\ P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y)\isasymrbrakk \isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1336
\ \ \ \ \ \ \ \ \isasymLongrightarrow \ x\ =\ k%
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1337
\end{isabelle}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1338
As always with \isa{the_equality}, we must show existence and
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1339
uniqueness of the claimed solution,~\isa{k}.  Existence, the first
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1340
subgoal, is trivial.  Uniqueness, the second subgoal, follows by antisymmetry:
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1341
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1342
\isasymlbrakk x\ \isasymle \ y;\ y\ \isasymle \ x\isasymrbrakk \ \isasymLongrightarrow \ x\ =\ y%
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1343
\rulename{order_antisym}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1344
\end{isabelle}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1345
The assumptions imply both \isa{k~\isasymle~x} and \isa{x~\isasymle~k}.  One
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1346
call to \isa{auto} does it all: 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1347
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1348
\isacommand{by}\ (auto\ intro:\ order_antisym)
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1349
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1350
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1351
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1352
\subsection{Indefinite Descriptions}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1353
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1354
\index{Hilbert's $\varepsilon$-operator}%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1355
\index{descriptions!indefinite}%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1356
An indefinite description is traditionally written $\varepsilon x. P(x)$ and is
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1357
known as Hilbert's $\varepsilon$-operator.  It denotes
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1358
some $x$ such that $P(x)$ is true, provided one exists.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1359
Isabelle uses \sdx{SOME} for the Greek letter~$\varepsilon$.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1360
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1361
Here is the definition of~\cdx{inv}, which expresses inverses of
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1362
functions:
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1363
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1364
inv\ f\ \isasymequiv \ \isasymlambda y.\ SOME\ x.\ f\ x\ =\ y%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1365
\rulename{inv_def}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1366
\end{isabelle}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1367
Using \isa{SOME} rather than \isa{THE} makes \isa{inv~f} behave well
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1368
even if \isa{f} is not injective.  As it happens, most useful theorems about
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1369
\isa{inv} do assume the function to be injective.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1370
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1371
The inverse of \isa{f}, when applied to \isa{y}, returns some~\isa{x} such that
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1372
\isa{f~x~=~y}.  For example, we can prove \isa{inv~Suc} really is the inverse
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1373
of the \isa{Suc} function 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1374
\begin{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1375
\isacommand{lemma}\ "inv\ Suc\ (Suc\ n)\ =\ n"\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1376
\isacommand{by}\ (simp\ add:\ inv_def)
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1377
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1378
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1379
\noindent
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1380
The proof is a one-liner: the subgoal simplifies to a degenerate application of
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1381
\isa{SOME}, which is then erased.  In detail, the left-hand side simplifies
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1382
to \isa{SOME\ x.\ Suc\ x\ =\ Suc\ n}, then to \isa{SOME\ x.\ x\ =\ n} and
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1383
finally to~\isa{n}.  
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1384
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1385
We know nothing about what
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1386
\isa{inv~Suc} returns when applied to zero.  The proof above still treats
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1387
\isa{SOME} as a definite description, since it only reasons about
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1388
situations in which the value is described uniquely.  Indeed, \isa{SOME}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1389
satisfies this rule:
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1390
\begin{isabelle}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1391
\isasymlbrakk P\ a;\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow \ x\ =\ a\isasymrbrakk \ 
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1392
\isasymLongrightarrow \ (SOME\ x.\ P\ x)\ =\ a%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1393
\rulenamedx{some_equality}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1394
\end{isabelle}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1395
To go further is
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1396
tricky and requires rules such as these:
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1397
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1398
P\ x\ \isasymLongrightarrow \ P\ (SOME\ x.\ P\ x)
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1399
\rulenamedx{someI}\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1400
\isasymlbrakk P\ a;\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow \ Q\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1401
x\isasymrbrakk \ \isasymLongrightarrow \ Q\ (SOME\ x.\ P\ x)
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1402
\rulenamedx{someI2}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1403
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1404
Rule \isa{someI} is basic: if anything satisfies \isa{P} then so does
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1405
\hbox{\isa{SOME\ x.\ P\ x}}.  The repetition of~\isa{P} in the conclusion makes it
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1406
difficult to apply in a backward proof, so the derived rule \isa{someI2} is
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1407
also provided. 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1408
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1409
\medskip
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1410
For example, let us prove the \rmindex{axiom of choice}:
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1411
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1412
\isacommand{theorem}\ axiom_of_choice:
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1413
\ "(\isasymforall x.\ \isasymexists y.\ P\ x\ y)\ \isasymLongrightarrow \
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1414
\isasymexists f.\ \isasymforall x.\ P\ x\ (f\ x)"\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1415
\isacommand{apply}\ (rule\ exI,\ rule\ allI)\isanewline
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1416
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1417
\ 1.\ \isasymAnd x.\ \isasymforall x.\ \isasymexists y.\ P\ x\ y\
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1418
\isasymLongrightarrow \ P\ x\ (?f\ x)
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1419
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1420
%
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1421
We have applied the introduction rules; now it is time to apply the elimination
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1422
rules.
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1423
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1424
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1425
\isacommand{apply}\ (drule\ spec,\ erule\ exE)\isanewline
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1426
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1427
\ 1.\ \isasymAnd x\ y.\ P\ (?x2\ x)\ y\ \isasymLongrightarrow \ P\ x\ (?f\ x)
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1428
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1429
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1430
\noindent
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1431
The rule \isa{someI} automatically instantiates
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1432
\isa{f} to \hbox{\isa{\isasymlambda x.\ SOME y.\ P\ x\ y}}, which is the choice
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1433
function.  It also instantiates \isa{?x2\ x} to \isa{x}.
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1434
\begin{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1435
\isacommand{by}\ (rule\ someI)\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1436
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1437
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1438
\subsubsection{Historical Note}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1439
The original purpose of Hilbert's $\varepsilon$-operator was to express an
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1440
existential destruction rule:
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1441
\[ \infer{P[(\varepsilon x. P) / \, x]}{\exists x.\,P} \]
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1442
This rule is seldom used for that purpose --- it can cause exponential
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1443
blow-up --- but it is occasionally used as an introduction rule
13791
3b6ff7ceaf27 *** empty log message ***
nipkow
parents: 13751
diff changeset
  1444
for the~$\varepsilon$-operator.  Its name in HOL is \tdxbold{someI_ex}.%%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11428
diff changeset
  1445
\index{description operators|)}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1446
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1447
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1448
\section{Some Proofs That Fail}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1449
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1450
\index{proofs!examples of failing|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1451
Most of the examples in this tutorial involve proving theorems.  But not every 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1452
conjecture is true, and it can be instructive to see how  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1453
proofs fail. Here we attempt to prove a distributive law involving 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1454
the existential quantifier and conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1455
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1456
\isacommand{lemma}\ "({\isasymexists}x.\ P\ x)\ \isasymand\ 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1457
({\isasymexists}x.\ Q\ x)\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1458
\isasymand\ Q\ x"
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1459
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1460
The first steps are  routine.  We apply conjunction elimination to break
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1461
the assumption into two existentially quantified assumptions. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1462
Applying existential elimination removes one of the quantifiers. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1463
\begin{isabelle}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1464
\isacommand{apply}\ (erule\ conjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1465
\isacommand{apply}\ (erule\ exE)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1466
\ 1.\ \isasymAnd x.\ \isasymlbrakk{\isasymexists}x.\ Q\ x;\ P\ x\isasymrbrakk\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1467
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1468
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1469
When we remove the other quantifier, we get a different bound 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1470
variable in the subgoal.  (The name \isa{xa} is generated automatically.)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1471
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1472
\isacommand{apply}\ (erule\ exE)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1473
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1474
\isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1475
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1476
The proviso of the existential elimination rule has forced the variables to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1477
differ: we can hardly expect two arbitrary values to be equal!  There is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1478
no way to prove this subgoal.  Removing the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1479
conclusion's existential quantifier yields two
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1480
identical placeholders, which can become  any term involving the variables \isa{x}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1481
and~\isa{xa}.  We need one to become \isa{x}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1482
and the other to become~\isa{xa}, but Isabelle requires all instances of a
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1483
placeholder to be identical. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1484
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1485
\isacommand{apply}\ (rule\ exI)\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1486
\isacommand{apply}\ (rule\ conjI)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1487
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1488
\isasymLongrightarrow\ P\ (?x3\ x\ xa)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1489
\ 2.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\ \isasymLongrightarrow\ Q\ (?x3\ x\ xa)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1490
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1491
We can prove either subgoal 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1492
using the \isa{assumption} method.  If we prove the first one, the placeholder
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1493
changes into~\isa{x}. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1494
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1495
\ \isacommand{apply}\ assumption\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1496
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1497
\isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1498
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1499
We are left with a subgoal that cannot be proved.  Applying the \isa{assumption}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1500
method results in an error message:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1501
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1502
*** empty result sequence -- proof command failed
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1503
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1504
When interacting with Isabelle via the shell interface,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1505
you can abandon a proof using the \isacommand{oops} command.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1506
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1507
\medskip 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1508
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1509
Here is another abortive proof, illustrating the interaction between 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1510
bound variables and unknowns.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1511
If $R$ is a reflexive relation, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1512
is there an $x$ such that $R\,x\,y$ holds for all $y$?  Let us see what happens when
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1513
we attempt to prove it. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1514
\begin{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1515
\isacommand{lemma}\ "\isasymforall y.\ R\ y\ y\ \isasymLongrightarrow 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1516
\ \isasymexists x.\ \isasymforall y.\ R\ x\ y"
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1517
\end{isabelle}
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1518
First,  we remove the existential quantifier. The new proof state has  an
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1519
unknown, namely~\isa{?x}. 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1520
\begin{isabelle}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1521
\isacommand{apply}\ (rule\ exI)\isanewline
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1522
\ 1.\ \isasymforall y.\ R\ y\ y\ \isasymLongrightarrow \ \isasymforall y.\ R\ ?x\ y%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1523
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1524
It looks like we can just apply \isa{assumption}, but it fails.  Isabelle
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1525
refuses to substitute \isa{y}, a bound variable, for~\isa{?x}; that would be
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1526
a bound variable capture.  We can still try to finish the proof in some
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1527
other way. We remove the universal quantifier  from the conclusion, moving
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1528
the bound variable~\isa{y} into the subgoal.  But note that it is still
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1529
bound!
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1530
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1531
\isacommand{apply}\ (rule\ allI)\isanewline
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1532
\ 1.\ \isasymAnd y.\ \isasymforall y.\ R\ y\ y\ \isasymLongrightarrow \ R\ ?x\ y%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1533
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1534
Finally, we try to apply our reflexivity assumption.  We obtain a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1535
new assumption whose identical placeholders may be replaced by 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1536
any term involving~\isa{y}. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1537
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1538
\isacommand{apply}\ (drule\ spec)\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1539
\ 1.\ \isasymAnd y.\ R\ (?z2\ y)\ (?z2\ y)\ \isasymLongrightarrow\ R\ ?x\ y
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1540
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1541
This subgoal can only be proved by putting \isa{y} for all the placeholders,
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1542
making the assumption and conclusion become \isa{R\ y\ y}.  Isabelle can
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1543
replace \isa{?z2~y} by \isa{y}; this involves instantiating
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1544
\isa{?z2} to the identity function.  But, just as two steps earlier,
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1545
Isabelle refuses to substitute~\isa{y} for~\isa{?x}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1546
This example is typical of how Isabelle enforces sound quantifier reasoning. 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1547
\index{proofs!examples of failing|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1548
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1549
\section{Proving Theorems Using the {\tt\slshape blast} Method}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1550
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1551
\index{*blast (method)|(}%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1552
It is hard to prove many theorems using the methods 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1553
described above. A proof may be hundreds of steps long.  You 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1554
may need to search among different ways of proving certain 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1555
subgoals. Often a choice that proves one subgoal renders another 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1556
impossible to prove.  There are further complications that we have not
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1557
discussed, concerning negation and disjunction.  Isabelle's
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1558
\textbf{classical reasoner} is a family of tools that perform such
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1559
proofs automatically.  The most important of these is the 
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1560
\isa{blast} method. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1561
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1562
In this section, we shall first see how to use the classical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1563
reasoner in its default mode and then how to insert additional 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1564
rules, enabling it to work in new problem domains. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1565
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1566
 We begin with examples from pure predicate logic. The following 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1567
example is known as Andrew's challenge. Peter Andrews designed 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1568
it to be hard to prove by automatic means.
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1569
It is particularly hard for a resolution prover, where 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1570
converting the nested biconditionals to
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1571
clause form produces a combinatorial
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1572
explosion~\cite{pelletier86}. However, the
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1573
\isa{blast} method proves it in a fraction  of a second. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1574
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1575
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1576
"(({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1577
{\isasymforall}y.\
10301
paulson
parents: 10295
diff changeset
  1578
p(x){=}p(y))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1579
=\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1580
(({\isasymexists}x.\
10301
paulson
parents: 10295
diff changeset
  1581
q(x))=({\isasymforall}y.\
paulson
parents: 10295
diff changeset
  1582
p(y))))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1583
\ \ =\ \ \ \ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1584
\ \ \ \ \ \ \ \
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1585
(({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1586
{\isasymforall}y.\
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1587
q(x){=}q(y))\ =\ (({\isasymexists}x.\ p(x))=({\isasymforall}y.\ q(y))))"\isanewline
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1588
\isacommand{by}\ blast
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1589
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1590
The next example is a logic problem composed by Lewis Carroll. 
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1591
The \isa{blast} method finds it trivial. Moreover, it turns out 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1592
that not all of the assumptions are necessary. We can  
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1593
experiment with variations of this formula and see which ones 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1594
can be proved. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1595
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1596
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1597
"({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1598
honest(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1599
industrious(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1600
healthy(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1601
\isasymand\ \ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1602
\ \ \ \ \ \ \ \ \isasymnot\ ({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1603
grocer(x)\ \isasymand\
10301
paulson
parents: 10295
diff changeset
  1604
healthy(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1605
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1606
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1607
industrious(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1608
grocer(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1609
honest(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1610
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1611
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1612
cyclist(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1613
industrious(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1614
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1615
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1616
{\isasymnot}healthy(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1617
cyclist(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1618
{\isasymnot}honest(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1619
\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1620
\ \ \ \ \ \ \ \ \isasymlongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1621
({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1622
grocer(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1623
{\isasymnot}cyclist(x))"\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1624
\isacommand{by}\ blast
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1625
\end{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1626
The \isa{blast} method is also effective for set theory, which is
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1627
described in the next chapter.  The formula below may look horrible, but
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1628
the \isa{blast} method proves it in milliseconds. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1629
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1630
\isacommand{lemma}\ "({\isasymUnion}i{\isasymin}I.\ A(i))\ \isasyminter\ ({\isasymUnion}j{\isasymin}J.\ B(j))\ =\isanewline
paulson
parents: 10295
diff changeset
  1631
\ \ \ \ \ \ \ \ ({\isasymUnion}i{\isasymin}I.\ {\isasymUnion}j{\isasymin}J.\ A(i)\ \isasyminter\ B(j))"\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1632
\isacommand{by}\ blast
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1633
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1634
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1635
Few subgoals are couched purely in predicate logic and set theory.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1636
We can extend the scope of the classical reasoner by giving it new rules. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1637
Extending it effectively requires understanding the notions of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1638
introduction, elimination and destruction rules.  Moreover, there is a
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1639
distinction between  safe and unsafe rules. A 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1640
\textbf{safe}\indexbold{safe rules} rule is one that can be applied 
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1641
backwards without losing information; an
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1642
\textbf{unsafe}\indexbold{unsafe rules} rule loses  information, perhaps
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1643
transforming the subgoal into one that cannot be proved.  The safe/unsafe
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1644
distinction affects the proof search: if a proof attempt fails, the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1645
classical reasoner backtracks to the most recent unsafe rule application
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1646
and makes another choice. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1647
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1648
An important special case avoids all these complications.  A logical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1649
equivalence, which in higher-order logic is an equality between 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1650
formulas, can be given to the classical 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1651
reasoner and simplifier by using the attribute \attrdx{iff}.  You 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1652
should do so if the right hand side of the equivalence is  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1653
simpler than the left-hand side.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1654
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1655
For example, here is a simple fact about list concatenation. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1656
The result of appending two lists is empty if and only if both 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1657
of the lists are themselves empty. Obviously, applying this equivalence 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1658
will result in a simpler goal. When stating this lemma, we include 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1659
the \attrdx{iff} attribute. Once we have proved the lemma, Isabelle 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1660
will make it known to the classical reasoner (and to the simplifier). 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1661
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1662
\isacommand{lemma}\
10854
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1663
[iff]:\ "(xs{\isacharat}ys\ =\ [])\ =\
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1664
(xs=[]\ \isasymand\ ys=[])"\isanewline
10854
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1665
\isacommand{apply}\ (induct_tac\ xs)\isanewline
d1ff1ff5c5ad case_tac on bools
paulson
parents: 10848
diff changeset
  1666
\isacommand{apply}\ (simp_all)\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1667
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1668
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1669
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1670
This fact about multiplication is also appropriate for 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1671
the \attrdx{iff} attribute:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1672
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1673
(\mbox{?m}\ *\ \mbox{?n}\ =\ 0)\ =\ (\mbox{?m}\ =\ 0\ \isasymor\ \mbox{?n}\ =\ 0)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1674
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1675
A product is zero if and only if one of the factors is zero.  The
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1676
reasoning  involves a disjunction.  Proving new rules for
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1677
disjunctive reasoning  is hard, but translating to an actual disjunction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1678
works:  the classical reasoner handles disjunction properly.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1679
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1680
In more detail, this is how the \attrdx{iff} attribute works.  It converts
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1681
the equivalence $P=Q$ to a pair of rules: the introduction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1682
rule $Q\Imp P$ and the destruction rule $P\Imp Q$.  It gives both to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1683
classical reasoner as safe rules, ensuring that all occurrences of $P$ in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1684
a subgoal are replaced by~$Q$.  The simplifier performs the same
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1685
replacement, since \isa{iff} gives $P=Q$ to the
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1686
simplifier.  
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1687
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1688
Classical reasoning is different from
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1689
simplification.  Simplification is deterministic.  It applies rewrite rules
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1690
repeatedly, as long as possible, transforming a goal into another goal.  Classical
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1691
reasoning uses search and backtracking in order to prove a goal outright.%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1692
\index{*blast (method)|)}%
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1693
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1694
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1695
\section{Other Classical Reasoning Methods}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1696
 
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1697
The \isa{blast} method is our main workhorse for proving theorems 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1698
automatically. Other components of the classical reasoner interact 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1699
with the simplifier. Still others perform classical reasoning 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1700
to a limited extent, giving the user fine control over the proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1701
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1702
Of the latter methods, the most useful is 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1703
\methdx{clarify}.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1704
It performs 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1705
all obvious reasoning steps without splitting the goal into multiple 
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1706
parts. It does not apply unsafe rules that could render the 
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1707
goal unprovable. By performing the obvious 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1708
steps, \isa{clarify} lays bare the difficult parts of the problem, 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1709
where human intervention is necessary. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1710
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1711
For example, the following conjecture is false:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1712
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1713
\isacommand{lemma}\ "({\isasymforall}x.\ P\ x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1714
({\isasymexists}x.\ Q\ x)\ \isasymlongrightarrow\ ({\isasymforall}x.\ P\ x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1715
\isasymand\ Q\ x)"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1716
\isacommand{apply}\ clarify
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1717
\end{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1718
The \isa{blast} method would simply fail, but \isa{clarify} presents 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1719
a subgoal that helps us see why we cannot continue the proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1720
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1721
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk{\isasymforall}x.\ P\ x;\ Q\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1722
xa\isasymrbrakk\ \isasymLongrightarrow\ P\ x\ \isasymand\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1723
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1724
The proof must fail because the assumption \isa{Q\ xa} and conclusion \isa{Q\ x}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1725
refer to distinct bound variables.  To reach this state, \isa{clarify} applied
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1726
the introduction rules for \isa{\isasymlongrightarrow} and \isa{\isasymforall}
12535
wenzelm
parents: 12408
diff changeset
  1727
and the elimination rule for \isa{\isasymand}.  It did not apply the introduction
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1728
rule for  \isa{\isasymand} because of its policy never to split goals.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1729
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1730
Also available is \methdx{clarsimp}, a method
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1731
that interleaves \isa{clarify} and \isa{simp}.  Also there is  \methdx{safe},
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1732
which like \isa{clarify} performs obvious steps but even applies those that
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1733
split goals.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1734
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1735
The \methdx{force} method applies the classical
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1736
reasoner and simplifier  to one goal. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1737
Unless it can prove the goal, it fails. Contrast 
10546
b0ad1ed24cf6 replaced Eps by SOME
paulson
parents: 10399
diff changeset
  1738
that with the \isa{auto} method, which also combines classical reasoning 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1739
with simplification. The latter's purpose is to prove all the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1740
easy subgoals and parts of subgoals. Unfortunately, it can produce 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1741
large numbers of new subgoals; also, since it proves some subgoals 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1742
and splits others, it obscures the structure of the proof tree. 
10546
b0ad1ed24cf6 replaced Eps by SOME
paulson
parents: 10399
diff changeset
  1743
The \isa{force} method does not have these drawbacks. Another 
b0ad1ed24cf6 replaced Eps by SOME
paulson
parents: 10399
diff changeset
  1744
difference: \isa{force} tries harder than {\isa{auto}} to prove 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1745
its goal, so it can take much longer to terminate.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1746
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1747
Older components of the classical reasoner have largely been 
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1748
superseded by \isa{blast}, but they still have niche applications. 
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1749
Most important among these are \isa{fast} and \isa{best}. While \isa{blast} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1750
searches for proofs using a built-in first-order reasoner, these 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1751
earlier methods search for proofs using standard Isabelle inference. 
11179
bee6673b020a subst method and a new section on rule, rule_tac, etc
paulson
parents: 11159
diff changeset
  1752
That makes them slower but enables them to work in the 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1753
presence of the more unusual features of Isabelle rules, such 
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1754
as type classes and function unknowns. For example, recall the introduction rule
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  1755
for Hilbert's $\varepsilon$-operator: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1756
\begin{isabelle}
10546
b0ad1ed24cf6 replaced Eps by SOME
paulson
parents: 10399
diff changeset
  1757
?P\ ?x\ \isasymLongrightarrow\ ?P\ (SOME\ x.\ ?P x)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1758
\rulename{someI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1759
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1760
%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1761
The repeated occurrence of the variable \isa{?P} makes this rule tricky 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1762
to apply. Consider this contrived example: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1763
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1764
\isacommand{lemma}\ "\isasymlbrakk Q\ a;\ P\ a\isasymrbrakk\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1765
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ P\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1766
\isasymand\ Q\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1767
\isacommand{apply}\ (rule\ someI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1768
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1769
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1770
We can apply rule \isa{someI} explicitly.  It yields the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1771
following subgoal: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1772
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1773
\ 1.\ \isasymlbrakk Q\ a;\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P\ ?x\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1774
\isasymand\ Q\ ?x%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1775
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1776
The proof from this point is trivial.  Could we have
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1777
proved the theorem with a single command? Not using \isa{blast}: it
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1778
cannot perform  the higher-order unification needed here.  The
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1779
\methdx{fast} method succeeds: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1780
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1781
\isacommand{apply}\ (fast\ intro!:\ someI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1782
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1783
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1784
The \methdx{best} method is similar to
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1785
\isa{fast} but it uses a  best-first search instead of depth-first search.
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1786
Accordingly,  it is slower but is less susceptible to divergence.
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1787
Transitivity  rules usually cause \isa{fast} to loop where \isa{best} 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1788
can often manage.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1789
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1790
Here is a summary of the classical reasoning methods:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1791
\begin{itemize}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1792
\item \methdx{blast} works automatically and is the fastest
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1793
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1794
\item \methdx{clarify} and \methdx{clarsimp} perform obvious steps without
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1795
splitting the goal;  \methdx{safe} even splits goals
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1796
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1797
\item \methdx{force} uses classical reasoning and simplification to prove a goal;
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1798
 \methdx{auto} is similar but leaves what it cannot prove
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1799
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1800
\item \methdx{fast} and \methdx{best} are legacy methods that work well with rules
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1801
involving unusual features
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1802
\end{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1803
A table illustrates the relationships among four of these methods. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1804
\begin{center}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1805
\begin{tabular}{r|l|l|}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1806
           & no split   & split \\ \hline
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1807
  no simp  & \methdx{clarify}    & \methdx{safe} \\ \hline
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1808
     simp  & \methdx{clarsimp}   & \methdx{auto} \\ \hline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1809
\end{tabular}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1810
\end{center}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1811
16546
77e7fd18b785 added find2
nipkow
parents: 16523
diff changeset
  1812
\section{Finding More Theorems}
77e7fd18b785 added find2
nipkow
parents: 16523
diff changeset
  1813
\label{sec:find2}
77e7fd18b785 added find2
nipkow
parents: 16523
diff changeset
  1814
\input{Rules/document/find2.tex}
77e7fd18b785 added find2
nipkow
parents: 16523
diff changeset
  1815
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1816
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1817
\section{Forward Proof: Transforming Theorems}\label{sec:forward}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1818
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1819
\index{forward proof|(}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1820
Forward proof means deriving new facts from old ones.  It is  the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1821
most fundamental type of proof.  Backward proof, by working  from goals to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1822
subgoals, can help us find a difficult proof.  But it is
14403
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1823
not always the best way of presenting the proof thus found.  Forward
10301
paulson
parents: 10295
diff changeset
  1824
proof is particularly good for reasoning from the general
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1825
to the specific.  For example, consider this distributive law for
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1826
the greatest common divisor:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1827
\[ k\times\gcd(m,n) = \gcd(k\times m,k\times n)\]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1828
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1829
Putting $m=1$ we get (since $\gcd(1,n)=1$ and $k\times1=k$) 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1830
\[ k = \gcd(k,k\times n)\]
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1831
We have derived a new fact; if re-oriented, it might be
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1832
useful for simplification.  After re-orienting it and putting $n=1$, we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1833
derive another useful law: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1834
\[ \gcd(k,k)=k \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1835
Substituting values for variables --- instantiation --- is a forward step. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1836
Re-orientation works by applying the symmetry of equality to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1837
an equation, so it too is a forward step.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1838
14403
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1839
\subsection{Modifying a Theorem using {\tt\slshape of},  {\tt\slshape where}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1840
 and {\tt\slshape THEN}}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1841
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1842
\label{sec:THEN}
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1843
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1844
Let us reproduce our examples in Isabelle.  Recall that in
25258
22d16596c306 recdef -> fun
nipkow
parents: 16546
diff changeset
  1845
{\S}\ref{sec:fun-simplification} we declared the recursive function
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1846
\isa{gcd}:\index{*gcd (constant)|(}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1847
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1848
\isacommand{fun}\ gcd\ ::\ "nat\ \isasymRightarrow \ nat\ \isasymRightarrow \ nat"\ \isakeyword{where}\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1849
\ \ "gcd\ m\ n\ =\ (if\ n=0\ then\ m\ else\ gcd\ n\ (m\ mod\ n))"
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1850
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1851
%
12333
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
  1852
From this definition, it is possible to prove the distributive law.  
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
  1853
That takes us to the starting point for our example.
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1854
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1855
?k\ *\ gcd\ ?m\ ?n\ =\ gcd\ (?k\ *\ ?m)\ (?k\ *\ ?n)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1856
\rulename{gcd_mult_distrib2}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1857
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1858
%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1859
The first step in our derivation is to replace \isa{?m} by~1.  We instantiate the
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1860
theorem using~\attrdx{of}, which identifies variables in order of their
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1861
appearance from left to right.  In this case, the variables  are \isa{?k}, \isa{?m}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1862
and~\isa{?n}. So, the expression
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1863
\hbox{\texttt{[of k 1]}} replaces \isa{?k} by~\isa{k} and \isa{?m}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  1864
by~\isa{1}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1865
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1866
\isacommand{lemmas}\ gcd_mult_0\ =\ gcd_mult_distrib2\ [of\ k\ 1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1867
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1868
%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1869
The keyword \commdx{lemmas} declares a new theorem, which can be derived
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1870
from an existing one using attributes such as \isa{[of~k~1]}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1871
The command 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1872
\isa{thm gcd_mult_0}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1873
displays the result:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1874
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1875
\ \ \ \ \ k\ *\ gcd\ 1\ ?n\ =\ gcd\ (k\ *\ 1)\ (k\ *\ ?n)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1876
\end{isabelle}
14403
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1877
Something is odd: \isa{k} is an ordinary variable, while \isa{?n} 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1878
is schematic.  We did not specify an instantiation 
14403
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1879
for \isa{?n}.  In its present form, the theorem does not allow 
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1880
substitution for \isa{k}.  One solution is to avoid giving an instantiation for
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1881
\isa{?k}: instead of a term we can put an underscore~(\isa{_}).  For example,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1882
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1883
\ \ \ \ \ gcd_mult_distrib2\ [of\ _\ 1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1884
\end{isabelle}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1885
replaces \isa{?m} by~\isa{1} but leaves \isa{?k} unchanged.  
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1886
14403
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1887
An equivalent solution is to use the attribute \isa{where}. 
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1888
\begin{isabelle}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1889
\ \ \ \ \ gcd\_mult\_distrib2\ [where\ m=1]
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1890
\end{isabelle}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1891
While \isa{of} refers to
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1892
variables by their position, \isa{where} refers to variables by name. Multiple
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1893
instantiations are separated by~\isa{and}, as in this example:
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1894
\begin{isabelle}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1895
\ \ \ \ \ gcd\_mult\_distrib2\ [where\ m=1\ and\ k=1]
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1896
\end{isabelle}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1897
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1898
We now continue the present example with the version of \isa{gcd_mult_0}
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1899
shown above, which has \isa{k} instead of \isa{?k}.
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1900
Once we have replaced \isa{?m} by~1, we must next simplify
32d1526d3237 new "where" section
paulson
parents: 13791
diff changeset
  1901
the theorem \isa{gcd_mult_0}, performing the steps 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1902
$\gcd(1,n)=1$ and $k\times1=k$.  The \attrdx{simplified}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1903
attribute takes a theorem
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1904
and returns the result of simplifying it, with respect to the default
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1905
simplification rules:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1906
\begin{isabelle}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1907
\isacommand{lemmas}\ gcd_mult_1\ =\ gcd_mult_0\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1908
[simplified]%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1909
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1910
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1911
Again, we display the resulting theorem:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1912
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1913
\ \ \ \ \ k\ =\ gcd\ k\ (k\ *\ ?n)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1914
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1915
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1916
To re-orient the equation requires the symmetry rule:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1917
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1918
?s\ =\ ?t\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1919
\isasymLongrightarrow\ ?t\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1920
?s%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1921
\rulenamedx{sym}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1922
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1923
The following declaration gives our equation to \isa{sym}:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1924
\begin{isabelle}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1925
\ \ \ \isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_1\ [THEN\ sym]
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1926
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1927
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1928
Here is the result:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1929
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1930
\ \ \ \ \ gcd\ k\ (k\ *\ ?n)\ =\ k%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1931
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1932
\isa{THEN~sym}\indexbold{*THEN (attribute)} gives the current theorem to the
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1933
rule \isa{sym} and returns the resulting conclusion.  The effect is to
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1934
exchange the two operands of the equality. Typically \isa{THEN} is used
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1935
with destruction rules.  Also useful is \isa{THEN~spec}, which removes the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1936
quantifier from a theorem of the form $\forall x.\,P$, and \isa{THEN~mp},
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1937
which converts the implication $P\imp Q$ into the rule
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1938
$\vcenter{\infer{Q}{P}}$. Similar to \isa{mp} are the following two rules,
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1939
which extract  the two directions of reasoning about a boolean equivalence:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1940
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1941
\isasymlbrakk?Q\ =\ ?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1942
\rulenamedx{iffD1}%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1943
\isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  1944
\isasymlbrakk?P\ =\ ?Q;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  1945
\rulenamedx{iffD2}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1946
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1947
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1948
Normally we would never name the intermediate theorems
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  1949
such as \isa{gcd_mult_0} and \isa{gcd_mult_1} but would combine
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1950
the three forward steps: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1951
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1952
\isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym]%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1953
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1954
The directives, or attributes, are processed from left to right.  This
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1955
declaration of \isa{gcd_mult} is equivalent to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1956
previous one.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1957
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1958
Such declarations can make the proof script hard to read.  Better   
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1959
is to state the new lemma explicitly and to prove it using a single
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1960
\isa{rule} method whose operand is expressed using forward reasoning:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1961
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1962
\isacommand{lemma}\ gcd\_mult\ [simp]:\ "gcd\ k\ (k*n)\ =\ k"\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1963
\isacommand{by}\ (rule\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym])
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1964
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1965
Compared with the previous proof of \isa{gcd_mult}, this
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1966
version shows the reader what has been proved.  Also, the result will be processed
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1967
in the normal way.  In particular, Isabelle generalizes over all variables: the
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1968
resulting theorem will have {\isa{?k}} instead of {\isa{k}}.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1969
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1970
At the start  of this section, we also saw a proof of $\gcd(k,k)=k$.  Here
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1971
is the Isabelle version:\index{*gcd (constant)|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1972
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  1973
\isacommand{lemma}\ gcd\_self\ [simp]:\ "gcd\ k\ k\ =\ k"\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1974
\isacommand{by}\ (rule\ gcd_mult\ [of\ k\ 1,\ simplified])
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1975
\end{isabelle}
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1976
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1977
\begin{warn}
12535
wenzelm
parents: 12408
diff changeset
  1978
To give~\isa{of} a nonatomic term, enclose it in quotation marks, as in
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1979
\isa{[of "k*m"]}.  The term must not contain unknowns: an
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1980
attribute such as 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1981
\isa{[of "?k*m"]} will be rejected.
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1982
\end{warn}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1983
15952
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1984
%Answer is now included in that section! Is a modified version of this
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1985
%  exercise worth including? E.g. find a difference between the two ways
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1986
%  of substituting.
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1987
%\begin{exercise}
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1988
%In {\S}\ref{sec:subst} the method \isa{subst\ mult_commute} was applied.  How
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1989
%can we achieve the same effect using \isa{THEN} with the rule \isa{ssubst}?
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1990
%% answer  rule (mult_commute [THEN ssubst])
ad9e27c1b2c8 documented new subst method
paulson
parents: 15617
diff changeset
  1991
%\end{exercise}
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  1992
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1993
\subsection{Modifying a Theorem using {\tt\slshape OF}}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1994
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  1995
\index{*OF (attribute)|(}%
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1996
Recall that \isa{of} generates an instance of a
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  1997
rule by specifying values for its variables.  Analogous is \isa{OF}, which
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1998
generates an instance of a rule by specifying facts for its premises.  
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  1999
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2000
We again need the divides relation\index{divides relation} of number theory, which
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2001
as we recall is defined by 
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2002
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2003
?m\ dvd\ ?n\ \isasymequiv\ {\isasymexists}k.\ ?n\ =\ ?m\ *\ k
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2004
\rulename{dvd_def}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2005
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2006
%
12333
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
  2007
Suppose, for example, that we have proved the following rule.  
ef43a3d6e962 minor tweaks
paulson
parents: 12156
diff changeset
  2008
It states that if $k$ and $n$ are relatively prime
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2009
and if $k$ divides $m\times n$ then $k$ divides $m$.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2010
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2011
\isasymlbrakk gcd ?k ?n {=} 1;\ ?k\ dvd\ ?m * ?n\isasymrbrakk\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2012
\isasymLongrightarrow\ ?k\ dvd\ ?m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2013
\rulename{relprime_dvd_mult}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2014
\end{isabelle}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2015
We can use \isa{OF} to create an instance of this rule.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2016
First, we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2017
prove an instance of its first premise:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2018
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2019
\isacommand{lemma}\ relprime\_20\_81:\ "gcd\ 20\ 81\ =\ 1"\isanewline
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  2020
\isacommand{by}\ (simp\ add:\ gcd.simps)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2021
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2022
We have evaluated an application of the \isa{gcd} function by
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2023
simplification.  Expression evaluation involving recursive functions is not
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2024
guaranteed to terminate, and it can be slow; Isabelle
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2025
performs arithmetic by  rewriting symbolic bit strings.  Here,
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2026
however, the simplification takes less than one second.  We can
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2027
give this new lemma to \isa{OF}.  The expression
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2028
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2029
\ \ \ \ \ relprime_dvd_mult [OF relprime_20_81]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2030
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2031
yields the theorem
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2032
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2033
\ \ \ \ \ 20\ dvd\ (?m\ *\ 81)\ \isasymLongrightarrow\ 20\ dvd\ ?m%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2034
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2035
%
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2036
\isa{OF} takes any number of operands.  Consider 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2037
the following facts about the divides relation: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2038
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2039
\isasymlbrakk?k\ dvd\ ?m;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2040
?k\ dvd\ ?n\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2041
\isasymLongrightarrow\ ?k\ dvd\
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  2042
?m\ +\ ?n
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2043
\rulename{dvd_add}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2044
?m\ dvd\ ?m%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2045
\rulename{dvd_refl}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2046
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2047
Let us supply \isa{dvd_refl} for each of the premises of \isa{dvd_add}:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2048
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2049
\ \ \ \ \ dvd_add [OF dvd_refl dvd_refl]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2050
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2051
Here is the theorem that we have expressed: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2052
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2053
\ \ \ \ \ ?k\ dvd\ (?k\ +\ ?k)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2054
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2055
As with \isa{of}, we can use the \isa{_} symbol to leave some positions
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2056
unspecified:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2057
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2058
\ \ \ \ \ dvd_add [OF _ dvd_refl]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2059
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2060
The result is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2061
\begin{isabelle}
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  2062
\ \ \ \ \ ?k\ dvd\ ?m\ \isasymLongrightarrow\ ?k\ dvd\ ?m\ +\ ?k
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2063
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2064
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2065
You may have noticed that \isa{THEN} and \isa{OF} are based on 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2066
the same idea, namely to combine two rules.  They differ in the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2067
order of the combination and thus in their effect.  We use \isa{THEN}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2068
typically with a destruction rule to extract a subformula of the current
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2069
theorem.  We use \isa{OF} with a list of facts to generate an instance of
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2070
the current theorem.%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2071
\index{*OF (attribute)|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2072
10848
7b3ee4695fe6 various changes including the SOME examples, rule_format and "by"
paulson
parents: 10792
diff changeset
  2073
Here is a summary of some primitives for forward reasoning:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2074
\begin{itemize}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2075
\item \attrdx{of} instantiates the variables of a rule to a list of terms
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2076
\item \attrdx{OF} applies a rule to a list of theorems
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2077
\item \attrdx{THEN} gives a theorem to a named rule and returns the
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2078
conclusion 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2079
%\item \attrdx{rule_format} puts a theorem into standard form
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2080
%  by removing \isa{\isasymlongrightarrow} and~\isa{\isasymforall}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2081
\item \attrdx{simplified} applies the simplifier to a theorem
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2082
\item \isacommand{lemmas} assigns a name to the theorem produced by the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2083
attributes above
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2084
\end{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2085
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2086
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2087
\section{Forward Reasoning in a Backward Proof}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2088
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2089
We have seen that the forward proof directives work well within a backward 
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2090
proof.  There are many ways to achieve a forward style using our existing
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2091
proof methods.  We shall also meet some new methods that perform forward
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2092
reasoning.  
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2093
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2094
The methods \isa{drule}, \isa{frule}, \isa{drule_tac}, etc.,
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2095
reason forward from a subgoal.  We have seen them already, using rules such as
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2096
\isa{mp} and
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2097
\isa{spec} to operate on formulae.  They can also operate on terms, using rules
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2098
such as these:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2099
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2100
x\ =\ y\ \isasymLongrightarrow \ f\ x\ =\ f\ y%
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  2101
\rulenamedx{arg_cong}\isanewline
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2102
i\ \isasymle \ j\ \isasymLongrightarrow \ i\ *\ k\ \isasymle \ j\ *\ k%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2103
\rulename{mult_le_mono1}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2104
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2105
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2106
For example, let us prove a fact about divisibility in the natural numbers:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2107
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2108
\isacommand{lemma}\ "2\ \isasymle \ u\ \isasymLongrightarrow \ u*m\ \isasymnoteq
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2109
\ Suc(u*n)"\isanewline
12408
2884148a9fe9 intro and elim now require arguments
paulson
parents: 12333
diff changeset
  2110
\isacommand{apply}\ (intro\ notI)\isanewline
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2111
\ 1.\ \isasymlbrakk 2\ \isasymle \ u;\ u\ *\ m\ =\ Suc\ (u\ *\ n)\isasymrbrakk \ \isasymLongrightarrow \ False%
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2112
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2113
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2114
The key step is to apply the function \ldots\isa{mod\ u} to both sides of the
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2115
equation
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2116
\isa{u*m\ =\ Suc(u*n)}:\index{*drule_tac (method)}
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2117
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2118
\isacommand{apply}\ (drule_tac\ f="\isasymlambda x.\ x\ mod\ u"\ \isakeyword{in}\
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2119
arg_cong)\isanewline
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2120
\ 1.\ \isasymlbrakk 2\ \isasymle \ u;\ u\ *\ m\ mod\ u\ =\ Suc\ (u\ *\ n)\ mod\
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2121
u\isasymrbrakk \ \isasymLongrightarrow \ False
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2122
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2123
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2124
Simplification reduces the left side to 0 and the right side to~1, yielding the
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2125
required contradiction.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2126
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2127
\isacommand{apply}\ (simp\ add:\ mod_Suc)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2128
\isacommand{done}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2129
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2130
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2131
Our proof has used a fact about remainder:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2132
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2133
Suc\ m\ mod\ n\ =\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2134
(if\ Suc\ (m\ mod\ n)\ =\ n\ then\ 0\ else\ Suc\ (m\ mod\ n))
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2135
\rulename{mod_Suc}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2136
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2137
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2138
\subsection{The Method {\tt\slshape insert}}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2139
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2140
\index{*insert (method)|(}%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2141
The \isa{insert} method
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2142
inserts a given theorem as a new assumption of the current subgoal.  This
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2143
already is a forward step; moreover, we may (as always when using a
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2144
theorem) apply
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2145
\isa{of}, \isa{THEN} and other directives.  The new assumption can then
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2146
be used to help prove the subgoal.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2147
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2148
For example, consider this theorem about the divides relation.  The first
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2149
proof step inserts the distributive law for
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2150
\isa{gcd}.  We specify its variables as shown. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2151
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2152
\isacommand{lemma}\ relprime\_dvd\_mult:\ \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2153
\ \ \ \ \ \ "\isasymlbrakk \ gcd\ k\ n\ =\ 1;\ k\ dvd\ m*n\ \isasymrbrakk \ \isasymLongrightarrow \ k\ dvd\ m"\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2154
\isacommand{apply}\ (insert\ gcd_mult_distrib2\ [of\ m\ k\ n])
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2155
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2156
In the resulting subgoal, note how the equation has been 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2157
inserted: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2158
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2159
\ 1.\ \isasymlbrakk gcd\ k\ n\ =\ 1;\ k\ dvd\ m\ *\ n;\ m\ *\ gcd\ k\ n\ =\ gcd\ (m\ *\ k)\ (m\ *\ n)\isasymrbrakk \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2160
\isaindent{\ 1.\ }\isasymLongrightarrow \ k\ dvd\ m%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2161
\end{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2162
The next proof step utilizes the assumption \isa{gcd\ k\ n\ =\ 1}
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2163
(note that \isa{Suc\ 0} is another expression for 1):
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2164
\begin{isabelle}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2165
\isacommand{apply}(simp)\isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2166
\ 1.\ \isasymlbrakk gcd\ k\ n\ =\ Suc\ 0;\ k\ dvd\ m\ *\ n;\ m\ =\ gcd\ (m\ *\ k)\ (m\ *\ n)\isasymrbrakk \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2167
\isaindent{\ 1.\ }\isasymLongrightarrow \ k\ dvd\ m%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2168
\end{isabelle}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2169
Simplification has yielded an equation for~\isa{m}.  The rest of the proof
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2170
is omitted.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2171
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2172
\medskip
11417
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  2173
Here is another demonstration of \isa{insert}.  Division and
499435b4084e less indexing of theorem names
paulson
parents: 11411
diff changeset
  2174
remainder obey a well-known law: 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2175
\begin{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2176
(?m\ div\ ?n)\ *\ ?n\ +\ ?m\ mod\ ?n\ =\ ?m
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2177
\rulename{mod_div_equality}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2178
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2179
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2180
We refer to this law explicitly in the following proof: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2181
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2182
\isacommand{lemma}\ div_mult_self_is_m:\ \isanewline
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2183
\ \ \ \ \ \ "0{\isacharless}n\ \isasymLongrightarrow\ (m*n)\ div\ n\ =\
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2184
(m::nat)"\isanewline
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2185
\isacommand{apply}\ (insert\ mod_div_equality\ [of\ "m*n"\ n])\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2186
\isacommand{apply}\ (simp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2187
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2188
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2189
The first step inserts the law, specifying \isa{m*n} and
10301
paulson
parents: 10295
diff changeset
  2190
\isa{n} for its variables.  Notice that non-trivial expressions must be
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2191
enclosed in quotation marks.  Here is the resulting 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2192
subgoal, with its new assumption: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2193
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2194
%0\ \isacharless\ n\ \isasymLongrightarrow\ (m\
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2195
%*\ n)\ div\ n\ =\ m\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2196
\ 1.\ \isasymlbrakk0\ \isacharless\
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2197
n;\ \ (m\ *\ n)\ div\ n\ *\ n\ +\ (m\ *\ n)\ mod\ n\
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2198
=\ m\ *\ n\isasymrbrakk\isanewline
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2199
\ \ \ \ \isasymLongrightarrow\ (m\ *\ n)\ div\ n\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2200
=\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2201
\end{isabelle}
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2202
Simplification reduces \isa{(m\ *\ n)\ mod\ n} to zero.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2203
Then it cancels the factor~\isa{n} on both
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2204
sides of the equation \isa{(m\ *\ n)\ div\ n\ *\ n\ =\ m\ *\ n}, proving the
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2205
theorem.
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2206
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2207
\begin{warn}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2208
Any unknowns in the theorem given to \methdx{insert} will be universally
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2209
quantified in the new assumption.
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2210
\end{warn}%
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2211
\index{*insert (method)|)}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2212
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2213
\subsection{The Method {\tt\slshape subgoal_tac}}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2214
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2215
\index{*subgoal_tac (method)}%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2216
A related method is \isa{subgoal_tac}, but instead
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2217
of inserting  a theorem as an assumption, it inserts an arbitrary formula. 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2218
This formula must be proved later as a separate subgoal. The 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2219
idea is to claim that the formula holds on the basis of the current 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2220
assumptions, to use this claim to complete the proof, and finally 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2221
to justify the claim. It gives the proof 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2222
some structure.  If you find yourself generating a complex assumption by a
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2223
long series of forward steps, consider using \isa{subgoal_tac} instead: you can
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2224
state the formula you are aiming for, and perhaps prove it automatically.
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2225
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2226
Look at the following example. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2227
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2228
\isacommand{lemma}\ "\isasymlbrakk(z::int)\ <\ 37;\ 66\ <\ 2*z;\ z*z\
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2229
\isasymnoteq\ 1225;\ Q(34);\ Q(36)\isasymrbrakk\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2230
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ Q(z)"\isanewline
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2231
\isacommand{apply}\ (subgoal_tac\ "z\ =\ 34\ \isasymor\ z\ =\
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2232
36")\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2233
\isacommand{apply}\ blast\isanewline
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2234
\isacommand{apply}\ (subgoal_tac\ "z\ \isasymnoteq\ 35")\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2235
\isacommand{apply}\ arith\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2236
\isacommand{apply}\ force\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2237
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2238
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2239
The first assumption tells us 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2240
that \isa{z} is no greater than~36. The second tells us that \isa{z} 
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2241
is at least~34. The third assumption tells us that \isa{z} cannot be 35, since
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2242
$35\times35=1225$.  So \isa{z} is either 34 or~36, and since \isa{Q} holds for
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2243
both of those  values, we have the conclusion. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2244
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2245
The Isabelle proof closely follows this reasoning. The first 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2246
step is to claim that \isa{z} is either 34 or 36. The resulting proof 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2247
state gives us two subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2248
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2249
%\isasymlbrakk z\ <\ 37;\ 66\ <\ 2\ *\ z;\ z\ *\ z\ \isasymnoteq\ 1225;\
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2250
%Q\ 34;\ Q\ 36\isasymrbrakk\ \isasymLongrightarrow\ Q\ z\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2251
\ 1.\ \isasymlbrakk z\ <\ 37;\ 66\ <\ 2\ *\ z;\ z\ *\ z\ \isasymnoteq\ 1225;\ Q\ 34;\ Q\ 36;\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2252
\ \ \ \ \ z\ =\ 34\ \isasymor\ z\ =\ 36\isasymrbrakk\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2253
\ \ \ \ \isasymLongrightarrow\ Q\ z\isanewline
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2254
\ 2.\ \isasymlbrakk z\ <\ 37;\ 66\ <\ 2\ *\ z;\ z\ *\ z\ \isasymnoteq\ 1225;\ Q\ 34;\ Q\ 36\isasymrbrakk\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2255
\ \ \ \ \isasymLongrightarrow\ z\ =\ 34\ \isasymor\ z\ =\ 36
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2256
\end{isabelle}
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  2257
The first subgoal is trivial (\isa{blast}), but for the second Isabelle needs help to eliminate
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2258
the case
10596
77951eaeb5b0 tidying
paulson
parents: 10578
diff changeset
  2259
\isa{z}=35.  The second invocation  of {\isa{subgoal_tac}} leaves two
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2260
subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2261
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2262
\ 1.\ \isasymlbrakk z\ <\ 37;\ 66\ <\ 2\ *\ z;\ z\ *\ z\ \isasymnoteq\
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2263
1225;\ Q\ 34;\ Q\ 36;\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2264
\ \ \ \ \ z\ \isasymnoteq\ 35\isasymrbrakk\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2265
\ \ \ \ \isasymLongrightarrow\ z\ =\ 34\ \isasymor\ z\ =\ 36\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2266
\ 2.\ \isasymlbrakk z\ <\ 37;\ 66\ <\ 2\ *\ z;\ z\ *\ z\ \isasymnoteq\ 1225;\ Q\ 34;\ Q\ 36\isasymrbrakk\isanewline
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2267
\ \ \ \ \isasymLongrightarrow\ z\ \isasymnoteq\ 35
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2268
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2269
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  2270
Assuming that \isa{z} is not 35, the first subgoal follows by linear arithmetic
6852682eaf16 *** empty log message ***
nipkow
parents: 10967
diff changeset
  2271
(\isa{arith}). For the second subgoal we apply the method \isa{force}, 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2272
which proceeds by assuming that \isa{z}=35 and arriving at a contradiction.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2273
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2274
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2275
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2276
Summary of these methods:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2277
\begin{itemize}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2278
\item \methdx{insert} adds a theorem as a new assumption
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2279
\item \methdx{subgoal_tac} adds a formula as a new assumption and leaves the
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2280
subgoal of proving that formula
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  2281
\end{itemize}
11077
8f4fa58e6fba snapshot of a new version
paulson
parents: 10983
diff changeset
  2282
\index{forward proof|)}
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2283
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2284
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2285
\section{Managing Large Proofs}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2286
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2287
Naturally you should try to divide proofs into manageable parts.  Look for lemmas
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2288
that can be proved separately.  Sometimes you will observe that they are
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2289
instances of much simpler facts.  On other occasions, no lemmas suggest themselves
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2290
and you are forced to cope with a long proof involving many subgoals.  
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2291
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2292
\subsection{Tacticals, or Control Structures}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2293
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2294
\index{tacticals|(}%
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2295
If the proof is long, perhaps it at least has some regularity.  Then you can
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2296
express it more concisely using \textbf{tacticals}, which provide control
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2297
structures.  Here is a proof (it would be a one-liner using
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2298
\isa{blast}, but forget that) that contains a series of repeated
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2299
commands:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2300
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2301
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2302
\isacommand{lemma}\ "\isasymlbrakk P\isasymlongrightarrow Q;\
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2303
Q\isasymlongrightarrow R;\ R\isasymlongrightarrow S;\ P\isasymrbrakk \
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2304
\isasymLongrightarrow \ S"\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2305
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2306
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2307
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2308
\isacommand{apply}\ (assumption)\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2309
\isacommand{done}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2310
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2311
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2312
Each of the three identical commands finds an implication and proves its
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2313
antecedent by assumption.  The first one finds \isa{P\isasymlongrightarrow Q} and
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2314
\isa{P}, concluding~\isa{Q}; the second one concludes~\isa{R} and the third one
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2315
concludes~\isa{S}.  The final step matches the assumption \isa{S} with the goal to
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2316
be proved.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2317
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2318
Suffixing a method with a plus sign~(\isa+)\index{*"+ (tactical)}
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2319
expresses one or more repetitions:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2320
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2321
\isacommand{lemma}\ "\isasymlbrakk P\isasymlongrightarrow Q;\ Q\isasymlongrightarrow R;\ R\isasymlongrightarrow S;\ P\isasymrbrakk \ \isasymLongrightarrow \ S"\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2322
\isacommand{by}\ (drule\ mp,\ assumption)+
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2323
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2324
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2325
Using \isacommand{by} takes care of the final use of \isa{assumption}.  The new
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2326
proof is more concise.  It is also more general: the repetitive method works
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2327
for a chain of implications having any length, not just three.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2328
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2329
Choice is another control structure.  Separating two methods by a vertical
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2330
% we must use ?? rather than "| as the sorting item because somehow the presence
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2331
% of | (even quoted) stops hyperref from putting |hyperpage at the end of the index
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2332
% entry.
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2333
bar~(\isa|)\index{??@\texttt{"|} (tactical)}  gives the effect of applying the
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2334
first method, and if that fails, trying the second.  It can be combined with
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2335
repetition, when the choice must be made over and over again.  Here is a chain of
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2336
implications in which most of the antecedents are proved by assumption, but one is
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2337
proved by arithmetic:
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2338
\begin{isabelle}
12156
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2339
\isacommand{lemma}\ "\isasymlbrakk Q\isasymlongrightarrow R;\ P\isasymlongrightarrow Q;\ x<5\isasymlongrightarrow P;\
d2758965362e new-style numerals without leading #, along with generic 0 and 1
paulson
parents: 11494
diff changeset
  2340
Suc\ x\ <\ 5\isasymrbrakk \ \isasymLongrightarrow \ R"\ \isanewline
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2341
\isacommand{by}\ (drule\ mp,\ (assumption|arith))+
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2342
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2343
The \isa{arith}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2344
method can prove $x<5$ from $x+1<5$, but it cannot duplicate the effect of
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2345
\isa{assumption}.  Therefore, we combine these methods using the choice
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2346
operator.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2347
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2348
A postfixed question mark~(\isa?)\index{*"? (tactical)} expresses zero or one
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2349
repetitions of a method.  It can also be viewed as the choice between executing a
12540
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  2350
method and doing nothing.  It is useless at top level but can be valuable
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  2351
within other control structures; for example, 
a5604ff1ef4e minor suggestions from Markus
paulson
parents: 12535
diff changeset
  2352
\isa{($m$+)?} performs zero or more repetitions of method~$m$.%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2353
\index{tacticals|)}
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2354
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2355
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2356
\subsection{Subgoal Numbering}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2357
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2358
Another problem in large proofs is contending with huge
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2359
subgoals or many subgoals.  Induction can produce a proof state that looks
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2360
like this:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2361
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2362
\ 1.\ bigsubgoal1\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2363
\ 2.\ bigsubgoal2\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2364
\ 3.\ bigsubgoal3\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2365
\ 4.\ bigsubgoal4\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2366
\ 5.\ bigsubgoal5\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2367
\ 6.\ bigsubgoal6
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2368
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2369
If each \isa{bigsubgoal} is 15 lines or so, the proof state will be too big to
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2370
scroll through.  By default, Isabelle displays at most 10 subgoals.  The 
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2371
\commdx{pr} command lets you change this limit:
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2372
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2373
\isacommand{pr}\ 2\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2374
\ 1.\ bigsubgoal1\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2375
\ 2.\ bigsubgoal2\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2376
A total of 6 subgoals...
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2377
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2378
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2379
\medskip
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2380
All methods apply to the first subgoal.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2381
Sometimes, not only in a large proof, you may want to focus on some other
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2382
subgoal.  Then you should try the commands \isacommand{defer} or \isacommand{prefer}.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2383
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2384
In the following example, the first subgoal looks hard, while the others
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2385
look as if \isa{blast} alone could prove them:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2386
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2387
\ 1.\ hard\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2388
\ 2.\ \isasymnot \ \isasymnot \ P\ \isasymLongrightarrow \ P\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2389
\ 3.\ Q\ \isasymLongrightarrow \ Q%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2390
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2391
%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2392
The \commdx{defer} command moves the first subgoal into the last position.
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2393
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2394
\isacommand{defer}\ 1\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2395
\ 1.\ \isasymnot \ \isasymnot \ P\ \isasymLongrightarrow \ P\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2396
\ 2.\ Q\ \isasymLongrightarrow \ Q\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2397
\ 3.\ hard%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2398
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2399
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2400
Now we apply \isa{blast} repeatedly to the easy subgoals:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2401
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2402
\isacommand{apply}\ blast+\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2403
\ 1.\ hard%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2404
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2405
Using \isacommand{defer}, we have cleared away the trivial parts of the proof so
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2406
that we can devote attention to the difficult part.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2407
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2408
\medskip
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2409
The \commdx{prefer} command moves the specified subgoal into the
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2410
first position.  For example, if you suspect that one of your subgoals is
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2411
invalid (not a theorem), then you should investigate that subgoal first.  If it
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2412
cannot be proved, then there is no point in proving the other subgoals.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2413
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2414
\ 1.\ ok1\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2415
\ 2.\ ok2\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2416
\ 3.\ doubtful%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2417
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2418
%
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2419
We decide to work on the third subgoal.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2420
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2421
\isacommand{prefer}\ 3\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2422
\ 1.\ doubtful\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2423
\ 2.\ ok1\isanewline
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2424
\ 3.\ ok2
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2425
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2426
If we manage to prove \isa{doubtful}, then we can work on the other
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2427
subgoals, confident that we are not wasting our time.  Finally we revise the
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2428
proof script to remove the \isacommand{prefer} command, since we needed it only to
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2429
focus our exploration.  The previous example is different: its use of
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2430
\isacommand{defer} stops trivial subgoals from cluttering the rest of the
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2431
proof.  Even there, we should consider proving \isa{hard} as a preliminary
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2432
lemma.  Always seek ways to streamline your proofs.
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2433
 
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2434
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2435
\medskip
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2436
Summary:
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2437
\begin{itemize}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2438
\item the control structures \isa+, \isa? and \isa| help express complicated proofs
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2439
\item the \isacommand{pr} command can limit the number of subgoals to display
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2440
\item the \isacommand{defer} and \isacommand{prefer} commands move a 
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2441
subgoal to the last or first position
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2442
\end{itemize}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2443
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2444
\begin{exercise}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2445
Explain the use of \isa? and \isa+ in this proof.
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2446
\begin{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2447
\isacommand{lemma}\ "\isasymlbrakk P\isasymand Q\isasymlongrightarrow R;\ P\isasymlongrightarrow Q;\ P\isasymrbrakk \ \isasymLongrightarrow \ R"\isanewline
15617
4c7bba41483a auto update
paulson
parents: 15364
diff changeset
  2448
\isacommand{by}\ (drule\ mp,\ (intro conjI)?,\ assumption+)+
10967
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2449
\end{isabelle}
69937e62a28e arg_cong, tacticals, pr, defer, prefer
paulson
parents: 10887
diff changeset
  2450
\end{exercise}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2451
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2452
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2453
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2454
\section{Proving the Correctness of Euclid's Algorithm}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2455
\label{sec:proving-euclid}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2456
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2457
\index{Euclid's algorithm|(}\index{*gcd (constant)|(}\index{divides relation|(}%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2458
A brief development will demonstrate the techniques of this chapter,
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2459
including \isa{blast} applied with additional rules.  We shall also see
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2460
\isa{case_tac} used to perform a Boolean case split.
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2461
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2462
Let us prove that \isa{gcd} computes the greatest common
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2463
divisor of its two arguments.  
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2464
%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2465
We use induction: \isa{gcd.induct} is the
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2466
induction rule returned by \isa{fun}.  We simplify using
25258
22d16596c306 recdef -> fun
nipkow
parents: 16546
diff changeset
  2467
rules proved in {\S}\ref{sec:fun-simplification}, since rewriting by the
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2468
definition of \isa{gcd} can loop.
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2469
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2470
\isacommand{lemma}\ gcd\_dvd\_both:\ "(gcd\ m\ n\ dvd\ m)\ \isasymand \ (gcd\ m\ n\ dvd\ n)"
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2471
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2472
The induction formula must be a conjunction.  In the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2473
inductive step, each conjunct establishes the other. 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2474
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2475
\ 1.\ \isasymAnd m\ n.\ (n\ \isasymnoteq \ 0\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2476
\isaindent{\ 1.\ \isasymAnd m\ n.\ (}gcd\ n\ (m\ mod\ n)\ dvd\ n\ \isasymand \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2477
\isaindent{\ 1.\ \isasymAnd m\ n.\ (}gcd\ n\ (m\ mod\ n)\ dvd\ m\ mod\ n)\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2478
\isaindent{\ 1.\ \isasymAnd m\ n.\ }gcd\ m\ n\ dvd\ m\ \isasymand \ gcd\ m\ n\ dvd\ n%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2479
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2480
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2481
The conditional induction hypothesis suggests doing a case
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2482
analysis on \isa{n=0}.  We apply \methdx{case_tac} with type
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2483
\isa{bool} --- and not with a datatype, as we have done until now.  Since
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2484
\isa{nat} is a datatype, we could have written
12535
wenzelm
parents: 12408
diff changeset
  2485
\isa{case_tac~n} instead of \isa{case_tac~"n=0"}.  However, the definition
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2486
of \isa{gcd} makes a Boolean decision:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2487
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2488
\ \ \ \ "gcd\ m\ n\ =\ (if\ n=0\ then\ m\ else\ gcd\ n\ (m\ mod\ n))"
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2489
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2490
Proofs about a function frequently follow the function's definition, so we perform
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2491
case analysis over the same formula.
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2492
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2493
\isacommand{apply}\ (case_tac\ "n=0")\isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2494
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2495
\isaindent{\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk }gcd\ n\ (m\ mod\ n)\ dvd\ n\ \isasymand \ gcd\ n\ (m\ mod\ n)\ dvd\ m\ mod\ n;\isanewline
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2496
\isaindent{\ 1.\ \isasymAnd m\ n.\ \ }n\ =\ 0\isasymrbrakk \isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2497
\isaindent{\ 1.\ \isasymAnd m\ n.\ }\isasymLongrightarrow \ gcd\ m\ n\ dvd\ m\ \isasymand \ gcd\ m\ n\ dvd\ n\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2498
\ 2.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2499
\isaindent{\ 2.\ \isasymAnd m\ n.\ \isasymlbrakk }gcd\ n\ (m\ mod\ n)\ dvd\ n\ \isasymand \ gcd\ n\ (m\ mod\ n)\ dvd\ m\ mod\ n;\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2500
\isaindent{\ 2.\ \isasymAnd m\ n.\ \ }n\ \isasymnoteq \ 0\isasymrbrakk \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2501
\isaindent{\ 2.\ \isasymAnd m\ n.\ }\isasymLongrightarrow \ gcd\ m\ n\ dvd\ m\ \isasymand \ gcd\ m\ n\ dvd\ n%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2502
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2503
%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2504
Simplification leaves one subgoal: 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2505
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2506
\isacommand{apply}\ (simp_all)\isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2507
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk gcd\ n\ (m\ mod\ n)\ dvd\ n\ \isasymand \ gcd\ n\ (m\ mod\ n)\ dvd\ m\ mod\ n;\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2508
\isaindent{\ 1.\ \isasymAnd m\ n.\ \ }0\ <\ n\isasymrbrakk \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2509
\isaindent{\ 1.\ \isasymAnd m\ n.\ }\isasymLongrightarrow \ gcd\ n\ (m\ mod\ n)\ dvd\ m%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2510
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2511
%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2512
Here, we can use \isa{blast}.  
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2513
One of the assumptions, the induction hypothesis, is a conjunction. 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2514
The two divides relationships it asserts are enough to prove 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2515
the conclusion, for we have the following theorem at our disposal: 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2516
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2517
\isasymlbrakk?k\ dvd\ (?m\ mod\ ?n){;}\ ?k\ dvd\ ?n\isasymrbrakk\ \isasymLongrightarrow\ ?k\ dvd\ ?m%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2518
\rulename{dvd_mod_imp_dvd}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2519
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2520
%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2521
This theorem can be applied in various ways.  As an introduction rule, it
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2522
would cause backward chaining from  the conclusion (namely
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2523
\isa{?k~dvd~?m}) to the two premises, which 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2524
also involve the divides relation. This process does not look promising
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2525
and could easily loop.  More sensible is  to apply the rule in the forward
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2526
direction; each step would eliminate an occurrence of the \isa{mod} symbol, so the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2527
process must terminate.  
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2528
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2529
\isacommand{apply}\ (blast\ dest:\ dvd_mod_imp_dvd)\isanewline
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2530
\isacommand{done}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2531
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2532
Attaching the \attrdx{dest} attribute to \isa{dvd_mod_imp_dvd} tells
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2533
\isa{blast} to use it as destruction rule; that is, in the forward direction.
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2534
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2535
\medskip
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2536
We have proved a conjunction.  Now, let us give names to each of the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2537
two halves:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2538
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2539
\isacommand{lemmas}\ gcd_dvd1\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct1]\isanewline
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2540
\isacommand{lemmas}\ gcd_dvd2\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct2]%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2541
\end{isabelle}
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2542
Here we see \commdx{lemmas}
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2543
used with the \attrdx{iff} attribute, which supplies the new theorems to the
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2544
classical reasoner and the simplifier.  Recall that \attrdx{THEN} is
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2545
frequently used with destruction rules; \isa{THEN conjunct1} extracts the
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2546
first half of a conjunctive theorem.  Given \isa{gcd_dvd_both} it yields
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2547
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2548
\ \ \ \ \ gcd\ ?m1\ ?n1\ dvd\ ?m1
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2549
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2550
The variable names \isa{?m1} and \isa{?n1} arise because
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2551
Isabelle renames schematic variables to prevent 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2552
clashes.  The second \isacommand{lemmas} declaration yields
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2553
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2554
\ \ \ \ \ gcd\ ?m1\ ?n1\ dvd\ ?n1
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2555
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2556
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2557
\medskip
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2558
To complete the verification of the \isa{gcd} function, we must 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2559
prove that it returns the greatest of all the common divisors 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2560
of its arguments.  The proof is by induction, case analysis and simplification.
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2561
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2562
\isacommand{lemma}\ gcd\_greatest\ [rule\_format]:\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2563
\ \ \ \ \ \ "k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ m\ n"
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2564
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2565
%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2566
The goal is expressed using HOL implication,
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2567
\isa{\isasymlongrightarrow}, because the induction affects the two
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2568
preconditions.  The directive \isa{rule_format} tells Isabelle to replace
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2569
each \isa{\isasymlongrightarrow} by \isa{\isasymLongrightarrow} before
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2570
storing the eventual theorem.  This directive can also remove outer
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2571
universal quantifiers, converting the theorem into the usual format for
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2572
inference rules.  It can replace any series of applications of
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2573
\isa{THEN} to the rules \isa{mp} and \isa{spec}.  We did not have to
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2574
write this:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2575
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2576
\isacommand{lemma}\ gcd_greatest\
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2577
[THEN mp, THEN mp]:\isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2578
\ \ \ \ \ \ "k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ m\ n"
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2579
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2580
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2581
Because we are again reasoning about \isa{gcd}, we perform the same
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2582
induction and case analysis as in the previous proof:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2583
\begingroup\samepage
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2584
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2585
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2586
\isaindent{\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk }k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ m\ mod\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ n\ (m\ mod\ n);\isanewline
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2587
\isaindent{\ 1.\ \isasymAnd m\ n.\ \ }n\ =\ 0\isasymrbrakk \isanewline
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2588
\isaindent{\ 1.\ \isasymAnd m\ n.\ }\isasymLongrightarrow \ k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ m\ n\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2589
\ 2.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymLongrightarrow \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2590
\isaindent{\ 2.\ \isasymAnd m\ n.\ \isasymlbrakk }k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ m\ mod\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ n\ (m\ mod\ n);\isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2591
\isaindent{\ 2.\ \isasymAnd m\ n.\ \ }n\ \isasymnoteq \ 0\isasymrbrakk \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2592
\isaindent{\ 2.\ \isasymAnd m\ n.\ }\isasymLongrightarrow \ k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ m\ n%
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2593
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2594
\endgroup
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2595
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2596
\noindent Simplification proves both subgoals. 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2597
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2598
\isacommand{apply}\ (simp_all\ add:\ dvd_mod)\isanewline
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2599
\isacommand{done}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2600
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2601
In the first, where \isa{n=0}, the implication becomes trivial: \isa{k\ dvd\
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2602
gcd\ m\ n} goes to~\isa{k\ dvd\ m}.  The second subgoal is proved by
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2603
an unfolding of \isa{gcd}, using this rule about divides:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2604
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2605
\isasymlbrakk ?f\ dvd\ ?m;\ ?f\ dvd\ ?n\isasymrbrakk \
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2606
\isasymLongrightarrow \ ?f\ dvd\ ?m\ mod\ ?n%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2607
\rulename{dvd_mod}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2608
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2609
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2610
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2611
\medskip
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2612
The facts proved above can be summarized as a single logical 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2613
equivalence.  This step gives us a chance to see another application
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2614
of \isa{blast}.
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2615
\begin{isabelle}
25264
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2616
\isacommand{theorem}\ gcd\_greatest\_iff\ [iff]:\ \isanewline
7007bc8ddae4 recdef to fun
paulson
parents: 25258
diff changeset
  2617
\ \ \ \ \ \ \ \ "(k\ dvd\ gcd\ m\ n)\ =\ (k\ dvd\ m\ \isasymand \ k\ dvd\ n)"\isanewline
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2618
\isacommand{by}\ (blast\ intro!:\ gcd_greatest\ intro:\ dvd_trans)
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2619
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2620
This theorem concisely expresses the correctness of the \isa{gcd} 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2621
function. 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2622
We state it with the \isa{iff} attribute so that 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2623
Isabelle can use it to remove some occurrences of \isa{gcd}. 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2624
The theorem has a one-line 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2625
proof using \isa{blast} supplied with two additional introduction 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2626
rules. The exclamation mark 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2627
({\isa{intro}}{\isa{!}})\ signifies safe rules, which are 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2628
applied aggressively.  Rules given without the exclamation mark 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2629
are applied reluctantly and their uses can be undone if 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2630
the search backtracks.  Here the unsafe rule expresses transitivity  
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2631
of the divides relation:
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2632
\begin{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2633
\isasymlbrakk?m\ dvd\ ?n;\ ?n\ dvd\ ?p\isasymrbrakk\ \isasymLongrightarrow\ ?m\ dvd\ ?p%
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2634
\rulename{dvd_trans}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2635
\end{isabelle}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2636
Applying \isa{dvd_trans} as 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2637
an introduction rule entails a risk of looping, for it multiplies 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2638
occurrences of the divides symbol. However, this proof relies 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2639
on transitivity reasoning.  The rule {\isa{gcd\_greatest}} is safe to apply 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2640
aggressively because it yields simpler subgoals.  The proof implicitly
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2641
uses \isa{gcd_dvd1} and \isa{gcd_dvd2} as safe rules, because they were
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 11077
diff changeset
  2642
declared using \isa{iff}.%
11406
2b17622e1929 indexing and tweaks
paulson
parents: 11300
diff changeset
  2643
\index{Euclid's algorithm|)}\index{*gcd (constant)|)}\index{divides relation|)}