bsc-leon-vatthauer/slides/sections/00_intro.tex

96 lines
3.2 KiB
TeX
Raw Normal View History

2024-01-03 19:51:04 +01:00
\begin{frame}[t, fragile]{Partiality in Haskell}{}
2024-01-06 15:04:04 +01:00
\begin{itemize}[<+->]
\item Haskell allows users to define arbitrary partial functions, some can be spotted easily by their definition:
\vskip 1cm
\begin{lstlisting}[language=myhaskell, linewidth=12cm]
2024-01-03 19:51:04 +01:00
head :: [a] -> a
head [] = error "empty list"
head (x:xs) = x
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\mycallout<3->{21, 1.5}{
ghci> head []\\
*** Exception: empty list\\
CallStack (from HasCallStack):\\
error, called at example.hs:2:9 in main:Main
}
\item
others might be more subtle:
\vskip 1cm
\begin{lstlisting}[language=myhaskell, linewidth=12cm]
2024-01-03 19:51:04 +01:00
reverse l = rev l []
where
rev [] a = a
rev (x:xs) a = rev xs (x:a)
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\mycallout<4->{21, 2}{
ghci> ones = 1 : ones\\
ghci> reverse ones\\
...
}
\end{itemize}
2024-01-03 19:51:04 +01:00
% TODO right of this add error bubble that shows `reverse ones`
\end{frame}
\begin{frame}[t, fragile]{Partiality in Agda}{The Maybe Monad}
In Agda every function has to be total and terminating, so how do we model partial functions?
2024-01-06 15:04:04 +01:00
\begin{itemize}[<+->]
\item Simple errors can be modelled with the maybe monad
\begin{lstlisting}[linewidth=14cm, language=myagda]
2024-01-03 19:51:04 +01:00
data Maybe (A : Set) : Set where
just : A $\rightarrow$ Maybe A
nothing : Maybe A
\end{lstlisting}
2024-01-06 15:04:04 +01:00
for head we can then do:
2024-01-03 19:51:04 +01:00
2024-01-06 15:04:04 +01:00
\begin{lstlisting}[linewidth=14cm, language=myagda]
2024-01-03 19:51:04 +01:00
head : $\forall$ A $\rightarrow$ List A $\rightarrow$ Maybe A
head nil = nothing
head (cons x xs) = just x
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\item What about \lstinline|reverse|?
\end{itemize}
2024-01-03 19:51:04 +01:00
\end{frame}
\begin{frame}[t, fragile]{Partiality in Agda}{Capretta's Delay Monad}
2024-01-06 15:04:04 +01:00
\begin{itemize}[<+->]
\item Capretta's Delay Monad is a \textbf{coinductive} data type whose inhabitants can be viewed as suspended computations.
\begin{lstlisting}[linewidth=20cm, language=myagda]
2024-01-03 19:51:04 +01:00
data Delay (A : Set) : Set where
now : A $\rightarrow$ Delay A
later : $\infty$ (Delay A) $\rightarrow$ Delay A
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\item The delay datatype contains a constant for non-termination:
\begin{lstlisting}[linewidth=20cm, language=myagda]
2024-01-03 19:51:04 +01:00
never : Delay A
never = later ($\sharp$ never)
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\item and we can define a function for \textit{running} a computation (for some amount of steps):
2024-01-03 19:51:04 +01:00
2024-01-06 15:04:04 +01:00
\begin{lstlisting}[linewidth=20cm, language=myagda]
2024-01-03 19:51:04 +01:00
run_for_steps : Delay A $\rightarrow$ $\mathbb{N}$ $\rightarrow$ Delay A
run now x for n steps = now x
run later x for zero steps = later x
run later x for suc n steps = run $\flat$ x for n steps
\end{lstlisting}
2024-01-06 15:04:04 +01:00
\end{itemize}
2024-01-03 19:51:04 +01:00
\end{frame}
2024-01-06 15:04:04 +01:00
\begin{frame}[c, fragile]{Partiality in Agda}{Reversing (possibly infinite) lists}
\centering
\begin{lstlisting}[language=myagda]
2024-01-03 19:51:04 +01:00
foldl : $\forall$ {A B : Set} $\rightarrow$ (A $\rightarrow$ B $\rightarrow$ A) $\rightarrow$ A $\rightarrow$ Colist B $\rightarrow$ Delay A
foldl c n [] = now n
foldl c n (x $\squaredots$ xs) = later ($\sharp$ foldl c (c n x) ($\flat$ xs))
reverse : $\forall$ {A : Set} $\rightarrow$ Colist A $\rightarrow$ Delay (Colist A)
reverse {A} = reverseAcc []
where
reverseAcc : Colist A $\rightarrow$ Colist A $\rightarrow$ Delay (Colist A)
2024-01-06 15:04:04 +01:00
reverseAcc = foldl ($\lambda$ xs x $\rightarrow$ x $\squaredots$ ($\sharp$ xs))
2024-01-03 19:51:04 +01:00
\end{lstlisting}
2023-12-14 14:54:23 +01:00
\end{frame}