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