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