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{(w) tc 3J}] -> ghc-prim:GHC.Types.Int{(w) tc 3J} [LclId] 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} [LclId] 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 3J} $dNum{v agU} [lid]) 0 ((base:GHC.Num.fromInteger{v 02A}) @ ghc-prim: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{(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

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@haskell.org [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Johan Tibell Sent: 08 March 2013 07:08 To: ghc-devs@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.Inthttp://GHC.Types.Int{(w) tc 3J}] -> ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J} [LclId] Binds: {Test.hs:8:1-26} mysum{v agP} [lid] :: [ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J}] -> ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J} [LclId] mysum{v agP} [lid] ((xs{v agw} [lid] :: [ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J}])) = {Test.hs:8:12-26} (base:Data.List.foldl'{v r4u}) @ ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J} @ ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J} (((base:GHC.Num.+{v rt})) @ ghc-prim:GHC.Types.Inthttp://GHC.Types.Int{(w) tc 3J} $dNum{v agU} [lid]) 0 ((base:GHC.Num.fromInteger{v 02A}) @ ghc-prim:GHC.Types.Inthttp://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.Inthttp://GHC.Types.Int{(w) tc 3J}] -> ghc-prim:GHC.Types.Inthttp://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

Hello,
On Fri, Mar 8, 2013 at 1:39 AM, Simon Peyton-Jones
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?
Agreed that this is probably a better solution, although having derived `Show` instances would allow one to see exactly what data-structure was
I have a package called "pretty-show", which can convert standard `Show` instances into human-readable form, and it can also produce HTML renditions of `Show`-ed data structures. It is very useful for debugging or learning a new code base. produced at some point, which can be helpful when figuring out how some part of GHC works. -Iavor

On Fri, Mar 8, 2013 at 9:39 AM, Simon Peyton-Jones
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. ****
**
If plugins could be used before the conversion to Core, such a plugin might be a tidy way to workaround the issue of bloating GHC's binary size with all that Show code. This previous email thread http://www.haskell.org/pipermail/glasgow-haskell-users/2012-March/022076.htm... seems relevant, and Marlow's comment there makes it sound like a pretty easy change.

On Fri, Mar 8, 2013 at 8:26 AM, Nicolas Frisby
On Fri, Mar 8, 2013 at 9:39 AM, Simon Peyton-Jones
wrote: 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. ****
**
If plugins could be used before the conversion to Core, such a plugin might be a tidy way to workaround the issue of bloating GHC's binary size with all that Show code.
This previous email thread http://www.haskell.org/pipermail/glasgow-haskell-users/2012-March/022076.htm... seems relevant, and Marlow's comment there makes it sound like a pretty easy change.
I'm currently writing a ShowInstances module with orphan Show instances for all GHC data types. I will just import that during development, but not include it in the final code. It will hopefully satisfy my current need.

On Fri, Mar 8, 2013 at 5:53 PM, Johan Tibell wrote:
I'm currently writing a ShowInstances module with orphan Show instances for all GHC data types. I will just import that during development, but not include it in the final code. It will hopefully satisfy my current need.
This would have been useful to me in the past, and I'm guessing others would find it useful, too. It seems like a package of standalone derived Show instances would require little maintenance (since you don't need to modify the instances for changes to datatypes, only for adding/removing them) yet provide a great benefit for newcomers to GHC, while not bloating GHC itself. Regards, Sean

Last time I checked GHC's AST types had some fields which you weren't
supposed to look at. Depending on what stage you're in those may be
different fields. They are initialised to error thunks, hence
automatically derived Show instances wouldn't work out of the box as
they would force those error thunks.
As I said, it's been a while since I looked at that closely, so maybe
it was removed when the Typeable instances were moved into the GHC
codebase?
/ Thomas
On 8 March 2013 23:26, Sean Leather
On Fri, Mar 8, 2013 at 5:53 PM, Johan Tibell wrote:
I'm currently writing a ShowInstances module with orphan Show instances for all GHC data types. I will just import that during development, but not include it in the final code. It will hopefully satisfy my current need.
This would have been useful to me in the past, and I'm guessing others would find it useful, too. It seems like a package of standalone derived Show instances would require little maintenance (since you don't need to modify the instances for changes to datatypes, only for adding/removing them) yet provide a great benefit for newcomers to GHC, while not bloating GHC itself.
Regards, Sean
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

On Fri, Mar 8, 2013 at 3:01 PM, Thomas Schilling
Last time I checked GHC's AST types had some fields which you weren't supposed to look at. Depending on what stage you're in those may be different fields. They are initialised to error thunks, hence automatically derived Show instances wouldn't work out of the box as they would force those error thunks.
As I said, it's been a while since I looked at that closely, so maybe it was removed when the Typeable instances were moved into the GHC codebase?
I noticed that some of those are still around. I'm mainly looking at the AST post type-checking.
participants (6)
-
Iavor Diatchki
-
Johan Tibell
-
Nicolas Frisby
-
Sean Leather
-
Simon Peyton-Jones
-
Thomas Schilling