How can I stop GHCi from calling "show" for IO actions?

Prelude> let inf = repeat 1 Prelude> inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude> let x = inf (no output here!) Prelude> :t x x :: [Integer] Prelude> return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude> y <- return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude> :t y <interactive>:1:0: Not in scope: 'y' Is this a bug? Why does "y <- return exp" have different behavior than "let y = exp"? Is there a way to make GHCi not print the result of an action but still make my variables get bound? -- ryan

On Sat, Sep 15, 2007 at 08:35:02PM -0700, Ryan Ingram wrote:
Prelude> let inf = repeat 1 Prelude> inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude> let x = inf (no output here!) Prelude> :t x x :: [Integer] Prelude> return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude> y <- return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude> :t y
<interactive>:1:0: Not in scope: 'y'
Is this a bug? Why does "y <- return exp" have different behavior than "let y = exp"? Is there a way to make GHCi not print the result of an action but still make my variables get bound?
GHC manual, Flag reference, Interactive-mode options: | -fno-print-bind-result │ Turn off printing of binding results in GHCi │ dynamic │ - │ with a link to: http://haskell.org/ghc/dist/current/docs/users_guide/interactive-evaluation.... "Note that let bindings do not automatically print the value bound, unlike monadic bindings." "The automatic printing of binding results can be supressed with :set -fno-print-bind-result (this does not supress printing the result of non-binding statements). You might want to do this to prevent the result of binding statements from being fully evaluated by the act of printing them, for example." Stefan

Ryan Ingram wrote:
Prelude> let inf = repeat 1 Prelude> inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude> let x = inf (no output here!) Prelude> :t x x :: [Integer] Prelude> return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude> y <- return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude> :t y
<interactive>:1:0: Not in scope: 'y'
Is this a bug? Why does "y <- return exp" have different behavior than "let y = exp"? Is there a way to make GHCi not print the result of an action but still make my variables get bound?
That's weird. Prelude> (x,y) <- return $ (repeat 1, repeat 2) Prelude> Just x <- return $ Just (repeat 1) [1,1,1,... Prelude> (x,_) <- return $ (repeat 1, repeat 2) [1,1,1,... Prelude> Just (x,y) <- return $ Just (repeat 1, repeat 2) Prelude> It seems that GHCi outputs the contents of the variable you've created when there's only one of them. - Sam

