Proposal: Warn about ArrowLoop instance for Kleisli Arrows
Heinrich Apfelmus
apfelmus at quantentunnel.de
Sun Oct 9 09:44:40 CEST 2011
Hello,
I recently noticed that the instance
MonadFix m => ArrowLoop (Kleisli m)
does not fulfill the ArrowLoop laws for most monads, rendering it either
useless or dangerous for the unwary. Removing it would be the correct
thing to do, but I think that the sensible thing to do is to merely
document said fact at the instance declaration in the haddocks. This
way, the few monads that do support an ArrowLoop instance (most notably
the lazy state monad) can keep their instance while everyone else is warned.
Thus, I suggest to add documentation to the instances, along the lines
of "Even though every monad that supports 'mfix' can be made an instance
of 'ArrowLoop', it usually /does not/ fulfill all of the corresponding
ArrowLoop laws. Use this instance at your own risk."
The main problem is the right-tightening law
loop (first h <<< f) = h <<< loop f
which fails for most monads. Consider the following example:
bad :: String -> IO ()
bad = runKleisli $ loop (first (Kleisli putStrLn) <<< arr swap)
where swap (a,b) = (b,a)
Using the ArrowLoop laws [1], this example should be equal to
... = runKleisli $ Kleisli putStrLn <<< loop (arr swap)
= putStrLn
However, trying the example in GHCi will throw "Exception: <<loop>>",
which clearly shows that this equality is not true. The underlying
reason is that MonadFix doesn't support a corresponding right-tightening
law, just as mentioned for example in [2].
[1]: http://www.soi.city.ac.uk/~ross/papers/fop.html
[2]: http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.6.5172
Discussion period: 2 weeks (ends 24 October 2011)
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Libraries
mailing list