ChengsongTanPhdThesis/Chapters/Finite.tex
author Chengsong
Sun, 06 Nov 2022 23:05:47 +0000
changeset 618 233cf2b97d1a
parent 614 d5e9bcb384ec
child 620 ae6010c14e49
permissions -rwxr-xr-x
chapter 5 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
\chapter{Finiteness Bound} % Main chapter title
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
\label{Finite} 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
%  In Chapter 4 \ref{Chapter4} we give the second guarantee
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     7
%of our bitcoded algorithm, that is a finite bound on the size of any 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     8
%regex's derivatives. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     9
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    10
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    11
In this chapter we give a guarantee in terms of size of derivatives: 
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    12
given an annotated regular expression $a$, for any string $s$
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    13
our algorithm $\blexersimp$'s internal annotated regular expression 
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    14
size  is finitely bounded
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    15
by a constant  that only depends on $a$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    16
Formally we show that there exists an $N_a$ such that
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
    17
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    18
	$\llbracket \bderssimp{a}{s} \rrbracket \leq N_a$
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
    19
\end{center}
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
    20
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    21
where the size ($\llbracket \_ \rrbracket$) of 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    22
an annotated regular expression is defined
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    23
in terms of the number of nodes in its 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    24
tree structure (its recursive definition given in the next page).
613
Chengsong
parents: 611
diff changeset
    25
We believe this size bound
Chengsong
parents: 611
diff changeset
    26
is important in the context of POSIX lexing, because 
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    27
\begin{itemize}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    28
	\item
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    29
		It is a stepping stone towards the goal 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    30
		of eliminating ``catastrophic backtracking''. 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    31
		If the internal data structures used by our algorithm
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    32
		grows beyond a finite bound, then clearly 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    33
		the algorithm (which traverses these structures) will
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    34
		be slow.
613
Chengsong
parents: 611
diff changeset
    35
		The next step would be to refine the bound $N_a$ so that it
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    36
		is polynomial on $\llbracket a\rrbracket$.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    37
	\item
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    38
		Having the finite bound formalised 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    39
		gives us a higher confidence that
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    40
		our simplification algorithm $\simp$ does not ``mis-behave''
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    41
		like $\simpsulz$ does.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    42
		The bound is universal for a given regular expression, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    43
		which is an advantage over work which 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    44
		only gives empirical evidence on some test cases (see Verbatim++\cite{Verbatimpp}).
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    45
\end{itemize}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    46
In the next section we describe in more detail
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    47
what the finite bound means in our algorithm
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    48
and why the size of the internal data structures of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    49
a typical derivative-based lexer such as
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    50
Sulzmann and Lu's need formal treatment.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    51
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
    52
\section{Formalising About Size}
577
f47fc4840579 thesis chap2
Chengsong
parents: 576
diff changeset
    53
\noindent
613
Chengsong
parents: 611
diff changeset
    54
In our lexer ($\blexersimp$),
Chengsong
parents: 611
diff changeset
    55
we take an annotated regular expression as input,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    56
and repeately take derivative of and simplify it.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    57
\begin{figure}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    58
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    59
		\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    60
			$\llbracket _{bs}\ONE \rrbracket$ & $\dn$ & $1$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    61
			$\llbracket \ZERO \rrbracket$ & $\dn$ & $1$ \\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    62
			$\llbracket _{bs} r_1 \cdot r_2 \rrbracket$ & $\dn$ & $\llbracket r_1 \rrbracket + \llbracket r_2 \rrbracket + 1$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    63
			$\llbracket _{bs}\mathbf{c} \rrbracket $ & $\dn$ & $1$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    64
			$\llbracket _{bs}\sum as \rrbracket $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket)\; as   + 1$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    65
			$\llbracket _{bs} a^* \rrbracket $ & $\dn$ & $\llbracket a \rrbracket + 1$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    66
		\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    67
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    68
	\caption{The size function of bitcoded regular expressions}\label{brexpSize}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    69
