GHC API question: resolving dependencies for modules

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

GHC API question: resolving dependencies for modules

Dmitriy Kovanikov
Hello!

I’m trying to use GHC as a library. And my goal is to be able to gather information about where each function or data type came from. I’ve started by simply calling `getNamesInScope` function and observing its result. Here is my code:


And here is the code for my test modules:


Unfortunately, my implementation doesn't work since I’m not very familiar with GHC API. 
And I see the following errors after executing my `Main.hs` file (I’m using ghc-8.2.2):


Could you please point me to places or parts of GHC API or some documentation about module dependencies and how to make ghc see imports of other modules? I can’t find simple and small enough usage example of this part of the library.

Thanks in advance,
Dmitrii Kovanikov

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: GHC API question: resolving dependencies for modules

Matthew Pickering
This doesn't answer your question directly but if you want to gather
information about a module then using a source plugin would probably
be easier and more robust than using the GHC API.

You need to write a function of type:

```
ModSummary -> TcGblEnv -> TcM TcGblEnv
```

In `TcGblEnv` you will find `tcg_rdr_env` which contains all top-level
things and describes how they came to be in scope.

Source plugins will be in GHC 8.6.

Cheers,

Matt


On Thu, Jun 21, 2018 at 10:06 AM, Dmitriy Kovanikov <[hidden email]> wrote:

> Hello!
>
> I’m trying to use GHC as a library. And my goal is to be able to gather
> information about where each function or data type came from. I’ve started
> by simply calling `getNamesInScope` function and observing its result. Here
> is my code:
>
> * Main.hs: https://lpaste.net/9026688686753841152
>
> And here is the code for my test modules:
>
> * test/X.hs: https://lpaste.net/6844657232357883904
> * test/Y.hs: https://lpaste.net/8673289058127970304
>
> Unfortunately, my implementation doesn't work since I’m not very familiar
> with GHC API.
> And I see the following errors after executing my `Main.hs` file (I’m using
> ghc-8.2.2):
>
> * error messages: https://lpaste.net/3316737208131518464
>
> Could you please point me to places or parts of GHC API or some
> documentation about module dependencies and how to make ghc see imports of
> other modules? I can’t find simple and small enough usage example of this
> part of the library.
>
> Thanks in advance,
> Dmitrii Kovanikov
>
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: GHC API question: resolving dependencies for modules

Dmitriy Kovanikov
Thanks a lot for your suggestion! I’ve looked into GHC source plugins. 
And, specifically, into your `hashtag-coerce` example:


As far as I can see, this allows only to analyse source code (to produce warnings or errors).
While my actual goal is actually to refactor code automatically. And I would
like to avoid full compilation process to make it work faster.

Thanks,
Dmitrii

On 21 Jun 2018, at 5:51 PM, Matthew Pickering <[hidden email]> wrote:

This doesn't answer your question directly but if you want to gather
information about a module then using a source plugin would probably
be easier and more robust than using the GHC API.

You need to write a function of type:

```
ModSummary -> TcGblEnv -> TcM TcGblEnv
```

In `TcGblEnv` you will find `tcg_rdr_env` which contains all top-level
things and describes how they came to be in scope.

Source plugins will be in GHC 8.6.

Cheers,

Matt


On Thu, Jun 21, 2018 at 10:06 AM, Dmitriy Kovanikov <[hidden email]> wrote:
Hello!

I’m trying to use GHC as a library. And my goal is to be able to gather
information about where each function or data type came from. I’ve started
by simply calling `getNamesInScope` function and observing its result. Here
is my code:

* Main.hs: https://lpaste.net/9026688686753841152

And here is the code for my test modules:

* test/X.hs: https://lpaste.net/6844657232357883904
* test/Y.hs: https://lpaste.net/8673289058127970304

Unfortunately, my implementation doesn't work since I’m not very familiar
with GHC API.
And I see the following errors after executing my `Main.hs` file (I’m using
ghc-8.2.2):

* error messages: https://lpaste.net/3316737208131518464

Could you please point me to places or parts of GHC API or some
documentation about module dependencies and how to make ghc see imports of
other modules? I can’t find simple and small enough usage example of this
part of the library.

Thanks in advance,
Dmitrii Kovanikov

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs



_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: GHC API question: resolving dependencies for modules

Ben Gamari-3
Dmitriy Kovanikov <[hidden email]> writes:

> Thanks a lot for your suggestion! I’ve looked into GHC source plugins.
> And, specifically, into your `hashtag-coerce` example:
>
> * https://github.com/mpickering/hashtag-coerce <https://github.com/mpickering/hashtag-coerce>
>
> As far as I can see, this allows only to analyse source code (to
> produce warnings or errors).

It depends upon which type of source plugin you are using. There are a
few points in the compilation pipeline that source plugins allow you to
plug in to. Some of these allow the plugin to modify the AST while
others only allow inspection. See the users guide [1] for details.

Cheers,

- Ben


[1] https://github.com/ghc/ghc/blob/master/docs/users_guide/extending_ghc.rst#source-plugins

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (497 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GHC API question: resolving dependencies for modules

Matthew Pickering
In reply to this post by Dmitriy Kovanikov
I'm sorry of my poor form for not really answering your question here
but it's because no one really knows how to use the GHC API.

If you are stuck on this path then you could look at how GHC uses the
API or an existing user of the API like haddock or haskell-indexer.

However, these solutions are not going to be as robust as using a
plugin and you will end up with writing a lot of code probably in
order to get it to work.

Cheers,

Matt

On Mon, Jun 25, 2018 at 9:34 AM, Dmitriy Kovanikov <[hidden email]> wrote:

> Thanks a lot for your suggestion! I’ve looked into GHC source plugins.
> And, specifically, into your `hashtag-coerce` example:
>
> * https://github.com/mpickering/hashtag-coerce
>
> As far as I can see, this allows only to analyse source code (to produce
> warnings or errors).
> While my actual goal is actually to refactor code automatically. And I would
> like to avoid full compilation process to make it work faster.
>
> Thanks,
> Dmitrii
>
>
> On 21 Jun 2018, at 5:51 PM, Matthew Pickering <[hidden email]>
> wrote:
>
> This doesn't answer your question directly but if you want to gather
> information about a module then using a source plugin would probably
> be easier and more robust than using the GHC API.
>
> You need to write a function of type:
>
> ```
> ModSummary -> TcGblEnv -> TcM TcGblEnv
> ```
>
> In `TcGblEnv` you will find `tcg_rdr_env` which contains all top-level
> things and describes how they came to be in scope.
>
> Source plugins will be in GHC 8.6.
>
> Cheers,
>
> Matt
>
>
> On Thu, Jun 21, 2018 at 10:06 AM, Dmitriy Kovanikov <[hidden email]>
> wrote:
>
> Hello!
>
> I’m trying to use GHC as a library. And my goal is to be able to gather
> information about where each function or data type came from. I’ve started
> by simply calling `getNamesInScope` function and observing its result. Here
> is my code:
>
> * Main.hs: https://lpaste.net/9026688686753841152
>
> And here is the code for my test modules:
>
> * test/X.hs: https://lpaste.net/6844657232357883904
> * test/Y.hs: https://lpaste.net/8673289058127970304
>
> Unfortunately, my implementation doesn't work since I’m not very familiar
> with GHC API.
> And I see the following errors after executing my `Main.hs` file (I’m using
> ghc-8.2.2):
>
> * error messages: https://lpaste.net/3316737208131518464
>
> Could you please point me to places or parts of GHC API or some
> documentation about module dependencies and how to make ghc see imports of
> other modules? I can’t find simple and small enough usage example of this
> part of the library.
>
> Thanks in advance,
> Dmitrii Kovanikov
>
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
>
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

RE: GHC API question: resolving dependencies for modules

GHC - devs mailing list
|  I'm sorry of my poor form for not really answering your question here but
|  it's because no one really knows how to use the GHC API.

That may be true, but it's alarming if true.

The GHC API has grown rather than being consciously and carefully designed.  It would be Really Good if someone cares about using the API would like to write down the API they'd *like*, and agree it with others.  Then we could implement it!

Simon

|  -----Original Message-----
|  From: ghc-devs <[hidden email]> On Behalf Of Matthew Pickering
|  Sent: 25 June 2018 22:09
|  To: Dmitriy Kovanikov <[hidden email]>
|  Cc: GHC developers <[hidden email]>
|  Subject: Re: GHC API question: resolving dependencies for modules
|  
|  If you are stuck on this path then you could look at how GHC uses the API or
|  an existing user of the API like haddock or haskell-indexer.
|  
|  However, these solutions are not going to be as robust as using a plugin and
|  you will end up with writing a lot of code probably in order to get it to
|  work.
|  
|  Cheers,
|  
|  Matt
|  
|  On Mon, Jun 25, 2018 at 9:34 AM, Dmitriy Kovanikov <[hidden email]>
|  wrote:
|  > Thanks a lot for your suggestion! I’ve looked into GHC source plugins.
|  > And, specifically, into your `hashtag-coerce` example:
|  >
|  > *
|  > https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithu
|  > b.com%2Fmpickering%2Fhashtag-coerce&amp;data=02%7C01%7Csimonpj%40micro
|  > soft.com%7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988bf86f141af91ab2d7c
|  > d011db47%7C1%7C0%7C636655577449540446&amp;sdata=sC4G%2BjqdgA9%2B%2FJrl
|  > PJ10ZNls%2FTsmLc16kx8YGpAKkqI%3D&amp;reserved=0
|  >
|  > As far as I can see, this allows only to analyse source code (to
|  > produce warnings or errors).
|  > While my actual goal is actually to refactor code automatically. And I
|  > would like to avoid full compilation process to make it work faster.
|  >
|  > Thanks,
|  > Dmitrii
|  >
|  >
|  > On 21 Jun 2018, at 5:51 PM, Matthew Pickering
|  > <[hidden email]>
|  > wrote:
|  >
|  > This doesn't answer your question directly but if you want to gather
|  > information about a module then using a source plugin would probably
|  > be easier and more robust than using the GHC API.
|  >
|  > You need to write a function of type:
|  >
|  > ```
|  > ModSummary -> TcGblEnv -> TcM TcGblEnv ```
|  >
|  > In `TcGblEnv` you will find `tcg_rdr_env` which contains all top-level
|  > things and describes how they came to be in scope.
|  >
|  > Source plugins will be in GHC 8.6.
|  >
|  > Cheers,
|  >
|  > Matt
|  >
|  >
|  > On Thu, Jun 21, 2018 at 10:06 AM, Dmitriy Kovanikov
|  > <[hidden email]>
|  > wrote:
|  >
|  > Hello!
|  >
|  > I’m trying to use GHC as a library. And my goal is to be able to
|  > gather information about where each function or data type came from.
|  > I’ve started by simply calling `getNamesInScope` function and
|  > observing its result. Here is my code:
|  >
|  > * Main.hs:
|  > https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flpast
|  > e.net%2F9026688686753841152&amp;data=02%7C01%7Csimonpj%40microsoft.com
|  > %7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988bf86f141af91ab2d7cd011db47
|  > %7C1%7C0%7C636655577449540446&amp;sdata=gRr1Ze2i4NRXqOtlwqoI1mqEEv4ux2
|  > oZs5ZbA1O1938%3D&amp;reserved=0
|  >
|  > And here is the code for my test modules:
|  >
|  > * test/X.hs:
|  > https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flpast
|  > e.net%2F6844657232357883904&amp;data=02%7C01%7Csimonpj%40microsoft.com
|  > %7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988bf86f141af91ab2d7cd011db47
|  > %7C1%7C0%7C636655577449540446&amp;sdata=QRMPG6I18wg9x7JQ6q2SpfQlUD1ag%
|  > 2Binofx3ZPj0TWM%3D&amp;reserved=0
|  > * test/Y.hs:
|  > https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flpast
|  > e.net%2F8673289058127970304&amp;data=02%7C01%7Csimonpj%40microsoft.com
|  > %7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988bf86f141af91ab2d7cd011db47
|  > %7C1%7C0%7C636655577449540446&amp;sdata=35E0iM%2BITqeE4SRWlt9czJkkvzsg
|  > JCixnRFvV6YLnO0%3D&amp;reserved=0
|  >
|  > Unfortunately, my implementation doesn't work since I’m not very
|  > familiar with GHC API.
|  > And I see the following errors after executing my `Main.hs` file (I’m
|  > using
|  > ghc-8.2.2):
|  >
|  > * error messages:
|  > https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flpast
|  > e.net%2F3316737208131518464&amp;data=02%7C01%7Csimonpj%40microsoft.com
|  > %7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988bf86f141af91ab2d7cd011db47
|  > %7C1%7C0%7C636655577449540446&amp;sdata=yF1UAiQbLOYPrmIKFpA4b2g5ooI%2B
|  > YBbMvNcRhOGH26A%3D&amp;reserved=0
|  >
|  > Could you please point me to places or parts of GHC API or some
|  > documentation about module dependencies and how to make ghc see
|  > imports of other modules? I can’t find simple and small enough usage
|  > example of this part of the library.
|  >
|  > Thanks in advance,
|  > Dmitrii Kovanikov
|  >
|  > _______________________________________________
|  > ghc-devs mailing list
|  > [hidden email]
|  > https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.h
|  > askell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-devs&amp;data=02%7C01%
|  > 7Csimonpj%40microsoft.com%7Cdf4bf5c9bffc4815f65c08d5dadfe0d8%7C72f988b
|  > f86f141af91ab2d7cd011db47%7C1%7C0%7C636655577449540446&amp;sdata=xa7Kq
|  > Meknxs6ru%2BN%2FO%2BwWaubGEWGyumWc5VY%2FYZrZxg%3D&amp;reserved=0
|  >
|  >
|  _______________________________________________
|  ghc-devs mailing list
|  [hidden email]
|  https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.haskell
|  .org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-
|  devs&amp;data=02%7C01%7Csimonpj%40microsoft.com%7Cdf4bf5c9bffc4815f65c08d5da
|  dfe0d8%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636655577449540446&amp;s
|  data=xa7KqMeknxs6ru%2BN%2FO%2BwWaubGEWGyumWc5VY%2FYZrZxg%3D&amp;reserved=0
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs