GHC API - list all exports of a module

Dear Cafe, currently I'm working on a small program for which I need a list of all *exported* function and data types. In order to achieve this I've been toying around with the GHC API. My observations so far: * Using the pure `parser` function yields a `HsModule` object, which gives me a list of all exports and all declarations (internal & exported). I cannot or don't know how to use the list of exports (just `RdrName`s) to filter the declarations. * Using `parseModule` (which is essentially the same as the `parser` function) and `typecheckModule` on a `ModuleGraph` yields a `TypecheckedModule` object which contains a list of exported `Name`s which in turn can be used to get additional information about them. So, the second approach seems more promising, but I'm not too happy with it. The reason is that calling `typecheckModule` results in a compilation of the module and all of its dependencies, which I think is not necessary for what I want to achieve. This is especially cumbersome for large modules like `GHC.hs` where I'm struggling to figure out all of the dependencies without the build system. My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them. I think that'd be helpful for upgrading dependant packages or for writing compatibility layers. Thank you and all the best, Jochen

Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes? Best regards, Peter

Hello Peter, On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good. One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case) Cheers, Jochen

On Mon, Feb 2, 2015 at 12:46 PM, Jochen Keil
Hello Peter,
On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good.
One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case)
There are basically two routes to choose from with such a tool (of which a few already exist, I think): you can use GHC and its API, like you're doing, or you can use haskell-src-exts, which is a separate parser, and use haskell-names for name resolution (and haskell-packages for dependency information). Sadly there's no haskell-type-exts for typechecking (yet). The GHC route is probably more powerful, the haskell-suite route is simpler and doesn't depend on GHC. Regards, Erik

Also, prior to GHC 7.10, there are serious gotchas to processing the GHC
API Parsed Source, as certain fields are initialised to a `panic "xxx"`
value, and should never be accessed.
Alan
On Mon, Feb 2, 2015 at 1:55 PM, Erik Hesselink
On Mon, Feb 2, 2015 at 12:46 PM, Jochen Keil
wrote: Hello Peter,
On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good.
One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case)
There are basically two routes to choose from with such a tool (of which a few already exist, I think): you can use GHC and its API, like you're doing, or you can use haskell-src-exts, which is a separate parser, and use haskell-names for name resolution (and haskell-packages for dependency information). Sadly there's no haskell-type-exts for typechecking (yet). The GHC route is probably more powerful, the haskell-suite route is simpler and doesn't depend on GHC.
Regards,
Erik _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Erik, On 02.02.2015 12:55, Erik Hesselink wrote:
On Mon, Feb 2, 2015 at 12:46 PM, Jochen Keil
wrote: Hello Peter,
On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good.
One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case)
There are basically two routes to choose from with such a tool (of which a few already exist, I think): you can use GHC and its API, like you're doing, or you can use haskell-src-exts, which is a separate parser, and use haskell-names for name resolution (and haskell-packages for dependency information). Sadly there's no haskell-type-exts for typechecking (yet). The GHC route is probably more powerful, the haskell-suite route is simpler and doesn't depend on GHC.
thanks for the clarification! Indeed it seems to me that the GHC approach is more powerful, because after running the typechecker I would have the AST available for comparing modules (did I get this right?). Drawback would obviously be the need for running the typechecker. :) So, just for the sake of my curiosity: Is there a way to get the exported declarations (and not just the RdrNames) from GHC without running the typechecker? I don't understand why it's necessary to run the typechecker in order to get this list. The parser already returns a list with all declarations, so shouldn't it be possible to filter this list for only the exported ones? Thanks, Jochen

