ChengsongTanPhdThesis/Chapters/Cubic.tex
author Chengsong
Sat, 26 Nov 2022 16:18:10 +0000
changeset 628 7af4e2420a8c
parent 625 b797c9a709d9
child 630 d50a309a0645
permissions -rwxr-xr-x
ready to submit~~
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{A Better Bound and Other Extensions} % Main chapter title
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
\label{Cubic} %In Chapter 5\ref{Chapter5} we discuss stronger simplifications to improve the finite bound
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
%in Chapter 4 to a polynomial one, and demonstrate how one can extend the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     7
%algorithm to include constructs such as bounded repetitions and negations.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
     8
\lstset{style=myScalastyle}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
     9
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    10
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    11
This chapter is a ``work-in-progress''
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    12
chapter which records
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    13
extensions to our $\blexersimp$.
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    14
We intend to formalise this part, which
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    15
we have not been able to finish due to time constraints of the PhD.
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    16
Nevertheless, we outline the ideas we intend to use for the proof.
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    17
b797c9a709d9 section reorganising, related work
Chengsong
parents: 621
diff changeset
    18
We present further improvements
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    19
for our lexer algorithm $\blexersimp$.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    20
We devise a stronger simplification algorithm,
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    21
called $\bsimpStrong$, which can prune away
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    22
similar components in two regular expressions at the same 
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    23
alternative level,
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    24
even if these regular expressions are not exactly the same.
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    25
We call the lexer that uses this stronger simplification function
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    26
$\blexerStrong$.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    27
Unfortunately we did not have time to 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    28
work out the proofs, like in
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    29
the previous chapters.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    30
We conjecture that both
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    31
\begin{center}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    32
	$\blexerStrong \;r \; s = \blexer\; r\;s$
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    33
\end{center}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    34
and
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    35
\begin{center}
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    36
	$\llbracket \bdersStrong{a}{s} \rrbracket = O(\llbracket a \rrbracket^3)$
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    37
\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    38
hold, but a formalisation
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    39
is still future work.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    40
We give an informal justification 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    41
why the correctness and cubic size bound proofs
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    42
can be achieved
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    43
by exploring the connection between the internal
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    44
data structure of our $\blexerStrong$ and
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    45
Animirov's partial derivatives.\\
621
Chengsong
parents: 620
diff changeset
    46
%We also present the idempotency property proof
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
    47
%of $\bsimp$, which leverages the idempotency proof of $\rsimp$.
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
    48
%This reinforces our claim that the fixpoint construction
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
    49
%originally required by Sulzmann and Lu can be removed in $\blexersimp$.
621
Chengsong
parents: 620
diff changeset
    50
Chengsong
parents: 620
diff changeset
    51
%Last but not least, we present our efforts and challenges we met
Chengsong
parents: 620
diff changeset
    52
%in further improving the algorithm by data
Chengsong
parents: 620
diff changeset
    53
%structures such as zippers.
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    54
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    55
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    56
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    57
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    58
%	SECTION strongsimp
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    59
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    60
\section{A Stronger Version of Simplification}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    61
%TODO: search for isabelle proofs of algorithms that check equivalence 
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
    62
In our bitcoded lexing algorithm, (sub)terms represent (sub)matches.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    63
For example, the regular expression 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    64
\[
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    65
	aa \cdot a^*+ a \cdot a^* + aa\cdot a^*
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    66
\]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    67
contains three terms, 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    68
expressing three possibilities for how it can match some input.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    69
The first and the third terms are identical, which means we can eliminate
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    70
the latter as it will not contribute to a POSIX value.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    71
In $\bsimps$, the $\distinctBy$ function takes care of 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    72
such instances.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    73
The criteria $\distinctBy$ uses for removing a duplicate
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    74
$a_2$ in the list
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    75
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    76
	$rs_a@[a_1]@rs_b@[a_2]@rs_c$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    77
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    78
is that 
533
Chengsong
parents: 532
diff changeset
    79
\begin{center}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    80
	$\rerase{a_1} = \rerase{a_2}$.
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    81
\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    82
It is characterised as the $LD$ 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    83
rewrite rule in figure \ref{rrewriteRules}.\\
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    84
The problem , however, is that identical components
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    85
in two slightly different regular expressions cannot be removed.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    86
Consider the simplification
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    87
\begin{equation}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    88
	\label{eqn:partialDedup}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    89
	(a+b+d) \cdot r_1 + (a+c+e) \cdot r_1 \stackrel{?}{\rightsquigarrow} (a+b+d) \cdot r_1 + (c+e) \cdot r_1
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    90
\end{equation}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    91
where the $(a+\ldots)\cdot r_1$ is deleted in the right alternative.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    92
This is permissible because we have $(a+\ldots)\cdot r_1$ in the left
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    93
alternative.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    94
The difficulty is that such  ``buried''
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    95
alternatives-sequences are not easily recognised.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    96
But simplification like this actually
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
    97
cannot be omitted,
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    98
as without it the size of derivatives can still
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
    99
blow up even with our $\textit{bsimp}$ 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   100
function: 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   101
consider again the example
621
Chengsong
parents: 620
diff changeset
   102
