handouts/ho02.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 17 Oct 2019 14:02:59 +0100
changeset 662 8da26d4c2ca8
parent 646 2abd285c66d1
child 727 eb9343126625
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
646
2abd285c66d1 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
     1
% !TEX program = xelatex
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
     3
\usepackage{../style}
217
cd6066f1056a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 140
diff changeset
     4
\usepackage{../langs}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
     5
\usepackage{../graphics}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
     6
\usepackage{../data}
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
     7
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
     8
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\begin{document}
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    10
\fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017, 2018, 2019}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    11
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
272
1446bc47a294 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
    13
\section*{Handout 2 (Regular Expression Matching)}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
    15
This lecture is about implementing a more efficient regular expression
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    16
matcher (the plots on the right below)---more efficient than the
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    17
matchers from regular expression libraries in Ruby, Python, JavaScript
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    18
and Java (the plots on the left). For this consider some experimental
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    19
data: The first pair of plots shows the running time for the
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    20
regular expression $(a^*)^*\cdot b$ and strings composed of $n$
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    21
\pcode{a}s (meaning this regular expression actually does not match
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    22
the strings). The second pair of plots shows the running time for the
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    23
regular expressions $a^?{}^{\{n\}}\cdot a^{\{n\}}$ and strings also
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    24
composed of $n$ \pcode{a}s (this time the regular expressions match
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    25
the strings).  To see the substantial differences in the left and
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    26
right plots below, note the different scales of the $x$-axes.
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    27
510
cu
parents: 492
diff changeset
    28
  
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    29
\begin{center}
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    30
Graphs: $(a^*)^* \cdot b$ and strings $\underbrace{a\ldots a}_{n}$
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    31
\begin{tabular}{@{}cc@{}}
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
    32
\begin{tikzpicture}[baseline=(current bounding box.north)]
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
    33
  \begin{axis}[
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    34
    xlabel={$n$},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    35
    x label style={at={(1.05,0.0)}},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    36
    ylabel={time in secs},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    37
    enlargelimits=false,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    38
    xtick={0,5,...,30},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    39
    xmax=33,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    40
    ymax=35,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    41
    ytick={0,5,...,30},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    42
    scaled ticks=false,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    43
    axis lines=left,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    44
    width=5cm,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    45
    height=5cm, 
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    46
    legend entries={Java 8, Python, JavaScript},  
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    47
    legend pos=north west,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    48
    legend cell align=left]
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    49
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    50
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 571
diff changeset
    51
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    52
\end{axis}
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    53
\end{tikzpicture}
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    54
&
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
    55
\begin{tikzpicture}[baseline=(current bounding box.north)]
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    56
  \begin{axis}[
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    57
    xlabel={$n$},
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
    58
    x label style={at={(1.1,0.0)}},
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
    59
    %%xtick={0,1000000,...,5000000}, 
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    60
    ylabel={time in secs},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    61
    enlargelimits=false,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    62
    ymax=35,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    63
    ytick={0,5,...,30},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    64
    axis lines=left,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
    65
    %scaled ticks=false,
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    66
    width=6.5cm,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    67
    height=5cm,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
    68
    legend entries={Our matcher},  
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    69
    legend pos=north east,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    70
    legend cell align=left]
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    71
%\addplot[green,mark=square*,mark options={fill=white}] table {re2a.data};    
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    72
\addplot[black,mark=square*,mark options={fill=white}] table {re3a.data};
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    73
\end{axis}
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    74
\end{tikzpicture}
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    75
\end{tabular}
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
    76
\end{center}\bigskip
263
92e6985018ae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    77
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
    78
\begin{center}
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
    79
Graphs: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings $\underbrace{a\ldots a}_{n}$\\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
    80
\begin{tabular}{@{}cc@{}}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
    81
\begin{tikzpicture}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    82
\begin{axis}[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    83
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    84
    x label style={at={(1.05,0.0)}},
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
    85
    ylabel={\small time in secs},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    86
    enlargelimits=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    87
    xtick={0,5,...,30},
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 272
diff changeset
    88
    xmax=33,
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
    89
    ymax=35,
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
    90
    ytick={0,5,...,30},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    91
    scaled ticks=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    92
    axis lines=left,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    93
    width=5cm,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    94
    height=5cm, 
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    95
    legend entries={Python,Ruby},  
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    96
    legend pos=north west,
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
    97
    legend cell align=left]
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    98
\addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    99
\addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data};  
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   100
\end{axis}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   101
\end{tikzpicture}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   102
&
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   103
\begin{tikzpicture}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   104
  \begin{axis}[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   105
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   106
    x label style={at={(1.1,0.05)}},
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   107
    ylabel={\small time in secs},
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   108
    enlargelimits=false,
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   109
    xtick={0,2500,...,11000},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   110
    xmax=12000,
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   111
    ymax=35,
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   112
    ytick={0,5,...,30},
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   113
    scaled ticks=false,
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   114
    axis lines=left,
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   115
    width=6.5cm,
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   116
    height=5cm,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   117
    legend entries={Our matcher},  
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   118
    legend pos=north east,
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   119
    legend cell align=left]
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   120
%\addplot[green,mark=square*,mark options={fill=white}] table {re2.data};
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   121
\addplot[black,mark=square*,mark options={fill=white}] table {re3.data};
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   122
\end{axis}
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   123
\end{tikzpicture}
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   124
\end{tabular}
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   125
\end{center}
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   126
\bigskip
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   127
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   128
\noindent
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   129
In what follows we will use these regular expressions and strings as
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   130
running examples. There will be several versions (V1, V2, V3,\ldots)
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   131
of our matcher.\footnote{The corresponding files are
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   132
  \texttt{re1.scala}, \texttt{re2.scala} and so on. As usual, you can
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   133
  find the code on KEATS.}\bigskip
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   134
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   135
\noindent
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   136
Having specified in the previous lecture what
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   137
problem our regular expression matcher is supposed to solve,
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   138
namely for any given regular expression $r$ and string $s$
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   139
answer \textit{true} if and only if
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
s \in L(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   145
\noindent we can look for an algorithm to solve this problem. Clearly
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   146
we cannot use the function $L$ directly for this, because in general
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   147
the set of strings $L$ returns is infinite (recall what $L(a^*)$ is).
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   148
In such cases there is no way we can implement an exhaustive test for
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   149
whether a string is member of this set or not. In contrast our
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   150
matching algorithm will operate on the regular expression $r$ and
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   151
string $s$, only, which are both finite objects. Before we explain 
646
2abd285c66d1 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   152
the matching algorithm, let us have a closer look at what it
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   153
means when two regular expressions are equivalent.
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   154
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   155
\subsection*{Regular Expression Equivalences}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   157
We already defined in Handout 1 what it means for two regular
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   158
expressions to be equivalent, namely if their meaning is the
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   159
same language:
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   160
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   161
\[
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   162
r_1 \equiv r_2 \;\dn\; L(r_1) = L(r_2)
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   163
\]
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   164
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   165
\noindent
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   166
It is relatively easy to verify that some concrete equivalences
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   167
hold, for example
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   169
\begin{center}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   170
\begin{tabular}{rcl}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   171
$(a + b)  + c$ & $\equiv$ & $a + (b + c)$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   172
$a + a$        & $\equiv$ & $a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   173
$a + b$        & $\equiv$ & $b + a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   174
$(a \cdot b) \cdot c$ & $\equiv$ & $a \cdot (b \cdot c)$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   175
$c \cdot (a + b)$ & $\equiv$ & $(c \cdot a) + (c \cdot b)$\\
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   176
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   177
\end{center}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   179
\noindent
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   180
but also easy to verify that the following regular expressions
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   181
are \emph{not} equivalent
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   182
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   183
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   184
\begin{tabular}{rcl}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   185
$a \cdot a$ & $\not\equiv$ & $a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   186
$a + (b \cdot c)$ & $\not\equiv$ & $(a + b) \cdot (a + c)$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   187
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   188
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   189
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   190
\noindent I leave it to you to verify these equivalences and
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   191
non-equivalences. It is also interesting to look at some
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   192
corner cases involving $\ONE$ and $\ZERO$:
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   193
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   194
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   195
\begin{tabular}{rcl}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   196
$a \cdot \ZERO$ & $\not\equiv$ & $a$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   197
$a + \ONE$ & $\not\equiv$ & $a$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   198
$\ONE$ & $\equiv$ & $\ZERO^*$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   199
$\ONE^*$ & $\equiv$ & $\ONE$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   200
$\ZERO^*$ & $\not\equiv$ & $\ZERO$\\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   201
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   202
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   203
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   204
\noindent Again I leave it to you to make sure you agree
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   205
with these equivalences and non-equivalences. 
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   206
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   207
318
7975e4f0d4de updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 296
diff changeset
   208
For our matching algorithm however the following seven
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   209
equivalences will play an important role:
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   210
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   211
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   212
\begin{tabular}{rcl}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   213
$r + \ZERO$  & $\equiv$ & $r$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   214
$\ZERO + r$  & $\equiv$ & $r$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   215
$r \cdot \ONE$ & $\equiv$ & $r$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   216
$\ONE \cdot r$     & $\equiv$ & $r$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   217
$r \cdot \ZERO$ & $\equiv$ & $\ZERO$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   218
$\ZERO \cdot r$ & $\equiv$ & $\ZERO$\\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   219
$r + r$ & $\equiv$ & $r$
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   220
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   221
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   222
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   223
\noindent which always hold no matter what the regular expression $r$
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   224
looks like. The first two are easy to verify since $L(\ZERO)$ is the
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   225
empty set. The next two are also easy to verify since $L(\ONE) =
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   226
\{[]\}$ and appending the empty string to every string of another set,
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   227
leaves the set unchanged. Be careful to fully comprehend the fifth and
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   228
sixth equivalence: if you concatenate two sets of strings and one is
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   229
the empty set, then the concatenation will also be the empty set. To
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   230
see this, check the definition of $\_ @ \_$ for sets. The last
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   231
equivalence is again trivial.
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   232
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   233
What will be important later on is that we can orient these
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   234
equivalences and read them from left to right. In this way we
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   235
can view them as \emph{simplification rules}. Consider for 
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   236
example the regular expression 
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   237
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   238
\begin{equation}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   239
(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   240
\label{big}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   241
\end{equation}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   242
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   243
\noindent If we can find an equivalent regular expression that is
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   244
simpler (that usually means smaller), then this might potentially make
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   245
our matching algorithm run faster. We can look for such a simpler
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   246
regular expression $r'$ because whether a string $s$ is in $L(r)$ or
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   247
in $L(r')$ with $r\equiv r'$ will always give the same answer. Yes?
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   248
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   249
In the example above you will see that the regular expression is
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   250
equivalent to just $r_1$. You can verify this by iteratively applying
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   251
the simplification rules from above:
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   252
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   253
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   254
\begin{tabular}{ll}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   255
 & $(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot 
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   256
(\underline{r_4 \cdot \ZERO})$\smallskip\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   257
$\equiv$ & $(r_1 + \ZERO) \cdot \ONE + \underline{((\ONE + r_2) + r_3) \cdot 
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   258
\ZERO}$\smallskip\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   259
$\equiv$ & $\underline{(r_1 + \ZERO) \cdot \ONE} + \ZERO$\smallskip\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   260
$\equiv$ & $(\underline{r_1 + \ZERO}) + \ZERO$\smallskip\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   261
$\equiv$ & $\underline{r_1 + \ZERO}$\smallskip\\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   262
$\equiv$ & $r_1$\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   263
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   264
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   265
296
796b9b81ac8d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 291
diff changeset
   266
\noindent In each step, I underlined where a simplification
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   267
rule is applied. Our matching algorithm in the next section
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   268
will often generate such ``useless'' $\ONE$s and
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   269
$\ZERO$s, therefore simplifying them away will make the
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   270
algorithm quite a bit faster.
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   271
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   272
Finally here are three equivalences between regular expressions which are
479
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   273
not so obvious:
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   274
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   275
\begin{center}
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   276
\begin{tabular}{rcl}
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   277
$r^*$  & $\equiv$ & $1 + r\cdot r^*$\\
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   278
$(r_1 + r_2)^*$  & $\equiv$ & $r_1^* \cdot (r_2\cdot r_1^*)^*$\\
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   279
$(r_1 \cdot r_2)^*$ & $\equiv$ & $1 + r_1\cdot (r_2 \cdot r_1)^* \cdot r_2$\\
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   280
\end{tabular}
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   281
\end{center}
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   282
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   283
\noindent
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   284
We will not use them in our algorithm, but feel free to convince yourself
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   285
that they hold. As an aside, there has been a lot of research about
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   286
questions like: Can one always decide when two regular expressions are
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   287
equivalent or not? What does an algorithm look like to decide this
510
cu
parents: 492
diff changeset
   288
efficiently? So in general it is not a trivial problem.
479
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   289
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   290
\subsection*{The Matching Algorithm}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   291
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   292
The algorithm we will define below consists of two parts. One
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   293
is the function $\textit{nullable}$ which takes a regular expression as
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   294
argument and decides whether it can match the empty string
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   295
(this means it returns a boolean in Scala). This can be easily
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   296
defined recursively as follows:
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   297
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   298
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   299
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   300
$\textit{nullable}(\ZERO)$      & $\dn$ & $\textit{false}$\\
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   301
$\textit{nullable}(\ONE)$         & $\dn$ & $\textit{true}$\\
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   302
$\textit{nullable}(c)$                & $\dn$ & $\textit{false}$\\
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   303
$\textit{nullable}(r_1 + r_2)$     & $\dn$ &  $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\ 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   304
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ &  $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   305
$\textit{nullable}(r^*)$              & $\dn$ & $\textit{true}$ \\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   306
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   307
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   308
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   309
\noindent The idea behind this function is that the following
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   310
property holds:
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   311
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   312
\[
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   313
\textit{nullable}(r) \;\;\text{if and only if}\;\; []\in L(r)
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   314
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   315
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   316
\noindent Note on the left-hand side of the if-and-only-if we
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   317
have a function we can implement; on the right we have its
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   318
specification (which we cannot implement in a programming
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   319
language).
124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
   320
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   321
The other function of our matching algorithm calculates a
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   322
\emph{derivative} of a regular expression. This is a function
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   323
which will take a regular expression, say $r$, and a
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   324
character, say $c$, as arguments and returns a new regular
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   325
expression. Be mindful that the intuition behind this function
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   326
is not so easy to grasp on first reading. Essentially this
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   327
function solves the following problem: if $r$ can match a
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   328
string of the form $c\!::\!s$, what does a regular
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   329
expression look like that can match just $s$? The definition
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   330
of this function is as follows:
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   331
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   332
\begin{center}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   333
\begin{tabular}{l@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   334
  $\textit{der}\, c\, (\ZERO)$      & $\dn$ & $\ZERO$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   335
  $\textit{der}\, c\, (\ONE)$         & $\dn$ & $\ZERO$ \\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   336
  $\textit{der}\, c\, (d)$                & $\dn$ & if $c = d$ then $\ONE$ else $\ZERO$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   337
  $\textit{der}\, c\, (r_1 + r_2)$        & $\dn$ & $\textit{der}\, c\, r_1 + \textit{der}\, c\, r_2$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   338
  $\textit{der}\, c\, (r_1 \cdot r_2)$  & $\dn$  & if $\textit{nullable} (r_1)$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   339
  & & then $(\textit{der}\,c\,r_1) \cdot r_2 + \textit{der}\, c\, r_2$\\ 
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   340
  & & else $(\textit{der}\, c\, r_1) \cdot r_2$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   341
  $\textit{der}\, c\, (r^*)$          & $\dn$ & $(\textit{der}\,c\,r) \cdot (r^*)$
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   342
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   343
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   344
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   345
\noindent The first two clauses can be rationalised as
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   346
follows: recall that $\textit{der}$ should calculate a regular
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   347
expression so that given the ``input'' regular expression can
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   348
match a string of the form $c\!::\!s$, we want a regular
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   349
expression for $s$. Since neither $\ZERO$ nor $\ONE$
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   350
can match a string of the form $c\!::\!s$, we return
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   351
$\ZERO$. In the third case we have to make a
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   352
case-distinction: In case the regular expression is $c$, then
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   353
clearly it can recognise a string of the form $c\!::\!s$, just
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   354
that $s$ is the empty string. Therefore we return the
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   355
$\ONE$-regular expression. In the other case we again
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   356
return $\ZERO$ since no string of the $c\!::\!s$ can be
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   357
matched. Next come the recursive cases, which are a bit more
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   358
involved. Fortunately, the $+$-case is still relatively
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   359
straightforward: all strings of the form $c\!::\!s$ are either
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   360
matched by the regular expression $r_1$ or $r_2$. So we just
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   361
have to recursively call $\textit{der}$ with these two regular
332
4755ad4b457b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 325
diff changeset
   362
expressions and compose the results again with $+$. Makes
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   363
sense? 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   364
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   365
The $\cdot$-case is more complicated: if $r_1\cdot r_2$
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   366
matches a string of the form $c\!::\!s$, then the first part
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   367
must be matched by $r_1$. Consequently, it makes sense to
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   368
construct the regular expression for $s$ by calling $\textit{der}$ with
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   369
$r_1$ and ``appending'' $r_2$. There is however one exception
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   370
to this simple rule: if $r_1$ can match the empty string, then
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   371
all of $c\!::\!s$ is matched by $r_2$. So in case $r_1$ is
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   372
nullable (that is can match the empty string) we have to allow
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   373
the choice $\textit{der}\,c\,r_2$ for calculating the regular
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   374
expression that can match $s$. Therefore we have to add the
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   375
regular expression $\textit{der}\,c\,r_2$ in the result. The $*$-case
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   376
is again simple: if $r^*$ matches a string of the form
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   377
$c\!::\!s$, then the first part must be ``matched'' by a
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   378
single copy of $r$. Therefore we call recursively $\textit{der}\,c\,r$
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   379
and ``append'' $r^*$ in order to match the rest of $s$. Still
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   380
makes sense?
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   381
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   382
If all this did not make sense yet, here is another way to explain the
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   383
definition of $\textit{der}$ by considering the following operation on
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   384
sets:
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   385
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   386
\begin{equation}\label{Der}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   387
\textit{Der}\,c\,A\;\dn\;\{s\,|\,c\!::\!s \in A\}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   388
\end{equation}
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   389
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 272
diff changeset
   390
\noindent This operation essentially transforms a set of
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 272
diff changeset
   391
strings $A$ by filtering out all strings that do not start
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 272
diff changeset
   392
with $c$ and then strips off the $c$ from all the remaining
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 272
diff changeset
   393
strings. For example suppose $A = \{f\!oo, bar, f\!rak\}$ then
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   394
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   395
\[ \textit{Der}\,f\,A = \{oo, rak\}\quad,\quad 
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   396
   \textit{Der}\,b\,A = \{ar\} \quad \text{and} \quad 
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   397
   \textit{Der}\,a\,A = \{\} 
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   398
\]
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   399
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   400
\noindent
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   401
Note that in the last case $\textit{Der}$ is empty, because no string in $A$
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   402
starts with $a$. With this operation we can state the following
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   403
property about $\textit{der}$:
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   404
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   405
\[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   406
L(\textit{der}\,c\,r) = \textit{Der}\,c\,(L(r))
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   407
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   408
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   409
\noindent
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   410
This property clarifies what regular expression $\textit{der}$ calculates,
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   411
namely take the set of strings that $r$ can match (that is $L(r)$),
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   412
filter out all strings not starting with $c$ and strip off the $c$
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   413
from the remaining strings---this is exactly the language that
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   414
$\textit{der}\,c\,r$ can match.
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   415
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   416
If we want to find out whether the string $abc$ is matched by
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   417
the regular expression $r_1$ then we can iteratively apply $\textit{der}$
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   418
as follows
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   419
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   420
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   421
\begin{tabular}{rll}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   422
Input: $r_1$, $abc$\medskip\\
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   423
Step 1: & build derivative of $a$ and $r_1$ & $(r_2 = \textit{der}\,a\,r_1)$\smallskip\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   424
Step 2: & build derivative of $b$ and $r_2$ & $(r_3 = \textit{der}\,b\,r_2)$\smallskip\\
433
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 416
diff changeset
   425
Step 3: & build derivative of $c$ and $r_3$ & $(r_4 = \textit{der}\,c\,r_3)$\smallskip\\
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 416
diff changeset
   426
Step 4: & the string is exhausted: & $(\textit{nullable}(r_4))$\\
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 416
diff changeset
   427
        & test whether $r_4$ can recognise the\\
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   428
        & empty string\smallskip\\
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   429
Output: & result of this test $\Rightarrow \textit{true} \,\text{or}\, \textit{false}$\\        
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   430
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   431
\end{center}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 133
diff changeset
   432
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   433
\noindent Again the operation $\textit{Der}$ might help to rationalise
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   434
this algorithm. We want to know whether $abc \in L(r_1)$. We
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   435
do not know yet---but let us assume it is. Then $\textit{Der}\,a\,L(r_1)$
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   436
builds the set where all the strings not starting with $a$ are
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   437
filtered out. Of the remaining strings, the $a$ is stripped
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   438
off. So we should still have $bc$ in the set.
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   439
Then we continue with filtering out all strings not
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   440
starting with $b$ and stripping off the $b$ from the remaining
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   441
strings, that means we build $\textit{Der}\,b\,(\textit{Der}\,a\,(L(r_1)))$.
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   442
Finally we filter out all strings not starting with $c$ and
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   443
strip off $c$ from the remaining string. This is
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   444
$\textit{Der}\,c\,(\textit{Der}\,b\,(\textit{Der}\,a\,(L(r_1))))$. Now if $abc$ was in the 
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   445
original set ($L(r_1)$), then $\textit{Der}\,c\,(\textit{Der}\,b\,(\textit{Der}\,a\,(L(r_1))))$ 
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   446
must contain the empty string. If not, then $abc$ was not in the 
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   447
language we started with.
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 133
diff changeset
   448
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   449
Our matching algorithm using $\textit{der}$ and $\textit{nullable}$ works
571
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   450
similarly, just using regular expressions instead of sets. In order to
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   451
define our algorithm we need to extend the notion of derivatives from single
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   452
characters to strings. This can be done using the following
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   453
function, taking a string and a regular expression as input and
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   454
a regular expression as output.
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   455
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   456
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   457
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {\hspace{-10mm}}l@ {}}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   458
  $\textit{ders}\, []\, r$     & $\dn$ & $r$ & \\
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   459
  $\textit{ders}\, (c\!::\!s)\, r$ & $\dn$ & $\textit{ders}\,s\,(\textit{der}\,c\,r)$ & \\
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   460
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   461
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   462
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   463
\noindent This function iterates $\textit{der}$ taking one character at
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   464
the time from the original string until the string is exhausted.
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   465
Having $\textit{der}s$ in place, we can finally define our matching
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   466
algorithm:
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   467
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   468
\[
571
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   469
\textit{matches}\,r\,s \dn \textit{nullable}(\textit{ders}\,s\,r)
125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   470
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   471
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 124
diff changeset
   472
\noindent
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   473
and we can claim that
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   474
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   475
\[
571
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   476
\textit{matches}\,r\,s\quad\text{if and only if}\quad s\in L(r)
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   477
\]
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   478
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   479
\noindent holds, which means our algorithm satisfies the
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   480
specification. Of course we can claim many things\ldots
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   481
whether the claim holds any water is a different question,
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   482
which for example is the point of the Strand-2 Coursework.
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   483
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   484
This algorithm was introduced by Janusz Brzozowski in 1964, but 
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   485
is more widely known only in the last 10 or so years. Its
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   486
main attractions are simplicity and being fast, as well as
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   487
being easily extendible for other regular expressions such as
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   488
$r^{\{n\}}$, $r^?$, $\sim{}r$ and so on (this is subject of
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   489
Strand-1 Coursework 1). 
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   490
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   491
\subsection*{The Matching Algorithm in Scala}
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   492
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   493
Another attraction of the algorithm is that it can be easily
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   494
implemented in a functional programming language, like Scala.
296
796b9b81ac8d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 291
diff changeset
   495
Given the implementation of regular expressions in Scala shown
796b9b81ac8d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 291
diff changeset
   496
in the first lecture and handout, the functions and subfunctions
796b9b81ac8d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 291
diff changeset
   497
for \pcode{matches} are shown in Figure~\ref{scala1}.
126
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 125
diff changeset
   498
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 125
diff changeset
   499
\begin{figure}[p]
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   500
  \lstinputlisting[numbers=left,linebackgroundcolor=
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   501
                  {\ifodd\value{lstnumber}\color{capri!3}\fi}]
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   502
                  {../progs/app5.scala}
512
a6aa52ecc1c5 updated
cu
parents: 511
diff changeset
   503
\caption{A Scala implementation of \textit{nullable} and 
a6aa52ecc1c5 updated
cu
parents: 511
diff changeset
   504
  derivative function. These functions are easy to
a6aa52ecc1c5 updated
cu
parents: 511
diff changeset
   505
  implement in functional programming languages. This is because pattern 
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   506
  matching and recursion allow us to mimic the mathematical
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   507
  definitions very closely. Nearly all functional
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   508
  programming languages support pattern matching and
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   509
  recursion out of the box.\label{scala1}}
126
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 125
diff changeset
   510
\end{figure}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   512
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   513
%Remember our second example involving the regular expression
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   514
%$(a^*)^* \cdot b$ which could not match strings of $n$ \texttt{a}s. 
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   515
%Java needed around 30 seconds to find this out a string with $n=28$.
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   516
%It seems our algorithm is doing rather well in comparison:
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   517
%
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   518
%\begin{center}
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   519
%\begin{tikzpicture}
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   520
%\begin{axis}[
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   521
%    title={Graph: $(a^*)^* \cdot b$ and strings $\underbrace{a\ldots a}_{n}$},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   522
%    xlabel={$n$},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   523
%    x label style={at={(1.05,0.0)}},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   524
%    ylabel={time in secs},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   525
%    enlargelimits=false,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   526
%    xtick={0,1000,...,6500},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   527
%    xmax=6800,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   528
%    ytick={0,5,...,30},
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   529
%    ymax=34,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   530
%    scaled ticks=false,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   531
%    axis lines=left,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   532
%    width=8cm,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   533
%    height=4.5cm, 
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   534
%    legend entries={Java,Scala V1},  
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   535
%    legend pos=north east,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   536
%    legend cell align=left]
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   537
%\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   538
%\addplot[red,mark=triangle*,mark options={fill=white}] table {re1a.data};
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   539
%\end{axis}
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   540
%\end{tikzpicture}
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   541
%\end{center}
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   542
%
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   543
%\noindent
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   544
%This is not an error: it hardly takes more than half a second for
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   545
%strings up to the length of 6500. After that we receive a
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   546
%StackOverflow exception, but still\ldots
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   547
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   548
For running the algorithm with our first example, the evil
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   549
regular expression $a^?{}^{\{n\}}\cdot a^{\{n\}}$, we need to implement
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   550
the optional regular expression and the `exactly $n$-times
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   551
regular expression'. This can be done with the translations
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   552
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   553
\lstinputlisting[numbers=none]{../progs/app51.scala}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   554
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   555
\noindent Running the matcher with this example, we find it is
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   556
slightly worse then the matcher in Ruby and Python.
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   557
Ooops\ldots
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   558
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   559
\begin{center}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   560
\begin{tikzpicture}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   561
\begin{axis}[    
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   562
    title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings $\underbrace{a\ldots a}_{n}$},
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   563
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   564
    x label style={at={(1.05,0.0)}},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   565
    ylabel={time in secs},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   566
    enlargelimits=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   567
    xtick={0,5,...,30},
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   568
    xmax=32,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   569
    ytick={0,5,...,30},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   570
    scaled ticks=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   571
    axis lines=left,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   572
    width=6cm,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   573
    height=5cm, 
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   574
    legend entries={Python,Ruby,Scala V1},  
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   575
    legend pos=outer north east,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   576
    legend cell align=left]
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   577
\addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   578
\addplot[brown,mark=pentagon*, mark options={fill=white}] table {re-ruby.data};  
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   579
\addplot[red,mark=triangle*,mark options={fill=white}] table {re1.data};  
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   580
\end{axis}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   581
\end{tikzpicture}
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   582
\end{center}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   583
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   584
\noindent Analysing this failure we notice that for $a^{\{n\}}$, for
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   585
example, we generate quite big regular expressions:
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   586
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   587
\begin{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   588
\begin{tabular}{rl}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   589
1: & $a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   590
2: & $a\cdot a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   591
3: & $a\cdot a\cdot a$\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   592
& \ldots\\
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   593
13: & $a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a\cdot a$\\
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   594
& \ldots
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   595
\end{tabular}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   596
\end{center}
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   597
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   598
\noindent Our algorithm traverses such regular expressions at
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
   599
least once every time a derivative is calculated. So having
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   600
large regular expressions will cause problems. This problem
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   601
is aggravated by $a^?$ being represented as $a + \ONE$.
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   602
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   603
We can however fix this easily by having an explicit constructor for
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   604
$r^{\{n\}}$. In Scala we would introduce a constructor like
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   605
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   606
\begin{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   607
\code{case class NTIMES(r: Rexp, n: Int) extends Rexp}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   608
\end{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   609
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   610
\noindent With this fix we have a constant ``size'' regular expression
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   611
for our running example no matter how large $n$ is (see the
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   612
\texttt{size} section in the implementations).  This means we have to
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   613
also add cases for \pcode{NTIMES} in the functions $\textit{nullable}$
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   614
and $\textit{der}$. Does the change have any effect?
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   615
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   616
\begin{center}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   617
\begin{tikzpicture}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   618
\begin{axis}[
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   619
    title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings $\underbrace{a\ldots a}_{n}$},
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   620
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   621
    x label style={at={(1.01,0.0)}},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   622
    ylabel={time in secs},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   623
    enlargelimits=false,
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   624
    xtick={0,200,...,1100},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   625
    xmax=1200,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   626
    ytick={0,5,...,30},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   627
    scaled ticks=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   628
    axis lines=left,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   629
    width=10cm,
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   630
    height=5cm, 
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   631
    legend entries={Python,Ruby,Scala V1,Scala V2},  
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   632
    legend pos=outer north east,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   633
    legend cell align=left]
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   634
\addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   635
\addplot[brown,mark=pentagon*, mark options={fill=white}] table {re-ruby.data};  
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   636
\addplot[red,mark=triangle*,mark options={fill=white}] table {re1.data};  
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
   637
\addplot[green,mark=square*,mark options={fill=white}] table {re2.data};
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   638
\end{axis}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   639
\end{tikzpicture}
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   640
\end{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   641
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   642
\noindent Now we are talking business! The modified matcher can within
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   643
25 seconds handle regular expressions up to $n = 1,100$ before a
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   644
StackOverflow is raised. Recall that Python and Ruby (and our first
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   645
version, Scala V1) could only handle $n = 27$ or so in 30
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   646
seconds. We have not tried our algorithm on the second example $(a^*)^* \cdot
511
   647
b$---I leave this to you.
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   648
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   649
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   650
The moral is that our algorithm is rather sensitive to the
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   651
size of regular expressions it needs to handle. This is of
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   652
course obvious because both $\textit{nullable}$ and $\textit{der}$ frequently
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   653
need to traverse the whole regular expression. There seems,
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   654
however, one more issue for making the algorithm run faster.
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   655
The derivative function often produces ``useless''
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   656
$\ZERO$s and $\ONE$s. To see this, consider $r = ((a
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   657
\cdot b) + b)^*$ and the following three derivatives
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   658
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   659
\begin{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   660
\begin{tabular}{l}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   661
$\textit{der}\,a\,r = ((\ONE \cdot b) + \ZERO) \cdot r$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   662
$\textit{der}\,b\,r = ((\ZERO \cdot b) + \ONE)\cdot r$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   663
$\textit{der}\,c\,r = ((\ZERO \cdot b) + \ZERO)\cdot r$
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   664
\end{tabular}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   665
\end{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   666
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   667
\noindent 
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   668
If we simplify them according to the simplification rules from the
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   669
beginning, we can replace the right-hand sides by the smaller
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   670
equivalent regular expressions
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   671
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   672
\begin{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   673
\begin{tabular}{l}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   674
$\textit{der}\,a\,r \equiv b \cdot r$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   675
$\textit{der}\,b\,r \equiv r$\\
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   676
$\textit{der}\,c\,r \equiv \ZERO$
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   677
\end{tabular}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   678
\end{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   679
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   680
\noindent I leave it to you to contemplate whether such a
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   681
simplification can have any impact on the correctness of our algorithm
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   682
(will it change any answers?). Figure~\ref{scala2} gives a
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   683
simplification function that recursively traverses a regular
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   684
expression and simplifies it according to the rules given at the
571
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   685
beginning. There are only rules for $+$ and $\cdot$. There is 
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   686
no simplification rule for a star, because
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   687
empirical data and also a little thought showed that simplifying under
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   688
a star is a waste of computation time. The simplification function
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   689
will be called after every derivation.  This additional step removes
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   690
all the ``junk'' the derivative function introduced. Does this improve
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   691
the speed? You bet!!
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   692
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   693
\begin{figure}[p]
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   694
\lstinputlisting[numbers=left,linebackgroundcolor=
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   695
  {\ifodd\value{lstnumber}\color{capri!3}\fi}]
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   696
                {../progs/app6.scala}
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   697
\caption{The simplification function and modified 
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   698
\texttt{ders}-function; this function now
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 332
diff changeset
   699
calls \texttt{der} first, but then simplifies
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   700
the resulting derivative regular expressions before
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   701
building the next derivative, see
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   702
Line~24.\label{scala2}}
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   703
\end{figure}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   704
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   705
\begin{center}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   706
\begin{tikzpicture}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   707
\begin{axis}[
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   708
    title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings $\underbrace{a\ldots a}_{n}$},
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   709
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   710
    x label style={at={(1.04,0.0)}},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   711
    ylabel={time in secs},
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   712
    enlargelimits=false,
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   713
    xtick={0,2500,...,10000},
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   714
    xmax=12000,
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   715
    ytick={0,5,...,30},
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   716
    ymax=32,
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   717
    scaled ticks=false,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   718
    axis lines=left,
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   719
    width=9cm,
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   720
    height=5cm, 
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   721
    legend entries={Scala V2,Scala V3},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   722
    legend pos=outer north east,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   723
    legend cell align=left]
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   724
\addplot[green,mark=square*,mark options={fill=white}] table {re2.data};
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   725
\addplot[black,mark=square*,mark options={fill=white}] table {re3.data};
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   726
\end{axis}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 263
diff changeset
   727
\end{tikzpicture}
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   728
\end{center}
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
   729
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   730
\noindent
510
cu
parents: 492
diff changeset
   731
To recap, Python and Ruby needed approximately 30 seconds to match a
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   732
string of 28 \texttt{a}s and the regular expression $a^{?\{n\}} \cdot
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   733
a^{\{n\}}$.  We need a third of this time to do the same with strings
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   734
up to 11,000 \texttt{a}s.  Similarly, Java 8 and Python needed 30
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   735
seconds to find out the regular expression $(a^*)^* \cdot b$ does not
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   736
match the string of 28 \texttt{a}s. In Java 9 and later this has been 
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   737
cranked up to 39,000 \texttt{a}s, but we can do the same in the same 
571
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   738
amount of time for strings composed of nearly 6,000,000 \texttt{a}s. 
499007a7bce2 updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
   739
This is shown in the following plot.
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   740
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   741
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   742
\begin{center}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   743
\begin{tikzpicture}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   744
\begin{axis}[
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   745
    title={Graph: $(a^*)^* \cdot b$ and strings $\underbrace{a\ldots a}_{n}$},
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   746
    xlabel={$n$},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   747
    ylabel={time in secs},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   748
    enlargelimits=false,
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   749
    ymax=35,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   750
    ytick={0,5,...,30},
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   751
    axis lines=left,
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
   752
    %%scaled ticks=false,
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   753
    x label style={at={(1.09,0.0)}},
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
   754
    %%xmax=7700000,
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   755
    width=9cm,
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   756
    height=5cm, 
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   757
    legend entries={Scala V3},
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   758
    legend pos=outer north east,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   759
    legend cell align=left]
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   760
%\addplot[green,mark=square*,mark options={fill=white}] table {re2a.data};
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   761
\addplot[black,mark=square*,mark options={fill=white}] table {re3a.data};
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   762
\end{axis}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   763
\end{tikzpicture}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   764
\end{center}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   765
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   766
\subsection*{Epilogue}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   767
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 512
diff changeset
   768
(23/Aug/2016) I found another place where this algorithm can
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   769
be sped up (this idea is not integrated with what is coming next, but
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   770
I present it nonetheless). The idea is to not define \texttt{ders}
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   771
that it iterates the derivative character-by-character, but in bigger
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   772
chunks. The resulting code for \texttt{ders2} looks as follows:
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   773
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   774
\lstinputlisting[numbers=none]{../progs/app52.scala} 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   775
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   776
\noindent
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   777
I have not fully understood why this version is much faster,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   778
but it seems it is a combination of the clauses for \texttt{ALT}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   779
and \texttt{SEQ}. In the latter case we call \texttt{der} with 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   780
a single character and this potentially produces an alternative.
510
cu
parents: 492
diff changeset
   781
The derivative of such an alternative can then be more efficiently  
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   782
calculated by \texttt{ders2} since it pushes a whole string
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   783
under an \texttt{ALT}. The numbers are that in the second case  
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   784
$(a^*)^* \cdot b$ both versions are pretty much the same, but in the 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   785
first case $a^{?\{n\}} \cdot a^{\{n\}}$ the improvement gives 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   786
another factor of 100 speedup. Nice!
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   787
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   788
\begin{center}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   789
\begin{tabular}{cc}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   790
\begin{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   791
\begin{axis}[
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   792
    title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings $\underbrace{a\ldots a}_{n}$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   793
    xlabel={$n$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   794
    x label style={at={(1.04,0.0)}},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   795
    ylabel={time in secs},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   796
    enlargelimits=false,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   797
    xmax=7100000,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   798
    ytick={0,5,...,30},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   799
    ymax=33,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   800
    %scaled ticks=false,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   801
    axis lines=left,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   802
    width=5.3cm,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   803
    height=5cm, 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   804
    legend entries={Scala V3, Scala V4},
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   805
    legend style={at={(0.1,-0.2)},anchor=north}]
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   806
\addplot[black,mark=square*,mark options={fill=white}] table {re3.data};
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   807
\addplot[purple,mark=square*,mark options={fill=white}] table {re4.data};
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   808
\end{axis}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   809
\end{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   810
&
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   811
\begin{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   812
\begin{axis}[
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   813
    title={Graph: $(a^*)^* \cdot b$ and strings $\underbrace{a\ldots a}_{n}$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   814
    xlabel={$n$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   815
    x label style={at={(1.09,0.0)}},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   816
    ylabel={time in secs},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   817
    enlargelimits=false,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   818
    xmax=8200000,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   819
    ytick={0,5,...,30},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   820
    ymax=33,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   821
    %scaled ticks=false,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   822
    axis lines=left,
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   823
    width=5.3cm,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   824
    height=5cm, 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   825
    legend entries={Scala V3, Scala V4},
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   826
    legend style={at={(0.1,-0.2)},anchor=north}]
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   827
\addplot[black,mark=square*,mark options={fill=white}] table {re3a.data};
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   828
\addplot[purple,mark=square*,mark options={fill=white}] table {re4a.data};
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   829
\end{axis}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   830
\end{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   831
\end{tabular}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   832
\end{center}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   833
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   834
334
fd89a63e9db3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   835
\section*{Proofs}
fd89a63e9db3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   836
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   837
You might not like doing proofs. But they serve a very
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   838
important purpose in Computer Science: How can we be sure that
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   839
our algorithm matches its specification? We can try to test
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   840
the algorithm, but that often overlooks corner cases and an
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   841
exhaustive testing is impossible (since there are infinitely
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   842
many inputs). Proofs allow us to ensure that an algorithm
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   843
really meets its specification. 
338
f16120cb4e19 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 334
diff changeset
   844
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   845
For the programs we look at in this module, the proofs will
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   846
mostly by some form of induction. Remember that regular
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   847
expressions are defined as 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   848
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   849
\begin{center}
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   850
\begin{tabular}{r@{\hspace{1mm}}r@{\hspace{1mm}}l@{\hspace{13mm}}l}
512
a6aa52ecc1c5 updated
cu
parents: 511
diff changeset
   851
  $r$ & $::=$ &   $\ZERO$         & nothing\\
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   852
        & $\mid$ & $\ONE$           & empty string / \texttt{""} / []\\
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   853
        & $\mid$ & $c$                  & single character\\
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   854
        & $\mid$ & $r_1 + r_2$          & alternative / choice\\
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   855
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   856
        & $\mid$ & $r^*$                & star (zero or more)\\
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   857
  \end{tabular}
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   858
\end{center}
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   859
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   860
\noindent If you want to show a property $P(r)$ for \emph{all} 
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   861
regular expressions $r$, then you have to follow essentially 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   862
the recipe:
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   863
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   864
\begin{itemize}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   865
\item $P$ has to hold for $\ZERO$, $\ONE$ and $c$
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   866
 (these are the base cases).
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   867
\item $P$ has to hold for $r_1 + r_2$ under the assumption 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   868
   that $P$ already holds for $r_1$ and $r_2$.
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   869
\item $P$ has to hold for $r_1 \cdot r_2$ under the 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   870
  assumption that $P$ already holds for $r_1$ and $r_2$.
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   871
\item $P$ has to hold for $r^*$ under the assumption 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   872
  that $P$ already holds for $r$.
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   873
\end{itemize}
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   874
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   875
\noindent 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   876
A simple proof is for example showing the following 
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   877
property:
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   878
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   879
\begin{equation}
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   880
\textit{nullable}(r) \;\;\text{if and only if}\;\; []\in L(r)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   881
\label{nullableprop}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   882
\end{equation}
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   883
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   884
\noindent
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   885
Let us say that this property is $P(r)$, then the first case
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   886
we need to check is whether $P(\ZERO)$ (see recipe 
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   887
above). So we have to show that
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   888
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   889
\[
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   890
\textit{nullable}(\ZERO) \;\;\text{if and only if}\;\; 
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   891
[]\in L(\ZERO)
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   892
\]
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   893
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   894
\noindent whereby $\textit{nullable}(\ZERO)$ is by definition of
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   895
the function $\textit{nullable}$ always $\textit{false}$. We also have
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   896
that $L(\ZERO)$ is by definition $\{\}$. It is
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   897
impossible that the empty string $[]$ is in the empty set.
339
bc395ccfba7f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   898
Therefore also the right-hand side is false. Consequently we
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   899
verified this case: both sides are false. We would still need
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   900
to do this for $P(\ONE)$ and $P(c)$. I leave this to
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   901
you to verify.
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   902
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   903
Next we need to check the inductive cases, for example
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   904
$P(r_1 + r_2)$, which is
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   905
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   906
\begin{equation}
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   907
\textit{nullable}(r_1 + r_2) \;\;\text{if and only if}\;\; 
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   908
[]\in L(r_1 + r_2)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   909
\label{propalt}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   910
\end{equation}
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   911
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   912
\noindent The difference to the base cases is that in the inductive
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   913
cases we can already assume we proved $P$ for the components, that is
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   914
we can assume.
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   915
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   916
\begin{center}
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   917
\begin{tabular}{l}
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   918
$\textit{nullable}(r_1) \;\;\text{if and only if}\;\; []\in L(r_1)$ and\\
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   919
$\textit{nullable}(r_2) \;\;\text{if and only if}\;\; []\in L(r_2)$\\
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   920
\end{tabular}
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   921
\end{center}
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   922
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   923
\noindent These are called the induction hypotheses. To check this 
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   924
case, we can start from $\textit{nullable}(r_1 + r_2)$, which by 
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   925
definition of $\textit{nullable}$ is
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   926
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   927
\[
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   928
\textit{nullable}(r_1) \vee \textit{nullable}(r_2)
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   929
\]
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   930
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   931
\noindent Using the two induction hypotheses from above,
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   932
we can transform this into 
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   933
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   934
\[
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   935
[] \in L(r_1) \vee []\in(r_2)
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   936
\]
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   937
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   938
\noindent We just replaced the $\textit{nullable}(\ldots)$ parts by
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   939
the equivalent $[] \in L(\ldots)$ from the induction 
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   940
hypotheses. A bit of thinking convinces you that if
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   941
$[] \in L(r_1) \vee []\in L(r_2)$ then the empty string
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   942
must be in the union $L(r_1)\cup L(r_2)$, that is
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   943
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   944
\[
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   945
[] \in L(r_1)\cup L(r_2)
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   946
\]
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   947
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   948
\noindent but this is by definition of $L$ exactly $[] \in L(r_1 +
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   949
r_2)$, which we needed to establish according to statement in
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   950
\eqref{propalt}. What we have shown is that starting from
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
   951
$\textit{nullable}(r_1 + r_2)$ we have done equivalent transformations
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   952
to end up with $[] \in L(r_1 + r_2)$. Consequently we have established
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   953
that $P(r_1 + r_2)$ holds.
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   954
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   955
In order to complete the proof we would now need to look 
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   956
at the cases \mbox{$P(r_1\cdot r_2)$} and $P(r^*)$. Again I let you
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   957
check the details.
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   958
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   959
You might also have to do induction proofs over strings. 
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   960
That means you want to establish a property $P(s)$ for all
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   961
strings $s$. For this remember strings are lists of 
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   962
characters. These lists can be either the empty list or a
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   963
list of the form $c::s$. If you want to perform an induction
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   964
proof for strings you need to consider the cases
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   965
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   966
\begin{itemize}
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   967
\item $P$ has to hold for $[]$ (this is the base case).
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   968
\item $P$ has to hold for $c::s$ under the assumption 
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   969
   that $P$ already holds for $s$.
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   970
\end{itemize}
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   971
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   972
\noindent
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   973
Given this recipe, I let you show
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   974
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   975
\begin{equation}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   976
\textit{Ders}\,s\,(L(r)) = L(\textit{ders}\,s\,r)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   977
\label{dersprop}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   978
\end{equation}
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   979
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   980
\noindent by induction on $s$. Recall $\textit{Der}$ is defined for 
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   981
character---see \eqref{Der}; $\textit{Ders}$ is similar, but for strings:
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   982
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   983
\[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   984
\textit{Ders}\,s\,A\;\dn\;\{s'\,|\,s @ s' \in A\}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   985
\]
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   986
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   987
\noindent In this proof you can assume the following property
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   988
for $der$ and $\textit{Der}$ has already been proved, that is you can
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   989
assume
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   990
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   991
\[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   992
L(\textit{der}\,c\,r) = \textit{Der}\,c\,(L(r))
340
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   993
\]
c49122dbcdd1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 339
diff changeset
   994
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   995
\noindent holds (this would be of course another property that needs
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   996
to be proved in a side-lemma by induction on $r$). This is a bit
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   997
more challenging, but not impossible.
338
f16120cb4e19 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 334
diff changeset
   998
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
   999
To sum up, using reasoning like the one shown above allows us 
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1000
to show the correctness of our algorithm. To see this,
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1001
start from the specification
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1002
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1003
\[
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1004
s \in L(r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1005
\]
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1006
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1007
\noindent That is the problem we want to solve. Thinking a 
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1008
little, you will see that this problem is equivalent to the 
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1009
following problem
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1010
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1011
\begin{equation}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1012
[] \in \textit{Ders}\,s\,(L(r))
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1013
\label{dersstep}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1014
\end{equation}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1015
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1016
\noindent You agree?  But we have shown above in \eqref{dersprop},
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1017
that the $\textit{Ders}$ can be replaced by
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1018
$L(\textit{ders}\ldots)$. That means \eqref{dersstep} is equivalent to
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1019
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1020
\begin{equation}
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1021
[] \in L(\textit{ders}\,s\,r)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1022
\label{prefinalstep}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1023
\end{equation}
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1024
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1025
\noindent We have also shown that testing whether the empty
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1026
string is in a language is equivalent to the $\textit{nullable}$
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1027
function; see \eqref{nullableprop}. That means
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1028
\eqref{prefinalstep} is equivalent with
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1029
 
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1030
\[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1031
\textit{nullable}(\textit{ders}\,s\,r)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1032
\] 
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1033
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1034
\noindent But this is just the definition of $matches$
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1035
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1036
\[
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1037
matches\,s\,r \dn nullable(\textit{ders}\,s\,r)
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1038
\]
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1039
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1040
\noindent In effect we have shown
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1041
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1042
\[
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1043
matches\,s\,r\;\;\text{if and only if}\;\;
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1044
s\in L(r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1045
\]
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1046
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1047
\noindent which is the property we set out to prove: our algorithm
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1048
meets its specification. To have done so, requires a few induction
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1049
proofs about strings and regular expressions. Following the \emph{induction
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1050
recipes} is already a big step in actually performing these proofs.
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1051
If you do not believe it, proofs have helped me to make sure my code
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
  1052
is correct and in several instances prevented me of letting slip
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
  1053
embarrassing mistakes into the `wild'. 
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 340
diff changeset
  1054
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
  1055
\end{document}
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
  1056
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
  1057
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 259
diff changeset
  1058
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
  1059
% !TeX program = latexmk -xelatex
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1060
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1061
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1062
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1063
%%% End: