diff -r cf3ca219c727 -r c8ce95067c1a handouts/scala-ho.tex --- a/handouts/scala-ho.tex Mon Feb 22 22:09:31 2016 +0000 +++ b/handouts/scala-ho.tex Tue Mar 22 17:09:24 2016 +0000 @@ -54,9 +54,8 @@ \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] $ scala -Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM). -Type in expressions to have them evaluated. -Type :help for more information. +Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM). +Type in expressions for evaluation. Or try :help. scala> \end{lstlisting} @@ -133,12 +132,12 @@ \begin{center} \begin{tabular}{r@{\hspace{2mm}}r@{\hspace{2mm}}l@{\hspace{13mm}}l} - $r$ & $::=$ & $\varnothing$ & null\\ - & $\mid$ & $\epsilon$ & empty string\\ - & $\mid$ & $c$ & single character\\ - & $\mid$ & $r_1 \cdot r_2$ & sequence\\ - & $\mid$ & $r_1 + r_2$ & alternative / choice\\ - & $\mid$ & $r^*$ & star (zero or more)\\ + $r$ & $::=$ & $\ZERO$ & null\\ + & $\mid$ & $\ONE$ & empty string\\ + & $\mid$ & $c$ & single character\\ + & $\mid$ & $r_1 \cdot r_2$ & sequence\\ + & $\mid$ & $r_1 + r_2$ & alternative / choice\\ + & $\mid$ & $r^\star$ & star (zero or more)\\ \end{tabular} \end{center} @@ -155,19 +154,19 @@ actually very simple: It first requires an \emph{abstract class}, say, \code{Rexp}. This will act as the type for regular expressions. Second, it requires a case for each -clause in the grammar. The cases for $\varnothing$ and -$\epsilon$ do not have any arguments, while in all the other -cases we do have arguments. For example the character regular -expression needs to take as an argument the character it is -supposed to recognise. In Scala, the cases without arguments -are called \emph{case objects}, whereas the ones with -arguments are \emph{case classes}. The corresponding Scala -code is as follows: +clause in the grammar. The cases for $\ZERO$ and $\ONE$ do not +have any arguments, while in all the other cases we do have +arguments. For example the character regular expression needs +to take as an argument the character it is supposed to +recognise. In Scala, the cases without arguments are called +\emph{case objects}, whereas the ones with arguments are +\emph{case classes}. The corresponding Scala code is as +follows: \begin{lstlisting}[numbers=none] abstract class Rexp -case object NULL extends Rexp -case object EMPTY extends Rexp +case object ZERO extends Rexp +case object ONE extends Rexp case class CHAR (c: Char) extends Rexp case class SEQ (r1: Rexp, r2: Rexp) extends Rexp case class ALT (r1: Rexp, r2: Rexp) extends Rexp @@ -202,20 +201,19 @@ omit such ``tricks'' here. Note that Scala in its response says the variable \code{r} is -of type \lstinline[emph={ALT}]!ALT!, not \code{Rexp}. This -might be a bit unexpected, but can be explained as follows: -Scala always tries to find the most general type that is -needed for a variable or expression, but does not -``over-generalise''. In our definition the type \code{Rexp} is -more general than \lstinline[emph={ALT}]!ALT!, since it is the -abstract class. But in this case there is no need to give -\code{r} the more general type of \code{Rexp}. This is -different if you want to form a list of regular expressions, -for example +of type \code{ALT}, not \code{Rexp}. This might be a bit +unexpected, but can be explained as follows: Scala always +tries to find the most general type that is needed for a +variable or expression, but does not ``over-generalise''. In +our definition the type \code{Rexp} is more general than +\code{Rexp}, since it is the abstract class. But in this case +there is no need to give \code{r} the more general type of +\code{Rexp}. This is different if you want +to form a list of regular expressions, for example \begin{lstlisting}[numbers=none] -scala> val ls = List(ALT(CHAR('a'), CHAR('b')), NULL) -ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), NULL) +scala> val ls = List(ALT(CHAR('a'), CHAR('b')), ZERO) +ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), ZERO) \end{lstlisting} \noindent In this case, Scala needs to assign a common type to @@ -301,8 +299,8 @@ \begin{center} \begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} -$rev(\varnothing)$ & $\dn$ & $\varnothing$\\ -$rev(\epsilon)$ & $\dn$ & $\epsilon$\\ +$rev(\ZERO)$ & $\dn$ & $\ZERO$\\ +$rev(\ONE)$ & $\dn$ & $\ONE$\\ $rev(c)$ & $\dn$ & $c$\\ $rev(r_1 + r_2)$ & $\dn$ & $rev(r_1) + rev(r_2)$\\ $rev(r_1 \cdot r_2)$ & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\