Finding & assembling class dictionaries from GHC plugins?

I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable. Thanks, -- Conal

I'm not sure what you mean by "synthesise". Typeable instances can no longer be hand-written; they must be derived by GHC. That's to keep them type-secure. A core-level plug-in can't generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with "deriving Typeable"? Simon From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal Elliott Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins? I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable. Thanks, -- Conal

On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”? We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.) We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API? Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

We just don't know how to start - ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
And I for my part don't know where to start helping you! Conal wrote
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That's exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
I gather you are talking to Pedro too? He knows a lot about this stuff.
Also there are a bunch of folk (Luite, Edsko) worked on the new front-end plugin stuff, and are much more expert in it than me.
We could have a skype call if that would help
S
From: Nicolas Frisby [mailto:nicolas.frisby@gmail.com]
Sent: 05 November 2013 14:36
To: Simon Peyton-Jones
Cc: ghc-devs@haskell.org; Conal Elliott
Subject: RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
A core-level plug-in can't generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with "deriving Typeable"?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.) We just don't know how to start - ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API? Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.orgmailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal Elliott Sent: 04 November 2013 20:08 To: ghc-devs@haskell.orgmailto:ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.orgmailto:ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

On Tue, Nov 5, 2013 at 4:37 PM, Simon Peyton-Jones
And I for my part don’t know where to start helping you! Conal wrote
Ha! Fair enough.
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That’s exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
Even "TcSimplify and friends" gives me something new-to-my-brain to dig around with; I'm new to this part of the compiler. I'll defer to Conal to more fully characterize his needs for you. My role in this specific project is to distill his and your eventual conclusion into a HERMIT module that Conal and others can use in the future. However, I will include my own estimation of the challenge — it's a question I've wondered before. "Given a list of non-top-level dictionary bindings and a constraint type, construct a Core term inhabiting that constraint type that can also use any top-level dictionaries in scope." How do I do that using the functions in the module in the compiler/typechecker directory? I think a wiki page for this would be great. Maybe one even already exists! (I have some unshared scaffolding for being able to run TcRn monads from a Core plugin.) We could have a skype call if that would help
Thanks for putting that on the table. I'll certainly need to do some more research before taking up a chunk of your time, but it will be very helpful when it happens.
*From:* Nicolas Frisby [mailto:nicolas.frisby@gmail.com] *Sent:* 05 November 2013 14:36 *To:* Simon Peyton-Jones *Cc:* ghc-devs@haskell.org; Conal Elliott *Subject:* RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
wrote: A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.)
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Hi Simon, Here's some context on what I'm after. I'll try to include enough to clarify what I'm after, without dragging you into unnecessary detail. I have a Core expression, which I'm systematically translating to a related Core expression. The second one reifies the first one, so that the generated code *evaluates* to a run-time (not compile/Core-time) representation akin to the original Core expression. In this second (reified) representation, I need some run-time representation of types. I use Typeable constraints in my representation, and these constraints don't appear in the original Core representation. For instance, I have the following constructor for function application in a GADT of lambda expressions:
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively
reifies the function and argument expressions, and makes an expression that
applies (:^) to the two recursive results. However--and the point of this
thread--I also need to come up with Core expressions for the two Typeable
dictionaries.
So, to clarify my request, given a Core type representation t, I want to
construct a Core expression that evaluates to the dictionary for Typeable t
if one exists, or fail if there isn't one. Note that t is not necessarily
an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
And I for my part don’t know where to start helping you! Conal wrote
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That’s exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
I gather you are talking to Pedro too? He knows a lot about this stuff.
Also there are a bunch of folk (Luite, Edsko) worked on the new front-end plugin stuff, and are much more expert in it than me.
We could have a skype call if that would help
S
*From:* Nicolas Frisby [mailto:nicolas.frisby@gmail.com] *Sent:* 05 November 2013 14:36 *To:* Simon Peyton-Jones *Cc:* ghc-devs@haskell.org; Conal Elliott *Subject:* RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
wrote: A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.)
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Hmm. One route would be to build a HsExpr (rather than a CoreExpr) for the reification of your CoreExpr, and stick that into the maw of the type checker. That would hand off all this dictionary construction to the type checker, which is designed for the purpose. Simon From: conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] On Behalf Of Conal Elliott Sent: 05 November 2013 23:41 To: Simon Peyton-Jones Cc: Nicolas Frisby; ghc-devs@haskell.org Subject: Re: Finding & assembling class dictionaries from GHC plugins? Hi Simon, Here's some context on what I'm after. I'll try to include enough to clarify what I'm after, without dragging you into unnecessary detail. I have a Core expression, which I'm systematically translating to a related Core expression. The second one reifies the first one, so that the generated code *evaluates* to a run-time (not compile/Core-time) representation akin to the original Core expression. In this second (reified) representation, I need some run-time representation of types. I use Typeable constraints in my representation, and these constraints don't appear in the original Core representation. For instance, I have the following constructor for function application in a GADT of lambda expressions:
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
A core-level plug-in can't generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with "deriving Typeable"?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.) We just don't know how to start - ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API? Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.orgmailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal Elliott Sent: 04 November 2013 20:08 To: ghc-devs@haskell.orgmailto:ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.orgmailto:ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Thanks for the suggestion. I hadn't considered backing up to an HsExpr. I
guess we'd have to start by converting the Core Type to an HsType to form a
type-annotated HsExpr. Am I on the right track here?
-- Conal
On Wed, Nov 6, 2013 at 5:59 AM, Simon Peyton-Jones
Hmm. One route would be to build a HsExpr (rather than a CoreExpr) for the reification of your CoreExpr, and stick that into the maw of the type checker. That would hand off all this dictionary construction to the type checker, which is designed for the purpose.
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 05 November 2013 23:41 *To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
Hi Simon,
Here's some context on what I'm after. I'll try to include enough to clarify what I'm after, without dragging you into unnecessary detail.
I have a Core expression, which I'm systematically translating to a related Core expression. The second one reifies the first one, so that the generated code *evaluates* to a run-time (not compile/Core-time) representation akin to the original Core expression. In this second (reified) representation, I need some run-time representation of types. I use Typeable constraints in my representation, and these constraints don't appear in the original Core representation. For instance, I have the following constructor for function application in a GADT of lambda expressions:
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
wrote: We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
And I for my part don’t know where to start helping you! Conal wrote
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That’s exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
I gather you are talking to Pedro too? He knows a lot about this stuff.
Also there are a bunch of folk (Luite, Edsko) worked on the new front-end plugin stuff, and are much more expert in it than me.
We could have a skype call if that would help
S
*From:* Nicolas Frisby [mailto:nicolas.frisby@gmail.com] *Sent:* 05 November 2013 14:36 *To:* Simon Peyton-Jones *Cc:* ghc-devs@haskell.org; Conal Elliott *Subject:* RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
wrote: A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.)
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Well, you are writing a function
reify :: CoreExpr -> HsExpr RdrName
It takes as its input a CoreExpr and produces as its output a reification of the input. I don't see why you need start by converting Core to HsExpr. Just write reify directly!
Simon
From: conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] On Behalf Of Conal Elliott
Sent: 07 November 2013 01:08
To: Simon Peyton-Jones
Cc: Nicolas Frisby; ghc-devs@haskell.org
Subject: Re: Finding & assembling class dictionaries from GHC plugins?
Thanks for the suggestion. I hadn't considered backing up to an HsExpr. I guess we'd have to start by converting the Core Type to an HsType to form a type-annotated HsExpr. Am I on the right track here?
-- Conal
On Wed, Nov 6, 2013 at 5:59 AM, Simon Peyton-Jones
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
A core-level plug-in can't generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with "deriving Typeable"?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.) We just don't know how to start - ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API? Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.orgmailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal Elliott Sent: 04 November 2013 20:08 To: ghc-devs@haskell.orgmailto:ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.orgmailto:ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

