[Haskell-cafe] commenting out a line of code

Andreas Abel andreas.abel at ifi.lmu.de
Thu Nov 17 16:15:53 UTC 2016


NondecreasingIndentation is useful to model the imperative pattern

   // exit early if cond
   if (cond) return res;
   // otherwise continue
   bla

in Haskell:

   if cond then return res else do
   bla

If you have several exit conditions and have to indent every time you 
get ugly "staircase" code.

However, it feels unintuitive that "then" and "else" do not set the 
indentation when they are first on a new line.  Thus, you get valid 
Haskell which defies any aethestics:

test = do
   if False
       then putStrLn "No"
       else case True of
     False -> putStrLn "No"
     True  -> putStrLn "Yes"

To be fair, Haskell is one of the first bigger languages with 
layout-sensitive parsing, and it is not surprising that the design is 
not smooth.

On 15.11.2016 17:21, Bernhard Herzog wrote:
> On 15.11.2016, David Turner wrote:
>> The outer do block should be desugared first, and the `putStrLn "done"` has
>> the same indent as the `if` so it gets a semicolon, in that block, meaning
>> it looks like this:
>>
>> main = do
>>   { if True
>>     then do
>>       putStrLn "A"
>>     else do
>>       -- putStrLn "B"
>>   ; putStrLn "done"
>>   }
>
> That's what one would expect. Except that GHC treats the 'putStrLn "done"' as
> part of the do-block in the else branch by default. This is due to the
> NondecreasingIndentation extension which is enabled by default (see
> https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/bugs.html#context-free-syntax).
> With the option -XNoNondecreasingIndentation (or -XHaskell2010) GHC does
> produce an error message because of the empty do-block.
>
>   Bernhard
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>


-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Department of Computer Science and Engineering
Chalmers and Gothenburg University, Sweden

andreas.abel at gu.se
http://www2.tcs.ifi.lmu.de/~abel/


More information about the Haskell-Cafe mailing list