ChengsongTanPhdThesis/Chapters/Bitcoded1.tex
author Chengsong
Wed, 17 Aug 2022 01:09:13 +0100
changeset 579 35df9cdd36ca
parent 576 3e1b699696b6
child 580 e0f0a81f907b
permissions -rwxr-xr-x
more chap3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     1
% Chapter Template
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     3
% Main chapter title
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
     4
\chapter{Bit-coded Algorithm of Sulzmann and Lu}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
\label{Bitcoded1} % Change X to a consecutive number; for referencing this chapter elsewhere, use \ref{ChapterX}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     7
%Then we illustrate how the algorithm without bitcodes falls short for such aggressive 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     8
%simplifications and therefore introduce our version of the bitcoded algorithm and 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     9
%its correctness proof in 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    10
%Chapter 3\ref{Chapter3}. 
564
Chengsong
parents: 543
diff changeset
    11
In this chapter, we are going to describe the bit-coded algorithm
Chengsong
parents: 543
diff changeset
    12
introduced by Sulzmann and Lu \parencite{Sulzmann2014} to address the growth problem of 
Chengsong
parents: 543
diff changeset
    13
regular expressions. 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    14
We have implemented their algorithm in Scala, and found out inefficiencies
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    15
in their algorithm such as de-duplication not working properly and redundant
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    16
fixpoint construction. Their algorithm is improved and verified with the help of
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    17
formal proofs.
537
Chengsong
parents: 536
diff changeset
    18
\section{Bit-coded Algorithm}
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    19
We first do a recap of what was going on 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    20
in the lexer algorithm in Chapter \ref{Inj},
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    21
\begin{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    22
\begin{tabular}{lcl}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    23
	$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\;  \Some(\mkeps \; r) \; \textit{else} \; \None$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    24
	$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    25
	& & $\quad \phantom{\mid}\; \None \implies \None$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    26
	& & $\quad \mid           \Some(v) \implies \Some(\inj \; r\; c\; v)$
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    27
\end{tabular}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    28
\end{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    29
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    30
The algorithm recursively calls $\lexer$ on 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    31
each new character input,
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    32
and before starting a child call
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    33
it stores information of previous lexing steps
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    34
on a stack, in the form of regular expressions
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    35
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    36
Each descent into deeper recursive calls in $\lexer$
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    37
causes a new pair of $r_i, c_i$ to be pushed to the call stack.
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    38
\begin{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    39
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    40
%\draw (-6,-6) grid (6,6);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    41
\node  [ circle ] (r) at (-6, 5) {$r$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    42
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    43
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    44
\node  [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    45
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    46
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    47
\node  [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    48
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    49
\node [minimum width = 2cm, rectangle, draw] (stack) at (0, 3) {Stack};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    50
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    51
\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    52
	(r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    53
        edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    54
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    55
\path   (r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    56
	edge [bend right, dashed] node {saved} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    57
\path   (c1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    58
	edge [bend right, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    59
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    60
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    61
\end{tikzpicture}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    62
\caption{First Derivative Taken}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    63
\end{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    64
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    65
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    66
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    67
\begin{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    68
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    69
%\draw (-6,-6) grid (6,6);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    70
\node  [ circle ] (r) at (-6, 5) {$r$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    71
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    72
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    73
\node  [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    74
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    75
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    76
\node  [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    77
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    78
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    79
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    80
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    81
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    82
\node [circle, draw] (r2) at (2, 5) {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    83
\node [minimum width = 3cm, minimum height = 1cm, rectangle, draw] (stack) at (0, 2) {\large Stack};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    84
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    85
\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    86
	(r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    87
        edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    88
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    89
\path   (r2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    90
	edge [bend right, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    91
\path   (c2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    92
	edge [bend right, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    93
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    94
\path   (r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    95
	edge [] node {} (r2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    96
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    97
\end{tikzpicture}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    98
\caption{Second Derivative Taken}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    99
\end{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   100
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   101
As the number of derivative steps increase,
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   102
the stack would increase:
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   103
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   104
\begin{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   105
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   106
%\draw (-6,-6) grid (6,6);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   107
\node  [ circle ] (r) at (-6, 5) {$r$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   108
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   109
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   110
\node  [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   111
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   112
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   113
\node  [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   114
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   115
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   116
\node [circle, minimum size = 0.1, ] (c2) at (0, 5.4) {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   117
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   118
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   119
\node [circle, ] (r2) at (2, 5) {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   120
\node [minimum width = 4cm, minimum height = 2.5cm, rectangle, draw] (stack) at (0, 1) { \large Stack};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   121
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   122
\node [] (ldots) at (3.5, 5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   123
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   124
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   125
\node [minimum size = 0.5, circle, ] (rn) at (6, 5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   126
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   127
\node (rldots) at ($(ldots)!.4!(rn)$) {\ldots};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   128
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   129
\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   130
	(r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   131
        edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   132
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   133
\path   (rldots)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   134
	edge [bend left, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   135
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   136
\path   (r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   137
	edge [] node {} (r2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   138
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   139
\path   (r2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   140
	edge [] node {} (ldots);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   141
\path   (ldots)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   142
	edge [bend left, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   143
\path   (5.03, 4.9)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   144
	edge [bend left, dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   145
\end{tikzpicture}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   146
\caption{More Derivatives Taken}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   147
\end{figure}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   148
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   149
After all derivatives have been taken, the stack grows to a maximum size
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   150
and the pair of regular expressions and characters $r_i, c_{i+1}$ 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   151
are then popped out and used in the injection phase:
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   152
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   153
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   154
%\draw (-6,-6) grid (6,6);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   155
\node  [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   156
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   157
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   158
\node  [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   159
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   160
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   161
\node  [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   162
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   163
%\node (0, 6)  (c2) circle [radius = 0.3] {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   164
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   165
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   166
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   167
\node [circle, draw] (r2) at (2, 5) {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   168
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   169
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   170
\node [] (ldots) at (4.5, 5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   171
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   172
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   173
\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   174
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   175
\node at ($(ldots)!.4!(rn)$) {\ldots};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   176
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   177
\node [minimum size = 0.5, circle, draw] (vn) at (6, -5) {$v_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   178
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   179
\node [] (ldots2) at (3.5, -5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   180
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   181
\node  [minimum size = 0.5, circle, draw] (v2) at (2, -5) {$v_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   182
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   183
\node at ($(ldots2)!.4!(v2)$) {\ldots};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   184
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   185
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   186
\node [circle, draw] (v1) at (-2, -5) {$v_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   187
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   188
\node  [radius = 0.5, circle, draw] (v) at (-6, -5) {$v$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   189
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   190
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   191
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   192
\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   193
	(r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   194
        edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   195
\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   196
	(r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   197
        edge [] node {} (r2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   198
\path   (r2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   199
	edge [] node {} (ldots);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   200
\path   (rn)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   201
	edge [] node {$\mkeps$} (vn);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   202
\path   (vn) 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   203
	edge [] node {} (ldots2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   204
\path   (v2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   205
	edge [] node {$\inj \; r_1 \; c_2\;v_2$} (v1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   206
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   207
\path   (v1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   208
	edge [] node {$\inj \; r \; c_1 \; v_1$} (v);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   209
\path   (r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   210
	edge [dashed, bend right] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   211
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   212
\path   (r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   213
	edge [dashed, ] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   214
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   215
\path   (c1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   216
	edge [dashed, bend right] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   217
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   218
\path   (c2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   219
	edge [dashed] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   220
\path   (4.5, 5)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   221
	edge [dashed, bend left] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   222
\path   (4.9, 5)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   223
	edge [dashed, bend left] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   224
\path   (5.3, 5)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   225
	edge [dashed, bend left] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   226
\path (r2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   227
	edge [dashed, ] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   228
\path (rn)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   229
	edge [dashed, bend left] node {} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   230
\end{tikzpicture}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   231
%\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick] 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   232
%%\draw (-6,-6) grid (6,6);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   233
%\node  [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   234
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   235
%%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   236
%\node  [circle, minimum size = 0.1] (c1) at (-4, 5.4) {$c_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   237
%%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   238
%%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   239
%\node  [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   240
%%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   241
%%\node (0, 6)  (c2) circle [radius = 0.3] {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   242
%\node [circle, minimum size = 0.1] (c2) at (0, 5.4) {$c_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   243
%%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   244
%%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   245
%\node [circle, draw] (r2) at (2, 5) {$r_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   246
%%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   247
%%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   248
%\node [] (ldots) at (4.5, 5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   249
%%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   250
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   251
%\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   252
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   253
%\node at ($(ldots)!.4!(rn)$) {\ldots};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   254
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   255
%\node [minimum size = 0.5, circle, draw] (vn) at (6, -5) {$v_n$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   256
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   257
%\node [] (ldots2) at (3.5, -5) {};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   258
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   259
%\node  [minimum size = 0.5, circle, draw] (v2) at (2, -5) {$v_2$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   260
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   261
%\node at ($(ldots2)!.4!(v2)$) {\ldots};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   262
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   263
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   264
%\node [circle, draw] (v1) at (-2, -5) {$v_1$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   265
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   266
%\node  [radius = 0.5, circle, draw] (v) at (-6, -5) {$v$};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   267
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   268
%\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   269
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   270
%\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   271
%	(r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   272
%        edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   273
%\path
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   274
%	(r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   275
%        edge [] node {} (r2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   276
%\path   (r2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   277
%	edge [] node {} (ldots);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   278
%\path   (rn)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   279
%	edge [] node {$\mkeps$} (vn);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   280
%\path   (vn) 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   281
%	edge [] node {} (ldots2);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   282
%\path   (v2)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   283
%	edge [] node {} (v1);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   284
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   285
%\path   (v1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   286
%	edge [] node {} (v);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   287
%\path   (r)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   288
%	edge [] node {saved} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   289
%
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   290
%\path   (r1)
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   291
%	edge [] node {saved} (stack);
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   292
%\end{tikzpicture}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   293
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   294
The information stored in characters and regular expressions
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   295
make the algorithm work in an elegant way, at the expense of being
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   296
storing quite a bit of verbose information.
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   297
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   298
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   299
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   300
The lexer algorithm in Chapter \ref{Inj},
537
Chengsong
parents: 536
diff changeset
   301
stores information of previous lexing steps
Chengsong
parents: 536
diff changeset
   302
on a stack, in the form of regular expressions
Chengsong
parents: 536
diff changeset
   303
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   304
The red part represents what we already know during the first
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   305
derivative phase,
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   306
and the blue part represents the unknown part of input.
537
Chengsong
parents: 536
diff changeset
   307
\begin{ceqn}
Chengsong
parents: 536
diff changeset
   308
\begin{equation}%\label{graph:injLexer}
564
Chengsong
parents: 543
diff changeset
   309
	\begin{tikzcd}[ampersand replacement=\&, execute at end picture={
Chengsong
parents: 543
diff changeset
   310
			\begin{scope}[on background layer]
Chengsong
parents: 543
diff changeset
   311
				\node[rectangle, fill={red!30},
Chengsong
parents: 543
diff changeset
   312
					pattern=north east lines, pattern color=red,
Chengsong
parents: 543
diff changeset
   313
					fit={(-3,-1) (-3, 1) (1, -1) 
Chengsong
parents: 543
diff changeset
   314
						(1, 1)}
Chengsong
parents: 543
diff changeset
   315
				     ] 
Chengsong
parents: 543
diff changeset
   316
				     {}; ,
Chengsong
parents: 543
diff changeset
   317
				\node[rectangle, fill={blue!20},
Chengsong
parents: 543
diff changeset
   318
					pattern=north east lines, pattern color=blue,
Chengsong
parents: 543
diff changeset
   319
					fit= {(1, -1) (1, 1) (3, -1) (3, 1)}
Chengsong
parents: 543
diff changeset
   320
					]
Chengsong
parents: 543
diff changeset
   321
					{};
Chengsong
parents: 543
diff changeset
   322
				\end{scope}}
Chengsong
parents: 543
diff changeset
   323
					]
Chengsong
parents: 543
diff changeset
   324
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] \\
Chengsong
parents: 543
diff changeset
   325
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
Chengsong
parents: 536
diff changeset
   326
\end{tikzcd}
Chengsong
parents: 536
diff changeset
   327
\end{equation}
Chengsong
parents: 536
diff changeset
   328
\end{ceqn}
Chengsong
parents: 536
diff changeset
   329
\noindent
564
Chengsong
parents: 543
diff changeset
   330
The red area expands as we move towards $r_n$, 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   331
indicating more is known about the lexing result.
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   332
Despite knowing this partial lexing information during
564
Chengsong
parents: 543
diff changeset
   333
the forward derivative phase, we choose to store them
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   334
all the way until $r_n$ is reached.
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   335
Then we reconstruct the value character by character
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   336
values at a later stage, using information in a Last-In-First-Out
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   337
manner. Although the algorithm is elegant and natural,
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   338
it can be inefficient and prone to stack overflow.\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   339
It turns out we can store lexing
537
Chengsong
parents: 536
diff changeset
   340
information on the fly, while still using regular expression 
564
Chengsong
parents: 543
diff changeset
   341
derivatives.
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   342
If we remove the individual 
564
Chengsong
parents: 543
diff changeset
   343
lexing steps, and use red and blue areas as before
Chengsong
parents: 543
diff changeset
   344
to indicate consumed (seen) input and constructed
Chengsong
parents: 543
diff changeset
   345
partial value (before recovering the rest of the stack),
Chengsong
parents: 543
diff changeset
   346
one could see that the seen part's lexical information
Chengsong
parents: 543
diff changeset
   347
is stored in the form of a regular expression.
Chengsong
parents: 543
diff changeset
   348
Consider the regular expression $(aa)^* \cdot bc$ matching the string $aabc$
Chengsong
parents: 543
diff changeset
   349
and assume we have just read the two characters $aa$:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   350
\begin{center}
537
Chengsong
parents: 536
diff changeset
   351
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 536
diff changeset
   352
    \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
564
Chengsong
parents: 543
diff changeset
   353
	    {Partial lexing info: $\ONE \cdot a \cdot (aa)^* \cdot bc$ etc.
537
Chengsong
parents: 536
diff changeset
   354
         \nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
Chengsong
parents: 536
diff changeset
   355
\end{tikzpicture}
Chengsong
parents: 536
diff changeset
   356
\end{center}
564
Chengsong
parents: 543
diff changeset
   357
\noindent
Chengsong
parents: 543
diff changeset
   358
In the injection-based lexing algorithm, we ``neglect" the red area
Chengsong
parents: 543
diff changeset
   359
by putting all the characters we have consumed and
Chengsong
parents: 543
diff changeset
   360
intermediate regular expressions on the stack when 
Chengsong
parents: 543
diff changeset
   361
we go from left to right in the derivative phase.
Chengsong
parents: 543
diff changeset
   362
The red area grows till the string is exhausted.
Chengsong
parents: 543
diff changeset
   363
During the injection phase, the value in the blue area
Chengsong
parents: 543
diff changeset
   364
is built up incrementally, while the red area shrinks.
Chengsong
parents: 543
diff changeset
   365
Before we have recovered all characters and intermediate
Chengsong
parents: 543
diff changeset
   366
derivative regular expressions from the stack,
Chengsong
parents: 543
diff changeset
   367
what values these characters and regular expressions correspond 
Chengsong
parents: 543
diff changeset
   368
to are unknown: 
537
Chengsong
parents: 536
diff changeset
   369
\begin{center}
Chengsong
parents: 536
diff changeset
   370
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 536
diff changeset
   371
    \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={white!30,blue!20},]
564
Chengsong
parents: 543
diff changeset
   372
	    {$(\ONE \cdot \ONE) \cdot (aa)^* \cdot bc $ correspond to:$???$
Chengsong
parents: 543
diff changeset
   373
         \nodepart{two}  $b c$ corresponds to  $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
537
Chengsong
parents: 536
diff changeset
   374
%\caption{term 1 \ref{term:1}'s matching configuration}
Chengsong
parents: 536
diff changeset
   375
\end{tikzpicture}
Chengsong
parents: 536
diff changeset
   376
\end{center}
Chengsong
parents: 536
diff changeset
   377
\noindent
564
Chengsong
parents: 543
diff changeset
   378
However, they should be calculable,
Chengsong
parents: 543
diff changeset
   379
as characters and regular expression shapes
Chengsong
parents: 543
diff changeset
   380
after taking derivative w.r.t those characters
Chengsong
parents: 543
diff changeset
   381
have already been known, therefore in our example,
Chengsong
parents: 543
diff changeset
   382
we know that the value starts with two $a$s,
Chengsong
parents: 543
diff changeset
   383
and makes up to an iteration in a Kleene star:
Chengsong
parents: 543
diff changeset
   384
(We have put the injection-based lexing's partial 
Chengsong
parents: 543
diff changeset
   385
result in the right part of the split rectangle
Chengsong
parents: 543
diff changeset
   386
to contrast it with the partial valued produced
Chengsong
parents: 543
diff changeset
   387
in a forward manner)
537
Chengsong
parents: 536
diff changeset
   388
\begin{center}
Chengsong
parents: 536
diff changeset
   389
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 536
diff changeset
   390
    \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
564
Chengsong
parents: 543
diff changeset
   391
	    {$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$
Chengsong
parents: 543
diff changeset
   392
	\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$  $\stackrel{Inj}{\longleftarrow}$};
537
Chengsong
parents: 536
diff changeset
   393
%\caption{term 1 \ref{term:1}'s matching configuration}
Chengsong
parents: 536
diff changeset
   394
\end{tikzpicture}
Chengsong
parents: 536
diff changeset
   395
\end{center}
Chengsong
parents: 536
diff changeset
   396
\noindent
Chengsong
parents: 536
diff changeset
   397
If we do this kind of "attachment"
Chengsong
parents: 536
diff changeset
   398
and each time augment the attached partially
Chengsong
parents: 536
diff changeset
   399
constructed value when taking off a 
Chengsong
parents: 536
diff changeset
   400
character:
Chengsong
parents: 536
diff changeset
   401
\begin{center}
564
Chengsong
parents: 543
diff changeset
   402
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 543
diff changeset
   403
	\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint)
Chengsong
parents: 543
diff changeset
   404
        {$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$
Chengsong
parents: 543
diff changeset
   405
         \nodepart{two} Remaining: $b c$};
Chengsong
parents: 543
diff changeset
   406
\end{tikzpicture}\\
Chengsong
parents: 543
diff changeset
   407
$\downarrow$\\
537
Chengsong
parents: 536
diff changeset
   408
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 536
diff changeset
   409
    \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
Chengsong
parents: 536
diff changeset
   410
        {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$
564
Chengsong
parents: 543
diff changeset
   411
         \nodepart{two} Remaining: $c$};
537
Chengsong
parents: 536
diff changeset
   412
\end{tikzpicture}\\
564
Chengsong
parents: 543
diff changeset
   413
$\downarrow$\\
537
Chengsong
parents: 536
diff changeset
   414
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
Chengsong
parents: 536
diff changeset
   415
    \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
Chengsong
parents: 536
diff changeset
   416
        {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$
Chengsong
parents: 536
diff changeset
   417
         \nodepart{two} EOF};
Chengsong
parents: 536
diff changeset
   418
\end{tikzpicture}
Chengsong
parents: 536
diff changeset
   419
\end{center}
Chengsong
parents: 536
diff changeset
   420
\noindent
Chengsong
parents: 536
diff changeset
   421
In the end we could recover the value without a backward phase.
Chengsong
parents: 536
diff changeset
   422
But (partial) values are a bit clumsy to stick together with a regular expression, so 
Chengsong
parents: 536
diff changeset
   423
we instead use bit-codes to encode them.
Chengsong
parents: 536
diff changeset
   424
Chengsong
parents: 536
diff changeset
   425
Bits and bitcodes (lists of bits) are defined as:
Chengsong
parents: 536
diff changeset
   426
\begin{center}
Chengsong
parents: 536
diff changeset
   427
		$b ::=   S \mid  Z \qquad
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   428
bs ::= [] \mid b::bs    
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   429
$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   430
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   431
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   432
\noindent
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   433
Using $S$ and $Z$ rather than $1$ and $0$ is to avoid
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   434
confusion with the regular expressions $\ZERO$ and $\ONE$.
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   435
Bitcodes (or
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   436
bit-lists) can be used to encode values (or potentially incomplete values) in a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   437
compact form. This can be straightforwardly seen in the following
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   438
coding function from values to bitcodes: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   439
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   440
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   441
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   442
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
537
Chengsong
parents: 536
diff changeset
   443
  $\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\
Chengsong
parents: 536
diff changeset
   444
  $\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   445
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
537
Chengsong
parents: 536
diff changeset
   446
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\
Chengsong
parents: 536
diff changeset
   447
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\;
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   448
                                                 code(\Stars\,vs)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   449
\end{tabular}    
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   450
\end{center} 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   451
\noindent
537
Chengsong
parents: 536
diff changeset
   452
Here $\textit{code}$ encodes a value into a bit-code by converting
Chengsong
parents: 536
diff changeset
   453
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty
Chengsong
parents: 536
diff changeset
   454
star iteration by $S$. The border where a local star terminates
Chengsong
parents: 536
diff changeset
   455
is marked by $Z$. 
Chengsong
parents: 536
diff changeset
   456
This coding is lossy, as it throws away the information about
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   457
characters, and also does not encode the ``boundary'' between two
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   458
sequence values. Moreover, with only the bitcode we cannot even tell
537
Chengsong
parents: 536
diff changeset
   459
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$. The
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   460
reason for choosing this compact way of storing information is that the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   461
relatively small size of bits can be easily manipulated and ``moved
564
Chengsong
parents: 543
diff changeset
   462
around" in a regular expression. 
537
Chengsong
parents: 536
diff changeset
   463
564
Chengsong
parents: 543
diff changeset
   464
Because of the lossiness, the process of decoding a bitlist requires additionally 
Chengsong
parents: 543
diff changeset
   465
a regular expression. The function $\decode$ is defined as:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   466
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   467
%\begin{definition}[Bitdecoding of Values]\mbox{}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   468
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   469
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   470
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   471
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
537
Chengsong
parents: 536
diff changeset
   472
  $\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   473
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   474
       (\Left\,v, bs_1)$\\
537
Chengsong
parents: 536
diff changeset
   475
  $\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   476
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   477
       (\Right\,v, bs_1)$\\                           
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   478
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   479
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   480
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   481
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
537
Chengsong
parents: 536
diff changeset
   482
  $\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
Chengsong
parents: 536
diff changeset
   483
  $\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ & 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   484
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   485
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   486
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   487
  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   488
  $\textit{decode}\,bs\,r$ & $\dn$ &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   489
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   490
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   491
       \textit{else}\;\textit{None}$                       
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   492
\end{tabular}    
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   493
\end{center} 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   494
%\end{definition}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   495
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   496
\noindent
575
Chengsong
parents: 564
diff changeset
   497
The function $\decode'$ returns a pair consisting of 
Chengsong
parents: 564
diff changeset
   498
a partially decoded value and some leftover bit list that cannot
Chengsong
parents: 564
diff changeset
   499
be decide yet.
Chengsong
parents: 564
diff changeset
   500
The function $\decode'$ succeeds if the left-over 
Chengsong
parents: 564
diff changeset
   501
bit-sequence is empty.
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   502
$\decode$ is terminating as $\decode'$ is terminating.
575
Chengsong
parents: 564
diff changeset
   503
$\decode'$ is terminating 
Chengsong
parents: 564
diff changeset
   504
because at least one of $\decode'$'s parameters will go down in terms
Chengsong
parents: 564
diff changeset
   505
of size.
Chengsong
parents: 564
diff changeset
   506
Assuming we have a value $v$ and regular expression $r$
Chengsong
parents: 564
diff changeset
   507
with $\vdash v:r$,
Chengsong
parents: 564
diff changeset
   508
then we have the property that $\decode$ and $\code$ are
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   509
reverse operations of one another:
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   510
\begin{lemma}
575
Chengsong
parents: 564
diff changeset
   511
\[If \vdash v : r \; then \;\decode \; (\code \; v) \; r = \textit{Some}(v) \]
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   512
\end{lemma}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   513
\begin{proof}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   514
By proving a more general version of the lemma, on $\decode'$:
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   515
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \]
575
Chengsong
parents: 564
diff changeset
   516
Then setting $ds$ to be $[]$ and unfolding $\decode$ definition,
Chengsong
parents: 564
diff changeset
   517
we obtain the property.
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   518
\end{proof}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   519
With the $\code$ and $\decode$ functions in hand, we know how to 
575
Chengsong
parents: 564
diff changeset
   520
switch between bit-codes and values. 
Chengsong
parents: 564
diff changeset
   521
The next step is to integrate this information into regular expression.
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   522
Attaching bits to the front of regular expressions is the solution Sulzamann and Lu
575
Chengsong
parents: 564
diff changeset
   523
gave for storing partial values in regular expressions. 
Chengsong
parents: 564
diff changeset
   524
Annotated regular expressions are therefore defined as the Isabelle
Chengsong
parents: 564
diff changeset
   525
datatype:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   526
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   527
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   528
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   529
  $\textit{a}$ & $::=$  & $\ZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   530
                  & $\mid$ & $_{bs}\ONE$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   531
                  & $\mid$ & $_{bs}{\bf c}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   532
                  & $\mid$ & $_{bs}\sum\,as$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   533
                  & $\mid$ & $_{bs}a_1\cdot a_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   534
                  & $\mid$ & $_{bs}a^*$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   535
\end{tabular}    
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   536
\end{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   537
%(in \textit{ALTS})
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   538
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   539
\noindent
575
Chengsong
parents: 564
diff changeset
   540
where $bs$ stands for bit-codes, $a$  for $\mathbf{a}$nnotated regular
Chengsong
parents: 564
diff changeset
   541
expressions and $as$ for lists of annotated regular expressions.
Chengsong
parents: 564
diff changeset
   542
The alternative constructor, written, $\sum$, has been generalised to 
Chengsong
parents: 564
diff changeset
   543
accept a list of annotated regular expressions rather than just two.
Chengsong
parents: 564
diff changeset
   544
Why is it generalised? This is because when we open up nested 
Chengsong
parents: 564
diff changeset
   545
alternatives, there could be more than two elements at the same level
Chengsong
parents: 564
diff changeset
   546
after de-duplication, which can no longer be stored in a binary
Chengsong
parents: 564
diff changeset
   547
constructor.
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   548
575
Chengsong
parents: 564
diff changeset
   549
The first operation we define related to bit-coded regular expressions
Chengsong
parents: 564
diff changeset
   550
is how we move bits to the inside of regular expressions.
Chengsong
parents: 564
diff changeset
   551
Called $\fuse$, this operation is attaches bit-codes 
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   552
to the front of an annotated regular expression:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   553
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   554
\begin{tabular}{lcl}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   555
  $\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   556
  $\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   557
     $_{bs @ bs'}\ONE$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   558
  $\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   559
     $_{bs@bs'}{\bf c}$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   560
  $\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   561
     $_{bs@bs'}\sum\textit{as}$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   562
  $\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   563
     $_{bs@bs'}a_1 \cdot a_2$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   564
  $\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   565
     $_{bs @ bs'}a^*$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   566
\end{tabular}    
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   567
\end{center} 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   568
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   569
\noindent
575
Chengsong
parents: 564
diff changeset
   570
With \emph{fuse} we are able to define the $\internalise$ function
Chengsong
parents: 564
diff changeset
   571
that translates a ``standard'' regular expression into an
Chengsong
parents: 564
diff changeset
   572
annotated regular expression.
Chengsong
parents: 564
diff changeset
   573
This function will be applied before we start
Chengsong
parents: 564
diff changeset
   574
with the derivative phase of the algorithm.
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   575
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   576
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   577
\begin{tabular}{lcl}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   578
  $(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   579
  $(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   580
  $(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   581
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   582
  $_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   583
  \textit{fuse}\,[S]\,r_2^\uparrow]$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   584
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   585
         $_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   586
  $(r^*)^\uparrow$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   587
         $_{[]}(r^\uparrow)^*$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   588
\end{tabular}    
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   589
\end{center}    
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   590
%\end{definition}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   591
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   592
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   593
We use an up arrow with postfix notation
575
Chengsong
parents: 564
diff changeset
   594
to denote this operation.
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   595
for convenience. The $\textit{internalise} \; r$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   596
notation is more cumbersome.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   597
The opposite of $\textit{internalise}$ is
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   598
$\erase$, where all the bit-codes are removed,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   599
and the alternative operator $\sum$ for annotated
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   600
regular expressions is transformed to the binary alternatives
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   601
for plain regular expressions.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   602
\begin{center}
575
Chengsong
parents: 564
diff changeset
   603
	\begin{tabular}{lcl}
Chengsong
parents: 564
diff changeset
   604
		$\ZERO_\downarrow$ & $\dn$ & $\ZERO$\\
Chengsong
parents: 564
diff changeset
   605
		$( _{bs}\ONE )_\downarrow$ & $\dn$ & $\ONE$\\
Chengsong
parents: 564
diff changeset
   606
		$( _{bs}\mathbf{c} )_\downarrow$ & $\dn$ & $\mathbf{c}$\\
Chengsong
parents: 564
diff changeset
   607
		$( _{bs} a_1 \cdot a_2 )_\downarrow$ & $\dn$ & 
Chengsong
parents: 564
diff changeset
   608
		$ (a_1) _\downarrow \cdot  (a_2) _\downarrow$\\
Chengsong
parents: 564
diff changeset
   609
		$( _{bs} [])_\downarrow $ & $\dn$ & $\ZERO $\\
Chengsong
parents: 564
diff changeset
   610
		$( _{bs} [a]  )_\downarrow$ & $\dn$ & $a_\downarrow$\\
Chengsong
parents: 564
diff changeset
   611
		$_{bs} \sum [a_1, \; a_2]$ & $\dn$ & $ (a_1) _\downarrow + ( a_2 ) _\downarrow $\\
Chengsong
parents: 564
diff changeset
   612
		$(_{bs} \sum (a :: as))_\downarrow$ & $\dn$ & $ a_\downarrow + \; (_{[]} \sum as)_\downarrow$\\
Chengsong
parents: 564
diff changeset
   613
		$( _{bs} a^* )_\downarrow$ & $\dn$ & $(a_\downarrow)^*$
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   614
	\end{tabular}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   615
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   616
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   617
We also abbreviate the $\erase\; a$ operation
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   618
as $a_\downarrow$, for conciseness.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   619
For bit-coded regular expressions, as a different datatype, 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   620
testing whether they contain empty string in their lauguage requires
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   621
a dedicated function $\bnullable$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   622
which simply calls $\erase$ first before testing whether it is $\nullable$.
575
Chengsong
parents: 564
diff changeset
   623
\begin{definition}
Chengsong
parents: 564
diff changeset
   624
		$\bnullable \; a \dn  \nullable \; (a_\downarrow)$
Chengsong
parents: 564
diff changeset
   625
\end{definition}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   626
The function for collecting the
575
Chengsong
parents: 564
diff changeset
   627
bitcodes at the end of the derivative 
Chengsong
parents: 564
diff changeset
   628
phase from a (b)nullable regular expression
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   629
is a generalised version of the $\textit{mkeps}$ function
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   630
for annotated regular expressions, called $\textit{bmkeps}$:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   631
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   632
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   633
%\begin{definition}[\textit{bmkeps}]\mbox{}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   634
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   635
\begin{tabular}{lcl}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   636
  $\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   637
  $\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   638
     $\textit{if}\;\textit{bnullable}\,a$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   639
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   640
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   641
  $\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   642
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   643
  $\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   644
     $bs \,@\, [Z]$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   645
\end{tabular}    
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   646
\end{center}    
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   647
%\end{definition}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   648
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   649
\noindent
575
Chengsong
parents: 564
diff changeset
   650
$\bmkeps$ completes the value information by travelling along the
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   651
path of the regular expression that corresponds to a POSIX value and
575
Chengsong
parents: 564
diff changeset
   652
collecting all the bitcodes, and attaching $S$ to indicate the end of star
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   653
iterations. 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   654
575
Chengsong
parents: 564
diff changeset
   655
Now we give out the central part of this lexing algorithm,
Chengsong
parents: 564
diff changeset
   656
the $\bder$ function (stands for \emph{b}itcoded-derivative).
Chengsong
parents: 564
diff changeset
   657
For most time we use the infix notation $(\_\backslash\_)$ 
Chengsong
parents: 564
diff changeset
   658
to mean $\bder$ for brevity when
Chengsong
parents: 564
diff changeset
   659
there is no danger of confusion with derivatives on plain regular expressions.
Chengsong
parents: 564
diff changeset
   660
For example, we write $( _{[]}r^* ) \backslash c$ instead of $\bder \;c \; _{[]}r^*$,
Chengsong
parents: 564
diff changeset
   661
as the bitcodes at the front of $r^*$ indicates that it is 
Chengsong
parents: 564
diff changeset
   662
a bit-coded regular expression, not a plain one.
Chengsong
parents: 564
diff changeset
   663
$\bder$ tells us how regular expressions can be recursively traversed,
Chengsong
parents: 564
diff changeset
   664
where the bitcodes are augmented and carried around 
Chengsong
parents: 564
diff changeset
   665
when a derivative is taken.
Chengsong
parents: 564
diff changeset
   666
\begin{center}
Chengsong
parents: 564
diff changeset
   667
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 564
diff changeset
   668
  $(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 564
diff changeset
   669
  $(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 564
diff changeset
   670
  $(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
Chengsong
parents: 564
diff changeset
   671
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 564
diff changeset
   672
         _{bs}\ONE\;\textit{else}\;\ZERO$\\  
Chengsong
parents: 564
diff changeset
   673
  $(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
Chengsong
parents: 564
diff changeset
   674
  $_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\
Chengsong
parents: 564
diff changeset
   675
  $(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 564
diff changeset
   676
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 564
diff changeset
   677
					       & &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
Chengsong
parents: 564
diff changeset
   678
					       & &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
Chengsong
parents: 564
diff changeset
   679
  & &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
Chengsong
parents: 564
diff changeset
   680
  $(_{bs}a^*)\,\backslash c$ & $\dn$ &
Chengsong
parents: 564
diff changeset
   681
      $_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot
Chengsong
parents: 564
diff changeset
   682
       (_{[]}r^*))$
Chengsong
parents: 564
diff changeset
   683
\end{tabular}    
Chengsong
parents: 564
diff changeset
   684
\end{center}    
Chengsong
parents: 564
diff changeset
   685
\noindent
Chengsong
parents: 564
diff changeset
   686
We give the intuition behind some of the more involved cases in 
Chengsong
parents: 564
diff changeset
   687
$\bder$. For example,
Chengsong
parents: 564
diff changeset
   688
in the \emph{star} case,
Chengsong
parents: 564
diff changeset
   689
a derivative on $_{bs}a^*$ means 
Chengsong
parents: 564
diff changeset
   690
that one more star iteratoin needs to be taken.
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   691
we need to unfold it into a sequence,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   692
and attach an additional bit $Z$ to the front of $r \backslash c$
575
Chengsong
parents: 564
diff changeset
   693
as a record to indicate one new star iteration is unfolded.
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   694
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   695
\noindent
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   696
\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   697
  \begin{tabular}{@{}lcl@{}}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   698
  $(_{bs}a^*)\,\backslash c$ & $\dn$ &
575
Chengsong
parents: 564
diff changeset
   699
  $_{bs}(\underbrace{\textit{fuse}\, [Z] \; a\,\backslash c}_{\text{One more iteration}})\cdot
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   700
       (_{[]}a^*))$
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   701
\end{tabular}    
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   702
\end{center}   
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   703
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   704
\noindent
575
Chengsong
parents: 564
diff changeset
   705
This information will be recovered later by the $\decode$ function.
Chengsong
parents: 564
diff changeset
   706
The intuition is that the bit $Z$ will be decoded at the right location,
Chengsong
parents: 564
diff changeset
   707
because we accumulate bits from left to right (a rigorous proof will be given
Chengsong
parents: 564
diff changeset
   708
later).
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   709
575
Chengsong
parents: 564
diff changeset
   710
\begin{tikzpicture}[ > = stealth, % arrow head style
Chengsong
parents: 564
diff changeset
   711
        shorten > = 1pt, % don't touch arrow head to node
Chengsong
parents: 564
diff changeset
   712
        semithick % line style
Chengsong
parents: 564
diff changeset
   713
    ]
Chengsong
parents: 564
diff changeset
   714
Chengsong
parents: 564
diff changeset
   715
    \tikzstyle{every state}=[
Chengsong
parents: 564
diff changeset
   716
        draw = black,
Chengsong
parents: 564
diff changeset
   717
        thin,
Chengsong
parents: 564
diff changeset
   718
        fill = cyan!29,
Chengsong
parents: 564
diff changeset
   719
        minimum size = 7mm
Chengsong
parents: 564
diff changeset
   720
    ]
Chengsong
parents: 564
diff changeset
   721
    \begin{scope}[node distance=1cm and 0cm, every node/.style=state]
Chengsong
parents: 564
diff changeset
   722
		\node (k) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   723
        {$bs$
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   724
         \nodepart{two} $a^*$ };
575
Chengsong
parents: 564
diff changeset
   725
	 \node (l) [below =of k, rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
Chengsong
parents: 564
diff changeset
   726
        { $bs$ + [Z]
Chengsong
parents: 564
diff changeset
   727
         \nodepart{two}  $(a\backslash c )\cdot a^*$ };
Chengsong
parents: 564
diff changeset
   728
    \end{scope}
Chengsong
parents: 564
diff changeset
   729
    \path[->] 
Chengsong
parents: 564
diff changeset
   730
	      (k) edge (l);
Chengsong
parents: 564
diff changeset
   731
\end{tikzpicture}
Chengsong
parents: 564
diff changeset
   732
Chengsong
parents: 564
diff changeset
   733
Pictorially the process looks like below.
Chengsong
parents: 564
diff changeset
   734
Like before, the red region denotes
Chengsong
parents: 564
diff changeset
   735
previous lexing information (stored as bitcodes in $bs$).
Chengsong
parents: 564
diff changeset
   736
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   737
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
575
Chengsong
parents: 564
diff changeset
   738
	\begin{scope}[node distance=1cm]   
Chengsong
parents: 564
diff changeset
   739
		\node (a) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
Chengsong
parents: 564
diff changeset
   740
        {$bs$
Chengsong
parents: 564
diff changeset
   741
         \nodepart{two} $a^*$ };
Chengsong
parents: 564
diff changeset
   742
	 \node (b) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   743
        { $bs$ + [Z]
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   744
         \nodepart{two}  $(a\backslash c )\cdot a^*$ };
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   745
%\caption{term 1 \ref{term:1}'s matching configuration}
575
Chengsong
parents: 564
diff changeset
   746
 	\end{scope}
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   747
\end{tikzpicture}
575
Chengsong
parents: 564
diff changeset
   748
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   749
\noindent
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   750
Another place in the $\bder$ function where it differs
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   751
from normal derivatives (on un-annotated regular expressions)
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   752
is the sequence case:
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   753
\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   754
  \begin{tabular}{@{}lcl@{}}
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   755
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   756
  $(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   757
     $\textit{if}\;\textit{bnullable}\,a_1$\\
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   758
					       & &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   759
					       & &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   760
  & &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   761
\end{tabular}    
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   762
\end{center}    
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   763
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   764
The difference is that (when $a_1$ is $\bnullable$)
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   765
we use $\bmkeps$ to store the lexing information
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   766
in $a_1$ before collapsing it (as it has been fully matched by string prior to $c$, 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   767
and attach the collected bit-codes to the front of $a_2$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   768
before throwing away $a_1$. We assume that $\bmkeps$ correctly extracts the bitcode for how $a_1$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   769
matches the string prior to $c$ (more on this later).
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   770
The bitsequence $\textit{bs}$ which was initially attached to the first element of the sequence
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   771
$a_1 \cdot a_2$, has now been elevated to the top level of teh $\sum$. 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   772
This is because this piece of information will be needed whichever way the sequence is matched,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   773
regardless of whether $c$ belongs to $a_1$ or $a_2$.
538
8016a2480704 intro and chap2
Chengsong
parents: 537
diff changeset
   774
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   775
In the injection-based lexing, $r_1$ is immediately thrown away in 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   776
subsequent derivatives on the right branch (when $r_1$ is $\nullable$),
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   777
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   778
	$(r_1 \cdot r_2 )\backslash c = (r_1 \backslash c) \cdot r_2 + r_2 \backslash c$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   779
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   780
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   781
as it knows $r_1$ is stored on stack and available once the recursive 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   782
call to later derivatives finish.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   783
Therefore, if the $\Right$ branch is taken in a $\POSIX$ match,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   784
we construct back the sequence value once step back by
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   785
calling a on $\mkeps(r_1)$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   786
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   787
	\begin{tabular}{lcr}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   788
		$\ldots r_1 \cdot r_2$ & $\rightarrow$ & $r_1\cdot r_2 + r_2 \backslash c \ldots $\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   789
		$\ldots \Seq(v_1, v_2) (\Seq(\mkeps(r1), (\inj \; r_2 \; c\; v_{2c})))$ & $\leftarrow$ & $\Right(v_{2c})\ldots$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   790
	\end{tabular}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   791
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   792
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   793
The rest of the clauses of $\bder$ is rather similar to
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   794
$\der$, and is put together here as a wholesome definition
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   795
for $\bder$:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   796
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   797
Generalising the derivative operation with bitcodes to strings, we have 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   798
\begin{center}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   799
	\begin{tabular}{@{}lcl@{}}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   800
		$a\backslash_s [] $ & $\dn$ & $a$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   801
		$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   802
	\end{tabular}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   803
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   804
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   805
of confusion.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   806
Putting this all together, we obtain a lexer with bit-coded regular expressions
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   807
as its internal data structures, which we call $\blexer$:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   808
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   809
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   810
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   811
  $\textit{blexer}\;r\,s$ & $\dn$ &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   812
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   813
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   814
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   815
  & & $\;\;\textit{else}\;\textit{None}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   816
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   817
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   818
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   819
\noindent
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   820
Ausaf and Urban formally proved the correctness of the $\blexer$, namely
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   821
\begin{conjecture}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   822
$\blexer \;r \; s = \lexer \; r \; s$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   823
\end{conjecture}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   824
This was claimed but not formalised in Sulzmann and Lu's work.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   825
We introduce the proof later, after we give out all
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   826
the needed auxiliary functions and definitions
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   827
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   828
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   829
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   830
%	SUBSECTION 1
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   831
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   832
\section{Specifications of Some Helper Functions}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   833
The functions we introduce will give a more detailed glimpse into 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   834
the lexing process, which might not be possible
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   835
using $\lexer$ or $\blexer$ themselves.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   836
The first function we shall look at is $\retrieve$.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 542
diff changeset
   837
\subsection{$\textit{Retrieve}$}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   838
Our bit-coded lexer "retrieve"s the bitcodes using $\bmkeps$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   839
after we finished doing all the derivatives:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   840
\begin{center}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   841
\begin{tabular}{lcl}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   842
	& & $\ldots$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   843
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   844
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   845
  & & $\ldots$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   846
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   847
\end{center}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   848
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   849
Recall that $\bmkeps$ looks for the leftmost branch of an alternative
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   850
and completes a star's iterations by attaching a $Z$ at the end of the bitcodes
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   851
extracted. It "retrieves" a sequence by visiting both children and then stitch together 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   852
two bitcodes using concatenation. After the entire tree structure of the regular 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   853
expression has been traversed using the above manner, we get a bitcode encoding the 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   854
lexing result.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   855
We know that this "retrieved" bitcode leads to the correct value after decoding,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   856
which is $v_0$ in the bird's eye view of the injection-based lexing diagram.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   857
Now assume we keep every other data structure in the diagram \ref{InjFigure},
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   858
and only replace all the plain regular expression by their annotated counterparts,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   859
computed during a $\blexer$ run.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   860
Then we obtain a diagram for the annotated regular expression derivatives and
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   861
their corresponding values, though the values are never calculated in $\blexer$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   862
We have that $a_n$ contains all the lexing result information.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   863
\vspace{20mm}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   864
\begin{center}%\label{graph:injLexer}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   865
\begin{tikzcd}[
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   866
	every matrix/.append style = {name=p},
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   867
	remember picture, overlay,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   868
	]
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   869
	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] \\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   870
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]         
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   871
\end{tikzcd}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   872
\begin{tikzpicture}[
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   873
	remember picture, overlay,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   874
E/.style = {ellipse, draw=blue, dashed,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   875
            inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   876
                        ]
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   877
\node[E = (p-1-1) (p-2-1)] {};
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   878
\node[E = (p-1-4) (p-2-4)] {};
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   879
\end{tikzpicture}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   880
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   881
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   882
\vspace{20mm}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   883
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   884
On the other hand, $v_0$ also encodes the correct lexing result, as we have proven for $\lexer$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   885
Encircled in the diagram  are the two pairs $v_0, a_0$ and $v_n, a_n$, which both 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   886
encode the correct lexical result. Though for the leftmost pair, we have
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   887
the information condensed in $v_0$ the value part, whereas for the rightmost pair,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   888
the information is concentrated on $a_n$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   889
We know that in the intermediate steps the pairs $v_i, a_i$, must in some way encode the complete
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   890
lexing information as well. Therefore, we need a unified approach to extract such lexing result
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   891
from a value $v_i$ and its annotated regular expression $a_i$. 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   892
And the function $f$ must satisfy these requirements:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   893
\begin{itemize}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   894
	\item
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   895
		$f \; a_i\;v_i = f \; a_n \; v_n = \decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   896
	\item
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   897
		$f \; a_i\;v_i = f \; a_0 \; v_0 = v_0 = \decode \;(\code \; v_0) \; (\erase \; a_0)$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   898
\end{itemize}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   899
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   900
If we factor out the common part $\decode \; \_ \; (\erase \; a_0)$,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   901
The core of the function $f$ is something that produces the bitcodes
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   902
$\code \; v_0$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   903
It is unclear how, but Sulzmann and Lu came up with a function satisfying all the above
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   904
requirements, named \emph{retrieve}:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   905
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   906
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   907
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   908
\begin{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   909
\begin{tabular}{lcr}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   910
	$\retrieve \; \, (_{bs} \ONE) \; \, (\Empty)$ & $\dn$ & $\textit{bs}$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   911
	$\retrieve \; \, (_{bs} \mathbf{c} ) \; \Char(c)$ & $\dn$ & $ \textit{bs}$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   912
	$\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)$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   913
	$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as}) \; \,\Left(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v)$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   914
	$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as} \; \, \Right(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   915
	$\retrieve \; \, (_{bs} a^*) \; \, (\Stars(v :: vs)) $ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v) @ (\retrieve \; (_{[]} a^*) \; (\Stars(vs)))$\\
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   916
	$\retrieve \; \, (_{bs} a^*) \; \, (\Stars([]) $ & $\dn$ & $\textit{bs} @ [Z]$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   917
\end{tabular}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   918
\end{center}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   919
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   920
As promised, $\retrieve$ collects the right bit-codes from the 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   921
final derivative $a_n$:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   922
\begin{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   923
	$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (\erase \; a))$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   924
\end{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   925
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   926
	By a routine induction on $a$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   927
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   928
The design of $\retrieve$ enables extraction of bit-codes
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   929
from not only $\bnullable$ (annotated) regular expressions,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   930
but also those that are not $\bnullable$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   931
For example, if we have the regular expression just internalised
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   932
and the lexing result value, we could $\retrieve$ the bitcdoes
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   933
that look as if we have en$\code$-ed the value:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   934
\begin{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   935
	$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   936
\end{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   937
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   938
	By induction on $r$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   939
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   940
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   941
The following property is more interesting, as
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   942
it provides a "bridge" between $a_0, v_0$ and $a_n, v_n$ in the
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   943
lexing diagram.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   944
If you take derivative of an annotated regular expression, 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   945
you can $\retrieve$ the same bit-codes as before the derivative took place,
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   946
provided that you use the corresponding value:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   947
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   948
\begin{lemma}\label{retrieveStepwise}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   949
	$\vdash v : (r\backslash c) \implies \retrieve \; (r \backslash c)  \;  v= \retrieve \; r \; (\inj \; r\; c\; v)$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   950
\end{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   951
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   952
	By induction on $r$, where $v$ is allowed to be arbitrary.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   953
	The induction principle is function $\erase$'s cases.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   954
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   955
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   956
$\retrieve$ is connected to the $\blexer$ in the following way:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   957
\begin{lemma}\label{blexer_retrieve}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   958
$\blexer \; r \; s = \decode  \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   959
\end{lemma}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   960
\noindent
564
Chengsong
parents: 543
diff changeset
   961
$\retrieve$ allows free navigation on the diagram \ref{InjFigure} for annotated regular expressiones of $\blexer$.
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   962
For plain regular expressions something similar is required as well.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   963
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   964
\subsection{$\flex$}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   965
Ausaf and Urban cleverly defined an auxiliary function called $\flex$ for $\lexer$,
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   966
defined as
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   967
\begin{center}
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   968
\begin{tabular}{lcr}
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   969
$\flex \; r \; f \; [] \; v$       &  $=$ &   $f\; v$\\
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   970
$\flex \; r \; f \; c :: s \; v$ &  $=$ &   $\flex \; r \; \lambda v. \, f (\inj \; r\; c\; v)\; s \; v$
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   971
\end{tabular}
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   972
\end{center}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   973
which accumulates the characters that needs to be injected back, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   974
and does the injection in a stack-like manner (last taken derivative first injected).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   975
$\flex$ is connected to the $\lexer$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   976
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   977
$\flex \; r \; \textit{id}\; s \; \mkeps (r\backslash s) = \lexer \; r \; s$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   978
\end{lemma}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   979
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   980
	By reverse induction on $s$.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   981
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   982
$\flex$ provides us a bridge between $\lexer$'s intermediate steps.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   983
What is even better about $\flex$ is that it allows us to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   984
directly operate on the value $\mkeps (r\backslash v)$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   985
which is pivotal in the definition of  $\lexer $ and $\blexer$, but not visible as an argument.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   986
When the value created by $\mkeps$ becomes available, one can 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   987
prove some stepwise properties of lexing nicely:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   988
\begin{lemma}\label{flexStepwise}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   989
$\textit{flex} \; r \; f \; s@[c] \; v= \flex \; r \; f\; s \; (\inj \; (r\backslash s) \; c \; v) $
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   990
\end{lemma}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   991
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   992
	By induction on the shape of $r\backslash s$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   993
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   994
\noindent
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   995
With $\flex$ and $\retrieve$ ready, we are ready to connect $\lexer$ and $\blexer$ .
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   996
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
   997
\subsection{Correctness Proof of Bit-coded Algorithm}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   998
\begin{lemma}\label{flex_retrieve}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   999
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1000
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1001
\begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1002
By induction on $s$. The induction tactic is reverse induction on strings.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1003
$v$ is allowed to be arbitrary.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1004
The crucial point is to rewrite 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1005
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1006
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c]) 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1007
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1008
as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1009
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1010
\retrieve \; (r \backslash s) \; (\inj \; (r \backslash s) \; c\;  \mkeps (r \backslash s@[c]))
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1011
\].
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1012
This enables us to equate 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1013
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1014
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c]) 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1015
\] 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1016
with 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1017
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1018
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c])))
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1019
\],
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1020
which in turn can be rewritten as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1021
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1022
\flex \; r \; \textit{id} \; s@[c] \;  (\mkeps (r\backslash s@[c]))
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1023
\].
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1024
\end{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1025
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1026
With the above lemma we can now link $\flex$ and $\blexer$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1027
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1028
%----------------------------------------------------------------------------------------
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1029
%	SECTION  correctness proof
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1030
%----------------------------------------------------------------------------------------
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1031
\section{Correctness of Bit-coded Algorithm (Without Simplification)}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1032
We now give the proof the correctness of the algorithm with bit-codes.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1033
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1034
\begin{lemma}\label{flex_blexer}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1035
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s)  = \blexer \; r \; s$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1036
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1037
\begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1038
Using two of the above lemmas: \ref{flex_retrieve} and \ref{blexer_retrieve}.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1039
\end{proof}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1040
Finally the correctness of $\blexer$ is given as it outputs the same result as $\lexer$:
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1041
\begin{theorem}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1042
	$\blexer\; r \; s = \lexer \; r \; s$
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1043
\end{theorem}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1044
\begin{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1045
	Straightforward corollary of \ref{flex_blexer}.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1046
\end{proof}
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1047
\noindent
576
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1048
To piece things together and spell out the exact correctness
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1049
of the bitcoded lexer
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1050
in terms of producing POSIX values,
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1051
we use the fact from the previous chapter that
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1052
\[
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1053
	If \; (r, s) \rightarrow v \; then \; \lexer \; r \; s = v
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1054
\]
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1055
to obtain this corollary:
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1056
\begin{corollary}\label{blexerPOSIX}
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1057
	$If \; (r, s) \rightarrow v \; then \blexer \; r \; s = v$
3e1b699696b6 thesis chap5
Chengsong
parents: 575
diff changeset
  1058
\end{corollary}
542
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1059
Our main reason for wanting a bit-coded algorithm over 
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1060
the injection-based one is for its capabilities of allowing
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1061
more aggressive simplifications.
a7344c9afbaf chapter3 finished
Chengsong
parents: 538
diff changeset
  1062
We will elaborate on this in the next chapter.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1063
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1064