\end{figure}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    70
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    71
\begin{figure}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    72
	\begin{tikzpicture}[scale=2,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    73
		every node/.style={minimum size=11mm},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    74
		->,>=stealth',shorten >=1pt,auto,thick
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    75
		]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    76
		\node (r0) [rectangle, draw=black, thick, minimum size = 5mm, draw=blue] {$a$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    77
		\node (r1) [rectangle, draw=black, thick, right=of r0, minimum size = 7mm]{$a_1$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    78
		\draw[->,line width=0.2mm](r0)--(r1) node[above,midway] {$\backslash c_1$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    79
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    80
		\node (r1s) [rectangle, draw=blue, thick, right=of r1, minimum size=6mm]{$a_{1s}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    81
		\draw[->, line width=0.2mm](r1)--(r1s) node[above, midway] {$\simp$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    82
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    83
		\node (r2) [rectangle, draw=black, thick,  right=of r1s, minimum size = 12mm]{$a_2$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    84
		\draw[->,line width=0.2mm](r1s)--(r2) node[above,midway] {$\backslash c_2$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    85
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    86
		\node (r2s) [rectangle, draw = blue, thick, right=of r2,minimum size=6mm]{$a_{2s}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    87
		\draw[->,line width=0.2mm](r2)--(r2s) node[above,midway] {$\simp$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    88
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    89
		\node (rns) [rectangle, draw = blue, thick, right=of r2s,minimum size=6mm]{$a_{ns}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    90
		\draw[->,line width=0.2mm, dashed](r2s)--(rns) node[above,midway] {$\backslash \ldots$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    91
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    92
		\node (v) [circle, thick, draw, right=of rns, minimum size=6mm, right=1.7cm]{$v$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    93
		\draw[->, line width=0.2mm](rns)--(v) node[above, midway] {\bmkeps} node [below, midway] {\decode};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    94
	\end{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
    95
	\caption{Regular expression size change during our $\blexersimp$ algorithm}\label{simpShrinks}
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    96
\end{figure}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
    97
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
    98
\noindent
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
    99
Each time
613
Chengsong
parents: 611
diff changeset
   100
a derivative is taken, the regular expression might grow.
Chengsong
parents: 611
diff changeset
   101
However, the simplification that is immediately afterwards will always shrink it so that 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   102
it stays relatively small.
577
f47fc4840579 thesis chap2
Chengsong
parents: 576
diff changeset
   103
This intuition is depicted by the relative size
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   104
change between the black and blue nodes:
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   105
After $\simp$ the node always shrinks.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   106
Our proof states that all the blue nodes
613
Chengsong
parents: 611
diff changeset
   107
stay below a size bound $N_a$ determined by the input $a$.
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   108
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   109
\noindent
613
Chengsong
parents: 611
diff changeset
   110
Sulzmann and Lu's assumed a similar picture about their algorithm,
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   111
though in fact their algorithm's size might be better depicted by the following graph:
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   112
\begin{figure}[H]
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   113
	\begin{tikzpicture}[scale=2,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   114
		every node/.style={minimum size=11mm},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   115
		->,>=stealth',shorten >=1pt,auto,thick
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   116
		]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   117
		\node (r0) [rectangle, draw=black, thick, minimum size = 5mm, draw=blue] {$a$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   118
		\node (r1) [rectangle, draw=black, thick, right=of r0, minimum size = 7mm]{$a_1$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   119
		\draw[->,line width=0.2mm](r0)--(r1) node[above,midway] {$\backslash c_1$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   120
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   121
		\node (r1s) [rectangle, draw=blue, thick, right=of r1, minimum size=7mm]{$a_{1s}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   122
		\draw[->, line width=0.2mm](r1)--(r1s) node[above, midway] {$\simp'$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   123
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   124
		\node (r2) [rectangle, draw=black, thick,  right=of r1s, minimum size = 17mm]{$a_2$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   125
		\draw[->,line width=0.2mm](r1s)--(r2) node[above,midway] {$\backslash c_2$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   126
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   127
		\node (r2s) [rectangle, draw = blue, thick, right=of r2,minimum size=14mm]{$a_{2s}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   128
		\draw[->,line width=0.2mm](r2)--(r2s) node[above,midway] {$\simp'$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   129
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   130
		\node (r3) [rectangle, draw = black, thick, right= of r2s, minimum size = 22mm]{$a_3$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   131
		\draw[->,line width=0.2mm](r2s)--(r3) node[above,midway] {$\backslash c_3$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   132
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   133
		\node (rns) [right = of r3, draw=blue, minimum size = 20mm]{$a_{3s}$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   134
		\draw[->,line width=0.2mm] (r3)--(rns) node [above, midway] {$\simp'$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   135
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   136
		\node (rnn) [right = of rns, minimum size = 1mm]{};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   137
		\draw[->, dashed] (rns)--(rnn) node [above, midway] {$\ldots$};
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   138
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   139
	\end{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   140
	\caption{Regular expression size change during our $\blexersimp$ algorithm}\label{sulzShrinks}
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   141
\end{figure}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   142
\noindent
613
Chengsong
parents: 611
diff changeset
   143
The picture means that on certain cases their lexer (where they use $\simpsulz$ 
Chengsong
parents: 611
diff changeset
   144
as the simplification function)
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   145
will have a size explosion, causing the running time 
613
Chengsong
parents: 611
diff changeset
   146
of each derivative step to grow continuously (for example 
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   147
in \ref{SulzmannLuLexerTime}).
613
Chengsong
parents: 611
diff changeset
   148
They tested out the run time of their
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   149
lexer on particular examples such as $(a+b+ab)^*$
613
Chengsong
parents: 611
diff changeset
   150
and claimed that their algorithm is linear w.r.t to the input.
Chengsong
parents: 611
diff changeset
   151
With our mecahnised proof, we avoid this type of unintentional
Chengsong
parents: 611
diff changeset
   152
generalisation.\\
Chengsong
parents: 611
diff changeset
   153
Chengsong
parents: 611
diff changeset
   154
Before delving in to the details of the formalisation,
Chengsong
parents: 611
diff changeset
   155
we are going to provide an overview of it.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   156
In the next subsection, we give a high-level view
613
Chengsong
parents: 611
diff changeset
   157
of the proof.
Chengsong
parents: 611
diff changeset
   158
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   159
577
f47fc4840579 thesis chap2
Chengsong
parents: 576
diff changeset
   160
\subsection{Overview of the Proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   161
A high-level overview of the main components of the finiteness proof
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   162
is as follows:
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   163
\begin{figure}[H]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   164
	\begin{tikzpicture}[scale=1,font=\bf,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   165
		node/.style={
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   166
			rectangle,rounded corners=3mm,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   167
			ultra thick,draw=black!50,minimum height=18mm, 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   168
			minimum width=20mm,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   169
		top color=white,bottom color=black!20}]
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   170
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   171
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   172
		\node (0) at (-5,0) 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   173
			[node, text width=1.8cm, text centered] 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   174
			{$\llbracket \bderssimp{a}{s} \rrbracket$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   175
		\node (A) at (0,0) 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   176
			[node,text width=1.6cm,  text centered] 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   177
			{$\llbracket \rderssimp{r}{s} \rrbracket_r$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   178
		\node (B) at (3,0) 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   179
			[node,text width=3.0cm, anchor=west, minimum width = 40mm] 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   180
			{$\llbracket \textit{ClosedForm}(r, s)\rrbracket_r$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   181
		\node (C) at (9.5,0) [node, minimum width=10mm] {$N_r$};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   182
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   183
		\draw [->,line width=0.5mm] (0) -- 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   184
			node [above,pos=0.45] {=} (A) node [below, pos = 0.45] {$(r = a \downarrow_r)$} (A); 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   185
		\draw [->,line width=0.5mm] (A) -- 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   186
			node [above,pos=0.35] {$\quad =\ldots=$} (B); 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   187
		\draw [->,line width=0.5mm] (B) -- 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   188
			node [above,pos=0.35] {$\quad \leq \ldots \leq$} (C); 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   189
	\end{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   190
	%\caption{
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   191
\end{figure}
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   192
\noindent
577
f47fc4840579 thesis chap2
Chengsong
parents: 576
diff changeset
   193
We explain the steps one by one:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   194
\begin{itemize}
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   195
	\item
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   196
		We first introduce the operations such as 
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   197
		derivatives, simplification, size calculation, etc.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   198
		associated with $\rrexp$s, which we have introduced
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   199
		in chapter \ref{Bitcoded2}.
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   200
		The operations on $\rrexp$s are identical to those on
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   201
		annotated regular expressions except that they dispense with
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   202
		bitcodes. This means that all proofs about size of $\rrexp$s will apply to
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   203
		annotated regular expressions, because the size of a regular
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   204
		expression is independent of the bitcodes.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   205
	\item
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   206
		We prove that $\rderssimp{r}{s} = \textit{ClosedForm}(r, s)$,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   207
		where $\textit{ClosedForm}(r, s)$ is entirely 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   208
		given as the derivatives of their children regular 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   209
		expressions.
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   210
		We call the right-hand-side the \emph{Closed Form}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   211
		of the derivative $\rderssimp{r}{s}$.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
   212
	\item
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   213
		Formally we give an estimate of 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   214
		$\llbracket \textit{ClosedForm}(r, s) \rrbracket_r$.
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   215
		The key observation is that $\distinctBy$'s output is
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   216
		a list with a constant length bound.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   217
\end{itemize}
594
Chengsong
parents: 593
diff changeset
   218
We will expand on these steps in the next sections.\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   219
613
Chengsong
parents: 611
diff changeset
   220
\section{The $\textit{Rrexp}$ Datatype}
594
Chengsong
parents: 593
diff changeset
   221
The first step is to define 
Chengsong
parents: 593
diff changeset
   222
$\textit{rrexp}$s.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   223
They are annotated regular expressions without bitcodes,
594
Chengsong
parents: 593
diff changeset
   224
allowing a much simpler size bound proof.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   225
%Of course, the bits which encode the lexing information 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   226
%would grow linearly with respect 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   227
%to the input, which should be taken into account when we wish to tackle the runtime comlexity.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   228
%But for the sake of the structural size 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   229
%we can safely ignore them.\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   230
The datatype 
594
Chengsong
parents: 593
diff changeset
   231
definition of the $\rrexp$, called
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   232
\emph{r-regular expressions},
594
Chengsong
parents: 593
diff changeset
   233
was initially defined in \ref{rrexpDef}.
Chengsong
parents: 593
diff changeset
   234
The reason for the prefix $r$ is
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   235
to make a distinction  
594
Chengsong
parents: 593
diff changeset
   236
with basic regular expressions.
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   237
\[			\rrexp ::=   \RZERO \mid  \RONE
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   238
	\mid  \RCHAR{c}  
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   239
	\mid  \RSEQ{r_1}{r_2}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   240
	\mid  \RALTS{rs}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   241
	\mid \RSTAR{r}        
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   242
\]
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   243
The size of an r-regular expression is
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   244
written $\llbracket r\rrbracket_r$, 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   245
whose definition mirrors that of an annotated regular expression. 
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   246
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   247
	\begin{tabular}{lcl}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   248
		$\llbracket _{bs}\ONE \rrbracket_r$ & $\dn$ & $1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   249
		$\llbracket \ZERO \rrbracket_r$ & $\dn$ & $1$ \\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   250
		$\llbracket _{bs} r_1 \cdot r_2 \rrbracket_r$ & $\dn$ & $\llbracket r_1 \rrbracket_r + \llbracket r_2 \rrbracket_r + 1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   251
		$\llbracket _{bs}\mathbf{c} \rrbracket_r $ & $\dn$ & $1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   252
		$\llbracket _{bs}\sum as \rrbracket_r $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket_r)\; as   + 1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   253
		$\llbracket _{bs} a^* \rrbracket_r $ & $\dn$ & $\llbracket a \rrbracket_r + 1$.
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   254
	\end{tabular}
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   255
\end{center}
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   256
\noindent
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   257
The $r$ in the subscript of $\llbracket \rrbracket_r$ is to 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   258
differentiate with the same operation for annotated regular expressions.
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   259
Adding $r$ as subscript will be used in 
594
Chengsong
parents: 593
diff changeset
   260
other operations as well.\\
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   261
The transformation from an annotated regular expression
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   262
to an r-regular expression is straightforward.
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   263
\begin{center}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   264
	\begin{tabular}{lcl}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   265
		$\rerase{\ZERO}$ & $\dn$ & $\RZERO$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   266
		$\rerase{_{bs}\ONE}$ & $\dn$ & $\RONE$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   267
		$\rerase{_{bs}\mathbf{c}}$ & $\dn$ & $\RCHAR{c}$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   268
		$\rerase{_{bs}r_1\cdot r_2}$ & $\dn$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   269
		$\rerase{_{bs}\sum as}$ & $\dn$ & $\RALTS{\map \; \rerase{\_} \; as}$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   270
		$\rerase{_{bs} a ^*}$ & $\dn$ & $\rerase{a} ^*$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   271
	\end{tabular}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   272
\end{center}
594
Chengsong
parents: 593
diff changeset
   273
613
Chengsong
parents: 611
diff changeset
   274
\subsection{Why a New Datatype?}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   275
The reason we
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   276
define a new datatype is that 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   277
the $\erase$ function 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   278
does not preserve the structure of annotated
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   279
regular expressions.
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   280
We initially started by using 
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   281
plain regular expressions and tried to prove
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   282
lemma \ref{rsizeAsize},
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   283
however the $\erase$ function messes with the structure of the 
594
Chengsong
parents: 593
diff changeset
   284
annotated regular expression.
Chengsong
parents: 593
diff changeset
   285
The $+$ constructor
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   286
of basic regular expressions is only binary, whereas $\sum$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   287
takes a list. Therefore we need to convert between
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   288
annotated and normal regular expressions as follows:
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   289
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   290
	\begin{tabular}{lcl}
594
Chengsong
parents: 593
diff changeset
   291
		$\erase \; _{bs}\sum [] $ & $\dn$ & $\ZERO$\\
Chengsong
parents: 593
diff changeset
   292
		$\erase \; _{bs}\sum [a]$ & $\dn$ & $a$\\
Chengsong
parents: 593
diff changeset
   293
		$\erase \; _{bs}\sum a :: as$ & $\dn$ & $a + (\erase \; _{[]} \sum as)\quad \text{if $as$ length over 1}$
Chengsong
parents: 593
diff changeset
   294
	\end{tabular}
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
   295
\end{center}
594
Chengsong
parents: 593
diff changeset
   296
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   297
As can be seen alternative regular expression with an empty argument list
594
Chengsong
parents: 593
diff changeset
   298
will be turned into a $\ZERO$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   299
The singleton alternative $\sum [r]$ becomes $r$ during the
594
Chengsong
parents: 593
diff changeset
   300
$\erase$ function.
Chengsong
parents: 593
diff changeset
   301
The  annotated regular expression $\sum[a, b, c]$ would turn into
Chengsong
parents: 593
diff changeset
   302
$(a+(b+c))$.
Chengsong
parents: 593
diff changeset
   303
All these operations change the size and structure of
Chengsong
parents: 593
diff changeset
   304
an annotated regular expressions, adding unnecessary 
Chengsong
parents: 593
diff changeset
   305
complexities to the size bound proof.\\
613
Chengsong
parents: 611
diff changeset
   306
For example, if we define the size of a basic plain regular expression 
594
Chengsong
parents: 593
diff changeset
   307
in the usual way,
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   308
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   309
	\begin{tabular}{lcl}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   310
		$\llbracket \ONE \rrbracket_p$ & $\dn$ & $1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   311
		$\llbracket \ZERO \rrbracket_p$ & $\dn$ & $1$ \\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   312
		$\llbracket r_1 \cdot r_2 \rrbracket_p$ & $\dn$ & $\llbracket r_1 \rrbracket_p + \llbracket r_2 \rrbracket_p + 1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   313
		$\llbracket \mathbf{c} \rrbracket_p $ & $\dn$ & $1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   314
		$\llbracket r_1 \cdot r_2 \rrbracket_p $ & $\dn$ & $\llbracket r_1 \rrbracket_p \; + \llbracket r_2 \rrbracket_p + 1$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   315
		$\llbracket a^* \rrbracket_p $ & $\dn$ & $\llbracket a \rrbracket_p + 1$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   316
	\end{tabular}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   317
\end{center}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   318
\noindent
594
Chengsong
parents: 593
diff changeset
   319
Then the property
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   320
\begin{center}
613
Chengsong
parents: 611
diff changeset
   321
	$\llbracket a \rrbracket \stackrel{?}{=} \llbracket a_\downarrow \rrbracket_p$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   322
\end{center}
594
Chengsong
parents: 593
diff changeset
   323
does not hold.
613
Chengsong
parents: 611
diff changeset
   324
With $\textit{rerase}$, however, 
Chengsong
parents: 611
diff changeset
   325
only the bitcodes are thrown away.
Chengsong
parents: 611
diff changeset
   326
Everything about the structure remains intact.
Chengsong
parents: 611
diff changeset
   327
Therefore it does not change the size
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   328
of an annotated regular expression and we have:
613
Chengsong
parents: 611
diff changeset
   329
\begin{lemma}\label{rsizeAsize}
Chengsong
parents: 611
diff changeset
   330
	$\rsize{\rerase a} = \asize a$
Chengsong
parents: 611
diff changeset
   331
\end{lemma}
Chengsong
parents: 611
diff changeset
   332
\begin{proof}
Chengsong
parents: 611
diff changeset
   333
	By routine structural induction on $a$.
Chengsong
parents: 611
diff changeset
   334
\end{proof}
Chengsong
parents: 611
diff changeset
   335
\noindent
594
Chengsong
parents: 593
diff changeset
   336
One might be able to prove an inequality such as
Chengsong
parents: 593
diff changeset
   337
$\llbracket a \rrbracket  \leq \llbracket  a_\downarrow \rrbracket_p $
Chengsong
parents: 593
diff changeset
   338
and then estimate $\llbracket  a_\downarrow \rrbracket_p$,
Chengsong
parents: 593
diff changeset
   339
but we found our approach more straightforward.\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   340
613
Chengsong
parents: 611
diff changeset
   341
\subsection{Functions for R-regular Expressions}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   342
In this section we shall define the r-regular expression version
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   343
of $\blexer$, and $\blexersimp$ related functions.
613
Chengsong
parents: 611
diff changeset
   344
We use $r$ as the prefix or subscript to differentiate
Chengsong
parents: 611
diff changeset
   345
with the bitcoded version.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   346
%For example,$\backslash_r$, $\rdistincts$, and $\rsimp$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   347
%as opposed to $\backslash$, $\distinctBy$, and $\bsimp$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   348
%As promised, they are much simpler than their bitcoded counterparts.
613
Chengsong
parents: 611
diff changeset
   349
%The operations on r-regular expressions are 
Chengsong
parents: 611
diff changeset
   350
%almost identical to those of the annotated regular expressions,
Chengsong
parents: 611
diff changeset
   351
%except that no bitcodes are used. For example,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   352
The derivative operation for an r-regular expression is\\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   353
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   354
	\begin{tabular}{@{}lcl@{}}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   355
		$(\ZERO)\,\backslash_r c$ & $\dn$ & $\ZERO$\\  
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   356
		$(\ONE)\,\backslash_r c$ & $\dn$ &
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   357
		$\textit{if}\;c=d\; \;\textit{then}\;
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   358
		\ONE\;\textit{else}\;\ZERO$\\  
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   359
		$(\sum \;\textit{rs})\,\backslash_r c$ & $\dn$ &
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   360
		$\sum\;(\textit{map} \; (\_\backslash_r c) \; rs )$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   361
		$(r_1\cdot r_2)\,\backslash_r c$ & $\dn$ &
594
Chengsong
parents: 593
diff changeset
   362
		$\textit{if}\;(\textit{rnullable}\,r_1)$\\
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   363
						 & &$\textit{then}\;\sum\,[(r_1\,\backslash_r c)\cdot\,r_2,$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   364
						 & &$\phantom{\textit{then},\;\sum\,}((r_2\,\backslash_r c))]$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   365
						 & &$\textit{else}\;\,(r_1\,\backslash_r c)\cdot r_2$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   366
		$(r^*)\,\backslash_r c$ & $\dn$ &
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   367
		$( r\,\backslash_r c)\cdot
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   368
		(_{[]}r^*))$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   369
	\end{tabular}    
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   370
\end{center}  
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   371
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   372
where we omit the definition of $\textit{rnullable}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   373
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   374
The function $\distinctBy$ for r-regular expressions does not need 
594
Chengsong
parents: 593
diff changeset
   375
a function checking equivalence because
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   376
there are no bit annotations.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   377
Therefore we have
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   378
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   379
	\begin{tabular}{lcl}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   380
		$\rdistinct{[]}{rset} $ & $\dn$ & $[]$\\
594
Chengsong
parents: 593
diff changeset
   381
		$\rdistinct{r :: rs}{rset}$ & $\dn$ & 
Chengsong
parents: 593
diff changeset
   382
		$\textit{if}(r \in \textit{rset}) \; \textit{then} \; \rdistinct{rs}{rset}$\\
Chengsong
parents: 593
diff changeset
   383
					    &        & $\textit{else}\; \;
Chengsong
parents: 593
diff changeset
   384
					    r::\rdistinct{rs}{(rset \cup \{r\})}$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   385
	\end{tabular}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   386
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   387
%TODO: definition of rsimp (maybe only the alternative clause)
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   388
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   389
%We would like to make clear
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   390
%a difference between our $\rdistincts$ and
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   391
%the Isabelle $\textit {distinct}$ predicate.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   392
%In Isabelle $\textit{distinct}$ is a function that returns a boolean
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   393
%rather than a list.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   394
%It tests if all the elements of a list are unique.\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   395
With $\textit{rdistinct}$ in place,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   396
the flatten function for $\rrexp$ is as follows:
595
Chengsong
parents: 594
diff changeset
   397
 \begin{center}
Chengsong
parents: 594
diff changeset
   398
  \begin{tabular}{@{}lcl@{}}
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   399
  $\textit{rflts} \; (\sum \textit{as}) :: \textit{as'}$ & $\dn$ & $as \; @ \; \textit{rflts} \; as' $ \\
595
Chengsong
parents: 594
diff changeset
   400
  $\textit{rflts} \; \ZERO :: as'$ & $\dn$ & $ \textit{rflts} \;  \textit{as'} $ \\
Chengsong
parents: 594
diff changeset
   401
    $\textit{rflts} \; a :: as'$ & $\dn$ & $a :: \textit{rflts} \; \textit{as'}$ \quad(otherwise) 
Chengsong
parents: 594
diff changeset
   402
\end{tabular}    
Chengsong
parents: 594
diff changeset
   403
\end{center}  
Chengsong
parents: 594
diff changeset
   404
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   405
The function 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   406
$\rsimpalts$ corresponds to $\textit{bsimp}_{ALTS}$:
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   407
\begin{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   408
  \begin{tabular}{@{}lcl@{}}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   409
	  $\rsimpalts \;\; nil$ & $\dn$ & $\RZERO$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   410
	  $\rsimpalts \;\; r::nil$ & $\dn$ & $r$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   411
	  $\rsimpalts \;\; rs$ & $\dn$ & $\sum rs$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   412
\end{tabular}    
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   413
\end{center}  
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   414
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   415
Similarly, we have $\rsimpseq$ which corresponds to $\textit{bsimp}_{SEQ}$:
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   416
\begin{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   417
  \begin{tabular}{@{}lcl@{}}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   418
	  $\rsimpseq \;\; \RZERO \; \_ $ &   $=$ &   $\RZERO$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   419
	  $\rsimpseq \;\; \_ \; \RZERO $ &   $=$ &   $\RZERO$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   420
	  $\rsimpseq \;\; \RONE \cdot r_2$ & $\dn$ & $r_2$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   421
	  $\rsimpseq \;\; r_1 r_2$ & $\dn$ & $r_1 \cdot r_2$\\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   422
\end{tabular}    
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   423
\end{center}  
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   424
and get $\textit{rsimp}$ and $\rderssimp{\_}{\_}$:
595
Chengsong
parents: 594
diff changeset
   425
\begin{center}
Chengsong
parents: 594
diff changeset
   426
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 594
diff changeset
   427
   
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   428
	  $\textit{rsimp} \; (r_1\cdot r_2)$ & $\dn$ & $ \textit{rsimp}_{SEQ} \; bs \;(\textit{rsimp} \; r_1) \; (\textit{rsimp}  \; r_2)  $ \\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   429
	  $\textit{rsimp} \; (_{bs}\sum \textit{rs})$ & $\dn$ & $\textit{rsimp}_{ALTS} \; \textit{bs} \; (\textit{rdistinct} \; ( \textit{rflts} ( \textit{map} \; rsimp \; rs)) \; \rerases \; \varnothing) $ \\
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   430
   $\textit{rsimp} \; r$ & $\dn$ & $\textit{r} \qquad \textit{otherwise}$   
595
Chengsong
parents: 594
diff changeset
   431
\end{tabular}    
Chengsong
parents: 594
diff changeset
   432
\end{center} 
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   433
\begin{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   434
	\begin{tabular}{@{}lcl@{}}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   435
		$r\backslash_{rsimp} \, c$ & $\dn$ & $\rsimp \; (r\backslash_r \, c)$
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   436
	\end{tabular}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   437
\end{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   438
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   439
\begin{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   440
	\begin{tabular}{@{}lcl@{}}
601
Chengsong
parents: 596
diff changeset
   441
$r \backslash_{rsimps} \; \; c\!::\!s $ & $\dn$ & $(r \backslash_{rsimp}\, c) \backslash_{rsimps}\, s$ \\
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   442
$r \backslash_{rsimps} [\,] $ & $\dn$ & $r$
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   443
	\end{tabular}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   444
\end{center}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   445
\noindent
601
Chengsong
parents: 596
diff changeset
   446
We do not define an r-regular expression version of $\blexersimp$,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   447
as our proof does not depend on it.
613
Chengsong
parents: 611
diff changeset
   448
Now we are ready to introduce how r-regular expressions allow
Chengsong
parents: 611
diff changeset
   449
us to prove the size bound on bitcoded regular expressions.
Chengsong
parents: 611
diff changeset
   450
Chengsong
parents: 611
diff changeset
   451
\subsection{Using R-regular Expressions to Bound Bit-coded Regular Expressions}
Chengsong
parents: 611
diff changeset
   452
Everything about the size of annotated regular expressions after the application
Chengsong
parents: 611
diff changeset
   453
of function $\bsimp$ and $\backslash_{simps}$
Chengsong
parents: 611
diff changeset
   454
can be calculated via the size of r-regular expressions after the application
Chengsong
parents: 611
diff changeset
   455
of $\rsimp$ and $\backslash_{rsimps}$:
564
Chengsong
parents: 562
diff changeset
   456
\begin{lemma}\label{sizeRelations}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   457
	The following two equalities hold:
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   458
	\begin{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   459
		\item
601
Chengsong
parents: 596
diff changeset
   460
			$\asize{\bsimps \; a} = \rsize{\rsimp{ \rerase{a}}}$
554
Chengsong
parents: 553
diff changeset
   461
		\item
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   462
			$\asize{\bderssimp{a}{s}} =  \rsize{\rderssimp{\rerase{a}}{s}}$
554
Chengsong
parents: 553
diff changeset
   463
	\end{itemize}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   464
\end{lemma}
601
Chengsong
parents: 596
diff changeset
   465
\begin{proof}
Chengsong
parents: 596
diff changeset
   466
	The first part is by induction on the inductive cases
Chengsong
parents: 596
diff changeset
   467
	of $\textit{bsimp}$.
Chengsong
parents: 596
diff changeset
   468
	The second part is by induction on the string $s$,
Chengsong
parents: 596
diff changeset
   469
	where the inductive step follows from part one.
Chengsong
parents: 596
diff changeset
   470
\end{proof}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   471
\noindent
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   472
With lemma \ref{sizeRelations},
601
Chengsong
parents: 596
diff changeset
   473
we will be able to focus on 
Chengsong
parents: 596
diff changeset
   474
estimating only
Chengsong
parents: 596
diff changeset
   475
$\rsize{\rderssimp{\rerase{a}}{s}}$
Chengsong
parents: 596
diff changeset
   476
in later parts because
Chengsong
parents: 596
diff changeset
   477
\begin{center}
Chengsong
parents: 596
diff changeset
   478
	$\rsize{\rderssimp{\rerase{a}}{s}} \leq N_r \quad$
Chengsong
parents: 596
diff changeset
   479
	implies
Chengsong
parents: 596
diff changeset
   480
	$\quad \llbracket a \backslash_{bsimps} s \rrbracket \leq N_r$.
Chengsong
parents: 596
diff changeset
   481
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   482
%From now on we 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   483
%Unless stated otherwise in the rest of this 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   484
%chapter all regular expressions without
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   485
%bitcodes are seen as r-regular expressions ($\rrexp$s).
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   486
%For the binary alternative r-regular expression $\RALTS{[r_1, r_2]}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   487
%we use the notation $r_1 + r_2$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   488
%for brevity.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   489
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   490
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   491
%-----------------------------------
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   492
%	SUB SECTION ROADMAP RREXP BOUND
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   493
%-----------------------------------
553
0f00d440f484 more changes
Chengsong
parents: 543
diff changeset
   494
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   495
%\subsection{Roadmap to a Bound for $\textit{Rrexp}$}
553
0f00d440f484 more changes
Chengsong
parents: 543
diff changeset
   496
596
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   497
%The way we obtain the bound for $\rrexp$s is by two steps:
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   498
%\begin{itemize}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   499
%	\item
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   500
%		First, we rewrite $r\backslash s$ into something else that is easier
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   501
%		to bound. This step is especially important for the inductive case 
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   502
%		$r_1 \cdot r_2$ and $r^*$, where the derivative can grow and bloat in a wild way,
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   503
%		but after simplification they will always be equal or smaller to a form consisting of an alternative
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   504
%		list of regular expressions $f \; (g\; (\sum rs))$ with some functions applied to it, where each element will be distinct after the function application.
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   505
%	\item
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   506
%		Then, for such a sum  list of regular expressions $f\; (g\; (\sum rs))$, we can control its size
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   507
%		by estimation, since $\distinctBy$ and $\flts$ are well-behaved and working together would only 
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   508
%		reduce the size of a regular expression, not adding to it.
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   509
%\end{itemize}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   510
%
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   511
%\section{Step One: Closed Forms}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   512
%We transform the function application $\rderssimp{r}{s}$
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   513
%into an equivalent 
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   514
%form $f\; (g \; (\sum rs))$.
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   515
%The functions $f$ and $g$ can be anything from $\flts$, $\distinctBy$ and other helper functions from $\bsimp{\_}$.
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   516
%This way we get a different but equivalent way of expressing : $r\backslash s = f \; (g\; (\sum rs))$, we call the
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   517
%right hand side the "closed form" of $r\backslash s$.
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   518
%
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   519
%\begin{quote}\it
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   520
%	Claim: For regular expressions $r_1 \cdot r_2$, we claim that
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   521
%\end{quote}
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   522
%\noindent
b306628a0eab more chap 56
Chengsong
parents: 595
diff changeset
   523
%We explain in detail how we reached those claims.
601
Chengsong
parents: 596
diff changeset
   524
If we attempt to prove 
Chengsong
parents: 596
diff changeset
   525
\begin{center}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   526
	$\forall r. \; \exists N_r.\;\; s.t. \llbracket r\backslash_{rsimps} s \rrbracket_r \leq N_r$
601
Chengsong
parents: 596
diff changeset
   527
\end{center}
Chengsong
parents: 596
diff changeset
   528
using a naive induction on the structure of $r$,
Chengsong
parents: 596
diff changeset
   529
then we are stuck at the inductive cases such as
Chengsong
parents: 596
diff changeset
   530
$r_1\cdot r_2$.
Chengsong
parents: 596
diff changeset
   531
The inductive hypotheses are:
Chengsong
parents: 596
diff changeset
   532
\begin{center}
Chengsong
parents: 596
diff changeset
   533
	1: $\text{for } r_1, \text{there exists } N_{r_1}.\;\; s.t. 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   534
	\;\;\forall s.  \llbracket r_1 \backslash_{rsimps} s \rrbracket_r \leq N_{r_1}. $\\
601
Chengsong
parents: 596
diff changeset
   535
	2: $\text{for } r_2, \text{there exists } N_{r_2}.\;\; s.t. 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   536
	\;\; \forall s. \llbracket r_2 \backslash_{rsimps} s \rrbracket_r \leq N_{r_2}. $
601
Chengsong
parents: 596
diff changeset
   537
\end{center}
Chengsong
parents: 596
diff changeset
   538
The inductive step to prove would be 
Chengsong
parents: 596
diff changeset
   539
\begin{center}
Chengsong
parents: 596
diff changeset
   540
	$\text{there exists } N_{r_1\cdot r_2}. \;\; s.t. \forall s. 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   541
	\llbracket (r_1 \cdot r_2) \backslash_{rsimps} s \rrbracket_r \leq N_{r_1\cdot r_2}.$
601
Chengsong
parents: 596
diff changeset
   542
\end{center}
Chengsong
parents: 596
diff changeset
   543
The problem is that it is not clear what 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   544
$(r_1\cdot r_2) \backslash_{rsimps} s$ looks like,
601
Chengsong
parents: 596
diff changeset
   545
and therefore $N_{r_1}$ and $N_{r_2}$ in the
Chengsong
parents: 596
diff changeset
   546
inductive hypotheses cannot be directly used.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   547
%We have already seen that $(r_1 \cdot r_2)\backslash s$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   548
%and $(r^*)\backslash s$ can grow in a wild way.
613
Chengsong
parents: 611
diff changeset
   549
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   550
The point however, is that they will be equivalent to a list of
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   551
terms $\sum rs$, where each term in $rs$ will
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   552
be made of $r_1 \backslash s' $, $r_2\backslash s'$,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   553
and $r \backslash s'$ with $s' \in \textit{SubString} \; s$ (which stands
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   554
for the set of substrings of $s$).
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   555
The list $\sum rs$ will then be de-duplicated by $\textit{rdistinct}$
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   556
in the simplification, which prevents the $rs$ from growing indefinitely.
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   557
613
Chengsong
parents: 611
diff changeset
   558
Based on this idea, we develop a proof in two steps.
Chengsong
parents: 611
diff changeset
   559
First, we show the equality (where
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   560
$f$ and $g$ are functions that do not increase the size of the input)
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   561
\begin{center}
613
Chengsong
parents: 611
diff changeset
   562
$r\backslash_{rsimps} s = f\; (\textit{rdistinct} \; (g\; \sum rs))$,
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   563
\end{center}
613
Chengsong
parents: 611
diff changeset
   564
where $r = r_1 \cdot r_2$ or $r = r_0^*$ and so on.
Chengsong
parents: 611
diff changeset
   565
For example, for $r_1 \cdot r_2$ we have the equality as
Chengsong
parents: 611
diff changeset
   566
	\begin{center}
Chengsong
parents: 611
diff changeset
   567
		$ \rderssimp{r_1 \cdot r_2}{s} = 
Chengsong
parents: 611
diff changeset
   568
		\rsimp{(\sum (r_1 \backslash s \cdot r_2 ) \; :: \;(\map \; \rderssimp{r2}{\_} \;(\vsuf{s}{r_1})))}$
Chengsong
parents: 611
diff changeset
   569
	\end{center}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   570
We call the right-hand-side the 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   571
\emph{Closed Form} of $(r_1 \cdot r_2)\backslash_{rsimps} s$.
613
Chengsong
parents: 611
diff changeset
   572
Second, we will bound the closed form of r-regular expressions
Chengsong
parents: 611
diff changeset
   573
using some estimation techniques
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   574
and then apply
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   575
lemma \ref{sizeRelations} to show that the bitcoded regular expressions
613
Chengsong
parents: 611
diff changeset
   576
in our $\blexersimp$ are finitely bounded.
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   577
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   578
We will describe in detail the first step of the proof
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   579
in the next section.
613
Chengsong
parents: 611
diff changeset
   580
Chengsong
parents: 611
diff changeset
   581
\section{Closed Forms}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   582
In this section we introduce in detail
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   583
how we express the string derivatives
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   584
of regular expressions (i.e. $r \backslash_r s$ where $s$ is a string
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   585
rather than a single character) in a different way than 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   586
our previous definition.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   587
In previous chapters, the derivative of a 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   588
regular expression $r$ w.r.t a string $s$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   589
was recursively defined on the string:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   590
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   591
	$r \backslash_s (c::s) \dn (r \backslash c) \backslash_s s$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   592
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   593
The problem is that 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   594
this definition does not provide much information
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   595
on what $r \backslash_s s$ looks like.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   596
If we are interested in the size of a derivative like 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   597
$(r_1 \cdot r_2)\backslash s$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   598
we have to somehow get a more concrete form to begin.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   599
We call such more concrete representations the ``closed forms'' of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   600
string derivatives as opposed to their original definitions.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   601
The terminology ``closed form'' is borrowed from mathematics,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   602
which usually describe expressions that are solely comprised of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   603
well-known and easy-to-compute operations such as 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   604
additions, multiplications, exponential functions.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   605
613
Chengsong
parents: 611
diff changeset
   606
We start by proving some basic identities
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   607
involving the simplification functions for r-regular expressions.
613
Chengsong
parents: 611
diff changeset
   608
After that we introduce the rewrite relations
Chengsong
parents: 611
diff changeset
   609
$\rightsquigarrow_h$, $\rightsquigarrow^*_{scf}$
Chengsong
parents: 611
diff changeset
   610
$\rightsquigarrow_f$ and $\rightsquigarrow_g$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   611
These relations involves similar techniques as in chapter \ref{Bitcoded2}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   612
for annotated regular expressions.
613
Chengsong
parents: 611
diff changeset
   613
Finally, we use these identities to establish the
Chengsong
parents: 611
diff changeset
   614
closed forms of the alternative regular expression,
Chengsong
parents: 611
diff changeset
   615
the sequence regular expression, and the star regular expression.
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   616
%$r_1\cdot r_2$, $r^*$ and $\sum rs$.
601
Chengsong
parents: 596
diff changeset
   617
Chengsong
parents: 596
diff changeset
   618
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   619
613
Chengsong
parents: 611
diff changeset
   620
\subsection{Some Basic Identities}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   621
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   622
In what follows we will often convert between lists
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   623
and sets.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   624
We use Isabelle's $set$ to refere to the 
611
Chengsong
parents: 610
diff changeset
   625
function that converts a list $rs$ to the set
Chengsong
parents: 610
diff changeset
   626
containing all the elements in $rs$.
Chengsong
parents: 610
diff changeset
   627
\subsubsection{$\textit{rdistinct}$'s Does the Job of De-duplication}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   628
The $\textit{rdistinct}$ function, as its name suggests, will
613
Chengsong
parents: 611
diff changeset
   629
de-duplicate an r-regular expression list.
Chengsong
parents: 611
diff changeset
   630
It will also remove any elements that 
Chengsong
parents: 611
diff changeset
   631
is already in the accumulator set.
555
Chengsong
parents: 554
diff changeset
   632
\begin{lemma}\label{rdistinctDoesTheJob}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   633
	%The function $\textit{rdistinct}$ satisfies the following
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   634
	%properties:
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   635
	Assume we have the predicate $\textit{isDistinct}$\footnote{We omit its
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   636
	recursive definition here. Its Isabelle counterpart would be $\textit{distinct}$.} 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   637
	for testing
613
Chengsong
parents: 611
diff changeset
   638
	whether a list's elements are unique. Then the following
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   639
	properties about $\textit{rdistinct}$ hold:
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   640
	\begin{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   641
		\item
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   642
			If $a \in acc$ then $a \notin (\rdistinct{rs}{acc})$.
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   643
		\item
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   644
			%If list $rs'$ is the result of $\rdistinct{rs}{acc}$,
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   645
			$\textit{isDistinct} \;\;\; (\rdistinct{rs}{acc})$.
555
Chengsong
parents: 554
diff changeset
   646
		\item
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   647
			$\textit{set} \; (\rdistinct{rs}{acc}) 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   648
			= (textit{set} \; rs) - acc$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   649
	\end{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   650
\end{lemma}
555
Chengsong
parents: 554
diff changeset
   651
\noindent
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   652
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   653
	The first part is by an induction on $rs$.
555
Chengsong
parents: 554
diff changeset
   654
	The second and third part can be proven by using the 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   655
	inductive cases of $\textit{rdistinct}$.
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   656
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   657
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   658
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   659
\noindent
613
Chengsong
parents: 611
diff changeset
   660
%$\textit{rdistinct}$ will out all regular expression terms
Chengsong
parents: 611
diff changeset
   661
%that are in the accumulator, therefore 
Chengsong
parents: 611
diff changeset
   662
Concatenating a list $rs_a$ at the front of another
Chengsong
parents: 611
diff changeset
   663
list $rs$ whose elements are all from the accumulator, and then calling $\textit{rdistinct}$
Chengsong
parents: 611
diff changeset
   664
on the merged list, the output will be as if we had called $\textit{rdistinct}$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   665
without the prepending of $rs$:
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   666
\begin{lemma}\label{rdistinctConcat}
554
Chengsong
parents: 553
diff changeset
   667
	The elements appearing in the accumulator will always be removed.
Chengsong
parents: 553
diff changeset
   668
	More precisely,
Chengsong
parents: 553
diff changeset
   669
	\begin{itemize}
Chengsong
parents: 553
diff changeset
   670
		\item
Chengsong
parents: 553
diff changeset
   671
			If $rs \subseteq rset$, then 
Chengsong
parents: 553
diff changeset
   672
			$\rdistinct{rs@rsa }{acc} = \rdistinct{rsa }{acc}$.
Chengsong
parents: 553
diff changeset
   673
		\item
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   674
			More generally, if $a \in rset$ and $\rdistinct{rs}{\{a\}} = []$,
554
Chengsong
parents: 553
diff changeset
   675
			then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{rs'}{rset}$
Chengsong
parents: 553
diff changeset
   676
	\end{itemize}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   677
\end{lemma}
554
Chengsong
parents: 553
diff changeset
   678
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   679
\begin{proof}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
   680
	By induction on $rs$ and using \ref{rdistinctDoesTheJob}.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   681
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   682
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   683
On the other hand, if an element $r$ does not appear in the input list waiting to be deduplicated,
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   684
then expanding the accumulator to include that element will not cause the output list to change:
611
Chengsong
parents: 610
diff changeset
   685
\begin{lemma}\label{rdistinctOnDistinct}
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   686
	The accumulator can be augmented to include elements not appearing in the input list,
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   687
	and the output will not change.	
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   688
	\begin{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   689
		\item
611
Chengsong
parents: 610
diff changeset
   690
			If $r \notin rs$, then $\rdistinct{rs}{acc} = \rdistinct{rs}{(\{r\} \cup acc)}$.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   691
		\item
611
Chengsong
parents: 610
diff changeset
   692
			Particularly, if $\;\;\textit{isDistinct} \; rs$, then we have\\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   693
			\[ \rdistinct{rs}{\varnothing} = rs \]
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   694
	\end{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   695
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   696
\begin{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   697
	The first half is by induction on $rs$. The second half is a corollary of the first.
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   698
\end{proof}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   699
\noindent
611
Chengsong
parents: 610
diff changeset
   700
The function $\textit{rdistinct}$ removes duplicates from anywhere in a list.
Chengsong
parents: 610
diff changeset
   701
Despite being seemingly obvious, 
Chengsong
parents: 610
diff changeset
   702
the induction technique is not as straightforward.
554
Chengsong
parents: 553
diff changeset
   703
\begin{lemma}\label{distinctRemovesMiddle}
Chengsong
parents: 553
diff changeset
   704
	The two properties hold if $r \in rs$:
Chengsong
parents: 553
diff changeset
   705
	\begin{itemize}
Chengsong
parents: 553
diff changeset
   706
		\item
555
Chengsong
parents: 554
diff changeset
   707
			$\rdistinct{rs}{rset} = \rdistinct{(rs @ [r])}{rset}$\\
Chengsong
parents: 554
diff changeset
   708
			and\\
554
Chengsong
parents: 553
diff changeset
   709
			$\rdistinct{(ab :: rs @ [ab])}{rset'} = \rdistinct{(ab :: rs)}{rset'}$
Chengsong
parents: 553
diff changeset
   710
		\item
555
Chengsong
parents: 554
diff changeset
   711
			$\rdistinct{ (rs @ rs') }{rset} = \rdistinct{rs @ [r] @ rs'}{rset}$\\
Chengsong
parents: 554
diff changeset
   712
			and\\
554
Chengsong
parents: 553
diff changeset
   713
			$\rdistinct{(ab :: rs @ [ab] @ rs'')}{rset'} = 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   714
			\rdistinct{(ab :: rs @ rs'')}{rset'}$
554
Chengsong
parents: 553
diff changeset
   715
	\end{itemize}
Chengsong
parents: 553
diff changeset
   716
\end{lemma}
Chengsong
parents: 553
diff changeset
   717
\noindent
Chengsong
parents: 553
diff changeset
   718
\begin{proof}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   719
	By induction on $rs$. All other variables are allowed to be arbitrary.
611
Chengsong
parents: 610
diff changeset
   720
	The second part of the lemma requires the first.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   721
	Note that for each part, the two sub-propositions need to be proven 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   722
	at the same time,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
   723
	so that the induction goes through.
554
Chengsong
parents: 553
diff changeset
   724
\end{proof}
555
Chengsong
parents: 554
diff changeset
   725
\noindent
611
Chengsong
parents: 610
diff changeset
   726
This allows us to prove a few more equivalence relations involving 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   727
$\textit{rdistinct}$ (they will be useful later):
555
Chengsong
parents: 554
diff changeset
   728
\begin{lemma}\label{rdistinctConcatGeneral}
611
Chengsong
parents: 610
diff changeset
   729
	\mbox{}
555
Chengsong
parents: 554
diff changeset
   730
	\begin{itemize}
Chengsong
parents: 554
diff changeset
   731
		\item
Chengsong
parents: 554
diff changeset
   732
			$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{((\rdistinct{rs}{\varnothing})@ rs')}{\varnothing}$
Chengsong
parents: 554
diff changeset
   733
		\item
Chengsong
parents: 554
diff changeset
   734
			$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{(\rdistinct{rs}{\varnothing} @ rs')}{\varnothing}$
Chengsong
parents: 554
diff changeset
   735
		\item
Chengsong
parents: 554
diff changeset
   736
			If $rset' \subseteq rset$, then $\rdistinct{rs}{rset} = 
Chengsong
parents: 554
diff changeset
   737
			\rdistinct{(\rdistinct{rs}{rset'})}{rset}$. As a corollary
Chengsong
parents: 554
diff changeset
   738
			of this,
Chengsong
parents: 554
diff changeset
   739
		\item
Chengsong
parents: 554
diff changeset
   740
			$\rdistinct{(rs @ rs')}{rset} = \rdistinct{
Chengsong
parents: 554
diff changeset
   741
			(\rdistinct{rs}{\varnothing}) @ rs')}{rset}$. This
Chengsong
parents: 554
diff changeset
   742
			gives another corollary use later:
Chengsong
parents: 554
diff changeset
   743
		\item
Chengsong
parents: 554
diff changeset
   744
			If $a \in rset$, then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{
Chengsong
parents: 554
diff changeset
   745
			(\rdistinct{(a :: rs)}{\varnothing} @ rs')}{rset} $,
Chengsong
parents: 554
diff changeset
   746
Chengsong
parents: 554
diff changeset
   747
	\end{itemize}
Chengsong
parents: 554
diff changeset
   748
\end{lemma}
Chengsong
parents: 554
diff changeset
   749
\begin{proof}
Chengsong
parents: 554
diff changeset
   750
	By \ref{rdistinctDoesTheJob} and \ref{distinctRemovesMiddle}.
Chengsong
parents: 554
diff changeset
   751
\end{proof}
611
Chengsong
parents: 610
diff changeset
   752
\noindent
613
Chengsong
parents: 611
diff changeset
   753
The next lemma is a more general form of \ref{rdistinctConcat},
Chengsong
parents: 611
diff changeset
   754
it says that
611
Chengsong
parents: 610
diff changeset
   755
$\textit{rdistinct}$ is composable w.r.t list concatenation:
Chengsong
parents: 610
diff changeset
   756
\begin{lemma}\label{distinctRdistinctAppend}
Chengsong
parents: 610
diff changeset
   757
			If $\;\; \textit{isDistinct} \; rs_1$, 
Chengsong
parents: 610
diff changeset
   758
			and $(set \; rs_1) \cap acc = \varnothing$,
Chengsong
parents: 610
diff changeset
   759
			then applying $\textit{rdistinct}$ on $rs_1 @ rs_a$ does not 
Chengsong
parents: 610
diff changeset
   760
			have an effect on $rs_1$:
Chengsong
parents: 610
diff changeset
   761
			\[\textit{rdistinct}\;  (rs_1 @ rsa)\;\, acc
Chengsong
parents: 610
diff changeset
   762
			= rs_1@(\textit{rdistinct} rsa \; (acc \cup rs_1))\]
Chengsong
parents: 610
diff changeset
   763
\end{lemma}
Chengsong
parents: 610
diff changeset
   764
\begin{proof}
Chengsong
parents: 610
diff changeset
   765
	By an induction on 
Chengsong
parents: 610
diff changeset
   766
	$rs_1$, where $rsa$ and $acc$ are allowed to be arbitrary.
Chengsong
parents: 610
diff changeset
   767
\end{proof}
Chengsong
parents: 610
diff changeset
   768
\noindent
Chengsong
parents: 610
diff changeset
   769
$\textit{rdistinct}$ needs to be applied only once, and 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   770
applying it multiple times does not make any difference:
611
Chengsong
parents: 610
diff changeset
   771
\begin{corollary}\label{distinctOnceEnough}
Chengsong
parents: 610
diff changeset
   772
	$\textit{rdistinct} \; (rs @ rsa) {} = \textit{rdistinct} \; (rdistinct \; 
Chengsong
parents: 610
diff changeset
   773
	rs \{ \} @ (\textit{rdistinct} \; rs_a \; (set \; rs)))$
Chengsong
parents: 610
diff changeset
   774
\end{corollary}
Chengsong
parents: 610
diff changeset
   775
\begin{proof}
Chengsong
parents: 610
diff changeset
   776
	By lemma \ref{distinctRdistinctAppend}.
Chengsong
parents: 610
diff changeset
   777
\end{proof}
555
Chengsong
parents: 554
diff changeset
   778
611
Chengsong
parents: 610
diff changeset
   779
\subsubsection{The Properties of $\textit{Rflts}$} 
Chengsong
parents: 610
diff changeset
   780
We give in this subsection some properties
Chengsong
parents: 610
diff changeset
   781
involving $\backslash_r$, $\backslash_{rsimp}$, $\textit{rflts}$ and 
Chengsong
parents: 610
diff changeset
   782
$\textit{rsimp}_{ALTS} $, together with any non-trivial lemmas that lead to them.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   783
These will be helpful in later closed form proofs, when
611
Chengsong
parents: 610
diff changeset
   784
we want to transform derivative terms which have
Chengsong
parents: 610
diff changeset
   785
%the ways in which multiple functions involving
Chengsong
parents: 610
diff changeset
   786
%those are composed together
Chengsong
parents: 610
diff changeset
   787
interleaving derivatives and  simplifications applied to them.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   788
611
Chengsong
parents: 610
diff changeset
   789
\noindent
Chengsong
parents: 610
diff changeset
   790
%When the function $\textit{Rflts}$ 
Chengsong
parents: 610
diff changeset
   791
%is applied to the concatenation of two lists, the output can be calculated by first applying the
Chengsong
parents: 610
diff changeset
   792
%functions on two lists separately, and then concatenating them together.
Chengsong
parents: 610
diff changeset
   793
$\textit{Rflts}$ is composable in terms of concatenation:
554
Chengsong
parents: 553
diff changeset
   794
\begin{lemma}\label{rfltsProps}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   795
	The function $\rflts$ has the properties below:\\
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   796
	\begin{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   797
		\item
554
Chengsong
parents: 553
diff changeset
   798
			$\rflts \; (rs_1 @ rs_2) = \rflts \; rs_1 @ \rflts \; rs_2$
Chengsong
parents: 553
diff changeset
   799
		\item
Chengsong
parents: 553
diff changeset
   800
			If $r \neq \RZERO$ and $\nexists rs_1. r = \RALTS{rs}_1$, then $\rflts \; (r::rs) = r :: \rflts \; rs$
Chengsong
parents: 553
diff changeset
   801
		\item
Chengsong
parents: 553
diff changeset
   802
			$\rflts \; (rs @ [\RZERO]) = \rflts \; rs$
Chengsong
parents: 553
diff changeset
   803
		\item
Chengsong
parents: 553
diff changeset
   804
			$\rflts \; (rs' @ [\RALTS{rs}]) = \rflts \; rs'@rs$
Chengsong
parents: 553
diff changeset
   805
		\item
Chengsong
parents: 553
diff changeset
   806
			$\rflts \; (rs @ [\RONE]) = \rflts \; rs @ [\RONE]$
Chengsong
parents: 553
diff changeset
   807
		\item
Chengsong
parents: 553
diff changeset
   808
			If $r \neq \RZERO$ and $\nexists rs'. r = \RALTS{rs'}$ then $\rflts \; (rs @ [r])
Chengsong
parents: 553
diff changeset
   809
			= (\rflts \; rs) @ [r]$
555
Chengsong
parents: 554
diff changeset
   810
		\item
Chengsong
parents: 554
diff changeset
   811
			If $r = \RALTS{rs}$ and $r \in rs'$ then for all $r_1 \in rs. 
Chengsong
parents: 554
diff changeset
   812
			r_1 \in \rflts \; rs'$.
Chengsong
parents: 554
diff changeset
   813
		\item
Chengsong
parents: 554
diff changeset
   814
			$\rflts \; (rs_a @ \RZERO :: rs_b) = \rflts \; (rs_a @ rs_b)$
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   815
	\end{itemize}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   816
\end{lemma}
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   817
\noindent
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   818
\begin{proof}
555
Chengsong
parents: 554
diff changeset
   819
	By induction on $rs_1$ in the first sub-lemma, and induction on $r$ in the second part,
Chengsong
parents: 554
diff changeset
   820
	and induction on $rs$, $rs'$, $rs$, $rs'$, $rs_a$ in the third, fourth, fifth, sixth and 
Chengsong
parents: 554
diff changeset
   821
	last sub-lemma.
543
b2bea5968b89 thesis_thys
Chengsong
parents: 532
diff changeset
   822
\end{proof}
611
Chengsong
parents: 610
diff changeset
   823
\noindent
Chengsong
parents: 610
diff changeset
   824
Now we introduce the property that the operations 
Chengsong
parents: 610
diff changeset
   825
derivative and $\rsimpalts$
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   826
commute, this will be used later on, when deriving the closed form for
611
Chengsong
parents: 610
diff changeset
   827
the alternative regular expression:
Chengsong
parents: 610
diff changeset
   828
\begin{lemma}\label{rderRsimpAltsCommute}
Chengsong
parents: 610
diff changeset
   829
	$\rder{x}{(\rsimpalts \; rs)} = \rsimpalts \; (\map \; (\rder{x}{\_}) \; rs)$
Chengsong
parents: 610
diff changeset
   830
\end{lemma}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   831
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   832
	By induction on $rs$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   833
\end{proof}
611
Chengsong
parents: 610
diff changeset
   834
\noindent
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   835
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   836
\subsubsection{The $RL$ Function: Language Interpretation of $\textit{Rrexp}$s}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   837
Much like the definition of $L$ on plain regular expressions, one can also 
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   838
define the language interpretation of $\rrexp$s.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   839
\begin{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   840
	\begin{tabular}{lcl}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   841
		$RL \; (\ZERO)$ & $\dn$ & $\phi$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   842
		$RL \; (\ONE)$ & $\dn$ & $\{[]\}$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   843
		$RL \; (c)$ & $\dn$ & $\{[c]\}$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   844
		$RL \; \sum rs$ & $\dn$ & $ \bigcup_{r \in rs} (RL \; r)$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   845
		$RL \; (r_1 \cdot r_2)$ & $\dn$ & $ RL \; (r_1) @ RL \; (r_2)$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   846
		$RL \; (r^*)$ & $\dn$ & $ (RL(r))^*$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   847
	\end{tabular}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   848
\end{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   849
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   850
The main use of $RL$ is to establish some connections between $\rsimp{}$ 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   851
and $\rnullable{}$:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   852
\begin{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   853
	The following properties hold:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   854
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   855
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   856
			If $\rnullable{r}$, then $\rsimp{r} \neq \RZERO$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   857
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   858
			$\rnullable{r \backslash s} \quad $ if and only if $\quad \rnullable{\rderssimp{r}{s}}$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   859
	\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   860
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   861
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   862
	The first part is by induction on $r$. 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   863
	The second part is true because property 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   864
	\[ RL \; r = RL \; (\rsimp{r})\] holds.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   865
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   866
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   867
\subsubsection{Simplified $\textit{Rrexp}$s are Good}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   868
We formalise the notion of ``good" regular expressions,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   869
which means regular expressions that
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   870
are fully simplified in terms of our $\textit{rsimp}$ function. 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   871
For alternative regular expressions that means they
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   872
do not contain any nested alternatives, un-eliminated $\RZERO$s
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   873
or duplicate elements (for example, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   874
$r_1 + (r_2 + r_3)$, $\RZERO + r$ and $ \sum [r, r, \ldots]$).
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   875
The clauses for $\good$ are:
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   876
\begin{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   877
	\begin{tabular}{@{}lcl@{}}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   878
		$\good\; \RZERO$ & $\dn$ & $\textit{false}$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   879
		$\good\; \RONE$ & $\dn$ & $\textit{true}$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   880
		$\good\; \RCHAR{c}$ & $\dn$ & $\btrue$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   881
		$\good\; \RALTS{[]}$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   882
		$\good\; \RALTS{[r]}$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   883
		$\good\; \RALTS{r_1 :: r_2 :: rs}$ & $\dn$ & 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   884
		$\textit{isDistinct} \; (r_1 :: r_2 :: rs) \;$\\
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   885
						   & & $\land \; (\forall r' \in (r_1 :: r_2 :: rs).\; \good \; r'\; \,  \land \; \, \textit{nonAlt}\; r')$\\
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   886
		$\good \; \RSEQ{\RZERO}{r}$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   887
		$\good \; \RSEQ{\RONE}{r}$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   888
		$\good \; \RSEQ{r}{\RZERO}$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   889
		$\good \; \RSEQ{r_1}{r_2}$ & $\dn$ & $\good \; r_1 \;\, \textit{and} \;\, \good \; r_2$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   890
		$\good \; \RSTAR{r}$ & $\dn$ & $\btrue$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   891
	\end{tabular}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   892
\end{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   893
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   894
We omit the recursive definition of the predicate $\textit{nonAlt}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   895
which evaluates to true when the regular expression is not an
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   896
alternative, and false otherwise.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   897
The $\good$ property is preserved under $\rsimp_{ALTS}$, provided that
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   898
its non-empty argument list of expressions are all good themsleves, and $\textit{nonAlt}$, 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   899
and unique:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   900
\begin{lemma}\label{rsimpaltsGood}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   901
	If $rs \neq []$ and forall $r \in rs. \textit{nonAlt} \; r$ and $\textit{isDistinct} \; rs$,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   902
	then $\good \; (\rsimpalts \; rs)$ if and only if forall $r \in rs. \; \good \; r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   903
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   904
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   905
We also note that
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   906
if a regular expression $r$ is good, then $\rflts$ on the singleton
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   907
list $[r]$ will not break goodness:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   908
\begin{lemma}\label{flts2}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   909
	If $\good \; r$, then forall $r' \in \rflts \; [r]. \; \good \; r'$ and $\textit{nonAlt} \; r'$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   910
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   911
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   912
	By an induction on $r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   913
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   914
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   915
The other observation we make about $\rsimp{r}$ is that it never
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   916
comes with nested alternatives, which we describe as the $\nonnested$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   917
property:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   918
\begin{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   919
	\begin{tabular}{lcl}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   920
		$\nonnested \; \, \sum []$ & $\dn$ & $\btrue$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   921
		$\nonnested \; \, \sum ((\sum rs_1) :: rs_2)$ & $\dn$ & $\bfalse$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   922
		$\nonnested \; \, \sum (r :: rs)$ & $\dn$ & $\nonnested (\sum rs)$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   923
		$\nonnested \; \, r $ & $\dn$ & $\btrue$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   924
	\end{tabular}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   925
\end{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   926
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   927
The $\rflts$ function
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   928
always opens up nested alternatives,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   929
which enables $\rsimp$ to be non-nested:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   930
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   931
\begin{lemma}\label{nonnestedRsimp}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   932
	It is always the case that
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   933
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   934
		$\nonnested \; (\rsimp{r})$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   935
	\end{center}
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   936
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   937
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   938
	By induction on $r$.
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   939
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   940
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   941
With this we could prove that a regular expressions
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   942
after simplification and flattening and de-duplication,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   943
will not contain any alternative regular expression directly:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   944
\begin{lemma}\label{nonaltFltsRd}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   945
	If $x \in \rdistinct{\rflts\; (\map \; \rsimp{} \; rs)}{\varnothing}$ 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   946
	then $\textit{nonAlt} \; x$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   947
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   948
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   949
	By \ref{nonnestedRsimp}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   950
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   951
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   952
The other fact we know is that once $\rsimp{}$ has finished
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   953
processing an alternative regular expression, it will not
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   954
contain any $\RZERO$s. This is because all the recursive 
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   955
calls to the simplification on the children regular expressions
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   956
make the children good, and $\rflts$ will not delete
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   957
any $\RZERO$s out of a good regular expression list,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   958
and $\rdistinct{}$ will not ``mess'' with the result.
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   959
\begin{lemma}\label{flts3Obv}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   960
	The following are true:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   961
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   962
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   963
			If for all $r \in rs. \, \good \; r $ or $r = \RZERO$,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   964
			then for all $r \in \rflts\; rs. \, \good \; r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   965
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   966
			If $x \in \rdistinct{\rflts\; (\map \; rsimp{}\; rs)}{\varnothing}$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   967
			and for all $y$ such that $\llbracket y \rrbracket_r$ less than
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   968
			$\llbracket rs \rrbracket_r + 1$, either
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   969
			$\good \; (\rsimp{y})$ or $\rsimp{y} = \RZERO$,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   970
			then $\good \; x$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   971
	\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   972
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   973
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   974
	The first part is by induction on $rs$, where the induction
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   975
	rule is the inductive cases for $\rflts$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   976
	The second part is a corollary from the first part.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   977
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   978
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   979
And this leads to good structural property of $\rsimp{}$,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   980
that after simplification, a regular expression is
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   981
either good or $\RZERO$:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   982
\begin{lemma}\label{good1}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   983
	For any r-regular expression $r$, $\good \; \rsimp{r}$ or $\rsimp{r} = \RZERO$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   984
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   985
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   986
	By an induction on $r$. The inductive measure is the size $\llbracket \rrbracket_r$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
   987
	Lemma \ref{rsimpMono} says that 
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   988
	$\llbracket \rsimp{r}\rrbracket_r$ is smaller than or equal to
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   989
	$\llbracket r \rrbracket_r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   990
	Therefore, in the $r_1 \cdot r_2$ and $\sum rs$ case,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   991
	Inductive hypothesis applies to the children regular expressions
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   992
	$r_1$, $r_2$, etc. The lemma \ref{flts3Obv}'s precondition is satisfied
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   993
	by that as well.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   994
	The lemmas \ref{nonnestedRsimp} and  \ref{nonaltFltsRd} are used
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   995
	to ensure that goodness is preserved at the topmost level.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   996
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   997
We shall prove that any good regular expression is 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   998
a fixed-point for $\rsimp{}$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
   999
First we prove an auxiliary lemma:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1000
\begin{lemma}\label{goodaltsNonalt}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1001
	If $\good \; \sum rs$, then $\rflts\; rs = rs$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1002
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1003
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1004
	By an induction on $\sum rs$. The inductive rules are the cases
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1005
	for $\good$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1006
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1007
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1008
Now we are ready to prove that good regular expressions are invariant
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1009
with respect to $\rsimp{}$:
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1010
\begin{lemma}\label{test}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1011
	If $\good \;r$ then $\rsimp{r} = r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1012
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1013
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1014
	By an induction on the inductive cases of $\good$, using lemmas
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1015
	\ref{goodaltsNonalt} and \ref{rdistinctOnDistinct}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1016
	The lemma \ref{goodaltsNonalt} is used in the alternative
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1017
	case where 2 or more elements are present in the list.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1018
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1019
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1020
Given below is a property involving $\rflts$, $\textit{rdistinct}$, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1021
$\rsimp{}$ and $\rsimp_{ALTS}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1022
which requires $\ref{good1}$ to go through smoothly:
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1023
\begin{lemma}\label{flattenRsimpalts}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1024
An application of $\rsimp_{ALTS}$ can be ``absorbed'',
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1025
if its output is concatenated with a list and then applied to $\rflts$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1026
\begin{center}
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1027
	$\rflts \; ( (\rsimp_{ALTS} \; 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1028
	(\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing})) :: 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1029
	\map \; \rsimp{} \; rs' ) = 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1030
	\rflts \; ( (\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing}) @ (
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1031
	\map \; \rsimp{rs'}))$
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1032
\end{center}
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1033
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1034
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1035
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1036
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1037
	By \ref{good1}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1038
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1039
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1040
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1041
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1042
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1043
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1044
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1045
We are also ready to prove that $\textit{rsimp}$ is idempotent.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1046
\subsubsection{$\rsimp$ is Idempotent}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1047
The idempotency of $\rsimp$ is very useful in 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1048
manipulating regular expression terms into desired
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1049
forms so that key steps allowing further rewriting to closed forms
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1050
are possible.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1051
\begin{lemma}\label{rsimpIdem}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1052
	$\rsimp{r} = \rsimp{(\rsimp{r})}$
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1053
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1054
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1055
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1056
	By \ref{test} and \ref{good1}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1057
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1058
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1059
This property means we do not have to repeatedly
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1060
apply simplification in each step, which justifies
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1061
our definition of $\blexersimp$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1062
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1063
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1064
On the other hand, we can repeat the same $\rsimp{}$ applications
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1065
on regular expressions as many times as we want, if we have at least
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1066
one simplification applied to it, and apply it wherever we would like to:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1067
\begin{corollary}\label{headOneMoreSimp}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1068
	The following properties hold, directly from \ref{rsimpIdem}:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1069
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1070
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1071
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1072
			$\map \; \rsimp{(r :: rs)} = \map \; \rsimp{} \; (\rsimp{r} :: rs)$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1073
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1074
			$\rsimp{(\RALTS{rs})} = \rsimp{(\RALTS{\map \; \rsimp{} \; rs})}$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1075
	\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1076
\end{corollary}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1077
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1078
This will be useful in the later closed form proof's rewriting steps.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1079
Similarly, we state the following useful facts below:
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1080
\begin{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1081
	The following equalities hold if $r = \rsimp{r'}$ for some $r'$:
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1082
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1083
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1084
			If $r  = \sum rs$ then $\rsimpalts \; rs = \sum rs$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1085
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1086
			If $r = \sum rs$ then $\rdistinct{rs}{\varnothing} = rs$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1087
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1088
			$\rsimpalts \; (\rdistinct{\rflts \; [r]}{\varnothing}) = r$.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1089
	\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1090
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1091
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1092
	By application of lemmas \ref{rsimpIdem} and \ref{good1}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1093
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1094
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1095
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1096
With the idempotency of $\rsimp{}$ and its corollaries, 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1097
we can start proving some key equalities leading to the 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1098
closed forms.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1099
Now presented are a few equivalent terms under $\rsimp{}$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1100
To make the notation more concise
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1101
We use $r_1 \sequal r_2 $ to denote that $\rsimp{r_1} = \rsimp{r_2}$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1102
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1103
\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1104
	$a \sequal b$ & $ \dn$ & $ \textit{rsimp} \; a = \textit{rsimp} \; b$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1105
\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1106
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1107
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1108
%\vspace{0em}
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1109
\begin{lemma}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1110
	The following equivalence hold:
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1111
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1112
	\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1113
		$\rsimpalts \; (\RZERO :: rs) \sequal \rsimpalts\; rs$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1114
	\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1115
		$\rsimpalts \; rs \sequal \rsimpalts (\map \; \rsimp{} \; rs)$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1116
	\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1117
		$\RALTS{\RALTS{rs}} \sequal \RALTS{rs}$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1118
	\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1119
		$\sum ((\sum rs_a) :: rs_b) \sequal \sum rs_a @ rs_b$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1120
	\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1121
		$\RALTS{rs} = \RALTS{\map \; \rsimp{} \; rs}$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1122
\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1123
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1124
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1125
	By induction on the lists involved.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1126
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1127
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1128
The above allows us to prove
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1129
two similar equalities (which are a bit more involved).
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1130
It says that we could flatten the elements
614
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1131
before simplification and still get the same result.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1132
\begin{lemma}\label{simpFlatten3}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1133
	One can flatten the inside $\sum$ of a $\sum$ if it is being 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1134
	simplified. Concretely,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1135
	\begin{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1136
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1137
			If for all $r \in rs, rs', rs''$, we have $\good \; r $
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1138
			or $r = \RZERO$, then $\sum (rs' @ rs @ rs'') \sequal 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1139
			\sum (rs' @ [\sum rs] @ rs'')$ holds. As a corollary,
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1140
		\item
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1141
			$\sum (rs' @ [\sum rs] @ rs'') \sequal \sum (rs' @ rs @ rs'')$
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1142
	\end{itemize}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1143
\end{lemma}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1144
\begin{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1145
	By rewriting steps involving the use of \ref{test} and \ref{rdistinctConcatGeneral}.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1146
	The second sub-lemma is a corollary of the previous.
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1147
\end{proof}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1148
%Rewriting steps not put in--too long and complicated-------------------------------
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1149
\begin{comment}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1150
	\begin{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1151
		$\rsimp{\sum (rs' @ rs @ rs'')}  \stackrel{def of bsimp}{=}$  \\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1152
		$\rsimpalts \; (\rdistinct{\rflts \; ((\map \; \rsimp{}\; rs') @ (\map \; \rsimp{} \; rs ) @ (\map \; \rsimp{} \; rs''))}{\varnothing})$ \\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1153
		$\stackrel{by \ref{test}}{=} 
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1154
		\rsimpalts \; (\rdistinct{(\rflts \; rs' @ \rflts \; rs @ \rflts \; rs'')}{
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1155
		\varnothing})$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1156
		$\stackrel{by \ref{rdistinctConcatGeneral}}{=}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1157
		\rsimpalts \; (\rdistinct{\rflts \; rs'}{\varnothing} @ \rdistinct{(
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1158
		\rflts\; rs @ \rflts \; rs'')}{\rflts \; rs'})$\\
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1159
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1160
	\end{center}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1161
\end{comment}
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1162
%Rewriting steps not put in--too long and complicated-------------------------------
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1163
\noindent
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1164
d5e9bcb384ec reorder
Chengsong
parents: 613
diff changeset
  1165
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1166
We need more equalities like the above to enable a closed form lemma,
613
Chengsong
parents: 611
diff changeset
  1167
for which we need to introduce a few rewrite relations
Chengsong
parents: 611
diff changeset
  1168
to help
Chengsong
parents: 611
diff changeset
  1169
us obtain them.
554
Chengsong
parents: 553
diff changeset
  1170
610
d028c662a3df data files
Chengsong
parents: 609
diff changeset
  1171
\subsection{The rewrite relation $\hrewrite$ , $\scfrewrites$ , $\frewrite$ and $\grewrite$}
613
Chengsong
parents: 611
diff changeset
  1172
Inspired by the success we had in the correctness proof 
Chengsong
parents: 611
diff changeset
  1173
in \ref{Bitcoded2},
Chengsong
parents: 611
diff changeset
  1174
we follow suit here, defining atomic simplification
Chengsong
parents: 611
diff changeset
  1175
steps as ``small-step'' rewriting steps. This allows capturing 
555
Chengsong
parents: 554
diff changeset
  1176
similarities between terms that would be otherwise
Chengsong
parents: 554
diff changeset
  1177
hard to express.
Chengsong
parents: 554
diff changeset
  1178
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1179
We use $\hrewrite$ for one-step atomic rewrite of 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1180
regular expression simplification, 
555
Chengsong
parents: 554
diff changeset
  1181
$\frewrite$ for rewrite of list of regular expressions that 
Chengsong
parents: 554
diff changeset
  1182
include all operations carried out in $\rflts$, and $\grewrite$ for
613
Chengsong
parents: 611
diff changeset
  1183
rewriting a list of regular expressions possible in both $\rflts$ and $\textit{rdistinct}$.
555
Chengsong
parents: 554
diff changeset
  1184
Their reflexive transitive closures are used to denote zero or many steps,
Chengsong
parents: 554
diff changeset
  1185
as was the case in the previous chapter.
613
Chengsong
parents: 611
diff changeset
  1186
As we have already
Chengsong
parents: 611
diff changeset
  1187
done something similar, the presentation about
Chengsong
parents: 611
diff changeset
  1188
these rewriting rules will be more concise than that in \ref{Bitcoded2}.
554
Chengsong
parents: 553
diff changeset
  1189
To differentiate between the rewriting steps for annotated regular expressions
Chengsong
parents: 553
diff changeset
  1190
and $\rrexp$s, we add characters $h$ and $g$ below the squig arrow symbol
Chengsong
parents: 553
diff changeset
  1191
to mean atomic simplification transitions 
Chengsong
parents: 553
diff changeset
  1192
of $\rrexp$s and $\rrexp$ lists, respectively.
Chengsong
parents: 553
diff changeset
  1193
555
Chengsong
parents: 554
diff changeset
  1194
Chengsong
parents: 554
diff changeset
  1195
Chengsong
parents: 554
diff changeset
  1196
613
Chengsong
parents: 611
diff changeset
  1197
\begin{figure}[H]
554
Chengsong
parents: 553
diff changeset
  1198
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1199
	\begin{mathpar}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1200
		\inferrule[RSEQ0L]{}{\RZERO \cdot r_2 \hrewrite \RZERO\\}
555
Chengsong
parents: 554
diff changeset
  1201
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1202
		\inferrule[RSEQ0R]{}{r_1 \cdot \RZERO \hrewrite \RZERO\\}
555
Chengsong
parents: 554
diff changeset
  1203
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1204
		\inferrule[RSEQ1]{}{(\RONE \cdot r) \hrewrite  r\\}\\	
555
Chengsong
parents: 554
diff changeset
  1205
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1206
		\inferrule[RSEQL]{ r_1 \hrewrite r_2}{r_1 \cdot r_3 \hrewrite r_2 \cdot r_3\\}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1207
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1208
		\inferrule[RSEQR]{ r_3 \hrewrite r_4}{r_1 \cdot r_3 \hrewrite r_1 \cdot r_4\\}\\
555
Chengsong
parents: 554
diff changeset
  1209
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1210
		\inferrule[RALTSChild]{r \hrewrite r'}{\sum (rs_1 @ [r] @ rs_2) \hrewrite \sum (rs_1 @ [r'] @ rs_2)\\}
555
Chengsong
parents: 554
diff changeset
  1211
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1212
		\inferrule[RALTS0]{}{\sum (rs_a @ [\RZERO] @ rs_b) \hrewrite \sum (rs_a @ rs_b)}
555
Chengsong
parents: 554
diff changeset
  1213
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1214
		\inferrule[RALTSNested]{}{\sum (rs_a @ [\sum rs_1] @ rs_b) \hrewrite \sum (rs_a @ rs_1 @ rs_b)}
555
Chengsong
parents: 554
diff changeset
  1215
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1216
		\inferrule[RALTSNil]{}{ \sum [] \hrewrite \RZERO\\}
555
Chengsong
parents: 554
diff changeset
  1217
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1218
		\inferrule[RALTSSingle]{}{ \sum [r] \hrewrite  r\\}	
555
Chengsong
parents: 554
diff changeset
  1219
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1220
		\inferrule[RALTSDelete]{\\ r_1 = r_2}{\sum rs_a @ [r_1] @ rs_b @ [r_2] @ rsc \hrewrite \sum rs_a @ [r_1] @ rs_b @ rs_c}
555
Chengsong
parents: 554
diff changeset
  1221
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1222
	\end{mathpar}
555
Chengsong
parents: 554
diff changeset
  1223
\end{center}
613
Chengsong
parents: 611
diff changeset
  1224
\caption{List of one-step rewrite rules for r-regular expressions ($\hrewrite$)}\label{hRewrite}
Chengsong
parents: 611
diff changeset
  1225
\end{figure}
554
Chengsong
parents: 553
diff changeset
  1226
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1227
613
Chengsong
parents: 611
diff changeset
  1228
Like $\rightsquigarrow_s$, it is
Chengsong
parents: 611
diff changeset
  1229
convenient to define rewrite rules for a list of regular expressions,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1230
where each element can rewrite in many steps to the other (scf stands for
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1231
li\emph{s}t \emph{c}losed \emph{f}orm). This relation is similar to the 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1232
$\stackrel{s*}{\rightsquigarrow}$ for annotated regular expressions.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1233
613
Chengsong
parents: 611
diff changeset
  1234
\begin{figure}[H]
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1235
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1236
	\begin{mathpar}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1237
		\inferrule{}{[] \scfrewrites [] }
613
Chengsong
parents: 611
diff changeset
  1238
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1239
		\inferrule{r \hrewrites r' \\ rs \scfrewrites rs'}{r :: rs \scfrewrites r' :: rs'}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1240
	\end{mathpar}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1241
\end{center}
613
Chengsong
parents: 611
diff changeset
  1242
\caption{List of one-step rewrite rules for a list of r-regular expressions}\label{scfRewrite}
Chengsong
parents: 611
diff changeset
  1243
\end{figure}
555
Chengsong
parents: 554
diff changeset
  1244
%frewrite
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1245
List of one-step rewrite rules for flattening 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1246
a list of  regular expressions($\frewrite$):
613
Chengsong
parents: 611
diff changeset
  1247
\begin{figure}[H]
555
Chengsong
parents: 554
diff changeset
  1248
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1249
	\begin{mathpar}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1250
		\inferrule{}{\RZERO :: rs \frewrite rs \\}
555
Chengsong
parents: 554
diff changeset
  1251
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1252
		\inferrule{}{(\sum rs) :: rs_a \frewrite rs @ rs_a \\}
555
Chengsong
parents: 554
diff changeset
  1253
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1254
		\inferrule{rs_1 \frewrite rs_2}{r :: rs_1 \frewrite r :: rs_2}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1255
	\end{mathpar}
555
Chengsong
parents: 554
diff changeset
  1256
\end{center}
613
Chengsong
parents: 611
diff changeset
  1257
\caption{List of one-step rewrite rules characterising the $\rflts$ operation on a list}\label{fRewrites}
Chengsong
parents: 611
diff changeset
  1258
\end{figure}
555
Chengsong
parents: 554
diff changeset
  1259
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1260
Lists of one-step rewrite rules for flattening and de-duplicating
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1261
a list of regular expressions ($\grewrite$):
613
Chengsong
parents: 611
diff changeset
  1262
\begin{figure}[H]
555
Chengsong
parents: 554
diff changeset
  1263
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1264
	\begin{mathpar}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1265
		\inferrule{}{\RZERO :: rs \grewrite rs \\}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1266
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1267
		\inferrule{}{(\sum rs) :: rs_a \grewrite rs @ rs_a \\}
555
Chengsong
parents: 554
diff changeset
  1268
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1269
		\inferrule{rs_1 \grewrite rs_2}{r :: rs_1 \grewrite r :: rs_2}
555
Chengsong
parents: 554
diff changeset
  1270
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1271
		\inferrule[dB]{}{rs_a @ [a] @ rs_b @[a] @ rs_c \grewrite rs_a @ [a] @ rsb @ rsc}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1272
	\end{mathpar}
555
Chengsong
parents: 554
diff changeset
  1273
\end{center}
613
Chengsong
parents: 611
diff changeset
  1274
\caption{List of one-step rewrite rules characterising the $\rflts$ and $\textit{rdistinct}$
Chengsong
parents: 611
diff changeset
  1275
operations}\label{gRewrite}
Chengsong
parents: 611
diff changeset
  1276
\end{figure}
555
Chengsong
parents: 554
diff changeset
  1277
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1278
We define
613
Chengsong
parents: 611
diff changeset
  1279
two separate list rewriting relations $\frewrite$ and $\grewrite$.
611
Chengsong
parents: 610
diff changeset
  1280
The rewriting steps that take place during
Chengsong
parents: 610
diff changeset
  1281
flattening is characterised by $\frewrite$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1282
The rewrite relation $\grewrite$ characterises both flattening and de-duplicating.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1283
Sometimes $\grewrites$ is slightly too powerful
613
Chengsong
parents: 611
diff changeset
  1284
so we would rather use $\frewrites$ to prove
Chengsong
parents: 611
diff changeset
  1285
%because we only
Chengsong
parents: 611
diff changeset
  1286
equalities related to $\rflts$.
Chengsong
parents: 611
diff changeset
  1287
%certain equivalence under the rewriting steps of $\frewrites$.
556
Chengsong
parents: 555
diff changeset
  1288
For example, when proving the closed-form for the alternative regular expression,
613
Chengsong
parents: 611
diff changeset
  1289
one of the equalities needed is:
Chengsong
parents: 611
diff changeset
  1290
\begin{center}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1291
	$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1292
	\sum (\rDistinct \;\;  (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1293
	$
613
Chengsong
parents: 611
diff changeset
  1294
\end{center}
556
Chengsong
parents: 555
diff changeset
  1295
\noindent
Chengsong
parents: 555
diff changeset
  1296
Proving this is by first showing 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1297
\begin{lemma}\label{earlyLaterDerFrewrites}
556
Chengsong
parents: 555
diff changeset
  1298
	$\map \; (\_ \backslash x) \;  (\rflts \; rs) \frewrites
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1299
	\rflts \; (\map \; (\_ \backslash x) \; rs)$
556
Chengsong
parents: 555
diff changeset
  1300
\end{lemma}
Chengsong
parents: 555
diff changeset
  1301
\noindent
613
Chengsong
parents: 611
diff changeset
  1302
and then the equivalence between two terms
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1303
that can reduce in many steps to each other:
556
Chengsong
parents: 555
diff changeset
  1304
\begin{lemma}\label{frewritesSimpeq}
Chengsong
parents: 555
diff changeset
  1305
	If $rs_1 \frewrites rs_2 $, then $\sum (\rDistinct \; rs_1 \; \varnothing) \sequal 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1306
	\sum (\rDistinct \;  rs_2 \;  \varnothing)$.
556
Chengsong
parents: 555
diff changeset
  1307
\end{lemma}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1308
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1309
These two lemmas can both be proven using a straightforward induction (and
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1310
the proofs for them are therefore omitted).
613
Chengsong
parents: 611
diff changeset
  1311
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1312
Now the above equalities can be derived like a breeze:
613
Chengsong
parents: 611
diff changeset
  1313
\begin{corollary}
Chengsong
parents: 611
diff changeset
  1314
	$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
Chengsong
parents: 611
diff changeset
  1315
	\sum (\rDistinct \;\;  (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
Chengsong
parents: 611
diff changeset
  1316
	$
Chengsong
parents: 611
diff changeset
  1317
\end{corollary}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1318
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1319
	By lemmas \ref{earlyLaterDerFrewrites} and \ref{frewritesSimpeq}.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1320
\end{proof}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1321
But this trick will not work for $\grewrites$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1322
For example, a rewriting step in proving
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1323
closed forms is:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1324
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1325
	$\rsimp{(\rsimpalts \; (\map \; (\_ \backslash x) \; (\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs}))))}{\varnothing})))}$\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1326
	$=$ \\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1327
	$\rsimp{(\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \; (\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}))} $
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1328
	\noindent
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1329
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1330
For this, one would hope to have a rewriting relation between the two lists involved,
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1331
similar to \ref{earlyLaterDerFrewrites}. However, it turns out that 
556
Chengsong
parents: 555
diff changeset
  1332
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1333
	$\map \; (\_ \backslash x) \; (\rDistinct \; rs \; rset) \grewrites \rDistinct \; (\map \;
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1334
	(\_ \backslash x) \; rs) \; ( rset \backslash x)$
556
Chengsong
parents: 555
diff changeset
  1335
\end{center}
Chengsong
parents: 555
diff changeset
  1336
\noindent
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1337
does $\mathbf{not}$ hold in general.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1338
For this rewriting step we will introduce some slightly more cumbersome
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1339
proof technique later.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1340
The point is that $\frewrite$
613
Chengsong
parents: 611
diff changeset
  1341
allows us to prove equivalence in a straightforward way that is 
Chengsong
parents: 611
diff changeset
  1342
not possible for $\grewrite$. 
555
Chengsong
parents: 554
diff changeset
  1343
556
Chengsong
parents: 555
diff changeset
  1344
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1345
\subsubsection{Terms That Can Be Rewritten Using $\hrewrites$, $\grewrites$, and $\frewrites$}
613
Chengsong
parents: 611
diff changeset
  1346
In this part, we present lemmas stating
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1347
pairs of r-regular expressions and r-regular expression lists
613
Chengsong
parents: 611
diff changeset
  1348
where one could rewrite from one in many steps to the other.
Chengsong
parents: 611
diff changeset
  1349
Most of the proofs to these lemmas are straightforward, using
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1350
an induction on the corresponding rewriting relations.
613
Chengsong
parents: 611
diff changeset
  1351
These proofs will therefore be omitted when this is the case.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1352
We present in the following lemma a few pairs of terms that are rewritable via 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1353
$\grewrites$:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1354
\begin{lemma}\label{gstarRdistinctGeneral}
613
Chengsong
parents: 611
diff changeset
  1355
	\mbox{}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1356
	\begin{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1357
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1358
			$rs_1 @ rs \grewrites rs_1 @ (\rDistinct \; rs \; rs_1)$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1359
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1360
			$rs \grewrites \rDistinct \; rs \; \varnothing$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1361
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1362
			$rs_a @ (\rDistinct \; rs \; rs_a) \grewrites rs_a @ (\rDistinct \; 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1363
			rs \; (\{\RZERO\} \cup rs_a))$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1364
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1365
			$rs \;\; @ \;\; \rDistinct \; rs_a \; rset \grewrites rs @  \rDistinct \; rs_a \;
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1366
			(rest \cup rs)$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1367
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1368
	\end{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1369
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1370
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1371
If a pair of terms $rs_1, rs_2$ are rewritable via $\grewrites$ to each other,
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1372
then they are equivalent under $\rsimp{}$:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1373
\begin{lemma}\label{grewritesSimpalts}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1374
	\mbox{}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1375
	If $rs_1 \grewrites rs_2$, then
613
Chengsong
parents: 611
diff changeset
  1376
	we have the following equivalence:
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1377
	\begin{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1378
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1379
			$\sum rs_1 \sequal \sum rs_2$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1380
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1381
			$\rsimpalts \; rs_1 \sequal \rsimpalts \; rs_2$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1382
	\end{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1383
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1384
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1385
Here are a few connecting lemmas showing that
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1386
if a list of regular expressions can be rewritten using $\grewrites$ or $\frewrites $ or
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1387
$\scfrewrites$,
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1388
then an alternative constructor taking the list can also be rewritten using $\hrewrites$:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1389
\begin{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1390
	\begin{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1391
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1392
			If $rs \grewrites rs'$ then $\sum rs \hrewrites \sum rs'$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1393
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1394
			If $rs \grewrites rs'$ then $\sum rs \hrewrites \rsimpalts \; rs'$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1395
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1396
			If $rs_1 \scfrewrites rs_2$ then $\sum (rs @ rs_1) \hrewrites \sum (rs @ rs_2)$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1397
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1398
			If $rs_1 \scfrewrites rs_2$ then $\sum rs_1 \hrewrites \sum rs_2$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1399
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1400
	\end{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1401
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1402
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1403
Now comes the core of the proof, 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1404
which says that once two lists are rewritable to each other,
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1405
then they are equivalent under $\rsimp{}$:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1406
\begin{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1407
	If $r_1 \hrewrites r_2$ then $r_1 \sequal r_2$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1408
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1409
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1410
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1411
Similar to what we did in \ref{Bitcoded2}, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1412
we prove that if one can rewrite from one r-regular expression ($r$)
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1413
to the other ($r'$), after taking derivatives one could still rewrite
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1414
the first ($r\backslash c$) to the other ($r'\backslash c$).
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1415
\begin{lemma}\label{interleave}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1416
	If $r \hrewrites r' $ then $\rder{c}{r} \hrewrites \rder{c}{r'}$
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1417
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1418
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1419
This allows us proving more $\mathbf{rsimp}$-equivalent terms, involving $\backslash_r$.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1420
\begin{lemma}\label{insideSimpRemoval}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1421
	$\rsimp{(\rder{c}{(\rsimp{r})})} = \rsimp{(\rder{c}{r})}  $
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1422
\end{lemma}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1423
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1424
\begin{proof}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1425
	By \ref{interleave} and \ref{rsimpIdem}.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1426
\end{proof}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1427
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1428
And this unlocks more equivalent terms:
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1429
\begin{lemma}\label{Simpders}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1430
	As corollaries of \ref{insideSimpRemoval}, we have
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1431
	\begin{itemize}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1432
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1433
			If $s \neq []$ then $\rderssimp{r}{s} = \rsimp{(\rders \; r \; s)}$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1434
		\item
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1435
			$\rsimpalts \; (\map \; (\_ \backslash_r x) \;
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1436
			(\rdistinct{rs}{\varnothing})) \sequal
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1437
			\rsimpalts \; (\rDistinct \; 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1438
			(\map \; (\_ \backslash_r x) rs) \;\varnothing  )$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1439
	\end{itemize}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1440
\end{lemma}
611
Chengsong
parents: 610
diff changeset
  1441
\begin{proof}
Chengsong
parents: 610
diff changeset
  1442
	Part 1 is by lemma \ref{insideSimpRemoval},
613
Chengsong
parents: 611
diff changeset
  1443
	part 2 is by lemma \ref{insideSimpRemoval} .%and \ref{distinctDer}.
611
Chengsong
parents: 610
diff changeset
  1444
\end{proof}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1445
\noindent
613
Chengsong
parents: 611
diff changeset
  1446
Chengsong
parents: 611
diff changeset
  1447
\subsection{Closed Forms for $\sum rs$, $r_1\cdot r_2$ and $r^*$}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1448
Lemma \ref{Simpders} leads to our first closed form,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1449
which is for the alternative regular expression:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1450
\begin{theorem}\label{altsClosedForm}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1451
	\mbox{}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1452
	\begin{center}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1453
		$\rderssimp{(\sum rs)}{s} \sequal
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1454
		\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1455
	\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1456
\end{theorem}
556
Chengsong
parents: 555
diff changeset
  1457
\noindent
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1458
\begin{proof}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1459
	By a reverse induction on the string $s$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1460
	One rewriting step, as we mentioned earlier,
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1461
	involves
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1462
	\begin{center}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1463
		$\rsimpalts \; (\map \; (\_ \backslash x) \; 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1464
		(\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \; 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1465
		(\lambda r. \rderssimp{r}{xs}))))}{\varnothing}))
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1466
		\sequal
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1467
		\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \; 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1468
			(\rflts \; (\map \; (\rsimp{} \; \circ \; 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1469
		(\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}) $.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1470
	\end{center}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1471
	This can be proven by a combination of 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1472
	\ref{grewritesSimpalts}, \ref{gstarRdistinctGeneral}, \ref{rderRsimpAltsCommute}, and
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1473
	\ref{insideSimpRemoval}.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1474
\end{proof}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1475
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1476
This closed form has a variant which can be more convenient in later proofs:
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1477
\begin{corollary}\label{altsClosedForm1}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1478
	If $s \neq []$ then 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1479
	$\rderssimp \; (\sum \; rs) \; s = 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1480
	\rsimp{(\sum \; (\map \; \rderssimp{\_}{s} \; rs))}$.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1481
\end{corollary}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1482
\noindent
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1483
The harder closed forms are the sequence and star ones.
613
Chengsong
parents: 611
diff changeset
  1484
Before we obtain them, some preliminary definitions
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1485
are needed to make proof statements concise.
556
Chengsong
parents: 555
diff changeset
  1486
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  1487
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  1488
\subsubsection{Closed Form for Sequence Regular Expressions}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1489
For the sequence regular expression,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1490
let's first look at a series of derivative steps on it 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1491
(assuming that each time when a derivative is taken,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1492
the head of the sequence is always nullable):
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1493
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1494
	\begin{tabular}{llll}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1495
		$r_1 \cdot r_2$ &  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1496
		$\longrightarrow_{\backslash c}$ &  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1497
		$r_1\backslash c \cdot r_2 + r_2 \backslash c$ &
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1498
		$ \longrightarrow_{\backslash c'} $ \\ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1499
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1500
		$(r_1 \backslash cc' \cdot r_2 + r_2 \backslash c') + r_2 \backslash cc'$ & 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1501
		$\longrightarrow_{\backslash c''} $ &
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1502
		$((r_1 \backslash cc'c'' \cdot r_2 + r_2 \backslash c'') + r_2 \backslash c'c'') 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1503
		+ r_2 \backslash cc'c''$ & 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1504
		$   \longrightarrow_{\backslash c''} \quad \ldots$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1505
	\end{tabular}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1506
\end{center}
558
Chengsong
parents: 557
diff changeset
  1507
Roughly speaking $r_1 \cdot r_2 \backslash s$ can be expresssed as 
Chengsong
parents: 557
diff changeset
  1508
a giant alternative taking a list of terms 
Chengsong
parents: 557
diff changeset
  1509
$[r_1 \backslash_r s \cdot r_2, r_2 \backslash_r s'', r_2 \backslash_r s_1'', \ldots]$,
Chengsong
parents: 557
diff changeset
  1510
where the head of the list is always the term
Chengsong
parents: 557
diff changeset
  1511
representing a match involving only $r_1$, and the tail of the list consisting of
Chengsong
parents: 557
diff changeset
  1512
terms of the shape $r_2 \backslash_r s''$, $s''$ being a suffix of $s$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1513
This intuition is also echoed by Murugesan and Sundaram \cite{Murugesan2014}, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1514
where they gave
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1515
a pencil-and-paper derivation of $(r_1 \cdot r_2)\backslash s$:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1516
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1517
	\begin{tabular}{lc}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1518
		$L \; [ (r_1 \cdot r_2) \backslash_r (c_1 :: c_2 :: \ldots c_n) ]$ & $ =$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1519
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1520
		\rule{0pt}{3ex} $L \; [ ((r_1 \backslash_r c_1) \cdot r_2 + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1521
		(\delta\; (\nullable \; r_1) \; (r_2 \backslash_r c_1) )) \backslash_r 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1522
		(c_2 :: \ldots c_n) ]$ &
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1523
		$=$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1524
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1525
		\rule{0pt}{3ex} $L \; [ ((r_1 \backslash_r c_1c_2 \cdot r_2 + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1526
		(\delta \; (\nullable \; r_1) \; (r_2 \backslash_r c_1c_2)))
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1527
		$ & \\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1528
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1529
		$\quad + (\delta \ (\nullable \; r_1 \backslash_r c)\; (r_2 \backslash_r c_2) )) 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1530
		\backslash_r (c_3 \ldots c_n) ]$ & $\ldots$ \\
558
Chengsong
parents: 557
diff changeset
  1531
	\end{tabular}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1532
\end{center}
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1533
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1534
The $\delta$ function 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1535
returns $r$ when the boolean condition
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1536
$b$ evaluates to true and
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1537
$\ZERO$ otherwise:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1538
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1539
	\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1540
		$\delta \; b\; r$ & $\dn$ & $r \quad \textit{if} \; b \; is \;\textit{true}$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1541
				  & $\dn$ & $\ZERO \quad otherwise$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1542
	\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1543
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1544
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1545
Note that the term
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1546
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1547
	\begin{tabular}{lc}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1548
		\rule{0pt}{3ex} $((r_1 \backslash_r c_1c_2 \cdot r_2 + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1549
		(\delta \; (\nullable \; r_1) \; (r_2 \backslash_r c_1c_2)))
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1550
		$ & \\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1551
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1552
		$\quad + (\delta \ (\nullable \; r_1 \backslash_r c)\; (r_2 \backslash_r c_2) )) 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1553
		\backslash_r (c_3 \ldots c_n)$ &\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1554
	\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1555
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1556
\noindent
558
Chengsong
parents: 557
diff changeset
  1557
does not faithfully
Chengsong
parents: 557
diff changeset
  1558
represent what the intermediate derivatives would actually look like
Chengsong
parents: 557
diff changeset
  1559
when one or more intermediate results $r_1 \backslash s' \cdot r_2$ are not 
Chengsong
parents: 557
diff changeset
  1560
nullable in the head of the sequence.
Chengsong
parents: 557
diff changeset
  1561
For example, when $r_1$ and $r_1 \backslash_r c_1$ are not nullable,
Chengsong
parents: 557
diff changeset
  1562
the regular expression would not look like 
Chengsong
parents: 557
diff changeset
  1563
\[
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1564
	r_1 \backslash_r c_1c_2
558
Chengsong
parents: 557
diff changeset
  1565
\]
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1566
instead of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1567
\[
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1568
	(r_1 \backslash_r c_1c_2 + \ZERO ) + \ZERO.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1569
\]
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1570
The redundant $\ZERO$s will not be created in the
558
Chengsong
parents: 557
diff changeset
  1571
first place.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1572
In a closed-form one needs to take into account this (because
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1573
closed forms require exact equality rather than language equivalence)
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1574
and only generate the 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1575
$r_2 \backslash_r s''$ terms satisfying the property
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1576
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1577
$\exists  s'.  such \; that \; s'@s'' = s \;\; \land \;\;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1578
r_1 \backslash s' \; is \; nullable$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1579
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1580
Given the arguments $s$ and $r_1$, we denote the list of strings
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1581
$s''$ satisfying the above property as $\vsuf{s}{r_1}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1582
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string\footnote{
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1583
	Perhaps a better name of it would be ``NullablePrefixSuffix''
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1584
	to differentiate with the list of \emph{all} prefixes of $s$, but
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1585
	that is a bit too long for a function name and we are yet to find
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1586
a more concise and easy-to-understand name.}
558
Chengsong
parents: 557
diff changeset
  1587
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1588
	\begin{tabular}{lcl}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1589
		$\vsuf{[]}{\_} $ & $=$ &  $[]$\\
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1590
		$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} \; (\rnullable{r_1}) \; \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1591
				    && $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) })  $
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1592
	\end{tabular}
558
Chengsong
parents: 557
diff changeset
  1593
\end{center}
Chengsong
parents: 557
diff changeset
  1594
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1595
The list starts with shorter suffixes
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1596
and ends with longer ones (in other words, the string elements $s''$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1597
in the list $\vsuf{s}{r_1}$ are sorted
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1598
in the same order as that of the terms $r_2\backslash s''$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1599
appearing in $(r_1\cdot r_2)\backslash s$).
558
Chengsong
parents: 557
diff changeset
  1600
In essence, $\vsuf{\_}{\_}$ is doing a 
Chengsong
parents: 557
diff changeset
  1601
"virtual derivative" of $r_1 \cdot r_2$, but instead of producing 
Chengsong
parents: 557
diff changeset
  1602
the entire result $(r_1 \cdot r_2) \backslash s$, 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1603
it only stores strings,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1604
with each string $s''$ representing a term such that $r_2 \backslash s''$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1605
is occurring in $(r_1\cdot r_2)\backslash s$.
558
Chengsong
parents: 557
diff changeset
  1606
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1607
With $\textit{Suffix}$ we are ready to express the
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1608
sequence regular expression's closed form,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1609
but before doing so 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1610
more devices are needed.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1611
The first thing is the flattening function $\sflat{\_}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1612
which takes an alternative regular expression and produces a flattened version
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1613
of that alternative regular expression.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1614
It is needed to convert
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1615
a left-associative nested sequence of alternatives into 
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1616
a flattened list:
558
Chengsong
parents: 557
diff changeset
  1617
\[
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1618
	\sum(\ldots ((r_1 + r_2) + r_3) + \ldots)
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1619
	\stackrel{\sflat{\_}}{\rightarrow} 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1620
	\sum[r_1, r_2, r_3, \ldots]
558
Chengsong
parents: 557
diff changeset
  1621
\]
Chengsong
parents: 557
diff changeset
  1622
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1623
The definitions of $\sflat{\_}$ and helper functions
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1624
$\sflataux{\_}$ and $\llparenthesis \_ \rrparenthesis''$ are given below.
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1625
\begin{center}  
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1626
	\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1627
		$\sflataux{\sum r :: rs}$ & $\dn$ & $\sflataux{r} @ rs$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1628
		$\sflataux{\sum []}$ & $ \dn $ & $ []$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1629
		$\sflataux r$ & $\dn$ & $ [r]$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1630
	\end{tabular}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1631
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1632
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1633
\begin{center} 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1634
	\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1635
		$\sflat{(\sum r :: rs)}$ & $\dn$ & $\sum (\sflataux{r} @ rs)$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1636
		$\sflat{\sum []}$ & $ \dn $ & $ \sum []$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1637
		$\sflat r$ & $\dn$ & $ r$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1638
	\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1639
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1640
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1641
\begin{center}  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1642
	\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1643
		$\sflataux{[]}'$ & $ \dn $ & $ []$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1644
		$\sflataux{ (r_1 + r_2) :: rs }'$ & $\dn$ & $r_1 :: r_2 :: rs$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1645
		$\sflataux{r :: rs}$ & $\dn$ & $ r::rs$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1646
	\end{tabular}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1647
\end{center}
558
Chengsong
parents: 557
diff changeset
  1648
\noindent
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
  1649
$\sflataux{\_}$ breaks up nested alternative regular expressions 
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1650
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
558
Chengsong
parents: 557
diff changeset
  1651
into a "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1652
It will return the singleton list $[r]$ otherwise.
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1653
$\sflat{\_}$ works the same  as $\sflataux{\_}$, except that it keeps
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1654
the output type a regular expression, not a list.
558
Chengsong
parents: 557
diff changeset
  1655
$\sflataux{\_}$  and $\sflat{\_}$ are only recursive on the  
Chengsong
parents: 557
diff changeset
  1656
first element of the list.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1657
$\sflataux{\_}'$ takes a list of regular expressions as input, and outputs
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1658
a list of regular expressions.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1659
The use of $\sflataux{\_}$ and $\sflataux{\_}'$ is clear once we have
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1660
$\textit{createdBySequence}$ defined:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1661
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1662
	\begin{mathpar}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1663
		\inferrule{\mbox{}}{\textit{createdBySequence}\; (r_1 \cdot r_2)}
558
Chengsong
parents: 557
diff changeset
  1664
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1665
		\inferrule{\textit{createdBySequence} \; r_1}{\textit{createdBySequence} \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1666
		(r_1 + r_2)}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1667
	\end{mathpar}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1668
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1669
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1670
The predicate $\textit{createdBySequence}$ is used to describe the shape of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1671
the derivative regular expressions $(r_1\cdot r_2) \backslash s$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1672
\begin{lemma}\label{recursivelyDerseq}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1673
	It is always the case that
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1674
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1675
		$\textit{createdBySequence} \; ( (r_1\cdot r_2) \backslash_r s) $
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1676
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1677
	holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1678
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1679
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1680
	By a reverse induction on the string $s$, where the inductive cases are $[]$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1681
	and $xs  @ [x]$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1682
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1683
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1684
If we have a regular expression $r$ whose shpae 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1685
fits into those described by $\textit{createdBySequence}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1686
then we can convert between 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1687
$r \backslash_r c$ and $(\sflataux{r}) \backslash_r c$ with
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1688
$\sflataux{\_}'$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1689
\begin{lemma}\label{sfauIdemDer}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1690
	If $\textit{createdBySequence} \; r$, then 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1691
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1692
		$\sflataux{ r \backslash_r c} = 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1693
		\llparenthesis (\map \; (\_ \backslash_r c) \; (\sflataux{r}) ) \rrparenthesis''$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1694
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1695
	holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1696
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1697
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1698
	By a simple induction on the inductive cases of $\textit{createdBySequence}.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1699
	$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1700
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1701
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1702
Now we are ready to express
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1703
the shape of $r_1 \cdot r_2 \backslash s$
558
Chengsong
parents: 557
diff changeset
  1704
\begin{lemma}\label{seqSfau0}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1705
	$\sflataux{(r_1 \cdot r_2) \backslash_r s} = (r_1 \backslash_r s) \cdot r_2 
558
Chengsong
parents: 557
diff changeset
  1706
	:: (\map \; (r_2 \backslash_r \_) \; (\textit{Suffix} \; s \; r1))$ 
Chengsong
parents: 557
diff changeset
  1707
\end{lemma}
Chengsong
parents: 557
diff changeset
  1708
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1709
	By a reverse induction on the string $s$, where the inductive cases 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1710
	are $[]$ and $xs @ [x]$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1711
	For the inductive case, we know that $\textit{createdBySequence} \; ((r_1 \cdot r_2)
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1712
	\backslash_r xs)$ holds from lemma \ref{recursivelyDerseq},
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1713
	which can be used to prove
558
Chengsong
parents: 557
diff changeset
  1714
	\[
Chengsong
parents: 557
diff changeset
  1715
		\map \; (r_2 \backslash_r \_) \; (\vsuf{[x]}{(r_1 \backslash_r xs)}) \;\; @ \;\;
Chengsong
parents: 557
diff changeset
  1716
		\map \; (\_ \backslash_r x) \; (\map \; (r_2 \backslash \_) \; (\vsuf{xs}{r_1}))
Chengsong
parents: 557
diff changeset
  1717
	\]
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1718
	=
558
Chengsong
parents: 557
diff changeset
  1719
	\[
Chengsong
parents: 557
diff changeset
  1720
		\map \; (r_2 \backslash_r \_) \; (\vsuf{xs @ [x]}{r_1})
Chengsong
parents: 557
diff changeset
  1721
	\]
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1722
	using lemma \ref{sfauIdemDer}.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1723
	This equality enables the inductive case to go through.
558
Chengsong
parents: 557
diff changeset
  1724
\end{proof}
Chengsong
parents: 557
diff changeset
  1725
\noindent 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1726
This lemma says that $(r_1\cdot r_2)\backslash s$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1727
can be flattened into a list whose head and tail meet the description
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1728
we gave earlier.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1729
%Note that this lemma does $\mathbf{not}$ depend on any
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1730
%specific definitions we used,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1731
%allowing people investigating derivatives to get an alternative
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1732
%view of what $r_1 \cdot r_2$ is.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  1733
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1734
We now use $\textit{createdBySequence}$ and
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1735
$\sflataux{\_}$ to describe an intuition
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1736
behind the closed form of the sequence closed form.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1737
If two regular expressions only differ in the way their
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1738
alternatives are nested, then we should be able to get the same result
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1739
once we apply simplification to both of them:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1740
\begin{lemma}\label{sflatRsimpeq}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1741
	If $r$ is created from a sequence through
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1742
	a series of derivatives 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1743
	(i.e. if $\textit{createdBySequence} \; r$ holds), 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1744
	and that $\sflataux{r} = rs$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1745
	then we have
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1746
	that 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1747
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1748
		$\textit{rsimp} \; r = \textit{rsimp} \; (\sum \; rs)$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1749
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1750
	holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1751
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1752
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1753
	By an induction on the inductive cases of $\textit{createdBySequence}$. 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1754
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1755
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1756
Now we are ready for the closed form 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1757
for the sequence regular expressions (without the inner applications
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1758
of simplifications):
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1759
\begin{lemma}\label{seqClosedFormGeneral}
558
Chengsong
parents: 557
diff changeset
  1760
	$\rsimp{\sflat{(r_1 \cdot r_2) \backslash s} }
Chengsong
parents: 557
diff changeset
  1761
	=\rsimp{(\sum (  (r_1 \backslash s) \cdot r_2 :: 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1762
	\map\; (r_2 \backslash \_) \; (\vsuf{s}{r_1})))}$
558
Chengsong
parents: 557
diff changeset
  1763
\end{lemma}
Chengsong
parents: 557
diff changeset
  1764
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1765
	We know that 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1766
	$\sflataux{(r_1 \cdot r_2) \backslash_r s} = (r_1 \backslash_r s) \cdot r_2 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1767
	:: (\map \; (r_2 \backslash_r \_) \; (\textit{Suffix} \; s \; r1))$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1768
	holds
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1769
	by lemma \ref{seqSfau0}.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1770
	This allows the theorem to go through because of lemma \ref{sflatRsimpeq}.
558
Chengsong
parents: 557
diff changeset
  1771
\end{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1772
Together with the idempotency property of $\rsimp{}$ (lemma \ref{rsimpIdem}),
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1773
it is possible to convert the above lemma to obtain the
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1774
proper closed form for $\backslash_{rsimps}$ rather than $\backslash_r$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1775
for  derivatives nested with simplification:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1776
\begin{theorem}\label{seqClosedForm}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1777
	$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{(\sum ((r_1 \backslash s) \cdot r_2 ) 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1778
	:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1})))}$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1779
\end{theorem}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1780
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1781
	By a case analysis of the string $s$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1782
	When $s$ is an empty list, the rewrite is straightforward.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1783
	When $s$ is a non-empty list, the
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1784
	lemmas \ref{seqClosedFormGeneral} and \ref{Simpders} apply,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1785
	making the proof go through.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1786
\end{proof}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  1787
\subsubsection{Closed Forms for Star Regular Expressions}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1788
The closed form for the star regular expression involves similar tricks
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1789
for the sequence regular expression.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1790
The $\textit{Suffix}$ function is now replaced by something
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1791
slightly more complex, because the growth pattern of star
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1792
regular expressions' derivatives is a bit different:
564
Chengsong
parents: 562
diff changeset
  1793
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1794
	\begin{tabular}{lclc}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1795
		$r^* $ & $\longrightarrow_{\backslash c}$ & 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1796
		$(r\backslash c)  \cdot  r^*$ & $\longrightarrow_{\backslash c'}$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1797
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1798
		$r \backslash cc'  \cdot r^* + r \backslash c' \cdot r^*$ &
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1799
		$\longrightarrow_{\backslash c''}$ & 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1800
		$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1801
		(r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*)$ & 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1802
		$\longrightarrow_{\backslash c'''}$ \\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1803
		\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1804
		$\ldots$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1805
	\end{tabular}
564
Chengsong
parents: 562
diff changeset
  1806
\end{center}
Chengsong
parents: 562
diff changeset
  1807
When we have a string $s = c :: c' :: c'' \ldots$  such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$, 
Chengsong
parents: 562
diff changeset
  1808
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1809
the number of terms in $r^* \backslash s$ will grow exponentially rather than linearly
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1810
in the sequence case.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1811
The good news is that the function $\textit{rsimp}$ will again
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1812
ignore the difference between differently nesting patterns of alternatives,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1813
and the exponentially growing star derivative like
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1814
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1815
	$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1816
	(r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1817
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1818
can be treated as
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1819
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1820
	$\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'', 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1821
	r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1822
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1823
which can be de-duplicated by $\rDistinct$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1824
and therefore bounded finitely.
564
Chengsong
parents: 562
diff changeset
  1825
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1826
and then de-duplicate terms of the form  ($s'$ being a substring of $s$).
564
Chengsong
parents: 562
diff changeset
  1827
This allows us to use a similar technique as $r_1 \cdot r_2$ case,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1828
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1829
Now the crux of this section is finding a suitable description
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1830
for $rs$ where
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1831
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1832
	$\rderssimp{r^*}{s} = \rsimp{\sum rs}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1833
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1834
holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1835
In addition, the list $rs$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1836
shall be in the form of 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1837
$\map \; (\lambda s'. r\backslash s' \cdot r^*) \; Ss$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1838
The $Ss$ is a list of strings, and for example in the sequence
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1839
closed form it is specified as $\textit{Suffix} \; s \; r_1$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1840
To get $Ss$ for the star regular expression,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1841
we need to introduce $\starupdate$ and $\starupdates$:
558
Chengsong
parents: 557
diff changeset
  1842
\begin{center}
Chengsong
parents: 557
diff changeset
  1843
	\begin{tabular}{lcl}
Chengsong
parents: 557
diff changeset
  1844
		$\starupdate \; c \; r \; [] $ & $\dn$ & $[]$\\
Chengsong
parents: 557
diff changeset
  1845
		$\starupdate \; c \; r \; (s :: Ss)$ & $\dn$ & \\
Chengsong
parents: 557
diff changeset
  1846
						     & & $\textit{if} \; 
Chengsong
parents: 557
diff changeset
  1847
						     (\rnullable \; (\rders \; r \; s))$ \\
Chengsong
parents: 557
diff changeset
  1848
						     & & $\textit{then} \;\; (s @ [c]) :: [c] :: (
Chengsong
parents: 557
diff changeset
  1849
						     \starupdate \; c \; r \; Ss)$ \\
Chengsong
parents: 557
diff changeset
  1850
						     & & $\textit{else} \;\; (s @ [c]) :: (
Chengsong
parents: 557
diff changeset
  1851
						     \starupdate \; c \; r \; Ss)$
Chengsong
parents: 557
diff changeset
  1852
	\end{tabular}
Chengsong
parents: 557
diff changeset
  1853
\end{center}
Chengsong
parents: 557
diff changeset
  1854
\begin{center}
Chengsong
parents: 557
diff changeset
  1855
	\begin{tabular}{lcl}
Chengsong
parents: 557
diff changeset
  1856
		$\starupdates \; [] \; r \; Ss$ & $=$ & $Ss$\\
Chengsong
parents: 557
diff changeset
  1857
		$\starupdates \; (c :: cs) \; r \; Ss$ &  $=$ &  $\starupdates \; cs \; r \; (
Chengsong
parents: 557
diff changeset
  1858
		\starupdate \; c \; r \; Ss)$
Chengsong
parents: 557
diff changeset
  1859
	\end{tabular}
Chengsong
parents: 557
diff changeset
  1860
\end{center}
Chengsong
parents: 557
diff changeset
  1861
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1862
Assuming we have that
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1863
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1864
	$\rderssimp{r^*}{s} = \rsimp{(\sum \map \; (\lambda s'. r\backslash s' \cdot r^*) \; Ss)}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1865
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1866
holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1867
The idea of $\starupdate$ and $\starupdates$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1868
is to update $Ss$ when another
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1869
derivative is taken on $\rderssimp{r^*}{s}$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1870
w.r.t a character $c$ and a string $s'$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1871
respectively.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1872
Both $\starupdate$ and $\starupdates$ take three arguments as input:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1873
the new character $c$ or string $s$ to take derivative with, 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1874
the regular expression
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1875
$r$ under the star $r^*$, and the
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1876
list of strings $Ss$ for the derivative $r^* \backslash s$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1877
up until this point  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1878
such that 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1879
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1880
$(r^*) \backslash s = \sum_{s' \in sSet} (r\backslash s') \cdot r^*$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1881
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1882
is satisfied.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1883
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1884
Functions $\starupdate$ and $\starupdates$ characterise what the 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1885
star derivatives will look like once ``straightened out'' into lists.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1886
The helper functions for such operations will be similar to
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1887
$\sflat{\_}$, $\sflataux{\_}$ and $\sflataux{\_}$, which we defined for sequence.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1888
We use similar symbols to denote them, with a $*$ subscript to mark the difference.
558
Chengsong
parents: 557
diff changeset
  1889
\begin{center}
Chengsong
parents: 557
diff changeset
  1890
	\begin{tabular}{lcl}
Chengsong
parents: 557
diff changeset
  1891
		$\hflataux{r_1 + r_2}$ & $\dn$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
Chengsong
parents: 557
diff changeset
  1892
		$\hflataux{r}$ & $\dn$ & $[r]$
Chengsong
parents: 557
diff changeset
  1893
	\end{tabular}
Chengsong
parents: 557
diff changeset
  1894
\end{center}
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1895
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  1896
\begin{center}
558
Chengsong
parents: 557
diff changeset
  1897
	\begin{tabular}{lcl}
Chengsong
parents: 557
diff changeset
  1898
		$\hflat{r_1 + r_2}$ & $\dn$ & $\sum (\hflataux {r_1} @ \hflataux {r_2}) $\\
Chengsong
parents: 557
diff changeset
  1899
		$\hflat{r}$ & $\dn$ & $r$
Chengsong
parents: 557
diff changeset
  1900
	\end{tabular}
Chengsong
parents: 557
diff changeset
  1901
\end{center}
Chengsong
parents: 557
diff changeset
  1902
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1903
These definitions are tailor-made for dealing with alternatives that have
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1904
originated from a star's derivatives.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1905
A typical star derivative always has the structure of a balanced binary tree:
564
Chengsong
parents: 562
diff changeset
  1906
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1907
	$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1908
	(r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $ 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1909
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1910
All of the nested structures of alternatives
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1911
generated from derivatives are binary, and therefore
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1912
$\hflat{\_}$ and $\hflataux{\_}$ only deal with binary alternatives.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1913
$\hflat{\_}$ ``untangles'' like the following:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1914
\[
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1915
	\sum ((r_1 + r_2) + (r_3 + r_4))  + \ldots \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1916
	\stackrel{\hflat{\_}}{\longrightarrow} \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1917
	\RALTS{[r_1, r_2, \ldots, r_n]} 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1918
\]
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1919
Here is a lemma stating the recursive property of $\starupdate$ and $\starupdates$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1920
with the helpers $\hflat{\_}$ and $\hflataux{\_}$\footnote{The function $\textit{concat}$ takes a list of lists 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1921
			and merges each of the element lists to form a flattened list.}:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1922
\begin{lemma}\label{stupdateInduct1}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1923
	\mbox
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1924
	For a list of strings $Ss$, the following hold.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1925
	\begin{itemize}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1926
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1927
			If we do a derivative on the terms 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1928
			$r\backslash_r s \cdot r^*$ (where $s$ is taken from the list $Ss$),
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1929
			the result will be the same as if we apply $\starupdate$ to $Ss$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1930
			\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1931
				\begin{tabular}{c}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1932
			$\textit{concat} \; (\map \; (\hflataux{\_} \circ ( (\_\backslash_r x)
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1933
			\circ (\lambda s.\;\; (r \backslash_r s) \cdot r^*)))\; Ss )\;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1934
			$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1935
			\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1936
			$=$ \\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1937
			\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1938
			$\map \; (\lambda s. (r \backslash_r s) \cdot (r^*)) \; 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1939
			(\starupdate \; x \; r \; Ss)$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1940
				\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1941
			\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1942
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1943
			$\starupdates$ is ``composable'' w.r.t a derivative.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1944
			It piggybacks the character $x$ to the tail of the string
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1945
			$xs$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1946
			\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1947
				\begin{tabular}{c}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1948
					$\textit{concat} \; (\map \; \hflataux{\_} \; 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1949
					(\map \; (\_\backslash_r x) \; 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1950
					(\map \; (\lambda s.\;\; (r \backslash_r s) \cdot 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1951
					(r^*) ) \; (\starupdates \; xs \; r \; Ss))))$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1952
					\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1953
					$=$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1954
					\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1955
					$\map \; (\lambda s.\;\; (r\backslash_r s) \cdot (r^*)) \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1956
					(\starupdates \; (xs @ [x]) \; r \; Ss)$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1957
				\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1958
			\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1959
	\end{itemize}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1960
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1961
			
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1962
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1963
	Part 1 is by induction on $Ss$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1964
	Part 2 is by induction on $xs$, where $Ss$ is left to take arbitrary values.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1965
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1966
			
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1967
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1968
Like $\textit{createdBySequence}$, we need
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1969
a predicate for ``star-created'' regular expressions:
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1970
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1971
	\begin{mathpar}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1972
		\inferrule{\mbox{}}{ \textit{createdByStar}\; \RSEQ{ra}{\RSTAR{rb}} }
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1973
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1974
		\inferrule{  \textit{createdByStar} \; r_1\; \land  \; \textit{createdByStar} \; r_2 }{\textit{createdByStar} \; (r_1 + r_2) } 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1975
	\end{mathpar}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  1976
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1977
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1978
All regular expressions created by taking derivatives of
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1979
$r_1 \cdot (r_2)^*$ satisfy the $\textit{createdByStar}$ predicate:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1980
\begin{lemma}\label{starDersCbs}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1981
	$\textit{createdByStar} \; ((r_1 \cdot r_2^*) \backslash_r s) $ holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1982
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1983
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1984
	By a reverse induction on $s$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1985
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1986
If a regular expression conforms to the shape of a star's derivative,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1987
then we can push an application of $\hflataux{\_}$ inside a derivative of it:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1988
\begin{lemma}\label{hfauPushin}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1989
	If $\textit{createdByStar} \; r$ holds, then
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1990
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1991
		$\hflataux{r \backslash_r c} = \textit{concat} \; (
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1992
		\map \; \hflataux{\_} (\map \; (\_\backslash_r c) \;(\hflataux{r})))$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1993
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1994
	holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1995
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1996
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1997
	By an induction on the inductivev cases of $\textit{createdByStar}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1998
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  1999
%This is not entirely true for annotated regular expressions: 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2000
%%TODO: bsimp bders \neq bderssimp
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2001
%\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2002
%	$(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2003
%\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2004
%For bit-codes, the order in which simplification is applied
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2005
%might cause a difference in the location they are placed.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2006
%If we want something like
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2007
%\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2008
%	$\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2009
%\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2010
%Some "canonicalization" procedure is required,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2011
%which either pushes all the common bitcodes to nodes
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2012
%as senior as possible:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2013
%\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2014
%	$_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2015
%\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2016
%or does the reverse. However bitcodes are not of interest if we are talking about
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2017
%the $\llbracket r \rrbracket$ size of a regex.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2018
%Therefore for the ease and simplicity of producing a
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2019
%proof for a size bound, we are happy to restrict ourselves to 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2020
%unannotated regular expressions, and obtain such equalities as
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2021
%TODO: rsimp sflat
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2022
% The simplification of a flattened out regular expression, provided it comes
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2023
%from the derivative of a star, is the same as the one nested.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2024
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2025
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2026
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2027
Now we introduce an inductive property
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2028
for $\starupdate$ and $\hflataux{\_}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2029
\begin{lemma}\label{starHfauInduct}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2030
	If we do derivatives of $r^*$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2031
	with a string that starts with $c$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2032
	then flatten it out,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2033
	we obtain a list
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2034
	of the shape $\sum_{s' \in sS} (r\backslash_r s') \cdot r^*$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2035
	where $sS = \starupdates \; s \; r \; [[c]]$. Namely,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2036
	\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2037
		$\hflataux{(\rders{( (\rder{c}{r_0})\cdot(r_0^*))}{s})} = 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2038
		\map \; (\lambda s_1. (r_0 \backslash_r s_1) \cdot (r_0^*)) \; 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2039
		(\starupdates \; s \; r_0 \; [[c]])$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2040
	\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2041
holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2042
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2043
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2044
	By an induction on $s$, the inductive cases
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2045
	being $[]$ and $s@[c]$. The lemmas \ref{hfauPushin} and \ref{starDersCbs} are used.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2046
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2047
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2048
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2049
$\hflataux{\_}$ has a similar effect as $\textit{flatten}$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2050
\begin{lemma}\label{hflatauxGrewrites}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2051
	$a :: rs \grewrites \hflataux{a} @ rs$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2052
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2053
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2054
	By induction on $a$. $rs$ is set to take arbitrary values.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2055
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2056
It is also not surprising that $\textit{rsimp}$ will cover
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2057
the effect of $\hflataux{\_}$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2058
\begin{lemma}\label{cbsHfauRsimpeq1}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2059
	$\rsimp{(r_1 + r_2)} = \rsimp{(\RALTS{\hflataux{r_1} @ \hflataux{r_2}})}$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2060
\end{lemma}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2061
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2062
\begin{proof}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2063
	By using the rewriting relation $\rightsquigarrow$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2064
\end{proof}
564
Chengsong
parents: 562
diff changeset
  2065
And from this we obtain a proof that a star's derivative will be the same
Chengsong
parents: 562
diff changeset
  2066
as if it had all its nested alternatives created during deriving being flattened out:
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2067
For example,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2068
\begin{lemma}\label{hfauRsimpeq2}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2069
	$\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2070
\end{lemma}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2071
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2072
	By structural induction on $r$, where the induction rules 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2073
	are these of $\createdByStar{\_}$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2074
	Lemma \ref{cbsHfauRsimpeq1} is used in the inductive case.
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2075
\end{proof}
564
Chengsong
parents: 562
diff changeset
  2076
Chengsong
parents: 562
diff changeset
  2077
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2078
%Here is a corollary that states the lemma in
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2079
%a more intuitive way:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2080
%\begin{corollary}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2081
%	$\hflataux{r^* \backslash_r (c::xs)} = \map \; (\lambda s. (r \backslash_r s) \cdot
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2082
%	(r^*))\; (\starupdates \; c\; r\; [[c]])$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2083
%\end{corollary}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2084
%\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2085
%Note that this is also agnostic of the simplification
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2086
%function we defined, and is therefore of more general interest.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2087
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2088
Together with the rewriting relation
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2089
\begin{lemma}\label{starClosedForm6Hrewrites}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2090
	We have the following set of rewriting relations or equalities:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2091
	\begin{itemize}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2092
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2093
			$\textit{rsimp} \; (r^* \backslash_r (c::s)) 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2094
			\sequal
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2095
			\sum \; ( ( \sum (\lambda s. (r\backslash_r s) \cdot r^*) \; (
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2096
			\starupdates \; s \; r \; [ c::[]] ) ) )$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2097
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2098
			$r \backslash_{rsimp} (c::s) = \textit{rsimp} \; ( (
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2099
			\sum ( (\map \; (\lambda s_1. (r\backslash s_1) \; r^*) \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2100
			(\starupdates \;s \; r \; [ c::[] ])))))$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2101
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2102
			$\sum ( (\map \; (\lambda s. (r\backslash s) \; r^*) \; Ss))
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2103
			\sequal
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2104
			 \sum ( (\map \; (\lambda s. \textit{rsimp} \; (r\backslash s) \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2105
			 r^*) \; Ss) )$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2106
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2107
			$\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2108
			\scfrewrites
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2109
			\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2110
		\item
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2111
			$( ( \sum ( ( \map \ (\lambda s. \;\;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2112
			(\textit{rsimp} \; (r \backslash_r s)) \cdot r^*) \; (\starupdates \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2113
			s \; r \; [ c::[] ])))))$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2114
			$\sequal$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2115
			$( ( \sum ( ( \map \ (\lambda s. \;\;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2116
			( r \backslash_{rsimp} s)) \cdot r^*) \; (\starupdates \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2117
			s \; r \; [ c::[] ]))))$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2118
	\end{itemize}
558
Chengsong
parents: 557
diff changeset
  2119
\end{lemma}
Chengsong
parents: 557
diff changeset
  2120
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2121
	Part 1 leads to part 2.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2122
	The rest of them are routine.
558
Chengsong
parents: 557
diff changeset
  2123
\end{proof}
Chengsong
parents: 557
diff changeset
  2124
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2125
Next the closed form for star regular expressions can be derived:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2126
\begin{theorem}\label{starClosedForm}
558
Chengsong
parents: 557
diff changeset
  2127
	$\rderssimp{r^*}{c::s} = 
Chengsong
parents: 557
diff changeset
  2128
	\rsimp{
Chengsong
parents: 557
diff changeset
  2129
		(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \; 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2130
		(\starupdates \; s\; r \; [[c]])
558
Chengsong
parents: 557
diff changeset
  2131
		)
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2132
		)
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2133
	}
558
Chengsong
parents: 557
diff changeset
  2134
	$
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2135
\end{theorem}
558
Chengsong
parents: 557
diff changeset
  2136
\begin{proof}
Chengsong
parents: 557
diff changeset
  2137
	By an induction on $s$.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2138
	The lemmas \ref{rsimpIdem}, \ref{starHfauInduct}, \ref{starClosedForm6Hrewrites} 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2139
	and \ref{hfauRsimpeq2}
558
Chengsong
parents: 557
diff changeset
  2140
	are used.	
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2141
	In \ref{starClosedForm6Hrewrites}, the equalities are
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2142
	used to link the LHS and RHS.
558
Chengsong
parents: 557
diff changeset
  2143
\end{proof}
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2144
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2145
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2146
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2147
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2148
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2149
613
Chengsong
parents: 611
diff changeset
  2150
%----------------------------------------------------------------------------------------
Chengsong
parents: 611
diff changeset
  2151
%	SECTION ??
Chengsong
parents: 611
diff changeset
  2152
%----------------------------------------------------------------------------------------
Chengsong
parents: 611
diff changeset
  2153
Chengsong
parents: 611
diff changeset
  2154
%-----------------------------------
Chengsong
parents: 611
diff changeset
  2155
%	SECTION syntactic equivalence under simp
Chengsong
parents: 611
diff changeset
  2156
%-----------------------------------
Chengsong
parents: 611
diff changeset
  2157
Chengsong
parents: 611
diff changeset
  2158
Chengsong
parents: 611
diff changeset
  2159
%----------------------------------------------------------------------------------------
Chengsong
parents: 611
diff changeset
  2160
%	SECTION ALTS CLOSED FORM
Chengsong
parents: 611
diff changeset
  2161
%----------------------------------------------------------------------------------------
Chengsong
parents: 611
diff changeset
  2162
%\section{A Closed Form for \textit{ALTS}}
Chengsong
parents: 611
diff changeset
  2163
%Now we prove that  $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
Chengsong
parents: 611
diff changeset
  2164
%
Chengsong
parents: 611
diff changeset
  2165
%
Chengsong
parents: 611
diff changeset
  2166
%There are a few key steps, one of these steps is
Chengsong
parents: 611
diff changeset
  2167
%
Chengsong
parents: 611
diff changeset
  2168
%
Chengsong
parents: 611
diff changeset
  2169
%
Chengsong
parents: 611
diff changeset
  2170
%One might want to prove this by something a simple statement like: 
Chengsong
parents: 611
diff changeset
  2171
%
Chengsong
parents: 611
diff changeset
  2172
%For this to hold we want the $\textit{distinct}$ function to pick up
Chengsong
parents: 611
diff changeset
  2173
%the elements before and after derivatives correctly:
Chengsong
parents: 611
diff changeset
  2174
%$r \in rset \equiv (rder x r) \in (rder x rset)$.
Chengsong
parents: 611
diff changeset
  2175
%which essentially requires that the function $\backslash$ is an injective mapping.
Chengsong
parents: 611
diff changeset
  2176
%
Chengsong
parents: 611
diff changeset
  2177
%Unfortunately the function $\backslash c$ is not an injective mapping.
Chengsong
parents: 611
diff changeset
  2178
%
Chengsong
parents: 611
diff changeset
  2179
%\subsection{function $\backslash c$ is not injective (1-to-1)}
Chengsong
parents: 611
diff changeset
  2180
%\begin{center}
Chengsong
parents: 611
diff changeset
  2181
%	The derivative $w.r.t$ character $c$ is not one-to-one.
Chengsong
parents: 611
diff changeset
  2182
%	Formally,
Chengsong
parents: 611
diff changeset
  2183
%	$\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
Chengsong
parents: 611
diff changeset
  2184
%\end{center}
Chengsong
parents: 611
diff changeset
  2185
%This property is trivially true for the
Chengsong
parents: 611
diff changeset
  2186
%character regex example:
Chengsong
parents: 611
diff changeset
  2187
%\begin{center}
Chengsong
parents: 611
diff changeset
  2188
%	$r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
Chengsong
parents: 611
diff changeset
  2189
%\end{center}
Chengsong
parents: 611
diff changeset
  2190
%But apart from the cases where the derivative
Chengsong
parents: 611
diff changeset
  2191
%output is $\ZERO$, are there non-trivial results
Chengsong
parents: 611
diff changeset
  2192
%of derivatives which contain strings?
Chengsong
parents: 611
diff changeset
  2193
%The answer is yes.
Chengsong
parents: 611
diff changeset
  2194
%For example,
Chengsong
parents: 611
diff changeset
  2195
%\begin{center}
Chengsong
parents: 611
diff changeset
  2196
%	Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
Chengsong
parents: 611
diff changeset
  2197
%	where $a$ is not nullable.\\
Chengsong
parents: 611
diff changeset
  2198
%	$r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
Chengsong
parents: 611
diff changeset
  2199
%	$r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
Chengsong
parents: 611
diff changeset
  2200
%\end{center}
Chengsong
parents: 611
diff changeset
  2201
%We start with two syntactically different regular expressions,
Chengsong
parents: 611
diff changeset
  2202
%and end up with the same derivative result.
Chengsong
parents: 611
diff changeset
  2203
%This is not surprising as we have such 
Chengsong
parents: 611
diff changeset
  2204
%equality as below in the style of Arden's lemma:\\
Chengsong
parents: 611
diff changeset
  2205
%\begin{center}
Chengsong
parents: 611
diff changeset
  2206
%	$L(A^*B) = L(A\cdot A^* \cdot B + B)$
Chengsong
parents: 611
diff changeset
  2207
%\end{center}
Chengsong
parents: 611
diff changeset
  2208
\section{Bounding Closed Forms}
Chengsong
parents: 611
diff changeset
  2209
Chengsong
parents: 611
diff changeset
  2210
In this section, we introduce how we formalised the bound
Chengsong
parents: 611
diff changeset
  2211
on closed forms.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2212
We first show that in general regular expressions up to a certain 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2213
size are finite.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2214
Then we prove that functions such as $\rflts$
613
Chengsong
parents: 611
diff changeset
  2215
will not cause the size of r-regular expressions to grow.
Chengsong
parents: 611
diff changeset
  2216
Putting this together with a general bound 
Chengsong
parents: 611
diff changeset
  2217
on the finiteness of distinct regular expressions
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2218
up to a certain size, we obtain a bound on 
613
Chengsong
parents: 611
diff changeset
  2219
the closed forms.
Chengsong
parents: 611
diff changeset
  2220
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2221
\subsection{Finiteness of Distinct Regular Expressions}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2222
We define the set of regular expressions whose size are no more than
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2223
a certain size $N$ as $\textit{sizeNregex} \; N$:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2224
\[
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2225
	\textit{sizeNregex} \; N \dn \{r\; \mid \;  \llbracket r \rrbracket_r \leq N \}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2226
\]
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2227
We have that $\textit{sizeNregex} \; N$ is always a finite set:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2228
\begin{lemma}\label{finiteSizeN}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2229
	$\textit{finite} \; (\textit{sizeNregex} \; N)$ holds.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2230
\end{lemma}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2231
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2232
	By splitting the set $\textit{sizeNregex} \; (N + 1)$ into
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2233
	subsets by their categories:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2234
	$\{\ZERO, \ONE, c\}$, $\{* `` \textit{sizeNregex} \; N\}$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2235
	and so on. Each of these subsets are finitely bounded.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2236
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2237
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2238
From this we get a corollary that
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2239
if forall $r \in rs$, $\rsize{r} \leq N$, then the output of 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2240
$\rdistinct{rs}{\varnothing}$ is a list of regular
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2241
expressions of finite size depending on $N$ only. 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2242
\begin{corollary}\label{finiteSizeNCorollary}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2243
	$\rsize{\rdistinct{rs}{\varnothing}} \leq c_N * N$ holds,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2244
	where the constant $c_N$ is equal to $\textit{card} \; (\textit{sizeNregex} \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2245
	N)$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2246
\end{corollary}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2247
\begin{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2248
	For all $r$ in 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2249
	$\textit{set} \; (\rdistinct{rs}{\varnothing})$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2250
	it is always the case that $\rsize{r} \leq N$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2251
	In addition, the list length is bounded by
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2252
	$c_N$, yielding the desired bound.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2253
\end{proof}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2254
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2255
This fact will be handy in estimating the closed form sizes.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2256
%We have proven that the size of the
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2257
%output of $\textit{rdistinct} \; rs' \; \varnothing$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2258
%is bounded by a constant $N * c_N$ depending only on $N$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2259
%provided that each of $rs'$'s element
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2260
%is bounded by $N$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2261
613
Chengsong
parents: 611
diff changeset
  2262
\subsection{$\textit{rsimp}$ Does Not Increment the Size}
Chengsong
parents: 611
diff changeset
  2263
Although it seems evident, we need a series
Chengsong
parents: 611
diff changeset
  2264
of non-trivial lemmas to establish that functions such as $\rflts$
Chengsong
parents: 611
diff changeset
  2265
do not cause the regular expressions to grow.
Chengsong
parents: 611
diff changeset
  2266
\begin{lemma}\label{rsimpMonoLemmas}
Chengsong
parents: 611
diff changeset
  2267
	\mbox{}
Chengsong
parents: 611
diff changeset
  2268
	\begin{itemize}
Chengsong
parents: 611
diff changeset
  2269
		\item
Chengsong
parents: 611
diff changeset
  2270
			\[
Chengsong
parents: 611
diff changeset
  2271
				\llbracket \rsimpalts \; rs \rrbracket_r \leq
Chengsong
parents: 611
diff changeset
  2272
				\llbracket \sum \; rs \rrbracket_r
Chengsong
parents: 611
diff changeset
  2273
			\]
Chengsong
parents: 611
diff changeset
  2274
		\item
Chengsong
parents: 611
diff changeset
  2275
			\[
Chengsong
parents: 611
diff changeset
  2276
				\llbracket \rsimpseq \; r_1 \;  r_2 \rrbracket_r \leq
Chengsong
parents: 611
diff changeset
  2277
				\llbracket r_1 \cdot r_2 \rrbracket_r
Chengsong
parents: 611
diff changeset
  2278
			\]
Chengsong
parents: 611
diff changeset
  2279
		\item
Chengsong
parents: 611
diff changeset
  2280
			\[
Chengsong
parents: 611
diff changeset
  2281
				\llbracket \rflts \; rs \rrbracket_r  \leq
Chengsong
parents: 611
diff changeset
  2282
				\llbracket rs \rrbracket_r 
Chengsong
parents: 611
diff changeset
  2283
			\]
Chengsong
parents: 611
diff changeset
  2284
		\item
Chengsong
parents: 611
diff changeset
  2285
			\[
Chengsong
parents: 611
diff changeset
  2286
				\llbracket \rDistinct \; rs \; ss \rrbracket_r  \leq
Chengsong
parents: 611
diff changeset
  2287
				\llbracket rs \rrbracket_r 
Chengsong
parents: 611
diff changeset
  2288
			\]
Chengsong
parents: 611
diff changeset
  2289
		\item
Chengsong
parents: 611
diff changeset
  2290
			If all elements $a$ in the set $as$ satisfy the property
Chengsong
parents: 611
diff changeset
  2291
			that $\llbracket \textit{rsimp} \; a \rrbracket_r \leq
Chengsong
parents: 611
diff changeset
  2292
			\llbracket a \rrbracket_r$, then we have 
Chengsong
parents: 611
diff changeset
  2293
			\[
Chengsong
parents: 611
diff changeset
  2294
				\llbracket \; \rsimpalts \; (\textit{rdistinct} \;
Chengsong
parents: 611
diff changeset
  2295
				(\textit{rflts} \; (\textit{map}\;\textit{rsimp} as)) \{\})
Chengsong
parents: 611
diff changeset
  2296
				\rrbracket \leq
Chengsong
parents: 611
diff changeset
  2297
				\llbracket \; \sum \; (\rDistinct \; (\rflts \;(\map \;
Chengsong
parents: 611
diff changeset
  2298
				\textit{rsimp} \; x))\; \{ \} ) \rrbracket_r 
Chengsong
parents: 611
diff changeset
  2299
			\]
Chengsong
parents: 611
diff changeset
  2300
	\end{itemize}
Chengsong
parents: 611
diff changeset
  2301
\end{lemma}
Chengsong
parents: 611
diff changeset
  2302
\begin{proof}
Chengsong
parents: 611
diff changeset
  2303
	Point 1, 3, 4 can be proven by an induction on $rs$.
Chengsong
parents: 611
diff changeset
  2304
	Point 2 is by case analysis on $r_1$ and $r_2$.
Chengsong
parents: 611
diff changeset
  2305
	The last part is a corollary of the previous ones.
Chengsong
parents: 611
diff changeset
  2306
\end{proof}
Chengsong
parents: 611
diff changeset
  2307
\noindent
Chengsong
parents: 611
diff changeset
  2308
With the lemmas for each inductive case in place, we are ready to get 
Chengsong
parents: 611
diff changeset
  2309
the non-increasing property as a corollary:
Chengsong
parents: 611
diff changeset
  2310
\begin{corollary}\label{rsimpMono}
Chengsong
parents: 611
diff changeset
  2311
	$\llbracket \textit{rsimp} \; r \rrbracket_r \leq \llbracket r \rrbracket_r$
Chengsong
parents: 611
diff changeset
  2312
\end{corollary}
Chengsong
parents: 611
diff changeset
  2313
\begin{proof}
Chengsong
parents: 611
diff changeset
  2314
	By \ref{rsimpMonoLemmas}.
Chengsong
parents: 611
diff changeset
  2315
\end{proof}
Chengsong
parents: 611
diff changeset
  2316
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2317
\subsection{Estimating the Closed Forms' sizes}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2318
We recap the closed forms we obtained
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2319
earlier by putting them together down below:
558
Chengsong
parents: 557
diff changeset
  2320
\begin{itemize}
Chengsong
parents: 557
diff changeset
  2321
	\item
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2322
		$\rderssimp{(\sum rs)}{s} \sequal
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2323
		\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
558
Chengsong
parents: 557
diff changeset
  2324
	\item
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2325
		$\rderssimp{(r_1 \cdot r_2)}{s} \sequal \sum ((r_1 \backslash s) \cdot r_2 ) 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2326
		:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1}))$
558
Chengsong
parents: 557
diff changeset
  2327
	\item
Chengsong
parents: 557
diff changeset
  2328
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2329
		$\rderssimp{r^*}{c::s} = 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2330
		\rsimp{
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2331
			(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \; 
558
Chengsong
parents: 557
diff changeset
  2332
			(\starupdates \; s\; r \; [[c]])
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2333
			)
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2334
			)
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2335
		}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2336
		$
558
Chengsong
parents: 557
diff changeset
  2337
\end{itemize}	
Chengsong
parents: 557
diff changeset
  2338
\noindent	
Chengsong
parents: 557
diff changeset
  2339
The closed forms on the left-hand-side
Chengsong
parents: 557
diff changeset
  2340
are all of the same shape: $\rsimp{ (\sum rs)} $.
Chengsong
parents: 557
diff changeset
  2341
Such regular expression will be bounded by the size of $\sum rs'$, 
Chengsong
parents: 557
diff changeset
  2342
where every element in $rs'$ is distinct, and each element 
Chengsong
parents: 557
diff changeset
  2343
can be described by some inductive sub-structures 
Chengsong
parents: 557
diff changeset
  2344
(for example when $r = r_1 \cdot r_2$ then $rs'$ 
Chengsong
parents: 557
diff changeset
  2345
will be solely comprised of $r_1 \backslash s'$ 
Chengsong
parents: 557
diff changeset
  2346
and $r_2 \backslash s''$, $s'$ and $s''$ being 
Chengsong
parents: 557
diff changeset
  2347
sub-strings of $s$).
Chengsong
parents: 557
diff changeset
  2348
which will each have a size uppder bound 
Chengsong
parents: 557
diff changeset
  2349
according to inductive hypothesis, which controls $r \backslash s$.
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  2350
558
Chengsong
parents: 557
diff changeset
  2351
We elaborate the above reasoning by a series of lemmas
Chengsong
parents: 557
diff changeset
  2352
below, where straightforward proofs are omitted.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2353
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2354
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2355
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2356
558
Chengsong
parents: 557
diff changeset
  2357
We want to apply it to our setting $\rsize{\rsimp{\sum rs}}$.
Chengsong
parents: 557
diff changeset
  2358
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2359
We show that $\rdistinct$ and $\rflts$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2360
working together is at least as 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2361
good as $\rdistinct{}{}$ alone, which can be written as
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2362
\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2363
	$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2364
	\leq 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2365
	\llbracket \rdistinct{rs}{\varnothing}  \rrbracket_r  $.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2366
\end{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2367
We need this so that we know the outcome of our real 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2368
simplification is better than or equal to a rough estimate,
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2369
and therefore can be bounded by that estimate.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2370
This is a bit harder to establish compared with proving
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2371
$\textit{flts}$ does not make a list larger (which can
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2372
be proven using routine induction):
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2373
\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2374
	$\llbracket  \textit{rflts}\; rs \rrbracket_r \leq
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2375
	\llbracket  \textit{rs} \rrbracket_r$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2376
\end{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2377
We cannot simply prove how each helper function
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2378
reduces the size and then put them together:
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2379
From
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2380
\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2381
$\llbracket  \textit{rflts}\; rs \rrbracket_r \leq
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2382
	\llbracket  \textit{rs} \rrbracket_r$
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2383
\end{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2384
and
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2385
\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2386
     $\llbracket  \textit{rdistinct} \; rs \; \varnothing \leq
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2387
     \llbracket rs \rrbracket_r$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2388
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2389
one cannot infer 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2390
\begin{center}
558
Chengsong
parents: 557
diff changeset
  2391
	$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r 
Chengsong
parents: 557
diff changeset
  2392
	\leq 
Chengsong
parents: 557
diff changeset
  2393
	\llbracket \rdistinct{rs}{\varnothing}  \rrbracket_r  $.
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2394
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2395
What we can infer is that 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2396
\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2397
	$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2398
	\leq
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2399
	\llbracket rs \rrbracket_r$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2400
\end{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2401
but this estimate is too rough and $\llbracket rs \rrbracket_r$	is unbounded.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2402
The way we 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2403
get around this is by first proving a more general lemma 
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2404
(so that the inductive case goes through):
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2405
\begin{lemma}\label{fltsSizeReductionAlts}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2406
	If we have three accumulator sets:
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2407
	$noalts\_set$, $alts\_set$ and $corr\_set$,
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2408
	satisfying:
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2409
	\begin{itemize}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2410
		\item
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2411
			$\forall r \in noalts\_set. \; \nexists xs.\; r = \sum  xs$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2412
		\item
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2413
			$\forall r \in alts\_set. \; \exists xs. \; r = \sum xs
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2414
			\; \textit{and} \; set \; xs \subseteq corr\_set$
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2415
	\end{itemize}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2416
	then we have that
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2417
	\begin{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2418
	\begin{tabular}{lcl}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2419
	$\llbracket  (\textit{rdistinct} \; (\textit{rflts} \; as) \;
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2420
	(noalts\_set \cup corr\_set)) \rrbracket_r$ & $\leq$ &\\
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2421
						    $\llbracket  (\textit{rdistinct} \; as \; (noalts\_set \cup alts\_set \cup
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2422
	\{ \ZERO \} )) \rrbracket_r$ & & \\ 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2423
	\end{tabular}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2424
	\end{center}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2425
		holds.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2426
\end{lemma}
558
Chengsong
parents: 557
diff changeset
  2427
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2428
We split the accumulator into two parts: the part
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2429
which contains alternative regular expressions ($alts\_set$), and 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2430
the part without any of them($noalts\_set$).
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2431
This is because $\rflts$ opens up the alternatives in $as$,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2432
causing the accumulators on both sides of the inequality
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2433
to diverge slightly.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2434
If we want to compare the accumulators that are not
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2435
perfectly in sync, we need to consider the alternatives and non-alternatives
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2436
separately.
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2437
The set $corr\_set$ is the corresponding set
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2438
of $alts\_set$ with all elements under the alternative constructor
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2439
spilled out.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2440
\begin{proof}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2441
	By induction on the list $as$. We make use of lemma \ref{rdistinctConcat}.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2442
\end{proof}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2443
By setting all three sets to the empty set, one gets the desired size estimate:
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2444
\begin{corollary}\label{interactionFltsDB}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2445
	$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2446
	\leq 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2447
	\llbracket \rdistinct{rs}{\varnothing}  \rrbracket_r  $.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2448
\end{corollary}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2449
\begin{proof}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2450
	By using the lemma \ref{fltsSizeReductionAlts}.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2451
\end{proof}
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2452
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2453
The intuition for why this is true
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2454
is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of 
558
Chengsong
parents: 557
diff changeset
  2455
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$. 
Chengsong
parents: 557
diff changeset
  2456
Chengsong
parents: 557
diff changeset
  2457
Now this $\rsimp{\sum rs}$ can be estimated using $\rdistinct{rs}{\varnothing}$:
Chengsong
parents: 557
diff changeset
  2458
\begin{lemma}\label{altsSimpControl}
Chengsong
parents: 557
diff changeset
  2459
	$\rsize{\rsimp{\sum rs}} \leq \rsize{\rdistinct{rs}{\varnothing}}+ 1$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2460
\end{lemma}
558
Chengsong
parents: 557
diff changeset
  2461
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2462
	By using corollary \ref{interactionFltsDB}.
558
Chengsong
parents: 557
diff changeset
  2463
\end{proof}
Chengsong
parents: 557
diff changeset
  2464
\noindent
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2465
This is a key lemma in establishing the bounds on all the 
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2466
closed forms.
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2467
With this we are now ready to control the sizes of
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2468
$(r_1 \cdot r_2 )\backslash s$ and $r^* \backslash s$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2469
\begin{theorem}\label{rBound}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2470
	For any regex $r$, $\exists N_r. \forall s. \; \rsize{\rderssimp{r}{s}} \leq N_r$
558
Chengsong
parents: 557
diff changeset
  2471
\end{theorem}
Chengsong
parents: 557
diff changeset
  2472
\noindent
Chengsong
parents: 557
diff changeset
  2473
\begin{proof}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2474
	We prove this by induction on $r$. The base cases for $\RZERO$,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2475
	$\RONE $ and $\RCHAR{c}$ are straightforward. 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2476
	In the sequence $r_1 \cdot r_2$ case,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2477
	the inductive hypotheses state 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2478
	$\exists N_1. \forall s. \; \llbracket \rderssimp{r}{s} \rrbracket \leq N_1$ and
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2479
	$\exists N_2. \forall s. \; \llbracket \rderssimp{r_2}{s} \rrbracket \leq N_2$. 
562
Chengsong
parents: 561
diff changeset
  2480
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2481
	When the string $s$ is not empty, we can reason as follows
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2482
	%
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2483
	\begin{center}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2484
		\begin{tabular}{lcll}
558
Chengsong
parents: 557
diff changeset
  2485
& & $ \llbracket   \rderssimp{r_1\cdot r_2 }{s} \rrbracket_r $\\
561
486fb297ac7c more done
Chengsong
parents: 559
diff changeset
  2486
& $ = $ & $\llbracket \rsimp{(\sum(r_1 \backslash_{rsimp} s \cdot r_2 \; \;  :: \; \; 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2487
		\map \; (r_2\backslash_{rsimp} \_)\; (\vsuf{s}{r})))} \rrbracket_r $ & (1) \\			
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2488
										     & $\leq$ & $\llbracket \rdistinct{(r_1 \backslash_{rsimp} s \cdot r_2 \; \;  :: \; \; 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2489
	\map \; (r_2\backslash_{rsimp} \_)\; (\vsuf{s}{r}))}{\varnothing} \rrbracket_r  + 1$ & (2) \\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2490
											     & $\leq$ & $2 + N_1 + \rsize{r_2} + (N_2 * (card\;(\sizeNregex \; N_2)))$ & (3)\\
558
Chengsong
parents: 557
diff changeset
  2491
\end{tabular}
Chengsong
parents: 557
diff changeset
  2492
\end{center}
561
486fb297ac7c more done
Chengsong
parents: 559
diff changeset
  2493
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2494
(1) is by theorem \ref{seqClosedForm}.
561
486fb297ac7c more done
Chengsong
parents: 559
diff changeset
  2495
(2) is by \ref{altsSimpControl}.
486fb297ac7c more done
Chengsong
parents: 559
diff changeset
  2496
(3) is by \ref{finiteSizeNCorollary}.
562
Chengsong
parents: 561
diff changeset
  2497
Chengsong
parents: 561
diff changeset
  2498
Chengsong
parents: 561
diff changeset
  2499
Combining the cases when $s = []$ and $s \neq []$, we get (4):
Chengsong
parents: 561
diff changeset
  2500
\begin{center}
Chengsong
parents: 561
diff changeset
  2501
	\begin{tabular}{lcll}
Chengsong
parents: 561
diff changeset
  2502
		$\rsize{(r_1 \cdot r_2) \backslash_r s}$ & $\leq$ & 
Chengsong
parents: 561
diff changeset
  2503
		$max \; (2 + N_1 + 
Chengsong
parents: 561
diff changeset
  2504
		\llbracket r_2 \rrbracket_r + 
Chengsong
parents: 561
diff changeset
  2505
		N_2 * (card\; (\sizeNregex \; N_2))) \; \rsize{r_1\cdot r_2}$ & (4)
Chengsong
parents: 561
diff changeset
  2506
	\end{tabular}
Chengsong
parents: 561
diff changeset
  2507
\end{center}
558
Chengsong
parents: 557
diff changeset
  2508
562
Chengsong
parents: 561
diff changeset
  2509
We reason similarly for  $\STAR$.
Chengsong
parents: 561
diff changeset
  2510
The inductive hypothesis is
Chengsong
parents: 561
diff changeset
  2511
$\exists N. \forall s. \; \llbracket \rderssimp{r}{s} \rrbracket \leq N$.
564
Chengsong
parents: 562
diff changeset
  2512
Let $n_r = \llbracket r^* \rrbracket_r$.
562
Chengsong
parents: 561
diff changeset
  2513
When $s = c :: cs$ is not empty,
Chengsong
parents: 561
diff changeset
  2514
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2515
	\begin{tabular}{lcll}
562
Chengsong
parents: 561
diff changeset
  2516
& & $ \llbracket   \rderssimp{r^* }{c::cs} \rrbracket_r $\\
Chengsong
parents: 561
diff changeset
  2517
& $ = $ & $\llbracket \rsimp{(\sum (\map \; (\lambda s. (r \backslash_{rsimp} s) \cdot r^*) \; (\starupdates\; 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2518
	cs \; r \; [[c]] )) )} \rrbracket_r $ & (5) \\			
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2519
					      & $\leq$ & $\llbracket 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2520
					      \rdistinct{
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2521
						      (\map \; 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2522
						      (\lambda s. (r \backslash_{rsimp} s) \cdot r^*) \; 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2523
						      (\starupdates\; cs \; r \; [[c]] )
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2524
					      )}
562
Chengsong
parents: 561
diff changeset
  2525
	{\varnothing} \rrbracket_r  + 1$ & (6) \\
Chengsong
parents: 561
diff changeset
  2526
					 & $\leq$ & $1 + (\textit{card} (\sizeNregex \; (N + n_r)))
Chengsong
parents: 561
diff changeset
  2527
	* (1 + (N + n_r)) $ & (7)\\
Chengsong
parents: 561
diff changeset
  2528
\end{tabular}
Chengsong
parents: 561
diff changeset
  2529
\end{center}
Chengsong
parents: 561
diff changeset
  2530
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2531
(5) is by theorem \ref{starClosedForm}.
562
Chengsong
parents: 561
diff changeset
  2532
(6) is by \ref{altsSimpControl}.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2533
(7) is by corollary \ref{finiteSizeNCorollary}.
562
Chengsong
parents: 561
diff changeset
  2534
Combining with the case when $s = []$, one gets
Chengsong
parents: 561
diff changeset
  2535
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2536
	\begin{tabular}{lcll}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2537
		$\rsize{r^* \backslash_r s}$ & $\leq$ & $max \; n_r \; 1 + (\textit{card} (\sizeNregex \; (N + n_r)))
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2538
		* (1 + (N + n_r)) $ & (8)\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2539
	\end{tabular}
562
Chengsong
parents: 561
diff changeset
  2540
\end{center}
Chengsong
parents: 561
diff changeset
  2541
\noindent
Chengsong
parents: 561
diff changeset
  2542
Chengsong
parents: 561
diff changeset
  2543
The alternative case is slightly less involved.
Chengsong
parents: 561
diff changeset
  2544
The inductive hypothesis 
Chengsong
parents: 561
diff changeset
  2545
is equivalent to $\exists N. \forall r \in (\map \; (\_ \backslash_r s) \; rs). \rsize{r} \leq N$.
Chengsong
parents: 561
diff changeset
  2546
In the case when $s = c::cs$, we have 
Chengsong
parents: 561
diff changeset
  2547
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2548
	\begin{tabular}{lcll}
562
Chengsong
parents: 561
diff changeset
  2549
& & $ \llbracket   \rderssimp{\sum rs }{c::cs} \rrbracket_r $\\
Chengsong
parents: 561
diff changeset
  2550
& $ = $ & $\llbracket \rsimp{(\sum (\map \; (\_ \backslash_{rsimp} s)  \; rs) )} \rrbracket_r $ & (9) \\			
Chengsong
parents: 561
diff changeset
  2551
& $\leq$ & $\llbracket (\sum (\map \; (\_ \backslash_{rsimp} s)  \; rs) ) \rrbracket_r $  & (10) \\
Chengsong
parents: 561
diff changeset
  2552
& $\leq$ & $1 + N * (length \; rs) $ & (11)\\
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2553
	\end{tabular}
562
Chengsong
parents: 561
diff changeset
  2554
\end{center}
Chengsong
parents: 561
diff changeset
  2555
\noindent
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2556
(9) is by theorem \ref{altsClosedForm}, (10) by lemma \ref{rsimpMono} and (11) by inductive hypothesis.
562
Chengsong
parents: 561
diff changeset
  2557
Chengsong
parents: 561
diff changeset
  2558
Combining with the case when $s = []$, one gets
Chengsong
parents: 561
diff changeset
  2559
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2560
	\begin{tabular}{lcll}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2561
		$\rsize{\sum rs \backslash_r s}$ & $\leq$ & $max \; \rsize{\sum rs} \; 1+N*(length \; rs)$ 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2562
						 & (12)\\
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2563
	\end{tabular}
562
Chengsong
parents: 561
diff changeset
  2564
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2565
We have all the inductive cases proven.
558
Chengsong
parents: 557
diff changeset
  2566
\end{proof}
Chengsong
parents: 557
diff changeset
  2567
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2568
This leads to our main result on the size bound:
564
Chengsong
parents: 562
diff changeset
  2569
\begin{corollary}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2570
	For any annotated regular expression $a$, $\exists N_r. \forall s. \; \rsize{\bderssimp{a}{s}} \leq N_r$
564
Chengsong
parents: 562
diff changeset
  2571
\end{corollary}
Chengsong
parents: 562
diff changeset
  2572
\begin{proof}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2573
	By lemma \ref{sizeRelations} and theorem \ref{rBound}.
564
Chengsong
parents: 562
diff changeset
  2574
\end{proof}
558
Chengsong
parents: 557
diff changeset
  2575
\noindent
Chengsong
parents: 557
diff changeset
  2576
609
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2577
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2578
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2579
61139fdddae0 chap1 totally done
Chengsong
parents: 601
diff changeset
  2580
558
Chengsong
parents: 557
diff changeset
  2581
%-----------------------------------
Chengsong
parents: 557
diff changeset
  2582
%	SECTION 2
Chengsong
parents: 557
diff changeset
  2583
%-----------------------------------
Chengsong
parents: 557
diff changeset
  2584
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2585
557
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  2586
%----------------------------------------------------------------------------------------
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  2587
%	SECTION 3
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  2588
%----------------------------------------------------------------------------------------
812e5d112f49 more changes
Chengsong
parents: 556
diff changeset
  2589
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2590
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2591
\section{Comments and Future Improvements}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2592
\subsection{Some Experimental Results}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2593
What guarantee does this bound give us?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2594
Whatever the regex is, it will not grow indefinitely.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2595
Take our previous example $(a + aa)^*$ as an example:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2596
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2597
	\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2598
		\begin{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2599
			\begin{axis}[
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2600
				xlabel={number of $a$'s},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2601
				x label style={at={(1.05,-0.05)}},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2602
				ylabel={regex size},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2603
				enlargelimits=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2604
				xtick={0,5,...,30},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2605
				xmax=33,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2606
				ymax= 40,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2607
				ytick={0,10,...,40},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2608
				scaled ticks=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2609
				axis lines=left,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2610
				width=5cm,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2611
				height=4cm, 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2612
				legend entries={$(a + aa)^*$},  
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2613
				legend pos=south east,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2614
				legend cell align=left]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2615
				\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2616
			\end{axis}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2617
		\end{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2618
	\end{tabular}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2619
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2620
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2621
with our simplification
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2622
rules very effectively.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2623
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2624
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2625
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2626
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2627
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2628
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2629
$f(x) = x * 2^x$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2630
This means the bound we have will surge up at least
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2631
tower-exponentially with a linear increase of the depth.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2632
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2633
One might be quite skeptical about what this non-elementary
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2634
bound can bring us.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2635
It turns out that the giant bounds are far from being hit.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2636
Here we have some test data from randomly generated regular expressions:
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2637
\begin{figure}[H]
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2638
	\begin{tabular}{@{}c@{\hspace{2mm}}c@{\hspace{0mm}}c@{}}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2639
		\begin{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2640
			\begin{axis}[
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2641
				xlabel={$n$},
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2642
				x label style={at={(1.05,-0.05)}},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2643
				ylabel={regex size},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2644
				enlargelimits=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2645
				xtick={0,5,...,30},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2646
				xmax=33,
611
Chengsong
parents: 610
diff changeset
  2647
				%ymax=1000,
Chengsong
parents: 610
diff changeset
  2648
				%ytick={0,100,...,1000},
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2649
				scaled ticks=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2650
				axis lines=left,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2651
				width=4.75cm,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2652
				height=3.8cm, 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2653
				legend entries={regex1},  
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2654
				legend pos=north east,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2655
				legend cell align=left]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2656
				\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2657
			\end{axis}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2658
		\end{tikzpicture}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2659
 & 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2660
  \begin{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2661
	  \begin{axis}[
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2662
		  xlabel={$n$},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2663
		  x label style={at={(1.05,-0.05)}},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2664
		  %ylabel={time in secs},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2665
		  enlargelimits=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2666
		  xtick={0,5,...,30},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2667
		  xmax=33,
611
Chengsong
parents: 610
diff changeset
  2668
		  %ymax=1000,
Chengsong
parents: 610
diff changeset
  2669
		  %ytick={0,100,...,1000},
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2670
		  scaled ticks=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2671
		  axis lines=left,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2672
		  width=4.75cm,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2673
		  height=3.8cm, 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2674
		  legend entries={regex2},  
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2675
		  legend pos=south east,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2676
		  legend cell align=left]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2677
		  \addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2678
	  \end{axis}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2679
  \end{tikzpicture}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2680
 & 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2681
  \begin{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2682
	  \begin{axis}[
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2683
		  xlabel={$n$},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2684
		  x label style={at={(1.05,-0.05)}},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2685
		  %ylabel={time in secs},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2686
		  enlargelimits=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2687
		  xtick={0,5,...,30},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2688
		  xmax=33,
611
Chengsong
parents: 610
diff changeset
  2689
		  %ymax=1000,
Chengsong
parents: 610
diff changeset
  2690
		  %ytick={0,100,...,1000},
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2691
		  scaled ticks=false,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2692
		  axis lines=left,
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2693
		  width=4.75cm,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2694
		  height=3.8cm, 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2695
		  legend entries={regex3},  
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2696
		  legend pos=south east,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2697
		  legend cell align=left]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2698
		  \addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2699
	  \end{axis}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2700
  \end{tikzpicture}\\
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2701
  \multicolumn{3}{c}{}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2702
	\end{tabular}    
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2703
  \caption{Graphs: size change of 3 randomly generated 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2704
  regular expressions $w.r.t.$ input string length. 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2705
  The x axis represents the length of input.}
611
Chengsong
parents: 610
diff changeset
  2706
\end{figure}  
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2707
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2708
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2709
original size.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
  2710
We will discuss improvements to this bound in the next chapter.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2711
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2712
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2713
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2714
\subsection{Possible Further Improvements}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2715
There are two problems with this finiteness result, though.\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2716
(i)	
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2717
		First, it is not yet a direct formalisation of our lexer's complexity,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2718
		as a complexity proof would require looking into 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2719
		the time it takes to execute {\bf all} the operations
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2720
		involved in the lexer (simp, collect, decode), not just the derivative.\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2721
(ii)
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2722
		Second, the bound is not yet tight, and we seek to improve $N_a$ so that
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2723
		it is polynomial on $\llbracket a \rrbracket$.\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2724
Still, we believe this contribution is useful,
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2725
because
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2726
\begin{itemize}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2727
	\item
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2728
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2729
		The size proof can serve as a starting point for a complexity
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2730
		formalisation.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2731
		Derivatives are the most important phases of our lexer algorithm.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2732
		Size properties about derivatives covers the majority of the algorithm
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2733
		and is therefore a good indication of complexity of the entire program.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2734
	\item
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2735
		The bound is already a strong indication that catastrophic
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2736
		backtracking is much less likely to occur in our $\blexersimp$
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2737
		algorithm.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2738
		We refine $\blexersimp$ with $\blexerStrong$ in the next chapter
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2739
		so that the bound becomes polynomial.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 577
diff changeset
  2740
\end{itemize}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2741
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2742
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2743
%	SECTION 4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2744
%----------------------------------------------------------------------------------------
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2745
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2746
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2747
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2748
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2749
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2750
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2751
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2752
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2753
One might wonder the actual bound rather than the loose bound we gave
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2754
for the convenience of an easier proof.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2755
How much can the regex $r^* \backslash s$ grow? 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2756
As  earlier graphs have shown,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2757
%TODO: reference that graph where size grows quickly
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2758
they can grow at a maximum speed
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2759
exponential $w.r.t$ the number of characters, 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2760
but will eventually level off when the string $s$ is long enough.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2761
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2762
would still be slow.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2763
And unfortunately, we have concrete examples
576
3e1b699696b6 thesis chap5
Chengsong
parents: 564
diff changeset
  2764
where such regular expressions grew exponentially large before levelling off:
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2765
\begin{center}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2766
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots + 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2767
(\underbrace{a \ldots a}_{\text{n a's}})^*)^*$ 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2768
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2769
will already have a maximum
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2770
size that is  exponential on the number $n$ 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2771
under our current simplification rules:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2772
%TODO: graph of a regex whose size increases exponentially.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2773
\begin{center}
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2774
	\begin{tikzpicture}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2775
		\begin{axis}[
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2776
			height=0.5\textwidth,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2777
			width=\textwidth,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2778
			xlabel=number of a's,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2779
			xtick={0,...,9},
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2780
			ylabel=maximum size,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2781
			ymode=log,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2782
			log basis y={2}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2783
			]
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2784
			\addplot[mark=*,blue] table {re-chengsong.data};
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2785
		\end{axis}
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2786
	\end{tikzpicture}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2787
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2788
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2789
For convenience we use $(\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2790
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots + 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2791
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2792
The exponential size is triggered by that the regex
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2793
$\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2794
inside the $(\ldots) ^*$ having exponentially many
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2795
different derivatives, despite those difference being minor.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2796
$(\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2797
will therefore contain the following terms (after flattening out all nested 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2798
alternatives):
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2799
\begin{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2800
$(\sum_{i = 1}^{n}  (\underbrace{a \ldots a}_{\text{((i - (m' \% i))\%i) a's}})\cdot  (\underbrace{a \ldots a}_{\text{i a's}})^* )\cdot (\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2801
[1mm]
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2802
	$(1 \leq m' \leq m )$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2803
\end{center}
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2804
There at at least exponentially
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2805
many such terms.\footnote{To be exact, these terms are 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2806
distinct for $m' \leq L.C.M.(1, \ldots, n)$, the details are omitted,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2807
but the point is that the number is exponential.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2808
} 
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2809
With each new input character taking the derivative against the intermediate result, more and more such distinct
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2810
terms will accumulate.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2811
The function $\textit{distinctBy}$ will not be able to de-duplicate any two of these terms 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2812
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2813
$(\sum_{i = 1}^{n}  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2814
(\underbrace{a \ldots a}_{\text{((i - (m' \% i))\%i) a's}})\cdot  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2815
(\underbrace{a \ldots a}_{\text{i a's}})^* )\cdot 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2816
(\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2817
$(\sum_{i = 1}^{n}  (\underbrace{a \ldots a}_{\text{((i - (m'' \% i))\%i) a's}})\cdot  
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2818
(\underbrace{a \ldots a}_{\text{i a's}})^* )\cdot 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2819
(\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2820
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2821
\noindent
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2822
where $m' \neq m''$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2823
as they are slightly different.
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2824
This means that with our current simplification methods,
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2825
we will not be able to control the derivative so that
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2826
$\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial. %\leq O((\llbracket r\rrbacket)^c)$
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2827
These terms are similar in the sense that the head of those terms
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2828
are all consisted of sub-terms of the form: 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2829
$(\underbrace{a \ldots a}_{\text{j a's}})\cdot  (\underbrace{a \ldots a}_{\text{i a's}})^* $.
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2830
For  $\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2831
$n * (n + 1) / 2$ such terms. 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2832
For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2833
can be described by 6 terms:
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2834
$a^*$, $a\cdot (aa)^*$, $ (aa)^*$, 
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2835
$aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2836
The total number of different "head terms",  $n * (n + 1) / 2$,
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2837
is proportional to the number of characters in the regex 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2838
$(\sum_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2839
If we can improve our deduplication process so that it becomes smarter
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2840
and only keep track of these $n * (n+1) /2$ terms, then we can keep
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2841
the size growth polynomial again.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2842
This example also suggests a slightly different notion of size, which we call the 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2843
alphabetic width:
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2844
\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2845
	\begin{tabular}{lcl}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2846
		$\textit{awidth} \; \ZERO$ & $\dn$ & $0$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2847
		$\textit{awidth} \; \ONE$ & $\dn$ & $0$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2848
		$\textit{awidth} \; c$ & $\dn$ & $1$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2849
		$\textit{awidth} \; r_1 + r_2$ & $\dn$ & $\textit{awidth} \; 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2850
		r_1 + \textit{awidth} \; r_2$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2851
		$\textit{awidth} \; r_1 \cdot r_2$ & $\dn$ & $\textit{awidth} \;
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2852
		r_1 + \textit{awidth} \; r_2$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2853
		$\textit{awidth} \; r^*$ & $\dn$ & $\textit{awidth} \; r$\\
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2854
	\end{tabular}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2855
\end{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2856
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2857
593
83fab852d72d more chap5
Chengsong
parents: 591
diff changeset
  2858
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2859
Antimirov\parencite{Antimirov95} has proven that 
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2860
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$,
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2861
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2862
created by doing derivatives of $r$ against all possible strings.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2863
If we can make sure that at any moment in our lexing algorithm our 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2864
intermediate result hold at most one copy of each of the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2865
subterms then we can get the same bound as Antimirov's.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2866
This leads to the algorithm in the next chapter.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2867
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2868
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2869
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2870
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2871
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2872
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2873
%	SECTION 1
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2874
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2875
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2876
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2877
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2878
%	SUBSECTION 1
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2879
%-----------------------------------
618
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2880
%\subsection{Syntactic Equivalence Under $\simp$}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2881
%We prove that minor differences can be annhilated
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2882
%by $\simp$.
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2883
%For example,
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2884
%\begin{center}
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2885
%	$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) = 
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2886
%	\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
233cf2b97d1a chapter 5 finished!!
Chengsong
parents: 614
diff changeset
  2887
%\end{center}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
  2888