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