doc-src/TutorialI/Inductive/document/Star.tex
author nipkow
Fri, 18 May 2001 16:45:55 +0200
changeset 11308 b28bbb153603
parent 11257 622331bbdb7f
child 11494 23a118849801
permissions -rw-r--r--
*** empty log message ***
nipkow@10225
     1
%
nipkow@10225
     2
\begin{isabellebody}%
nipkow@10225
     3
\def\isabellecontext{Star}%
nipkow@10225
     4
%
paulson@10878
     5
\isamarkupsection{The Reflexive Transitive Closure%
wenzelm@10395
     6
}
nipkow@10225
     7
%
nipkow@10225
     8
\begin{isamarkuptext}%
nipkow@10242
     9
\label{sec:rtc}
paulson@10878
    10
An inductive definition may accept parameters, so it can express 
paulson@10878
    11
functions that yield sets.
paulson@10878
    12
Relations too can be defined inductively, since they are just sets of pairs.
paulson@10878
    13
A perfect example is the function that maps a relation to its
paulson@10878
    14
reflexive transitive closure.  This concept was already
nipkow@11147
    15
introduced in \S\ref{sec:Relations}, where the operator \isa{\isactrlsup {\isacharasterisk}} was
nipkow@10520
    16
defined as a least fixed point because inductive definitions were not yet
nipkow@10520
    17
available. But now they are:%
nipkow@10225
    18
\end{isamarkuptext}%
nipkow@10242
    19
\isacommand{consts}\ rtc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set{\isachardoublequote}\ \ \ {\isacharparenleft}{\isachardoublequote}{\isacharunderscore}{\isacharasterisk}{\isachardoublequote}\ {\isacharbrackleft}{\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharbrackright}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharparenright}\isanewline
nipkow@10225
    20
\isacommand{inductive}\ {\isachardoublequote}r{\isacharasterisk}{\isachardoublequote}\isanewline
nipkow@10225
    21
\isakeyword{intros}\isanewline
nipkow@10242
    22
rtc{\isacharunderscore}refl{\isacharbrackleft}iff{\isacharbrackright}{\isacharcolon}\ \ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}x{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
nipkow@10242
    23
rtc{\isacharunderscore}step{\isacharcolon}\ \ \ \ \ \ \ {\isachardoublequote}{\isasymlbrakk}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}%
nipkow@10242
    24
\begin{isamarkuptext}%
nipkow@10242
    25
\noindent
nipkow@10242
    26
The function \isa{rtc} is annotated with concrete syntax: instead of
nipkow@10520
    27
\isa{rtc\ r} we can read and write \isa{r{\isacharasterisk}}. The actual definition
nipkow@10520
    28
consists of two rules. Reflexivity is obvious and is immediately given the
nipkow@10520
    29
\isa{iff} attribute to increase automation. The
nipkow@10363
    30
second rule, \isa{rtc{\isacharunderscore}step}, says that we can always add one more
nipkow@10363
    31
\isa{r}-step to the left. Although we could make \isa{rtc{\isacharunderscore}step} an
nipkow@10520
    32
introduction rule, this is dangerous: the recursion in the second premise
nipkow@10520
    33
slows down and may even kill the automatic tactics.
nipkow@10242
    34
nipkow@10242
    35
The above definition of the concept of reflexive transitive closure may
nipkow@10242
    36
be sufficiently intuitive but it is certainly not the only possible one:
paulson@10878
    37
for a start, it does not even mention transitivity.
nipkow@10242
    38
The rest of this section is devoted to proving that it is equivalent to
paulson@10878
    39
the standard definition. We start with a simple lemma:%
nipkow@10242
    40
\end{isamarkuptext}%
nipkow@11308
    41
\isacommand{lemma}\ {\isacharbrackleft}intro{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
nipkow@10242
    42
\isacommand{by}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isacharunderscore}step{\isacharparenright}%
nipkow@10242
    43
\begin{isamarkuptext}%
nipkow@10242
    44
\noindent
nipkow@10242
    45
Although the lemma itself is an unremarkable consequence of the basic rules,
nipkow@10242
    46
