Apparently the only possible interpretation of runSomeMonad do ...
is runSomeMonad (do ...)
. Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar
could be actually ambiguous?
Asked
Active
Viewed 2,030 times
32

Petr
- 62,528
- 13
- 153
- 317
-
7Not just do, `foo if bar then quux else baz` as well. – daniel gratzer Feb 20 '14 at 19:27
-
2It probably has something to do with the fact that you can have something like `runSomeMonad (do ...) x y z ...`. – David Young Feb 20 '14 at 20:44
-
2@DavidYoung I don't see how the proposed grammar change would prevent that kind of thing. – Daniel Wagner Feb 20 '14 at 22:51
-
2Just a historical accident, as far as I know. – augustss Feb 20 '14 at 23:38
-
1As of GHC 8.6.1, you can enable the [`BlockArguments` extension](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-BlockArguments) to allow, among other things, `runSomeMonad do ...`. – chepner Nov 13 '18 at 17:51
1 Answers
26
Note that you can observe this effect with not just do
, but also let
, if
, \
, case
, the extensions mdo
and proc
…and the dread unary -
. I cannot think of a case in which this is ambiguous except for unary -
. Here’s how the grammar is defined in the Haskell 2010 Language Report, §3: Expressions.
exp
→ infixexp :: [context =>] type
| infixexp
infixexp
→ lexp qop infixexp
| - infixexp
| lexp
lexp
→ \ apat1 … apatn -> exp
| let decls in exp
| if exp [;] then exp [;] else exp
| case exp of { alts }
| do { stmts }
| fexp
fexp
→ [fexp] aexp
aexp
→ ( exp )
| …
There just happens to be no case defined in fexp
(function application) or aexp
(literal expression) that allows an unparenthesised lexp
(lambda, let
, etc.). I would consider this a bug in the grammar.
Fixing this would also remove the need for the $
typing hack.
-
9Years ago, I built GHC from sources with the grammar modified in this sort of way, and it indeed seemed not to break anything (e.g, it could successfully rebuild itself). I especially liked the look of calls which pass arguments to the block, like `withOpenFile path \ handle -> do ...` – Brandon Feb 21 '14 at 04:29
-
1@Brandon: I suppose you wouldn't have the time to push this modification as an e.g. `{-# LANGUAGE UnparenthesizedLayoutHeralds #-}` extension? – leftaroundabout Mar 04 '14 at 13:01
-
-
What, _shorter_? Whenever I type `MultiParamTypeClasses` I'm so delighted that Emacs' auto-complete is disabled in "comments", I'd love more of that kind of fun... – leftaroundabout Mar 05 '14 at 00:24
-
@leftaroundabout: Solution: type `XMulti`, hit M-/ to `dabbrev-expand` from the compiler error “perhaps you meant `-XMultiParamTypeClasses`”, then delete the `X`. :P – Jon Purdy Mar 05 '14 at 01:23