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