doc-src/TutorialI/Inductive/document/AB.tex
author nipkow
Thu, 29 Nov 2001 21:12:37 +0100
changeset 12332 aea72a834c85
parent 11870 181bd2050cf4
child 12815 1f073030b97a
permissions -rw-r--r--
*** empty log message ***
nipkow@10217
     1
%
nipkow@10217
     2
\begin{isabellebody}%
nipkow@10217
     3
\def\isabellecontext{AB}%
wenzelm@11866
     4
\isamarkupfalse%
nipkow@10225
     5
%
paulson@10878
     6
\isamarkupsection{Case Study: A Context Free Grammar%
wenzelm@10395
     7
}
wenzelm@11866
     8
\isamarkuptrue%
nipkow@10236
     9
%
nipkow@10236
    10
\begin{isamarkuptext}%
nipkow@10242
    11
\label{sec:CFG}
paulson@11494
    12
\index{grammars!defining inductively|(}%
nipkow@10236
    13
Grammars are nothing but shorthands for inductive definitions of nonterminals
nipkow@10236
    14
which represent sets of strings. For example, the production
nipkow@10236
    15
$A \to B c$ is short for
nipkow@10236
    16
\[ w \in B \Longrightarrow wc \in A \]
paulson@10878
    17
This section demonstrates this idea with an example
paulson@10878
    18
due to Hopcroft and Ullman, a grammar for generating all words with an
paulson@10878
    19
equal number of $a$'s and~$b$'s:
nipkow@10236
    20
\begin{eqnarray}
nipkow@10236
    21
S &\to& \epsilon \mid b A \mid a B \nonumber\\
nipkow@10236
    22
A &\to& a S \mid b A A \nonumber\\
nipkow@10236
    23
B &\to& b S \mid a B B \nonumber
nipkow@10236
    24
\end{eqnarray}
paulson@10878
    25
At the end we say a few words about the relationship between
paulson@10878
    26
the original proof \cite[p.\ts81]{HopcroftUllman} and our formal version.
nipkow@10236
    27
paulson@10299
    28
We start by fixing the alphabet, which consists only of \isa{a}'s
paulson@10878
    29
and~\isa{b}'s:%
nipkow@10236
    30
\end{isamarkuptext}%
wenzelm@11866
    31
\isamarkuptrue%
wenzelm@11866
    32
\isacommand{datatype}\ alfa\ {\isacharequal}\ a\ {\isacharbar}\ b\isamarkupfalse%
wenzelm@11866
    33
%
nipkow@10236
    34
\begin{isamarkuptext}%
nipkow@10236
    35
\noindent
paulson@10299
    36
For convenience we include the following easy lemmas as simplification rules:%
nipkow@10236
    37
\end{isamarkuptext}%
wenzelm@11866
    38
\isamarkuptrue%
nipkow@10236
    39
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}x\ {\isasymnoteq}\ a{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ b{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}x\ {\isasymnoteq}\ b{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ a{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@11866
    40
\isamarkupfalse%
wenzelm@11866
    41
\isacommand{by}\ {\isacharparenleft}case{\isacharunderscore}tac\ x{\isacharcomma}\ auto{\isacharparenright}\isamarkupfalse%
wenzelm@11866
    42
%
nipkow@10236
    43
\begin{isamarkuptext}%
nipkow@10236
    44
\noindent
nipkow@10236
    45
Words over this alphabet are of type \isa{alfa\ list}, and
paulson@10878
    46
the three nonterminals are declared as sets of such words:%
nipkow@10236
    47
\end{isamarkuptext}%
wenzelm@11866
    48
\isamarkuptrue%
nipkow@10217
    49
\isacommand{consts}\ S\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline
nipkow@10217
    50
\ \ \ \ \ \ \ A\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline
wenzelm@11866
    51
\ \ \ \ \ \ \ B\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
    52
%
nipkow@10236
    53
\begin{isamarkuptext}%
nipkow@10236
    54
\noindent
paulson@10878
    55
The productions above are recast as a \emph{mutual} inductive
nipkow@10242
    56
definition\index{inductive definition!simultaneous}
paulson@10878
    57
of \isa{S}, \isa{A} and~\isa{B}:%
nipkow@10236
    58
\end{isamarkuptext}%
wenzelm@11866
    59
\isamarkuptrue%
nipkow@10217
    60
\isacommand{inductive}\ S\ A\ B\isanewline
nipkow@10217
    61
\isakeyword{intros}\isanewline
nipkow@10236
    62
\ \ {\isachardoublequote}{\isacharbrackleft}{\isacharbrackright}\ {\isasymin}\ S{\isachardoublequote}\isanewline
nipkow@10236
    63
\ \ {\isachardoublequote}w\ {\isasymin}\ A\ {\isasymLongrightarrow}\ b{\isacharhash}w\ {\isasymin}\ S{\isachardoublequote}\isanewline
nipkow@10236
    64
\ \ {\isachardoublequote}w\ {\isasymin}\ B\ {\isasymLongrightarrow}\ a{\isacharhash}w\ {\isasymin}\ S{\isachardoublequote}\isanewline
nipkow@10217
    65
\isanewline
nipkow@10236
    66
\ \ {\isachardoublequote}w\ {\isasymin}\ S\ \ \ \ \ \ \ \ {\isasymLongrightarrow}\ a{\isacharhash}w\ \ \ {\isasymin}\ A{\isachardoublequote}\isanewline
nipkow@10236
    67
\ \ {\isachardoublequote}{\isasymlbrakk}\ v{\isasymin}A{\isacharsemicolon}\ w{\isasymin}A\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ b{\isacharhash}v{\isacharat}w\ {\isasymin}\ A{\isachardoublequote}\isanewline
nipkow@10217
    68
\isanewline
nipkow@10236
    69
\ \ {\isachardoublequote}w\ {\isasymin}\ S\ \ \ \ \ \ \ \ \ \ \ \ {\isasymLongrightarrow}\ b{\isacharhash}w\ \ \ {\isasymin}\ B{\isachardoublequote}\isanewline
wenzelm@11866
    70
\ \ {\isachardoublequote}{\isasymlbrakk}\ v\ {\isasymin}\ B{\isacharsemicolon}\ w\ {\isasymin}\ B\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ a{\isacharhash}v{\isacharat}w\ {\isasymin}\ B{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
    71
%
nipkow@10236
    72
\begin{isamarkuptext}%
nipkow@10236
    73
\noindent
paulson@10878
    74
First we show that all words in \isa{S} contain the same number of \isa{a}'s and \isa{b}'s. Since the definition of \isa{S} is by mutual
paulson@10878
    75
induction, so is the proof: we show at the same time that all words in
nipkow@10236
    76
\isa{A} contain one more \isa{a} than \isa{b} and all words in \isa{B} contains one more \isa{b} than \isa{a}.%
nipkow@10236
    77
\end{isamarkuptext}%
wenzelm@11866
    78
\isamarkuptrue%
nipkow@10236
    79
\isacommand{lemma}\ correctness{\isacharcolon}\isanewline
nipkow@10236
    80
\ \ {\isachardoublequote}{\isacharparenleft}w\ {\isasymin}\ S\ {\isasymlongrightarrow}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}{\isacharparenright}\ \ \ \ \ {\isasymand}\isanewline
nipkow@10237
    81
\ \ \ {\isacharparenleft}w\ {\isasymin}\ A\ {\isasymlongrightarrow}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}\ {\isasymand}\isanewline
wenzelm@11866
    82
\ \ \ {\isacharparenleft}w\ {\isasymin}\ B\ {\isasymlongrightarrow}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
    83
%
nipkow@10236
    84
\begin{isamarkuptxt}%
nipkow@10236
    85
\noindent
nipkow@10283
    86
These propositions are expressed with the help of the predefined \isa{filter} function on lists, which has the convenient syntax \isa{{\isacharbrackleft}x{\isasymin}xs{\isachardot}\ P\ x{\isacharbrackright}}, the list of all elements \isa{x} in \isa{xs} such that \isa{P\ x}
paulson@10878
    87
holds. Remember that on lists \isa{size} and \isa{length} are synonymous.
nipkow@10236
    88
nipkow@10236
    89
The proof itself is by rule induction and afterwards automatic:%
nipkow@10236
    90
\end{isamarkuptxt}%
wenzelm@11866
    91
\isamarkuptrue%
wenzelm@11866
    92
\isacommand{by}\ {\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}induct{\isacharcomma}\ auto{\isacharparenright}\isamarkupfalse%
wenzelm@11866
    93
%
nipkow@10236
    94
\begin{isamarkuptext}%
nipkow@10236
    95
\noindent
nipkow@10236
    96
This may seem surprising at first, and is indeed an indication of the power
nipkow@10236
    97
of inductive definitions. But it is also quite straightforward. For example,
nipkow@10236
    98
consider the production $A \to b A A$: if $v,w \in A$ and the elements of $A$
paulson@10878
    99
contain one more $a$ than~$b$'s, then $bvw$ must again contain one more $a$
paulson@10878
   100
than~$b$'s.
nipkow@10236
   101
nipkow@10236
   102
As usual, the correctness of syntactic descriptions is easy, but completeness
nipkow@10236
   103
is hard: does \isa{S} contain \emph{all} words with an equal number of
nipkow@10236
   104
\isa{a}'s and \isa{b}'s? It turns out that this proof requires the
paulson@10878
   105
following lemma: every string with two more \isa{a}'s than \isa{b}'s can be cut somewhere such that each half has one more \isa{a} than
nipkow@10236
   106
\isa{b}. This is best seen by imagining counting the difference between the
nipkow@10283
   107
number of \isa{a}'s and \isa{b}'s starting at the left end of the
nipkow@10283
   108
word. We start with 0 and end (at the right end) with 2. Since each move to the
nipkow@10236
   109
right increases or decreases the difference by 1, we must have passed through
nipkow@10236
   110
1 on our way from 0 to 2. Formally, we appeal to the following discrete
nipkow@10236
   111
intermediate value theorem \isa{nat{\isadigit{0}}{\isacharunderscore}intermed{\isacharunderscore}int{\isacharunderscore}val}
nipkow@10236
   112
\begin{isabelle}%
paulson@11870
   113
\ \ \ \ \ {\isasymlbrakk}{\isasymforall}i{\isachardot}\ i\ {\isacharless}\ n\ {\isasymlongrightarrow}\ {\isasymbar}f\ {\isacharparenleft}i\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}\ {\isacharminus}\ f\ i{\isasymbar}\ {\isasymle}\ {\isadigit{1}}{\isacharsemicolon}\ f\ {\isadigit{0}}\ {\isasymle}\ k{\isacharsemicolon}\ k\ {\isasymle}\ f\ n{\isasymrbrakk}\isanewline
wenzelm@10950
   114
\isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isasymexists}i{\isachardot}\ i\ {\isasymle}\ n\ {\isasymand}\ f\ i\ {\isacharequal}\ k%
nipkow@10236
   115
