{-# LANGUAGE CPP #-}
module Curry.Base.Pretty
( module Curry.Base.Pretty
, module Text.PrettyPrint
) where
#if __GLASGOW_HASKELL__ >= 804
import Prelude hiding ((<>))
#endif
import Text.PrettyPrint
class Pretty a where
pPrint :: a -> Doc
pPrint = pPrintPrec 0
pPrintPrec :: Int -> a -> Doc
pPrintPrec _ = pPrint
pPrintList :: [a] -> Doc
pPrintList = brackets . fsep . punctuate comma . map (pPrintPrec 0)
#if __GLASGOW_HASKELL__ >= 707
{-# MINIMAL pPrintPrec | pPrint #-}
#endif
prettyShow :: Pretty a => a -> String
prettyShow = render . pPrint
parenIf :: Bool -> Doc -> Doc
parenIf False = id
parenIf True = parens
ppIf :: Bool -> Doc -> Doc
ppIf True = id
ppIf False = const empty
maybePP :: (a -> Doc) -> Maybe a -> Doc
maybePP pp = maybe empty pp
blankLine :: Doc
blankLine = text ""
($++$) :: Doc -> Doc -> Doc
d1 $++$ d2 | isEmpty d1 = d2
| isEmpty d2 = d1
| otherwise = d1 $+$ blankLine $+$ d2
($-$) :: Doc -> Doc -> Doc
d1 $-$ d2 | isEmpty d1 = d2
| isEmpty d2 = d1
| otherwise = d1 $$ space $$ d2
sepByBlankLine :: [Doc] -> Doc
sepByBlankLine = foldr ($++$) empty
dot :: Doc
dot = char '.'
appPrec :: Int
appPrec = 10
larrow :: Doc
larrow = text "<-"
rarrow :: Doc
rarrow = text "->"
darrow :: Doc
darrow = text "=>"
backQuote :: Doc
backQuote = char '`'
backsl :: Doc
backsl = char '\\'
vbar :: Doc
vbar = char '|'
bquotes :: Doc -> Doc
bquotes doc = backQuote <> doc <> backQuote
bquotesIf :: Bool -> Doc -> Doc
bquotesIf b doc = if b then bquotes doc else doc
list :: [Doc] -> Doc
list = fsep . punctuate comma . filter (not . isEmpty)
instance Pretty Int where pPrint = int
instance Pretty Integer where pPrint = integer
instance Pretty Float where pPrint = float
instance Pretty Double where pPrint = double
instance Pretty () where pPrint _ = text "()"
instance Pretty Bool where pPrint = text . show
instance Pretty Ordering where pPrint = text . show
instance Pretty Char where
pPrint = char
pPrintList = text . show
instance (Pretty a) => Pretty (Maybe a) where
pPrintPrec _ Nothing = text "Nothing"
pPrintPrec p (Just x) = parenIf (p > appPrec)
$ text "Just" <+> pPrintPrec (appPrec + 1) x
instance (Pretty a, Pretty b) => Pretty (Either a b) where
pPrintPrec p (Left x) = parenIf (p > appPrec)
$ text "Left" <+> pPrintPrec (appPrec + 1) x
pPrintPrec p (Right x) = parenIf (p > appPrec)
$ text "Right" <+> pPrintPrec (appPrec + 1) x
instance (Pretty a) => Pretty [a] where
pPrintPrec _ xs = pPrintList xs
instance (Pretty a, Pretty b) => Pretty (a, b) where
pPrintPrec _ (a, b) = parens $ fsep $ punctuate comma [pPrint a, pPrint b]
instance (Pretty a, Pretty b, Pretty c) => Pretty (a, b, c) where
pPrintPrec _ (a, b, c) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c]
instance (Pretty a, Pretty b, Pretty c, Pretty d) => Pretty (a, b, c, d) where
pPrintPrec _ (a, b, c, d) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c, pPrint d]
instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e)
=> Pretty (a, b, c, d, e) where
pPrintPrec _ (a, b, c, d, e) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c, pPrint d, pPrint e]
instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e, Pretty f)
=> Pretty (a, b, c, d, e, f) where
pPrintPrec _ (a, b, c, d, e, f) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c, pPrint d, pPrint e, pPrint f]
instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e, Pretty f, Pretty g)
=> Pretty (a, b, c, d, e, f, g) where
pPrintPrec _ (a, b, c, d, e, f, g) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c, pPrint d, pPrint e, pPrint f, pPrint g]
instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e, Pretty f, Pretty g, Pretty h)
=> Pretty (a, b, c, d, e, f, g, h) where
pPrintPrec _ (a, b, c, d, e, f, g, h) = parens $ fsep $ punctuate comma
[pPrint a, pPrint b, pPrint c, pPrint d, pPrint e, pPrint f, pPrint g, pPrint h]