Haskell parsing error due to incorrect indentation

Can someone explain why this is a syntax error?

f =
  f'
  where f' = do
    if True then
      return ()
    else
      return ()

main = f

If I give more indentation for a block if, then it somehow compiles well.

f =
  f'
  where f' = do
        if True then
          return ()
        else
          return ()

main = f

Or I can just separate wherewhat I usually do.

f =
  f'
  where
    f' = do
      if True then
        return ()
      else
        return ()

main = f

I begin generosity to get a good explanation for the two questions below. (Yes, I read the Haskell report. Shame on me for not understanding 10.3 Layout)

  • Why is the first example a mistake?
  • Why not a second example of an error?
+4
source share
1 answer

The rule you are violating is described in Note 1 in Section 10.3 . Let me quote:

1. , (n > m). , L , . : [, . ] p , , h.

:

f x = let  
         h y = let  
  p z = z  
               in p  
      in h

if -statement - f'. ( , , p - h.) if -statement , , (.. if , f'). .

p f', .


Edit:

, , p , f', , , (.. ). , do . :

f =
  f'
  where f' = -- no do
        if True then
          return ()
        else
          return ()

where/let do, -, . :

1) :

f = let y = do
        Just 1
    in y

2) :

f = let y =
        Just 1
    in y

3) :

f = do
 Just 1

4) :

f = do
Just 1

, (4) (1), (1) ( ).

2:

, , ! ( , . . .)

: , do -notation . NondecreasingIndentation, . . .

, , if-then-else -part:

{-# LANGUAGE NoNondecreasingIndentation #-}

f =
  f'
  where f' = do
        if True then
          return ()
        else
          return ()

NondecreasingIndentation , . (4), , .

TL; DR

. do - , . GHC do - - , NoNondecreasingIndentation.

+6

Source: https://habr.com/ru/post/1607023/


All Articles