Vladimir Still
Vladimir Still

Reputation: 631

Disable printing of IO results in GHCi?

When running IO actions in GHCi prompt it automatically runs the action and shows result, this is nice, but not for students trying to understand difference between IO and non-IO. Is there a way to change configuration of GHCi so that it runs the action, but shows something like <<IO Int action>> instead? Something more like result for ST actions (but action should be performed):

now it does:

> return 1 :: IO Int
1
> return 1 :: ST s Int
<<ST action>>

i would like:

> return 1 :: IO Int
<<IO Int action>>
> putStrLn "bla"
bla
<<IO () action>>

Edit:

  1. I just found that IO is probably the only thing handled specially by GHCi, ST actually has instance for Show (ST s a) which returns "<<ST action>>". So maybe if I could disable this special treatment of IO it would be sufficient.
  2. As for allowed code changes: manually changing evaluated expression is not an option. Change in libraries might be, but I would prefer not to do that (I considered creating wrapped IO type, but then interpreter will not run the action). If GHCi could automatically wrap IO actions somehow, that would be an option.

Upvotes: 18

Views: 1237

Answers (1)

MathematicalOrchid
MathematicalOrchid

Reputation: 62818

This is an interesting question. The only thing I can come up with is writing some kind of custom prelude module that exports a type called IO, but which isn't "the" I/O type that GHCi is special-casing.

Of course, this is no help at all unless the student remembers to import this rather than the real prelude. I suppose you could write that into the GHCi config file, but... well, it's certainly not perfect.

The only other way I can think of is to use the GHC-API to basically reimplement GHCi yourself. But that sounds like waaaay too much work...

Upvotes: 2

Related Questions