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