How about a special syntax for generalised (= MonadPlus) comprehension?

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. 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` :). 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.

On Sunday, May 15, 2016 at 5:20:42 PM UTC+2, Alexey Muranov wrote:
Then matching analogs of `[]` and `[1,2,3]` could be made:
``` {||} :: Maybe Int -- Nothing {| Just 1, Just 2, Just 3 |} :: Maybe Int -- Just 1 ```
Sorry, i meant of course ``` {|1, 2, 3|} :: Maybe Int -- Just 1 ``` Alexey.

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...
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.

Ok. My motivation for suggesting a special syntax for MonadPlus comprehension was that, generally speaking, it feels weird that in the standard Haskell there is a special syntax for a unique data structure. Even Python has it for lists, dictionaries, and sets. I understand well the motivation for leaving list comprehension on its own, but having simultaneously a similar syntax for a more general purpose would make it less weird IMO. Alexey.
participants (2)
-
Alexey Muranov
-
Mario Blažević