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