GHC API: type class instance check

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

GHC API: type class instance check

J. Stutterheim
Hi all,

I'm writing a core-to-core GHC plugin and have two questions:

- Given some t :: Type, how can I check whether there exists an instance of some type class C for t?
- How can I check whether some type class C exists, and if so, how can I get a value c (presumably :: Class) of this class?

Thanks!

- Jurriën
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GHC API: type class instance check

migmit-2
I'm no expert in GHC plugins, but isn't it true that class instances can be added in other modules? So that this information is simply not available when compiling one?

> On 11 Nov 2016, at 08:57, J. Stutterheim <[hidden email]> wrote:
>
> Hi all,
>
> I'm writing a core-to-core GHC plugin and have two questions:
>
> - Given some t :: Type, how can I check whether there exists an instance of some type class C for t?
> - How can I check whether some type class C exists, and if so, how can I get a value c (presumably :: Class) of this class?
>
> Thanks!
>
> - Jurriën_______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: GHC API: type class instance check

J. Stutterheim
Yes, that's true, but suppose we have

        module M where

        import OtherModPossiblyWithInstance

Then as far as I understand, the compiler will have processed OtherModPossiblyWithInstance already and the existence of class instances in OtherModPossiblyWithInstance should be know.

> On 11 Nov 2016, at 10:07, MigMit <[hidden email]> wrote:
>
> I'm no expert in GHC plugins, but isn't it true that class instances can be added in other modules? So that this information is simply not available when compiling one?
>
>> On 11 Nov 2016, at 08:57, J. Stutterheim <[hidden email]> wrote:
>>
>> Hi all,
>>
>> I'm writing a core-to-core GHC plugin and have two questions:
>>
>> - Given some t :: Type, how can I check whether there exists an instance of some type class C for t?
>> - How can I check whether some type class C exists, and if so, how can I get a value c (presumably :: Class) of this class?
>>
>> Thanks!
>>
>> - Jurriën_______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GHC API: type class instance check

migmit-2
True, but what if it's the other way around? What if OtherModule imports M? Your code in M would think there is no instance and behave accordingly, and when it comes to OtherModule there would be one, so, your compiled code would operate on incorrect assumption. Isn't it dangerous?

And what about instances that have context? What if module M contains something like

data T a = ...
instance OtherClass a => C (T a)

and later OtherModule adds

instance OtherClass a

?

On Fri, Nov 11, 2016 at 10:55 AM, J. Stutterheim <[hidden email]> wrote:
Yes, that's true, but suppose we have

        module M where

        import OtherModPossiblyWithInstance

Then as far as I understand, the compiler will have processed OtherModPossiblyWithInstance already and the existence of class instances in OtherModPossiblyWithInstance should be know.

> On 11 Nov 2016, at 10:07, MigMit <[hidden email]> wrote:
>
> I'm no expert in GHC plugins, but isn't it true that class instances can be added in other modules? So that this information is simply not available when compiling one?
>
>> On 11 Nov 2016, at 08:57, J. Stutterheim <[hidden email]> wrote:
>>
>> Hi all,
>>
>> I'm writing a core-to-core GHC plugin and have two questions:
>>
>> - Given some t :: Type, how can I check whether there exists an instance of some type class C for t?
>> - How can I check whether some type class C exists, and if so, how can I get a value c (presumably :: Class) of this class?
>>
>> Thanks!
>>
>> - Jurriën_______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: GHC API: type class instance check

J. Stutterheim
For my purposes, I only need to do something when an instance is explicitly imported, e.g.

        module M where

        import ModWithInstances

Then in the core pass I would have a check for each module such as:

        if instanceIsImported theInstanceThatISomehowNeedToIdentify
          then doSomeThingsToTheModule
          else leaveTheModuleAlone



> On 11 Nov 2016, at 11:00, Miguel <[hidden email]> wrote:
>
> True, but what if it's the other way around? What if OtherModule imports M? Your code in M would think there is no instance and behave accordingly, and when it comes to OtherModule there would be one, so, your compiled code would operate on incorrect assumption. Isn't it dangerous?
>
> And what about instances that have context? What if module M contains something like
>
> data T a = ...
> instance OtherClass a => C (T a)
>
> and later OtherModule adds
>
> instance OtherClass a
>
> ?
>
> On Fri, Nov 11, 2016 at 10:55 AM, J. Stutterheim <[hidden email]> wrote:
> Yes, that's true, but suppose we have
>
>         module M where
>
>         import OtherModPossiblyWithInstance
>
> Then as far as I understand, the compiler will have processed OtherModPossiblyWithInstance already and the existence of class instances in OtherModPossiblyWithInstance should be know.
>
> > On 11 Nov 2016, at 10:07, MigMit <[hidden email]> wrote:
> >
> > I'm no expert in GHC plugins, but isn't it true that class instances can be added in other modules? So that this information is simply not available when compiling one?
> >
> >> On 11 Nov 2016, at 08:57, J. Stutterheim <[hidden email]> wrote:
> >>
> >> Hi all,
> >>
> >> I'm writing a core-to-core GHC plugin and have two questions:
> >>
> >> - Given some t :: Type, how can I check whether there exists an instance of some type class C for t?
> >> - How can I check whether some type class C exists, and if so, how can I get a value c (presumably :: Class) of this class?
> >>
> >> Thanks!
> >>
> >> - Jurriën_______________________________________________
> >> Haskell-Cafe mailing list
> >> To (un)subscribe, modify options or view archives go to:
> >> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> >> Only members subscribed via the mailman list are allowed to post.
> >
>
>

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

smime.p7s (4K) Download Attachment