X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fharbl.git;a=blobdiff_plain;f=src%2FPretty.hs;fp=src%2FPretty.hs;h=360bd4c1b8a4ded3875972f8a30c195284051c5e;hp=0000000000000000000000000000000000000000;hb=edb0055e306eb6b323bbc75bfb7d15089344af81;hpb=e1060ef815f35309c7ca0800a345d7c54ce346bd diff --git a/src/Pretty.hs b/src/Pretty.hs new file mode 100644 index 0000000..360bd4c --- /dev/null +++ b/src/Pretty.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE FlexibleInstances #-} + +-- | A typeclass for pretty-printing. Types that wish to be +-- pretty-printed should make themselves an instance of the 'Pretty' +-- class. The only class function that they need to implement is +-- 'pretty_show', which turns the thing into a string in a nice +-- way. The 'pretty_print' function then prints the result of +-- 'pretty_show' by default. +-- +module Pretty +where + +import Text.Parsec ( ParseError ) + + +class Pretty a where + -- | Obtain a pretty 'String' representation of the given thingy. + pretty_show :: a -> String + + -- | Pretty-print the given thingy. + pretty_print :: a -> IO () + pretty_print = putStrLn . pretty_show + + +-- | Define a 'Pretty' instance for the result of 'parse'. This lets +-- us pretty-print the result of a parse attempt without worrying +-- about whether or not it failed. If the parse failed, you get the +-- same output that you usually would. Otherwise we pretty-print the +-- parsed value. +-- +instance Pretty a => Pretty (Either ParseError a) where + pretty_show (Left err) = show err + pretty_show (Right v) = pretty_show v