aboutsummaryrefslogtreecommitdiff
path: root/utils/CC/Pretty.hs
blob: ccb886ac089560eefbf942babc7c6711bff913c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module CC.Pretty where


class Pretty a where
    {-# MINIMAL pretty | prettyPrec #-}
    pretty :: a -> String
    pretty = prettyPrec (-1)

    -- | Higher precedence binds tighter.
    -- Parentheses are required if the printed element has lower precedence
    -- than the Int argument.
    prettyPrec :: Int -> a -> String
    prettyPrec _ = pretty

instance Pretty Int where
    pretty = show


-- Useful if you want the Pretty instance to be equal to the Show instance.
newtype PrettyShow a = PrettyShow a
  deriving (Show)

instance Show a => Pretty (PrettyShow a) where
    pretty (PrettyShow x) = show x


pprint :: Pretty a => a -> IO ()
pprint = putStrLn . pretty

precParens :: Int -> Int -> String -> String
precParens environ self s
    | self < environ = "(" ++ s ++ ")"
    | otherwise = s