[Haskell-cafe] How about a special syntax for generalised (= MonadPlus) comprehension?

Mario Blažević mblazevic at stilo.com
Thu May 19 13:35:17 UTC 2016


On 2016-05-15 11:20 AM, Alexey Muranov wrote:
> If i understand correctly, the main reason that generalised comprehension was
> removed from Haskell 98 must have been that its syntax "collides" with the
> syntax for lists.

	Sorry about the late response. I don't know about Haskell 98, but there 
already is a GHC language extension for what you're looking for:

https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/syntax-extns.html#monad-comprehensions


> Indeed, if
>
> ```
>    [x^2 | x <- msum (map return [0..10]), x `mod` 2 == 1]
> ```
>
> is made ad-hoc polymorphic, then so probably should `[]` and `[1,2,3]`.
 >
> Moreover, for consistency, the meaning of `[]` as the list type constructor
> (like in `[]Int` or `[Int]`), should probably be changed to denote an
> arbitrary instance of `MonadPlus` :).

	Yes, that is probably the reason the extension as it stands won't be 
made a part of the standard language. On the other hand, having to 
request it explicitly still seems like a lesser evil than adding new 
syntax to support it.


> My question is: why not to have a special syntax for `MonadPlus`
> comprehension?  For example:
>
> ```
>    {| x + y | x <- Just 1, y <- Just 2 |}
> ```
>
> or
>
> ```
>    [* x + y | x <- Just 1, y <- Just 2 *]
> ```
>
> Then matching analogs of `[]` and `[1,2,3]` could be made:
>
> ```
>    {||} :: Maybe Int  -- Nothing
>    {| Just 1, Just 2, Just 3 |} :: Maybe Int  -- Just 1
> ```
>
> Alexey.


More information about the Haskell-Cafe mailing list