cws/cw03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 25 Jan 2017 14:51:09 +0000
changeset 106 fae9db0d9793
parent 100 93260be6770f
child 110 62389faa66e4
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     2
\usepackage{../style}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
     3
\usepackage{../langs}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
100
93260be6770f updated
Christian Urban <urbanc@in.tum.de>
parents: 94
diff changeset
     7
\section*{Coursework 8 (Scala, Regular Expressions)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
     9
This coursework is worth 10\%. It is about regular expressions,
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    10
pattern matching and polymorphism. The first part is due on 30
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    11
November at 11pm; the second, more advanced part, is due on 7 December
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    12
at 11pm. You are asked to implement a regular expression matcher. Make
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    13
sure the files you submit can be processed by just calling
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    14
\texttt{scala <<filename.scala>>}.\bigskip
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    15
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    16
\noindent
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    17
\textbf{Important:} Do not use any mutable data structures in your
74
fb2d8012ed08 updated
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    18
submission! They are not needed. This excludes the use of
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    19
\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    20
code! It has a different meaning in Scala, than in Java.  Do not use
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    21
\texttt{var}! This declares a mutable variable.  Make sure the
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    22
functions you submit are defined on the ``top-level'' of Scala, not
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    23
inside a class or object. Also note that the running time of
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    24
each part will be restricted to a maximum of 360 seconds. 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    25
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    28
\subsection*{Disclaimer!!!!!!!!}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
It should be understood that the work you submit represents
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    31
your own effort! You have not copied from anyone else. An
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
exception is the Scala code I showed during the lectures or
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
uploaded to KEATS, which you can freely use.\bigskip
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    36
\subsection*{Part 1 (6 Marks)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    38
The task is to implement a regular expression matcher that is based on
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    39
derivatives of regular expressions. The implementation can deal
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    40
with the following regular expressions, which have been predefined
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    41
in the file re.scala:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
\begin{tabular}{lcll}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  $r$ & $::=$ & $\ZERO$     & cannot match anything\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
      &   $|$ & $\ONE$      & can only match the empty string\\
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    47
      &   $|$ & $c$         & can match a character (in this case $c$)\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    48
      &   $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    49
  &   $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    50
          &  & & then the second part with $r_2$\\
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      &   $|$ & $r^*$       & can match zero or more times $r$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
\end{tabular}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    55
\noindent 
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    56
Why? Knowing how to match regular expressions and strings fast will
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    57
let you solve a lot of problems that vex other humans. Regular
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    58
expressions are one of the fastest and simplest ways to match patterns
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    59
in text, and are endlessly useful for searching, editing and
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    60
analysing text in all sorts of places. However, you need to be
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    61
fast, otherwise you will stumble over problems such as recently reported at
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    62
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    63
{\small
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    64
\begin{itemize}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    65
\item[$\bullet$] \url{http://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    66
\item[$\bullet$] \url{https://vimeo.com/112065252}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    67
\item[$\bullet$] \url{http://davidvgalbraith.com/how-i-fixed-atom/}  
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    68
\end{itemize}}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    69
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    70
\subsubsection*{Tasks (file re.scala)}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    71
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    72
\begin{itemize}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    73
\item[(1a)] Implement a function, called \textit{nullable}, by recursion over
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    74
  regular expressions. This function tests whether a regular expression can match
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    75
  the empty string.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
$\textit{nullable}(\ONE)$  & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
$\textit{nullable}(c)$     & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
\end{tabular}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    86
\end{center}\hfill[1 Mark]
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    87
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    88
\item[(1b)] Implement a function, called \textit{der}, by recursion over
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    89
  regular expressions. It takes a character and a regular expression
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    90
  as arguments and calculates the derivative regular expression according
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    91
  to the rules:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
$\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
$\textit{der}\;c\;(d)$     & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
      & & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
      & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
\end{tabular}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   104
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   105
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   106
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   107
w.r.t.~the characters $a$, $b$ and $c$ are
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   108
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   109
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   110
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   111
    $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & ($= r'$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   112
    $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   113
    $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   114
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   115
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   116
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   117
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   118
w.r.t.~the characters $a$, $b$ and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   119
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   120
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   121
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   122
    $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   123
    $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & ($= r''$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   124
    $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   125
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   126
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   127
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   128
One more example: Let $r''$ stand for the second derivative above,
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   129
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   130
and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   131
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   132
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   133
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   134
    $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   135
    $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   136
    $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   137
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   138
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   139
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   140
Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   141
\mbox{}\hfill\mbox{[1 Mark]}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   143
\item[(1c)] Implement the function \textit{simp}, which recursively
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   144
  traverses a regular expression from the inside to the outside, and
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   145
  simplifies every sub-regular-expression on the left (see below) to
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   146
  the regular expression on the right, except it does not simplify inside
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   147
  ${}^*$-regular expressions.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   149
  \begin{center}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   150
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
$r \cdot \ONE$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
$\ONE \cdot r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
$r + \ZERO$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
$\ZERO + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
$r + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
\end{tabular}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   159
  \end{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   160
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   161
  For example the regular expression
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   162
  \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\]
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   163
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   164
  simplifies to just $r_1$. \textbf{Hints:} Regular expressions can be
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   165
  seen as trees and there are several methods for traversing
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   166
  trees. One of them corresponds to the inside-out traversal. Also
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   167
  remember numerical expressions from school: there you had exprssions
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   168
  like $u + \ldots + (1 \cdot x) * \ldots (z + (y \cdot 0)) \ldots$
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   169
  and simplification rules that looked very similar to rules
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   170
  above. You would simplify such numerical expressions by replacing
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   171
  for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   172
  look if more rules are applicable. If you organise this
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   173
  simplification in an inside-out fashion, it is always clear which
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   174
  rule should applied next.\\\mbox{}\hfill[1 Mark]
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   175
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   176
\item[(1d)] Implement two functions: The first, called \textit{ders},
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   177
  takes a list of characters and a regular expression as arguments, and
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   178
  builds the derivative w.r.t.~the list as follows:
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   179
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   180
\begin{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   181
\begin{tabular}{lcl}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   182
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   183
  $\textit{ders}\;(c::cs)\;r$  & $\dn$ &
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   184
    $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   185
\end{tabular}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   188
Note that this function is different from \textit{der}, which only
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   189
takes a single character.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   190
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   191
The second function, called \textit{matcher}, takes a string and a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   192
regular expression as arguments. It builds first the derivatives
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   193
according to \textit{ders} and after that tests whether the resulting
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   194
derivative regular expression can match the empty string (using
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   195
\textit{nullable}).  For example the \textit{matcher} will produce
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   196
true given the regular expression $(a\cdot b)\cdot c$ and the string
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   197
$abc$.\\  \mbox{}\hfill[1 Mark]
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   199
\item[(1e)] Implement the function $\textit{replace}\;r\;s_1\;s_2$: it searches
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   200
  (from the left to 
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   201
right) in the string $s_1$ all the non-empty substrings that match the 
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   202
regular expression $r$---these substrings are assumed to be
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   203
the longest substrings matched by the regular expression and
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   204
assumed to be non-overlapping. All these substrings in $s_1$ matched by $r$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   205
are replaced by $s_2$. For example given the regular expression
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   207
\[(a \cdot a)^* + (b \cdot b)\]
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   209
\noindent the string $s_1 = aabbbaaaaaaabaaaaabbaaaabb$ and
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   210
replacement the string $s_2 = c$ yields the string
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   212
\[
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   213
ccbcabcaccc
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   214
\]
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   216
\hfill[2 Marks]
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   217
\end{itemize}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   219
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   220
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   221
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   222
\subsection*{Part 2 (4 Marks)}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   223
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   224
You need to copy all the code from \texttt{re.scala} into
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   225
\texttt{re2.scala} in order to complete Part 2.  Parts (2a) and (2b)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   226
give you another method and datapoints for testing the \textit{der}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   227
and \textit{simp} functions from Part~1.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   228
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   229
\subsubsection*{Tasks (file re2.scala)}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   230
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   231
\begin{itemize}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   232
\item[(2a)] Write a \textbf{polymorphic} function, called
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   233
  \textit{iterT}, that is \textbf{tail-recursive}(!) and takes an
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   234
  integer $n$, a function $f$ and an $x$ as arguments. This function
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   235
  should iterate $f$ $n$-times starting with the argument $x$, like
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   236
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   237
  \[\underbrace{f(\ldots (f}_{n\text{-times}}(x)))
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   238
  \]
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   239
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   240
  More formally that means \textit{iterT} behaves as follows:
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   241
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   242
  \begin{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   243
    \begin{tabular}{lcl}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   244
      $\textit{iterT}(n, f, x)$ & $\dn$ &
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   245
      $\begin{cases}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   246
        \;x & \textit{if}\;n = 0\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   247
        \;f(\textit{iterT}(n - 1, f, x)) & \textit{otherwise}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   248
        \end{cases}$      
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   249
    \end{tabular}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   250
\end{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   251
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   252
  Make sure you write a \textbf{tail-recursive} version of
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   253
  \textit{iterT}.  If you add the annotation \texttt{@tailrec} (see
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   254
  below) your code should not produce an error message.
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   255
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   256
  \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]  
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   257
  import scala.annotation.tailrec
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   258
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   259
  @tailrec
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   260
  def iterT[A](n: Int, f: A => A, x: A): A = ...
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   261
  \end{lstlisting}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   262
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   263
  You can assume that \textit{iterT} will only be called for positive
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   264
  integers $0 \le n$. Given the type variable \texttt{A}, the type of
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   265
  $f$ is \texttt{A => A} and the type of $x$ is \texttt{A}. This means
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   266
  \textit{iterT} can be used, for example, for functions from integers
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   267
  to integers, or strings to strings, or regular expressions to
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   268
  regular expressions.  \\ \mbox{}\hfill[2 Marks]
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   269
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   270
\item[(2b)] Implement a function, called \textit{size}, by recursion
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   271
  over regular expressions. If a regular expression is seen as a tree,
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   272
  then \textit{size} should return the number of nodes in such a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   273
  tree. Therefore this function is defined as follows:
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   274
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   275
\begin{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   276
\begin{tabular}{lcl}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   277
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   278
$\textit{size}(\ONE)$  & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   279
$\textit{size}(c)$     & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   280
$\textit{size}(r_1 + r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   281
$\textit{size}(r_1 \cdot r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   282
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   283
\end{tabular}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   284
\end{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   285
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   286
You can use \textit{size} and \textit{iterT} in order to test how much
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   287
the 'evil' regular expression $(a^*)^* \cdot b$ grows when taking
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   288
successive derivatives according the letter $a$ and then compare it to
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   289
taking the derivative, but simlifying the derivative after each step.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   290
For example, the calls
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   291
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   292
  \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]  
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   293
  size(iterT(20, (r: Rexp) => der('a', r), EVIL))
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   294
  size(iterT(20, (r: Rexp) => simp(der('a', r)), EVIL))
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   295
  \end{lstlisting}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   296
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   297
  produce without simplification a regular expression of size of
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   298
  7340068 after 20 iterations, while the one with
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   299
  simplification gives
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   300
  just 8.\\ \mbox{}\hfill[1 Mark]
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   301
  
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   302
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   303
\item[(2c)] Write a \textbf{polymorphic} function, called
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   304
  \textit{fixpT}, that takes
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   305
  a function $f$ and an $x$ as arguments. The purpose
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   306
  of \textit{fixpT} is to calculate a fixpoint of the function $f$
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   307
  starting from the argument $x$.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   308
  A fixpoint, say $y$, is when $f(y) = y$ holds. 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   309
  That means \textit{fixpT} behaves as follows:
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   310
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   311
  \begin{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   312
    \begin{tabular}{lcl}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   313
      $\textit{fixpT}(f, x)$ & $\dn$ &
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   314
      $\begin{cases}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   315
        \;x & \textit{if}\;f(x) = x\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   316
        \;\textit{fixpT}(f, f(x)) & \textit{otherwise}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   317
        \end{cases}$      
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   318
    \end{tabular}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   319
\end{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   320
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   321
  Make sure you calculate in the code of $\textit{fixpT}$ the result
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   322
  of $f(x)$ only once. Given the type variable \texttt{A} in
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   323
  $\textit{fixpT}$, the type of $f$ is \texttt{A => A} and the type of
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   324
  $x$ is \texttt{A}. The file \texttt{re2.scala} gives two example
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   325
  function where in one the fixpoint is 1 and in the other
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   326
  it is the string $a$.\\ \mbox{}\hfill[1 Mark]  
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   327
\end{itemize}\bigskip  
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   328
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   329
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   330
\subsection*{Background}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   331
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   332
Although easily implementable in Scala, the idea behind the derivative
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   333
function might not so easy to be seen. To understand its purpose
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   334
better, assume a regular expression $r$ can match strings of the form
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   335
$c::cs$ (that means strings which start with a character $c$ and have
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   336
some rest, or tail, $cs$). If you now take the derivative of $r$ with
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   337
respect to the character $c$, then you obtain a regular expressions
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   338
that can match all the strings $cs$.  In other words, the regular
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   339
expression $\textit{der}\;c\;r$ can match the same strings $c::cs$
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   340
that can be matched by $r$, except that the $c$ is chopped off.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   341
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   342
Assume now $r$ can match the string $abc$. If you take the derivative
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   343
according to $a$ then you obtain a regular expression that can match
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   344
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   345
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r))$ you
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   346
obtain a regular expression that can match the string $c$ (it is $bc$
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   347
where $b$ is chopped off). If you finally build the derivative of this
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   348
according $c$, that is
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   349
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r)))$, you
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   350
obtain a regular expression that can match the empty string. You can
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   351
test this using the function nullable, which is what your matcher is
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   352
doing.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   353
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   354
The purpose of the simp function is to keep the regular expression
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   355
small. Normally the derivative function makes the regular expression
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   356
bigger (see the SEQ case and the example in (2b)) and the algorithm
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   357
would be slower and slower over time. The simp function counters this
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   358
increase in size and the result is that the algorithm is fast
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   359
throughout.  By the way, this algorithm is by Janusz Brzozowski who
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   360
came up with the idea of derivatives in 1964 in his PhD thesis.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   361
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   362
\begin{center}\small
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   363
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   364
\end{center}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   368
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
%%% End: