The reason this cannot work is because in mfix f , any effect in f is executed exactly once. This follows from the tightening rule.
mfix (\x -> a >>= \y -> fxy) = a >>= \y -> mfix (\x -> fxy)
in particular
mfix (\x -> a >> fx) = a >> mfix f
for any valid instance of MonadFix . Thus, a fixed point is calculated only for a pure (lazily calculated) value inside a monadic action, and not for effects. In your case, using mfix need to print / read characters once so that the input signal is equal to the output, which is impossible. This is not suitable for mfix . You would use mfix with IO , for example, to build a circular data structure in IO , for example, in these examples .
In your case, you should use iterateM_ or something similar, not mfix . see also iteration + forever = iterateM? Repeating the action with feedback .
source share