cws/main_cw03.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 14 Nov 2025 10:57:21 +0000
changeset 500 fd7f8aff848c
parent 498 0f1b97538ad4
child 502 5321311c02ad
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
257
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
     1
% !TEX program = xelatex
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
423
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 422
diff changeset
     3
\usepackage{../styles/style}
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 422
diff changeset
     4
\usepackage{../styles/langs}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     5
\usepackage{disclaimer}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     6
\usepackage{tikz}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     7
\usepackage{pgf}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     8
\usepackage{pgfplots}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     9
\usepackage{stackengine}
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
    10
\usepackage{scalerel}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
    11
\DeclareMathOperator*{\amp}{\scalerel*{\&}{\sum}}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    12
%% \usepackage{accents}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    13
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    14
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    15
\begin{filecontents}{re-python2.data}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    16
1 0.033
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    17
5 0.036
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    18
10 0.034
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    19
15 0.036
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    20
18 0.059
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    21
19 0.084 
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    22
20 0.141
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    23
21 0.248
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    24
22 0.485
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    25
23 0.878
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    26
24 1.71
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    27
25 3.40
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    28
26 7.08
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    29
27 14.12
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    30
28 26.69
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    31
\end{filecontents}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    32
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    33
\begin{filecontents}{re-java.data}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    34
5  0.00298
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    35
10  0.00418
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    36
15  0.00996
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    37
16  0.01710
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    38
17  0.03492
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    39
18  0.03303
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    40
19  0.05084
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    41
20  0.10177
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    42
21  0.19960
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    43
22  0.41159
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    44
23  0.82234
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    45
24  1.70251
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    46
25  3.36112
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    47
26  6.63998
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    48
27  13.35120
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    49
28  29.81185
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    50
\end{filecontents}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    51
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    52
\begin{filecontents}{re-js.data}
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    53
5   0.061
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    54
10  0.061
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    55
15  0.061
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    56
20  0.070
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    57
23  0.131
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    58
25  0.308
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    59
26  0.564
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    60
28  1.994
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    61
30  7.648
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    62
31  15.881 
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    63
32  32.190
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    64
\end{filecontents}
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    65
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    66
\begin{filecontents}{re-java9.data}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    67
1000  0.01410
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    68
2000  0.04882
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    69
3000  0.10609
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    70
4000  0.17456
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    71
5000  0.27530
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    72
6000  0.41116
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    73
7000  0.53741
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    74
8000  0.70261
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    75
9000  0.93981
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    76
10000 0.97419
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    77
11000 1.28697
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    78
12000 1.51387
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    79
14000 2.07079
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    80
16000 2.69846
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    81
20000 4.41823
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    82
24000 6.46077
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    83
26000 7.64373
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    84
30000 9.99446
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    85
34000 12.966885
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    86
38000 16.281621
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    87
42000 19.180228
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    88
46000 21.984721
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    89
50000 26.950203
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    90
60000 43.0327746
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    91
\end{filecontents}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    92
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    93
\begin{filecontents}{re-swift.data}
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    94
5   0.001
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    95
10  0.001
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    96
15  0.009
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    97
20  0.178
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    98
23  1.399
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
    99
