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

106 lines
3.1 KiB
TeX
Raw Permalink Normal View History

2024-01-11 13:38:32 +01:00
\section{Partiality in Type Theory}
2024-01-12 17:36:59 +01:00
2024-01-03 19:51:04 +01:00
\begin{frame}[t, fragile]{Partiality in Haskell}{}
2024-01-22 17:57:12 +01:00
In Haskell we are able to define arbitrary partial functions
2024-01-11 13:38:32 +01:00
\begin{itemize}
2024-01-12 17:36:59 +01:00
\item<1-> Some can be spotted easily by their definition:
\vskip 0.5cm
\begin{minted}{haskell}
2024-01-14 18:12:29 +01:00
head :: [a] -> a
head (x:xs) = x
\end{minted}
2024-01-22 17:57:12 +01:00
\mycallout<2->{22.5, 1.5}{
2024-01-06 15:04:04 +01:00
ghci> head []\\
2024-01-22 17:57:12 +01:00
Exception: code-examples/example.hs:2:1-14: Non-exhaustive patterns in function head
2024-01-06 15:04:04 +01:00
}
2024-01-11 13:38:32 +01:00
\item<3->
2024-01-06 15:04:04 +01:00
others might be more subtle:
2024-01-12 17:36:59 +01:00
\vskip 0.5cm
\begin{minted}{haskell}
2024-01-14 18:12:29 +01:00
reverse :: [a] -> [a]
reverse l = revAcc l []
where
revAcc [] a = a
revAcc (x:xs) a = revAcc xs (x:a)
\end{minted}
2024-01-06 15:04:04 +01:00
2024-01-22 17:57:12 +01:00
\mycallout<4->{22.5, 2}{
2024-01-06 15:04:04 +01:00
ghci> ones = 1 : ones\\
ghci> reverse ones\\
...
}
\end{itemize}
2024-01-03 19:51:04 +01:00
\end{frame}
\begin{frame}[t, fragile]{Partiality in Agda}{The Maybe Monad}
2024-01-06 15:04:04 +01:00
\begin{itemize}[<+->]
2024-01-14 18:12:29 +01:00
\item In Agda every function has to be total and terminating, so how do we model partial functions?
2024-01-03 19:51:04 +01:00
2024-01-14 18:12:29 +01:00
\item Simple errors can be modelled with the maybe monad
\begin{multicols}{2}
\begin{minted}{agda}
data Maybe (A : Set) : Set where
just : A → Maybe A
nothing : Maybe A
\end{minted}
\columnbreak
\begin{minted}{agda}
head : ∀ A → List A → Maybe A
head nil = nothing
head (cons x xs) = just x
\end{minted}
\end{multicols}
2024-01-03 19:51:04 +01:00
2024-01-21 17:19:14 +01:00
\item What about \mintinline{agda}|reverse| for (possibly) infinite lists:
\begin{minted}{agda}
data Colist (A : Set) : Set where
[] : Colist A
__ : A → ∞ (Colist A) → Colist A
\end{minted}
2024-01-14 18:12:29 +01:00
2024-01-06 15:04:04 +01:00
\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-21 17:19:14 +01:00
\begin{itemize}[<+->]
2024-01-06 15:04:04 +01:00
\item Capretta's Delay Monad is a \textbf{coinductive} data type whose inhabitants can be viewed as suspended computations.
2024-01-14 18:12:29 +01:00
\vskip 0.5cm
2024-01-11 13:38:32 +01:00
\begin{minted}{agda}
2024-01-14 18:12:29 +01:00
data Delay (A : Set) : Set where
now : A → Delay A
later : ∞ (Delay A) → Delay A
\end{minted}
\vskip 0.5cm
2024-01-06 15:04:04 +01:00
\item The delay datatype contains a constant for non-termination:
2024-01-14 18:12:29 +01:00
\vskip 0.5cm
2024-01-11 13:38:32 +01:00
\begin{minted}{agda}
2024-01-14 18:12:29 +01:00
never : Delay A
never = later (♯ never)
\end{minted}
\vskip 0.5cm
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-14 18:12:29 +01:00
\vskip 0.5cm
2024-01-11 13:38:32 +01:00
\begin{minted}{agda}
2024-01-14 18:12:29 +01:00
run_for_steps : Delay A → → 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 ♭ x for n steps
\end{minted}
2024-01-06 15:04:04 +01:00
\end{itemize}
2024-01-03 19:51:04 +01:00
\end{frame}
2024-01-14 18:12:29 +01:00
\begin{frame}[t, fragile]{Partiality in Agda}{Capretta's Delay Monad}
\begin{itemize}
\item Now we can define a reverse function for possibly infinite lists:
\begin{minted}{agda}
reverse : ∀ {A : Set} → Colist A → Delay (Colist A)
2024-01-22 17:57:12 +01:00
reverse {A} l = revAcc l []
2024-01-14 18:12:29 +01:00
where
revAcc : Colist A → Colist A → Delay (Colist A)
revAcc [] a = now a
revAcc (x ∷ xs) a = later (♯ revAcc (♭ xs) (x ∷ (♯ a)))
\end{minted}
\end{itemize}
2023-12-14 14:54:23 +01:00
\end{frame}