PrettyPrint.hs

Plain Haskell source file: PrettyPrint.hs

-- $Id: PrettyPrint.hs,v 1.7 2001/08/30 22:57:46 hallgren Exp $

  This uses PrettyM, rather than Pretty, as its starting point.


module PrettyPrint(module PrettyM,
		   ppiTuple,
		   ppiFTuple,  ppiFSeq,  ppiFSet,
		   wrapTuple, wrapSeq, wrapSet,
		   wrapFTuple, wrapFSeq, wrapFSet,
		   maybepp)
where

import PrettyM


-- Predefined instances:

instance Printable Char where
    ppi	c   = char c
    ppiList = text
    wrap    = ppi


  String is a type synonym.

instance Printable String where
    ppi     = text
    ppiList = text
    wrap    = ppi


instance Printable Bool    where wrap = ppi
instance Printable Int     where wrap = ppi
instance Printable Integer where wrap = ppi
instance Printable Float   where wrap = ppi
instance Printable Double  where wrap = ppi
instance Integral a => Printable (Ratio a) where wrap = ppi

instance Printable a => Printable [a] where
    ppi  = ppiList
    ppis = map ppi
    wrap = ppi

instance Printable a => Printable (Maybe a) where
    ppi (Just a) = ppi a
    ppi Nothing  = empty

    wrap (Just a) = wrap a
    wrap Nothing  = empty

instance (Printable a, Printable b) => Printable (a, b) where
    ppi (a, b) = parens $ ppi a <> comma <+> ppi b
    wrap = ppi

instance (Printable a, Printable b, Printable c) => Printable (a, b, c) where
    ppi (a, b, c) = parens $ ppi a <> comma <+>
                             ppi b <> comma <+>
                             ppi c
    wrap = ppi

instance (Printable a, Printable b, Printable c, Printable d) =>
    Printable (a, b, c, d) where
    ppi (a, b, c, d) = parens $ ppi a <> comma <+>
                                ppi b <> comma <+>
                                ppi c <> comma <+>
				ppi d
    wrap = ppi

instance (Printable a, Printable b, Printable c, Printable d, Printable e) =>
    Printable (a, b, c, d, e) where
    ppi (a, b, c, d, e) = parens $ ppi a <> comma <+>
                                   ppi b <> comma <+>
                                   ppi c <> comma <+>
				   ppi d <> comma <+>
				   ppi e
    wrap = ppi


Hugs doesn't define Show for 6-tuples or larger it seems.

instance (Printable a, Printable b, Printable c,
	  Printable d, Printable e, Printable e) =>
    Printable (a, b, c, d, e, f) where
    ppi (a, b, c, d, e, f) = lparen <> ppi a <> text ", "
                                    <> ppi b <> text ", "
                                    <> ppi c <> text ", "
                                    <> ppi d <> text ", "
                                    <> ppi e <> text ", "
			            <> ppi f <> rparen
    wrap = ppi


ppiTuple :: Printable a => [a] -> Doc
ppiTuple = parens . ppiSeq

ppiFTuple :: Printable a => [a] -> Doc
ppiFTuple = parens . ppiFSeq

ppiFSeq :: Printable a => [a] -> Doc
ppiFSeq = ppiFSet comma

ppiFSet :: Printable a => Doc -> [a] -> Doc
ppiFSet = ppiSep0 fsep

wrapTuple :: Printable a => [a] -> Doc
wrapTuple = parens . wrapSeq

wrapSeq :: Printable a => [a] -> Doc
wrapSeq = wrapSet comma

wrapSet :: Printable a => Doc -> [a] -> Doc
wrapSet = wrapSep0 sep


wrapFTuple :: Printable a => [a] -> Doc
wrapFTuple = parens . wrapFSeq

wrapFSeq :: Printable a => [a] -> Doc
wrapFSeq = wrapFSet comma

wrapFSet :: Printable a => Doc -> [a] -> Doc
wrapFSet = wrapSep0 fsep



wrapSep0 :: Printable a => ([Doc] -> Doc) -> Doc -> [a] -> Doc
wrapSep0 sepOp separator []  = empty
wrapSep0 sepOp separator [d] = wrap d
wrapSep0 sepOp separator ds  = sepOp $ punctuate separator $ map wrap ds



maybepp :: (a -> Doc) -> Maybe a -> Doc
maybepp pf (Just a) = pf a
maybepp _  Nothing  = empty

Index