For the Free Monad
:
data Free f a = Var a
| Node (f (Free f a))
I implemented instance Functor (Free f)
:
instance Functor f => Functor (Free f) where
fmap g (Var x) = Var (g x)
fmap g (Node x) = Node $ fmap (\y -> fmap g y) x
Then I tried to implement instance Applicative (Free f)
:
instance Functor f => Applicative (Free f) where
pure x = Var x
My intuition is that var x
is the right definition of pure
.
However, regardless of whether that's correct, I'm not sure how to implement <*>
.
In particular, is it necessary to support the following cases? Note that I ignored the make-up of the Var
and Node
with _
.
(Var _) <*> (Var _)
(Var _) <*> (Node _)
(Node _) <*> (Var _)
(Node _) <*> (Node _)
Please give me a hint as to whether the above cases need to be matched.
Also, please provide me with an intuition as to what it means for both Free f a
instances to exist on either side of <*>
.