Show-like output of GHC data types

Johan Tibell johan.tibell at
Fri Mar 8 08:07:59 CET 2013

I'm trying to better understand how a GHC produced TypedcheckedSource is
composed. I have this little example:

module Test
    ( mysum
    ) where

import Data.List (foldl')

mysum :: [Int] -> Int
mysum xs = foldl' (+) 0 xs

Pretty-printing TypecheckedSource gives me something like this:

 AbsBinds [] []
   {Exports: [main:Test.mysum{v rgu} [lid] <= mysum{v agP} [lid]
    Exported types: main:Test.mysum{v rgu} [lid]
                      :: [ghc-prim:GHC.Types.Int{(w) tc 3J}]
                         -> ghc-prim:GHC.Types.Int{(w) tc 3J}
    Binds: {Test.hs:8:1-26}
           mysum{v agP} [lid]
             :: [ghc-prim:GHC.Types.Int{(w) tc 3J}]
                -> ghc-prim:GHC.Types.Int{(w) tc 3J}
           mysum{v agP} [lid]
             ((xs{v agw} [lid] :: [ghc-prim:GHC.Types.Int{(w) tc 3J}]))
             = {Test.hs:8:12-26}
               (base:Data.List.foldl'{v r4u}) @ ghc-prim:GHC.Types.Int{(w)
tc 3J}
               @ ghc-prim:GHC.Types.Int{(w) tc 3J}
                 (((base:GHC.Num.+{v rt})) @ ghc-prim:GHC.Types.Int{(w) tc
                    $dNum{v agU} [lid])
                 0 ((base:GHC.Num.fromInteger{v 02A})
                    @ ghc-prim:GHC.Types.Int{(w) tc 3J}
                      $dNum{v ahc} [lid]
                 xs{v agw}
    Evidence: EvBinds{}}} {Test.hs:7:1-21}
                          main:Test.mysum{v rgu} ::
                            [ghc-prim:GHC.Types.Int{(w) tc 3J}]
                            -> ghc-prim:GHC.Types.Int{(w) tc 3J}
                          nonrec {Test.hs:8:1-26}
                                 main:Test.mysum{v rgu}
                                 main:Test.mysum{v rgu} (xs{v agw})
                                   = {Test.hs:8:12-26}
                                     base:Data.List.foldl'{v r4u}
                                       (base:GHC.Num.+{v rt})
                                       0 (base:GHC.Num.fromInteger{v 02A})
                                       xs{v agw}

This doesn't really help me understand the AST any better, as this is
essentially the source printed back to me. What I would like to see is
something like:

HsApp (HsApp (HsApp (HsVar "foldl'") (HsVar "+")) (HsLit 0)) (HsVar "xs")

Is this possible today. If not, can we derive Show for all the GHC data
types so it's possible to print their structure?

P.S. An example of why the pretty-printed output is not very helpful in
understanding the AST: the above simple example generates a HsWrap
constructor, which is nowhere to be seen in the pretty-printed output.

-- Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list