Haskell indent rules violation for if-then-else

According to Haskell's indentation rules , "Code that is part of some expression must be indented further than the beginning of that expression." However, I found the following example, which seems to violate the above rule, compiles without any errors or warnings:

someFunction :: Bool -> Int -> Int -> Int someFunction condition ab = if condition then a - b else a + b 

Here I define the function someFunction , its body is an if-then-else block. According to the indentation rule, the then block is part of the same expression in the first line, so it must be indented further than the previous line. However, in my example, the second row then starts from the same column as the first row, and this example compiles.

I'm not sure what is going on here. I am working with GHC version 8.0.1.

+7
haskell
source share
1 answer

I am sure this is an artifact of intentionally changing the GHC in the indentation rule . Good catch!

Ghc reads this

 foo = do item if a then b else c item 

but

 foo = do { item ; if a ; then b ; else c ; item } 

which should cause a parsing error.

However, it was so common that at some point, the GHC developers decided to enable the option ; to then and else . This change in the if grammar makes code compilation.

This means that if become β€œspecial” since it should not be indented anymore, but only as much as the previous element. In the code placed in the question, then is indented in the same way as the previous element, so there is an implicit one in front of it ; , and it does code compilation.

I would still try to avoid this "style", though, since it’s bizarre.

(Personally, I would not add this special case to the GHC, but this is not very important.)

Now I noticed that Wikibook mentions this option as a "suggestion" for a future version of Haskell. This is a bit outdated now, and has since been implemented in the GHC.

+6
source share

All Articles