Monads in various languages

While working on implementing monads and comprehensions, I ran into a complication in translating comprehensions to their underlying monadic expressions. Looking at other languages for insight, I realized that all languages which make use of monads use type deduction and that this feature appears to be critical to making comprehensions work.

To translate a comprehension for a monad M, you need to translate expressions like [x+1 | x <- xm] where you know that xm:M(int) for a statically unknown monad M. But how do you typecheck "x+1", which requires that x is a numeric type? Nothing in this expression explicitly indicates that x is numeric. Thus one would need type deduction and explicit universal quantification with matching to deduce that x is numeric, or ugly explicit type specification on comprehensions like [x+1 | x::int <- xm].

My conjecture is that this is why monads happen to be widely-used only in languages supporting Hindley-Milner type deduction, while traditional languages use imperative constructs like iterators, exceptions and side-effects for roughly similar tasks. Monads would be awfully inconvenient without this syntactic sugar, which happens to rely on a rather narrowly-available language feature.

I would appreciate any counterarguments on this conjecture. Also any references to non Hindley-Milner languages supporting monads conveniently.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

What language are you using?

This question seems to be fairly language-specific, or at least type-system-specific. All functional languages I know of have either type inference or dynamic typing, and I don't know of any languages with syntactic extension (flexible enough to write [x+1 | x::int

I think that the reason monads aren't used more is a) they are hard to understand, b) for most languages, builtin syntax would be required to make it reasonable pretty, and c) they are functional, not fitting with the cannonical style of the language. I'm not defending any of these; I prefer languages that make it easier for monads to be implemented (such as Kogut, which has a parser monad in its standard library despite having no builtin syntax for monads).


List comprehensions are mostly just syntactic sugar for various higher-order list operations, so they aren't needed where those higher-order operations are easy to use. In most languages, list comprehensions simply wouldn't fit in the syntax without radical changes to the syntax. Anyway, if type declarations are required for everything, I don't see why it's particularly annoying in the context of list comprehensions to the point where it makes them unusable.

This problem appears to exist

This problem appears to exist generally when translating comprehensions into any strongly-typed language without type deduction. Python generator expressions avoid this by being weakly typed.

Requiring explicit type annotations would be unfortunate. For example, using comprehension syntax for monadic parser combinators is very convenient, but would be about 50% more verbose if type annotations were required.

Are there any languages in which monad operations are convenient and readable without comprehensions? For example, complex Haskell monad operations seem considerably less readable without the helper syntax.