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