8.4. Pretty: Pretty printing combimators

This library contains Simon Peyton Jones' implementation of John Hughes's pretty printer combinators.

infixl 6 <>, <+>
infixl 5 $$, $+$
data Doc   -- the document datatype, abstract, instance of Show

-- primitive documents
empty              :: Doc
semi, comma, colon :: Doc
space, equals      :: Doc
lparen, rparen     :: Doc
lbrack, rbrack     :: Doc
lbrace, rbrace     :: Doc

-- converting values into documents
text     :: String   -> Doc 
ptext    :: String   -> Doc 
char     :: Char     -> Doc
int      :: Int      -> Doc
integer  :: Integer  -> Doc
float    :: Float    -> Doc
double   :: Double   -> Doc
rational :: Rational -> Doc

-- wrapping documents into delimiters
parens, brackets, braces :: Doc -> Doc 
quotes, doubleQuotes     :: Doc -> Doc

-- combining documents
(<>)      :: Doc -> Doc -> Doc  -- Beside
hcat      :: [Doc] -> Doc       -- List version of <>
(<+>)     :: Doc -> Doc -> Doc  -- Beside, separated by space
hsep      :: [Doc] -> Doc       -- List version of <+>
($$)      :: Doc -> Doc -> Doc  -- Above; "dovetails" if no overlap
vcat      :: [Doc] -> Doc       -- List version of $$
($+$)     :: Doc -> Doc -> Doc  -- Above; never overlaps
cat       :: [Doc] -> Doc       -- Either hcat or vcat
sep       :: [Doc] -> Doc       -- Either hsep or vcat
fcat      :: [Doc] -> Doc       -- "Paragraph fill" version of cat
fsep      :: [Doc] -> Doc       -- "Paragraph fill" version of sep
nest      :: Int -> Doc -> Doc  -- Nested
hang      :: Doc -> Int -> Doc -> Doc
punctuate :: Doc -> [Doc] -> [Doc]      
-- punctuate p [d1, ... dn] = [d1 <> p, d2 <> p, ... dn-1 <> p, dn]

-- default rendering (normal mode, line length 100, 1.5 ribbons per line)
render :: Doc -> String         

-- general rendering of documents
fullRender ::
      Mode
   -> Int                       -- Line length
   -> Float                     -- Ribbons per line
   -> (TextDetails -> a -> a)   -- What to do with text
   -> a                         -- What to do at the end
   -> Doc                       -- The document to render
   -> a                         -- Result

data Mode =
     PageMode                   -- Normal
   | ZigZagMode                 -- With zig-zag cuts
   | LeftMode                   -- No indentation, infinitely long lines
   | OneLineMode                -- All on one line

data TextDetails =
     Chr  Char
   | Str  String
   | PStr String

-- predicate on documents
isEmpty :: Doc -> Bool