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 #-}
open import Codata.Musical.Colist hiding (_++_)
open import Codata.Musical.Colist.Bisimilarity
-- open import Codata.Musical.Colist hiding (_++_)
open import Codata.Musical.Notation
open import Data.Nat
open import Data.Nat.Show renaming (show to show)
@ -13,6 +12,18 @@ module reverse where
now : 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} = reverseAcc []
where
@ -20,12 +31,6 @@ module reverse where
reverseAcc [] a = now 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 = 1 (2 (3 []))

View file

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