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