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