:def and ghci command output

Simon Marlow simonmarhaskell at gmail.com
Fri Aug 24 03:59:42 EDT 2007

Claus Reinke wrote:
> ghci's :def takes a 'String -> IO String' and converts it into a ghci 
> command.
> this makes it possible to pass a command parameter to a haskell 
> function, and to interpret the result of the call as a ghci command.
> that is nice, but how do i get access to the command's output? that is, 
> i'd like to run a ghci command, and capture its ouput as a haskell 
> String, eg, to pass it on to another ghci command, or to a haskell 
> function.
> perhaps something simple, like an additional ghci top-level binding:
>    GHCi.cmd :: String -> IO String
> so that i could write things like
>    let doSomething output = ..
>    :def mymodules \_->GHCi.cmd ":show modules" >>= doSomething
> or a way to compose ghci commands, so that i could write
>    :def doSomething \output->..
>    :def mymodules \_->return ":show modules :>>= :doSomething"
> would that be difficult to provide?

It would be possible, but needs a bit of refactoring because all the output 
is currently just sent to stdout.  I'd go with something like your second 
option, but perhaps add some new syntax:

 >    :def mymodules \_->return "str <- :show modules; doSomething str"

Write up a feature request?


More information about the Glasgow-haskell-users mailing list