Re: Determine instance method from class method callsite

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

Re: Determine instance method from class method callsite

Haskell - Haskell-Cafe mailing list

Did you get a reply?

 

I’m not 100% certain of your question, but consider the

 

    bar = show <expr>

for some expression e.   In the input to the type type checker the syntax tree for the RHS will be something like

            HsApp (HsVar “show”) <syntax tree for expr>

 

(The “show” isn’t really a string, it’s the Name for the class method.)

 

After typechecking the syntax tree is augmented (or “elaborated”) with type and dictionary application.  So in concrete form it might look like

            bar = show @Foo dShowFoo <expr>

 

Because show :: forall a. Show a => a -> String, so show is apply to the type of its argument, and then to the dictionary. 

 

In HsSyn this part is done with a HsWrapper See TcEvidence.HsWrapper.  The elaborated syntax tree look like

 

            HsApp (HsWrap <wrapper> (HsVar “show”))

                          <syntax tree for elaborated expr>

 

The <wrapper> part expresses the type and dictionary application. In this case it’ll look like

            WpEvApp dShowFoo (WpTyApp Foo WpHole)

 

See the notes with `HsWrapper` in TcEvidence.

 

Does that help?

 

It would be great to augment the https://ghc.haskell.org/trac/ghc/wiki/Commentary with this sort of info (insofar as it doesn’t have it already).  If you augment I can review.  Email is quickly lost.

 

Simon

From: ghc-devs [mailto:[hidden email]] On Behalf Of Robin Palotai
Sent: 19 September 2017 06:39
To: GHC developers <[hidden email]>; haskell <[hidden email]>
Subject: Determine instance method from class method callsite

 

Sorry, I messed up subject and mailing list. Copying to both list now after the mistake (wanted only ghc-devs for specificity).

 

Thanks!

 

2017-09-19 7:36 GMT+02:00 Robin Palotai <[hidden email]>:

Hello GHC devs,

 

Before inventing the wheel, want to check if there is a GHC API way to look up the (fully) resolved instance method from a class method.

 

For example, given a code

 

    data Foo Int deriving Show

 

    bar = show (Foo 3)

 

when inspecting the Typechecked AST for bar's show call, I would like to get to the Name / Id of 'show' of the 'Show' typeclass.

 

I believe I could use splitHsSigmaTy on the HsType of the function call to get the context, and then evaluate the HsWrapper somehow to find out what instance dictionary is applied to the class restriction in the context, and then look up the instance method from the dictionary..

 

Two questions:

 

1) Is there maybe functionality for this?

 

2) If not, is there any guarantee about the constraint order in the context, at the method call? So I could more easily determine which constraint's application to look for..

 

Any hints welcome && Thank you!

Robin

 


_______________________________________________
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: Determine instance method from class method callsite

Haskell - Haskell-Cafe mailing list

well the HsWrapper might have something much more complicated than a DFunId.  It might need to construct the dictionary for Eq [a] from the DFun for Eq [a] and a dictionary for Eq a.

 

it would be easy to resolve the Var of the instance method.

I don’t know what “the var of the instance method” is.

 

I feel I’m missing the point.  maybe others can help?

 

Simon

 

From: Robin Palotai [mailto:[hidden email]]
Sent: 05 October 2017 11:17
To: Simon Peyton Jones <[hidden email]>
Cc: GHC developers <[hidden email]>; haskell <[hidden email]>
Subject: Re: Determine instance method from class method callsite

 

Hello Simon - I outlined the approach in https://github.com/google/haskell-indexer/issues/73.
TLDR is
1) at callsite, indeed the HsWrapper needs to be analysed to get the instance DFunId

2) at instance declaration site, we need to again take note of the DFunId.

It is slightly more complicated than I expected the API. I would have expected that from having the Var of a class method, and the <something> of the instance (here DFunId), it would be easy to resolve the Var of the instance method.

But it seems there's no direct way, one has to build a lookup table from the instance methods' (DFunId + plain stringy method name), and look that up from the callsite.

Or I might have missed a way to deconstruct / query a DFunId for the method Vars.

I'll put augmenting the commentary on my mental TODO list :)

 

2017-10-05 11:58 GMT+02:00 Simon Peyton Jones <[hidden email]>:

Did you get a reply?

 

I’m not 100% certain of your question, but consider the

 

    bar = show <expr>

for some expression e.   In the input to the type type checker the syntax tree for the RHS will be something like

            HsApp (HsVar “show”) <syntax tree for expr>

 

(The “show” isn’t really a string, it’s the Name for the class method.)

 

After typechecking the syntax tree is augmented (or “elaborated”) with type and dictionary application.  So in concrete form it might look like

            bar = show @Foo dShowFoo <expr>

 

Because show :: forall a. Show a => a -> String, so show is apply to the type of its argument, and then to the dictionary. 

 

In HsSyn this part is done with a HsWrapper See TcEvidence.HsWrapper.  The elaborated syntax tree look like

 

            HsApp (HsWrap <wrapper> (HsVar “show”))

                          <syntax tree for elaborated expr>

 

The <wrapper> part expresses the type and dictionary application. In this case it’ll look like

            WpEvApp dShowFoo (WpTyApp Foo WpHole)

 

See the notes with `HsWrapper` in TcEvidence.

 

Does that help?

 

It would be great to augment the https://ghc.haskell.org/trac/ghc/wiki/Commentary with this sort of info (insofar as it doesn’t have it already).  If you augment I can review.  Email is quickly lost.

 

Simon

From: ghc-devs [mailto:[hidden email]] On Behalf Of Robin Palotai
Sent: 19 September 2017 06:39
To: GHC developers <[hidden email]>; haskell <[hidden email]>
Subject: Determine instance method from class method callsite

 

Sorry, I messed up subject and mailing list. Copying to both list now after the mistake (wanted only ghc-devs for specificity).

 

Thanks!

 

2017-09-19 7:36 GMT+02:00 Robin Palotai <[hidden email]>:

Hello GHC devs,

 

Before inventing the wheel, want to check if there is a GHC API way to look up the (fully) resolved instance method from a class method.

 

For example, given a code

 

    data Foo Int deriving Show

 

    bar = show (Foo 3)

 

when inspecting the Typechecked AST for bar's show call, I would like to get to the Name / Id of 'show' of the 'Show' typeclass.

 

I believe I could use splitHsSigmaTy on the HsType of the function call to get the context, and then evaluate the HsWrapper somehow to find out what instance dictionary is applied to the class restriction in the context, and then look up the instance method from the dictionary..

 

Two questions:

 

1) Is there maybe functionality for this?

 

2) If not, is there any guarantee about the constraint order in the context, at the method call? So I could more easily determine which constraint's application to look for..

 

Any hints welcome && Thank you!

Robin

 

 


_______________________________________________
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.