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