\end{isabelle}
nipkow@10236
   116
where \isa{f} is of type \isa{nat\ {\isasymRightarrow}\ int}, \isa{int} are the integers,
nipkow@11308
   117
\isa{{\isasymbar}{\isachardot}{\isasymbar}} is the absolute value function\footnote{See
nipkow@11308
   118
Table~\ref{tab:ascii} in the Appendix for the correct \textsc{ascii}
wenzelm@11708
   119
syntax.}, and \isa{{\isadigit{1}}} is the integer 1 (see \S\ref{sec:numbers}).
nipkow@10236
   120
nipkow@11147
   121
First we show that our specific function, the difference between the
nipkow@10236
   122
numbers of \isa{a}'s and \isa{b}'s, does indeed only change by 1 in every
nipkow@10236
   123
move to the right. At this point we also start generalizing from \isa{a}'s
nipkow@10236
   124
and \isa{b}'s to an arbitrary property \isa{P}. Otherwise we would have
nipkow@10236
   125
to prove the desired lemma twice, once as stated above and once with the
nipkow@10236
   126
roles of \isa{a}'s and \isa{b}'s interchanged.%
nipkow@10236
   127
\end{isamarkuptext}%
wenzelm@11866
   128
\isamarkuptrue%
nipkow@10236
   129
