
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? claus

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? Cheers, Simon

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.
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:
i think i've found a less disruptive approach. replace io (putStrLn output) with enqueueCommands ["let output = "++show output,"putStrLn output"] there's not always such a nice output/io separation for many commands, but we could introduce this a few commands at a time, following demand. :b Data.List .. putStrLn $ unlines $ filter (\l->"Bool" `elem` words l) $ lines output all :: (a -> Bool) -> [a] -> Bool and :: [Bool] -> Bool any :: (a -> Bool) -> [a] -> Bool elem :: (Eq a) => a -> [a] -> Bool notElem :: (Eq a) => a -> [a] -> Bool null :: [a] -> Bool or :: [Bool] -> Bool isPrefixOf :: (Eq a) => [a] -> [a] -> Bool isSuffixOf :: (Eq a) => [a] -> [a] -> Bool isInfixOf :: (Eq a) => [a] -> [a] -> Bool what worries me is that ghci's standard "it" bindings do not seem to be done this way at all, but are burried somewhere in the type-checking? also, it might be nice to introduce a GHCi qualifier, so that we could refer to GHCi.ouput and GHCi.it, in case of ambiguities? claus oh, and while i'm at it, the pretty-printing is rather less than optimal:-) zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [(a, b, c, d, e, f, g)]
participants (2)
-
Claus Reinke
-
Simon Marlow