Search code examples
haskellsyntaxparse-errorletdo-notation

Why is this a parse error in do notation?


Consider the following code:

foo = do
  let bar = do
    baz
  bar

It doesn't parse in ghc, version 8. It complains about the line containing baz. This code does parse, though:

foo = do
  let bar = do
      baz
  bar

I find this confusing. What's the essential difference between the two versions?


Solution

  • The problem is the indentation puts baz in no-mans land. It isn't indented far enough to be part of the let expression, but it is indented too far to be the next part of the do expression containing the let expression.