\isacommand{lemma}\ step{\isadigit{1}}{\isacharcolon}\ {\isachardoublequote}{\isasymforall}i\ {\isacharless}\ size\ w{\isachardot}\isanewline
nipkow@10608
   130
\ \ {\isasymbar}{\isacharparenleft}int{\isacharparenleft}size{\isacharbrackleft}x{\isasymin}take\ {\isacharparenleft}i{\isacharplus}{\isadigit{1}}{\isacharparenright}\ w{\isachardot}\ P\ x{\isacharbrackright}{\isacharparenright}{\isacharminus}int{\isacharparenleft}size{\isacharbrackleft}x{\isasymin}take\ {\isacharparenleft}i{\isacharplus}{\isadigit{1}}{\isacharparenright}\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharparenright}{\isacharparenright}\isanewline
paulson@11870
   131
\ \ \ {\isacharminus}\ {\isacharparenleft}int{\isacharparenleft}size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ P\ x{\isacharbrackright}{\isacharparenright}{\isacharminus}int{\isacharparenleft}size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharparenright}{\isacharparenright}{\isasymbar}\ {\isasymle}\ {\isadigit{1}}{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
   132
%
nipkow@10236
   133
\begin{isamarkuptxt}%
nipkow@10236
   134
\noindent
nipkow@10236
   135
The lemma is a bit hard to read because of the coercion function
nipkow@11147
   136
\isa{int\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ int}. It is required because \isa{size} returns
paulson@10878
   137
a natural number, but subtraction on type~\isa{nat} will do the wrong thing.
nipkow@10236
   138
Function \isa{take} is predefined and \isa{take\ i\ xs} is the prefix of
paulson@10878
   139
length \isa{i} of \isa{xs}; below we also need \isa{drop\ i\ xs}, which
nipkow@10236
   140
is what remains after that prefix has been dropped from \isa{xs}.
nipkow@10236
   141
nipkow@10236
   142
The proof is by induction on \isa{w}, with a trivial base case, and a not
nipkow@10236
   143
so trivial induction step. Since it is essentially just arithmetic, we do not
nipkow@10236
   144
discuss it.%
nipkow@10236
   145
\end{isamarkuptxt}%
wenzelm@11866
   146
\isamarkuptrue%
nipkow@12332
   147
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ w{\isacharparenright}\isanewline
wenzelm@11866
   148
\ \isamarkupfalse%
wenzelm@11866
   149
\isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline
wenzelm@11866
   150
\isamarkupfalse%
wenzelm@11866
   151
\isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}\ zabs{\isacharunderscore}def\ take{\isacharunderscore}Cons\ split{\isacharcolon}\ nat{\isachardot}split\ if{\isacharunderscore}splits{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   152
%
nipkow@10236
   153
\begin{isamarkuptext}%
paulson@11494
   154
Finally we come to the above-mentioned lemma about cutting in half a word with two more elements of one sort than of the other sort:%
nipkow@10236
   155
\end{isamarkuptext}%
wenzelm@11866
   156
\isamarkuptrue%
nipkow@10236
   157
\isacommand{lemma}\ part{\isadigit{1}}{\isacharcolon}\isanewline
nipkow@10236
   158
\ {\isachardoublequote}size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{2}}\ {\isasymLongrightarrow}\isanewline
wenzelm@11866
   159
