\documentclass{article}
\usepackage{../style}
\usepackage{../langs}
\usepackage{marvosym}
\usepackage{tcolorbox}
%% \usepackage[]{hvextern}

%cheat sheet
%http://worldline.github.io/scala-cheatsheet/

\begin{document}
\fnote{\copyright{} Christian Urban, King's College London, 2025}

\section*{Ammonite ``Quirks''}

While Ammonite is great when it comes to additional features, such as
multiple main's and dynamically loading files, and in the past it was
a rock-solid piece of software written by one of my Scala-heroes
(Haoyi Li), it seems it has acquired some ``quirks'' when it
transitioned to Scala 3. Interestingly, the problems listed below only occur
inside the REPL. So running Scala-scripts on the command
line with for example

\begin{lstlisting}[language={},numbers=none]
$ amm cw02.sc
\end{lstlisting} %% $

\noindent
works fine. Similarly, the original Scala REPL seems to not suffer 
from these quirks. Unfortunately, this REPL does not allow you to
dynamically load files which might be relevant in later CWs.

\subsection*{Problem with Implicits}

In CW2 (and possibly others), if you use the template code and develop
your code inside the REPL, a problem arises in the line

\begin{lstlisting}[numbers=none,language=Scala]
val KEYWORD : Rexp = "while" | "if" | "then" 
\end{lstlisting}

\noindent with the error

\begin{lstlisting}[numbers=none,language=Scala]
-- [E008] Not Found Error: cmd17.sc:1:36 -----------------------------
1 |val KEYWORD : Rexp = "while" | "if" | "then" 
  |                     ^^^^^^^^^^^^^^^^
  |value | is not a member of ammonite.$sess.cmd4.ALT.
  |An extension method was tried, but could not be fully constructed:
  |
  |    ammonite.$sess.cmd16.|()
  |
  |    failed with:
  |
  |        value |: <overloaded ammonite.$sess.cmd16.|> does not take 
  |        parameters
  |Compilation Failed
\end{lstlisting}%$\medskip


\noindent\textbf{Fix:} The code runs absolutely fine when called on the
commandline. It also runs absolutely fine inside the standard Scala REPL.
If you prefer to use to use the Ammonite REPL, then you need to make the 
following changes: (1) comment out the implicits below and change the 
\code{$} to for example \code{&}%$:

\begin{lstlisting}[numbers=none,language=Scala]
extension (s: String) {
  def & (r: Rexp) = RECD(s, r)
  //def | (r: Rexp) = ALT(s, r)
  //def | (r: String) = ALT(s, r)
  //def % = STAR(s)
  //def ~ (r: Rexp) = SEQ(s, r)
  //def ~ (r: String) = SEQ(s, r)
}
\end{lstlisting}

\noindent You need to then use \code{&} in the code for the 
\code{WHILE_REGS}.

\subsection*{Problem with Loading Dynamically Files}

There seems to be cache problems with loading dynamically files in the REPL.
For example in the file \code{thompson.sc}, the code for DFAs, NFAs etc is 
loaded using:

\begin{lstlisting}[numbers=none,language=Scala]
import $file.dfa, dfa._ 
import $file.nfa, nfa._
import $file.enfa, enfa._
\end{lstlisting}%$

\noindent The menace is that all works perfectly in the first REPL session,
but if you close it and then re-open it for example the next day, you 
receive the following error when you try to re-evaluate the same lines:

\begin{lstlisting}[numbers=none,language=Scala]
-- [E008] Not Found Error: ~/cfl-material/progs/automata/nfa.sc:50:2 
50 |  dfa
   |  ^^^
   |  value dfa is not a member of ammonite.$sess - did you mean $sess.nfa?
-- [E006] Not Found Error: ~/cfl-material/progs/automata/nfa.sc:6:18 
6 |import $file.$  , dfa._ 
  |                  ^^^
  |                  Not found: dfa
  |
  | longer explanation available when compiling with `-explain`
Compilation Failed
\end{lstlisting}%$\medskip

\noindent\textbf{Fix:} The code again runs absolutely fine when called on the
commandline. But the REPL trips over. In this case, the trick with using the 
Scala REPL does not work, because it does not support dynamically loading
files. However, one ugly fix is to cut-and-paste all the code into a 
single file and then develop this one gigantic file. Or a slightly
less ugly solution is toi clear out the cache of ammonite. The cache
on my system is located in 

\begin{lstlisting}[language={},numbers=none]
~/.ammonite/cache/3.0.3/
\end{lstlisting}

\noindent
I usually delete all files inside this directory and in this way force 
ammonite to be in a consistent state.

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
