Injecting imported functions using a core plugin

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

Injecting imported functions using a core plugin

Josef Svenningsson-3

Hi ghc-devs,

 

I’m currently writing a core plugin that I could use some help with.

Consider the following two modules:

 

```

module A where

 

foo :: Int

bar :: Int

 

module B where

 

baz :: Int

baz = bar

```

 

When compiling module B I run my plugin. The goal of the plugin is to replace the occurrence of `bar` with `foo`. Note that we can be sure that `foo` is actually imported, but unfortunately doesn’t occur anywhere in B before the plugin performs the transformation.

 

The problem I have is that in order to inject `foo` in B I need to have an `Id` which represents `foo` and I’m having some trouble constructing such an `Id`. I’ve looked through the various environments that are available during the core to core transformations but none of them provides enough information to actually produce the `foo` `Id` as far as I can see. I hope I’m missing something. What do I need to do in order to construct the `foo` `Id` in module B?

 

Thanks,

 

Josef

 

PS. The way I’ve phrased my problem in this email it would be possible to solve it with rewrite rules. My actual use case is unfortunately more complicated and rewrite rules don’t provide enough power to do what I want.


_______________________________________________
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: Injecting imported functions using a core plugin

Facundo Domínguez-3
Hello Josef,

Do you know the location of foo when building the plugin? Otherwise, how is the plugin supposed to learn where it comes from?

Facundo

On Tue, Nov 26, 2019 at 8:49 AM Josef Svenningsson <[hidden email]> wrote:

Hi ghc-devs,

 

I’m currently writing a core plugin that I could use some help with.

Consider the following two modules:

 

```

module A where

 

foo :: Int

bar :: Int

 

module B where

 

baz :: Int

baz = bar

```

 

When compiling module B I run my plugin. The goal of the plugin is to replace the occurrence of `bar` with `foo`. Note that we can be sure that `foo` is actually imported, but unfortunately doesn’t occur anywhere in B before the plugin performs the transformation.

 

The problem I have is that in order to inject `foo` in B I need to have an `Id` which represents `foo` and I’m having some trouble constructing such an `Id`. I’ve looked through the various environments that are available during the core to core transformations but none of them provides enough information to actually produce the `foo` `Id` as far as I can see. I hope I’m missing something. What do I need to do in order to construct the `foo` `Id` in module B?

 

Thanks,

 

Josef

 

PS. The way I’ve phrased my problem in this email it would be possible to solve it with rewrite rules. My actual use case is unfortunately more complicated and rewrite rules don’t provide enough power to do what I want.

_______________________________________________
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: Injecting imported functions using a core plugin

Josef Svenningsson-3

Yes, the plugin is fully aware of module A in my example.

 

Thanks,

 

Josef

 

From: "Domínguez, Facundo" <[hidden email]>
Date: Tuesday, November 26, 2019 at 11:56 AM
To: Josef Svenningsson <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Subject: Re: Injecting imported functions using a core plugin

 

Hello Josef,

 

Do you know the location of foo when building the plugin? Otherwise, how is the plugin supposed to learn where it comes from?

 

Facundo

 

On Tue, Nov 26, 2019 at 8:49 AM Josef Svenningsson <[hidden email]> wrote:

Hi ghc-devs,

 

I’m currently writing a core plugin that I could use some help with.

Consider the following two modules:

 

```

module A where

 

foo :: Int

bar :: Int

 

module B where

 

baz :: Int

baz = bar

```

 

When compiling module B I run my plugin. The goal of the plugin is to replace the occurrence of `bar` with `foo`. Note that we can be sure that `foo` is actually imported, but unfortunately doesn’t occur anywhere in B before the plugin performs the transformation.

 

The problem I have is that in order to inject `foo` in B I need to have an `Id` which represents `foo` and I’m having some trouble constructing such an `Id`. I’ve looked through the various environments that are available during the core to core transformations but none of them provides enough information to actually produce the `foo` `Id` as far as I can see. I hope I’m missing something. What do I need to do in order to construct the `foo` `Id` in module B?

 

Thanks,

 

Josef

 

PS. The way I’ve phrased my problem in this email it would be possible to solve it with rewrite rules. My actual use case is unfortunately more complicated and rewrite rules don’t provide enough power to do what I want.

_______________________________________________
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: Injecting imported functions using a core plugin

