From 39ea4ac3a4b7663882a83f2ada43c8238f087d9b Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 23 Jul 2020 20:15:15 +0200 Subject: Use Pretty for errors and expressions --- utils/CC/Pretty.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'utils/CC') 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 -- cgit v1.2.3-70-g09d2