-
-
Save tonymorris/3f534e5ef92990c8c3618411aae07dea to your computer and use it in GitHub Desktop.
This file contains 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
data Free f a = Unit a | Bind (f (Free f a)) | |
instance Functor f => Functor (Free f) where | |
fmap f (Unit a) = Unit (f a) | |
fmap f (Bind x) = Bind (fmap (fmap f) x) | |
instance Functor f => Applicative (Free f) where | |
pure = Unit | |
Unit f <*> Unit a = Unit (f a) | |
Unit f <*> Bind x = Bind (fmap (fmap f) x) | |
Bind x <*> y = Bind (fmap (\f -> f <*> y) x) | |
instance Functor f => Monad (Free f) where | |
return = pure | |
Unit a >>= f = f a | |
Bind x >>= f = Bind (fmap (\a -> a >>= f) x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment