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