[Haskell-cafe] $ do?
Brandon Moore
brandon_m_moore at yahoo.com
Thu Dec 16 07:31:14 CET 2010
> From: Roman Cheplyaka <roma at ro-che.info>
> Sent: Wed, December 15, 2010 1:36:55 AM
>
> * Jonathan Geddes <geddes.jonathan at gmail.com> [2010-12-14 19:59:14-0700]
> > Quick question:
> >
> > Why do I need the $ in the following bits of code?
> >
> > > main = withSocketsDo $ do
> > > --do something with sockets
> >
> > > foo = fromMaybe 0 $ do
> > > --do something in the maybe monad
> >
> > I don't see (after admittedly only a minute or so thinking about it)
> > where any grammar ambiguities would be if 'do' had an implicit $ in
> > front of it:
> >
> > > foo = fromMaybe 0 do
> > > --do something in maybe
> >
> > Though now that I've written it down, that is hard for me to visually
> > parse at a glance. I'm still curious though.
>
> Hi Jonathan,
>
> it's not clear whether you ask how this implies from the standard or
> what the rationale of such syntax is.
>
> Regarding the former, there are two distinct syntactic categories in
> Haskell: lexp and aexp. aexp produces all the constructs that can be
> used as function arguments and does not include do expressions. lexp is
> more broad and produces do expressions as well. Next look at these
> productions to see the difference between function application and
> operator application (sections 3.3 and 3.4 of 2010 report):
>
> fexp → [fexp] aexp
>
> infixexp → lexp qop infixexp
>
> Regarding the rationale, I'm not so sure and I'd like to hear an
> explanation from someone competent. But I assume it has something
> to do with the fact that if you supply a 'do' argument, you cannot
> supply any more arguments (because 'do' extends to the right as far as
> possible). Not that I'm convinced that it is a valid reason to prohibit
> such construct.
Years ago, I built GHC with a modified grammar including do and lambdas as aexp.
It seemed to work. There were certainly conflicts reported by Happy, but
examples
seemed to work nicely and I couldn't think of any actual ambiguity.
Brandon
More information about the Haskell-Cafe
mailing list