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