Fwd: Availability of Coercible Instances at TH Runtime

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

Fwd: Availability of Coercible Instances at TH Runtime

Travis Whitaker
Given that Coercible instances are Deeply Magical, perhaps I'm being a bit naive here, but I recently tried to write a TH function that can check if one type is a newtype of another (or a newtype of a newtype of another, etc).

coercibleToFrom :: Type -> Type -> Q Bool
coercibleToFrom tx ty = (&&) <$> isInstance ''Coercible [tx, ty]
                             <*> isInstance ''Coercible [ty, tx]


If this worked as I'd hoped, I'm almost certain checking reflexively is redundant. However, I can't seem to get reifyInstances to ever return an InstanceDec for a Coercible instance. Given that these instances are generated on the fly by the typechecker, there's no way to make them available at TH runtime, correct? And, given that, is there an easy way to find out with TH whether not I'll be able to use coerce without taking all the Decs apart to hunt for NewtypeD?

Travis


_______________________________________________
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: Availability of Coercible Instances at TH Runtime

Richard Eisenberg-4
I don't think there's an easy way to do this. We could imagine extending Quasi to have a method to check for coercibility, but I don't think there's a way to do this in the current TH API. Sorry!

Richard

On May 4, 2018, at 3:44 AM, Travis Whitaker <[hidden email]> wrote:

Given that Coercible instances are Deeply Magical, perhaps I'm being a bit naive here, but I recently tried to write a TH function that can check if one type is a newtype of another (or a newtype of a newtype of another, etc).

coercibleToFrom :: Type -> Type -> Q Bool
coercibleToFrom tx ty = (&&) <$> isInstance ''Coercible [tx, ty]
                             <*> isInstance ''Coercible [ty, tx]


If this worked as I'd hoped, I'm almost certain checking reflexively is redundant. However, I can't seem to get reifyInstances to ever return an InstanceDec for a Coercible instance. Given that these instances are generated on the fly by the typechecker, there's no way to make them available at TH runtime, correct? And, given that, is there an easy way to find out with TH whether not I'll be able to use coerce without taking all the Decs apart to hunt for NewtypeD?

Travis

_______________________________________________
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: Availability of Coercible Instances at TH Runtime

Travis Whitaker
A workaround that's suitable for what I'm after is defining a class like this for some fixed T.

instance (Coercible a T) => C a

I'm curious what it'd take to add a qReifyCoercible method; does the renamer know anything about Coercible?

Thanks,
Travis

On Fri, May 4, 2018 at 11:01 AM, Richard Eisenberg <[hidden email]> wrote:
I don't think there's an easy way to do this. We could imagine extending Quasi to have a method to check for coercibility, but I don't think there's a way to do this in the current TH API. Sorry!

Richard

On May 4, 2018, at 3:44 AM, Travis Whitaker <[hidden email]> wrote:

Given that Coercible instances are Deeply Magical, perhaps I'm being a bit naive here, but I recently tried to write a TH function that can check if one type is a newtype of another (or a newtype of a newtype of another, etc).

coercibleToFrom :: Type -> Type -> Q Bool
coercibleToFrom tx ty = (&&) <$> isInstance ''Coercible [tx, ty]
                             <*> isInstance ''Coercible [ty, tx]


If this worked as I'd hoped, I'm almost certain checking reflexively is redundant. However, I can't seem to get reifyInstances to ever return an InstanceDec for a Coercible instance. Given that these instances are generated on the fly by the typechecker, there's no way to make them available at TH runtime, correct? And, given that, is there an easy way to find out with TH whether not I'll be able to use coerce without taking all the Decs apart to hunt for NewtypeD?

Travis

_______________________________________________
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: Availability of Coercible Instances at TH Runtime

Richard Eisenberg-4


On May 4, 2018, at 10:01 PM, Travis Whitaker <[hidden email]> wrote:

I'm curious what it'd take to add a qReifyCoercible method; does the renamer know anything about Coercible?

No, it doesn't, but the Q monad is actually a wrapper around TcM, the type-checker monad. I don't think it would be all that hard to do this. Could be a suitable project for someone new to GHC hacking...

Richard

_______________________________________________
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: Availability of Coercible Instances at TH Runtime

David Kraeutmann
Tangentially related, but wasn't there a plan at some point to integrate
TH more tightly with GHC?

On 5/7/2018 4:39 AM, Richard Eisenberg wrote:
>
> No, it doesn't, but the Q monad is actually a wrapper around TcM, the
> type-checker monad. I don't think it would be all that hard to do
> this. Could be a suitable project for someone new to GHC hacking...
_______________________________________________
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: Availability of Coercible Instances at TH Runtime

Richard Eisenberg-4
Yes: https://ghc.haskell.org/trac/ghc/wiki/TemplateHaskell/Introspective
 
I think this may be waiting for the trees-that-grow work to be completed, and as far as I know, no one is actively working on this. But I still think it would be a Good Thing.

Richard

> On May 7, 2018, at 12:37 AM, David Kraeutmann <[hidden email]> wrote:
>
> Tangentially related, but wasn't there a plan at some point to integrate TH more tightly with GHC?
>
> On 5/7/2018 4:39 AM, Richard Eisenberg wrote:
>>
>> No, it doesn't, but the Q monad is actually a wrapper around TcM, the type-checker monad. I don't think it would be all that hard to do this. Could be a suitable project for someone new to GHC hacking...
> _______________________________________________
> 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: Availability of Coercible Instances at TH Runtime

Shayan Najd Javadipour-2
> I think this may be waiting for the trees-that-grow work to be completed, and as far as I know, no one is actively working on this.

We've just got a new Google SoC project accepted to push this forward :)

/Shayan

On Mon, May 7, 2018 at 3:14 PM, Richard Eisenberg <[hidden email]> wrote:

> Yes: https://ghc.haskell.org/trac/ghc/wiki/TemplateHaskell/Introspective
>
> I think this may be waiting for the trees-that-grow work to be completed, and as far as I know, no one is actively working on this. But I still think it would be a Good Thing.
>
> Richard
>
>> On May 7, 2018, at 12:37 AM, David Kraeutmann <[hidden email]> wrote:
>>
>> Tangentially related, but wasn't there a plan at some point to integrate TH more tightly with GHC?
>>
>> On 5/7/2018 4:39 AM, Richard Eisenberg wrote:
>>>
>>> No, it doesn't, but the Q monad is actually a wrapper around TcM, the type-checker monad. I don't think it would be all that hard to do this. Could be a suitable project for someone new to GHC hacking...
>> _______________________________________________
>> 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