etnms/etnms.tex
author Chengsong
Wed, 05 Feb 2020 11:27:51 +0000
changeset 117 0acf6b58236e
parent 116 dfcad6f19e06
child 118 c7825cfacc76
permissions -rw-r--r--
just incase
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
Chengsong
parents:
diff changeset
     1
\documentclass[a4paper,UKenglish]{lipics}
Chengsong
parents:
diff changeset
     2
\usepackage{graphic}
Chengsong
parents:
diff changeset
     3
\usepackage{data}
Chengsong
parents:
diff changeset
     4
\usepackage{tikz-cd}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
     5
\usepackage{tikz}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
     6
\usetikzlibrary{graphs}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
     7
\usetikzlibrary{graphdrawing}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
     8
\usegdlibrary{trees}
94
Chengsong
parents:
diff changeset
     9
%\usepackage{algorithm}
Chengsong
parents:
diff changeset
    10
\usepackage{amsmath}
110
Chengsong
parents: 109
diff changeset
    11
\usepackage{xcolor}
94
Chengsong
parents:
diff changeset
    12
\usepackage[noend]{algpseudocode}
Chengsong
parents:
diff changeset
    13
\usepackage{enumitem}
Chengsong
parents:
diff changeset
    14
\usepackage{nccmath}
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
    15
\usepackage{soul}
94
Chengsong
parents:
diff changeset
    16
Chengsong
parents:
diff changeset
    17
\definecolor{darkblue}{rgb}{0,0,0.6}
Chengsong
parents:
diff changeset
    18
\hypersetup{colorlinks=true,allcolors=darkblue}
Chengsong
parents:
diff changeset
    19