On Sat, 15 Sep 2007, Sam Hughes wrote:
That's weird.
Prelude> (x,y) <- return $ (repeat 1, repeat 2)
You didn't tell, which Monad this shall be.
Prelude> Just x <- return $ Just (repeat 1) [1,1,1,... Prelude> (x,_) <- return $ (repeat 1, repeat 2) [1,1,1,... Prelude> Just (x,y) <- return $ Just (repeat 1, repeat 2) Prelude>

| -----Original Message----- | From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Sam | Hughes | Sent: 16 September 2007 04:53 | To: Ryan Ingram | Cc: haskell-cafe | Subject: Re: [Haskell-cafe] How can I stop GHCi from calling "show" for IO actions? | | Ryan Ingram wrote: | > Prelude> let inf = repeat 1 | > Prelude> inf | > [1,1,(lots of output until I press ctrl-c),Interrupted. | > (I expect this to happen) | > Prelude> let x = inf | > (no output here!) | > Prelude> :t x | > x :: [Integer] | > Prelude> return inf | > [1,1,(lots of output until I press ctrl-c),Interrupted. | > (I also expect this to happen) | > Prelude> y <- return inf | > [1,1,(lots of output until I press ctrl-c),Interrupted. | > (I do not expect this to happen here!) | > Prelude> :t y | > | > <interactive>:1:0: Not in scope: 'y' | > | > Is this a bug? Why does "y <- return exp" have different behavior | > than "let y = exp"? Is there a way to make GHCi not print the result | > of an action but still make my variables get bound? | | That's weird. | | Prelude> (x,y) <- return $ (repeat 1, repeat 2) | Prelude> Just x <- return $ Just (repeat 1) | [1,1,1,... | Prelude> (x,_) <- return $ (repeat 1, repeat 2) | [1,1,1,... | Prelude> Just (x,y) <- return $ Just (repeat 1, repeat 2) | Prelude> | | It seems that GHCi outputs the contents of the variable you've created | when there's only one of them. Indeed, that is documented behaviour (first bullet here: http://www.haskell.org/ghc/docs/latest/html/users_guide/ch03s04.html#ghci-st... ) Perhaps it's confusing behaviour? If so do suggest an alternative. Simon

| It seems that GHCi outputs the contents of the variable you've created | when there's only one of them.
Indeed, that is documented behaviour (first bullet here: http://www.haskell.org/ghc/docs/latest/html/users_guide/ch03s04.html#ghci-st... ) Perhaps it's confusing behaviour? If so do suggest an alternative.
why not simply do what the flag suggests, either always try to print the bind-result, or never? i assume there has been a use case where this special case has been found useful/necessary? as for earlier questions/suggestions in this thread:
Is there a way to make GHCi not print the result of an action but still make my variables get bound?
This seems to be a common question (I myself asked it recently), so I've added an entry to the GHCi page on the wiki.
Ideally, it would be nice if this were discoverable from within GHCi, but I'm not sure how this would best be done.
i've run into this myself!-) there is a patch pending for ghc head (so it may not be in 6.8.1:-( that would show the GHCi-specific flags together with the GHCi options: Prelude> :set options currently set: none. GHCi-specific dynamic flag settings: -fno-print-explicit-foralls -fprint-bind-result -fno-break-on-exception -fno-break-on-error -fno-print-evld-with-show other dynamic, non-language, flag settings: .. so you'd at least know which possibly relevant flags there are, and if the names are not expressive enough, the :help would point you to the flag reference: :help .. Options for ':set' and ':unset': +r revert top-level expressions after each evaluation +s print timing/memory stats after each evaluation +t print type after evaluation -<flags> most GHC command line flags can also be set here (eg. -v2, -fglasgow-exts, etc.) for GHCi-specific flags, see User's Guide, Flag reference, Interactive-mode options ..
I've always wondered if ghc(i) --help should be a bit more instructive, or perhaps if there were a man page that lay somewhere between the --help message and the manual in terms of comprehensiveness. It's a pretty major jump from a short description of 4 command line options (only one of which I have ever used) to the entire manual, with a ~10 page table of contents.
please note that the flag reference, which was pointed to earlier in this thread, is just such a summary, and there's a subsection dedicated to GHCi-specific options. there isn't much in that section in 6.6.1: http://www.haskell.org/ghc/docs/latest/html/users_guide/flag-reference.html#... but in ghc head and the upcoming 6.8, you'll find most of the GHCi-related flags summarized there. hth, claus

| >| It seems that GHCi outputs the contents of the variable you've created | >| when there's only one of them. | > | >Indeed, that is documented behaviour (first bullet here: | >http://www.haskell.org/ghc/docs/latest/html/users_guide/ch03s04.html#ghci-st... | >) | >Perhaps it's confusing behaviour? If so do suggest an alternative. | | why not simply do what the flag suggests, either always try to | print the bind-result, or never? i assume there has been a use | case where this special case has been found useful/necessary? Hmm. Currently GHCi prints the value of the *variable* that you bind. For example Just x <- foo 4 will print the value of x, *not* the value of (Just x). So if two variables are bound, there'd be two values to print. But an alternative behaviour would be to print the value of the LHS, constructors and all. That is in the above example, print Just <value of x> Then if there were two values, for example (x,Just y) <- bar 5 then it'd print ( <value of x>, Just <value of y> ) That would be a change from current behaviour, but it'd look identical when the LHS was a simple variable (the wildly common case), and it'd do something useful when multiple variables are bound. I'll add a Trac feature request so as not to forget this suggestion. Simon

On 9/20/07, Simon Peyton-Jones
Hmm. Currently GHCi prints the value of the *variable* that you bind. For example
Just x <- foo 4
will print the value of x, *not* the value of (Just x). So if two variables are bound, there'd be two values to print.
But an alternative behaviour would be to print the value of the LHS, constructors and all. That is in the above example, print Just <value of x> Then if there were two values, for example (x,Just y) <- bar 5 then it'd print ( <value of x>, Just <value of y> )
That would be a change from current behaviour, but it'd look identical when the LHS was a simple variable (the wildly common case), and it'd do something useful when multiple variables are bound.
I think a more consistent behavior would be to not print the LHS at all. If you wanted to print the result of the computation you could just do: Prelude> bar 5 or, if you also wanted bound variables afterwards: Prelude> (x, Just y) <- bar 5 Prelude> (x, Just y) Perhaps this is my imperative background speaking, but I don't see much difference at the GHCi prompt between these two: Prelude> let x = 5 Prelude> x <- return 5 I think they should have the same behavior by default; otherwise it's just confusing to the new user. For reference, I ran into this problem while playing around on the ICFP contest problem, and I became quite frustrated trying to do dna <- Data.ByteString.Char8.readFile "endo.dna" and having to wait a few minutes for the console to catch up while using GHCi as a simple interactive debugger. The best argument I can think of for changing the default value of that option is this: The workaround for printing the value of bound variables, if the default is to not print and you don't know the options, is simple: just type the bound variable in as an expression afterwards. On the other hand, there is no workaround to not print the value of bound variables, if you don't know about the existence of that option. In my case, I did do a cursory look to find an option to change this behavior, but I missed it in the documentation. -- ryan

On 20/09/2007, Ryan Ingram
I think a more consistent behavior would be to not print the LHS at all. If you wanted to print the result of the computation you could just do:
Prelude> bar 5
or, if you also wanted bound variables afterwards:
Prelude> (x, Just y) <- bar 5 Prelude> (x, Just y)
Perhaps this is my imperative background speaking, but I don't see much difference at the GHCi prompt between these two:
Prelude> let x = 5 Prelude> x <- return 5
I agree with this interpretation. If I want to just *do* the action, I'll type it. If I want to *store* the action then I'll bind it to something. That's the intuition I have, anyway. And it works for pure functions: Prelude> let ns = [1..] Prelude> [1..] The difference between those two feels like the difference between: Prelude> contents <- readFile "massive.txt" Prelude> readFile "massive.txt" But as Ryan mentioned, this may just be poor intuition from an overly imperative mindset... Cheers, D.

| I think a more consistent behavior would be to not print the LHS at | all. If you wanted to print the result of the computation you could | just do: | | Prelude> bar 5 | | or, if you also wanted bound variables afterwards: | | Prelude> (x, Just y) <- bar 5 | Prelude> (x, Just y) I've added Ryan's comments to http://hackage.haskell.org/trac/ghc/ticket/1721 Others might want to add further suggestions there. Simon

On 9/16/07, Ryan Ingram
Is there a way to make GHCi not print the result of an action but still make my variables get bound?
This seems to be a common question (I myself asked it recently), so I've added an entry to the GHCi page on the wiki. Ideally, it would be nice if this were discoverable from within GHCi, but I'm not sure how this would best be done. Stuart

I've always wondered if ghc(i) --help should be a bit more
instructive, or perhaps if there were a man page that lay somewhere
between the --help message and the manual in terms of
comprehensiveness. It's a pretty major jump from a short description
of 4 command line options (only one of which I have ever used) to the
entire manual, with a ~10 page table of contents.
On 16/09/2007, Stuart Cook
On 9/16/07, Ryan Ingram
wrote: Is there a way to make GHCi not print the result of an action but still make my variables get bound?
This seems to be a common question (I myself asked it recently), so I've added an entry to the GHCi page on the wiki.
Ideally, it would be nice if this were discoverable from within GHCi, but I'm not sure how this would best be done.
Stuart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 9/16/07, Ryan Ingram
wrote: Is there a way to make GHCi not print the result of an action but still make my variables get bound?
This seems to be a common question (I myself asked it recently), so I've added an entry to the GHCi page on the wiki.
Ideally, it would be nice if this were discoverable from within GHCi, but I'm not sure how this would best be done.
Stuart What is the url for the wiki entry?

On 9/17/07, Martin Lütke
What is the url for the wiki entry?
There was already a page at http://haskell.org/haskellwiki/GHC/GHCi so I put it there, but I also took the liberty of creating some #REDIRECTs, so http://haskell.org/haskellwiki/ghci should work just as well. Stuart
participants (10)
-
Claus Reinke
-
Dougal Stanton
-
Henning Thielemann
-
Martin Lütke
-
Rodrigo Queiro
-
Ryan Ingram
-
Sam Hughes
-
Simon Peyton-Jones
-
Stefan O'Rear
-
Stuart Cook