Have you looked at ModuleInfo [1] ?
This is accesible from a TypecheckedModule.
And what do you mean by the exported declarations?
If you are prepared to use the HaRe API (ony up to GHC 7.6.3) you can use
definingDeclsNames [2]
[1]
https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/GHC.h...
[2]
http://hackage.haskell.org/package/HaRe-0.7.2.8/docs/Language-Haskell-Refact...
On Mon, Feb 2, 2015 at 6:58 PM, Jochen Keil
Hi Erik,
On 02.02.2015 12:55, Erik Hesselink wrote:
On Mon, Feb 2, 2015 at 12:46 PM, Jochen Keil
wrote: Hello Peter,
On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good.
One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case)
There are basically two routes to choose from with such a tool (of which a few already exist, I think): you can use GHC and its API, like you're doing, or you can use haskell-src-exts, which is a separate parser, and use haskell-names for name resolution (and haskell-packages for dependency information). Sadly there's no haskell-type-exts for typechecking (yet). The GHC route is probably more powerful, the haskell-suite route is simpler and doesn't depend on GHC.
thanks for the clarification! Indeed it seems to me that the GHC approach is more powerful, because after running the typechecker I would have the AST available for comparing modules (did I get this right?). Drawback would obviously be the need for running the typechecker. :)
So, just for the sake of my curiosity: Is there a way to get the exported declarations (and not just the RdrNames) from GHC without running the typechecker? I don't understand why it's necessary to run the typechecker in order to get this list. The parser already returns a list with all declarations, so shouldn't it be possible to filter this list for only the exported ones?
Thanks, Jochen
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi, On 02.02.2015 18:36, Alan & Kim Zimmerman wrote:
Have you looked at ModuleInfo [1] ?
Yes, that's the path I went after type checking the module. There's also ModIface which could be suitable for my purpose.
And what do you mean by the exported declarations?
I'm sorry if I didn't make my intent clear enough. There's the pure parser [1], which (at least I think so) is also used for parseModule [2]. This results in a HsModule object [3]. HsModule provides amongst others a field for exports [4] and *all* declarations [5]. I am explicitly only interested in *exported declarations*. But I don't see how I could combine [4] and [5] to achieve that, while avoiding to run the type checker. Sorry again if I was or still am unclear, but I hope it's better now. :) Cheers, Jochen [1] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/GHC.h... [2] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/GHC.h... [3] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/HsSyn... [4] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/HsSyn... [5] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/HsSyn...
If you are prepared to use the HaRe API (ony up to GHC 7.6.3) you can use definingDeclsNames [2]
[1] https://downloads.haskell.org/~ghc/7.8.3/docs/html/libraries/ghc-7.8.3/GHC.h... [2] http://hackage.haskell.org/package/HaRe-0.7.2.8/docs/Language-Haskell-Refact...
On Mon, Feb 2, 2015 at 6:58 PM, Jochen Keil
wrote: Hi Erik,
On 02.02.2015 12:55, Erik Hesselink wrote:
On Mon, Feb 2, 2015 at 12:46 PM, Jochen Keil
wrote: Hello Peter,
On 02.02.2015 12:31, Peter Simons wrote:
Hi Jochen,
My main goal would be to have a program which takes two modules of different versions, compare them and highlight the differences between them.
maybe http://hackage.haskell.org/package/hackage-diff would work for your purposes?
Thanks for the hint, this looks pretty good.
One thing though: hackage-diff uses haskell-src-exts for parsing modules. Does haskell-src-exts re-use the GHC API or does it run its own parser? (from a quick glance I'd say the latter case)
There are basically two routes to choose from with such a tool (of which a few already exist, I think): you can use GHC and its API, like you're doing, or you can use haskell-src-exts, which is a separate parser, and use haskell-names for name resolution (and haskell-packages for dependency information). Sadly there's no haskell-type-exts for typechecking (yet). The GHC route is probably more powerful, the haskell-suite route is simpler and doesn't depend on GHC.
thanks for the clarification! Indeed it seems to me that the GHC approach is more powerful, because after running the typechecker I would have the AST available for comparing modules (did I get this right?). Drawback would obviously be the need for running the typechecker. :)
So, just for the sake of my curiosity: Is there a way to get the exported declarations (and not just the RdrNames) from GHC without running the typechecker? I don't understand why it's necessary to run the typechecker in order to get this list. The parser already returns a list with all declarations, so shouldn't it be possible to filter this list for only the exported ones?
Thanks, Jochen
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Alan & Kim Zimmerman
-
Erik Hesselink
-
Jochen Keil
-
Peter Simons