author | Chengsong |
Tue, 25 Jul 2023 17:28:29 +0100 | |
changeset 667 | 660cf698eb26 |
parent 657 | 00171b627b8d |
child 668 | 3831621d7b14 |
permissions | -rwxr-xr-x |
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 |
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
649
diff
changeset
|
12 |
introduced by Sulzmann and Lu \parencite{Sulzmann2014} and their correctness proof. |
667 | 13 |
Just like in chapter \ref{Inj}, the algorithms and proofs have been included |
14 |
for self-containedness reasons, |
|
15 |
even though they have been originally found and described by |
|
16 |
Sulzmann and Lu (\cite{Sulzmann2014}) and |
|
17 |
Ausaf et al. (\cite{AusafDyckhoffUrban2016} and \cite{Ausaf}). |
|
18 |
%In addition to this, the |
|
19 |
The details of the proofs in this thesis |
|
20 |
also follow more closely the actual Isabelle formalisation. |
|
21 |
For example, lemma \ref{flexStepwise} and \ref{retrieveStepwise} |
|
22 |
are not included in the publications by Ausaf et al., despite them being |
|
23 |
some of the key lemmas leading to the correctness result. |
|
24 |
||
25 |
We will first motivate the bit-coded algorithm in section \ref{bitMotivate}, |
|
26 |
and then introduce their formal definitions in section \ref{bitDef}, |
|
27 |
followed by a description of the correctness proof of $\blexer$ in section \ref{blexerProof}. |
|
28 |
||
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
649
diff
changeset
|
29 |
%to address the growth problem of |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
649
diff
changeset
|
30 |
%derivatives of |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
649
diff
changeset
|
31 |
%regular expressions. |
667 | 32 |
%We have implemented their algorithm in Scala and Isabelle, |
33 |
%and found problems |
|
34 |
%in their algorithm, such as de-duplication not working properly and redundant |
|
35 |
%fixpoint construction. |
|
36 |
\section{The Motivation Behind Using Bitcodes}\label{bitMotivate} |
|
624 | 37 |
Let us give again the definition of $\lexer$ from Chapter \ref{Inj}: |
579 | 38 |
\begin{center} |
39 |
\begin{tabular}{lcl} |
|
40 |
$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\; \Some(\mkeps \; r) \; \textit{else} \; \None$\\ |
|
41 |
$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\ |
|
42 |
& & $\quad \phantom{\mid}\; \None \implies \None$\\ |
|
43 |
& & $\quad \mid \Some(v) \implies \Some(\inj \; r\; c\; v)$ |
|
44 |
\end{tabular} |
|
45 |
\end{center} |
|
46 |
\noindent |
|
624 | 47 |
The first problem with this algorithm is that |
48 |
for the function $\inj$ to work properly |
|
49 |
we cannot destroy the structure of a regular expression, |
|
638 | 50 |
and therefore cannot simplify too aggressively. |
624 | 51 |
For example, |
52 |
\[ |
|
53 |
r + (r + a) \rightarrow r + a |
|
54 |
\] |
|
638 | 55 |
cannot be applied because that would require |
624 | 56 |
breaking up the inner alternative. |
638 | 57 |
The $\lexer$ plus $\textit{simp}$ therefore only enables |
624 | 58 |
same-level de-duplications like |
59 |
\[ |
|
60 |
r + r \rightarrow r. |
|
61 |
\] |
|
62 |
Secondly, the algorithm recursively calls $\lexer$ on |
|
579 | 63 |
each new character input, |
64 |
and before starting a child call |
|
65 |
it stores information of previous lexing steps |
|
66 |
on a stack, in the form of regular expressions |
|
67 |
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc. |
|
68 |
Each descent into deeper recursive calls in $\lexer$ |
|
69 |
causes a new pair of $r_i, c_i$ to be pushed to the call stack. |
|
580 | 70 |
\begin{figure}[H] |
579 | 71 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
72 |
%\draw (-6,-6) grid (6,6); |
|
73 |
\node [ circle ] (r) at (-6, 5) {$r$}; |
|
74 |
||
75 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
76 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$}; |
|
77 |
% |
|
78 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
|
79 |
\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$}; |
|
80 |
||
81 |
\node [minimum width = 2cm, rectangle, draw] (stack) at (0, 3) {Stack}; |
|
82 |
||
83 |
\path |
|
84 |
(r) |
|
85 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
86 |
||
87 |
\path (r1) |
|
88 |
edge [bend right, dashed] node {saved} (stack); |
|
89 |
\path (c1) |
|
90 |
edge [bend right, dashed] node {} (stack); |
|
91 |
||
92 |
||
93 |
\end{tikzpicture} |
|
638 | 94 |
\caption{First derivative taken} |
579 | 95 |
\end{figure} |
96 |
||
97 |
||
98 |
||
580 | 99 |
\begin{figure}[H] |
579 | 100 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
101 |
%\draw (-6,-6) grid (6,6); |
|
102 |
\node [ circle ] (r) at (-6, 5) {$r$}; |
|
103 |
||
104 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
105 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$}; |
|
106 |
% |
|
107 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
|
108 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$}; |
|
109 |
||
110 |
||
111 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$}; |
|
112 |
% |
|
113 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$}; |
|
114 |
\node [circle, draw] (r2) at (2, 5) {$r_2$}; |
|
115 |
\node [minimum width = 3cm, minimum height = 1cm, rectangle, draw] (stack) at (0, 2) {\large Stack}; |
|
116 |
||
117 |
\path |
|
118 |
(r) |
|
119 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
120 |
||
121 |
\path (r2) |
|
122 |
edge [bend right, dashed] node {} (stack); |
|
123 |
\path (c2) |
|
124 |
edge [bend right, dashed] node {} (stack); |
|
125 |
||
126 |
\path (r1) |
|
127 |
edge [] node {} (r2); |
|
128 |
||
129 |
\end{tikzpicture} |
|
638 | 130 |
\caption{Second derivative taken} |
579 | 131 |
\end{figure} |
132 |
\noindent |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
133 |
As the number of derivative steps increases, |
579 | 134 |
the stack would increase: |
135 |
||
580 | 136 |
\begin{figure}[H] |
579 | 137 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
138 |
%\draw (-6,-6) grid (6,6); |
|
139 |
\node [ circle ] (r) at (-6, 5) {$r$}; |
|
140 |
||
141 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
142 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$}; |
|
143 |
% |
|
144 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
|
145 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$}; |
|
146 |
||
147 |
||
148 |
\node [circle, minimum size = 0.1, ] (c2) at (0, 5.4) {$c_2$}; |
|
149 |
% |
|
150 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$}; |
|
151 |
\node [circle, ] (r2) at (2, 5) {$r_2$}; |
|
152 |
\node [minimum width = 4cm, minimum height = 2.5cm, rectangle, draw] (stack) at (0, 1) { \large Stack}; |
|
153 |
||
154 |
\node [] (ldots) at (3.5, 5) {}; |
|
155 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$}; |
|
156 |
||
157 |
\node [minimum size = 0.5, circle, ] (rn) at (6, 5) {}; |
|
158 |
||
159 |
\node (rldots) at ($(ldots)!.4!(rn)$) {\ldots}; |
|
160 |
||
161 |
\path |
|
162 |
(r) |
|
163 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
164 |
||
165 |
\path (rldots) |
|
166 |
edge [bend left, dashed] node {} (stack); |
|
167 |
||
168 |
\path (r1) |
|
169 |
edge [] node {} (r2); |
|
170 |
||
171 |
\path (r2) |
|
172 |
edge [] node {} (ldots); |
|
173 |
\path (ldots) |
|
174 |
edge [bend left, dashed] node {} (stack); |
|
175 |
\path (5.03, 4.9) |
|
176 |
edge [bend left, dashed] node {} (stack); |
|
177 |
\end{tikzpicture} |
|
638 | 178 |
\caption{More derivatives taken} |
579 | 179 |
\end{figure} |
580 | 180 |
|
579 | 181 |
|
580 | 182 |
\begin{figure}[H] |
579 | 183 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
184 |
%\draw (-6,-6) grid (6,6); |
|
185 |
\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$}; |
|
186 |
||
187 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
188 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$}; |
|
189 |
% |
|
190 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
|
191 |
\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$}; |
|
192 |
% |
|
193 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$}; |
|
194 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$}; |
|
195 |
% |
|
196 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$}; |
|
197 |
\node [circle, draw] (r2) at (2, 5) {$r_2$}; |
|
198 |
% |
|
199 |
% |
|
200 |
\node [] (ldots) at (4.5, 5) {}; |
|
201 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$}; |
|
202 |
||
203 |
\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$}; |
|
204 |
||
205 |
\node at ($(ldots)!.4!(rn)$) {\ldots}; |
|
206 |
||
207 |
||
208 |
||
209 |
||
210 |
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack}; |
|
211 |
||
212 |
\path |
|
213 |
(r) |
|
214 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
215 |
\path |
|
216 |
(r1) |
|
217 |
edge [] node {} (r2); |
|
218 |
\path (r2) |
|
219 |
edge [] node {} (ldots); |
|
220 |
\path (r) |
|
221 |
edge [dashed, bend right] node {} (stack); |
|
222 |
||
223 |
\path (r1) |
|
224 |
edge [dashed, ] node {} (stack); |
|
225 |
||
226 |
\path (c1) |
|
227 |
edge [dashed, bend right] node {} (stack); |
|
228 |
||
229 |
\path (c2) |
|
230 |
edge [dashed] node {} (stack); |
|
231 |
\path (4.5, 5) |
|
232 |
edge [dashed, bend left] node {} (stack); |
|
233 |
\path (4.9, 5) |
|
234 |
edge [dashed, bend left] node {} (stack); |
|
235 |
\path (5.3, 5) |
|
236 |
edge [dashed, bend left] node {} (stack); |
|
237 |
\path (r2) |
|
238 |
edge [dashed, ] node {} (stack); |
|
239 |
\path (rn) |
|
240 |
edge [dashed, bend left] node {} (stack); |
|
241 |
\end{tikzpicture} |
|
638 | 242 |
\caption{Before injection phase starts} |
580 | 243 |
\end{figure} |
244 |
||
245 |
||
246 |
\noindent |
|
247 |
After all derivatives have been taken, the stack grows to a maximum size |
|
248 |
and the pair of regular expressions and characters $r_i, c_{i+1}$ |
|
249 |
are then popped out and used in the injection phase. |
|
250 |
||
251 |
||
252 |
||
253 |
\begin{figure}[H] |
|
254 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
|
255 |
%\draw (-6,-6) grid (6,6); |
|
256 |
\node [radius = 0.5, circle, ] (r) at (-6, 5) {$r$}; |
|
257 |
||
258 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
259 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$}; |
|
579 | 260 |
% |
580 | 261 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
262 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$}; |
|
579 | 263 |
% |
580 | 264 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$}; |
265 |
\node [circle, minimum size = 0.1, ] (c2) at (0, 5.4) {$c_2$}; |
|
579 | 266 |
% |
580 | 267 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$}; |
268 |
\node [circle, ] (r2) at (2, 5) {$r_2$}; |
|
579 | 269 |
% |
270 |
% |
|
580 | 271 |
\node [] (ldots) at (4.5, 5) {}; |
272 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$}; |
|
273 |
||
274 |
\node [minimum size = 0.5, circle, ] (rn) at (6, 5) {$r_n$}; |
|
275 |
||
276 |
\node at ($(ldots)!.4!(rn)$) {\ldots}; |
|
277 |
||
278 |
\node [minimum size = 0.5, circle, ] (vn) at (6, -5) {$v_n$}; |
|
279 |
||
280 |
\node [] (ldots2) at (3.5, -5) {}; |
|
281 |
||
282 |
\node [minimum size = 0.5, circle, ] (v2) at (2, -5) {$v_2$}; |
|
283 |
||
284 |
\node at ($(ldots2)!.4!(v2)$) {\ldots}; |
|
285 |
||
286 |
||
287 |
\node [circle, ] (v1) at (-2, -5) {$v_1$}; |
|
288 |
||
289 |
\node [radius = 0.5, circle, ] (v) at (-6, -5) {$v$}; |
|
290 |
||
291 |
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack}; |
|
292 |
||
293 |
\path |
|
294 |
(r) |
|
295 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
296 |
\path |
|
297 |
(r1) |
|
298 |
edge [] node {} (r2); |
|
299 |
\path (r2) |
|
300 |
edge [] node {} (ldots); |
|
301 |
\path (rn) |
|
302 |
edge [] node {$\mkeps$} (vn); |
|
303 |
\path (vn) |
|
304 |
edge [] node {} (ldots2); |
|
305 |
\path (v2) |
|
306 |
edge [] node {$\inj \; r_1 \; c_2\;v_2$} (v1); |
|
307 |
||
308 |
\path (v1) |
|
309 |
edge [] node {$\inj \; r \; c_1 \; v_1$} (v); |
|
310 |
||
311 |
\path (stack) |
|
312 |
edge [dashed] node {} (-4.2, -5.2); |
|
313 |
\path (stack) |
|
314 |
edge [dashed] node {} (-4, -5.2); |
|
315 |
\path (stack) |
|
316 |
edge [dashed] node {} (-0.1, -5.2); |
|
317 |
\path (stack) |
|
318 |
edge [dashed] node {} (0.2, -5.26); |
|
319 |
\path (stack) |
|
320 |
edge [dashed] node {} (3.2, -5); |
|
321 |
\path (stack) |
|
322 |
edge [dashed] node {} (2.7, -5); |
|
323 |
\path (stack) |
|
324 |
edge [dashed] node {} (3.7, -5); |
|
325 |
\end{tikzpicture} |
|
326 |
\caption{Stored $r_i, c_{i+1}$ Used by $\inj$} |
|
327 |
\end{figure} |
|
328 |
\noindent |
|
329 |
Storing all $r_i, c_{i+1}$ pairs recursively |
|
330 |
allows the algorithm to work in an elegant way, at the expense of |
|
624 | 331 |
storing quite a bit of verbose information on the stack. |
332 |
The stack seems to grow at least quadratically with respect |
|
580 | 333 |
to the input (not taking into account the size bloat of $r_i$), |
624 | 334 |
which can be inefficient and prone to stack overflows. |
667 | 335 |
\section{Bitcoded Algorithm}\label{bitDef} |
580 | 336 |
To address this, |
624 | 337 |
Sulzmann and Lu defined a new datatype |
580 | 338 |
called \emph{annotated regular expression}, |
339 |
which condenses all the partial lexing information |
|
340 |
(that was originally stored in $r_i, c_{i+1}$ pairs) |
|
341 |
into bitcodes. |
|
581 | 342 |
The bitcodes are then carried with the regular |
343 |
expression, and augmented or moved around |
|
638 | 344 |
as the lexing proceeds. |
581 | 345 |
It becomes unnecessary |
346 |
to remember all the |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
347 |
intermediate expressions, but only the most recent one |
581 | 348 |
with this bit-carrying regular expression. |
580 | 349 |
Annotated regular expressions |
350 |
are defined as the following |
|
638 | 351 |
Isabelle datatype:\footnote{ We use subscript notation to indicate |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
352 |
that the bitcodes are auxiliary information that does not |
638 | 353 |
interfere with the structure of the regular expressions } |
580 | 354 |
\begin{center} |
355 |
\begin{tabular}{lcl} |
|
356 |
$\textit{a}$ & $::=$ & $\ZERO$\\ |
|
357 |
& $\mid$ & $_{bs}\ONE$\\ |
|
358 |
& $\mid$ & $_{bs}{\bf c}$\\ |
|
359 |
& $\mid$ & $_{bs}\sum\,as$\\ |
|
360 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\ |
|
361 |
& $\mid$ & $_{bs}a^*$ |
|
362 |
\end{tabular} |
|
363 |
\end{center} |
|
364 |
\noindent |
|
365 |
where $bs$ stands for bit-codes, $a$ for $\mathbf{a}$nnotated regular |
|
366 |
expressions and $as$ for lists of annotated regular expressions. |
|
638 | 367 |
The alternative constructor, written $\sum$, has been generalised to |
624 | 368 |
take a list of annotated regular expressions rather than just two. |
369 |
Why is it generalised? This is because when we analyse nested |
|
370 |
alternatives, there can be more than two elements at the same level |
|
580 | 371 |
after de-duplication, which can no longer be stored in a binary |
372 |
constructor. |
|
373 |
Bits and bitcodes (lists of bits) are defined as: |
|
374 |
\begin{center} |
|
375 |
$b ::= S \mid Z \qquad |
|
376 |
bs ::= [] \mid b::bs |
|
377 |
$ |
|
378 |
\end{center} |
|
379 |
\noindent |
|
380 |
We use $S$ and $Z$ rather than $1$ and $0$ is to avoid |
|
381 |
confusion with the regular expressions $\ZERO$ and $\ONE$. |
|
624 | 382 |
The idea is to use the bitcodes |
580 | 383 |
to indicate which choice was made at a certain point |
384 |
during lexing. |
|
385 |
For example, |
|
386 |
$(_{Z}a+_{S}b) \backslash a$ gives us |
|
624 | 387 |
$_{Z}\ONE + \ZERO$, where the $Z$ bitcode will |
580 | 388 |
later be used to decode that a left branch was |
638 | 389 |
selected at the time when the part $a+b$ was analysed by |
624 | 390 |
derivative. |
580 | 391 |
\subsection{A Bird's Eye View of the Bit-coded Lexer} |
624 | 392 |
Before we give the details of the functions and definitions |
580 | 393 |
related to our |
394 |
$\blexer$ (\emph{b}-itcoded lexer), we first provide a high-level |
|
624 | 395 |
view of the algorithm. |
580 | 396 |
\begin{figure}[H] |
397 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] |
|
398 |
%\draw (-6,-6) grid (6,6); |
|
399 |
||
400 |
\node [circle, draw] (r0) at (-6, 2) {$r$}; |
|
401 |
||
402 |
\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$_{bs}a$}; |
|
403 |
\path (r0) |
|
404 |
edge [] node {$\internalise$} (r); |
|
405 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$}; |
|
406 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$}; |
|
579 | 407 |
% |
580 | 408 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$}; |
409 |
\node [minimum size = 1.0cm, circle, draw] (r1) at (-2, 5) {$_{bs_1}a_1$}; |
|
410 |
% |
|
411 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$}; |
|
412 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$}; |
|
413 |
% |
|
414 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$}; |
|
415 |
\node [circle, draw, minimum size = 1.4cm] (r2) at (2, 5) {$_{bs_2}a_2$}; |
|
416 |
% |
|
417 |
% |
|
418 |
\node [] (ldots) at (4.5, 5) {}; |
|
419 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$}; |
|
420 |
||
421 |
\node [minimum size = 2.2cm, circle, draw] (rn) at (6, 5) {$_{bs_n}a_n$}; |
|
422 |
||
423 |
\node at ($(ldots)!.1!(rn)$) {\ldots}; |
|
424 |
||
425 |
\node [minimum size = 0.5, circle, ] (v) at (6, 2) {$v$}; |
|
426 |
||
427 |
%\node [] (v2) at (4, -5) {}; |
|
428 |
% |
|
429 |
%\node [draw, cross out] (ldots2) at (5, -5) {}; |
|
579 | 430 |
% |
580 | 431 |
%\node at ($(ldots2)!.4!(v2)$) {\ldots}; |
432 |
||
433 |
||
434 |
\node [align = center] (decode) at (6.6, 3.2) {$\bmkeps$\\$\decode$}; |
|
435 |
||
436 |
\path (c1) |
|
437 |
edge [dashed, bend left] node {} (r0); |
|
438 |
||
439 |
\path (c2) |
|
440 |
edge [dashed, bend left] node {} (r0); |
|
441 |
||
442 |
\path (r1) |
|
443 |
edge [dashed, bend right] node {} (r2); |
|
444 |
||
445 |
||
446 |
\path |
|
447 |
(r) |
|
448 |
edge [dashed, bend right] node[left] {} (r1); |
|
449 |
||
450 |
\path |
|
451 |
(r) |
|
452 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1); |
|
453 |
||
454 |
\path |
|
455 |
(r1) |
|
456 |
edge [] node {} (r2); |
|
457 |
\path (r2) |
|
458 |
edge [] node {} (ldots); |
|
459 |
\path (rn) |
|
460 |
edge [] node {} (v); |
|
461 |
||
462 |
\path (r0) |
|
463 |
edge [dashed, bend right] node {} (decode); |
|
464 |
%\path (v) |
|
465 |
%edge [] node {} (ldots2); |
|
579 | 466 |
|
467 |
||
468 |
||
580 | 469 |
\end{tikzpicture} |
624 | 470 |
\caption{A bird's eye view of $\blexer$. The $_{bsi}a_{i}$s stand |
471 |
for the annotated regular expressions in each derivative step. |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
472 |
The characters used in each derivative are written as $c_i$. |
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
473 |
The size of the derivatives typically increases during each derivative step. |
638 | 474 |
The process starts with an internalise phase and concludes with a decoding phase.} |
580 | 475 |
\end{figure} |
476 |
\noindent |
|
477 |
The plain regular expressions |
|
478 |
are first ``lifted'' to an annotated regular expression, |
|
624 | 479 |
with the function called \emph{internalise} ($r \rightarrow _{bs}a$). |
580 | 480 |
Then the annotated regular expression $_{bs}a$ will |
624 | 481 |
be transformed by successive derivatives with respect |
580 | 482 |
to the input stream of characters $c_1, c_2$ etc. |
483 |
Each time a derivative is taken, the bitcodes |
|
484 |
are moved around, discarded or augmented together |
|
485 |
with the regular expression they are attached to. |
|
486 |
After all input has been consumed, the |
|
487 |
bitcodes are collected by $\bmkeps$, |
|
488 |
which traverses the nullable part of the regular expression |
|
624 | 489 |
and collects the bitcodes along the way. |
580 | 490 |
The collected bitcodes are then $\decode$d with the guidance |
624 | 491 |
of the input regular expression $r$ ( $_{bs}a \rightarrow r$). |
580 | 492 |
The most notable improvements of $\blexer$ |
493 |
over $\lexer$ are |
|
494 |
\begin{itemize} |
|
495 |
\item |
|
496 |
||
638 | 497 |
the absence of the second injection phase. |
580 | 498 |
\item |
638 | 499 |
one does not need to store each pair of the |
580 | 500 |
intermediate regular expressions $_{bs_i}a_i$ and $c_{i+1}$. |
501 |
The previous annotated regular expression $_{bs_i}a_i$'s information is passed |
|
502 |
on without loss to its successor $_{bs_{i+1}}a_{i+1}$, |
|
624 | 503 |
and $c_{i+1}$'s information is stored in $L\;r$.\footnote{ |
504 |
which will be used during the decode phase, where we use $r$ as |
|
505 |
a source of information.} |
|
580 | 506 |
\item |
638 | 507 |
simplification works much smoother--one can maintain correctness |
624 | 508 |
while applying quite aggressive simplifications. |
580 | 509 |
\end{itemize} |
624 | 510 |
%In the next section we will |
511 |
%give the operations needed in $\blexer$, |
|
512 |
%with some remarks on the idea behind their definitions. |
|
580 | 513 |
\subsection{Operations in $\textit{Blexer}$} |
514 |
The first operation we define related to bit-coded regular expressions |
|
515 |
is how we move bits to the inside of regular expressions. |
|
624 | 516 |
This operation is called $\fuse$: |
580 | 517 |
\begin{center} |
518 |
\begin{tabular}{lcl} |
|
624 | 519 |
$\textit{fuse}\;bs \; (\ZERO)$ & $\dn$ & $\ZERO$\\ |
520 |
$\textit{fuse}\;bs\; (_{bs'})\ONE$ & $\dn$ & |
|
580 | 521 |
$_{bs @ bs'}\ONE$\\ |
624 | 522 |
$\textit{fuse}\;bs\;(_{bs'}{\bf c})$ & $\dn$ & |
580 | 523 |
$_{bs@bs'}{\bf c}$\\ |
624 | 524 |
$\textit{fuse}\;bs\;(_{bs'}\sum\textit{as})$ & $\dn$ & |
580 | 525 |
$_{bs@bs'}\sum\textit{as}$\\ |
624 | 526 |
$\textit{fuse}\;bs\; (_{bs'}a_1\cdot a_2)$ & $\dn$ & |
580 | 527 |
$_{bs@bs'}a_1 \cdot a_2$\\ |
624 | 528 |
$\textit{fuse}\;bs\;(_{bs'}a^*)$ & $\dn$ & |
580 | 529 |
$_{bs @ bs'}a^*$ |
530 |
\end{tabular} |
|
531 |
\end{center} |
|
532 |
||
533 |
\noindent |
|
624 | 534 |
With \emph{fuse} we are able to define the $\internalise$ function, |
535 |
written $(\_)^\uparrow$, |
|
580 | 536 |
that translates a ``standard'' regular expression into an |
537 |
annotated regular expression. |
|
538 |
This function will be applied before we start |
|
539 |
with the derivative phase of the algorithm. |
|
540 |
||
541 |
\begin{center} |
|
542 |
\begin{tabular}{lcl} |
|
543 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\ |
|
544 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\ |
|
545 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\ |
|
546 |
$(r_1 + r_2)^\uparrow$ & $\dn$ & |
|
547 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\, |
|
548 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\ |
|
549 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ & |
|
550 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\ |
|
551 |
$(r^*)^\uparrow$ & $\dn$ & |
|
552 |
$_{[]}(r^\uparrow)^*$\\ |
|
553 |
\end{tabular} |
|
554 |
\end{center} |
|
555 |
\noindent |
|
556 |
The opposite of $\textit{internalise}$ is |
|
624 | 557 |
$\erase$, where all bit-codes are removed, |
580 | 558 |
and the alternative operator $\sum$ for annotated |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
559 |
regular expressions is transformed into the binary version |
624 | 560 |
in (plain) regular expressions. This can be defined as follows: |
561 |
\begin{center}\label{eraseDef} |
|
580 | 562 |
\begin{tabular}{lcl} |
563 |
$\ZERO_\downarrow$ & $\dn$ & $\ZERO$\\ |
|
564 |
$( _{bs}\ONE )_\downarrow$ & $\dn$ & $\ONE$\\ |
|
565 |
$( _{bs}\mathbf{c} )_\downarrow$ & $\dn$ & $\mathbf{c}$\\ |
|
566 |
$( _{bs} a_1 \cdot a_2 )_\downarrow$ & $\dn$ & |
|
567 |
$ (a_1) _\downarrow \cdot (a_2) _\downarrow$\\ |
|
568 |
$( _{bs} [])_\downarrow $ & $\dn$ & $\ZERO $\\ |
|
569 |
$( _{bs} [a] )_\downarrow$ & $\dn$ & $a_\downarrow$\\ |
|
624 | 570 |
$(_{bs} \sum [a_1, \; a_2])_\downarrow$ & $\dn$ & $ (a_1) _\downarrow + ( a_2 ) _\downarrow $\\ |
580 | 571 |
$(_{bs} \sum (a :: as))_\downarrow$ & $\dn$ & $ a_\downarrow + \; (_{[]} \sum as)_\downarrow$\\ |
572 |
$( _{bs} a^* )_\downarrow$ & $\dn$ & $(a_\downarrow)^*$ |
|
573 |
\end{tabular} |
|
574 |
\end{center} |
|
575 |
\noindent |
|
638 | 576 |
Where we abbreviate the $\erase\; a$ operation |
577 |
as $(a)_\downarrow$, for conciseness. |
|
580 | 578 |
|
624 | 579 |
Determining whether an annotated regular expression |
638 | 580 |
contains the empty string in its language requires |
580 | 581 |
a dedicated function $\bnullable$. |
624 | 582 |
In our formalisation this function is defined by simply calling $\erase$ |
583 |
before $\nullable$. |
|
580 | 584 |
\begin{definition} |
585 |
$\bnullable \; a \dn \nullable \; (a_\downarrow)$ |
|
586 |
\end{definition} |
|
587 |
The function for collecting |
|
588 |
bitcodes from a |
|
589 |
(b)nullable regular expression is called $\bmkeps$. |
|
624 | 590 |
This function is a lifted version of the $\textit{mkeps}$ function, |
580 | 591 |
which follows the path $\mkeps$ takes to traverse the |
592 |
$\nullable$ branches when visiting a regular expression, |
|
624 | 593 |
but collects bitcodes instead of generating a value. |
580 | 594 |
\begin{center} |
595 |
\begin{tabular}{lcl} |
|
596 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\ |
|
597 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ & |
|
598 |
$\textit{if}\;\textit{bnullable}\,a$\\ |
|
599 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\ |
|
600 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\ |
|
601 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ & |
|
602 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\ |
|
603 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ & |
|
585 | 604 |
$bs \,@\, [S]$ |
580 | 605 |
\end{tabular} |
606 |
\end{center} |
|
607 |
\noindent |
|
624 | 608 |
This function, just like $\mkeps$, |
581 | 609 |
visits a regular expression tree respecting |
624 | 610 |
the POSIX rules. |
581 | 611 |
It traverses each child of the sequence regular expression |
612 |
from left to right and creates a bitcode by stitching |
|
613 |
together bitcodes obtained from the children expressions. |
|
614 |
In the case of alternative regular expressions, |
|
615 |
it looks for the leftmost |
|
616 |
$\nullable$ branch |
|
638 | 617 |
to visit and ignores the other siblings. |
581 | 618 |
%Whenever there is some bitcodes attached to a |
619 |
%node, it returns the bitcodes concatenated with whatever |
|
620 |
%child recursive calls return. |
|
621 |
The only time when $\bmkeps$ creates new bitcodes |
|
622 |
is when it completes a star's iterations by attaching a $S$ to the end of the bitcode |
|
624 | 623 |
list it returns. |
624 |
||
580 | 625 |
The bitcodes extracted by $\bmkeps$ need to be |
638 | 626 |
$\decode$d (with the guidance of a ``plain'' regular expression): |
580 | 627 |
%\begin{definition}[Bitdecoding of Values]\mbox{} |
628 |
\begin{center} |
|
629 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
|
630 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\ |
|
631 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\ |
|
632 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
633 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\; |
|
634 |
(\Left\,v, bs_1)$\\ |
|
635 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
636 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\; |
|
637 |
(\Right\,v, bs_1)$\\ |
|
638 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ & |
|
639 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\ |
|
640 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\ |
|
641 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\ |
|
585 | 642 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\ |
643 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & |
|
580 | 644 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
645 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\ |
|
646 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\ |
|
647 |
||
648 |
$\textit{decode}\,bs\,r$ & $\dn$ & |
|
649 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
650 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\; |
|
651 |
\textit{else}\;\textit{None}$ |
|
652 |
\end{tabular} |
|
653 |
\end{center} |
|
537 | 654 |
\noindent |
580 | 655 |
The function $\decode'$ returns a pair consisting of |
624 | 656 |
a partially decoded value and some leftover bit-list. |
580 | 657 |
The function $\decode'$ succeeds if the left-over |
658 |
bit-sequence is empty. |
|
624 | 659 |
|
660 |
%$\decode$ is terminating as $\decode'$ is terminating. |
|
661 |
%$\decode'$ is terminating |
|
662 |
%because at least one of $\decode'$'s parameters will go down in terms |
|
663 |
%of size.\\ |
|
580 | 664 |
The reverse operation of $\decode$ is $\code$. |
624 | 665 |
This function encodes a value into a bitcode by converting |
580 | 666 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
667 |
star iteration by $S$. $ Z$ marks the border where a star iteration |
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
668 |
terminates. |
580 | 669 |
This coding is lossy, as it throws away the information about |
670 |
characters, and does not encode the ``boundary'' between two |
|
671 |
sequence values. Moreover, with only the bitcode we cannot even tell |
|
624 | 672 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$, |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
673 |
but this will not be necessary for our correctness proof. |
580 | 674 |
\begin{center} |
675 |
\begin{tabular}{lcl} |
|
676 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\ |
|
677 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\ |
|
678 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\ |
|
679 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\ |
|
680 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\ |
|
681 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\ |
|
682 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\; |
|
683 |
code(\Stars\,vs)$ |
|
684 |
\end{tabular} |
|
685 |
\end{center} |
|
686 |
\noindent |
|
624 | 687 |
We can prove that given a value $v$ and regular expression $r$ |
580 | 688 |
with $\vdash v:r$, |
689 |
then we have the property that $\decode$ and $\code$ are |
|
690 |
reverse operations of one another: |
|
691 |
\begin{lemma} |
|
692 |
\[If \vdash v : r \; then \;\decode \; (\code \; v) \; r = \textit{Some}(v) \] |
|
693 |
\end{lemma} |
|
694 |
\begin{proof} |
|
695 |
By proving a more general version of the lemma, on $\decode'$: |
|
696 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \] |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
697 |
Then we obtain the property by setting $ds$ to be $[]$ and unfolding the $\decode$ definition. |
580 | 698 |
\end{proof} |
699 |
\noindent |
|
638 | 700 |
Now we define the central part of Sulzmann and Lu's second lexing algorithm, |
624 | 701 |
the $\bder$ function (which stands for \emph{b}itcoded-derivative): |
580 | 702 |
\begin{center} |
703 |
\begin{tabular}{@{}lcl@{}} |
|
704 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
705 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
706 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ & |
|
707 |
$\textit{if}\;c=d\; \;\textit{then}\; |
|
708 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\ |
|
709 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ & |
|
710 |
$_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\ |
|
711 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ & |
|
712 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
713 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\ |
|
714 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\ |
|
715 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\ |
|
716 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ & |
|
717 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot |
|
718 |
(_{[]}r^*))$ |
|
719 |
\end{tabular} |
|
720 |
\end{center} |
|
721 |
\noindent |
|
639 | 722 |
For $\bder \; c\; a$, we use the infix notation $a\backslash c$ for readability. |
624 | 723 |
The $\bder$ function tells us how regular expressions can be recursively traversed, |
580 | 724 |
where the bitcodes are augmented and carried around |
725 |
when a derivative is taken. |
|
726 |
We give the intuition behind some of the more involved cases in |
|
638 | 727 |
$\bder$. |
728 |
||
580 | 729 |
For example, |
730 |
in the \emph{star} case, |
|
624 | 731 |
a derivative of $_{bs}a^*$ means |
732 |
that one more star iteration needs to be taken. |
|
733 |
We therefore need to unfold it into a sequence, |
|
734 |
and attach an additional bit $Z$ to the front of $a \backslash c$ |
|
580 | 735 |
as a record to indicate one new star iteration is unfolded. |
736 |
||
737 |
\noindent |
|
738 |
\begin{center} |
|
739 |
\begin{tabular}{@{}lcl@{}} |
|
740 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ & |
|
741 |
$_{bs}(\underbrace{\textit{fuse}\, [Z] \; a\,\backslash c}_{\text{One more iteration}})\cdot |
|
742 |
(_{[]}a^*))$ |
|
743 |
\end{tabular} |
|
744 |
\end{center} |
|
745 |
||
746 |
\noindent |
|
747 |
This information will be recovered later by the $\decode$ function. |
|
624 | 748 |
%The intuition is that the bit $Z$ will be decoded at the right location, |
749 |
%because we accumulate bits from left to right (a rigorous proof will be given |
|
750 |
%later). |
|
580 | 751 |
|
752 |
%\begin{tikzpicture}[ > = stealth, % arrow head style |
|
753 |
% shorten > = 1pt, % don't touch arrow head to node |
|
754 |
% semithick % line style |
|
755 |
% ] |
|
756 |
% |
|
757 |
% \tikzstyle{every state}=[ |
|
758 |
% draw = black, |
|
759 |
% thin, |
|
760 |
% fill = cyan!29, |
|
761 |
% minimum size = 7mm |
|
762 |
% ] |
|
763 |
% \begin{scope}[node distance=1cm and 0cm, every node/.style=state] |
|
764 |
% \node (k) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
765 |
% {$bs$ |
|
766 |
% \nodepart{two} $a^*$ }; |
|
767 |
% \node (l) [below =of k, rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
768 |
% { $bs$ + [Z] |
|
769 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ }; |
|
770 |
% \end{scope} |
|
771 |
% \path[->] |
|
772 |
% (k) edge (l); |
|
773 |
%\end{tikzpicture} |
|
774 |
% |
|
775 |
%Pictorially the process looks like below. |
|
776 |
%Like before, the red region denotes |
|
777 |
%previous lexing information (stored as bitcodes in $bs$). |
|
778 |
||
779 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
780 |
% \begin{scope}[node distance=1cm] |
|
781 |
% \node (a) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
782 |
% {$bs$ |
|
783 |
% \nodepart{two} $a^*$ }; |
|
784 |
% \node (b) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
785 |
% { $bs$ + [Z] |
|
786 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ }; |
|
787 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
788 |
% \end{scope} |
|
789 |
%\end{tikzpicture} |
|
790 |
||
791 |
\noindent |
|
792 |
Another place the $\bder$ function differs |
|
624 | 793 |
from derivatives on regular expressions |
580 | 794 |
is the sequence case: |
795 |
\begin{center} |
|
796 |
\begin{tabular}{@{}lcl@{}} |
|
797 |
||
798 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ & |
|
799 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
800 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\ |
|
801 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\ |
|
802 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$ |
|
803 |
\end{tabular} |
|
804 |
\end{center} |
|
805 |
\noindent |
|
624 | 806 |
%We assume that $\bmkeps$ |
807 |
%correctly extracts the bitcode for how $a_1$ |
|
808 |
%matches the string prior to $c$ (more on this later). |
|
809 |
%The bitsequence $\textit{bs}$ which was initially |
|
810 |
%attached to the first element of the sequence |
|
811 |
%$a_1 \cdot a_2$, has now been |
|
812 |
%elevated to the top level of the $\sum$ constructor. |
|
813 |
%This is because this piece of information will be needed |
|
814 |
%whichever way the sequence is matched, |
|
815 |
%regardless of whether $c$ belongs to $a_1$ or $a_2$. |
|
816 |
The difference mainly lies in the $\textit{if}$ branch (when $a_1$ is $\bnullable$): |
|
580 | 817 |
we use $\bmkeps$ to store the lexing information |
818 |
in $a_1$ before collapsing |
|
819 |
it (as it has been fully matched by string prior to $c$), |
|
820 |
and attach the collected bit-codes to the front of $a_2$ |
|
624 | 821 |
before throwing away $a_1$. |
580 | 822 |
In the injection-based lexer, $r_1$ is immediately thrown away in |
638 | 823 |
the $\textit{if}$ branch, |
624 | 824 |
the information $r_1$ stores is therefore lost: |
580 | 825 |
\begin{center} |
624 | 826 |
\begin{tabular}{lcl} |
827 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\ |
|
828 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\ |
|
829 |
& & $\mathit{else}\; \ldots$\\ |
|
830 |
\end{tabular} |
|
580 | 831 |
\end{center} |
832 |
\noindent |
|
624 | 833 |
|
834 |
%\noindent |
|
835 |
%this loss of information makes it necessary |
|
836 |
%to store on stack all the regular expressions' |
|
837 |
%``snapshots'' before they were |
|
838 |
%taken derivative of. |
|
839 |
%So that the related information will be available |
|
840 |
%once the child recursive |
|
841 |
%call finishes.\\ |
|
580 | 842 |
The rest of the clauses of $\bder$ is rather similar to |
843 |
$\der$. \\ |
|
844 |
Generalising the derivative operation with bitcodes to strings, we have |
|
845 |
\begin{center} |
|
846 |
\begin{tabular}{@{}lcl@{}} |
|
847 |
$a\backslash_s [] $ & $\dn$ & $a$\\ |
|
848 |
$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$ |
|
849 |
\end{tabular} |
|
850 |
\end{center} |
|
851 |
\noindent |
|
852 |
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger |
|
853 |
of confusion. |
|
854 |
\subsection{Putting Things Together} |
|
855 |
Putting these operations altogether, we obtain a lexer with bit-coded regular expressions |
|
856 |
as its internal data structures, which we call $\blexer$: |
|
857 |
||
858 |
\begin{center} |
|
859 |
\begin{tabular}{lcl} |
|
860 |
$\textit{blexer}\;r\,s$ & $\dn$ & |
|
861 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\ |
|
862 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
863 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
864 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
865 |
\end{tabular} |
|
866 |
\end{center} |
|
867 |
\noindent |
|
624 | 868 |
This function first attaches bitcodes to a |
869 |
plain regular expression $r$, and then builds successive derivatives |
|
581 | 870 |
with respect to the input string $s$, and |
624 | 871 |
then test whether the result is (b)nullable. |
872 |
If yes, then extract the bitcodes from the nullable expression, |
|
581 | 873 |
and decodes the bitcodes into a lexical value. |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
874 |
If there does not exist a match between $r$ and $s$, the lexer |
638 | 875 |
outputs $\None$ indicating a mismatch. |
580 | 876 |
Ausaf and Urban formally proved the correctness of the $\blexer$, namely |
877 |
\begin{property} |
|
878 |
$\blexer \;r \; s = \lexer \; r \; s$. |
|
879 |
\end{property} |
|
581 | 880 |
\noindent |
580 | 881 |
This was claimed but not formalised in Sulzmann and Lu's work. |
882 |
We introduce the proof later, after we give all |
|
883 |
the needed auxiliary functions and definitions. |
|
581 | 884 |
\subsection{An Example $\blexer$ Run} |
624 | 885 |
Before introducing the proof we shall first walk |
580 | 886 |
through a concrete example of our $\blexer$ calculating the right |
638 | 887 |
lexical information through bit-coded regular expressions. |
888 |
||
581 | 889 |
Consider the regular expression $(aa)^* \cdot (b+c)$ matching the string $aab$. |
890 |
We give again the bird's eye view of this particular example |
|
891 |
in each stage of the algorithm: |
|
892 |
||
893 |
\tikzset{three sided/.style={ |
|
894 |
draw=none, |
|
895 |
append after command={ |
|
896 |
[-,shorten <= -0.5\pgflinewidth] |
|
897 |
([shift={(-1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
898 |
edge([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
899 |
([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
900 |
edge([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
901 |
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
902 |
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
903 |
} |
|
904 |
} |
|
905 |
} |
|
906 |
||
907 |
\tikzset{three sided1/.style={ |
|
908 |
draw=none, |
|
909 |
append after command={ |
|
910 |
[-,shorten <= -0.5\pgflinewidth] |
|
911 |
([shift={(1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
912 |
edge([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
913 |
([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
914 |
edge([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
915 |
([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
916 |
edge([shift={(1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
917 |
} |
|
918 |
} |
|
919 |
} |
|
920 |
||
921 |
\begin{figure}[H] |
|
922 |
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, auto, thick] |
|
923 |
\node [rectangle, draw] (r) at (-6, -1) {$(aa)^*(b+c)$}; |
|
924 |
\node [rectangle, draw] (a) at (-6, 4) {$(aa)^*(_{Z}b + _{S}c)$}; |
|
925 |
\path (r) |
|
926 |
edge [] node {$\internalise$} (a); |
|
927 |
\node [rectangle, draw] (a1) at (-3, 1) {$(_{Z}(\ONE \cdot a) \cdot (aa)^*) (_{Z}b + _Sc)$}; |
|
928 |
\path (a) |
|
929 |
edge [] node {$\backslash a$} (a1); |
|
930 |
||
931 |
\node [rectangle, draw, three sided] (a21) at (-2.5, 4) {$(_{Z}\ONE \cdot (aa)^*)$}; |
|
932 |
\node [rectangle, draw, three sided1] (a22) at (-0.8, 4) {$(_{Z}b + _{S}c)$}; |
|
933 |
\path (a1) |
|
934 |
edge [] node {$\backslash a$} (a21); |
|
935 |
\node [rectangle, draw] (a3) at (0.5, 2) {$_{ZS}(_{Z}\ONE + \ZERO)$}; |
|
936 |
\path (a22) |
|
937 |
edge [] node {$\backslash b$} (a3); |
|
938 |
\path (a21) |
|
939 |
edge [dashed, bend right] node {} (a3); |
|
940 |
\node [rectangle, draw] (bs) at (2, 4) {$ZSZ$}; |
|
941 |
\path (a3) |
|
942 |
edge [below] node {$\bmkeps$} (bs); |
|
943 |
\node [rectangle, draw] (v) at (3, 0) {$\Seq \; (\Stars\; [\Seq \; a \; a]) \; (\Left \; b)$}; |
|
944 |
\path (bs) |
|
945 |
edge [] node {$\decode$} (v); |
|
946 |
||
947 |
||
948 |
\end{tikzpicture} |
|
638 | 949 |
\caption{$\blexer$ with the regular expression $(aa)^*(b+c)$ and $aab$} |
581 | 950 |
\end{figure} |
537 | 951 |
\noindent |
581 | 952 |
The one dashed arrow indicates that $_Z(\ONE \cdot (aa)^*)$ |
953 |
turned into $ZS$ after derivative w.r.t $b$ |
|
638 | 954 |
is taken, which calls $\bmkeps$ on the nullable $_Z\ONE\cdot (aa)^*$ |
955 |
before processing $_Zb+_Sc$. |
|
956 |
||
581 | 957 |
The annotated regular expressions |
624 | 958 |
would look overwhelming if we explicitly indicate all the |
581 | 959 |
locations where bitcodes are attached. |
960 |
For example, |
|
961 |
$(aa)^*\cdot (b+c)$ would |
|
962 |
look like $_{[]}(_{[]}(_{[]}a \cdot _{[]}a)^*\cdot _{[]}(_{[]}b+_{[]}c))$ |
|
963 |
after |
|
964 |
internalise. |
|
965 |
Therefore for readability we omit bitcodes if they are empty. |
|
624 | 966 |
This applies to all annotated |
638 | 967 |
regular expressions in this thesis. |
968 |
||
581 | 969 |
%and assume we have just read the first character $a$: |
970 |
%\begin{center} |
|
971 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
972 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
973 |
% {$(_{[Z]}(\ONE \cdot a) \cdot (aa)^* )\cdot bc$ |
|
974 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq\; a \; ??, \;??]) \; ??$}; |
|
975 |
%\end{tikzpicture} |
|
976 |
%\end{center} |
|
977 |
%\noindent |
|
978 |
%We use the red area for (annotated) regular expressions and the blue |
|
979 |
%area the (partially calculated) bitcodes |
|
980 |
%and its corresponding (partial) values. |
|
981 |
%The first derivative |
|
982 |
%generates a $Z$ bitcode to indicate |
|
983 |
%a new iteration has been started. |
|
984 |
%This bitcode is attached to the front of |
|
985 |
%the unrolled star iteration $\ONE\cdot a$ |
|
986 |
%for later decoding. |
|
987 |
%\begin{center} |
|
988 |
%\begin{tikzpicture}[] |
|
989 |
% \node [rectangle split, rectangle split horizontal, |
|
990 |
% rectangle split parts=2, rectangle split part fill={red!30,blue!20}, draw, rounded corners, inner sep=10pt] |
|
991 |
% (der2) at (0,0) |
|
992 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot bc $ |
|
993 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$}; |
|
994 |
% |
|
995 |
%\node [draw=none, minimum size = 0.1, ] (r) at (-7, 0) {$a_1$}; |
|
996 |
%\path |
|
997 |
% (r) |
|
998 |
% edge [->, >=stealth',shorten >=1pt, above] node {$\backslash a$} (der2); |
|
999 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
1000 |
%\end{tikzpicture} |
|
1001 |
%\end{center} |
|
1002 |
%\noindent |
|
1003 |
%After we take derivative with respect to |
|
1004 |
%second input character $a$, the annotated |
|
1005 |
%regular expression has the second $a$ chopped off. |
|
1006 |
%The second derivative does not involve any |
|
1007 |
%new bitcodes being generated, because |
|
1008 |
%there are no new iterations or bifurcations |
|
1009 |
%in the regular expression requiring any $S$ or $Z$ marker |
|
1010 |
%to indicate choices. |
|
1011 |
%\begin{center} |
|
1012 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1013 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1014 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot (\ONE \cdot c) $ |
|
1015 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$}; |
|
1016 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
1017 |
%\end{tikzpicture} |
|
1018 |
%\end{center} |
|
1019 |
%\noindent |
|
1020 |
% |
|
1021 |
% |
|
1022 |
%\begin{center} |
|
1023 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1024 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1025 |
% {$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$ |
|
1026 |
% \nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$ $\stackrel{Inj}{\longleftarrow}$}; |
|
1027 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
1028 |
%\end{tikzpicture} |
|
1029 |
%\end{center} |
|
1030 |
%\noindent |
|
1031 |
%If we do this kind of "attachment" |
|
1032 |
%and each time augment the attached partially |
|
1033 |
%constructed value when taking off a |
|
1034 |
%character: |
|
1035 |
%\begin{center} |
|
1036 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1037 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint) |
|
1038 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$ |
|
1039 |
% \nodepart{two} Remaining: $b c$}; |
|
1040 |
%\end{tikzpicture}\\ |
|
1041 |
%$\downarrow$\\ |
|
1042 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1043 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1044 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$ |
|
1045 |
% \nodepart{two} Remaining: $c$}; |
|
1046 |
%\end{tikzpicture}\\ |
|
1047 |
%$\downarrow$\\ |
|
1048 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1049 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1050 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$ |
|
1051 |
% \nodepart{two} EOF}; |
|
1052 |
%\end{tikzpicture} |
|
1053 |
%\end{center} |
|
638 | 1054 |
|
581 | 1055 |
In the next section we introduce the correctness proof |
1056 |
found by Ausaf and Urban |
|
1057 |
of the bitcoded lexer. |
|
532 | 1058 |
%----------------------------------- |
1059 |
% SUBSECTION 1 |
|
1060 |
%----------------------------------- |
|
667 | 1061 |
\section{Correctness Proof of $\textit{Blexer}$}\label{blexerProof} |
581 | 1062 |
Why is $\blexer$ correct? |
1063 |
In other words, why is it the case that |
|
1064 |
$\blexer$ outputs the same value as $\lexer$? |
|
1065 |
Intuitively, |
|
1066 |
that is because |
|
1067 |
\begin{itemize} |
|
1068 |
\item |
|
1069 |
$\blexer$ follows an almost identical |
|
1070 |
path as that of $\lexer$, |
|
1071 |
for example $r_1, r_2, \ldots$ and $a_1, a_2, \ldots$ being produced, |
|
1072 |
which are the same up to the application of $\erase$. |
|
1073 |
\item |
|
1074 |
The bit-encodings work properly, |
|
1075 |
allowing the possibility of |
|
1076 |
pulling out the right lexical value from an |
|
1077 |
annotated regular expression at |
|
624 | 1078 |
any stage, using $\bmkeps$ or $\retrieve$ (details in |
1079 |
lemmas \ref{bmkepsRetrieve} and \ref{blexer_retrieve}). |
|
581 | 1080 |
\end{itemize} |
1081 |
We will elaborate on this, with the help of |
|
1082 |
some helper functions such as $\retrieve$ and |
|
1083 |
$\flex$. |
|
1084 |
\subsection{Specifications of Some Helper Functions} |
|
622 | 1085 |
The functions we introduce will give a more detailed view into |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1086 |
the lexing process, which is not possible |
581 | 1087 |
using $\lexer$ or $\blexer$ alone. |
1088 |
\subsubsection{$\textit{Retrieve}$} |
|
1089 |
The first function we shall introduce is $\retrieve$. |
|
1090 |
Sulzmann and Lu gave its definition, and |
|
638 | 1091 |
Ausaf and Urban found it |
624 | 1092 |
useful in their mechanised proofs. |
581 | 1093 |
Our bit-coded lexer ``retrieve''s the bitcodes using $\bmkeps$, |
624 | 1094 |
after all the derivatives have been taken: |
532 | 1095 |
\begin{center} |
542 | 1096 |
\begin{tabular}{lcl} |
1097 |
& & $\ldots$\\ |
|
1098 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
1099 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
1100 |
& & $\ldots$ |
|
532 | 1101 |
\end{tabular} |
1102 |
\end{center} |
|
542 | 1103 |
\noindent |
624 | 1104 |
The function $\bmkeps$ retrieves the value $v$'s |
581 | 1105 |
information in the format |
1106 |
of bitcodes, by travelling along the |
|
1107 |
path of the regular expression that corresponds to a POSIX match, |
|
1108 |
collecting all the bitcodes. |
|
542 | 1109 |
We know that this "retrieved" bitcode leads to the correct value after decoding, |
624 | 1110 |
which is $v_0$ in the injection-based lexing diagram: |
542 | 1111 |
\vspace{20mm} |
581 | 1112 |
\begin{figure}[H]%\label{graph:injLexer} |
1113 |
\begin{center} |
|
542 | 1114 |
\begin{tikzcd}[ |
1115 |
every matrix/.append style = {name=p}, |
|
1116 |
remember picture, overlay, |
|
1117 |
] |
|
1118 |
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] \\ |
|
1119 |
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] |
|
1120 |
\end{tikzcd} |
|
581 | 1121 |
\end{center} |
542 | 1122 |
\begin{tikzpicture}[ |
1123 |
remember picture, overlay, |
|
1124 |
E/.style = {ellipse, draw=blue, dashed, |
|
1125 |
inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1} |
|
1126 |
] |
|
1127 |
\node[E = (p-1-1) (p-2-1)] {}; |
|
1128 |
\node[E = (p-1-4) (p-2-4)] {}; |
|
581 | 1129 |
\node[E = (p-1-2) (p-2-2)] {}; |
1130 |
\node[E = (p-1-3) (p-2-3)] {}; |
|
542 | 1131 |
\end{tikzpicture} |
624 | 1132 |
\caption{Injection-based lexing diagram, |
1133 |
with matching value and regular expression pairs |
|
1134 |
encircled}\label{Inj2} |
|
581 | 1135 |
\end{figure} |
542 | 1136 |
\vspace{20mm} |
1137 |
\noindent |
|
624 | 1138 |
We have that $\vdash v_i:(a_i)_\downarrow$, |
1139 |
where $v_i$ are the intermediate values generated step by step in $\lexer$. |
|
1140 |
These values are not explicitly created in $\blexer$. |
|
1141 |
||
1142 |
We encircled in the diagram all the pairs $v_i, a_i$ to show that these values |
|
582 | 1143 |
and regular expressions share the same structure. |
1144 |
These pairs all contain adequate information to |
|
1145 |
obtain the final lexing result. |
|
1146 |
For example, in the leftmost pair the |
|
581 | 1147 |
lexical information is condensed in |
582 | 1148 |
$v_0$, whereas for the rightmost pair, |
638 | 1149 |
the lexing result hides is in the bitcodes of $a_n$. |
1150 |
||
624 | 1151 |
The function $\bmkeps$ is able to extract from $a_n$ the result |
582 | 1152 |
by looking for nullable parts of the regular expression. |
1153 |
However for regular expressions $a_i$ in general, |
|
1154 |
they might not necessarily be nullable. |
|
1155 |
For those regular expressions that are not nullable, |
|
1156 |
$\bmkeps$ will not work. |
|
1157 |
Therefore they need some additional ``help'' |
|
1158 |
finding the POSIX bit-encoding. |
|
581 | 1159 |
The most straightforward ``help'' comes from $a_i$'s corresponding |
1160 |
value $v_i$, and this suggests a function $f$ satisfying the |
|
1161 |
following properties: |
|
542 | 1162 |
\begin{itemize} |
1163 |
\item |
|
581 | 1164 |
$f \; a_i\;v_i = f \; a_n \; v_n = \bmkeps \; a_n$%\decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$ |
542 | 1165 |
\item |
582 | 1166 |
$f \; a_i\;v_i = f \; a_0 \; v_0 = \code \; v_0$ %\decode \;(\code \; v_0) \; (\erase \; a_0)$ |
542 | 1167 |
\end{itemize} |
1168 |
\noindent |
|
624 | 1169 |
Sulzmann and Lu came up with the following definition for $f$ satisfying |
582 | 1170 |
the above |
624 | 1171 |
requirements, and named it \emph{retrieve}: |
1172 |
\vspace{-7mm} |
|
542 | 1173 |
\begin{center} |
582 | 1174 |
\begin{tabular}{llcl} |
1175 |
$\retrieve \; \, _{bs} \ONE$ & $ \Empty$ & $\dn$ & $\textit{bs}$\\ |
|
1176 |
||
1177 |
$\retrieve \; \, _{bs} \mathbf{c}$ & $ (\Char \; c) $ & $\dn$ & $ \textit{bs}$\\ |
|
1178 |
||
1179 |
$\retrieve \; \, _{bs} a_1 \cdot a_2$ & $ (\Seq \; v_1 \; v_2) $ & |
|
1180 |
$\dn$ & $\textit{bs} \; @\; (\retrieve \; a_1\; v_1)\; @ \;(\retrieve \; a_2 \; v_2)$\\ |
|
1181 |
||
1182 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Left \; v) $ & |
|
1183 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; a \; v)$\\ |
|
1184 |
||
1185 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Right \; v) $ & |
|
1186 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\ |
|
1187 |
||
1188 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; (v :: vs)) $ & $\dn$ & |
|
585 | 1189 |
$\textit{bs}\; @\; [Z] \; @ \; (\retrieve \; a \; v)\; @ \; |
582 | 1190 |
(\retrieve \; (_{[]} a^*) \; (\Stars \; vs) )$\\ |
1191 |
||
585 | 1192 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; []) $ & $\dn$ & $\textit{bs} \; @ \; [S]$ |
542 | 1193 |
\end{tabular} |
1194 |
\end{center} |
|
1195 |
\noindent |
|
624 | 1196 |
As stated, $\retrieve$ collects the right bit-codes from the |
1197 |
final derivative $a_n$, guided by $v_n$. This can be proved |
|
1198 |
as follows: |
|
582 | 1199 |
\begin{lemma}\label{bmkepsRetrieve} |
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1200 |
\mbox{} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1201 |
\begin{itemize} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1202 |
\item |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1203 |
If $\forall v \in vs. \vdash v : r$, and $\code \; v = \retrieve \; (\internalise\; r) \; v$\\ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1204 |
then $\code \; (\Stars \; vs) = \retrieve \; _{[]}((\internalise \; r)^*) \; (\Stars \; vs)$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1205 |
\item |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1206 |
$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1207 |
\item |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1208 |
$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (a_\downarrow))$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1209 |
\end{itemize} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1210 |
\begin{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1211 |
The first part is by induction on the value list $vs$. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1212 |
The second part is by induction on $r$, |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1213 |
and the star case uses the first part. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1214 |
The last part is by a routine induction on $a$. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1215 |
\end{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1216 |
\noindent |
542 | 1217 |
\end{lemma} |
1218 |
\begin{proof} |
|
1219 |
By a routine induction on $a$. |
|
1220 |
\end{proof} |
|
582 | 1221 |
\noindent |
624 | 1222 |
%The design of $\retrieve$ enables us to extract bitcodes |
1223 |
%from both annotated regular expressions and values. |
|
1224 |
%$\retrieve$ always ``sucks up'' all the information. |
|
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1225 |
%When more information is stored in the value, we would be able to |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1226 |
%extract more from the value, and vice versa. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1227 |
%For example in star iterations, $\retrieve$ will be able to extract from $\Stars \; vs$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1228 |
%exactly the same bitcodes as $\code \; (\Stars \; vs)$: |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1229 |
%\begin{lemma}\label{retrieveEncodeSTARS} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1230 |
% If $\forall v \in vs. \vdash v : r$, and $\code \; v = \retrieve \; (\internalise\; r) \; v$\\ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1231 |
% then $\code \; (\Stars \; vs) = \retrieve \; _{[]}((\internalise \; r)^*) \; (\Stars \; vs)$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1232 |
%\end{lemma} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1233 |
%\begin{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1234 |
% By induction on the value list $vs$. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1235 |
%\end{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1236 |
%\noindent |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1237 |
%Similarly, when the value list does not hold information, |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1238 |
%only the bitcodes plus some delimiter will be recorded: |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1239 |
%\begin{center} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1240 |
% $\retrieve \; _{bs}((\internalise \; r)^*) \; (\Stars \; [] ) = bs @ [S]$. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1241 |
%\end{center} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1242 |
%In general, if we have a regular expression that is just internalised |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1243 |
%and the final lexing result value, we can $\retrieve$ the bitcodes |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1244 |
%that look as if we have en$\code$-ed the value as bitcodes: |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1245 |
%\begin{lemma} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1246 |
% $\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1247 |
%\end{lemma} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1248 |
%\begin{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1249 |
% By induction on $r$. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1250 |
% The star case uses lemma \ref{retrieveEncodeSTARS}. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1251 |
%\end{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1252 |
%\noindent |
624 | 1253 |
The following property of $\retrieve$ is crucial, as |
582 | 1254 |
it provides a "bridge" between $a_0$ and $a_n $ in the |
1255 |
lexing diagram by linking adjacent regular expressions $a_i$ and |
|
1256 |
$a_{i+1}$. |
|
1257 |
The property says that one |
|
1258 |
can retrieve the same bits from a derivative |
|
1259 |
regular expression as those from |
|
1260 |
before the derivative took place, |
|
624 | 1261 |
provided that the corresponding values are used respectively: |
542 | 1262 |
\begin{lemma}\label{retrieveStepwise} |
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1263 |
$\vdash v : (a\backslash c)_\downarrow \implies \retrieve \; (a \backslash c) \; v= |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1264 |
\retrieve \; a \; (\inj \; a_\downarrow\; c\; v)$ |
542 | 1265 |
\end{lemma} |
1266 |
\begin{proof} |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1267 |
We do an induction on $r$, generalising over $v$. |
624 | 1268 |
The induction principle in our formalisation |
1269 |
is function $\erase$'s cases. |
|
542 | 1270 |
\end{proof} |
1271 |
\noindent |
|
649 | 1272 |
Intuitively the lemma can be understood as all lexical value |
1273 |
information being preserved and recoverable throughout each lexing step. |
|
1274 |
We shall see in the next chapter that this no longer |
|
1275 |
holds with simplifications which takes away certain sub-expressions |
|
1276 |
corresponding to non-POSIX lexical values. |
|
1277 |
||
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1278 |
%Before we move on to the next |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1279 |
%helper function, we rewrite $\blexer$ in |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1280 |
%the following way which makes later proofs more convenient: |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1281 |
%\begin{lemma}\label{blexer_retrieve} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1282 |
%$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1283 |
%\end{lemma} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1284 |
%\begin{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1285 |
% Using lemma \ref{bmkepsRetrieve}. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1286 |
%\end{proof} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1287 |
%\noindent |
624 | 1288 |
The function $\retrieve$ allows connecting |
582 | 1289 |
between the intermediate |
1290 |
results $a_i$ and $a_{i+1}$ in $\blexer$. |
|
1291 |
For plain regular expressions something similar is required. |
|
542 | 1292 |
|
638 | 1293 |
\subsection{The Function $\flex$} |
624 | 1294 |
Ausaf and Urban defined an auxiliary function called $\flex$ for $\lexer$, |
532 | 1295 |
defined as |
536 | 1296 |
\begin{center} |
582 | 1297 |
\begin{tabular}{lcl} |
536 | 1298 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\ |
582 | 1299 |
$\flex \; r \; f \; (c :: s) \; v$ & $=$ & $\flex \; r \; (\lambda v. \, f (\inj \; r\; c\; v)) \;\; s \; v$ |
536 | 1300 |
\end{tabular} |
1301 |
\end{center} |
|
582 | 1302 |
which accumulates the characters that need to be injected back, |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1303 |
and does the injection in a stack-like manner (the last character being chopped off in the derivatives phase is first injected). |
624 | 1304 |
The function |
582 | 1305 |
$\flex$ can calculate what $\lexer$ calculates, given the input regular expression |
1306 |
$r$, the identity function $id$, the input string $s$ and the value |
|
1307 |
$v_n= \mkeps \; (r\backslash s)$: |
|
532 | 1308 |
\begin{lemma} |
582 | 1309 |
$\flex \;\; r \;\; \textit{id}\;\; s \;\; (\mkeps \;(r\backslash s)) = \lexer \; r \; s$ |
532 | 1310 |
\end{lemma} |
542 | 1311 |
\begin{proof} |
1312 |
By reverse induction on $s$. |
|
1313 |
\end{proof} |
|
582 | 1314 |
\noindent |
1315 |
The main advantage of using $\flex$ |
|
624 | 1316 |
in $\lexer$ is that |
582 | 1317 |
$\flex$ makes the value $v$ and function $f$ |
1318 |
that manipulates the value explicit parameters, |
|
1319 |
which ``exposes'' $\lexer$'s recursive call |
|
1320 |
arguments and allows us to ``set breakpoints'' and ``resume'' |
|
624 | 1321 |
at any point during $\lexer$'s recursive calls. Therefore |
1322 |
the following stepwise property holds. |
|
532 | 1323 |
\begin{lemma}\label{flexStepwise} |
582 | 1324 |
$\textit{flex} \;\; r \;\; f \;\; (s@[c]) \;\; v = \flex \;\; r \;\; f \;\; s \;\; (\inj \; (r\backslash s) \; c \; v) $ |
532 | 1325 |
\end{lemma} |
1326 |
\begin{proof} |
|
582 | 1327 |
By induction on the shape of $r\backslash s$. |
532 | 1328 |
\end{proof} |
582 | 1329 |
\noindent |
1330 |
With $\flex$ and $\retrieve$, |
|
1331 |
we are ready to connect $\lexer$ and $\blexer$, |
|
624 | 1332 |
giving the correctness proof for $\blexer$. |
532 | 1333 |
|
542 | 1334 |
%---------------------------------------------------------------------------------------- |
1335 |
% SECTION correctness proof |
|
1336 |
%---------------------------------------------------------------------------------------- |
|
624 | 1337 |
\section{Correctness of the Bit-coded Algorithm (Without Simplification)} |
1338 |
We can first show that |
|
638 | 1339 |
$\flex$ and $\blexer$ calculates the same value. |
582 | 1340 |
\begin{lemma}\label{flex_retrieve} |
1341 |
If $\vdash v: (r\backslash s)$, then\\ |
|
1342 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$ |
|
532 | 1343 |
\end{lemma} |
1344 |
\begin{proof} |
|
582 | 1345 |
By induction on $s$. |
1346 |
We prove the interesting case where |
|
1347 |
both $\flex$ and $\decode$ successfully terminates |
|
1348 |
with some value. |
|
1349 |
We take advantage of the stepwise properties |
|
1350 |
both sides. |
|
1351 |
The induction tactic is reverse induction on string $s$. |
|
1352 |
The inductive hypothesis says that $\flex \; r \; id \;s \; v = |
|
1353 |
\decode \; (\retrieve \; (r\backslash s) \; v) \; r$ holds, |
|
1354 |
where $v$ can be any value satisfying |
|
1355 |
the assumption $\vdash v: (r\backslash s)$. |
|
1356 |
The crucial point is to rewrite |
|
1357 |
\[ |
|
1358 |
\retrieve \;\; (r \backslash (s@[c])) \;\; (\mkeps\; (r \backslash (s@[c]) )) |
|
1359 |
\] |
|
1360 |
as |
|
1361 |
\[ |
|
1362 |
\retrieve \;\; (r \backslash s) |
|
1363 |
\;\; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash (s@[c]))) |
|
1364 |
\] |
|
1365 |
using lemma \ref{retrieveStepwise}. |
|
1366 |
This enables us to equate |
|
1367 |
\[ |
|
1368 |
\retrieve \; (r \backslash (s@[c])) \; (\mkeps \; (r \backslash (s@[c]) )) |
|
1369 |
\] |
|
1370 |
with |
|
1371 |
\[ |
|
1372 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c]))) |
|
1373 |
\] |
|
1374 |
using IH, |
|
1375 |
which in turn can be rewritten as |
|
1376 |
\[ |
|
1377 |
\flex \; r \; \textit{id} \; (s@[c]) \; (\mkeps \; (r \backslash (s@[c]))) |
|
1378 |
\]. |
|
532 | 1379 |
\end{proof} |
582 | 1380 |
\noindent |
1381 |
With this pivotal lemma we can now link $\flex$ and $\blexer$ |
|
1382 |
and finally give the correctness of $\blexer$--it outputs the same result as $\lexer$: |
|
649 | 1383 |
\begin{theorem}\label{blexerCorrect} |
542 | 1384 |
$\blexer\; r \; s = \lexer \; r \; s$ |
1385 |
\end{theorem} |
|
1386 |
\begin{proof} |
|
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1387 |
We can rewrite the expression $\blexer \; r \; s$ by using lemma \ref{bmkepsRetrieve}: |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1388 |
\[ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1389 |
\blexer \; r \; s = \decode \; (\retrieve \; (\; r^\uparrow) \; (\mkeps \; (r \backslash s) )) \; r |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1390 |
\] |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1391 |
From \ref{flex_retrieve} we have that the left-hand-side is equal to |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1392 |
\[ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1393 |
\textit{flex} \; r \; \textit{id} \; s \; \mkeps \; (r \backslash s), |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1394 |
\] |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1395 |
which in turn equals |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1396 |
\[ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1397 |
\lexer \; r \; s. |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1398 |
\] |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1399 |
%which immediately implies this theorem. |
542 | 1400 |
\end{proof} |
1401 |
\noindent |
|
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1402 |
To piece things together |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1403 |
%and spell out the correctness |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1404 |
%result of the bitcoded lexer more explicitly, |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1405 |
and give the correctness result explicitly, |
576 | 1406 |
we use the fact from the previous chapter that |
1407 |
\[ |
|
624 | 1408 |
(r, s) \rightarrow v \;\; \textit{iff} \;\; \lexer \; r \; s =\Some \; v |
1409 |
\quad \quad |
|
1410 |
\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \lexer \;r\;s = None |
|
576 | 1411 |
\] |
1412 |
to obtain this corollary: |
|
1413 |
\begin{corollary}\label{blexerPOSIX} |
|
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1414 |
The $\blexer$ function correctly implements a POSIX lexer, namely |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1415 |
\begin{itemize} |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1416 |
\item |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1417 |
$(r, s) \rightarrow v \;\; \textit{iff} \;\; \blexer \; r \; s = \Some \; v$ |
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1418 |
\item |
624 | 1419 |
$\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \blexer \;r\;s = None$ |
657
00171b627b8d
Fixed some annotated/unannotated a/r notation inconsistencies.
Chengsong
parents:
653
diff
changeset
|
1420 |
\end{itemize} |
576 | 1421 |
\end{corollary} |
624 | 1422 |
Our main reason for analysing the bit-coded algorithm over |
1423 |
the injection-based one is that it allows us to define |
|
542 | 1424 |
more aggressive simplifications. |
1425 |
We will elaborate on this in the next chapter. |
|
532 | 1426 |
|
1427 |