532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
% Main chapter title
|
538
|
4 |
\chapter{Bit-coded Algorithm of Sulzmann and Lu}
|
532
|
5 |
|
|
6 |
\label{Bitcoded1} % Change X to a consecutive number; for referencing this chapter elsewhere, use \ref{ChapterX}
|
|
7 |
%Then we illustrate how the algorithm without bitcodes falls short for such aggressive
|
|
8 |
%simplifications and therefore introduce our version of the bitcoded algorithm and
|
|
9 |
%its correctness proof in
|
|
10 |
%Chapter 3\ref{Chapter3}.
|
564
|
11 |
In this chapter, we are going to describe the bit-coded algorithm
|
|
12 |
introduced by Sulzmann and Lu \parencite{Sulzmann2014} to address the growth problem of
|
|
13 |
regular expressions.
|
537
|
14 |
\section{Bit-coded Algorithm}
|
|
15 |
The lexer algorithm in Chapter \ref{Inj}, as shown in \ref{InjFigure},
|
|
16 |
stores information of previous lexing steps
|
|
17 |
on a stack, in the form of regular expressions
|
|
18 |
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
|
|
19 |
\begin{ceqn}
|
|
20 |
\begin{equation}%\label{graph:injLexer}
|
564
|
21 |
\begin{tikzcd}[ampersand replacement=\&, execute at end picture={
|
|
22 |
\begin{scope}[on background layer]
|
|
23 |
\node[rectangle, fill={red!30},
|
|
24 |
pattern=north east lines, pattern color=red,
|
|
25 |
fit={(-3,-1) (-3, 1) (1, -1)
|
|
26 |
(1, 1)}
|
|
27 |
]
|
|
28 |
{}; ,
|
|
29 |
\node[rectangle, fill={blue!20},
|
|
30 |
pattern=north east lines, pattern color=blue,
|
|
31 |
fit= {(1, -1) (1, 1) (3, -1) (3, 1)}
|
|
32 |
]
|
|
33 |
{};
|
|
34 |
\end{scope}}
|
|
35 |
]
|
|
36 |
r_0 \arrow[r, "\backslash c_0"] \arrow[d] \& r_1 \arrow[r, "\backslash c_1"] \arrow[d] \& r_2 \arrow[r, dashed] \arrow[d] \& r_n \arrow[d, "mkeps" description] \\
|
|
37 |
v_0 \& v_1 \arrow[l,"inj_{r_0} c_0"] \& v_2 \arrow[l, "inj_{r_1} c_1"] \& v_n \arrow[l, dashed] \\
|
537
|
38 |
\end{tikzcd}
|
|
39 |
\end{equation}
|
|
40 |
\end{ceqn}
|
|
41 |
\noindent
|
564
|
42 |
The red part represents what we already know during the first
|
|
43 |
derivative phase,
|
|
44 |
and the blue part represents the unknown part of input.
|
|
45 |
The red area expands as we move towards $r_n$,
|
|
46 |
indicating an increasing stack size during lexing.
|
|
47 |
Despite having some partial lexing information during
|
|
48 |
the forward derivative phase, we choose to store them
|
|
49 |
temporarily, only to convert the information to lexical
|
|
50 |
values at a later stage. In essence we are repeating work we
|
|
51 |
have already done.
|
537
|
52 |
This is both inefficient and prone to stack overflow.
|
|
53 |
A natural question arises as to whether we can store lexing
|
|
54 |
information on the fly, while still using regular expression
|
564
|
55 |
derivatives.
|
537
|
56 |
|
564
|
57 |
If we remove the details of the individual
|
|
58 |
lexing steps, and use red and blue areas as before
|
|
59 |
to indicate consumed (seen) input and constructed
|
|
60 |
partial value (before recovering the rest of the stack),
|
|
61 |
one could see that the seen part's lexical information
|
|
62 |
is stored in the form of a regular expression.
|
|
63 |
Consider the regular expression $(aa)^* \cdot bc$ matching the string $aabc$
|
|
64 |
and assume we have just read the two characters $aa$:
|
532
|
65 |
\begin{center}
|
537
|
66 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
67 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
564
|
68 |
{Partial lexing info: $\ONE \cdot a \cdot (aa)^* \cdot bc$ etc.
|
537
|
69 |
\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
|
|
70 |
\end{tikzpicture}
|
|
71 |
\end{center}
|
564
|
72 |
\noindent
|
|
73 |
In the injection-based lexing algorithm, we ``neglect" the red area
|
|
74 |
by putting all the characters we have consumed and
|
|
75 |
intermediate regular expressions on the stack when
|
|
76 |
we go from left to right in the derivative phase.
|
|
77 |
The red area grows till the string is exhausted.
|
|
78 |
During the injection phase, the value in the blue area
|
|
79 |
is built up incrementally, while the red area shrinks.
|
|
80 |
Before we have recovered all characters and intermediate
|
|
81 |
derivative regular expressions from the stack,
|
|
82 |
what values these characters and regular expressions correspond
|
|
83 |
to are unknown:
|
537
|
84 |
\begin{center}
|
|
85 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
86 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={white!30,blue!20},]
|
564
|
87 |
{$(\ONE \cdot \ONE) \cdot (aa)^* \cdot bc $ correspond to:$???$
|
|
88 |
\nodepart{two} $b c$ corresponds to $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
|
537
|
89 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
90 |
\end{tikzpicture}
|
|
91 |
\end{center}
|
|
92 |
\noindent
|
564
|
93 |
However, they should be calculable,
|
|
94 |
as characters and regular expression shapes
|
|
95 |
after taking derivative w.r.t those characters
|
|
96 |
have already been known, therefore in our example,
|
|
97 |
we know that the value starts with two $a$s,
|
|
98 |
and makes up to an iteration in a Kleene star:
|
|
99 |
(We have put the injection-based lexing's partial
|
|
100 |
result in the right part of the split rectangle
|
|
101 |
to contrast it with the partial valued produced
|
|
102 |
in a forward manner)
|
537
|
103 |
\begin{center}
|
|
104 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
105 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
564
|
106 |
{$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$
|
|
107 |
\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$ $\stackrel{Inj}{\longleftarrow}$};
|
537
|
108 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
109 |
\end{tikzpicture}
|
|
110 |
\end{center}
|
|
111 |
\noindent
|
|
112 |
If we do this kind of "attachment"
|
|
113 |
and each time augment the attached partially
|
|
114 |
constructed value when taking off a
|
|
115 |
character:
|
|
116 |
\begin{center}
|
564
|
117 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
118 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint)
|
|
119 |
{$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$
|
|
120 |
\nodepart{two} Remaining: $b c$};
|
|
121 |
\end{tikzpicture}\\
|
|
122 |
$\downarrow$\\
|
537
|
123 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
124 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
125 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$
|
564
|
126 |
\nodepart{two} Remaining: $c$};
|
537
|
127 |
\end{tikzpicture}\\
|
564
|
128 |
$\downarrow$\\
|
537
|
129 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
130 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
131 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$
|
|
132 |
\nodepart{two} EOF};
|
|
133 |
\end{tikzpicture}
|
|
134 |
\end{center}
|
|
135 |
\noindent
|
|
136 |
In the end we could recover the value without a backward phase.
|
|
137 |
But (partial) values are a bit clumsy to stick together with a regular expression, so
|
|
138 |
we instead use bit-codes to encode them.
|
|
139 |
|
|
140 |
Bits and bitcodes (lists of bits) are defined as:
|
|
141 |
\begin{center}
|
|
142 |
$b ::= S \mid Z \qquad
|
532
|
143 |
bs ::= [] \mid b::bs
|
|
144 |
$
|
|
145 |
\end{center}
|
|
146 |
|
|
147 |
\noindent
|
538
|
148 |
Using $S$ and $Z$ rather than $1$ and $0$ is to avoid
|
|
149 |
confusion with the regular expressions $\ZERO$ and $\ONE$.
|
|
150 |
Bitcodes (or
|
532
|
151 |
bit-lists) can be used to encode values (or potentially incomplete values) in a
|
|
152 |
compact form. This can be straightforwardly seen in the following
|
|
153 |
coding function from values to bitcodes:
|
|
154 |
\begin{center}
|
|
155 |
\begin{tabular}{lcl}
|
|
156 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
|
|
157 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
|
537
|
158 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\
|
|
159 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\
|
532
|
160 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
|
537
|
161 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\
|
|
162 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\;
|
532
|
163 |
code(\Stars\,vs)$
|
|
164 |
\end{tabular}
|
|
165 |
\end{center}
|
|
166 |
\noindent
|
537
|
167 |
Here $\textit{code}$ encodes a value into a bit-code by converting
|
|
168 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty
|
|
169 |
star iteration by $S$. The border where a local star terminates
|
|
170 |
is marked by $Z$.
|
|
171 |
This coding is lossy, as it throws away the information about
|
532
|
172 |
characters, and also does not encode the ``boundary'' between two
|
|
173 |
sequence values. Moreover, with only the bitcode we cannot even tell
|
537
|
174 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$. The
|
532
|
175 |
reason for choosing this compact way of storing information is that the
|
|
176 |
relatively small size of bits can be easily manipulated and ``moved
|
564
|
177 |
around" in a regular expression.
|
537
|
178 |
|
564
|
179 |
Because of the lossiness, the process of decoding a bitlist requires additionally
|
|
180 |
a regular expression. The function $\decode$ is defined as:
|
537
|
181 |
We define the reverse operation of $\code$, which is $\decode$.
|
|
182 |
As expected, $\decode$ not only requires the bit-codes,
|
|
183 |
but also a regular expression to guide the decoding and
|
|
184 |
fill the gaps of characters:
|
532
|
185 |
|
|
186 |
|
|
187 |
%\begin{definition}[Bitdecoding of Values]\mbox{}
|
|
188 |
\begin{center}
|
|
189 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
190 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
|
|
191 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
|
537
|
192 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
193 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
|
|
194 |
(\Left\,v, bs_1)$\\
|
537
|
195 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
196 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
|
|
197 |
(\Right\,v, bs_1)$\\
|
|
198 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
|
|
199 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
|
|
200 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
|
|
201 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
|
537
|
202 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
|
|
203 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ &
|
532
|
204 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
205 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
|
|
206 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
|
|
207 |
|
|
208 |
$\textit{decode}\,bs\,r$ & $\dn$ &
|
|
209 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
210 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
|
|
211 |
\textit{else}\;\textit{None}$
|
|
212 |
\end{tabular}
|
538
|
213 |
\end{center}
|
532
|
214 |
%\end{definition}
|
|
215 |
|
538
|
216 |
\noindent
|
564
|
217 |
The function $\decode'$ returns a pair consisting of a partially decoded value and some leftover:
|
538
|
218 |
$\decode'$ does most of the job while $\decode$ throws
|
|
219 |
away leftover bit-codes and returns the value only.
|
|
220 |
$\decode$ is terminating as $\decode'$ is terminating.
|
|
221 |
We have the property that $\decode$ and $\code$ are
|
|
222 |
reverse operations of one another:
|
|
223 |
\begin{lemma}
|
|
224 |
\[\vdash v : r \implies \decode \; (\code \; v) \; r = \textit{Some}(v) \]
|
|
225 |
\end{lemma}
|
|
226 |
\begin{proof}
|
|
227 |
By proving a more general version of the lemma, on $\decode'$:
|
|
228 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \]
|
|
229 |
Then setting $ds$ to be $[]$ and unfolding $\decode$ definition
|
|
230 |
we get the lemma.
|
|
231 |
\end{proof}
|
|
232 |
With the $\code$ and $\decode$ functions in hand, we know how to
|
|
233 |
switch between bit-codes and value--the two different representations of
|
|
234 |
lexing information.
|
|
235 |
The next step is to integrate this information into the working regular expression.
|
|
236 |
Attaching bits to the front of regular expressions is the solution Sulzamann and Lu
|
|
237 |
gave for storing partial values on the fly:
|
532
|
238 |
|
|
239 |
\begin{center}
|
|
240 |
\begin{tabular}{lcl}
|
|
241 |
$\textit{a}$ & $::=$ & $\ZERO$\\
|
|
242 |
& $\mid$ & $_{bs}\ONE$\\
|
|
243 |
& $\mid$ & $_{bs}{\bf c}$\\
|
|
244 |
& $\mid$ & $_{bs}\sum\,as$\\
|
|
245 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\
|
|
246 |
& $\mid$ & $_{bs}a^*$
|
|
247 |
\end{tabular}
|
|
248 |
\end{center}
|
|
249 |
%(in \textit{ALTS})
|
|
250 |
|
|
251 |
\noindent
|
538
|
252 |
We call these regular expressions carrying bit-codes \emph{Annotated regular expressions}.
|
|
253 |
$bs$ stands for bit-codes, $a$ for $\mathbf{a}$nnotated regular
|
532
|
254 |
expressions and $as$ for a list of annotated regular expressions.
|
538
|
255 |
The alternative constructor ($\sum$) has been generalised to
|
532
|
256 |
accept a list of annotated regular expressions rather than just 2.
|
542
|
257 |
|
|
258 |
|
|
259 |
The first thing we define related to bit-coded regular expressions
|
564
|
260 |
is how we move bits, for instance pasting it at the front of an annotated regular expression.
|
542
|
261 |
The operation $\fuse$ is just to attach bit-codes
|
|
262 |
to the front of an annotated regular expression:
|
|
263 |
\begin{center}
|
|
264 |
\begin{tabular}{lcl}
|
|
265 |
$\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
|
|
266 |
$\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
|
|
267 |
$_{bs @ bs'}\ONE$\\
|
|
268 |
$\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
|
|
269 |
$_{bs@bs'}{\bf c}$\\
|
|
270 |
$\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ &
|
|
271 |
$_{bs@bs'}\sum\textit{as}$\\
|
|
272 |
$\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
|
|
273 |
$_{bs@bs'}a_1 \cdot a_2$\\
|
|
274 |
$\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
|
|
275 |
$_{bs @ bs'}a^*$
|
|
276 |
\end{tabular}
|
|
277 |
\end{center}
|
|
278 |
|
|
279 |
\noindent
|
|
280 |
With that we are able to define $\internalise$.
|
|
281 |
|
|
282 |
To do lexing using annotated regular expressions, we shall first
|
|
283 |
transform the usual (un-annotated) regular expressions into annotated
|
|
284 |
regular expressions. This operation is called \emph{internalisation} and
|
|
285 |
defined as follows:
|
|
286 |
|
|
287 |
%\begin{definition}
|
|
288 |
\begin{center}
|
|
289 |
\begin{tabular}{lcl}
|
|
290 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
|
|
291 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
|
|
292 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
|
|
293 |
$(r_1 + r_2)^\uparrow$ & $\dn$ &
|
|
294 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\,
|
|
295 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\
|
|
296 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ &
|
|
297 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
|
|
298 |
$(r^*)^\uparrow$ & $\dn$ &
|
|
299 |
$_{[]}(r^\uparrow)^*$\\
|
|
300 |
\end{tabular}
|
|
301 |
\end{center}
|
|
302 |
%\end{definition}
|
|
303 |
|
|
304 |
\noindent
|
|
305 |
We use an up arrow with postfix notation
|
|
306 |
to denote the operation,
|
|
307 |
for convenience. The $\textit{internalise} \; r$
|
|
308 |
notation is more cumbersome.
|
|
309 |
The opposite of $\textit{internalise}$ is
|
|
310 |
$\erase$, where all the bit-codes are removed,
|
|
311 |
and the alternative operator $\sum$ for annotated
|
|
312 |
regular expressions is transformed to the binary alternatives
|
|
313 |
for plain regular expressions.
|
|
314 |
\begin{center}
|
|
315 |
\begin{tabular}{lcr}
|
|
316 |
$\erase \; \ZERO$ & $\dn$ & $\ZERO$\\
|
|
317 |
$\erase \; _{bs}\ONE$ & $\dn$ & $\ONE$\\
|
|
318 |
$\erase \; _{bs}\mathbf{c}$ & $\dn$ & $\mathbf{c}$\\
|
|
319 |
$\erase \; _{bs} a_1 \cdot a_2$ & $\dn$ & $\erase \; r_1\cdot\erase\; r_2$\\
|
|
320 |
$\erase \; _{bs} [] $ & $\dn$ & $\ZERO$\\
|
|
321 |
$\erase \; _{bs} [a] $ & $\dn$ & $\erase \; a$\\
|
|
322 |
$\erase \; _{bs} \sum [a_1, \; a_2]$ & $\dn$ & $\erase \; a_1 +\erase \; a_2$\\
|
|
323 |
$\erase \; _{bs} \sum (a :: as)$ & $\dn$ & $\erase \; a + \erase \; _{[]} \sum as$\\
|
|
324 |
$\erase \; _{bs} a^*$ & $\dn$ & $(\erase \; a)^*$
|
|
325 |
\end{tabular}
|
|
326 |
\end{center}
|
|
327 |
\noindent
|
|
328 |
We also abbreviate the $\erase\; a$ operation
|
|
329 |
as $a_\downarrow$, for conciseness.
|
|
330 |
For bit-coded regular expressions, as a different datatype,
|
|
331 |
testing whether they contain empty string in their lauguage requires
|
|
332 |
a dedicated function $\bnullable$
|
|
333 |
which simply calls $\erase$ first before testing whether it is $\nullable$.
|
|
334 |
\begin{center}
|
|
335 |
\begin{tabular}{lcr}
|
|
336 |
$\bnullable \; a $ & $\dn$ & $\nullable \; (a_\downarrow)$
|
|
337 |
\end{tabular}
|
|
338 |
\end{center}
|
|
339 |
The function for collecting the
|
|
340 |
bitcodes of a $\bnullable$ annotated regular expression
|
|
341 |
is a generalised version of the $\textit{mkeps}$ function
|
|
342 |
for annotated regular expressions, called $\textit{bmkeps}$:
|
|
343 |
|
|
344 |
|
|
345 |
%\begin{definition}[\textit{bmkeps}]\mbox{}
|
|
346 |
\begin{center}
|
|
347 |
\begin{tabular}{lcl}
|
|
348 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
|
|
349 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
|
|
350 |
$\textit{if}\;\textit{bnullable}\,a$\\
|
|
351 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
|
|
352 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\
|
|
353 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
|
|
354 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
|
|
355 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
|
|
356 |
$bs \,@\, [Z]$
|
|
357 |
\end{tabular}
|
|
358 |
\end{center}
|
|
359 |
%\end{definition}
|
|
360 |
|
|
361 |
\noindent
|
|
362 |
This function completes the value information by travelling along the
|
|
363 |
path of the regular expression that corresponds to a POSIX value and
|
|
364 |
collecting all the bitcodes, and using $S$ to indicate the end of star
|
|
365 |
iterations.
|
|
366 |
|
538
|
367 |
The most central question is how these partial lexing information
|
|
368 |
represented as bit-codes is augmented and carried around
|
|
369 |
during a derivative is taken.
|
|
370 |
This is done by adding bitcodes to the
|
|
371 |
derivatives, for example when one more star iteratoin is taken (we
|
|
372 |
call the operation of derivatives on annotated regular expressions $\bder$
|
564
|
373 |
because it is derivatives on regular expressiones with \emph{b}itcodes),
|
542
|
374 |
we need to unfold it into a sequence,
|
|
375 |
and attach an additional bit $Z$ to the front of $r \backslash c$
|
|
376 |
to indicate one more star iteration.
|
538
|
377 |
\begin{center}
|
|
378 |
\begin{tabular}{@{}lcl@{}}
|
|
379 |
$\bder \; c\; (_{bs}a^*) $ & $\dn$ &
|
|
380 |
$_{bs}(\textit{fuse}\, [Z] \; \bder \; c \; a)\cdot
|
|
381 |
(_{[]}a^*))$
|
|
382 |
\end{tabular}
|
|
383 |
\end{center}
|
|
384 |
|
|
385 |
\noindent
|
|
386 |
For most time we use the infix notation $\backslash$ to mean $\bder$ for brevity when
|
|
387 |
there is no danger of confusion with derivatives on plain regular expressions,
|
|
388 |
for example, the above can be expressed as
|
|
389 |
\begin{center}
|
|
390 |
\begin{tabular}{@{}lcl@{}}
|
|
391 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
392 |
$_{bs}(\textit{fuse}\, [Z] \; a\,\backslash c)\cdot
|
|
393 |
(_{[]}a^*))$
|
|
394 |
\end{tabular}
|
|
395 |
\end{center}
|
|
396 |
|
542
|
397 |
\noindent
|
538
|
398 |
Using the picture we used earlier to depict this, the transformation when
|
|
399 |
taking a derivative w.r.t a star is like below:
|
542
|
400 |
|
538
|
401 |
\begin{tabular}{@{}l@{\hspace{1mm}}l@{\hspace{0mm}}c@{}}
|
|
402 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
403 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
404 |
{$bs$
|
|
405 |
\nodepart{two} $a^*$ };
|
|
406 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
407 |
\end{tikzpicture}
|
|
408 |
&
|
|
409 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
410 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
411 |
{$v_{\text{previous iterations}}$
|
|
412 |
\nodepart{two} $a^*$};
|
|
413 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
414 |
\end{tikzpicture}
|
|
415 |
\\
|
|
416 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
417 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
418 |
{ $bs$ + [Z]
|
|
419 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
420 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
421 |
\end{tikzpicture}
|
|
422 |
&
|
|
423 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
424 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
425 |
{$v_{\text{previous iterations}}$ + 1 more iteration
|
|
426 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
427 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
428 |
\end{tikzpicture}
|
|
429 |
\end{tabular}
|
|
430 |
\noindent
|
|
431 |
Another place in the $\bder$ function where it differs
|
542
|
432 |
from normal derivatives (on un-annotated regular expressions)
|
538
|
433 |
is the sequence case:
|
|
434 |
\begin{center}
|
|
435 |
\begin{tabular}{@{}lcl@{}}
|
|
436 |
|
|
437 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
438 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
439 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
440 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
441 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$
|
|
442 |
\end{tabular}
|
|
443 |
\end{center}
|
542
|
444 |
|
|
445 |
The difference is that (when $a_1$ is $\bnullable$)
|
|
446 |
we use $\bmkeps$ to store the lexing information
|
|
447 |
in $a_1$ before collapsing it (as it has been fully matched by string prior to $c$,
|
|
448 |
and attach the collected bit-codes to the front of $a_2$
|
|
449 |
before throwing away $a_1$. We assume that $\bmkeps$ correctly extracts the bitcode for how $a_1$
|
|
450 |
matches the string prior to $c$ (more on this later).
|
|
451 |
The bitsequence $\textit{bs}$ which was initially attached to the first element of the sequence
|
|
452 |
$a_1 \cdot a_2$, has now been elevated to the top level of teh $\sum$.
|
|
453 |
This is because this piece of information will be needed whichever way the sequence is matched,
|
|
454 |
regardless of whether $c$ belongs to $a_1$ or $a_2$.
|
538
|
455 |
|
542
|
456 |
In the injection-based lexing, $r_1$ is immediately thrown away in
|
|
457 |
subsequent derivatives on the right branch (when $r_1$ is $\nullable$),
|
|
458 |
\begin{center}
|
|
459 |
$(r_1 \cdot r_2 )\backslash c = (r_1 \backslash c) \cdot r_2 + r_2 \backslash c$
|
|
460 |
\end{center}
|
|
461 |
\noindent
|
|
462 |
as it knows $r_1$ is stored on stack and available once the recursive
|
|
463 |
call to later derivatives finish.
|
|
464 |
Therefore, if the $\Right$ branch is taken in a $\POSIX$ match,
|
|
465 |
we construct back the sequence value once step back by
|
|
466 |
calling a on $\mkeps(r_1)$.
|
|
467 |
\begin{center}
|
|
468 |
\begin{tabular}{lcr}
|
|
469 |
$\ldots r_1 \cdot r_2$ & $\rightarrow$ & $r_1\cdot r_2 + r_2 \backslash c \ldots $\\
|
|
470 |
$\ldots \Seq(v_1, v_2) (\Seq(\mkeps(r1), (\inj \; r_2 \; c\; v_{2c})))$ & $\leftarrow$ & $\Right(v_{2c})\ldots$
|
|
471 |
\end{tabular}
|
|
472 |
\end{center}
|
|
473 |
\noindent
|
|
474 |
The rest of the clauses of $\bder$ is rather similar to
|
|
475 |
$\der$, and is put together here as a wholesome definition
|
|
476 |
for $\bder$:
|
538
|
477 |
\begin{center}
|
|
478 |
\begin{tabular}{@{}lcl@{}}
|
|
479 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
480 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
481 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
|
|
482 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
483 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\
|
|
484 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
|
542
|
485 |
$_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\
|
538
|
486 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
487 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
488 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
489 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
490 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
|
|
491 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
492 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot
|
|
493 |
(_{[]}r^*))$
|
|
494 |
\end{tabular}
|
|
495 |
\end{center}
|
542
|
496 |
\noindent
|
|
497 |
Generalising the derivative operation with bitcodes to strings, we have
|
532
|
498 |
\begin{center}
|
542
|
499 |
\begin{tabular}{@{}lcl@{}}
|
|
500 |
$a\backslash_s [] $ & $\dn$ & $a$\\
|
|
501 |
$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$
|
|
502 |
\end{tabular}
|
|
503 |
\end{center}
|
|
504 |
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger
|
|
505 |
of confusion.
|
|
506 |
Putting this all together, we obtain a lexer with bit-coded regular expressions
|
|
507 |
as its internal data structures, which we call $\blexer$:
|
532
|
508 |
|
|
509 |
\begin{center}
|
|
510 |
\begin{tabular}{lcl}
|
|
511 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
512 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
513 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
514 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
515 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
516 |
\end{tabular}
|
|
517 |
\end{center}
|
|
518 |
|
|
519 |
\noindent
|
542
|
520 |
Ausaf and Urban formally proved the correctness of the $\blexer$, namely
|
|
521 |
\begin{conjecture}
|
|
522 |
$\blexer \;r \; s = \lexer \; r \; s$.
|
|
523 |
\end{conjecture}
|
|
524 |
This was claimed but not formalised in Sulzmann and Lu's work.
|
|
525 |
We introduce the proof later, after we give out all
|
|
526 |
the needed auxiliary functions and definitions
|
532
|
527 |
|
|
528 |
|
|
529 |
%-----------------------------------
|
|
530 |
% SUBSECTION 1
|
|
531 |
%-----------------------------------
|
|
532 |
\section{Specifications of Some Helper Functions}
|
542
|
533 |
The functions we introduce will give a more detailed glimpse into
|
|
534 |
the lexing process, which might not be possible
|
|
535 |
using $\lexer$ or $\blexer$ themselves.
|
|
536 |
The first function we shall look at is $\retrieve$.
|
543
|
537 |
\subsection{$\textit{Retrieve}$}
|
542
|
538 |
Our bit-coded lexer "retrieve"s the bitcodes using $\bmkeps$
|
|
539 |
after we finished doing all the derivatives:
|
532
|
540 |
\begin{center}
|
542
|
541 |
\begin{tabular}{lcl}
|
|
542 |
& & $\ldots$\\
|
|
543 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
544 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
545 |
& & $\ldots$
|
532
|
546 |
\end{tabular}
|
|
547 |
\end{center}
|
542
|
548 |
\noindent
|
|
549 |
Recall that $\bmkeps$ looks for the leftmost branch of an alternative
|
|
550 |
and completes a star's iterations by attaching a $Z$ at the end of the bitcodes
|
|
551 |
extracted. It "retrieves" a sequence by visiting both children and then stitch together
|
|
552 |
two bitcodes using concatenation. After the entire tree structure of the regular
|
|
553 |
expression has been traversed using the above manner, we get a bitcode encoding the
|
|
554 |
lexing result.
|
|
555 |
We know that this "retrieved" bitcode leads to the correct value after decoding,
|
|
556 |
which is $v_0$ in the bird's eye view of the injection-based lexing diagram.
|
|
557 |
Now assume we keep every other data structure in the diagram \ref{InjFigure},
|
|
558 |
and only replace all the plain regular expression by their annotated counterparts,
|
|
559 |
computed during a $\blexer$ run.
|
|
560 |
Then we obtain a diagram for the annotated regular expression derivatives and
|
|
561 |
their corresponding values, though the values are never calculated in $\blexer$.
|
|
562 |
We have that $a_n$ contains all the lexing result information.
|
|
563 |
\vspace{20mm}
|
|
564 |
\begin{center}%\label{graph:injLexer}
|
|
565 |
\begin{tikzcd}[
|
|
566 |
every matrix/.append style = {name=p},
|
|
567 |
remember picture, overlay,
|
|
568 |
]
|
|
569 |
a_0 \arrow[r, "\backslash c_0"] \arrow[d] & a_1 \arrow[r, "\backslash c_1"] \arrow[d] & a_2 \arrow[r, dashed] \arrow[d] & a_n \arrow[d] \\
|
|
570 |
v_0 & v_1 \arrow[l,"inj_{r_0} c_0"] & v_2 \arrow[l, "inj_{r_1} c_1"] & v_n \arrow[l, dashed]
|
|
571 |
\end{tikzcd}
|
|
572 |
\begin{tikzpicture}[
|
|
573 |
remember picture, overlay,
|
|
574 |
E/.style = {ellipse, draw=blue, dashed,
|
|
575 |
inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1}
|
|
576 |
]
|
|
577 |
\node[E = (p-1-1) (p-2-1)] {};
|
|
578 |
\node[E = (p-1-4) (p-2-4)] {};
|
|
579 |
\end{tikzpicture}
|
|
580 |
|
|
581 |
\end{center}
|
|
582 |
\vspace{20mm}
|
|
583 |
\noindent
|
|
584 |
On the other hand, $v_0$ also encodes the correct lexing result, as we have proven for $\lexer$.
|
|
585 |
Encircled in the diagram are the two pairs $v_0, a_0$ and $v_n, a_n$, which both
|
|
586 |
encode the correct lexical result. Though for the leftmost pair, we have
|
|
587 |
the information condensed in $v_0$ the value part, whereas for the rightmost pair,
|
|
588 |
the information is concentrated on $a_n$.
|
|
589 |
We know that in the intermediate steps the pairs $v_i, a_i$, must in some way encode the complete
|
|
590 |
lexing information as well. Therefore, we need a unified approach to extract such lexing result
|
|
591 |
from a value $v_i$ and its annotated regular expression $a_i$.
|
|
592 |
And the function $f$ must satisfy these requirements:
|
|
593 |
\begin{itemize}
|
|
594 |
\item
|
|
595 |
$f \; a_i\;v_i = f \; a_n \; v_n = \decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$
|
|
596 |
\item
|
|
597 |
$f \; a_i\;v_i = f \; a_0 \; v_0 = v_0 = \decode \;(\code \; v_0) \; (\erase \; a_0)$
|
|
598 |
\end{itemize}
|
|
599 |
\noindent
|
|
600 |
If we factor out the common part $\decode \; \_ \; (\erase \; a_0)$,
|
|
601 |
The core of the function $f$ is something that produces the bitcodes
|
|
602 |
$\code \; v_0$.
|
|
603 |
It is unclear how, but Sulzmann and Lu came up with a function satisfying all the above
|
|
604 |
requirements, named \emph{retrieve}:
|
532
|
605 |
|
|
606 |
|
|
607 |
|
542
|
608 |
\begin{center}
|
|
609 |
\begin{tabular}{lcr}
|
|
610 |
$\retrieve \; \, (_{bs} \ONE) \; \, (\Empty)$ & $\dn$ & $\textit{bs}$\\
|
|
611 |
$\retrieve \; \, (_{bs} \mathbf{c} ) \; \Char(c)$ & $\dn$ & $ \textit{bs}$\\
|
|
612 |
$\retrieve \; \, (_{bs} a_1 \cdot a_2) \; \Seq(v_1, v_2)$ & $\dn$ & $\textit{bs} @ (\retrieve \; a_1\; v_1) @ (\retrieve \; a_2 \; v_2)$\\
|
|
613 |
$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as}) \; \,\Left(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v)$\\
|
|
614 |
$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as} \; \, \Right(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\
|
|
615 |
$\retrieve \; \, (_{bs} a^*) \; \, (\Stars(v :: vs)) $ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v) @ (\retrieve \; (_{[]} a^*) \; (\Stars(vs)))$\\
|
|
616 |
$\retrieve \; \, (_{bs} a^*) \; \, (\Stars([]) $ & $\dn$ & $\textit{bs} @ [Z]$
|
|
617 |
\end{tabular}
|
|
618 |
\end{center}
|
|
619 |
\noindent
|
|
620 |
As promised, $\retrieve$ collects the right bit-codes from the
|
|
621 |
final derivative $a_n$:
|
|
622 |
\begin{lemma}
|
|
623 |
$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (\erase \; a))$
|
|
624 |
\end{lemma}
|
|
625 |
\begin{proof}
|
|
626 |
By a routine induction on $a$.
|
|
627 |
\end{proof}
|
|
628 |
The design of $\retrieve$ enables extraction of bit-codes
|
|
629 |
from not only $\bnullable$ (annotated) regular expressions,
|
|
630 |
but also those that are not $\bnullable$.
|
|
631 |
For example, if we have the regular expression just internalised
|
|
632 |
and the lexing result value, we could $\retrieve$ the bitcdoes
|
|
633 |
that look as if we have en$\code$-ed the value:
|
|
634 |
\begin{lemma}
|
|
635 |
$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$
|
|
636 |
\end{lemma}
|
|
637 |
\begin{proof}
|
|
638 |
By induction on $r$.
|
|
639 |
\end{proof}
|
|
640 |
\noindent
|
|
641 |
The following property is more interesting, as
|
|
642 |
it provides a "bridge" between $a_0, v_0$ and $a_n, v_n$ in the
|
|
643 |
lexing diagram.
|
|
644 |
If you take derivative of an annotated regular expression,
|
|
645 |
you can $\retrieve$ the same bit-codes as before the derivative took place,
|
|
646 |
provided that you use the corresponding value:
|
|
647 |
|
|
648 |
\begin{lemma}\label{retrieveStepwise}
|
|
649 |
$\vdash v : (r\backslash c) \implies \retrieve \; (r \backslash c) \; v= \retrieve \; r \; (\inj \; r\; c\; v)$
|
|
650 |
\end{lemma}
|
|
651 |
\begin{proof}
|
|
652 |
By induction on $r$, where $v$ is allowed to be arbitrary.
|
|
653 |
The induction principle is function $\erase$'s cases.
|
|
654 |
\end{proof}
|
|
655 |
\noindent
|
|
656 |
$\retrieve$ is connected to the $\blexer$ in the following way:
|
|
657 |
\begin{lemma}\label{blexer_retrieve}
|
|
658 |
$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$
|
|
659 |
\end{lemma}
|
|
660 |
\noindent
|
564
|
661 |
$\retrieve$ allows free navigation on the diagram \ref{InjFigure} for annotated regular expressiones of $\blexer$.
|
542
|
662 |
For plain regular expressions something similar is required as well.
|
|
663 |
|
|
664 |
\subsection{$\flex$}
|
|
665 |
Ausaf and Urban cleverly defined an auxiliary function called $\flex$ for $\lexer$,
|
532
|
666 |
defined as
|
536
|
667 |
\begin{center}
|
|
668 |
\begin{tabular}{lcr}
|
|
669 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\
|
|
670 |
$\flex \; r \; f \; c :: s \; v$ & $=$ & $\flex \; r \; \lambda v. \, f (\inj \; r\; c\; v)\; s \; v$
|
|
671 |
\end{tabular}
|
|
672 |
\end{center}
|
532
|
673 |
which accumulates the characters that needs to be injected back,
|
|
674 |
and does the injection in a stack-like manner (last taken derivative first injected).
|
|
675 |
$\flex$ is connected to the $\lexer$:
|
|
676 |
\begin{lemma}
|
|
677 |
$\flex \; r \; \textit{id}\; s \; \mkeps (r\backslash s) = \lexer \; r \; s$
|
|
678 |
\end{lemma}
|
542
|
679 |
\begin{proof}
|
|
680 |
By reverse induction on $s$.
|
|
681 |
\end{proof}
|
|
682 |
$\flex$ provides us a bridge between $\lexer$'s intermediate steps.
|
532
|
683 |
What is even better about $\flex$ is that it allows us to
|
|
684 |
directly operate on the value $\mkeps (r\backslash v)$,
|
|
685 |
which is pivotal in the definition of $\lexer $ and $\blexer$, but not visible as an argument.
|
|
686 |
When the value created by $\mkeps$ becomes available, one can
|
|
687 |
prove some stepwise properties of lexing nicely:
|
|
688 |
\begin{lemma}\label{flexStepwise}
|
|
689 |
$\textit{flex} \; r \; f \; s@[c] \; v= \flex \; r \; f\; s \; (\inj \; (r\backslash s) \; c \; v) $
|
|
690 |
\end{lemma}
|
542
|
691 |
\begin{proof}
|
|
692 |
By induction on the shape of $r\backslash s$
|
|
693 |
\end{proof}
|
|
694 |
\noindent
|
|
695 |
With $\flex$ and $\retrieve$ ready, we are ready to connect $\lexer$ and $\blexer$ .
|
532
|
696 |
|
542
|
697 |
\subsection{Correctness Proof of Bit-coded Algorithm}
|
532
|
698 |
\begin{lemma}\label{flex_retrieve}
|
|
699 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$
|
|
700 |
\end{lemma}
|
|
701 |
\begin{proof}
|
|
702 |
By induction on $s$. The induction tactic is reverse induction on strings.
|
|
703 |
$v$ is allowed to be arbitrary.
|
|
704 |
The crucial point is to rewrite
|
|
705 |
\[
|
|
706 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
707 |
\]
|
|
708 |
as
|
|
709 |
\[
|
|
710 |
\retrieve \; (r \backslash s) \; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash s@[c]))
|
|
711 |
\].
|
|
712 |
This enables us to equate
|
|
713 |
\[
|
|
714 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
715 |
\]
|
|
716 |
with
|
|
717 |
\[
|
|
718 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c])))
|
|
719 |
\],
|
|
720 |
which in turn can be rewritten as
|
|
721 |
\[
|
|
722 |
\flex \; r \; \textit{id} \; s@[c] \; (\mkeps (r\backslash s@[c]))
|
|
723 |
\].
|
|
724 |
\end{proof}
|
|
725 |
|
|
726 |
With the above lemma we can now link $\flex$ and $\blexer$.
|
|
727 |
|
542
|
728 |
%----------------------------------------------------------------------------------------
|
|
729 |
% SECTION correctness proof
|
|
730 |
%----------------------------------------------------------------------------------------
|
|
731 |
\section{Correctness of Bit-coded Algorithm (Without Simplification)}
|
|
732 |
We now give the proof the correctness of the algorithm with bit-codes.
|
|
733 |
|
532
|
734 |
\begin{lemma}\label{flex_blexer}
|
|
735 |
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s) = \blexer \; r \; s$
|
|
736 |
\end{lemma}
|
|
737 |
\begin{proof}
|
|
738 |
Using two of the above lemmas: \ref{flex_retrieve} and \ref{blexer_retrieve}.
|
|
739 |
\end{proof}
|
542
|
740 |
Finally the correctness of $\blexer$ is given as it outputs the same result as $\lexer$:
|
|
741 |
\begin{theorem}
|
|
742 |
$\blexer\; r \; s = \lexer \; r \; s$
|
|
743 |
\end{theorem}
|
|
744 |
\begin{proof}
|
|
745 |
Straightforward corollary of \ref{flex_blexer}.
|
|
746 |
\end{proof}
|
|
747 |
\noindent
|
|
748 |
Our main reason for wanting a bit-coded algorithm over
|
|
749 |
the injection-based one is for its capabilities of allowing
|
|
750 |
more aggressive simplifications.
|
|
751 |
We will elaborate on this in the next chapter.
|
532
|
752 |
|
|
753 |
|