\newcommand{\comment}[1]%
Chengsong
parents:
diff changeset
    20
{{\color{red}$\Rightarrow$}\marginpar{\raggedright\small{\bf\color{red}#1}}}
Chengsong
parents:
diff changeset
    21
Chengsong
parents:
diff changeset
    22
% \documentclass{article}
Chengsong
parents:
diff changeset
    23
%\usepackage[utf8]{inputenc}
Chengsong
parents:
diff changeset
    24
%\usepackage[english]{babel}
Chengsong
parents:
diff changeset
    25
%\usepackage{listings}
Chengsong
parents:
diff changeset
    26
% \usepackage{amsthm}
Chengsong
parents:
diff changeset
    27
%\usepackage{hyperref}
Chengsong
parents:
diff changeset
    28
% \usepackage[margin=0.5in]{geometry}
Chengsong
parents:
diff changeset
    29
%\usepackage{pmboxdraw}
Chengsong
parents:
diff changeset
    30
 
Chengsong
parents:
diff changeset
    31
\title{POSIX Regular Expression Matching and Lexing}
Chengsong
parents:
diff changeset
    32
\author{Chengsong Tan}
Chengsong
parents:
diff changeset
    33
\affil{King's College London\\
Chengsong
parents:
diff changeset
    34
London, UK\\
Chengsong
parents:
diff changeset
    35
\texttt{chengsong.tan@kcl.ac.uk}}
Chengsong
parents:
diff changeset
    36
\authorrunning{Chengsong Tan}
Chengsong
parents:
diff changeset
    37
\Copyright{Chengsong Tan}
Chengsong
parents:
diff changeset
    38
Chengsong
parents:
diff changeset
    39
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Chengsong
parents:
diff changeset
    40
\newcommand{\ZERO}{\mbox{\bf 0}}
Chengsong
parents:
diff changeset
    41
\newcommand{\ONE}{\mbox{\bf 1}}
101
Chengsong
parents: 100
diff changeset
    42
\def\erase{\textit{erase}}
94
Chengsong
parents:
diff changeset
    43
\def\bders{\textit{bders}}
Chengsong
parents:
diff changeset
    44
\def\lexer{\mathit{lexer}}
Chengsong
parents:
diff changeset
    45
\def\blexer{\textit{blexer}}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
    46
\def\fuse{\textit{fuse}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
    47
\def\flatten{\textit{flatten}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
    48
\def\map{\textit{map}}
94
Chengsong
parents:
diff changeset
    49
\def\blexers{\mathit{blexer\_simp}}
95
Chengsong
parents: 94
diff changeset
    50
\def\simp{\mathit{simp}}
94
Chengsong
parents:
diff changeset
    51
\def\mkeps{\mathit{mkeps}}
Chengsong
parents:
diff changeset
    52
\def\bmkeps{\textit{bmkeps}}
Chengsong
parents:
diff changeset
    53
\def\inj{\mathit{inj}}
Chengsong
parents:
diff changeset
    54
\def\Empty{\mathit{Empty}}
Chengsong
parents:
diff changeset
    55
\def\Left{\mathit{Left}}
Chengsong
parents:
diff changeset
    56
\def\Right{\mathit{Right}}
Chengsong
parents:
diff changeset
    57
\def\Stars{\mathit{Stars}}
Chengsong
parents:
diff changeset
    58
\def\Char{\mathit{Char}}
Chengsong
parents:
diff changeset
    59
\def\Seq{\mathit{Seq}}
Chengsong
parents:
diff changeset
    60
\def\Der{\mathit{Der}}
Chengsong
parents:
diff changeset
    61
\def\nullable{\mathit{nullable}}
Chengsong
parents:
diff changeset
    62
\def\Z{\mathit{Z}}
Chengsong
parents:
diff changeset
    63
\def\S{\mathit{S}}
Chengsong
parents:
diff changeset
    64
\def\flex{\textit{flex}}
Chengsong
parents:
diff changeset
    65
\def\rup{r^\uparrow}
Chengsong
parents:
diff changeset
    66
\def\retrieve{\textit{retrieve}}
Chengsong
parents:
diff changeset
    67
\def\AALTS{\textit{AALTS}}
Chengsong
parents:
diff changeset
    68
\def\AONE{\textit{AONE}}
Chengsong
parents:
diff changeset
    69
%\theoremstyle{theorem}
Chengsong
parents:
diff changeset
    70
%\newtheorem{theorem}{Theorem}
Chengsong
parents:
diff changeset
    71
%\theoremstyle{lemma}
Chengsong
parents:
diff changeset
    72
%\newtheorem{lemma}{Lemma}
Chengsong
parents:
diff changeset
    73
%\newcommand{\lemmaautorefname}{Lemma}
Chengsong
parents:
diff changeset
    74
%\theoremstyle{definition}
Chengsong
parents:
diff changeset
    75
%\newtheorem{definition}{Definition}
Chengsong
parents:
diff changeset
    76
\algnewcommand\algorithmicswitch{\textbf{switch}}
Chengsong
parents:
diff changeset
    77
\algnewcommand\algorithmiccase{\textbf{case}}
Chengsong
parents:
diff changeset
    78
\algnewcommand\algorithmicassert{\texttt{assert}}
Chengsong
parents:
diff changeset
    79
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
Chengsong
parents:
diff changeset
    80
% New "environments"
Chengsong
parents:
diff changeset
    81
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
Chengsong
parents:
diff changeset
    82
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
Chengsong
parents:
diff changeset
    83
\algtext*{EndSwitch}%
Chengsong
parents:
diff changeset
    84
\algtext*{EndCase}%
Chengsong
parents:
diff changeset
    85
Chengsong
parents:
diff changeset
    86
Chengsong
parents:
diff changeset
    87
\begin{document}
Chengsong
parents:
diff changeset
    88
Chengsong
parents:
diff changeset
    89
\maketitle
Chengsong
parents:
diff changeset
    90
Chengsong
parents:
diff changeset
    91
\begin{abstract}
Chengsong
parents:
diff changeset
    92
  Brzozowski introduced in 1964 a beautifully simple algorithm for
Chengsong
parents:
diff changeset
    93
  regular expression matching based on the notion of derivatives of
Chengsong
parents:
diff changeset
    94
  regular expressions. In 2014, Sulzmann and Lu extended this
Chengsong
parents:
diff changeset
    95
  algorithm to not just give a YES/NO answer for whether or not a
Chengsong
parents:
diff changeset
    96
  regular expression matches a string, but in case it does also
Chengsong
parents:
diff changeset
    97
  answers with \emph{how} it matches the string.  This is important for
Chengsong
parents:
diff changeset
    98
  applications such as lexing (tokenising a string). The problem is to
Chengsong
parents:
diff changeset
    99
  make the algorithm by Sulzmann and Lu fast on all inputs without
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   100
  breaking its correctness. Being fast depends on a complete set of 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   101
  simplification rules, some of which 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   102
  have been put forward by Sulzmann and Lu. We have extended their
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   103
  rules in order to obtain a tight bound on the size of regular expressions.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   104
  We have tested these extended rules, but have not 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   105
  formally established their correctness. We have also not yet looked 
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   106
  at extended regular expressions, such as bounded repetitions,
94
Chengsong
parents:
diff changeset
   107
  negation and back-references.
Chengsong
parents:
diff changeset
   108
\end{abstract}
Chengsong
parents:
diff changeset
   109
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   110
\section{Recapitulation of Concepts From the Last Report}
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   111
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   112
\subsection*{Regular Expressions and Derivatives}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   113
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   114
Suppose (basic) regular expressions are given by the following grammar:
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   115
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   116
\[			r ::=   \ZERO \mid  \ONE
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   117
			 \mid  c  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   118
			 \mid  r_1 \cdot r_2
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   119
			 \mid  r_1 + r_2   
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   120
			 \mid r^*         
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   121
\]
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   122
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   123
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   124
The ingenious contribution of Brzozowski is the notion of \emph{derivatives} of
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   125
regular expressions, written~$\_ \backslash \_$. It uses the auxiliary notion of
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   126
$\nullable$ defined below.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   127
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   128
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   129
		\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   130
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   131
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   132
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   133
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   134
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   135
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   136
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   137
	\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   138
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   139
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   140
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   141
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   142
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   143
		$d \backslash c$     & $\dn$ & 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   144
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   145
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   146
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   147
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   148
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   149
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   150
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   151
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   152
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   153
%Assuming the classic notion of a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   154
%\emph{language} of a regular expression, written $L(\_)$, t
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   155
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   156
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   157
The main property of the derivative operation is that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   158
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   159
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   160
$c\!::\!s \in L(r)$ holds
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   161
if and only if $s \in L(r\backslash c)$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   162
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   163
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   164
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   165
We can generalise the derivative operation shown above for single characters
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   166
to strings as follows:
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   167
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   168
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   169
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   170
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   171
$r \backslash [\,] $ & $\dn$ & $r$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   172
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   173
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   174
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   175
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   176
and then define Brzozowski's  regular-expression matching algorithm as:
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   177
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   178
\[
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   179
match\;s\;r \;\dn\; nullable(r\backslash s)
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   180
\]
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   181
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   182
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   183
Assuming the a string is givane as a sequence of characters, say $c_0c_1..c_n$, 
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   184
this algorithm presented graphically is as follows:
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   185
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   186
\begin{equation}\label{graph:*}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   187
\begin{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   188
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   189
\end{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   190
\end{equation}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   191
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   192
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   193
where we start with  a regular expression  $r_0$, build successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   194
derivatives until we exhaust the string and then use \textit{nullable}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   195
to test whether the result can match the empty string. It can  be
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   196
relatively  easily shown that this matcher is correct  (that is given
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   197
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   198
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   199
 
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   200
\subsection*{Values and the Lexing Algorithm by Sulzmann and Lu}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   201
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   202
One limitation of Brzozowski's algorithm is that it only produces a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   203
YES/NO answer for whether a string is being matched by a regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   204
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   205
to allow generation of an actual matching, called a \emph{value} or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   206
sometimes also \emph{lexical value}.  These values and regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   207
expressions correspond to each other as illustrated in the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   208
table:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   209
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   210
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   211
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   212
	\begin{tabular}{c@{\hspace{20mm}}c}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   213
		\begin{tabular}{@{}rrl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   214
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   215
			$r$ & $::=$  & $\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   216
			& $\mid$ & $\ONE$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   217
			& $\mid$ & $c$          \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   218
			& $\mid$ & $r_1 \cdot r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   219
			& $\mid$ & $r_1 + r_2$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   220
			\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   221
			& $\mid$ & $r^*$         \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   222
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   223
		&
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   224
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   225
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   226
			$v$ & $::=$  & \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   227
			&        & $\Empty$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   228
			& $\mid$ & $\Char(c)$          \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   229
			& $\mid$ & $\Seq\,v_1\, v_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   230
			& $\mid$ & $\Left(v)$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   231
			& $\mid$ & $\Right(v)$  \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   232
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   233
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   234
	\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   235
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   236
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   237
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   238
The contribution of Sulzmann and Lu is an extension of Brzozowski's
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   239
algorithm by a second phase (the first phase being building successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   240
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   241
is generated in case the regular expression matches  the string. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   242
Pictorially, the Sulzmann and Lu algorithm is as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   243
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   244
\begin{ceqn}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   245
\begin{equation}\label{graph:2}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   246
\begin{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   247
r_0 \arrow[r, "\backslash c_0"]  \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   248
v_0           & v_1 \arrow[l,"inj_{r_0} c_0"]                & v_2 \arrow[l, "inj_{r_1} c_1"]              & v_n \arrow[l, dashed]         
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   249
\end{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   250
\end{equation}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   251
\end{ceqn}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   252
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   253
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   254
For convenience, we shall employ the following notations: the regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   255
expression we start with is $r_0$, and the given string $s$ is composed
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   256
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   257
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   258
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   259
the derivative $r_n$. We test whether this derivative is
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   260
$\textit{nullable}$ or not. If not, we know the string does not match
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   261
$r$ and no value needs to be generated. If yes, we start building the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   262
values incrementally by \emph{injecting} back the characters into the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   263
earlier values $v_n, \ldots, v_0$. This is the second phase of the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   264
algorithm from the right to left. For the first value $v_n$, we call the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   265
function $\textit{mkeps}$, which builds the lexical value
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   266
for how the empty string has been matched by the (nullable) regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   267
expression $r_n$. This function is defined as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   268
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   269
	\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   270
		\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   271
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   272
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   273
			& \textit{if} $\nullable(r_{1})$\\ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   274
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   275
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   276
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   277
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   278
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   279
	\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   280
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   281
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   282
\noindent 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   283
After the $\mkeps$-call, we inject back the characters one by one in order to build
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   284
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   285
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   286
After injecting back $n$ characters, we get the lexical value for how $r_0$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   287
matches $s$. For this Sulzmann and Lu defined a function that reverses
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   288
the ``chopping off'' of characters during the derivative phase. The
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   289
corresponding function is called \emph{injection}, written
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   290
$\textit{inj}$; it takes three arguments: the first one is a regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   291
expression ${r_{i-1}}$, before the character is chopped off, the second
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   292
is a character ${c_{i-1}}$, the character we want to inject and the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   293
third argument is the value ${v_i}$, into which one wants to inject the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   294
character (it corresponds to the regular expression after the character
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   295
has been chopped off). The result of this function is a new value. The
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   296
definition of $\textit{inj}$ is as follows: 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   297
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   298
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   299
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   300
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   301
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   302
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   303
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   304
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   305
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   306
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   307
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   308
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   309
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   310
\noindent This definition is by recursion on the ``shape'' of regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   311
expressions and values. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   312
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   313
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   314
\subsection*{Simplification of Regular Expressions}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   315
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   316
The main drawback of building successive derivatives according
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   317
to Brzozowski's definition is that they can grow very quickly in size.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   318
This is mainly due to the fact that the derivative operation generates
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   319
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result, if
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   320
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   321
are excruciatingly slow. For example when starting with the regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   322
expression $(a + aa)^*$ and building 12 successive derivatives
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   323
w.r.t.~the character $a$, one obtains a derivative regular expression
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   324
with more than 8000 nodes (when viewed as a tree). Operations like
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   325
$\textit{der}$ and $\nullable$ need to traverse such trees and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   326
consequently the bigger the size of the derivative the slower the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   327
algorithm. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   328
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   329
Fortunately, one can simplify regular expressions after each derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   330
step. Various simplifications of regular expressions are possible, such
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   331
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   332
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   333
affect the answer for whether a regular expression matches a string or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   334
not, but fortunately also do not affect the POSIX strategy of how
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   335
regular expressions match strings---although the latter is much harder
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   336
to establish. Some initial results in this regard have been
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   337
obtained in \cite{AusafDyckhoffUrban2016}. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   338
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   339
If we want the size of derivatives in Sulzmann and Lu's algorithm to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   340
stay below this bound, we would need more aggressive simplifications.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   341
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   342
deleting duplicates whenever possible. For example, the parentheses in
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   343
$(a+b) \cdot c + b\cdot c$ can be opened up to get $a\cdot c + b \cdot c + b
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   344
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   345
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   346
$a^*+a+\ONE$. Adding these more aggressive simplification rules help us
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   347
to achieve the same size bound as that of the partial derivatives. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   348
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   349
In order to implement the idea of ``spilling out alternatives'' and to
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   350
make them compatible with the $\textit{inj}$-mechanism, we use
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   351
\emph{bitcodes}. They were first introduced by Sulzmann and Lu.
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   352
Here bits and bitcodes (lists of bits) are defined as:
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   353
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   354
\begin{center}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   355
		$b ::=   1 \mid  0 \qquad
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   356
bs ::= [] \mid b:bs    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   357
$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   358
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   359
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   360
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   361
The $1$ and $0$ are not in bold in order to avoid 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   362
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   363
bit-lists) can be used to encode values (or incomplete values) in a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   364
compact form. This can be straightforwardly seen in the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   365
coding function from values to bitcodes: 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   366
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   367
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   368
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   369
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   370
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   371
  $\textit{code}(\Left\,v)$ & $\dn$ & $0 :: code(v)$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   372
  $\textit{code}(\Right\,v)$ & $\dn$ & $1 :: code(v)$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   373
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   374
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[0]$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   375
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $1 :: code(v) \;@\;
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   376
                                                 code(\Stars\,vs)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   377
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   378
\end{center} 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   379
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   380
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   381
Here $\textit{code}$ encodes a value into a bitcodes by converting
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   382
$\Left$ into $0$, $\Right$ into $1$, and marks the start of a non-empty
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   383
star iteration by $1$. The border where a local star terminates
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   384
is marked by $0$. This coding is lossy, as it throws away the information about
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   385
characters, and also does not encode the ``boundary'' between two
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   386
sequence values. Moreover, with only the bitcode we cannot even tell
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   387
whether the $1$s and $0$s are for $\Left/\Right$ or $\Stars$. The
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   388
reason for choosing this compact way of storing information is that the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   389
relatively small size of bits can be easily manipulated and ``moved
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   390
around'' in a regular expression. In order to recover values, we will 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   391
need the corresponding regular expression as an extra information. This
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   392
means the decoding function is defined as:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   393
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   394
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   395
%\begin{definition}[Bitdecoding of Values]\mbox{}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   396
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   397
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   398
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   399
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   400
  $\textit{decode}'\,(0\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   401
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   402
       (\Left\,v, bs_1)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   403
  $\textit{decode}'\,(1\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   404
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   405
       (\Right\,v, bs_1)$\\                           
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   406
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   407
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   408
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   409
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   410
  $\textit{decode}'\,(0\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   411
  $\textit{decode}'\,(1\!::\!bs)\,(r^*)$ & $\dn$ & 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   412
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   413
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   414
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   415
  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   416
  $\textit{decode}\,bs\,r$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   417
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   418
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   419
       \textit{else}\;\textit{None}$                       
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   420
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   421
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   422
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   423
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   424
Sulzmann and Lu's integrated the bitcodes into regular expressions to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   425
create annotated regular expressions \cite{Sulzmann2014}.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   426
\emph{Annotated regular expressions} are defined by the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   427
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   428
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   429
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   430
\begin{tabular}{lcl}
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   431
  $\textit{a}$ & $::=$  & $\ZERO$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   432
                  & $\mid$ & $_{bs}\ONE$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   433
                  & $\mid$ & $_{bs}{\bf c}$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   434
                  & $\mid$ & $_{bs}\oplus\,as$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   435
                  & $\mid$ & $_{bs}a_1\cdot a_2$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   436
                  & $\mid$ & $_{bs}a^*$
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   437
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   438
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   439
%(in \textit{ALTS})
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   440
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   441
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   442
where $bs$ stands for bitcodes, $a$  for $\bold{a}$nnotated regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   443
expressions and $as$ for a list of annotated regular expressions.
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   444
The alternative constructor($\oplus$) has been generalized to 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   445
accept a list of annotated regular expressions rather than just 2.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   446
We will show that these bitcodes encode information about
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   447
the (POSIX) value that should be generated by the Sulzmann and Lu
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   448
algorithm.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   449
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   450
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   451
To do lexing using annotated regular expressions, we shall first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   452
transform the usual (un-annotated) regular expressions into annotated
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   453
regular expressions. This operation is called \emph{internalisation} and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   454
defined as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   455
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   456
%\begin{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   457
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   458
\begin{tabular}{lcl}
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   459
  $(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   460
  $(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   461
  $(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   462
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   463
  $_{[]}\oplus[\textit{fuse}\,[0]\,r_1^\uparrow,\,
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   464
  \textit{fuse}\,[1]\,r_2^\uparrow]$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   465
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   466
         $_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   467
  $(r^*)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   468
         $_{[]}(r^\uparrow)^*$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   469
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   470
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   471
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   472
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   473
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   474
We use up arrows here to indicate that the basic un-annotated regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   475
expressions are ``lifted up'' into something slightly more complex. In the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   476
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   477
attach bits to the front of an annotated regular expression. Its
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   478
definition is as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   479
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   480
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   481
\begin{tabular}{lcl}
117
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   482
  $\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   483
  $\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   484
     $_{bs @ bs'}\ONE$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   485
  $\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   486
     $_{bs@bs'}{\bf c}$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   487
  $\textit{fuse}\;bs\,_{bs'}\oplus\textit{as}$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   488
     $_{bs@bs'}\oplus\textit{as}$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   489
  $\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   490
     $_{bs@bs'}a_1 \cdot a_2$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   491
  $\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   492
     $_{bs @ bs'}a^*$
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   493
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   494
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   495
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   496
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   497
After internalising the regular expression, we perform successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   498
derivative operations on the annotated regular expressions. This
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   499
derivative operation is the same as what we had previously for the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   500
basic regular expressions, except that we beed to take care of
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   501
the bitcodes:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   502
109
Chengsong
parents: 108
diff changeset
   503
Chengsong
parents: 108
diff changeset
   504
\iffalse
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   505
 %\begin{definition}{bder}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   506
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   507
  \begin{tabular}{@{}lcl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   508
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   509
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   510
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   511
        $\textit{if}\;c=d\; \;\textit{then}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   512
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   513
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   514
  $\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   515
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   516
     $\textit{if}\;\textit{bnullable}\,a_1$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   517
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   518
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   519
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   520
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   521
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   522
       (\textit{STAR}\,[]\,r)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   523
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   524
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   525
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   526
109
Chengsong
parents: 108
diff changeset
   527
\begin{center}
Chengsong
parents: 108
diff changeset
   528
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 108
diff changeset
   529
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 108
diff changeset
   530
  $(_{bs}\textit{ONE})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 108
diff changeset
   531
  $(_{bs}\textit{CHAR}\;d)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   532
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 108
diff changeset
   533
         _{bs}\textit{ONE}\;\textit{else}\;\textit{ZERO}$\\  
Chengsong
parents: 108
diff changeset
   534
  $(_{bs}\textit{ALTS}\;as)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   535
  $_{bs}\textit{ALTS}\;(as.map(\backslash c))$\\
Chengsong
parents: 108
diff changeset
   536
  $(_{bs}\textit{SEQ}\;a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   537
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 108
diff changeset
   538
					       & &$\textit{then}\;_{bs}\textit{ALTS}\,List((_{[]}\textit{SEQ}\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents: 108
diff changeset
   539
					       & &$\phantom{\textit{then}\;_{bs}\textit{ALTS}\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents: 108
diff changeset
   540
  & &$\textit{else}\;_{bs}\textit{SEQ}\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents: 108
diff changeset
   541
  $(_{bs}\textit{STAR}\,a)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   542
      $_{bs}\textit{SEQ}\;(\textit{fuse}\, [\Z] (r\,\backslash c))\,
Chengsong
parents: 108
diff changeset
   543
       (_{bs}\textit{STAR}\,[]\,r)$
Chengsong
parents: 108
diff changeset
   544
\end{tabular}    
Chengsong
parents: 108
diff changeset
   545
\end{center}    
Chengsong
parents: 108
diff changeset
   546
%\end{definition}
Chengsong
parents: 108
diff changeset
   547
\fi
Chengsong
parents: 108
diff changeset
   548
Chengsong
parents: 108
diff changeset
   549
\begin{center}
Chengsong
parents: 108
diff changeset
   550
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 108
diff changeset
   551
  $(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 108
diff changeset
   552
  $(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 108
diff changeset
   553
  $(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   554
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 108
diff changeset
   555
         _{bs}\ONE\;\textit{else}\;\ZERO$\\  
Chengsong
parents: 108
diff changeset
   556
  $(_{bs}\oplus \;as)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   557
  $_{bs}\oplus\;(as.map(\backslash c))$\\
Chengsong
parents: 108
diff changeset
   558
  $(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   559
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 108
diff changeset
   560
					       & &$\textit{then}\;_{bs}\oplus\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
Chengsong
parents: 108
diff changeset
   561
					       & &$\phantom{\textit{then},\;_{bs}\oplus\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
Chengsong
parents: 108
diff changeset
   562
  & &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
Chengsong
parents: 108
diff changeset
   563
  $(_{bs}a^*)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   564
      $_{bs}\;((\textit{fuse}\, [\Z] (r\,\backslash c))\cdot
Chengsong
parents: 108
diff changeset
   565
       (_{[]}r^*))$
Chengsong
parents: 108
diff changeset
   566
\end{tabular}    
Chengsong
parents: 108
diff changeset
   567
\end{center}    
Chengsong
parents: 108
diff changeset
   568
Chengsong
parents: 108
diff changeset
   569
%\end{definition}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   570
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   571
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   572
we need to attach an additional bit $Z$ to the front of $r \backslash c$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   573
to indicate that there is one more star iteration. Also the $SEQ$ clause
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   574
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   575
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   576
that it is for annotated regular expressions, therefore we omit the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   577
definition). Assume that $bmkeps$ correctly extracts the bitcode for how
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   578
$a_1$ matches the string prior to character $c$ (more on this later),
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   579
then the right branch of $ALTS$, which is $fuse \; bmkeps \;  a_1 (a_2
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   580
\backslash c)$ will collapse the regular expression $a_1$(as it has
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   581
already been fully matched) and store the parsing information at the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   582
head of the regular expression $a_2 \backslash c$ by fusing to it. The
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   583
bitsequence $bs$, which was initially attached to the head of $SEQ$, has
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   584
now been elevated to the top-level of $ALTS$, as this information will be
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   585
needed whichever way the $SEQ$ is matched---no matter whether $c$ belongs
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   586
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   587
the lexing information, we complete the lexing by collecting the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   588
bitcodes using a generalised version of the $\textit{mkeps}$ function
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   589
for annotated regular expressions, called $\textit{bmkeps}$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   590
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   591
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   592
%\begin{definition}[\textit{bmkeps}]\mbox{}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   593
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   594
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   595
  $\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   596
  $\textit{bmkeps}\,(\textit{ALTS}\;bs\,a::as)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   597
     $\textit{if}\;\textit{bnullable}\,a$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   598
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   599
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(\textit{ALTS}\;bs\,as)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   600
  $\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   601
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   602
  $\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   603
     $bs \,@\, [\S]$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   604
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   605
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   606
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   607
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   608
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   609
This function completes the value information by travelling along the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   610
path of the regular expression that corresponds to a POSIX value and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   611
collecting all the bitcodes, and using $S$ to indicate the end of star
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   612
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   613
decode them, we get the value we expect. The corresponding lexing
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   614
algorithm looks as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   615
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   616
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   617
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   618
  $\textit{blexer}\;r\,s$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   619
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   620
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   621
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   622
  & & $\;\;\textit{else}\;\textit{None}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   623
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   624
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   625
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   626
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   627
In this definition $\_\backslash s$ is the  generalisation  of the derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   628
operation from characters to strings (just like the derivatives for un-annotated
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   629
regular expressions).
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   630
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   631
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   632
\subsection*{Our Simplification Rules}
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   633
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   634
The main point of the bitcodes and annotated regular expressions is that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   635
we can apply rather aggressive (in terms of size) simplification rules
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   636
in order to keep derivatives small. We have developed such
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   637
``aggressive'' simplification rules and generated test data that show
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   638
that the expected bound can be achieved. Obviously we could only
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   639
partially cover  the search space as there are infinitely many regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   640
expressions and strings. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   641
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   642
One modification we introduced is to allow a list of annotated regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   643
expressions in the \textit{ALTS} constructor. This allows us to not just
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   644
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   645
also unnecessary ``copies'' of regular expressions (very similar to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   646
simplifying $r + r$ to just $r$, but in a more general setting). Another
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   647
modification is that we use simplification rules inspired by Antimirov's
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   648
work on partial derivatives. They maintain the idea that only the first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   649
``copy'' of a regular expression in an alternative contributes to the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   650
calculation of a POSIX value. All subsequent copies can be pruned away from
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   651
the regular expression. A recursive definition of our  simplification function 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   652
that looks somewhat similar to our Scala code is given below:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   653
%\comment{Use $\ZERO$, $\ONE$ and so on. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   654
%Is it $ALTS$ or $ALTS$?}\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   655
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   656
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   657
  \begin{tabular}{@{}lcl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   658
   
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   659
  $\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   660
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   661
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   662
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   663
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   664
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   665
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   666
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   667
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   668
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   669
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   670
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   671
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   672
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   673
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   674
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   675
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   676
The simplification does a pattern matching on the regular expression.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   677
When it detected that the regular expression is an alternative or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   678
sequence, it will try to simplify its children regular expressions
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   679
recursively and then see if one of the children turn into $\ZERO$ or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   680
$\ONE$, which might trigger further simplification at the current level.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   681
The most involved part is the $\textit{ALTS}$ clause, where we use two
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   682
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   683
$\textit{ALTS}$ and reduce as many duplicates as possible. Function
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   684
$\textit{distinct}$  keeps the first occurring copy only and remove all later ones
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   685
when detected duplicates. Function $\textit{flatten}$ opens up nested \textit{ALTS}.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   686
Its recursive definition is given below:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   687
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   688
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   689
  \begin{tabular}{@{}lcl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   690
  $\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   691
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   692
  $\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \;  as' $ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   693
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise) 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   694
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   695
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   696
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   697
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   698
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   699
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   700
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   701
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   702
Suppose we apply simplification after each derivative step, and view
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   703
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   704
\textit{simp}(a \backslash c)$. Then we can use the previous natural
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   705
extension from derivative w.r.t.~character to derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   706
w.r.t.~string:%\comment{simp in  the [] case?}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   707
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   708
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   709
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   710
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   711
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   712
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   713
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   714
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   715
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   716
we obtain an optimised version of the algorithm:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   717
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   718
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   719
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   720
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   721
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   722
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   723
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   724
  & & $\;\;\textit{else}\;\textit{None}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   725
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   726
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   727
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   728
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   729
This algorithm keeps the regular expression size small, for example,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   730
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   731
will be reduced to just 6 and stays constant, no matter how long the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   732
input string is.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   733
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   734
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   735
94
Chengsong
parents:
diff changeset
   736
\section{Introduction}
Chengsong
parents:
diff changeset
   737
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   738
While we believe derivatives of regular expressions, written
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   739
$r\backslash s$, are a beautiful concept (in terms of ease of
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   740
implementing them in functional programming languages and in terms of
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   741
reasoning about them formally), they have one major drawback: every
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   742
derivative step can make regular expressions grow drastically in
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   743
size. This in turn has negative effect on the runtime of the
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   744
corresponding lexing algorithms. Consider for example the regular
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   745
expression $(a+aa)^*$ and the short string $aaaaaaaaaaaa$. The
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   746
corresponding derivative contains already 8668 nodes where we assume
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   747
the derivative is given as a tree. The reason for the poor runtime of
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   748
the derivative-based lexing algorithms is that they need to traverse
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   749
such trees over and over again. The solution is to find a complete set
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   750
of simplification rules that keep the sizes of derivatives uniformly
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   751
small.
94
Chengsong
parents:
diff changeset
   752
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   753
For reasons beyond this report, it turns out that a complete set of
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   754
simplification rules depends on values being encoded as
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   755
bitsequences.\footnote{Values are the results the lexing algorithms
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   756
  generate; they encode how a regular expression matched a string.} We
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   757
already know that the lexing algorithm using bitsequences but
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   758
\emph{without} simplification is correct, albeilt horribly
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   759
slow. Therefore in the past 6 months I was trying to prove that the
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   760
algorithm using bitsequences plus our simplification rules is
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   761
also correct. Formally this amounts to show that
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   762
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   763
\begin{equation}\label{mainthm}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   764
\blexers \; r \; s = \blexer \;r\;s
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   765
\end{equation}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   766
94
Chengsong
parents:
diff changeset
   767
\noindent
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   768
whereby $\blexers$ simplifies (makes derivatives smaller) in each
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   769
step, whereas with $\blexer$ the size can grow exponentially. This
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   770
would be an important milestone for my thesis, because we already
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   771
have a very good idea how to establish that our set our simplification
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   772
rules keeps the size of derivativs below a relatively tight bound.
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   773
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   774
In order to prove the main theorem in \eqref{mainthm}, we need to prove that the
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   775
two functions produce the same output. The definition of these two  functions 
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   776
is shown below.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   777
94
Chengsong
parents:
diff changeset
   778
\begin{center}
Chengsong
parents:
diff changeset
   779
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   780
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
   781
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
   782
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
   783
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
   784
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
   785
\end{tabular}
Chengsong
parents:
diff changeset
   786
\end{center}
Chengsong
parents:
diff changeset
   787
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   788
\begin{center}
94
Chengsong
parents:
diff changeset
   789
\begin{tabular}{lcl}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   790
  $\blexers \; r \, s$ &$\dn$ &
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   791
    $\textit{let} \; a = (r^\uparrow)\backslash_{simp}\, s\; \textit{in}$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   792
  & & $\; \; \textit{if} \; \textit{bnullable}(a)$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   793
  & & $\; \; \textit{then} \; \textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   794
  & & $\;\;   \textit{else}\;\textit{None}$
94
Chengsong
parents:
diff changeset
   795
\end{tabular}
Chengsong
parents:
diff changeset
   796
\end{center}
Chengsong
parents:
diff changeset
   797
\noindent
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   798
In these definitions $(r^\uparrow)$ is a kind of coding function that
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   799
is the same in each case, similarly the decode and the \textit{bmkeps}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   800
are functions that are the same in each case. Our main
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   801
theorem~\eqref{mainthm} therefore boils down to proving the following
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   802
two propositions (depending on which branch the if-else clause
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   803
takes). They establish how the derivatives \emph{with} simplification
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   804
do not change the computed result:
94
Chengsong
parents:
diff changeset
   805
Chengsong
parents:
diff changeset
   806
\begin{itemize}
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   807
\item{(a)} If a string $s$ is in the language of $L(r)$, then \\
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   808
$\textit{bmkeps} (r^\uparrow)\backslash_{simp}\,s = \textit{bmkeps} (r^\uparrow)\backslash s$,\\
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   809
\item{(b)} If a string $s$ is in the language $L(r)$, then 
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   810
$\rup \backslash_{simp} \,s$ is not nullable.
94
Chengsong
parents:
diff changeset
   811
\end{itemize}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   812
94
Chengsong
parents:
diff changeset
   813
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   814
We have already proved the second part  in Isabelle. This is actually
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   815
not too difficult because we can show that simplification does not
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   816
change the language of simplified regular expressions.
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   817
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   818
If we can prove the first part, that is the bitsequence algorithm with
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   819
simplification produces the same result as the one without
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   820
simplification, then we are done.  Unfortunately that part requires
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   821
more effort, because simplification does not only need to \emph{not}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   822
change the language, but also not change the value (that is the
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   823
computed result).
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   824
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   825
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   826
%Do you want to keep this? You essentially want to say that the old
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   827
%method used retrieve, which unfortunately cannot be adopted to 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   828
%the simplification rules. You could just say that and give an example.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   829
%However you have to think about how you give the example....nobody knows
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   830
%about AZERO etc yet. Maybe it might be better to use normal regexes
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   831
%like $a + aa$, but annotate bitsequences as subscript like $_1(_0a + _1aa)$.
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   832
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   833
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   834
%REPLY:\\
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   835
%Yes, I am essentially saying that the old method
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   836
%cannot be adopted without adjustments.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   837
%But this does not mean we should skip
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   838
%the proof of the bit-coded algorithm
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   839
%as it is still the main direction we are looking into
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   840
%to prove things. We are trying to modify
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   841
%the old proof to suit our needs, but not give 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   842
%up it totally, that is why i believe the old 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   843
%proof is fundamental in understanding
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   844
%what we are doing in the past 6 months.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   845
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   846
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   847
\subsubsection*{Existing Proof}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   848
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   849
For this we have started with looking at the original proof that
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   850
established that the bitsequence algorrithm produces the same result
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   851
as the algorithm not using bitsequences. Formally this proof
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   852
established
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   853
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   854
\begin{equation}\label{lexer}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   855
\blexer \; (r^\uparrow)  s = \lexer \;r \;s
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   856
\end{equation}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   857
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   858
%\noindent
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   859
%might provide us insight into proving 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   860
%\begin{center}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   861
%$\blexer \; r^\uparrow \;s = \blexers \; r^\uparrow \;s$
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   862
%\end{center}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   863
94
Chengsong
parents:
diff changeset
   864
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   865
The proof uses two ``tricks''. One is that it uses a \flex-function
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   866
94
Chengsong
parents:
diff changeset
   867
\begin{center}
Chengsong
parents:
diff changeset
   868
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   869
$\textit{flex} \;r\; f\; (c\!::\!s) $ & $\dn$ & $\textit{flex} \;  (r\backslash c) \;(\lambda v. f (inj \; r \; c \; v)) \;s$ \\
Chengsong
parents:
diff changeset
   870
$\textit{flex} \;r\; f\;  [\,] $ & $\dn$ & $f$
Chengsong
parents:
diff changeset
   871
\end{tabular}
Chengsong
parents:
diff changeset
   872
\end{center}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   873
94
Chengsong
parents:
diff changeset
   874
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   875
and then proves for the right-hand side in \eqref{lexer}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   876
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   877
\begin{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   878
$\lexer \;r\; s = \flex \;\textit{id} \; r\;s \;(\mkeps \; (r\backslash s))$
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   879
\end{center}.
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   880
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   881
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   882
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   883
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   884
\noindent
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   885
The $\flex$-function essentially does lexing by
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   886
stacking up injection functions while doing derivatives.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   887
94
Chengsong
parents:
diff changeset
   888
explicitly showing the order of characters being
Chengsong
parents:
diff changeset
   889
injected back in each step.
Chengsong
parents:
diff changeset
   890
With $\flex$ we can write $\lexer$ this way: 
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   891
94
Chengsong
parents:
diff changeset
   892
\begin{center}
Chengsong
parents:
diff changeset
   893
$\lexer \;r\; s = \flex \;id \; r\;s \;(\mkeps r\backslash s)$
Chengsong
parents:
diff changeset
   894
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   895
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   896
%\noindent
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   897
%$\flex$ focuses on the injections instead of the derivatives ,
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   898
%compared to the original definition of $\lexer$, which puts equal
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   899
%amount of emphasis on injection and derivative with respect to each
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   900
%character:
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   901
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   902
%\begin{center}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   903
%\begin{tabular}{lcl}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   904
%$\textit{lexer} \; r\; (c\!::\!s) $ & $\dn$ & $\textit{case} \; \lexer \; (r\backslash c) \;s \; %\textit{of}$ \\
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   905
% & & $\textit{None} \; \Longrightarrow \; \textit{None}$\\
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   906
%  & & $\textbar \; v \; \Longrightarrow \; \inj \; r\;c\;v$\\
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   907
%$\textit{lexer} \; r\;  [\,] $ & $\dn$ & $\textit{if} \; \nullable (r) \; \textit{then} \; \mkeps% (r) \; \textit{else} \;None$
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   908
%\end{tabular}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   909
%\end{center}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   910
94
Chengsong
parents:
diff changeset
   911
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   912
The crux in the existing proof is how $\flex$ relates to injection, namely
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   913
94
Chengsong
parents:
diff changeset
   914
\begin{center}
Chengsong
parents:
diff changeset
   915
$\flex \; r \; id \; (s@[c]) \; v = \flex \;  r \; id \; s \; (inj \; (r\backslash s) \; c\; v)$.
Chengsong
parents:
diff changeset
   916
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   917
94
Chengsong
parents:
diff changeset
   918
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   919
This property allows one to rewrite an induction hypothesis like 
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   920
94
Chengsong
parents:
diff changeset
   921
\begin{center} 
Chengsong
parents:
diff changeset
   922
$ \flex \; r\; id\; (s@[c])\; v = \textit{decode} \;( \textit{retrieve}\; (\rup \backslash s) \; (\inj \; (r\backslash s) \;c\;v)\;) r$
Chengsong
parents:
diff changeset
   923
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   924
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   925
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   926
\subsubsection{Retrieve Function}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   927
The crucial point is to find the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   928
$\textit{POSIX}$  information of a regular expression and how it is modified,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   929
augmented and propagated 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   930
during simplification in parallel with the regular expression that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   931
has not been simplified in the subsequent derivative operations.  To aid this,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   932
we use the helper function retrieve described by Sulzmann and Lu:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   933
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   934
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
110
Chengsong
parents: 109
diff changeset
   935
  $\textit{retrieve}\,(_{bs}\ONE)\,\Empty$ & $\dn$ & $bs$\\
Chengsong
parents: 109
diff changeset
   936
  $\textit{retrieve}\,(_{bs}{\bf c})\,(\Char\,d)$ & $\dn$ & $bs$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   937
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Left\,v)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   938
     $bs \,@\, \textit{retrieve}\,a\,v$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   939
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Right\,v)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   940
  $bs \,@\, \textit{retrieve}\,(\textit{ALTS}\,bs\,as)\,v$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   941
  $\textit{retrieve}\,(\textit{SEQ}\,bs\,a_1\,a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   942
     $bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   943
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,[])$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   944
     $bs \,@\, [\S]$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   945
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   946
  \multicolumn{3}{l}{
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   947
     \hspace{3cm}$bs \,@\, [\Z] \,@\, \textit{retrieve}\,a\,v\,@\,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   948
                    \textit{retrieve}\,(\textit{STAR}\,[]\,a)\,(\Stars\,vs)$}\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   949
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   950
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   951
%\comment{Did not read further}\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   952
This function assembles the bitcode 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   953
%that corresponds to a lexical value for how
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   954
%the current derivative matches the suffix of the string(the characters that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   955
%have not yet appeared, but will appear as the successive derivatives go on.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   956
%How do we get this "future" information? By the value $v$, which is
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   957
%computed by a pass of the algorithm that uses
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   958
%$inj$ as described in the previous section).  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   959
using information from both the derivative regular expression and the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   960
value. Sulzmann and Lu poroposed this function, but did not prove
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   961
anything about it. Ausaf and Urban used it to connect the bitcoded
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   962
algorithm to the older algorithm by the following equation:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   963
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   964
 \begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   965
	 (r^\uparrow)\backslash_{simp} \,c)\,v)$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   966
 \end{center} 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   967
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   968
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   969
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   970
and Urban also used this fact to prove  the correctness of bitcoded
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   971
algorithm without simplification.  Our purpose of using this, however,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   972
is to establish 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   973
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   974
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   975
$ \textit{retrieve} \;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   976
a \; v \;=\; \textit{retrieve}  \; (\textit{simp}\,a) \; v'.$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   977
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   978
The idea is that using $v'$, a simplified version of $v$ that had gone
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   979
through the same simplification step as $\textit{simp}(a)$, we are able
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   980
to extract the bitcode that gives the same parsing information as the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   981
unsimplified one. However, we noticed that constructing such a  $v'$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   982
from $v$ is not so straightforward. The point of this is that  we might
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   983
be able to finally bridge the gap by proving
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   984
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   985
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   986
\noindent\rule[1.5ex]{\linewidth}{4pt}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   987
There is no mention of retrieve yet .... this is the second trick in the
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   988
existing proof. I am not sure whether you need to explain annotated regular
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   989
expressions much earlier - maybe before the ``existing proof'' section, or
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   990
evan earlier.
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   991
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   992
\noindent\rule[1.5ex]{\linewidth}{4pt}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   993
94
Chengsong
parents:
diff changeset
   994
\noindent
Chengsong
parents:
diff changeset
   995
By using a property of retrieve we have the $\textit{RHS}$ of the above equality is
Chengsong
parents:
diff changeset
   996
$decode (retrieve (r^\uparrow \backslash(s @ [c])) v) r$, and this gives the 
Chengsong
parents:
diff changeset
   997
main lemma result:
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   998
94
Chengsong
parents:
diff changeset
   999
\begin{center}
Chengsong
parents:
diff changeset
  1000
$ \flex \;r\;  id \; (s@[c]) \; v =\textit{decode}(\textit{retrieve} (\rup \backslash (s@[c])) \;v) r$
Chengsong
parents:
diff changeset
  1001
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1002
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1003
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1004
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1005
94
Chengsong
parents:
diff changeset
  1006
\noindent
Chengsong
parents:
diff changeset
  1007
To use this lemma result for our 
Chengsong
parents:
diff changeset
  1008
correctness proof, simply replace the $v$ in the
Chengsong
parents:
diff changeset
  1009
$\textit{RHS}$ of the above equality with
Chengsong
parents:
diff changeset
  1010
$\mkeps\;(r\backslash (s@[c]))$, and apply the lemma that
Chengsong
parents:
diff changeset
  1011
 
Chengsong
parents:
diff changeset
  1012
\begin{center}
Chengsong
parents:
diff changeset
  1013
$\textit{decode} \; \bmkeps \; \rup \; r = \textit{decode} \; (\textit{retrieve} \; \rup \; \mkeps(r)) \;r$
Chengsong
parents:
diff changeset
  1014
\end{center}
Chengsong
parents:
diff changeset
  1015
\noindent
Chengsong
parents:
diff changeset
  1016
We get the correctness of our bit-coded algorithm:
Chengsong
parents:
diff changeset
  1017
\begin{center}
Chengsong
parents:
diff changeset
  1018
$\flex \;r\;  id \; s \; (\mkeps \; r\backslash s) = \textit{decode} \; \bmkeps \; \rup\backslash s \; r$
Chengsong
parents:
diff changeset
  1019
\end{center}
Chengsong
parents:
diff changeset
  1020
\noindent
Chengsong
parents:
diff changeset
  1021
The bridge between the above chain of equalities
Chengsong
parents:
diff changeset
  1022
is the use of $\retrieve$,
Chengsong
parents:
diff changeset
  1023
if we want to use a similar technique for the 
Chengsong
parents:
diff changeset
  1024
simplified version of algorithm,
Chengsong
parents:
diff changeset
  1025
we face the problem that in the above 
Chengsong
parents:
diff changeset
  1026
equalities,
Chengsong
parents:
diff changeset
  1027
$\retrieve \; a \; v$ is not always defined.
Chengsong
parents:
diff changeset
  1028
for example,
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
  1029
$\retrieve \; _0(_1a+_0a) \; \Left(\Empty)$
101
Chengsong
parents: 100
diff changeset
  1030
is defined, but not $\retrieve \; (_{01}a) \;\Left(\Empty)$,
94
Chengsong
parents:
diff changeset
  1031
though we can extract the same POSIX
Chengsong
parents:
diff changeset
  1032
bits from the two annotated regular expressions.
95
Chengsong
parents: 94
diff changeset
  1033
The latter might occur when we try to retrieve from 
Chengsong
parents: 94
diff changeset
  1034
a simplified regular expression using the same value
Chengsong
parents: 94
diff changeset
  1035
as the unsimplified one.
Chengsong
parents: 94
diff changeset
  1036
This is because $\Left(\Empty)$ corresponds to
101
Chengsong
parents: 100
diff changeset
  1037
the regular expression structure $\ONE+r_2$ instead of
Chengsong
parents: 100
diff changeset
  1038
$\ONE$.
94
Chengsong
parents:
diff changeset
  1039
That means, if we 
Chengsong
parents:
diff changeset
  1040
want to prove that 
Chengsong
parents:
diff changeset
  1041
\begin{center}
Chengsong
parents:
diff changeset
  1042
$\textit{decode} \; \bmkeps \; \rup\backslash s \; r = \textit{decode} \; \bmkeps \; \rup\backslash_{simp} s \; r$
Chengsong
parents:
diff changeset
  1043
\end{center}
Chengsong
parents:
diff changeset
  1044
\noindent
Chengsong
parents:
diff changeset
  1045
holds by using $\retrieve$,
Chengsong
parents:
diff changeset
  1046
we probably need to prove an equality like below:
Chengsong
parents:
diff changeset
  1047
\begin{center}
Chengsong
parents:
diff changeset
  1048
%$\retrieve \; \rup\backslash_{simp} s \; \mkeps(r\backslash_{simp} s)=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
101
Chengsong
parents: 100
diff changeset
  1049
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(f(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
94
Chengsong
parents:
diff changeset
  1050
\end{center}
Chengsong
parents:
diff changeset
  1051
\noindent
101
Chengsong
parents: 100
diff changeset
  1052
$f$ rectifies $r\backslash s$ so the value $\mkeps(f(r\backslash s))$ becomes
Chengsong
parents: 100
diff changeset
  1053
 something simpler
94
Chengsong
parents:
diff changeset
  1054
to make the retrieve function defined.\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1055
\subsubsection{Ways to Rectify Value}
95
Chengsong
parents: 94
diff changeset
  1056
One way to do this is to prove the following:
Chengsong
parents: 94
diff changeset
  1057
\begin{center}
Chengsong
parents: 94
diff changeset
  1058
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(\simp(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
Chengsong
parents: 94
diff changeset
  1059
\end{center}
Chengsong
parents: 94
diff changeset
  1060
\noindent
101
Chengsong
parents: 100
diff changeset
  1061
The reason why we choose $\simp$ as $f$ is because
Chengsong
parents: 100
diff changeset
  1062
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash \, s)$
Chengsong
parents: 100
diff changeset
  1063
have the same shape:
Chengsong
parents: 100
diff changeset
  1064
\begin{center}
Chengsong
parents: 100
diff changeset
  1065
$\erase (\rup\backslash_{simp} \, s) = \erase(\simp(\rup\backslash s))$
Chengsong
parents: 100
diff changeset
  1066
\end{center}
Chengsong
parents: 100
diff changeset
  1067
Chengsong
parents: 100
diff changeset
  1068
\noindent
Chengsong
parents: 100
diff changeset
  1069
$\erase$ in the above equality means to remove the bit-codes
Chengsong
parents: 100
diff changeset
  1070
in an annotated regular expression and only keep the original
Chengsong
parents: 100
diff changeset
  1071
regular expression(just like "erasing" the bits). Its definition is omitted.
Chengsong
parents: 100
diff changeset
  1072
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash s)$
Chengsong
parents: 100
diff changeset
  1073
are very closely related, but not identical.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1074
\subsubsection{Example for $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$}
101
Chengsong
parents: 100
diff changeset
  1075
For example, let $r$ be the regular expression
Chengsong
parents: 100
diff changeset
  1076
$(a+b)(a+a*)$ and $s$  be the string $aa$, then
103
Chengsong
parents: 102
diff changeset
  1077
both $\erase (\rup\backslash_{simp} \, s)$ and $\erase (\simp (\rup\backslash s))$
Chengsong
parents: 102
diff changeset
  1078
are $\ONE + a^*$. However, without $\erase$ 
101
Chengsong
parents: 100
diff changeset
  1079
\begin{center}
Chengsong
parents: 100
diff changeset
  1080
$\rup\backslash_{simp} \, s$ is equal to $_0(_0\ONE +_{11}a^*)$
Chengsong
parents: 100
diff changeset
  1081
\end{center}
Chengsong
parents: 100
diff changeset
  1082
\noindent
Chengsong
parents: 100
diff changeset
  1083
whereas
Chengsong
parents: 100
diff changeset
  1084
\begin{center}
103
Chengsong
parents: 102
diff changeset
  1085
$\simp(\rup\backslash  s)$ is equal to $(_{00}\ONE +_{011}a^*)$
101
Chengsong
parents: 100
diff changeset
  1086
\end{center}
Chengsong
parents: 100
diff changeset
  1087
\noindent
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1088
(For the sake of visual simplicity, we use numbers to denote the bits
103
Chengsong
parents: 102
diff changeset
  1089
in bitcodes as we have previously defined for annotated 
Chengsong
parents: 102
diff changeset
  1090
regular expressions. $\S$ is replaced by 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1091
subscript $_1$ and $\Z$ by $_0$.)
103
Chengsong
parents: 102
diff changeset
  1092
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1093
What makes the difference?
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1094
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1095
%Two "rules" might be inferred from the above example.
103
Chengsong
parents: 102
diff changeset
  1096
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1097
%First, after erasing the bits the two regular expressions
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1098
%are exactly the same: both become $1+a^*$. Here the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1099
%function $\simp$ exhibits the "one in the end equals many times
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1100
%at the front"
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1101
%property: one simplification in the end causes the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1102
%same regular expression structure as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1103
%successive simplifications done alongside derivatives.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1104
%$\rup\backslash_{simp} \, s$ unfolds to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1105
%$\simp((\simp(r\backslash a))\backslash a)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1106
%and $\simp(\rup\backslash s)$ unfolds to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1107
%$\simp((r\backslash a)\backslash a)$. The one simplification
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1108
%in the latter causes the resulting regular expression to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1109
%become $1+a^*$, exactly the same as the former with
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1110
%two simplifications.
103
Chengsong
parents: 102
diff changeset
  1111
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1112
%Second, the bit-codes are different, but they are essentially
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1113
%the same: if we push the outmost bits ${\bf_0}(_0\ONE +_{11}a^*)$ of $\rup\backslash_{simp} \, s$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1114
%inside then we get $(_{00}\ONE +_{011}a^*)$, exactly the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1115
%same as that of $\rup\backslash \, s$. And this difference 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1116
%does not matter when we try to apply $\bmkeps$ or $\retrieve$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1117
%to it. This seems a good news if we want to use $\retrieve$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1118
%to prove things.
103
Chengsong
parents: 102
diff changeset
  1119
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1120
%If we look into the difference above, we could see that the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1121
%difference is not fundamental: the bits are just being moved
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1122
%around in a way that does not hurt the correctness.
103
Chengsong
parents: 102
diff changeset
  1123
During the first derivative operation, 
Chengsong
parents: 102
diff changeset
  1124
$\rup\backslash a=(_0\ONE  + \ZERO)(_0a  +  _1a^*)$  is
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1125
in the form of a sequence regular expression with
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1126
two components, the first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1127
one $\ONE + \ZERO$ being nullable. 
103
Chengsong
parents: 102
diff changeset
  1128
Recall the simplification function definition:
Chengsong
parents: 102
diff changeset
  1129
\begin{center}
Chengsong
parents: 102
diff changeset
  1130
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 102
diff changeset
  1131
   
Chengsong
parents: 102
diff changeset
  1132
  $\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
Chengsong
parents: 102
diff changeset
  1133
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1134
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1135
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
Chengsong
parents: 102
diff changeset
  1136
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
Chengsong
parents: 102
diff changeset
  1137
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
Chengsong
parents: 102
diff changeset
  1138
Chengsong
parents: 102
diff changeset
  1139
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
Chengsong
parents: 102
diff changeset
  1140
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1141
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
Chengsong
parents: 102
diff changeset
  1142
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
Chengsong
parents: 102
diff changeset
  1143
Chengsong
parents: 102
diff changeset
  1144
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
Chengsong
parents: 102
diff changeset
  1145
\end{tabular}    
Chengsong
parents: 102
diff changeset
  1146
\end{center}    
Chengsong
parents: 102
diff changeset
  1147
Chengsong
parents: 102
diff changeset
  1148
\noindent
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1149
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1150
and the difinition of $\flatten$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1151
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1152
 \begin{tabular}{c c c}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1153
 $\flatten \; []$ & $\dn$ & $[]$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1154
 $\flatten \; \ZERO::rs$ & $\dn$ & $rs$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1155
 $\flatten \;(_{\textit{bs}_1}\oplus \textit{rs}_1 ::rs)$ & $\dn$ & $(\map \, (\fuse \, \textit{bs}_1) \,\textit{rs}_1) ::: \flatten(rs)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1156
 $\flatten \; r :: rs$ & $\dn$ & $r::\flatten(rs)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1157
 \end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1158
 \end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1159
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1160
 \noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1161
If we call $\simp$ on $\rup\backslash a$, just as $\backslash_{simp}$
103
Chengsong
parents: 102
diff changeset
  1162
requires, then we would go throught the third clause of 
Chengsong
parents: 102
diff changeset
  1163
the sequence case:$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1164
The $\ZERO$ of $(_0\ONE  + \ZERO)$ is thrown away 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1165
by $\flatten$ and 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1166
$_0\ONE$ merged into $(_0a  +  _1a^*)$ by simply
103
Chengsong
parents: 102
diff changeset
  1167
putting its bits($_0$) to the front of the second component:
Chengsong
parents: 102
diff changeset
  1168
 ${\bf_0}(_0a  +  _1a^*)$. 
Chengsong
parents: 102
diff changeset
  1169
 After a second derivative operation,
Chengsong
parents: 102
diff changeset
  1170
 namely, $(_0(_0a  +  _1a^*))\backslash a$, we get 
Chengsong
parents: 102
diff changeset
  1171
 $
Chengsong
parents: 102
diff changeset
  1172
 _0(_0 \ONE  +  _1(_1\ONE \cdot a^*))
Chengsong
parents: 102
diff changeset
  1173
 $, and this simplifies to $_0(_0 \ONE  +  _{11} a^*)$
Chengsong
parents: 102
diff changeset
  1174
 by the third clause of the alternative case:
Chengsong
parents: 102
diff changeset
  1175
 $\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1176
The outmost bit $_0$ stays with 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1177
the outmost regular expression, rather than being fused to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1178
its child regular expressions, as what we will later see happens
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1179
to $\simp(\rup\backslash \, s)$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1180
If we choose to not simplify in the midst of derivative operations,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1181
but only do it at the end after the string has been exhausted, 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1182
namely, $\simp(\rup\backslash \, s)=\simp((\rup\backslash a)\backslash a)$,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1183
then at the {\bf second} derivative of 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1184
$(\rup\backslash a)\bf{\backslash a}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1185
we will go throught the clause of $\backslash$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1186
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1187
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1188
$(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1189
     $(when \; \textit{bnullable}\,a_1)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1190
					       & &$\textit{ALTS}\,bs\,List(\;\;(\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1191
					       & &$(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))\;\;)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1192
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1193
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1194
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1195
because
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1196
$\rup\backslash a = (_0\ONE  + \ZERO)(_0a  +  _1a^*)$  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1197
is a sequence
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1198
with the first component being nullable
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1199
(unsimplified, unlike the first round of running$\backslash_{simp}$).
103
Chengsong
parents: 102
diff changeset
  1200
Therefore $((_0\ONE  + \ZERO)(_0a  +  _1a^*))\backslash a$ splits into
Chengsong
parents: 102
diff changeset
  1201
$([(\ZERO + \ZERO)\cdot(_0a  +  _1a^*)] + _0( _0\ONE  + _1[_1\ONE \cdot a^*]))$.
Chengsong
parents: 102
diff changeset
  1202
After these two successive derivatives without simplification,
Chengsong
parents: 102
diff changeset
  1203
we apply $\simp$ to this regular expression, which goes through
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1204
the alternative clause, and each component of 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1205
$([(\ZERO + \ZERO)\cdot(_0a  +  _1a^*)] + _0( _0\ONE  + _1[_1\ONE \cdot a^*]))$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1206
will be simplified, giving us the list:$[\ZERO, _0(_0\ONE  + _{11}a^*)]$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1207
This list is then "flattened"--$\ZERO$ will be
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1208
thrown away by $\textit{flatten}$; $ _0(_0\ONE  + _{11}a^*)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1209
is opened up to make the list consisting of two separate elements 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1210
$_{00}\ONE$ and $_{011}a^*$, note that $flatten$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1211
$\fuse$s the bit(s) $_0$ to the front of $_0\ONE $ and $_{11}a^*$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1212
In a nutshell, the order of simplification causes
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1213
the bits to be moved differently.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1214
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1215
 \subsubsection{A Failed Attempt To Remedy the Problem Above}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1216
A simple class of regular expression and string
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1217
pairs $(r, s)$ can be deduced from the above example 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1218
which trigger the difference between 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1219
$\rup\backslash_{simp} \, s$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1220
and  $\simp(\rup\backslash s)$:
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1221
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1222
\begin{tabular}{lcl}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1223
$D =\{ (r_1 \cdot r_2,\; [c_1c_2]) \mid $ & $\simp(r_2) = r_2, \simp(r_1 \backslash c_1) = \ONE,$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1224
 $r_1 \; not \; \nullable, c_2 \in L(r_2),$ & $\exists \textit{rs},\textit{bs}.\;  r_2 \backslash c_2 = _{bs}{\oplus rs}$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1225
$\exists \textit{rs}_1. \; \simp(r_2 \backslash c_2) = _{bs}{\oplus \textit{rs}_1}$ &  $and \;\simp(r_1 \backslash [c_1c_2]) = \ZERO\}$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1226
\end{tabular}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1227
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1228
We take a pair $(r, \;s)$ from the set $D$.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1229
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1230
Now we compute ${\bf \rup \backslash_{simp} s}$, we get:
110
Chengsong
parents: 109
diff changeset
  1231
\begin{center}
Chengsong
parents: 109
diff changeset
  1232
\begin{tabular}{lcl}
Chengsong
parents: 109
diff changeset
  1233
$(r_1\cdot r_2)\backslash_{simp} \, [c_1c_2]$ & $= \simp\left[ \big(\simp\left[ \left( r_1\cdot r_2 \right) \backslash c_1\right] \big)\backslash c_2\right]$\\
Chengsong
parents: 109
diff changeset
  1234
								      & $= \simp\left[ \big(\simp \left[  \left(r_1 \backslash c_1\right) \cdot r_2 \right] \big) \backslash c_2 \right]$\\
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1235
								      & $= \simp \left[  (\fuse \; \bmkeps(r_1\backslash c_1) \; \simp(r_2) ) \backslash c_2 \right]$,\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1236
								      & $= \simp \left[  (\fuse \; \bmkeps(r_1\backslash c_1) \; r_2 ) \backslash c_2 \right]$,
110
Chengsong
parents: 109
diff changeset
  1237
\end{tabular}
Chengsong
parents: 109
diff changeset
  1238
\end{center}
Chengsong
parents: 109
diff changeset
  1239
\noindent
Chengsong
parents: 109
diff changeset
  1240
from the definition of $D$ we know $r_1 \backslash c_1$ is nullable, therefore
Chengsong
parents: 109
diff changeset
  1241
$\bmkeps(r_1\backslash c_1)$  returns a bitcode, we shall call it
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1242
 $\textit{bs}_2$. 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1243
The above term can be rewritten as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1244
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1245
$ \simp \left[  \fuse \; \textit{bs}_2\; r_2  \backslash c_2 \right]$,
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1246
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1247
which is equal to 
110
Chengsong
parents: 109
diff changeset
  1248
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1249
$\simp \left[ \fuse \; \textit{bs}_2 \; _{bs}{\oplus rs} \right]$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1250
$=\simp \left[  \; _{bs_2++bs}{\oplus rs} \right]$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1251
$=  \; _{bs_2++bs}{\oplus \textit{rs}_1} $
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1252
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1253
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1254
by using the properties from the set $D$ again
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1255
and again(The reason why we set so many conditions 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1256
that the pair $(r,s)$ need to satisfy is because we can
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1257
rewrite them easily to construct the difference.)
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1258
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1259
Now we compute ${\bf \simp(\rup \backslash s)}$:
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1260
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1261
$\simp \big[(r_1\cdot r_2) \backslash [c_1c_2] \big]= \simp \left[ ((r_1 \cdot r_2 )\backslash c_1) \backslash c_2 \right]$
110
Chengsong
parents: 109
diff changeset
  1262
\end{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1263
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1264
Again, using the properties above, we obtain
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1265
the following chain of equalities:
110
Chengsong
parents: 109
diff changeset
  1266
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1267
$\simp(\rup \backslash s)= \simp \left[ ((r_1 \cdot r_2 )\backslash c_1) \backslash c_2 \right]= \simp\left[    \left(r_1 \backslash c_1\right) \cdot r_2  \big) \backslash c_2 \right]$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1268
$= \simp \left[ \oplus[\big( \left(r_1 \backslash c_1\right) \backslash c_2 \big) \cdot r_2 \; , \; \fuse \; (\bmkeps \;r_1\backslash c_1) \; r_2 \backslash c_2 ] \right]$,
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1269
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1270
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1271
as before, we call the bitcode returned by 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1272
$\bmkeps(r_1\backslash c_1)$ as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1273
$\textit{bs}_2$. 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1274
Also, $\simp(r_2 \backslash c_2)$ is 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1275
$_{bs}\oplus \textit{rs}_1$, 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1276
and $( \left(r_1 \backslash c_1\right) \backslash c_2  \cdot r_2)$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1277
simplifies to $\ZERO$,
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1278
so the above term can be expanded as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1279
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1280
\begin{tabular}{l}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1281
$\textit{distinct}(\flatten[\ZERO\;, \; _{\textit{bs}_2++\textit{bs}}\oplus \textit{rs}_1] ) \; \textit{match} $ \\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1282
  $\textit{case} \; [] \Rightarrow  \ZERO$ \\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1283
   $\textit{case} \; a :: [] \Rightarrow  \textit{\fuse \; \textit{bs} a}$ \\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1284
    $\textit{case} \;  as' \Rightarrow  _{[]}\oplus as'$\\ 
110
Chengsong
parents: 109
diff changeset
  1285
\end{tabular}
Chengsong
parents: 109
diff changeset
  1286
\end{center}
Chengsong
parents: 109
diff changeset
  1287
\noindent
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1288
Applying the definition of $\flatten$, we get
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1289
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1290
$_{[]}\oplus (\textit{map} \; \fuse (\textit{bs}_2 ++ bs) rs_1)$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1291
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1292
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1293
compared to the result 
110
Chengsong
parents: 109
diff changeset
  1294
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1295
$ \; _{bs_2++bs}{\oplus \textit{rs}_1} $
110
Chengsong
parents: 109
diff changeset
  1296
\end{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1297
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1298
Note how these two regular expressions only
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1299
differ in terms of the position of the bits 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1300
$\textit{bs}_2++\textit{bs}$. They are the same otherwise.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1301
What caused this difference to happen?
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1302
The culprit is the $\flatten$ function, which spills
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1303
out the bitcodes in the inner alternatives when 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1304
there exists an outer alternative.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1305
Note how the absence of simplification
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1306
caused $\simp(\rup \backslash s)$ to
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1307
generate the nested alternatives structure:
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1308
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1309
$  \oplus[\ZERO \;, \; _{bs}\oplus \textit{rs} ]$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1310
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1311
and this will always trigger the $\flatten$ to 
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1312
spill out the inner alternative's bitcode $\textit{bs}$,
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1313
whereas when
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1314
simplification is done along the way, 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1315
the structure of nested alternatives is never created(we can
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1316
actually prove that simplification function never allows nested
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1317
alternatives to happen, more on this later).
110
Chengsong
parents: 109
diff changeset
  1318
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1319
How about we do not allow the function $\simp$
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1320
to fuse out the bits when it is unnecessary?
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1321
Like, for the above regular expression, we might
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1322
just delete the outer layer of alternative
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1323
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1324
\st{$ {\oplus[\ZERO \;,}$} $_{bs}\oplus \textit{rs}$ \st{$]$}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1325
\end{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1326
and get $_{bs}\oplus \textit{rs}$ instead, without
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1327
fusing the bits $\textit{bs}$ inside to every element 
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1328
of $\textit{rs}$.
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1329
This idea can be realized by making the following
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1330
changes to the $\simp$-function:
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1331
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1332
  \begin{tabular}{@{}lcl@{}}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1333
   
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1334
  $\textit{simp}' \; (_{\textit{bs}}(a_1 \cdot a_2))$ & $\dn$ & $\textit{as} \; \simp \; \textit{was} \; \textit{before} $ \\
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1335
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1336
  $\textit{simp}' \; (_{bs}\oplus as)$ & $\dn$ & \st{$\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $} \\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1337
  &&\st{$\quad\textit{case} \; [] \Rightarrow  \ZERO$} \\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1338
   &&\st{$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$} \\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1339
   &&\st{$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$}\\ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1340
   &&$\textit{if}(\textit{hollowAlternatives}( \textit{map \; simp \; as}))$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1341
   &&$\textit{then} \; \fuse  \; \textit{bs}\; \textit{extractAlt}(\textit{map} \; \simp \; \textit{as} )$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1342
   &&$\textit{else} \; \simp(_{bs} \oplus \textit{as})$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1343
   
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1344
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1345
   $\textit{simp}' \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1346
\end{tabular}    
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1347
\end{center}    
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1348
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1349
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1350
given the definition of $\textit{hollowAlternatives}$ and  $\textit{extractAlt}$ :
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1351
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1352
$\textit{hollowAlternatives}( \textit{rs}) \dn 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1353
\exists r = (_{\textit{bs}_1}\oplus \textit{rs}_1)  \in \textit{rs}.  \forall r' \in \textit{rs}, \;
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1354
\textit{either} \; r' = \ZERO \; \textit{or} \; r' = r $
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1355
$\textit{extractAlt}( \textit{rs}) \dn \textit{if}\big(
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1356
\exists r = (_{\textit{bs}_1}\oplus \textit{rs}_1)  \in \textit{rs}.  \forall r' \in \textit{rs}, \;
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1357
\textit{either} \; r' = \ZERO \; \textit{or} \; r' = r \big)\; \textit{then} \; \textit{return} \; r$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1358
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1359
\noindent
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1360
Basically, $\textit{hollowAlternatives}$ captures the feature of
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1361
a list of regular expression of the shape 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1362
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1363
$  \oplus[\ZERO \;, \; _{bs}\oplus \textit{rs} ]$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1364
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1365
and this means we can simply elevate the 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1366
inner regular expression $_{bs}\oplus \textit{rs}$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1367
 to the outmost
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1368
and throw away the useless $\ZERO$s and
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1369
the outer $\oplus$ wrapper.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1370
Using this new definition of simp, 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1371
under the example where  $r$ is the regular expression
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1372
$(a+b)(a+a*)$ and $s$  is the string $aa$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1373
the problem of $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1374
is resolved.
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1375
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1376
Unfortunately this causes new problems:
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1377
for the counterexample where 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1378
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1379
$r$ is the regular expression
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1380
$(ab+(a^*+aa))$ and $s$  is the string $aa$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1381
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1382
$\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1383
happens again, whereas this does not happen for the old
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1384
version of $\simp$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1385
This dilemma causes this amendment not a successful 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1386
attempt to make $\rup\backslash_{simp} \, s = \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1387
under every possible regular expression and string.
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1388
\subsection{Properties of the Function $\simp$}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1389
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1390
We have proved in Isabelle quite a few properties
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1391
of the $\simp$-function, which helps the proof to go forward
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1392
and we list them here to aid comprehension.
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1393
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1394
To start, we need a bit of auxiliary notations,
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1395
which is quite basic and is only written here
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1396
for clarity.
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1397
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1398
$\textit{sub}(r)$ computes the set of the 
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1399
sub-regular expression of $r$:
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1400
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1401
$\textit{sub}(\ONE) \dn \{\ONE\}$\\
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1402
$\textit{sub}(r_1 \cdot r_2) \dn \textit{sub}(r_1) \cup \textit{sub}(r_2) \cup \{r_1 \cdot r_2\}$\\
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1403
$\textit{sub}(r_1 + r_2) \dn \textit{sub}(r_1) \cup \textit{sub}(r_2) \cup \{r_1+r_2\}$\\
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1404
\end{center}
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1405
$\textit{good}(r) \dn r \neq \ZERO \land \\
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1406
\forall r' \in \textit{sub}(r), \textit{if} \; r' = _{bs_1}\oplus(rs_1), \;
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1407
\textit{then} \nexists r'' \in \textit{rs}_1 \; s.t.\;
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1408
r'' = _{bs_2}\oplus \textit{rs}_2 $
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1409
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1410
The properties are mainly the ones below:
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1411
\begin{itemize}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1412
\item
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1413
\begin{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1414
$\simp(\simp(r)) = \simp(r)$
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1415
\end{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1416
\item
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1417
\begin{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1418
$\textit{if} r = \simp(r') \textit{then} \; \textit{good}(r) $
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1419
\end{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1420
\end{itemize}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1421
\subsection{the Contains relation}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1422
$\retrieve$ is a too strong relation in that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1423
it only extracts one bitcode instead of a set of them.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1424
Therefore we try to define another relation(predicate)
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1425
to capture the fact the regular expression has bits
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1426
being moved around but still has all the bits needed.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1427
The contains symbol, written$\gg$, is  a relation that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1428
takes two arguments in an infix form 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1429
and returns a truth value. 
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1430
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1431
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1432
In other words, from the set of regular expression and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1433
bitcode pairs 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1434
$\textit{RV} = \{(r, v) \mid r \text{r is a regular expression, v is a value}\}$,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1435
those that satisfy the following requirements are in the set
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1436
$\textit{RV}_Contains$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1437
Unlike the $\retrieve$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1438
function, which takes two arguments $r$ and $v$ and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1439
produces an only answer $\textit{bs}$, it takes only 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1440
one argument $r$ and returns a set of bitcodes that 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1441
can be generated by $r$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1442
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1443
\begin{tabular}{llclll}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1444
& & & $_{bs}\ONE$ & $\gg$ & $\textit{bs}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1445
& & & $_{bs}{\bf c}$ & $\gg$ & $\textit{bs}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1446
$\textit{if} \; r_1 \gg \textit{bs}_1$ & $r_2 \; \gg \textit{bs}_2$ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1447
& $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1448
 $_{bs}{r_1 \cdot r_2}$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1449
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1450
 $\textit{bs} @ \textit{bs}_1 @ \textit{bs}_2$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1451
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1452
 $\textit{if} \; r \gg \textit{bs}_1$ &  & $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1453
 $_{bs}{\oplus(r :: \textit{rs}})$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1454
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1455
 $\textit{bs} @ \textit{bs}_1 $\\ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1456
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1457
 $\textit{if} \; _{bs}(\oplus \textit{rs}) \gg \textit{bs} @ \textit{bs}_1$ &  & $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1458
 $_{bs}{\oplus(r :: \textit{rs}})$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1459
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1460
 $\textit{bs} @ \textit{bs}_1 $\\  
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1461
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1462
 $\textit{if} \; r \gg \textit{bs}_1\; \textit{and}$ &  $_{bs}r^* \gg \textit{bs} @ \textit{bs}_2$ & $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1463
 $_{bs}r^* $ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1464
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1465
 $\textit{bs} @ [0] @ \textit{bs}_1@ \textit{bs}_2 $\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1466
 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1467
 & & & $_{bs}r^*$ & $\gg$ & $\textit{bs} @ [1]$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1468
\end{tabular}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1469
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1470
It is a predicate in the sense that given 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1471
a regular expression and a bitcode, it 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1472
returns true or false, depending on whether 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1473
or not the regular expression can actually produce that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1474
value. If the predicates returns a true, then 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1475
we say that the regular expression $r$ contains
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1476
the bitcode $\textit{bs}$, written 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1477
$r \gg \textit{bs}$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1478
The $\gg$ operator with the
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1479
regular expression $r$ may also be seen as a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1480
machine that does a derivative of regular expressions
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1481
on all strings simultaneously, taking 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1482
the bits by going throught the regular expression tree
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1483
 structure in a depth first manner, regardless of whether
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1484
 the part being traversed is nullable or not.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1485
 It put all possible bits that can be produced on such a traversal
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1486
 into a set.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1487
 For example, if we are given the regular expression 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1488
$((a+b)(c+d))^*$, the tree structure may be written as
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1489
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1490
\begin{tikzpicture}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1491
\tikz[tree layout]\graph[nodes={draw, circle}] {
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1492
* -> 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1493
    {@-> {
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1494
    {+1 ->
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1495
        {a , b}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1496
        },
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1497
    {+ ->
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1498
        {c , d }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1499
        }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1500
        }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1501
    }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1502
};
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1503
\end{tikzpicture}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1504
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1505
\subsection{the $\textit{ders}_2$ Function}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1506
If we want to prove the result 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1507
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1508
	$ \textit{blexer}\_{simp}(r, \; s) =  \textit{blexer}(r, \; s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1509
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1510
inductively 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1511
on the structure of the regular expression,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1512
then we need to induct on the  case $r_1 \cdot r_2$,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1513
it can be good if we could express $(r_1 \cdot r_2) \backslash s$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1514
in terms of $r_1 \backslash s$ and $r_2 \backslash s$,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1515
and this naturally induces the $ders2$ function,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1516
which does a "convolution" on $r_1$ and $r_2$ using the string
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1517
$s$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1518
It is based on the observation that the derivative of $r_1 \cdot r_2$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1519
with respect to a string $s$ can actually be written in an "explicit form"
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1520
composed of $r_1$'s derivative of $s$ and $r_2$'s derivative of $s$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1521
This can be illustrated in the following procedure execution 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1522
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1523
	$ (r_1 \cdot r_2) \backslash [c_1c_2] =  (\textit{if} \; \nullable(r_1)\;  \textit{then} \; ((r_1 \backslash c_1) \cdot r_2 + r_2 \backslash c_1) \; \textit{else} \; (r_1\backslash c_1) \cdot r_2) \backslash c_2$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1524
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1525
which can also be written in a "convoluted sum"
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1526
format:
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1527
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1528
	$ (r_1 \cdot r_2) \backslash [c_1c_2] =  \sum{r_1 \backslash s_i \cdot r_2 \backslash s_j}$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1529
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1530
In a more serious manner, we should write
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1531
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1532
	$ (r_1 \cdot r_2) \backslash [c_1c_2] =  \sum{r_1 \backslash s_i \cdot r_2 \backslash s_j}$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1533
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1534
Note this differentiates from the previous form in the sense that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1535
it calculates the results $r_1\backslash s_i , r_2 \backslash s_j$ first and then glue them together
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1536
through nested alternatives whereas the $r_1 \cdot r_2 \backslash s$ procedure,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1537
used by algorithm $\lexer$, can only produce each component of the 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1538
resulting alternatives regular expression altogether rather than 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1539
generating each of the children nodes one by one
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1540
n a single recursive call that is only for generating that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1541
very expression itself.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1542
We have this 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1543
\section{Conclusion}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1544
Under the exhaustive tests we believe the main
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1545
result holds, yet a proof still seems elusive.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1546
We have tried out different approaches, and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1547
found a lot of properties of the function $\simp$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1548
The counterexamples where $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1549
are also valuable in the sense that 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1550
we get to know better why they are not equal and what 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1551
are the subtle differences between a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1552
nested simplified regular expression and a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1553
regular expression that is simplified at the final moment.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1554
We are almost there, but a last step is needed to make the proof work.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1555
Hopefully in the next few weeks we will be able to find one.
110
Chengsong
parents: 109
diff changeset
  1556
%CONSTRUCTION SITE HERE
101
Chengsong
parents: 100
diff changeset
  1557
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents: 100
diff changeset
  1558
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents: 100
diff changeset
  1559
after one simplification is exactly the same after the 
Chengsong
parents: 100
diff changeset
  1560
same sequence of derivative operations 
Chengsong
parents: 100
diff changeset
  1561
regardless of whether we did simplification
Chengsong
parents: 100
diff changeset
  1562
along the way.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1563
 One way would be to give a function that calls
110
Chengsong
parents: 109
diff changeset
  1564
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1565
fuse is the culprit: it causes the order in which alternatives
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1566
are opened up to be remembered and finally the difference
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1567
appear in $\simp(\rup \backslash s)$ and $\rup \backslash{simp} \,s$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1568
but we have to use them as they are essential in the simplification:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1569
flatten needs them.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1570
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1571
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1572
101
Chengsong
parents: 100
diff changeset
  1573
However, without erase the above equality does not hold:
Chengsong
parents: 100
diff changeset
  1574
for the regular expression  
Chengsong
parents: 100
diff changeset
  1575
$(a+b)(a+a*)$,
Chengsong
parents: 100
diff changeset
  1576
if we do derivative with respect to string $aa$,
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1577
we get 
103
Chengsong
parents: 102
diff changeset
  1578
109
Chengsong
parents: 108
diff changeset
  1579
\subsection{Another Proof Strategy}
101
Chengsong
parents: 100
diff changeset
  1580
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents: 100
diff changeset
  1581
For example,
Chengsong
parents: 100
diff changeset
  1582
Chengsong
parents: 100
diff changeset
  1583
This equicalence class method might still have the potential of proving this,
Chengsong
parents: 100
diff changeset
  1584
but not yet
Chengsong
parents: 100
diff changeset
  1585
i parallelly tried another method of using retrieve\\
Chengsong
parents: 100
diff changeset
  1586
Chengsong
parents: 100
diff changeset
  1587
94
Chengsong
parents:
diff changeset
  1588
The vsimp function, defined as follows
Chengsong
parents:
diff changeset
  1589
tries to simplify the value in lockstep with 
Chengsong
parents:
diff changeset
  1590
regular expression:\\
Chengsong
parents:
diff changeset
  1591
Chengsong
parents:
diff changeset
  1592
Chengsong
parents:
diff changeset
  1593
The problem here is that 
Chengsong
parents:
diff changeset
  1594
Chengsong
parents:
diff changeset
  1595
we used retrieve for the key induction:
Chengsong
parents:
diff changeset
  1596
$decode (retrieve (r\backslash (s @ [c])) v) r $
Chengsong
parents:
diff changeset
  1597
$decode (retrieve (r\backslash s) (inj (r\backslash s) c v)) r$
Chengsong
parents:
diff changeset
  1598
Here, decode recovers a value that corresponds to a match(possibly partial)
Chengsong
parents:
diff changeset
  1599
from bits, and the bits are extracted by retrieve,
Chengsong
parents:
diff changeset
  1600
and the key value $v$ that guides retrieve is
Chengsong
parents:
diff changeset
  1601
$mkeps r\backslash s$, $inj r c (mkeps r\backslash s)$, $inj (inj (v))$, ......
Chengsong
parents:
diff changeset
  1602
if we can 
Chengsong
parents:
diff changeset
  1603
the problem is that 
Chengsong
parents:
diff changeset
  1604
need vsiimp to make a value that is suitable for decoding
Chengsong
parents:
diff changeset
  1605
$Some(flex rid(s@[c])v) = Some(flex rids(inj (r\backslash s)cv))$
Chengsong
parents:
diff changeset
  1606
another way that christian came up with that might circumvent the 
Chengsong
parents:
diff changeset
  1607
prblem of finding suitable value is by not stating the visimp
Chengsong
parents:
diff changeset
  1608
function but include all possible value in a set that a regex is able to produce,
Chengsong
parents:
diff changeset
  1609
and proving that both r and sr are able to produce the bits that correspond the POSIX value
Chengsong
parents:
diff changeset
  1610
Chengsong
parents:
diff changeset
  1611
produced by feeding the same initial regular expression $r$ and string $s$ to the
Chengsong
parents:
diff changeset
  1612
 two functions $ders$ and $ders\_simp$.
Chengsong
parents:
diff changeset
  1613
The reason why
Chengsong
parents:
diff changeset
  1614
Namely, if $bmkeps( r_1) = bmkeps(r_2)$, then we 
Chengsong
parents:
diff changeset
  1615
Chengsong
parents:
diff changeset
  1616
Chengsong
parents:
diff changeset
  1617
If we define the equivalence relation $\sim_{m\epsilon}$ between two regular expressions
Chengsong
parents:
diff changeset
  1618
$r_1$ and $r_2$as follows:
Chengsong
parents:
diff changeset
  1619
$r_1 \sim_{m\epsilon} r_2  \iff bmkeps(r_1)= bmkeps(r_2)$
Chengsong
parents:
diff changeset
  1620
(in other words, they $r1$ and $r2$ produce the same output under the function $bmkeps$.)
Chengsong
parents:
diff changeset
  1621
Then the first goal 
Chengsong
parents:
diff changeset
  1622
might be restated as 
Chengsong
parents:
diff changeset
  1623
$(r^\uparrow)\backslash_{simp}\, s  \sim_{m\epsilon} (r^\uparrow)\backslash s$.
Chengsong
parents:
diff changeset
  1624
I tried to establish an equivalence relation between the regular experssions 
Chengsong
parents:
diff changeset
  1625
like dddr dddsr,.....
Chengsong
parents:
diff changeset
  1626
but right now i am only able to establish dsr and dr, using structural induction on r.
Chengsong
parents:
diff changeset
  1627
Those involve multiple derivative operations are harder to prove.
Chengsong
parents:
diff changeset
  1628
Two attempts have been made:
Chengsong
parents:
diff changeset
  1629
(1)induction on the number of der operations(or in other words, the length of the string s),
Chengsong
parents:
diff changeset
  1630
the inductive hypothesis was initially specified as 
Chengsong
parents:
diff changeset
  1631
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
  1632
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
  1633
the number n, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
  1634
and the proof goal may be stated as
Chengsong
parents:
diff changeset
  1635
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
  1636
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
  1637
the number n+1, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
  1638
the problem here is that although we can easily break down
Chengsong
parents:
diff changeset
  1639
a string s of length n+1 into s1@list(c), it is not that easy
Chengsong
parents:
diff changeset
  1640
to use the i.h. as a stepping stone to prove anything because s1 may well be not
Chengsong
parents:
diff changeset
  1641
in the language L(r). This inhibits us from obtaining the fact that
Chengsong
parents:
diff changeset
  1642
ders s1 r me derssimps s1 r.
Chengsong
parents:
diff changeset
  1643
Further exploration is needed to amend this hypothesis so it includes the
Chengsong
parents:
diff changeset
  1644
situation when s1 is not nullable.
Chengsong
parents:
diff changeset
  1645
For example, what information(bits? 
Chengsong
parents:
diff changeset
  1646
values?) can be extracted
Chengsong
parents:
diff changeset
  1647
from the regular expression ders(s1,r) so that we can compute or predict the possible 
Chengsong
parents:
diff changeset
  1648
result of bmkeps after another derivative operation. What function f can used to 
Chengsong
parents:
diff changeset
  1649
carry out the task? The possible way of exploration can be 
Chengsong
parents:
diff changeset
  1650
more directly perceived throught the graph below:
Chengsong
parents:
diff changeset
  1651
find a function
Chengsong
parents:
diff changeset
  1652
f
Chengsong
parents:
diff changeset
  1653
such that
Chengsong
parents:
diff changeset
  1654
f(bders s1 r)
Chengsong
parents:
diff changeset
  1655
= re1
Chengsong
parents:
diff changeset
  1656
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1657
= re2
Chengsong
parents:
diff changeset
  1658
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
  1659
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
  1660
and g(re1,c) = g(re2,c)
Chengsong
parents:
diff changeset
  1661
The inductive hypothesis would be
Chengsong
parents:
diff changeset
  1662
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
  1663
f(bders s1 r)
Chengsong
parents:
diff changeset
  1664
= re1
Chengsong
parents:
diff changeset
  1665
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1666
= re2"
Chengsong
parents:
diff changeset
  1667
proving this would be a lemma for the main proof:
Chengsong
parents:
diff changeset
  1668
the main proof would be 
Chengsong
parents:
diff changeset
  1669
"
Chengsong
parents:
diff changeset
  1670
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
  1671
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
  1672
for s = s1@c
Chengsong
parents:
diff changeset
  1673
"
Chengsong
parents:
diff changeset
  1674
and f need to be a recursive property for the lemma to be proved:
Chengsong
parents:
diff changeset
  1675
it needs to store not only the "after one char nullable info",
Chengsong
parents:
diff changeset
  1676
but also the "after two char nullable info",
Chengsong
parents:
diff changeset
  1677
and so on so that it is able to  predict what f will compute after a derivative operation,
Chengsong
parents:
diff changeset
  1678
in other words, it needs to be "infinitely recursive"\\
Chengsong
parents:
diff changeset
  1679
To prove the lemma, in other words, to get
Chengsong
parents:
diff changeset
  1680
"For all strings s1 of length <= n+1, 
Chengsong
parents:
diff changeset
  1681
f(bders s1 r)
Chengsong
parents:
diff changeset
  1682
= re3
Chengsong
parents:
diff changeset
  1683
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1684
= re4"\\
Chengsong
parents:
diff changeset
  1685
from\\
Chengsong
parents:
diff changeset
  1686
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
  1687
f(bders s1 r)
Chengsong
parents:
diff changeset
  1688
= re1
Chengsong
parents:
diff changeset
  1689
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1690
= re2"\\
Chengsong
parents:
diff changeset
  1691
it might be best to construct an auxiliary function h such that\\
Chengsong
parents:
diff changeset
  1692
h(re1, c) = re3\\
Chengsong
parents:
diff changeset
  1693
h(re2, c) = re4\\
Chengsong
parents:
diff changeset
  1694
and re3 = f(bder c (bders s1 r))\\
Chengsong
parents:
diff changeset
  1695
re4 = f(simp(bder c (bderss s1 r)))
Chengsong
parents:
diff changeset
  1696
The key point here is that we are not satisfied with what bders s r will produce under
Chengsong
parents:
diff changeset
  1697
bmkeps, but also how it will perform after a derivative operation and then bmkeps, and two 
Chengsong
parents:
diff changeset
  1698
derivative operations and so on. In essence, we are preserving the regular expression 
Chengsong
parents:
diff changeset
  1699
itself under the function f, in a less compact way than the regluar expression: we are
Chengsong
parents:
diff changeset
  1700
not just recording but also interpreting what the regular expression matches.
Chengsong
parents:
diff changeset
  1701
In other words, we need to prove the properties of bderss s r beyond the bmkeps result,
Chengsong
parents:
diff changeset
  1702
i.e., not just the nullable ones, but also those containing remaining characters.\\
Chengsong
parents:
diff changeset
  1703
(2)we observed the fact that 
Chengsong
parents:
diff changeset
  1704
erase sdddddr= erase sdsdsdsr
Chengsong
parents:
diff changeset
  1705
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents:
diff changeset
  1706
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents:
diff changeset
  1707
after one simplification is exactly the same after the 
Chengsong
parents:
diff changeset
  1708
same sequence of derivative operations 
Chengsong
parents:
diff changeset
  1709
regardless of whether we did simplification
Chengsong
parents:
diff changeset
  1710
along the way.
Chengsong
parents:
diff changeset
  1711
However, without erase the above equality does not hold:
Chengsong
parents:
diff changeset
  1712
for the regular expression  
Chengsong
parents:
diff changeset
  1713
$(a+b)(a+a*)$,
Chengsong
parents:
diff changeset
  1714
if we do derivative with respect to string $aa$,
Chengsong
parents:
diff changeset
  1715
we get
Chengsong
parents:
diff changeset
  1716
%TODO
Chengsong
parents:
diff changeset
  1717
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents:
diff changeset
  1718
For example,
Chengsong
parents:
diff changeset
  1719
Chengsong
parents:
diff changeset
  1720
This equicalence class method might still have the potential of proving this,
Chengsong
parents:
diff changeset
  1721
but not yet
Chengsong
parents:
diff changeset
  1722
i parallelly tried another method of using retrieve\\
Chengsong
parents:
diff changeset
  1723
Chengsong
parents:
diff changeset
  1724
Chengsong
parents:
diff changeset
  1725
Chengsong
parents:
diff changeset
  1726
\noindent\rule[0.5ex]{\linewidth}{1pt}
Chengsong
parents:
diff changeset
  1727
Chengsong
parents:
diff changeset
  1728
This PhD-project is about regular expression matching and
Chengsong
parents:
diff changeset
  1729
lexing. Given the maturity of this topic, the reader might wonder:
Chengsong
parents:
diff changeset
  1730
Surely, regular expressions must have already been studied to death?
Chengsong
parents:
diff changeset
  1731
What could possibly be \emph{not} known in this area? And surely all
Chengsong
parents:
diff changeset
  1732
implemented algorithms for regular expression matching are blindingly
Chengsong
parents:
diff changeset
  1733
fast?
Chengsong
parents:
diff changeset
  1734
Chengsong
parents:
diff changeset
  1735
Unfortunately these preconceptions are not supported by evidence: Take
Chengsong
parents:
diff changeset
  1736
for example the regular expression $(a^*)^*\,b$ and ask whether
Chengsong
parents:
diff changeset
  1737
strings of the form $aa..a$ match this regular
Chengsong
parents:
diff changeset
  1738
expression. Obviously this is not the case---the expected $b$ in the last
Chengsong
parents:
diff changeset
  1739
position is missing. One would expect that modern regular expression
Chengsong
parents:
diff changeset
  1740
matching engines can find this out very quickly. Alas, if one tries
Chengsong
parents:
diff changeset
  1741
this example in JavaScript, Python or Java 8 with strings like 28
Chengsong
parents:
diff changeset
  1742
$a$'s, one discovers that this decision takes around 30 seconds and
Chengsong
parents:
diff changeset
  1743
takes considerably longer when adding a few more $a$'s, as the graphs
Chengsong
parents:
diff changeset
  1744
below show:
Chengsong
parents:
diff changeset
  1745
Chengsong
parents:
diff changeset
  1746
\begin{center}
Chengsong
parents:
diff changeset
  1747
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents:
diff changeset
  1748
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  1749
\begin{axis}[
Chengsong
parents:
diff changeset
  1750
    xlabel={$n$},
Chengsong
parents:
diff changeset
  1751
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  1752
    ylabel={time in secs},
Chengsong
parents:
diff changeset
  1753
    enlargelimits=false,
Chengsong
parents:
diff changeset
  1754
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  1755
    xmax=33,
Chengsong
parents:
diff changeset
  1756
    ymax=35,
Chengsong
parents:
diff changeset
  1757
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
  1758
    scaled ticks=false,
Chengsong
parents:
diff changeset
  1759
    axis lines=left,
Chengsong
parents:
diff changeset
  1760
    width=5cm,
Chengsong
parents:
diff changeset
  1761
    height=4cm, 
Chengsong
parents:
diff changeset
  1762
    legend entries={JavaScript},  
Chengsong
parents:
diff changeset
  1763
    legend pos=north west,
Chengsong
parents:
diff changeset
  1764
    legend cell align=left]
Chengsong
parents:
diff changeset
  1765
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
Chengsong
parents:
diff changeset
  1766
\end{axis}
Chengsong
parents:
diff changeset
  1767
\end{tikzpicture}
Chengsong
parents:
diff changeset
  1768
  &
Chengsong
parents:
diff changeset
  1769
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  1770
\begin{axis}[
Chengsong
parents:
diff changeset
  1771
    xlabel={$n$},
Chengsong
parents:
diff changeset
  1772
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  1773
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
  1774
    enlargelimits=false,
Chengsong
parents:
diff changeset
  1775
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  1776
    xmax=33,
Chengsong
parents:
diff changeset
  1777
    ymax=35,
Chengsong
parents:
diff changeset
  1778
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
  1779
    scaled ticks=false,
Chengsong
parents:
diff changeset
  1780
    axis lines=left,
Chengsong
parents:
diff changeset
  1781
    width=5cm,
Chengsong
parents:
diff changeset
  1782
    height=4cm, 
Chengsong
parents:
diff changeset
  1783
    legend entries={Python},  
Chengsong
parents:
diff changeset
  1784
    legend pos=north west,
Chengsong
parents:
diff changeset
  1785
    legend cell align=left]
Chengsong
parents:
diff changeset
  1786
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
Chengsong
parents:
diff changeset
  1787
\end{axis}
Chengsong
parents:
diff changeset
  1788
\end{tikzpicture}
Chengsong
parents:
diff changeset
  1789
  &
Chengsong
parents:
diff changeset
  1790
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  1791
\begin{axis}[
Chengsong
parents:
diff changeset
  1792
    xlabel={$n$},
Chengsong
parents:
diff changeset
  1793
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  1794
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
  1795
    enlargelimits=false,
Chengsong
parents:
diff changeset
  1796
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  1797
    xmax=33,
Chengsong
parents:
diff changeset
  1798
    ymax=35,
Chengsong
parents:
diff changeset
  1799
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
  1800
    scaled ticks=false,
Chengsong
parents:
diff changeset
  1801
    axis lines=left,
Chengsong
parents:
diff changeset
  1802
    width=5cm,
Chengsong
parents:
diff changeset
  1803
    height=4cm, 
Chengsong
parents:
diff changeset
  1804
    legend entries={Java 8},  
Chengsong
parents:
diff changeset
  1805
    legend pos=north west,
Chengsong
parents:
diff changeset
  1806
    legend cell align=left]
Chengsong
parents:
diff changeset
  1807
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
Chengsong
parents:
diff changeset
  1808
\end{axis}
Chengsong
parents:
diff changeset
  1809
\end{tikzpicture}\\
Chengsong
parents:
diff changeset
  1810
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
Chengsong
parents:
diff changeset
  1811
           of the form $\underbrace{aa..a}_{n}$.}
Chengsong
parents:
diff changeset
  1812
\end{tabular}    
Chengsong
parents:
diff changeset
  1813
\end{center}  
Chengsong
parents:
diff changeset
  1814
Chengsong
parents:
diff changeset
  1815
\noindent These are clearly abysmal and possibly surprising results. One
Chengsong
parents:
diff changeset
  1816
would expect these systems to do  much better than that---after all,
Chengsong
parents:
diff changeset
  1817
given a DFA and a string, deciding whether a string is matched by this
Chengsong
parents:
diff changeset
  1818
DFA should be linear in terms of the size of the regular expression and
Chengsong
parents:
diff changeset
  1819
the string?
Chengsong
parents:
diff changeset
  1820
Chengsong
parents:
diff changeset
  1821
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
Chengsong
parents:
diff changeset
  1822
exhibit this super-linear behaviour.  But unfortunately, such regular
Chengsong
parents:
diff changeset
  1823
expressions are not just a few outliers. They are actually 
Chengsong
parents:
diff changeset
  1824
frequent enough to have a separate name created for
Chengsong
parents:
diff changeset
  1825
them---\emph{evil regular expressions}. In empiric work, Davis et al
Chengsong
parents:
diff changeset
  1826
report that they have found thousands of such evil regular expressions
Chengsong
parents:
diff changeset
  1827
in the JavaScript and Python ecosystems \cite{Davis18}. Static analysis
Chengsong
parents:
diff changeset
  1828
approach that is both sound and complete exists\cite{17Bir}, but the running 
Chengsong
parents:
diff changeset
  1829
time on certain examples in the RegExLib and Snort regular expressions
Chengsong
parents:
diff changeset
  1830
libraries is unacceptable. Therefore the problem of efficiency still remains.
Chengsong
parents:
diff changeset
  1831
Chengsong
parents:
diff changeset
  1832
This superlinear blowup in matching algorithms sometimes causes
Chengsong
parents:
diff changeset
  1833
considerable grief in real life: for example on 20 July 2016 one evil
Chengsong
parents:
diff changeset
  1834
regular expression brought the webpage
Chengsong
parents:
diff changeset
  1835
\href{http://stackexchange.com}{Stack Exchange} to its
Chengsong
parents:
diff changeset
  1836
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
Chengsong
parents:
diff changeset
  1837
In this instance, a regular expression intended to just trim white
Chengsong
parents:
diff changeset
  1838
spaces from the beginning and the end of a line actually consumed
Chengsong
parents:
diff changeset
  1839
massive amounts of CPU-resources---causing web servers to grind to a
Chengsong
parents:
diff changeset
  1840
halt. This happened when a post with 20,000 white spaces was submitted,
Chengsong
parents:
diff changeset
  1841
but importantly the white spaces were neither at the beginning nor at
Chengsong
parents:
diff changeset
  1842
the end. As a result, the regular expression matching engine needed to
Chengsong
parents:
diff changeset
  1843
backtrack over many choices. In this example, the time needed to process
Chengsong
parents:
diff changeset
  1844
the string was $O(n^2)$ with respect to the string length. This
Chengsong
parents:
diff changeset
  1845
quadratic overhead was enough for the homepage of Stack Exchange to
Chengsong
parents:
diff changeset
  1846
respond so slowly that the load balancer assumed there must be some
Chengsong
parents:
diff changeset
  1847
attack and therefore stopped the servers from responding to any
Chengsong
parents:
diff changeset
  1848
requests. This made the whole site become unavailable. Another very
Chengsong
parents:
diff changeset
  1849
recent example is a global outage of all Cloudflare servers on 2 July
Chengsong
parents:
diff changeset
  1850
2019. A poorly written regular expression exhibited exponential
Chengsong
parents:
diff changeset
  1851
behaviour and exhausted CPUs that serve HTTP traffic. Although the
Chengsong
parents:
diff changeset
  1852
outage had several causes, at the heart was a regular expression that
Chengsong
parents:
diff changeset
  1853
was used to monitor network
Chengsong
parents:
diff changeset
  1854
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}}
Chengsong
parents:
diff changeset
  1855
Chengsong
parents:
diff changeset
  1856
The underlying problem is that many ``real life'' regular expression
Chengsong
parents:
diff changeset
  1857
matching engines do not use DFAs for matching. This is because they
Chengsong
parents:
diff changeset
  1858
support regular expressions that are not covered by the classical
Chengsong
parents:
diff changeset
  1859
automata theory, and in this more general setting there are quite a few
Chengsong
parents:
diff changeset
  1860
research questions still unanswered and fast algorithms still need to be
Chengsong
parents:
diff changeset
  1861
developed (for example how to treat efficiently bounded repetitions, negation and
Chengsong
parents:
diff changeset
  1862
back-references).
Chengsong
parents:
diff changeset
  1863
%question: dfa can have exponential states. isn't this the actual reason why they do not use dfas?
Chengsong
parents:
diff changeset
  1864
%how do they avoid dfas exponential states if they use them for fast matching?
Chengsong
parents:
diff changeset
  1865
Chengsong
parents:
diff changeset
  1866
There is also another under-researched problem to do with regular
Chengsong
parents:
diff changeset
  1867
expressions and lexing, i.e.~the process of breaking up strings into
Chengsong
parents:
diff changeset
  1868
sequences of tokens according to some regular expressions. In this
Chengsong
parents:
diff changeset
  1869
setting one is not just interested in whether or not a regular
Chengsong
parents:
diff changeset
  1870
expression matches a string, but also in \emph{how}.  Consider for
Chengsong
parents:
diff changeset
  1871
example a regular expression $r_{key}$ for recognising keywords such as
Chengsong
parents:
diff changeset
  1872
\textit{if}, \textit{then} and so on; and a regular expression $r_{id}$
Chengsong
parents:
diff changeset
  1873
for recognising identifiers (say, a single character followed by
Chengsong
parents:
diff changeset
  1874
characters or numbers). One can then form the compound regular
Chengsong
parents:
diff changeset
  1875
expression $(r_{key} + r_{id})^*$ and use it to tokenise strings.  But
Chengsong
parents:
diff changeset
  1876
then how should the string \textit{iffoo} be tokenised?  It could be
Chengsong
parents:
diff changeset
  1877
tokenised as a keyword followed by an identifier, or the entire string
Chengsong
parents:
diff changeset
  1878
as a single identifier.  Similarly, how should the string \textit{if} be
Chengsong
parents:
diff changeset
  1879
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
Chengsong
parents:
diff changeset
  1880
``fire''---so is it an identifier or a keyword?  While in applications
Chengsong
parents:
diff changeset
  1881
there is a well-known strategy to decide these questions, called POSIX
Chengsong
parents:
diff changeset
  1882
matching, only relatively recently precise definitions of what POSIX
Chengsong
parents:
diff changeset
  1883
matching actually means have been formalised
Chengsong
parents:
diff changeset
  1884
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Such a
Chengsong
parents:
diff changeset
  1885
definition has also been given by Sulzmann and  Lu \cite{Sulzmann2014},
Chengsong
parents:
diff changeset
  1886
but the corresponding correctness proof turned out to be  faulty
Chengsong
parents:
diff changeset
  1887
\cite{AusafDyckhoffUrban2016}. Roughly, POSIX matching means matching
Chengsong
parents:
diff changeset
  1888
the longest initial substring. In the case of a tie, the initial
Chengsong
parents:
diff changeset
  1889
sub-match is chosen according to some priorities attached to the regular
Chengsong
parents:
diff changeset
  1890
expressions (e.g.~keywords have a higher priority than identifiers).
Chengsong
parents:
diff changeset
  1891
This sounds rather simple, but according to Grathwohl et al \cite[Page
Chengsong
parents:
diff changeset
  1892
36]{CrashCourse2014} this is not the case. They wrote:
Chengsong
parents:
diff changeset
  1893
Chengsong
parents:
diff changeset
  1894
\begin{quote}
Chengsong
parents:
diff changeset
  1895
\it{}``The POSIX strategy is more complicated than the greedy because of 
Chengsong
parents:
diff changeset
  1896
the dependence on information about the length of matched strings in the 
Chengsong
parents:
diff changeset
  1897
various subexpressions.''
Chengsong
parents:
diff changeset
  1898
\end{quote}
Chengsong
parents:
diff changeset
  1899
Chengsong
parents:
diff changeset
  1900
\noindent
Chengsong
parents:
diff changeset
  1901
This is also supported by evidence collected by Kuklewicz
Chengsong
parents:
diff changeset
  1902
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
Chengsong
parents:
diff changeset
  1903
matchers calculate incorrect results.
Chengsong
parents:
diff changeset
  1904
Chengsong
parents:
diff changeset
  1905
Our focus in this project is on an algorithm introduced by Sulzmann and
Chengsong
parents:
diff changeset
  1906
Lu in 2014 for regular expression matching according to the POSIX
Chengsong
parents:
diff changeset
  1907
strategy \cite{Sulzmann2014}. Their algorithm is based on an older
Chengsong
parents:
diff changeset
  1908
algorithm by Brzozowski from 1964 where he introduced the notion of
Chengsong
parents:
diff changeset
  1909
derivatives of regular expressions~\cite{Brzozowski1964}. We shall
Chengsong
parents:
diff changeset
  1910
briefly explain this algorithm next.
Chengsong
parents:
diff changeset
  1911
Chengsong
parents:
diff changeset
  1912
\section{The Algorithm by Brzozowski based on Derivatives of Regular
Chengsong
parents:
diff changeset
  1913
Expressions}
Chengsong
parents:
diff changeset
  1914
Chengsong
parents:
diff changeset
  1915
Suppose (basic) regular expressions are given by the following grammar:
Chengsong
parents:
diff changeset
  1916
\[			r ::=   \ZERO \mid  \ONE
Chengsong
parents:
diff changeset
  1917
			 \mid  c  
Chengsong
parents:
diff changeset
  1918
			 \mid  r_1 \cdot r_2
Chengsong
parents:
diff changeset
  1919
			 \mid  r_1 + r_2   
Chengsong
parents:
diff changeset
  1920
			 \mid r^*         
Chengsong
parents:
diff changeset
  1921
\]
Chengsong
parents:
diff changeset
  1922
Chengsong
parents:
diff changeset
  1923
\noindent
Chengsong
parents:
diff changeset
  1924
The intended meaning of the constructors is as follows: $\ZERO$
Chengsong
parents:
diff changeset
  1925
cannot match any string, $\ONE$ can match the empty string, the
Chengsong
parents:
diff changeset
  1926
character regular expression $c$ can match the character $c$, and so
Chengsong
parents:
diff changeset
  1927
on.
Chengsong
parents:
diff changeset
  1928
Chengsong
parents:
diff changeset
  1929
The ingenious contribution by Brzozowski is the notion of
Chengsong
parents:
diff changeset
  1930
\emph{derivatives} of regular expressions.  The idea behind this
Chengsong
parents:
diff changeset
  1931
notion is as follows: suppose a regular expression $r$ can match a
Chengsong
parents:
diff changeset
  1932
string of the form $c\!::\! s$ (that is a list of characters starting
Chengsong
parents:
diff changeset
  1933
with $c$), what does the regular expression look like that can match
Chengsong
parents:
diff changeset
  1934
just $s$? Brzozowski gave a neat answer to this question. He started
Chengsong
parents:
diff changeset
  1935
with the definition of $nullable$:
Chengsong
parents:
diff changeset
  1936
\begin{center}
Chengsong
parents:
diff changeset
  1937
		\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1938
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
Chengsong
parents:
diff changeset
  1939
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
Chengsong
parents:
diff changeset
  1940
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
Chengsong
parents:
diff changeset
  1941
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
Chengsong
parents:
diff changeset
  1942
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
Chengsong
parents:
diff changeset
  1943
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
Chengsong
parents:
diff changeset
  1944
		\end{tabular}
Chengsong
parents:
diff changeset
  1945
	\end{center}
Chengsong
parents:
diff changeset
  1946
This function simply tests whether the empty string is in $L(r)$.
Chengsong
parents:
diff changeset
  1947
He then defined
Chengsong
parents:
diff changeset
  1948
the following operation on regular expressions, written
Chengsong
parents:
diff changeset
  1949
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
Chengsong
parents:
diff changeset
  1950
Chengsong
parents:
diff changeset
  1951
\begin{center}
Chengsong
parents:
diff changeset
  1952
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1953
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents:
diff changeset
  1954
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
Chengsong
parents:
diff changeset
  1955
		$d \backslash c$     & $\dn$ & 
Chengsong
parents:
diff changeset
  1956
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
Chengsong
parents:
diff changeset
  1957
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
Chengsong
parents:
diff changeset
  1958
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
Chengsong
parents:
diff changeset
  1959
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
Chengsong
parents:
diff changeset
  1960
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
Chengsong
parents:
diff changeset
  1961
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
Chengsong
parents:
diff changeset
  1962
\end{tabular}
Chengsong
parents:
diff changeset
  1963
\end{center}
Chengsong
parents:
diff changeset
  1964
Chengsong
parents:
diff changeset
  1965
%Assuming the classic notion of a
Chengsong
parents:
diff changeset
  1966
%\emph{language} of a regular expression, written $L(\_)$, t
Chengsong
parents:
diff changeset
  1967
Chengsong
parents:
diff changeset
  1968
\noindent
Chengsong
parents:
diff changeset
  1969
The main property of the derivative operation is that
Chengsong
parents:
diff changeset
  1970
Chengsong
parents:
diff changeset
  1971
\begin{center}
Chengsong
parents:
diff changeset
  1972
$c\!::\!s \in L(r)$ holds
Chengsong
parents:
diff changeset
  1973
if and only if $s \in L(r\backslash c)$.
Chengsong
parents:
diff changeset
  1974
\end{center}
Chengsong
parents:
diff changeset
  1975
Chengsong
parents:
diff changeset
  1976
\noindent
Chengsong
parents:
diff changeset
  1977
For us the main advantage is that derivatives can be
Chengsong
parents:
diff changeset
  1978
straightforwardly implemented in any functional programming language,
Chengsong
parents:
diff changeset
  1979
and are easily definable and reasoned about in theorem provers---the
Chengsong
parents:
diff changeset
  1980
definitions just consist of inductive datatypes and simple recursive
Chengsong
parents:
diff changeset
  1981
functions. Moreover, the notion of derivatives can be easily
Chengsong
parents:
diff changeset
  1982
generalised to cover extended regular expression constructors such as
Chengsong
parents:
diff changeset
  1983
the not-regular expression, written $\neg\,r$, or bounded repetitions
Chengsong
parents:
diff changeset
  1984
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
Chengsong
parents:
diff changeset
  1985
straightforwardly realised within the classic automata approach.
Chengsong
parents:
diff changeset
  1986
For the moment however, we focus only on the usual basic regular expressions.
Chengsong
parents:
diff changeset
  1987
Chengsong
parents:
diff changeset
  1988
Chengsong
parents:
diff changeset
  1989
Now if we want to find out whether a string $s$ matches with a regular
Chengsong
parents:
diff changeset
  1990
expression $r$, we can build the derivatives of $r$ w.r.t.\ (in succession)
Chengsong
parents:
diff changeset
  1991
all the characters of the string $s$. Finally, test whether the
Chengsong
parents:
diff changeset
  1992
resulting regular expression can match the empty string.  If yes, then
Chengsong
parents:
diff changeset
  1993
$r$ matches $s$, and no in the negative case. To implement this idea
Chengsong
parents:
diff changeset
  1994
we can generalise the derivative operation to strings like this:
Chengsong
parents:
diff changeset
  1995
Chengsong
parents:
diff changeset
  1996
\begin{center}
Chengsong
parents:
diff changeset
  1997
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1998
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
Chengsong
parents:
diff changeset
  1999
$r \backslash [\,] $ & $\dn$ & $r$
Chengsong
parents:
diff changeset
  2000
\end{tabular}
Chengsong
parents:
diff changeset
  2001
\end{center}
Chengsong
parents:
diff changeset
  2002
Chengsong
parents:
diff changeset
  2003
\noindent
Chengsong
parents:
diff changeset
  2004
and then define as  regular-expression matching algorithm: 
Chengsong
parents:
diff changeset
  2005
\[
Chengsong
parents:
diff changeset
  2006
match\;s\;r \;\dn\; nullable(r\backslash s)
Chengsong
parents:
diff changeset
  2007
\]
Chengsong
parents:
diff changeset
  2008
Chengsong
parents:
diff changeset
  2009
\noindent
Chengsong
parents:
diff changeset
  2010
This algorithm looks graphically as follows:
Chengsong
parents:
diff changeset
  2011
\begin{equation}\label{graph:*}
Chengsong
parents:
diff changeset
  2012
\begin{tikzcd}
Chengsong
parents:
diff changeset
  2013
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
Chengsong
parents:
diff changeset
  2014
\end{tikzcd}
Chengsong
parents:
diff changeset
  2015
\end{equation}
Chengsong
parents:
diff changeset
  2016
Chengsong
parents:
diff changeset
  2017
\noindent
Chengsong
parents:
diff changeset
  2018
where we start with  a regular expression  $r_0$, build successive
Chengsong
parents:
diff changeset
  2019
derivatives until we exhaust the string and then use \textit{nullable}
Chengsong
parents:
diff changeset
  2020
to test whether the result can match the empty string. It can  be
Chengsong
parents:
diff changeset
  2021
relatively  easily shown that this matcher is correct  (that is given
Chengsong
parents:
diff changeset
  2022
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
Chengsong
parents:
diff changeset
  2023
Chengsong
parents:
diff changeset
  2024
 
Chengsong
parents:
diff changeset
  2025
\section{Values and the Algorithm by Sulzmann and Lu}
Chengsong
parents:
diff changeset
  2026
Chengsong
parents:
diff changeset
  2027
One limitation of Brzozowski's algorithm is that it only produces a
Chengsong
parents:
diff changeset
  2028
YES/NO answer for whether a string is being matched by a regular
Chengsong
parents:
diff changeset
  2029
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
Chengsong
parents:
diff changeset
  2030
to allow generation of an actual matching, called a \emph{value} or
Chengsong
parents:
diff changeset
  2031
sometimes also \emph{lexical value}.  These values and regular
Chengsong
parents:
diff changeset
  2032
expressions correspond to each other as illustrated in the following
Chengsong
parents:
diff changeset
  2033
table:
Chengsong
parents:
diff changeset
  2034
Chengsong
parents:
diff changeset
  2035
Chengsong
parents:
diff changeset
  2036
\begin{center}
Chengsong
parents:
diff changeset
  2037
	\begin{tabular}{c@{\hspace{20mm}}c}
Chengsong
parents:
diff changeset
  2038
		\begin{tabular}{@{}rrl@{}}
Chengsong
parents:
diff changeset
  2039
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
Chengsong
parents:
diff changeset
  2040
			$r$ & $::=$  & $\ZERO$\\
Chengsong
parents:
diff changeset
  2041
			& $\mid$ & $\ONE$   \\
Chengsong
parents:
diff changeset
  2042
			& $\mid$ & $c$          \\
Chengsong
parents:
diff changeset
  2043
			& $\mid$ & $r_1 \cdot r_2$\\
Chengsong
parents:
diff changeset
  2044
			& $\mid$ & $r_1 + r_2$   \\
Chengsong
parents:
diff changeset
  2045
			\\
Chengsong
parents:
diff changeset
  2046
			& $\mid$ & $r^*$         \\
Chengsong
parents:
diff changeset
  2047
		\end{tabular}
Chengsong
parents:
diff changeset
  2048
		&
Chengsong
parents:
diff changeset
  2049
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
Chengsong
parents:
diff changeset
  2050
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
Chengsong
parents:
diff changeset
  2051
			$v$ & $::=$  & \\
Chengsong
parents:
diff changeset
  2052
			&        & $\Empty$   \\
Chengsong
parents:
diff changeset
  2053
			& $\mid$ & $\Char(c)$          \\
Chengsong
parents:
diff changeset
  2054
			& $\mid$ & $\Seq\,v_1\, v_2$\\
Chengsong
parents:
diff changeset
  2055
			& $\mid$ & $\Left(v)$   \\
Chengsong
parents:
diff changeset
  2056
			& $\mid$ & $\Right(v)$  \\
Chengsong
parents:
diff changeset
  2057
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
Chengsong
parents:
diff changeset
  2058
		\end{tabular}
Chengsong
parents:
diff changeset
  2059
	\end{tabular}
Chengsong
parents:
diff changeset
  2060
\end{center}
Chengsong
parents:
diff changeset
  2061
Chengsong
parents:
diff changeset
  2062
\noindent
Chengsong
parents:
diff changeset
  2063
No value  corresponds to $\ZERO$; $\Empty$ corresponds to $\ONE$;
Chengsong
parents:
diff changeset
  2064
$\Char$ to the character regular expression; $\Seq$ to the sequence
Chengsong
parents:
diff changeset
  2065
regular expression and so on. The idea of values is to encode a kind of
Chengsong
parents:
diff changeset
  2066
lexical value for how the sub-parts of a regular expression match the
Chengsong
parents:
diff changeset
  2067
sub-parts of a string. To see this, suppose a \emph{flatten} operation,
Chengsong
parents:
diff changeset
  2068
written $|v|$ for values. We can use this function to extract the
Chengsong
parents:
diff changeset
  2069
underlying string of a value $v$. For example, $|\mathit{Seq} \,
Chengsong
parents:
diff changeset
  2070
(\textit{Char x}) \, (\textit{Char y})|$ is the string $xy$.  Using
Chengsong
parents:
diff changeset
  2071
flatten, we can describe how values encode lexical values: $\Seq\,v_1\,
Chengsong
parents:
diff changeset
  2072
v_2$ encodes a tree with two children nodes that tells how the string
Chengsong
parents:
diff changeset
  2073
$|v_1| @ |v_2|$ matches the regex $r_1 \cdot r_2$ whereby $r_1$ matches
Chengsong
parents:
diff changeset
  2074
the substring $|v_1|$ and, respectively, $r_2$ matches the substring
Chengsong
parents:
diff changeset
  2075
$|v_2|$. Exactly how these two are matched is contained in the children
Chengsong
parents:
diff changeset
  2076
nodes $v_1$ and $v_2$ of parent $\textit{Seq}$. 
Chengsong
parents:
diff changeset
  2077
Chengsong
parents:
diff changeset
  2078
To give a concrete example of how values work, consider the string $xy$
Chengsong
parents:
diff changeset
  2079
and the regular expression $(x + (y + xy))^*$. We can view this regular
Chengsong
parents:
diff changeset
  2080
expression as a tree and if the string $xy$ is matched by two Star
Chengsong
parents:
diff changeset
  2081
``iterations'', then the $x$ is matched by the left-most alternative in
Chengsong
parents:
diff changeset
  2082
this tree and the $y$ by the right-left alternative. This suggests to
Chengsong
parents:
diff changeset
  2083
record this matching as
Chengsong
parents:
diff changeset
  2084
Chengsong
parents:
diff changeset
  2085
\begin{center}
Chengsong
parents:
diff changeset
  2086
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
Chengsong
parents:
diff changeset
  2087
\end{center}
Chengsong
parents:
diff changeset
  2088
Chengsong
parents:
diff changeset
  2089
\noindent
Chengsong
parents:
diff changeset
  2090
where $\Stars \; [\ldots]$ records all the
Chengsong
parents:
diff changeset
  2091
iterations; and $\Left$, respectively $\Right$, which
Chengsong
parents:
diff changeset
  2092
alternative is used. The value for
Chengsong
parents:
diff changeset
  2093
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
Chengsong
parents:
diff changeset
  2094
would look as follows
Chengsong
parents:
diff changeset
  2095
Chengsong
parents:
diff changeset
  2096
\begin{center}
Chengsong
parents:
diff changeset
  2097
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
Chengsong
parents:
diff changeset
  2098
\end{center}
Chengsong
parents:
diff changeset
  2099
Chengsong
parents:
diff changeset
  2100
\noindent
Chengsong
parents:
diff changeset
  2101
where $\Stars$ has only a single-element list for the single iteration
Chengsong
parents:
diff changeset
  2102
and $\Seq$ indicates that $xy$ is matched by a sequence regular
Chengsong
parents:
diff changeset
  2103
expression.
Chengsong
parents:
diff changeset
  2104
Chengsong
parents:
diff changeset
  2105
The contribution of Sulzmann and Lu is an extension of Brzozowski's
Chengsong
parents:
diff changeset
  2106
algorithm by a second phase (the first phase being building successive
Chengsong
parents:
diff changeset
  2107
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
Chengsong
parents:
diff changeset
  2108
is generated in case the regular expression matches  the string. 
Chengsong
parents:
diff changeset
  2109
Pictorially, the Sulzmann and Lu algorithm is as follows:
Chengsong
parents:
diff changeset
  2110
Chengsong
parents:
diff changeset
  2111
\begin{ceqn}
Chengsong
parents:
diff changeset
  2112
\begin{equation}\label{graph:2}
Chengsong
parents:
diff changeset
  2113
\begin{tikzcd}
Chengsong
parents:
diff changeset
  2114
r_0 \arrow[r, "\backslash c_0"]  \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
Chengsong
parents:
diff changeset
  2115
v_0           & v_1 \arrow[l,"inj_{r_0} c_0"]                & v_2 \arrow[l, "inj_{r_1} c_1"]              & v_n \arrow[l, dashed]         
Chengsong
parents:
diff changeset
  2116
\end{tikzcd}
Chengsong
parents:
diff changeset
  2117
\end{equation}
Chengsong
parents:
diff changeset
  2118
\end{ceqn}
Chengsong
parents:
diff changeset
  2119
Chengsong
parents:
diff changeset
  2120
\noindent
Chengsong
parents:
diff changeset
  2121
For convenience, we shall employ the following notations: the regular
Chengsong
parents:
diff changeset
  2122
expression we start with is $r_0$, and the given string $s$ is composed
Chengsong
parents:
diff changeset
  2123
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
Chengsong
parents:
diff changeset
  2124
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
Chengsong
parents:
diff changeset
  2125
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
Chengsong
parents:
diff changeset
  2126
the derivative $r_n$. We test whether this derivative is
Chengsong
parents:
diff changeset
  2127
$\textit{nullable}$ or not. If not, we know the string does not match
Chengsong
parents:
diff changeset
  2128
$r$ and no value needs to be generated. If yes, we start building the
Chengsong
parents:
diff changeset
  2129
values incrementally by \emph{injecting} back the characters into the
Chengsong
parents:
diff changeset
  2130
earlier values $v_n, \ldots, v_0$. This is the second phase of the
Chengsong
parents:
diff changeset
  2131
algorithm from the right to left. For the first value $v_n$, we call the
Chengsong
parents:
diff changeset
  2132
function $\textit{mkeps}$, which builds the lexical value
Chengsong
parents:
diff changeset
  2133
for how the empty string has been matched by the (nullable) regular
Chengsong
parents:
diff changeset
  2134
expression $r_n$. This function is defined as
Chengsong
parents:
diff changeset
  2135
Chengsong
parents:
diff changeset
  2136
	\begin{center}
Chengsong
parents:
diff changeset
  2137
		\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2138
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
Chengsong
parents:
diff changeset
  2139
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
Chengsong
parents:
diff changeset
  2140
			& \textit{if} $\nullable(r_{1})$\\ 
Chengsong
parents:
diff changeset
  2141
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
Chengsong
parents:
diff changeset
  2142
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
Chengsong
parents:
diff changeset
  2143
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
Chengsong
parents:
diff changeset
  2144
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
Chengsong
parents:
diff changeset
  2145
		\end{tabular}
Chengsong
parents:
diff changeset
  2146
	\end{center}
Chengsong
parents:
diff changeset
  2147
Chengsong
parents:
diff changeset
  2148
Chengsong
parents:
diff changeset
  2149
\noindent There are no cases for $\ZERO$ and $c$, since
Chengsong
parents:
diff changeset
  2150
these regular expression cannot match the empty string. Note
Chengsong
parents:
diff changeset
  2151
also that in case of alternatives we give preference to the
Chengsong
parents:
diff changeset
  2152
regular expression on the left-hand side. This will become
Chengsong
parents:
diff changeset
  2153
important later on about what value is calculated.
Chengsong
parents:
diff changeset
  2154
Chengsong
parents:
diff changeset
  2155
After the $\mkeps$-call, we inject back the characters one by one in order to build
Chengsong
parents:
diff changeset
  2156
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
Chengsong
parents:
diff changeset
  2157
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
Chengsong
parents:
diff changeset
  2158
After injecting back $n$ characters, we get the lexical value for how $r_0$
Chengsong
parents:
diff changeset
  2159
matches $s$. For this Sulzmann and Lu defined a function that reverses
Chengsong
parents:
diff changeset
  2160
the ``chopping off'' of characters during the derivative phase. The
Chengsong
parents:
diff changeset
  2161
corresponding function is called \emph{injection}, written
Chengsong
parents:
diff changeset
  2162
$\textit{inj}$; it takes three arguments: the first one is a regular
Chengsong
parents:
diff changeset
  2163
expression ${r_{i-1}}$, before the character is chopped off, the second
Chengsong
parents:
diff changeset
  2164
is a character ${c_{i-1}}$, the character we want to inject and the
Chengsong
parents:
diff changeset
  2165
third argument is the value ${v_i}$, into which one wants to inject the
Chengsong
parents:
diff changeset
  2166
character (it corresponds to the regular expression after the character
Chengsong
parents:
diff changeset
  2167
has been chopped off). The result of this function is a new value. The
Chengsong
parents:
diff changeset
  2168
definition of $\textit{inj}$ is as follows: 
Chengsong
parents:
diff changeset
  2169
Chengsong
parents:
diff changeset
  2170
\begin{center}
Chengsong
parents:
diff changeset
  2171
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
Chengsong
parents:
diff changeset
  2172
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
Chengsong
parents:
diff changeset
  2173
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
Chengsong
parents:
diff changeset
  2174
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents:
diff changeset
  2175
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents:
diff changeset
  2176
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents:
diff changeset
  2177
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents:
diff changeset
  2178
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
Chengsong
parents:
diff changeset
  2179
\end{tabular}
Chengsong
parents:
diff changeset
  2180
\end{center}
Chengsong
parents:
diff changeset
  2181
Chengsong
parents:
diff changeset
  2182
\noindent This definition is by recursion on the ``shape'' of regular
Chengsong
parents:
diff changeset
  2183
expressions and values. To understands this definition better consider
Chengsong
parents:
diff changeset
  2184
the situation when we build the derivative on regular expression $r_{i-1}$.
Chengsong
parents:
diff changeset
  2185
For this we chop off a character from $r_{i-1}$ to form $r_i$. This leaves a
Chengsong
parents:
diff changeset
  2186
``hole'' in $r_i$ and its corresponding value $v_i$. 
Chengsong
parents:
diff changeset
  2187
To calculate $v_{i-1}$, we need to
Chengsong
parents:
diff changeset
  2188
locate where that hole is and fill it. 
Chengsong
parents:
diff changeset
  2189
We can find this location by
Chengsong
parents:
diff changeset
  2190
comparing $r_{i-1}$ and $v_i$. For instance, if $r_{i-1}$ is of shape
Chengsong
parents:
diff changeset
  2191
$r_a \cdot r_b$, and $v_i$ is of shape $\Left(Seq(v_1,v_2))$, we know immediately that 
Chengsong
parents:
diff changeset
  2192
%
Chengsong
parents:
diff changeset
  2193
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b \,+\, r_b\backslash c,\]
Chengsong
parents:
diff changeset
  2194
Chengsong
parents:
diff changeset
  2195
\noindent
Chengsong
parents:
diff changeset
  2196
otherwise if $r_a$ is not nullable,
Chengsong
parents:
diff changeset
  2197
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b,\]
Chengsong
parents:
diff changeset
  2198
Chengsong
parents:
diff changeset
  2199
\noindent
Chengsong
parents:
diff changeset
  2200
the value $v_i$ should be  $\Seq(\ldots)$, contradicting the fact that
Chengsong
parents:
diff changeset
  2201
$v_i$ is actually of shape $\Left(\ldots)$. Furthermore, since $v_i$ is of shape
Chengsong
parents:
diff changeset
  2202
$\Left(\ldots)$ instead of $\Right(\ldots)$, we know that the left
Chengsong
parents:
diff changeset
  2203
branch of \[ (r_a \cdot r_b)\backslash c =
Chengsong
parents:
diff changeset
  2204
\bold{\underline{ (r_a\backslash c) \cdot r_b} }\,+\, r_b\backslash c,\](underlined)
Chengsong
parents:
diff changeset
  2205
 is taken instead of the right one. This means $c$ is chopped off 
Chengsong
parents:
diff changeset
  2206
from $r_a$ rather than $r_b$.
Chengsong
parents:
diff changeset
  2207
We have therefore found out 
Chengsong
parents:
diff changeset
  2208
that the hole will be on $r_a$. So we recursively call $\inj\, 
Chengsong
parents:
diff changeset
  2209
r_a\,c\,v_a$ to fill that hole in $v_a$. After injection, the value 
Chengsong
parents:
diff changeset
  2210
$v_i$ for $r_i = r_a \cdot r_b$ should be $\Seq\,(\inj\,r_a\,c\,v_a)\,v_b$.
Chengsong
parents:
diff changeset
  2211
Other clauses can be understood in a similar way.
Chengsong
parents:
diff changeset
  2212
Chengsong
parents:
diff changeset
  2213
%\comment{Other word: insight?}
Chengsong
parents:
diff changeset
  2214
The following example gives an insight of $\textit{inj}$'s effect and
Chengsong
parents:
diff changeset
  2215
how Sulzmann and Lu's algorithm works as a whole. Suppose we have a
Chengsong
parents:
diff changeset
  2216
regular expression $((((a+b)+ab)+c)+abc)^*$, and want to match it
Chengsong
parents:
diff changeset
  2217
against the string $abc$ (when $abc$ is written as a regular expression,
Chengsong
parents:
diff changeset
  2218
the standard way of expressing it is $a \cdot (b \cdot c)$. But we
Chengsong
parents:
diff changeset
  2219
usually omit the parentheses and dots here for better readability. This
Chengsong
parents:
diff changeset
  2220
algorithm returns a POSIX value, which means it will produce the longest
Chengsong
parents:
diff changeset
  2221
matching. Consequently, it matches the string $abc$ in one star
Chengsong
parents:
diff changeset
  2222
iteration, using the longest alternative $abc$ in the sub-expression (we shall use $r$ to denote this
Chengsong
parents:
diff changeset
  2223
sub-expression for conciseness):
Chengsong
parents:
diff changeset
  2224
Chengsong
parents:
diff changeset
  2225
\[((((a+b)+ab)+c)+\underbrace{abc}_r)\] 
Chengsong
parents:
diff changeset
  2226
Chengsong
parents:
diff changeset
  2227
\noindent
Chengsong
parents:
diff changeset
  2228
Before $\textit{inj}$ is called, our lexer first builds derivative using
Chengsong
parents:
diff changeset
  2229
string $abc$ (we simplified some regular expressions like $\ZERO \cdot
Chengsong
parents:
diff changeset
  2230
b$ to $\ZERO$ for conciseness; we also omit parentheses if they are
Chengsong
parents:
diff changeset
  2231
clear from the context):
Chengsong
parents:
diff changeset
  2232
Chengsong
parents:
diff changeset
  2233
%Similarly, we allow
Chengsong
parents:
diff changeset
  2234
%$\textit{ALT}$ to take a list of regular expressions as an argument
Chengsong
parents:
diff changeset
  2235
%instead of just 2 operands to reduce the nested depth of
Chengsong
parents:
diff changeset
  2236
%$\textit{ALT}$
Chengsong
parents:
diff changeset
  2237
Chengsong
parents:
diff changeset
  2238
\begin{center}
Chengsong
parents:
diff changeset
  2239
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2240
$r^*$ & $\xrightarrow{\backslash a}$ & $r_1 = (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r^*$\\
Chengsong
parents:
diff changeset
  2241
      & $\xrightarrow{\backslash b}$ & $r_2 = (\ZERO+\ZERO+\ONE \cdot \ONE + \ZERO + \ONE \cdot \ONE \cdot c) \cdot r^* +(\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^*$\\
Chengsong
parents:
diff changeset
  2242
      & $\xrightarrow{\backslash c}$ & $r_3 = ((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot \ONE) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^*) + $\\ 
Chengsong
parents:
diff changeset
  2243
      &                              & $\phantom{r_3 = (} ((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* )$
Chengsong
parents:
diff changeset
  2244
\end{tabular}
Chengsong
parents:
diff changeset
  2245
\end{center}
Chengsong
parents:
diff changeset
  2246
Chengsong
parents:
diff changeset
  2247
\noindent
Chengsong
parents:
diff changeset
  2248
In  case $r_3$ is nullable, we can call $\textit{mkeps}$ 
Chengsong
parents:
diff changeset
  2249
to construct a lexical value for how $r_3$ matched the string $abc$. 
Chengsong
parents:
diff changeset
  2250
This function gives the following value $v_3$: 
Chengsong
parents:
diff changeset
  2251
Chengsong
parents:
diff changeset
  2252
Chengsong
parents:
diff changeset
  2253
\begin{center}
Chengsong
parents:
diff changeset
  2254
$\Left(\Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty,\Empty))), \Stars [])))$
Chengsong
parents:
diff changeset
  2255
\end{center}
Chengsong
parents:
diff changeset
  2256
The outer $\Left(\Left(\ldots))$ tells us the leftmost nullable part of $r_3$(underlined):
Chengsong
parents:
diff changeset
  2257
Chengsong
parents:
diff changeset
  2258
\begin{center}
Chengsong
parents:
diff changeset
  2259
	\begin{tabular}{l@{\hspace{2mm}}l}
Chengsong
parents:
diff changeset
  2260
    & $\big(\underline{(\ZERO+\ZERO+\ZERO+ \ZERO+ \ONE \cdot \ONE \cdot \ONE) \cdot r^*} 
Chengsong
parents:
diff changeset
  2261
    \;+\; (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*\big)$ \smallskip\\
Chengsong
parents:
diff changeset
  2262
    $+$ & $\big((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^*
Chengsong
parents:
diff changeset
  2263
    \;+\; (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* \big)$
Chengsong
parents:
diff changeset
  2264
  	\end{tabular}
Chengsong
parents:
diff changeset
  2265
 \end{center}
Chengsong
parents:
diff changeset
  2266
Chengsong
parents:
diff changeset
  2267
\noindent
Chengsong
parents:
diff changeset
  2268
 Note that the leftmost location of term $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot
Chengsong
parents:
diff changeset
  2269
 \ONE) \cdot r^*$ (which corresponds to the initial sub-match $abc$) allows
Chengsong
parents:
diff changeset
  2270
 $\textit{mkeps}$ to pick it up because $\textit{mkeps}$ is defined to always choose the
Chengsong
parents:
diff changeset
  2271
 left one when it is nullable. In the case of this example, $abc$ is
Chengsong
parents:
diff changeset
  2272
 preferred over $a$ or $ab$. This $\Left(\Left(\ldots))$ location is
Chengsong
parents:
diff changeset
  2273
 generated by two applications of the splitting clause
Chengsong
parents:
diff changeset
  2274
Chengsong
parents:
diff changeset
  2275
\begin{center}
Chengsong
parents:
diff changeset
  2276
     $(r_1 \cdot r_2)\backslash c  \;\;(when \; r_1 \; nullable) \, = (r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c.$
Chengsong
parents:
diff changeset
  2277
\end{center}
Chengsong
parents:
diff changeset
  2278
       
Chengsong
parents:
diff changeset
  2279
\noindent
Chengsong
parents:
diff changeset
  2280
By this clause, we put $r_1 \backslash c \cdot r_2 $ at the
Chengsong
parents:
diff changeset
  2281
$\textit{front}$ and $r_2 \backslash c$ at the $\textit{back}$. This
Chengsong
parents:
diff changeset
  2282
allows $\textit{mkeps}$ to always pick up among two matches the one with a longer
Chengsong
parents:
diff changeset
  2283
initial sub-match. Removing the outside $\Left(\Left(...))$, the inside
Chengsong
parents:
diff changeset
  2284
sub-value 
Chengsong
parents:
diff changeset
  2285
 
Chengsong
parents:
diff changeset
  2286
\begin{center}
Chengsong
parents:
diff changeset
  2287
 $\Seq(\Right(\Seq(\Empty, \Seq(\Empty, \Empty))), \Stars [])$
Chengsong
parents:
diff changeset
  2288
\end{center}
Chengsong
parents:
diff changeset
  2289
Chengsong
parents:
diff changeset
  2290
\noindent
Chengsong
parents:
diff changeset
  2291
tells us how the empty string $[]$ is matched with $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot
Chengsong
parents:
diff changeset
  2292
\ONE \cdot \ONE) \cdot r^*$. We match $[]$ by a sequence of two nullable regular
Chengsong
parents:
diff changeset
  2293
expressions. The first one is an alternative, we take the rightmost
Chengsong
parents:
diff changeset
  2294
alternative---whose language contains the empty string. The second
Chengsong
parents:
diff changeset
  2295
nullable regular expression is a Kleene star. $\Stars$ tells us how it
Chengsong
parents:
diff changeset
  2296
generates the nullable regular expression: by 0 iterations to form
Chengsong
parents:
diff changeset
  2297
$\ONE$. Now $\textit{inj}$ injects characters back and incrementally
Chengsong
parents:
diff changeset
  2298
builds a lexical value based on $v_3$. Using the value $v_3$, the character
Chengsong
parents:
diff changeset
  2299
c, and the regular expression $r_2$, we can recover how $r_2$ matched
Chengsong
parents:
diff changeset
  2300
the string $[c]$ : $\textit{inj} \; r_2 \; c \; v_3$ gives us
Chengsong
parents:
diff changeset
  2301
 \begin{center}
Chengsong
parents:
diff changeset
  2302
 $v_2 = \Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty, c))), \Stars [])),$
Chengsong
parents:
diff changeset
  2303
 \end{center}
Chengsong
parents:
diff changeset
  2304
which tells us how $r_2$ matched $[c]$. After this we inject back the character $b$, and get
Chengsong
parents:
diff changeset
  2305
\begin{center}
Chengsong
parents:
diff changeset
  2306
$v_1 = \Seq(\Right(\Seq(\Empty, \Seq(b, c))), \Stars [])$
Chengsong
parents:
diff changeset
  2307
\end{center}
Chengsong
parents:
diff changeset
  2308
 for how 
Chengsong
parents:
diff changeset
  2309
 \begin{center}
Chengsong
parents:
diff changeset
  2310
 $r_1= (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r*$
Chengsong
parents:
diff changeset
  2311
 \end{center}
Chengsong
parents:
diff changeset
  2312
  matched  the string $bc$ before it split into two substrings. 
Chengsong
parents:
diff changeset
  2313
  Finally, after injecting character $a$ back to $v_1$, 
Chengsong
parents:
diff changeset
  2314
  we get  the lexical value tree 
Chengsong
parents:
diff changeset
  2315
  \begin{center}
Chengsong
parents:
diff changeset
  2316
  $v_0= \Stars [\Right(\Seq(a, \Seq(b, c)))]$
Chengsong
parents:
diff changeset
  2317
  \end{center}
Chengsong
parents:
diff changeset
  2318
   for how $r$ matched $abc$. This completes the algorithm.
Chengsong
parents:
diff changeset
  2319
   
Chengsong
parents:
diff changeset
  2320
%We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. 
Chengsong
parents:
diff changeset
  2321
Readers might have noticed that the lexical value information is actually
Chengsong
parents:
diff changeset
  2322
already available when doing derivatives. For example, immediately after
Chengsong
parents:
diff changeset
  2323
the operation $\backslash a$ we know that if we want to match a string
Chengsong
parents:
diff changeset
  2324
that starts with $a$, we can either take the initial match to be 
Chengsong
parents:
diff changeset
  2325
Chengsong
parents:
diff changeset
  2326
 \begin{center}
Chengsong
parents:
diff changeset
  2327
\begin{enumerate}
Chengsong
parents:
diff changeset
  2328
    \item[1)] just $a$ or
Chengsong
parents:
diff changeset
  2329
    \item[2)] string $ab$ or 
Chengsong
parents:
diff changeset
  2330
    \item[3)] string $abc$.
Chengsong
parents:
diff changeset
  2331
\end{enumerate}
Chengsong
parents:
diff changeset
  2332
\end{center}
Chengsong
parents:
diff changeset
  2333
Chengsong
parents:
diff changeset
  2334
\noindent
Chengsong
parents:
diff changeset
  2335
In order to differentiate between these choices, we just need to
Chengsong
parents:
diff changeset
  2336
remember their positions---$a$ is on the left, $ab$ is in the middle ,
Chengsong
parents:
diff changeset
  2337
and $abc$ is on the right. Which of these alternatives is chosen
Chengsong
parents:
diff changeset
  2338
later does not affect their relative position because the algorithm does
Chengsong
parents:
diff changeset
  2339
not change this order. If this parsing information can be determined and
Chengsong
parents:
diff changeset
  2340
does not change because of later derivatives, there is no point in
Chengsong
parents:
diff changeset
  2341
traversing this information twice. This leads to an optimisation---if we
Chengsong
parents:
diff changeset
  2342
store the information for lexical values inside the regular expression,
Chengsong
parents:
diff changeset
  2343
update it when we do derivative on them, and collect the information
Chengsong
parents:
diff changeset
  2344
when finished with derivatives and call $\textit{mkeps}$ for deciding which
Chengsong
parents:
diff changeset
  2345
branch is POSIX, we can generate the lexical value in one pass, instead of
Chengsong
parents:
diff changeset
  2346
doing the rest $n$ injections. This leads to Sulzmann and Lu's novel
Chengsong
parents:
diff changeset
  2347
idea of using bitcodes in derivatives.
Chengsong
parents:
diff changeset
  2348
Chengsong
parents:
diff changeset
  2349
In the next section, we shall focus on the bitcoded algorithm and the
Chengsong
parents:
diff changeset
  2350
process of simplification of regular expressions. This is needed in
Chengsong
parents:
diff changeset
  2351
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
Chengsong
parents:
diff changeset
  2352
and Lu's algorithms.  This is where the PhD-project aims to advance the
Chengsong
parents:
diff changeset
  2353
state-of-the-art.
Chengsong
parents:
diff changeset
  2354
Chengsong
parents:
diff changeset
  2355
Chengsong
parents:
diff changeset
  2356
\section{Simplification of Regular Expressions}
Chengsong
parents:
diff changeset
  2357
Chengsong
parents:
diff changeset
  2358
Using bitcodes to guide  parsing is not a novel idea. It was applied to
Chengsong
parents:
diff changeset
  2359
context free grammars and then adapted by Henglein and Nielson for
Chengsong
parents:
diff changeset
  2360
efficient regular expression  lexing using DFAs~\cite{nielson11bcre}.
Chengsong
parents:
diff changeset
  2361
Sulzmann and Lu took this idea of bitcodes a step further by integrating
Chengsong
parents:
diff changeset
  2362
bitcodes into derivatives. The reason why we want to use bitcodes in
Chengsong
parents:
diff changeset
  2363
this project is that we want to introduce more aggressive simplification
Chengsong
parents:
diff changeset
  2364
rules in order to keep the size of derivatives small throughout. This is
Chengsong
parents:
diff changeset
  2365
because the main drawback of building successive derivatives according
Chengsong
parents:
diff changeset
  2366
to Brzozowski's definition is that they can grow very quickly in size.
Chengsong
parents:
diff changeset
  2367
This is mainly due to the fact that the derivative operation generates
Chengsong
parents:
diff changeset
  2368
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result, if
Chengsong
parents:
diff changeset
  2369
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
Chengsong
parents:
diff changeset
  2370
are excruciatingly slow. For example when starting with the regular
Chengsong
parents:
diff changeset
  2371
expression $(a + aa)^*$ and building 12 successive derivatives
Chengsong
parents:
diff changeset
  2372
w.r.t.~the character $a$, one obtains a derivative regular expression
Chengsong
parents:
diff changeset
  2373
with more than 8000 nodes (when viewed as a tree). Operations like
Chengsong
parents:
diff changeset
  2374
$\textit{der}$ and $\nullable$ need to traverse such trees and
Chengsong
parents:
diff changeset
  2375
consequently the bigger the size of the derivative the slower the
Chengsong
parents:
diff changeset
  2376
algorithm. 
Chengsong
parents:
diff changeset
  2377
Chengsong
parents:
diff changeset
  2378
Fortunately, one can simplify regular expressions after each derivative
Chengsong
parents:
diff changeset
  2379
step. Various simplifications of regular expressions are possible, such
Chengsong
parents:
diff changeset
  2380
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
Chengsong
parents:
diff changeset
  2381
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
Chengsong
parents:
diff changeset
  2382
affect the answer for whether a regular expression matches a string or
Chengsong
parents:
diff changeset
  2383
not, but fortunately also do not affect the POSIX strategy of how
Chengsong
parents:
diff changeset
  2384
regular expressions match strings---although the latter is much harder
Chengsong
parents:
diff changeset
  2385
to establish. Some initial results in this regard have been
Chengsong
parents:
diff changeset
  2386
obtained in \cite{AusafDyckhoffUrban2016}. 
Chengsong
parents:
diff changeset
  2387
Chengsong
parents:
diff changeset
  2388
Unfortunately, the simplification rules outlined above  are not
Chengsong
parents:
diff changeset
  2389
sufficient to prevent a size explosion in all cases. We
Chengsong
parents:
diff changeset
  2390
believe a tighter bound can be achieved that prevents an explosion in
Chengsong
parents:
diff changeset
  2391
\emph{all} cases. Such a tighter bound is suggested by work of Antimirov who
Chengsong
parents:
diff changeset
  2392
proved that (partial) derivatives can be bound by the number of
Chengsong
parents:
diff changeset
  2393
characters contained in the initial regular expression
Chengsong
parents:
diff changeset
  2394
\cite{Antimirov95}. He defined the \emph{partial derivatives} of regular
Chengsong
parents:
diff changeset
  2395
expressions as follows:
Chengsong
parents:
diff changeset
  2396
Chengsong
parents:
diff changeset
  2397
\begin{center}
Chengsong
parents:
diff changeset
  2398
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2399
 $\textit{pder} \; c \; \ZERO$ & $\dn$ & $\emptyset$\\
Chengsong
parents:
diff changeset
  2400
 $\textit{pder} \; c \; \ONE$ & $\dn$ & $\emptyset$ \\
Chengsong
parents:
diff changeset
  2401
 $\textit{pder} \; c \; d$ & $\dn$ & $\textit{if} \; c \,=\, d \; \{  \ONE   \}  \; \textit{else} \; \emptyset$ \\ 
Chengsong
parents:
diff changeset
  2402
  $\textit{pder} \; c \; r_1+r_2$ & $\dn$ & $pder \; c \; r_1 \cup pder \; c \;  r_2$ \\
Chengsong
parents:
diff changeset
  2403
   $\textit{pder} \; c \; r_1 \cdot r_2$ & $\dn$ & $\textit{if} \; nullable \; r_1 $\\
Chengsong
parents:
diff changeset
  2404
     & & $\textit{then} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \}  \cup pder \; c \; r_2 \;$\\
Chengsong
parents:
diff changeset
  2405
     & & $\textit{else} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \} $ \\ 
Chengsong
parents:
diff changeset
  2406
     $\textit{pder} \; c \; r^*$ & $\dn$ & $ \{  r' \cdot r^* \mid r' \in pder \; c \; r   \}  $ \\  
Chengsong
parents:
diff changeset
  2407
 \end{tabular}
Chengsong
parents:
diff changeset
  2408
 \end{center}
Chengsong
parents:
diff changeset
  2409
Chengsong
parents:
diff changeset
  2410
\noindent
Chengsong
parents:
diff changeset
  2411
A partial derivative of a regular expression $r$ is essentially a set of
Chengsong
parents:
diff changeset
  2412
regular expressions that are either $r$'s children expressions or a
Chengsong
parents:
diff changeset
  2413
concatenation of them. Antimirov has proved a tight bound of the sum of
Chengsong
parents:
diff changeset
  2414
the size of \emph{all} partial derivatives no matter what the string
Chengsong
parents:
diff changeset
  2415
looks like. Roughly speaking the size sum will be at most cubic in the
Chengsong
parents:
diff changeset
  2416
size of the regular expression.
Chengsong
parents:
diff changeset
  2417
Chengsong
parents:
diff changeset
  2418
If we want the size of derivatives in Sulzmann and Lu's algorithm to
Chengsong
parents:
diff changeset
  2419
stay below this bound, we would need more aggressive simplifications.
Chengsong
parents:
diff changeset
  2420
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
Chengsong
parents:
diff changeset
  2421
deleting duplicates whenever possible. For example, the parentheses in
Chengsong
parents:
diff changeset
  2422
$(a+b) \cdot c + bc$ can be opened up to get $a\cdot c +  b \cdot c + b
Chengsong
parents:
diff changeset
  2423
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
Chengsong
parents:
diff changeset
  2424
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
Chengsong
parents:
diff changeset
  2425
$a^*+a+\ONE$. Adding these more aggressive simplification rules helps us
Chengsong
parents:
diff changeset
  2426
to achieve the same size bound as that of the partial derivatives. 
Chengsong
parents:
diff changeset
  2427
Chengsong
parents:
diff changeset
  2428
In order to implement the idea of ``spilling out alternatives'' and to
Chengsong
parents:
diff changeset
  2429
make them compatible with the $\text{inj}$-mechanism, we use
Chengsong
parents:
diff changeset
  2430
\emph{bitcodes}. Bits and bitcodes (lists of bits) are just:
Chengsong
parents:
diff changeset
  2431
Chengsong
parents:
diff changeset
  2432
%This allows us to prove a tight
Chengsong
parents:
diff changeset
  2433
%bound on the size of regular expression during the running time of the
Chengsong
parents:
diff changeset
  2434
%algorithm if we can establish the connection between our simplification
Chengsong
parents:
diff changeset
  2435
%rules and partial derivatives.
Chengsong
parents:
diff changeset
  2436
Chengsong
parents:
diff changeset
  2437
 %We believe, and have generated test
Chengsong
parents:
diff changeset
  2438
%data, that a similar bound can be obtained for the derivatives in
Chengsong
parents:
diff changeset
  2439
%Sulzmann and Lu's algorithm. Let us give some details about this next.
Chengsong
parents:
diff changeset
  2440
Chengsong
parents:
diff changeset
  2441
Chengsong
parents:
diff changeset
  2442
\begin{center}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2443
		$b ::=   0 \mid  1 \qquad
94
Chengsong
parents:
diff changeset
  2444
bs ::= [] \mid b:bs    
Chengsong
parents:
diff changeset
  2445
$
Chengsong
parents:
diff changeset
  2446
\end{center}
Chengsong
parents:
diff changeset
  2447
Chengsong
parents:
diff changeset
  2448
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2449
The $0$ and $1$ are arbitrary names for the bits  and not in bold
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2450
in order to avoid 
94
Chengsong
parents:
diff changeset
  2451
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
Chengsong
parents:
diff changeset
  2452
bit-lists) can be used to encode values (or incomplete values) in a
Chengsong
parents:
diff changeset
  2453
compact form. This can be straightforwardly seen in the following
Chengsong
parents:
diff changeset
  2454
coding function from values to bitcodes: 
Chengsong
parents:
diff changeset
  2455
Chengsong
parents:
diff changeset
  2456
\begin{center}
Chengsong
parents:
diff changeset
  2457
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2458
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
Chengsong
parents:
diff changeset
  2459
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2460
  $\textit{code}(\Left\,v)$ & $\dn$ & $0 :: code(v)$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2461
  $\textit{code}(\Right\,v)$ & $\dn$ & $1 :: code(v)$\\
94
Chengsong
parents:
diff changeset
  2462
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2463
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[0]$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2464
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $1 :: code(v) \;@\;
94
Chengsong
parents:
diff changeset
  2465
                                                 code(\Stars\,vs)$
Chengsong
parents:
diff changeset
  2466
\end{tabular}    
Chengsong
parents:
diff changeset
  2467
\end{center} 
Chengsong
parents:
diff changeset
  2468
Chengsong
parents:
diff changeset
  2469
\noindent
Chengsong
parents:
diff changeset
  2470
Here $\textit{code}$ encodes a value into a bitcodes by converting
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2471
$\Left$ into $0$, $\Right$ into $1$, the start point of a non-empty
94
Chengsong
parents:
diff changeset
  2472
star iteration into $\S$, and the border where a local star terminates
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2473
into $0$. This coding is lossy, as it throws away the information about
94
Chengsong
parents:
diff changeset
  2474
characters, and also does not encode the ``boundary'' between two
Chengsong
parents:
diff changeset
  2475
sequence values. Moreover, with only the bitcode we cannot even tell
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2476
whether the $1$s and $0$s are for $\Left/\Right$ or $\Stars$. The
94
Chengsong
parents:
diff changeset
  2477
reason for choosing this compact way of storing information is that the
Chengsong
parents:
diff changeset
  2478
relatively small size of bits can be easily manipulated and ``moved
Chengsong
parents:
diff changeset
  2479
around'' in a regular expression. In order to recover values, we will 
Chengsong
parents:
diff changeset
  2480
need the corresponding regular expression as an extra information. This
Chengsong
parents:
diff changeset
  2481
means the decoding function is defined as:
Chengsong
parents:
diff changeset
  2482
Chengsong
parents:
diff changeset
  2483
Chengsong
parents:
diff changeset
  2484
%\begin{definition}[Bitdecoding of Values]\mbox{}
Chengsong
parents:
diff changeset
  2485
\begin{center}
Chengsong
parents:
diff changeset
  2486
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
Chengsong
parents:
diff changeset
  2487
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
Chengsong
parents:
diff changeset
  2488
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2489
  $\textit{decode}'\,(0\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
94
Chengsong
parents:
diff changeset
  2490
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
Chengsong
parents:
diff changeset
  2491
       (\Left\,v, bs_1)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2492
  $\textit{decode}'\,(1\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
94
Chengsong
parents:
diff changeset
  2493
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
Chengsong
parents:
diff changeset
  2494
       (\Right\,v, bs_1)$\\                           
Chengsong
parents:
diff changeset
  2495
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2496
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
Chengsong
parents:
diff changeset
  2497
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
Chengsong
parents:
diff changeset
  2498
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2499
  $\textit{decode}'\,(0\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2500
  $\textit{decode}'\,(1\!::\!bs)\,(r^*)$ & $\dn$ & 
94
Chengsong
parents:
diff changeset
  2501
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents:
diff changeset
  2502
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
Chengsong
parents:
diff changeset
  2503
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
Chengsong
parents:
diff changeset
  2504
  
Chengsong
parents:
diff changeset
  2505
  $\textit{decode}\,bs\,r$ & $\dn$ &
Chengsong
parents:
diff changeset
  2506
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents:
diff changeset
  2507
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
Chengsong
parents:
diff changeset
  2508
       \textit{else}\;\textit{None}$                       
Chengsong
parents:
diff changeset
  2509
\end{tabular}    
Chengsong
parents:
diff changeset
  2510
\end{center}    
Chengsong
parents:
diff changeset
  2511
%\end{definition}
Chengsong
parents:
diff changeset
  2512
Chengsong
parents:
diff changeset
  2513
Sulzmann and Lu's integrated the bitcodes into regular expressions to
Chengsong
parents:
diff changeset
  2514
create annotated regular expressions \cite{Sulzmann2014}.
Chengsong
parents:
diff changeset
  2515
\emph{Annotated regular expressions} are defined by the following
Chengsong
parents:
diff changeset
  2516
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
Chengsong
parents:
diff changeset
  2517
Chengsong
parents:
diff changeset
  2518
\begin{center}
Chengsong
parents:
diff changeset
  2519
\begin{tabular}{lcl}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2520
  $\textit{a}$ & $::=$  & $\ZERO$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2521
                  & $\mid$ & $_{bs}\ONE$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2522
                  & $\mid$ & $_{bs}{\bf c}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2523
                  & $\mid$ & $_{bs}\oplus \textit{as}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2524
                  & $\mid$ & $_{bs}a_1\cdot a_2$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2525
                  & $\mid$ & $_{bs}a^*$
94
Chengsong
parents:
diff changeset
  2526
\end{tabular}    
Chengsong
parents:
diff changeset
  2527
\end{center}  
Chengsong
parents:
diff changeset
  2528
%(in \textit{ALTS})
Chengsong
parents:
diff changeset
  2529
Chengsong
parents:
diff changeset
  2530
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2531
where $\textit{bs}$ stands for bitcodes, $a$  for $\textit{annotated}$ regular
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2532
expressions and $\textit{as}$ for a list of annotated regular expressions.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2533
The alternative constructor($\oplus$) has been generalized to 
94
Chengsong
parents:
diff changeset
  2534
accept a list of annotated regular expressions rather than just 2.
Chengsong
parents:
diff changeset
  2535
We will show that these bitcodes encode information about
Chengsong
parents:
diff changeset
  2536
the (POSIX) value that should be generated by the Sulzmann and Lu
Chengsong
parents:
diff changeset
  2537
algorithm.
Chengsong
parents:
diff changeset
  2538
Chengsong
parents:
diff changeset
  2539
Chengsong
parents:
diff changeset
  2540
To do lexing using annotated regular expressions, we shall first
Chengsong
parents:
diff changeset
  2541
transform the usual (un-annotated) regular expressions into annotated
Chengsong
parents:
diff changeset
  2542
regular expressions. This operation is called \emph{internalisation} and
Chengsong
parents:
diff changeset
  2543
defined as follows:
Chengsong
parents:
diff changeset
  2544
Chengsong
parents:
diff changeset
  2545
%\begin{definition}
Chengsong
parents:
diff changeset
  2546
\begin{center}
Chengsong
parents:
diff changeset
  2547
\begin{tabular}{lcl}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2548
  $(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2549
  $(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2550
  $(c)^\uparrow$ & $\dn$ & $_{bs}\textit{\bf c}$\\
94
Chengsong
parents:
diff changeset
  2551
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2552
  $_{[]}\oplus\,[(\textit{fuse}\,[0]\,r_1^\uparrow),\,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2553
  (\textit{fuse}\,[1]\,r_2^\uparrow)]$\\
94
Chengsong
parents:
diff changeset
  2554
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2555
         $_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
94
Chengsong
parents:
diff changeset
  2556
  $(r^*)^\uparrow$ & $\dn$ &
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2557
         $_{[]}r^\uparrow$\\
94
Chengsong
parents:
diff changeset
  2558
\end{tabular}    
Chengsong
parents:
diff changeset
  2559
\end{center}    
Chengsong
parents:
diff changeset
  2560
%\end{definition}
Chengsong
parents:
diff changeset
  2561
Chengsong
parents:
diff changeset
  2562
\noindent
Chengsong
parents:
diff changeset
  2563
We use up arrows here to indicate that the basic un-annotated regular
Chengsong
parents:
diff changeset
  2564
expressions are ``lifted up'' into something slightly more complex. In the
Chengsong
parents:
diff changeset
  2565
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
Chengsong
parents:
diff changeset
  2566
attach bits to the front of an annotated regular expression. Its
Chengsong
parents:
diff changeset
  2567
definition is as follows:
Chengsong
parents:
diff changeset
  2568
Chengsong
parents:
diff changeset
  2569
\begin{center}
Chengsong
parents:
diff changeset
  2570
\begin{tabular}{lcl}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  2571
  $\textit{fuse}\;bs\,(\ZERO)$ & $\dn$ & $\textit{ZERO}$\\
94
Chengsong
parents:
diff changeset
  2572
  $\textit{fuse}\;bs\,(\textit{ONE}\,bs')$ & $\dn$ &
Chengsong
parents:
diff changeset
  2573
     $\textit{ONE}\,(bs\,@\,bs')$\\
Chengsong
parents:
diff changeset
  2574
  $\textit{fuse}\;bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2575
     $\textit{CHAR}\,(bs\,@\,bs')\,c$\\
Chengsong
parents:
diff changeset
  2576
  $\textit{fuse}\;bs\,(\textit{ALTS}\,bs'\,as)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2577
     $\textit{ALTS}\,(bs\,@\,bs')\,as$\\
Chengsong
parents:
diff changeset
  2578
  $\textit{fuse}\;bs\,(\textit{SEQ}\,bs'\,a_1\,a_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2579
     $\textit{SEQ}\,(bs\,@\,bs')\,a_1\,a_2$\\
Chengsong
parents:
diff changeset
  2580
  $\textit{fuse}\;bs\,(\textit{STAR}\,bs'\,a)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2581
     $\textit{STAR}\,(bs\,@\,bs')\,a$
Chengsong
parents:
diff changeset
  2582
\end{tabular}    
Chengsong
parents:
diff changeset
  2583
\end{center}  
Chengsong
parents:
diff changeset
  2584
Chengsong
parents:
diff changeset
  2585
\noindent
Chengsong
parents:
diff changeset
  2586
After internalising the regular expression, we perform successive
Chengsong
parents:
diff changeset
  2587
derivative operations on the annotated regular expressions. This
Chengsong
parents:
diff changeset
  2588
derivative operation is the same as what we had previously for the
Chengsong
parents:
diff changeset
  2589
basic regular expressions, except that we beed to take care of
Chengsong
parents:
diff changeset
  2590
the bitcodes:
Chengsong
parents:
diff changeset
  2591
Chengsong
parents:
diff changeset
  2592
 %\begin{definition}{bder}
Chengsong
parents:
diff changeset
  2593
\begin{center}
Chengsong
parents:
diff changeset
  2594
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  2595
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  2596
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  2597
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  2598
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents:
diff changeset
  2599
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  2600
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  2601
  $\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
Chengsong
parents:
diff changeset
  2602
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  2603
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents:
diff changeset
  2604
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents:
diff changeset
  2605
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents:
diff changeset
  2606
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents:
diff changeset
  2607
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  2608
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
Chengsong
parents:
diff changeset
  2609
       (\textit{STAR}\,[]\,r)$
Chengsong
parents:
diff changeset
  2610
\end{tabular}    
Chengsong
parents:
diff changeset
  2611
\end{center}    
Chengsong
parents:
diff changeset
  2612
%\end{definition}
Chengsong
parents:
diff changeset
  2613
Chengsong
parents:
diff changeset
  2614
\noindent
Chengsong
parents:
diff changeset
  2615
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence,
Chengsong
parents:
diff changeset
  2616
we need to attach an additional bit $Z$ to the front of $r \backslash c$
Chengsong
parents:
diff changeset
  2617
to indicate that there is one more star iteration. Also the $SEQ$ clause
Chengsong
parents:
diff changeset
  2618
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
Chengsong
parents:
diff changeset
  2619
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
Chengsong
parents:
diff changeset
  2620
that it is for annotated regular expressions, therefore we omit the
Chengsong
parents:
diff changeset
  2621
definition). Assume that $bmkeps$ correctly extracts the bitcode for how
Chengsong
parents:
diff changeset
  2622
$a_1$ matches the string prior to character $c$ (more on this later),
Chengsong
parents:
diff changeset
  2623
then the right branch of $ALTS$, which is $fuse \; bmkeps \;  a_1 (a_2
Chengsong
parents:
diff changeset
  2624
\backslash c)$ will collapse the regular expression $a_1$(as it has
Chengsong
parents:
diff changeset
  2625
already been fully matched) and store the parsing information at the
Chengsong
parents:
diff changeset
  2626
head of the regular expression $a_2 \backslash c$ by fusing to it. The
Chengsong
parents:
diff changeset
  2627
bitsequence $bs$, which was initially attached to the head of $SEQ$, has
Chengsong
parents:
diff changeset
  2628
now been elevated to the top-level of $ALTS$, as this information will be
Chengsong
parents:
diff changeset
  2629
needed whichever way the $SEQ$ is matched---no matter whether $c$ belongs
Chengsong
parents:
diff changeset
  2630
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
Chengsong
parents:
diff changeset
  2631
the lexing information, we complete the lexing by collecting the
Chengsong
parents:
diff changeset
  2632
bitcodes using a generalised version of the $\textit{mkeps}$ function
Chengsong
parents:
diff changeset
  2633
for annotated regular expressions, called $\textit{bmkeps}$:
Chengsong
parents:
diff changeset
  2634
Chengsong
parents:
diff changeset
  2635
Chengsong
parents:
diff changeset
  2636
%\begin{definition}[\textit{bmkeps}]\mbox{}
Chengsong
parents:
diff changeset
  2637
\begin{center}
Chengsong
parents:
diff changeset
  2638
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2639
  $\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  2640
  $\textit{bmkeps}\,(\textit{ALTS}\;bs\,a::as)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2641
     $\textit{if}\;\textit{bnullable}\,a$\\
Chengsong
parents:
diff changeset
  2642
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
Chengsong
parents:
diff changeset
  2643
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(\textit{ALTS}\;bs\,as)$\\
Chengsong
parents:
diff changeset
  2644
  $\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2645
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
Chengsong
parents:
diff changeset
  2646
  $\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2647
     $bs \,@\, [\S]$
Chengsong
parents:
diff changeset
  2648
\end{tabular}    
Chengsong
parents:
diff changeset
  2649
\end{center}    
Chengsong
parents:
diff changeset
  2650
%\end{definition}
Chengsong
parents:
diff changeset
  2651
Chengsong
parents:
diff changeset
  2652
\noindent
Chengsong
parents:
diff changeset
  2653
This function completes the value information by travelling along the
Chengsong
parents:
diff changeset
  2654
path of the regular expression that corresponds to a POSIX value and
Chengsong
parents:
diff changeset
  2655
collecting all the bitcodes, and using $S$ to indicate the end of star
Chengsong
parents:
diff changeset
  2656
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
Chengsong
parents:
diff changeset
  2657
decode them, we get the value we expect. The corresponding lexing
Chengsong
parents:
diff changeset
  2658
algorithm looks as follows:
Chengsong
parents:
diff changeset
  2659
Chengsong
parents:
diff changeset
  2660
\begin{center}
Chengsong
parents:
diff changeset
  2661
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2662
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
  2663
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
  2664
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
  2665
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
  2666
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
  2667
\end{tabular}
Chengsong
parents:
diff changeset
  2668
\end{center}
Chengsong
parents:
diff changeset
  2669
Chengsong
parents:
diff changeset
  2670
\noindent
Chengsong
parents:
diff changeset
  2671
In this definition $\_\backslash s$ is the  generalisation  of the derivative
Chengsong
parents:
diff changeset
  2672
operation from characters to strings (just like the derivatives for un-annotated
Chengsong
parents:
diff changeset
  2673
regular expressions).
Chengsong
parents:
diff changeset
  2674
Chengsong
parents:
diff changeset
  2675
The main point of the bitcodes and annotated regular expressions is that
Chengsong
parents:
diff changeset
  2676
we can apply rather aggressive (in terms of size) simplification rules
Chengsong
parents:
diff changeset
  2677
in order to keep derivatives small. We have developed such
Chengsong
parents:
diff changeset
  2678
``aggressive'' simplification rules and generated test data that show
Chengsong
parents:
diff changeset
  2679
that the expected bound can be achieved. Obviously we could only
Chengsong
parents:
diff changeset
  2680
partially cover  the search space as there are infinitely many regular
Chengsong
parents:
diff changeset
  2681
expressions and strings. 
Chengsong
parents:
diff changeset
  2682
Chengsong
parents:
diff changeset
  2683
One modification we introduced is to allow a list of annotated regular
Chengsong
parents:
diff changeset
  2684
expressions in the \textit{ALTS} constructor. This allows us to not just
Chengsong
parents:
diff changeset
  2685
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
Chengsong
parents:
diff changeset
  2686
also unnecessary ``copies'' of regular expressions (very similar to
Chengsong
parents:
diff changeset
  2687
simplifying $r + r$ to just $r$, but in a more general setting). Another
Chengsong
parents:
diff changeset
  2688
modification is that we use simplification rules inspired by Antimirov's
Chengsong
parents:
diff changeset
  2689
work on partial derivatives. They maintain the idea that only the first
Chengsong
parents:
diff changeset
  2690
``copy'' of a regular expression in an alternative contributes to the
Chengsong
parents:
diff changeset
  2691
calculation of a POSIX value. All subsequent copies can be pruned away from
Chengsong
parents:
diff changeset
  2692
the regular expression. A recursive definition of our  simplification function 
Chengsong
parents:
diff changeset
  2693
that looks somewhat similar to our Scala code is given below:
Chengsong
parents:
diff changeset
  2694
%\comment{Use $\ZERO$, $\ONE$ and so on. 
Chengsong
parents:
diff changeset
  2695
%Is it $ALTS$ or $ALTS$?}\\
Chengsong
parents:
diff changeset
  2696
Chengsong
parents:
diff changeset
  2697
\begin{center}
Chengsong
parents:
diff changeset
  2698
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  2699
   
Chengsong
parents:
diff changeset
  2700
  $\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
Chengsong
parents:
diff changeset
  2701
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  2702
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  2703
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
Chengsong
parents:
diff changeset
  2704
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
Chengsong
parents:
diff changeset
  2705
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
Chengsong
parents:
diff changeset
  2706
Chengsong
parents:
diff changeset
  2707
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
Chengsong
parents:
diff changeset
  2708
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  2709
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
Chengsong
parents:
diff changeset
  2710
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
Chengsong
parents:
diff changeset
  2711
Chengsong
parents:
diff changeset
  2712
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
Chengsong
parents:
diff changeset
  2713
\end{tabular}    
Chengsong
parents:
diff changeset
  2714
\end{center}    
Chengsong
parents:
diff changeset
  2715
Chengsong
parents:
diff changeset
  2716
\noindent
Chengsong
parents:
diff changeset
  2717
The simplification does a pattern matching on the regular expression.
Chengsong
parents:
diff changeset
  2718
When it detected that the regular expression is an alternative or
Chengsong
parents:
diff changeset
  2719
sequence, it will try to simplify its children regular expressions
Chengsong
parents:
diff changeset
  2720
recursively and then see if one of the children turn into $\ZERO$ or
Chengsong
parents:
diff changeset
  2721
$\ONE$, which might trigger further simplification at the current level.
Chengsong
parents:
diff changeset
  2722
The most involved part is the $\textit{ALTS}$ clause, where we use two
Chengsong
parents:
diff changeset
  2723
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
Chengsong
parents:
diff changeset
  2724
$\textit{ALTS}$ and reduce as many duplicates as possible. Function
Chengsong
parents:
diff changeset
  2725
$\textit{distinct}$  keeps the first occurring copy only and remove all later ones
Chengsong
parents:
diff changeset
  2726
when detected duplicates. Function $\textit{flatten}$ opens up nested \textit{ALTS}.
Chengsong
parents:
diff changeset
  2727
Its recursive definition is given below:
Chengsong
parents:
diff changeset
  2728
Chengsong
parents:
diff changeset
  2729
 \begin{center}
Chengsong
parents:
diff changeset
  2730
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  2731
  $\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \;
Chengsong
parents:
diff changeset
  2732
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
Chengsong
parents:
diff changeset
  2733
  $\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \;  as' $ \\
Chengsong
parents:
diff changeset
  2734
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise) 
Chengsong
parents:
diff changeset
  2735
\end{tabular}    
Chengsong
parents:
diff changeset
  2736
\end{center}  
Chengsong
parents:
diff changeset
  2737
Chengsong
parents:
diff changeset
  2738
\noindent
Chengsong
parents:
diff changeset
  2739
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
Chengsong
parents:
diff changeset
  2740
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
Chengsong
parents:
diff changeset
  2741
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
Chengsong
parents:
diff changeset
  2742
Chengsong
parents:
diff changeset
  2743
Suppose we apply simplification after each derivative step, and view
Chengsong
parents:
diff changeset
  2744
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
Chengsong
parents:
diff changeset
  2745
\textit{simp}(a \backslash c)$. Then we can use the previous natural
Chengsong
parents:
diff changeset
  2746
extension from derivative w.r.t.~character to derivative
Chengsong
parents:
diff changeset
  2747
w.r.t.~string:%\comment{simp in  the [] case?}
Chengsong
parents:
diff changeset
  2748
Chengsong
parents:
diff changeset
  2749
\begin{center}
Chengsong
parents:
diff changeset
  2750
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2751
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
Chengsong
parents:
diff changeset
  2752
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
Chengsong
parents:
diff changeset
  2753
\end{tabular}
Chengsong
parents:
diff changeset
  2754
\end{center}
Chengsong
parents:
diff changeset
  2755
Chengsong
parents:
diff changeset
  2756
\noindent
Chengsong
parents:
diff changeset
  2757
we obtain an optimised version of the algorithm:
Chengsong
parents:
diff changeset
  2758
Chengsong
parents:
diff changeset
  2759
 \begin{center}
Chengsong
parents:
diff changeset
  2760
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2761
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
  2762
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
  2763
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
  2764
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
  2765
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
  2766
\end{tabular}
Chengsong
parents:
diff changeset
  2767
\end{center}
Chengsong
parents:
diff changeset
  2768
Chengsong
parents:
diff changeset
  2769
\noindent
Chengsong
parents:
diff changeset
  2770
This algorithm keeps the regular expression size small, for example,
Chengsong
parents:
diff changeset
  2771
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
Chengsong
parents:
diff changeset
  2772
will be reduced to just 6 and stays constant, no matter how long the
Chengsong
parents:
diff changeset
  2773
input string is.
Chengsong
parents:
diff changeset
  2774
Chengsong
parents:
diff changeset
  2775
Chengsong
parents:
diff changeset
  2776
Chengsong
parents:
diff changeset
  2777
\section{Current Work}
Chengsong
parents:
diff changeset
  2778
Chengsong
parents:
diff changeset
  2779
We are currently engaged in two tasks related to this algorithm. The
Chengsong
parents:
diff changeset
  2780
first task is proving that our simplification rules actually do not
Chengsong
parents:
diff changeset
  2781
affect the POSIX value that should be generated by the algorithm
Chengsong
parents:
diff changeset
  2782
according to the specification of a POSIX value and furthermore obtain a
Chengsong
parents:
diff changeset
  2783
much tighter bound on the sizes of derivatives. The result is that our
Chengsong
parents:
diff changeset
  2784
algorithm should be correct and faster on all inputs.  The original
Chengsong
parents:
diff changeset
  2785
blow-up, as observed in JavaScript, Python and Java, would be excluded
Chengsong
parents:
diff changeset
  2786
from happening in our algorithm. For this proof we use the theorem prover
Chengsong
parents:
diff changeset
  2787
Isabelle. Once completed, this result will advance the state-of-the-art:
Chengsong
parents:
diff changeset
  2788
Sulzmann and Lu wrote in their paper~\cite{Sulzmann2014} about the
Chengsong
parents:
diff changeset
  2789
bitcoded ``incremental parsing method'' (that is the lexing algorithm
Chengsong
parents:
diff changeset
  2790
outlined in this section):
Chengsong
parents:
diff changeset
  2791
Chengsong
parents:
diff changeset
  2792
\begin{quote}\it
Chengsong
parents:
diff changeset
  2793
  ``Correctness Claim: We further claim that the incremental parsing
Chengsong
parents:
diff changeset
  2794
  method in Figure~5 in combination with the simplification steps in
Chengsong
parents:
diff changeset
  2795
  Figure 6 yields POSIX parse tree [our lexical values]. We have tested this claim
Chengsong
parents:
diff changeset
  2796
  extensively by using the method in Figure~3 as a reference but yet
Chengsong
parents:
diff changeset
  2797
  have to work out all proof details.''
Chengsong
parents:
diff changeset
  2798
\end{quote}  
Chengsong
parents:
diff changeset
  2799
Chengsong
parents:
diff changeset
  2800
\noindent 
Chengsong
parents:
diff changeset
  2801
We like to settle this correctness claim. It is relatively
Chengsong
parents:
diff changeset
  2802
straightforward to establish that after one simplification step, the part of a
Chengsong
parents:
diff changeset
  2803
nullable derivative that corresponds to a POSIX value remains intact and can
Chengsong
parents:
diff changeset
  2804
still be collected, in other words, we can show that
Chengsong
parents:
diff changeset
  2805
%\comment{Double-check....I
Chengsong
parents:
diff changeset
  2806
%think this  is not the case}
Chengsong
parents:
diff changeset
  2807
%\comment{If i remember correctly, you have proved this lemma.
Chengsong
parents:
diff changeset
  2808
%I feel this is indeed not true because you might place arbitrary 
Chengsong
parents:
diff changeset
  2809
%bits on the regex r, however if this is the case, did i remember wrongly that
Chengsong
parents:
diff changeset
  2810
%you proved something like simplification does not affect $\textit{bmkeps}$ results?
Chengsong
parents:
diff changeset
  2811
%Anyway, i have amended this a little bit so it does not allow arbitrary bits attached
Chengsong
parents:
diff changeset
  2812
%to a regex. Maybe it works now.}
Chengsong
parents:
diff changeset
  2813
Chengsong
parents:
diff changeset
  2814
\begin{center}
Chengsong
parents:
diff changeset
  2815
	$\textit{bmkeps} \; a = \textit{bmkeps} \; \textit{bsimp} \; a\;($\textit{provided}$ \; a\; is \; \textit{bnullable} )$
Chengsong
parents:
diff changeset
  2816
\end{center} 
Chengsong
parents:
diff changeset
  2817
Chengsong
parents:
diff changeset
  2818
\noindent
Chengsong
parents:
diff changeset
  2819
as this basically comes down to proving actions like removing the
Chengsong
parents:
diff changeset
  2820
additional $r$ in $r+r$  does not delete important POSIX information in
Chengsong
parents:
diff changeset
  2821
a regular expression. The hard part of this proof is to establish that
Chengsong
parents:
diff changeset
  2822
Chengsong
parents:
diff changeset
  2823
\begin{center}
Chengsong
parents:
diff changeset
  2824
	$ \textit{blexer}\_{simp}(r, \; s) =  \textit{blexer}(r, \; s)$
Chengsong
parents:
diff changeset
  2825
\end{center}
Chengsong
parents:
diff changeset
  2826
%comment{This is not true either...look at the definion blexer/blexer-simp}
Chengsong
parents:
diff changeset
  2827
Chengsong
parents:
diff changeset
  2828
\noindent That is, if we do derivative on regular expression $r$ and then
Chengsong
parents:
diff changeset
  2829
simplify it, and repeat this process until we exhaust the string, we get a
Chengsong
parents:
diff changeset
  2830
regular expression $r''$($\textit{LHS}$)  that provides the POSIX matching
Chengsong
parents:
diff changeset
  2831
information, which is exactly the same as the result $r'$($\textit{RHS}$ of the
Chengsong
parents:
diff changeset
  2832
normal derivative algorithm that only does derivative repeatedly and has no
Chengsong
parents:
diff changeset
  2833
simplification at all.  This might seem at first glance very unintuitive, as
Chengsong
parents:
diff changeset
  2834
the $r'$ could be exponentially larger than $r''$, but can be explained in the
Chengsong
parents:
diff changeset
  2835
following way: we are pruning away the possible matches that are not POSIX.
Chengsong
parents:
diff changeset
  2836
Since there could be exponentially many 
Chengsong
parents:
diff changeset
  2837
non-POSIX matchings and only 1 POSIX matching, it
Chengsong
parents:
diff changeset
  2838
is understandable that our $r''$ can be a lot smaller.  we can still provide
Chengsong
parents:
diff changeset
  2839
the same POSIX value if there is one.  This is not as straightforward as the
Chengsong
parents:
diff changeset
  2840
previous proposition, as the two regular expressions $r'$ and $r''$ might have
Chengsong
parents:
diff changeset
  2841
become very different.  The crucial point is to find the
Chengsong
parents:
diff changeset
  2842
$\textit{POSIX}$  information of a regular expression and how it is modified,
Chengsong
parents:
diff changeset
  2843
augmented and propagated 
Chengsong
parents:
diff changeset
  2844
during simplification in parallel with the regular expression that
Chengsong
parents:
diff changeset
  2845
has not been simplified in the subsequent derivative operations.  To aid this,
Chengsong
parents:
diff changeset
  2846
we use the helper function retrieve described by Sulzmann and Lu:
Chengsong
parents:
diff changeset
  2847
\begin{center}
Chengsong
parents:
diff changeset
  2848
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
Chengsong
parents:
diff changeset
  2849
  $\textit{retrieve}\,(\textit{ONE}\,bs)\,\Empty$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  2850
  $\textit{retrieve}\,(\textit{CHAR}\,bs\,c)\,(\Char\,d)$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  2851
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Left\,v)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2852
     $bs \,@\, \textit{retrieve}\,a\,v$\\
Chengsong
parents:
diff changeset
  2853
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Right\,v)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2854
  $bs \,@\, \textit{retrieve}\,(\textit{ALTS}\,bs\,as)\,v$\\
Chengsong
parents:
diff changeset
  2855
  $\textit{retrieve}\,(\textit{SEQ}\,bs\,a_1\,a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  2856
     $bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
Chengsong
parents:
diff changeset
  2857
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,[])$ & $\dn$ &
Chengsong
parents:
diff changeset
  2858
     $bs \,@\, [\S]$\\
Chengsong
parents:
diff changeset
  2859
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
Chengsong
parents:
diff changeset
  2860
  \multicolumn{3}{l}{
Chengsong
parents:
diff changeset
  2861
     \hspace{3cm}$bs \,@\, [\Z] \,@\, \textit{retrieve}\,a\,v\,@\,
Chengsong
parents:
diff changeset
  2862
                    \textit{retrieve}\,(\textit{STAR}\,[]\,a)\,(\Stars\,vs)$}\\
Chengsong
parents:
diff changeset
  2863
\end{tabular}
Chengsong
parents:
diff changeset
  2864
\end{center}
Chengsong
parents:
diff changeset
  2865
%\comment{Did not read further}\\
Chengsong
parents:
diff changeset
  2866
This function assembles the bitcode 
Chengsong
parents:
diff changeset
  2867
%that corresponds to a lexical value for how
Chengsong
parents:
diff changeset
  2868
%the current derivative matches the suffix of the string(the characters that
Chengsong
parents:
diff changeset
  2869
%have not yet appeared, but will appear as the successive derivatives go on.
Chengsong
parents:
diff changeset
  2870
%How do we get this "future" information? By the value $v$, which is
Chengsong
parents:
diff changeset
  2871
%computed by a pass of the algorithm that uses
Chengsong
parents:
diff changeset
  2872
%$inj$ as described in the previous section).  
Chengsong
parents:
diff changeset
  2873
using information from both the derivative regular expression and the
Chengsong
parents:
diff changeset
  2874
value. Sulzmann and Lu poroposed this function, but did not prove
Chengsong
parents:
diff changeset
  2875
anything about it. Ausaf and Urban used it to connect the bitcoded
Chengsong
parents:
diff changeset
  2876
algorithm to the older algorithm by the following equation:
Chengsong
parents:
diff changeset
  2877
 
Chengsong
parents:
diff changeset
  2878
 \begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
Chengsong
parents:
diff changeset
  2879
	 (r^\uparrow)\backslash_{simp} \,c)\,v)$ 
Chengsong
parents:
diff changeset
  2880
 \end{center} 
Chengsong
parents:
diff changeset
  2881
Chengsong
parents:
diff changeset
  2882
\noindent
Chengsong
parents:
diff changeset
  2883
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
Chengsong
parents:
diff changeset
  2884
and Urban also used this fact to prove  the correctness of bitcoded
Chengsong
parents:
diff changeset
  2885
algorithm without simplification.  Our purpose of using this, however,
Chengsong
parents:
diff changeset
  2886
is to establish 
Chengsong
parents:
diff changeset
  2887
Chengsong
parents:
diff changeset
  2888
\begin{center}
Chengsong
parents:
diff changeset
  2889
$ \textit{retrieve} \;
Chengsong
parents:
diff changeset
  2890
a \; v \;=\; \textit{retrieve}  \; (\textit{simp}\,a) \; v'.$
Chengsong
parents:
diff changeset
  2891
\end{center}
Chengsong
parents:
diff changeset
  2892
The idea is that using $v'$, a simplified version of $v$ that had gone
Chengsong
parents:
diff changeset
  2893
through the same simplification step as $\textit{simp}(a)$, we are able
Chengsong
parents:
diff changeset
  2894
to extract the bitcode that gives the same parsing information as the
Chengsong
parents:
diff changeset
  2895
unsimplified one. However, we noticed that constructing such a  $v'$
Chengsong
parents:
diff changeset
  2896
from $v$ is not so straightforward. The point of this is that  we might
Chengsong
parents:
diff changeset
  2897
be able to finally bridge the gap by proving
Chengsong
parents:
diff changeset
  2898
Chengsong
parents:
diff changeset
  2899
\begin{center}
Chengsong
parents:
diff changeset
  2900
$\textit{retrieve} \; (r^\uparrow   \backslash  s) \; v = \;\textit{retrieve} \;
Chengsong
parents:
diff changeset
  2901
(\textit{simp}(r^\uparrow)  \backslash  s) \; v'$
Chengsong
parents:
diff changeset
  2902
\end{center}
Chengsong
parents:
diff changeset
  2903
Chengsong
parents:
diff changeset
  2904
\noindent
Chengsong
parents:
diff changeset
  2905
and subsequently
Chengsong
parents:
diff changeset
  2906
Chengsong
parents:
diff changeset
  2907
\begin{center}
Chengsong
parents:
diff changeset
  2908
$\textit{retrieve} \; (r^\uparrow \backslash  s) \; v\; = \; \textit{retrieve} \;
Chengsong
parents:
diff changeset
  2909
(r^\uparrow  \backslash_{simp}  \, s) \; v'$.
Chengsong
parents:
diff changeset
  2910
\end{center}
Chengsong
parents:
diff changeset
  2911
Chengsong
parents:
diff changeset
  2912
\noindent
Chengsong
parents:
diff changeset
  2913
The $\textit{LHS}$ of the above equation is the bitcode we want. This
Chengsong
parents:
diff changeset
  2914
would prove that our simplified version of regular expression still
Chengsong
parents:
diff changeset
  2915
contains all the bitcodes needed. The task here is to find a way to
Chengsong
parents:
diff changeset
  2916
compute the correct $v'$.
Chengsong
parents:
diff changeset
  2917
Chengsong
parents:
diff changeset
  2918
The second task is to speed up the more aggressive simplification.  Currently
Chengsong
parents:
diff changeset
  2919
it is slower than the original naive simplification by Ausaf and Urban (the
Chengsong
parents:
diff changeset
  2920
naive version as implemented by Ausaf   and Urban of course can ``explode'' in
Chengsong
parents:
diff changeset
  2921
some cases).  It is therefore not surprising that the speed is also much slower
Chengsong
parents:
diff changeset
  2922
than regular expression engines in popular programming languages such as Java
Chengsong
parents:
diff changeset
  2923
and Python on most inputs that are linear. For example, just by rewriting the
Chengsong
parents:
diff changeset
  2924
example regular expression in the beginning of this report  $(a^*)^*\,b$ into
Chengsong
parents:
diff changeset
  2925
$a^*\,b$ would eliminate the ambiguity in the matching and make the time
Chengsong
parents:
diff changeset
  2926
for matching linear with respect to the input string size. This allows the 
Chengsong
parents:
diff changeset
  2927
DFA approach to become blindingly fast, and dwarf the speed of our current
Chengsong
parents:
diff changeset
  2928
implementation. For example, here is a comparison of Java regex engine 
Chengsong
parents:
diff changeset
  2929
and our implementation on this example.
Chengsong
parents:
diff changeset
  2930
Chengsong
parents:
diff changeset
  2931
\begin{center}
Chengsong
parents:
diff changeset
  2932
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents:
diff changeset
  2933
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  2934
\begin{axis}[
Chengsong
parents:
diff changeset
  2935
    xlabel={$n*1000$},
Chengsong
parents:
diff changeset
  2936
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  2937
    ylabel={time in secs},
Chengsong
parents:
diff changeset
  2938
    enlargelimits=false,
Chengsong
parents:
diff changeset
  2939
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  2940
    xmax=33,
Chengsong
parents:
diff changeset
  2941
    ymax=9,
Chengsong
parents:
diff changeset
  2942
    scaled ticks=true,
Chengsong
parents:
diff changeset
  2943
    axis lines=left,
Chengsong
parents:
diff changeset
  2944
    width=5cm,
Chengsong
parents:
diff changeset
  2945
    height=4cm, 
Chengsong
parents:
diff changeset
  2946
    legend entries={Bitcoded Algorithm},  
Chengsong
parents:
diff changeset
  2947
    legend pos=north west,
Chengsong
parents:
diff changeset
  2948
    legend cell align=left]
Chengsong
parents:
diff changeset
  2949
\addplot[red,mark=*, mark options={fill=white}] table {bad-scala.data};
Chengsong
parents:
diff changeset
  2950
\end{axis}
Chengsong
parents:
diff changeset
  2951
\end{tikzpicture}
Chengsong
parents:
diff changeset
  2952
  &
Chengsong
parents:
diff changeset
  2953
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  2954
\begin{axis}[
Chengsong
parents:
diff changeset
  2955
    xlabel={$n*1000$},
Chengsong
parents:
diff changeset
  2956
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  2957
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
  2958
    enlargelimits=false,
Chengsong
parents:
diff changeset
  2959
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  2960
    xmax=33,
Chengsong
parents:
diff changeset
  2961
    ymax=9,
Chengsong
parents:
diff changeset
  2962
    scaled ticks=false,
Chengsong
parents:
diff changeset
  2963
    axis lines=left,
Chengsong
parents:
diff changeset
  2964
    width=5cm,
Chengsong
parents:
diff changeset
  2965
    height=4cm, 
Chengsong
parents:
diff changeset
  2966
    legend entries={Java},  
Chengsong
parents:
diff changeset
  2967
    legend pos=north west,
Chengsong
parents:
diff changeset
  2968
    legend cell align=left]
Chengsong
parents:
diff changeset
  2969
\addplot[cyan,mark=*, mark options={fill=white}] table {good-java.data};
Chengsong
parents:
diff changeset
  2970
\end{axis}
Chengsong
parents:
diff changeset
  2971
\end{tikzpicture}\\
Chengsong
parents:
diff changeset
  2972
\multicolumn{3}{c}{Graphs: Runtime for matching $a^*\,b$ with strings 
Chengsong
parents:
diff changeset
  2973
           of the form $\underbrace{aa..a}_{n}$.}
Chengsong
parents:
diff changeset
  2974
\end{tabular}    
Chengsong
parents:
diff changeset
  2975
\end{center}  
Chengsong
parents:
diff changeset
  2976
Chengsong
parents:
diff changeset
  2977
Chengsong
parents:
diff changeset
  2978
Java regex engine can match string of thousands of characters in a few milliseconds,
Chengsong
parents:
diff changeset
  2979
whereas our current algorithm gets excruciatingly slow on input of this size.
Chengsong
parents:
diff changeset
  2980
The running time in theory is linear, however it does not appear to be the 
Chengsong
parents:
diff changeset
  2981
case in an actual implementation. So it needs to be explored how to
Chengsong
parents:
diff changeset
  2982
make our algorithm faster on all inputs.  It could be the recursive calls that are
Chengsong
parents:
diff changeset
  2983
needed to manipulate bits that are causing the slow down. A possible solution
Chengsong
parents:
diff changeset
  2984
is to write recursive functions into tail-recusive form.
Chengsong
parents:
diff changeset
  2985
Another possibility would be to explore
Chengsong
parents:
diff changeset
  2986
again the connection to DFAs to speed up the algorithm on 
Chengsong
parents:
diff changeset
  2987
subcalls that are small enough. This is very much work in progress.
Chengsong
parents:
diff changeset
  2988
Chengsong
parents:
diff changeset
  2989
\section{Conclusion}
Chengsong
parents:
diff changeset
  2990
Chengsong
parents:
diff changeset
  2991
In this PhD-project we are interested in fast algorithms for regular
Chengsong
parents:
diff changeset
  2992
expression matching. While this seems to be a ``settled'' area, in
Chengsong
parents:
diff changeset
  2993
fact interesting research questions are popping up as soon as one steps
Chengsong
parents:
diff changeset
  2994
outside the classic automata theory (for example in terms of what kind
Chengsong
parents:
diff changeset
  2995
of regular expressions are supported). The reason why it is
Chengsong
parents:
diff changeset
  2996
interesting for us to look at the derivative approach introduced by
Chengsong
parents:
diff changeset
  2997
Brzozowski for regular expression matching, and then much further
Chengsong
parents:
diff changeset
  2998
developed by Sulzmann and Lu, is that derivatives can elegantly deal
Chengsong
parents:
diff changeset
  2999
with some of the regular expressions that are of interest in ``real
Chengsong
parents:
diff changeset
  3000
life''. This includes the not-regular expression, written $\neg\,r$
Chengsong
parents:
diff changeset
  3001
(that is all strings that are not recognised by $r$), but also bounded
Chengsong
parents:
diff changeset
  3002
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
Chengsong
parents:
diff changeset
  3003
also hope that the derivatives can provide another angle for how to
Chengsong
parents:
diff changeset
  3004
deal more efficiently with back-references, which are one of the
Chengsong
parents:
diff changeset
  3005
reasons why regular expression engines in JavaScript, Python and Java
Chengsong
parents:
diff changeset
  3006
choose to not implement the classic automata approach of transforming
Chengsong
parents:
diff changeset
  3007
regular expressions into NFAs and then DFAs---because we simply do not
Chengsong
parents:
diff changeset
  3008
know how such back-references can be represented by DFAs.
Chengsong
parents:
diff changeset
  3009
We also plan to implement the bitcoded algorithm
Chengsong
parents:
diff changeset
  3010
in some imperative language like C to see if the inefficiency of the 
Chengsong
parents:
diff changeset
  3011
Scala implementation
Chengsong
parents:
diff changeset
  3012
is language specific. To make this research more comprehensive we also plan
Chengsong
parents:
diff changeset
  3013
to contrast our (faster) version of bitcoded algorithm with the
Chengsong
parents:
diff changeset
  3014
Symbolic Regex Matcher, the RE2, the Rust Regex Engine, and the static
Chengsong
parents:
diff changeset
  3015
analysis approach by implementing them in the same language and then compare
Chengsong
parents:
diff changeset
  3016
their performance.
Chengsong
parents:
diff changeset
  3017
Chengsong
parents:
diff changeset
  3018
\bibliographystyle{plain}
Chengsong
parents:
diff changeset
  3019
\bibliography{root}
Chengsong
parents:
diff changeset
  3020
Chengsong
parents:
diff changeset
  3021
Chengsong
parents:
diff changeset
  3022
\end{document}