Show-like output of GHC data types

Simon Peyton-Jones simonpj at microsoft.com
Fri Mar 8 10:39:02 CET 2013


What I would like to see is something like:

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


We could derive 'Show' on everything but that would generate a LOT of new code, that would seldom be used.  Also 'Show' doesn't pretty-print so the result would be illegible.

I'm a bit dubious.  Maybe a better thing might be to improve the existing pretty-printer so it gave you the cues you are looking for?

S

From: ghc-devs-bounces at haskell.org [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Johan Tibell
Sent: 08 March 2013 07:08
To: ghc-devs at haskell.org
Subject: Show-like output of GHC data types

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:

{{Test.hs:7:10-21}
 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<http://GHC.Types.Int>{(w) tc 3J}]
                         -> ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
                    [LclId]
    Binds: {Test.hs:8:1-26}
           mysum{v agP} [lid]
             :: [ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}]
                -> ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
           [LclId]
           mysum{v agP} [lid]
             ((xs{v agw} [lid] :: [ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}]))
             = {Test.hs:8:12-26}
               (base:Data.List.foldl'{v r4u}) @ ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
               @ ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
                 (((base:GHC.Num.+{v rt})) @ ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
                    $dNum{v agU} [lid])
                 0 ((base:GHC.Num.fromInteger{v 02A})
                    @ ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}
                      $dNum{v ahc} [lid]
                      0)
                 xs{v agw}
           <>
    Evidence: EvBinds{}}} {Test.hs:7:1-21}
                          main:Test.mysum{v rgu} ::
                            {Test.hs:7:10-21}
                            [ghc-prim:GHC.Types.Int<http://GHC.Types.Int>{(w) tc 3J}]
                            -> ghc-prim:GHC.Types.Int<http://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: <http://www.haskell.org/pipermail/ghc-devs/attachments/20130308/119acee9/attachment.htm>


More information about the ghc-devs mailing list