[Haskell-cafe] Re: HSH and IO ()

John Goerzen jgoerzen at complete.org
Sat Jun 13 00:46:18 EDT 2009

Dimitry Golubovsky wrote:
> John & all,
> I use HSH in my project where several external programs and Haskell
> functions need to be piped together: HSH is of great help here.
> I however came across the situation when one of pipe-connected
> functions has signature IO (), yet it reads from stdin* and writes to
> stdout.
> The documentation mentions "instance ShellCommand (Handle -> Handle ->
> IO ())" which could be of some help, but in the latest version of HSH
> on Hackage this instance is commented out.
> What was the reason of doing that? Is this to be expected in the
> upcoming versions?

Yes; that's due to the new more flexible way of sending data between
processes in HSH -- the Channel.

You can replace it with a function that can take any Channel, and
produce a result as a Channel of one particular sort.  In particular,
this instance:

instance ShellCommand (Channel -> IO Channel) where

is the direct replacement for what you were doing.

A Channel is generally a String, a lazy ByteString, or a Handle.

There are helper functions in HSH.Channel to deal with these:
chanAsString, chanAsBSL, and chanToHandle.  You can think of the first
two as similar to hGetContents.  The last will write the channel out
literally to a passed-along handle.

So, let's say that you wanted to process input as a String, and before
you were given a Handle that you used hGetContents on.  Now, you will
get a Channel, on which you will call chanAsString.  It will convert
whatever type of Channel you were handed into a String, lazily.

Does that make sense?

More information about the Haskell-Cafe mailing list