Given
newtype Tree m a = Tree { runTree :: m (Node m a) }
data Node m a = Node
{ nodeValue :: a
, nodeChildren :: [Tree m a]
}
Is there a valid MonadFix
instance?
My attempt was
instance MonadFix m => MonadFix (Tree m) where
mfix f = Tree $ do
Node
<$> mfix (runTree . f . nodeValue)
<*> fmap nodeChildren (runTree (mfix f))
Yet this doesn't seem to terminate when I actually try and use it. The instance is somewhat inspired by the MonadFix
instance for lists.
The real solution really comes from gallais with a small modification. We lifted the core idea out into the
containers
library too, withMonadFix Tree
instance here