GHC as a library - getting output from GHCI

Simon Marlow simonmarhaskell at gmail.com
Thu May 10 04:34:53 EDT 2007


Simon Peyton-Jones wrote:
> | Also if stmt =
> |
> |     SomeModule.prettyPrinter "foobar"
> |
> | and SomeModule contains
> |
> |     prettyPrinter x = putStrLn $ "Pretty: " ++ x
> |
> | then the let binding will not catch it.
> 
> Indeed, that's exactly what I meant in my original msg. I have always thought it ugly that stdin and stdout are top-level definitions.  I want to be able to say
> 
>         withStdout :: Handle -> IO a -> IO a
> 
> so that (withStdout h a) runs 'a' but sends all std-out output to h.
> 
> Haskell should support this.  Starting a whole OS process to redirect stdout must surely be overkill!

And we do have support for this in GHC.

$ ghci
    ___         ___ _
   / _ \ /\  /\/ __(_)
  / /_\// /_/ / /  | |    GHC Interactive, version 6.7, for Haskell 98.
/ /_\\/ __  / /___| |    http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|    Type :? for help.

Loading package base ... linking ... done.
Prelude> :m + System.IO
Prelude System.IO> h <- openFile "out" WriteMode
{handle: out}
Prelude System.IO> GHC.Handle.hDuplicateTo h stdout
-- as this point, GHCi goes quiet: I type ":quit"
$ cat out
Prelude System.IO> :quit
Leaving GHCi.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list