etnms/etnms.tex
author Chengsong
Sat, 08 Feb 2020 21:37:20 +0000
changeset 127 580e044af0f7
parent 126 1260b383ae2c
child 128 0203065d1370
permissions -rw-r--r--
test
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
126
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   110
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   111
\section{Introduction}
127
Chengsong
parents: 126
diff changeset
   112
test
126
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   113
While we believe derivatives of regular expressions, written
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   114
$r\backslash s$, are a beautiful concept (in terms of ease of
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   115
implementing them in functional programming languages and in terms of
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   116
reasoning about them formally), they have one major drawback: every
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   117
derivative step can make regular expressions grow drastically in
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   118
size. This in turn has negative effect on the runtime of the
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   119
corresponding lexing algorithms. Consider for example the regular
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   120
expression $(a+aa)^*$ and the short string $aaaaaaaaaaaa$. The
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   121
corresponding derivative contains already 8668 nodes where we assume
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   122
the derivative is given as a tree. The reason for the poor runtime of
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   123
the derivative-based lexing algorithms is that they need to traverse
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   124
such trees over and over again. The solution is to find a complete set
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   125
of simplification rules that keep the sizes of derivatives uniformly
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   126
small.
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   127
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   128
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   129
\section{Recapitulation of Concepts From the Last Report}
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   130
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   131
\subsection*{Regular Expressions and Derivatives}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   132
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   133
Suppose (basic) regular expressions are given by the following grammar:
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   134
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   135
\[			r ::=   \ZERO \mid  \ONE
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   136
			 \mid  c  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   137
			 \mid  r_1 \cdot r_2
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   138
			 \mid  r_1 + r_2   
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   139
			 \mid r^*         
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   140
\]
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   141
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   142
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   143
The ingenious contribution of Brzozowski is the notion of \emph{derivatives} of
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   144
regular expressions, written~$\_ \backslash \_$. It uses the auxiliary notion of
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   145
$\nullable$ defined below.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   146
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   147
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   148
		\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   149
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   150
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   151
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   152
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   153
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   154
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   155
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   156
	\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   157
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   158
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   159
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   160
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   161
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   162
		$d \backslash c$     & $\dn$ & 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   163
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   164
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   165
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   166
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   167
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   168
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   169
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   170
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   171
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   172
%Assuming the classic notion of a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   173
%\emph{language} of a regular expression, written $L(\_)$, t
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   174
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   175
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   176
The main property of the derivative operation is that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   177
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   178
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   179
$c\!::\!s \in L(r)$ holds
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   180
if and only if $s \in L(r\backslash c)$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   181
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   182
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   183
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   184
We can generalise the derivative operation shown above for single characters
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   185
to strings as follows:
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   186
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   187
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   188
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   189
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   190
$r \backslash [\,] $ & $\dn$ & $r$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   191
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   192
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   193
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   194
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   195
and then define Brzozowski's  regular-expression matching algorithm as:
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   196
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   197
\[
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   198
match\;s\;r \;\dn\; nullable(r\backslash s)
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   199
\]
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   200
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   201
\noindent
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   202
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
   203
this algorithm presented graphically is as follows:
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   204
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   205
\begin{equation}\label{graph:*}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   206
\begin{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   207
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
   208
\end{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   209
\end{equation}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   210
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   211
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   212
where we start with  a regular expression  $r_0$, build successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   213
derivatives until we exhaust the string and then use \textit{nullable}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   214
to test whether the result can match the empty string. It can  be
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   215
relatively  easily shown that this matcher is correct  (that is given
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   216
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
   217
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   218
 
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   219
\subsection*{Values and the Lexing Algorithm by Sulzmann and Lu}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   220
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   221
One limitation of Brzozowski's algorithm is that it only produces a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   222
YES/NO answer for whether a string is being matched by a regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   223
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   224
to allow generation of an actual matching, called a \emph{value} or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   225
sometimes also \emph{lexical value}.  These values and regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   226
expressions correspond to each other as illustrated in the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   227
table:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   228
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   229
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   230
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   231
	\begin{tabular}{c@{\hspace{20mm}}c}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   232
		\begin{tabular}{@{}rrl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   233
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   234
			$r$ & $::=$  & $\ZERO$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   235
			& $\mid$ & $\ONE$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   236
			& $\mid$ & $c$          \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   237
			& $\mid$ & $r_1 \cdot r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   238
			& $\mid$ & $r_1 + r_2$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   239
			\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   240
			& $\mid$ & $r^*$         \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   241
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   242
		&
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   243
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   244
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   245
			$v$ & $::=$  & \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   246
			&        & $\Empty$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   247
			& $\mid$ & $\Char(c)$          \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   248
			& $\mid$ & $\Seq\,v_1\, v_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   249
			& $\mid$ & $\Left(v)$   \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   250
			& $\mid$ & $\Right(v)$  \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   251
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   252
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   253
	\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   254
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   255
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   256
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   257
The contribution of Sulzmann and Lu is an extension of Brzozowski's
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   258
algorithm by a second phase (the first phase being building successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   259
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   260
is generated in case the regular expression matches  the string. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   261
Pictorially, the Sulzmann and Lu algorithm is as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   262
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   263
\begin{ceqn}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   264
\begin{equation}\label{graph:2}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   265
\begin{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   266
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
   267
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
   268
\end{tikzcd}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   269
\end{equation}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   270
\end{ceqn}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   271
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   272
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   273
For convenience, we shall employ the following notations: the regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   274
expression we start with is $r_0$, and the given string $s$ is composed
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   275
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   276
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   277
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   278
the derivative $r_n$. We test whether this derivative is
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   279
$\textit{nullable}$ or not. If not, we know the string does not match
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   280
$r$ and no value needs to be generated. If yes, we start building the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   281
values incrementally by \emph{injecting} back the characters into the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   282
earlier values $v_n, \ldots, v_0$. This is the second phase of the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   283
algorithm from the right to left. For the first value $v_n$, we call the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   284
function $\textit{mkeps}$, which builds the lexical value
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   285
for how the empty string has been matched by the (nullable) regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   286
expression $r_n$. This function is defined as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   287
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   288
	\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   289
		\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   290
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   291
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   292
			& \textit{if} $\nullable(r_{1})$\\ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   293
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   294
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   295
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   296
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   297
		\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   298
	\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   299
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   300
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   301
\noindent 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   302
After the $\mkeps$-call, we inject back the characters one by one in order to build
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   303
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   304
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   305
After injecting back $n$ characters, we get the lexical value for how $r_0$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   306
matches $s$. For this Sulzmann and Lu defined a function that reverses
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   307
the ``chopping off'' of characters during the derivative phase. The
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   308
corresponding function is called \emph{injection}, written
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   309
$\textit{inj}$; it takes three arguments: the first one is a regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   310
expression ${r_{i-1}}$, before the character is chopped off, the second
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   311
is a character ${c_{i-1}}$, the character we want to inject and the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   312
third argument is the value ${v_i}$, into which one wants to inject the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   313
character (it corresponds to the regular expression after the character
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   314
has been chopped off). The result of this function is a new value. The
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   315
definition of $\textit{inj}$ is as follows: 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   316
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   317
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   318
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   319
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   320
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   321
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   322
  $\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
   323
  $\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
   324
  $\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
   325
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   326
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   327
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   328
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   329
\noindent This definition is by recursion on the ``shape'' of regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   330
expressions and values. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   331
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   332
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   333
\subsection*{Simplification of Regular Expressions}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   334
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   335
The main drawback of building successive derivatives according
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   336
to Brzozowski's definition is that they can grow very quickly in size.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   337
This is mainly due to the fact that the derivative operation generates
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   338
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result, if
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   339
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   340
are excruciatingly slow. For example when starting with the regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   341
expression $(a + aa)^*$ and building 12 successive derivatives
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   342
w.r.t.~the character $a$, one obtains a derivative regular expression
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   343
with more than 8000 nodes (when viewed as a tree). Operations like
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   344
$\textit{der}$ and $\nullable$ need to traverse such trees and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   345
consequently the bigger the size of the derivative the slower the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   346
algorithm. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   347
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   348
Fortunately, one can simplify regular expressions after each derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   349
step. Various simplifications of regular expressions are possible, such
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   350
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   351
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   352
affect the answer for whether a regular expression matches a string or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   353
not, but fortunately also do not affect the POSIX strategy of how
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   354
regular expressions match strings---although the latter is much harder
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   355
to establish. Some initial results in this regard have been
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   356
obtained in \cite{AusafDyckhoffUrban2016}. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   357
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   358
If we want the size of derivatives in Sulzmann and Lu's algorithm to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   359
stay below this bound, we would need more aggressive simplifications.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   360
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   361
deleting duplicates whenever possible. For example, the parentheses in
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   362
$(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
   363
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   364
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   365
$a^*+a+\ONE$. Adding these more aggressive simplification rules help us
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   366
to achieve the same size bound as that of the partial derivatives. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   367
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   368
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
   369
make them compatible with the $\textit{inj}$-mechanism, we use
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   370
\emph{bitcodes}. They were first introduced by Sulzmann and Lu.
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   371
Here bits and bitcodes (lists of bits) are defined as:
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   372
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   373
\begin{center}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   374
		$b ::=   1 \mid  0 \qquad
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   375
bs ::= [] \mid b:bs    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   376
$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   377
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   378
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   379
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   380
The $1$ and $0$ are not in bold in order to avoid 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   381
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   382
bit-lists) can be used to encode values (or incomplete values) in a
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   383
compact form. This can be straightforwardly seen in the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   384
coding function from values to bitcodes: 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   385
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   386
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   387
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   388
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   389
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   390
  $\textit{code}(\Left\,v)$ & $\dn$ & $0 :: code(v)$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   391
  $\textit{code}(\Right\,v)$ & $\dn$ & $1 :: code(v)$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   392
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   393
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[0]$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   394
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $1 :: code(v) \;@\;
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   395
                                                 code(\Stars\,vs)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   396
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   397
\end{center} 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   398
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   399
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   400
Here $\textit{code}$ encodes a value into a bitcodes by converting
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   401
$\Left$ into $0$, $\Right$ into $1$, and marks the start of a non-empty
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   402
star iteration by $1$. The border where a local star terminates
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   403
is marked by $0$. This coding is lossy, as it throws away the information about
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   404
characters, and also does not encode the ``boundary'' between two
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   405
sequence values. Moreover, with only the bitcode we cannot even tell
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
   406
whether the $1$s and $0$s are for $\Left/\Right$ or $\Stars$. The
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   407
reason for choosing this compact way of storing information is that the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   408
relatively small size of bits can be easily manipulated and ``moved
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   409
around'' in a regular expression. In order to recover values, we will 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   410
need the corresponding regular expression as an extra information. This
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   411
means the decoding function is defined as:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   412
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   413
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   414
%\begin{definition}[Bitdecoding of Values]\mbox{}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   415
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   416
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   417
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   418
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   419
  $\textit{decode}'\,(0\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   420
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   421
       (\Left\,v, bs_1)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   422
  $\textit{decode}'\,(1\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   423
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   424
       (\Right\,v, bs_1)$\\                           
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   425
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   426
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   427
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   428
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   429
  $\textit{decode}'\,(0\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   430
  $\textit{decode}'\,(1\!::\!bs)\,(r^*)$ & $\dn$ & 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   431
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   432
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   433
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   434
  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   435
  $\textit{decode}\,bs\,r$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   436
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   437
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   438
       \textit{else}\;\textit{None}$                       
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   439
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   440
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   441
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   442
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   443
Sulzmann and Lu's integrated the bitcodes into regular expressions to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   444
create annotated regular expressions \cite{Sulzmann2014}.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   445
\emph{Annotated regular expressions} are defined by the following
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   446
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   447
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   448
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   449
\begin{tabular}{lcl}
115
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   450
  $\textit{a}$ & $::=$  & $\ZERO$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   451
                  & $\mid$ & $_{bs}\ONE$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   452
                  & $\mid$ & $_{bs}{\bf c}$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   453
                  & $\mid$ & $_{bs}\oplus\,as$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   454
                  & $\mid$ & $_{bs}a_1\cdot a_2$\\
5c8afe4a8090 for secu
Chengsong
parents: 114
diff changeset
   455
                  & $\mid$ & $_{bs}a^*$
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   456
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   457
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   458
%(in \textit{ALTS})
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   459
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   460
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   461
where $bs$ stands for bitcodes, $a$  for $\bold{a}$nnotated regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   462
expressions and $as$ for a list of annotated regular expressions.
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   463
The alternative constructor($\oplus$) has been generalized to 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   464
accept a list of annotated regular expressions rather than just 2.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   465
We will show that these bitcodes encode information about
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   466
the (POSIX) value that should be generated by the Sulzmann and Lu
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   467
algorithm.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   468
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   469
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   470
To do lexing using annotated regular expressions, we shall first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   471
transform the usual (un-annotated) regular expressions into annotated
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   472
regular expressions. This operation is called \emph{internalisation} and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   473
defined as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   474
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   475
%\begin{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   476
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   477
\begin{tabular}{lcl}
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   478
  $(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   479
  $(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   480
  $(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   481
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   482
  $_{[]}\oplus[\textit{fuse}\,[0]\,r_1^\uparrow,\,
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   483
  \textit{fuse}\,[1]\,r_2^\uparrow]$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   484
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   485
         $_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   486
  $(r^*)^\uparrow$ & $\dn$ &
116
dfcad6f19e06 just incase
Chengsong
parents: 115
diff changeset
   487
         $_{[]}(r^\uparrow)^*$\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   488
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   489
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   490
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   491
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   492
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   493
We use up arrows here to indicate that the basic un-annotated regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   494
expressions are ``lifted up'' into something slightly more complex. In the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   495
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   496
attach bits to the front of an annotated regular expression. Its
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   497
definition is as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   498
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   499
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   500
\begin{tabular}{lcl}
117
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   501
  $\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   502
  $\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   503
     $_{bs @ bs'}\ONE$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   504
  $\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   505
     $_{bs@bs'}{\bf c}$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   506
  $\textit{fuse}\;bs\,_{bs'}\oplus\textit{as}$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   507
     $_{bs@bs'}\oplus\textit{as}$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   508
  $\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   509
     $_{bs@bs'}a_1 \cdot a_2$\\
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   510
  $\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
0acf6b58236e just incase
Chengsong
parents: 116
diff changeset
   511
     $_{bs @ bs'}a^*$
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   512
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   513
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   514
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   515
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   516
After internalising the regular expression, we perform successive
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   517
derivative operations on the annotated regular expressions. This
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   518
derivative operation is the same as what we had previously for the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   519
basic regular expressions, except that we beed to take care of
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   520
the bitcodes:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   521
109
Chengsong
parents: 108
diff changeset
   522
Chengsong
parents: 108
diff changeset
   523
\iffalse
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   524
 %\begin{definition}{bder}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   525
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   526
  \begin{tabular}{@{}lcl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   527
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   528
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   529
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   530
        $\textit{if}\;c=d\; \;\textit{then}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   531
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   532
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   533
  $\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   534
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   535
     $\textit{if}\;\textit{bnullable}\,a_1$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   536
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   537
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   538
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   539
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   540
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   541
       (\textit{STAR}\,[]\,r)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   542
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   543
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   544
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   545
109
Chengsong
parents: 108
diff changeset
   546
\begin{center}
Chengsong
parents: 108
diff changeset
   547
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 108
diff changeset
   548
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 108
diff changeset
   549
  $(_{bs}\textit{ONE})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 108
diff changeset
   550
  $(_{bs}\textit{CHAR}\;d)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   551
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 108
diff changeset
   552
         _{bs}\textit{ONE}\;\textit{else}\;\textit{ZERO}$\\  
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   553
  $(_{bs}\textit{ALTS}\;\textit{as})\,\backslash c$ & $\dn$ &
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   554
  $_{bs}\textit{ALTS}\;(\textit{as}.\textit{map}(\backslash c))$\\
109
Chengsong
parents: 108
diff changeset
   555
  $(_{bs}\textit{SEQ}\;a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   556
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 108
diff changeset
   557
					       & &$\textit{then}\;_{bs}\textit{ALTS}\,List((_{[]}\textit{SEQ}\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents: 108
diff changeset
   558
					       & &$\phantom{\textit{then}\;_{bs}\textit{ALTS}\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents: 108
diff changeset
   559
  & &$\textit{else}\;_{bs}\textit{SEQ}\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents: 108
diff changeset
   560
  $(_{bs}\textit{STAR}\,a)\,\backslash c$ & $\dn$ &
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   561
      $_{bs}\textit{SEQ}\;(\textit{fuse}\, [0] \; r\,\backslash c )\,
109
Chengsong
parents: 108
diff changeset
   562
       (_{bs}\textit{STAR}\,[]\,r)$
Chengsong
parents: 108
diff changeset
   563
\end{tabular}    
Chengsong
parents: 108
diff changeset
   564
\end{center}    
Chengsong
parents: 108
diff changeset
   565
%\end{definition}
Chengsong
parents: 108
diff changeset
   566
\fi
Chengsong
parents: 108
diff changeset
   567
Chengsong
parents: 108
diff changeset
   568
\begin{center}
Chengsong
parents: 108
diff changeset
   569
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 108
diff changeset
   570
  $(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 108
diff changeset
   571
  $(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 108
diff changeset
   572
  $(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   573
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 108
diff changeset
   574
         _{bs}\ONE\;\textit{else}\;\ZERO$\\  
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   575
  $(_{bs}\oplus \;\textit{as})\,\backslash c$ & $\dn$ &
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   576
  $_{bs}\oplus\;(\textit{as.map}(\backslash c))$\\
109
Chengsong
parents: 108
diff changeset
   577
  $(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 108
diff changeset
   578
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 108
diff changeset
   579
					       & &$\textit{then}\;_{bs}\oplus\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
Chengsong
parents: 108
diff changeset
   580
					       & &$\phantom{\textit{then},\;_{bs}\oplus\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
Chengsong
parents: 108
diff changeset
   581
  & &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
Chengsong
parents: 108
diff changeset
   582
  $(_{bs}a^*)\,\backslash c$ & $\dn$ &
119
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   583
      $_{bs}(\textit{fuse}\, [0] \; r\,\backslash c)\cdot
109
Chengsong
parents: 108
diff changeset
   584
       (_{[]}r^*))$
Chengsong
parents: 108
diff changeset
   585
\end{tabular}    
Chengsong
parents: 108
diff changeset
   586
\end{center}    
Chengsong
parents: 108
diff changeset
   587
Chengsong
parents: 108
diff changeset
   588
%\end{definition}
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   589
\noindent
119
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   590
For instance, when we do derivative of  $_{bs}a^*$ with respect to c,
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   591
we need to unfold it into a sequence,
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   592
and attach an additional bit $0$ to the front of $r \backslash c$
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   593
to indicate that there is one more star iteration. Also the sequence clause
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   594
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   595
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   596
that it is for annotated regular expressions, therefore we omit the
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   597
definition). Assume that $\textit{bmkeps}$ correctly extracts the bitcode for how
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   598
$a_1$ matches the string prior to character $c$ (more on this later),
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   599
then the right branch of alternative, which is $\textit{fuse} \; \bmkeps \;  a_1 (a_2
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   600
\backslash c)$ will collapse the regular expression $a_1$(as it has
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   601
already been fully matched) and store the parsing information at the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   602
head of the regular expression $a_2 \backslash c$ by fusing to it. The
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   603
bitsequence $\textit{bs}$, which was initially attached to the
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   604
first element of the sequence $a_1 \cdot a_2$, has
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   605
now been elevated to the top-level of $\oplus$, as this information will be
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
   606
needed whichever way the sequence is matched---no matter whether $c$ belongs
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   607
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   608
the lexing information, we complete the lexing by collecting the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   609
bitcodes using a generalised version of the $\textit{mkeps}$ function
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   610
for annotated regular expressions, called $\textit{bmkeps}$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   611
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   612
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   613
%\begin{definition}[\textit{bmkeps}]\mbox{}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   614
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   615
\begin{tabular}{lcl}
119
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   616
  $\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   617
  $\textit{bmkeps}\,(_{bs}\oplus a::\textit{as})$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   618
     $\textit{if}\;\textit{bnullable}\,a$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   619
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
121
Chengsong
parents: 120
diff changeset
   620
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{bs}\oplus \textit{as})$\\
120
1ca011142964 for fear
Chengsong
parents: 119
diff changeset
   621
  $\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   622
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
119
cc12352272f2 for fear
Chengsong
parents: 118
diff changeset
   623
  $\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
121
Chengsong
parents: 120
diff changeset
   624
     $bs \,@\, [0]$
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   625
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   626
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   627
%\end{definition}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   628
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   629
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   630
This function completes the value information by travelling along the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   631
path of the regular expression that corresponds to a POSIX value and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   632
collecting all the bitcodes, and using $S$ to indicate the end of star
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   633
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   634
decode them, we get the value we expect. The corresponding lexing
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   635
algorithm looks as follows:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   636
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   637
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   638
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   639
  $\textit{blexer}\;r\,s$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   640
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   641
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   642
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   643
  & & $\;\;\textit{else}\;\textit{None}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   644
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   645
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   646
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   647
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   648
In this definition $\_\backslash s$ is the  generalisation  of the derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   649
operation from characters to strings (just like the derivatives for un-annotated
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   650
regular expressions).
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   651
108
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   652
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   653
\subsection*{Our Simplification Rules}
0a0c551bb368 updated
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   654
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   655
The main point of the bitcodes and annotated regular expressions is that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   656
we can apply rather aggressive (in terms of size) simplification rules
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   657
in order to keep derivatives small. We have developed such
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   658
``aggressive'' simplification rules and generated test data that show
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   659
that the expected bound can be achieved. Obviously we could only
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   660
partially cover  the search space as there are infinitely many regular
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   661
expressions and strings. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   662
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   663
One modification we introduced is to allow a list of annotated regular
121
Chengsong
parents: 120
diff changeset
   664
expressions in the $\oplus$ constructor. This allows us to not just
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   665
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   666
also unnecessary ``copies'' of regular expressions (very similar to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   667
simplifying $r + r$ to just $r$, but in a more general setting). Another
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   668
modification is that we use simplification rules inspired by Antimirov's
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   669
work on partial derivatives. They maintain the idea that only the first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   670
``copy'' of a regular expression in an alternative contributes to the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   671
calculation of a POSIX value. All subsequent copies can be pruned away from
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   672
the regular expression. A recursive definition of our  simplification function 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   673
that looks somewhat similar to our Scala code is given below:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   674
%\comment{Use $\ZERO$, $\ONE$ and so on. 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   675
%Is it $ALTS$ or $ALTS$?}\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   676
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   677
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   678
  \begin{tabular}{@{}lcl@{}}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   679
   
121
Chengsong
parents: 120
diff changeset
   680
  $\textit{simp} \; (_{bs}a_1\cdot a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   681
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   682
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   683
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   684
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
121
Chengsong
parents: 120
diff changeset
   685
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow   _{bs}a_1' \cdot a_2'$ \\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   686
121
Chengsong
parents: 120
diff changeset
   687
  $\textit{simp} \; (_{bs}\oplus \textit{as})$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   688
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   689
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
121
Chengsong
parents: 120
diff changeset
   690
   &&$\quad\textit{case} \;  as' \Rightarrow _{bs}\oplus \textit{as'}$\\ 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   691
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   692
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   693
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   694
\end{center}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   695
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   696
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   697
The simplification does a pattern matching on the regular expression.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   698
When it detected that the regular expression is an alternative or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   699
sequence, it will try to simplify its children regular expressions
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   700
recursively and then see if one of the children turn into $\ZERO$ or
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   701
$\ONE$, which might trigger further simplification at the current level.
121
Chengsong
parents: 120
diff changeset
   702
The most involved part is the $\oplus$ clause, where we use two
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   703
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
121
Chengsong
parents: 120
diff changeset
   704
alternatives and reduce as many duplicates as possible. Function
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   705
$\textit{distinct}$  keeps the first occurring copy only and remove all later ones
121
Chengsong
parents: 120
diff changeset
   706
when detected duplicates. Function $\textit{flatten}$ opens up nested $\oplus$s.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   707
Its recursive definition is given below:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   708
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   709
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   710
  \begin{tabular}{@{}lcl@{}}
121
Chengsong
parents: 120
diff changeset
   711
  $\textit{flatten} \; (_{bs}\oplus \textit{as}) :: \textit{as'}$ & $\dn$ & $(\textit{map} \;
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   712
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
121
Chengsong
parents: 120
diff changeset
   713
  $\textit{flatten} \; \ZERO :: as'$ & $\dn$ & $ \textit{flatten} \;  \textit{as'} $ \\
Chengsong
parents: 120
diff changeset
   714
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; \textit{as'}$ \quad(otherwise) 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   715
\end{tabular}    
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   716
\end{center}  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   717
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   718
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   719
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   720
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   721
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   722
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   723
Suppose we apply simplification after each derivative step, and view
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   724
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   725
\textit{simp}(a \backslash c)$. Then we can use the previous natural
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   726
extension from derivative w.r.t.~character to derivative
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   727
w.r.t.~string:%\comment{simp in  the [] case?}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   728
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   729
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   730
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   731
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   732
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   733
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   734
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   735
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   736
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   737
we obtain an optimised version of the algorithm:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   738
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   739
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   740
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   741
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   742
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   743
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   744
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   745
  & & $\;\;\textit{else}\;\textit{None}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   746
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   747
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   748
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   749
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   750
This algorithm keeps the regular expression size small, for example,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   751
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   752
will be reduced to just 6 and stays constant, no matter how long the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   753
input string is.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   754
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   755
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   756
126
1260b383ae2c forfear
Chengsong
parents: 125
diff changeset
   757
\section{Current Work and Progress}
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   758
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
   759
simplification rules depends on values being encoded as
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   760
bitsequences.\footnote{Values are the results the lexing algorithms
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   761
  generate; they encode how a regular expression matched a string.} We
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   762
already know that the lexing algorithm using bitsequences but
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   763
\emph{without} simplification is correct, albeilt horribly
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   764
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
   765
algorithm using bitsequences plus our simplification rules is
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   766
also correct. Formally this amounts to show that
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   767
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   768
\begin{equation}\label{mainthm}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   769
\blexers \; r \; s = \blexer \;r\;s
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   770
\end{equation}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   771
94
Chengsong
parents:
diff changeset
   772
\noindent
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   773
whereby $\blexers$ simplifies (makes derivatives smaller) in each
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   774
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
   775
would be an important milestone for my thesis, because we already
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   776
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
   777
rules keeps the size of derivativs below a relatively tight bound.
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   778
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   779
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
   780
two functions produce the same output. The definition of these two  functions 
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   781
is shown below.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   782
94
Chengsong
parents:
diff changeset
   783
\begin{center}
Chengsong
parents:
diff changeset
   784
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   785
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
   786
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
   787
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
   788
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
   789
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
   790
\end{tabular}
Chengsong
parents:
diff changeset
   791
\end{center}
Chengsong
parents:
diff changeset
   792
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   793
\begin{center}
94
Chengsong
parents:
diff changeset
   794
\begin{tabular}{lcl}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   795
  $\blexers \; r \, s$ &$\dn$ &
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   796
    $\textit{let} \; a = (r^\uparrow)\backslash_{simp}\, s\; \textit{in}$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   797
  & & $\; \; \textit{if} \; \textit{bnullable}(a)$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   798
  & & $\; \; \textit{then} \; \textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   799
  & & $\;\;   \textit{else}\;\textit{None}$
94
Chengsong
parents:
diff changeset
   800
\end{tabular}
Chengsong
parents:
diff changeset
   801
\end{center}
Chengsong
parents:
diff changeset
   802
\noindent
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   803
In these definitions $(r^\uparrow)$ is a kind of coding function that
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   804
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
   805
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
   806
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
   807
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
   808
takes). They establish how the derivatives \emph{with} simplification
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   809
do not change the computed result:
94
Chengsong
parents:
diff changeset
   810
Chengsong
parents:
diff changeset
   811
\begin{itemize}
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   812
\item{(a)} If a string $s$ is in the language of $L(r)$, then \\
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   813
$\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
   814
\item{(b)} If a string $s$ is in the language $L(r)$, then 
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   815
$\rup \backslash_{simp} \,s$ is not nullable.
94
Chengsong
parents:
diff changeset
   816
\end{itemize}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   817
94
Chengsong
parents:
diff changeset
   818
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   819
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
   820
not too difficult because we can show that simplification does not
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   821
change the language of simplified regular expressions.
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   822
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   823
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
   824
simplification produces the same result as the one without
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   825
simplification, then we are done.  Unfortunately that part requires
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   826
more effort, because simplification does not only need to \emph{not}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   827
change the language, but also not change the value (that is the
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   828
computed result).
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   829
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   830
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   831
%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
   832
%method used retrieve, which unfortunately cannot be adopted to 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   833
%the simplification rules. You could just say that and give an example.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   834
%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
   835
%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
   836
%like $a + aa$, but annotate bitsequences as subscript like $_1(_0a + _1aa)$.
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   837
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   838
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   839
%REPLY:\\
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   840
%Yes, I am essentially saying that the old method
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   841
%cannot be adopted without adjustments.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   842
%But this does not mean we should skip
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   843
%the proof of the bit-coded algorithm
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   844
%as it is still the main direction we are looking into
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   845
%to prove things. We are trying to modify
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   846
%the old proof to suit our needs, but not give 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   847
%up it totally, that is why i believe the old 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   848
%proof is fundamental in understanding
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   849
%what we are doing in the past 6 months.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   850
%\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   851
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   852
\subsubsection*{Existing Proof}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   853
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   854
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
   855
established that the bitsequence algorrithm produces the same result
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   856
as the algorithm not using bitsequences. Formally this proof
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   857
established
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   858
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   859
\begin{equation}\label{lexer}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   860
\blexer \; (r^\uparrow)  s = \lexer \;r \;s
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   861
\end{equation}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   862
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   863
%\noindent
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   864
%might provide us insight into proving 
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   865
%\begin{center}
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   866
%$\blexer \; r^\uparrow \;s = \blexers \; r^\uparrow \;s$
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   867
%\end{center}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   868
94
Chengsong
parents:
diff changeset
   869
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   870
The proof uses two ``tricks''. One is that it uses a \flex-function
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   871
94
Chengsong
parents:
diff changeset
   872
\begin{center}
Chengsong
parents:
diff changeset
   873
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   874
$\textit{flex} \;r\; f\; (c\!::\!s) $ & $\dn$ & $\textit{flex} \;  (r\backslash c) \;(\lambda v. f (inj \; r \; c \; v)) \;s$ \\
Chengsong
parents:
diff changeset
   875
$\textit{flex} \;r\; f\;  [\,] $ & $\dn$ & $f$
Chengsong
parents:
diff changeset
   876
\end{tabular}
Chengsong
parents:
diff changeset
   877
\end{center}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   878
94
Chengsong
parents:
diff changeset
   879
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   880
and then proves for the right-hand side in \eqref{lexer}
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   881
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   882
\begin{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   883
$\lexer \;r\; s = \flex \;\textit{id} \; r\;s \;(\mkeps \; (r\backslash s))$
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   884
\end{center}.
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   885
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   886
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   887
104
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   888
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   889
\noindent
e7ba4da53930 merged christian changes
Chengsong
parents: 103
diff changeset
   890
The $\flex$-function essentially does lexing by
105
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   891
stacking up injection functions while doing derivatives.
317a7d54ebcc updated
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
   892
94
Chengsong
parents:
diff changeset
   893
explicitly showing the order of characters being
Chengsong
parents:
diff changeset
   894
injected back in each step.
Chengsong
parents:
diff changeset
   895
With $\flex$ we can write $\lexer$ this way: 
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   896
94
Chengsong
parents:
diff changeset
   897
\begin{center}
Chengsong
parents:
diff changeset
   898
$\lexer \;r\; s = \flex \;id \; r\;s \;(\mkeps r\backslash s)$
Chengsong
parents:
diff changeset
   899
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   900
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   901
%\noindent
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   902
%$\flex$ focuses on the injections instead of the derivatives ,
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   903
%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
   904
%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
   905
%character:
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   906
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   907
%\begin{center}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   908
%\begin{tabular}{lcl}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   909
%$\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
   910
% & & $\textit{None} \; \Longrightarrow \; \textit{None}$\\
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   911
%  & & $\textbar \; v \; \Longrightarrow \; \inj \; r\;c\;v$\\
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   912
%$\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
   913
%\end{tabular}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   914
%\end{center}
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   915
94
Chengsong
parents:
diff changeset
   916
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   917
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
   918
94
Chengsong
parents:
diff changeset
   919
\begin{center}
Chengsong
parents:
diff changeset
   920
$\flex \; r \; id \; (s@[c]) \; v = \flex \;  r \; id \; s \; (inj \; (r\backslash s) \; c\; v)$.
Chengsong
parents:
diff changeset
   921
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   922
94
Chengsong
parents:
diff changeset
   923
\noindent
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   924
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
   925
94
Chengsong
parents:
diff changeset
   926
\begin{center} 
Chengsong
parents:
diff changeset
   927
$ \flex \; r\; id\; (s@[c])\; v = \textit{decode} \;( \textit{retrieve}\; (\rup \backslash s) \; (\inj \; (r\backslash s) \;c\;v)\;) r$
Chengsong
parents:
diff changeset
   928
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   929
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   930
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   931
\subsubsection{Retrieve Function}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   932
The crucial point is to find the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   933
$\textit{POSIX}$  information of a regular expression and how it is modified,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   934
augmented and propagated 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   935
during simplification in parallel with the regular expression that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   936
has not been simplified in the subsequent derivative operations.  To aid this,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   937
we use the helper function retrieve described by Sulzmann and Lu:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   938
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   939
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
110
Chengsong
parents: 109
diff changeset
   940
  $\textit{retrieve}\,(_{bs}\ONE)\,\Empty$ & $\dn$ & $bs$\\
Chengsong
parents: 109
diff changeset
   941
  $\textit{retrieve}\,(_{bs}{\bf c})\,(\Char\,d)$ & $\dn$ & $bs$\\
121
Chengsong
parents: 120
diff changeset
   942
  $\textit{retrieve}\,(_{bs}\oplus a::as)\,(\Left\,v)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   943
     $bs \,@\, \textit{retrieve}\,a\,v$\\
121
Chengsong
parents: 120
diff changeset
   944
  $\textit{retrieve}\,(_{bs}\oplus a::as)\,(\Right\,v)$ & $\dn$ &
Chengsong
parents: 120
diff changeset
   945
  $\textit{bs} \,@\, \textit{retrieve}\,(_{[]}\oplus as)\,v$\\
Chengsong
parents: 120
diff changeset
   946
  $\textit{retrieve}\,(_{bs}a_1\cdot a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   947
     $bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
121
Chengsong
parents: 120
diff changeset
   948
  $\textit{retrieve}\,(_{bs}a^*)\,(\Stars\,[])$ & $\dn$ &
Chengsong
parents: 120
diff changeset
   949
     $bs \,@\, [0]$\\
Chengsong
parents: 120
diff changeset
   950
  $\textit{retrieve}\,(_{bs}a^*)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   951
  \multicolumn{3}{l}{
121
Chengsong
parents: 120
diff changeset
   952
     \hspace{3cm}$bs \,@\, [1] \,@\, \textit{retrieve}\,a\,v\,@\,
Chengsong
parents: 120
diff changeset
   953
                    \textit{retrieve}\,(_{[]}a^*)\,(\Stars\,vs)$}\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   954
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   955
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   956
%\comment{Did not read further}\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   957
This function assembles the bitcode 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   958
%that corresponds to a lexical value for how
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   959
%the current derivative matches the suffix of the string(the characters that
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   960
%have not yet appeared, but will appear as the successive derivatives go on.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   961
%How do we get this "future" information? By the value $v$, which is
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   962
%computed by a pass of the algorithm that uses
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   963
%$inj$ as described in the previous section).  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   964
using information from both the derivative regular expression and the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   965
value. Sulzmann and Lu poroposed this function, but did not prove
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   966
anything about it. Ausaf and Urban used it to connect the bitcoded
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   967
algorithm to the older algorithm by the following equation:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   968
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   969
 \begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   970
	 (r^\uparrow)\backslash_{simp} \,c)\,v)$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   971
 \end{center} 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   972
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   973
\noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   974
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   975
and Urban also used this fact to prove  the correctness of bitcoded
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   976
algorithm without simplification.  Our purpose of using this, however,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   977
is to establish 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   978
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   979
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   980
$ \textit{retrieve} \;
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   981
a \; v \;=\; \textit{retrieve}  \; (\textit{simp}\,a) \; v'.$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   982
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   983
The idea is that using $v'$, a simplified version of $v$ that had gone
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   984
through the same simplification step as $\textit{simp}(a)$, we are able
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   985
to extract the bitcode that gives the same parsing information as the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   986
unsimplified one. However, we noticed that constructing such a  $v'$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   987
from $v$ is not so straightforward. The point of this is that  we might
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   988
be able to finally bridge the gap by proving
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
   989
94
Chengsong
parents:
diff changeset
   990
\noindent
Chengsong
parents:
diff changeset
   991
By using a property of retrieve we have the $\textit{RHS}$ of the above equality is
Chengsong
parents:
diff changeset
   992
$decode (retrieve (r^\uparrow \backslash(s @ [c])) v) r$, and this gives the 
Chengsong
parents:
diff changeset
   993
main lemma result:
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   994
94
Chengsong
parents:
diff changeset
   995
\begin{center}
Chengsong
parents:
diff changeset
   996
$ \flex \;r\;  id \; (s@[c]) \; v =\textit{decode}(\textit{retrieve} (\rup \backslash (s@[c])) \;v) r$
Chengsong
parents:
diff changeset
   997
\end{center}
106
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   998
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
   999
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1000
e0db3242d8b5 added my comments
Christian Urban <urbanc@in.tum.de>
parents: 105 104
diff changeset
  1001
94
Chengsong
parents:
diff changeset
  1002
\noindent
Chengsong
parents:
diff changeset
  1003
To use this lemma result for our 
Chengsong
parents:
diff changeset
  1004
correctness proof, simply replace the $v$ in the
Chengsong
parents:
diff changeset
  1005
$\textit{RHS}$ of the above equality with
Chengsong
parents:
diff changeset
  1006
$\mkeps\;(r\backslash (s@[c]))$, and apply the lemma that
Chengsong
parents:
diff changeset
  1007
 
Chengsong
parents:
diff changeset
  1008
\begin{center}
Chengsong
parents:
diff changeset
  1009
$\textit{decode} \; \bmkeps \; \rup \; r = \textit{decode} \; (\textit{retrieve} \; \rup \; \mkeps(r)) \;r$
Chengsong
parents:
diff changeset
  1010
\end{center}
Chengsong
parents:
diff changeset
  1011
\noindent
Chengsong
parents:
diff changeset
  1012
We get the correctness of our bit-coded algorithm:
Chengsong
parents:
diff changeset
  1013
\begin{center}
Chengsong
parents:
diff changeset
  1014
$\flex \;r\;  id \; s \; (\mkeps \; r\backslash s) = \textit{decode} \; \bmkeps \; \rup\backslash s \; r$
Chengsong
parents:
diff changeset
  1015
\end{center}
Chengsong
parents:
diff changeset
  1016
\noindent
Chengsong
parents:
diff changeset
  1017
The bridge between the above chain of equalities
Chengsong
parents:
diff changeset
  1018
is the use of $\retrieve$,
Chengsong
parents:
diff changeset
  1019
if we want to use a similar technique for the 
Chengsong
parents:
diff changeset
  1020
simplified version of algorithm,
Chengsong
parents:
diff changeset
  1021
we face the problem that in the above 
Chengsong
parents:
diff changeset
  1022
equalities,
Chengsong
parents:
diff changeset
  1023
$\retrieve \; a \; v$ is not always defined.
Chengsong
parents:
diff changeset
  1024
for example,
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
  1025
$\retrieve \; _0(_1a+_0a) \; \Left(\Empty)$
101
Chengsong
parents: 100
diff changeset
  1026
is defined, but not $\retrieve \; (_{01}a) \;\Left(\Empty)$,
94
Chengsong
parents:
diff changeset
  1027
though we can extract the same POSIX
Chengsong
parents:
diff changeset
  1028
bits from the two annotated regular expressions.
95
Chengsong
parents: 94
diff changeset
  1029
The latter might occur when we try to retrieve from 
Chengsong
parents: 94
diff changeset
  1030
a simplified regular expression using the same value
Chengsong
parents: 94
diff changeset
  1031
as the unsimplified one.
Chengsong
parents: 94
diff changeset
  1032
This is because $\Left(\Empty)$ corresponds to
101
Chengsong
parents: 100
diff changeset
  1033
the regular expression structure $\ONE+r_2$ instead of
Chengsong
parents: 100
diff changeset
  1034
$\ONE$.
94
Chengsong
parents:
diff changeset
  1035
That means, if we 
Chengsong
parents:
diff changeset
  1036
want to prove that 
Chengsong
parents:
diff changeset
  1037
\begin{center}
Chengsong
parents:
diff changeset
  1038
$\textit{decode} \; \bmkeps \; \rup\backslash s \; r = \textit{decode} \; \bmkeps \; \rup\backslash_{simp} s \; r$
Chengsong
parents:
diff changeset
  1039
\end{center}
Chengsong
parents:
diff changeset
  1040
\noindent
Chengsong
parents:
diff changeset
  1041
holds by using $\retrieve$,
Chengsong
parents:
diff changeset
  1042
we probably need to prove an equality like below:
Chengsong
parents:
diff changeset
  1043
\begin{center}
Chengsong
parents:
diff changeset
  1044
%$\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
  1045
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(f(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
94
Chengsong
parents:
diff changeset
  1046
\end{center}
Chengsong
parents:
diff changeset
  1047
\noindent
101
Chengsong
parents: 100
diff changeset
  1048
$f$ rectifies $r\backslash s$ so the value $\mkeps(f(r\backslash s))$ becomes
Chengsong
parents: 100
diff changeset
  1049
 something simpler
94
Chengsong
parents:
diff changeset
  1050
to make the retrieve function defined.\\
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1051
\subsubsection{Ways to Rectify Value}
95
Chengsong
parents: 94
diff changeset
  1052
One way to do this is to prove the following:
Chengsong
parents: 94
diff changeset
  1053
\begin{center}
Chengsong
parents: 94
diff changeset
  1054
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(\simp(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
Chengsong
parents: 94
diff changeset
  1055
\end{center}
Chengsong
parents: 94
diff changeset
  1056
\noindent
101
Chengsong
parents: 100
diff changeset
  1057
The reason why we choose $\simp$ as $f$ is because
Chengsong
parents: 100
diff changeset
  1058
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash \, s)$
Chengsong
parents: 100
diff changeset
  1059
have the same shape:
Chengsong
parents: 100
diff changeset
  1060
\begin{center}
Chengsong
parents: 100
diff changeset
  1061
$\erase (\rup\backslash_{simp} \, s) = \erase(\simp(\rup\backslash s))$
Chengsong
parents: 100
diff changeset
  1062
\end{center}
Chengsong
parents: 100
diff changeset
  1063
Chengsong
parents: 100
diff changeset
  1064
\noindent
Chengsong
parents: 100
diff changeset
  1065
$\erase$ in the above equality means to remove the bit-codes
Chengsong
parents: 100
diff changeset
  1066
in an annotated regular expression and only keep the original
Chengsong
parents: 100
diff changeset
  1067
regular expression(just like "erasing" the bits). Its definition is omitted.
Chengsong
parents: 100
diff changeset
  1068
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash s)$
Chengsong
parents: 100
diff changeset
  1069
are very closely related, but not identical.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1070
\subsubsection{Example for $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$}
101
Chengsong
parents: 100
diff changeset
  1071
For example, let $r$ be the regular expression
Chengsong
parents: 100
diff changeset
  1072
$(a+b)(a+a*)$ and $s$  be the string $aa$, then
103
Chengsong
parents: 102
diff changeset
  1073
both $\erase (\rup\backslash_{simp} \, s)$ and $\erase (\simp (\rup\backslash s))$
Chengsong
parents: 102
diff changeset
  1074
are $\ONE + a^*$. However, without $\erase$ 
101
Chengsong
parents: 100
diff changeset
  1075
\begin{center}
Chengsong
parents: 100
diff changeset
  1076
$\rup\backslash_{simp} \, s$ is equal to $_0(_0\ONE +_{11}a^*)$
Chengsong
parents: 100
diff changeset
  1077
\end{center}
Chengsong
parents: 100
diff changeset
  1078
\noindent
Chengsong
parents: 100
diff changeset
  1079
whereas
Chengsong
parents: 100
diff changeset
  1080
\begin{center}
103
Chengsong
parents: 102
diff changeset
  1081
$\simp(\rup\backslash  s)$ is equal to $(_{00}\ONE +_{011}a^*)$
101
Chengsong
parents: 100
diff changeset
  1082
\end{center}
Chengsong
parents: 100
diff changeset
  1083
\noindent
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1084
(For the sake of visual simplicity, we use numbers to denote the bits
103
Chengsong
parents: 102
diff changeset
  1085
in bitcodes as we have previously defined for annotated 
Chengsong
parents: 102
diff changeset
  1086
regular expressions. $\S$ is replaced by 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1087
subscript $_1$ and $\Z$ by $_0$.)
103
Chengsong
parents: 102
diff changeset
  1088
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1089
What makes the difference?
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1090
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1091
%Two "rules" might be inferred from the above example.
103
Chengsong
parents: 102
diff changeset
  1092
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1093
%First, after erasing the bits the two regular expressions
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1094
%are exactly the same: both become $1+a^*$. Here the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1095
%function $\simp$ exhibits the "one in the end equals many times
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1096
%at the front"
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1097
%property: one simplification in the end causes the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1098
%same regular expression structure as
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1099
%successive simplifications done alongside derivatives.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1100
%$\rup\backslash_{simp} \, s$ unfolds to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1101
%$\simp((\simp(r\backslash a))\backslash a)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1102
%and $\simp(\rup\backslash s)$ unfolds to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1103
%$\simp((r\backslash a)\backslash a)$. The one simplification
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1104
%in the latter causes the resulting regular expression to 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1105
%become $1+a^*$, exactly the same as the former with
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1106
%two simplifications.
103
Chengsong
parents: 102
diff changeset
  1107
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1108
%Second, the bit-codes are different, but they are essentially
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1109
%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
  1110
%inside then we get $(_{00}\ONE +_{011}a^*)$, exactly the 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1111
%same as that of $\rup\backslash \, s$. And this difference 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1112
%does not matter when we try to apply $\bmkeps$ or $\retrieve$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1113
%to it. This seems a good news if we want to use $\retrieve$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1114
%to prove things.
103
Chengsong
parents: 102
diff changeset
  1115
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1116
%If we look into the difference above, we could see that the
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1117
%difference is not fundamental: the bits are just being moved
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1118
%around in a way that does not hurt the correctness.
103
Chengsong
parents: 102
diff changeset
  1119
During the first derivative operation, 
Chengsong
parents: 102
diff changeset
  1120
$\rup\backslash a=(_0\ONE  + \ZERO)(_0a  +  _1a^*)$  is
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1121
in the form of a sequence regular expression with
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1122
two components, the first
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1123
one $\ONE + \ZERO$ being nullable. 
103
Chengsong
parents: 102
diff changeset
  1124
Recall the simplification function definition:
Chengsong
parents: 102
diff changeset
  1125
\begin{center}
Chengsong
parents: 102
diff changeset
  1126
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 102
diff changeset
  1127
   
Chengsong
parents: 102
diff changeset
  1128
  $\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
  1129
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1130
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1131
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
Chengsong
parents: 102
diff changeset
  1132
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
Chengsong
parents: 102
diff changeset
  1133
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
Chengsong
parents: 102
diff changeset
  1134
Chengsong
parents: 102
diff changeset
  1135
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
Chengsong
parents: 102
diff changeset
  1136
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
Chengsong
parents: 102
diff changeset
  1137
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
Chengsong
parents: 102
diff changeset
  1138
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
Chengsong
parents: 102
diff changeset
  1139
Chengsong
parents: 102
diff changeset
  1140
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
Chengsong
parents: 102
diff changeset
  1141
\end{tabular}    
Chengsong
parents: 102
diff changeset
  1142
\end{center}    
Chengsong
parents: 102
diff changeset
  1143
Chengsong
parents: 102
diff changeset
  1144
\noindent
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1145
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1146
and the difinition of $\flatten$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1147
 \begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1148
 \begin{tabular}{c c c}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1149
 $\flatten \; []$ & $\dn$ & $[]$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1150
 $\flatten \; \ZERO::rs$ & $\dn$ & $rs$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1151
 $\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
  1152
 $\flatten \; r :: rs$ & $\dn$ & $r::\flatten(rs)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1153
 \end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1154
 \end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1155
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1156
 \noindent
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1157
If we call $\simp$ on $\rup\backslash a$, just as $\backslash_{simp}$
103
Chengsong
parents: 102
diff changeset
  1158
requires, then we would go throught the third clause of 
Chengsong
parents: 102
diff changeset
  1159
the sequence case:$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1160
The $\ZERO$ of $(_0\ONE  + \ZERO)$ is thrown away 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1161
by $\flatten$ and 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1162
$_0\ONE$ merged into $(_0a  +  _1a^*)$ by simply
103
Chengsong
parents: 102
diff changeset
  1163
putting its bits($_0$) to the front of the second component:
Chengsong
parents: 102
diff changeset
  1164
 ${\bf_0}(_0a  +  _1a^*)$. 
Chengsong
parents: 102
diff changeset
  1165
 After a second derivative operation,
Chengsong
parents: 102
diff changeset
  1166
 namely, $(_0(_0a  +  _1a^*))\backslash a$, we get 
Chengsong
parents: 102
diff changeset
  1167
 $
Chengsong
parents: 102
diff changeset
  1168
 _0(_0 \ONE  +  _1(_1\ONE \cdot a^*))
Chengsong
parents: 102
diff changeset
  1169
 $, and this simplifies to $_0(_0 \ONE  +  _{11} a^*)$
Chengsong
parents: 102
diff changeset
  1170
 by the third clause of the alternative case:
Chengsong
parents: 102
diff changeset
  1171
 $\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1172
The outmost bit $_0$ stays with 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1173
the outmost regular expression, rather than being fused to
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1174
its child regular expressions, as what we will later see happens
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1175
to $\simp(\rup\backslash \, s)$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1176
If we choose to not simplify in the midst of derivative operations,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1177
but only do it at the end after the string has been exhausted, 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1178
namely, $\simp(\rup\backslash \, s)=\simp((\rup\backslash a)\backslash a)$,
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1179
then at the {\bf second} derivative of 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1180
$(\rup\backslash a)\bf{\backslash a}$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1181
we will go throught the clause of $\backslash$:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1182
\begin{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1183
\begin{tabular}{lcl}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1184
$(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1185
     $(when \; \textit{bnullable}\,a_1)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1186
					       & &$\textit{ALTS}\,bs\,List(\;\;(\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1187
					       & &$(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))\;\;)$\\
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1188
\end{tabular}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1189
\end{center}
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1190
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1191
because
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1192
$\rup\backslash a = (_0\ONE  + \ZERO)(_0a  +  _1a^*)$  
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1193
is a sequence
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1194
with the first component being nullable
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1195
(unsimplified, unlike the first round of running$\backslash_{simp}$).
103
Chengsong
parents: 102
diff changeset
  1196
Therefore $((_0\ONE  + \ZERO)(_0a  +  _1a^*))\backslash a$ splits into
Chengsong
parents: 102
diff changeset
  1197
$([(\ZERO + \ZERO)\cdot(_0a  +  _1a^*)] + _0( _0\ONE  + _1[_1\ONE \cdot a^*]))$.
Chengsong
parents: 102
diff changeset
  1198
After these two successive derivatives without simplification,
Chengsong
parents: 102
diff changeset
  1199
we apply $\simp$ to this regular expression, which goes through
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1200
the alternative clause, and each component of 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1201
$([(\ZERO + \ZERO)\cdot(_0a  +  _1a^*)] + _0( _0\ONE  + _1[_1\ONE \cdot a^*]))$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1202
will be simplified, giving us the list:$[\ZERO, _0(_0\ONE  + _{11}a^*)]$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1203
This list is then "flattened"--$\ZERO$ will be
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1204
thrown away by $\textit{flatten}$; $ _0(_0\ONE  + _{11}a^*)$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1205
is opened up to make the list consisting of two separate elements 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1206
$_{00}\ONE$ and $_{011}a^*$, note that $flatten$ 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1207
$\fuse$s the bit(s) $_0$ to the front of $_0\ONE $ and $_{11}a^*$.
122
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1208
The order of simplification, which impacts the order that alternatives
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1209
are  opened up, causes
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1210
the bits to be moved differently.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1211
 
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1212
 \subsubsection{A Failed Attempt To Remedy the Problem Above}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1213
A simple class of regular expression and string
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1214
pairs $(r, s)$ can be deduced from the above example 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1215
which trigger the difference between 
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1216
$\rup\backslash_{simp} \, s$
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1217
and  $\simp(\rup\backslash s)$:
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1218
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1219
\begin{tabular}{lcl}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1220
$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
  1221
 $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
  1222
$\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
  1223
\end{tabular}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1224
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1225
We take a pair $(r, \;s)$ from the set $D$.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1226
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1227
Now we compute ${\bf \rup \backslash_{simp} s}$, we get:
110
Chengsong
parents: 109
diff changeset
  1228
\begin{center}
Chengsong
parents: 109
diff changeset
  1229
\begin{tabular}{lcl}
Chengsong
parents: 109
diff changeset
  1230
$(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
  1231
								      & $= \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
  1232
								      & $= \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
  1233
								      & $= \simp \left[  (\fuse \; \bmkeps(r_1\backslash c_1) \; r_2 ) \backslash c_2 \right]$,
110
Chengsong
parents: 109
diff changeset
  1234
\end{tabular}
Chengsong
parents: 109
diff changeset
  1235
\end{center}
Chengsong
parents: 109
diff changeset
  1236
\noindent
Chengsong
parents: 109
diff changeset
  1237
from the definition of $D$ we know $r_1 \backslash c_1$ is nullable, therefore
Chengsong
parents: 109
diff changeset
  1238
$\bmkeps(r_1\backslash c_1)$  returns a bitcode, we shall call it
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1239
 $\textit{bs}_2$. 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1240
The above term can be rewritten as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1241
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1242
$ \simp \left[  \fuse \; \textit{bs}_2\; r_2  \backslash c_2 \right]$,
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1243
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1244
which is equal to 
110
Chengsong
parents: 109
diff changeset
  1245
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1246
$\simp \left[ \fuse \; \textit{bs}_2 \; _{bs}{\oplus rs} \right]$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1247
$=\simp \left[  \; _{bs_2++bs}{\oplus rs} \right]$\\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1248
$=  \; _{bs_2++bs}{\oplus \textit{rs}_1} $
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1249
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1250
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1251
by using the properties from the set $D$ again
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1252
and again(The reason why we set so many conditions 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1253
that the pair $(r,s)$ need to satisfy is because we can
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1254
rewrite them easily to construct the difference.)
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1255
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1256
Now we compute ${\bf \simp(\rup \backslash s)}$:
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1257
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1258
$\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
  1259
\end{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1260
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1261
Again, using the properties above, we obtain
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1262
the following chain of equalities:
110
Chengsong
parents: 109
diff changeset
  1263
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1264
$\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
  1265
$= \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
  1266
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1267
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1268
as before, we call the bitcode returned by 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1269
$\bmkeps(r_1\backslash c_1)$ as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1270
$\textit{bs}_2$. 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1271
Also, $\simp(r_2 \backslash c_2)$ is 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1272
$_{bs}\oplus \textit{rs}_1$, 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1273
and $( \left(r_1 \backslash c_1\right) \backslash c_2  \cdot r_2)$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1274
simplifies to $\ZERO$,
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1275
so the above term can be expanded as
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1276
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1277
\begin{tabular}{l}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1278
$\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
  1279
  $\textit{case} \; [] \Rightarrow  \ZERO$ \\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1280
   $\textit{case} \; a :: [] \Rightarrow  \textit{\fuse \; \textit{bs} a}$ \\
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1281
    $\textit{case} \;  as' \Rightarrow  _{[]}\oplus as'$\\ 
110
Chengsong
parents: 109
diff changeset
  1282
\end{tabular}
Chengsong
parents: 109
diff changeset
  1283
\end{center}
Chengsong
parents: 109
diff changeset
  1284
\noindent
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1285
Applying the definition of $\flatten$, we get
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1286
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1287
$_{[]}\oplus (\textit{map} \; \fuse (\textit{bs}_2 ++ bs) rs_1)$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1288
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1289
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1290
compared to the result 
110
Chengsong
parents: 109
diff changeset
  1291
\begin{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1292
$ \; _{bs_2++bs}{\oplus \textit{rs}_1} $
110
Chengsong
parents: 109
diff changeset
  1293
\end{center}
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1294
\noindent
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1295
Note how these two regular expressions only
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1296
differ in terms of the position of the bits 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1297
$\textit{bs}_2++\textit{bs}$. They are the same otherwise.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1298
What caused this difference to happen?
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1299
The culprit is the $\flatten$ function, which spills
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1300
out the bitcodes in the inner alternatives when 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1301
there exists an outer alternative.
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1302
Note how the absence of simplification
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1303
caused $\simp(\rup \backslash s)$ to
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1304
generate the nested alternatives structure:
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1305
\begin{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1306
$  \oplus[\ZERO \;, \; _{bs}\oplus \textit{rs} ]$
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1307
\end{center}
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1308
and this will always trigger the $\flatten$ to 
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1309
spill out the inner alternative's bitcode $\textit{bs}$,
111
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1310
whereas when
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1311
simplification is done along the way, 
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1312
the structure of nested alternatives is never created(we can
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1313
actually prove that simplification function never allows nested
af2c63f9bdf9 refined section a bit
Chengsong
parents: 110
diff changeset
  1314
alternatives to happen, more on this later).
110
Chengsong
parents: 109
diff changeset
  1315
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1316
How about we do not allow the function $\simp$
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1317
to fuse out the bits when it is unnecessary?
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1318
Like, for the above regular expression, we might
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1319
just delete the outer layer of alternative
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1320
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1321
\st{$ {\oplus[\ZERO \;,}$} $_{bs}\oplus \textit{rs}$ \st{$]$}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1322
\end{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1323
and get $_{bs}\oplus \textit{rs}$ instead, without
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1324
fusing the bits $\textit{bs}$ inside to every element 
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1325
of $\textit{rs}$.
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1326
This idea can be realized by making the following
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1327
changes to the $\simp$-function:
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1328
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1329
  \begin{tabular}{@{}lcl@{}}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1330
   
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1331
  $\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
  1332
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1333
  $\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
  1334
  &&\st{$\quad\textit{case} \; [] \Rightarrow  \ZERO$} \\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1335
   &&\st{$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$} \\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1336
   &&\st{$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$}\\ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1337
   &&$\textit{if}(\textit{hollowAlternatives}( \textit{map \; simp \; as}))$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1338
   &&$\textit{then} \; \fuse  \; \textit{bs}\; \textit{extractAlt}(\textit{map} \; \simp \; \textit{as} )$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1339
   &&$\textit{else} \; \simp(_{bs} \oplus \textit{as})$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1340
   
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1341
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1342
   $\textit{simp}' \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1343
\end{tabular}    
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1344
\end{center}    
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1345
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1346
\noindent
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1347
given the definition of $\textit{hollowAlternatives}$ and  $\textit{extractAlt}$ :
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1348
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1349
$\textit{hollowAlternatives}( \textit{rs}) \dn 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1350
\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
  1351
\textit{either} \; r' = \ZERO \; \textit{or} \; r' = r $
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1352
$\textit{extractAlt}( \textit{rs}) \dn \textit{if}\big(
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 \big)\; \textit{then} \; \textit{return} \; r$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1355
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1356
\noindent
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1357
Basically, $\textit{hollowAlternatives}$ captures the feature of
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1358
a list of regular expression of the shape 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1359
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1360
$  \oplus[\ZERO \;, \; _{bs}\oplus \textit{rs} ]$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1361
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1362
and this means we can simply elevate the 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1363
inner regular expression $_{bs}\oplus \textit{rs}$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1364
 to the outmost
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1365
and throw away the useless $\ZERO$s and
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1366
the outer $\oplus$ wrapper.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1367
Using this new definition of simp, 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1368
under the example where  $r$ is the regular expression
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1369
$(a+b)(a+a*)$ and $s$  is the string $aa$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1370
the problem of $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1371
is resolved.
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1372
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1373
Unfortunately this causes new problems:
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1374
for the counterexample where 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1375
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1376
$r$ is the regular expression
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1377
$(ab+(a^*+aa))$ and $s$  is the string $aa$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1378
\end{center}
122
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1379
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1380
\noindent
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1381
$\rup\backslash_{simp} \, s$ is equal to 
125
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1382
$ _1(_{011}a^* +  _1\ONE)  $ whereas
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1383
$ \simp(\rup\backslash s) = (_{1011}a^* +  _{11}\ONE)$.
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1384
This discrepancy does not appear for the old
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1385
version of $\simp$.
125
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1386
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1387
Why?
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1388
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1389
During the first derivative operation, 
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1390
\begin{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1391
$\rup\backslash a=(    _0[ \ONE\cdot {\bf b}] + _1( _0[ _1\ONE \cdot {\bf a}^*] + [ \ONE \cdot {\bf a}])      )$,
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1392
\end{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1393
\noindent
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1394
 the second derivative gives us
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1395
 \begin{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1396
$\rup\backslash a=(_0( [\ZERO\cdot {\bf b}] + 0) + _1( _0( [\ZERO\cdot {\bf a}^*] + _1[ _1\ONE \cdot {\bf a}^*]) + _1( [\ZERO \cdot {\bf a}] + \ONE)  ))$,
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1397
\end{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1398
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1399
\noindent
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1400
and this simplifies to
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1401
\begin{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1402
$ _1(_{011}{\bf a}^* +  _1\ONE)  $ 
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1403
\end{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1404
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1405
If, after the first derivative we apply simplification we get
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1406
$(_0{\bf b}  + _{101}{\bf  a}^* + _{11}{\bf a}  )$,
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1407
and we do another derivative, getting
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1408
$(\ZERO  + (_{101}(\ONE \cdot _1{\bf a}^*)+_{11}\ONE)$,
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1409
which simplifies to 
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1410
\begin{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1411
$ (_{1011}a^* +  _{11}\ONE)  $ 
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1412
\end{center}
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1413
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1414
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1415
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1416
788f4aa28bc5 added a bit?
Chengsong
parents: 123
diff changeset
  1417
122
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1418
We have changed the algorithm to suppress the old
dc0cdfc5fc66 version
Chengsong
parents: 121
diff changeset
  1419
counterexample, but this gives rise to new counterexamples.
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1420
This dilemma causes this amendment not a successful 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1421
attempt to make $\rup\backslash_{simp} \, s = \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1422
under every possible regular expression and string.
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1423
\subsection{Properties of the Function $\simp$}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1424
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1425
We have proved in Isabelle quite a few properties
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1426
of the $\simp$-function, which helps the proof to go forward
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1427
and we list them here to aid comprehension.
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1428
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1429
To start, we need a bit of auxiliary notations,
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1430
which is quite basic and is only written here
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1431
for clarity.
113
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1432
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1433
$\textit{sub}(r)$ computes the set of the 
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1434
sub-regular expression of $r$:
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1435
\begin{center}
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1436
$\textit{sub}(\ONE) \dn \{\ONE\}$\\
8dbc83ecea3b test for talisker address change
Chengsong
parents: 112
diff changeset
  1437
$\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
  1438
$\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
  1439
\end{center}
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1440
$\textit{good}(r) \dn r \neq \ZERO \land \\
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1441
\forall r' \in \textit{sub}(r), \textit{if} \; r' = _{bs_1}\oplus(rs_1), \;
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1442
\textit{then} \nexists r'' \in \textit{rs}_1 \; s.t.\;
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1443
r'' = _{bs_2}\oplus \textit{rs}_2 $
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1444
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1445
The properties are mainly the ones below:
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1446
\begin{itemize}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1447
\item
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1448
\begin{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1449
$\simp(\simp(r)) = \simp(r)$
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1450
\end{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1451
\item
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1452
\begin{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1453
$\textit{if} r = \simp(r') \textit{then} \; \textit{good}(r) $
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1454
\end{center}
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1455
\end{itemize}
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1456
\subsection{the Contains relation}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1457
$\retrieve$ is a too strong relation in that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1458
it only extracts one bitcode instead of a set of them.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1459
Therefore we try to define another relation(predicate)
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1460
to capture the fact the regular expression has bits
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1461
being moved around but still has all the bits needed.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1462
The contains symbol, written$\gg$, is  a relation that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1463
takes two arguments in an infix form 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1464
and returns a truth value. 
112
c19f2d20d92c added section
Chengsong
parents: 111
diff changeset
  1465
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1466
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1467
In other words, from the set of regular expression and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1468
bitcode pairs 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1469
$\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
  1470
those that satisfy the following requirements are in the set
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1471
$\textit{RV}_Contains$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1472
Unlike the $\retrieve$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1473
function, which takes two arguments $r$ and $v$ and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1474
produces an only answer $\textit{bs}$, it takes only 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1475
one argument $r$ and returns a set of bitcodes that 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1476
can be generated by $r$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1477
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1478
\begin{tabular}{llclll}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1479
& & & $_{bs}\ONE$ & $\gg$ & $\textit{bs}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1480
& & & $_{bs}{\bf c}$ & $\gg$ & $\textit{bs}$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1481
$\textit{if} \; r_1 \gg \textit{bs}_1$ & $r_2 \; \gg \textit{bs}_2$ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1482
& $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1483
 $_{bs}{r_1 \cdot r_2}$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1484
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1485
 $\textit{bs} @ \textit{bs}_1 @ \textit{bs}_2$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1486
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1487
 $\textit{if} \; r \gg \textit{bs}_1$ &  & $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1488
 $_{bs}{\oplus(r :: \textit{rs}})$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1489
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1490
 $\textit{bs} @ \textit{bs}_1 $\\ 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1491
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1492
 $\textit{if} \; _{bs}(\oplus \textit{rs}) \gg \textit{bs} @ \textit{bs}_1$ &  & $\textit{then}$  &
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1493
 $_{bs}{\oplus(r :: \textit{rs}})$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1494
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1495
 $\textit{bs} @ \textit{bs}_1 $\\  
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1496
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1497
 $\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
  1498
 $_{bs}r^* $ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1499
 $\gg$ & 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1500
 $\textit{bs} @ [0] @ \textit{bs}_1@ \textit{bs}_2 $\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1501
 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1502
 & & & $_{bs}r^*$ & $\gg$ & $\textit{bs} @ [1]$\\
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1503
\end{tabular}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1504
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1505
It is a predicate in the sense that given 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1506
a regular expression and a bitcode, it 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1507
returns true or false, depending on whether 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1508
or not the regular expression can actually produce that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1509
value. If the predicates returns a true, then 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1510
we say that the regular expression $r$ contains
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1511
the bitcode $\textit{bs}$, written 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1512
$r \gg \textit{bs}$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1513
The $\gg$ operator with the
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1514
regular expression $r$ may also be seen as a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1515
machine that does a derivative of regular expressions
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1516
on all strings simultaneously, taking 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1517
the bits by going throught the regular expression tree
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1518
 structure in a depth first manner, regardless of whether
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1519
 the part being traversed is nullable or not.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1520
 It put all possible bits that can be produced on such a traversal
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1521
 into a set.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1522
 For example, if we are given the regular expression 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1523
$((a+b)(c+d))^*$, the tree structure may be written as
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1524
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1525
\begin{tikzpicture}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1526
\tikz[tree layout]\graph[nodes={draw, circle}] {
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1527
* -> 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1528
    {@-> {
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1529
    {+1 ->
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1530
        {a , b}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1531
        },
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1532
    {+ ->
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1533
        {c , d }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1534
        }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1535
        }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1536
    }
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1537
};
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1538
\end{tikzpicture}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1539
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1540
\subsection{the $\textit{ders}_2$ Function}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1541
If we want to prove the result 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1542
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1543
	$ \textit{blexer}\_{simp}(r, \; s) =  \textit{blexer}(r, \; s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1544
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1545
inductively 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1546
on the structure of the regular expression,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1547
then we need to induct on the  case $r_1 \cdot r_2$,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1548
it can be good if we could express $(r_1 \cdot r_2) \backslash s$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1549
in terms of $r_1 \backslash s$ and $r_2 \backslash s$,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1550
and this naturally induces the $ders2$ function,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1551
which does a "convolution" on $r_1$ and $r_2$ using the string
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1552
$s$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1553
It is based on the observation that the derivative of $r_1 \cdot r_2$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1554
with respect to a string $s$ can actually be written in an "explicit form"
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1555
composed of $r_1$'s derivative of $s$ and $r_2$'s derivative of $s$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1556
This can be illustrated in the following procedure execution 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1557
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1558
	$ (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
  1559
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1560
which can also be written in a "convoluted sum"
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1561
format:
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1562
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1563
	$ (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
  1564
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1565
In a more serious manner, we should write
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1566
\begin{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1567
	$ (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
  1568
\end{center}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1569
Note this differentiates from the previous form in the sense that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1570
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
  1571
through nested alternatives whereas the $r_1 \cdot r_2 \backslash s$ procedure,
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1572
used by algorithm $\lexer$, can only produce each component of the 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1573
resulting alternatives regular expression altogether rather than 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1574
generating each of the children nodes one by one
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1575
n a single recursive call that is only for generating that
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1576
very expression itself.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1577
We have this 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1578
\section{Conclusion}
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1579
Under the exhaustive tests we believe the main
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1580
result holds, yet a proof still seems elusive.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1581
We have tried out different approaches, and 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1582
found a lot of properties of the function $\simp$.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1583
The counterexamples where $\rup\backslash_{simp} \, s \neq \simp(\rup\backslash s)$
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1584
are also valuable in the sense that 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1585
we get to know better why they are not equal and what 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1586
are the subtle differences between a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1587
nested simplified regular expression and a 
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1588
regular expression that is simplified at the final moment.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1589
We are almost there, but a last step is needed to make the proof work.
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1590
Hopefully in the next few weeks we will be able to find one.
110
Chengsong
parents: 109
diff changeset
  1591
%CONSTRUCTION SITE HERE
101
Chengsong
parents: 100
diff changeset
  1592
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents: 100
diff changeset
  1593
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents: 100
diff changeset
  1594
after one simplification is exactly the same after the 
Chengsong
parents: 100
diff changeset
  1595
same sequence of derivative operations 
Chengsong
parents: 100
diff changeset
  1596
regardless of whether we did simplification
Chengsong
parents: 100
diff changeset
  1597
along the way.
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1598
 One way would be to give a function that calls
110
Chengsong
parents: 109
diff changeset
  1599
107
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1600
fuse is the culprit: it causes the order in which alternatives
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1601
are opened up to be remembered and finally the difference
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1602
appear in $\simp(\rup \backslash s)$ and $\rup \backslash{simp} \,s$.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1603
but we have to use them as they are essential in the simplification:
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1604
flatten needs them.
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1605
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1606
b1e365afa29c changes
Chengsong
parents: 106
diff changeset
  1607
101
Chengsong
parents: 100
diff changeset
  1608
However, without erase the above equality does not hold:
Chengsong
parents: 100
diff changeset
  1609
for the regular expression  
Chengsong
parents: 100
diff changeset
  1610
$(a+b)(a+a*)$,
Chengsong
parents: 100
diff changeset
  1611
if we do derivative with respect to string $aa$,
114
dd7f719c451d avoid work loss
Chengsong
parents: 113
diff changeset
  1612
we get 
103
Chengsong
parents: 102
diff changeset
  1613
109
Chengsong
parents: 108
diff changeset
  1614
\subsection{Another Proof Strategy}
101
Chengsong
parents: 100
diff changeset
  1615
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents: 100
diff changeset
  1616
For example,
Chengsong
parents: 100
diff changeset
  1617
Chengsong
parents: 100
diff changeset
  1618
This equicalence class method might still have the potential of proving this,
Chengsong
parents: 100
diff changeset
  1619
but not yet
Chengsong
parents: 100
diff changeset
  1620
i parallelly tried another method of using retrieve\\
Chengsong
parents: 100
diff changeset
  1621
Chengsong
parents: 100
diff changeset
  1622
94
Chengsong
parents:
diff changeset
  1623
The vsimp function, defined as follows
Chengsong
parents:
diff changeset
  1624
tries to simplify the value in lockstep with 
Chengsong
parents:
diff changeset
  1625
regular expression:\\
Chengsong
parents:
diff changeset
  1626
Chengsong
parents:
diff changeset
  1627
Chengsong
parents:
diff changeset
  1628
The problem here is that 
Chengsong
parents:
diff changeset
  1629
Chengsong
parents:
diff changeset
  1630
we used retrieve for the key induction:
Chengsong
parents:
diff changeset
  1631
$decode (retrieve (r\backslash (s @ [c])) v) r $
Chengsong
parents:
diff changeset
  1632
$decode (retrieve (r\backslash s) (inj (r\backslash s) c v)) r$
Chengsong
parents:
diff changeset
  1633
Here, decode recovers a value that corresponds to a match(possibly partial)
Chengsong
parents:
diff changeset
  1634
from bits, and the bits are extracted by retrieve,
Chengsong
parents:
diff changeset
  1635
and the key value $v$ that guides retrieve is
Chengsong
parents:
diff changeset
  1636
$mkeps r\backslash s$, $inj r c (mkeps r\backslash s)$, $inj (inj (v))$, ......
Chengsong
parents:
diff changeset
  1637
if we can 
Chengsong
parents:
diff changeset
  1638
the problem is that 
Chengsong
parents:
diff changeset
  1639
need vsiimp to make a value that is suitable for decoding
Chengsong
parents:
diff changeset
  1640
$Some(flex rid(s@[c])v) = Some(flex rids(inj (r\backslash s)cv))$
Chengsong
parents:
diff changeset
  1641
another way that christian came up with that might circumvent the 
Chengsong
parents:
diff changeset
  1642
prblem of finding suitable value is by not stating the visimp
Chengsong
parents:
diff changeset
  1643
function but include all possible value in a set that a regex is able to produce,
Chengsong
parents:
diff changeset
  1644
and proving that both r and sr are able to produce the bits that correspond the POSIX value
Chengsong
parents:
diff changeset
  1645
Chengsong
parents:
diff changeset
  1646
produced by feeding the same initial regular expression $r$ and string $s$ to the
Chengsong
parents:
diff changeset
  1647
 two functions $ders$ and $ders\_simp$.
Chengsong
parents:
diff changeset
  1648
The reason why
Chengsong
parents:
diff changeset
  1649
Namely, if $bmkeps( r_1) = bmkeps(r_2)$, then we 
Chengsong
parents:
diff changeset
  1650
Chengsong
parents:
diff changeset
  1651
Chengsong
parents:
diff changeset
  1652
If we define the equivalence relation $\sim_{m\epsilon}$ between two regular expressions
Chengsong
parents:
diff changeset
  1653
$r_1$ and $r_2$as follows:
Chengsong
parents:
diff changeset
  1654
$r_1 \sim_{m\epsilon} r_2  \iff bmkeps(r_1)= bmkeps(r_2)$
Chengsong
parents:
diff changeset
  1655
(in other words, they $r1$ and $r2$ produce the same output under the function $bmkeps$.)
Chengsong
parents:
diff changeset
  1656
Then the first goal 
Chengsong
parents:
diff changeset
  1657
might be restated as 
Chengsong
parents:
diff changeset
  1658
$(r^\uparrow)\backslash_{simp}\, s  \sim_{m\epsilon} (r^\uparrow)\backslash s$.
Chengsong
parents:
diff changeset
  1659
I tried to establish an equivalence relation between the regular experssions 
Chengsong
parents:
diff changeset
  1660
like dddr dddsr,.....
Chengsong
parents:
diff changeset
  1661
but right now i am only able to establish dsr and dr, using structural induction on r.
Chengsong
parents:
diff changeset
  1662
Those involve multiple derivative operations are harder to prove.
Chengsong
parents:
diff changeset
  1663
Two attempts have been made:
Chengsong
parents:
diff changeset
  1664
(1)induction on the number of der operations(or in other words, the length of the string s),
Chengsong
parents:
diff changeset
  1665
the inductive hypothesis was initially specified as 
Chengsong
parents:
diff changeset
  1666
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
  1667
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
  1668
the number n, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
  1669
and the proof goal may be stated as
Chengsong
parents:
diff changeset
  1670
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
  1671
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
  1672
the number n+1, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
  1673
the problem here is that although we can easily break down
Chengsong
parents:
diff changeset
  1674
a string s of length n+1 into s1@list(c), it is not that easy
Chengsong
parents:
diff changeset
  1675
to use the i.h. as a stepping stone to prove anything because s1 may well be not
Chengsong
parents:
diff changeset
  1676
in the language L(r). This inhibits us from obtaining the fact that
Chengsong
parents:
diff changeset
  1677
ders s1 r me derssimps s1 r.
Chengsong
parents:
diff changeset
  1678
Further exploration is needed to amend this hypothesis so it includes the
Chengsong
parents:
diff changeset
  1679
situation when s1 is not nullable.
Chengsong
parents:
diff changeset
  1680
For example, what information(bits? 
Chengsong
parents:
diff changeset
  1681
values?) can be extracted
Chengsong
parents:
diff changeset
  1682
from the regular expression ders(s1,r) so that we can compute or predict the possible 
Chengsong
parents:
diff changeset
  1683
result of bmkeps after another derivative operation. What function f can used to 
Chengsong
parents:
diff changeset
  1684
carry out the task? The possible way of exploration can be 
Chengsong
parents:
diff changeset
  1685
more directly perceived throught the graph below:
Chengsong
parents:
diff changeset
  1686
find a function
Chengsong
parents:
diff changeset
  1687
f
Chengsong
parents:
diff changeset
  1688
such that
Chengsong
parents:
diff changeset
  1689
f(bders s1 r)
Chengsong
parents:
diff changeset
  1690
= re1
Chengsong
parents:
diff changeset
  1691
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1692
= re2
Chengsong
parents:
diff changeset
  1693
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
  1694
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
  1695
and g(re1,c) = g(re2,c)
Chengsong
parents:
diff changeset
  1696
The inductive hypothesis would be
Chengsong
parents:
diff changeset
  1697
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
  1698
f(bders s1 r)
Chengsong
parents:
diff changeset
  1699
= re1
Chengsong
parents:
diff changeset
  1700
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1701
= re2"
Chengsong
parents:
diff changeset
  1702
proving this would be a lemma for the main proof:
Chengsong
parents:
diff changeset
  1703
the main proof would be 
Chengsong
parents:
diff changeset
  1704
"
Chengsong
parents:
diff changeset
  1705
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
  1706
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
  1707
for s = s1@c
Chengsong
parents:
diff changeset
  1708
"
Chengsong
parents:
diff changeset
  1709
and f need to be a recursive property for the lemma to be proved:
Chengsong
parents:
diff changeset
  1710
it needs to store not only the "after one char nullable info",
Chengsong
parents:
diff changeset
  1711
but also the "after two char nullable info",
Chengsong
parents:
diff changeset
  1712
and so on so that it is able to  predict what f will compute after a derivative operation,
Chengsong
parents:
diff changeset
  1713
in other words, it needs to be "infinitely recursive"\\
Chengsong
parents:
diff changeset
  1714
To prove the lemma, in other words, to get
Chengsong
parents:
diff changeset
  1715
"For all strings s1 of length <= n+1, 
Chengsong
parents:
diff changeset
  1716
f(bders s1 r)
Chengsong
parents:
diff changeset
  1717
= re3
Chengsong
parents:
diff changeset
  1718
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1719
= re4"\\
Chengsong
parents:
diff changeset
  1720
from\\
Chengsong
parents:
diff changeset
  1721
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
  1722
f(bders s1 r)
Chengsong
parents:
diff changeset
  1723
= re1
Chengsong
parents:
diff changeset
  1724
f(bderss s1 r)
Chengsong
parents:
diff changeset
  1725
= re2"\\
Chengsong
parents:
diff changeset
  1726
it might be best to construct an auxiliary function h such that\\
Chengsong
parents:
diff changeset
  1727
h(re1, c) = re3\\
Chengsong
parents:
diff changeset
  1728
h(re2, c) = re4\\
Chengsong
parents:
diff changeset
  1729
and re3 = f(bder c (bders s1 r))\\
Chengsong
parents:
diff changeset
  1730
re4 = f(simp(bder c (bderss s1 r)))
Chengsong
parents:
diff changeset
  1731
The key point here is that we are not satisfied with what bders s r will produce under
Chengsong
parents:
diff changeset
  1732
bmkeps, but also how it will perform after a derivative operation and then bmkeps, and two 
Chengsong
parents:
diff changeset
  1733
derivative operations and so on. In essence, we are preserving the regular expression 
Chengsong
parents:
diff changeset
  1734
itself under the function f, in a less compact way than the regluar expression: we are
Chengsong
parents:
diff changeset
  1735
not just recording but also interpreting what the regular expression matches.
Chengsong
parents:
diff changeset
  1736
In other words, we need to prove the properties of bderss s r beyond the bmkeps result,
Chengsong
parents:
diff changeset
  1737
i.e., not just the nullable ones, but also those containing remaining characters.\\
Chengsong
parents:
diff changeset
  1738
(2)we observed the fact that 
Chengsong
parents:
diff changeset
  1739
erase sdddddr= erase sdsdsdsr
Chengsong
parents:
diff changeset
  1740
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents:
diff changeset
  1741
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents:
diff changeset
  1742
after one simplification is exactly the same after the 
Chengsong
parents:
diff changeset
  1743
same sequence of derivative operations 
Chengsong
parents:
diff changeset
  1744
regardless of whether we did simplification
Chengsong
parents:
diff changeset
  1745
along the way.
Chengsong
parents:
diff changeset
  1746
However, without erase the above equality does not hold:
Chengsong
parents:
diff changeset
  1747
for the regular expression  
Chengsong
parents:
diff changeset
  1748
$(a+b)(a+a*)$,
Chengsong
parents:
diff changeset
  1749
if we do derivative with respect to string $aa$,
Chengsong
parents:
diff changeset
  1750
we get
Chengsong
parents:
diff changeset
  1751
%TODO
Chengsong
parents:
diff changeset
  1752
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents:
diff changeset
  1753
For example,
Chengsong
parents:
diff changeset
  1754
Chengsong
parents:
diff changeset
  1755
This equicalence class method might still have the potential of proving this,
Chengsong
parents:
diff changeset
  1756
but not yet
Chengsong
parents:
diff changeset
  1757
i parallelly tried another method of using retrieve\\
Chengsong
parents:
diff changeset
  1758
Chengsong
parents:
diff changeset
  1759
Chengsong
parents:
diff changeset
  1760
Chengsong
parents:
diff changeset
  1761
\noindent\rule[0.5ex]{\linewidth}{1pt}
Chengsong
parents:
diff changeset
  1762
Chengsong
parents:
diff changeset
  1763
Chengsong
parents:
diff changeset
  1764
Chengsong
parents:
diff changeset
  1765
Chengsong
parents:
diff changeset
  1766
Chengsong
parents:
diff changeset
  1767
Chengsong
parents:
diff changeset
  1768
\bibliographystyle{plain}
Chengsong
parents:
diff changeset
  1769
\bibliography{root}
Chengsong
parents:
diff changeset
  1770
Chengsong
parents:
diff changeset
  1771
Chengsong
parents:
diff changeset
  1772
\end{document}
118
c7825cfacc76 lualatex is probably the culprit
Chengsong
parents: 117
diff changeset
  1773