
On April 26, 2011 13:50:10 Maciej Marcin Piechotka wrote:
On Tue, 2011-04-19 at 23:48 -0400, Tyson Whitehead wrote:
On April 19, 2011 23:22:12 Tyson Whitehead wrote:
ArrowLoop from MonadFix
loop' f = fst' .' loop'' (f .' arr' (second snd))
where loop'' f = mfix (\y -> f .' arr' (,y))
BTW haskellers, I've been wondering if mfix would better be defined as
mfix' :: (m a -> m a) -> m a
where "mfix' f = mfix (f . pure)" for the computational monads. The advantage being you can give a useful definition for structural monads as well.
What would be the difference with fix?
For the IO monad, for example, normal fix would give you an IO value which would repeat the underlying IO action. The mfix' above give you the result of the first run of the IO action lazily bound in an IO context. Consider Prelude> fix $ fmap (1:) ... never returns (overflows the stack and dies) ... Prelude> mfix' $ fmap (1:) ... returns "return $ repeat 1" ... The difference between mfix and mfix' being you can't (or at least I couldn't) implement it for non-singleton monads such as list. The problem was mfix had to do a map like operation across the underlying structure to invoke the function on the lazily bound singletons before any of the structure existed. Cheers! -Tyson