Created
November 13, 2020 22:22
-
-
Save miladhub/ffd4c34742ba7f5ca1d92ca61f47730b to your computer and use it in GitHub Desktop.
Free monad notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
milad.bourhani:~/projects/free$ cat Free.hs | |
module Free where | |
data Free f r = Free (f (Free f r)) | Pure r | |
instance (Functor f) => Functor (Free f) where | |
fmap f (Pure a) = Pure (f a) | |
fmap f (Free x) = Free $ fmap (fmap f) x | |
instance (Functor f) => Applicative (Free f) where | |
pure = Pure | |
Pure a <*> Pure b = Pure (a b) | |
Pure a <*> Free fb = Free $ fmap (fmap a) fb | |
Free fa <*> b = Free $ fmap (<*> b) fa | |
instance (Functor f) => Monad (Free f) where | |
return = pure | |
(Free x) >>= f = Free $ fmap (>>= f) x | |
(Pure r) >>= f = f r | |
data Toy b next = | |
Output b next | |
| Bell next | |
| Done | |
instance Functor (Toy b) where | |
fmap f (Output x next) = Output x (f next) | |
fmap f (Bell next) = Bell (f next) | |
fmap f Done = Done | |
output :: a -> Free (Toy a) () | |
output x = Free (Output x (Pure ())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment