ChengsongTanPhdThesis/Chapters/Bitcoded2.tex
author Chengsong
Sat, 27 Aug 2022 00:37:03 +0100
changeset 585 4969ef817d92
parent 584 1734bd5975a3
child 586 826af400b068
permissions -rwxr-xr-x
chap4 more
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
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     4
\chapter{Correctness of Bit-coded Algorithm with Simplification}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
\label{Bitcoded2} % 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}. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    11
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    12
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    13
583
Chengsong
parents: 582
diff changeset
    14
In this chapter we introduce the simplifications
Chengsong
parents: 582
diff changeset
    15
on annotated regular expressions that can be applied to 
Chengsong
parents: 582
diff changeset
    16
each intermediate derivative result. This allows
Chengsong
parents: 582
diff changeset
    17
us to make $\blexer$ much more efficient.
Chengsong
parents: 582
diff changeset
    18
We contrast this simplification function 
Chengsong
parents: 582
diff changeset
    19
with Sulzmann and Lu's original
Chengsong
parents: 582
diff changeset
    20
simplifications, indicating the simplicity of our algorithm and
Chengsong
parents: 582
diff changeset
    21
improvements we made, demostrating
Chengsong
parents: 582
diff changeset
    22
the usefulness and reliability of formal proofs on algorithms.
Chengsong
parents: 582
diff changeset
    23
These ``aggressive'' simplifications would not be possible in the injection-based 
Chengsong
parents: 582
diff changeset
    24
lexing we introduced in chapter \ref{Inj}.
Chengsong
parents: 582
diff changeset
    25
We then go on to prove the correctness with the improved version of 
Chengsong
parents: 582
diff changeset
    26
$\blexer$, called $\blexersimp$, by establishing 
Chengsong
parents: 582
diff changeset
    27
$\blexer \; r \; s= \blexersimp \; r \; s$ using a term rewriting system.
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
    28
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    29
\section{Simplifications by Sulzmann and Lu}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
    30
The first thing we notice in the fast growth of examples such as $(a^*a^*)^*$'s
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
    31
and $(a^* + (aa)^*)^*$'s derivatives is that a lot of duplicated sub-patterns
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    32
are scattered around different levels, and therefore requires 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    33
de-duplication at different levels:
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
    34