24  2.893
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   100
25  5.671
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   101
26  11.357
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   102
27  22.430
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   103
\end{filecontents}
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   104
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   105
\begin{filecontents}{re-dart.data}
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   106
20 0.042
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   107
21 0.084
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   108
22 0.190
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   109
23 0.340
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   110
24 0.678
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   111
25 1.369
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   112
26 2.700
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   113
27 5.462
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   114
28 10.908
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   115
29 21.725
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   116
30 43.492
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   117
\end{filecontents}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   121
% BF IDE
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   122
% https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   123
  
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   124
\section*{Main Part 3 (Scala, 6 Marks)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
422
be20e7f67f76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 421
diff changeset
   126
\mbox{}\hfill\textit{``Java is the most distressing thing to happen to computing since MS-DOS.''}\smallskip\\
be20e7f67f76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 421
diff changeset
   127
\mbox{}\hfill\textit{ --- Alan Kay, the inventor of object-oriented programming}\bigskip\medskip
275
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
   128
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
   129
\noindent
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   130
This part is about a regular expression matcher described by
412
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   131
Brzozowski in 1964.  The
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   132
background is that ``out-of-the-box'' regular expression matching in
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   133
mainstream languages like Java, JavaScript and Python can sometimes be
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   134
excruciatingly slow.  You are supposed to implement a regular
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   135
expression matcher that is much, much faster. \bigskip
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   136
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   137
\IMPORTANTNONE{}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   138
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   139
\noindent
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   140
Also note that the running time of each part will be restricted to a
257
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
   141
maximum of 30 seconds on my laptop.  
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   142
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   143
\DISCLAIMER{}
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
   144
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   145
\subsection*{Reference Implementation}
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   146
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   147
This Scala assignment comes with a reference implementation in form of
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   148
a \texttt{jar}-file. This allows you to run any test cases on your own
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   149
computer. For example you can call \texttt{scala} on the command
472
fbff6f601370 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   150
line with the option \texttt{--extra-jars re.jar} and then query any function
fbff6f601370 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   151
from the \texttt{re.scala} template file. As usual you have to prefix
fbff6f601370 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   152
the calls with \texttt{M3} or import this object.  Since some tasks
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   153
are time sensitive, you can check the reference implementation as
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   154
follows: if you want to know, for example, how long it takes to match
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   155
strings of $a$'s using the regular expression $(a^*)^*\cdot b$ you can
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   156
query as follows:
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   157
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   158
245
744496a2bade added marking
Christian Urban <urbanc@in.tum.de>
parents: 224
diff changeset
   159
\begin{lstlisting}[xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small]
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   160
$ scala --extra-jars re.jar
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   161
scala> import M3._  
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   162
scala> for (i <- 0 to 5000000 by 500000) {
478
0e6ca70496c1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
   163
  println(s"$i: ${time_needed(2, matcher(EVIL, "a" * i))}")
0e6ca70496c1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
   164
}
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   165
0: 0.00002 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   166
500000: 0.10608 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   167
1000000: 0.22286 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   168
1500000: 0.35982 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   169
2000000: 0.45828 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   170
2500000: 0.59558 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   171
3000000: 0.73191 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   172
3500000: 0.83499 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   173
4000000: 0.99149 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   174
4500000: 1.15395 secs.
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 289
diff changeset
   175
5000000: 1.29659 secs.
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   176
\end{lstlisting}%$
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   177
478
0e6ca70496c1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
   178
\noindent
0e6ca70496c1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
   179
For this you need to copy the \texttt{time\_needed} function and the \texttt{EVIL} regular
0e6ca70496c1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
   180
expression from the comments given in \texttt{re.scala}.
421
Christian Urban <christian.urban@kcl.ac.uk>
parents: 415
diff changeset
   181
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   182
\subsection*{Preliminaries}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   183
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   184
The task is to implement a regular expression matcher that is based on
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   185
derivatives of regular expressions. Most of the functions are defined by
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   186
recursion over regular expressions and can be elegantly implemented
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   187
using Scala's pattern-matching. The implementation should deal with the
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   188
following regular expressions, which have been predefined in the file
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   189
\texttt{re.scala}:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   191
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   192
\begin{tabular}{lcll}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   193
  $r$ & $::=$ & $\ZERO$     & cannot match anything\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   194
      &   $|$ & $\ONE$      & can only match the empty string\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   195
      &   $|$ & $c$         & can match a single character (in this case $c$)\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   196
      &   $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   197
      &   $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   198
          &  & & then the second part with $r_2$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   199
      &   $|$ & $r_1 \,\&\, r_2$ & has to match a string with both $r_1$ and $r_2$\\
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   200
      &   $|$ & $r^*$       & can match a string with zero or more copies of $r$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   201
      &   $|$ & $r^{\{n\}}$  & can match a string with exactly n copies of $r$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   202
\end{tabular}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   203
\end{center}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   205
\noindent 
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   206
Why? Regular expressions are
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   207
one of the simplest ways to match patterns in text, and
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   208
are endlessly useful for searching, editing and analysing data in all
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   209
sorts of places (for example analysing network traffic in order to
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   210
detect security breaches). However, you need to be fast, otherwise you
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   211
will stumble over problems such as reported in
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   212
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   213
{\small
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   214
\begin{itemize}
289
38f67c1264e0 updated
Christian Urban <urbanc@in.tum.de>
parents: 284
diff changeset
   215
\item[$\bullet$] \url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019}  
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   216
\item[$\bullet$] \texttt{\href{https://web.archive.org/web/20160801163029/https://www.stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   217
\item[$\bullet$] \url{https://vimeo.com/112065252}
289
38f67c1264e0 updated
Christian Urban <urbanc@in.tum.de>
parents: 284
diff changeset
   218
\item[$\bullet$] \url{https://davidvgalbraith.com/how-i-fixed-atom}  
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   219
\end{itemize}}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   220
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   221
% Knowing how to match regular expressions and strings will let you
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   222
% solve a lot of problems that vex other humans.
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   223
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   224
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   225
\subsubsection*{Tasks (file re.scala)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   227
The file \texttt{re.scala} has already a definition for regular
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   228
expressions and also defines some handy shorthand notation for regular
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   229
expressions. The notation in this coursework description matches up
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   230
with the code as follows:
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   231
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   232
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   233
  \begin{tabular}{rcl@{\hspace{10mm}}l}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   234
    & & code: & shorthand:\smallskip \\ 
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   235
  $\ZERO$ & $\mapsto$ & \texttt{ZERO}\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   236
  $\ONE$  & $\mapsto$ & \texttt{ONE}\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   237
  $c$     & $\mapsto$ & \texttt{CHAR(c)}\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   238
  $r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   239
  $r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)} & \texttt{r1 $\sim$ r2}\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   240
  $r_1 \,\&\, r_2$ & $\mapsto$ & \texttt{AND(r1, r2)}\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   241
  $r^*$ & $\mapsto$ &  \texttt{STAR(r)} & \texttt{r.\%}\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   242
  $r^{\{n\}}$ & $\mapsto$ &  \texttt{REP(r, n)} & 
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   243
\end{tabular}    
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   244
\end{center}  
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   245
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   246
\begin{itemize}
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   247
\item[(1)] Implement a function, called \textit{nullable}, by
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   248
  recursion over regular expressions. This function tests whether a
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   249
  regular expression can match the empty string. This means given a
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   250
  regular expression, it either returns true or false. The function
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   251
  \textit{nullable}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   252
  is defined as follows:
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   253
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   254
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   255
\begin{tabular}{lcl}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   256
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   257
$\textit{nullable}(\ONE)$  & $\dn$ & $\textit{true}$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   258
$\textit{nullable}(c)$     & $\dn$ & $\textit{false}$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   259
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee  \textit{nullable}(r_2)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   260
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge  \textit{nullable}(r_2)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   261
$\textit{nullable}(r_1 \& r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge  \textit{nullable}(r_2)$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   262
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   263
$\textit{nullable}(r^{\{n\}})$ & $\dn$ & 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   264
$\begin{cases}\textit{true} & n = 0\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   265
\textit{nullable}(r) & \textit{otherwise}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   266
\end{cases}$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   267
\end{tabular}
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   268
\end{center}~\hfill[0.5 Marks]
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   269
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   270
\item[(2)] Implement a function, called \textit{der}, by recursion over
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   271
  regular expressions. It takes a character and a regular expression
421
Christian Urban <christian.urban@kcl.ac.uk>
parents: 415
diff changeset
   272
  as arguments and calculates the \emph{derivative} of a regular expression according
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   273
  to the rules:
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   274
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   275
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   276
\begin{tabular}{lcl}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   277
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   278
$\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   279
$\textit{der}\;c\;(d)$     & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   280
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   281
$\textit{der}\;c\;(r_1 \& r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) \,\&\, (\textit{der}\;c\;r_2)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   282
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   283
      & & $\textit{then}\;(\textit{der}\;c\;r_1)\cdot r_2 \,+\, (\textit{der}\;c\;r_2)$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   284
      & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   285
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   286
$\textit{der}\;c\;(r^{\{n\}})$ & $\dn$ & 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   287
$\begin{cases}\ZERO & n = 0\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   288
(der\,c\,r)\cdot (r^{\{n-1\}}) & \textit{otherwise}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   289
\end{cases}$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   290
\end{tabular}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   291
\end{center}
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   292
\mbox{}\hfill\mbox{[1.5 Marks]}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   293
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   294
% \item[(3)] We next want to simplify regular expressions: essentially
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   295
%   we want to remove $\ZERO$ in regular expressions like $r + \ZERO$
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   296
%   and $\ZERO + r$.  However, our n-ary alternative takes a list of
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   297
%   regular expressions as argument, and we therefore need a more general
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   298
%   ``denesting'' function, which deletes $\ZERO$s and ``spills out'' nested
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   299
%   $\sum$s.  This function, called \texttt{denest}, should analyse a
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   300
%   list of regular expressions, say $rs$, as follows:
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   301
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   302
%   \begin{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   303
%     \begin{tabular}{lllll}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   304
%       1) &$rs = []$                & $\dn$ & $[]$ & (empty list)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   305
%       2) &$rs = \ZERO :: rest$     & $\dn$ & $\texttt{denest}\;rest$ & (throw away $\ZERO$)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   306
%       3) &$rs = (\sum rs) :: rest$ & $\dn$ & $rs ::: \texttt{denest}\;rest$ & (spill out $\sum$)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   307
%       4) &$rs = r :: rest$         & $\dn$ & $r :: \texttt{denest}\;rest$ & (otherwise)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   308
%     \end{tabular}  
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   309
%   \end{center}  
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   310
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   311
%   The first clause states that empty lists cannot be further
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   312
%   denested. The second removes the first $\ZERO$ from the list and recurses.
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   313
%   The third is when the first regular expression is an \texttt{ALTs}, then
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   314
%   the content of this alternative should be spilled out and appended
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   315
%   with the denested rest of the list. The last case is for all other
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   316
%   cases where the head of the list is not $\ZERO$ and not an \texttt{ALTs},
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   317
%   then we just keep the head of the list and denest the rest.\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   318
%   \mbox{}\hfill\mbox{[1 Mark]}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   319
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   320
\item[(3)] Implement the function \textit{simp}, which recursively
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   321
  traverses a regular expression, and on the way up simplifies every
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   322
  regular expression on the left (see below) to the regular expression
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   323
  on the right, except it does not simplify inside ${}^*$ and ${}^{\{n\}}$-regular
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   324
  expressions.
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   325
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   326
  \begin{center}
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   327
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   328
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   329
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   330
$r \cdot \ONE$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   331
$\ONE \cdot r$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   332
$r + \ZERO$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   333
$\ZERO + r$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   334
$r + r$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   335
$r \,\&\, \ZERO$ & $\mapsto$ & $\ZERO$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   336
$\ZERO \,\&\, r$ & $\mapsto$ & $\ZERO$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   337
$r \,\&\, r$ & $\mapsto$ & $r$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   338
\end{tabular}
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   339
  \end{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   340
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   341
  For example the regular expression
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   342
  \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \,\&\, (r_4 \cdot \ZERO)\]
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   343
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   344
  simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   345
  seen as trees and there are several methods for traversing
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   346
  trees. One of them corresponds to the inside-out traversal, which is also
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   347
  sometimes called post-order tra\-versal: you traverse inside the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   348
  tree and on the way up you apply simplification rules.
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   349
  \textbf{Another Hint:}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   350
  Remember numerical expressions from school times---there you had expressions
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   351
  like $u + \ldots + (1 \cdot x) - \ldots (z + (y \cdot 0)) \ldots$
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   352
  and simplification rules that looked very similar to rules
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   353
  above. You would simplify such numerical expressions by replacing
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   354
  for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   355
  look whether more rules are applicable. If you organise the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   356
  simplification in an inside-out fashion, it is always clear which
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   357
  simplification should be applied next.\hfill[2 Marks]
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   358
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   359
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   360
% \item[(4)] Implement the function \texttt{flts} which flattens our
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   361
%   n-ary sequence regular expression $\prod$. Like \texttt{denest}, this
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   362
%   function is intended to delete $\ONE$s and spill out nested $\prod$s.
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   363
%   Unfortunately, there is a special case to do with $\ZERO$: If this function encounters a $\ZERO$, then
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   364
%   the whole ``product'' should be $\ZERO$. The problem is that the $\ZERO$ can be anywhere
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   365
%   inside the list. The easiest way to implement this function is therefore by using an
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   366
%   accumulator, which when called is set to \texttt{Nil}. This means \textit{flts} takes
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   367
%   two arguments (which are both lists of regular expressions)
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   368
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   369
%   \[
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   370
%   \texttt{flts}\;rs\;acc  
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   371
%   \]
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   372
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   373
%   This function analyses the list $rs$ as follows
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   374
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   375
%   \begin{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   376
%     \begin{tabular}{@{}lllll@{}}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   377
%       1) &$rs = []$                   & $\dn$ & $acc$ & (empty list)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   378
%       2) &$rs = \ZERO :: rest$        & $\dn$ & $[\ZERO]$ & (special case for $\ZERO$)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   379
%       3) &$rs = \ONE :: rest$         & $\dn$ & $\texttt{flts}\,rest\,acc$ & (throw away $\ONE$)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   380
%       4) &$rs = (\prod rs) :: rest$ & $\dn$ & $\texttt{flts}\;rest\,(acc ::: rs)$ & (spill out $\prod$)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   381
%       5) &$rs = r :: rest$            & $\dn$ & $\texttt{flts}\;rest\,(acc ::: [r])$& (otherwise)\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   382
%     \end{tabular}  
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   383
%   \end{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   384
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   385
%   In the first case we just return whatever has accumulated in
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   386
%   $acc$. In the fourth case we spill out the $rs$ by appending the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   387
%   $rs$ to the end of the accumulator. Similarly in the last case we
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   388
%   append the single regular expression $r$ to the end of the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   389
%   accumulator. I let you think why you have to add it to the end. \mbox{}\hfill\mbox{[1 Mark]}
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   390
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   391
% \item[(5)] Before we can simplify regular expressions, we need what is often called
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   392
%   \emph{smart constructors} for $\sum$ and $\prod$. While the ``normal'' constructors
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   393
%   \texttt{ALTs} and \texttt{SEQs} give us alternatives and sequences, respectively, \emph{smart}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   394
%   constructors might return something different depending on what list of regular expressions
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   395
%   they are given as argument.
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   396
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   397
%   \begin{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   398
%   \begin{tabular}{@{}c@{\hspace{9mm}}c@{}}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   399
%     \begin{tabular}{l@{\hspace{2mm}}l@{\hspace{1mm}}ll}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   400
%       & $\sum^{smart}$\smallskip\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   401
%       1) & $rs = []$  & $\dn$ & $\ZERO$\\ 
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   402
%       2) & $rs = [r]$ & $\dn$ & $r$\\ \\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   403
%       3) & otherwise  & $\dn$ & $\sum\,rs$
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   404
%     \end{tabular} &
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   405
%     \begin{tabular}{l@{\hspace{2mm}}l@{\hspace{1mm}}ll}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   406
%         & $\prod^{smart}$\smallskip\\              
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   407
%         1) & $rs = []$ & $\dn$ & $\ONE$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   408
%         2a) & $rs = [\ZERO]$ & $\dn$ & $\ZERO$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   409
%         2b) & $rs = [r]$ & $\dn$ & $r$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   410
%         3) & otherwise  & $\dn$ & $\prod\,rs$
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   411
%     \end{tabular}              
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   412
%   \end{tabular}    
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   413
%   \end{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   414
%   \mbox{}\hfill\mbox{[0.5 Marks]}
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   415
  
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   416
% \item[(6)] Implement the function \textit{simp}, which recursively
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   417
%   traverses a regular expression, and on the way up simplifies every
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   418
%   regular expression on the left (see below) to the regular expression
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   419
%   on the right, except it does not simplify inside ${}^*$-regular
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   420
%   expressions and also does not simplify $\ZERO$, $\ONE$ and characters.
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   421
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   422
%   \begin{center}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   423
%     \begin{tabular}{@{}l@{\hspace{3mm}}c@{\hspace{3mm}}ll@{}}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   424
%       LHS: & & RHS:\smallskip\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   425
% $\sum\;[r_1,..,r_n]$ & $\mapsto$ & $\sum^{smart}\;(\texttt{(denest + distinct)}[simp(r_1),..,simp(r_n)])$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   426
%       $\prod\;[r_1,..,r_n]$ & $\mapsto$ & $\prod^{smart}\;(\texttt{(flts)}[simp(r_1),..,simp(r_n)])$\\
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   427
%       $r$ & $\mapsto$ & $r$ \quad (all other cases)
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   428
% \end{tabular}
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   429
% \end{center}
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   430
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   431
% The first case is as follows: first apply $simp$ to all regular
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   432
% expressions $r_1,.. ,r_n$; then denest the resulting list using
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   433
% \texttt{denest}; after that remove all duplicates in this list (this can be
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   434
% done in Scala using the function
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   435
% \texttt{\_.distinct}). Finally, you end up with a list of (simplified)
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   436
% regular expressions; apply the smart constructor $\sum^{smart}$ to this list.
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   437
% Similarly in the $\prod$ case: simplify first all regular
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   438
% expressions $r_1,.. ,r_n$; then flatten the resulting list using \texttt{flts} and apply the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   439
% smart constructor $\prod^{smart}$ to the result. In all other cases, just return the
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   440
% input $r$ as is.
415
368556c8df56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 412
diff changeset
   441
  
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   442
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   443
%   For example the regular expression
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   444
%   \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\]
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   445
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   446
%   simplifies to just $r_1$. \mbox{}\hfill\mbox{[1 Mark]}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   447
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   448
\item[(4)] Implement two functions: The first, called \textit{ders},
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   449
  takes a list of characters and a regular expression as arguments, and
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   450
  builds the derivative w.r.t.~the list as follows:
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   451
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   452
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   453
\begin{tabular}{lcl}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   454
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   455
  $\textit{ders}\;(c::cs)\;r$  & $\dn$ &
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   456
    $\textit{ders}\;cs\;(\textit{simp}\,(\textit{der}\;c\;r))$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   457
