This commit is contained in:
Leon Vatthauer 2024-01-12 17:36:59 +01:00
parent 09fc7f8fa9
commit a90a5df672
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
2 changed files with 36 additions and 22 deletions

View file

@ -1,6 +1,5 @@
{-# OPTIONS --guardedness #-} {-# OPTIONS --guardedness #-}
open import Codata.Musical.Colist hiding (_++_) -- open import Codata.Musical.Colist hiding (_++_)
open import Codata.Musical.Colist.Bisimilarity
open import Codata.Musical.Notation open import Codata.Musical.Notation
open import Data.Nat open import Data.Nat
open import Data.Nat.Show renaming (show to show) open import Data.Nat.Show renaming (show to show)
@ -13,6 +12,18 @@ module reverse where
now : A Delay A now : A Delay A
later : (Delay A) Delay A later : (Delay A) Delay A
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 = later ( (run x for n steps))
never : {A : Set} Delay A
never = later ( never)
data Colist (A : Set) : Set where
[] : Colist A
_∷_ : A (Colist A) Colist A
reverse : {A : Set} Colist A Delay (Colist A) reverse : {A : Set} Colist A Delay (Colist A)
reverse {A} = reverseAcc [] reverse {A} = reverseAcc []
where where
@ -20,12 +31,6 @@ module reverse where
reverseAcc [] a = now a reverseAcc [] a = now a
reverseAcc (x xs) a = later ( reverseAcc ( xs) (x ( a))) reverseAcc (x xs) a = later ( reverseAcc ( xs) (x ( a)))
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 : Colist
fin-colist = 1 (2 (3 [])) fin-colist = 1 (2 (3 []))

View file

@ -1,14 +1,20 @@
\section{Partiality in Type Theory} \section{Partiality in Type Theory}
% TODO
% - remove vskips and use begin{listing}
% - add colist definition
\begin{frame}[t, fragile]{Partiality in Haskell}{} \begin{frame}[t, fragile]{Partiality in Haskell}{}
Haskell allows users to define arbitrary partial functions
\begin{itemize} \begin{itemize}
\item<1-> Haskell allows users to define arbitrary partial functions, some can be spotted easily by their definition: \item<1-> Some can be spotted easily by their definition:
\vskip 1cm \vskip 0.5cm
\begin{minted}{agda} \begin{minted}{haskell}
head :: [a] -> a head :: [a] -> a
head [] = error "empty list" head [] = error "empty list"
head (x:xs) = x head (x:xs) = x
\end{minted} \end{minted}
\mycallout<2->{21, 1.5}{ \mycallout<2->{22, 1.5}{
ghci> head []\\ ghci> head []\\
*** Exception: empty list\\ *** Exception: empty list\\
CallStack (from HasCallStack):\\ CallStack (from HasCallStack):\\
@ -16,16 +22,16 @@ head (x:xs) = x
} }
\item<3-> \item<3->
others might be more subtle: others might be more subtle:
\vskip 1cm \vskip 0.5cm
\begin{minted}{agda} \begin{minted}{haskell}
reverse :: [a] -> [a] reverse :: [a] -> [a]
reverse l = reverseAcc l [] reverse l = revAcc l []
where where
reverseAcc [] a = a revAcc [] a = a
reverseAcc (x:xs) a = reverseAcc xs (x:a) revAcc (x:xs) a = revAcc xs (x:a)
\end{minted} \end{minted}
\mycallout<4->{21, 2}{ \mycallout<4->{22, 2}{
ghci> ones = 1 : ones\\ ghci> ones = 1 : ones\\
ghci> reverse ones\\ ghci> reverse ones\\
... ...
@ -39,13 +45,16 @@ In Agda every function has to be total and terminating, so how do we model parti
\begin{itemize}[<+->] \begin{itemize}[<+->]
\item Simple errors can be modelled with the maybe monad \item Simple errors can be modelled with the maybe monad
\begin{listing}[H]
\begin{minted}{agda} \begin{minted}{agda}
data Maybe (A : Set) : Set where data Maybe (A : Set) : Set where
just : A → Maybe A just : A → Maybe A
nothing : Maybe A nothing : Maybe A
\end{minted} \end{minted}
\end{listing}
for head we can then do: for head we can then do:
\vskip 0.5cm
\begin{minted}{agda} \begin{minted}{agda}
head : ∀ A → List A → Maybe A head : ∀ A → List A → Maybe A
@ -85,10 +94,10 @@ run later x for suc n steps = run ♭ x for n steps
\centering \centering
\begin{minted}{agda} \begin{minted}{agda}
reverse : ∀ {A : Set} → Colist A → Delay (Colist A) reverse : ∀ {A : Set} → Colist A → Delay (Colist A)
reverse {A} = reverseAcc [] reverse {A} = revAcc []
where where
reverseAcc : Colist A → Colist A → Delay (Colist A) revAcc : Colist A → Colist A → Delay (Colist A)
reverseAcc [] a = now a revAcc [] a = now a
reverseAcc (x ∷ xs) a = later (♯ reverseAcc (♭ xs) (x ∷ (♯ a))) revAcc (x ∷ xs) a = later (♯ revAcc (♭ xs) (x ∷ (♯ a)))
\end{minted} \end{minted}
\end{frame} \end{frame}