diff --git a/slides/code-examples/example.hs b/slides/code-examples/example.hs new file mode 100644 index 0000000..642dde5 --- /dev/null +++ b/slides/code-examples/example.hs @@ -0,0 +1,6 @@ +hd :: [a] -> a +hd [] = error "empty list" +hd (x : _) = x + +main :: IO () +main = print (hd []::[String]) diff --git a/slides/code-examples/examples.agda-lib b/slides/code-examples/examples.agda-lib new file mode 100644 index 0000000..f1720ed --- /dev/null +++ b/slides/code-examples/examples.agda-lib @@ -0,0 +1,3 @@ +name: examples +include: . +depend: standard-library \ No newline at end of file diff --git a/slides/code-examples/reverse.agda b/slides/code-examples/reverse.agda new file mode 100644 index 0000000..4a308c3 --- /dev/null +++ b/slides/code-examples/reverse.agda @@ -0,0 +1,40 @@ +{-# OPTIONS --guardedness #-} +open import Codata.Musical.Colist hiding (_++_) +open import Codata.Musical.Colist.Bisimilarity +open import Codata.Musical.Notation +open import Data.Nat +open import Data.Nat.Show renaming (show to showℕ) +open import Function.Base +open import Relation.Binary.PropositionalEquality +open import Data.String using (String; _++_) + +module reverse where + data Delay (A : Set) : Set where + now : A → Delay A + later : ∞ (Delay A) → Delay A + + foldl : ∀ {A B : Set} → (A → B → A) → A → Colist B → Delay A + foldl c n [] = now n + foldl c n (x ∷ xs) = later (♯ foldl c (c n x) (♭ xs)) + + -- reversing possibly infinite lists + reverse : ∀ {A : Set} → Colist A → Delay (Colist A) + reverse {A} = reverseAcc [] + where + reverseAcc : Colist A → Colist A → Delay (Colist A) + reverseAcc = foldl (λ xs x → x ∷ (♯ xs)) -- 'flip _∷_' with extra steps + + run_for_steps : ∀ {A : Set} → 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 + + + fin-colist : Colist ℕ + fin-colist = 1 ∷ ♯ (2 ∷ ♯ (3 ∷ ♯ [])) + + inf-colist : Colist ℕ + inf-colist = 1 ∷ ♯ inf-colist + + -- run reverse fin-colist for 5 steps + -- run reverse inf-colist for 1000 steps \ No newline at end of file diff --git a/slides/main.tex b/slides/main.tex index 286bbb5..7778687 100644 --- a/slides/main.tex +++ b/slides/main.tex @@ -137,6 +137,9 @@ Leon Vatthauer%\inst{1} % ================================================ % The main document % ------------------------------------------------ +\usepackage{MnSymbol} % for \squaredots +\usepackage{listings} +\lstset{mathescape} \begin{document} % Title page \begin{frame}[t,titleimage]{-} diff --git a/slides/sections/00_intro.tex b/slides/sections/00_intro.tex index 7d1535a..3d63504 100644 --- a/slides/sections/00_intro.tex +++ b/slides/sections/00_intro.tex @@ -1,3 +1,80 @@ -\begin{frame}[t]{Introduction}{} -An introduction +\begin{frame}[t, fragile]{Partiality in Haskell}{} +Haskell allows users to define arbitrary partial functions, some can be spotted easily by their definition: +\begin{lstlisting}[language=haskell] +head :: [a] -> a +head [] = error "empty list" +head (x:xs) = x +\end{lstlisting} + +% TODO right of this add error bubble that shows what happens for `head []` + +others might be more subtle: + +\begin{lstlisting}[language=haskell] +reverse l = rev l [] +where + rev [] a = a + rev (x:xs) a = rev xs (x:a) +\end{lstlisting} +% 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? + +\begin{lstlisting} +data Maybe (A : Set) : Set where + just : A $\rightarrow$ Maybe A + nothing : Maybe A +\end{lstlisting} + +for head we can then do: + +\begin{lstlisting} +head : $\forall$ A $\rightarrow$ List A $\rightarrow$ Maybe A +head nil = nothing +head (cons x xs) = just x +\end{lstlisting} + +But what about \lstinline|reverse|? +\end{frame} + +\begin{frame}[t, fragile]{Partiality in Agda}{Capretta's Delay Monad} +Capretta's Delay Monad is a coinductive data type whose inhabitants can be viewed as suspended computations. +\begin{lstlisting} +data Delay (A : Set) : Set where + now : A $\rightarrow$ Delay A + later : $\infty$ (Delay A) $\rightarrow$ Delay A +\end{lstlisting} +\lstinline|now| lifts a computation, while \lstinline|later| delays it by one time unit. + +The delay datatype contains a constant for non-termination: +\begin{lstlisting} +never : Delay A +never = later ($\sharp$ never) +\end{lstlisting} + +and we can define a function for \textit{running} a computation (for some amount of steps): + +\begin{lstlisting} +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} +\end{frame} + +\begin{frame}[t, fragile]{Partiality in Agda}{Reversing (possibly infinite) lists} +\begin{lstlisting} +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) + reverseAcc = foldl ($\lambda$ xs x $\rightarrow$ x $\squaredots$ ($\sharp$ xs)) -- 'flip _$\squaredots$_' with extra steps +\end{lstlisting} \end{frame} \ No newline at end of file