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