Layout rules for if then else

I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement. My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`. Using GHC 7.10 RC2, ghci cheerfully loads the following ``` f = if True then 1 else 2 ``` Is this valid? Have the rules been relaxed? Alan

What you state is true within a `do` expression. What you wrote is a
single if_then_else_ expression on the rhs of the function definition.
Cheers,
Thu
2015-03-10 17:01 GMT+01:00 Alan & Kim Zimmerman
I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement.
My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`.
Using GHC 7.10 RC2, ghci cheerfully loads the following
``` f = if True then 1 else 2
```
Is this valid? Have the rules been relaxed?
Alan
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Tue, Mar 10, 2015 at 12:01 PM, Alan & Kim Zimmerman
I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement.
My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`.
I believe you are looking for DoAndIfThenElse. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Ok, adding a do does make a difference. More complexity.
Thanks
Alan
On Tue, Mar 10, 2015 at 6:06 PM, Brandon Allbery
On Tue, Mar 10, 2015 at 12:01 PM, Alan & Kim Zimmerman < alan.zimm@gmail.com> wrote:
I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement.
My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`.
I believe you are looking for DoAndIfThenElse.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Just to be clear, I *think* layout rules don't apply here at all, actually.
If I understand correctly, "layout" has to do with turning spacing into
braces and semicolons. A new line is a semicolon. A brace group is inserted
around things that are aligned where a brace group actually makes sense.
e.g.
let x = y
y = z
in ...
gets turned into roughly
let { x = y
; y = z
} in ...
if-then-else however is just an expression, like a ternary operator, so it
doesn't need any semicolons or braces. So layout is unrelated, except for
the issue of `do` blocks inserting semicolons into `if-then-else` groups
(and that's what DoAndIfThenElse fixes).
-- Andrew
PS. I am very glad someone is working on ghc-exactprint. It's a really
important step in developing better Haskell tooling, imho. Doing that sort
of thing right now with haskell-src-exts right now is a real pain (see half
of the closed issues on hindent... about how it doesn't preserve formatting
in many places.)
On Tue, Mar 10, 2015 at 9:25 AM, Alan & Kim Zimmerman
Ok, adding a do does make a difference. More complexity.
Thanks Alan
On Tue, Mar 10, 2015 at 6:06 PM, Brandon Allbery
wrote: On Tue, Mar 10, 2015 at 12:01 PM, Alan & Kim Zimmerman < alan.zimm@gmail.com> wrote:
I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement.
My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`.
I believe you are looking for DoAndIfThenElse.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Thanks, I interpret layout as you say, but with the aim of preserving valid spacing if syntax elements are moved around as a result of AST modifications. My implementation flags those points, such as the contents of a let .. in, or a do expression. In terms of ghc-exactprint, I actually think the technique I am using of converting all the original absolute locations to relative ones, editing the AST and then outputting it should be transferrable to haskell-src-exts too. Alan On Tue, Mar 10, 2015 at 8:05 PM, Andrew Gibiansky < andrew.gibiansky@gmail.com> wrote:
Just to be clear, I *think* layout rules don't apply here at all, actually.
If I understand correctly, "layout" has to do with turning spacing into braces and semicolons. A new line is a semicolon. A brace group is inserted around things that are aligned where a brace group actually makes sense.
e.g.
let x = y y = z in ...
gets turned into roughly
let { x = y ; y = z } in ...
if-then-else however is just an expression, like a ternary operator, so it doesn't need any semicolons or braces. So layout is unrelated, except for the issue of `do` blocks inserting semicolons into `if-then-else` groups (and that's what DoAndIfThenElse fixes).
-- Andrew
PS. I am very glad someone is working on ghc-exactprint. It's a really important step in developing better Haskell tooling, imho. Doing that sort of thing right now with haskell-src-exts right now is a real pain (see half of the closed issues on hindent... about how it doesn't preserve formatting in many places.)
On Tue, Mar 10, 2015 at 9:25 AM, Alan & Kim Zimmerman
wrote:
Ok, adding a do does make a difference. More complexity.
Thanks Alan
On Tue, Mar 10, 2015 at 6:06 PM, Brandon Allbery
wrote: On Tue, Mar 10, 2015 at 12:01 PM, Alan & Kim Zimmerman < alan.zimm@gmail.com> wrote:
I am working on ghc-exactprint, and need to flag points where layout must be preserved, and considering the `if` statement.
My understanding of the layout rules for if then else is that the `then` has to start more to the right than the `if`.
I believe you are looking for DoAndIfThenElse.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

To torment you further, I and others have legal (Haskell 2010), bizzare examples at http://lpaste.net/81623 (Most of them were illegal in Haskell 98.)

Wow. Thanks, will put them in as test cases
Alan
On Thu, Mar 12, 2015 at 7:01 PM, Albert Y. C. Lai
To torment you further, I and others have legal (Haskell 2010), bizzare examples at
(Most of them were illegal in Haskell 98.)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (5)
-
Alan & Kim Zimmerman
-
Albert Y. C. Lai
-
Andrew Gibiansky
-
Brandon Allbery
-
Vo Minh Thu