[Haskell-cafe] Is this a GHC parser error? A test case compiles from one line but not another.
Jeffrey Brown
jeffbrown.the at gmail.com
Sat Sep 9 04:53:45 UTC 2017
Thanks, Alexis! Your explanation of the distinction between let and where
is the first I've understood.
On Fri, Sep 8, 2017 at 9:05 PM, Alexis King <lexi.lambda at gmail.com> wrote:
> No, it isn’t a GHC parser error.
>
> Your preceding expression ends in a where clause, like this:
>
> assertBool "4" $ p == Right a where
> p = ...
>
> That’s fine, but make sure you understand how where works. Where clauses
> are not attached to individual expressions, they are *always* attached
> to definitions. In this case, the where clause is not attached to the
> assertion but to your definition of tHash. GHC parses your program the
> same way as this one:
>
> tHash = TestCase $ do
> ...
> assertBool "4" $ p == Right a
> where
> p = ...
>
> Therefore, the where clause forces the termination of the do block,
> since it exists syntactically outside the “body” of a definition. It is
> its own syntactic construct, an optional sequence of definitions in
> scope within the RHS of a definition. You cannot have any more
> expression after the where clause because they would be free-floating,
> outside of any particular definition.
>
> If you really want definitions that are scoped to that single assertion,
> use a let expression instead of a where clause:
>
> let p = ...
> a = ...
> in assertBool "4" $ p == Right a
>
> That works, since let...in blocks serve as completely ordinary
> expressions, and they are permitted anywhere an expression is permitted
> (unlike where clauses, which must “belong” to a top-level or let-bound
> definition).
>
> > On Sep 8, 2017, at 20:54, Jeffrey Brown <jeffbrown.the at gmail.com>
> > wrote:
> >
> > The test suite below[1] does what it's supposed to. The last line of
> > it is a commented-out test case:
> >
> > -- assertBool "broken" $ True
> >
> > If I uncomment that, it fails to compile:
> >
> > > :reload
> > [13 of 13] Compiling TParse ( test/TParse.hs, interpreted )
> >
> > test/TParse.hs:67:3: error: parse error on input ‘assertBool’
> > |
> > 67 | assertBool "broken" $ True
> > | ^^^^^^^^^^
> > Failed, 12 modules loaded.
> > >
> >
> > However, if I move it to somewhere earlier in the code, it works.
> >
> >
> > [1] https://github.com/JeffreyBenjaminBrown/digraphs-with-text/blob/
> e5afd39b950752ccb65997b89ab625d859299b4a/test/TParse.hs
> >
> > Jeff Brown | Jeffrey Benjamin Brown
>
>
--
Jeff Brown | Jeffrey Benjamin Brown
Website <https://msu.edu/~brown202/> | Facebook
<https://www.facebook.com/mejeff.younotjeff> | LinkedIn
<https://www.linkedin.com/in/jeffreybenjaminbrown>(spammy, so I often miss
messages here) | Github <https://github.com/jeffreybenjaminbrown>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170908/4536e73d/attachment.html>
More information about the Haskell-Cafe
mailing list