Pretty printer for Template Haskell

I use GHCi a lot, and want to look at the TH splices, and see how the results of my own TH, however, the default Show instance doesn’t do any indentaition. I know it is definitely possible to write the printer that would do what I want, with Data.Data, however is there already one defined?
E.G.
[d|instance Show (a -> b) where show _ = "Fun" |]
should give

[InstanceD 
    Nothing 
    [] 
    (AppT 
        (ConT GHC.Internal.Show.Show) 
        (AppT 
            (AppT 
                ArrowT 
                (VarT a_0))
            (VarT b_1))) 
    [FunD 
        GHC.Internal.Show.show 
        [Clause 
            [WildP]
            (NormalB 
                (LitE (StringL "Fun")))
            []]]]

I just want to get a printer like this, doesn’t need to be identitical.

TH comes with a pretty printer: pprint. It implements stuff from the pretty package so you can use the other typeclass methods too, but this is likely the function you want

This gives the desuagered haskell, not the AST. What it gives is essentially the same as the input I gave to the quasiquoter.

1 Like

Ah my bad! Maybe something like pretty-simple then, which just pretty prints the Show instance.

3 Likes