532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
\chapter{A Better Bound and Other Extensions} % Main chapter title
|
|
4 |
|
|
5 |
\label{Cubic} %In Chapter 5\ref{Chapter5} we discuss stronger simplifications to improve the finite bound
|
|
6 |
%in Chapter 4 to a polynomial one, and demonstrate how one can extend the
|
|
7 |
%algorithm to include constructs such as bounded repetitions and negations.
|
590
|
8 |
\lstset{style=myScalastyle}
|
|
9 |
|
|
10 |
|
625
|
11 |
This chapter is a ``work-in-progress''
|
|
12 |
chapter which records
|
|
13 |
extensions to our $\blexersimp$.
|
|
14 |
We intend to formalise this part, which
|
|
15 |
we have not been able to finish due to time constraints of the PhD.
|
|
16 |
Nevertheless, we outline the ideas we intend to use for the proof.
|
|
17 |
|
|
18 |
We present further improvements
|
590
|
19 |
made to our lexer algorithm $\blexersimp$.
|
|
20 |
We devise a stronger simplification algorithm,
|
|
21 |
called $\bsimpStrong$, which can prune away
|
|
22 |
similar components in two regular expressions at the same
|
|
23 |
alternative level,
|
|
24 |
even if these regular expressions are not exactly the same.
|
|
25 |
We call the lexer that uses this stronger simplification function
|
|
26 |
$\blexerStrong$.
|
|
27 |
We conjecture that both
|
|
28 |
\begin{center}
|
|
29 |
$\blexerStrong \;r \; s = \blexer\; r\;s$
|
|
30 |
\end{center}
|
|
31 |
and
|
|
32 |
\begin{center}
|
|
33 |
$\llbracket \bdersStrong{a}{s} \rrbracket = O(\llbracket a \rrbracket^3)$
|
|
34 |
\end{center}
|
|
35 |
hold, but formalising
|
|
36 |
them is still work in progress.
|
|
37 |
We give reasons why the correctness and cubic size bound proofs
|
|
38 |
can be achieved,
|
|
39 |
by exploring the connection between the internal
|
|
40 |
data structure of our $\blexerStrong$ and
|
|
41 |
Animirov's partial derivatives.\\
|
621
|
42 |
%We also present the idempotency property proof
|
620
|
43 |
%of $\bsimp$, which leverages the idempotency proof of $\rsimp$.
|
|
44 |
%This reinforces our claim that the fixpoint construction
|
|
45 |
%originally required by Sulzmann and Lu can be removed in $\blexersimp$.
|
621
|
46 |
|
|
47 |
%Last but not least, we present our efforts and challenges we met
|
|
48 |
%in further improving the algorithm by data
|
|
49 |
%structures such as zippers.
|
590
|
50 |
|
|
51 |
|
|
52 |
|
532
|
53 |
%----------------------------------------------------------------------------------------
|
|
54 |
% SECTION strongsimp
|
|
55 |
%----------------------------------------------------------------------------------------
|
|
56 |
\section{A Stronger Version of Simplification}
|
|
57 |
%TODO: search for isabelle proofs of algorithms that check equivalence
|
590
|
58 |
In our bitcoded lexing algorithm, (sub)terms represent (sub)matches.
|
591
|
59 |
For example, the regular expression
|
|
60 |
\[
|
|
61 |
aa \cdot a^*+ a \cdot a^* + aa\cdot a^*
|
|
62 |
\]
|
|
63 |
contains three terms,
|
|
64 |
expressing three possibilities it will match future input.
|
|
65 |
The first and the third terms are identical, which means we can eliminate
|
|
66 |
the latter as we know it will not be picked up by $\bmkeps$.
|
|
67 |
In $\bsimps$, the $\distinctBy$ function takes care of this.
|
|
68 |
The criteria $\distinctBy$ uses for removing a duplicate
|
|
69 |
$a_2$ in the list
|
|
70 |
\begin{center}
|
|
71 |
$rs_a@[a_1]@rs_b@[a_2]@rs_c$
|
|
72 |
\end{center}
|
|
73 |
is that
|
533
|
74 |
\begin{center}
|
591
|
75 |
$\rerase{a_1} = \rerase{a_2}$.
|
|
76 |
\end{center}
|
|
77 |
It can be characterised as the $LD$
|
|
78 |
rewrite rule in \ref{rrewriteRules}.\\
|
|
79 |
The problem , however, is that identical components
|
|
80 |
in two slightly different regular expressions cannot be removed:
|
|
81 |
\begin{figure}[H]
|
|
82 |
\[
|
|
83 |
(a+b+d) \cdot r_1 + (a+c+e) \cdot r_1 \stackrel{?}{\rightsquigarrow} (a+b+d) \cdot r_1 + (c+e) \cdot r_1
|
|
84 |
\]
|
|
85 |
\caption{Desired simplification, but not done in $\blexersimp$}\label{partialDedup}
|
|
86 |
\end{figure}
|
|
87 |
\noindent
|
|
88 |
A simplification like this actually
|
|
89 |
cannot be omitted,
|
621
|
90 |
as without it the size could blow up even with our $\textit{bsimp}$
|
|
91 |
function: for the chapter \ref{Finite} example
|
|
92 |
$\protect((a^* + (aa)^* + \ldots + (\underbrace{a\ldots a}_{n a's})^* )^*)^*$,
|
|
93 |
by just setting n to a small number,
|
|
94 |
we get exponential growth that does not stop before it becomes huge:
|
591
|
95 |
\begin{figure}[H]
|
|
96 |
\centering
|
|
97 |
\begin{tikzpicture}
|
|
98 |
\begin{axis}[
|
|
99 |
%xlabel={$n$},
|
|
100 |
myplotstyle,
|
|
101 |
xlabel={input length},
|
|
102 |
ylabel={size},
|
|
103 |
]
|
|
104 |
\addplot[blue,mark=*, mark options={fill=white}] table {bsimpExponential.data};
|
|
105 |
\end{axis}
|
533
|
106 |
\end{tikzpicture}
|
591
|
107 |
\caption{Runtime of $\blexersimp$ for matching
|
|
108 |
$\protect((a^* + (aa)^* + \ldots + (aaaaa)^* )^*)^*$
|
|
109 |
with strings
|
|
110 |
of the form $\protect\underbrace{aa..a}_{n}$.}\label{blexerExp}
|
|
111 |
\end{figure}
|
|
112 |
\noindent
|
|
113 |
We would like to apply the rewriting at some stage
|
|
114 |
\begin{figure}[H]
|
|
115 |
\[
|
|
116 |
(a+b+d) \cdot r_1 \longrightarrow a \cdot r_1 + b \cdot r_1 + d \cdot r_1
|
|
117 |
\]
|
|
118 |
\caption{Desired simplification, but not done in $\blexersimp$}\label{desiredSimp}
|
533
|
119 |
\end{figure}
|
|
120 |
\noindent
|
591
|
121 |
in our $\simp$ function,
|
|
122 |
so that it makes the simplification in \ref{partialDedup} possible.
|
621
|
123 |
Translating the rule into our $\textit{bsimp}$ function simply
|
|
124 |
involves adding a new clause to the $\textit{bsimp}_{ASEQ}$ function:
|
|
125 |
\begin{center}
|
|
126 |
\begin{tabular}{@{}lcl@{}}
|
|
127 |
$\textit{bsimp}_{ASEQ} \; bs\; a \; b$ & $\dn$ & $ (a,\; b) \textit{match}$\\
|
|
128 |
&& $\ldots$\\
|
|
129 |
&&$\quad\textit{case} \; (_{bs1}\sum as, a_2') \Rightarrow _{bs1}\sum (
|
|
130 |
\map \; (_{[]}\textit{ASEQ} \; \_ \; a_2') \; as)$\\
|
|
131 |
&&$\quad\textit{case} \; (a_1', a_2') \Rightarrow _{bs}a_1' \cdot a_2'$ \\
|
|
132 |
\end{tabular}
|
|
133 |
\end{center}
|
|
134 |
|
|
135 |
|
533
|
136 |
|
591
|
137 |
Unfortunately,
|
|
138 |
if we introduce them in our
|
|
139 |
setting we would lose the POSIX property of our calculated values.
|
|
140 |
For example given the regular expression
|
|
141 |
\begin{center}
|
|
142 |
$(a + ab)(bc + c)$
|
|
143 |
\end{center}
|
|
144 |
and the string
|
|
145 |
\begin{center}
|
|
146 |
$ab$,
|
533
|
147 |
\end{center}
|
591
|
148 |
then our algorithm generates the following
|
|
149 |
correct POSIX value
|
|
150 |
\begin{center}
|
|
151 |
$\Seq \; (\Right \; ab) \; (\Right \; c)$.
|
533
|
152 |
\end{center}
|
591
|
153 |
Essentially it matches the string with the longer Right-alternative
|
|
154 |
in the first sequence (and
|
|
155 |
then the 'rest' with the character regular expression $c$ from the second sequence).
|
|
156 |
If we add the simplification above, then we obtain the following value
|
|
157 |
\begin{center}
|
|
158 |
$\Left \; (\Seq \; a \; (\Left \; bc))$
|
|
159 |
\end{center}
|
|
160 |
where the $\Left$-alternatives get priority.
|
|
161 |
However this violates the POSIX rules.
|
|
162 |
The reason for getting this undesired value
|
|
163 |
is that the new rule splits this regular expression up into
|
|
164 |
\begin{center}
|
|
165 |
$a\cdot(b c + c) + ab \cdot (bc + c)$,
|
533
|
166 |
\end{center}
|
591
|
167 |
which becomes a regular expression with a
|
|
168 |
totally different structure--the original
|
|
169 |
was a sequence, and now it becomes an alternative.
|
|
170 |
With an alternative the maximum munch rule no longer works.\\
|
|
171 |
A method to reconcile this is to do the
|
|
172 |
transformation in \ref{desiredSimp} ``non-invasively'',
|
|
173 |
meaning that we traverse the list of regular expressions
|
|
174 |
\begin{center}
|
|
175 |
$rs_a@[a]@rs_c$
|
|
176 |
\end{center}
|
|
177 |
in the alternative
|
|
178 |
\begin{center}
|
|
179 |
$\sum ( rs_a@[a]@rs_c)$
|
533
|
180 |
\end{center}
|
591
|
181 |
using a function similar to $\distinctBy$,
|
|
182 |
but this time
|
|
183 |
we allow a more general list rewrite:
|
592
|
184 |
\begin{mathpar}\label{cubicRule}
|
621
|
185 |
\inferrule * [Right = cubicRule]{\vspace{0mm} }{rs_a@[a]@rs_c
|
591
|
186 |
\stackrel{s}{\rightsquigarrow }
|
|
187 |
rs_a@[\textit{prune} \; a \; rs_a]@rs_c }
|
592
|
188 |
\end{mathpar}
|
591
|
189 |
%L \; a_1' = L \; a_1 \setminus (\cup_{a \in rs_a} L \; a)
|
|
190 |
where $\textit{prune} \;a \; acc$ traverses $a$
|
|
191 |
without altering the structure of $a$, removing components in $a$
|
|
192 |
that have appeared in the accumulator $acc$.
|
|
193 |
For example
|
|
194 |
\begin{center}
|
|
195 |
$\textit{prune} \;\;\; (r_a+r_f+r_g+r_h)r_d \;\; \; [(r_a+r_b+r_c)r_d, (r_e+r_f)r_d] $
|
|
196 |
\end{center}
|
|
197 |
should be equal to
|
|
198 |
\begin{center}
|
|
199 |
$(r_g+r_h)r_d$
|
|
200 |
\end{center}
|
|
201 |
because $r_gr_d$ and
|
|
202 |
$r_hr_d$ are the only terms
|
|
203 |
that have not appeared in the accumulator list
|
|
204 |
\begin{center}
|
|
205 |
$[(r_a+r_b+r_c)r_d, (r_e+r_f)r_d]$.
|
|
206 |
\end{center}
|
|
207 |
We implemented
|
|
208 |
function $\textit{prune}$ in Scala,
|
|
209 |
and incorporated into our lexer,
|
|
210 |
by replacing the $\simp$ function
|
|
211 |
with a stronger version called $\bsimpStrong$
|
|
212 |
that prunes regular expressions.
|
590
|
213 |
\begin{figure}[H]
|
621
|
214 |
|
591
|
215 |
\begin{lstlisting}
|
|
216 |
def atMostEmpty(r: Rexp) : Boolean = r match {
|
|
217 |
case ZERO => true
|
|
218 |
case ONE => true
|
|
219 |
case STAR(r) => atMostEmpty(r)
|
|
220 |
case SEQ(r1, r2) => atMostEmpty(r1) && atMostEmpty(r2)
|
|
221 |
case ALTS(r1, r2) => atMostEmpty(r1) && atMostEmpty(r2)
|
|
222 |
case CHAR(_) => false
|
|
223 |
}
|
|
224 |
|
|
225 |
|
|
226 |
def isOne(r: Rexp) : Boolean = r match {
|
|
227 |
case ONE => true
|
|
228 |
case SEQ(r1, r2) => isOne(r1) && isOne(r2)
|
|
229 |
case ALTS(r1, r2) => (isOne(r1) || isOne(r2)) && (atMostEmpty(r1) && atMostEmpty(r2))//rs.forall(atMostEmpty) && rs.exists(isOne)
|
|
230 |
case STAR(r0) => atMostEmpty(r0)
|
|
231 |
case CHAR(c) => false
|
|
232 |
case ZERO => false
|
|
233 |
}
|
|
234 |
|
|
235 |
//r = r' ~ tail' : If tail' matches tail => returns r'
|
|
236 |
def removeSeqTail(r: Rexp, tail: Rexp) : Rexp = r match {
|
|
237 |
case SEQ(r1, r2) =>
|
|
238 |
if(r2 == tail)
|
|
239 |
r1
|
|
240 |
else
|
|
241 |
ZERO
|
|
242 |
case r => ZERO
|
|
243 |
}
|
|
244 |
|
|
245 |
def prune(r: ARexp, acc: Set[Rexp]) : ARexp = r match{
|
|
246 |
case AALTS(bs, rs) => rs.map(r => prune(r, acc)).filter(_ != ZERO) match
|
|
247 |
{
|
|
248 |
//all components have been removed, meaning this is effectively a duplicate
|
|
249 |
//flats will take care of removing this AZERO
|
|
250 |
case Nil => AZERO
|
|
251 |
case r::Nil => fuse(bs, r)
|
|
252 |
case rs1 => AALTS(bs, rs1)
|
|
253 |
}
|
|
254 |
case ASEQ(bs, r1, r2) =>
|
|
255 |
//remove the r2 in (ra + rb)r2 to identify the duplicate contents of r1
|
|
256 |
prune(r1, acc.map(r => removeSeqTail(r, erase(r2)))) match {
|
|
257 |
//after pruning, returns 0
|
|
258 |
case AZERO => AZERO
|
|
259 |
//after pruning, got r1'.r2, where r1' is equal to 1
|
|
260 |
case r1p if(isOne(erase(r1p))) => fuse(bs ++ mkepsBC(r1p), r2)
|
|
261 |
//assemble the pruned head r1p with r2
|
|
262 |
case r1p => ASEQ(bs, r1p, r2)
|
|
263 |
}
|
|
264 |
//this does the duplicate component removal task
|
|
265 |
case r => if(acc(erase(r))) AZERO else r
|
|
266 |
}
|
|
267 |
\end{lstlisting}
|
|
268 |
\caption{pruning function together with its helper functions}
|
|
269 |
\end{figure}
|
|
270 |
\noindent
|
|
271 |
The benefits of using
|
|
272 |
$\textit{prune}$ such as refining the finiteness bound
|
|
273 |
to a cubic bound has not been formalised yet.
|
|
274 |
Therefore we choose to use Scala code rather than an Isabelle-style formal
|
|
275 |
definition like we did for $\simp$, as the definitions might change
|
|
276 |
to suit proof needs.
|
|
277 |
In the rest of the chapter we will use this convention consistently.
|
|
278 |
\begin{figure}[H]
|
|
279 |
\begin{lstlisting}
|
|
280 |
def distinctWith(rs: List[ARexp],
|
|
281 |
pruneFunction: (ARexp, Set[Rexp]) => ARexp,
|
|
282 |
acc: Set[Rexp] = Set()) : List[ARexp] =
|
|
283 |
rs match{
|
|
284 |
case Nil => Nil
|
|
285 |
case r :: rs =>
|
|
286 |
if(acc(erase(r)))
|
|
287 |
distinctWith(rs, pruneFunction, acc)
|
|
288 |
else {
|
|
289 |
val pruned_r = pruneFunction(r, acc)
|
|
290 |
pruned_r ::
|
|
291 |
distinctWith(rs,
|
|
292 |
pruneFunction,
|
|
293 |
turnIntoTerms(erase(pruned_r)) ++: acc
|
|
294 |
)
|
|
295 |
}
|
|
296 |
}
|
|
297 |
\end{lstlisting}
|
|
298 |
\caption{A Stronger Version of $\textit{distinctBy}$}
|
|
299 |
\end{figure}
|
|
300 |
\noindent
|
|
301 |
The function $\textit{prune}$ is used in $\distinctWith$.
|
|
302 |
$\distinctWith$ is a stronger version of $\distinctBy$
|
|
303 |
which not only removes duplicates as $\distinctBy$ would
|
|
304 |
do, but also uses the $\textit{pruneFunction}$
|
|
305 |
argument to prune away verbose components in a regular expression.\\
|
|
306 |
\begin{figure}[H]
|
|
307 |
\begin{lstlisting}
|
|
308 |
//a stronger version of simp
|
|
309 |
def bsimpStrong(r: ARexp): ARexp =
|
|
310 |
{
|
|
311 |
r match {
|
|
312 |
case ASEQ(bs1, r1, r2) => (bsimpStrong(r1), bsimpStrong(r2)) match {
|
|
313 |
//normal clauses same as simp
|
|
314 |
case (AZERO, _) => AZERO
|
|
315 |
case (_, AZERO) => AZERO
|
|
316 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
|
|
317 |
//bs2 can be discarded
|
|
318 |
case (r1s, AONE(bs2)) => fuse(bs1, r1s) //assert bs2 == Nil
|
|
319 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
|
|
320 |
}
|
|
321 |
case AALTS(bs1, rs) => {
|
|
322 |
//distinctBy(flat_res, erase)
|
|
323 |
distinctWith(flats(rs.map(bsimpStrong(_))), prune) match {
|
|
324 |
case Nil => AZERO
|
|
325 |
case s :: Nil => fuse(bs1, s)
|
|
326 |
case rs => AALTS(bs1, rs)
|
|
327 |
}
|
|
328 |
}
|
|
329 |
//stars that can be treated as 1
|
|
330 |
case ASTAR(bs, r0) if(atMostEmpty(erase(r0))) => AONE(bs)
|
|
331 |
case r => r
|
|
332 |
}
|
|
333 |
}
|
|
334 |
\end{lstlisting}
|
|
335 |
\caption{The function $\bsimpStrong$ and $\bdersStrongs$}
|
|
336 |
\end{figure}
|
|
337 |
\noindent
|
|
338 |
$\distinctWith$, is in turn used in $\bsimpStrong$:
|
|
339 |
\begin{figure}[H]
|
|
340 |
\begin{lstlisting}
|
|
341 |
//Conjecture: [| bdersStrong(s, r) |] = O([| r |]^3)
|
|
342 |
def bdersStrong(s: List[Char], r: ARexp) : ARexp = s match {
|
|
343 |
case Nil => r
|
|
344 |
case c::s => bdersStrong(s, bsimpStrong(bder(c, r)))
|
|
345 |
}
|
|
346 |
\end{lstlisting}
|
|
347 |
\caption{The function $\bsimpStrong$ and $\bdersStrongs$}
|
|
348 |
\end{figure}
|
|
349 |
\noindent
|
|
350 |
We conjecture that the above Scala function $\bdersStrongs$,
|
|
351 |
written $\bdersStrong{\_}{\_}$ as an infix notation,
|
|
352 |
satisfies the following property:
|
|
353 |
\begin{conjecture}
|
|
354 |
$\llbracket \bdersStrong{a}{s} \rrbracket = O(\llbracket a \rrbracket^3)$
|
|
355 |
\end{conjecture}
|
|
356 |
The stronger version of $\blexersimp$'s
|
|
357 |
code in Scala looks like:
|
|
358 |
\begin{figure}[H]
|
|
359 |
\begin{lstlisting}
|
|
360 |
def strongBlexer(r: Rexp, s: String) : Option[Val] = {
|
|
361 |
Try(Some(decode(r, strong_blex_simp(internalise(r), s.toList)))).getOrElse(None)
|
|
362 |
}
|
|
363 |
def strong_blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
|
|
364 |
case Nil => {
|
|
365 |
if (bnullable(r)) {
|
|
366 |
mkepsBC(r)
|
|
367 |
}
|
|
368 |
else
|
|
369 |
throw new Exception("Not matched")
|
|
370 |
}
|
|
371 |
case c::cs => {
|
|
372 |
strong_blex_simp(strongBsimp(bder(c, r)), cs)
|
|
373 |
}
|
|
374 |
}
|
|
375 |
\end{lstlisting}
|
|
376 |
\end{figure}
|
|
377 |
\noindent
|
621
|
378 |
We call this lexer $\blexerStrong$.
|
|
379 |
$\blexerStrong$ is able to drastically reduce the
|
|
380 |
internal data structure size which could
|
|
381 |
trigger exponential behaviours in
|
|
382 |
$\blexersimp$.
|
|
383 |
\begin{figure}[H]
|
|
384 |
\centering
|
|
385 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
386 |
\begin{tikzpicture}
|
|
387 |
\begin{axis}[
|
|
388 |
%xlabel={$n$},
|
|
389 |
myplotstyle,
|
|
390 |
xlabel={input length},
|
|
391 |
ylabel={size},
|
|
392 |
width = 7cm,
|
|
393 |
height = 5cm,
|
|
394 |
]
|
|
395 |
\addplot[red,mark=*, mark options={fill=white}] table {strongSimpCurve.data};
|
|
396 |
\end{axis}
|
|
397 |
\end{tikzpicture}
|
|
398 |
&
|
|
399 |
\begin{tikzpicture}
|
|
400 |
\begin{axis}[
|
|
401 |
%xlabel={$n$},
|
|
402 |
myplotstyle,
|
|
403 |
xlabel={input length},
|
|
404 |
ylabel={size},
|
|
405 |
width = 7cm,
|
|
406 |
height = 5cm,
|
|
407 |
]
|
|
408 |
\addplot[blue,mark=*, mark options={fill=white}] table {bsimpExponential.data};
|
|
409 |
\end{axis}
|
|
410 |
\end{tikzpicture}\\
|
|
411 |
\multicolumn{2}{l}{}
|
|
412 |
\end{tabular}
|
|
413 |
\caption{Runtime for matching
|
|
414 |
$\protect((a^* + (aa)^* + \ldots + (aaaaa)^* )^*)^*$ with strings
|
|
415 |
of the form $\protect\underbrace{aa..a}_{n}$.}\label{fig:aaaaaStarStar}
|
|
416 |
\end{figure}
|
|
417 |
\noindent
|
591
|
418 |
We would like to preserve the correctness like the one
|
|
419 |
we had for $\blexersimp$:
|
|
420 |
\begin{conjecture}\label{cubicConjecture}
|
|
421 |
$\blexerStrong \;r \; s = \blexer\; r\;s$
|
|
422 |
\end{conjecture}
|
592
|
423 |
\noindent
|
621
|
424 |
The idea is to maintain key lemmas in
|
|
425 |
chapter \ref{Bitcoded2} like
|
|
426 |
$r \stackrel{*}{\rightsquigarrow} \textit{bsimp} \; r$
|
|
427 |
with the new rewriting rule \ref{cubicRule} .
|
591
|
428 |
|
621
|
429 |
In the next sub-section,
|
592
|
430 |
we will describe why we
|
|
431 |
believe a cubic bound can be achieved.
|
|
432 |
We give an introduction to the
|
|
433 |
partial derivatives,
|
|
434 |
which was invented by Antimirov \cite{Antimirov95},
|
|
435 |
and then link it with the result of the function
|
|
436 |
$\bdersStrongs$.
|
|
437 |
|
621
|
438 |
\subsection{Antimirov's partial derivatives}
|
|
439 |
Partial derivatives were first introduced by
|
|
440 |
Antimirov \cite{Antimirov95}.
|
|
441 |
It does derivatives in a similar way as suggested by Brzozowski,
|
|
442 |
but splits children of alternative regular expressions into
|
|
443 |
multiple independent terms, causing the output to become a
|
|
444 |
set of regular expressions:
|
|
445 |
\begin{center}
|
|
446 |
\begin{tabular}{lcl}
|
|
447 |
$\partial_x \; (a \cdot b)$ &
|
|
448 |
$\dn$ & $\partial_x \; a\cdot b \cup
|
|
449 |
\partial_x \; b \; \textit{if} \; \; \nullable\; a$\\
|
|
450 |
& & $\partial_x \; a\cdot b \quad\quad
|
|
451 |
\textit{otherwise}$\\
|
|
452 |
$\partial_x \; r^*$ & $\dn$ & $\partial_x \; r \cdot r^*$\\
|
|
453 |
$\partial_x \; c $ & $\dn$ & $\textit{if} \; x = c \;
|
|
454 |
\textit{then} \;
|
|
455 |
\{ \ONE\} \;\;\textit{else} \; \varnothing$\\
|
|
456 |
$\partial_x(a+b)$ & $=$ & $\partial_x(a) \cup \partial_x(b)$\\
|
|
457 |
$\partial_x(\ONE)$ & $=$ & $\varnothing$\\
|
|
458 |
$\partial_x(\ZERO)$ & $\dn$ & $\varnothing$\\
|
|
459 |
\end{tabular}
|
591
|
460 |
\end{center}
|
621
|
461 |
\noindent
|
|
462 |
The $\cdot$ between for example
|
|
463 |
$\partial_x \; a\cdot b $
|
|
464 |
is a shorthand notation for the cartesian product
|
|
465 |
$\partial_x \; a \times \{ b\}$.
|
|
466 |
%Each element in the set generated by a partial derivative
|
|
467 |
%corresponds to a (potentially partial) match
|
|
468 |
%TODO: define derivatives w.r.t string s
|
591
|
469 |
Rather than joining the calculated derivatives $\partial_x a$ and $\partial_x b$ together
|
621
|
470 |
using the $\sum$ constructor, Antimirov put them into
|
|
471 |
a set. This causes maximum de-duplication to happen,
|
591
|
472 |
allowing us to understand what are the "atomic" components of it.
|
|
473 |
For example, To compute what regular expression $x^*(xx + y)^*$'s
|
|
474 |
derivative against $x$ is made of, one can do a partial derivative
|
|
475 |
of it and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
|
|
476 |
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
|
621
|
477 |
|
|
478 |
The set of all possible partial derivatives is defined
|
|
479 |
as the union of derivatives w.r.t all the strings in the universe:
|
|
480 |
\begin{center}
|
|
481 |
\begin{tabular}{lcl}
|
|
482 |
$\textit{PDER}_{UNIV} \; r $ & $\dn $ & $\bigcup_{w \in A^*}\partial_w \; r$
|
|
483 |
\end{tabular}
|
591
|
484 |
\end{center}
|
621
|
485 |
\noindent
|
591
|
486 |
|
621
|
487 |
Back to our
|
|
488 |
\begin{center}
|
|
489 |
$((a^* + (aa)^* + \ldots + (\underbrace{a\ldots a}_{n a's})^* )^*)^*$
|
|
490 |
\end{center}
|
|
491 |
example, if we denote this regular expression as $A$,
|
|
492 |
we have that
|
|
493 |
\begin{center}
|
|
494 |
$\textit{PDER}_{UNIV} \; A =
|
|
495 |
\bigcup_{i=1}^{n}\bigcup_{j=0}^{i-1} \{
|
|
496 |
(\underbrace{a \ldots a}_{\text{j a's}}\cdot
|
|
497 |
(\underbrace{a \ldots a}_{\text{i a's}})^*)\cdot A \}$,
|
591
|
498 |
\end{center}
|
621
|
499 |
with exactly $n * (n + 1) / 2$ terms.
|
|
500 |
This is in line with our speculation that only $n*(n+1)/2$ terms are
|
|
501 |
needed. We conjecture that $\bsimpStrong$ is also able to achieve this
|
|
502 |
upper limit in general
|
|
503 |
\begin{conjecture}\label{bsimpStrongInclusionPder}
|
|
504 |
Using a suitable transformation $f$, we have
|
|
505 |
\begin{center}
|
|
506 |
$\forall s.\; f \; (r \bdersStrong \; s) \subseteq
|
|
507 |
\textit{PDER}_{UNIV} \; r$
|
|
508 |
\end{center}
|
|
509 |
\end{conjecture}
|
|
510 |
\noindent
|
|
511 |
because our \ref{cubicRule} will keep only one copy of each term,
|
|
512 |
where the function $\textit{prune}$ takes care of maintaining
|
|
513 |
a set like structure similar to partial derivatives.
|
|
514 |
It is anticipated we might need to adjust $\textit{prune}$
|
|
515 |
slightly to make sure all duplicate terms are eliminated,
|
|
516 |
which should be doable.
|
591
|
517 |
|
621
|
518 |
Antimirov had proven that the sum of all the partial derivative
|
|
519 |
terms' sizes is bounded by the cubic of the size of that regular
|
|
520 |
expression:
|
|
521 |
\begin{property}\label{pderBound}
|
|
522 |
$\llbracket \textit{PDER}_{UNIV} \; r \rrbracket \leq O((\llbracket r \rrbracket)^3)$
|
|
523 |
\end{property}
|
|
524 |
This property was formalised by Urban, and the details are in the PDERIVS.thy file
|
|
525 |
in our repository.
|
|
526 |
Once conjecture \ref{bsimpStrongInclusionPder} is proven, then property \ref{pderBound}
|
|
527 |
would yield us a cubic bound for our $\blexerStrong$ algorithm:
|
|
528 |
\begin{conjecture}\label{strongCubic}
|
|
529 |
$\llbracket r \bdersStrong\; s \rrbracket \leq \llbracket r \rrbracket^3$
|
|
530 |
\end{conjecture}
|
591
|
531 |
|
|
532 |
|
621
|
533 |
%To get all the "atomic" components of a regular expression's possible derivatives,
|
|
534 |
%there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
|
|
535 |
%whatever character is available at the head of the string inside the language of a
|
|
536 |
%regular expression, and gives back the character and the derivative regular expression
|
|
537 |
%as a pair (which he called "monomial"):
|
|
538 |
% \begin{center}
|
|
539 |
% \begin{tabular}{ccc}
|
|
540 |
% $\lf(\ONE)$ & $=$ & $\phi$\\
|
|
541 |
%$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
|
|
542 |
% $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
|
|
543 |
% $\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
|
|
544 |
%\end{tabular}
|
|
545 |
%\end{center}
|
|
546 |
%%TODO: completion
|
|
547 |
%
|
|
548 |
%There is a slight difference in the last three clauses compared
|
|
549 |
%with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular
|
|
550 |
%expression $r$ with every element inside $\textit{rset}$ to create a set of
|
|
551 |
%sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates
|
|
552 |
%on a set of monomials (which Antimirov called "linear form") and a regular
|
|
553 |
%expression, and returns a linear form:
|
|
554 |
% \begin{center}
|
|
555 |
% \begin{tabular}{ccc}
|
|
556 |
% $l \bigodot (\ZERO)$ & $=$ & $\phi$\\
|
|
557 |
% $l \bigodot (\ONE)$ & $=$ & $l$\\
|
|
558 |
% $\phi \bigodot t$ & $=$ & $\phi$\\
|
|
559 |
% $\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
|
|
560 |
% $\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
|
|
561 |
% $\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
|
|
562 |
% $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
|
|
563 |
% $\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
|
|
564 |
%\end{tabular}
|
|
565 |
%\end{center}
|
|
566 |
%%TODO: completion
|
|
567 |
%
|
|
568 |
% Some degree of simplification is applied when doing $\bigodot$, for example,
|
|
569 |
% $l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
|
|
570 |
% and $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
|
|
571 |
% $\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
|
|
572 |
% and so on.
|
|
573 |
%
|
|
574 |
% With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regular expression $r$ with
|
|
575 |
% an iterative procedure:
|
|
576 |
% \begin{center}
|
|
577 |
% \begin{tabular}{llll}
|
|
578 |
%$\textit{while}$ & $(\Delta_i \neq \phi)$ & & \\
|
|
579 |
% & $\Delta_{i+1}$ & $ =$ & $\lf(\Delta_i) - \PD_i$ \\
|
|
580 |
% & $\PD_{i+1}$ & $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
|
|
581 |
%$\partial_{UNIV}(r)$ & $=$ & $\PD$ &
|
|
582 |
%\end{tabular}
|
|
583 |
%\end{center}
|
|
584 |
%
|
|
585 |
%
|
|
586 |
% $(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
|
591
|
587 |
|
|
588 |
|
532
|
589 |
|
|
590 |
|
|
591 |
%----------------------------------------------------------------------------------------
|
|
592 |
% SECTION 2
|
|
593 |
%----------------------------------------------------------------------------------------
|
|
594 |
|
620
|
595 |
|
621
|
596 |
%The closed form for them looks like:
|
|
597 |
%%\begin{center}
|
|
598 |
%% \begin{tabular}{llrclll}
|
|
599 |
%% $r^{\{n+1\}}$ & $ \backslash_{rsimps}$ & $(c::s)$ & $=$ & & \\
|
|
600 |
%% $\textit{rsimp}$ & $($ & $
|
|
601 |
%% \sum \; ( $ & $\map$ & $(\textit{optermsimp}\;r)$ & $($\\
|
|
602 |
%% & & & & $\textit{nupdates} \;$ &
|
|
603 |
%% $ s \; r_0 \; [ \textit{Some} \; ([c], n)]$\\
|
|
604 |
%% & & & & $)$ &\\
|
|
605 |
%% & & $)$ & & &\\
|
|
606 |
%% & $)$ & & & &\\
|
|
607 |
%% \end{tabular}
|
|
608 |
%%\end{center}
|
620
|
609 |
%\begin{center}
|
621
|
610 |
% \begin{tabular}{llrcllrllll}
|
|
611 |
% $r^{\{n+1\}}$ & $ \backslash_{rsimps}$ & $(c::s)$ & $=$ & & &&&&\\
|
|
612 |
% &&&&$\textit{rsimp}$ & $($ & $
|
|
613 |
% \sum \; ( $ & $\map$ & $(\textit{optermsimp}\;r)$ & $($\\
|
|
614 |
% &&&& & & & & $\;\; \textit{nupdates} \;$ &
|
|
615 |
% $ s \; r_0 \; [ \textit{Some} \; ([c], n)]$\\
|
|
616 |
% &&&& & & & & $)$ &\\
|
|
617 |
% &&&& & & $)$ & & &\\
|
|
618 |
% &&&& & $)$ & & & &\\
|
|
619 |
% \end{tabular}
|
620
|
620 |
%\end{center}
|
621
|
621 |
%The $\textit{optermsimp}$ function with the argument $r$
|
|
622 |
%chooses from two options: $\ZERO$ or
|
|
623 |
%We define for the $r^{\{n\}}$ constructor something similar to $\starupdate$
|
|
624 |
%and $\starupdates$:
|
620
|
625 |
%\begin{center}
|
621
|
626 |
% \begin{tabular}{lcl}
|
|
627 |
% $\starupdate \; c \; r \; [] $ & $\dn$ & $[]$\\
|
|
628 |
% $\starupdate \; c \; r \; (s :: Ss)$ & $\dn$ & \\
|
|
629 |
% & & $\textit{if} \;
|
|
630 |
% (\rnullable \; (\rders \; r \; s))$ \\
|
|
631 |
% & & $\textit{then} \;\; (s @ [c]) :: [c] :: (
|
|
632 |
% \starupdate \; c \; r \; Ss)$ \\
|
|
633 |
% & & $\textit{else} \;\; (s @ [c]) :: (
|
|
634 |
% \starupdate \; c \; r \; Ss)$
|
|
635 |
% \end{tabular}
|
620
|
636 |
%\end{center}
|
621
|
637 |
%\noindent
|
|
638 |
%As a generalisation from characters to strings,
|
|
639 |
%$\starupdates$ takes a string instead of a character
|
|
640 |
%as the first input argument, and is otherwise the same
|
|
641 |
%as $\starupdate$.
|
|
642 |
%\begin{center}
|
|
643 |
% \begin{tabular}{lcl}
|
|
644 |
% $\starupdates \; [] \; r \; Ss$ & $=$ & $Ss$\\
|
|
645 |
% $\starupdates \; (c :: cs) \; r \; Ss$ & $=$ & $\starupdates \; cs \; r \; (
|
|
646 |
% \starupdate \; c \; r \; Ss)$
|
|
647 |
% \end{tabular}
|
|
648 |
%\end{center}
|
|
649 |
%\noindent
|
620
|
650 |
|
|
651 |
|
|
652 |
|
621
|
653 |
%\section{Zippers}
|
|
654 |
%Zipper is a data structure designed to operate on
|
|
655 |
%and navigate between local parts of a tree.
|
|
656 |
%It was first formally described by Huet \cite{HuetZipper}.
|
|
657 |
%Typical applications of zippers involve text editor buffers
|
|
658 |
%and proof system databases.
|
|
659 |
%In our setting, the idea is to compactify the representation
|
|
660 |
%of derivatives with zippers, thereby making our algorithm faster.
|
|
661 |
%Some initial results
|
|
662 |
%We first give a brief introduction to what zippers are,
|
|
663 |
%and other works
|
|
664 |
%that apply zippers to derivatives
|
|
665 |
%When dealing with large trees, it would be a waste to
|
|
666 |
%traverse the entire tree if
|
|
667 |
%the operation only
|
|
668 |
%involves a small fraction of it.
|
|
669 |
%The idea is to put the focus on that subtree, turning other parts
|
|
670 |
%of the tree into a context
|
|
671 |
%
|
|
672 |
%
|
|
673 |
%One observation about our derivative-based lexing algorithm is that
|
|
674 |
%the derivative operation sometimes traverses the entire regular expression
|
|
675 |
%unnecessarily:
|
620
|
676 |
|
|
677 |
|
612
|
678 |
%----------------------------------------------------------------------------------------
|
|
679 |
% SECTION 1
|
|
680 |
%----------------------------------------------------------------------------------------
|
532
|
681 |
|
612
|
682 |
%\section{Adding Support for the Negation Construct, and its Correctness Proof}
|
|
683 |
%We now add support for the negation regular expression:
|
|
684 |
%\[ r ::= \ZERO \mid \ONE
|
|
685 |
% \mid c
|
|
686 |
% \mid r_1 \cdot r_2
|
|
687 |
% \mid r_1 + r_2
|
|
688 |
% \mid r^*
|
|
689 |
% \mid \sim r
|
|
690 |
%\]
|
|
691 |
%The $\textit{nullable}$ function's clause for it would be
|
|
692 |
%\[
|
|
693 |
%\textit{nullable}(~r) = \neg \nullable(r)
|
|
694 |
%\]
|
|
695 |
%The derivative would be
|
|
696 |
%\[
|
|
697 |
%~r \backslash c = ~ (r \backslash c)
|
|
698 |
%\]
|
|
699 |
%
|
|
700 |
%The most tricky part of lexing for the $~r$ regular expression
|
|
701 |
% is creating a value for it.
|
|
702 |
% For other regular expressions, the value aligns with the
|
|
703 |
% structure of the regular expression:
|
|
704 |
% \[
|
|
705 |
% \vdash \Seq(\Char(a), \Char(b)) : a \cdot b
|
|
706 |
% \]
|
|
707 |
%But for the $~r$ regular expression, $s$ is a member of it if and only if
|
|
708 |
%$s$ does not belong to $L(r)$.
|
|
709 |
%That means when there
|
|
710 |
%is a match for the not regular expression, it is not possible to generate how the string $s$ matched
|
|
711 |
%with $r$.
|
|
712 |
%What we can do is preserve the information of how $s$ was not matched by $r$,
|
|
713 |
%and there are a number of options to do this.
|
|
714 |
%
|
|
715 |
%We could give a partial value when there is a partial match for the regular expression inside
|
|
716 |
%the $\mathbf{not}$ construct.
|
|
717 |
%For example, the string $ab$ is not in the language of $(a\cdot b) \cdot c$,
|
|
718 |
%A value for it could be
|
|
719 |
% \[
|
|
720 |
% \vdash \textit{Not}(\Seq(\Char(a), \Char(b))) : ~((a \cdot b ) \cdot c)
|
|
721 |
% \]
|
|
722 |
% The above example demonstrates what value to construct
|
|
723 |
% when the string $s$ is at most a real prefix
|
|
724 |
% of the strings in $L(r)$. When $s$ instead is not a prefix of any strings
|
|
725 |
% in $L(r)$, it becomes unclear what to return as a value inside the $\textit{Not}$
|
|
726 |
% constructor.
|
|
727 |
%
|
|
728 |
% Another option would be to either store the string $s$ that resulted in
|
|
729 |
% a mis-match for $r$ or a dummy value as a placeholder:
|
|
730 |
% \[
|
|
731 |
% \vdash \textit{Not}(abcd) : ~( r_1 )
|
|
732 |
% \]
|
|
733 |
%or
|
|
734 |
% \[
|
|
735 |
% \vdash \textit{Not}(\textit{Dummy}) : ~( r_1 )
|
|
736 |
% \]
|
|
737 |
% We choose to implement this as it is most straightforward:
|
|
738 |
% \[
|
|
739 |
% \mkeps(~(r)) = \textit{if}(\nullable(r)) \; \textit{Error} \; \textit{else} \; \textit{Not}(\textit{Dummy})
|
|
740 |
% \]
|
|
741 |
%
|
|
742 |
%
|
620
|
743 |
%\begin{center}
|
|
744 |
% \begin{tabular}{lcl}
|
|
745 |
% $\ntset \; r \; (n+1) \; c::cs $ & $\dn$ & $\nupdates \;
|
|
746 |
% cs \; r \; [\Some \; ([c], n)]$\\
|
|
747 |
% $\ntset \; r\; 0 \; \_$ & $\dn$ & $\None$\\
|
|
748 |
% $\ntset \; r \; \_ \; [] $ & $ \dn$ & $[]$\\
|
|
749 |
% \end{tabular}
|
|
750 |
%\end{center}
|