$\protect((a^* + (aa)^* + \ldots + (\underbrace{a\ldots a}_{n a's})^* )^*)^*$,
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   103
and set $n$ to a relatively small number,
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   104
we get exponential growth:
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   105
\begin{figure}[H]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   106
\centering
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   107
\begin{tikzpicture}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   108
\begin{axis}[
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   109
    %xlabel={$n$},
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   110
    myplotstyle,
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   111
    xlabel={input length},
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   112
    ylabel={size},
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   113
    ]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   114
\addplot[blue,mark=*, mark options={fill=white}] table {bsimpExponential.data};
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   115
\end{axis}
533
Chengsong
parents: 532
diff changeset
   116
\end{tikzpicture}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   117
\caption{Size of derivatives of $\blexersimp$ for matching 
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   118
	$\protect((a^* + (aa)^* + \ldots + (aaaaa)^* )^*)^*$ 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   119
	with strings 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   120
	of the form $\protect\underbrace{aa..a}_{n}$.}\label{blexerExp}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   121
\end{figure}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   122
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   123
One possible approach would be to apply the rewriting
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   124
rule
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   125
\[
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   126
	(a+b+d) \cdot r_1  \longrightarrow a \cdot r_1 + b \cdot r_1 + d \cdot r_1
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   127
\]
533
Chengsong
parents: 532
diff changeset
   128
\noindent
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   129
in our $\simp$ function,
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   130
so that it makes the simplification in \eqref{eqn:partialDedup} possible.
621
Chengsong
parents: 620
diff changeset
   131
Translating the rule into our $\textit{bsimp}$ function simply
Chengsong
parents: 620
diff changeset
   132
involves adding a new clause to the $\textit{bsimp}_{ASEQ}$ function:
Chengsong
parents: 620
diff changeset
   133
\begin{center}
Chengsong
parents: 620
diff changeset
   134
	\begin{tabular}{@{}lcl@{}}
Chengsong
parents: 620
diff changeset
   135
		$\textit{bsimp}_{ASEQ} \; bs\; a \; b$ & $\dn$ & $ (a,\; b) \textit{match}$\\
Chengsong
parents: 620
diff changeset
   136
						       && $\ldots$\\
Chengsong
parents: 620
diff changeset
   137
   &&$\quad\textit{case} \; (_{bs1}\sum as, a_2') \Rightarrow _{bs1}\sum (
Chengsong
parents: 620
diff changeset
   138
   \map \; (_{[]}\textit{ASEQ} \; \_ \; a_2') \; as)$\\
Chengsong
parents: 620
diff changeset
   139
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow   _{bs}a_1' \cdot a_2'$ \\
Chengsong
parents: 620
diff changeset
   140
	\end{tabular}
Chengsong
parents: 620
diff changeset
   141
\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   142
\noindent
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   143
Unfortunately,
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   144
if we introduce them in our
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   145
setting we would lose the POSIX property of our calculated values. 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   146
For example given the regular expression 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   147
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   148
	$(a + ab)(bc + c)$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   149
\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   150
and the string $ab$,
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   151
then our algorithm generates the following
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   152
correct POSIX value
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   153
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   154
	$\Seq \; (\Right \; ab) \; (\Right \; c)$.
533
Chengsong
parents: 532
diff changeset
   155
\end{center}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   156
Essentially it matches the string with the longer Right-alternative
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   157
in the first sequence (and
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   158
then the 'rest' with the character regular expression $c$ from the second sequence).
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   159
If we add the simplification above, then we obtain the following value
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   160
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   161
	$\Left \; (\Seq \; a \; (\Left \; bc))$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   162
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   163
where the $\Left$-alternatives get priority. 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   164
However this violates the POSIX rules.
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   165
The reason for getting this undesired value
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   166
is that the new rule splits this regular expression up into 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   167
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   168
	$a\cdot(b c + c) + ab \cdot (bc + c)$,
533
Chengsong
parents: 532
diff changeset
   169
\end{center}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   170
which becomes a regular expression with a 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   171
quite different structure--the original
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   172
was a sequence, and now it becomes an alternative.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   173
With an alternative the maximal munch rule no longer works.\\
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   174
A method to reconcile this is to do the 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   175
transformation in \eqref{eqn:partialDedup} ``non-invasively'',
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   176
meaning that we traverse the list of regular expressions
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   177
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   178
	$rs_a@[a]@rs_c$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   179
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   180
in the alternative
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   181
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   182
	$\sum ( rs_a@[a]@rs_c)$
533
Chengsong
parents: 532
diff changeset
   183
\end{center}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   184
using  a function similar to $\distinctBy$,
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   185
but this time 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   186
we allow a more general list rewrite:
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   187
\begin{figure}[H]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   188
\begin{mathpar}	
621
Chengsong
parents: 620
diff changeset
   189
	\inferrule * [Right = cubicRule]{\vspace{0mm} }{rs_a@[a]@rs_c 
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   190
			\stackrel{s}{\rightsquigarrow }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   191
		rs_a@[\textit{prune} \; a \; rs_a]@rs_c }
592
Chengsong
parents: 591
diff changeset
   192
\end{mathpar}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   193
\caption{The rule capturing the pruning simplification needed to achieve
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   194
a cubic bound}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   195
\label{fig:cubicRule}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   196
\end{figure}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   197
%L \; a_1' = L \; a_1 \setminus (\cup_{a \in rs_a} L \; a)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   198
where $\textit{prune} \;a \; acc$ traverses $a$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   199
without altering the structure of $a$, removing components in $a$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   200
that have appeared in the accumulator $acc$.
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   201
For example
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   202
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   203
	$\textit{prune} \;\;\; (r_a+r_f+r_g+r_h)r_d \;\; \; [(r_a+r_b+r_c)r_d, (r_e+r_f)r_d] $
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   204
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   205
should be equal to 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   206
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   207
	$(r_g+r_h)r_d$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   208
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   209
because $r_gr_d$ and 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   210
$r_hr_d$ are the only terms
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   211
that have not appeared in the accumulator list 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   212
\begin{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   213
$[(r_a+r_b+r_c)r_d, (r_e+r_f)r_d]$.
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   214
\end{center}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   215
We implemented 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   216
function $\textit{prune}$ in Scala:
590
988e92a70704 more chap5 and chap6 bsimp_idem
Chengsong
parents: 538
diff changeset
   217
\begin{figure}[H]
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   218
\begin{lstlisting}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   219
    def prune(r: ARexp, acc: Set[Rexp]) : ARexp = r match{
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   220
      case AALTS(bs, rs) => rs.map(r => prune(r, acc)).filter(_ != AZERO) match
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   221
      {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   222
        //all components have been removed, meaning this is effectively a duplicate
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   223
        //flats will take care of removing this AZERO 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   224
        case Nil => AZERO
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   225
        case r::Nil => fuse(bs, r)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   226
        case rs1 => AALTS(bs, rs1)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   227
      }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   228
      case ASEQ(bs, r1, r2) => 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   229
        //remove the r2 in (ra + rb)r2 to identify the duplicate contents of r1
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   230
        prune(r1, acc.map(r => removeSeqTail(r, erase(r2)))) match {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   231
          //after pruning, returns 0
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   232
          case AZERO => AZERO
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   233
          //after pruning, got r1'.r2, where r1' is equal to 1
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   234
          case r1p if(isOne(erase(r1p))) => fuse(bs ++ mkepsBC(r1p), r2)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   235
          //assemble the pruned head r1p with r2
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   236
          case r1p => ASEQ(bs, r1p, r2)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   237
        }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   238
        //this does the duplicate component removal task
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   239
      case r => if(acc(erase(r))) AZERO else r
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   240
    }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   241
\end{lstlisting}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   242
\caption{The function $\textit{prune}$ }
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   243
\end{figure}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   244
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   245
The function $\textit{prune}$ 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   246
is a stronger version of $\textit{distinctBy}$.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   247
It does not just walk through a list looking for exact duplicates,
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   248
but prunes sub-expressions recursively.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   249
It manages proper contexts by the helper functions
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   250
$\textit{removeSeqTail}$, $\textit{isOne}$ and $\textit{atMostEmpty}$.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   251
\begin{figure}[H]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   252
\begin{lstlisting}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   253
    def atMostEmpty(r: Rexp) : Boolean = r match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   254
      case ZERO => true
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   255
      case ONE => true
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   256
      case STAR(r) => atMostEmpty(r)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   257
      case SEQ(r1, r2) => atMostEmpty(r1) && atMostEmpty(r2)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   258
      case ALTS(r1, r2) => atMostEmpty(r1) && atMostEmpty(r2)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   259
      case CHAR(_) => false
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   260
    }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   261
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   262
    def isOne(r: Rexp) : Boolean = r match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   263
      case ONE => true
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   264
      case SEQ(r1, r2) => isOne(r1) && isOne(r2)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   265
      case ALTS(r1, r2) => (isOne(r1) || isOne(r2)) && (atMostEmpty(r1) && atMostEmpty(r2))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   266
      case STAR(r0) => atMostEmpty(r0)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   267
      case CHAR(c) => false
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   268
      case ZERO => false
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   269
    }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   270
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   271
    def removeSeqTail(r: Rexp, tail: Rexp) : Rexp = 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   272
      if (r == tail)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   273
        ONE
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   274
      else {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   275
        r match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   276
          case SEQ(r1, r2) => 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   277
            if(r2 == tail) 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   278
              r1
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   279
            else
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   280
              ZERO
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   281
          case r => ZERO
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   282
        }
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   283
      }
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   284
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   285
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   286
\end{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   287
\caption{The helper functions of $\textit{prune}$}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   288
\end{figure}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   289
\noindent
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   290
Suppose we feed 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   291
\begin{center}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   292
	$r= (\underline{\ONE}+(\underline{f}+b)\cdot g)\cdot (a\cdot(d\cdot e))$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   293
\end{center}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   294
and 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   295
\begin{center}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   296
	$acc = \{a\cdot(d\cdot e),f\cdot (g \cdot (a \cdot (d \cdot e))) \}$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   297
\end{center}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   298
as the input for $\textit{prune}$.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   299
The end result will be
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   300
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   301
	b\cdot(g\cdot(a\cdot(d\cdot e)))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   302
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   303
where the underlined components in $r$ are eliminated.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   304
Looking more closely, at the topmost call 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   305
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   306
	\textit{prune} \quad (\ONE+
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   307
	(f+b)\cdot g)\cdot (a\cdot(d\cdot e)) \quad
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   308
	\{a\cdot(d\cdot e),f\cdot (g \cdot (a \cdot (d \cdot e))) \}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   309
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   310
The sequence clause will be called,
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   311
where a sub-call
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   312
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   313
	\textit{prune} \;\; (\ONE+(f+b)\cdot g)\;\; \{\ONE, f\cdot g \}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   314
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   315
is made. The terms in the new accumulator $\{\ONE,\; f\cdot g \}$ come from
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   316
the two calls to $\textit{removeSeqTail}$:
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   317
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   318
	\textit{removeSeqTail} \quad\;\; a \cdot(d\cdot e) \quad\;\; a \cdot(d\cdot e) 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   319
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   320
and
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   321
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   322
	\textit{removeSeqTail} \quad \;\; 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   323
	f\cdot(g\cdot (a \cdot(d\cdot e)))\quad  \;\; a \cdot(d\cdot e).
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   324
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   325
The idea behind $\textit{removeSeqTail}$ is that
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   326
when pruning recursively, we need to ``zoom in''
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   327
to sub-expressions, and this ``zoom in'' needs to be performed
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   328
on the
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   329
accumulators as well, otherwise we will be comparing
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   330
apples with oranges.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   331
The sub-call 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   332
$\textit{prune} \;\; (\ONE+(f+b)\cdot g)\;\; \{\ONE, f\cdot g \}$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   333
is simpler, which will trigger the alternative clause, causing
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   334
a pruning on each element in $(\ONE+(f+b)\cdot g)$,
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   335
leaving us $b\cdot g$ only.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   336
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   337
Our new lexer with stronger simplification
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   338
uses $\textit{prune}$ by making it 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   339
the core component of the deduplicating function
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   340
called $\textit{distinctWith}$.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   341
$\textit{DistinctWith}$ ensures that all verbose
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   342
parts of a regular expression are pruned away.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   343
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   344
\begin{figure}[H]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   345
\begin{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   346
    def turnIntoTerms(r: Rexp): List[Rexp] = r match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   347
      case SEQ(r1, r2)  => 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   348
        turnIntoTerms(r1).flatMap(r11 => furtherSEQ(r11, r2))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   349
          case ALTS(r1, r2) => turnIntoTerms(r1) ::: turnIntoTerms(r2)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   350
          case ZERO => Nil
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   351
          case _ => r :: Nil
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   352
    }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   353
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   354
    def distinctWith(rs: List[ARexp], 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   355
      pruneFunction: (ARexp, Set[Rexp]) => ARexp, 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   356
      acc: Set[Rexp] = Set()) : List[ARexp] = 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   357
        rs match{
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   358
          case Nil => Nil
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   359
          case r :: rs => 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   360
            if(acc(erase(r)))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   361
              distinctWith(rs, pruneFunction, acc)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   362
            else {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   363
              val pruned_r = pruneFunction(r, acc)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   364
              pruned_r :: 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   365
              distinctWith(rs, 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   366
                pruneFunction, 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   367
                turnIntoTerms(erase(pruned_r)) ++: acc
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   368
                )
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   369
            }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   370
        }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   371
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   372
\end{lstlisting}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   373
\caption{A Stronger Version of $\textit{distinctBy}$}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   374
\end{figure}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   375
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   376
Once a regular expression has been pruned,
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   377
all its components will be added to the accumulator
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   378
to remove any future regular expressions' duplicate components.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   379
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   380
The function $\textit{bsimpStrong}$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   381
is very much the same as $\textit{bsimp}$, just with
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   382
$\textit{distinctBy}$ replaced 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   383
by $\textit{distinctWith}$.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   384
\begin{figure}[H]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   385
\begin{lstlisting}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   386
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   387
    def bsimpStrong(r: ARexp): ARexp =
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   388
    {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   389
      r match {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   390
        case ASEQ(bs1, r1, r2) => (bsimpStrong(r1), bsimpStrong(r2)) match {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   391
        case (AZERO, _) => AZERO
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   392
        case (_, AZERO) => AZERO
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   393
        case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   394
        case (r1s, AONE(bs2)) => fuse(bs1, r1s) //assert bs2 == Nil
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   395
        case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   396
        }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   397
        case AALTS(bs1, rs) => {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   398
          distinctWith(flats(rs.map(bsimpStrong(_))), prune) match {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   399
            case Nil => AZERO
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   400
            case s :: Nil => fuse(bs1, s)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   401
            case rs => AALTS(bs1, rs)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   402
          }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   403
        }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   404
        case ASTAR(bs, r0) if(atMostEmpty(erase(r0))) => AONE(bs)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   405
        case r => r
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   406
      }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   407
    }
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   408
    def bdersStrong(s: List[Char], r: ARexp) : ARexp = s match {
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   409
        case Nil => r
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   410
        case c::s => bdersStrong(s, bsimpStrong(bder(c, r)))
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   411
      }
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   412
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   413
\end{lstlisting}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   414
\caption{The function
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   415
$\textit{bsimpStrong}$: a stronger version of $\textit{bsimp}$}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   416
\end{figure}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   417
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   418
The benefits of using 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   419
$\textit{prune}$ refining the finiteness bound
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   420
to a cubic bound has not been formalised yet.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   421
Therefore we choose to use Scala code rather than an Isabelle-style formal 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   422
definition like we did for $\simp$, as the definitions might change
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   423
to suit our proof needs.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   424
In the rest of the chapter we will use this convention consistently.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   425
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   426
%The function $\textit{prune}$ is used in $\distinctWith$.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   427
%$\distinctWith$ is a stronger version of $\distinctBy$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   428
%which not only removes duplicates as $\distinctBy$ would
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   429
%do, but also uses the $\textit{pruneFunction}$
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   430
%argument to prune away verbose components in a regular expression.\\
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   431
%\begin{figure}[H]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   432
%\begin{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   433
%   //a stronger version of simp
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   434
%    def bsimpStrong(r: ARexp): ARexp =
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   435
%    {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   436
%      r match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   437
%        case ASEQ(bs1, r1, r2) => (bsimpStrong(r1), bsimpStrong(r2)) match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   438
%          //normal clauses same as simp
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   439
%        case (AZERO, _) => AZERO
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   440
%        case (_, AZERO) => AZERO
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   441
%        case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   442
%        //bs2 can be discarded
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   443
%        case (r1s, AONE(bs2)) => fuse(bs1, r1s) //assert bs2 == Nil
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   444
%        case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   445
%        }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   446
%        case AALTS(bs1, rs) => {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   447
%          //distinctBy(flat_res, erase)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   448
%          distinctWith(flats(rs.map(bsimpStrong(_))), prune) match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   449
%            case Nil => AZERO
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   450
%            case s :: Nil => fuse(bs1, s)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   451
%            case rs => AALTS(bs1, rs)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   452
%          }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   453
%        }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   454
%        //stars that can be treated as 1
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   455
%        case ASTAR(bs, r0) if(atMostEmpty(erase(r0))) => AONE(bs)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   456
%        case r => r
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   457
%      }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   458
%    }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   459
%    def bdersStrong(s: List[Char], r: ARexp) : ARexp = s match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   460
%        case Nil => r
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   461
%        case c::s => bdersStrong(s, bsimpStrong(bder(c, r)))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   462
%      }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   463
%\end{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   464
%\caption{The function $\bsimpStrong$ and $\bdersStrongs$}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   465
%\end{figure}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   466
%\noindent
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   467
%$\distinctWith$, is in turn used in $\bsimpStrong$:
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   468
%\begin{figure}[H]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   469
%\begin{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   470
%      //Conjecture: [| bdersStrong(s, r) |] = O([| r |]^3)
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   471
%      def bdersStrong(s: List[Char], r: ARexp) : ARexp = s match {
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   472
%        case Nil => r
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   473
%        case c::s => bdersStrong(s, bsimpStrong(bder(c, r)))
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   474
%      }
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   475
%\end{lstlisting}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   476
%\caption{The function $\bsimpStrong$ and $\bdersStrongs$}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   477
%\end{figure}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   478
%\noindent
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   479
We conjecture that the above Scala function $\bdersStrongs$,
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   480
written $\bdersStrong{\_}{\_}$ as an infix notation, 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   481
satisfies the following property:
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   482
\begin{conjecture}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   483
	$\llbracket \bdersStrong{a}{s} \rrbracket = O(\llbracket a \rrbracket^3)$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   484
\end{conjecture}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   485
\noindent
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   486
The stronger version of $\blexersimp$'s
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   487
code in Scala looks like: 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   488
\begin{figure}[H]
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   489
\begin{lstlisting}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   490
      def strongBlexer(r: Rexp, s: String) : Option[Val] = {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   491
        Try(Some(decode(r, strong_blex_simp(internalise(r), s.toList)))).getOrElse(None)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   492
      }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   493
      def strong_blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   494
        case Nil => {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   495
          if (bnullable(r)) {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   496
            mkepsBC(r)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   497
          }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   498
          else
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   499
            throw new Exception("Not matched")
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   500
        }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   501
        case c::cs => {
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   502
          strong_blex_simp(strongBsimp(bder(c, r)), cs)
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   503
        }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   504
      }
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   505
\end{lstlisting}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   506
\end{figure}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   507
\noindent
621
Chengsong
parents: 620
diff changeset
   508
We call this lexer $\blexerStrong$.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   509
This version is able to drastically reduce the
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   510
internal data structure size which 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   511
otherwise could
621
Chengsong
parents: 620
diff changeset
   512
trigger exponential behaviours in
Chengsong
parents: 620
diff changeset
   513
$\blexersimp$.
Chengsong
parents: 620
diff changeset
   514
\begin{figure}[H]
Chengsong
parents: 620
diff changeset
   515
\centering
Chengsong
parents: 620
diff changeset
   516
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents: 620
diff changeset
   517
\begin{tikzpicture}
Chengsong
parents: 620
diff changeset
   518
\begin{axis}[
Chengsong
parents: 620
diff changeset
   519
    %xlabel={$n$},
Chengsong
parents: 620
diff changeset
   520
    myplotstyle,
Chengsong
parents: 620
diff changeset
   521
    xlabel={input length},
Chengsong
parents: 620
diff changeset
   522
    ylabel={size},
Chengsong
parents: 620
diff changeset
   523
    width = 7cm,
Chengsong
parents: 620
diff changeset
   524
    height = 5cm,
Chengsong
parents: 620
diff changeset
   525
    ]
Chengsong
parents: 620
diff changeset
   526
\addplot[red,mark=*, mark options={fill=white}] table {strongSimpCurve.data};
Chengsong
parents: 620
diff changeset
   527
\end{axis}
Chengsong
parents: 620
diff changeset
   528
\end{tikzpicture}
Chengsong
parents: 620
diff changeset
   529
  &
Chengsong
parents: 620
diff changeset
   530
\begin{tikzpicture}
Chengsong
parents: 620
diff changeset
   531
\begin{axis}[
Chengsong
parents: 620
diff changeset
   532
    %xlabel={$n$},
Chengsong
parents: 620
diff changeset
   533
    myplotstyle,
Chengsong
parents: 620
diff changeset
   534
    xlabel={input length},
Chengsong
parents: 620
diff changeset
   535
    ylabel={size},
Chengsong
parents: 620
diff changeset
   536
    width = 7cm,
Chengsong
parents: 620
diff changeset
   537
    height = 5cm,
Chengsong
parents: 620
diff changeset
   538
    ]
Chengsong
parents: 620
diff changeset
   539
\addplot[blue,mark=*, mark options={fill=white}] table {bsimpExponential.data};
Chengsong
parents: 620
diff changeset
   540
\end{axis}
Chengsong
parents: 620
diff changeset
   541
\end{tikzpicture}\\
Chengsong
parents: 620
diff changeset
   542
\multicolumn{2}{l}{}
Chengsong
parents: 620
diff changeset
   543
\end{tabular}    
Chengsong
parents: 620
diff changeset
   544
\caption{Runtime for matching 
Chengsong
parents: 620
diff changeset
   545
	$\protect((a^* + (aa)^* + \ldots + (aaaaa)^* )^*)^*$ with strings 
Chengsong
parents: 620
diff changeset
   546
	   of the form $\protect\underbrace{aa..a}_{n}$.}\label{fig:aaaaaStarStar}
Chengsong
parents: 620
diff changeset
   547
\end{figure}
Chengsong
parents: 620
diff changeset
   548
\noindent
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   549
We would like to preserve the correctness like the one 
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   550
we had for $\blexersimp$:
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   551
\begin{conjecture}\label{cubicConjecture}
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   552
	$\blexerStrong \;r \; s = \blexer\; r\;s$
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   553
\end{conjecture}
592
Chengsong
parents: 591
diff changeset
   554
\noindent
621
Chengsong
parents: 620
diff changeset
   555
The idea is to maintain key lemmas in
Chengsong
parents: 620
diff changeset
   556
chapter \ref{Bitcoded2} like
Chengsong
parents: 620
diff changeset
   557
$r \stackrel{*}{\rightsquigarrow} \textit{bsimp} \; r$
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   558
with the new rewriting rule 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   559
shown in figure \ref{fig:cubicRule} .
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   560
621
Chengsong
parents: 620
diff changeset
   561
In the next sub-section,
592
Chengsong
parents: 591
diff changeset
   562
we will describe why we 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   563
believe a cubic size bound can be achieved with
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   564
the stronger simplification.
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   565
For this we give a short introduction to the
592
Chengsong
parents: 591
diff changeset
   566
partial derivatives,
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   567
which were invented by Antimirov \cite{Antimirov95},
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   568
and then link them with the result of the function
592
Chengsong
parents: 591
diff changeset
   569
$\bdersStrongs$.
Chengsong
parents: 591
diff changeset
   570
 
621
Chengsong
parents: 620
diff changeset
   571
\subsection{Antimirov's partial derivatives}
Chengsong
parents: 620
diff changeset
   572
Partial derivatives were first introduced by 
Chengsong
parents: 620
diff changeset
   573
Antimirov \cite{Antimirov95}.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   574
Partial derivatives are very similar
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   575
to Brzozowski derivatives,
621
Chengsong
parents: 620
diff changeset
   576
but splits children of alternative regular expressions into 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   577
multiple independent terms. This means the output of
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   578
partial derivatives become a 
621
Chengsong
parents: 620
diff changeset
   579
set of regular expressions:
Chengsong
parents: 620
diff changeset
   580
\begin{center}  
Chengsong
parents: 620
diff changeset
   581
 	\begin{tabular}{lcl}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   582
		$\partial_x \; (r_1 \cdot r_2)$ & 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   583
		$\dn$ & $(\partial_x \; r_1) \cdot r_2 \cup
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   584
		\partial_x \; r_2 \; \textit{if} \; \; \nullable\; r_1$\\
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   585
		      & & $(\partial_x \; r_1)\cdot r_2 \quad\quad 
621
Chengsong
parents: 620
diff changeset
   586
		      \textit{otherwise}$\\ 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   587
		$\partial_x \; r^*$ & $\dn$ & $(\partial_x \; r) \cdot r^*$\\
621
Chengsong
parents: 620
diff changeset
   588
		$\partial_x \; c $ & $\dn$ & $\textit{if} \; x = c \; 
Chengsong
parents: 620
diff changeset
   589
		\textit{then} \;
Chengsong
parents: 620
diff changeset
   590
		\{ \ONE\} \;\;\textit{else} \; \varnothing$\\
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   591
		$\partial_x(r_1+r_2)$ & $=$ & $\partial_x(r_1) \cup \partial_x(r_2)$\\
621
Chengsong
parents: 620
diff changeset
   592
		$\partial_x(\ONE)$ & $=$ & $\varnothing$\\
Chengsong
parents: 620
diff changeset
   593
		$\partial_x(\ZERO)$ & $\dn$ & $\varnothing$\\
Chengsong
parents: 620
diff changeset
   594
	\end{tabular}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   595
\end{center}
621
Chengsong
parents: 620
diff changeset
   596
\noindent
Chengsong
parents: 620
diff changeset
   597
The $\cdot$ between for example 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   598
$(\partial_x \; r_1) \cdot r_2 $ 
621
Chengsong
parents: 620
diff changeset
   599
is a shorthand notation for the cartesian product 
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   600
$(\partial_x \; r_1) \times \{ r_2\}$.
621
Chengsong
parents: 620
diff changeset
   601
%Each element in the set generated by a partial derivative
Chengsong
parents: 620
diff changeset
   602
%corresponds to a (potentially partial) match
Chengsong
parents: 620
diff changeset
   603
%TODO: define derivatives w.r.t string s
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   604
Rather than joining the calculated derivatives $\partial_x r_1$ and $\partial_x r_2$ together
621
Chengsong
parents: 620
diff changeset
   605
using the $\sum$ constructor, Antimirov put them into
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   606
a set.  This means many subterms will be de-duplicated
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   607
because they are sets, 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   608
For example, to compute what regular expression $x^*(xx + y)^*$'s 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   609
derivative w.r.t. $x$ is, one can compute a partial derivative
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   610
and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   611
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
621
Chengsong
parents: 620
diff changeset
   612
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   613
The partial derivative w.r.t. a string is defined recursively:
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   614
\[
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   615
	\partial_{c::cs} r \dn \bigcup_{r'\in (\partial_c r)}
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   616
	\partial_{cs} r'
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   617
\]
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   618
Given an alphabet $\Sigma$, we denote the set of all possible strings
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   619
from this alphabet as $\Sigma^*$. 
621
Chengsong
parents: 620
diff changeset
   620
The set of all possible partial derivatives is defined
Chengsong
parents: 620
diff changeset
   621
as the union of derivatives w.r.t all the strings in the universe:
Chengsong
parents: 620
diff changeset
   622
\begin{center}
Chengsong
parents: 620
diff changeset
   623
	\begin{tabular}{lcl}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   624
		$\textit{PDER}_{\Sigma^*} \; r $ & $\dn $ & $\bigcup_{w \in \Sigma^*}\partial_w \; r$
621
Chengsong
parents: 620
diff changeset
   625
	\end{tabular}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   626
\end{center}
621
Chengsong
parents: 620
diff changeset
   627
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   628
Consider now again our pathological case where the derivatives
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   629
grow with a rather aggressive simplification
621
Chengsong
parents: 620
diff changeset
   630
\begin{center}
Chengsong
parents: 620
diff changeset
   631
	$((a^* + (aa)^* + \ldots + (\underbrace{a\ldots a}_{n a's})^* )^*)^*$
Chengsong
parents: 620
diff changeset
   632
\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   633
example, if we denote this regular expression as $r$,
621
Chengsong
parents: 620
diff changeset
   634
we have that
Chengsong
parents: 620
diff changeset
   635
\begin{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   636
$\textit{PDER}_{\Sigma^*} \; r = 
621
Chengsong
parents: 620
diff changeset
   637
\bigcup_{i=1}^{n}\bigcup_{j=0}^{i-1} \{ 
Chengsong
parents: 620
diff changeset
   638
	(\underbrace{a \ldots a}_{\text{j a's}}\cdot
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   639
(\underbrace{a \ldots a}_{\text{i a's}})^*)\cdot r \}$, 
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   640
\end{center}
621
Chengsong
parents: 620
diff changeset
   641
with exactly $n * (n + 1) / 2$ terms.
Chengsong
parents: 620
diff changeset
   642
This is in line with our speculation that only $n*(n+1)/2$ terms are
Chengsong
parents: 620
diff changeset
   643
needed. We conjecture that $\bsimpStrong$ is also able to achieve this
Chengsong
parents: 620
diff changeset
   644
upper limit in general
Chengsong
parents: 620
diff changeset
   645
\begin{conjecture}\label{bsimpStrongInclusionPder}
Chengsong
parents: 620
diff changeset
   646
	Using a suitable transformation $f$, we have 
Chengsong
parents: 620
diff changeset
   647
	\begin{center}
Chengsong
parents: 620
diff changeset
   648
		$\forall s.\; f \; (r \bdersStrong  \; s) \subseteq
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   649
		 \textit{PDER}_{\Sigma^*} \; r$
621
Chengsong
parents: 620
diff changeset
   650
	\end{center}
Chengsong
parents: 620
diff changeset
   651
\end{conjecture}
Chengsong
parents: 620
diff changeset
   652
\noindent
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   653
because our \ref{fig:cubicRule} will keep only one copy of each term,
621
Chengsong
parents: 620
diff changeset
   654
where the function $\textit{prune}$ takes care of maintaining
Chengsong
parents: 620
diff changeset
   655
a set like structure similar to partial derivatives.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   656
We might need to adjust $\textit{prune}$
621
Chengsong
parents: 620
diff changeset
   657
slightly to make sure all duplicate terms are eliminated,
Chengsong
parents: 620
diff changeset
   658
which should be doable.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   659
621
Chengsong
parents: 620
diff changeset
   660
Antimirov had proven that the sum of all the partial derivative 
Chengsong
parents: 620
diff changeset
   661
terms' sizes is bounded by the cubic of the size of that regular
Chengsong
parents: 620
diff changeset
   662
expression:
Chengsong
parents: 620
diff changeset
   663
\begin{property}\label{pderBound}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   664
	$\llbracket \textit{PDER}_{\Sigma^*} \; r \rrbracket \leq O(\llbracket r \rrbracket^3)$
621
Chengsong
parents: 620
diff changeset
   665
\end{property}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   666
This property was formalised by Wu et al. \cite{Wu2014}, and the 
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   667
details can be found in the archive of formal proofs. \footnote{https://www.isa-afp.org/entries/Myhill-Nerode.html}
621
Chengsong
parents: 620
diff changeset
   668
Once conjecture \ref{bsimpStrongInclusionPder} is proven, then property \ref{pderBound}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   669
would yield us also a cubic bound for our $\blexerStrong$ algorithm: 
621
Chengsong
parents: 620
diff changeset
   670
\begin{conjecture}\label{strongCubic}
Chengsong
parents: 620
diff changeset
   671
	$\llbracket r \bdersStrong\; s \rrbracket \leq \llbracket r \rrbracket^3$
Chengsong
parents: 620
diff changeset
   672
\end{conjecture}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   673
\noindent
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   674
We leave this as future work.
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   675
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   676
621
Chengsong
parents: 620
diff changeset
   677
%To get all the "atomic" components of a regular expression's possible derivatives,
Chengsong
parents: 620
diff changeset
   678
%there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
Chengsong
parents: 620
diff changeset
   679
%whatever character is available at the head of the string inside the language of a
Chengsong
parents: 620
diff changeset
   680
%regular expression, and gives back the character and the derivative regular expression
Chengsong
parents: 620
diff changeset
   681
%as a pair (which he called "monomial"):
Chengsong
parents: 620
diff changeset
   682
% \begin{center}  
Chengsong
parents: 620
diff changeset
   683
% \begin{tabular}{ccc}
Chengsong
parents: 620
diff changeset
   684
% $\lf(\ONE)$ & $=$ & $\phi$\\
Chengsong
parents: 620
diff changeset
   685
%$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
Chengsong
parents: 620
diff changeset
   686
% $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
Chengsong
parents: 620
diff changeset
   687
% $\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
Chengsong
parents: 620
diff changeset
   688
%\end{tabular}
Chengsong
parents: 620
diff changeset
   689
%\end{center}
Chengsong
parents: 620
diff changeset
   690
%%TODO: completion
Chengsong
parents: 620
diff changeset
   691
%
Chengsong
parents: 620
diff changeset
   692
%There is a slight difference in the last three clauses compared
Chengsong
parents: 620
diff changeset
   693
%with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular 
Chengsong
parents: 620
diff changeset
   694
%expression $r$ with every element inside $\textit{rset}$ to create a set of 
Chengsong
parents: 620
diff changeset
   695
%sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates 
Chengsong
parents: 620
diff changeset
   696
%on a set of monomials (which Antimirov called "linear form") and a regular 
Chengsong
parents: 620
diff changeset
   697
%expression, and returns a linear form:
Chengsong
parents: 620
diff changeset
   698
% \begin{center}  
Chengsong
parents: 620
diff changeset
   699
% \begin{tabular}{ccc}
Chengsong
parents: 620
diff changeset
   700
% $l \bigodot (\ZERO)$ & $=$ & $\phi$\\
Chengsong
parents: 620
diff changeset
   701
% $l \bigodot (\ONE)$ & $=$ & $l$\\
Chengsong
parents: 620
diff changeset
   702
% $\phi \bigodot t$ & $=$ & $\phi$\\
Chengsong
parents: 620
diff changeset
   703
% $\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
Chengsong
parents: 620
diff changeset
   704
% $\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
Chengsong
parents: 620
diff changeset
   705
%  $\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
Chengsong
parents: 620
diff changeset
   706
% $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
Chengsong
parents: 620
diff changeset
   707
% $\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
Chengsong
parents: 620
diff changeset
   708
%\end{tabular}
Chengsong
parents: 620
diff changeset
   709
%\end{center}
Chengsong
parents: 620
diff changeset
   710
%%TODO: completion
Chengsong
parents: 620
diff changeset
   711
%
Chengsong
parents: 620
diff changeset
   712
% Some degree of simplification is applied when doing $\bigodot$, for example,
Chengsong
parents: 620
diff changeset
   713
% $l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
Chengsong
parents: 620
diff changeset
   714
% and  $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
Chengsong
parents: 620
diff changeset
   715
%  $\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
Chengsong
parents: 620
diff changeset
   716
%  and so on.
Chengsong
parents: 620
diff changeset
   717
%  
Chengsong
parents: 620
diff changeset
   718
%  With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regular expression $r$ with 
Chengsong
parents: 620
diff changeset
   719
%  an iterative procedure:
Chengsong
parents: 620
diff changeset
   720
%   \begin{center}  
Chengsong
parents: 620
diff changeset
   721
% \begin{tabular}{llll}
Chengsong
parents: 620
diff changeset
   722
%$\textit{while}$ & $(\Delta_i \neq \phi)$  &                &          \\
Chengsong
parents: 620
diff changeset
   723
% 		       &  $\Delta_{i+1}$           &        $ =$ & $\lf(\Delta_i) - \PD_i$ \\
Chengsong
parents: 620
diff changeset
   724
%		       &  $\PD_{i+1}$              &         $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
Chengsong
parents: 620
diff changeset
   725
%$\partial_{UNIV}(r)$ & $=$ & $\PD$ &		     
Chengsong
parents: 620
diff changeset
   726
%\end{tabular}
Chengsong
parents: 620
diff changeset
   727
%\end{center}
Chengsong
parents: 620
diff changeset
   728
%
Chengsong
parents: 620
diff changeset
   729
%
Chengsong
parents: 620
diff changeset
   730
% $(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   731
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 590
diff changeset
   732
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   733
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   734
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   735
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   736
%	SECTION 2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   737
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   738
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   739
621
Chengsong
parents: 620
diff changeset
   740
%The closed form for them looks like:
Chengsong
parents: 620
diff changeset
   741
%%\begin{center}
Chengsong
parents: 620
diff changeset
   742
%%	\begin{tabular}{llrclll}
Chengsong
parents: 620
diff changeset
   743
%%		$r^{\{n+1\}}$ & $ \backslash_{rsimps}$ & $(c::s)$ & $=$ & & \\
Chengsong
parents: 620
diff changeset
   744
%%		$\textit{rsimp}$ & $($ & $
Chengsong
parents: 620
diff changeset
   745
%%		\sum \; ( $ & $\map$ & $(\textit{optermsimp}\;r)$ & $($\\
Chengsong
parents: 620
diff changeset
   746
%%			 & & & & $\textit{nupdates} \;$ & 
Chengsong
parents: 620
diff changeset
   747
%%			 $ s \; r_0 \; [ \textit{Some} \; ([c], n)]$\\
Chengsong
parents: 620
diff changeset
   748
%%			 & & & & $)$ &\\
Chengsong
parents: 620
diff changeset
   749
%%			 & &  $)$ & & &\\
Chengsong
parents: 620
diff changeset
   750
%%			 & $)$ & & & &\\
Chengsong
parents: 620
diff changeset
   751
%%	\end{tabular}
Chengsong
parents: 620
diff changeset
   752
%%\end{center}
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   753
%\begin{center}
621
Chengsong
parents: 620
diff changeset
   754
%	\begin{tabular}{llrcllrllll}
Chengsong
parents: 620
diff changeset
   755
%		$r^{\{n+1\}}$ & $ \backslash_{rsimps}$ & $(c::s)$ & $=$ & & &&&&\\
Chengsong
parents: 620
diff changeset
   756
%			      &&&&$\textit{rsimp}$ & $($ & $
Chengsong
parents: 620
diff changeset
   757
%			      \sum \; ( $ & $\map$ & $(\textit{optermsimp}\;r)$ & $($\\
Chengsong
parents: 620
diff changeset
   758
%					  &&&& & & & & $\;\; \textit{nupdates} \;$ & 
Chengsong
parents: 620
diff changeset
   759
%			 		  $ s \; r_0 \; [ \textit{Some} \; ([c], n)]$\\
Chengsong
parents: 620
diff changeset
   760
%					  &&&& & & & & $)$ &\\
Chengsong
parents: 620
diff changeset
   761
%					  &&&& & &  $)$ & & &\\
Chengsong
parents: 620
diff changeset
   762
%					  &&&& & $)$ & & & &\\
Chengsong
parents: 620
diff changeset
   763
%	\end{tabular}
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   764
%\end{center}
621
Chengsong
parents: 620
diff changeset
   765
%The $\textit{optermsimp}$ function with the argument $r$ 
Chengsong
parents: 620
diff changeset
   766
%chooses from two options: $\ZERO$ or 
Chengsong
parents: 620
diff changeset
   767
%We define for the $r^{\{n\}}$ constructor something similar to $\starupdate$
Chengsong
parents: 620
diff changeset
   768
%and $\starupdates$:
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   769
%\begin{center}
621
Chengsong
parents: 620
diff changeset
   770
%	\begin{tabular}{lcl}
Chengsong
parents: 620
diff changeset
   771
%		$\starupdate \; c \; r \; [] $ & $\dn$ & $[]$\\
Chengsong
parents: 620
diff changeset
   772
%		$\starupdate \; c \; r \; (s :: Ss)$ & $\dn$ & \\
Chengsong
parents: 620
diff changeset
   773
%						     & & $\textit{if} \; 
Chengsong
parents: 620
diff changeset
   774
%						     (\rnullable \; (\rders \; r \; s))$ \\
Chengsong
parents: 620
diff changeset
   775
%						     & & $\textit{then} \;\; (s @ [c]) :: [c] :: (
Chengsong
parents: 620
diff changeset
   776
%						     \starupdate \; c \; r \; Ss)$ \\
Chengsong
parents: 620
diff changeset
   777
%						     & & $\textit{else} \;\; (s @ [c]) :: (
Chengsong
parents: 620
diff changeset
   778
%						     \starupdate \; c \; r \; Ss)$
Chengsong
parents: 620
diff changeset
   779
%	\end{tabular}
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   780
%\end{center}
621
Chengsong
parents: 620
diff changeset
   781
%\noindent
Chengsong
parents: 620
diff changeset
   782
%As a generalisation from characters to strings,
Chengsong
parents: 620
diff changeset
   783
%$\starupdates$ takes a string instead of a character
Chengsong
parents: 620
diff changeset
   784
%as the first input argument, and is otherwise the same
Chengsong
parents: 620
diff changeset
   785
%as $\starupdate$.
Chengsong
parents: 620
diff changeset
   786
%\begin{center}
Chengsong
parents: 620
diff changeset
   787
%	\begin{tabular}{lcl}
Chengsong
parents: 620
diff changeset
   788
%		$\starupdates \; [] \; r \; Ss$ & $=$ & $Ss$\\
Chengsong
parents: 620
diff changeset
   789
%		$\starupdates \; (c :: cs) \; r \; Ss$ &  $=$ &  $\starupdates \; cs \; r \; (
Chengsong
parents: 620
diff changeset
   790
%		\starupdate \; c \; r \; Ss)$
Chengsong
parents: 620
diff changeset
   791
%	\end{tabular}
Chengsong
parents: 620
diff changeset
   792
%\end{center}
Chengsong
parents: 620
diff changeset
   793
%\noindent
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   794
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   795
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   796
621
Chengsong
parents: 620
diff changeset
   797
%\section{Zippers}
Chengsong
parents: 620
diff changeset
   798
%Zipper is a data structure designed to operate on 
Chengsong
parents: 620
diff changeset
   799
%and navigate between local parts of a tree.
Chengsong
parents: 620
diff changeset
   800
%It was first formally described by Huet \cite{HuetZipper}.
Chengsong
parents: 620
diff changeset
   801
%Typical applications of zippers involve text editor buffers
Chengsong
parents: 620
diff changeset
   802
%and proof system databases.
Chengsong
parents: 620
diff changeset
   803
%In our setting, the idea is to compactify the representation
Chengsong
parents: 620
diff changeset
   804
%of derivatives with zippers, thereby making our algorithm faster.
Chengsong
parents: 620
diff changeset
   805
%Some initial results 
Chengsong
parents: 620
diff changeset
   806
%We first give a brief introduction to what zippers are,
Chengsong
parents: 620
diff changeset
   807
%and other works
Chengsong
parents: 620
diff changeset
   808
%that apply zippers to derivatives
Chengsong
parents: 620
diff changeset
   809
%When dealing with large trees, it would be a waste to 
Chengsong
parents: 620
diff changeset
   810
%traverse the entire tree if
Chengsong
parents: 620
diff changeset
   811
%the operation only
Chengsong
parents: 620
diff changeset
   812
%involves a small fraction of it.
Chengsong
parents: 620
diff changeset
   813
%The idea is to put the focus on that subtree, turning other parts
Chengsong
parents: 620
diff changeset
   814
%of the tree into a context
Chengsong
parents: 620
diff changeset
   815
%
Chengsong
parents: 620
diff changeset
   816
%
Chengsong
parents: 620
diff changeset
   817
%One observation about our derivative-based lexing algorithm is that
Chengsong
parents: 620
diff changeset
   818
%the derivative operation sometimes traverses the entire regular expression
Chengsong
parents: 620
diff changeset
   819
%unnecessarily:
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   820
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   821
612
Chengsong
parents: 596
diff changeset
   822
%----------------------------------------------------------------------------------------
Chengsong
parents: 596
diff changeset
   823
%	SECTION 1
Chengsong
parents: 596
diff changeset
   824
%----------------------------------------------------------------------------------------
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   825
612
Chengsong
parents: 596
diff changeset
   826
%\section{Adding Support for the Negation Construct, and its Correctness Proof}
Chengsong
parents: 596
diff changeset
   827
%We now add support for the negation regular expression:
Chengsong
parents: 596
diff changeset
   828
%\[			r ::=   \ZERO \mid  \ONE
Chengsong
parents: 596
diff changeset
   829
%			 \mid  c  
Chengsong
parents: 596
diff changeset
   830
%			 \mid  r_1 \cdot r_2
Chengsong
parents: 596
diff changeset
   831
%			 \mid  r_1 + r_2   
Chengsong
parents: 596
diff changeset
   832
%			 \mid r^*    
Chengsong
parents: 596
diff changeset
   833
%			 \mid \sim r
Chengsong
parents: 596
diff changeset
   834
%\]
Chengsong
parents: 596
diff changeset
   835
%The $\textit{nullable}$ function's clause for it would be 
Chengsong
parents: 596
diff changeset
   836
%\[
Chengsong
parents: 596
diff changeset
   837
%\textit{nullable}(~r) = \neg \nullable(r)
Chengsong
parents: 596
diff changeset
   838
%\]
Chengsong
parents: 596
diff changeset
   839
%The derivative would be
Chengsong
parents: 596
diff changeset
   840
%\[
Chengsong
parents: 596
diff changeset
   841
%~r \backslash c = ~ (r \backslash c)
Chengsong
parents: 596
diff changeset
   842
%\]
Chengsong
parents: 596
diff changeset
   843
% 
Chengsong
parents: 596
diff changeset
   844
%The most tricky part of lexing for the $~r$ regular expression
Chengsong
parents: 596
diff changeset
   845
% is creating a value for it.
Chengsong
parents: 596
diff changeset
   846
% For other regular expressions, the value aligns with the 
Chengsong
parents: 596
diff changeset
   847
% structure of the regular expression:
Chengsong
parents: 596
diff changeset
   848
% \[
Chengsong
parents: 596
diff changeset
   849
% \vdash \Seq(\Char(a), \Char(b)) : a \cdot b
Chengsong
parents: 596
diff changeset
   850
% \]
Chengsong
parents: 596
diff changeset
   851
%But for the $~r$ regular expression, $s$ is a member of it if and only if
Chengsong
parents: 596
diff changeset
   852
%$s$ does not belong to $L(r)$. 
Chengsong
parents: 596
diff changeset
   853
%That means when there
Chengsong
parents: 596
diff changeset
   854
%is a match for the not regular expression, it is not possible to generate how the string $s$ matched
Chengsong
parents: 596
diff changeset
   855
%with $r$. 
Chengsong
parents: 596
diff changeset
   856
%What we can do is preserve the information of how $s$ was not matched by $r$,
Chengsong
parents: 596
diff changeset
   857
%and there are a number of options to do this.
Chengsong
parents: 596
diff changeset
   858
%
Chengsong
parents: 596
diff changeset
   859
%We could give a partial value when there is a partial match for the regular expression inside
Chengsong
parents: 596
diff changeset
   860
%the $\mathbf{not}$ construct.
Chengsong
parents: 596
diff changeset
   861
%For example, the string $ab$ is not in the language of $(a\cdot b) \cdot c$,
Chengsong
parents: 596
diff changeset
   862
%A value for it could be 
Chengsong
parents: 596
diff changeset
   863
% \[
Chengsong
parents: 596
diff changeset
   864
% \vdash \textit{Not}(\Seq(\Char(a), \Char(b))) : ~((a \cdot b ) \cdot c)
Chengsong
parents: 596
diff changeset
   865
% \]
Chengsong
parents: 596
diff changeset
   866
% The above example demonstrates what value to construct 
Chengsong
parents: 596
diff changeset
   867
% when the string $s$ is at most a real prefix
Chengsong
parents: 596
diff changeset
   868
% of the strings in $L(r)$. When $s$ instead is not a prefix of any strings
Chengsong
parents: 596
diff changeset
   869
% in $L(r)$, it becomes unclear what to return as a value inside the $\textit{Not}$
Chengsong
parents: 596
diff changeset
   870
% constructor.
Chengsong
parents: 596
diff changeset
   871
% 
Chengsong
parents: 596
diff changeset
   872
% Another option would be to either store the string $s$ that resulted in 
Chengsong
parents: 596
diff changeset
   873
% a mis-match for $r$ or a dummy value as a placeholder:
Chengsong
parents: 596
diff changeset
   874
% \[
Chengsong
parents: 596
diff changeset
   875
% \vdash \textit{Not}(abcd) : ~( r_1 )
Chengsong
parents: 596
diff changeset
   876
% \]
Chengsong
parents: 596
diff changeset
   877
%or
Chengsong
parents: 596
diff changeset
   878
% \[
Chengsong
parents: 596
diff changeset
   879
% \vdash \textit{Not}(\textit{Dummy}) : ~( r_1 )
Chengsong
parents: 596
diff changeset
   880
% \] 
Chengsong
parents: 596
diff changeset
   881
% We choose to implement this as it is most straightforward:
Chengsong
parents: 596
diff changeset
   882
% \[
Chengsong
parents: 596
diff changeset
   883
% \mkeps(~(r))  = \textit{if}(\nullable(r)) \; \textit{Error} \; \textit{else} \; \textit{Not}(\textit{Dummy})
Chengsong
parents: 596
diff changeset
   884
% \]
Chengsong
parents: 596
diff changeset
   885
% 
Chengsong
parents: 596
diff changeset
   886
%
620
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   887
%\begin{center}
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   888
%	\begin{tabular}{lcl}
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   889
%		$\ntset \; r \; (n+1) \; c::cs $ & $\dn$ & $\nupdates \;
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   890
%		cs \; r \; [\Some \; ([c], n)]$\\
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   891
%		$\ntset \; r\; 0 \; \_$ &  $\dn$ &  $\None$\\
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   892
%		$\ntset \; r \; \_ \; [] $ & $ \dn$ & $[]$\\
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   893
%	\end{tabular}
ae6010c14e49 chap6 almost done
Chengsong
parents: 613
diff changeset
   894
%\end{center}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 625
diff changeset
   895