\end{tabular}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   458
\end{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   459
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   460
Note that this function is different from \textit{der}, which only
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   461
takes a single character.
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   462
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   463
The second function, called \textit{matcher}, takes a string and a
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   464
regular expression as arguments. It builds first the derivatives
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   465
according to \textit{ders} and after that tests whether the resulting
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   466
derivative regular expression can match the empty string (using
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   467
\textit{nullable}).  For example the \textit{matcher} will produce
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   468
true for the regular expression $(a\cdot b)\cdot c$ and the string
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   469
$abc$, but false if you give it the string $ab$. \hfill[0.5 Mark]
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   470
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   471
\item[(5)] Implement a function, called \textit{size}, by recursion
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   472
  over regular expressions. If a regular expression is seen as a tree,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   473
  then \textit{size} should return the number of nodes in such a
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   474
  tree. Therefore this function is defined as follows:
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   475
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   476
\begin{center}
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   477
\begin{tabular}{lcll}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   478
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   479
$\textit{size}(\ONE)$  & $\dn$ & $1$\\
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   480
$\textit{size}(c)$     & $\dn$ & $1$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   481
$\textit{size}(r_1 \,?\, r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$ & where $? \in \{\cdot, +, \&\}$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   482
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   483
$\textit{size}(r^{\{n\}})$ & $\dn$ & $1 + \textit{size}(r)$\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   484
\end{tabular}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   485
\end{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   486
224
1a45c9bcc77b updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   487
You can use \textit{size} in order to test how much the ``evil'' regular
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   488
expression $(a^*)^* \cdot b$ grows when taking successive derivatives
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   489
according the letter $a$ without simplification and then compare it to
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   490
taking the derivative, but simplify the result.  The sizes
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   491
are given in \texttt{re.scala}. \hfill[0.5 Marks]
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   492
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   493
\item[(6)] You do not have to implement anything specific under this
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   494
  task.  The purpose here is that you will be marked for some ``power''
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   495
  test cases. For example can your matcher decide within 30 seconds
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   496
  whether the regular expression $(a^*)^*\cdot b$ matches strings of the
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   497
  form $aaa\ldots{}aaaa$, for say 1 Million $a$'s. And does simplification
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   498
  simplify the regular expression
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   499
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   500
  \[
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   501
  \texttt{SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)}
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   502
  \]  
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   503
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   504
  \noindent correctly to just \texttt{ONE}, where \texttt{SEQ} is nested
245
744496a2bade added marking
Christian Urban <urbanc@in.tum.de>
parents: 224
diff changeset
   505
  50 or more times?\\
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   506
  \mbox{}\hfill[1 Mark]
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   507
\end{itemize}
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   508
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   509
\subsection*{Background}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   510
500
fd7f8aff848c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 498
diff changeset
   511
Although easily implementable in Scala (ok maybe the \texttt{simp} function and
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   512
the constructors \texttt{ALT}/\texttt{SEQ}/\texttt{AND} needs a bit more thinking), the idea behind the
396
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   513
derivative function might not so easy to be seen. To understand its
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   514
purpose better, assume a regular expression $r$ can match strings of
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   515
the form $c\!::\!cs$ (that means strings which start with a character
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   516
$c$ and have some rest, or tail, $cs$). If you take the derivative of
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   517
$r$ with respect to the character $c$, then you obtain a regular
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   518
expression that can match all the strings $cs$.  In other words, the
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   519
regular expression $\textit{der}\;c\;r$ can match the same strings
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   520
$c\!::\!cs$ that can be matched by $r$, except that the $c$ is chopped
ea39bbc8d98d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 390
diff changeset
   521
off.
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   522
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   523
Assume now $r$ can match the string $abc$. If you take the derivative
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   524
according to $a$ then you obtain a regular expression that can match
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   525
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   526
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   527
obtain a regular expression that can match the string $c$ (it is $bc$
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   528
where $b$ is chopped off). If you finally build the derivative of this
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   529
according $c$, that is
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   530
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   531
a regular expression that can match the empty string. You can test
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   532
whether this is indeed the case using the function nullable, which is
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   533
what the matcher you have implemented is doing.
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   534
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   535
The purpose of the $\textit{simp}$ function is to keep the regular
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   536
expressions small. Normally the derivative function makes the regular
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   537
expression bigger (see the \texttt{SEQs} case and the example in Task (2)) and the
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   538
algorithm would be slower and slower over time. The $\textit{simp}$
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   539
function counters this increase in size and the result is that the
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   540
algorithm is fast throughout.  By the way, this algorithm is by Janusz
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   541
Brzozowski who came up with the idea of derivatives in 1964 in his PhD
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   542
thesis.
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   543
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   544
\begin{center}\small
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   545
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   546
\end{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   547
105
0f9f774c7697 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   548
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   549
If you want to see how badly the regular expression matchers do in
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   550
Java\footnote{Version 8 and below; Version 9 and above does not seem
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   551
  to be as catastrophic, but still much worse than the regular
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   552
  expression matcher based on derivatives. BTW, Scala uses the regular
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   553
  expression matcher provided by the Java libraries. So is just as bad.}, JavaScript,
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   554
Python Swift and Dart with the ``evil'' regular expression
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   555
$(a^*)^*\cdot b$, then have a look at the graphs below (you can try it
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   556
out for yourself: have a look at the files
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   557
\texttt{catastrophic9.java}, \texttt{catastrophic.js},
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   558
\texttt{catastrophic.py} etc on KEATS). Compare this with the matcher
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   559
you have implemented. How long can a string of $a$'s be in your
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   560
matcher and still stay within the 30 seconds time limit? It should be
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   561
mu(uu)$^*$ch better than your off-the-shelf matcher in your
472
fbff6f601370 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   562
bog-standard programming language.
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   563
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   564
\begin{center}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   565
\begin{tabular}{@{}cc@{}}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   566
\multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings 
421
Christian Urban <christian.urban@kcl.ac.uk>
parents: 415
diff changeset
   567
           $\underbrace{a\ldots a}_{n}$}\medskip\\
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   568
  
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   569
\begin{tikzpicture}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   570
\begin{axis}[
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   571
    xlabel={$n$},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   572
    x label style={at={(1.05,0.0)}},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   573
    ylabel={time in secs},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   574
    y label style={at={(0.06,0.5)}},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   575
    enlargelimits=false,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   576
    xtick={0,5,...,30},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   577
    xmax=33,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   578
    ymax=45,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   579
    ytick={0,5,...,40},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   580
    scaled ticks=false,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   581
    axis lines=left,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   582
    width=6cm,
421
Christian Urban <christian.urban@kcl.ac.uk>
parents: 415
diff changeset
   583
    height=5.5cm, 
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   584
    legend entries={Python, Java 8, JavaScript, Swift, Dart},  
222
ec9cbf969edf updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   585
    legend pos=north west,
ec9cbf969edf updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   586
    legend cell align=left]
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   587
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   588
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
221
d061f3a94fa7 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   589
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
351
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   590
\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data};
97d01d2a93b1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 311
diff changeset
   591
\addplot[brown,mark=*, mark options={fill=white}] table {re-dart.data};
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   592
\end{axis}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   593
\end{tikzpicture}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   594
  & 
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   595
\begin{tikzpicture}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   596
\begin{axis}[
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   597
    xlabel={$n$},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   598
    x label style={at={(1.05,0.0)}},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   599
    ylabel={time in secs},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   600
    y label style={at={(0.06,0.5)}},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   601
    %enlargelimits=false,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   602
    %xtick={0,5000,...,30000},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   603
    xmax=65000,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   604
    ymax=45,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   605
    ytick={0,5,...,40},
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   606
    scaled ticks=false,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   607
    axis lines=left,
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   608
    width=6cm,
421
Christian Urban <christian.urban@kcl.ac.uk>
parents: 415
diff changeset
   609
    height=5.5cm, 
498
0f1b97538ad4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 480
diff changeset
   610
    legend entries={Java 9 and above},  
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   611
    legend pos=north west]
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   612
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data};
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   613
\end{axis}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   614
\end{tikzpicture}
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   615
\end{tabular}  
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   616
\end{center}
480
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   617
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   618
%\end{document}
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   619
\newpage
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   620
480
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   621
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   622
For the calculation below, I prefer to use the more ``mathematical''
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   623
notation for regular expressions. Therefore let us first look at this
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   624
notation and the corresponding Scala code.
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   625
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   626
\begin{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   627
\begin{tabular}{r@{\hspace{10mm}}l}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   628
  ``mathematical'' notation &  \\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   629
  for regular expressions & Scala code\smallskip\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   630
  $\ZERO$ &  \texttt{ZERO}\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   631
  $\ONE$  &  \texttt{ONE}\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   632
  $c$     &  \texttt{CHAR(c)}\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   633
  $\sum rs$ & \texttt{ALTs(rs)}\\  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   634
  $\prod rs$ & \texttt{SEQs(rs)}\\  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   635
  $r^*$ & \texttt{STAR(r)}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   636
\end{tabular}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   637
\end{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   638
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   639
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   640
My own convention is that \texttt{rs} stands for a list of regular
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   641
expressions.  Also of note is that these are \textbf{all} regular
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   642
expressions in Main 3 and the template file defines them as the
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   643
(algebraic) datatype \texttt{Rexp}. A confusion might arise from the
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   644
fact that there is also some shorthand notation for some regular
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   645
expressions, namely
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   646
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   647
\begin{lstlisting}[xleftmargin=10mm,numbers=none]
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   648
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   649
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   650
\end{lstlisting}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   651
  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   652
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   653
Since these are functions, everything of the form \texttt{ALT(r1, r2)}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   654
will immediately be translated into the regular expression
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   655
\texttt{ALTs(List(r1, r2))} (similarly for \texttt{SEQ}).  Maybe even
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   656
more confusing is that Scala allows one to define
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   657
\textit{extensions} that provide an even shorter notation for
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   658
\texttt{ALT} and \texttt{SEQ}, namely
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   659
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   660
\begin{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   661
\begin{tabular}{lclcl}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   662
  \texttt{r1} $\sim$ \texttt{r2} & $\dn$ & \texttt{SEQ(r1, r2)} & $\dn$ & \texttt{SEQs(List(r1, r2))}\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   663
  \texttt{r1} $|$ \texttt{r2} & $\dn$ & \texttt{ALT(r1, r2)}    & $\dn$ & \texttt{ALTs(List(r1, r2))}\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   664
\end{tabular}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   665
\end{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   666
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   667
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   668
The right hand sides are the fully expanded definitions.
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   669
The reason for this even shorter notation is that in the mathematical
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   670
notation one often writes 
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   671
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   672
\begin{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   673
\begin{tabular}{lcl}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   674
  $r_1 \;\cdot\; r_2$ & $\dn$ & $\prod\;[r_1, r_2]$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   675
  $r_1 + r_2$ & $\dn$ & $\sum\;[r_1, r_2]$
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   676
\end{tabular}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   677
\end{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   678
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   679
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   680
The first one is for binary \textit{sequence} regular expressions and
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   681
the second for binary \textit{alternative} regular expressions.
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   682
The regex in question in the shorthand notation is $(a + 1)\cdot a$,
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   683
which is the same as
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   684
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   685
\[
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   686
\prod\; [\Sigma\,[a, 1], a]
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   687
\]
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   688
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   689
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   690
or in Scala code
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   691
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   692
\[
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   693
\texttt{(CHAR('a') | ONE)} \;\sim\; \texttt{CHAR('a')}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   694
\]  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   695
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   696
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   697
Using the mathematical notation, the definition of $\textit{der}$ is
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   698
given by the rules:
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   699
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   700
\begin{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   701
\begin{tabular}{llcl}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   702
(1) & $\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   703
(2) & $\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   704
(3) & $\textit{der}\;c\;(d)$     & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   705
(4) & $\textit{der}\;c\;(\sum\;[r_1,..,r_n])$ & $\dn$ & $\sum\;[\textit{der}\;c\;r_1,..,\textit{der}\;c\;r_n]$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   706
(5) & $\textit{der}\;c\;(\prod\;[])$ & $\dn$ & $\ZERO$\\  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   707
(6) & $\textit{der}\;c\;(\prod\;r\!::\!rs)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r)$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   708
   &   & & $\textit{then}\;(\prod\;(\textit{der}\;c\;r)\!::\!rs) + (\textit{der}\;c\;(\prod rs))$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   709
   &   & & $\textit{else}\;(\prod\;(\textit{der}\;c\;r)\!::\! rs)$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   710
(7) & $\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   711
\end{tabular}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   712
\end{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   713
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   714
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   715
480
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   716
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   717
Let's finally do the calculation for the derivative of the regular
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   718
expression with respect to the letter $a$ (in red is in each line which
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   719
regular expression is ana-lysed):
218
ae788eeea8a0 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   720
480
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   721
\begin{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   722
\begin{tabular}{cll}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   723
      & $\textit{der}(a, \textcolor{red}{(a + 1) \cdot a})$ & by (6) and since $a + 1$ is nullable\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   724
$\dn$ & $(\textit{der}(a, \textcolor{red}{a + 1})\cdot a) + \textit{der}(a, \,\prod\,[a])$  & by (4)\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   725
$\dn$ & $((\textit{der}(a, \textcolor{red}{a}) + \texttt{der}(a, \ONE))\cdot a) + \textit{der}(a, \,\prod\,[a])$& by (3)\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   726
$\dn$ & $((\ONE + \texttt{der}(a, \textcolor{red}{1}))\cdot a) + \textit{der}(a, \,\prod\,[a])$ & by (2)\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   727
$\dn$ & $((\ONE + \ZERO)\cdot a) + \textit{der}(a, \textcolor{red}{\prod\,[a]})$ & by (6) and $a$ not being nullable\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   728
$\dn$ & $((\ONE + \ZERO)\cdot a) + \prod\,[\texttt{der}(a, \textcolor{red}{a})]$ & by (3)\\
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   729
$\dn$ & $((\ONE + \ZERO)\cdot a) + \prod\,[\ONE]$ \\  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   730
\end{tabular}  
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   731
\end{center}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   732
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   733
\noindent
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   734
Translating this result back into Scala code gives you
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   735
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   736
\[
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   737
\texttt{ALT(\,} \underbrace{\texttt{(ONE | ZERO)} \sim \texttt{CHAR('a')}}_{(\textbf{1} + \textbf{0})\,\cdot\, a}\;,\;\underbrace{\texttt{SEQs(List(ONE))}}_{\prod\,[\textbf{1}]}\texttt{)}
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   738
\]
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   739
a1151868a997 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 478
diff changeset
   740
 
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   744
425
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   745
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   746
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   747
w.r.t.~the characters $a$, $b$ and $c$ are
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   748
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   749
\begin{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   750
  \begin{tabular}{lcll}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   751
    $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & \quad($= r'$)\\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   752
    $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   753
    $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   754
  \end{tabular}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   755
\end{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   756
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   757
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   758
w.r.t.~the characters $a$, $b$ and $c$ gives
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   759
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   760
\begin{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   761
  \begin{tabular}{lcll}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   762
    $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   763
    $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & \quad($= r''$)\\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   764
    $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   765
  \end{tabular}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   766
\end{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   767
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   768
One more example: Let $r''$ stand for the second derivative above,
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   769
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   770
and $c$ gives
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   771
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   772
\begin{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   773
  \begin{tabular}{lcll}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   774
    $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   775
    $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   776
    $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ &
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   777
    (is $\textit{nullable}$)                      
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   778
  \end{tabular}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   779
\end{center}
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   780
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   781
Note, the last derivative can match the empty string, that is it is \textit{nullable}.
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   782
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   783
6e990ae2c6a3 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   784
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
%%% End: