aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-07-23 20:15:15 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-07-23 20:15:15 +0200
commit39ea4ac3a4b7663882a83f2ada43c8238f087d9b (patch)
tree70b28d7f0bd301f8eb912837b126956ecdaa1ca3 /utils
parentbc52411ae2ed26cab1d5086ae6df68f23ebbd052 (diff)
Use Pretty for errors and expressions
Diffstat (limited to 'utils')
-rw-r--r--utils/CC/Pretty.hs24
1 files changed, 24 insertions, 0 deletions
diff --git a/utils/CC/Pretty.hs b/utils/CC/Pretty.hs
index 0a41abe..ccb886a 100644
--- a/utils/CC/Pretty.hs
+++ b/utils/CC/Pretty.hs
@@ -2,8 +2,32 @@ 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