wiki:RecursiveDo

Version 3 (modified by john@…, 8 years ago) (diff)

--

Recursive Do Notation

See ExtensionDescriptionHowto for information on how to write these extension descriptions. Please add any new extensions to the list of HaskellExtensions.

Brief Explanation

An extended form of do notation allowing feedback for monads in the MonadFix class.

Recursive do is invoked via the 'mdo' keyword rather than the normal 'do' one, a MonadFix? constraint is added to the Monad when 'mdo' is used. Some advocate replacing 'do' with 'mdo' always.

There are connections to the proposed syntactic support for Arrows using an extended form of do-noation. In particular, the Arrowsextension has its own syntax for recursive bindings/feedback. It would clearly be preferable if there was only one variant of recursive do. Even if the Arrows extensions are not adopted, it does offer a different explicit syntax for recursive bindings through the keyword rec that arguably is a bit more suggestive of its meaning than mdo. Conversely, if it is decided to go for implicit recursion, then it would seem reasonable to opt for the same in the case of Arrows, if possible. If not, then that might be another argument against implicit recursion.

References

Pros

  • makes programs much more readable that the equivalent forms using mfix.

Cons

  • a dependency analysis is required to determine the semantics.
  • a lot more difficult to understand the desugaring

Cons of reusing 'do'

  • not backward compatible with Haskell 98, unless a different keyword is used: using a variable before it is bound is treated as recursion.
  • shadowing variables can often be convinient
  • not every monad is a MonadFix?