Text.PrettyPrint.HughesPJ inserting blank lines
simonpj at microsoft.com
Thu Feb 3 04:06:36 EST 2005
I recall that making 'empty' a unit for <> and <+> and $$ was one of my
main goals in revising John Hughes's library. I recall also that it
got more complicated than I expected, and I didn't feel very satisfied
with the result, though it has worked pretty well for years.
If anyone wants to peer into the implementation and add combinators
along the lines of those below, that's fine with me. Vertical
operations should be easier to do than horizontal ones, because the
question of where to insert line breaks doesn't arise. Whether the code
below does the job, I couldn't say without looking in detail -- and I'm
hoping someone else will do that.
It's crucial to maintain the property that you can print an arbitrary
long document in bounded space.
It would be a great service to also add Haddock documentation, please!
| -----Original Message-----
| From: libraries-bounces at haskell.org
[mailto:libraries-bounces at haskell.org] On Behalf Of Christian
| Sent: 02 February 2005 12:58
| To: Tomasz Zielonka
| Cc: libraries at haskell.org
| Subject: Re: Text.PrettyPrint.HughesPJ inserting blank lines
| Tomasz Zielonka wrote:
| > This is ugly:
| > x $++$ y = (x <> text "") $+$ text "" $+$ (text "" <> y)
| > However, I don't know if it's OK that (show (empty $++$ empty) ==
| > "\n\n").
| I want "empty" to be a unit of $++$ as well, so that
| -- | list version of '($++$)'
| vsep :: [Doc] -> Doc
| vsep = foldr ($++$) empty
| works. Therefore I suggest:
| -- | vertical composition with a specified number of blank lines
| aboveWithNLs :: Int -> Doc -> Doc -> Doc
| aboveWithNLs n d1 d2 = if isEmpty d2 then d1 else
| if isEmpty d1 then d2 else
| d1 $+$ foldr ($+$) d2 (replicate n $ text "")
| -- | vertical composition with one blank line
| ($++$) :: Doc -> Doc -> Doc
| ($++$) = aboveWithNLs 1
| Cheers Christian
| Libraries mailing list
| Libraries at haskell.org
More information about the Libraries