Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let bindings in do notation without layout require "in"?

Tags:

haskell

In Haskell you can say

main = do
  let x = 5
  print x

and this will not compile:

main = do
  let x = 5
  in print x

But if I am using explicit layout, this does not compile:

main = do {
  let x = 5;
  print x;
  }

but this works:

main = do {
  let x = 5
  in print x;
  }

Am I right? Is there anyplace I can read more about explicit layout and do and let notation? Section 3.14 of the Haskell 98 report seems to me to suggest that my third example should work, as it says I can write

do { let DECLS; stmts }

and it translates to

let DECLS in do { stmts }
like image 345
Omari Norman Avatar asked Jan 28 '14 22:01

Omari Norman


1 Answers

The normative answer to your question can be found in the Haskell report's description of the layout rule.

Briefly, you need to place a semicolon between your let block and the next statement of the do block. That semicolon needs to lie outside of the let block. If you don't use layout for the let block, that's easy, just say:

  let {x = 5};

However, if you do use layout for the let block, then the only way to close the let block is to start a line in a column before the column of x. So that means you'd have to write something like this:

main = do {
  let x = 5
  ; print x;
}

Oh, and for your other example, again with layout a semicolon is getting inserted before the in, so your code desugars to:

main = do {
  let {x = 5
  };
  in print x
}
like image 98
user3188445 Avatar answered Sep 29 '22 18:09

user3188445