On Wed, Nov 6, 2013 at 11:44 PM, Simon Peyton-Jones
Well, you are writing a function
reify :: CoreExpr -> HsExpr RdrName
Not exactly. I'm reifying CoreExpr directly to another CoreExpr. (The target CoreExpr calls into an API for GADT-typed lambda termshttps://github.com/conal/lambda-ccc/blob/master/src/LambdaCCC/Lambda.hs. All works swimmingly as long as I'm able to avoid dictionary construction, but I'm no longer able to do so.) I haven't explored your suggestion of translating to HsExpr instead. Do you know of any plugins that work this way? Do you really believe it would work out? For instance, I wouldn't know where to insert type annotations, and I'd have to back-translate names. I imagine there are other challenges I don't foresee.
It takes as its input a CoreExpr and produces as its output a reification of the input. I don’t see why you need start by converting Core to HsExpr. Just write reify directly!
I wasn't suggesting converting a CoreExpr to an HsExpr. Rather, I was wondering out loud how I might get the type-checker's help to generate Core to find/assemble a Typeable dictionary for a given Core type (since I'm starting with Core). One idea is translate the core type cty to an HsType hty, use hty in a very simple HsExpr like "Dict :: Dict hty" (where Dict is from Ed K's constraints package), use the typechecker to convert that simple HsExpr to a CoreExpr, and then extract the dictionary-forming sub-CoreExpr. Or something simpler along the same lines. Thanks for the help, -- Conal
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 07 November 2013 01:08
*To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
Thanks for the suggestion. I hadn't considered backing up to an HsExpr. I guess we'd have to start by converting the Core Type to an HsType to form a type-annotated HsExpr. Am I on the right track here?
-- Conal
On Wed, Nov 6, 2013 at 5:59 AM, Simon Peyton-Jones
wrote: Hmm. One route would be to build a HsExpr (rather than a CoreExpr) for the reification of your CoreExpr, and stick that into the maw of the type checker. That would hand off all this dictionary construction to the type checker, which is designed for the purpose.
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 05 November 2013 23:41 *To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
Hi Simon,
Here's some context on what I'm after. I'll try to include enough to clarify what I'm after, without dragging you into unnecessary detail.
I have a Core expression, which I'm systematically translating to a related Core expression. The second one reifies the first one, so that the generated code *evaluates* to a run-time (not compile/Core-time) representation akin to the original Core expression. In this second (reified) representation, I need some run-time representation of types. I use Typeable constraints in my representation, and these constraints don't appear in the original Core representation. For instance, I have the following constructor for function application in a GADT of lambda expressions:
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
wrote: We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
And I for my part don’t know where to start helping you! Conal wrote
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That’s exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
I gather you are talking to Pedro too? He knows a lot about this stuff.
Also there are a bunch of folk (Luite, Edsko) worked on the new front-end plugin stuff, and are much more expert in it than me.
We could have a skype call if that would help
S
*From:* Nicolas Frisby [mailto:nicolas.frisby@gmail.com] *Sent:* 05 November 2013 14:36 *To:* Simon Peyton-Jones *Cc:* ghc-devs@haskell.org; Conal Elliott *Subject:* RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
wrote: A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.)
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Conal
Email is not good for this. I think it'd be more productive to have a Skype call, with both of use looking at GHC's source code.
If you'd like to do that, do suggest a time.
Simon
From: conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] On Behalf Of Conal Elliott
Sent: 07 November 2013 18:15
To: Simon Peyton-Jones
Cc: Nicolas Frisby; ghc-devs@haskell.org
Subject: Re: Finding & assembling class dictionaries from GHC plugins?
On Wed, Nov 6, 2013 at 11:44 PM, Simon Peyton-Jones
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
A core-level plug-in can't generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with "deriving Typeable"?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.) We just don't know how to start - ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API? Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.orgmailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal Elliott Sent: 04 November 2013 20:08 To: ghc-devs@haskell.orgmailto:ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.orgmailto:ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Hi Simon. Yes, I'd appreciate such a call, with at least one of the HERMIT
folks in the conversation as well. They'll be implementing whatever we come
up with as an improvement to HERMIT, and I'll be using the result. I'll
follow up about choosing a time that works for all. -- Conal
On Mon, Nov 11, 2013 at 1:49 PM, Simon Peyton-Jones
Conal
Email is not good for this. I think it’d be more productive to have a Skype call, with both of use looking at GHC’s source code.
If you’d like to do that, do suggest a time.
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 07 November 2013 18:15
*To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
On Wed, Nov 6, 2013 at 11:44 PM, Simon Peyton-Jones
wrote: Well, you are writing a function
reify :: CoreExpr -> HsExpr RdrName
Not exactly. I'm reifying CoreExpr directly to another CoreExpr. (The target CoreExpr calls into an API for GADT-typed lambda termshttps://github.com/conal/lambda-ccc/blob/master/src/LambdaCCC/Lambda.hs. All works swimmingly as long as I'm able to avoid dictionary construction, but I'm no longer able to do so.) I haven't explored your suggestion of translating to HsExpr instead. Do you know of any plugins that work this way? Do you really believe it would work out? For instance, I wouldn't know where to insert type annotations, and I'd have to back-translate names. I imagine there are other challenges I don't foresee.
It takes as its input a CoreExpr and produces as its output a reification of the input. I don’t see why you need start by converting Core to HsExpr. Just write reify directly!
I wasn't suggesting converting a CoreExpr to an HsExpr. Rather, I was wondering out loud how I might get the type-checker's help to generate Core to find/assemble a Typeable dictionary for a given Core type (since I'm starting with Core). One idea is translate the core type cty to an HsType hty, use hty in a very simple HsExpr like "Dict :: Dict hty" (where Dict is from Ed K's constraints package), use the typechecker to convert that simple HsExpr to a CoreExpr, and then extract the dictionary-forming sub-CoreExpr. Or something simpler along the same lines.
Thanks for the help,
-- Conal
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 07 November 2013 01:08
*To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
Thanks for the suggestion. I hadn't considered backing up to an HsExpr. I guess we'd have to start by converting the Core Type to an HsType to form a type-annotated HsExpr. Am I on the right track here?
-- Conal
On Wed, Nov 6, 2013 at 5:59 AM, Simon Peyton-Jones
wrote: Hmm. One route would be to build a HsExpr (rather than a CoreExpr) for the reification of your CoreExpr, and stick that into the maw of the type checker. That would hand off all this dictionary construction to the type checker, which is designed for the purpose.
Simon
*From:* conal.elliott@gmail.com [mailto:conal.elliott@gmail.com] *On Behalf Of *Conal Elliott *Sent:* 05 November 2013 23:41 *To:* Simon Peyton-Jones *Cc:* Nicolas Frisby; ghc-devs@haskell.org *Subject:* Re: Finding & assembling class dictionaries from GHC plugins?
Hi Simon,
Here's some context on what I'm after. I'll try to include enough to clarify what I'm after, without dragging you into unnecessary detail.
I have a Core expression, which I'm systematically translating to a related Core expression. The second one reifies the first one, so that the generated code *evaluates* to a run-time (not compile/Core-time) representation akin to the original Core expression. In this second (reified) representation, I need some run-time representation of types. I use Typeable constraints in my representation, and these constraints don't appear in the original Core representation. For instance, I have the following constructor for function application in a GADT of lambda expressions:
(:^) :: (Typeable a, Typeable b) => E (a -> b) -> E a -> E b
My reification plugin takes a Core application expression, recursively reifies the function and argument expressions, and makes an expression that applies (:^) to the two recursive results. However--and the point of this thread--I also need to come up with Core expressions for the two Typeable dictionaries.
So, to clarify my request, given a Core type representation t, I want to construct a Core expression that evaluates to the dictionary for Typeable t if one exists, or fail if there isn't one. Note that t is not necessarily an atomic type; it could instead be something like [(Bool,Int -> String)].
I'm happy to Skype-chat if it'd help.
Thanks, - Conal
On Tue, Nov 5, 2013 at 2:37 PM, Simon Peyton-Jones
wrote: We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
And I for my part don’t know where to start helping you! Conal wrote
I don't think I need to construct dictionaries for new Typeable instances. I only need to find and assemble *existing* Typeable instances into combinations like Typeable [(Bool,Int -> String)], and I don't know how to do so in a GHC plug-in
That’s exactly what the constraint solver does. (TcSimplify and friends.) But again I need more context.
I gather you are talking to Pedro too? He knows a lot about this stuff.
Also there are a bunch of folk (Luite, Edsko) worked on the new front-end plugin stuff, and are much more expert in it than me.
We could have a skype call if that would help
S
*From:* Nicolas Frisby [mailto:nicolas.frisby@gmail.com] *Sent:* 05 November 2013 14:36 *To:* Simon Peyton-Jones *Cc:* ghc-devs@haskell.org; Conal Elliott *Subject:* RE: Finding & assembling class dictionaries from GHC plugins?
On Nov 5, 2013 3:14 AM, "Simon Peyton-Jones"
wrote: A core-level plug-in can’t generate fresh instances. There are some new
plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
We're willing to do some awkward shoehorning in HERMIT to make this work in limited circumstances. (Hopefully including Conal's.)
We just don't know how to start — ie how call the right functions from the type checker. Are there any resources explaining the relevant subset of the TcRnIf API?
Thanks.
From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Conal
Sent: 04 November 2013 20:08 To: ghc-devs@haskell.org Subject: Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling
Elliott them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

