I want to write a slick bit of code (saving me much time to implement otherwise) by tying the knot. It goes roughly like this,
n <- myinstr n x
where in theory, myinstr
should run x
to get a value, which will become n
. myinstr
, which runs inside a State
monad, will put n
into the state, but this doesn't affect x
's computation.
I've tried using DoRec
and a naiive implementation of mfix
,
instance Monad => MonadFix (MyMonad ) where
mfix f = fix (\mx -> mx >>= f)
but things freeze. Are there any methods for fixing my code (or methodologies for designing it correctly the first time) or should I write something more straightforward?
There is no generic way to make an arbitrary monad an instance of
MonadFix
. The actual code depends on the monad, and it's not even possible for all monads. You can look at the various monads to see how it's done. And if your monad is in factState
there should already be an instance.