\ \ {\isasymexists}i{\isasymle}size\ w{\isachardot}\ size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{1}}{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
   160
%
nipkow@10236
   161
\begin{isamarkuptxt}%
nipkow@10236
   162
\noindent
paulson@10878
   163
This is proved by \isa{force} with the help of the intermediate value theorem,
nipkow@10608
   164
instantiated appropriately and with its first premise disposed of by lemma
nipkow@10608
   165
\isa{step{\isadigit{1}}}:%
nipkow@10236
   166
\end{isamarkuptxt}%
wenzelm@11866
   167
\isamarkuptrue%
paulson@11870
   168
\isacommand{apply}{\isacharparenleft}insert\ nat{\isadigit{0}}{\isacharunderscore}intermed{\isacharunderscore}int{\isacharunderscore}val{\isacharbrackleft}OF\ step{\isadigit{1}}{\isacharcomma}\ of\ {\isachardoublequote}P{\isachardoublequote}\ {\isachardoublequote}w{\isachardoublequote}\ {\isachardoublequote}{\isadigit{1}}{\isachardoublequote}{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   169
\isamarkupfalse%
wenzelm@11866
   170
\isacommand{by}\ force\isamarkupfalse%
wenzelm@11866
   171
%
nipkow@10236
   172
\begin{isamarkuptext}%
nipkow@10236
   173
\noindent
nipkow@10236
   174
nipkow@10236
   175
Lemma \isa{part{\isadigit{1}}} tells us only about the prefix \isa{take\ i\ w}.
paulson@10878
   176
An easy lemma deals with the suffix \isa{drop\ i\ w}:%
nipkow@10236
   177
\end{isamarkuptext}%
wenzelm@11866
   178
\isamarkuptrue%
nipkow@10236
   179
\isacommand{lemma}\ part{\isadigit{2}}{\isacharcolon}\isanewline
nipkow@10236
   180
\ \ {\isachardoublequote}{\isasymlbrakk}size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\isanewline
nipkow@10236
   181
\ \ \ \ size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{2}}{\isacharsemicolon}\isanewline
nipkow@10236
   182
\ \ \ \ size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}take\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{1}}{\isasymrbrakk}\isanewline
nipkow@10236
   183
\ \ \ {\isasymLongrightarrow}\ size{\isacharbrackleft}x{\isasymin}drop\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}drop\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{1}}{\isachardoublequote}\isanewline
wenzelm@11866
   184
\isamarkupfalse%
wenzelm@11866
   185
\isacommand{by}{\isacharparenleft}simp\ del{\isacharcolon}append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   186
%
nipkow@10236
   187
\begin{isamarkuptext}%
nipkow@10236
   188
\noindent
nipkow@11257
   189
In the proof we have disabled the normally useful lemma
paulson@10878
   190
\begin{isabelle}
paulson@10878
   191
\isa{take\ n\ xs\ {\isacharat}\ drop\ n\ xs\ {\isacharequal}\ xs}
paulson@10878
   192
\rulename{append_take_drop_id}
paulson@10878
   193
\end{isabelle}
nipkow@11257
   194
to allow the simplifier to apply the following lemma instead:
nipkow@11257
   195
\begin{isabelle}%
nipkow@11257
   196
\ \ \ \ \ {\isacharbrackleft}x{\isasymin}xs{\isacharat}ys{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}x{\isasymin}xs{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharat}\ {\isacharbrackleft}x{\isasymin}ys{\isachardot}\ P\ x{\isacharbrackright}%
nipkow@11257
   197
\end{isabelle}
nipkow@10236
   198
nipkow@10236
   199
To dispose of trivial cases automatically, the rules of the inductive
nipkow@10236
   200
definition are declared simplification rules:%
nipkow@10236
   201
\end{isamarkuptext}%
wenzelm@11866
   202
\isamarkuptrue%
wenzelm@11866
   203
\isacommand{declare}\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharbrackleft}simp{\isacharbrackright}\isamarkupfalse%
wenzelm@11866
   204
%
nipkow@10236
   205
\begin{isamarkuptext}%
nipkow@10236
   206
\noindent
nipkow@10236
   207
This could have been done earlier but was not necessary so far.
nipkow@10236
   208
nipkow@10236
   209
The completeness theorem tells us that if a word has the same number of
paulson@10878
   210
\isa{a}'s and \isa{b}'s, then it is in \isa{S}, and similarly 
paulson@10878
   211
for \isa{A} and \isa{B}:%
nipkow@10236
   212
\end{isamarkuptext}%
wenzelm@11866
   213
\isamarkuptrue%
nipkow@10236
   214
\isacommand{theorem}\ completeness{\isacharcolon}\isanewline
nipkow@10236
   215
\ \ {\isachardoublequote}{\isacharparenleft}size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}\ \ \ \ \ {\isasymlongrightarrow}\ w\ {\isasymin}\ S{\isacharparenright}\ {\isasymand}\isanewline
nipkow@10237
   216
\ \ \ {\isacharparenleft}size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}\ {\isasymlongrightarrow}\ w\ {\isasymin}\ A{\isacharparenright}\ {\isasymand}\isanewline
wenzelm@11866
   217
