84f9406bcd6187046442c9acddd1723f598f87d3
[dead/harbl.git] / src / Pretty.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 -- | A typeclass for pretty-printing. Types that wish to be
4 -- pretty-printed should make themselves an instance of the 'Pretty'
5 -- class. The only class function that they need to implement is
6 -- 'pretty_show', which turns the thing into a string in a nice
7 -- way. The 'pretty_print' function then prints the result of
8 -- 'pretty_show' by default.
9 --
10 module Pretty ( Pretty(..) )
11 where
12
13 import Text.Parsec ( ParseError )
14
15
16 class Pretty a where
17 -- | Obtain a pretty 'String' representation of the given thingy.
18 pretty_show :: a -> String
19
20 -- | Pretty-print the given thingy.
21 pretty_print :: a -> IO ()
22 pretty_print = putStrLn . pretty_show
23
24
25 -- | Define a 'Pretty' instance for the result of 'parse'. This lets
26 -- us pretty-print the result of a parse attempt without worrying
27 -- about whether or not it failed. If the parse failed, you get the
28 -- same output that you usually would. Otherwise we pretty-print the
29 -- parsed value.
30 --
31 instance Pretty a => Pretty (Either ParseError a) where
32 pretty_show (Left err) = show err
33 pretty_show (Right v) = pretty_show v