| I'm using GHCi version 6.2.1 under Cygwin. I'm trying to implement a | compiler that translates a DSL into Haskell; I'd like to build the | structure incrementally and then use the pretty printer to output the | target code. However, the pretty printer does not always produce | valid output; sometimes needed parentheses are lacking, as in: | | Main> pprDec $ DataD [] "M" [] [NormalC "M" [(NotStrict, AppT (ConT Maybe) (ConT "Int"))]] [] | data M = M Maybe Int Good point. I've fixed this in the HEAD, and on the stable branch, so it'll be in 6.2.2 Prelude Language.Haskell.TH> ppr $ DataD [] (mkName "M") [] [NormalC (mkName "M") [(NotStrict, AppT (ConT ''Maybe) (ConT ''Int))]] [] data M = M (Data.Maybe.Maybe GHC.Base.Int) | Related question: I've defined a utility function | | > pdq decq = runQ decq >>= mapM_ print . map pprDec | | so I can pretty-print declarations in code brackets, viz.: | | Main> pdq [d| data M = M (Maybe Int) |] | | Is there an easy way to transform the declaration to use the | short forms of type names, e.g. "Int" instead of "GHC.Base:Int"? That's harder, because it's not clear what qualifications to omit. All? But then the program might be ambiguous. I guess this is so you can feed it back into GHC, right? You can always import GHC.Base, and then you'll be fine. Still, one could imagine a flag to pprDec (or, more generally, to ppr) that, say, printed all top-level names unqualified. But that'd require a bit of re-plumbing in the pretty-printer to get the flag to the right place. Simon
On Wed, 2004-05-19 at 08:44, Simon Peyton-Jones wrote:
| Is there an easy way to transform the declaration to use the | short forms of type names, e.g. "Int" instead of "GHC.Base:Int"?
That's harder, because it's not clear what qualifications to omit. All? But then the program might be ambiguous. I guess this is so you can feed it back into GHC, right? You can always import GHC.Base, and then you'll be fine.
How about a pretty printer that is given a module context so is can print the least-qualified identifiers possible given the identifiers in scope in that module. I guess this is what ghc does when it prints its error messages. eg (in the Q monad): module <- currentModule pprExpInModuleContext module Then not only will the code be valid to feed back to the compiler, but it'll also be readable! (I've been looking at too much machine generated code recently :-) ) Duncan
participants (2)
-
Duncan Coutts -
Simon Peyton-Jones