#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Safe #-}
#endif

-----------------------------------------------------------------------------
-- |
-- Module      :  Text.PrettyPrint.Annotated.HughesPJClass
-- Copyright   :  (c) Trevor Elliott <revor@galois.com> 2015
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  David Terei <code@davidterei.com>
-- Stability   :  stable
-- Portability :  portable
--
-- Pretty printing class, simlar to 'Show' but nicer looking.
--
-- Note that the precedence level is a 'Rational' so there is an unlimited
-- number of levels. This module re-exports
-- 'Text.PrettyPrint.Annotated.HughesPJ'.
--
-----------------------------------------------------------------------------

module Text.PrettyPrint.Annotated.HughesPJClass (
    -- * Pretty typeclass
    Pretty(..),

    PrettyLevel(..), prettyNormal,
    prettyShow, prettyParen,

    -- re-export HughesPJ
    module Text.PrettyPrint.Annotated.HughesPJ
  ) where

import Text.PrettyPrint.Annotated.HughesPJ

-- | Level of detail in the pretty printed output. Level 0 is the least
-- detail.
newtype PrettyLevel = PrettyLevel Int
  deriving (PrettyLevel -> PrettyLevel -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PrettyLevel -> PrettyLevel -> Bool
$c/= :: PrettyLevel -> PrettyLevel -> Bool
== :: PrettyLevel -> PrettyLevel -> Bool
$c== :: PrettyLevel -> PrettyLevel -> Bool
Eq, Eq PrettyLevel
PrettyLevel -> PrettyLevel -> Bool
PrettyLevel -> PrettyLevel -> Ordering
PrettyLevel -> PrettyLevel -> PrettyLevel
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PrettyLevel -> PrettyLevel -> PrettyLevel
$cmin :: PrettyLevel -> PrettyLevel -> PrettyLevel
max :: PrettyLevel -> PrettyLevel -> PrettyLevel
$cmax :: PrettyLevel -> PrettyLevel -> PrettyLevel
>= :: PrettyLevel -> PrettyLevel -> Bool
$c>= :: PrettyLevel -> PrettyLevel -> Bool
> :: PrettyLevel -> PrettyLevel -> Bool
$c> :: PrettyLevel -> PrettyLevel -> Bool
<= :: PrettyLevel -> PrettyLevel -> Bool
$c<= :: PrettyLevel -> PrettyLevel -> Bool
< :: PrettyLevel -> PrettyLevel -> Bool
$c< :: PrettyLevel -> PrettyLevel -> Bool
compare :: PrettyLevel -> PrettyLevel -> Ordering
$ccompare :: PrettyLevel -> PrettyLevel -> Ordering
Ord, Int -> PrettyLevel -> ShowS
[PrettyLevel] -> ShowS
PrettyLevel -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PrettyLevel] -> ShowS
$cshowList :: [PrettyLevel] -> ShowS
show :: PrettyLevel -> String
$cshow :: PrettyLevel -> String
showsPrec :: Int -> PrettyLevel -> ShowS
$cshowsPrec :: Int -> PrettyLevel -> ShowS
Show)

-- | The "normal" (Level 0) of detail.
prettyNormal :: PrettyLevel
prettyNormal :: PrettyLevel
prettyNormal = Int -> PrettyLevel
PrettyLevel Int
0

-- | Pretty printing class. The precedence level is used in a similar way as in
-- the 'Show' class. Minimal complete definition is either 'pPrintPrec' or
-- 'pPrint'.
class Pretty a where
  pPrintPrec :: PrettyLevel -> Rational -> a -> Doc ann
  pPrintPrec PrettyLevel
_ Rational
_ = forall a ann. Pretty a => a -> Doc ann
pPrint

  pPrint :: a -> Doc ann
  pPrint = forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
prettyNormal Rational
0

  pPrintList :: PrettyLevel -> [a] -> Doc ann
  pPrintList PrettyLevel
l = forall a. Doc a -> Doc a
brackets forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Doc a] -> Doc a
fsep forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
l Rational
0)

#if __GLASGOW_HASKELL__ >= 708
  {-# MINIMAL pPrintPrec | pPrint #-}
#endif

-- | Pretty print a value with the 'prettyNormal' level.
prettyShow :: (Pretty a) => a -> String
prettyShow :: forall a. Pretty a => a -> String
prettyShow = forall a. Doc a -> String
render forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a ann. Pretty a => a -> Doc ann
pPrint

pPrint0 :: (Pretty a) => PrettyLevel -> a -> Doc ann
pPrint0 :: forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l = forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
l Rational
0

appPrec :: Rational
appPrec :: Rational
appPrec = Rational
10

-- | Parenthesize an value if the boolean is true.
{-# DEPRECATED prettyParen "Please use 'maybeParens' instead" #-}
prettyParen :: Bool -> Doc ann -> Doc ann
prettyParen :: forall ann. Bool -> Doc ann -> Doc ann
prettyParen = forall ann. Bool -> Doc ann -> Doc ann
maybeParens

-- Various Pretty instances
instance Pretty Int where pPrint :: forall ann. Int -> Doc ann
pPrint = forall ann. Int -> Doc ann
int

instance Pretty Integer where pPrint :: forall ann. Integer -> Doc ann
pPrint = forall ann. Integer -> Doc ann
integer

instance Pretty Float where pPrint :: forall ann. Float -> Doc ann
pPrint = forall ann. Float -> Doc ann
float

instance Pretty Double where pPrint :: forall ann. Double -> Doc ann
pPrint = forall ann. Double -> Doc ann
double

instance Pretty () where pPrint :: forall ann. () -> Doc ann
pPrint ()
_ = forall a. String -> Doc a
text String
"()"

instance Pretty Bool where pPrint :: forall ann. Bool -> Doc ann
pPrint = forall a. String -> Doc a
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance Pretty Ordering where pPrint :: forall ann. Ordering -> Doc ann
pPrint = forall a. String -> Doc a
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance Pretty Char where
  pPrint :: forall ann. Char -> Doc ann
pPrint = forall ann. Char -> Doc ann
char
  pPrintList :: forall ann. PrettyLevel -> String -> Doc ann
pPrintList PrettyLevel
_ = forall a. String -> Doc a
text forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance (Pretty a) => Pretty (Maybe a) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> Maybe a -> Doc ann
pPrintPrec PrettyLevel
_ Rational
_ Maybe a
Nothing = forall a. String -> Doc a
text String
"Nothing"
  pPrintPrec PrettyLevel
l Rational
p (Just a
x) =
    forall ann. Bool -> Doc ann -> Doc ann
prettyParen (Rational
p forall a. Ord a => a -> a -> Bool
> Rational
appPrec) forall a b. (a -> b) -> a -> b
$ forall a. String -> Doc a
text String
"Just" forall a. Doc a -> Doc a -> Doc a
<+> forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
l (Rational
appPrecforall a. Num a => a -> a -> a
+Rational
1) a
x

instance (Pretty a, Pretty b) => Pretty (Either a b) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> Either a b -> Doc ann
pPrintPrec PrettyLevel
l Rational
p (Left a
x) =
    forall ann. Bool -> Doc ann -> Doc ann
prettyParen (Rational
p forall a. Ord a => a -> a -> Bool
> Rational
appPrec) forall a b. (a -> b) -> a -> b
$ forall a. String -> Doc a
text String
"Left" forall a. Doc a -> Doc a -> Doc a
<+> forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
l (Rational
appPrecforall a. Num a => a -> a -> a
+Rational
1) a
x
  pPrintPrec PrettyLevel
l Rational
p (Right b
x) =
    forall ann. Bool -> Doc ann -> Doc ann
prettyParen (Rational
p forall a. Ord a => a -> a -> Bool
> Rational
appPrec) forall a b. (a -> b) -> a -> b
$ forall a. String -> Doc a
text String
"Right" forall a. Doc a -> Doc a -> Doc a
<+> forall a ann. Pretty a => PrettyLevel -> Rational -> a -> Doc ann
pPrintPrec PrettyLevel
l (Rational
appPrecforall a. Num a => a -> a -> a
+Rational
1) b
x

instance (Pretty a) => Pretty [a] where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> [a] -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ = forall a ann. Pretty a => PrettyLevel -> [a] -> Doc ann
pPrintList PrettyLevel
l

instance (Pretty a, Pretty b) => Pretty (a, b) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> (a, b) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b]

instance (Pretty a, Pretty b, Pretty c) => Pretty (a, b, c) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> (a, b, c) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c]

instance (Pretty a, Pretty b, Pretty c, Pretty d) => Pretty (a, b, c, d) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> (a, b, c, d) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c, d
d) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma
      [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l d
d]

instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e) => Pretty (a, b, c, d, e) where
  pPrintPrec :: forall ann. PrettyLevel -> Rational -> (a, b, c, d, e) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c, d
d, e
e) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma
      [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l d
d, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l e
e]

instance (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e, Pretty f) => Pretty (a, b, c, d, e, f) where
  pPrintPrec :: forall ann.
PrettyLevel -> Rational -> (a, b, c, d, e, f) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c, d
d, e
e, f
f) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma
      [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c,
        forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l d
d, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l e
e, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l f
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 :: forall ann.
PrettyLevel -> Rational -> (a, b, c, d, e, f, g) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c, d
d, e
e, f
f, g
g) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma
      [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c,
        forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l d
d, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l e
e, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l f
f, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l g
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 :: forall ann.
PrettyLevel -> Rational -> (a, b, c, d, e, f, g, h) -> Doc ann
pPrintPrec PrettyLevel
l Rational
_ (a
a, b
b, c
c, d
d, e
e, f
f, g
g, h
h) =
    forall a. Doc a -> Doc a
parens forall a b. (a -> b) -> a -> b
$ forall a. [Doc a] -> Doc a
fsep forall a b. (a -> b) -> a -> b
$ forall a. Doc a -> [Doc a] -> [Doc a]
punctuate forall a. Doc a
comma
      [forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l a
a, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l b
b, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l c
c,
        forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l d
d, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l e
e, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l f
f, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l g
g, forall a ann. Pretty a => PrettyLevel -> a -> Doc ann
pPrint0 PrettyLevel
l h
h]