mirror of
https://git8.cs.fau.de/theses/bsc-leon-vatthauer.git
synced 2024-05-31 07:28:34 +02:00
work on slides
This commit is contained in:
parent
fd7b498554
commit
2d2d26795b
5 changed files with 131 additions and 2 deletions
6
slides/code-examples/example.hs
Normal file
6
slides/code-examples/example.hs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
hd :: [a] -> a
|
||||||
|
hd [] = error "empty list"
|
||||||
|
hd (x : _) = x
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = print (hd []::[String])
|
3
slides/code-examples/examples.agda-lib
Normal file
3
slides/code-examples/examples.agda-lib
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
name: examples
|
||||||
|
include: .
|
||||||
|
depend: standard-library
|
40
slides/code-examples/reverse.agda
Normal file
40
slides/code-examples/reverse.agda
Normal file
|
@ -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
|
|
@ -137,6 +137,9 @@ Leon Vatthauer%\inst{1}
|
||||||
% ================================================
|
% ================================================
|
||||||
% The main document
|
% The main document
|
||||||
% ------------------------------------------------
|
% ------------------------------------------------
|
||||||
|
\usepackage{MnSymbol} % for \squaredots
|
||||||
|
\usepackage{listings}
|
||||||
|
\lstset{mathescape}
|
||||||
\begin{document}
|
\begin{document}
|
||||||
% Title page
|
% Title page
|
||||||
\begin{frame}[t,titleimage]{-}
|
\begin{frame}[t,titleimage]{-}
|
||||||
|
|
|
@ -1,3 +1,80 @@
|
||||||
\begin{frame}[t]{Introduction}{}
|
\begin{frame}[t, fragile]{Partiality in Haskell}{}
|
||||||
An introduction
|
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}
|
\end{frame}
|
Loading…
Reference in a new issue