commenting out a line of code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

commenting out a line of code

Johannes Waldmann-2
Dear Cafe -

Sometimes, a comment can change the meaning of a program.
No? Well, but "commenting out" a line of code certainly does.
So, can you guess what this program will output?

main = do
  if True
    then do
      putStrLn "A"
    else do
      -- putStrLn "B"
  putStrLn "done"

I nominate this for inclusion in a hypothetical
Haskell equivalent of http://www.javapuzzlers.com/

- J.
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

Norbert Melzer
I'm not quite sure, but I'd guess either one of

1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.

Johannes Waldmann <[hidden email]> schrieb am Di., 15. Nov. 2016 um 13:11 Uhr:
Dear Cafe -

Sometimes, a comment can change the meaning of a program.
No? Well, but "commenting out" a line of code certainly does.
So, can you guess what this program will output?

main = do
  if True
    then do
      putStrLn "A"
    else do
      -- putStrLn "B"
  putStrLn "done"

I nominate this for inclusion in a hypothetical
Haskell equivalent of http://www.javapuzzlers.com/

- J.
_______________________________________________
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.

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

S. Doaitse Swierstra
The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.


 Doaitse


Op 15 nov. 2016, om 13:31  heeft Norbert Melzer <[hidden email]> het volgende geschreven:

I'm not quite sure, but I'd guess either one of

1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.

Johannes Waldmann <[hidden email]> schrieb am Di., 15. Nov. 2016 um 13:11 Uhr:
Dear Cafe -

Sometimes, a comment can change the meaning of a program.
No? Well, but "commenting out" a line of code certainly does.
So, can you guess what this program will output?

main = do
  if True
    then do
      putStrLn "A"
    else do
      -- putStrLn "B"
  putStrLn "done"

I nominate this for inclusion in a hypothetical
Haskell equivalent of http://www.javapuzzlers.com/

- J.
_______________________________________________
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.
_______________________________________________
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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

David Turner-2
On 15 November 2016 at 12:53, Doaitse Swierstra <[hidden email]> wrote:
The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.
 
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"
  }



On 15 November 2016 at 12:31, Norbert Melzer <[hidden email]> wrote:
I'm not quite sure, but I'd guess either one of

1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.


I get *both* of these behaviours! When building, it complains about an empty 'do' block. When running through `stack ghci` it doesn't seem to mind.


[linuxadmin@trac-leeds-09-vm01 13:07:57 ~/indent-puzzle ]
$ stack build
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
[linuxadmin@trac-leeds-09-vm01 13:08:00 ~/indent-puzzle ]
$ stack ghci
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
Warning: build failed, but optimistically launching GHCi anyway
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Using main module: 1. Package `indent-puzzle' component exe:indent-puzzle-exe with main-is file: /home/linuxadmin/indent-puzzle/app/Main.hs
Configuring GHCi with the following packages: indent-puzzle
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( /home/linuxadmin/indent-puzzle/app/Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
A
*Main>

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

Lana Black
I havve just tested this with ghc8, and surprisingly when B is commented out the last putStrLn is treated as a part of if-else. So, with B commented out the only thing printed out is A and not 'done'.

From: David Turner
Sent: Tuesday, November 15, 2016 1:14 PM
To: Haskell Cafe
Subject: Re: [Haskell-cafe] commenting out a line of code

On 15 November 2016 at 12:53, Doaitse Swierstra <[hidden email]> wrote:
The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.
 
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"
  }



On 15 November 2016 at 12:31, Norbert Melzer <[hidden email]> wrote:
I'm not quite sure, but I'd guess either one of

1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.


I get *both* of these behaviours! When building, it complains about an empty 'do' block. When running through `stack ghci` it doesn't seem to mind.


[linuxadmin@trac-leeds-09-vm01 13:07:57 ~/indent-puzzle ]
$ stack build
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
[linuxadmin@trac-leeds-09-vm01 13:08:00 ~/indent-puzzle ]
$ stack ghci
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
Warning: build failed, but optimistically launching GHCi anyway
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Using main module: 1. Package `indent-puzzle' component exe:indent-puzzle-exe with main-is file: /home/linuxadmin/indent-puzzle/app/Main.hs
Configuring GHCi with the following packages: indent-puzzle
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( /home/linuxadmin/indent-puzzle/app/Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
A
*Main>


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

Alex Belanger

GHCi is very lax with do blocks; but as far as I know GHC should refuse to compile that snippet and give a diagnosis about an empty do.

- nitrix


On Nov 15, 2016 10:12 AM, "Lana Black" <[hidden email]> wrote:
I havve just tested this with ghc8, and surprisingly when B is commented out the last putStrLn is treated as a part of if-else. So, with B commented out the only thing printed out is A and not 'done'.

From: David Turner
Sent: Tuesday, November 15, 2016 1:14 PM
To: Haskell Cafe
Subject: Re: [Haskell-cafe] commenting out a line of code

On 15 November 2016 at 12:53, Doaitse Swierstra <[hidden email]> wrote:
The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.
 
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"
  }



On 15 November 2016 at 12:31, Norbert Melzer <[hidden email]> wrote:
I'm not quite sure, but I'd guess either one of

1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.


I get *both* of these behaviours! When building, it complains about an empty 'do' block. When running through `stack ghci` it doesn't seem to mind.


[linuxadmin@trac-leeds-09-vm01 13:07:57 ~/indent-puzzle ]
$ stack build
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
[linuxadmin@trac-leeds-09-vm01 13:08:00 ~/indent-puzzle ]
$ stack ghci
indent-puzzle-0.1.0.0: build
Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-puzzle-exe/indent-puzzle-exe-tmp/Main.o )

/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block

--  While building package indent-puzzle-0.1.0.0 using:
      /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
Warning: build failed, but optimistically launching GHCi anyway
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Using main module: 1. Package `indent-puzzle' component exe:indent-puzzle-exe with main-is file: /home/linuxadmin/indent-puzzle/app/Main.hs
Configuring GHCi with the following packages: indent-puzzle
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( /home/linuxadmin/indent-puzzle/app/Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
A
*Main>


_______________________________________________
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.

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

Bernhard Herzog
In reply to this post by Johannes Waldmann-2
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.
Reply | Threaded
Open this post in threaded view
|

Re: commenting out a line of code

Andreas Abel
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

[hidden email]
http://www2.tcs.ifi.lmu.de/~abel/
_______________________________________________
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.