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