patch applied (packages/HaXml): Use ByteString for output (contributed by Jason Dagit, with modifications)

Duncan Coutts duncan.coutts at
Tue Nov 14 19:21:55 EST 2006

On Tue, 2006-11-14 at 14:53 -0800, Jason Dagit wrote:
> On 11/14/06, Bulat Ziganshin <bulat.ziganshin at> wrote:
> > Hello Malcolm,
> >
> > Tuesday, November 14, 2006, 8:49:31 PM, you wrote:
> >
> > > involved, which is why I choose a different route.  I am open to further
> > > ideas about how to best achieve a similar effect (i.e. ByteString ops
> > > available, but minimal code duplication).

For minimal code duplication make it generate ByteString internally and
have the String version just call pack/unpack. If I remember correctly,
unpack is lazy (and if it isn't, it should be).

> It would also be nice if the pretty printer came with a version that
> runs in instances of MonadIO so that they write their output eagerly
> instead of lazily.  My experience with haskell tells me that laziness
> is wonderful, especially when you can separate the concerns of
> computation and IO.  But, sometimes it seems that when you move to the
> optimization phase of a project that, that same approach becomes a
> problem and sometimes you end up needing to do your IO as soon/often
> as you can to keep from having poor space behavior.

The Text.PrettyPrint.HughesPJ module can output to various targets using
the fullRender function:

:: Mode
Rendering mode
-> Int
Line length
-> Float
Ribbons per line
-> (TextDetails -> a -> a)
What to do with text
-> a
What to do at the end
-> Doc
The document
-> a

So you can produce String, ByteString, do IO directly etc. Note how it
doesn't need MonadIO or anything, it's a pure function.

So for example:

hRender :: Handle -> Doc -> IO ()
hRender h =
  fullRender blah blah blah hPut
  where hPut (Chr c) next = hPutChar h c
        hPut (Str c) next = hPutStr  h s

Note that Text.PrettyPrint.HughesPJ originally envisaged optionally
using a packed string type: the TextDetails has a PStr constructor for a
packed string type. However at the moment that's also just String,
perhaps we should change that to Data.PackedString once the new
implementation of PackedString is available.


More information about the Libraries mailing list