\ \ \ {\isacharparenleft}size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}b{\isacharbrackright}\ {\isacharequal}\ size{\isacharbrackleft}x{\isasymin}w{\isachardot}\ x{\isacharequal}a{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}\ {\isasymlongrightarrow}\ w\ {\isasymin}\ B{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
wenzelm@11866
   218
%
nipkow@10236
   219
\begin{isamarkuptxt}%
nipkow@10236
   220
\noindent
nipkow@10236
   221
The proof is by induction on \isa{w}. Structural induction would fail here
nipkow@10236
   222
because, as we can see from the grammar, we need to make bigger steps than
nipkow@10236
   223
merely appending a single letter at the front. Hence we induct on the length
nipkow@10236
   224
of \isa{w}, using the induction rule \isa{length{\isacharunderscore}induct}:%
nipkow@10236
   225
\end{isamarkuptxt}%
wenzelm@11866
   226
\isamarkuptrue%
wenzelm@11866
   227
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ w\ rule{\isacharcolon}\ length{\isacharunderscore}induct{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   228
\isamarkupfalse%
wenzelm@11866
   229
%
nipkow@10236
   230
\begin{isamarkuptxt}%
nipkow@10236
   231
\noindent
nipkow@10236
   232
The \isa{rule} parameter tells \isa{induct{\isacharunderscore}tac} explicitly which induction
nipkow@10236
   233
rule to use. For details see \S\ref{sec:complete-ind} below.
nipkow@10236
   234
In this case the result is that we may assume the lemma already
nipkow@10236
   235
holds for all words shorter than \isa{w}.
nipkow@10236
   236
nipkow@10236
   237
The proof continues with a case distinction on \isa{w},
paulson@11494
   238
on whether \isa{w} is empty or not.%
nipkow@10236
   239
\end{isamarkuptxt}%
wenzelm@11866
   240
\isamarkuptrue%
nipkow@10236
   241
\isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ w{\isacharparenright}\isanewline
wenzelm@11866
   242
\ \isamarkupfalse%
wenzelm@11866
   243
\isacommand{apply}{\isacharparenleft}simp{\isacharunderscore}all{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   244
\isamarkupfalse%
wenzelm@11866
   245
%
nipkow@10236
   246
\begin{isamarkuptxt}%
nipkow@10236
   247
\noindent
nipkow@11257
   248
Simplification disposes of the base case and leaves only a conjunction
nipkow@11257
   249
of two step cases to be proved:
paulson@10878
   250
if \isa{w\ {\isacharequal}\ a\ {\isacharhash}\ v} and \begin{isabelle}%
paulson@10878
   251
\ \ \ \ \ length\ {\isacharbrackleft}x{\isasymin}v\ {\isachardot}\ x\ {\isacharequal}\ a{\isacharbrackright}\ {\isacharequal}\ length\ {\isacharbrackleft}x{\isasymin}v\ {\isachardot}\ x\ {\isacharequal}\ b{\isacharbrackright}\ {\isacharplus}\ {\isadigit{2}}%
paulson@10878
   252
\end{isabelle} then
nipkow@10236
   253
\isa{b\ {\isacharhash}\ v\ {\isasymin}\ A}, and similarly for \isa{w\ {\isacharequal}\ b\ {\isacharhash}\ v}.
nipkow@10236
   254
We only consider the first case in detail.
nipkow@10236
   255
nipkow@11257
   256
After breaking the conjunction up into two cases, we can apply
nipkow@10236
   257
\isa{part{\isadigit{1}}} to the assumption that \isa{w} contains two more \isa{a}'s than \isa{b}'s.%
nipkow@10236
   258
\end{isamarkuptxt}%
wenzelm@11866
   259
\isamarkuptrue%
nipkow@10217
   260
\isacommand{apply}{\isacharparenleft}rule\ conjI{\isacharparenright}\isanewline
wenzelm@11866
   261
\ \isamarkupfalse%
wenzelm@11866
   262
\isacommand{apply}{\isacharparenleft}clarify{\isacharparenright}\isanewline
wenzelm@11866
   263
\ \isamarkupfalse%
wenzelm@11866
   264
\isacommand{apply}{\isacharparenleft}frule\ part{\isadigit{1}}{\isacharbrackleft}of\ {\isachardoublequote}{\isasymlambda}x{\isachardot}\ x{\isacharequal}a{\isachardoublequote}{\isacharcomma}\ simplified{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   265
\ \isamarkupfalse%
wenzelm@11866
   266
\isacommand{apply}{\isacharparenleft}clarify{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   267
%
nipkow@10236
   268
\begin{isamarkuptxt}%
nipkow@10236
   269
\noindent
nipkow@10236
   270
This yields an index \isa{i\ {\isasymle}\ length\ v} such that
paulson@10878
   271
\begin{isabelle}%
paulson@10878
   272
\ \ \ \ \ length\ {\isacharbrackleft}x{\isasymin}take\ i\ v\ {\isachardot}\ x\ {\isacharequal}\ a{\isacharbrackright}\ {\isacharequal}\ length\ {\isacharbrackleft}x{\isasymin}take\ i\ v\ {\isachardot}\ x\ {\isacharequal}\ b{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}%
paulson@10878
   273
\end{isabelle}
nipkow@11147
   274
With the help of \isa{part{\isadigit{2}}} it follows that
paulson@10878
   275
\begin{isabelle}%
paulson@10878
   276
\ \ \ \ \ length\ {\isacharbrackleft}x{\isasymin}drop\ i\ v\ {\isachardot}\ x\ {\isacharequal}\ a{\isacharbrackright}\ {\isacharequal}\ length\ {\isacharbrackleft}x{\isasymin}drop\ i\ v\ {\isachardot}\ x\ {\isacharequal}\ b{\isacharbrackright}\ {\isacharplus}\ {\isadigit{1}}%
paulson@10878
   277
\end{isabelle}%
nipkow@10236
   278
\end{isamarkuptxt}%
wenzelm@11866
   279
\ \isamarkuptrue%
wenzelm@11866
   280
\isacommand{apply}{\isacharparenleft}drule\ part{\isadigit{2}}{\isacharbrackleft}of\ {\isachardoublequote}{\isasymlambda}x{\isachardot}\ x{\isacharequal}a{\isachardoublequote}{\isacharcomma}\ simplified{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   281
\ \ \isamarkupfalse%
wenzelm@11866
   282
\isacommand{apply}{\isacharparenleft}assumption{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   283
%
nipkow@10236
   284
\begin{isamarkuptxt}%
nipkow@10236
   285
\noindent
nipkow@10236
   286
Now it is time to decompose \isa{v} in the conclusion \isa{b\ {\isacharhash}\ v\ {\isasymin}\ A}
nipkow@11257
   287
into \isa{take\ i\ v\ {\isacharat}\ drop\ i\ v},%
nipkow@11257
   288
\end{isamarkuptxt}%
wenzelm@11866
   289
\ \isamarkuptrue%
wenzelm@11866
   290
\isacommand{apply}{\isacharparenleft}rule{\isacharunderscore}tac\ n{\isadigit{1}}{\isacharequal}i\ \isakeyword{and}\ t{\isacharequal}v\ \isakeyword{in}\ subst{\isacharbrackleft}OF\ append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id{\isacharbrackright}{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   291
%
nipkow@11257
   292
\begin{isamarkuptxt}%
nipkow@11257
   293
\noindent
nipkow@11257
   294
(the variables \isa{n{\isadigit{1}}} and \isa{t} are the result of composing the
nipkow@11257
   295
theorems \isa{subst} and \isa{append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id})
nipkow@10236
   296
after which the appropriate rule of the grammar reduces the goal
nipkow@10236
   297
to the two subgoals \isa{take\ i\ v\ {\isasymin}\ A} and \isa{drop\ i\ v\ {\isasymin}\ A}:%
nipkow@10236
   298
\end{isamarkuptxt}%
wenzelm@11866
   299
\ \isamarkuptrue%
wenzelm@11866
   300
\isacommand{apply}{\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   301
%
nipkow@10236
   302
\begin{isamarkuptxt}%
nipkow@10236
   303
Both subgoals follow from the induction hypothesis because both \isa{take\ i\ v} and \isa{drop\ i\ v} are shorter than \isa{w}:%
nipkow@10236
   304
\end{isamarkuptxt}%
wenzelm@11866
   305
\ \ \isamarkuptrue%
wenzelm@11866
   306
\isacommand{apply}{\isacharparenleft}force\ simp\ add{\isacharcolon}\ min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj{\isacharparenright}\isanewline
wenzelm@11866
   307
\ \isamarkupfalse%
wenzelm@11866
   308
\isacommand{apply}{\isacharparenleft}force\ split\ add{\isacharcolon}\ nat{\isacharunderscore}diff{\isacharunderscore}split{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   309
%
nipkow@10236
   310
\begin{isamarkuptxt}%
paulson@10878
   311
The case \isa{w\ {\isacharequal}\ b\ {\isacharhash}\ v} is proved analogously:%
nipkow@10236
   312
\end{isamarkuptxt}%
wenzelm@11866
   313
\isamarkuptrue%
nipkow@10217
   314
\isacommand{apply}{\isacharparenleft}clarify{\isacharparenright}\isanewline
wenzelm@11866
   315
\isamarkupfalse%
nipkow@10236
   316
\isacommand{apply}{\isacharparenleft}frule\ part{\isadigit{1}}{\isacharbrackleft}of\ {\isachardoublequote}{\isasymlambda}x{\isachardot}\ x{\isacharequal}b{\isachardoublequote}{\isacharcomma}\ simplified{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   317
\isamarkupfalse%
nipkow@11257
   318
\isacommand{apply}{\isacharparenleft}clarify{\isacharparenright}\isanewline
wenzelm@11866
   319
\isamarkupfalse%
nipkow@10236
   320
\isacommand{apply}{\isacharparenleft}drule\ part{\isadigit{2}}{\isacharbrackleft}of\ {\isachardoublequote}{\isasymlambda}x{\isachardot}\ x{\isacharequal}b{\isachardoublequote}{\isacharcomma}\ simplified{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   321
\ \isamarkupfalse%
wenzelm@11866
   322
\isacommand{apply}{\isacharparenleft}assumption{\isacharparenright}\isanewline
wenzelm@11866
   323
\isamarkupfalse%
nipkow@10236
   324
\isacommand{apply}{\isacharparenleft}rule{\isacharunderscore}tac\ n{\isadigit{1}}{\isacharequal}i\ \isakeyword{and}\ t{\isacharequal}v\ \isakeyword{in}\ subst{\isacharbrackleft}OF\ append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id{\isacharbrackright}{\isacharparenright}\isanewline
wenzelm@11866
   325
\isamarkupfalse%
nipkow@10217
   326
\isacommand{apply}{\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharparenright}\isanewline
wenzelm@11866
   327
\ \isamarkupfalse%
wenzelm@11866
   328
\isacommand{apply}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj{\isacharparenright}\isanewline
wenzelm@11866
   329
\isamarkupfalse%
wenzelm@11866
   330
\isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj\ split\ add{\isacharcolon}\ nat{\isacharunderscore}diff{\isacharunderscore}split{\isacharparenright}\isamarkupfalse%
wenzelm@11866
   331
%
nipkow@10236
   332
\begin{isamarkuptext}%
paulson@10878
   333
We conclude this section with a comparison of our proof with 
paulson@11494
   334
Hopcroft\index{Hopcroft, J. E.} and Ullman's\index{Ullman, J. D.}
paulson@11494
   335
\cite[p.\ts81]{HopcroftUllman}.
paulson@11494
   336
For a start, the textbook
nipkow@11257
   337
grammar, for no good reason, excludes the empty word, thus complicating
nipkow@11257
   338
matters just a little bit: they have 8 instead of our 7 productions.
nipkow@10236
   339
nipkow@11158
   340
More importantly, the proof itself is different: rather than
nipkow@11158
   341
separating the two directions, they perform one induction on the
nipkow@11158
   342
length of a word. This deprives them of the beauty of rule induction,
nipkow@11158
   343
and in the easy direction (correctness) their reasoning is more
nipkow@11158
   344
detailed than our \isa{auto}. For the hard part (completeness), they
nipkow@11158
   345
consider just one of the cases that our \isa{simp{\isacharunderscore}all} disposes of
nipkow@11158
   346
automatically. Then they conclude the proof by saying about the
nipkow@11158
   347
remaining cases: ``We do this in a manner similar to our method of
nipkow@11158
   348
proof for part (1); this part is left to the reader''. But this is
nipkow@11158
   349
precisely the part that requires the intermediate value theorem and
nipkow@11158
   350
thus is not at all similar to the other cases (which are automatic in
nipkow@11158
   351
Isabelle). The authors are at least cavalier about this point and may
nipkow@11158
   352
even have overlooked the slight difficulty lurking in the omitted
paulson@11494
   353
cases.  Such errors are found in many pen-and-paper proofs when they
paulson@11494
   354
are scrutinized formally.%
paulson@11494
   355
\index{grammars!defining inductively|)}%
nipkow@10236
   356
\end{isamarkuptext}%
wenzelm@11866
   357
\isamarkuptrue%
wenzelm@11866
   358
\isamarkupfalse%
nipkow@10217
   359
\end{isabellebody}%
nipkow@10217
   360
%%% Local Variables:
nipkow@10217
   361
%%% mode: latex
nipkow@10217
   362
%%% TeX-master: "root"
nipkow@10217
   363
%%% End: