pretty-printing data

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

pretty-printing data

Michael Mossey
For debugging purposes I'm interested in pretty-printing data; to start
with, lists of algebraic data types. Basically I'd like 'show' with the
ability to put each entry of a list on a separate line, and indented. Note
that the algebraic data might have an inner list as one of its elements, so
this is a non-obvious formatting problem.

I believe I can make instances of Show, can I not? Is there something
called showList which I can use to code my own method of showing lists of a
particular type? My understanding is that I can't make [a] an instance of
Show; hence they provided showList.

So basically, I'm wondering if a pretty-printing (for data!) library
already exists, or whether I should try to make instances of Show.

Thanks,
Mike
Reply | Threaded
Open this post in threaded view
|

pretty-printing data

Magnus Therning
On Sat, Aug 29, 2009 at 3:22 PM, Michael Mossey<[hidden email]> wrote:

> For debugging purposes I'm interested in pretty-printing data; to start
> with, lists of algebraic data types. Basically I'd like 'show' with the
> ability to put each entry of a list on a separate line, and indented. Note
> that the algebraic data might have an inner list as one of its elements, so
> this is a non-obvious formatting problem.
>
> I believe I can make instances of Show, can I not? Is there something called
> showList which I can use to code my own method of showing lists of a
> particular type? My understanding is that I can't make [a] an instance of
> Show; hence they provided showList.
>
> So basically, I'm wondering if a pretty-printing (for data!) library already
> exists, or whether I should try to make instances of Show.

Take a look at the pretty-printing libraries available on Hackage.
I've personally used two of them, pretty and wl-pprint.  I'm fairly
sure both of them define pretty-printing for lists, but I don't know
whether it's done the way you want.

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe
Reply | Threaded
Open this post in threaded view
|

Re: pretty-printing data

Christian Maeder-2
In reply to this post by Michael Mossey
Michael Mossey wrote:

> For debugging purposes I'm interested in pretty-printing data; to start
> with, lists of algebraic data types. Basically I'd like 'show' with the
> ability to put each entry of a list on a separate line, and indented.
> Note that the algebraic data might have an inner list as one of its
> elements, so this is a non-obvious formatting problem.
>
> I believe I can make instances of Show, can I not? Is there something
> called showList which I can use to code my own method of showing lists
> of a particular type? My understanding is that I can't make [a] an
> instance of Show; hence they provided showList.

For your own data type say "Foo" you can provide your own showList
definition, that will be used whenever you show something of type
"[Foo]". You cannot rewrite the (generic) "instance Show a => Show [a]".

instance Show Foo where
  show _ = "Foo"
  showList l s = unlines (map (("  " ++) . show) l) ++ s

With overlapping instances you could rewrite "instance Show [Foo]", but
you should prefer the above or start with a separate class "Pretty" and
use show as default implementation:

 class Show a = Pretty a
  pretty :: a -> String
  pretty = show

With some ghc extension (undecidable instances?) you can get instances
for all types:

 instance Show a = Pretty a

Provide a list instance:

 instance Pretty a => Pretty [a] where
   pretty l = ...

and you can write (overlapping) instances for other types. For your own
data types use "deriving Show" and provide a Pretty instance (if you
don't like the Show result).

HTH Christian