\documentclass{beamer}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{clean}
\usepackage{spl}

\title[cc1516]{SPLC}
\subtitle{\texttt{<splc>~::= <spl> <parser> `,' <lexer> `and' <compiler>}}
\author[P. Jager, M. Lubbers]{Pim Jager\inst{1}\and Mart Lubbers\inst{1}}
\institute[Radboud University]{%
	\inst{1}%
	Computer Science: Software Science\\
	Radboud University
}
\subject{SPL Compiler}
\date{\today}

\lstset{%
	basicstyle=\ttfamily\footnotesize,
	breaklines
}

\usetheme{Warsaw}
\usecolortheme{beaver}

\begin{document}
\frame{\titlepage}

\begin{frame}[fragile]
	\frametitle{Today}
	\begin{itemize}
		\item Higher order functions
		\item Anonymous lambda functions
		\item Syntactic sugar
		\item Future work 
		\item Demo
	\end{itemize}
\end{frame}

\begin{frame}[fragile]
	\frametitle{Higher order functions implemented using function pointers}

	\begin{SPLCode}
map(f, xs) :: (a -> b) -> [a] -> [b] {
    if( isEmpty(xs) ) { return []; }
    else { return f(xs.hd) : map(f, xs.tl); }
}
id(x) { return x; }

main() {
    map(id, 1:2:3:[]);
}
	\end{SPLCode}
	\pause
	\begin{itemize}
		\item Function arguments are passed by name.
		\item During compilation these are replaced by unique function ID's
		\item During runtime the corresponding function label is retrieved using
			a dictionary like approach. 
	\end{itemize}

\end{frame}

\begin{frame}[fragile]
	\frametitle{Function application can be curried}

	\begin{SPLCode}
plus(x,y) :: Int -> Int -> Int {
    return x + y;
}

main() {
    map(plus(4), 1:2:3:[]);
}
	\end{SPLCode}
	\pause
	\begin{columns}[T]
		\begin{column}[T]{0.5\textwidth}
			\begin{enumerate}
				\item Function ID is placed on the heap
				\item Number of passed arguments is placed on the heap
				\item Arguments are pushed on the heap
			\end{enumerate}
		\end{column}
		\pause
		\begin{column}[T]{0.5\textwidth}
			\begin{block}{Heap contents}
			\begin{tabular}{c | l | r}
				address & Content & \\  \hline
				n 	& 8 & (1)\\  
				n+1 & 1 & (2)\\  
				n+2 & 4 & (3)
			\end{tabular}
			\end{block}
		\end{column}
	\end{columns}

\end{frame}


\begin{frame}[fragile]
	\frametitle{SPLC has lambda functions through AST rewriting}
	\framesubtitle{\texttt{<LamdaExpr> ::= `\textbackslash'<id>* 
					`->' <Expr>}}
	\begin{block}{Implementation}<3->
		Implemented by promoting lambdas to regular named functions 
			prior to semantical analysis.
	\end{block}
	\begin{columns}
		\begin{column}[T]{0.5\textwidth}<1->
			\begin{SPLCode}
main() {
    map(\x-> x+1, 1:2:3:[]);
}
			\end{SPLCode}
		\end{column}
		\begin{column}[T]{0.5\textwidth}<4->
			\begin{SPLCode}
1lambda_23(x) {
    return x+1;
}
main() {
    map(1lambda_23, 1:2:3:[]);
}
			\end{SPLCode}
		\end{column}
	\end{columns}
	\begin{itemize}
		\item<2-> SPLC supports anonymous lambda functions with arbitrary arity
		\item<2-> Lambdas are fully type checked, same as regular functions 
	\end{itemize}
\end{frame}

\begin{frame}[fragile]
	\frametitle{Syntactic sugar to ease our programmers lives}
	\begin{block}{Global constants}
		\begin{itemize}
			\item SPLC does \emph{not} feature global variables. 
			\begin{itemize}
				\item We want to protect our users from global state (as much 
					as possible).
			\end{itemize}
			\item Global constants allowed through \texttt{let} statements
			\item \texttt{<LetDecl> ::= `let' <type> <id> `=' <Expr> `;'}
			\item Lets are rewritten to constant functions
		\end{itemize}
		\begin{columns}[T]
			\begin{column}[T]{0.4\linewidth}
				\begin{SPLCode}
let Int x = 5;
				\end{SPLCode}
			\end{column}
			\begin{column}[T]{0.4\linewidth}
				\begin{SPLCode}
x() :: Int { return 5; }
				\end{SPLCode}
			\end{column}
		\end{columns}
	\end{block}
\end{frame}

\begin{frame}[fragile]
	\frametitle{Syntactic sugar to ease our programmers lives}
	\framesubtitle{continued}
	\begin{block}{String Literals}
		\begin{columns}[T]
			\begin{column}[T]{0.34\linewidth}
				\begin{SPLCode}
var a = "Hello world!";
				\end{SPLCode}
			\end{column}
			\begin{column}[T]{0.5\linewidth}
				\begin{SPLCode}
var a = 'H':'e':'l':'l':'o':' ':
    'w':'o':'r':'l':'d':'!':[];
				\end{SPLCode}
			\end{column}
		\end{columns}
	\end{block}
	\begin{block}{List Literals}
		\begin{columns}[T]
			\begin{column}[T]{0.34\linewidth}
				\begin{SPLCode}
var xs = [1,2,3];
				\end{SPLCode}
			\end{column}
			\begin{column}[T]{0.5\linewidth}
				\begin{SPLCode}
var xs = 1:2:3:[];
				\end{SPLCode}
			\end{column}
		\end{columns}
	\end{block}
	\begin{block}{Variable argument printing}
		\begin{columns}[T]
			\begin{column}[T]{0.34\linewidth}
				\begin{SPLCode}
print("x is: ", x, "\n");
				\end{SPLCode}
			\end{column}
			\begin{column}[T]{0.5\linewidth}
				\begin{SPLCode}
print("x is: ");
print(x);
print("\n");
				\end{SPLCode}
			\end{column}
		\end{columns}
	\end{block}
\end{frame}

\begin{frame}[fragile]
	\frametitle{Improvements and Future Work}
	\begin{block}{Improve type checker}
		Type checker allows some programs which should not be allowed\\  
		e.g. the polymorphic value examples of Markus.
	\end{block}
	\pause
	\begin{block}{Improve overloaded equality}
		Equality only preforms shallow (JAVA-style) equality on Lists and Tuple
		types.
	\end{block}
	\pause
	\begin{block}{Improve error reporting}
		Not all errors are reported with the position the error occurs in. Most
		notably type errors always occur on position (0,0).
	\end{block}
\end{frame}

\begin{frame}
	\frametitle{Questions \& Cool demo's}

\end{frame}


\end{document}
