doc-src/TutorialI/Rules/rules.tex
author nipkow
Fri, 03 Nov 2000 17:14:06 +0100
changeset 10373 f9211fc8cd7d
parent 10341 6eb91805a012
child 10399 e37e123738f7
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10341
6eb91805a012 added the $Id:$ line
paulson
parents: 10301
diff changeset
     1
% ID:         $Id$
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     2
\chapter{The Rules of the Game}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     3
\label{chap:rules}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     4
 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     5
Until now, we have proved everything using only induction and simplification.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     6
Substantial proofs require more elaborate forms of inference.  This chapter
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     7
outlines the concepts and techniques that underlie reasoning in Isabelle. The examples
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     8
are mainly drawn from predicate logic.  The first examples in this
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     9
chapter will consist of detailed, low-level proof steps.  Later, we shall
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    10
see how to automate such reasoning using the methods \isa{blast},
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    11
\isa{auto} and others. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    12
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    13
\section{Natural deduction}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    14
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    15
In Isabelle, proofs are constructed using inference rules. The 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    16
most familiar inference rule is probably \emph{modus ponens}: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    17
\[ \infer{Q}{P\imp Q & P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    18
This rule says that from $P\imp Q$ and $P$  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    19
we may infer~$Q$.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    20
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    21
%Early logical formalisms had this  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    22
%rule and at most one or two others, along with many complicated 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    23
%axioms. Any desired theorem could be obtained by applying \emph{modus 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    24
%ponens} or other rules to the axioms, but proofs were 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    25
%hard to find. For example, a standard inference system has 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    26
%these two axioms (amongst others): 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    27
%\begin{gather*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    28
%  P\imp(Q\imp P) \tag{K}\\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    29
%  (P\imp(Q\imp R))\imp ((P\imp Q)\imp(P\imp R))  \tag{S}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    30
%\end{gather*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    31
%Try proving the trivial fact $P\imp P$ using these axioms and \emph{modus
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    32
%ponens}!
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    33
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    34
\textbf{Natural deduction} is an attempt to formalize logic in a way 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    35
that mirrors human reasoning patterns. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    36
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    37
%Instead of having a few 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    38
%inference rules and many axioms, it has many inference rules 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    39
%and few axioms. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    40
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    41
For each logical symbol (say, $\conj$), there 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    42
are two kinds of rules: \textbf{introduction} and \textbf{elimination} rules. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    43
The introduction rules allow us to infer this symbol (say, to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    44
infer conjunctions). The elimination rules allow us to deduce 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    45
consequences from this symbol. Ideally each rule should mention 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    46
one symbol only.  For predicate logic this can be 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    47
done, but when users define their own concepts they typically 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    48
have to refer to other symbols as well.  It is best not be dogmatic.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    49
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    50
Natural deduction generally deserves its name.  It is easy to use.  Each
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    51
proof step consists of identifying the outermost symbol of a formula and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    52
applying the corresponding rule.  It creates new subgoals in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    53
an obvious way from parts of the chosen formula.  Expanding the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    54
definitions of constants can blow up the goal enormously.  Deriving natural
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    55
deduction rules for such constants lets us reason in terms of their key
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    56
properties, which might otherwise be obscured by the technicalities of its
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    57
definition.  Natural deduction rules also lend themselves to automation.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    58
Isabelle's
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    59
\textbf{classical  reasoner} accepts any suitable  collection of natural deduction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    60
rules and uses them to search for proofs automatically.  Isabelle is designed around
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    61
natural deduction and many of its  tools use the terminology of introduction and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    62
elimination rules.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    63
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    64
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    65
\section{Introduction rules}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    66
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    67
An \textbf{introduction} rule tells us when we can infer a formula 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    68
containing a specific logical symbol. For example, the conjunction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    69
introduction rule says that if we have $P$ and if we have $Q$ then 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    70
we have $P\conj Q$. In a mathematics text, it is typically shown 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    71
like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    72
\[  \infer{P\conj Q}{P & Q} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    73
The rule introduces the conjunction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    74
symbol~($\conj$) in its conclusion.  Of course, in Isabelle proofs we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    75
mainly  reason backwards.  When we apply this rule, the subgoal already has
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    76
the form of a conjunction; the proof step makes this conjunction symbol
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    77
disappear. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    78
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    79
In Isabelle notation, the rule looks like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    80
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    81
\isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P\ \isasymand\ ?Q\rulename{conjI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    82
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    83
Carefully examine the syntax.  The premises appear to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    84
left of the arrow and the conclusion to the right.  The premises (if 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    85
more than one) are grouped using the fat brackets.  The question marks
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    86
indicate \textbf{schematic variables} (also called \textbf{unknowns}): they may
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    87
be replaced by arbitrary formulas.  If we use the rule backwards, Isabelle
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    88
tries to unify the current subgoal with the conclusion of the rule, which
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    89
has the form \isa{?P\ \isasymand\ ?Q}.  (Unification is discussed below,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    90
\S\ref{sec:unification}.)  If successful,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    91
it yields new subgoals given by the formulas assigned to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    92
\isa{?P} and \isa{?Q}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    93
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    94
The following trivial proof illustrates this point. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    95
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    96
\isacommand{lemma}\ conj_rule:\ "{\isasymlbrakk}P;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    97
Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\
10301
paulson
parents: 10295
diff changeset
    98
(Q\ \isasymand\ P)"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    99
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   100
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   101
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   102
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   103
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   104
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   105
At the start, Isabelle presents 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   106
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
   107
\isa{P\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   108
(Q\ \isasymand\ P)}.  We are working backwards, so when we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   109
apply conjunction introduction, the rule removes the outermost occurrence
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   110
of the \isa{\isasymand} symbol.  To apply a  rule to a subgoal, we apply
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   111
the proof method {\isa{rule}} --- here with {\isa{conjI}}, the  conjunction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   112
introduction rule. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   113
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   114
%{\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   115
%\isasymand\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   116
\ 1.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   117
\ 2.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\ \isasymand\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   118
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   119
Isabelle leaves two new subgoals: the two halves of the original conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   120
The first is simply \isa{P}, which is trivial, since \isa{P} is among 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   121
the assumptions.  We can apply the {\isa{assumption}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   122
method, which proves a subgoal by finding a matching assumption.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   123
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   124
\ 1.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   125
Q\ \isasymand\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   126
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   127
We are left with the subgoal of proving  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   128
\isa{Q\ \isasymand\ P} from the assumptions \isa{P} and~\isa{Q}.  We apply
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   129
\isa{rule conjI} again. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   130
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   131
\ 1.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   132
\ 2.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   133
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   134
We are left with two new subgoals, \isa{Q} and~\isa{P}, each of which can be proved
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   135
using the {\isa{assumption}} method. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   136
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   137
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   138
\section{Elimination rules}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   139
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   140
\textbf{Elimination} rules work in the opposite direction from introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   141
rules. In the case of conjunction, there are two such rules. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   142
From $P\conj Q$ we infer $P$. also, from $P\conj Q$  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   143
we infer $Q$:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   144
\[ \infer{P}{P\conj Q} \qquad \infer{Q}{P\conj Q}  \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   145
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   146
Now consider disjunction. There are two introduction rules, which resemble inverted forms of the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   147
conjunction elimination rules:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   148
\[ \infer{P\disj Q}{P} \qquad \infer{P\disj Q}{Q}  \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   149
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   150
What is the disjunction elimination rule?  The situation is rather different from 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   151
conjunction.  From $P\disj Q$ we cannot conclude  that $P$ is true and we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   152
cannot conclude that $Q$ is true; there are no direct
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   153
elimination rules of the sort that we have seen for conjunction.  Instead,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   154
there is an elimination  rule that works indirectly.  If we are trying  to prove
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   155
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
   156
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
   157
true and prove $R$ a second  time.  Here we see a fundamental concept used in natural
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   158
deduction:  that of the \textbf{assumptions}. We have to prove $R$ twice, under
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   159
different assumptions.  The assumptions are local to these subproofs and are visible 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   160
nowhere else. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   161
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   162
In a logic text, the disjunction elimination rule might be shown 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   163
like this:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   164
\[ \infer{R}{P\disj Q & \infer*{R}{[P]} & \infer*{R}{[Q]}} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   165
The assumptions $[P]$ and $[Q]$ are bracketed 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   166
to emphasize that they are local to their subproofs.  In Isabelle 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   167
notation, the already-familiar \isa\isasymLongrightarrow syntax serves the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   168
same  purpose:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   169
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   170
\isasymlbrakk?P\ \isasymor\ ?Q;\ ?P\ \isasymLongrightarrow\ ?R;\ ?Q\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulename{disjE}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   171
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   172
When we use this sort of elimination rule backwards, it produces 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   173
a case split.  (We have this before, in proofs by induction.)  The following  proof
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   174
illustrates the use of disjunction elimination.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   175
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
   176
\isacommand{lemma}\ disj_swap:\ "P\ \isasymor\ Q\ 
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   177
\isasymLongrightarrow\ Q\ \isasymor\ P"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   178
\isacommand{apply}\ (erule\ disjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   179
\ \isacommand{apply}\ (rule\ disjI2)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   180
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   181
\isacommand{apply}\ (rule\ disjI1)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   182
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   183
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   184
We assume \isa{P\ \isasymor\ Q} and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   185
must prove \isa{Q\ \isasymor\ P}\@.  Our first step uses the disjunction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   186
elimination rule, \isa{disjE}.  The method {\isa{erule}}  applies an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   187
elimination rule to the assumptions, searching for one that matches the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   188
rule's first premise.  Deleting that assumption, it
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   189
return the subgoals for the remaining premises.  Most of the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   190
time, this is  the best way to use elimination rules; only rarely is there
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   191
any  point in keeping the assumption.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   192
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   193
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   194
%P\ \isasymor\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   195
\ 1.\ P\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   196
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   197
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   198
Here it leaves us with two subgoals.  The first assumes \isa{P} and the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   199
second assumes \isa{Q}.  Tackling the first subgoal, we need to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   200
show \isa{Q\ \isasymor\ P}\@.  The second introduction rule (\isa{disjI2})
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   201
can reduce this  to \isa{P}, which matches the assumption. So, we apply the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   202
{\isa{rule}}  method with \isa{disjI2} \ldots
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   203
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   204
\ 1.\ P\ \isasymLongrightarrow\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   205
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   206
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   207
\ldots and finish off with the {\isa{assumption}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   208
method.  We are left with the other subgoal, which 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   209
assumes \isa{Q}.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   210
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   211
\ 1.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   212
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   213
Its proof is similar, using the introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   214
rule \isa{disjI1}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   215
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   216
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
   217
an introduction nor an elimination rule, but which might 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   218
be useful.  We can use it to replace any goal of the form $Q\disj P$
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   219
by a one of the form $P\disj Q$.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   220
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   221
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   222
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   223
\section{Destruction rules: some examples}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   224
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   225
Now let us examine the analogous proof for conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   226
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
   227
\isacommand{lemma}\ conj_swap:\ "P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   228
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   229
\ \isacommand{apply}\ (drule\ conjunct2)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   230
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   231
\isacommand{apply}\ (drule\ conjunct1)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   232
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   233
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   234
Recall that the conjunction elimination rules --- whose Isabelle names are 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   235
\isa{conjunct1} and \isa{conjunct2} --- simply return the first or second half
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   236
of a conjunction.  Rules of this sort (where the conclusion is a subformula of a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   237
premise) are called \textbf{destruction} rules, by analogy with the destructor
10301
paulson
parents: 10295
diff changeset
   238
functions of functional programming.%
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   239
\footnote{This Isabelle terminology has no counterpart in standard logic texts, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   240
although the distinction between the two forms of elimination rule is well known. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   241
Girard \cite[page 74]{girard89}, for example, writes ``The elimination rules are very
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   242
bad.  What is catastrophic about them is the parasitic presence of a formula [$R$]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   243
which has no structural link with the formula which is eliminated.''}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   244
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   245
The first proof step applies conjunction introduction, leaving 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   246
two subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   247
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   248
%P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   249
\ 1.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   250
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   251
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   252
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   253
To invoke the elimination rule, we apply a new method, \isa{drule}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   254
Think of the \isa{d} as standing for \textbf{destruction} (or \textbf{direct}, if
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   255
you prefer).   Applying the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   256
second conjunction rule using \isa{drule} replaces the assumption 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   257
\isa{P\ \isasymand\ Q} by \isa{Q}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   258
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   259
\ 1.\ Q\ \isasymLongrightarrow\ Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   260
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   261
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   262
The resulting subgoal can be proved by applying \isa{assumption}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   263
The other subgoal is similarly proved, using the \isa{conjunct1} rule and the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   264
\isa{assumption} method.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   265
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   266
Choosing among the methods \isa{rule}, \isa{erule} and \isa{drule} is up to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   267
you.  Isabelle does not attempt to work out whether a rule 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   268
is an introduction rule or an elimination rule.  The 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   269
method determines how the rule will be interpreted. Many rules 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   270
can be used in more than one way.  For example, \isa{disj_swap} can 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   271
be applied to assumptions as well as to goals; it replaces any
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   272
assumption of the form
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   273
$P\disj Q$ by a one of the form $Q\disj P$.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   274
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   275
Destruction rules are simpler in form than indirect rules such as \isa{disjE},
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   276
but they can be inconvenient.  Each of the conjunction rules discards half 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   277
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
   278
assumptions.  The easiest way to do so is by using an 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   279
alternative conjunction elimination rule that resembles \isa{disjE}.  It is seldom,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   280
if ever, seen in logic books.  In Isabelle syntax it looks like this: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   281
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   282
\isasymlbrakk?P\ \isasymand\ ?Q;\ \isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulename{conjE}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   283
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   284
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   285
\begin{exercise}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   286
Use the rule {\isa{conjE}} to shorten the proof above. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   287
\end{exercise}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   288
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   289
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   290
\section{Implication}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   291
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   292
At the start of this chapter, we saw the rule \textit{modus ponens}.  It is, in fact,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   293
a destruction rule. The matching introduction rule looks like this 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   294
in Isabelle: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   295
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   296
(?P\ \isasymLongrightarrow\ ?Q)\ \isasymLongrightarrow\ ?P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   297
\isasymlongrightarrow\ ?Q\rulename{impI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   298
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   299
And this is \textit{modus ponens}:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   300
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   301
\isasymlbrakk?P\ \isasymlongrightarrow\ ?Q;\ ?P\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   302
\isasymLongrightarrow\ ?Q
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   303
\rulename{mp}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   304
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   305
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   306
Here is a proof using the rules for implication.  This 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   307
lemma performs a sort of uncurrying, replacing the two antecedents 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   308
of a nested implication by a conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   309
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   310
\isacommand{lemma}\ imp_uncurry:\
10301
paulson
parents: 10295
diff changeset
   311
"P\ \isasymlongrightarrow\ (Q\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   312
\isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   313
\isasymand\ Q\ \isasymlongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   314
R"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   315
\isacommand{apply}\ (rule\ impI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   316
\isacommand{apply}\ (erule\ conjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   317
\isacommand{apply}\ (drule\ mp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   318
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   319
\isacommand{apply}\ (drule\ mp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   320
\ \ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   321
\ \isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   322
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   323
First, we state the lemma and apply implication introduction (\isa{rule impI}), 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   324
which moves the conjunction to the assumptions. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   325
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   326
%P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R\ \isasymLongrightarrow\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   327
%\isasymand\ Q\ \isasymlongrightarrow\ R\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   328
\ 1.\ {\isasymlbrakk}P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P\ \isasymand\ Q\isasymrbrakk\ \isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   329
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   330
Next, we apply conjunction elimination (\isa{erule conjE}), which splits this
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   331
conjunction into two  parts. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   332
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   333
\ 1.\ {\isasymlbrakk}P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   334
Q\isasymrbrakk\ \isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   335
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   336
Now, we work on the assumption \isa{P\ \isasymlongrightarrow\ (Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   337
\isasymlongrightarrow\ R)}, where the parentheses have been inserted for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   338
clarity.  The nested implication requires two applications of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   339
\textit{modus ponens}: \isa{drule mp}.  The first use  yields the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   340
implication \isa{Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   341
\isasymlongrightarrow\ R}, but first we must prove the extra subgoal 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   342
\isa{P}, which we do by assumption. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   343
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   344
\ 1.\ {\isasymlbrakk}P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   345
\ 2.\ {\isasymlbrakk}P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\ \isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   346
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   347
Repeating these steps for \isa{Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   348
\isasymlongrightarrow\ R} yields the conclusion we seek, namely~\isa{R}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   349
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   350
\ 1.\ {\isasymlbrakk}P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   351
\isasymLongrightarrow\ R
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   352
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   353
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   354
The symbols \isa{\isasymLongrightarrow} and \isa{\isasymlongrightarrow}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   355
both stand for implication, but they differ in many respects.  Isabelle
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   356
uses \isa{\isasymLongrightarrow} to express inference rules; the symbol is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   357
built-in and Isabelle's inference mechanisms treat it specially.  On the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   358
other hand, \isa{\isasymlongrightarrow} is just one of the many connectives
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   359
available in higher-order logic.  We reason about it using inference rules
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   360
such as \isa{impI} and \isa{mp}, just as we reason about the other
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   361
connectives.  You will have to use \isa{\isasymlongrightarrow} in any
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   362
context that requires a formula of higher-order logic.  Use
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   363
\isa{\isasymLongrightarrow} to separate a theorem's preconditions from its
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   364
conclusion.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   365
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   366
When using induction, often the desired theorem results in an induction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   367
hypothesis that is too weak.  In such cases you may have to invent a more
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   368
complicated induction formula, typically involving
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   369
\isa{\isasymlongrightarrow} and \isa{\isasymforall}.  From this lemma you
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   370
derive the desired theorem , typically involving
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   371
\isa{\isasymLongrightarrow}.  We shall see an example below,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   372
\S\ref{sec:proving-euclid}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   373
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   374
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   375
\section{Unification and substitution}\label{sec:unification}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   376
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   377
As we have seen, Isabelle rules involve variables that begin  with a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   378
question mark. These are called \textbf{schematic} variables  and act as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   379
placeholders for terms. \textbf{Unification} refers to  the process of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   380
making two terms identical, possibly by replacing  their variables by
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   381
terms. The simplest case is when the two terms  are already the same. Next
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   382
simplest is when the variables in only one of the term
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   383
 are replaced; this is called \textbf{pattern-matching}.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   384
{\isa{rule}} method typically  matches the rule's conclusion
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   385
against the current subgoal.  In the most complex case,  variables in both
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   386
terms are replaced; the {\isa{rule}} method can do this the goal
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   387
itself contains schematic variables.  Other occurrences of the variables in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   388
the rule or proof state are updated at the same time.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   389
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   390
Schematic variables in goals are sometimes called \textbf{unknowns}.  They
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   391
are useful because they let us proceed with a proof even  when we do not
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   392
know what certain terms should be --- as when the goal is $\exists x.\,P$. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   393
They can be  filled in later, often automatically. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   394
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   395
 Unification is well known to Prolog programmers. Isabelle uses \textbf{higher-order} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   396
unification, which is unification in the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   397
typed $\lambda$-calculus.  The general case is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   398
undecidable, but for our purposes, the differences from ordinary
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   399
unification are straightforward.  It handles bound  variables
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   400
correctly, avoiding capture.  The two terms \isa{{\isasymlambda}x.\ ?P} and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   401
\isa{{\isasymlambda}x.\ t x}  are not unifiable; replacing \isa{?P} by
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   402
\isa{t x} is forbidden because the free occurrence of~\isa{x} would become
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   403
bound.  The two terms
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   404
\isa{{\isasymlambda}x.\ f(x,z)} and \isa{{\isasymlambda}y.\ f(y,z)} are
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   405
trivially unifiable because they differ only by a bound variable renaming.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   406
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   407
Higher-order unification sometimes must invent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   408
$\lambda$-terms to replace function  variables,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   409
which can lead to a combinatorial explosion. However,  Isabelle proofs tend
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   410
to involve easy cases where there are few possibilities for the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   411
$\lambda$-term being constructed. In the easiest case, the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   412
function variable is applied only to bound variables, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   413
as when we try to unify \isa{{\isasymlambda}x\ y.\ f(?h x y)} and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   414
\isa{{\isasymlambda}x\ y.\ f(x+y+a)}.  The only solution is to replace
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   415
\isa{?h} by \isa{{\isasymlambda}x\ y.\ x+y+a}.  Such cases admit at most
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   416
one unifier, like ordinary unification.  A harder case is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   417
unifying \isa{?h a} with~\isa{a+b}; it admits two solutions for \isa{?h},
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   418
namely \isa{{\isasymlambda}x.~a+b} and \isa{{\isasymlambda}x.~x+b}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   419
Unifying \isa{?h a} with~\isa{a+a+b} admits four solutions; their number is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   420
exponential in the number of occurrences of~\isa{a} in the second term.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   421
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   422
Isabelle also uses function variables to express \textbf{substitution}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   423
A typical substitution rule allows us to replace one term by 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   424
another if we know that two terms are equal. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   425
\[ \infer{P[t/x]}{s=t & P[s/x]} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   426
The conclusion uses a notation for substitution: $P[t/x]$ is the result of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   427
replacing $x$ by~$t$ in~$P$.  The rule only substitutes in the positions
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   428
designated by~$x$, which gives it additional power. For example, it can
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   429
derive symmetry of equality from reflexivity.  Using $x=s$ for~$P$
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   430
replaces just the first $s$ in $s=s$ by~$t$.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   431
\[ \infer{t=s}{s=t & \infer{s=s}{}} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   432
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   433
The Isabelle version of the substitution rule looks like this: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   434
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   435
\isasymlbrakk?t\ =\ ?s;\ ?P\ ?s\isasymrbrakk\ \isasymLongrightarrow\ ?P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   436
?t
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   437
\rulename{ssubst}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   438
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   439
Crucially, \isa{?P} is a function 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   440
variable: it can be replaced by a $\lambda$-expression 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   441
involving one bound variable whose occurrences identify the places 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   442
in which $s$ will be replaced by~$t$.  The proof above requires
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   443
\isa{{\isasymlambda}x.~x=s}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   444
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   445
The \isa{simp} method replaces equals by equals, but using the substitution
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   446
rule gives us more control. Consider this proof: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   447
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   448
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   449
"{\isasymlbrakk}\ x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   450
=\ f\ x;\ odd(f\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   451
x)\ \isasymrbrakk\ \isasymLongrightarrow\ odd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   452
x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   453
\isacommand{apply}\ (erule\ ssubst)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   454
\isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   455
\isacommand{done}\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   456
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   457
The simplifier might loop, replacing \isa{x} by \isa{f x} and then by
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   458
\isa{f(f x)} and so forth. (Actually, \isa{simp} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   459
sees the danger and re-orients this equality, but in more complicated cases
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   460
it can be fooled.) When we apply substitution,  Isabelle replaces every
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   461
\isa{x} in the subgoal by \isa{f x} just once: it cannot loop.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   462
resulting subgoal is trivial by assumption. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   463
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   464
We are using the \isa{erule} method it in a novel way. Hitherto, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   465
the conclusion of the rule was just a variable such as~\isa{?R}, but it may
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   466
be any term. The conclusion is unified with the subgoal just as 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   467
it would be with the \isa{rule} method. At the same time \isa{erule} looks 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   468
for an assumption that matches the rule's first premise, as usual.  With
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   469
\isa{ssubst} the effect is to find, use and delete an equality 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   470
assumption.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   471
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   472
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   473
Higher-order unification can be tricky, as this example indicates: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   474
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   475
\isacommand{lemma}\ "{\isasymlbrakk}\ x\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   476
f\ x;\ triple\ (f\ x)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   477
(f\ x)\ x\ \isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   478
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   479
\isacommand{apply}\ (erule\ ssubst)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   480
\isacommand{back}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   481
\isacommand{back}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   482
\isacommand{back}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   483
\isacommand{back}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   484
\isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   485
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   486
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   487
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   488
By default, Isabelle tries to substitute for all the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   489
occurrences.  Applying \isa{erule\ ssubst} yields this subgoal:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   490
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   491
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ (f\ x)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   492
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   493
The substitution should have been done in the first two occurrences 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   494
of~\isa{x} only. Isabelle has gone too far. The \isa{back} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   495
method allows us to reject this possibility and get a new one: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   496
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   497
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ (f\ x)\ (f\ x)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   498
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   499
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   500
Now Isabelle has left the first occurrence of~\isa{x} alone. That is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   501
promising but it is not the desired combination. So we use \isa{back} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   502
again:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   503
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   504
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ x\ (f\ x)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   505
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   506
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   507
This also is wrong, so we use \isa{back} again: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   508
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   509
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ x\ (f\ x)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   510
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   511
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   512
And this one is wrong too. Looking carefully at the series 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   513
of alternatives, we see a binary countdown with reversed bits: 111,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   514
011, 101, 001.  Invoke \isa{back} again: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   515
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   516
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ x%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   517
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   518
At last, we have the right combination!  This goal follows by assumption.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   519
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   520
Never use {\isa{back}} in the final version of a proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   521
It should only be used for exploration. One way to get rid of {\isa{back}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   522
to combine two methods in a single \textbf{apply} command. Isabelle 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   523
applies the first method and then the second. If the second method 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   524
fails then Isabelle automatically backtracks. This process continues until 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   525
the first method produces an output that the second method can 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   526
use. We get a one-line proof of our example: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   527
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   528
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   529
"{\isasymlbrakk}\ x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   530
=\ f\ x;\ triple\ (f\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   531
x)\ (f\ x)\ x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   532
\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   533
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   534
\isacommand{apply}\ (erule\ ssubst,\ assumption)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   535
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   536
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   537
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   538
The most general way to get rid of the {\isa{back}} command is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   539
to instantiate variables in the rule.  The method {\isa{rule\_tac}} is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   540
similar to \isa{rule}, but it
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   541
makes some of the rule's variables  denote specified terms.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   542
Also available are {\isa{drule\_tac}}  and \isa{erule\_tac}.  Here we need
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   543
\isa{erule\_tac} since above we used
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   544
\isa{erule}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   545
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   546
\isacommand{lemma}\ "{\isasymlbrakk}\ x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\ \isasymrbrakk\ \isasymLongrightarrow\ triple\ x\ x\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   547
\isacommand{apply}\ (erule_tac\
10301
paulson
parents: 10295
diff changeset
   548
P="{\isasymlambda}u.\ triple\ u\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   549
u\ x"\ \isakeyword{in}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   550
ssubst)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   551
\isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   552
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   553
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   554
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   555
To specify a desired substitution 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   556
requires instantiating the variable \isa{?P} with a $\lambda$-expression. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   557
The bound variable occurrences in \isa{{\isasymlambda}u.\ P\ u\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   558
u\ x} indicate that the first two arguments have to be substituted, leaving
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   559
the third unchanged.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   560
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   561
An alternative to {\isa{rule\_tac}} is to use \isa{rule} with the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   562
{\isa{of}}  directive, described in \S\ref{sec:forward} below.   An
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   563
advantage  of {\isa{rule\_tac}} is that the instantiations may refer to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   564
variables bound in the current subgoal.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   565
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   566
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   567
\section{Negation}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   568
 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   569
Negation causes surprising complexity in proofs.  Its natural 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   570
deduction rules are straightforward, but additional rules seem 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   571
necessary in order to handle negated assumptions gracefully. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   572
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   573
Negation introduction deduces $\neg P$ if assuming $P$ leads to a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   574
contradiction. Negation elimination deduces any formula in the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   575
presence of $\neg P$ together with~$P$: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   576
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   577
(?P\ \isasymLongrightarrow\ False)\ \isasymLongrightarrow\ \isasymnot\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   578
\rulename{notI}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   579
\isasymlbrakk{\isasymnot}\ ?P;\ ?P\isasymrbrakk\ \isasymLongrightarrow\ ?R%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   580
\rulename{notE}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   581
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   582
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   583
Classical logic allows us to assume $\neg P$ 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   584
when attempting to prove~$P$: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   585
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   586
(\isasymnot\ ?P\ \isasymLongrightarrow\ ?P)\ \isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   587
\rulename{classical}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   588
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   589
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   590
Three further rules are variations on the theme of contrapositive. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   591
They differ in the placement of the negation symbols: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   592
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   593
\isasymlbrakk?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ \isasymnot\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   594
\rulename{contrapos_pp}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   595
\isasymlbrakk{\isasymnot}\ ?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   596
\rulename{contrapos_np}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   597
\isasymlbrakk{\isasymnot}\ ?Q;\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ \isasymnot\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   598
\rulename{contrapos_nn}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   599
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   600
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   601
These rules are typically applied using the {\isa{erule}} method, where 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   602
their effect is to form a contrapositive from an 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   603
assumption and the goal's conclusion.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   604
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   605
The most important of these is \isa{contrapos_np}.  It is useful
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   606
for applying introduction rules to negated assumptions.  For instance, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   607
the assumption $\neg(P\imp Q)$ is equivalent to the conclusion $P\imp Q$ and we 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   608
might want to use conjunction introduction on it. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   609
Before we can do so, we must move that assumption so that it 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   610
becomes the conclusion. The following proof demonstrates this 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   611
technique: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   612
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   613
\isacommand{lemma}\ "\isasymlbrakk{\isasymnot}(P{\isasymlongrightarrow}Q);\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   614
\isasymnot(R{\isasymlongrightarrow}Q)\isasymrbrakk\ \isasymLongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   615
R"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   616
\isacommand{apply}\ (erule_tac\ Q="R{\isasymlongrightarrow}Q"\ \isakeyword{in}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   617
contrapos_np)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   618
\isacommand{apply}\ intro\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   619
\isacommand{apply}\ (erule\ notE,\ assumption)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   620
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   621
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   622
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   623
There are two negated assumptions and we need to exchange the conclusion with the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   624
second one.  The method \isa{erule contrapos_np} would select the first assumption,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   625
which we do not want.  So we specify the desired assumption explicitly, using
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   626
\isa{erule_tac}.  This is the resulting subgoal: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   627
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   628
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   629
R\isasymrbrakk\ \isasymLongrightarrow\ R\ \isasymlongrightarrow\ Q%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   630
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   631
The former conclusion, namely \isa{R}, now appears negated among the assumptions,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   632
while the negated formula \isa{R\ \isasymlongrightarrow\ Q} becomes the new
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   633
conclusion.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   634
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   635
We can now apply introduction rules.  We use the {\isa{intro}} method, which
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   636
repeatedly  applies built-in introduction rules.  Here its effect is equivalent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   637
to \isa{rule impI}.\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   638
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\ R;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   639
R\isasymrbrakk\ \isasymLongrightarrow\ Q%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   640
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   641
We can see a contradiction in the form of assumptions \isa{\isasymnot\ R}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   642
and~\isa{R}, which suggests using negation elimination.  If applied on its own,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   643
however, it will select the first negated assumption, which is useless.   Instead,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   644
we combine the rule with  the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   645
\isa{assumption} method:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   646
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   647
\ \ \ \ \ (erule\ notE,\ assumption)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   648
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   649
Now when Isabelle selects the first assumption, it tries to prove \isa{P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   650
\isasymlongrightarrow\ Q} and fails; it then backtracks, finds the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   651
assumption~\isa{\isasymnot\ R} and finally proves \isa{R} by assumption.  That
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   652
concludes the proof.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   653
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   654
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   655
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   656
Here is another example. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   657
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   658
\isacommand{lemma}\ "(P\ \isasymor\ Q)\ \isasymand\ R\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   659
\isasymLongrightarrow\ P\ \isasymor\ Q\ \isasymand\ R"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   660
\isacommand{apply}\ intro%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   661
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   662
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   663
\isacommand{apply}\ (elim\ conjE\ disjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   664
\ \isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   665
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   666
\isacommand{apply}\ (erule\ contrapos_np,\ rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   667
\ \ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   668
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   669
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   670
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   671
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   672
The first proof step applies the {\isa{intro}} method, which repeatedly 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   673
uses built-in introduction rules.  Here it creates the negative assumption \isa{\isasymnot\ (Q\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   674
R)}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   675
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   676
\ 1.\ \isasymlbrakk(P\ \isasymor\ Q)\ \isasymand\ R;\ \isasymnot\ (Q\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   677
R)\isasymrbrakk\ \isasymLongrightarrow\ P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   678
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   679
It comes from \isa{disjCI},  a disjunction introduction rule that is more
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   680
powerful than the separate rules  \isa{disjI1} and  \isa{disjI2}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   681
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   682
Next we apply the {\isa{elim}} method, which repeatedly applies 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   683
elimination rules; here, the elimination rules given 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   684
in the command.  One of the subgoals is trivial, leaving us with one other:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   685
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   686
\ 1.\ \isasymlbrakk{\isasymnot}\ (Q\ \isasymand\ R);\ R;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   687
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   688
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   689
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
   690
combination 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   691
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   692
\ \ \ \ \ (erule\ contrapos_np,\ rule\ conjI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   693
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   694
is robust: the \isa{conjI} forces the \isa{erule} to select a
10301
paulson
parents: 10295
diff changeset
   695
conjunction.  The two subgoals are the ones we would expect from applying
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   696
conjunction introduction to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   697
\isa{Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   698
\isasymand\ R}:  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   699
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   700
\ 1.\ {\isasymlbrakk}R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   701
Q\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   702
\ 2.\ {\isasymlbrakk}R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\ R%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   703
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   704
The rest of the proof is trivial.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   705
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   706
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   707
\section{The universal quantifier}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   708
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   709
Quantifiers require formalizing syntactic substitution and the notion of \textbf{arbitrary
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   710
value}.  Consider the universal quantifier.  In a logic book, its
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   711
introduction  rule looks like this: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   712
\[ \infer{\forall x.\,P}{P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   713
Typically, a proviso written in English says that $x$ must not
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   714
occur in the assumptions.  This proviso guarantees that $x$ can be regarded as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   715
arbitrary, since it has not been assumed to satisfy any special conditions. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   716
Isabelle's  underlying formalism, called the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   717
\textbf{meta-logic}, eliminates the  need for English.  It provides its own universal
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   718
quantifier (\isasymAnd) to express the notion of an arbitrary value.  We have
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   719
already seen  another symbol of the meta-logic, namely
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   720
\isa\isasymLongrightarrow, which expresses  inference rules and the treatment of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   721
assumptions. The only other  symbol in the meta-logic is \isa\isasymequiv, which
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   722
can be used to define constants.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   723
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   724
Returning to the universal quantifier, we find that having a similar quantifier
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   725
as part of the meta-logic makes the introduction rule trivial to express:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   726
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   727
({\isasymAnd}x.\ ?P\ x)\ \isasymLongrightarrow\ {\isasymforall}x.\ ?P\ x\rulename{allI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   728
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   729
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   730
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   731
The following trivial proof demonstrates how the universal introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   732
rule works. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   733
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   734
\isacommand{lemma}\ "{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   735
\isacommand{apply}\ (rule\ allI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   736
\isacommand{apply}\ (rule\ impI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   737
\isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   738
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   739
The first step invokes the rule by applying the method \isa{rule allI}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   740
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   741
%{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ x\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   742
\ 1.\ {\isasymAnd}x.\ P\ x\ \isasymlongrightarrow\ P\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   743
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   744
Note  that the resulting proof state has a bound variable,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   745
namely~\bigisa{x}.  The rule has replaced the universal quantifier of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   746
higher-order  logic by Isabelle's meta-level quantifier.  Our goal is to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   747
prove
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   748
\isa{P\ x\ \isasymlongrightarrow\ P\ x} for arbitrary~\isa{x}; it is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   749
an implication, so we apply the corresponding introduction rule (\isa{impI}). 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   750
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   751
\ 1.\ {\isasymAnd}x.\ P\ x\ \isasymLongrightarrow\ P\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   752
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   753
The {\isa{assumption}} method proves this last subgoal. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   754
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   755
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   756
Now consider universal elimination. In a logic text, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   757
the rule looks like this: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   758
\[ \infer{P[t/x]}{\forall x.\,P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   759
The conclusion is $P$ with $t$ substituted for the variable~$x$.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   760
Isabelle expresses substitution using a function variable: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   761
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   762
{\isasymforall}x.\ ?P\ x\ \isasymLongrightarrow\ ?P\ ?x\rulename{spec}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   763
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   764
This destruction rule takes a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   765
universally quantified formula and removes the quantifier, replacing 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   766
the bound variable \bigisa{x} by the schematic variable \bigisa{?x}.  Recall that a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   767
schematic variable starts with a question mark and acts as a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   768
placeholder: it can be replaced by any term. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   769
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   770
To see how this works, let us derive a rule about reducing 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   771
the scope of a universal quantifier.  In mathematical notation we write
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   772
\[ \infer{P\imp\forall x.\,Q}{\forall x.\,P\imp Q} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   773
with the proviso `$x$ not free in~$P$.'  Isabelle's treatment of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   774
substitution makes the proviso unnecessary.  The conclusion is expressed as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   775
\isa{P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   776
\isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)}. No substitution for the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   777
variable \isa{P} can introduce a dependence upon~\isa{x}: that would be a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   778
bound variable capture.  Here is the isabelle proof in full:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   779
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   780
\isacommand{lemma}\ "({\isasymforall}x.\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   781
\isasymlongrightarrow\ Q\ x)\ \isasymLongrightarrow\ P\
10301
paulson
parents: 10295
diff changeset
   782
\isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   783
\isacommand{apply}\ (rule\ impI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   784
\isacommand{apply}\ (rule\ allI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   785
\isacommand{apply}\ (drule\ spec)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   786
\isacommand{apply}\ (drule\ mp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   787
\ \ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   788
\ \isacommand{apply}\ assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   789
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   790
First we apply implies introduction (\isa{rule impI}), 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   791
which moves the \isa{P} from the conclusion to the assumptions. Then 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   792
we apply universal introduction (\isa{rule allI}).  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   793
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   794
%{\isasymforall}x.\ P\ \isasymlongrightarrow\ Q\ x\ \isasymLongrightarrow\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   795
%\isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   796
\ 1.\ {\isasymAnd}x.\ \isasymlbrakk{\isasymforall}x.\ P\ \isasymlongrightarrow\ Q\ x;\ P\isasymrbrakk\ \isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   797
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   798
As before, it replaces the HOL 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   799
quantifier by a meta-level quantifier, producing a subgoal that 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   800
binds the variable~\bigisa{x}.  The leading bound variables
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   801
(here \isa{x}) and the assumptions (here \isa{{\isasymforall}x.\ P\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   802
\isasymlongrightarrow\ Q\ x} and \isa{P}) form the \textbf{context} for the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   803
conclusion, here \isa{Q\ x}.  At each proof step, the subgoals inherit the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   804
previous context, though some context elements may be added or deleted. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   805
Applying \isa{erule} deletes an assumption, while many natural deduction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   806
rules add bound variables or assumptions.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   807
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   808
Now, to reason from the universally quantified 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   809
assumption, we apply the elimination rule using the {\isa{drule}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   810
method.  This rule is called \isa{spec} because it specializes a universal formula
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   811
to a particular term.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   812
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   813
\ 1.\ {\isasymAnd}x.\ {\isasymlbrakk}P;\ P\ \isasymlongrightarrow\ Q\ (?x2\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   814
x){\isasymrbrakk}\ \isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   815
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   816
Observe how the context has changed.  The quantified formula is gone,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   817
replaced by a new assumption derived from its body.  Informally, we have
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   818
removed the quantifier.  The quantified variable
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   819
has been replaced by the curious term 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   820
\bigisa{?x2~x}; it acts as a placeholder that may be replaced 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   821
by any term that can be built up from~\bigisa{x}.  (Formally, \bigisa{?x2} is an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   822
unknown of function type, applied to the argument~\bigisa{x}.)  This new assumption is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   823
an implication, so we can  use \emph{modus ponens} on it. As before, it requires
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   824
proving the  antecedent (in this case \isa{P}) and leaves us with the consequent. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   825
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   826
\ 1.\ {\isasymAnd}x.\ {\isasymlbrakk}P;\ Q\ (?x2\ x){\isasymrbrakk}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   827
\isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   828
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   829
The consequent is \isa{Q} applied to that placeholder.  It may be replaced by any
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   830
term built from~\bigisa{x}, and here 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   831
it should simply be~\bigisa{x}.  The \isa{assumption} method will do this.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   832
The assumption need not be identical to the conclusion, provided the two formulas are
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   833
unifiable.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   834
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   835
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   836
Note that \isa{drule spec} removes the universal quantifier and --- as
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   837
usual with elimination rules --- discards the original formula.  Sometimes, a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   838
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
   839
method: \isa{frule}.  It acts like \isa{drule} but copies rather than replaces
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   840
the selected assumption.  The \isa{f} is for `forward.'
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   841
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   842
In this example, we intuitively see that to go from \isa{P\ a} to \isa{P(f\ (f\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   843
a))} requires two uses of the quantified assumption, one for each
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   844
additional~\isa{f}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   845
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   846
\isacommand{lemma}\ "\isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (f\ x);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   847
\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P(f\ (f\ a))"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   848
\isacommand{apply}\ (frule\ spec)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   849
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   850
\isacommand{apply}\ (drule\ spec)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   851
\isacommand{apply}\ (drule\ mp,\ assumption,\ assumption)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   852
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   853
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   854
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   855
Applying \isa{frule\ spec} leaves this subgoal:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   856
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   857
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (f\ x);\ P\ a;\ P\ ?x\ \isasymlongrightarrow\ P\ (f\ ?x)\isasymrbrakk\ \isasymLongrightarrow\ P\ (f\ (f\ a))
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   858
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   859
It is just what  \isa{drule} would have left except that the quantified
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   860
assumption is still present.  The next step is to apply \isa{mp} to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   861
implication and the assumption \isa{P\ a}, which leaves this subgoal:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   862
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   863
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (f\ x);\ P\ a;\ P\ (f\ a)\isasymrbrakk\ \isasymLongrightarrow\ P\ (f\ (f\ a))
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   864
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   865
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   866
We have created the assumption \isa{P(f\ a)}, which is progress.  To finish the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   867
proof, we apply \isa{spec} one last time, using \isa{drule}.  One final trick: if
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   868
we then apply
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   869
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   870
\ \ \ \ \ (drule\ mp,\ assumption)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   871
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   872
it will add a second copy of \isa{P(f\ a)} instead of the desired \isa{P(f\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   873
(f\ a))}.  Bundling both \isa{assumption} calls with \isa{drule mp} causes
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   874
Isabelle to backtrack and find the correct one.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   875
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   876
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   877
\section{The existential quantifier}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   878
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   879
The concepts just presented also apply to the existential quantifier,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   880
whose introduction rule looks like this in Isabelle: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   881
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   882
?P\ ?x\ \isasymLongrightarrow\ {\isasymexists}x.\ ?P\ x\rulename{exI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   883
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   884
If we can exhibit some $x$ such that $P(x)$ is true, then $\exists x.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   885
P(x)$ is also true. It is essentially a dual of the universal elimination rule, and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   886
logic texts present it using the same notation for substitution.  The existential
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   887
elimination rule looks like this
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   888
in a logic text: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   889
\[ \infer{R}{\exists x.\,P & \infer*{R}{[P]}} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   890
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   891
It looks like this in Isabelle: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   892
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   893
\isasymlbrakk{\isasymexists}x.\ ?P\ x;\ {\isasymAnd}x.\ ?P\ x\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?Q\rulename{exE}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   894
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   895
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   896
Given an existentially quantified theorem and some
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   897
formula $Q$ to prove, it creates a new assumption by removing the quantifier.  As with
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   898
the universal introduction  rule, the textbook version imposes a proviso on the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   899
quantified variable, which Isabelle expresses using its meta-logic.  Note that it is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   900
enough to have a universal quantifier in the meta-logic; we do not need an existential
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   901
quantifier to be built in as well.\remark{EX example needed?}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   902
 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   903
Isabelle/HOL also provides Hilbert's
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   904
$\epsilon$-operator.  The term $\epsilon x. P(x)$ denotes some $x$ such that $P(x)$ is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   905
true, provided such a value exists.  Using this operator, we can express an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   906
existential destruction rule:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   907
\[ \infer{P[(\epsilon x. P) / \, x]}{\exists x.\,P} \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   908
This rule is seldom used, for it can cause exponential blow-up.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   909
main use of $\epsilon x. P(x)$ is in definitions when $P(x)$ characterizes $x$
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   910
uniquely.  For instance, we can define the cardinality of a finite set~$A$ to be that
10301
paulson
parents: 10295
diff changeset
   911
$n$ such that $A$ is in one-to-one correspondence with $\{1,\ldots,n\}$.  We can then
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   912
prove that the cardinality of the empty set is zero (since $n=0$ satisfies the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   913
description) and proceed to prove other facts.\remark{SOME theorems
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   914
and example}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   915
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   916
\begin{exercise}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   917
Prove the lemma
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   918
\[ \exists x.\, P\conj Q(x)\Imp P\conj(\exists x.\, Q(x)). \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   919
\emph{Hint}: the proof is similar 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   920
to the one just above for the universal quantifier. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   921
\end{exercise}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   922
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   923
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   924
\section{Some proofs that fail}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   925
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   926
Most of the examples in this tutorial involve proving theorems.  But not every 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   927
conjecture is true, and it can be instructive to see how  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   928
proofs fail. Here we attempt to prove a distributive law involving 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   929
the existential quantifier and conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   930
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   931
\isacommand{lemma}\ "({\isasymexists}x.\ P\ x)\ \isasymand\ ({\isasymexists}x.\ Q\ x)\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   932
\isacommand{apply}\ (erule\ conjE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   933
\isacommand{apply}\ (erule\ exE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   934
\isacommand{apply}\ (erule\ exE)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   935
\isacommand{apply}\ (rule\ exI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   936
\isacommand{apply}\ (rule\ conjI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   937
\ \isacommand{apply}\ assumption\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   938
\isacommand{oops}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   939
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   940
The first steps are  routine.  We apply conjunction elimination (\isa{erule
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   941
conjE}) to split the assumption  in two, leaving two existentially quantified
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   942
assumptions.  Applying existential elimination  (\isa{erule exE}) removes one of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   943
the quantifiers. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   944
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   945
%({\isasymexists}x.\ P\ x)\ \isasymand\ ({\isasymexists}x.\ Q\ x)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   946
%\isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   947
\ 1.\ {\isasymAnd}x.\ \isasymlbrakk{\isasymexists}x.\ Q\ x;\ P\ x\isasymrbrakk\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   948
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   949
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   950
When we remove the other quantifier, we get a different bound 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   951
variable in the subgoal.  (The name \isa{xa} is generated automatically.)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   952
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   953
\ 1.\ {\isasymAnd}x\ xa.\ {\isasymlbrakk}P\ x;\ Q\ xa\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   954
\isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   955
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   956
The proviso of the existential elimination rule has forced the variables to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   957
differ: we can hardly expect two arbitrary values to be equal!  There is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   958
no way to prove this subgoal.  Removing the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   959
conclusion's existential quantifier yields two
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   960
identical placeholders, which can become  any term involving the variables \bigisa{x}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   961
and~\bigisa{xa}.  We need one to become \bigisa{x}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   962
and the other to become~\bigisa{xa}, but Isabelle requires all instances of a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   963
placeholder to be identical. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   964
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   965
\ 1.\ {\isasymAnd}x\ xa.\ {\isasymlbrakk}P\ x;\ Q\ xa\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   966
\isasymLongrightarrow\ P\ (?x3\ x\ xa)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   967
\ 2.\ {\isasymAnd}x\ xa.\ {\isasymlbrakk}P\ x;\ Q\ xa\isasymrbrakk\ \isasymLongrightarrow\ Q\ (?x3\ x\ xa)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   968
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   969
We can prove either subgoal 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   970
using the \isa{assumption} method.  If we prove the first one, the placeholder
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   971
changes  into~\bigisa{x}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   972
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   973
\ 1.\ {\isasymAnd}x\ xa.\ {\isasymlbrakk}P\ x;\ Q\ xa\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   974
\isasymLongrightarrow\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   975
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   976
We are left with a subgoal that cannot be proved, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   977
because there is no way to prove that \bigisa{x}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   978
equals~\bigisa{xa}.  Applying the \isa{assumption} method results in an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   979
error message:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   980
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   981
*** empty result sequence -- proof command failed
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   982
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   983
We can tell Isabelle to abandon a failed proof using the \isacommand{oops} command.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   984
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   985
\medskip 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   986
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   987
Here is another abortive proof, illustrating the interaction between 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   988
bound variables and unknowns.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   989
If $R$ is a reflexive relation, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   990
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
   991
we attempt to prove it. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   992
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   993
\isacommand{lemma}\ "{\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   994
{\isasymexists}x.\ {\isasymforall}y.\ R\ x\ y"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   995
\isacommand{apply}\ (rule\ exI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   996
\isacommand{apply}\ (rule\ allI)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   997
\isacommand{apply}\ (drule\ spec)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   998
\isacommand{oops}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   999
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1000
First, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1001
we remove the existential quantifier. The new proof state has 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1002
an unknown, namely~\bigisa{?x}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1003
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1004
%{\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ {\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1005
%{\isasymforall}y.\ R\ x\ y\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1006
\ 1.\ {\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ {\isasymforall}y.\ R\ ?x\ y
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1007
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1008
Next, we remove the universal quantifier 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1009
from the conclusion, putting the bound variable~\isa{y} into the subgoal. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1010
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1011
\ 1.\ {\isasymAnd}y.\ {\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ R\ ?x\ y
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1012
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1013
Finally, we try to apply our reflexivity assumption.  We obtain a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1014
new assumption whose identical placeholders may be replaced by 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1015
any term involving~\bigisa{y}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1016
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1017
\ 1.\ {\isasymAnd}y.\ R\ (?z2\ y)\ (?z2\ y)\ \isasymLongrightarrow\ R\ ?x\ y
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1018
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1019
This subgoal can only be proved by putting \bigisa{y} for all the placeholders,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1020
making the assumption and conclusion become \isa{R\ y\ y}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1021
But Isabelle refuses to substitute \bigisa{y}, a bound variable, for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1022
\bigisa{?x}; that would be a bound variable capture.  The proof fails.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1023
Note that Isabelle can replace \bigisa{?z2~y} by \bigisa{y}; this involves
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1024
instantiating
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1025
\bigisa{?z2} to the identity function.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1026
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1027
This example is typical of how Isabelle enforces sound quantifier reasoning. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1028
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1029
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1030
\section{Proving theorems using the \emph{\texttt{blast}} method}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1031
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1032
It is hard to prove substantial theorems using the methods 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1033
described above. A proof may be dozens or hundreds of steps long.  You 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1034
may need to search among different ways of proving certain 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1035
subgoals. Often a choice that proves one subgoal renders another 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1036
impossible to prove.  There are further complications that we have not
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1037
discussed, concerning negation and disjunction.  Isabelle's
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1038
\textbf{classical reasoner} is a family of tools that perform such
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1039
proofs automatically.  The most important of these is the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1040
{\isa{blast}} method. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1041
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1042
In this section, we shall first see how to use the classical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1043
reasoner in its default mode and then how to insert additional 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1044
rules, enabling it to work in new problem domains. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1045
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1046
 We begin with examples from pure predicate logic. The following 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1047
example is known as Andrew's challenge. Peter Andrews designed 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1048
it to be hard to prove by automatic means.%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1049
\footnote{Pelletier~\cite{pelletier86} describes it and many other
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1050
problems for automatic theorem provers.}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1051
The nested biconditionals cause an exponential explosion: the formal
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1052
proof is  enormous.  However, the {\isa{blast}} method proves it in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1053
a fraction  of a second. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1054
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1055
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1056
"(({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1057
{\isasymforall}y.\
10301
paulson
parents: 10295
diff changeset
  1058
p(x){=}p(y))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1059
=\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1060
(({\isasymexists}x.\
10301
paulson
parents: 10295
diff changeset
  1061
q(x))=({\isasymforall}y.\
paulson
parents: 10295
diff changeset
  1062
p(y))))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1063
\ \ =\ \ \ \ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1064
\ \ \ \ \ \ \ \
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1065
(({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1066
{\isasymforall}y.\
10301
paulson
parents: 10295
diff changeset
  1067
q(x){=}q(y))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1068
=\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1069
(({\isasymexists}x.\
10301
paulson
parents: 10295
diff changeset
  1070
p(x))=({\isasymforall}y.\
paulson
parents: 10295
diff changeset
  1071
q(y))))"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1072
\isacommand{apply}\ blast\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1073
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1074
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1075
The next example is a logic problem composed by Lewis Carroll. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1076
The {\isa{blast}} method finds it trivial. Moreover, it turns out 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1077
that not all of the assumptions are necessary. We can easily 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1078
experiment with variations of this formula and see which ones 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1079
can be proved. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1080
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1081
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1082
"({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1083
honest(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1084
industrious(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1085
healthy(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1086
\isasymand\ \ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1087
\ \ \ \ \ \ \ \ \isasymnot\ ({\isasymexists}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1088
grocer(x)\ \isasymand\
10301
paulson
parents: 10295
diff changeset
  1089
healthy(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1090
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1091
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1092
industrious(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1093
grocer(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1094
honest(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1095
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1096
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1097
cyclist(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1098
industrious(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1099
\isasymand\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1100
\ \ \ \ \ \ \ \ ({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1101
{\isasymnot}healthy(x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1102
cyclist(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1103
{\isasymnot}honest(x))\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1104
\ \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1105
\ \ \ \ \ \ \ \ \isasymlongrightarrow\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1106
({\isasymforall}x.\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1107
grocer(x)\ \isasymlongrightarrow\
10301
paulson
parents: 10295
diff changeset
  1108
{\isasymnot}cyclist(x))"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1109
\isacommand{apply}\ blast\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1110
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1111
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1112
The {\isa{blast}} method is also effective for set theory, which is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1113
described in the next chapter.  This formula below may look horrible, but
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1114
the \isa{blast} method proves it easily. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1115
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1116
\isacommand{lemma}\ "({\isasymUnion}i{\isasymin}I.\ A(i))\ \isasyminter\ ({\isasymUnion}j{\isasymin}J.\ B(j))\ =\isanewline
paulson
parents: 10295
diff changeset
  1117
\ \ \ \ \ \ \ \ ({\isasymUnion}i{\isasymin}I.\ {\isasymUnion}j{\isasymin}J.\ A(i)\ \isasyminter\ B(j))"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1118
\isacommand{apply}\ blast\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1119
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1120
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1121
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1122
Few subgoals are couched purely in predicate logic and set theory.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1123
We can extend the scope of the classical reasoner by giving it new rules. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1124
Extending it effectively requires understanding the notions of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1125
introduction, elimination and destruction rules.  Moreover, there is a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1126
distinction between  safe and unsafe rules. A \textbf{safe} rule is one
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1127
that can be applied  backwards without losing information; an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1128
\textbf{unsafe} rule loses  information, perhaps transforming the subgoal
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1129
into one that cannot be proved.  The safe/unsafe
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1130
distinction affects the proof search: if a proof attempt fails, the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1131
classical reasoner backtracks to the most recent unsafe rule application
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1132
and makes another choice. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1133
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1134
An important special case avoids all these complications.  A logical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1135
equivalence, which in higher-order logic is an equality between 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1136
formulas, can be given to the classical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1137
reasoner and simplifier by using the attribute {\isa{iff}}.  You 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1138
should do so if the right hand side of the equivalence is  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1139
simpler than the left-hand side.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1140
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1141
For example, here is a simple fact about list concatenation. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1142
The result of appending two lists is empty if and only if both 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1143
of the lists are themselves empty. Obviously, applying this equivalence 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1144
will result in a simpler goal. When stating this lemma, we include 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1145
the {\isa{iff}} attribute. Once we have proved the lemma, Isabelle 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1146
will make it known to the classical reasoner (and to the simplifier). 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1147
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1148
\isacommand{lemma}\
10301
paulson
parents: 10295
diff changeset
  1149
[iff]:\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1150
"(xs{\isacharat}ys\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1151
\isacharbrackleft{]})\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1152
(xs=[]\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1153
\isacharampersand\
10301
paulson
parents: 10295
diff changeset
  1154
ys=[])"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1155
\isacommand{apply}\ (induct_tac\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1156
xs)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1157
\isacommand{apply}\ (simp_all)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1158
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1159
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1160
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1161
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1162
This fact about multiplication is also appropriate for 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1163
the {\isa{iff}} attribute:\remark{the ?s are ugly here but we need
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1164
them again when talking about \isa{of}; we need a consistent style}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1165
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1166
(\mbox{?m}\ \isacharasterisk\ \mbox{?n}\ =\ 0)\ =\ (\mbox{?m}\ =\ 0\ \isasymor\ \mbox{?n}\ =\ 0)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1167
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1168
A product is zero if and only if one of the factors is zero.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1169
reasoning  involves a logical \textsc{or}.  Proving new rules for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1170
disjunctive reasoning  is hard, but translating to an actual disjunction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1171
works:  the classical reasoner handles disjunction properly.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1172
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1173
In more detail, this is how the {\isa{iff}} attribute works.  It converts
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1174
the equivalence $P=Q$ to a pair of rules: the introduction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1175
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
  1176
classical reasoner as safe rules, ensuring that all occurrences of $P$ in
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1177
a subgoal are replaced by~$Q$.  The simplifier performs the same
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1178
replacement, since \isa{iff} gives $P=Q$ to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1179
simplifier.  But classical reasoning is different from
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1180
simplification.  Simplification is deterministic: it applies rewrite rules
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1181
repeatedly, as long as possible, in order to \emph{transform} a goal.  Classical
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1182
reasoning uses search and backtracking in order to \emph{prove} a goal. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1183
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1184
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1185
\section{Proving the correctness of Euclid's algorithm}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1186
\label{sec:proving-euclid}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1187
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1188
A brief development will illustrate advanced use of  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1189
\isa{blast}.  In \S\ref{sec:recdef-simplification}, we declared the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1190
recursive function {\isa{gcd}}:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1191
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1192
\isacommand{consts}\ gcd\ ::\ "nat{\isacharasterisk}nat\ \isasymRightarrow\ nat"\
paulson
parents: 10295
diff changeset
  1193
\
paulson
parents: 10295
diff changeset
  1194
\
paulson
parents: 10295
diff changeset
  1195
\ \ \ \ \ \ \ \ \ \ \ \ \isanewline
paulson
parents: 10295
diff changeset
  1196
\isacommand{recdef}\ gcd\ "measure\ ((\isasymlambda(m,n).n)\
paulson
parents: 10295
diff changeset
  1197
::nat{\isacharasterisk}nat\ \isasymRightarrow\ nat)"\isanewline
paulson
parents: 10295
diff changeset
  1198
\ \ \ \ "gcd\ (m,n)\ =\ (if\ n=0\ then\ m\ else\ gcd(n,\ m\ mod\ n))"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1199
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1200
Let us prove that it computes the greatest common
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1201
divisor of its two arguments.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1202
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1203
%The declaration yields a recursion
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1204
%equation  for {\isa{gcd}}.  Simplifying with this equation can 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1205
%cause looping, expanding to ever-larger expressions of if-then-else 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1206
%and {\isa{gcd}} calls.  To prevent this, we prove separate simplification rules
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1207
%for $n=0$\ldots
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1208
%\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1209
%\isacommand{lemma}\ gcd_0\ [simp]:\ "gcd(m,0)\ =\ m"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1210
%\isacommand{apply}\ (simp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1211
%\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1212
%\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1213
%\ldots{} and for $n>0$:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1214
%\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1215
%\isacommand{lemma}\ gcd_non_0:\ "0{\isacharless}n\ \isasymLongrightarrow\ gcd(m,n)\ =\ gcd\ (n,\ m\ mod\ n)"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1216
%\isacommand{apply}\ (simp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1217
%\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1218
%\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1219
%This second rule is similar to the original equation but
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1220
%does not loop because it is conditional.  It can be applied only
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1221
%when the second argument is known to be non-zero.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1222
%Armed with our two new simplification rules, we now delete the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1223
%original {\isa{gcd}} recursion equation. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1224
%\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1225
%\isacommand{declare}\ gcd.simps\ [simp\ del]
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1226
%\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1227
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1228
%Now we can prove  some interesting facts about the {\isa{gcd}} function,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1229
%for exampe, that it computes a common divisor of its arguments.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1230
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1231
The theorem is expressed in terms of the familiar
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1232
\textbf{divides} relation from number theory: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1233
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1234
?m\ dvd\ ?n\ \isasymequiv\ {\isasymexists}k.\ ?n\ =\ ?m\ \isacharasterisk\ k
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1235
\rulename{dvd_def}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1236
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1237
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1238
A simple induction proves the theorem.  Here \isa{gcd.induct} refers to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1239
induction rule returned by \isa{recdef}.  The proof relies on the simplification
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1240
rules proved in \S\ref{sec:recdef-simplification}, since rewriting by the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1241
definition of \isa{gcd} can cause looping.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1242
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1243
\isacommand{lemma}\ gcd_dvd_both:\ "(gcd(m,n)\ dvd\ m)\ \isasymand\ (gcd(m,n)\ dvd\ n)"\isanewline
paulson
parents: 10295
diff changeset
  1244
\isacommand{apply}\ (induct_tac\ m\ n\ rule:\ gcd.induct)\isanewline
paulson
parents: 10295
diff changeset
  1245
\isacommand{apply}\ (case_tac\ "n=0")\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1246
\isacommand{apply}\ (simp_all)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1247
\isacommand{apply}\ (blast\ dest:\ dvd_mod_imp_dvd)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1248
\isacommand{done}%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1249
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1250
Notice that the induction formula 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1251
is a conjunction.  This is necessary: in the inductive step, each 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1252
half of the conjunction establishes the other. The first three proof steps 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1253
are applying induction, performing a case analysis on \isa{n}, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1254
and simplifying.  Let us pass over these quickly and consider
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1255
the use of {\isa{blast}}.  We have reached the following 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1256
subgoal: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1257
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1258
%gcd\ (m,\ n)\ dvd\ m\ \isasymand\ gcd\ (m,\ n)\ dvd\ n\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1259
\ 1.\ {\isasymAnd}m\ n.\ \isasymlbrakk0\ \isacharless\ n;\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1260
 \ \ \ \ \ \ \ \ \ \ \ \ gcd\ (n,\ m\ mod\ n)\ dvd\ n\ \isasymand\ gcd\ (n,\ m\ mod\ n)\ dvd\ (m\ mod\ n){\isasymrbrakk}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1261
\ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow\ gcd\ (n,\ m\ mod\ n)\ dvd\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1262
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1263
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1264
One of the assumptions, the induction hypothesis, is a conjunction. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1265
The two divides relationships it asserts are enough to prove 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1266
the conclusion, for we have the following theorem at our disposal: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1267
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1268
\isasymlbrakk?k\ dvd\ (?m\ mod\ ?n){;}\ ?k\ dvd\ ?n\isasymrbrakk\ \isasymLongrightarrow\ ?k\ dvd\ ?m%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1269
\rulename{dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1270
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1271
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1272
This theorem can be applied in various ways.  As an introduction rule, it
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1273
would cause backward chaining from  the conclusion (namely
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1274
\isa{?k\ dvd\ ?m}) to the two premises, which 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1275
also involve the divides relation. This process does not look promising
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1276
and could easily loop.  More sensible is  to apply the rule in the forward
10301
paulson
parents: 10295
diff changeset
  1277
direction; each step would eliminate  the \isa{mod} symbol from an
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1278
assumption, so the process must terminate.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1279
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1280
So the final proof step applies the \isa{blast} method.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1281
Attaching the {\isa{dest}} attribute to \isa{dvd_mod_imp_dvd} tells \isa{blast}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1282
to use it as destruction rule: in the forward direction.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1283
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1284
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1285
We have proved a conjunction.  Now, let us give names to each of the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1286
two halves:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1287
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1288
\isacommand{lemmas}\ gcd_dvd1\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct1]\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1289
\isacommand{lemmas}\ gcd_dvd2\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct2]%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1290
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1291
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1292
Several things are happening here. The keyword \isacommand{lemmas}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1293
tells Isabelle to transform a theorem in some way and to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1294
give a name to the resulting theorem.  Attributes can be given,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1295
here \isa{iff}, which supplies the new theorems to the classical reasoner
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1296
and the simplifier.  The directive {\isa{THEN}}, which will be explained
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1297
below, supplies the lemma 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1298
\isa{gcd_dvd_both} to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1299
destruction rule \isa{conjunct1} in order to extract the first part.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1300
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1301
\ \ \ \ \ gcd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1302
(?m1,\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1303
?n1)\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1304
?m1%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1305
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1306
The variable names \isa{?m1} and \isa{?n1} arise because
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1307
Isabelle renames schematic variables to prevent 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1308
clashes.  The second \isacommand{lemmas} declaration yields
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1309
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1310
\ \ \ \ \ gcd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1311
(?m1,\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1312
?n1)\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1313
?n1%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1314
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1315
Later, we shall explore this type of forward reasoning in detail. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1316
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1317
To complete the verification of the {\isa{gcd}} function, we must 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1318
prove that it returns the greatest of all the common divisors 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1319
of its arguments.  The proof is by induction and simplification.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1320
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1321
\isacommand{lemma}\ gcd_greatest\
10301
paulson
parents: 10295
diff changeset
  1322
[rule_format]:\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1323
\ \ \ \ \ \ \ "(k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1324
m)\ \isasymlongrightarrow\ (k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1325
n)\ \isasymlongrightarrow\ k\ dvd\
10301
paulson
parents: 10295
diff changeset
  1326
gcd(m,n)"\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1327
\isacommand{apply}\ (induct_tac\ m\ n\
10301
paulson
parents: 10295
diff changeset
  1328
rule:\ gcd.induct)\isanewline
paulson
parents: 10295
diff changeset
  1329
\isacommand{apply}\ (case_tac\ "n=0")\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1330
\isacommand{apply}\ (simp_all\ add:\ gcd_non_0\ dvd_mod)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1331
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1332
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1333
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1334
Note that the theorem has been expressed using HOL implication,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1335
\isa{\isasymlongrightarrow}, because the induction affects the two
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1336
preconditions.  The directive \isa{rule_format} tells Isabelle to replace
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1337
each \isa{\isasymlongrightarrow} by \isa{\isasymLongrightarrow} before
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1338
storing the theorem we have proved.  This directive also removes outer
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1339
universal quantifiers, converting a theorem into the usual format for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1340
inference rules.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1341
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1342
The facts proved above can be summarized as a single logical 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1343
equivalence.  This step gives us a chance to see another application
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1344
of \isa{blast}, and it is worth doing for sound logical reasons.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1345
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1346
\isacommand{theorem}\ gcd_greatest_iff\ [iff]:\isanewline
paulson
parents: 10295
diff changeset
  1347
\ \ \ \ \ \ \ \ \ "k\ dvd\ gcd(m,n)\ =\ (k\ dvd\ m\ \isasymand\ k\ dvd\ n)"\isanewline
paulson
parents: 10295
diff changeset
  1348
\isacommand{apply}\ (blast\ intro!:\ gcd_greatest\ intro:\ dvd_trans)\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1349
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1350
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1351
This theorem concisely expresses the correctness of the {\isa{gcd}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1352
function. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1353
We state it with the {\isa{iff}} attribute so that 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1354
Isabelle can use it to remove some occurrences of {\isa{gcd}}. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1355
The theorem has a one-line 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1356
proof using {\isa{blast}} supplied with four introduction 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1357
rules: note the {\isa{intro}} attribute. The exclamation mark 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1358
({\isa{intro}}{\isa{!}})\ signifies safe rules, which are 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1359
applied aggressively.  Rules given without the exclamation mark 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1360
are applied reluctantly and their uses can be undone if 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1361
the search backtracks.  Here the unsafe rule expresses transitivity  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1362
of the divides relation:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1363
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1364
\isasymlbrakk?m\ dvd\ ?n;\ ?n\ dvd\ ?p\isasymrbrakk\ \isasymLongrightarrow\ ?m\ dvd\ ?p%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1365
\rulename{dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1366
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1367
Applying \isa{dvd_trans} as 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1368
an introduction rule entails a risk of looping, for it multiplies 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1369
occurrences of the divides symbol. However, this proof relies 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1370
on transitivity reasoning.  The rule {\isa{gcd\_greatest}} is safe to apply 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1371
aggressively because it yields simpler subgoals.  The proof implicitly
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1372
uses \isa{gcd_dvd1} and \isa{gcd_dvd2} as safe rules, because they were
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1373
declared using \isa{iff}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1374
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1375
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1376
\section{Other classical reasoning methods}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1377
 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1378
The {\isa{blast}} method is our main workhorse for proving theorems 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1379
automatically. Other components of the classical reasoner interact 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1380
with the simplifier. Still others perform classical reasoning 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1381
to a limited extent, giving the user fine control over the proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1382
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1383
Of the latter methods, the most useful is {\isa{clarify}}. It performs 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1384
all obvious reasoning steps without splitting the goal into multiple 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1385
parts. It does not apply rules that could render the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1386
goal unprovable (so-called unsafe rules). By performing the obvious 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1387
steps, {\isa{clarify}} lays bare the difficult parts of the problem, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1388
where human intervention is necessary. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1389
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1390
For example, the following conjecture is false:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1391
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1392
\isacommand{lemma}\ "({\isasymforall}x.\ P\ x)\ \isasymand\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1393
({\isasymexists}x.\ Q\ x)\ \isasymlongrightarrow\ ({\isasymforall}x.\ P\ x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1394
\isasymand\ Q\ x)"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1395
\isacommand{apply}\ clarify
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1396
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1397
The {\isa{blast}} method would simply fail, but {\isa{clarify}} presents 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1398
a subgoal that helps us see why we cannot continue the proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1399
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1400
\ 1.\ {\isasymAnd}x\ xa.\ \isasymlbrakk{\isasymforall}x.\ P\ x;\ Q\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1401
xa\isasymrbrakk\ \isasymLongrightarrow\ P\ x\ \isasymand\ Q\ x
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1402
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1403
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
  1404
refer to distinct bound variables.  To reach this state, \isa{clarify} applied
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1405
the introduction rules for \isa{\isasymlongrightarrow} and \isa{\isasymforall}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1406
and the elimination rule for ~\isa{\isasymand}.  It did not apply the introduction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1407
rule for  \isa{\isasymand} because of its policy never to split goals.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1408
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1409
Also available is {\isa{clarsimp}}, a method that interleaves {\isa{clarify}}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1410
and {\isa{simp}}.  Also there is \isa{safe}, which like \isa{clarify} performs
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1411
obvious steps and even applies those that split goals.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1412
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1413
The {\isa{force}} method applies the classical reasoner and simplifier 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1414
to one goal. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1415
\remark{example needed? most
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1416
things done by blast, simp or auto can also be done by force, so why add a new
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1417
one?}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1418
Unless it can prove the goal, it fails. Contrast 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1419
that with the auto method, which also combines classical reasoning 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1420
with simplification. The latter's purpose is to prove all the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1421
easy subgoals and parts of subgoals. Unfortunately, it can produce 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1422
large numbers of new subgoals; also, since it proves some subgoals 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1423
and splits others, it obscures the structure of the proof tree. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1424
The {\isa{force}} method does not have these drawbacks. Another 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1425
difference: {\isa{force}} tries harder than {\isa{auto}} to prove 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1426
its goal, so it can take much longer to terminate.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1427
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1428
Older components of the classical reasoner have largely been 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1429
superseded by {\isa{blast}}, but they still have niche applications. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1430
Most important among these are {\isa{fast}} and {\isa{best}}. While {\isa{blast}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1431
searches for proofs using a built-in first-order reasoner, these 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1432
earlier methods search for proofs using standard Isabelle inference. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1433
That makes them slower but enables them to work correctly in the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1434
presence of the more unusual features of Isabelle rules, such 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1435
as type classes and function unknowns. For example, the introduction rule
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1436
for Hilbert's epsilon-operator has the following form: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1437
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1438
?P\ ?x\ \isasymLongrightarrow\ ?P\ (Eps\ ?P)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1439
\rulename{someI}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1440
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1441
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1442
The repeated occurrence of the variable \isa{?P} makes this rule tricky 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1443
to apply. Consider this contrived example: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1444
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1445
\isacommand{lemma}\ "{\isasymlbrakk}Q\ a;\ P\ a\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1446
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ P\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1447
\isasymand\ Q\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1448
\isacommand{apply}\ (rule\ someI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1449
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1450
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1451
We can apply rule \isa{someI} explicitly.  It yields the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1452
following subgoal: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1453
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1454
\ 1.\ {\isasymlbrakk}Q\ a;\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P\ ?x\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1455
\isasymand\ Q\ ?x%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1456
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1457
The proof from this point is trivial.  The question now arises, could we have
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1458
proved the theorem with a single command? Not using {\isa{blast}} method: it
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1459
cannot perform  the higher-order unification that is necessary here.  The
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1460
{\isa{fast}}  method succeeds: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1461
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1462
\isacommand{apply}\ (fast\ intro!:\ someI)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1463
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1464
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1465
The {\isa{best}} method is similar to {\isa{fast}} but it uses a 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1466
best-first search instead of depth-first search. Accordingly, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1467
it is slower but is less susceptible to divergence. Transitivity 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1468
rules usually cause {\isa{fast}} to loop where often {\isa{best}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1469
can manage.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1470
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1471
Here is a summary of the classical reasoning methods:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1472
\begin{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1473
\item \isa{blast} works automatically and is the fastest
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1474
\item \isa{clarify} and \isa{clarsimp} perform obvious steps without splitting the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1475
goal; \isa{safe} even splits goals
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1476
\item \isa{force} uses classical reasoning and simplification to prove a goal;
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1477
 \isa{auto} is similar but leaves what it cannot prove
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1478
\item \isa{fast} and \isa{best} are legacy methods that work well with rules involving
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1479
unusual features
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1480
\end{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1481
A table illustrates the relationships among four of these methods. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1482
\begin{center}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1483
\begin{tabular}{r|l|l|}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1484
           & no split   & split \\ \hline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1485
  no simp  & \isa{clarify}    & \isa{safe} \\ \hline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1486
     simp  & \isa{clarsimp}   & \isa{auto} \\ \hline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1487
\end{tabular}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1488
\end{center}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1489
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1490
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1491
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1492
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1493
\section{Forward proof}\label{sec:forward}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1494
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1495
Forward proof means deriving new facts from old ones.  It is  the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1496
most fundamental type of proof.  Backward proof, by working  from goals to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1497
subgoals, can help us find a difficult proof.  But it is
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1498
not always the best way of presenting the proof so found.  Forward
10301
paulson
parents: 10295
diff changeset
  1499
proof is particularly good for reasoning from the general
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1500
to the specific.  For example, consider the following distributive law for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1501
the 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1502
\isa{gcd} function:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1503
\[ k\times\gcd(m,n) = \gcd(k\times m,k\times n)\]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1504
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1505
Putting $m=1$ we get (since $\gcd(1,n)=1$ and $k\times1=k$) 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1506
\[ k = \gcd(k,k\times n)\]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1507
We have derived a new fact about \isa{gcd}; if re-oriented, it might be
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1508
useful for simplification.  After re-orienting it and putting $n=1$, we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1509
derive another useful law: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1510
\[ \gcd(k,k)=k \]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1511
Substituting values for variables --- instantiation --- is a forward step. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1512
Re-orientation works by applying the symmetry of equality to 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1513
an equation, so it too is a forward step.  
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1514
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1515
Now let us reproduce our examples in Isabelle.  Here is the distributive
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1516
law:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1517
\begin{isabelle}%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1518
?k\ \isacharasterisk\ gcd\ (?m,\ ?n)\ =\ gcd\ (?k\ \isacharasterisk\ ?m,\ ?k\ \isacharasterisk\ ?n)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1519
\rulename{gcd_mult_distrib2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1520
\end{isabelle}%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1521
The first step is to replace \isa{?m} by~1 in this law.  We refer to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1522
variables not by name but by their position in the theorem, from left to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1523
right.  In this case, the variables  are \isa{?k}, \isa{?m} and~\isa{?n}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1524
So, the expression
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1525
\hbox{\texttt{[of k 1]}} replaces \isa{?k} by~\isa{k} and \isa{?m}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1526
by~\isa{1}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1527
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1528
\isacommand{lemmas}\ gcd_mult_0\ =\ gcd_mult_distrib2\ [of\ k\ 1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1529
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1530
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1531
The command 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1532
\isa{thm gcd_mult_0}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1533
displays the resulting theorem:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1534
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1535
\ \ \ \ \ k\ \isacharasterisk\ gcd\ (1,\ ?n)\ =\ gcd\ (k\ \isacharasterisk\ 1,\ k\ \isacharasterisk\ ?n)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1536
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1537
Something is odd: {\isa{k}} is an ordinary variable, while {\isa{?n}} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1538
is schematic.  We did not specify an instantiation 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1539
for {\isa{?n}}.  In its present form, the theorem does not allow 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1540
substitution for {\isa{k}}.  One solution is to avoid giving an instantiation for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1541
\isa{?k}: instead of a term we can put an underscore~(\isa{_}).  For example,
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1542
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1543
\ \ \ \ \ gcd_mult_distrib2\ [of\ _\ 1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1544
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1545
replaces \isa{?m} by~\isa{1} but leaves \isa{?k} unchanged.  Anyway, let us put
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1546
the theorem \isa{gcd_mult_0} into a simplified form: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1547
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1548
\isacommand{lemmas}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1549
gcd_mult_1\ =\ gcd_mult_0\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1550
[simplified]%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1551
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1552
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1553
Again, we display the resulting theorem:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1554
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1555
\ \ \ \ \ k\ =\ gcd\ (k,\ k\ \isacharasterisk\ ?n)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1556
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1557
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1558
To re-orient the equation requires the symmetry rule:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1559
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1560
?s\ =\ ?t\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1561
\isasymLongrightarrow\ ?t\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1562
?s%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1563
\rulename{sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1564
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1565
The following declaration gives our equation to \isa{sym}:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1566
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1567
\ \ \ \isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_1\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1568
[THEN\ sym]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1569
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1570
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1571
Here is the result:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1572
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1573
\ \ \ \ \ gcd\ (k,\ k\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1574
?n)\ =\ k%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1575
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1576
\isa{THEN~sym} gives the current theorem to the rule \isa{sym} and returns the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1577
resulting conclusion.\remark{figure necessary?}  The effect is to exchange the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1578
two operands of the equality. Typically {\isa{THEN}} is used with destruction
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1579
rules.  Above we have used
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1580
\isa{THEN~conjunct1} to extract the first part of the theorem
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1581
\isa{gcd_dvd_both}.  Also useful is \isa{THEN~spec}, which removes the quantifier
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1582
from a theorem of the form $\forall x.\,P$, and \isa{THEN~mp}, which converts the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1583
implication $P\imp Q$ into the rule $\vcenter{\infer{Q}{P}}$.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1584
Similar to \isa{mp} are the following two rules, which extract 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1585
the two directions of reasoning about a boolean equivalence:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1586
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1587
\isasymlbrakk?Q\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1588
?P;\ ?Q\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1589
\isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1590
\rulename{iffD1}%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1591
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1592
\isasymlbrakk?P\ =\ ?Q;\ ?Q\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1593
\isasymLongrightarrow\ ?P%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1594
\rulename{iffD2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1595
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1596
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1597
Normally we would never name the intermediate theorems
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1598
such as \isa{gcd_mult_0} and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1599
\isa{gcd_mult_1} but would combine
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1600
the three forward steps: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1601
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1602
\isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym]%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1603
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1604
The directives, or attributes, are processed from left to right.  This
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1605
declaration of \isa{gcd_mult} is equivalent to the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1606
previous one.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1607
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1608
Such declarations can make the proof script hard to read: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1609
what is being proved?  More legible   
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1610
is to state the new lemma explicitly and to prove it using a single
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1611
\isa{rule} method whose operand is expressed using forward reasoning:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1612
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1613
\isacommand{lemma}\ gcd_mult\
10301
paulson
parents: 10295
diff changeset
  1614
[simp]:\
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1615
"gcd(k,\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1616
k{\isacharasterisk}n)\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1617
k"\isanewline
10301
paulson
parents: 10295
diff changeset
  1618
\isacommand{apply}\ (rule\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym])\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1619
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1620
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1621
Compared with the previous proof of \isa{gcd_mult}, this
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1622
version shows the reader what has been proved.  Also, it receives
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1623
the usual Isabelle treatment.  In particular, Isabelle generalizes over all
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1624
variables: the resulting theorem will have {\isa{?k}} instead of {\isa{k}}.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1625
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1626
At the start  of this section, we also saw a proof of $\gcd(k,k)=k$.  Here
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1627
is the Isabelle version: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1628
\begin{isabelle}
10301
paulson
parents: 10295
diff changeset
  1629
\isacommand{lemma}\ gcd_self\ [simp]:\ "gcd(k,k)\ =\ k"\isanewline
paulson
parents: 10295
diff changeset
  1630
\isacommand{apply}\ (rule\ gcd_mult\ [of\ k\ 1,\ simplified])\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1631
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1632
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1633
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1634
Recall that \isa{of} generates an instance of a rule by specifying
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1635
values for its variables.  Analogous is \isa{OF}, which generates an
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1636
instance of a rule by specifying facts for its premises.  Let us try
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1637
it with this rule:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1638
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1639
{\isasymlbrakk}gcd(?k,?n){=}1;\ ?k\ dvd\ (?m * ?n){\isasymrbrakk}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1640
\isasymLongrightarrow\ ?k\ dvd\ ?m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1641
\rulename{relprime_dvd_mult}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1642
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1643
First, we
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1644
prove an instance of its first premise:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1645
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1646
\isacommand{lemma}\ relprime_20_81:\ "gcd(\#20,\#81)\ =\ 1"\isanewline
10301
paulson
parents: 10295
diff changeset
  1647
\isacommand{apply}\ (simp\ add:\ gcd.simps)\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1648
\isacommand{done}%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1649
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1650
We have evaluated an application of the \isa{gcd} function by
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1651
simplification.  Expression evaluation  is not guaranteed to terminate, and
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1652
certainly is not  efficient; Isabelle performs arithmetic operations by 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1653
rewriting symbolic bit strings.  Here, however, the simplification takes
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1654
less than one second.  We can specify this new lemma to {\isa{OF}},
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1655
generating an instance of \isa{relprime_dvd_mult}.  The expression
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1656
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1657
\ \ \ \ \ relprime_dvd_mult [OF relprime_20_81]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1658
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1659
yields the theorem
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1660
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1661
\ \ \ \ \ \isacharhash20\ dvd\ (?m\ \isacharasterisk\ \isacharhash81)\ \isasymLongrightarrow\ \isacharhash20\ dvd\ ?m%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1662
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1663
%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1664
{\isa{OF}} takes any number of operands.  Consider 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1665
the following facts about the divides relation: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1666
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1667
\isasymlbrakk?k\ dvd\ ?m;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1668
?k\ dvd\ ?n\isasymrbrakk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1669
\isasymLongrightarrow\ ?k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1670
(?m\ \isacharplus\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1671
?n)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1672
\rulename{dvd_add}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1673
?m\ dvd\ ?m%
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1674
\rulename{dvd_refl}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1675
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1676
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
  1677
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1678
\ \ \ \ \ dvd_add [OF dvd_refl dvd_refl]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1679
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1680
Here is the theorem that we have expressed: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1681
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1682
\ \ \ \ \ ?k\ dvd\ (?k\ \isacharplus\ ?k)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1683
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1684
As with \isa{of}, we can use the \isa{_} symbol to leave some positions
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1685
unspecified:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1686
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1687
\ \ \ \ \ dvd_add [OF _ dvd_refl]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1688
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1689
The result is 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1690
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1691
\ \ \ \ \ ?k\ dvd\ ?m\ \isasymLongrightarrow\ ?k\ dvd\ (?m\ \isacharplus\ ?k)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1692
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1693
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1694
You may have noticed that {\isa{THEN}} and {\isa{OF}} are based on 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1695
the same idea, namely to combine two rules.  They differ in the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1696
order of the combination and thus in their effect.  We use \isa{THEN}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1697
typically with a destruction rule to extract a subformula of the current
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1698
theorem.  We use \isa{OF} with a list of facts to generate an instance of
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1699
the current theorem.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1700
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1701
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1702
Here is a summary of the primitives for forward reasoning:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1703
\begin{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1704
\item {\isa{of}} instantiates the variables of a rule to a list of terms
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1705
\item {\isa{OF}} applies a rule to a list of theorems
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1706
\item {\isa{THEN}} gives a theorem to a named rule and returns the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1707
conclusion 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1708
\end{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1709
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1710
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1711
\section{Methods for forward proof}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1712
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1713
We have seen that forward proof works well within a backward 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1714
proof.  Also in that spirit is the \isa{insert} method, which inserts a
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1715
given theorem as a new assumption of the current subgoal.  This already
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1716
is a forward step; moreover, we may (as always when using a theorem) apply
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1717
{\isa{of}}, {\isa{THEN}} and other directives.  The new assumption can then
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1718
be used to help prove the subgoal.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1719
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1720
For example, consider this theorem about the divides relation. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1721
Only the first proof step is given; it inserts the distributive law for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1722
\isa{gcd}.  We specify its variables as shown. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1723
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1724
\isacommand{lemma}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1725
relprime_dvd_mult:\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1726
\ \ \ \ \ \ \ "{\isasymlbrakk}\ gcd(k,n){=}1;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1727
k\ dvd\ (m*n)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1728
{\isasymrbrakk}\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1729
\isasymLongrightarrow\ k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1730
m"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1731
\isacommand{apply}\ (insert\ gcd_mult_distrib2\ [of\ m\ k\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1732
n])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1733
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1734
In the resulting subgoal, note how the equation has been 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1735
inserted: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1736
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1737
{\isasymlbrakk}gcd\ (k,\ n)\ =\ 1;\ k\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1738
dvd\ (m\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1739
n){\isasymrbrakk}\ \isasymLongrightarrow\ k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1740
m\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1741
\ 1.\ {\isasymlbrakk}gcd\ (k,\ n)\ =\ 1;\ k\ dvd\ (m\ \isacharasterisk\ n){;}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1742
\ \ \ \ \ m\ \isacharasterisk\ gcd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1743
(k,\ n)\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1744
=\ gcd\ (m\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1745
k,\ m\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1746
n){\isasymrbrakk}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1747
\ \ \ \ \isasymLongrightarrow\ k\ dvd\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1748
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1749
The next proof step, \isa{\isacommand{apply}(simp)}, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1750
utilizes the assumption \isa{gcd(k,n)\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1751
1}. Here is the result: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1752
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1753
{\isasymlbrakk}gcd\ (k,\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1754
n)\ =\ 1;\ k\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1755
dvd\ (m\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1756
n){\isasymrbrakk}\ \isasymLongrightarrow\ k\ dvd\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1757
m\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1758
\ 1.\ {\isasymlbrakk}gcd\ (k,\ n)\ =\ 1;\ k\ dvd\ (m\ \isacharasterisk\ n){;}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1759
\ \ \ \ \ m\ =\ gcd\ (m\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1760
\isacharasterisk\ k,\ m\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1761
n){\isasymrbrakk}\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1762
\ \ \ \ \isasymLongrightarrow\ k\ dvd\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1763
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1764
Simplification has yielded an equation for \isa{m} that will be used to
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1765
complete the proof. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1766
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1767
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1768
Here is another proof using \isa{insert}.  \remark{Effect with unknowns?}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1769
Division  and remainder obey a well-known law: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1770
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1771
(?m\ div\ ?n)\ \isacharasterisk\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1772
?n\ \isacharplus\ ?m\ mod\ ?n\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1773
=\ ?m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1774
\rulename{mod_div_equality}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1775
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1776
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1777
We refer to this law explicitly in the following proof: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1778
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1779
\isacommand{lemma}\ div_mult_self_is_m:\ \isanewline
10301
paulson
parents: 10295
diff changeset
  1780
\ \ \ \ \ \ "0{\isacharless}n\ \isasymLongrightarrow\ (m{\isacharasterisk}n)\ div\ n\ =\ (m::nat)"\isanewline
paulson
parents: 10295
diff changeset
  1781
\isacommand{apply}\ (insert\ mod_div_equality\ [of\ "m{\isacharasterisk}n"\ n])\isanewline
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1782
\isacommand{apply}\ (simp)\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1783
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1784
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1785
The first step inserts the law, specifying \isa{m*n} and
10301
paulson
parents: 10295
diff changeset
  1786
\isa{n} for its variables.  Notice that non-trivial expressions must be
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1787
enclosed in quotation marks.  Here is the resulting 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1788
subgoal, with its new assumption: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1789
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1790
%0\ \isacharless\ n\ \isasymLongrightarrow\ (m\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1791
%\isacharasterisk\ n)\ div\ n\ =\ m\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1792
\ 1.\ \isasymlbrakk0\ \isacharless\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1793
n;\ \ (m\ \isacharasterisk\ n)\ div\ n\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1794
\isacharasterisk\ n\ \isacharplus\ (m\ \isacharasterisk\ n)\ mod\ n\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1795
=\ m\ \isacharasterisk\ n\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1796
\ \ \ \ \isasymLongrightarrow\ (m\ \isacharasterisk\ n)\ div\ n\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1797
=\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1798
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1799
Simplification reduces \isa{(m\ \isacharasterisk\ n)\ mod\ n} to zero.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1800
Then it cancels the factor~\isa{n} on both
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1801
sides of the equation, proving the theorem. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1802
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1803
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1804
A similar method is {\isa{subgoal\_tac}}. Instead of inserting 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1805
a theorem as an assumption, it inserts an arbitrary formula. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1806
This formula must be proved later as a separate subgoal. The 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1807
idea is to claim that the formula holds on the basis of the current 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1808
assumptions, to use this claim to complete the proof, and finally 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1809
to justify the claim. It is a valuable means of giving the proof 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1810
some structure. The explicit formula will be more readable than 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1811
proof commands that yield that formula indirectly.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1812
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1813
Look at the following example. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1814
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1815
\isacommand{lemma}\ "\isasymlbrakk(z::int)\ <\ \#37;\ \#66\ <\ \#2*z;\ z*z\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1816
\isasymnoteq\ \#1225;\ Q(\#34);\ Q(\#36)\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1817
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ Q(z)"\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1818
\isacommand{apply}\ (subgoal_tac\ "z\ =\ \#34\ \isasymor\ z\ =\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1819
\#36")\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1820
\isacommand{apply}\ blast\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1821
\isacommand{apply}\ (subgoal_tac\ "z\ \isasymnoteq\ \#35")\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1822
\isacommand{apply}\ arith\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1823
\isacommand{apply}\ force\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1824
\isacommand{done}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1825
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1826
Let us prove it informally.  The first assumption tells us 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1827
that \isa{z} is no greater than 36. The second tells us that \isa{z} 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1828
is at least 34. The third assumption tells us that \isa{z} cannot be 35, since
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1829
$35\times35=1225$.  So \isa{z} is either 34 or 36, and since \isa{Q} holds for
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1830
both of those  values, we have the conclusion. 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1831
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1832
The Isabelle proof closely follows this reasoning. The first 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1833
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
  1834
state gives us two subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1835
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1836
%{\isasymlbrakk}z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1837
%Q\ \#34;\ Q\ \#36\isasymrbrakk\ \isasymLongrightarrow\ Q\ z\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1838
\ 1.\ {\isasymlbrakk}z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36;\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1839
\ \ \ \ \ z\ =\ \#34\ \isasymor\ z\ =\ \#36\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1840
\ \ \ \ \isasymLongrightarrow\ Q\ z\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1841
\ 2.\ {\isasymlbrakk}z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1842
\ \ \ \ \isasymLongrightarrow\ z\ =\ \#34\ \isasymor\ z\ =\ \#36
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1843
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1844
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1845
The first subgoal is trivial, but for the second Isabelle needs help to eliminate
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1846
the case
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1847
\isa{z}=35.  The second invocation  of {\isa{subgoal\_tac}} leaves two
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1848
subgoals: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1849
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1850
\ 1.\ {\isasymlbrakk}z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1851
\#1225;\ Q\ \#34;\ Q\ \#36;\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1852
\ \ \ \ \ z\ \isasymnoteq\ \#35\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1853
\ \ \ \ \isasymLongrightarrow\ z\ =\ \#34\ \isasymor\ z\ =\ \#36\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1854
\ 2.\ {\isasymlbrakk}z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1855
\ \ \ \ \isasymLongrightarrow\ z\ \isasymnoteq\ \#35
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1856
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1857
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1858
Assuming that \isa{z} is not 35, the first subgoal follows by linear arithmetic:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1859
the method {\isa{arith}}. For the second subgoal we apply the method {\isa{force}}, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1860
which proceeds by assuming that \isa{z}=35 and arriving at a contradiction.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1861
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1862
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1863
\medskip
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1864
Summary of these methods:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1865
\begin{itemize}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1866
\item {\isa{insert}} adds a theorem as a new assumption
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1867
\item {\isa{subgoal_tac}} adds a formula as a new assumption and leaves the
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1868
subgoal of proving that formula
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
  1869
\end{itemize}