
I wrote a parsec parser that does symbols lookups during the parsing process (ParsecT String Store IO a). Now I'd like to write a pretty printer that does the reverse. Unfortunately there doesn't appear to be a transformer version of Text.PrettyPrint.HughesPJ. Can anyone suggest a way to do this? Thanks, Warren

Hi Warren,
On Thu, Apr 12, 2012 at 6:31 PM, Warren Harris
I wrote a parsec parser that does symbols lookups during the parsing process (ParsecT String Store IO a). Now I'd like to write a pretty printer that does the reverse. Unfortunately there doesn't appear to be a transformer version of Text.PrettyPrint.HughesPJ. Can anyone suggest a way to do this? Thanks,
It seems like the opposite would be a function of type 'a -> Store -> IO Doc'. Maybe a function of type 'a -> ReaderT Store IO Doc' could be easier to work with. If you go this route you could write a lifted versions of (<>), (<+>), hcat etc. An example: (<>) :: Applicative m => m Doc -> m Doc -> m Doc I haven't tried any of this, so I'm not sure if you would get any big win over just using the first suggestion (a function of type 'a -> Store -> IO Doc') and using the stock combinators and threading the store around by hand. But do let me know if something works out. Antoine

On Thu, Apr 12, 2012 at 6:22 PM, Antoine Latter
Hi Warren,
On Thu, Apr 12, 2012 at 6:31 PM, Warren Harris
wrote: I wrote a parsec parser that does symbols lookups during the parsing process (ParsecT String Store IO a). Now I'd like to write a pretty printer that does the reverse. Unfortunately there doesn't appear to be a transformer version of Text.PrettyPrint.HughesPJ. Can anyone suggest a way to do this? Thanks,
It seems like the opposite would be a function of type 'a -> Store -> IO Doc'.
Maybe a function of type 'a -> ReaderT Store IO Doc' could be easier to work with.
If you go this route you could write a lifted versions of (<>), (<+>), hcat etc.
An example:
(<>) :: Applicative m => m Doc -> m Doc -> m Doc
On this note, there are various Wadler-Leijin pretty printers variants on hackage, some lifted into a monad so you can do effects, IO, or whatever. E.g. wl-pprint-extras. In my experience though WL gives worse results than H-PJ despite having some more powerful operators.

Antoine, Thanks for the suggestions. No sooner did I send my message than I came to the same conclusion of creating a monadic version of the combinators to simplify the migration. It actually worked out fairly well -- most of the code ported over to the monadic version unaltered. The only exception is with literal lists used by combinators such as 'sep', e.g. sep [...]. This has to become sep $ sequence [...] in order to convert the argument into the expected monad. I put my code up on github https://github.com/warrenharris/pretty/blob/master/src/Text/PrettyPrint/Read.... If you can take a look, I'd appreciate your suggestions. Warren On Apr 12, 2012, at 6:22 PM, Antoine Latter wrote:
Hi Warren,
On Thu, Apr 12, 2012 at 6:31 PM, Warren Harris
wrote: I wrote a parsec parser that does symbols lookups during the parsing process (ParsecT String Store IO a). Now I'd like to write a pretty printer that does the reverse. Unfortunately there doesn't appear to be a transformer version of Text.PrettyPrint.HughesPJ. Can anyone suggest a way to do this? Thanks,
It seems like the opposite would be a function of type 'a -> Store -> IO Doc'.
Maybe a function of type 'a -> ReaderT Store IO Doc' could be easier to work with.
If you go this route you could write a lifted versions of (<>), (<+>), hcat etc.
An example:
(<>) :: Applicative m => m Doc -> m Doc -> m Doc
I haven't tried any of this, so I'm not sure if you would get any big win over just using the first suggestion (a function of type 'a -> Store -> IO Doc') and using the stock combinators and threading the store around by hand.
But do let me know if something works out.
Antoine

Haskell-src-exts has had a monadic wrapper over HughesPJ for a long
time. It is just a Reader (not a transformer) so it can handle user
supplied spacing widths, etc.
On 13 April 2012 06:02, Warren Harris
Thanks for the suggestions. No sooner did I send my message than I came to the same conclusion of creating a monadic version of the combinators to simplify the migration.
participants (4)
-
Antoine Latter
-
Evan Laforge
-
Stephen Tetley
-
Warren Harris