Hi Simon,
I don't think I need to construct dictionaries for new Typeable instances.
I only need to find and assemble *existing* Typeable instances into
combinations like Typeable [(Bool,Int -> String)], and I don't know how to
do so in a GHC plug-in. Pointers greatly appreciated!
-- Conal
On Tue, Nov 5, 2013 at 1:13 AM, Simon Peyton-Jones
I’m not sure what you mean by “synthesise”.
Typeable instances can no longer be hand-written; they must be derived by GHC. That’s to keep them type-secure.
A core-level plug-in can’t generate fresh instances. There are some new plug-in hooks that fit earlier in the pipeline, which can. Maybe you can use that, and generate a data type decl with “deriving Typeable”?
Simon
*From:* ghc-devs [mailto:ghc-devs-bounces@haskell.org] *On Behalf Of *Conal Elliott *Sent:* 04 November 2013 20:08 *To:* ghc-devs@haskell.org *Subject:* Finding & assembling class dictionaries from GHC plugins?
I'm working on a GHC plugin (for compiling Haskell to hardware), and I need to synthesize Typeable (and maybe other) class dictionaries for a wide range of types, including composite types (functions, pairs, lists, etc). Can it be done, and how? I think I'm mainly looking for mechanics of finding existing class instances (however they're defined) and assembling them (for parametrized/composite dictionaries), rather than mechanisms specific to Typeable.
Thanks, -- Conal
participants (3)
-
Conal Elliott
-
Nicolas Frisby
-
Simon Peyton-Jones