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