handouts/amm-ho.tex
changeset 994 35db8ee453c0
parent 965 94f5cce73a4f
--- a/handouts/amm-ho.tex	Sun Sep 28 14:06:57 2025 +0100
+++ b/handouts/amm-ho.tex	Sun Sep 28 18:56:01 2025 +0100
@@ -9,7 +9,7 @@
 %http://worldline.github.io/scala-cheatsheet/
 
 \begin{document}
-\fnote{\copyright{} Christian Urban, King's College London, 2020, 2021, 2023}
+\fnote{\copyright{} Christian Urban, King's College London, 2020, 2021, 2023, 2025}
 
 \section*{Scala 3 in 6CCS3CFL}
 
@@ -29,7 +29,7 @@
 \noindent
 If you need a reminder of the Scala handouts from PEP updated to Scala 3
 have a look here
-\hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}. But as said, you do not need to use Scala for the CWs.\footnote{Haskell, Rust, Ocaml were other languages that have
+\hr{https://cflmark.nms.kcl.ac.uk/hg/pep-material/raw-file/tip/handouts/pep-ho.pdf}. But as said, you do not need to use Scala for the CWs.\footnote{Haskell, Rust, Ocaml were other languages that have
   been used previously in CFL. I do not recommend to use Java or C or C++ for
   writing a compiler, but if you insist, feel free. It has been done
   before.}  
@@ -50,7 +50,7 @@
 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 $ amm
 Loading...
-Welcome to the Ammonite Repl 3.0.0-M2 (Scala 3.3.3 Java 21.0.4)
+Welcome to the Ammonite Repl 3.0.2 (Scala 3.3.5 Java 21.0.8)
 @ 1 + 2
 res0: Int = 3
 \end{lstlisting} %% $
@@ -67,7 +67,7 @@
 
 \begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small]
 $ curl -L https://github.com/com-lihaoyi/Ammonite/releases/\
-  download/3.0.0-M2/3.3-3.0.0-M2 --output amm
+  download/3.0.2/3.5-3.0.2 --output amm
 \end{lstlisting} %% $  
 
 \noindent
@@ -77,7 +77,7 @@
 
 \begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small]
 $ curl -L https://github.com/com-lihaoyi/Ammonite/releases/\
-  download/3.0.0-M2/3.3-3.0.0-M2 --output amm.bat
+   download/3.0.2/3.5-3.0.2 --output amm.bat
 \end{lstlisting} %% $  
 
 \noindent
@@ -99,7 +99,11 @@
 \noindent
 The second line writes the string \code{"foo bar"} into the file
 \code{"file.name"}, which is located in the current working
-directory (\code{pwd}). For loading and accessing code from
+directory (\code{pwd}). We want to implement a compiler---therefore 
+reading and writing files will come in handy.
+
+
+For loading and accessing code from
 another Scala file, you can import the code into Ammonite
 as follows:
 
@@ -111,8 +115,8 @@
 \noindent
 This assumes the other Scala file is called
 \texttt{name-of-the-file.sc} and requires the file to be in the same
-directory where \texttt{amm} is working in. This will be very convenient 
-for the compiler we implement in CFL, because it allows us to easily
+directory where \texttt{amm} is working in. Again this will be very convenient 
+for our compiler we implement in CFL, because it allows us to easily
 break up the code into the lexer, parser and code generator.
 
 Another handy feature of Ammonite is that you can mark functions as
@@ -161,8 +165,7 @@
 
 \noindent Of course this requires that you use \texttt{println} for
 inspecting any data as otherwise nothing will be displayed at the
-commandline.
-\smallskip
+commandline.\medskip
 
 \noindent
 To sum up, Ammonite is a really useful addition to the Scala ecosystem.
@@ -174,6 +177,89 @@
 \url{https://www.handsonscala.com/part-i-introduction-to-scala.html}
 \end{center}
 
+
+\subsection*{Some Updates in Scala 3 and the Videos}
+
+While Scala 2 and Scala 3 code is on the whole quite compatible, there are some
+corners where my Scala 3 code differs from the code shown in the videos. I am still
+fond of using \texttt{\{...\}} rather than Pythonesque indentation syntax. But
+I switched to the \texttt{enum}-syntax for abstract datatypes. Defining regular
+expressions in the ``old'' way can be done using abstract classes, like:
+
+\begin{lstlisting}[language=Scala,basicstyle=\ttfamily\small]
+abstract class Rexp
+case object ZERO extends Rexp
+case object ONE extends Rexp
+case class CHAR(c: Char) extends Rexp
+case class ALT(r1: Rexp, r2: Rexp) extends Rexp
+case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
+case class STAR(r: Rexp) extends Rexp
+\end{lstlisting} 
+
+\noindent
+While this code still works in Scala 3 as expected, the definition can now 
+be simplified to:
+
+\begin{lstlisting}[language=Scala,basicstyle=\ttfamily\small]
+enum Rexp {
+  case ZERO                     
+  case ONE                      
+  case CHAR(c: Char)            
+  case ALT(r1: Rexp, r2: Rexp)  
+  case SEQ(r1: Rexp, r2: Rexp)  
+  case STAR(r: Rexp)            
+}
+import Rexp._
+\end{lstlisting} 
+
+\noindent
+Note that the syntax with \texttt{enum} needs an import, otherwise you need
+to refer to the constructors slightly clumsily as in \texttt{Rexp.CHAR('a')} and 
+so on.
+
+Also implicits are now defined differently in Scala 3 and need to be
+split up into \texttt{given}s and extension methods. If you want to
+construct regular expressions using strings, for example \texttt{STAR("a")},
+then you need to declare a \texttt{given}-clause:
+
+\begin{lstlisting}[language=Scala,basicstyle=\ttfamily\small]
+def charlist2rexp(s : List[Char]): Rexp = s match {
+  case Nil => ONE
+  case c::Nil => CHAR(c)
+  case c::s => SEQ(CHAR(c), charlist2rexp(s))
+}
+
+given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
+\end{lstlisting} 
+
+\noindent
+This uses the auxiliary function \texttt{charlist2rexp} which translates
+a string (list of chars) into a regular expression. The ``magic'' is then 
+installed in the \texttt{given}-clause which calls \texttt{charlist2rexp} 
+whenever a \texttt{Rexp} is expected, but a string is given.
+
+More convenient operator syntax for regular instructions needs to be
+defined in Scala 3 as \emph{extension method}. For example, the shorthand-syntax
+for alternatives, sequences and stars needs to be defined as:
+
+\begin{lstlisting}[language=Scala,basicstyle=\ttfamily\small]
+extension (r: Rexp) {
+  def | (s: Rexp) = ALT(r, s)
+  def ~ (s: Rexp) = SEQ(r, s)
+  def % = STAR(r)
+}
+\end{lstlisting} 
+
+\noindent
+After that you can define regular expressions more conveniently
+as \pcode{"ab" | "bc"}, \pcode{"ab" ~ "bc"} or \pcode{"ab".\%}\;.\medskip
+
+\noindent
+To sum up, it should be easy for you to translate the old syntax that
+is in some places used in the videos and the newer syntax used in the
+uploaded files. There are a few additional rough edges in the code
+for parsers, but I will mention them nearer the time. 
+
 \end{document}
 
 %%% Local Variables: