Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created June 20, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymorris/3f534e5ef92990c8c3618411aae07dea to your computer and use it in GitHub Desktop.
Save tonymorris/3f534e5ef92990c8c3618411aae07dea to your computer and use it in GitHub Desktop.
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