[Haskell-cafe] mfix for a nested monad

Benjamin Redelings benjamin.redelings at gmail.com
Thu Oct 25 14:43:08 UTC 2018


Hi,

I'm trying to implement mfix for a monad that is nested inside another 
monad.  I came up with the following logic for how to implement this, 
but I suspect there are some things I'm missing. My conclusion is that I 
want

interpret (MFix f) = mfix (interpret.f)

Does this seem right?  Has this situation been discussed somewhere?

-BenRI


P.S. Here's what I mean by the monad being nested in another monad.  
Let's say that the monad M2 has interpreter i2, with type

i2 :: M2 a -> M1 a

and then M1 is the other monad, and has interpreter i1:

i1 :: M1a -> a

I suppose that the nesting is really a nesting of interpreters.



P.P.S. I came up with some equational reasoning for how to treat mfix in 
the i2 interpreter.

(a) For some interpreters `i`, it makes sense to require

     i (mfix f) = let x = (i . f) x in x

(b) I want the composition of interpreters (i1.i2) to act like (a) above:

     (i1 . i2) (mfix f) = let x = (i1 . i2 . f) x in x

(c) Rearranging, and then substituting using (a):

     i1 (i2 (mfix f)) = let x = i1 . (i2 . f) x in x
                            = i1 (mfix (i2 . f))

(d) Therefore, we could set

     i2 (mfix f) = mfix (i2 . f)

I'm probably going to make a `data` declaration for M2 so I could 
actually write

     i2 (Mfix f) = mfix (i2 . f)

-BenRI



More information about the Haskell-Cafe mailing list