1 % |
1 % |
2 \begin{isabellebody}% |
2 \begin{isabellebody}% |
3 \def\isabellecontext{AB}% |
3 \def\isabellecontext{AB}% |
4 % |
4 % |
5 \isamarkupsection{Case study: A context free grammar% |
5 \isamarkupsection{Case Study: A Context Free Grammar% |
6 } |
6 } |
7 % |
7 % |
8 \begin{isamarkuptext}% |
8 \begin{isamarkuptext}% |
9 \label{sec:CFG} |
9 \label{sec:CFG} |
10 Grammars are nothing but shorthands for inductive definitions of nonterminals |
10 Grammars are nothing but shorthands for inductive definitions of nonterminals |
11 which represent sets of strings. For example, the production |
11 which represent sets of strings. For example, the production |
12 $A \to B c$ is short for |
12 $A \to B c$ is short for |
13 \[ w \in B \Longrightarrow wc \in A \] |
13 \[ w \in B \Longrightarrow wc \in A \] |
14 This section demonstrates this idea with a standard example |
14 This section demonstrates this idea with an example |
15 \cite[p.\ 81]{HopcroftUllman}, a grammar for generating all words with an |
15 due to Hopcroft and Ullman, a grammar for generating all words with an |
16 equal number of $a$'s and $b$'s: |
16 equal number of $a$'s and~$b$'s: |
17 \begin{eqnarray} |
17 \begin{eqnarray} |
18 S &\to& \epsilon \mid b A \mid a B \nonumber\\ |
18 S &\to& \epsilon \mid b A \mid a B \nonumber\\ |
19 A &\to& a S \mid b A A \nonumber\\ |
19 A &\to& a S \mid b A A \nonumber\\ |
20 B &\to& b S \mid a B B \nonumber |
20 B &\to& b S \mid a B B \nonumber |
21 \end{eqnarray} |
21 \end{eqnarray} |
22 At the end we say a few words about the relationship of the formalization |
22 At the end we say a few words about the relationship between |
23 and the text in the book~\cite[p.\ 81]{HopcroftUllman}. |
23 the original proof \cite[p.\ts81]{HopcroftUllman} and our formal version. |
24 |
24 |
25 We start by fixing the alphabet, which consists only of \isa{a}'s |
25 We start by fixing the alphabet, which consists only of \isa{a}'s |
26 and \isa{b}'s:% |
26 and~\isa{b}'s:% |
27 \end{isamarkuptext}% |
27 \end{isamarkuptext}% |
28 \isacommand{datatype}\ alfa\ {\isacharequal}\ a\ {\isacharbar}\ b% |
28 \isacommand{datatype}\ alfa\ {\isacharequal}\ a\ {\isacharbar}\ b% |
29 \begin{isamarkuptext}% |
29 \begin{isamarkuptext}% |
30 \noindent |
30 \noindent |
31 For convenience we include the following easy lemmas as simplification rules:% |
31 For convenience we include the following easy lemmas as simplification rules:% |
32 \end{isamarkuptext}% |
32 \end{isamarkuptext}% |
33 \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 |
33 \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 |
34 \isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ x{\isacharparenright}\isanewline |
34 \isacommand{by}\ {\isacharparenleft}case{\isacharunderscore}tac\ x{\isacharcomma}\ auto{\isacharparenright}% |
35 \isacommand{by}{\isacharparenleft}auto{\isacharparenright}% |
|
36 \begin{isamarkuptext}% |
35 \begin{isamarkuptext}% |
37 \noindent |
36 \noindent |
38 Words over this alphabet are of type \isa{alfa\ list}, and |
37 Words over this alphabet are of type \isa{alfa\ list}, and |
39 the three nonterminals are declare as sets of such words:% |
38 the three nonterminals are declared as sets of such words:% |
40 \end{isamarkuptext}% |
39 \end{isamarkuptext}% |
41 \isacommand{consts}\ S\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline |
40 \isacommand{consts}\ S\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline |
42 \ \ \ \ \ \ \ A\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline |
41 \ \ \ \ \ \ \ A\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}\isanewline |
43 \ \ \ \ \ \ \ B\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}% |
42 \ \ \ \ \ \ \ B\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}alfa\ list\ set{\isachardoublequote}% |
44 \begin{isamarkuptext}% |
43 \begin{isamarkuptext}% |
45 \noindent |
44 \noindent |
46 The above productions are recast as a \emph{simultaneous} inductive |
45 The productions above are recast as a \emph{mutual} inductive |
47 definition\index{inductive definition!simultaneous} |
46 definition\index{inductive definition!simultaneous} |
48 of \isa{S}, \isa{A} and \isa{B}:% |
47 of \isa{S}, \isa{A} and~\isa{B}:% |
49 \end{isamarkuptext}% |
48 \end{isamarkuptext}% |
50 \isacommand{inductive}\ S\ A\ B\isanewline |
49 \isacommand{inductive}\ S\ A\ B\isanewline |
51 \isakeyword{intros}\isanewline |
50 \isakeyword{intros}\isanewline |
52 \ \ {\isachardoublequote}{\isacharbrackleft}{\isacharbrackright}\ {\isasymin}\ S{\isachardoublequote}\isanewline |
51 \ \ {\isachardoublequote}{\isacharbrackleft}{\isacharbrackright}\ {\isasymin}\ S{\isachardoublequote}\isanewline |
53 \ \ {\isachardoublequote}w\ {\isasymin}\ A\ {\isasymLongrightarrow}\ b{\isacharhash}w\ {\isasymin}\ S{\isachardoublequote}\isanewline |
52 \ \ {\isachardoublequote}w\ {\isasymin}\ A\ {\isasymLongrightarrow}\ b{\isacharhash}w\ {\isasymin}\ S{\isachardoublequote}\isanewline |
58 \isanewline |
57 \isanewline |
59 \ \ {\isachardoublequote}w\ {\isasymin}\ S\ \ \ \ \ \ \ \ \ \ \ \ {\isasymLongrightarrow}\ b{\isacharhash}w\ \ \ {\isasymin}\ B{\isachardoublequote}\isanewline |
58 \ \ {\isachardoublequote}w\ {\isasymin}\ S\ \ \ \ \ \ \ \ \ \ \ \ {\isasymLongrightarrow}\ b{\isacharhash}w\ \ \ {\isasymin}\ B{\isachardoublequote}\isanewline |
60 \ \ {\isachardoublequote}{\isasymlbrakk}\ v\ {\isasymin}\ B{\isacharsemicolon}\ w\ {\isasymin}\ B\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ a{\isacharhash}v{\isacharat}w\ {\isasymin}\ B{\isachardoublequote}% |
59 \ \ {\isachardoublequote}{\isasymlbrakk}\ v\ {\isasymin}\ B{\isacharsemicolon}\ w\ {\isasymin}\ B\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ a{\isacharhash}v{\isacharat}w\ {\isasymin}\ B{\isachardoublequote}% |
61 \begin{isamarkuptext}% |
60 \begin{isamarkuptext}% |
62 \noindent |
61 \noindent |
63 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 simultaneous |
62 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 |
64 induction, so is this proof: we show at the same time that all words in |
63 induction, so is the proof: we show at the same time that all words in |
65 \isa{A} contain one more \isa{a} than \isa{b} and all words in \isa{B} contains one more \isa{b} than \isa{a}.% |
64 \isa{A} contain one more \isa{a} than \isa{b} and all words in \isa{B} contains one more \isa{b} than \isa{a}.% |
66 \end{isamarkuptext}% |
65 \end{isamarkuptext}% |
67 \isacommand{lemma}\ correctness{\isacharcolon}\isanewline |
66 \isacommand{lemma}\ correctness{\isacharcolon}\isanewline |
68 \ \ {\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 |
67 \ \ {\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 |
69 \ \ \ {\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 |
68 \ \ \ {\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 |
70 \ \ \ {\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}% |
69 \ \ \ {\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}% |
71 \begin{isamarkuptxt}% |
70 \begin{isamarkuptxt}% |
72 \noindent |
71 \noindent |
73 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} |
72 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} |
74 holds. Remember that on lists \isa{size} and \isa{size} are synonymous. |
73 holds. Remember that on lists \isa{size} and \isa{length} are synonymous. |
75 |
74 |
76 The proof itself is by rule induction and afterwards automatic:% |
75 The proof itself is by rule induction and afterwards automatic:% |
77 \end{isamarkuptxt}% |
76 \end{isamarkuptxt}% |
78 \isacommand{apply}{\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}induct{\isacharparenright}\isanewline |
77 \isacommand{by}\ {\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}induct{\isacharcomma}\ auto{\isacharparenright}% |
79 \isacommand{by}{\isacharparenleft}auto{\isacharparenright}% |
|
80 \begin{isamarkuptext}% |
78 \begin{isamarkuptext}% |
81 \noindent |
79 \noindent |
82 This may seem surprising at first, and is indeed an indication of the power |
80 This may seem surprising at first, and is indeed an indication of the power |
83 of inductive definitions. But it is also quite straightforward. For example, |
81 of inductive definitions. But it is also quite straightforward. For example, |
84 consider the production $A \to b A A$: if $v,w \in A$ and the elements of $A$ |
82 consider the production $A \to b A A$: if $v,w \in A$ and the elements of $A$ |
85 contain one more $a$ than $b$'s, then $bvw$ must again contain one more $a$ |
83 contain one more $a$ than~$b$'s, then $bvw$ must again contain one more $a$ |
86 than $b$'s. |
84 than~$b$'s. |
87 |
85 |
88 As usual, the correctness of syntactic descriptions is easy, but completeness |
86 As usual, the correctness of syntactic descriptions is easy, but completeness |
89 is hard: does \isa{S} contain \emph{all} words with an equal number of |
87 is hard: does \isa{S} contain \emph{all} words with an equal number of |
90 \isa{a}'s and \isa{b}'s? It turns out that this proof requires the |
88 \isa{a}'s and \isa{b}'s? It turns out that this proof requires the |
91 following little lemma: every string with two more \isa{a}'s than \isa{b}'s can be cut somehwere such that each half has one more \isa{a} than |
89 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 |
92 \isa{b}. This is best seen by imagining counting the difference between the |
90 \isa{b}. This is best seen by imagining counting the difference between the |
93 number of \isa{a}'s and \isa{b}'s starting at the left end of the |
91 number of \isa{a}'s and \isa{b}'s starting at the left end of the |
94 word. We start with 0 and end (at the right end) with 2. Since each move to the |
92 word. We start with 0 and end (at the right end) with 2. Since each move to the |
95 right increases or decreases the difference by 1, we must have passed through |
93 right increases or decreases the difference by 1, we must have passed through |
96 1 on our way from 0 to 2. Formally, we appeal to the following discrete |
94 1 on our way from 0 to 2. Formally, we appeal to the following discrete |
115 \ \ \ {\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}\ {\isacharhash}{\isadigit{1}}{\isachardoublequote}% |
113 \ \ \ {\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}\ {\isacharhash}{\isadigit{1}}{\isachardoublequote}% |
116 \begin{isamarkuptxt}% |
114 \begin{isamarkuptxt}% |
117 \noindent |
115 \noindent |
118 The lemma is a bit hard to read because of the coercion function |
116 The lemma is a bit hard to read because of the coercion function |
119 \isa{{\isachardoublequote}int\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ int{\isachardoublequote}}. It is required because \isa{size} returns |
117 \isa{{\isachardoublequote}int\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ int{\isachardoublequote}}. It is required because \isa{size} returns |
120 a natural number, but \isa{{\isacharminus}} on \isa{nat} will do the wrong thing. |
118 a natural number, but subtraction on type~\isa{nat} will do the wrong thing. |
121 Function \isa{take} is predefined and \isa{take\ i\ xs} is the prefix of |
119 Function \isa{take} is predefined and \isa{take\ i\ xs} is the prefix of |
122 length \isa{i} of \isa{xs}; below we als need \isa{drop\ i\ xs}, which |
120 length \isa{i} of \isa{xs}; below we also need \isa{drop\ i\ xs}, which |
123 is what remains after that prefix has been dropped from \isa{xs}. |
121 is what remains after that prefix has been dropped from \isa{xs}. |
124 |
122 |
125 The proof is by induction on \isa{w}, with a trivial base case, and a not |
123 The proof is by induction on \isa{w}, with a trivial base case, and a not |
126 so trivial induction step. Since it is essentially just arithmetic, we do not |
124 so trivial induction step. Since it is essentially just arithmetic, we do not |
127 discuss it.% |
125 discuss it.% |
128 \end{isamarkuptxt}% |
126 \end{isamarkuptxt}% |
129 \isacommand{apply}{\isacharparenleft}induct\ w{\isacharparenright}\isanewline |
127 \isacommand{apply}{\isacharparenleft}induct\ w{\isacharparenright}\isanewline |
130 \ \isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline |
128 \ \isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline |
131 \isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}zabs{\isacharunderscore}def\ take{\isacharunderscore}Cons\ split{\isacharcolon}nat{\isachardot}split\ if{\isacharunderscore}splits{\isacharparenright}% |
129 \isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}zabs{\isacharunderscore}def\ take{\isacharunderscore}Cons\ split{\isacharcolon}nat{\isachardot}split\ if{\isacharunderscore}splits{\isacharparenright}% |
132 \begin{isamarkuptext}% |
130 \begin{isamarkuptext}% |
133 Finally we come to the above mentioned lemma about cutting a word with two |
131 Finally we come to the above mentioned lemma about cutting in half a word with two |
134 more elements of one sort than of the other sort into two halves:% |
132 more elements of one sort than of the other sort:% |
135 \end{isamarkuptext}% |
133 \end{isamarkuptext}% |
136 \isacommand{lemma}\ part{\isadigit{1}}{\isacharcolon}\isanewline |
134 \isacommand{lemma}\ part{\isadigit{1}}{\isacharcolon}\isanewline |
137 \ {\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 |
135 \ {\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 |
138 \ \ {\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}% |
136 \ \ {\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}% |
139 \begin{isamarkuptxt}% |
137 \begin{isamarkuptxt}% |
140 \noindent |
138 \noindent |
141 This is proved by force with the help of the intermediate value theorem, |
139 This is proved by \isa{force} with the help of the intermediate value theorem, |
142 instantiated appropriately and with its first premise disposed of by lemma |
140 instantiated appropriately and with its first premise disposed of by lemma |
143 \isa{step{\isadigit{1}}}:% |
141 \isa{step{\isadigit{1}}}:% |
144 \end{isamarkuptxt}% |
142 \end{isamarkuptxt}% |
145 \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}{\isacharhash}{\isadigit{1}}{\isachardoublequote}{\isacharbrackright}{\isacharparenright}\isanewline |
143 \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}{\isacharhash}{\isadigit{1}}{\isachardoublequote}{\isacharbrackright}{\isacharparenright}\isanewline |
146 \isacommand{by}\ force% |
144 \isacommand{by}\ force% |
147 \begin{isamarkuptext}% |
145 \begin{isamarkuptext}% |
148 \noindent |
146 \noindent |
149 |
147 |
150 Lemma \isa{part{\isadigit{1}}} tells us only about the prefix \isa{take\ i\ w}. |
148 Lemma \isa{part{\isadigit{1}}} tells us only about the prefix \isa{take\ i\ w}. |
151 The suffix \isa{drop\ i\ w} is dealt with in the following easy lemma:% |
149 An easy lemma deals with the suffix \isa{drop\ i\ w}:% |
152 \end{isamarkuptext}% |
150 \end{isamarkuptext}% |
153 \isacommand{lemma}\ part{\isadigit{2}}{\isacharcolon}\isanewline |
151 \isacommand{lemma}\ part{\isadigit{2}}{\isacharcolon}\isanewline |
154 \ \ {\isachardoublequote}{\isasymlbrakk}size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\isanewline |
152 \ \ {\isachardoublequote}{\isasymlbrakk}size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ P\ x{\isacharbrackright}\ {\isacharequal}\isanewline |
155 \ \ \ \ size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{2}}{\isacharsemicolon}\isanewline |
153 \ \ \ \ size{\isacharbrackleft}x{\isasymin}take\ i\ w\ {\isacharat}\ drop\ i\ w{\isachardot}\ {\isasymnot}P\ x{\isacharbrackright}{\isacharplus}{\isadigit{2}}{\isacharsemicolon}\isanewline |
156 \ \ \ \ 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 |
154 \ \ \ \ 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 |
157 \ \ \ {\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 |
155 \ \ \ {\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 |
158 \isacommand{by}{\isacharparenleft}simp\ del{\isacharcolon}append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id{\isacharparenright}% |
156 \isacommand{by}{\isacharparenleft}simp\ del{\isacharcolon}append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id{\isacharparenright}% |
159 \begin{isamarkuptext}% |
157 \begin{isamarkuptext}% |
160 \noindent |
158 \noindent |
161 Lemma \isa{append{\isacharunderscore}take{\isacharunderscore}drop{\isacharunderscore}id}, \isa{take\ n\ xs\ {\isacharat}\ drop\ n\ xs\ {\isacharequal}\ xs}, |
159 In the proof, we have had to disable a normally useful lemma: |
162 which is generally useful, needs to be disabled for once. |
160 \begin{isabelle} |
|
161 \isa{take\ n\ xs\ {\isacharat}\ drop\ n\ xs\ {\isacharequal}\ xs} |
|
162 \rulename{append_take_drop_id} |
|
163 \end{isabelle} |
163 |
164 |
164 To dispose of trivial cases automatically, the rules of the inductive |
165 To dispose of trivial cases automatically, the rules of the inductive |
165 definition are declared simplification rules:% |
166 definition are declared simplification rules:% |
166 \end{isamarkuptext}% |
167 \end{isamarkuptext}% |
167 \isacommand{declare}\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharbrackleft}simp{\isacharbrackright}% |
168 \isacommand{declare}\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharbrackleft}simp{\isacharbrackright}% |
168 \begin{isamarkuptext}% |
169 \begin{isamarkuptext}% |
169 \noindent |
170 \noindent |
170 This could have been done earlier but was not necessary so far. |
171 This could have been done earlier but was not necessary so far. |
171 |
172 |
172 The completeness theorem tells us that if a word has the same number of |
173 The completeness theorem tells us that if a word has the same number of |
173 \isa{a}'s and \isa{b}'s, then it is in \isa{S}, and similarly and |
174 \isa{a}'s and \isa{b}'s, then it is in \isa{S}, and similarly |
174 simultaneously for \isa{A} and \isa{B}:% |
175 for \isa{A} and \isa{B}:% |
175 \end{isamarkuptext}% |
176 \end{isamarkuptext}% |
176 \isacommand{theorem}\ completeness{\isacharcolon}\isanewline |
177 \isacommand{theorem}\ completeness{\isacharcolon}\isanewline |
177 \ \ {\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 |
178 \ \ {\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 |
178 \ \ \ {\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 |
179 \ \ \ {\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 |
179 \ \ \ {\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}% |
180 \ \ \ {\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}% |
253 \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 |
260 \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 |
254 \isacommand{apply}{\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharparenright}\isanewline |
261 \isacommand{apply}{\isacharparenleft}rule\ S{\isacharunderscore}A{\isacharunderscore}B{\isachardot}intros{\isacharparenright}\isanewline |
255 \ \isacommand{apply}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj{\isacharparenright}\isanewline |
262 \ \isacommand{apply}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj{\isacharparenright}\isanewline |
256 \isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj\ split\ add{\isacharcolon}\ nat{\isacharunderscore}diff{\isacharunderscore}split{\isacharparenright}% |
263 \isacommand{by}{\isacharparenleft}force\ simp\ add{\isacharcolon}min{\isacharunderscore}less{\isacharunderscore}iff{\isacharunderscore}disj\ split\ add{\isacharcolon}\ nat{\isacharunderscore}diff{\isacharunderscore}split{\isacharparenright}% |
257 \begin{isamarkuptext}% |
264 \begin{isamarkuptext}% |
258 We conclude this section with a comparison of the above proof and the one |
265 We conclude this section with a comparison of our proof with |
259 in the textbook \cite[p.\ 81]{HopcroftUllman}. For a start, the texbook |
266 Hopcroft and Ullman's \cite[p.\ts81]{HopcroftUllman}. For a start, the texbook |
260 grammar, for no good reason, excludes the empty word, which complicates |
267 grammar, for no good reason, excludes the empty word. That complicates |
261 matters just a little bit because we now have 8 instead of our 7 productions. |
268 matters just a little bit because we now have 8 instead of our 7 productions. |
262 |
269 |
263 More importantly, the proof itself is different: rather than separating the |
270 More importantly, the proof itself is different: rather than separating the |
264 two directions, they perform one induction on the length of a word. This |
271 two directions, they perform one induction on the length of a word. This |
265 deprives them of the beauty of rule induction and in the easy direction |
272 deprives them of the beauty of rule induction and in the easy direction |