it has the advantage that it can be declared an introduction rule without the
nipkow@10242
    47
danger of killing the automatic tactics because \isa{r{\isacharasterisk}} occurs only in
nipkow@10242
    48
the conclusion and not in the premise. Thus some proofs that would otherwise
nipkow@10242
    49
need \isa{rtc{\isacharunderscore}step} can now be found automatically. The proof also
paulson@10878
    50
shows that \isa{blast} is able to handle \isa{rtc{\isacharunderscore}step}. But
nipkow@10242
    51
some of the other automatic tactics are more sensitive, and even \isa{blast} can be lead astray in the presence of large numbers of rules.
nipkow@10242
    52
nipkow@10520
    53
To prove transitivity, we need rule induction, i.e.\ theorem
nipkow@10520
    54
\isa{rtc{\isachardot}induct}:
nipkow@10520
    55
\begin{isabelle}%
nipkow@10696
    56
\ \ \ \ \ {\isasymlbrakk}{\isacharparenleft}{\isacharquery}xb{\isacharcomma}\ {\isacharquery}xa{\isacharparenright}\ {\isasymin}\ {\isacharquery}r{\isacharasterisk}{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ {\isacharquery}P\ x\ x{\isacharsemicolon}\isanewline
wenzelm@10950
    57
\isaindent{\ \ \ \ \ \ \ \ }{\isasymAnd}x\ y\ z{\isachardot}\ {\isasymlbrakk}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isasymin}\ {\isacharquery}r{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ {\isacharquery}r{\isacharasterisk}{\isacharsemicolon}\ {\isacharquery}P\ y\ z{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}P\ x\ z{\isasymrbrakk}\isanewline
wenzelm@10950
    58
\isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isacharquery}P\ {\isacharquery}xb\ {\isacharquery}xa%
nipkow@10520
    59
\end{isabelle}
nipkow@10520
    60
It says that \isa{{\isacharquery}P} holds for an arbitrary pair \isa{{\isacharparenleft}{\isacharquery}xb{\isacharcomma}{\isacharquery}xa{\isacharparenright}\ {\isasymin}\ {\isacharquery}r{\isacharasterisk}} if \isa{{\isacharquery}P} is preserved by all rules of the inductive definition,
nipkow@10520
    61
i.e.\ if \isa{{\isacharquery}P} holds for the conclusion provided it holds for the
nipkow@10520
    62
premises. In general, rule induction for an $n$-ary inductive relation $R$
nipkow@10520
    63
expects a premise of the form $(x@1,\dots,x@n) \in R$.
nipkow@10520
    64
nipkow@10520
    65
Now we turn to the inductive proof of transitivity:%
nipkow@10242
    66
\end{isamarkuptext}%
nipkow@10520
    67
\isacommand{lemma}\ rtc{\isacharunderscore}trans{\isacharcolon}\ {\isachardoublequote}{\isasymlbrakk}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
nipkow@10242
    68
\isacommand{apply}{\isacharparenleft}erule\ rtc{\isachardot}induct{\isacharparenright}%
nipkow@10363
    69
\begin{isamarkuptxt}%
nipkow@10242
    70
\noindent
nipkow@10520
    71
Unfortunately, even the resulting base case is a problem
nipkow@10363
    72
\begin{isabelle}%
nipkow@10363
    73
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}x{\isachardot}\ {\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}%
nipkow@10242
    74
\end{isabelle}
nipkow@10242
    75
and maybe not what you had expected. We have to abandon this proof attempt.
nipkow@10520
    76
To understand what is going on, let us look again at \isa{rtc{\isachardot}induct}.
nipkow@10520
    77
In the above application of \isa{erule}, the first premise of
nipkow@10520
    78
\isa{rtc{\isachardot}induct} is unified with the first suitable assumption, which
nipkow@10520
    79
is \isa{{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}} rather than \isa{{\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}}. Although that
nipkow@10520
    80
is what we want, it is merely due to the order in which the assumptions occur
nipkow@10520
    81
in the subgoal, which it is not good practice to rely on. As a result,
nipkow@10520
    82
\isa{{\isacharquery}xb} becomes \isa{x}, \isa{{\isacharquery}xa} becomes
nipkow@10520
    83
\isa{y} and \isa{{\isacharquery}P} becomes \isa{{\isasymlambda}u\ v{\isachardot}\ {\isacharparenleft}u{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}}, thus
nipkow@10242
    84
yielding the above subgoal. So what went wrong?
nipkow@10242
    85
nipkow@10520
    86
When looking at the instantiation of \isa{{\isacharquery}P} we see that it does not
nipkow@10520
    87
depend on its second parameter at all. The reason is that in our original
nipkow@10520
    88
goal, of the pair \isa{{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}} only \isa{x} appears also in the
nipkow@10520
    89
conclusion, but not \isa{y}. Thus our induction statement is too
nipkow@10520
    90
weak. Fortunately, it can easily be strengthened:
nipkow@10242
    91
transfer the additional premise \isa{{\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}} into the conclusion:%
nipkow@10363
    92
\end{isamarkuptxt}%
nipkow@10242
    93
\isacommand{lemma}\ rtc{\isacharunderscore}trans{\isacharbrackleft}rule{\isacharunderscore}format{\isacharbrackright}{\isacharcolon}\isanewline
nipkow@10242
    94
\ \ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymLongrightarrow}\ {\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymlongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}%
nipkow@10242
    95
\begin{isamarkuptxt}%
nipkow@10242
    96
\noindent
nipkow@10242
    97
This is not an obscure trick but a generally applicable heuristic:
nipkow@10242
    98
\begin{quote}\em
nipkow@11257
    99
When proving a statement by rule induction on $(x@1,\dots,x@n) \in R$,
nipkow@10242
   100
pull all other premises containing any of the $x@i$ into the conclusion
nipkow@10242
   101
using $\longrightarrow$.
nipkow@10242
   102
\end{quote}
nipkow@10242
   103
A similar heuristic for other kinds of inductions is formulated in
nipkow@10242
   104
\S\ref{sec:ind-var-in-prems}. The \isa{rule{\isacharunderscore}format} directive turns
nipkow@11147
   105
\isa{{\isasymlongrightarrow}} back into \isa{{\isasymLongrightarrow}}: in the end we obtain the original
nipkow@10363
   106
statement of our lemma.%
nipkow@10363
   107
\end{isamarkuptxt}%
nipkow@10363
   108
\isacommand{apply}{\isacharparenleft}erule\ rtc{\isachardot}induct{\isacharparenright}%
nipkow@10363
   109
\begin{isamarkuptxt}%
nipkow@10363
   110
\noindent
nipkow@10242
   111
Now induction produces two subgoals which are both proved automatically:
nipkow@10363
   112
\begin{isabelle}%
nipkow@10242
   113
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}x{\isachardot}\ {\isacharparenleft}x{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymlongrightarrow}\ {\isacharparenleft}x{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\isanewline
nipkow@10242
   114
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}x\ y\ za{\isachardot}\isanewline
wenzelm@10950
   115
\isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }{\isasymlbrakk}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isasymin}\ r{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}\ za{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isacharsemicolon}\ {\isacharparenleft}za{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymlongrightarrow}\ {\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isasymrbrakk}\isanewline
wenzelm@10950
   116
\isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }{\isasymLongrightarrow}\ {\isacharparenleft}za{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymlongrightarrow}\ {\isacharparenleft}x{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}%
nipkow@10242
   117
\end{isabelle}%
nipkow@10242
   118
\end{isamarkuptxt}%
nipkow@10225
   119
\ \isacommand{apply}{\isacharparenleft}blast{\isacharparenright}\isanewline
nipkow@10237
   120
\isacommand{apply}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isacharunderscore}step{\isacharparenright}\isanewline
nipkow@10242
   121
\isacommand{done}%
nipkow@10242
   122
\begin{isamarkuptext}%
nipkow@10242
   123
Let us now prove that \isa{r{\isacharasterisk}} is really the reflexive transitive closure
nipkow@10242
   124
of \isa{r}, i.e.\ the least reflexive and transitive
nipkow@10242
   125
relation containing \isa{r}. The latter is easily formalized%
nipkow@10242
   126
\end{isamarkuptext}%
nipkow@10237
   127
\isacommand{consts}\ rtc{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set{\isachardoublequote}\isanewline
nipkow@10237
   128
\isacommand{inductive}\ {\isachardoublequote}rtc{\isadigit{2}}\ r{\isachardoublequote}\isanewline
nipkow@10225
   129
\isakeyword{intros}\isanewline
nipkow@10237
   130
{\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r{\isachardoublequote}\isanewline
nipkow@10237
   131
{\isachardoublequote}{\isacharparenleft}x{\isacharcomma}x{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r{\isachardoublequote}\isanewline
nipkow@10237
   132
{\isachardoublequote}{\isasymlbrakk}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r{\isachardoublequote}%
nipkow@10237
   133
\begin{isamarkuptext}%
nipkow@10242
   134
\noindent
nipkow@10242
   135
and the equivalence of the two definitions is easily shown by the obvious rule
nipkow@10237
   136
inductions:%
nipkow@10237
   137
\end{isamarkuptext}%
nipkow@10237
   138
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
nipkow@10237
   139
\isacommand{apply}{\isacharparenleft}erule\ rtc{\isadigit{2}}{\isachardot}induct{\isacharparenright}\isanewline
nipkow@10237
   140
\ \ \isacommand{apply}{\isacharparenleft}blast{\isacharparenright}\isanewline
nipkow@10237
   141
\ \isacommand{apply}{\isacharparenleft}blast{\isacharparenright}\isanewline
nipkow@10237
   142
\isacommand{apply}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isacharunderscore}trans{\isacharparenright}\isanewline
nipkow@10237
   143
\isacommand{done}\isanewline
nipkow@10225
   144
\isanewline
nipkow@10237
   145
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ rtc{\isadigit{2}}\ r{\isachardoublequote}\isanewline
nipkow@10237
   146
\isacommand{apply}{\isacharparenleft}erule\ rtc{\isachardot}induct{\isacharparenright}\isanewline
nipkow@10237
   147
\ \isacommand{apply}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isadigit{2}}{\isachardot}intros{\isacharparenright}\isanewline
nipkow@10237
   148
\isacommand{apply}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isadigit{2}}{\isachardot}intros{\isacharparenright}\isanewline
nipkow@10242
   149
\isacommand{done}%
nipkow@10242
   150
\begin{isamarkuptext}%
nipkow@10242
   151
So why did we start with the first definition? Because it is simpler. It
nipkow@10242
   152
contains only two rules, and the single step rule is simpler than
nipkow@10242
   153
transitivity.  As a consequence, \isa{rtc{\isachardot}induct} is simpler than
paulson@10878
   154
\isa{rtc{\isadigit{2}}{\isachardot}induct}. Since inductive proofs are hard enough
nipkow@11147
   155
anyway, we should always pick the simplest induction schema available.
nipkow@10242
   156
Hence \isa{rtc} is the definition of choice.
nipkow@10242
   157
nipkow@10520
   158
\begin{exercise}\label{ex:converse-rtc-step}
nipkow@10242
   159
Show that the converse of \isa{rtc{\isacharunderscore}step} also holds:
nipkow@10242
   160
\begin{isabelle}%
nipkow@10696
   161
\ \ \ \ \ {\isasymlbrakk}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}\ z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}%
nipkow@10242
   162
\end{isabelle}
nipkow@10520
   163
\end{exercise}
nipkow@10520
   164
\begin{exercise}
nipkow@10520
   165
Repeat the development of this section, but starting with a definition of
nipkow@10520
   166
\isa{rtc} where \isa{rtc{\isacharunderscore}step} is replaced by its converse as shown
nipkow@10520
   167
in exercise~\ref{ex:converse-rtc-step}.
nipkow@10242
   168
\end{exercise}%
nipkow@10242
   169
\end{isamarkuptext}%
nipkow@10225
   170
\end{isabellebody}%
nipkow@10225
   171
%%% Local Variables:
nipkow@10225
   172
%%% mode: latex
nipkow@10225
   173
%%% TeX-master: "root"
nipkow@10225
   174
%%% End: