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