Matthew Pickering
You can use `thNameToGhcName` to turn a quoted name ('foo) into a GHC
Name and and then use `lookupId` in order to get the `Id` for that
Name.

Cheers,

Matt

On Tue, Nov 26, 2019 at 12:55 PM Josef Svenningsson <[hidden email]> wrote:

>
> Yes, the plugin is fully aware of module A in my example.
>
>
>
> Thanks,
>
>
>
> Josef
>
>
>
> From: "Domínguez, Facundo" <[hidden email]>
> Date: Tuesday, November 26, 2019 at 11:56 AM
> To: Josef Svenningsson <[hidden email]>
> Cc: "[hidden email]" <[hidden email]>
> Subject: Re: Injecting imported functions using a core plugin
>
>
>
> Hello Josef,
>
>
>
> Do you know the location of foo when building the plugin? Otherwise, how is the plugin supposed to learn where it comes from?
>
>
>
> Facundo
>
>
>
> On Tue, Nov 26, 2019 at 8:49 AM Josef Svenningsson <[hidden email]> wrote:
>
> Hi ghc-devs,
>
>
>
> I’m currently writing a core plugin that I could use some help with.
>
> Consider the following two modules:
>
>
>
> ```
>
> module A where
>
>
>
> foo :: Int
>
> bar :: Int
>
>
>
> module B where
>
>
>
> baz :: Int
>
> baz = bar
>
> ```
>
>
>
> When compiling module B I run my plugin. The goal of the plugin is to replace the occurrence of `bar` with `foo`. Note that we can be sure that `foo` is actually imported, but unfortunately doesn’t occur anywhere in B before the plugin performs the transformation.
>
>
>
> The problem I have is that in order to inject `foo` in B I need to have an `Id` which represents `foo` and I’m having some trouble constructing such an `Id`. I’ve looked through the various environments that are available during the core to core transformations but none of them provides enough information to actually produce the `foo` `Id` as far as I can see. I hope I’m missing something. What do I need to do in order to construct the `foo` `Id` in module B?
>
>
>
> Thanks,
>
>
>
> Josef
>
>
>
> PS. The way I’ve phrased my problem in this email it would be possible to solve it with rewrite rules. My actual use case is unfortunately more complicated and rewrite rules don’t provide enough power to do what I want.
>
> _______________________________________________
> 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
_______________________________________________
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: Injecting imported functions using a core plugin

Josef Svenningsson-3
That works splendidly! Neat!

Thanks,

Josef

On 11/26/19, 1:37 PM, "Matthew Pickering" <[hidden email]> wrote:

    You can use `thNameToGhcName` to turn a quoted name ('foo) into a GHC
    Name and and then use `lookupId` in order to get the `Id` for that
    Name.
   
    Cheers,
   
    Matt
   
    On Tue, Nov 26, 2019 at 12:55 PM Josef Svenningsson <[hidden email]> wrote:
    >
    > Yes, the plugin is fully aware of module A in my example.
    >
    >
    >
    > Thanks,
    >
    >
    >
    > Josef
    >
    >
    >
    > From: "Domínguez, Facundo" <[hidden email]>
    > Date: Tuesday, November 26, 2019 at 11:56 AM
    > To: Josef Svenningsson <[hidden email]>
    > Cc: "[hidden email]" <[hidden email]>
    > Subject: Re: Injecting imported functions using a core plugin
    >
    >
    >
    > Hello Josef,
    >
    >
    >
    > Do you know the location of foo when building the plugin? Otherwise, how is the plugin supposed to learn where it comes from?
    >
    >
    >
    > Facundo
    >
    >
    >
    > On Tue, Nov 26, 2019 at 8:49 AM Josef Svenningsson <[hidden email]> wrote:
    >
    > Hi ghc-devs,
    >
    >
    >
    > I’m currently writing a core plugin that I could use some help with.
    >
    > Consider the following two modules:
    >
    >
    >
    > ```
    >
    > module A where
    >
    >
    >
    > foo :: Int
    >
    > bar :: Int
    >
    >
    >
    > module B where
    >
    >
    >
    > baz :: Int
    >
    > baz = bar
    >
    > ```
    >
    >
    >
    > When compiling module B I run my plugin. The goal of the plugin is to replace the occurrence of `bar` with `foo`. Note that we can be sure that `foo` is actually imported, but unfortunately doesn’t occur anywhere in B before the plugin performs the transformation.
    >
    >
    >
    > The problem I have is that in order to inject `foo` in B I need to have an `Id` which represents `foo` and I’m having some trouble constructing such an `Id`. I’ve looked through the various environments that are available during the core to core transformations but none of them provides enough information to actually produce the `foo` `Id` as far as I can see. I hope I’m missing something. What do I need to do in order to construct the `foo` `Id` in module B?
    >
    >
    >
    > Thanks,
    >
    >
    >
    > Josef
    >
    >
    >
    > PS. The way I’ve phrased my problem in this email it would be possible to solve it with rewrite rules. My actual use case is unfortunately more complicated and rewrite rules don’t provide enough power to do what I want.
    >
    > _______________________________________________
    > ghc-devs mailing list
    > [hidden email]
    > https://urldefense.proofpoint.com/v2/url?u=http-3A__mail.haskell.org_cgi-2Dbin_mailman_listinfo_ghc-2Ddevs&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=APxRu21VjqVNNweqFCcqEg&m=dCV829NFCGL4-CrhlI0o5o3t7wME9DVjxJ6993GLXp0&s=dpLaNzy0ViowB29tAS4s0ljm_UczJJoxLkgJFhOuRf4&e= 
    >
    > _______________________________________________
    > ghc-devs mailing list
    > [hidden email]
    > https://urldefense.proofpoint.com/v2/url?u=http-3A__mail.haskell.org_cgi-2Dbin_mailman_listinfo_ghc-2Ddevs&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=APxRu21VjqVNNweqFCcqEg&m=dCV829NFCGL4-CrhlI0o5o3t7wME9DVjxJ6993GLXp0&s=dpLaNzy0ViowB29tAS4s0ljm_UczJJoxLkgJFhOuRf4&e= 
   

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