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