|
1 (*<*) |
|
2 theory Paper |
|
3 imports |
|
4 "../Lexer" |
|
5 "../Simplifying" |
|
6 "../Positions" |
|
7 |
|
8 "../SizeBound" |
|
9 "HOL-Library.LaTeXsugar" |
|
10 begin |
|
11 |
|
12 lemma Suc_0_fold: |
|
13 "Suc 0 = 1" |
|
14 by simp |
|
15 |
|
16 |
|
17 |
|
18 declare [[show_question_marks = false]] |
|
19 |
|
20 syntax (latex output) |
|
21 "_Collect" :: "pttrn => bool => 'a set" ("(1{_ \<^latex>\<open>\\mbox{\\boldmath$\\mid$}\<close> _})") |
|
22 "_CollectIn" :: "pttrn => 'a set => bool => 'a set" ("(1{_ \<in> _ |e _})") |
|
23 |
|
24 syntax |
|
25 "_Not_Ex" :: "idts \<Rightarrow> bool \<Rightarrow> bool" ("(3\<nexists>_.a./ _)" [0, 10] 10) |
|
26 "_Not_Ex1" :: "pttrn \<Rightarrow> bool \<Rightarrow> bool" ("(3\<nexists>!_.a./ _)" [0, 10] 10) |
|
27 |
|
28 |
|
29 abbreviation |
|
30 "der_syn r c \<equiv> der c r" |
|
31 |
|
32 abbreviation |
|
33 "ders_syn r s \<equiv> ders s r" |
|
34 |
|
35 abbreviation |
|
36 "bder_syn r c \<equiv> bder c r" |
|
37 |
|
38 abbreviation |
|
39 "bders_syn r s \<equiv> bders r s" |
|
40 |
|
41 |
|
42 abbreviation |
|
43 "nprec v1 v2 \<equiv> \<not>(v1 :\<sqsubset>val v2)" |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 notation (latex output) |
|
49 If ("(\<^latex>\<open>\\textrm{\<close>if\<^latex>\<open>}\<close> (_)/ \<^latex>\<open>\\textrm{\<close>then\<^latex>\<open>}\<close> (_)/ \<^latex>\<open>\\textrm{\<close>else\<^latex>\<open>}\<close> (_))" 10) and |
|
50 Cons ("_\<^latex>\<open>\\mbox{$\\,$}\<close>::\<^latex>\<open>\\mbox{$\\,$}\<close>_" [75,73] 73) and |
|
51 |
|
52 ZERO ("\<^bold>0" 81) and |
|
53 ONE ("\<^bold>1" 81) and |
|
54 CH ("_" [1000] 80) and |
|
55 ALT ("_ + _" [77,77] 78) and |
|
56 SEQ ("_ \<cdot> _" [77,77] 78) and |
|
57 STAR ("_\<^sup>\<star>" [79] 78) and |
|
58 |
|
59 val.Void ("Empty" 78) and |
|
60 val.Char ("Char _" [1000] 78) and |
|
61 val.Left ("Left _" [79] 78) and |
|
62 val.Right ("Right _" [1000] 78) and |
|
63 val.Seq ("Seq _ _" [79,79] 78) and |
|
64 val.Stars ("Stars _" [79] 78) and |
|
65 |
|
66 L ("L'(_')" [10] 78) and |
|
67 LV ("LV _ _" [80,73] 78) and |
|
68 der_syn ("_\\_" [79, 1000] 76) and |
|
69 ders_syn ("_\\_" [79, 1000] 76) and |
|
70 flat ("|_|" [75] 74) and |
|
71 flats ("|_|" [72] 74) and |
|
72 Sequ ("_ @ _" [78,77] 63) and |
|
73 injval ("inj _ _ _" [79,77,79] 76) and |
|
74 mkeps ("mkeps _" [79] 76) and |
|
75 length ("len _" [73] 73) and |
|
76 intlen ("len _" [73] 73) and |
|
77 set ("_" [73] 73) and |
|
78 |
|
79 Prf ("_ : _" [75,75] 75) and |
|
80 Posix ("'(_, _') \<rightarrow> _" [63,75,75] 75) and |
|
81 |
|
82 lexer ("lexer _ _" [78,78] 77) and |
|
83 F_RIGHT ("F\<^bsub>Right\<^esub> _") and |
|
84 F_LEFT ("F\<^bsub>Left\<^esub> _") and |
|
85 F_ALT ("F\<^bsub>Alt\<^esub> _ _") and |
|
86 F_SEQ1 ("F\<^bsub>Seq1\<^esub> _ _") and |
|
87 F_SEQ2 ("F\<^bsub>Seq2\<^esub> _ _") and |
|
88 F_SEQ ("F\<^bsub>Seq\<^esub> _ _") and |
|
89 simp_SEQ ("simp\<^bsub>Seq\<^esub> _ _" [1000, 1000] 1) and |
|
90 simp_ALT ("simp\<^bsub>Alt\<^esub> _ _" [1000, 1000] 1) and |
|
91 slexer ("lexer\<^sup>+" 1000) and |
|
92 |
|
93 at ("_\<^latex>\<open>\\mbox{$\\downharpoonleft$}\<close>\<^bsub>_\<^esub>") and |
|
94 lex_list ("_ \<prec>\<^bsub>lex\<^esub> _") and |
|
95 PosOrd ("_ \<prec>\<^bsub>_\<^esub> _" [77,77,77] 77) and |
|
96 PosOrd_ex ("_ \<prec> _" [77,77] 77) and |
|
97 PosOrd_ex_eq ("_ \<^latex>\<open>\\mbox{$\\preccurlyeq$}\<close> _" [77,77] 77) and |
|
98 pflat_len ("\<parallel>_\<parallel>\<^bsub>_\<^esub>") and |
|
99 nprec ("_ \<^latex>\<open>\\mbox{$\\not\\prec$}\<close> _" [77,77] 77) and |
|
100 |
|
101 bder_syn ("_\<^latex>\<open>\\mbox{$\\bbslash$}\<close>_" [79, 1000] 76) and |
|
102 bders_syn ("_\<^latex>\<open>\\mbox{$\\bbslash$}\<close>_" [79, 1000] 76) and |
|
103 intern ("_\<^latex>\<open>\\mbox{$^\\uparrow$}\<close>" [900] 80) and |
|
104 erase ("_\<^latex>\<open>\\mbox{$^\\downarrow$}\<close>" [1000] 74) and |
|
105 bnullable ("nullable\<^latex>\<open>\\mbox{$_b$}\<close> _" [1000] 80) and |
|
106 bmkeps ("mkeps\<^latex>\<open>\\mbox{$_b$}\<close> _" [1000] 80) and |
|
107 blexer ("lexer\<^latex>\<open>\\mbox{$_b$}\<close> _ _" [77, 77] 80) and |
|
108 code ("code _" [79] 74) and |
|
109 |
|
110 DUMMY ("\<^latex>\<open>\\underline{\\hspace{2mm}}\<close>") |
|
111 |
|
112 |
|
113 definition |
|
114 "match r s \<equiv> nullable (ders s r)" |
|
115 |
|
116 |
|
117 lemma LV_STAR_ONE_empty: |
|
118 shows "LV (STAR ONE) [] = {Stars []}" |
|
119 by(auto simp add: LV_def elim: Prf.cases intro: Prf.intros) |
|
120 |
|
121 |
|
122 |
|
123 (* |
|
124 comments not implemented |
|
125 |
|
126 p9. The condition "not exists s3 s4..." appears often enough (in particular in |
|
127 the proof of Lemma 3) to warrant a definition. |
|
128 |
|
129 *) |
|
130 |
|
131 |
|
132 (*>*) |
|
133 |
|
134 |
|
135 |
|
136 section \<open>Introduction\<close> |
|
137 |
|
138 |
|
139 text \<open> |
|
140 |
|
141 This works builds on previous work by Ausaf and Urban using |
|
142 regular expression'd bit-coded derivatives to do lexing that |
|
143 is both fast and satisfied the POSIX specification. |
|
144 In their work, a bit-coded algorithm introduced by Sulzmann and Lu |
|
145 was formally verified in Isabelle, by a very clever use of |
|
146 flex function and retrieve to carefully mimic the way a value is |
|
147 built up by the injection funciton. |
|
148 |
|
149 In the previous work, Ausaf and Urban established the below equality: |
|
150 \begin{lemma} |
|
151 @{thm [mode=IfThen] bder_retrieve} |
|
152 \end{lemma} |
|
153 |
|
154 This lemma links the derivative of a bit-coded regular expression with |
|
155 the regular expression itself before the derivative. |
|
156 |
|
157 Brzozowski \cite{Brzozowski1964} introduced the notion of the {\em |
|
158 derivative} @{term "der c r"} of a regular expression \<open>r\<close> w.r.t.\ |
|
159 a character~\<open>c\<close>, and showed that it gave a simple solution to the |
|
160 problem of matching a string @{term s} with a regular expression @{term |
|
161 r}: if the derivative of @{term r} w.r.t.\ (in succession) all the |
|
162 characters of the string matches the empty string, then @{term r} |
|
163 matches @{term s} (and {\em vice versa}). The derivative has the |
|
164 property (which may almost be regarded as its specification) that, for |
|
165 every string @{term s} and regular expression @{term r} and character |
|
166 @{term c}, one has @{term "cs \<in> L(r)"} if and only if \mbox{@{term "s \<in> L(der c r)"}}. |
|
167 The beauty of Brzozowski's derivatives is that |
|
168 they are neatly expressible in any functional language, and easily |
|
169 definable and reasoned about in theorem provers---the definitions just |
|
170 consist of inductive datatypes and simple recursive functions. A |
|
171 mechanised correctness proof of Brzozowski's matcher in for example HOL4 |
|
172 has been mentioned by Owens and Slind~\cite{Owens2008}. Another one in |
|
173 Isabelle/HOL is part of the work by Krauss and Nipkow \cite{Krauss2011}. |
|
174 And another one in Coq is given by Coquand and Siles \cite{Coquand2012}. |
|
175 |
|
176 If a regular expression matches a string, then in general there is more |
|
177 than one way of how the string is matched. There are two commonly used |
|
178 disambiguation strategies to generate a unique answer: one is called |
|
179 GREEDY matching \cite{Frisch2004} and the other is POSIX |
|
180 matching~\cite{POSIX,Kuklewicz,OkuiSuzuki2010,Sulzmann2014,Vansummeren2006}. |
|
181 For example consider the string @{term xy} and the regular expression |
|
182 \mbox{@{term "STAR (ALT (ALT x y) xy)"}}. Either the string can be |
|
183 matched in two `iterations' by the single letter-regular expressions |
|
184 @{term x} and @{term y}, or directly in one iteration by @{term xy}. The |
|
185 first case corresponds to GREEDY matching, which first matches with the |
|
186 left-most symbol and only matches the next symbol in case of a mismatch |
|
187 (this is greedy in the sense of preferring instant gratification to |
|
188 delayed repletion). The second case is POSIX matching, which prefers the |
|
189 longest match. |
|
190 |
|
191 In the context of lexing, where an input string needs to be split up |
|
192 into a sequence of tokens, POSIX is the more natural disambiguation |
|
193 strategy for what programmers consider basic syntactic building blocks |
|
194 in their programs. These building blocks are often specified by some |
|
195 regular expressions, say \<open>r\<^bsub>key\<^esub>\<close> and \<open>r\<^bsub>id\<^esub>\<close> for recognising keywords and identifiers, |
|
196 respectively. There are a few underlying (informal) rules behind |
|
197 tokenising a string in a POSIX \cite{POSIX} fashion: |
|
198 |
|
199 \begin{itemize} |
|
200 \item[$\bullet$] \emph{The Longest Match Rule} (or \emph{``{M}aximal {M}unch {R}ule''}): |
|
201 The longest initial substring matched by any regular expression is taken as |
|
202 next token.\smallskip |
|
203 |
|
204 \item[$\bullet$] \emph{Priority Rule:} |
|
205 For a particular longest initial substring, the first (leftmost) regular expression |
|
206 that can match determines the token.\smallskip |
|
207 |
|
208 \item[$\bullet$] \emph{Star Rule:} A subexpression repeated by ${}^\star$ shall |
|
209 not match an empty string unless this is the only match for the repetition.\smallskip |
|
210 |
|
211 \item[$\bullet$] \emph{Empty String Rule:} An empty string shall be considered to |
|
212 be longer than no match at all. |
|
213 \end{itemize} |
|
214 |
|
215 \noindent Consider for example a regular expression \<open>r\<^bsub>key\<^esub>\<close> for recognising keywords such as \<open>if\<close>, |
|
216 \<open>then\<close> and so on; and \<open>r\<^bsub>id\<^esub>\<close> |
|
217 recognising identifiers (say, a single character followed by |
|
218 characters or numbers). Then we can form the regular expression |
|
219 \<open>(r\<^bsub>key\<^esub> + r\<^bsub>id\<^esub>)\<^sup>\<star>\<close> |
|
220 and use POSIX matching to tokenise strings, say \<open>iffoo\<close> and |
|
221 \<open>if\<close>. For \<open>iffoo\<close> we obtain by the Longest Match Rule |
|
222 a single identifier token, not a keyword followed by an |
|
223 identifier. For \<open>if\<close> we obtain by the Priority Rule a keyword |
|
224 token, not an identifier token---even if \<open>r\<^bsub>id\<^esub>\<close> |
|
225 matches also. By the Star Rule we know \<open>(r\<^bsub>key\<^esub> + |
|
226 r\<^bsub>id\<^esub>)\<^sup>\<star>\<close> matches \<open>iffoo\<close>, |
|
227 respectively \<open>if\<close>, in exactly one `iteration' of the star. The |
|
228 Empty String Rule is for cases where, for example, the regular expression |
|
229 \<open>(a\<^sup>\<star>)\<^sup>\<star>\<close> matches against the |
|
230 string \<open>bc\<close>. Then the longest initial matched substring is the |
|
231 empty string, which is matched by both the whole regular expression |
|
232 and the parenthesised subexpression. |
|
233 |
|
234 |
|
235 One limitation of Brzozowski's matcher is that it only generates a |
|
236 YES/NO answer for whether a string is being matched by a regular |
|
237 expression. Sulzmann and Lu~\cite{Sulzmann2014} extended this matcher |
|
238 to allow generation not just of a YES/NO answer but of an actual |
|
239 matching, called a [lexical] {\em value}. Assuming a regular |
|
240 expression matches a string, values encode the information of |
|
241 \emph{how} the string is matched by the regular expression---that is, |
|
242 which part of the string is matched by which part of the regular |
|
243 expression. For this consider again the string \<open>xy\<close> and |
|
244 the regular expression \mbox{\<open>(x + (y + xy))\<^sup>\<star>\<close>} |
|
245 (this time fully parenthesised). We can view this regular expression |
|
246 as tree and if the string \<open>xy\<close> is matched by two Star |
|
247 `iterations', then the \<open>x\<close> is matched by the left-most |
|
248 alternative in this tree and the \<open>y\<close> by the right-left alternative. This |
|
249 suggests to record this matching as |
|
250 |
|
251 \begin{center} |
|
252 @{term "Stars [Left(Char x), Right(Left(Char y))]"} |
|
253 \end{center} |
|
254 |
|
255 \noindent where @{const Stars}, \<open>Left\<close>, \<open>Right\<close> and \<open>Char\<close> are constructors for values. \<open>Stars\<close> records how many |
|
256 iterations were used; \<open>Left\<close>, respectively \<open>Right\<close>, which |
|
257 alternative is used. This `tree view' leads naturally to the idea that |
|
258 regular expressions act as types and values as inhabiting those types |
|
259 (see, for example, \cite{HosoyaVouillonPierce2005}). The value for |
|
260 matching \<open>xy\<close> in a single `iteration', i.e.~the POSIX value, |
|
261 would look as follows |
|
262 |
|
263 \begin{center} |
|
264 @{term "Stars [Seq (Char x) (Char y)]"} |
|
265 \end{center} |
|
266 |
|
267 \noindent where @{const Stars} has only a single-element list for the |
|
268 single iteration and @{const Seq} indicates that @{term xy} is matched |
|
269 by a sequence regular expression. |
|
270 |
|
271 %, which we will in what follows |
|
272 %write more formally as @{term "SEQ x y"}. |
|
273 |
|
274 |
|
275 Sulzmann and Lu give a simple algorithm to calculate a value that |
|
276 appears to be the value associated with POSIX matching. The challenge |
|
277 then is to specify that value, in an algorithm-independent fashion, |
|
278 and to show that Sulzmann and Lu's derivative-based algorithm does |
|
279 indeed calculate a value that is correct according to the |
|
280 specification. The answer given by Sulzmann and Lu |
|
281 \cite{Sulzmann2014} is to define a relation (called an ``order |
|
282 relation'') on the set of values of @{term r}, and to show that (once |
|
283 a string to be matched is chosen) there is a maximum element and that |
|
284 it is computed by their derivative-based algorithm. This proof idea is |
|
285 inspired by work of Frisch and Cardelli \cite{Frisch2004} on a GREEDY |
|
286 regular expression matching algorithm. However, we were not able to |
|
287 establish transitivity and totality for the ``order relation'' by |
|
288 Sulzmann and Lu. There are some inherent problems with their approach |
|
289 (of which some of the proofs are not published in |
|
290 \cite{Sulzmann2014}); perhaps more importantly, we give in this paper |
|
291 a simple inductive (and algorithm-independent) definition of what we |
|
292 call being a {\em POSIX value} for a regular expression @{term r} and |
|
293 a string @{term s}; we show that the algorithm by Sulzmann and Lu |
|
294 computes such a value and that such a value is unique. Our proofs are |
|
295 both done by hand and checked in Isabelle/HOL. The experience of |
|
296 doing our proofs has been that this mechanical checking was absolutely |
|
297 essential: this subject area has hidden snares. This was also noted by |
|
298 Kuklewicz \cite{Kuklewicz} who found that nearly all POSIX matching |
|
299 implementations are ``buggy'' \cite[Page 203]{Sulzmann2014} and by |
|
300 Grathwohl et al \cite[Page 36]{CrashCourse2014} who wrote: |
|
301 |
|
302 \begin{quote} |
|
303 \it{}``The POSIX strategy is more complicated than the greedy because of |
|
304 the dependence on information about the length of matched strings in the |
|
305 various subexpressions.'' |
|
306 \end{quote} |
|
307 |
|
308 |
|
309 |
|
310 \noindent {\bf Contributions:} We have implemented in Isabelle/HOL the |
|
311 derivative-based regular expression matching algorithm of |
|
312 Sulzmann and Lu \cite{Sulzmann2014}. We have proved the correctness of this |
|
313 algorithm according to our specification of what a POSIX value is (inspired |
|
314 by work of Vansummeren \cite{Vansummeren2006}). Sulzmann |
|
315 and Lu sketch in \cite{Sulzmann2014} an informal correctness proof: but to |
|
316 us it contains unfillable gaps.\footnote{An extended version of |
|
317 \cite{Sulzmann2014} is available at the website of its first author; this |
|
318 extended version already includes remarks in the appendix that their |
|
319 informal proof contains gaps, and possible fixes are not fully worked out.} |
|
320 Our specification of a POSIX value consists of a simple inductive definition |
|
321 that given a string and a regular expression uniquely determines this value. |
|
322 We also show that our definition is equivalent to an ordering |
|
323 of values based on positions by Okui and Suzuki \cite{OkuiSuzuki2010}. |
|
324 |
|
325 %Derivatives as calculated by Brzozowski's method are usually more complex |
|
326 %regular expressions than the initial one; various optimisations are |
|
327 %possible. We prove the correctness when simplifications of @{term "ALT ZERO r"}, |
|
328 %@{term "ALT r ZERO"}, @{term "SEQ ONE r"} and @{term "SEQ r ONE"} to |
|
329 %@{term r} are applied. |
|
330 |
|
331 We extend our results to ??? Bitcoded version?? |
|
332 |
|
333 \<close> |
|
334 |
|
335 section \<open>Preliminaries\<close> |
|
336 |
|
337 text \<open>\noindent Strings in Isabelle/HOL are lists of characters with |
|
338 the empty string being represented by the empty list, written @{term |
|
339 "[]"}, and list-cons being written as @{term "DUMMY # DUMMY"}. Often |
|
340 we use the usual bracket notation for lists also for strings; for |
|
341 example a string consisting of just a single character @{term c} is |
|
342 written @{term "[c]"}. We use the usual definitions for |
|
343 \emph{prefixes} and \emph{strict prefixes} of strings. By using the |
|
344 type @{type char} for characters we have a supply of finitely many |
|
345 characters roughly corresponding to the ASCII character set. Regular |
|
346 expressions are defined as usual as the elements of the following |
|
347 inductive datatype: |
|
348 |
|
349 \begin{center} |
|
350 \<open>r :=\<close> |
|
351 @{const "ZERO"} $\mid$ |
|
352 @{const "ONE"} $\mid$ |
|
353 @{term "CH c"} $\mid$ |
|
354 @{term "ALT r\<^sub>1 r\<^sub>2"} $\mid$ |
|
355 @{term "SEQ r\<^sub>1 r\<^sub>2"} $\mid$ |
|
356 @{term "STAR r"} |
|
357 \end{center} |
|
358 |
|
359 \noindent where @{const ZERO} stands for the regular expression that does |
|
360 not match any string, @{const ONE} for the regular expression that matches |
|
361 only the empty string and @{term c} for matching a character literal. The |
|
362 language of a regular expression is also defined as usual by the |
|
363 recursive function @{term L} with the six clauses: |
|
364 |
|
365 \begin{center} |
|
366 \begin{tabular}{l@ {\hspace{4mm}}rcl} |
|
367 \textit{(1)} & @{thm (lhs) L.simps(1)} & $\dn$ & @{thm (rhs) L.simps(1)}\\ |
|
368 \textit{(2)} & @{thm (lhs) L.simps(2)} & $\dn$ & @{thm (rhs) L.simps(2)}\\ |
|
369 \textit{(3)} & @{thm (lhs) L.simps(3)} & $\dn$ & @{thm (rhs) L.simps(3)}\\ |
|
370 \textit{(4)} & @{thm (lhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & |
|
371 @{thm (rhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
372 \textit{(5)} & @{thm (lhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & |
|
373 @{thm (rhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
374 \textit{(6)} & @{thm (lhs) L.simps(6)} & $\dn$ & @{thm (rhs) L.simps(6)}\\ |
|
375 \end{tabular} |
|
376 \end{center} |
|
377 |
|
378 \noindent In clause \textit{(4)} we use the operation @{term "DUMMY ;; |
|
379 DUMMY"} for the concatenation of two languages (it is also list-append for |
|
380 strings). We use the star-notation for regular expressions and for |
|
381 languages (in the last clause above). The star for languages is defined |
|
382 inductively by two clauses: \<open>(i)\<close> the empty string being in |
|
383 the star of a language and \<open>(ii)\<close> if @{term "s\<^sub>1"} is in a |
|
384 language and @{term "s\<^sub>2"} in the star of this language, then also @{term |
|
385 "s\<^sub>1 @ s\<^sub>2"} is in the star of this language. It will also be convenient |
|
386 to use the following notion of a \emph{semantic derivative} (or \emph{left |
|
387 quotient}) of a language defined as |
|
388 % |
|
389 \begin{center} |
|
390 @{thm Der_def}\;. |
|
391 \end{center} |
|
392 |
|
393 \noindent |
|
394 For semantic derivatives we have the following equations (for example |
|
395 mechanically proved in \cite{Krauss2011}): |
|
396 % |
|
397 \begin{equation}\label{SemDer} |
|
398 \begin{array}{lcl} |
|
399 @{thm (lhs) Der_null} & \dn & @{thm (rhs) Der_null}\\ |
|
400 @{thm (lhs) Der_empty} & \dn & @{thm (rhs) Der_empty}\\ |
|
401 @{thm (lhs) Der_char} & \dn & @{thm (rhs) Der_char}\\ |
|
402 @{thm (lhs) Der_union} & \dn & @{thm (rhs) Der_union}\\ |
|
403 @{thm (lhs) Der_Sequ} & \dn & @{thm (rhs) Der_Sequ}\\ |
|
404 @{thm (lhs) Der_star} & \dn & @{thm (rhs) Der_star} |
|
405 \end{array} |
|
406 \end{equation} |
|
407 |
|
408 |
|
409 \noindent \emph{\Brz's derivatives} of regular expressions |
|
410 \cite{Brzozowski1964} can be easily defined by two recursive functions: |
|
411 the first is from regular expressions to booleans (implementing a test |
|
412 when a regular expression can match the empty string), and the second |
|
413 takes a regular expression and a character to a (derivative) regular |
|
414 expression: |
|
415 |
|
416 \begin{center} |
|
417 \begin{tabular}{lcl} |
|
418 @{thm (lhs) nullable.simps(1)} & $\dn$ & @{thm (rhs) nullable.simps(1)}\\ |
|
419 @{thm (lhs) nullable.simps(2)} & $\dn$ & @{thm (rhs) nullable.simps(2)}\\ |
|
420 @{thm (lhs) nullable.simps(3)} & $\dn$ & @{thm (rhs) nullable.simps(3)}\\ |
|
421 @{thm (lhs) nullable.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) nullable.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
422 @{thm (lhs) nullable.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) nullable.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
423 @{thm (lhs) nullable.simps(6)} & $\dn$ & @{thm (rhs) nullable.simps(6)}\medskip\\ |
|
424 |
|
425 % \end{tabular} |
|
426 % \end{center} |
|
427 |
|
428 % \begin{center} |
|
429 % \begin{tabular}{lcl} |
|
430 |
|
431 @{thm (lhs) der.simps(1)} & $\dn$ & @{thm (rhs) der.simps(1)}\\ |
|
432 @{thm (lhs) der.simps(2)} & $\dn$ & @{thm (rhs) der.simps(2)}\\ |
|
433 @{thm (lhs) der.simps(3)} & $\dn$ & @{thm (rhs) der.simps(3)}\\ |
|
434 @{thm (lhs) der.simps(4)[of c "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) der.simps(4)[of c "r\<^sub>1" "r\<^sub>2"]}\\ |
|
435 @{thm (lhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]}\\ |
|
436 @{thm (lhs) der.simps(6)} & $\dn$ & @{thm (rhs) der.simps(6)} |
|
437 \end{tabular} |
|
438 \end{center} |
|
439 |
|
440 \noindent |
|
441 We may extend this definition to give derivatives w.r.t.~strings: |
|
442 |
|
443 \begin{center} |
|
444 \begin{tabular}{lcl} |
|
445 @{thm (lhs) ders.simps(1)} & $\dn$ & @{thm (rhs) ders.simps(1)}\\ |
|
446 @{thm (lhs) ders.simps(2)} & $\dn$ & @{thm (rhs) ders.simps(2)}\\ |
|
447 \end{tabular} |
|
448 \end{center} |
|
449 |
|
450 \noindent Given the equations in \eqref{SemDer}, it is a relatively easy |
|
451 exercise in mechanical reasoning to establish that |
|
452 |
|
453 \begin{proposition}\label{derprop}\mbox{}\\ |
|
454 \begin{tabular}{ll} |
|
455 \textit{(1)} & @{thm (lhs) nullable_correctness} if and only if |
|
456 @{thm (rhs) nullable_correctness}, and \\ |
|
457 \textit{(2)} & @{thm[mode=IfThen] der_correctness}. |
|
458 \end{tabular} |
|
459 \end{proposition} |
|
460 |
|
461 \noindent With this in place it is also very routine to prove that the |
|
462 regular expression matcher defined as |
|
463 % |
|
464 \begin{center} |
|
465 @{thm match_def} |
|
466 \end{center} |
|
467 |
|
468 \noindent gives a positive answer if and only if @{term "s \<in> L r"}. |
|
469 Consequently, this regular expression matching algorithm satisfies the |
|
470 usual specification for regular expression matching. While the matcher |
|
471 above calculates a provably correct YES/NO answer for whether a regular |
|
472 expression matches a string or not, the novel idea of Sulzmann and Lu |
|
473 \cite{Sulzmann2014} is to append another phase to this algorithm in order |
|
474 to calculate a [lexical] value. We will explain the details next. |
|
475 |
|
476 \<close> |
|
477 |
|
478 section \<open>POSIX Regular Expression Matching\label{posixsec}\<close> |
|
479 |
|
480 text \<open> |
|
481 |
|
482 There have been many previous works that use values for encoding |
|
483 \emph{how} a regular expression matches a string. |
|
484 The clever idea by Sulzmann and Lu \cite{Sulzmann2014} is to |
|
485 define a function on values that mirrors (but inverts) the |
|
486 construction of the derivative on regular expressions. \emph{Values} |
|
487 are defined as the inductive datatype |
|
488 |
|
489 \begin{center} |
|
490 \<open>v :=\<close> |
|
491 @{const "Void"} $\mid$ |
|
492 @{term "val.Char c"} $\mid$ |
|
493 @{term "Left v"} $\mid$ |
|
494 @{term "Right v"} $\mid$ |
|
495 @{term "Seq v\<^sub>1 v\<^sub>2"} $\mid$ |
|
496 @{term "Stars vs"} |
|
497 \end{center} |
|
498 |
|
499 \noindent where we use @{term vs} to stand for a list of |
|
500 values. (This is similar to the approach taken by Frisch and |
|
501 Cardelli for GREEDY matching \cite{Frisch2004}, and Sulzmann and Lu |
|
502 for POSIX matching \cite{Sulzmann2014}). The string underlying a |
|
503 value can be calculated by the @{const flat} function, written |
|
504 @{term "flat DUMMY"} and defined as: |
|
505 |
|
506 \begin{center} |
|
507 \begin{tabular}[t]{lcl} |
|
508 @{thm (lhs) flat.simps(1)} & $\dn$ & @{thm (rhs) flat.simps(1)}\\ |
|
509 @{thm (lhs) flat.simps(2)} & $\dn$ & @{thm (rhs) flat.simps(2)}\\ |
|
510 @{thm (lhs) flat.simps(3)} & $\dn$ & @{thm (rhs) flat.simps(3)}\\ |
|
511 @{thm (lhs) flat.simps(4)} & $\dn$ & @{thm (rhs) flat.simps(4)} |
|
512 \end{tabular}\hspace{14mm} |
|
513 \begin{tabular}[t]{lcl} |
|
514 @{thm (lhs) flat.simps(5)[of "v\<^sub>1" "v\<^sub>2"]} & $\dn$ & @{thm (rhs) flat.simps(5)[of "v\<^sub>1" "v\<^sub>2"]}\\ |
|
515 @{thm (lhs) flat.simps(6)} & $\dn$ & @{thm (rhs) flat.simps(6)}\\ |
|
516 @{thm (lhs) flat.simps(7)} & $\dn$ & @{thm (rhs) flat.simps(7)}\\ |
|
517 \end{tabular} |
|
518 \end{center} |
|
519 |
|
520 \noindent We will sometimes refer to the underlying string of a |
|
521 value as \emph{flattened value}. We will also overload our notation and |
|
522 use @{term "flats vs"} for flattening a list of values and concatenating |
|
523 the resulting strings. |
|
524 |
|
525 Sulzmann and Lu define |
|
526 inductively an \emph{inhabitation relation} that associates values to |
|
527 regular expressions. We define this relation as |
|
528 follows:\footnote{Note that the rule for @{term Stars} differs from |
|
529 our earlier paper \cite{AusafDyckhoffUrban2016}. There we used the |
|
530 original definition by Sulzmann and Lu which does not require that |
|
531 the values @{term "v \<in> set vs"} flatten to a non-empty |
|
532 string. The reason for introducing the more restricted version of |
|
533 lexical values is convenience later on when reasoning about an |
|
534 ordering relation for values.} |
|
535 |
|
536 \begin{center} |
|
537 \begin{tabular}{c@ {\hspace{12mm}}c}\label{prfintros} |
|
538 \\[-8mm] |
|
539 @{thm[mode=Axiom] Prf.intros(4)} & |
|
540 @{thm[mode=Axiom] Prf.intros(5)[of "c"]}\\[4mm] |
|
541 @{thm[mode=Rule] Prf.intros(2)[of "v\<^sub>1" "r\<^sub>1" "r\<^sub>2"]} & |
|
542 @{thm[mode=Rule] Prf.intros(3)[of "v\<^sub>2" "r\<^sub>1" "r\<^sub>2"]}\\[4mm] |
|
543 @{thm[mode=Rule] Prf.intros(1)[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>2" "r\<^sub>2"]} & |
|
544 @{thm[mode=Rule] Prf.intros(6)[of "vs"]} |
|
545 \end{tabular} |
|
546 \end{center} |
|
547 |
|
548 \noindent where in the clause for @{const "Stars"} we use the |
|
549 notation @{term "v \<in> set vs"} for indicating that \<open>v\<close> is a |
|
550 member in the list \<open>vs\<close>. We require in this rule that every |
|
551 value in @{term vs} flattens to a non-empty string. The idea is that |
|
552 @{term "Stars"}-values satisfy the informal Star Rule (see Introduction) |
|
553 where the $^\star$ does not match the empty string unless this is |
|
554 the only match for the repetition. Note also that no values are |
|
555 associated with the regular expression @{term ZERO}, and that the |
|
556 only value associated with the regular expression @{term ONE} is |
|
557 @{term Void}. It is routine to establish how values ``inhabiting'' |
|
558 a regular expression correspond to the language of a regular |
|
559 expression, namely |
|
560 |
|
561 \begin{proposition}\label{inhabs} |
|
562 @{thm L_flat_Prf} |
|
563 \end{proposition} |
|
564 |
|
565 \noindent |
|
566 Given a regular expression \<open>r\<close> and a string \<open>s\<close>, we define the |
|
567 set of all \emph{Lexical Values} inhabited by \<open>r\<close> with the underlying string |
|
568 being \<open>s\<close>:\footnote{Okui and Suzuki refer to our lexical values |
|
569 as \emph{canonical values} in \cite{OkuiSuzuki2010}. The notion of \emph{non-problematic |
|
570 values} by Cardelli and Frisch \cite{Frisch2004} is related, but not identical |
|
571 to our lexical values.} |
|
572 |
|
573 \begin{center} |
|
574 @{thm LV_def} |
|
575 \end{center} |
|
576 |
|
577 \noindent The main property of @{term "LV r s"} is that it is alway finite. |
|
578 |
|
579 \begin{proposition} |
|
580 @{thm LV_finite} |
|
581 \end{proposition} |
|
582 |
|
583 \noindent This finiteness property does not hold in general if we |
|
584 remove the side-condition about @{term "flat v \<noteq> []"} in the |
|
585 @{term Stars}-rule above. For example using Sulzmann and Lu's |
|
586 less restrictive definition, @{term "LV (STAR ONE) []"} would contain |
|
587 infinitely many values, but according to our more restricted |
|
588 definition only a single value, namely @{thm LV_STAR_ONE_empty}. |
|
589 |
|
590 If a regular expression \<open>r\<close> matches a string \<open>s\<close>, then |
|
591 generally the set @{term "LV r s"} is not just a singleton set. In |
|
592 case of POSIX matching the problem is to calculate the unique lexical value |
|
593 that satisfies the (informal) POSIX rules from the Introduction. |
|
594 Graphically the POSIX value calculation algorithm by Sulzmann and Lu |
|
595 can be illustrated by the picture in Figure~\ref{Sulz} where the |
|
596 path from the left to the right involving @{term |
|
597 derivatives}/@{const nullable} is the first phase of the algorithm |
|
598 (calculating successive \Brz's derivatives) and @{const |
|
599 mkeps}/\<open>inj\<close>, the path from right to left, the second |
|
600 phase. This picture shows the steps required when a regular |
|
601 expression, say \<open>r\<^sub>1\<close>, matches the string @{term |
|
602 "[a,b,c]"}. We first build the three derivatives (according to |
|
603 @{term a}, @{term b} and @{term c}). We then use @{const nullable} |
|
604 to find out whether the resulting derivative regular expression |
|
605 @{term "r\<^sub>4"} can match the empty string. If yes, we call the |
|
606 function @{const mkeps} that produces a value @{term "v\<^sub>4"} |
|
607 for how @{term "r\<^sub>4"} can match the empty string (taking into |
|
608 account the POSIX constraints in case there are several ways). This |
|
609 function is defined by the clauses: |
|
610 |
|
611 \begin{figure}[t] |
|
612 \begin{center} |
|
613 \begin{tikzpicture}[scale=2,node distance=1.3cm, |
|
614 every node/.style={minimum size=6mm}] |
|
615 \node (r1) {@{term "r\<^sub>1"}}; |
|
616 \node (r2) [right=of r1]{@{term "r\<^sub>2"}}; |
|
617 \draw[->,line width=1mm](r1)--(r2) node[above,midway] {@{term "der a DUMMY"}}; |
|
618 \node (r3) [right=of r2]{@{term "r\<^sub>3"}}; |
|
619 \draw[->,line width=1mm](r2)--(r3) node[above,midway] {@{term "der b DUMMY"}}; |
|
620 \node (r4) [right=of r3]{@{term "r\<^sub>4"}}; |
|
621 \draw[->,line width=1mm](r3)--(r4) node[above,midway] {@{term "der c DUMMY"}}; |
|
622 \draw (r4) node[anchor=west] {\;\raisebox{3mm}{@{term nullable}}}; |
|
623 \node (v4) [below=of r4]{@{term "v\<^sub>4"}}; |
|
624 \draw[->,line width=1mm](r4) -- (v4); |
|
625 \node (v3) [left=of v4] {@{term "v\<^sub>3"}}; |
|
626 \draw[->,line width=1mm](v4)--(v3) node[below,midway] {\<open>inj r\<^sub>3 c\<close>}; |
|
627 \node (v2) [left=of v3]{@{term "v\<^sub>2"}}; |
|
628 \draw[->,line width=1mm](v3)--(v2) node[below,midway] {\<open>inj r\<^sub>2 b\<close>}; |
|
629 \node (v1) [left=of v2] {@{term "v\<^sub>1"}}; |
|
630 \draw[->,line width=1mm](v2)--(v1) node[below,midway] {\<open>inj r\<^sub>1 a\<close>}; |
|
631 \draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{@{term "mkeps"}}}; |
|
632 \end{tikzpicture} |
|
633 \end{center} |
|
634 \mbox{}\\[-13mm] |
|
635 |
|
636 \caption{The two phases of the algorithm by Sulzmann \& Lu \cite{Sulzmann2014}, |
|
637 matching the string @{term "[a,b,c]"}. The first phase (the arrows from |
|
638 left to right) is \Brz's matcher building successive derivatives. If the |
|
639 last regular expression is @{term nullable}, then the functions of the |
|
640 second phase are called (the top-down and right-to-left arrows): first |
|
641 @{term mkeps} calculates a value @{term "v\<^sub>4"} witnessing |
|
642 how the empty string has been recognised by @{term "r\<^sub>4"}. After |
|
643 that the function @{term inj} ``injects back'' the characters of the string into |
|
644 the values. |
|
645 \label{Sulz}} |
|
646 \end{figure} |
|
647 |
|
648 \begin{center} |
|
649 \begin{tabular}{lcl} |
|
650 @{thm (lhs) mkeps.simps(1)} & $\dn$ & @{thm (rhs) mkeps.simps(1)}\\ |
|
651 @{thm (lhs) mkeps.simps(2)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) mkeps.simps(2)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
652 @{thm (lhs) mkeps.simps(3)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) mkeps.simps(3)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
653 @{thm (lhs) mkeps.simps(4)} & $\dn$ & @{thm (rhs) mkeps.simps(4)}\\ |
|
654 \end{tabular} |
|
655 \end{center} |
|
656 |
|
657 \noindent Note that this function needs only to be partially defined, |
|
658 namely only for regular expressions that are nullable. In case @{const |
|
659 nullable} fails, the string @{term "[a,b,c]"} cannot be matched by @{term |
|
660 "r\<^sub>1"} and the null value @{term "None"} is returned. Note also how this function |
|
661 makes some subtle choices leading to a POSIX value: for example if an |
|
662 alternative regular expression, say @{term "ALT r\<^sub>1 r\<^sub>2"}, can |
|
663 match the empty string and furthermore @{term "r\<^sub>1"} can match the |
|
664 empty string, then we return a \<open>Left\<close>-value. The \<open>Right\<close>-value will only be returned if @{term "r\<^sub>1"} cannot match the empty |
|
665 string. |
|
666 |
|
667 The most interesting idea from Sulzmann and Lu \cite{Sulzmann2014} is |
|
668 the construction of a value for how @{term "r\<^sub>1"} can match the |
|
669 string @{term "[a,b,c]"} from the value how the last derivative, @{term |
|
670 "r\<^sub>4"} in Fig.~\ref{Sulz}, can match the empty string. Sulzmann and |
|
671 Lu achieve this by stepwise ``injecting back'' the characters into the |
|
672 values thus inverting the operation of building derivatives, but on the level |
|
673 of values. The corresponding function, called @{term inj}, takes three |
|
674 arguments, a regular expression, a character and a value. For example in |
|
675 the first (or right-most) @{term inj}-step in Fig.~\ref{Sulz} the regular |
|
676 expression @{term "r\<^sub>3"}, the character @{term c} from the last |
|
677 derivative step and @{term "v\<^sub>4"}, which is the value corresponding |
|
678 to the derivative regular expression @{term "r\<^sub>4"}. The result is |
|
679 the new value @{term "v\<^sub>3"}. The final result of the algorithm is |
|
680 the value @{term "v\<^sub>1"}. The @{term inj} function is defined by recursion on regular |
|
681 expressions and by analysing the shape of values (corresponding to |
|
682 the derivative regular expressions). |
|
683 % |
|
684 \begin{center} |
|
685 \begin{tabular}{l@ {\hspace{5mm}}lcl} |
|
686 \textit{(1)} & @{thm (lhs) injval.simps(1)} & $\dn$ & @{thm (rhs) injval.simps(1)}\\ |
|
687 \textit{(2)} & @{thm (lhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]} & $\dn$ & |
|
688 @{thm (rhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]}\\ |
|
689 \textit{(3)} & @{thm (lhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ & |
|
690 @{thm (rhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\ |
|
691 \textit{(4)} & @{thm (lhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ |
|
692 & @{thm (rhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\ |
|
693 \textit{(5)} & @{thm (lhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ |
|
694 & @{thm (rhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\ |
|
695 \textit{(6)} & @{thm (lhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ |
|
696 & @{thm (rhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\ |
|
697 \textit{(7)} & @{thm (lhs) injval.simps(7)[of "r" "c" "v" "vs"]} & $\dn$ |
|
698 & @{thm (rhs) injval.simps(7)[of "r" "c" "v" "vs"]}\\ |
|
699 \end{tabular} |
|
700 \end{center} |
|
701 |
|
702 \noindent To better understand what is going on in this definition it |
|
703 might be instructive to look first at the three sequence cases (clauses |
|
704 \textit{(4)} -- \textit{(6)}). In each case we need to construct an ``injected value'' for |
|
705 @{term "SEQ r\<^sub>1 r\<^sub>2"}. This must be a value of the form @{term |
|
706 "Seq DUMMY DUMMY"}\,. Recall the clause of the \<open>derivative\<close>-function |
|
707 for sequence regular expressions: |
|
708 |
|
709 \begin{center} |
|
710 @{thm (lhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} $\dn$ @{thm (rhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} |
|
711 \end{center} |
|
712 |
|
713 \noindent Consider first the \<open>else\<close>-branch where the derivative is @{term |
|
714 "SEQ (der c r\<^sub>1) r\<^sub>2"}. The corresponding value must therefore |
|
715 be of the form @{term "Seq v\<^sub>1 v\<^sub>2"}, which matches the left-hand |
|
716 side in clause~\textit{(4)} of @{term inj}. In the \<open>if\<close>-branch the derivative is an |
|
717 alternative, namely @{term "ALT (SEQ (der c r\<^sub>1) r\<^sub>2) (der c |
|
718 r\<^sub>2)"}. This means we either have to consider a \<open>Left\<close>- or |
|
719 \<open>Right\<close>-value. In case of the \<open>Left\<close>-value we know further it |
|
720 must be a value for a sequence regular expression. Therefore the pattern |
|
721 we match in the clause \textit{(5)} is @{term "Left (Seq v\<^sub>1 v\<^sub>2)"}, |
|
722 while in \textit{(6)} it is just @{term "Right v\<^sub>2"}. One more interesting |
|
723 point is in the right-hand side of clause \textit{(6)}: since in this case the |
|
724 regular expression \<open>r\<^sub>1\<close> does not ``contribute'' to |
|
725 matching the string, that means it only matches the empty string, we need to |
|
726 call @{const mkeps} in order to construct a value for how @{term "r\<^sub>1"} |
|
727 can match this empty string. A similar argument applies for why we can |
|
728 expect in the left-hand side of clause \textit{(7)} that the value is of the form |
|
729 @{term "Seq v (Stars vs)"}---the derivative of a star is @{term "SEQ (der c r) |
|
730 (STAR r)"}. Finally, the reason for why we can ignore the second argument |
|
731 in clause \textit{(1)} of @{term inj} is that it will only ever be called in cases |
|
732 where @{term "c=d"}, but the usual linearity restrictions in patterns do |
|
733 not allow us to build this constraint explicitly into our function |
|
734 definition.\footnote{Sulzmann and Lu state this clause as @{thm (lhs) |
|
735 injval.simps(1)[of "c" "c"]} $\dn$ @{thm (rhs) injval.simps(1)[of "c"]}, |
|
736 but our deviation is harmless.} |
|
737 |
|
738 The idea of the @{term inj}-function to ``inject'' a character, say |
|
739 @{term c}, into a value can be made precise by the first part of the |
|
740 following lemma, which shows that the underlying string of an injected |
|
741 value has a prepended character @{term c}; the second part shows that |
|
742 the underlying string of an @{const mkeps}-value is always the empty |
|
743 string (given the regular expression is nullable since otherwise |
|
744 \<open>mkeps\<close> might not be defined). |
|
745 |
|
746 \begin{lemma}\mbox{}\smallskip\\\label{Prf_injval_flat} |
|
747 \begin{tabular}{ll} |
|
748 (1) & @{thm[mode=IfThen] Prf_injval_flat}\\ |
|
749 (2) & @{thm[mode=IfThen] mkeps_flat} |
|
750 \end{tabular} |
|
751 \end{lemma} |
|
752 |
|
753 \begin{proof} |
|
754 Both properties are by routine inductions: the first one can, for example, |
|
755 be proved by induction over the definition of @{term derivatives}; the second by |
|
756 an induction on @{term r}. There are no interesting cases.\qed |
|
757 \end{proof} |
|
758 |
|
759 Having defined the @{const mkeps} and \<open>inj\<close> function we can extend |
|
760 \Brz's matcher so that a value is constructed (assuming the |
|
761 regular expression matches the string). The clauses of the Sulzmann and Lu lexer are |
|
762 |
|
763 \begin{center} |
|
764 \begin{tabular}{lcl} |
|
765 @{thm (lhs) lexer.simps(1)} & $\dn$ & @{thm (rhs) lexer.simps(1)}\\ |
|
766 @{thm (lhs) lexer.simps(2)} & $\dn$ & \<open>case\<close> @{term "lexer (der c r) s"} \<open>of\<close>\\ |
|
767 & & \phantom{$|$} @{term "None"} \<open>\<Rightarrow>\<close> @{term None}\\ |
|
768 & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> @{term "Some (injval r c v)"} |
|
769 \end{tabular} |
|
770 \end{center} |
|
771 |
|
772 \noindent If the regular expression does not match the string, @{const None} is |
|
773 returned. If the regular expression \emph{does} |
|
774 match the string, then @{const Some} value is returned. One important |
|
775 virtue of this algorithm is that it can be implemented with ease in any |
|
776 functional programming language and also in Isabelle/HOL. In the remaining |
|
777 part of this section we prove that this algorithm is correct. |
|
778 |
|
779 The well-known idea of POSIX matching is informally defined by some |
|
780 rules such as the Longest Match and Priority Rules (see |
|
781 Introduction); as correctly argued in \cite{Sulzmann2014}, this |
|
782 needs formal specification. Sulzmann and Lu define an ``ordering |
|
783 relation'' between values and argue that there is a maximum value, |
|
784 as given by the derivative-based algorithm. In contrast, we shall |
|
785 introduce a simple inductive definition that specifies directly what |
|
786 a \emph{POSIX value} is, incorporating the POSIX-specific choices |
|
787 into the side-conditions of our rules. Our definition is inspired by |
|
788 the matching relation given by Vansummeren~\cite{Vansummeren2006}. |
|
789 The relation we define is ternary and |
|
790 written as \mbox{@{term "s \<in> r \<rightarrow> v"}}, relating |
|
791 strings, regular expressions and values; the inductive rules are given in |
|
792 Figure~\ref{POSIXrules}. |
|
793 We can prove that given a string @{term s} and regular expression @{term |
|
794 r}, the POSIX value @{term v} is uniquely determined by @{term "s \<in> r \<rightarrow> v"}. |
|
795 |
|
796 % |
|
797 \begin{figure}[t] |
|
798 \begin{center} |
|
799 \begin{tabular}{c} |
|
800 @{thm[mode=Axiom] Posix.intros(1)}\<open>P\<close>@{term "ONE"} \qquad |
|
801 @{thm[mode=Axiom] Posix.intros(2)}\<open>P\<close>@{term "c"}\medskip\\ |
|
802 @{thm[mode=Rule] Posix.intros(3)[of "s" "r\<^sub>1" "v" "r\<^sub>2"]}\<open>P+L\<close>\qquad |
|
803 @{thm[mode=Rule] Posix.intros(4)[of "s" "r\<^sub>2" "v" "r\<^sub>1"]}\<open>P+R\<close>\medskip\\ |
|
804 $\mprset{flushleft} |
|
805 \inferrule |
|
806 {@{thm (prem 1) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]} \qquad |
|
807 @{thm (prem 2) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]} \\\\ |
|
808 @{thm (prem 3) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]}} |
|
809 {@{thm (concl) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]}}$\<open>PS\<close>\\ |
|
810 @{thm[mode=Axiom] Posix.intros(7)}\<open>P[]\<close>\medskip\\ |
|
811 $\mprset{flushleft} |
|
812 \inferrule |
|
813 {@{thm (prem 1) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad |
|
814 @{thm (prem 2) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad |
|
815 @{thm (prem 3) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \\\\ |
|
816 @{thm (prem 4) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}} |
|
817 {@{thm (concl) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}$\<open>P\<star>\<close> |
|
818 \end{tabular} |
|
819 \end{center} |
|
820 \caption{Our inductive definition of POSIX values.}\label{POSIXrules} |
|
821 \end{figure} |
|
822 |
|
823 |
|
824 |
|
825 \begin{theorem}\mbox{}\smallskip\\\label{posixdeterm} |
|
826 \begin{tabular}{ll} |
|
827 (1) & If @{thm (prem 1) Posix1(1)} then @{thm (concl) |
|
828 Posix1(1)} and @{thm (concl) Posix1(2)}.\\ |
|
829 (2) & @{thm[mode=IfThen] Posix_determ(1)[of _ _ "v" "v'"]} |
|
830 \end{tabular} |
|
831 \end{theorem} |
|
832 |
|
833 \begin{proof} Both by induction on the definition of @{term "s \<in> r \<rightarrow> v"}. |
|
834 The second parts follows by a case analysis of @{term "s \<in> r \<rightarrow> v'"} and |
|
835 the first part.\qed |
|
836 \end{proof} |
|
837 |
|
838 \noindent |
|
839 We claim that our @{term "s \<in> r \<rightarrow> v"} relation captures the idea behind the four |
|
840 informal POSIX rules shown in the Introduction: Consider for example the |
|
841 rules \<open>P+L\<close> and \<open>P+R\<close> where the POSIX value for a string |
|
842 and an alternative regular expression, that is @{term "(s, ALT r\<^sub>1 r\<^sub>2)"}, |
|
843 is specified---it is always a \<open>Left\<close>-value, \emph{except} when the |
|
844 string to be matched is not in the language of @{term "r\<^sub>1"}; only then it |
|
845 is a \<open>Right\<close>-value (see the side-condition in \<open>P+R\<close>). |
|
846 Interesting is also the rule for sequence regular expressions (\<open>PS\<close>). The first two premises state that @{term "v\<^sub>1"} and @{term "v\<^sub>2"} |
|
847 are the POSIX values for @{term "(s\<^sub>1, r\<^sub>1)"} and @{term "(s\<^sub>2, r\<^sub>2)"} |
|
848 respectively. Consider now the third premise and note that the POSIX value |
|
849 of this rule should match the string \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}}. According to the |
|
850 Longest Match Rule, we want that the @{term "s\<^sub>1"} is the longest initial |
|
851 split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} such that @{term "s\<^sub>2"} is still recognised |
|
852 by @{term "r\<^sub>2"}. Let us assume, contrary to the third premise, that there |
|
853 \emph{exist} an @{term "s\<^sub>3"} and @{term "s\<^sub>4"} such that @{term "s\<^sub>2"} |
|
854 can be split up into a non-empty string @{term "s\<^sub>3"} and a possibly empty |
|
855 string @{term "s\<^sub>4"}. Moreover the longer string @{term "s\<^sub>1 @ s\<^sub>3"} can be |
|
856 matched by \<open>r\<^sub>1\<close> and the shorter @{term "s\<^sub>4"} can still be |
|
857 matched by @{term "r\<^sub>2"}. In this case @{term "s\<^sub>1"} would \emph{not} be the |
|
858 longest initial split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} and therefore @{term "Seq v\<^sub>1 |
|
859 v\<^sub>2"} cannot be a POSIX value for @{term "(s\<^sub>1 @ s\<^sub>2, SEQ r\<^sub>1 r\<^sub>2)"}. |
|
860 The main point is that our side-condition ensures the Longest |
|
861 Match Rule is satisfied. |
|
862 |
|
863 A similar condition is imposed on the POSIX value in the \<open>P\<star>\<close>-rule. Also there we want that @{term "s\<^sub>1"} is the longest initial |
|
864 split of @{term "s\<^sub>1 @ s\<^sub>2"} and furthermore the corresponding value |
|
865 @{term v} cannot be flattened to the empty string. In effect, we require |
|
866 that in each ``iteration'' of the star, some non-empty substring needs to |
|
867 be ``chipped'' away; only in case of the empty string we accept @{term |
|
868 "Stars []"} as the POSIX value. Indeed we can show that our POSIX values |
|
869 are lexical values which exclude those \<open>Stars\<close> that contain subvalues |
|
870 that flatten to the empty string. |
|
871 |
|
872 \begin{lemma}\label{LVposix} |
|
873 @{thm [mode=IfThen] Posix_LV} |
|
874 \end{lemma} |
|
875 |
|
876 \begin{proof} |
|
877 By routine induction on @{thm (prem 1) Posix_LV}.\qed |
|
878 \end{proof} |
|
879 |
|
880 \noindent |
|
881 Next is the lemma that shows the function @{term "mkeps"} calculates |
|
882 the POSIX value for the empty string and a nullable regular expression. |
|
883 |
|
884 \begin{lemma}\label{lemmkeps} |
|
885 @{thm[mode=IfThen] Posix_mkeps} |
|
886 \end{lemma} |
|
887 |
|
888 \begin{proof} |
|
889 By routine induction on @{term r}.\qed |
|
890 \end{proof} |
|
891 |
|
892 \noindent |
|
893 The central lemma for our POSIX relation is that the \<open>inj\<close>-function |
|
894 preserves POSIX values. |
|
895 |
|
896 \begin{lemma}\label{Posix2} |
|
897 @{thm[mode=IfThen] Posix_injval} |
|
898 \end{lemma} |
|
899 |
|
900 \begin{proof} |
|
901 By induction on \<open>r\<close>. We explain two cases. |
|
902 |
|
903 \begin{itemize} |
|
904 \item[$\bullet$] Case @{term "r = ALT r\<^sub>1 r\<^sub>2"}. There are |
|
905 two subcases, namely \<open>(a)\<close> \mbox{@{term "v = Left v'"}} and @{term |
|
906 "s \<in> der c r\<^sub>1 \<rightarrow> v'"}; and \<open>(b)\<close> @{term "v = Right v'"}, @{term |
|
907 "s \<notin> L (der c r\<^sub>1)"} and @{term "s \<in> der c r\<^sub>2 \<rightarrow> v'"}. In \<open>(a)\<close> we |
|
908 know @{term "s \<in> der c r\<^sub>1 \<rightarrow> v'"}, from which we can infer @{term "(c # s) |
|
909 \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v'"} by induction hypothesis and hence @{term "(c # |
|
910 s) \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> injval (ALT r\<^sub>1 r\<^sub>2) c (Left v')"} as needed. Similarly |
|
911 in subcase \<open>(b)\<close> where, however, in addition we have to use |
|
912 Proposition~\ref{derprop}(2) in order to infer @{term "c # s \<notin> L r\<^sub>1"} from @{term |
|
913 "s \<notin> L (der c r\<^sub>1)"}.\smallskip |
|
914 |
|
915 \item[$\bullet$] Case @{term "r = SEQ r\<^sub>1 r\<^sub>2"}. There are three subcases: |
|
916 |
|
917 \begin{quote} |
|
918 \begin{description} |
|
919 \item[\<open>(a)\<close>] @{term "v = Left (Seq v\<^sub>1 v\<^sub>2)"} and @{term "nullable r\<^sub>1"} |
|
920 \item[\<open>(b)\<close>] @{term "v = Right v\<^sub>1"} and @{term "nullable r\<^sub>1"} |
|
921 \item[\<open>(c)\<close>] @{term "v = Seq v\<^sub>1 v\<^sub>2"} and @{term "\<not> nullable r\<^sub>1"} |
|
922 \end{description} |
|
923 \end{quote} |
|
924 |
|
925 \noindent For \<open>(a)\<close> we know @{term "s\<^sub>1 \<in> der c r\<^sub>1 \<rightarrow> v\<^sub>1"} and |
|
926 @{term "s\<^sub>2 \<in> r\<^sub>2 \<rightarrow> v\<^sub>2"} as well as |
|
927 % |
|
928 \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s\<^sub>2 \<and> s\<^sub>1 @ s\<^sub>3 \<in> L (der c r\<^sub>1) \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\] |
|
929 |
|
930 \noindent From the latter we can infer by Proposition~\ref{derprop}(2): |
|
931 % |
|
932 \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s\<^sub>2 \<and> (c # s\<^sub>1) @ s\<^sub>3 \<in> L r\<^sub>1 \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\] |
|
933 |
|
934 \noindent We can use the induction hypothesis for \<open>r\<^sub>1\<close> to obtain |
|
935 @{term "(c # s\<^sub>1) \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v\<^sub>1"}. Putting this all together allows us to infer |
|
936 @{term "((c # s\<^sub>1) @ s\<^sub>2) \<in> SEQ r\<^sub>1 r\<^sub>2 \<rightarrow> Seq (injval r\<^sub>1 c v\<^sub>1) v\<^sub>2"}. The case \<open>(c)\<close> |
|
937 is similar. |
|
938 |
|
939 For \<open>(b)\<close> we know @{term "s \<in> der c r\<^sub>2 \<rightarrow> v\<^sub>1"} and |
|
940 @{term "s\<^sub>1 @ s\<^sub>2 \<notin> L (SEQ (der c r\<^sub>1) r\<^sub>2)"}. From the former |
|
941 we have @{term "(c # s) \<in> r\<^sub>2 \<rightarrow> (injval r\<^sub>2 c v\<^sub>1)"} by induction hypothesis |
|
942 for @{term "r\<^sub>2"}. From the latter we can infer |
|
943 % |
|
944 \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = c # s \<and> s\<^sub>3 \<in> L r\<^sub>1 \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\] |
|
945 |
|
946 \noindent By Lemma~\ref{lemmkeps} we know @{term "[] \<in> r\<^sub>1 \<rightarrow> (mkeps r\<^sub>1)"} |
|
947 holds. Putting this all together, we can conclude with @{term "(c # |
|
948 s) \<in> SEQ r\<^sub>1 r\<^sub>2 \<rightarrow> Seq (mkeps r\<^sub>1) (injval r\<^sub>2 c v\<^sub>1)"}, as required. |
|
949 |
|
950 Finally suppose @{term "r = STAR r\<^sub>1"}. This case is very similar to the |
|
951 sequence case, except that we need to also ensure that @{term "flat (injval r\<^sub>1 |
|
952 c v\<^sub>1) \<noteq> []"}. This follows from @{term "(c # s\<^sub>1) |
|
953 \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v\<^sub>1"} (which in turn follows from @{term "s\<^sub>1 \<in> der c |
|
954 r\<^sub>1 \<rightarrow> v\<^sub>1"} and the induction hypothesis).\qed |
|
955 \end{itemize} |
|
956 \end{proof} |
|
957 |
|
958 \noindent |
|
959 With Lemma~\ref{Posix2} in place, it is completely routine to establish |
|
960 that the Sulzmann and Lu lexer satisfies our specification (returning |
|
961 the null value @{term "None"} iff the string is not in the language of the regular expression, |
|
962 and returning a unique POSIX value iff the string \emph{is} in the language): |
|
963 |
|
964 \begin{theorem}\mbox{}\smallskip\\\label{lexercorrect} |
|
965 \begin{tabular}{ll} |
|
966 (1) & @{thm (lhs) lexer_correct_None} if and only if @{thm (rhs) lexer_correct_None}\\ |
|
967 (2) & @{thm (lhs) lexer_correct_Some} if and only if @{thm (rhs) lexer_correct_Some}\\ |
|
968 \end{tabular} |
|
969 \end{theorem} |
|
970 |
|
971 \begin{proof} |
|
972 By induction on @{term s} using Lemma~\ref{lemmkeps} and \ref{Posix2}.\qed |
|
973 \end{proof} |
|
974 |
|
975 \noindent In \textit{(2)} we further know by Theorem~\ref{posixdeterm} that the |
|
976 value returned by the lexer must be unique. A simple corollary |
|
977 of our two theorems is: |
|
978 |
|
979 \begin{corollary}\mbox{}\smallskip\\\label{lexercorrectcor} |
|
980 \begin{tabular}{ll} |
|
981 (1) & @{thm (lhs) lexer_correctness(2)} if and only if @{thm (rhs) lexer_correctness(2)}\\ |
|
982 (2) & @{thm (lhs) lexer_correctness(1)} if and only if @{thm (rhs) lexer_correctness(1)}\\ |
|
983 \end{tabular} |
|
984 \end{corollary} |
|
985 |
|
986 \noindent This concludes our correctness proof. Note that we have |
|
987 not changed the algorithm of Sulzmann and Lu,\footnote{All |
|
988 deviations we introduced are harmless.} but introduced our own |
|
989 specification for what a correct result---a POSIX value---should be. |
|
990 In the next section we show that our specification coincides with |
|
991 another one given by Okui and Suzuki using a different technique. |
|
992 |
|
993 \<close> |
|
994 |
|
995 section \<open>Ordering of Values according to Okui and Suzuki\<close> |
|
996 |
|
997 text \<open> |
|
998 |
|
999 While in the previous section we have defined POSIX values directly |
|
1000 in terms of a ternary relation (see inference rules in Figure~\ref{POSIXrules}), |
|
1001 Sulzmann and Lu took a different approach in \cite{Sulzmann2014}: |
|
1002 they introduced an ordering for values and identified POSIX values |
|
1003 as the maximal elements. An extended version of \cite{Sulzmann2014} |
|
1004 is available at the website of its first author; this includes more |
|
1005 details of their proofs, but which are evidently not in final form |
|
1006 yet. Unfortunately, we were not able to verify claims that their |
|
1007 ordering has properties such as being transitive or having maximal |
|
1008 elements. |
|
1009 |
|
1010 Okui and Suzuki \cite{OkuiSuzuki2010,OkuiSuzukiTech} described |
|
1011 another ordering of values, which they use to establish the |
|
1012 correctness of their automata-based algorithm for POSIX matching. |
|
1013 Their ordering resembles some aspects of the one given by Sulzmann |
|
1014 and Lu, but overall is quite different. To begin with, Okui and |
|
1015 Suzuki identify POSIX values as minimal, rather than maximal, |
|
1016 elements in their ordering. A more substantial difference is that |
|
1017 the ordering by Okui and Suzuki uses \emph{positions} in order to |
|
1018 identify and compare subvalues. Positions are lists of natural |
|
1019 numbers. This allows them to quite naturally formalise the Longest |
|
1020 Match and Priority rules of the informal POSIX standard. Consider |
|
1021 for example the value @{term v} |
|
1022 |
|
1023 \begin{center} |
|
1024 @{term "v == Stars [Seq (Char x) (Char y), Char z]"} |
|
1025 \end{center} |
|
1026 |
|
1027 \noindent |
|
1028 At position \<open>[0,1]\<close> of this value is the |
|
1029 subvalue \<open>Char y\<close> and at position \<open>[1]\<close> the |
|
1030 subvalue @{term "Char z"}. At the `root' position, or empty list |
|
1031 @{term "[]"}, is the whole value @{term v}. Positions such as \<open>[0,1,0]\<close> or \<open>[2]\<close> are outside of \<open>v\<close>. If it exists, the subvalue of @{term v} at a position \<open>p\<close>, written @{term "at v p"}, can be recursively defined by |
|
1032 |
|
1033 \begin{center} |
|
1034 \begin{tabular}{r@ {\hspace{0mm}}lcl} |
|
1035 @{term v} & \<open>\<downharpoonleft>\<^bsub>[]\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(1)}\\ |
|
1036 @{term "Left v"} & \<open>\<downharpoonleft>\<^bsub>0::ps\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(2)}\\ |
|
1037 @{term "Right v"} & \<open>\<downharpoonleft>\<^bsub>1::ps\<^esub>\<close> & \<open>\<equiv>\<close> & |
|
1038 @{thm (rhs) at.simps(3)[simplified Suc_0_fold]}\\ |
|
1039 @{term "Seq v\<^sub>1 v\<^sub>2"} & \<open>\<downharpoonleft>\<^bsub>0::ps\<^esub>\<close> & \<open>\<equiv>\<close> & |
|
1040 @{thm (rhs) at.simps(4)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \\ |
|
1041 @{term "Seq v\<^sub>1 v\<^sub>2"} & \<open>\<downharpoonleft>\<^bsub>1::ps\<^esub>\<close> |
|
1042 & \<open>\<equiv>\<close> & |
|
1043 @{thm (rhs) at.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2", simplified Suc_0_fold]} \\ |
|
1044 @{term "Stars vs"} & \<open>\<downharpoonleft>\<^bsub>n::ps\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(6)}\\ |
|
1045 \end{tabular} |
|
1046 \end{center} |
|
1047 |
|
1048 \noindent In the last clause we use Isabelle's notation @{term "vs ! n"} for the |
|
1049 \<open>n\<close>th element in a list. The set of positions inside a value \<open>v\<close>, |
|
1050 written @{term "Pos v"}, is given by |
|
1051 |
|
1052 \begin{center} |
|
1053 \begin{tabular}{lcl} |
|
1054 @{thm (lhs) Pos.simps(1)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(1)}\\ |
|
1055 @{thm (lhs) Pos.simps(2)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(2)}\\ |
|
1056 @{thm (lhs) Pos.simps(3)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(3)}\\ |
|
1057 @{thm (lhs) Pos.simps(4)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(4)}\\ |
|
1058 @{thm (lhs) Pos.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} |
|
1059 & \<open>\<equiv>\<close> |
|
1060 & @{thm (rhs) Pos.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1061 @{thm (lhs) Pos_stars} & \<open>\<equiv>\<close> & @{thm (rhs) Pos_stars}\\ |
|
1062 \end{tabular} |
|
1063 \end{center} |
|
1064 |
|
1065 \noindent |
|
1066 whereby \<open>len\<close> in the last clause stands for the length of a list. Clearly |
|
1067 for every position inside a value there exists a subvalue at that position. |
|
1068 |
|
1069 |
|
1070 To help understanding the ordering of Okui and Suzuki, consider again |
|
1071 the earlier value |
|
1072 \<open>v\<close> and compare it with the following \<open>w\<close>: |
|
1073 |
|
1074 \begin{center} |
|
1075 \begin{tabular}{l} |
|
1076 @{term "v == Stars [Seq (Char x) (Char y), Char z]"}\\ |
|
1077 @{term "w == Stars [Char x, Char y, Char z]"} |
|
1078 \end{tabular} |
|
1079 \end{center} |
|
1080 |
|
1081 \noindent Both values match the string \<open>xyz\<close>, that means if |
|
1082 we flatten these values at their respective root position, we obtain |
|
1083 \<open>xyz\<close>. However, at position \<open>[0]\<close>, \<open>v\<close> matches |
|
1084 \<open>xy\<close> whereas \<open>w\<close> matches only the shorter \<open>x\<close>. So |
|
1085 according to the Longest Match Rule, we should prefer \<open>v\<close>, |
|
1086 rather than \<open>w\<close> as POSIX value for string \<open>xyz\<close> (and |
|
1087 corresponding regular expression). In order to |
|
1088 formalise this idea, Okui and Suzuki introduce a measure for |
|
1089 subvalues at position \<open>p\<close>, called the \emph{norm} of \<open>v\<close> |
|
1090 at position \<open>p\<close>. We can define this measure in Isabelle as an |
|
1091 integer as follows |
|
1092 |
|
1093 \begin{center} |
|
1094 @{thm pflat_len_def} |
|
1095 \end{center} |
|
1096 |
|
1097 \noindent where we take the length of the flattened value at |
|
1098 position \<open>p\<close>, provided the position is inside \<open>v\<close>; if |
|
1099 not, then the norm is \<open>-1\<close>. The default for outside |
|
1100 positions is crucial for the POSIX requirement of preferring a |
|
1101 \<open>Left\<close>-value over a \<open>Right\<close>-value (if they can match the |
|
1102 same string---see the Priority Rule from the Introduction). For this |
|
1103 consider |
|
1104 |
|
1105 \begin{center} |
|
1106 @{term "v == Left (Char x)"} \qquad and \qquad @{term "w == Right (Char x)"} |
|
1107 \end{center} |
|
1108 |
|
1109 \noindent Both values match \<open>x\<close>. At position \<open>[0]\<close> |
|
1110 the norm of @{term v} is \<open>1\<close> (the subvalue matches \<open>x\<close>), |
|
1111 but the norm of \<open>w\<close> is \<open>-1\<close> (the position is outside |
|
1112 \<open>w\<close> according to how we defined the `inside' positions of |
|
1113 \<open>Left\<close>- and \<open>Right\<close>-values). Of course at position |
|
1114 \<open>[1]\<close>, the norms @{term "pflat_len v [1]"} and @{term |
|
1115 "pflat_len w [1]"} are reversed, but the point is that subvalues |
|
1116 will be analysed according to lexicographically ordered |
|
1117 positions. According to this ordering, the position \<open>[0]\<close> |
|
1118 takes precedence over \<open>[1]\<close> and thus also \<open>v\<close> will be |
|
1119 preferred over \<open>w\<close>. The lexicographic ordering of positions, written |
|
1120 @{term "DUMMY \<sqsubset>lex DUMMY"}, can be conveniently formalised |
|
1121 by three inference rules |
|
1122 |
|
1123 \begin{center} |
|
1124 \begin{tabular}{ccc} |
|
1125 @{thm [mode=Axiom] lex_list.intros(1)}\hspace{1cm} & |
|
1126 @{thm [mode=Rule] lex_list.intros(3)[where ?p1.0="p\<^sub>1" and ?p2.0="p\<^sub>2" and |
|
1127 ?ps1.0="ps\<^sub>1" and ?ps2.0="ps\<^sub>2"]}\hspace{1cm} & |
|
1128 @{thm [mode=Rule] lex_list.intros(2)[where ?ps1.0="ps\<^sub>1" and ?ps2.0="ps\<^sub>2"]} |
|
1129 \end{tabular} |
|
1130 \end{center} |
|
1131 |
|
1132 With the norm and lexicographic order in place, |
|
1133 we can state the key definition of Okui and Suzuki |
|
1134 \cite{OkuiSuzuki2010}: a value @{term "v\<^sub>1"} is \emph{smaller at position \<open>p\<close>} than |
|
1135 @{term "v\<^sub>2"}, written @{term "v\<^sub>1 \<sqsubset>val p v\<^sub>2"}, |
|
1136 if and only if $(i)$ the norm at position \<open>p\<close> is |
|
1137 greater in @{term "v\<^sub>1"} (that is the string @{term "flat (at v\<^sub>1 p)"} is longer |
|
1138 than @{term "flat (at v\<^sub>2 p)"}) and $(ii)$ all subvalues at |
|
1139 positions that are inside @{term "v\<^sub>1"} or @{term "v\<^sub>2"} and that are |
|
1140 lexicographically smaller than \<open>p\<close>, we have the same norm, namely |
|
1141 |
|
1142 \begin{center} |
|
1143 \begin{tabular}{c} |
|
1144 @{thm (lhs) PosOrd_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} |
|
1145 \<open>\<equiv>\<close> |
|
1146 $\begin{cases} |
|
1147 (i) & @{term "pflat_len v\<^sub>1 p > pflat_len v\<^sub>2 p"} \quad\text{and}\smallskip \\ |
|
1148 (ii) & @{term "(\<forall>q \<in> Pos v\<^sub>1 \<union> Pos v\<^sub>2. q \<sqsubset>lex p --> pflat_len v\<^sub>1 q = pflat_len v\<^sub>2 q)"} |
|
1149 \end{cases}$ |
|
1150 \end{tabular} |
|
1151 \end{center} |
|
1152 |
|
1153 \noindent The position \<open>p\<close> in this definition acts as the |
|
1154 \emph{first distinct position} of \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close>, where both values match strings of different length |
|
1155 \cite{OkuiSuzuki2010}. Since at \<open>p\<close> the values \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> match different strings, the |
|
1156 ordering is irreflexive. Derived from the definition above |
|
1157 are the following two orderings: |
|
1158 |
|
1159 \begin{center} |
|
1160 \begin{tabular}{l} |
|
1161 @{thm PosOrd_ex_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1162 @{thm PosOrd_ex_eq_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} |
|
1163 \end{tabular} |
|
1164 \end{center} |
|
1165 |
|
1166 While we encountered a number of obstacles for establishing properties like |
|
1167 transitivity for the ordering of Sulzmann and Lu (and which we failed |
|
1168 to overcome), it is relatively straightforward to establish this |
|
1169 property for the orderings |
|
1170 @{term "DUMMY :\<sqsubset>val DUMMY"} and @{term "DUMMY :\<sqsubseteq>val DUMMY"} |
|
1171 by Okui and Suzuki. |
|
1172 |
|
1173 \begin{lemma}[Transitivity]\label{transitivity} |
|
1174 @{thm [mode=IfThen] PosOrd_trans[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and ?v3.0="v\<^sub>3"]} |
|
1175 \end{lemma} |
|
1176 |
|
1177 \begin{proof} From the assumption we obtain two positions \<open>p\<close> |
|
1178 and \<open>q\<close>, where the values \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> (respectively \<open>v\<^sub>2\<close> and \<open>v\<^sub>3\<close>) are `distinct'. Since \<open>\<prec>\<^bsub>lex\<^esub>\<close> is trichotomous, we need to consider |
|
1179 three cases, namely @{term "p = q"}, @{term "p \<sqsubset>lex q"} and |
|
1180 @{term "q \<sqsubset>lex p"}. Let us look at the first case. Clearly |
|
1181 @{term "pflat_len v\<^sub>2 p < pflat_len v\<^sub>1 p"} and @{term |
|
1182 "pflat_len v\<^sub>3 p < pflat_len v\<^sub>2 p"} imply @{term |
|
1183 "pflat_len v\<^sub>3 p < pflat_len v\<^sub>1 p"}. It remains to show |
|
1184 that for a @{term "p' \<in> Pos v\<^sub>1 \<union> Pos v\<^sub>3"} |
|
1185 with @{term "p' \<sqsubset>lex p"} that @{term "pflat_len v\<^sub>1 |
|
1186 p' = pflat_len v\<^sub>3 p'"} holds. Suppose @{term "p' \<in> Pos |
|
1187 v\<^sub>1"}, then we can infer from the first assumption that @{term |
|
1188 "pflat_len v\<^sub>1 p' = pflat_len v\<^sub>2 p'"}. But this means |
|
1189 that @{term "p'"} must be in @{term "Pos v\<^sub>2"} too (the norm |
|
1190 cannot be \<open>-1\<close> given @{term "p' \<in> Pos v\<^sub>1"}). |
|
1191 Hence we can use the second assumption and |
|
1192 infer @{term "pflat_len v\<^sub>2 p' = pflat_len v\<^sub>3 p'"}, |
|
1193 which concludes this case with @{term "v\<^sub>1 :\<sqsubset>val |
|
1194 v\<^sub>3"}. The reasoning in the other cases is similar.\qed |
|
1195 \end{proof} |
|
1196 |
|
1197 \noindent |
|
1198 The proof for $\preccurlyeq$ is similar and omitted. |
|
1199 It is also straightforward to show that \<open>\<prec>\<close> and |
|
1200 $\preccurlyeq$ are partial orders. Okui and Suzuki furthermore show that they |
|
1201 are linear orderings for lexical values \cite{OkuiSuzuki2010} of a given |
|
1202 regular expression and given string, but we have not formalised this in Isabelle. It is |
|
1203 not essential for our results. What we are going to show below is |
|
1204 that for a given \<open>r\<close> and \<open>s\<close>, the orderings have a unique |
|
1205 minimal element on the set @{term "LV r s"}, which is the POSIX value |
|
1206 we defined in the previous section. We start with two properties that |
|
1207 show how the length of a flattened value relates to the \<open>\<prec>\<close>-ordering. |
|
1208 |
|
1209 \begin{proposition}\mbox{}\smallskip\\\label{ordlen} |
|
1210 \begin{tabular}{@ {}ll} |
|
1211 (1) & |
|
1212 @{thm [mode=IfThen] PosOrd_shorterE[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1213 (2) & |
|
1214 @{thm [mode=IfThen] PosOrd_shorterI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} |
|
1215 \end{tabular} |
|
1216 \end{proposition} |
|
1217 |
|
1218 \noindent Both properties follow from the definition of the ordering. Note that |
|
1219 \textit{(2)} entails that a value, say @{term "v\<^sub>2"}, whose underlying |
|
1220 string is a strict prefix of another flattened value, say @{term "v\<^sub>1"}, then |
|
1221 @{term "v\<^sub>1"} must be smaller than @{term "v\<^sub>2"}. For our proofs it |
|
1222 will be useful to have the following properties---in each case the underlying strings |
|
1223 of the compared values are the same: |
|
1224 |
|
1225 \begin{proposition}\mbox{}\smallskip\\\label{ordintros} |
|
1226 \begin{tabular}{ll} |
|
1227 \textit{(1)} & |
|
1228 @{thm [mode=IfThen] PosOrd_Left_Right[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1229 \textit{(2)} & If |
|
1230 @{thm (prem 1) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;then\; |
|
1231 @{thm (lhs) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;iff\; |
|
1232 @{thm (rhs) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1233 \textit{(3)} & If |
|
1234 @{thm (prem 1) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;then\; |
|
1235 @{thm (lhs) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;iff\; |
|
1236 @{thm (rhs) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\ |
|
1237 \textit{(4)} & If |
|
1238 @{thm (prem 1) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]} \;then\; |
|
1239 @{thm (lhs) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]} \;iff\; |
|
1240 @{thm (rhs) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]}\\ |
|
1241 \textit{(5)} & If |
|
1242 @{thm (prem 2) PosOrd_SeqI1[simplified, where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1243 ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]} \;and\; |
|
1244 @{thm (prem 1) PosOrd_SeqI1[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1245 ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]} \;then\; |
|
1246 @{thm (concl) PosOrd_SeqI1[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1247 ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]}\\ |
|
1248 \textit{(6)} & If |
|
1249 @{thm (prem 1) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;then\; |
|
1250 @{thm (lhs) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;iff\; |
|
1251 @{thm (rhs) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]}\\ |
|
1252 |
|
1253 \textit{(7)} & If |
|
1254 @{thm (prem 2) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1255 ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;and\; |
|
1256 @{thm (prem 1) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1257 ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;then\; |
|
1258 @{thm (concl) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and |
|
1259 ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]}\\ |
|
1260 \end{tabular} |
|
1261 \end{proposition} |
|
1262 |
|
1263 \noindent One might prefer that statements \textit{(4)} and \textit{(5)} |
|
1264 (respectively \textit{(6)} and \textit{(7)}) |
|
1265 are combined into a single \textit{iff}-statement (like the ones for \<open>Left\<close> and \<open>Right\<close>). Unfortunately this cannot be done easily: such |
|
1266 a single statement would require an additional assumption about the |
|
1267 two values @{term "Seq v\<^sub>1 v\<^sub>2"} and @{term "Seq w\<^sub>1 w\<^sub>2"} |
|
1268 being inhabited by the same regular expression. The |
|
1269 complexity of the proofs involved seems to not justify such a |
|
1270 `cleaner' single statement. The statements given are just the properties that |
|
1271 allow us to establish our theorems without any difficulty. The proofs |
|
1272 for Proposition~\ref{ordintros} are routine. |
|
1273 |
|
1274 |
|
1275 Next we establish how Okui and Suzuki's orderings relate to our |
|
1276 definition of POSIX values. Given a \<open>POSIX\<close> value \<open>v\<^sub>1\<close> |
|
1277 for \<open>r\<close> and \<open>s\<close>, then any other lexical value \<open>v\<^sub>2\<close> in @{term "LV r s"} is greater or equal than \<open>v\<^sub>1\<close>, namely: |
|
1278 |
|
1279 |
|
1280 \begin{theorem}\label{orderone} |
|
1281 @{thm [mode=IfThen] Posix_PosOrd[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} |
|
1282 \end{theorem} |
|
1283 |
|
1284 \begin{proof} By induction on our POSIX rules. By |
|
1285 Theorem~\ref{posixdeterm} and the definition of @{const LV}, it is clear |
|
1286 that \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> have the same |
|
1287 underlying string @{term s}. The three base cases are |
|
1288 straightforward: for example for @{term "v\<^sub>1 = Void"}, we have |
|
1289 that @{term "v\<^sub>2 \<in> LV ONE []"} must also be of the form |
|
1290 \mbox{@{term "v\<^sub>2 = Void"}}. Therefore we have @{term |
|
1291 "v\<^sub>1 :\<sqsubseteq>val v\<^sub>2"}. The inductive cases for |
|
1292 \<open>r\<close> being of the form @{term "ALT r\<^sub>1 r\<^sub>2"} and |
|
1293 @{term "SEQ r\<^sub>1 r\<^sub>2"} are as follows: |
|
1294 |
|
1295 |
|
1296 \begin{itemize} |
|
1297 |
|
1298 \item[$\bullet$] Case \<open>P+L\<close> with @{term "s \<in> (ALT r\<^sub>1 r\<^sub>2) |
|
1299 \<rightarrow> (Left w\<^sub>1)"}: In this case the value |
|
1300 @{term "v\<^sub>2"} is either of the |
|
1301 form @{term "Left w\<^sub>2"} or @{term "Right w\<^sub>2"}. In the |
|
1302 latter case we can immediately conclude with \mbox{@{term "v\<^sub>1 |
|
1303 :\<sqsubseteq>val v\<^sub>2"}} since a \<open>Left\<close>-value with the |
|
1304 same underlying string \<open>s\<close> is always smaller than a |
|
1305 \<open>Right\<close>-value by Proposition~\ref{ordintros}\textit{(1)}. |
|
1306 In the former case we have @{term "w\<^sub>2 |
|
1307 \<in> LV r\<^sub>1 s"} and can use the induction hypothesis to infer |
|
1308 @{term "w\<^sub>1 :\<sqsubseteq>val w\<^sub>2"}. Because @{term |
|
1309 "w\<^sub>1"} and @{term "w\<^sub>2"} have the same underlying string |
|
1310 \<open>s\<close>, we can conclude with @{term "Left w\<^sub>1 |
|
1311 :\<sqsubseteq>val Left w\<^sub>2"} using |
|
1312 Proposition~\ref{ordintros}\textit{(2)}.\smallskip |
|
1313 |
|
1314 \item[$\bullet$] Case \<open>P+R\<close> with @{term "s \<in> (ALT r\<^sub>1 r\<^sub>2) |
|
1315 \<rightarrow> (Right w\<^sub>1)"}: This case similar to the previous |
|
1316 case, except that we additionally know @{term "s \<notin> L |
|
1317 r\<^sub>1"}. This is needed when @{term "v\<^sub>2"} is of the form |
|
1318 \mbox{@{term "Left w\<^sub>2"}}. Since \mbox{@{term "flat v\<^sub>2 = flat |
|
1319 w\<^sub>2"} \<open>= s\<close>} and @{term "\<Turnstile> w\<^sub>2 : |
|
1320 r\<^sub>1"}, we can derive a contradiction for \mbox{@{term "s \<notin> L |
|
1321 r\<^sub>1"}} using |
|
1322 Proposition~\ref{inhabs}. So also in this case \mbox{@{term "v\<^sub>1 |
|
1323 :\<sqsubseteq>val v\<^sub>2"}}.\smallskip |
|
1324 |
|
1325 \item[$\bullet$] Case \<open>PS\<close> with @{term "(s\<^sub>1 @ |
|
1326 s\<^sub>2) \<in> (SEQ r\<^sub>1 r\<^sub>2) \<rightarrow> (Seq |
|
1327 w\<^sub>1 w\<^sub>2)"}: We can assume @{term "v\<^sub>2 = Seq |
|
1328 (u\<^sub>1) (u\<^sub>2)"} with @{term "\<Turnstile> u\<^sub>1 : |
|
1329 r\<^sub>1"} and \mbox{@{term "\<Turnstile> u\<^sub>2 : |
|
1330 r\<^sub>2"}}. We have @{term "s\<^sub>1 @ s\<^sub>2 = (flat |
|
1331 u\<^sub>1) @ (flat u\<^sub>2)"}. By the side-condition of the |
|
1332 \<open>PS\<close>-rule we know that either @{term "s\<^sub>1 = flat |
|
1333 u\<^sub>1"} or that @{term "flat u\<^sub>1"} is a strict prefix of |
|
1334 @{term "s\<^sub>1"}. In the latter case we can infer @{term |
|
1335 "w\<^sub>1 :\<sqsubset>val u\<^sub>1"} by |
|
1336 Proposition~\ref{ordlen}\textit{(2)} and from this @{term "v\<^sub>1 |
|
1337 :\<sqsubseteq>val v\<^sub>2"} by Proposition~\ref{ordintros}\textit{(5)} |
|
1338 (as noted above @{term "v\<^sub>1"} and @{term "v\<^sub>2"} must have the |
|
1339 same underlying string). |
|
1340 In the former case we know |
|
1341 @{term "u\<^sub>1 \<in> LV r\<^sub>1 s\<^sub>1"} and @{term |
|
1342 "u\<^sub>2 \<in> LV r\<^sub>2 s\<^sub>2"}. With this we can use the |
|
1343 induction hypotheses to infer @{term "w\<^sub>1 :\<sqsubseteq>val |
|
1344 u\<^sub>1"} and @{term "w\<^sub>2 :\<sqsubseteq>val u\<^sub>2"}. By |
|
1345 Proposition~\ref{ordintros}\textit{(4,5)} we can again infer |
|
1346 @{term "v\<^sub>1 :\<sqsubseteq>val |
|
1347 v\<^sub>2"}. |
|
1348 |
|
1349 \end{itemize} |
|
1350 |
|
1351 \noindent The case for \<open>P\<star>\<close> is similar to the \<open>PS\<close>-case and omitted.\qed |
|
1352 \end{proof} |
|
1353 |
|
1354 \noindent This theorem shows that our \<open>POSIX\<close> value for a |
|
1355 regular expression \<open>r\<close> and string @{term s} is in fact a |
|
1356 minimal element of the values in \<open>LV r s\<close>. By |
|
1357 Proposition~\ref{ordlen}\textit{(2)} we also know that any value in |
|
1358 \<open>LV r s'\<close>, with @{term "s'"} being a strict prefix, cannot be |
|
1359 smaller than \<open>v\<^sub>1\<close>. The next theorem shows the |
|
1360 opposite---namely any minimal element in @{term "LV r s"} must be a |
|
1361 \<open>POSIX\<close> value. This can be established by induction on \<open>r\<close>, but the proof can be drastically simplified by using the fact |
|
1362 from the previous section about the existence of a \<open>POSIX\<close> value |
|
1363 whenever a string @{term "s \<in> L r"}. |
|
1364 |
|
1365 |
|
1366 \begin{theorem} |
|
1367 @{thm [mode=IfThen] PosOrd_Posix[where ?v1.0="v\<^sub>1"]} |
|
1368 \end{theorem} |
|
1369 |
|
1370 \begin{proof} |
|
1371 If @{thm (prem 1) PosOrd_Posix[where ?v1.0="v\<^sub>1"]} then |
|
1372 @{term "s \<in> L r"} by Proposition~\ref{inhabs}. Hence by Theorem~\ref{lexercorrect}(2) |
|
1373 there exists a |
|
1374 \<open>POSIX\<close> value @{term "v\<^sub>P"} with @{term "s \<in> r \<rightarrow> v\<^sub>P"} |
|
1375 and by Lemma~\ref{LVposix} we also have \mbox{@{term "v\<^sub>P \<in> LV r s"}}. |
|
1376 By Theorem~\ref{orderone} we therefore have |
|
1377 @{term "v\<^sub>P :\<sqsubseteq>val v\<^sub>1"}. If @{term "v\<^sub>P = v\<^sub>1"} then |
|
1378 we are done. Otherwise we have @{term "v\<^sub>P :\<sqsubset>val v\<^sub>1"}, which |
|
1379 however contradicts the second assumption about @{term "v\<^sub>1"} being the smallest |
|
1380 element in @{term "LV r s"}. So we are done in this case too.\qed |
|
1381 \end{proof} |
|
1382 |
|
1383 \noindent |
|
1384 From this we can also show |
|
1385 that if @{term "LV r s"} is non-empty (or equivalently @{term "s \<in> L r"}) then |
|
1386 it has a unique minimal element: |
|
1387 |
|
1388 \begin{corollary} |
|
1389 @{thm [mode=IfThen] Least_existence1} |
|
1390 \end{corollary} |
|
1391 |
|
1392 |
|
1393 |
|
1394 \noindent To sum up, we have shown that the (unique) minimal elements |
|
1395 of the ordering by Okui and Suzuki are exactly the \<open>POSIX\<close> |
|
1396 values we defined inductively in Section~\ref{posixsec}. This provides |
|
1397 an independent confirmation that our ternary relation formalises the |
|
1398 informal POSIX rules. |
|
1399 |
|
1400 \<close> |
|
1401 |
|
1402 section \<open>Bitcoded Lexing\<close> |
|
1403 |
|
1404 |
|
1405 |
|
1406 |
|
1407 text \<open> |
|
1408 |
|
1409 Incremental calculation of the value. To simplify the proof we first define the function |
|
1410 @{const flex} which calculates the ``iterated'' injection function. With this we can |
|
1411 rewrite the lexer as |
|
1412 |
|
1413 \begin{center} |
|
1414 @{thm lexer_flex} |
|
1415 \end{center} |
|
1416 |
|
1417 |
|
1418 \<close> |
|
1419 |
|
1420 section \<open>Optimisations\<close> |
|
1421 |
|
1422 text \<open> |
|
1423 |
|
1424 Derivatives as calculated by \Brz's method are usually more complex |
|
1425 regular expressions than the initial one; the result is that the |
|
1426 derivative-based matching and lexing algorithms are often abysmally slow. |
|
1427 However, various optimisations are possible, such as the simplifications |
|
1428 of @{term "ALT ZERO r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and |
|
1429 @{term "SEQ r ONE"} to @{term r}. These simplifications can speed up the |
|
1430 algorithms considerably, as noted in \cite{Sulzmann2014}. One of the |
|
1431 advantages of having a simple specification and correctness proof is that |
|
1432 the latter can be refined to prove the correctness of such simplification |
|
1433 steps. While the simplification of regular expressions according to |
|
1434 rules like |
|
1435 |
|
1436 \begin{equation}\label{Simpl} |
|
1437 \begin{array}{lcllcllcllcl} |
|
1438 @{term "ALT ZERO r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1439 @{term "ALT r ZERO"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1440 @{term "SEQ ONE r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1441 @{term "SEQ r ONE"} & \<open>\<Rightarrow>\<close> & @{term r} |
|
1442 \end{array} |
|
1443 \end{equation} |
|
1444 |
|
1445 \noindent is well understood, there is an obstacle with the POSIX value |
|
1446 calculation algorithm by Sulzmann and Lu: if we build a derivative regular |
|
1447 expression and then simplify it, we will calculate a POSIX value for this |
|
1448 simplified derivative regular expression, \emph{not} for the original (unsimplified) |
|
1449 derivative regular expression. Sulzmann and Lu \cite{Sulzmann2014} overcome this obstacle by |
|
1450 not just calculating a simplified regular expression, but also calculating |
|
1451 a \emph{rectification function} that ``repairs'' the incorrect value. |
|
1452 |
|
1453 The rectification functions can be (slightly clumsily) implemented in |
|
1454 Isabelle/HOL as follows using some auxiliary functions: |
|
1455 |
|
1456 \begin{center} |
|
1457 \begin{tabular}{lcl} |
|
1458 @{thm (lhs) F_RIGHT.simps(1)} & $\dn$ & \<open>Right (f v)\<close>\\ |
|
1459 @{thm (lhs) F_LEFT.simps(1)} & $\dn$ & \<open>Left (f v)\<close>\\ |
|
1460 |
|
1461 @{thm (lhs) F_ALT.simps(1)} & $\dn$ & \<open>Right (f\<^sub>2 v)\<close>\\ |
|
1462 @{thm (lhs) F_ALT.simps(2)} & $\dn$ & \<open>Left (f\<^sub>1 v)\<close>\\ |
|
1463 |
|
1464 @{thm (lhs) F_SEQ1.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 ()) (f\<^sub>2 v)\<close>\\ |
|
1465 @{thm (lhs) F_SEQ2.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v) (f\<^sub>2 ())\<close>\\ |
|
1466 @{thm (lhs) F_SEQ.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)\<close>\medskip\\ |
|
1467 %\end{tabular} |
|
1468 % |
|
1469 %\begin{tabular}{lcl} |
|
1470 @{term "simp_ALT (ZERO, DUMMY) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_RIGHT f\<^sub>2)"}\\ |
|
1471 @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, DUMMY)"} & $\dn$ & @{term "(r\<^sub>1, F_LEFT f\<^sub>1)"}\\ |
|
1472 @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(ALT r\<^sub>1 r\<^sub>2, F_ALT f\<^sub>1 f\<^sub>2)"}\\ |
|
1473 @{term "simp_SEQ (ONE, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_SEQ1 f\<^sub>1 f\<^sub>2)"}\\ |
|
1474 @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (ONE, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>1, F_SEQ2 f\<^sub>1 f\<^sub>2)"}\\ |
|
1475 @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(SEQ r\<^sub>1 r\<^sub>2, F_SEQ f\<^sub>1 f\<^sub>2)"}\\ |
|
1476 \end{tabular} |
|
1477 \end{center} |
|
1478 |
|
1479 \noindent |
|
1480 The functions \<open>simp\<^bsub>Alt\<^esub>\<close> and \<open>simp\<^bsub>Seq\<^esub>\<close> encode the simplification rules |
|
1481 in \eqref{Simpl} and compose the rectification functions (simplifications can occur |
|
1482 deep inside the regular expression). The main simplification function is then |
|
1483 |
|
1484 \begin{center} |
|
1485 \begin{tabular}{lcl} |
|
1486 @{term "simp (ALT r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_ALT (simp r\<^sub>1) (simp r\<^sub>2)"}\\ |
|
1487 @{term "simp (SEQ r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_SEQ (simp r\<^sub>1) (simp r\<^sub>2)"}\\ |
|
1488 @{term "simp r"} & $\dn$ & @{term "(r, id)"}\\ |
|
1489 \end{tabular} |
|
1490 \end{center} |
|
1491 |
|
1492 \noindent where @{term "id"} stands for the identity function. The |
|
1493 function @{const simp} returns a simplified regular expression and a corresponding |
|
1494 rectification function. Note that we do not simplify under stars: this |
|
1495 seems to slow down the algorithm, rather than speed it up. The optimised |
|
1496 lexer is then given by the clauses: |
|
1497 |
|
1498 \begin{center} |
|
1499 \begin{tabular}{lcl} |
|
1500 @{thm (lhs) slexer.simps(1)} & $\dn$ & @{thm (rhs) slexer.simps(1)}\\ |
|
1501 @{thm (lhs) slexer.simps(2)} & $\dn$ & |
|
1502 \<open>let (r\<^sub>s, f\<^sub>r) = simp (r \<close>$\backslash$\<open> c) in\<close>\\ |
|
1503 & & \<open>case\<close> @{term "slexer r\<^sub>s s"} \<open>of\<close>\\ |
|
1504 & & \phantom{$|$} @{term "None"} \<open>\<Rightarrow>\<close> @{term None}\\ |
|
1505 & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> \<open>Some (inj r c (f\<^sub>r v))\<close> |
|
1506 \end{tabular} |
|
1507 \end{center} |
|
1508 |
|
1509 \noindent |
|
1510 In the second clause we first calculate the derivative @{term "der c r"} |
|
1511 and then simpli |
|
1512 |
|
1513 text \<open> |
|
1514 |
|
1515 Incremental calculation of the value. To simplify the proof we first define the function |
|
1516 @{const flex} which calculates the ``iterated'' injection function. With this we can |
|
1517 rewrite the lexer as |
|
1518 |
|
1519 \begin{center} |
|
1520 @{thm lexer_flex} |
|
1521 \end{center} |
|
1522 |
|
1523 \begin{center} |
|
1524 \begin{tabular}{lcl} |
|
1525 @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\ |
|
1526 @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\ |
|
1527 @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\ |
|
1528 @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\ |
|
1529 @{thm (lhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]} & $\dn$ & @{thm (rhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]}\\ |
|
1530 @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\ |
|
1531 @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)} |
|
1532 \end{tabular} |
|
1533 \end{center} |
|
1534 |
|
1535 \begin{center} |
|
1536 \begin{tabular}{lcl} |
|
1537 @{term areg} & $::=$ & @{term "AZERO"}\\ |
|
1538 & $\mid$ & @{term "AONE bs"}\\ |
|
1539 & $\mid$ & @{term "ACHAR bs c"}\\ |
|
1540 & $\mid$ & @{term "AALT bs r1 r2"}\\ |
|
1541 & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\ |
|
1542 & $\mid$ & @{term "ASTAR bs r"} |
|
1543 \end{tabular} |
|
1544 \end{center} |
|
1545 |
|
1546 |
|
1547 \begin{center} |
|
1548 \begin{tabular}{lcl} |
|
1549 @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\ |
|
1550 @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\ |
|
1551 @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\ |
|
1552 @{thm (lhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1553 @{thm (lhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1554 @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\ |
|
1555 \end{tabular} |
|
1556 \end{center} |
|
1557 |
|
1558 \begin{center} |
|
1559 \begin{tabular}{lcl} |
|
1560 @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\ |
|
1561 @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\ |
|
1562 @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\ |
|
1563 @{thm (lhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1564 @{thm (lhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1565 @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\ |
|
1566 \end{tabular} |
|
1567 \end{center} |
|
1568 |
|
1569 Some simple facts about erase |
|
1570 |
|
1571 \begin{lemma}\mbox{}\\ |
|
1572 @{thm erase_bder}\\ |
|
1573 @{thm erase_intern} |
|
1574 \end{lemma} |
|
1575 |
|
1576 \begin{center} |
|
1577 \begin{tabular}{lcl} |
|
1578 @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\ |
|
1579 @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\ |
|
1580 @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\ |
|
1581 @{thm (lhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1582 @{thm (lhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1583 @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\ |
|
1584 |
|
1585 % \end{tabular} |
|
1586 % \end{center} |
|
1587 |
|
1588 % \begin{center} |
|
1589 % \begin{tabular}{lcl} |
|
1590 |
|
1591 @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\ |
|
1592 @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\ |
|
1593 @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\ |
|
1594 @{thm (lhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1595 @{thm (lhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1596 @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)} |
|
1597 \end{tabular} |
|
1598 \end{center} |
|
1599 |
|
1600 |
|
1601 \begin{center} |
|
1602 \begin{tabular}{lcl} |
|
1603 @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\ |
|
1604 @{thm (lhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1605 @{thm (lhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1606 @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\ |
|
1607 \end{tabular} |
|
1608 \end{center} |
|
1609 |
|
1610 |
|
1611 @{thm [mode=IfThen] bder_retrieve} |
|
1612 |
|
1613 By induction on \<open>r\<close> |
|
1614 |
|
1615 \begin{theorem}[Main Lemma]\mbox{}\\ |
|
1616 @{thm [mode=IfThen] MAIN_decode} |
|
1617 \end{theorem} |
|
1618 |
|
1619 \noindent |
|
1620 Definition of the bitcoded lexer |
|
1621 |
|
1622 @{thm blexer_def} |
|
1623 |
|
1624 |
|
1625 \begin{theorem} |
|
1626 @{thm blexer_correctness} |
|
1627 \end{theorem} |
|
1628 |
|
1629 \<close> |
|
1630 |
|
1631 section \<open>Optimisations\<close> |
|
1632 |
|
1633 text \<open> |
|
1634 |
|
1635 Derivatives as calculated by \Brz's method are usually more complex |
|
1636 regular expressions than the initial one; the result is that the |
|
1637 derivative-based matching and lexing algorithms are often abysmally slow. |
|
1638 However, various optimisations are possible, such as the simplifications |
|
1639 of @{term "ALT ZERO r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and |
|
1640 @{term "SEQ r ONE"} to @{term r}. These simplifications can speed up the |
|
1641 algorithms considerably, as noted in \cite{Sulzmann2014}. One of the |
|
1642 advantages of having a simple specification and correctness proof is that |
|
1643 the latter can be refined to prove the correctness of such simplification |
|
1644 steps. While the simplification of regular expressions according to |
|
1645 rules like |
|
1646 |
|
1647 \begin{equation}\label{Simpl} |
|
1648 \begin{array}{lcllcllcllcl} |
|
1649 @{term "ALT ZERO r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1650 @{term "ALT r ZERO"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1651 @{term "SEQ ONE r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\ |
|
1652 @{term "SEQ r ONE"} & \<open>\<Rightarrow>\<close> & @{term r} |
|
1653 \end{array} |
|
1654 \end{equation} |
|
1655 |
|
1656 \noindent is well understood, there is an obstacle with the POSIX value |
|
1657 calculation algorithm by Sulzmann and Lu: if we build a derivative regular |
|
1658 expression and then simplify it, we will calculate a POSIX value for this |
|
1659 simplified derivative regular expression, \emph{not} for the original (unsimplified) |
|
1660 derivative regular expression. Sulzmann and Lu \cite{Sulzmann2014} overcome this obstacle by |
|
1661 not just calculating a simplified regular expression, but also calculating |
|
1662 a \emph{rectification function} that ``repairs'' the incorrect value. |
|
1663 |
|
1664 The rectification functions can be (slightly clumsily) implemented in |
|
1665 Isabelle/HOL as follows using some auxiliary functions: |
|
1666 |
|
1667 \begin{center} |
|
1668 \begin{tabular}{lcl} |
|
1669 @{thm (lhs) F_RIGHT.simps(1)} & $\dn$ & \<open>Right (f v)\<close>\\ |
|
1670 @{thm (lhs) F_LEFT.simps(1)} & $\dn$ & \<open>Left (f v)\<close>\\ |
|
1671 |
|
1672 @{thm (lhs) F_ALT.simps(1)} & $\dn$ & \<open>Right (f\<^sub>2 v)\<close>\\ |
|
1673 @{thm (lhs) F_ALT.simps(2)} & $\dn$ & \<open>Left (f\<^sub>1 v)\<close>\\ |
|
1674 |
|
1675 @{thm (lhs) F_SEQ1.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 ()) (f\<^sub>2 v)\<close>\\ |
|
1676 @{thm (lhs) F_SEQ2.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v) (f\<^sub>2 ())\<close>\\ |
|
1677 @{thm (lhs) F_SEQ.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)\<close>\medskip\\ |
|
1678 %\end{tabular} |
|
1679 % |
|
1680 %\begin{tabular}{lcl} |
|
1681 @{term "simp_ALT (ZERO, DUMMY) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_RIGHT f\<^sub>2)"}\\ |
|
1682 @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, DUMMY)"} & $\dn$ & @{term "(r\<^sub>1, F_LEFT f\<^sub>1)"}\\ |
|
1683 @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(ALT r\<^sub>1 r\<^sub>2, F_ALT f\<^sub>1 f\<^sub>2)"}\\ |
|
1684 @{term "simp_SEQ (ONE, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_SEQ1 f\<^sub>1 f\<^sub>2)"}\\ |
|
1685 @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (ONE, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>1, F_SEQ2 f\<^sub>1 f\<^sub>2)"}\\ |
|
1686 @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(SEQ r\<^sub>1 r\<^sub>2, F_SEQ f\<^sub>1 f\<^sub>2)"}\\ |
|
1687 \end{tabular} |
|
1688 \end{center} |
|
1689 |
|
1690 \noindent |
|
1691 The functions \<open>simp\<^bsub>Alt\<^esub>\<close> and \<open>simp\<^bsub>Seq\<^esub>\<close> encode the simplification rules |
|
1692 in \eqref{Simpl} and compose the rectification functions (simplifications can occur |
|
1693 deep inside the regular expression). The main simplification function is then |
|
1694 |
|
1695 \begin{center} |
|
1696 \begin{tabular}{lcl} |
|
1697 @{term "simp (ALT r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_ALT (simp r\<^sub>1) (simp r\<^sub>2)"}\\ |
|
1698 @{term "simp (SEQ r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_SEQ (simp r\<^sub>1) (simp r\<^sub>2)"}\\ |
|
1699 @{term "simp r"} & $\dn$ & @{term "(r, id)"}\\ |
|
1700 \end{tabular} |
|
1701 \end{center} |
|
1702 |
|
1703 \noindent where @{term "id"} stands for the identity function. The |
|
1704 function @{const simp} returns a simplified regular expression and a corresponding |
|
1705 rectification function. Note that we do not simplify under stars: this |
|
1706 seems to slow down the algorithm, rather than speed it up. The optimised |
|
1707 lexer is then given by the clauses: |
|
1708 |
|
1709 \begin{center} |
|
1710 \begin{tabular}{lcl} |
|
1711 @{thm (lhs) slexer.simps(1)} & $\dn$ & @{thm (rhs) slexer.simps(1)}\\ |
|
1712 @{thm (lhs) slexer.simps(2)} & $\dn$ & |
|
1713 \<open>let (r\<^sub>s, f\<^sub>r) = simp (r \<close>$\backslash$\<open> c) in\<close>\\ |
|
1714 & & \<open>case\<close> @{term "slexer r\<^sub>s s"} \<open>of\<close>\\ |
|
1715 & & \phantom{$|$} @{term "None"} \<open>\<Rightarrow>\<close> @{term None}\\ |
|
1716 & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> \<open>Some (inj r c (f\<^sub>r v))\<close> |
|
1717 \end{tabular} |
|
1718 \end{center} |
|
1719 |
|
1720 \noindent |
|
1721 In the second clause we first calculate the derivative @{term "der c r"} |
|
1722 and then simplify the result. This gives us a simplified derivative |
|
1723 \<open>r\<^sub>s\<close> and a rectification function \<open>f\<^sub>r\<close>. The lexer |
|
1724 is then recursively called with the simplified derivative, but before |
|
1725 we inject the character @{term c} into the value @{term v}, we need to rectify |
|
1726 @{term v} (that is construct @{term "f\<^sub>r v"}). Before we can establish the correctness |
|
1727 of @{term "slexer"}, we need to show that simplification preserves the language |
|
1728 and simplification preserves our POSIX relation once the value is rectified |
|
1729 (recall @{const "simp"} generates a (regular expression, rectification function) pair): |
|
1730 |
|
1731 \begin{lemma}\mbox{}\smallskip\\\label{slexeraux} |
|
1732 \begin{tabular}{ll} |
|
1733 (1) & @{thm L_fst_simp[symmetric]}\\ |
|
1734 (2) & @{thm[mode=IfThen] Posix_simp} |
|
1735 \end{tabular} |
|
1736 \end{lemma} |
|
1737 |
|
1738 \begin{proof} Both are by induction on \<open>r\<close>. There is no |
|
1739 interesting case for the first statement. For the second statement, |
|
1740 of interest are the @{term "r = ALT r\<^sub>1 r\<^sub>2"} and @{term "r = SEQ r\<^sub>1 |
|
1741 r\<^sub>2"} cases. In each case we have to analyse four subcases whether |
|
1742 @{term "fst (simp r\<^sub>1)"} and @{term "fst (simp r\<^sub>2)"} equals @{const |
|
1743 ZERO} (respectively @{const ONE}). For example for @{term "r = ALT |
|
1744 r\<^sub>1 r\<^sub>2"}, consider the subcase @{term "fst (simp r\<^sub>1) = ZERO"} and |
|
1745 @{term "fst (simp r\<^sub>2) \<noteq> ZERO"}. By assumption we know @{term "s \<in> |
|
1746 fst (simp (ALT r\<^sub>1 r\<^sub>2)) \<rightarrow> v"}. From this we can infer @{term "s \<in> fst (simp r\<^sub>2) \<rightarrow> v"} |
|
1747 and by IH also (*) @{term "s \<in> r\<^sub>2 \<rightarrow> (snd (simp r\<^sub>2) v)"}. Given @{term "fst (simp r\<^sub>1) = ZERO"} |
|
1748 we know @{term "L (fst (simp r\<^sub>1)) = {}"}. By the first statement |
|
1749 @{term "L r\<^sub>1"} is the empty set, meaning (**) @{term "s \<notin> L r\<^sub>1"}. |
|
1750 Taking (*) and (**) together gives by the \mbox{\<open>P+R\<close>}-rule |
|
1751 @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> Right (snd (simp r\<^sub>2) v)"}. In turn this |
|
1752 gives @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> snd (simp (ALT r\<^sub>1 r\<^sub>2)) v"} as we need to show. |
|
1753 The other cases are similar.\qed |
|
1754 \end{proof} |
|
1755 |
|
1756 \noindent We can now prove relatively straightforwardly that the |
|
1757 optimised lexer produces the expected result: |
|
1758 |
|
1759 \begin{theorem} |
|
1760 @{thm slexer_correctness} |
|
1761 \end{theorem} |
|
1762 |
|
1763 \begin{proof} By induction on @{term s} generalising over @{term |
|
1764 r}. The case @{term "[]"} is trivial. For the cons-case suppose the |
|
1765 string is of the form @{term "c # s"}. By induction hypothesis we |
|
1766 know @{term "slexer r s = lexer r s"} holds for all @{term r} (in |
|
1767 particular for @{term "r"} being the derivative @{term "der c |
|
1768 r"}). Let @{term "r\<^sub>s"} be the simplified derivative regular expression, that is @{term |
|
1769 "fst (simp (der c r))"}, and @{term "f\<^sub>r"} be the rectification |
|
1770 function, that is @{term "snd (simp (der c r))"}. We distinguish the cases |
|
1771 whether (*) @{term "s \<in> L (der c r)"} or not. In the first case we |
|
1772 have by Theorem~\ref{lexercorrect}(2) a value @{term "v"} so that @{term |
|
1773 "lexer (der c r) s = Some v"} and @{term "s \<in> der c r \<rightarrow> v"} hold. |
|
1774 By Lemma~\ref{slexeraux}(1) we can also infer from~(*) that @{term "s |
|
1775 \<in> L r\<^sub>s"} holds. Hence we know by Theorem~\ref{lexercorrect}(2) that |
|
1776 there exists a @{term "v'"} with @{term "lexer r\<^sub>s s = Some v'"} and |
|
1777 @{term "s \<in> r\<^sub>s \<rightarrow> v'"}. From the latter we know by |
|
1778 Lemma~\ref{slexeraux}(2) that @{term "s \<in> der c r \<rightarrow> (f\<^sub>r v')"} holds. |
|
1779 By the uniqueness of the POSIX relation (Theorem~\ref{posixdeterm}) we |
|
1780 can infer that @{term v} is equal to @{term "f\<^sub>r v'"}---that is the |
|
1781 rectification function applied to @{term "v'"} |
|
1782 produces the original @{term "v"}. Now the case follows by the |
|
1783 definitions of @{const lexer} and @{const slexer}. |
|
1784 |
|
1785 In the second case where @{term "s \<notin> L (der c r)"} we have that |
|
1786 @{term "lexer (der c r) s = None"} by Theorem~\ref{lexercorrect}(1). We |
|
1787 also know by Lemma~\ref{slexeraux}(1) that @{term "s \<notin> L r\<^sub>s"}. Hence |
|
1788 @{term "lexer r\<^sub>s s = None"} by Theorem~\ref{lexercorrect}(1) and |
|
1789 by IH then also @{term "slexer r\<^sub>s s = None"}. With this we can |
|
1790 conclude in this case too.\qed |
|
1791 |
|
1792 \end{proof} |
|
1793 |
|
1794 \<close> |
|
1795 fy the result. This gives us a simplified derivative |
|
1796 \<open>r\<^sub>s\<close> and a rectification function \<open>f\<^sub>r\<close>. The lexer |
|
1797 is then recursively called with the simplified derivative, but before |
|
1798 we inject the character @{term c} into the value @{term v}, we need to rectify |
|
1799 @{term v} (that is construct @{term "f\<^sub>r v"}). Before we can establish the correctness |
|
1800 of @{term "slexer"}, we need to show that simplification preserves the language |
|
1801 and simplification preserves our POSIX relation once the value is rectified |
|
1802 (recall @{const "simp"} generates a (regular expression, rectification function) pair): |
|
1803 |
|
1804 \begin{lemma}\mbox{}\smallskip\\\label{slexeraux} |
|
1805 \begin{tabular}{ll} |
|
1806 (1) & @{thm L_fst_simp[symmetric]}\\ |
|
1807 (2) & @{thm[mode=IfThen] Posix_simp} |
|
1808 \end{tabular} |
|
1809 \end{lemma} |
|
1810 |
|
1811 \begin{proof} Both are by induction on \<open>r\<close>. There is no |
|
1812 interesting case for the first statement. For the second statement, |
|
1813 of interest are the @{term "r = ALT r\<^sub>1 r\<^sub>2"} and @{term "r = SEQ r\<^sub>1 |
|
1814 r\<^sub>2"} cases. In each case we have to analyse four subcases whether |
|
1815 @{term "fst (simp r\<^sub>1)"} and @{term "fst (simp r\<^sub>2)"} equals @{const |
|
1816 ZERO} (respectively @{const ONE}). For example for @{term "r = ALT |
|
1817 r\<^sub>1 r\<^sub>2"}, consider the subcase @{term "fst (simp r\<^sub>1) = ZERO"} and |
|
1818 @{term "fst (simp r\<^sub>2) \<noteq> ZERO"}. By assumption we know @{term "s \<in> |
|
1819 fst (simp (ALT r\<^sub>1 r\<^sub>2)) \<rightarrow> v"}. From this we can infer @{term "s \<in> fst (simp r\<^sub>2) \<rightarrow> v"} |
|
1820 and by IH also (*) @{term "s \<in> r\<^sub>2 \<rightarrow> (snd (simp r\<^sub>2) v)"}. Given @{term "fst (simp r\<^sub>1) = ZERO"} |
|
1821 we know @{term "L (fst (simp r\<^sub>1)) = {}"}. By the first statement |
|
1822 @{term "L r\<^sub>1"} is the empty set, meaning (**) @{term "s \<notin> L r\<^sub>1"}. |
|
1823 Taking (*) and (**) together gives by the \mbox{\<open>P+R\<close>}-rule |
|
1824 @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> Right (snd (simp r\<^sub>2) v)"}. In turn this |
|
1825 gives @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> snd (simp (ALT r\<^sub>1 r\<^sub>2)) v"} as we need to show. |
|
1826 The other cases are similar.\qed |
|
1827 \end{proof} |
|
1828 |
|
1829 \noindent We can now prove relatively straightforwardly that the |
|
1830 optimised lexer produces the expected result: |
|
1831 |
|
1832 \begin{theorem} |
|
1833 @{thm slexer_correctness} |
|
1834 \end{theorem} |
|
1835 |
|
1836 \begin{proof} By induction on @{term s} generalising over @{term |
|
1837 r}. The case @{term "[]"} is trivial. For the cons-case suppose the |
|
1838 string is of the form @{term "c # s"}. By induction hypothesis we |
|
1839 know @{term "slexer r s = lexer r s"} holds for all @{term r} (in |
|
1840 particular for @{term "r"} being the derivative @{term "der c |
|
1841 r"}). Let @{term "r\<^sub>s"} be the simplified derivative regular expression, that is @{term |
|
1842 "fst (simp (der c r))"}, and @{term "f\<^sub>r"} be the rectification |
|
1843 function, that is @{term "snd (simp (der c r))"}. We distinguish the cases |
|
1844 whether (*) @{term "s \<in> L (der c r)"} or not. In the first case we |
|
1845 have by Theorem~\ref{lexercorrect}(2) a value @{term "v"} so that @{term |
|
1846 "lexer (der c r) s = Some v"} and @{term "s \<in> der c r \<rightarrow> v"} hold. |
|
1847 By Lemma~\ref{slexeraux}(1) we can also infer from~(*) that @{term "s |
|
1848 \<in> L r\<^sub>s"} holds. Hence we know by Theorem~\ref{lexercorrect}(2) that |
|
1849 there exists a @{term "v'"} with @{term "lexer r\<^sub>s s = Some v'"} and |
|
1850 @{term "s \<in> r\<^sub>s \<rightarrow> v'"}. From the latter we know by |
|
1851 Lemma~\ref{slexeraux}(2) that @{term "s \<in> der c r \<rightarrow> (f\<^sub>r v')"} holds. |
|
1852 By the uniqueness of the POSIX relation (Theorem~\ref{posixdeterm}) we |
|
1853 can infer that @{term v} is equal to @{term "f\<^sub>r v'"}---that is the |
|
1854 rectification function applied to @{term "v'"} |
|
1855 produces the original @{term "v"}. Now the case follows by the |
|
1856 definitions of @{const lexer} and @{const slexer}. |
|
1857 |
|
1858 In the second case where @{term "s \<notin> L (der c r)"} we have that |
|
1859 @{term "lexer (der c r) s = None"} by Theorem~\ref{lexercorrect}(1). We |
|
1860 also know by Lemma~\ref{slexeraux}(1) that @{term "s \<notin> L r\<^sub>s"}. Hence |
|
1861 @{term "lexer r\<^sub>s s = None"} by Theorem~\ref{lexercorrect}(1) and |
|
1862 by IH then also @{term "slexer r\<^sub>s s = None"}. With this we can |
|
1863 conclude in this case too.\qed |
|
1864 |
|
1865 \end{proof} |
|
1866 |
|
1867 \<close> |
|
1868 |
|
1869 |
|
1870 section \<open>HERE\<close> |
|
1871 |
|
1872 text \<open> |
|
1873 |
|
1874 \begin{lemma} |
|
1875 @{thm [mode=IfThen] bder_retrieve} |
|
1876 \end{lemma} |
|
1877 |
|
1878 \begin{proof} |
|
1879 By induction on the definition of @{term "erase r"}. The cases for rule 1) and 2) are |
|
1880 straightforward as @{term "der c ZERO"} and @{term "der c ONE"} are both equal to |
|
1881 @{term ZERO}. This means @{term "\<Turnstile> v : ZERO"} cannot hold. Similarly in case of rule 3) |
|
1882 where @{term r} is of the form @{term "ACHAR d"} with @{term "c = d"}. Then by assumption |
|
1883 we know @{term "\<Turnstile> v : ONE"}, which implies @{term "v = Void"}. The equation follows by |
|
1884 simplification of left- and right-hand side. In case @{term "c \<noteq> d"} we have again |
|
1885 @{term "\<Turnstile> v : ZERO"}, which cannot hold. |
|
1886 |
|
1887 For rule 4a) we have again @{term "\<Turnstile> v : ZERO"}. The property holds by IH for rule 4b). |
|
1888 The induction hypothesis is |
|
1889 \[ |
|
1890 @{term "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"} |
|
1891 \] |
|
1892 which is what left- and right-hand side simplify to. The slightly more interesting case |
|
1893 is for 4c). By assumption we have |
|
1894 @{term "\<Turnstile> v : ALT (der c (erase r\<^sub>1)) (der c (erase (AALTs bs (r\<^sub>2 # rs))))"}. This means we |
|
1895 have either (*) @{term "\<Turnstile> v1 : der c (erase r\<^sub>1)"} with @{term "v = Left v1"} or |
|
1896 (**) @{term "\<Turnstile> v2 : der c (erase (AALTs bs (r\<^sub>2 # rs)))"} with @{term "v = Right v2"}. |
|
1897 The former case is straightforward by simplification. The second case is \ldots TBD. |
|
1898 |
|
1899 Rule 5) TBD. |
|
1900 |
|
1901 Finally for rule 6) the reasoning is as follows: By assumption we have |
|
1902 @{term "\<Turnstile> v : SEQ (der c (erase r)) (STAR (erase r))"}. This means we also have |
|
1903 @{term "v = Seq v1 v2"}, @{term "\<Turnstile> v1 : der c (erase r)"} and @{term "v2 = Stars vs"}. |
|
1904 We want to prove |
|
1905 \begin{align} |
|
1906 & @{term "retrieve (ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)) v"}\\ |
|
1907 &= @{term "retrieve (ASTAR bs r) (injval (STAR (erase r)) c v)"} |
|
1908 \end{align} |
|
1909 The right-hand side @{term inj}-expression is equal to |
|
1910 @{term "Stars (injval (erase r) c v1 # vs)"}, which means the @{term retrieve}-expression |
|
1911 simplifies to |
|
1912 \[ |
|
1913 @{term "bs @ [Z] @ retrieve r (injval (erase r) c v1) @ retrieve (ASTAR [] r) (Stars vs)"} |
|
1914 \] |
|
1915 The left-hand side (3) above simplifies to |
|
1916 \[ |
|
1917 @{term "bs @ retrieve (fuse [Z] (bder c r)) v1 @ retrieve (ASTAR [] r) (Stars vs)"} |
|
1918 \] |
|
1919 We can move out the @{term "fuse [Z]"} and then use the IH to show that left-hand side |
|
1920 and right-hand side are equal. This completes the proof. |
|
1921 \end{proof} |
|
1922 |
|
1923 |
|
1924 |
|
1925 \bibliographystyle{plain} |
|
1926 \bibliography{root} |
|
1927 |
|
1928 \<close> |
|
1929 (*<*) |
|
1930 end |
|
1931 (*>*) |
|
1932 |
|
1933 (* |
|
1934 |
|
1935 \begin{center} |
|
1936 \begin{tabular}{lcl} |
|
1937 @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\ |
|
1938 @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\ |
|
1939 @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\ |
|
1940 @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\ |
|
1941 @{thm (lhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]} & $\dn$ & @{thm (rhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]}\\ |
|
1942 @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\ |
|
1943 @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)} |
|
1944 \end{tabular} |
|
1945 \end{center} |
|
1946 |
|
1947 \begin{center} |
|
1948 \begin{tabular}{lcl} |
|
1949 @{term areg} & $::=$ & @{term "AZERO"}\\ |
|
1950 & $\mid$ & @{term "AONE bs"}\\ |
|
1951 & $\mid$ & @{term "ACHAR bs c"}\\ |
|
1952 & $\mid$ & @{term "AALT bs r1 r2"}\\ |
|
1953 & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\ |
|
1954 & $\mid$ & @{term "ASTAR bs r"} |
|
1955 \end{tabular} |
|
1956 \end{center} |
|
1957 |
|
1958 |
|
1959 \begin{center} |
|
1960 \begin{tabular}{lcl} |
|
1961 @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\ |
|
1962 @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\ |
|
1963 @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\ |
|
1964 @{thm (lhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1965 @{thm (lhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1966 @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\ |
|
1967 \end{tabular} |
|
1968 \end{center} |
|
1969 |
|
1970 \begin{center} |
|
1971 \begin{tabular}{lcl} |
|
1972 @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\ |
|
1973 @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\ |
|
1974 @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\ |
|
1975 @{thm (lhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1976 @{thm (lhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1977 @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\ |
|
1978 \end{tabular} |
|
1979 \end{center} |
|
1980 |
|
1981 Some simple facts about erase |
|
1982 |
|
1983 \begin{lemma}\mbox{}\\ |
|
1984 @{thm erase_bder}\\ |
|
1985 @{thm erase_intern} |
|
1986 \end{lemma} |
|
1987 |
|
1988 \begin{center} |
|
1989 \begin{tabular}{lcl} |
|
1990 @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\ |
|
1991 @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\ |
|
1992 @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\ |
|
1993 @{thm (lhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1994 @{thm (lhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
1995 @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\ |
|
1996 |
|
1997 % \end{tabular} |
|
1998 % \end{center} |
|
1999 |
|
2000 % \begin{center} |
|
2001 % \begin{tabular}{lcl} |
|
2002 |
|
2003 @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\ |
|
2004 @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\ |
|
2005 @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\ |
|
2006 @{thm (lhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2007 @{thm (lhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2008 @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)} |
|
2009 \end{tabular} |
|
2010 \end{center} |
|
2011 |
|
2012 |
|
2013 \begin{center} |
|
2014 \begin{tabular}{lcl} |
|
2015 @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\ |
|
2016 @{thm (lhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2017 @{thm (lhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2018 @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\ |
|
2019 \end{tabular} |
|
2020 \end{center} |
|
2021 |
|
2022 |
|
2023 @{thm [mode=IfThen] bder_retrieve} |
|
2024 |
|
2025 By induction on \<open>r\<close> |
|
2026 |
|
2027 \begin{theorem}[Main Lemma]\mbox{}\\ |
|
2028 @{thm [mode=IfThen] MAIN_decode} |
|
2029 \end{theorem} |
|
2030 |
|
2031 \noindent |
|
2032 Definition of the bitcoded lexer |
|
2033 |
|
2034 @{thm blexer_def} |
|
2035 |
|
2036 |
|
2037 \begin{theorem} |
|
2038 @{thm blexer_correctness} |
|
2039 \end{theorem} |
|
2040 |
|
2041 |
|
2042 |
|
2043 |
|
2044 |
|
2045 \begin{center} |
|
2046 \begin{tabular}{lcl} |
|
2047 @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\ |
|
2048 @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\ |
|
2049 @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\ |
|
2050 @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\ |
|
2051 @{thm (lhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]} & $\dn$ & @{thm (rhs) code.simps(5)[of "v\<^sub>1" "v\<^sub>2"]}\\ |
|
2052 @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\ |
|
2053 @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)} |
|
2054 \end{tabular} |
|
2055 \end{center} |
|
2056 |
|
2057 \begin{center} |
|
2058 \begin{tabular}{lcl} |
|
2059 @{term areg} & $::=$ & @{term "AZERO"}\\ |
|
2060 & $\mid$ & @{term "AONE bs"}\\ |
|
2061 & $\mid$ & @{term "ACHAR bs c"}\\ |
|
2062 & $\mid$ & @{term "AALT bs r1 r2"}\\ |
|
2063 & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\ |
|
2064 & $\mid$ & @{term "ASTAR bs r"} |
|
2065 \end{tabular} |
|
2066 \end{center} |
|
2067 |
|
2068 |
|
2069 \begin{center} |
|
2070 \begin{tabular}{lcl} |
|
2071 @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\ |
|
2072 @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\ |
|
2073 @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\ |
|
2074 @{thm (lhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2075 @{thm (lhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) intern.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2076 @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\ |
|
2077 \end{tabular} |
|
2078 \end{center} |
|
2079 |
|
2080 \begin{center} |
|
2081 \begin{tabular}{lcl} |
|
2082 @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\ |
|
2083 @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\ |
|
2084 @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\ |
|
2085 @{thm (lhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2086 @{thm (lhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) erase.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2087 @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\ |
|
2088 \end{tabular} |
|
2089 \end{center} |
|
2090 |
|
2091 Some simple facts about erase |
|
2092 |
|
2093 \begin{lemma}\mbox{}\\ |
|
2094 @{thm erase_bder}\\ |
|
2095 @{thm erase_intern} |
|
2096 \end{lemma} |
|
2097 |
|
2098 \begin{center} |
|
2099 \begin{tabular}{lcl} |
|
2100 @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\ |
|
2101 @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\ |
|
2102 @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\ |
|
2103 @{thm (lhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2104 @{thm (lhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bnullable.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2105 @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\ |
|
2106 |
|
2107 % \end{tabular} |
|
2108 % \end{center} |
|
2109 |
|
2110 % \begin{center} |
|
2111 % \begin{tabular}{lcl} |
|
2112 |
|
2113 @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\ |
|
2114 @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\ |
|
2115 @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\ |
|
2116 @{thm (lhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(4)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2117 @{thm (lhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bder.simps(5)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2118 @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)} |
|
2119 \end{tabular} |
|
2120 \end{center} |
|
2121 |
|
2122 |
|
2123 \begin{center} |
|
2124 \begin{tabular}{lcl} |
|
2125 @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\ |
|
2126 @{thm (lhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(2)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2127 @{thm (lhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bmkeps.simps(3)[of bs "r\<^sub>1" "r\<^sub>2"]}\\ |
|
2128 @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\ |
|
2129 \end{tabular} |
|
2130 \end{center} |
|
2131 |
|
2132 |
|
2133 @{thm [mode=IfThen] bder_retrieve} |
|
2134 |
|
2135 By induction on \<open>r\<close> |
|
2136 |
|
2137 \begin{theorem}[Main Lemma]\mbox{}\\ |
|
2138 @{thm [mode=IfThen] MAIN_decode} |
|
2139 \end{theorem} |
|
2140 |
|
2141 \noindent |
|
2142 Definition of the bitcoded lexer |
|
2143 |
|
2144 @{thm blexer_def} |
|
2145 |
|
2146 |
|
2147 \begin{theorem} |
|
2148 @{thm blexer_correctness} |
|
2149 \end{theorem} |
|
2150 |
|
2151 \<close> |
|
2152 \<close>*) |