\begin{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    35
	$(a^*a^*)^* \stackrel{\backslash a}{\longrightarrow} (a^*a^* + a^*)\cdot(a^*a^*)^* \stackrel{\backslash a}{\longrightarrow} $\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    36
	$((a^*a^* + a^*) + a^*)\cdot(a^*a^*)^* + (a^*a^* + a^*)\cdot(a^*a^*)^* \stackrel{\backslash a}{\longrightarrow} \ldots$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
    37
\end{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
    38
\noindent
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    39
As we have already mentioned in \ref{eqn:growth2},
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    40
a simple-minded simplification function cannot simplify
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    41
the third regular expression in the above chain of derivative
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    42
regular expressions:
583
Chengsong
parents: 582
diff changeset
    43
\begin{center}
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    44
$((a^*a^* + a^*) + a^*)\cdot(a^*a^*)^* + (a^*a^* + a^*)\cdot(a^*a^*)^*$
583
Chengsong
parents: 582
diff changeset
    45
\end{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    46
one would expect a better simplification function to work in the 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    47
following way:
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    48
\begin{gather*}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    49
	((a^*a^* + \underbrace{a^*}_\text{A})+\underbrace{a^*}_\text{duplicate of A})\cdot(a^*a^*)^* + 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    50
	\underbrace{(a^*a^* + a^*)\cdot(a^*a^*)^*}_\text{further simp removes this}.\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    51
	\bigg\downarrow \\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    52
	(a^*a^* + a^* 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    53
	\color{gray} + a^* \color{black})\cdot(a^*a^*)^* + 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    54
	\underbrace{(a^*a^* + a^*)\cdot(a^*a^*)^*}_\text{further simp removes this} \\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    55
	\bigg\downarrow \\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    56
	(a^*a^* + a^* 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    57
	)\cdot(a^*a^*)^*  
583
Chengsong
parents: 582
diff changeset
    58
	\color{gray} + (a^*a^* + a^*) \cdot(a^*a^*)^*\\
Chengsong
parents: 582
diff changeset
    59
	\bigg\downarrow \\
Chengsong
parents: 582
diff changeset
    60
	(a^*a^* + a^* 
Chengsong
parents: 582
diff changeset
    61
	)\cdot(a^*a^*)^*  
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    62
\end{gather*}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    63
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    64
This motivating example came from testing Sulzmann and Lu's 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    65
algorithm: their simplification does 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    66
not work!
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    67
We quote their $\textit{simp}$ function verbatim here:
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    68
\begin{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    69
	\begin{tabular}{lcl}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    70
		$\simpsulz \; _{bs}(_{bs'}\ONE \cdot r)$ & $\dn$ & 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    71
		$\textit{if} \; (\textit{zeroable} \; r)\; \textit{then} \;\; \ZERO$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    72
						   & &$\textit{else}\;\; \fuse \; (bs@ bs') \; r$\\
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    73
		$\simpsulz \;(_{bs}r_1\cdot r_2)$ & $\dn$ & $\textit{if} 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    74
		\; (\textit{zeroable} \; r_1 \; \textit{or} \; \textit{zeroable}\; r_2)\;
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    75
		\textit{then} \;\; \ZERO$\\
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    76
					     & & $\textit{else}\;\;_{bs}((\simpsulz \;r_1)\cdot
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    77
					     (\simpsulz \; r_2))$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    78
		$\simpsulz  \; _{bs}\sum []$ & $\dn$ & $\ZERO$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    79
		$\simpsulz  \; _{bs}\sum ((_{bs'}\sum rs_1) :: rs_2)$ & $\dn$ &
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    80
		$_{bs}\sum ((\map \; (\fuse \; bs')\; rs_1) @ rs_2)$\\
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    81
		$\simpsulz  \; _{bs}\sum[r]$ & $\dn$ & $\fuse \; bs \; (\simpsulz  \; r)$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    82
		$\simpsulz  \; _{bs}\sum(r::rs)$ & $\dn$ & $_{bs}\sum 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
    83
		(\nub \; (\filter \; (\not \circ \zeroable)\;((\simpsulz  \; r) :: \map \; \simpsulz  \; rs)))$\\ 
579
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    84
		
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    85
	\end{tabular}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    86
\end{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    87
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    88
the $\textit{zeroable}$ predicate 
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    89
which tests whether the regular expression
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    90
is equivalent to $\ZERO$,
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    91
is defined as:
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    92
\begin{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    93
	\begin{tabular}{lcl}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    94
		$\zeroable \; _{bs}\sum (r::rs)$ & $\dn$ & $\zeroable \; r\;\; \land \;\;
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    95
		\zeroable \;_{[]}\sum\;rs $\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    96
		$\zeroable\;_{bs}(r_1 \cdot r_2)$ & $\dn$ & $\zeroable\; r_1 \;\; \lor \;\; \zeroable \; r_2$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    97
		$\zeroable\;_{bs}r^*$ & $\dn$ & $\textit{false}$ \\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    98
		$\zeroable\;_{bs}c$ & $\dn$ & $\textit{false}$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
    99
		$\zeroable\;_{bs}\ONE$ & $\dn$ & $\textit{false}$\\
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   100
		$\zeroable\;_{bs}\ZERO$ & $\dn$ & $\textit{true}$
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   101
	\end{tabular}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   102
\end{center}
35df9cdd36ca more chap3
Chengsong
parents: 576
diff changeset
   103
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   104
They suggested that the $\simpsulz $ function should be
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   105
applied repeatedly until a fixpoint is reached.
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   106
We call this construction $\textit{sulzSimp}$:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   107
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   108
	\begin{tabular}{lcl}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   109
		$\textit{sulzSimp} \; r$ & $\dn$ & 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   110
		$\textit{while}((\simpsulz  \; r)\; \cancel{=} \; r)$ \\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   111
		& & $\quad r := \simpsulz  \; r$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   112
		& & $\textit{return} \; r$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   113
	\end{tabular}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   114
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   115
We call the operation of alternatingly 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   116
applying derivatives and simplifications
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   117
(until the string is exhausted) Sulz-simp-derivative,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   118
written $\backslash_{sulzSimp}$:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   119
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   120
\begin{tabular}{lcl}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   121
	$r \backslash_{sulzSimp} (c\!::\!s) $ & $\dn$ & $(\textit{sulzSimp} \; (r \backslash c)) \backslash_{sulzSimp}\, s$ \\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   122
$r \backslash_{sulzSimp} [\,] $ & $\dn$ & $r$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   123
\end{tabular}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   124
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   125
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   126
After the derivatives have been taken, the bitcodes
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   127
are extracted and decoded in the same manner
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   128
as $\blexer$:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   129
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   130
\begin{tabular}{lcl}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   131
  $\textit{blexer\_sulzSimp}\;r\,s$ & $\dn$ &
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   132
      $\textit{let}\;a = (r^\uparrow)\backslash_{sulzSimp}\, s\;\textit{in}$\\                
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   133
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   134
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   135
  & & $\;\;\textit{else}\;\textit{None}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   136
\end{tabular}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   137
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   138
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   139
We implemented this lexing algorithm in Scala, 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   140
and found that the final derivative regular expression
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   141
size grows exponentially fast:
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   142
\begin{figure}[H]
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   143
	\centering
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   144
\begin{tikzpicture}
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   145
\begin{axis}[
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   146
    xlabel={$n$},
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   147
    ylabel={size},
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   148
    ymode = log,
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   149
    legend entries={Final Derivative Size},  
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   150
    legend pos=north west,
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   151
    legend cell align=left]
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   152
\addplot[red,mark=*, mark options={fill=white}] table {SulzmannLuLexer.data};
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   153
\end{axis}
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   154
\end{tikzpicture} 
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   155
\caption{Lexing the regular expression $(a^*a^*)^*$ against strings of the form
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   156
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   157
$ using Sulzmann and Lu's lexer}\label{SulzmannLuLexer}
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   158
\end{figure}
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   159
\noindent
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   160
At $n= 20$ we already get an out of memory error with Scala's normal 
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   161
JVM heap size settings.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   162
In fact their simplification does not improve over
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   163
the simple-minded simplifications we have shown in \ref{fig:BetterWaterloo}.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   164
The time required also grows exponentially:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   165
\begin{figure}[H]
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   166
	\centering
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   167
\begin{tikzpicture}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   168
\begin{axis}[
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   169
    xlabel={$n$},
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   170
    ylabel={time},
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   171
    ymode = log,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   172
    legend entries={time in secs},  
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   173
    legend pos=north west,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   174
    legend cell align=left]
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   175
\addplot[red,mark=*, mark options={fill=white}] table {SulzmannLuLexerTime.data};
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   176
\end{axis}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   177
\end{tikzpicture} 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   178
\caption{Lexing the regular expression $(a^*a^*)^*$ against strings of the form
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   179
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   180
$ using Sulzmann and Lu's lexer}\label{SulzmannLuLexerTime}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   181
\end{figure}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   182
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   183
which seems like a counterexample for 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   184
their linear complexity claim:
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   185
\begin{quote}\it
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   186
Linear-Time Complexity Claim \\It is easy to see that each call of one of the functions/operations:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   187
simp, fuse, mkEpsBC and isPhi leads to subcalls whose number is bound by the size of the regular expression involved. We claim that thanks to aggressively applying simp this size remains finite. Hence, we can argue that the above mentioned functions/operations have constant time complexity which implies that we can incrementally compute bit-coded parse trees in linear time in the size of the input. 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   188
\end{quote}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   189
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   190
The assumption that the size of the regular expressions
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   191
in the algorithm
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   192
would stay below a finite constant is not ture.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   193
In addition to that, even if the regular expressions size
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   194
do stay finite, one has to take into account that
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   195
the $\simpsulz$ function is applied many times
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   196
in each derivative step, and that number is not necessarily
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   197
a constant with respect to the size of the regular expression.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   198
To not get ``caught off guard'' by
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   199
these counterexamples,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   200
one needs to be more careful when designing the
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   201
simplification function and making claims about them.
584
1734bd5975a3 chap4 nub
Chengsong
parents: 583
diff changeset
   202
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   203
\section{Our $\textit{Simp}$ Function}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   204
We will now introduce our simplification function,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   205
by making a contrast with $\simpsulz$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   206
We describe
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   207
the ideas behind components in their algorithm 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   208
and why they fail to achieve the desired effect, followed
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   209
by our solution. These solutions come with correctness
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   210
statements that are backed up by formal proofs.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   211
\subsection{Flattening Nested Alternatives}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   212
The idea behind the 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   213
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   214
$\simpsulz  \; _{bs}\sum ((_{bs'}\sum rs_1) :: rs_2) \quad \dn \quad
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   215
	       _{bs}\sum ((\map \; (\fuse \; bs')\; rs_1) @ rs_2)$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   216
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   217
clause is that it allows
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   218
duplicate removal of regular expressions at different
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   219
levels.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   220
For example, this would help with the
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   221
following simplification:
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   222
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   223
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   224
$(a+r)+r \longrightarrow a+r$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   225
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   226
The problem here is that only the head element
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   227
is ``spilled out'',
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   228
whereas we would want to flatten
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   229
an entire list to open up possibilities for further simplifications.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   230
Not flattening the rest of the elements also means that
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   231
the later de-duplication processs 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   232
does not fully remove apparent duplicates.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   233
For example,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   234
using $\simpsulz$ we could not 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   235
simplify
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   236
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   237
$((a^* a^*)+ (a^* + a^*))\cdot (a^*a^*)^*+
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   238
((a^*a^*)+a^*)\cdot (a^*a^*)^*$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   239
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   240
due to the underlined part not in the first element
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   241
of the alternative.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   242
We define a flatten operation that flattens not only 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   243
the first regular expression of an alternative,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   244
but the entire list: 
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   245
 \begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   246
  \begin{tabular}{@{}lcl@{}}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   247
  $\textit{flts} \; (_{bs}\sum \textit{as}) :: \textit{as'}$ & $\dn$ & $(\textit{map} \;
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   248
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flts} \; as' $ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   249
  $\textit{flts} \; \ZERO :: as'$ & $\dn$ & $ \textit{flts} \;  \textit{as'} $ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   250
    $\textit{flts} \; a :: as'$ & $\dn$ & $a :: \textit{flts} \; \textit{as'}$ \quad(otherwise) 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   251
\end{tabular}    
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   252
\end{center}  
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   253
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   254
Our $\flts$ operation 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   255
also throws away $\ZERO$s
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   256
as they do not contribute to a lexing result.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   257
\subsection{Duplicate Removal}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   258
After flattening is done, we are ready to deduplicate.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   259
The de-duplicate function is called $\distinctBy$,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   260
and that is where we make our second improvement over
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   261
Sulzmann and Lu's.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   262
The process goes as follows:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   263
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   264
$rs \stackrel{\textit{flts}}{\longrightarrow} 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   265
rs_{flat} 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   266
\xrightarrow{\distinctBy \; 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   267
rs_{flat} \; \rerases\; \varnothing} rs_{distinct}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   268
%\stackrel{\distinctBy \; 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   269
%rs_{flat} \; \erase\; \varnothing}{\longrightarrow} \; rs_{distinct}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   270
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   271
where the $\distinctBy$ function is defined as:
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   272
\begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   273
	\begin{tabular}{@{}lcl@{}}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   274
		$\distinctBy \; [] \; f\; acc $ & $ =$ & $ []$\\
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   275
		$\distinctBy \; (x :: xs) \; f \; acc$ & $=$ & $\quad \textit{if} (f \; x \in acc)\;\; \textit{then} \;\; \distinctBy \; xs \; f \; acc$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   276
						       & & $\quad \textit{else}\;\; x :: (\distinctBy \; xs \; f \; (\{f \; x\} \cup acc))$ 
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   277
	\end{tabular}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   278
\end{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   279
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   280
The reason we define a distinct function under a mapping $f$ is because
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   281
we want to eliminate regular expressions that are syntactically the same,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   282
but with different bit-codes.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   283
For example, we can remove the second $a^*a^*$ from
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   284
$_{ZSZ}a^*a^* + _{SZZ}a^*a^*$, because it
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   285
represents a match with shorter initial sub-match 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   286
(and therefore is definitely not POSIX),
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   287
and will be discarded by $\bmkeps$ later.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   288
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   289
	$_{ZSZ}\underbrace{a^*}_{ZS:\; match \; 1\; times\quad}\underbrace{a^*}_{Z: \;match\; 1 \;times} + 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   290
	_{SZZ}\underbrace{a^*}_{S: \; match \; 0 \; times\quad}\underbrace{a^*}_{ZZ: \; match \; 2 \; times}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   291
	$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   292
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   293
%$_{bs1} r_1 + _{bs2} r_2 \text{where} (r_1)_{\downarrow} = (r_2)_{\downarrow}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   294
Due to the way our algorithm works,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   295
the matches that conform to the POSIX standard 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   296
will always be placed further to the left. When we 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   297
traverse the list from left to right,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   298
regular expressions we have already seen
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   299
will definitely not contribute to a POSIX value,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   300
even if they are attached with different bitcodes.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   301
These duplicates therefore need to be removed.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   302
To achieve this, we call $\rerases$ as the function $f$ during the distinction
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   303
operation.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   304
$\rerases$ is very similar to $\erase$, except that it preserves the structure
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   305
when erasing an alternative regular expression.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   306
The reason why we use $\rerases$ instead of $\erase$ is that
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   307
it keeps the structures of alternative 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   308
annotated regular expressions
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   309
whereas $\erase$ would turn it back into a binary structure.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   310
Not having to mess with the structure 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   311
greatly simplifies the finiteness proof in chapter \ref{Finite}.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   312
We give the definitions of $\rerases$ here together with
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   313
the new datatype used by $\rerases$ (as our plain
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   314
regular expression datatype does not allow non-binary alternatives),
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   315
and explain in detail
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   316
why we want it in the next chapter.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   317
For the moment the reader can just think of 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   318
$\rerases$ as $\erase$ and $\rrexp$ as plain regular expressions.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   319
\[			\rrexp ::=   \RZERO \mid  \RONE
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   320
			 \mid  \RCHAR{c}  
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   321
			 \mid  \RSEQ{r_1}{r_2}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   322
			 \mid  \RALTS{rs}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   323
			 \mid \RSTAR{r}        
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   324
\]
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   325
The notation of $\rerases$ also follows that of $\erase$,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   326
which is a postfix operator written as a subscript,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   327
except that it has an \emph{r} attached to it to distinguish against $\erase$:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   328
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   329
\begin{tabular}{lcl}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   330
$\rerase{\ZERO}$ & $\dn$ & $\RZERO$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   331
$\rerase{_{bs}\ONE}$ & $\dn$ & $\RONE$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   332
	$\rerase{_{bs}\mathbf{c}}$ & $\dn$ & $\RCHAR{c}$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   333
$\rerase{_{bs}r_1\cdot r_2}$ & $\dn$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   334
$\rerase{_{bs}\sum as}$ & $\dn$ & $\RALTS{\map \; \rerase{\_} \; as}$\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   335
$\rerase{_{bs} a ^*}$ & $\dn$ & $\rerase{a}^*$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   336
\end{tabular}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   337
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   338
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   339
\subsection{Putting Things Together}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   340
A recursive definition of our  simplification function 
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   341
is given below:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   342
%that looks somewhat similar to our Scala code is 
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   343
\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   344
  \begin{tabular}{@{}lcl@{}}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   345
   
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   346
	  $\textit{bsimp} \; (_{bs}a_1\cdot a_2)$ & $\dn$ & $ \textit{bsimp}_{ASEQ} \; bs \;(\textit{bsimp} \; a_1) \; (\textit{bsimp}  \; a_2)  $ \\
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   347
	  $\textit{bsimp} \; (_{bs}\sum \textit{as})$ & $\dn$ & $\textit{bsimp}_{ALTS} \; \textit{bs} \; (\textit{distinctBy} \; ( \textit{flatten} ( \textit{map} \; bsimp \; as)) \; \rerases \; \varnothing) $ \\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   348
   $\textit{bsimp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   349
\end{tabular}    
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   350
\end{center}    
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   351
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   352
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   353
The simplification (named $\textit{bsimp}$ for \emph{b}it-coded) 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   354
does a pattern matching on the regular expression.
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   355
When it detected that the regular expression is an alternative or
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   356
sequence, it will try to simplify its children regular expressions
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   357
recursively and then see if one of the children turns into $\ZERO$ or
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   358
$\ONE$, which might trigger further simplification at the current level.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   359
Current level simplifications are handled by the function $\textit{bsimp}_{ASEQ}$,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   360
using rules such as  $\ZERO \cdot r \rightarrow \ZERO$ and $\ONE \cdot r \rightarrow r$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   361
\begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   362
	\begin{tabular}{@{}lcl@{}}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   363
		$\textit{bsimp}_{ASEQ} \; bs\; a \; b$ & $\dn$ & $ (a,\; b) \textit{match}$\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   364
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   365
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   366
   &&$\quad\textit{case} \;  (_{bs1}\ONE, a_2') \Rightarrow  \textit{fuse} \; (bs@bs_1) \;  a_2'$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   367
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow   _{bs}a_1' \cdot a_2'$ 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   368
	\end{tabular}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   369
\end{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   370
\noindent
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   371
The most involved part is the $\sum$ clause, where we first call $\flts$ on
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   372
the simplified children regular expression list $\textit{map}\; \textit{bsimp}\; \textit{as}$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   373
and then call $\distinctBy$ on that list, the predicate determining whether two 
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   374
elements are the same is $\rerases \; r_1 = \rerases\; r_2$.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   375
Finally, depending on whether the regular expression list $as'$ has turned into a
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   376
singleton or empty list after $\flts$ and $\distinctBy$, $\textit{bsimp}_{AALTS}$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   377
decides whether to keep the current level constructor $\sum$ as it is, and 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   378
removes it when there are less than two elements:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   379
\begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   380
	\begin{tabular}{lcl}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   381
		$\textit{bsimp}_{AALTS} \; bs \; as'$ & $ \dn$ & $ as' \; \textit{match}$\\		
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   382
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   383
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   384
   &&$\quad\textit{case} \;  as' \Rightarrow _{bs}\sum \textit{as'}$\\ 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   385
	\end{tabular}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   386
	
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   387
\end{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   388
Having defined the $\bsimp$ function,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   389
we add it as a phase after a derivative is taken,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   390
so it stays small:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   391
\begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   392
	\begin{tabular}{lcl}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   393
		$r \backslash_{bsimp} s$ & $\dn$ & $\textit{bsimp}(r \backslash s)$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   394
	\end{tabular}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   395
\end{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   396
%Following previous notations
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   397
%when extending from derivatives w.r.t.~character to derivative
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   398
%w.r.t.~string, we define the derivative that nests simplifications 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   399
%with derivatives:%\comment{simp in  the [] case?}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   400
We extend this from character to string:
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   401
\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   402
\begin{tabular}{lcl}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   403
$r \backslash_{bsimps} (c\!::\!s) $ & $\dn$ & $(r \backslash_{bsimp}\, c) \backslash_{bsimps}\, s$ \\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   404
$r \backslash_{bsimps} [\,] $ & $\dn$ & $r$
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   405
\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   406
\end{center}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   407
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   408
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   409
The lexer that extracts bitcodes from the 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   410
derivatives with simplifications from our $\simp$ function
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   411
is called $\blexersimp$:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   412
\begin{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   413
\begin{tabular}{lcl}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   414
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   415
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   416
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   417
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   418
  & & $\;\;\textit{else}\;\textit{None}$
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   419
\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   420
\end{center}
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   421
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   422
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   423
This algorithm keeps the regular expression size small.
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   424
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   425
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   426
\subsection{$(a+aa)^*$ and $(a^*\cdot a^*)^*$  against 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   427
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$ After Simplification}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   428
For example,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   429
with our simplification the
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   430
previous $(a^*a^*)^*$ example
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   431
where $\simpsulz$ could not
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   432
stop the fast growth (over
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   433
3 million nodes just below $20$ input length)
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   434
will be reduced to just 15 and stays constant, no matter how long the
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   435
input string is.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   436
This is demonstrated in the graphs below.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   437
\begin{figure}[H]
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   438
\begin{center}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   439
\begin{tabular}{ll}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   440
\begin{tikzpicture}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   441
\begin{axis}[
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   442
    xlabel={$n$},
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   443
    ylabel={derivative size},
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   444
        width=7cm,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   445
    height=4cm, 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   446
    legend entries={Lexer with $\textit{bsimp}$},  
539
Chengsong
parents: 538
diff changeset
   447
    legend pos=  south east,
Chengsong
parents: 538
diff changeset
   448
    legend cell align=left]
Chengsong
parents: 538
diff changeset
   449
\addplot[red,mark=*, mark options={fill=white}] table {BitcodedLexer.data};
Chengsong
parents: 538
diff changeset
   450
\end{axis}
Chengsong
parents: 538
diff changeset
   451
\end{tikzpicture} %\label{fig:BitcodedLexer}
Chengsong
parents: 538
diff changeset
   452
&
Chengsong
parents: 538
diff changeset
   453
\begin{tikzpicture}
Chengsong
parents: 538
diff changeset
   454
\begin{axis}[
Chengsong
parents: 538
diff changeset
   455
    xlabel={$n$},
Chengsong
parents: 538
diff changeset
   456
    ylabel={derivative size},
Chengsong
parents: 538
diff changeset
   457
    width = 7cm,
Chengsong
parents: 538
diff changeset
   458
    height = 4cm,
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   459
    legend entries={Lexer with $\simpsulz$},  
539
Chengsong
parents: 538
diff changeset
   460
    legend pos=  north west,
Chengsong
parents: 538
diff changeset
   461
    legend cell align=left]
Chengsong
parents: 538
diff changeset
   462
\addplot[red,mark=*, mark options={fill=white}] table {BetterWaterloo.data};
Chengsong
parents: 538
diff changeset
   463
\end{axis}
Chengsong
parents: 538
diff changeset
   464
\end{tikzpicture} 
Chengsong
parents: 538
diff changeset
   465
\end{tabular}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   466
\end{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   467
\caption{Our Improvement over Sulzmann and Lu's in terms of size}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   468
\end{figure}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   469
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   470
Given the size difference, it is not
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   471
surprising that our $\blexersimp$ significantly outperforms
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   472
$\textit{blexer\_sulzSimp}$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   473
In the next section we are going to establish the
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   474
first important property of our lexer--the correctness.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   475
%----------------------------------------------------------------------------------------
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   476
%	SECTION rewrite relation
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   477
%----------------------------------------------------------------------------------------
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   478
\section{Correctness of $\blexersimp$}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   479
In this section we give details
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   480
of the correctness proof of $\blexersimp$,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   481
an important contribution of this thesis.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   482
We first introduce the rewriting relation \emph{rrewrite}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   483
($\rrewrite$) between two regular expressions,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   484
which expresses an atomic
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   485
simplification step from the left-hand-side
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   486
to the right-hand-side.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   487
We then prove properties about
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   488
this rewriting relation and its reflexive transitive closure.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   489
Finally we leverage these properties to show
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   490
an equivalence between the internal data structures of 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   491
$\blexer$ and $\blexersimp$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   492
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   493
\subsection{The Rewriting Relation $\rrewrite$($\rightsquigarrow$)}
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   494
In the $\blexer$'s correctness proof, we
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   495
did not directly derive the fact that $\blexer$ gives out the POSIX value,
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   496
but first proved that $\blexer$ is linked with $\lexer$.
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   497
Then we re-use
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   498
the correctness of $\lexer$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   499
to obtain
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   500
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   501
	$(r, s) \rightarrow v \;\; \textit{iff} \;\; \blexer \; r \;s = v$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   502
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   503
Here we apply this
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   504
modularised technique again
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   505
by first proving that
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   506
$\blexersimp \; r \; s $ 
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   507
produces the same output as $\blexer \; r\; s$,
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   508
and then piecing it together with 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   509
$\blexer$'s correctness to achieve our main
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   510
theorem:\footnote{ the case when 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   511
$s$ is not in $L \; r$, is routine to establish }
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   512
\begin{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   513
	$(r, s) \rightarrow v \; \;   \textit{iff} \;\;  \blexersimp \; r \; s = v$
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   514
\end{center}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   515
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   516
The overall idea for the proof
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   517
of $\blexer \;r \;s = \blexersimp \; r \;s$ 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   518
is that the transition from $r$ to $\textit{bsimp}\; r$ can be
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   519
broken down into finitely many rewrite steps:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   520
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   521
	$r \rightsquigarrow^* \textit{bsimp} \; r$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   522
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   523
where each rewrite step, written $\rightsquigarrow$,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   524
is an ``atomic'' simplification that
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   525
cannot be broken down any further:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   526
\begin{figure}[H]
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   527
\begin{mathpar}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   528
	\inferrule * [Right = $S\ZERO_l$]{\vspace{0em}}{_{bs} \ZERO \cdot r_2 \rightsquigarrow \ZERO\\}
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   529
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   530
	\inferrule * [Right = $S\ZERO_r$]{\vspace{0em}}{_{bs} r_1 \cdot \ZERO \rightsquigarrow \ZERO\\}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   531
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   532
	\inferrule * [Right = $S_1$]{\vspace{0em}}{_{bs1} ((_{bs2} \ONE) \cdot r) \rightsquigarrow \fuse \; (bs_1 @ bs_2) \; r\\}\\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   533
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   534
	
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   535
	
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   536
	\inferrule * [Right = $SL$] {\\ r_1 \rightsquigarrow r_2}{_{bs} r_1 \cdot r_3 \rightsquigarrow _{bs} r_2 \cdot r_3\\}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   537
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   538
	\inferrule * [Right = $SR$] {\\ r_3 \rightsquigarrow r_4}{_{bs} r_1 \cdot r_3 \rightsquigarrow _{bs} r_1 \cdot r_4\\}\\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   539
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   540
	\inferrule * [Right = $A0$] {\vspace{0em}}{ _{bs}\sum [] \rightsquigarrow \ZERO}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   541
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   542
	\inferrule * [Right = $A1$] {\vspace{0em}}{ _{bs}\sum [a] \rightsquigarrow \fuse \; bs \; a}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   543
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   544
	\inferrule * [Right = $AL$] {\\ rs_1 \stackrel{s}{\rightsquigarrow} rs_2}{_{bs}\sum rs_1 \rightsquigarrow rs_2}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   545
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   546
	\inferrule * [Right = $LE$] {\vspace{0em}}{ [] \stackrel{s}{\rightsquigarrow} []}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   547
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   548
	\inferrule * [Right = $LT$] {rs_1 \stackrel{s}{\rightsquigarrow} rs_2}{ r :: rs_1 \stackrel{s}{\rightsquigarrow} r :: rs_2 }
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   549
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   550
	\inferrule * [Right = $LH$] {r_1 \rightsquigarrow r_2}{ r_1 :: rs \stackrel{s}{\rightsquigarrow} r_2 :: rs}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   551
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   552
	\inferrule * [Right = $L\ZERO$] {\vspace{0em}}{\ZERO :: rs \stackrel{s}{\rightsquigarrow} rs}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   553
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   554
	\inferrule * [Right = $LS$] {\vspace{0em}}{_{bs} \sum (rs_1 :: rs_b) \stackrel{s}{\rightsquigarrow} ((\map \; (\fuse \; bs_1) \; rs_1) @ rsb) }
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   555
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   556
	\inferrule * [Right = $LD$] {\\ \rerase{a_1} = \rerase{a_2}}{rs_a @ [a_1] @ rs_b @ [a_2] @ rsc \stackrel{s}{\rightsquigarrow} rs_a @ [a_1] @ rs_b @ rs_c}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   557
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   558
\end{mathpar}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   559
\caption{
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   560
The rewrite rules that generate simplified regular expressions 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   561
in small steps: $r_1 \rightsquigarrow r_2$ is for bitcoded regular expressions 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   562
and $rs_1 \stackrel{s}{\rightsquigarrow} rs_2$ for 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   563
lists of bitcoded regular expressions. 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   564
Interesting is the LD rule that allows copies of regular expressions 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   565
to be removed provided a regular expression 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   566
earlier in the list can match the same strings.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   567
}\label{rrewriteRules}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   568
\end{figure}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   569
\noindent
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   570
The rules such as $LT$ and $LH$ are for rewriting between two regular expression lists
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   571
such that one regular expression
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   572
in the left-hand-side list is rewritable in one step
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   573
to the right-hand-side's regular expression at the same position.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   574
This helps with defining the ``context rules'' such as $AL$.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   575
The reflexive transitive closure of $\rightsquigarrow$ and $\stackrel{s}{\rightsquigarrow}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   576
are defined in the usual way:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   577
\begin{figure}[H]
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   578
	\centering
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   579
\begin{mathpar}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   580
	\inferrule{\vspace{0em}}{ r \rightsquigarrow^* r \\}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   581
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   582
	\inferrule{\vspace{0em}}{rs \stackrel{s*}{\rightsquigarrow} rs \\}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   583
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   584
	\inferrule{r_1 \rightsquigarrow^*  r_2 \land \; r_2 \rightsquigarrow^* r_3}{r_1 \rightsquigarrow^* r_3\\}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   585
	
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   586
	\inferrule{rs_1 \stackrel{s*}{\rightsquigarrow}  rs_2 \land \; rs_2 \stackrel{s*}{\rightsquigarrow} rs_3}{rs_1 \stackrel{s*}{\rightsquigarrow} rs_3}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   587
\end{mathpar}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   588
\caption{The Reflexive Transitive Closure of 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   589
$\rightsquigarrow$ and $\stackrel{s}{\rightsquigarrow}$}\label{transClosure}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   590
\end{figure}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   591
Two rewritable terms will remain rewritable to each other
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   592
even after a derivative is taken:
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   593
\begin{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   594
	$r_1 \rightsquigarrow r_2 \implies (r_1 \backslash c) \rightsquigarrow^* (r_2 \backslash c)$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   595
\end{center}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   596
And finally, if two terms are rewritable to each other,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   597
then they produce the same bitcodes:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   598
\begin{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   599
	$r \rightsquigarrow^* r' \;\; \textit{then} \; \; \bmkeps \; r = \bmkeps \; r'$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   600
\end{center}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   601
The decoding phase of both $\blexer$ and $\blexersimp$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   602
are the same, which means that if they get the same
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   603
bitcodes before the decoding phase,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   604
they get the same value after decoding is done.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   605
We will prove the three properties 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   606
we mentioned above in the next sub-section.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   607
\subsection{Important Properties of $\rightsquigarrow$}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   608
First we prove some basic facts 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   609
about $\rightsquigarrow$, $\stackrel{s}{\rightsquigarrow}$, 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   610
$\rightsquigarrow^*$ and $\stackrel{s*}{\rightsquigarrow}$,
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   611
which will be needed later.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   612
The inference rules (\ref{rrewriteRules}) we 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   613
gave in the previous section 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   614
have their ``many-steps version'':
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   615
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   616
\begin{lemma}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   617
	\hspace{0em}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   618
	\begin{itemize}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   619
		\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   620
			$rs_1 \stackrel{s*}{\rightsquigarrow} rs_2 \implies _{bs} \sum rs_1 \stackrel{*}{\rightsquigarrow} _{bs} \sum rs_2$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   621
		\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   622
			$r \stackrel{*}{\rightsquigarrow} r' \implies _{bs} \sum r :: rs \stackrel{*}{\rightsquigarrow}  _{bs} \sum r' :: rs$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   623
	\end{itemize}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   624
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   625
\begin{proof}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   626
	By an induction on 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   627
	the inductive cases of $\stackrel{s*}{\rightsquigarrow}$ and $\rightsquigarrow^*$ respectively.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   628
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   629
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   630
The inference rules of $\stackrel{s}{\rightsquigarrow}$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   631
are defined in terms of list cons operation, here
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   632
we establish that the 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   633
$\stackrel{s}{\rightsquigarrow}$ and $\stackrel{s*}{\rightsquigarrow}$ 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   634
relation is also preserved w.r.t appending and prepending of a list:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   635
\begin{lemma}\label{ssgqTossgs}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   636
	\hspace{0em}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   637
	\begin{itemize}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   638
		\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   639
			$rs_1 \stackrel{s}{\rightsquigarrow} rs_2 \implies rs @ rs_1 \stackrel{s}{\rightsquigarrow} rs @ rs_2$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   640
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   641
		\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   642
			$rs_1 \stackrel{s*}{\rightsquigarrow} rs_2 \implies 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   643
			rs @ rs_1 \stackrel{s*}{\rightsquigarrow} rs @ rs_2$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   644
		\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   645
			The $\stackrel{s}{\rightsquigarrow} $ relation after appending 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   646
			a list becomes $\stackrel{s*}{\rightsquigarrow}$:\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   647
			$rs_1 \stackrel{s}{\rightsquigarrow} rs_2 \implies rs_1 @ rs \stackrel{s*}{\rightsquigarrow} rs_2 @ rs$
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   648
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   649
	\end{itemize}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   650
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   651
\begin{proof}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   652
	The first part is by induction on the list $rs$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   653
	The second part is by induction on the inductive cases of $\stackrel{s*}{\rightsquigarrow}$.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   654
	The third part is 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   655
	by rule induction of $\stackrel{s}{\rightsquigarrow}$.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   656
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   657
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   658
	$rs_1 \stackrel{s*}{\rightsquigarrow} rs_2 \implies rs_1 @ rs \stackrel{s*}{\rightsquigarrow} rs_2 @ rs$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   659
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   660
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   661
	By rule induction of $\stackrel{s*}{\rightsquigarrow}$ and using \ref{ssgqTossgs}.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   662
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   663
Here are two lemmas relating $\stackrel{*}{\rightsquigarrow}$ and $\stackrel{s*}{\rightsquigarrow}$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   664
\begin{lemma}\label{singleton}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   665
	$r_1 \stackrel{*}{\rightsquigarrow} r_2 \implies [r_1] \stackrel{s*}{\rightsquigarrow} [r_2]$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   666
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   667
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   668
	By rule induction of $ \stackrel{*}{\rightsquigarrow} $.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   669
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   670
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   671
	$rs_3 \stackrel{s*}{\rightsquigarrow} rs_4 \land r_1 \stackrel{*}{\rightsquigarrow}  r_2 \implies
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   672
	r_2 :: rs_3 \stackrel{s*}{\rightsquigarrow} r_2 :: rs_4$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   673
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   674
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   675
	By using \ref{singleton}.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   676
\end{proof}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   677
In this section we give details for the proofs of the below properties:
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   678
\begin{itemize}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   679
	\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   680
		$(r \stackrel{*}{\rightsquigarrow} r'\land \bnullable \; r_1) 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   681
		\implies \bmkeps \; r = \bmkeps \; r'$. \\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   682
		If we can rewrite 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   683
		in many steps from $r$ to $r'$, then they  
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   684
		will produce the same bitcodes 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   685
		under $\bmkeps$. 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   686
	\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   687
		$r \rightsquigarrow^* \textit{bsimp} \;r$.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   688
		The simplification function
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   689
		$\textit{bsimp}$ only transforms the regex $r$ using steps specified by 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   690
		$\rightsquigarrow^*$ and nothing else.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   691
	\item
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   692
		$r \rightsquigarrow r' \implies r \backslash c \rightsquigarrow^* r'\backslash c$.\\
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   693
		The rewritability relation $\rightsquigarrow$ is preserved under derivatives--
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   694
		it is just that we might need more steps.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   695
\end{itemize}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   696
These properties would work together towards the correctness theorem.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   697
We start proving each of these lemmas below.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   698
\subsubsection{Property 1: $(r \stackrel{*}{\rightsquigarrow} r'\land \bnullable \; r_1) \implies \bmkeps \; r = \bmkeps \; r'$}
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   699
Intuitively this property says we can 
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   700
extract the same bitcodes from the nullable
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   701
components of two regular expressions
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   702
if we can rewrite from one to the other.
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   703
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   704
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   705
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   706
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   707
Now we get to the "meaty" part of the proof, which relates the relations $\stackrel{s*}{\rightsquigarrow}$ and 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   708
$\stackrel{*}{\rightsquigarrow} $ with our simplification components such $\distinctBy$ and $\flts$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   709
The first lemma below says that for a list made of two parts $rs_1 @ rs_2$, one can throw away the duplicate
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   710
elements in $rs_2$, as well as those that have appeared in $rs_1$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   711
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   712
	$rs_1 @ rs_2 \stackrel{s*}{\rightsquigarrow} (rs_1 @ (\distinctBy \; rs_2 \; \; \rerase{\_}\;  \; (\map\;\; \rerase{\_}\; \; rs_1)))$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   713
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   714
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   715
	By induction on $rs_2$, where $rs_1$ is allowed to be arbitrary.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   716
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   717
The above h as the corollary that is suitable for the actual way $\distinctBy$ is called in $\bsimp$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   718
\begin{lemma}\label{dBPreserves}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   719
	$rs_ 1 \rightarrow \distinctBy \; rs_1 \; \phi$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   720
\end{lemma}
538
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   721
8016a2480704 intro and chap2
Chengsong
parents: 532
diff changeset
   722
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   723
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   724
The flatten function $\flts$ works within the $\rightsquigarrow$ relation:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   725
\begin{lemma}\label{fltsPreserves}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   726
	$rs \stackrel{s*}{\rightsquigarrow} \flts \; rs$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   727
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   728
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   729
The rewriting in many steps property is composible in terms of regular expression constructors:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   730
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   731
	$r_1 \stackrel{*}{\rightsquigarrow} r_2 \implies _{bs} r_1 \cdot r_3 \stackrel{*}{\rightsquigarrow} \;  _{bs} r_2 \cdot r_3 \quad $ and 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   732
$r_3 \stackrel{*}{\rightsquigarrow} r_4 \implies _{bs} r_1 \cdot r_3 \stackrel{*}{\rightsquigarrow} _{bs} \; r_1 \cdot r_4$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   733
\end{lemma}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   734
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   735
The rewriting in many steps properties $\stackrel{*}{\rightsquigarrow}$ and $\stackrel{s*}{\rightsquigarrow}$ is preserved under the function $\fuse$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   736
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   737
	$r_1 \stackrel{*}{\rightsquigarrow} r_2 \implies \fuse \; bs \; r_1 \stackrel{*}{\rightsquigarrow} \; \fuse \; bs \; r_2 \quad $ and 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   738
	$rs_1 \stackrel{s}{\rightsquigarrow} rs_2 \implies \map \; (\fuse \; bs) \; rs_1 \stackrel{s*}{\rightsquigarrow} \map \; (\fuse \; bs) \; rs_2$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   739
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   740
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   741
	By the properties $r_1 \rightsquigarrow r_2 \implies \fuse \; bs \; r_1 \implies \fuse \; bs \; r_2$ and
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   742
	$rs_2 \stackrel{s}{\rightsquigarrow} rs_3 \implies \map \; (\fuse \; bs) rs_2 \stackrel{s*}{\rightsquigarrow} \map \; (\fuse \; bs)\; rs_3$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   743
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   744
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   745
If we could rewrite a regular expression in many steps to $\ZERO$, then 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   746
we could also rewrite any sequence containing it to $\ZERO$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   747
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   748
	$r_1 \stackrel{*}{\rightsquigarrow} \ZERO \implies _{bs}r_1\cdot r_2 \stackrel{*}{\rightsquigarrow} \ZERO$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   749
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   750
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   751
	$\bmkeps \; (r \backslash s) = \bmkeps \; \bderssimp{r}{s}$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   752
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   753
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   754
The function $\bsimpalts$ preserves rewritability:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   755
\begin{lemma}\label{bsimpaltsPreserves}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   756
	$_{bs} \sum rs \stackrel{*}{\rightsquigarrow} \bsimpalts \; _{bs} \; rs$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   757
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   758
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   759
Before we give out the next lemmas, we define a predicate for a list of regular expressions
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   760
having at least one nullable regular expressions:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   761
\begin{definition}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   762
	$\textit{bnullables} \; rs \dn  \exists r \in rs. \bnullable r$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   763
\end{definition}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   764
The rewriting relation $\rightsquigarrow$ preserves nullability:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   765
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   766
	$r_1 \rightsquigarrow r_2 \implies  \bnullable \; r_1 = \bnullable \; r_2$ and
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   767
	$rs_1 \stackrel{s}{\rightsquigarrow} rs_2 \implies \textit{bnullables} \; rs_1 = \textit{bnullables} \; rs_2$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   768
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   769
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   770
	By rule induction of $\rightarrow$ and $\stackrel{s}{\rightsquigarrow}$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   771
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   772
So does the many steps rewriting:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   773
\begin{lemma}\label{rewritesBnullable}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   774
	$r_1 \stackrel{*}{\rightsquigarrow}  r_2 \implies \bnullable \; r_1 = \bnullable \; r_2$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   775
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   776
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   777
	By rule induction of $\stackrel{*}{\rightsquigarrow} $.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   778
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   779
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   780
And if both regular expressions in a rewriting relation are nullable, then they 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   781
produce the same bit-codes:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   782
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   783
\begin{lemma}\label{rewriteBmkepsAux}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   784
	$r_1 \rightsquigarrow r_2 \implies (\bnullable \; r_1 \land \bnullable \; r_2 \implies \bmkeps \; r_1 = 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   785
	\bmkeps \; r_2)$ and
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   786
	$rs_ 1 \stackrel{s}{\rightsquigarrow} rs_2 \implies (\bnullables \; rs_1 \land \bnullables \; rs_2 \implies 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   787
	\bmkepss \; rs_1 = \bmkepss \; rs2)$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   788
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   789
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   790
The definition of $\bmkepss$ on list $rs$ is just to extract the bit-codes on the first element in $rs$ that 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   791
is $bnullable$:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   792
\begin{center}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   793
	\begin{tabular}{lcl}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   794
		$\bmkepss \; [] $ & $\dn$ & $[]$\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   795
		$\bmkepss \; r :: rs$ & $\dn$ & $\textit{if} \; \bnullable \; r then \; (\bmkeps \; r) \; \textit{else} \; \bmkepss \; rs$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   796
	\end{tabular}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   797
\end{center}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   798
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   799
And now we are ready to prove the key property that if you 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   800
have two regular expressions, one rewritable in many steps to the other,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   801
and one of them is $\bnullable$, then they will both yield the same bits under $\bmkeps$:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   802
\begin{lemma}
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   803
	$\text{If} \;\; r \stackrel{*}{\rightsquigarrow} r' \;\; \text{and} \;\; \bnullable \; r_1 \;\; \text{then} \;\; \bmkeps \; r = \bmkeps \; r'$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   804
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   805
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   806
	By rule induction of $\stackrel{*}{\rightsquigarrow} $, using \ref{rewriteBmkepsAux} and $\ref{rewritesBnullable}$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   807
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   808
\noindent
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   809
\subsubsection{Property 2: $r \stackrel{*}{\rightsquigarrow} \bsimp{r}$}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   810
the other property is also ready:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   811
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   812
	$r \stackrel{*}{\rightsquigarrow} \bsimp{r}$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   813
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   814
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   815
	By an induction on $r$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   816
The most difficult case would be the alternative case, where we using properties such as \ref{bsimpaltsPreserves} and \ref{fltsPreserves} and \ref{dBPreserves}, we could continuously rewrite a list like:\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   817
	$rs \stackrel{s*}{\rightsquigarrow} \map \; \textit{bsimp} \; rs$\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   818
	$\ldots \stackrel{s*}{\rightsquigarrow} \flts \; (\map \; \textit{bsimp} \; rs)$\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   819
	$\ldots \;\stackrel{s*}{\rightsquigarrow} \distinctBy \; (\flts \; (\map \; \textit{bsimp}\; rs)) \; \rerase \; \phi$\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   820
	Then we could do the following regular expresssion many steps rewrite:\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   821
	$ _{bs} \sum \distinctBy \; (\flts \; (\map \; \textit{bsimp}\; rs)) \; \rerase \; \phi \stackrel{*}{\rightsquigarrow} \bsimpalts \; bs \; (\distinctBy \; (\flts \; (\map \; \textit{bsimp}\; rs)) \; \rerase \; \phi)$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   822
	\\
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   823
	
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   824
\end{proof}
585
4969ef817d92 chap4 more
Chengsong
parents: 584
diff changeset
   825
\subsubsection{Property 3: $r_1 \stackrel{*}{\rightsquigarrow}  r_2 \implies r_1 \backslash c \stackrel{*}{\rightsquigarrow} r_2 \backslash c$}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   826
The first thing we prove is that if we could rewrite in one step, then after derivative
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   827
we could rewrite in many steps:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   828
\begin{lemma}\label{rewriteBder}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   829
	$r_1 \rightsquigarrow r_2 \implies r_1 \backslash c \stackrel{*}{\rightsquigarrow}  r_2 \backslash c$ and
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   830
	$rs_1 \stackrel{s}{\rightsquigarrow} rs_2 \implies \map \; (\_\backslash c) \; rs_1 \stackrel{s*}{\rightsquigarrow} \map \; (\_ \backslash c) \; rs_2$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   831
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   832
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   833
	By induction on $\rightsquigarrow$ and $\stackrel{s}{\rightsquigarrow}$, using a number of the previous lemmas.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   834
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   835
Now we can prove that once we could rewrite from one expression to another in many steps,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   836
then after a derivative on both sides we could still rewrite one to another in many steps:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   837
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   838
	$r_1 \stackrel{*}{\rightsquigarrow}  r_2 \implies r_1 \backslash c \stackrel{*}{\rightsquigarrow}  r_2 \backslash c$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   839
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   840
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   841
	By rule induction of $\stackrel{*}{\rightsquigarrow} $ and using the previous lemma :\ref{rewriteBder}.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   842
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   843
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   844
This can be extended and combined with the previous two important properties
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   845
so that a regular expression's successivve derivatives can be rewritten in many steps
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   846
to its simplified counterpart:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   847
\begin{lemma}\label{bderBderssimp}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   848
	$a \backslash s \stackrel{*}{\rightsquigarrow} \bderssimp{a}{s} $
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   849
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   850
\subsection{Main Theorem}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   851
Now with \ref{bdersBderssimp} we are ready for the main theorem.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   852
To link $\blexersimp$ and $\blexer$, 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   853
we first say that they give out the same bits, if the lexing result is a match:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   854
\begin{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   855
	$\bnullable \; (a \backslash s) \implies \bmkeps \; (a \backslash s) = \bmkeps \; (\bderssimp{a}{s})$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   856
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   857
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   858
Now that they give out the same bits, we know that they give the same value after decoding,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   859
which we know is correct value as $\blexer$ is correct:
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   860
\begin{theorem}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   861
	$\blexer \; r \; s = \blexersimp{r}{s}$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   862
\end{theorem}
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   863
\noindent
576
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   864
\begin{proof}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   865
	One can rewrite in many steps from the original lexer's derivative regular expressions to the 
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   866
	lexer with simplification applied:
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   867
	$a \backslash s \stackrel{*}{\rightsquigarrow} \bderssimp{a}{s} $.
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   868
	If two regular expressions are rewritable, then they produce the same bits.
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   869
	Under the condition that $ r_1$ is nullable, we have
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   870
	$\text{If} \;\; r \stackrel{*}{\rightsquigarrow} r', \;\;\text{then} \;\; \bmkeps \; r = \bmkeps \; r'$.
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   871
	This proves the \emph{if} (interesting) branch of
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   872
	$\blexer \; r \; s = \blexersimp{r}{s}$.
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   873
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   874
\end{proof}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   875
\noindent
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   876
As a corollary,
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   877
we link this result with the lemma we proved earlier that 
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   878
\begin{center}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   879
	$(r, s) \rightarrow v \implies \blexer \; r \; s = v$
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   880
\end{center}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   881
and obtain the corollary that the bit-coded lexer with simplification is
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   882
indeed correctly outputting POSIX lexing result, if such a result exists.
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   883
\begin{corollary}
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   884
	$(r, s) \rightarrow v \implies \blexersimp{r}{s}$
3e1b699696b6 thesis chap5
Chengsong
parents: 543
diff changeset
   885
\end{corollary}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   886
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   887
\subsection{Comments on the Proof Techniques Used}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   888
The non-trivial part of proving the correctness of the algorithm with simplification
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   889
compared with not having simplification is that we can no longer use the argument 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   890
in \cref{flex_retrieve}.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   891
The function \retrieve needs the cumbersome structure of the (umsimplified)
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   892
annotated regular expression to 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   893
agree with the structure of the value, but simplification will always mess with the 
543
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   894
structure.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   895
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   896
We also tried to prove $\bsimp{\bderssimp{a}{s}} = \bsimp{a\backslash s}$,
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   897
but this turns out to be not true, A counterexample of this being
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   898
\[ r = [(1+c)\cdot [aa \cdot (1+c)]] \land s = aa
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   899
\]
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   900
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   901
Then we would have $\bsimp{a \backslash s}$ being 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   902
$_{[]}(_{ZZ}\ONE +  _{ZS}c ) $
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   903
whereas $\bsimp{\bderssimp{a}{s}}$ would be 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   904
$_{Z}(_{Z} \ONE + _{S} c)$.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   905
Unfortunately if we apply $\textit{bsimp}$ at different
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   906
stages we will always have this discrepancy, due to 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   907
whether the $\map \; (\fuse\; bs) \; as$ operation in $\textit{bsimp}$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   908
is taken at some points will be entirely dependant on when the simplification 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   909
take place whether there is a larger alternative structure surrounding the 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   910
alternative being simplified.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   911
The good thing about $\stackrel{*}{\rightsquigarrow} $ is that it allows
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   912
us not specify how exactly the "atomic" simplification steps $\rightsquigarrow$
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   913
are taken, but simply say that they can be taken to make two similar 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   914
regular expressions equal, and can be done after interleaving derivatives
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   915
and simplifications.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   916
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   917
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   918
Having correctness property is good. But we would also like the lexer to be efficient in 
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   919
some sense, for exampe, not grinding to a halt at certain cases.
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   920
In the next chapter we shall prove that for a given $r$, the internal derivative size is always
b2bea5968b89 thesis_thys
Chengsong
parents: 539
diff changeset
   921
finitely bounded by a constant.
582
3e19073e91f4 chap3 done
Chengsong
parents: 579
diff changeset
   922
we would expect in the