2 Copyright(C) 1999 Erik Meijer
8 Quick reference for the simple Pretty-print Combinators
10 |---| |----| |-------|
11 |koe| <|> |beer| = |koebeer|
12 |---| |----| |-------|
14 |---| |----| |--------|
15 |koe| <+> |beer| = |koe beer|
16 |---| |----| |--------|
19 |koe| <-> |beer| = |koe |
23 |---| |----| |-------|
24 |koe| <|> nest 2 |beer| = |koebeer|
25 |---| |----| |-------|
28 |koe| <-> nest 2 |beer| = |koe |
37 Extremely simplified version of John Hughes' combinators,
38 without (sep), but with (empty).
40 TODO: use Okasaki-style catenable dequeues to represent Doc
42 (c) Erik Meijer and Arjan van IJzendoorn
52 instance Show Doc where
53 { showsPrec = showsPrecDoc }
55 showsPrecDoc i = \d ->
65 layout :: [(Int,ShowS)] -> ShowS
69 ; [(n,s)] -> indent n.s
70 ; (n,s):ds -> indent n.s.showString "\n".layout ds
77 ; Doc ds -> maximum [ i + length (s "") | (i,s) <- ds ]
81 text = \s -> Doc [(0,showString s)]
83 nest :: Int -> Doc -> Doc
87 ; Doc ds -> Doc [ (i+n,d) | (i,d) <- ds ]
90 (<->) :: Doc -> Doc -> Doc
91 Empty <-> Empty = Empty
92 Empty <-> (Doc d2) = Doc d2
93 (Doc d1) <-> Empty = Doc d1
94 (Doc d1) <-> (Doc d2) = Doc (d1++d2)
96 (<+>) :: Doc -> Doc -> Doc
97 a <+> b = a <|> (text " ") <|> b
99 (<|>) :: Doc -> Doc -> Doc
100 Empty <|> Empty = Empty
101 Empty <|> (Doc d2) = Doc d2
102 (Doc d1) <|> Empty = Doc d1
103 (Doc d1) <|> (Doc d2) =
105 { (d,(i,s)) = (init d1,last d1)
106 ; ((j,t),e) = (head d2,tail d2)
111 <-> nest (i + length (s "") - j) (Doc e)
114 -- Derived operations
121 horizontal s [a,b,c] =
122 a <|> (s <|> b) <|> (s <|> c)
126 horizontal :: Doc -> [Doc] -> Doc
127 horizontal s = \ds ->
130 ; ds -> foldr1 (\d -> \ds -> d <|> s <|> ds) ds
144 vertical :: [Doc] -> Doc
148 ; d:ds -> d <-> vertical ds
151 block (o,s,c) = \ds ->
154 ; [d] -> o<|>d<|>c
\r ; d:ds -> (vertical ((o <|> d):[s <|> d | d <- ds ])) <-> c
159 indent :: Int -> ShowS
161 showString [ ' ' | i <- [1..n] ]