Template Haskell: Generate annotated function of a typeclass

Hi all, I think this is the right place for the following questions and I thank beforehand for your answers :-) I'm experimenting with typeclasses and TH, and I want to define a 'macro' that works more or less like this: Given the name of a typeclass and a function, return the expressions corresponding to the type-annotated instances, for instance $(foo Show show) should translate to: ["(show :: Int -> String)", "(show :: Bool -> String)", ....] for all instances currently in scope. I'm currently playing with the isInstance function (I'm running GHC 7.4.1) and can get a list of instances, and check if a given type is part of a typeclass or not. But I don't know how to create the expression corresponding to "instantiated function", as above. Thanks! -- Ismael

Hello!
It seems like you would want to use "reifyInstances" in order to get
all of the instances associated with a class. Then, you can match up
the variables in each instance with the variables in the class
declaration, and create a mapping from the class variables to the
instance parameters. Then, you can apply these mappings with substT:
http://hackage.haskell.org/packages/archive/haskell-src-meta/0.5.1.2/doc/htm...
The result would also need to have the context of the instance,
perhaps reduced to just the constraints that mention the type
variables used in the selected function.
-Michael Sloan
On Fri, Apr 13, 2012 at 11:37 AM, Ismael Figueroa Palet
Hi all, I think this is the right place for the following questions and I thank beforehand for your answers :-)
I'm experimenting with typeclasses and TH, and I want to define a 'macro' that works more or less like this:
Given the name of a typeclass and a function, return the expressions corresponding to the type-annotated instances, for instance
$(foo Show show)
should translate to:
["(show :: Int -> String)", "(show :: Bool -> String)", ....]
for all instances currently in scope.
I'm currently playing with the isInstance function (I'm running GHC 7.4.1) and can get a list of instances, and check if a given type is part of a typeclass or not. But I don't know how to create the expression corresponding to "instantiated function", as above.
Thanks! -- Ismael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for your reply, in particular the reference to subsT!
I will work more on this next monday, and report my progress
Cheers!
2012/4/13 Michael Sloan
Hello!
It seems like you would want to use "reifyInstances" in order to get all of the instances associated with a class. Then, you can match up the variables in each instance with the variables in the class declaration, and create a mapping from the class variables to the instance parameters. Then, you can apply these mappings with substT:
http://hackage.haskell.org/packages/archive/haskell-src-meta/0.5.1.2/doc/htm...
The result would also need to have the context of the instance, perhaps reduced to just the constraints that mention the type variables used in the selected function.
-Michael Sloan
On Fri, Apr 13, 2012 at 11:37 AM, Ismael Figueroa Palet
wrote: Hi all, I think this is the right place for the following questions and I thank beforehand for your answers :-)
I'm experimenting with typeclasses and TH, and I want to define a 'macro' that works more or less like this:
Given the name of a typeclass and a function, return the expressions corresponding to the type-annotated instances, for instance
$(foo Show show)
should translate to:
["(show :: Int -> String)", "(show :: Bool -> String)", ....]
for all instances currently in scope.
I'm currently playing with the isInstance function (I'm running GHC 7.4.1) and can get a list of instances, and check if a given type is part of a typeclass or not. But I don't know how to create the expression corresponding to "instantiated function", as above.
Thanks! -- Ismael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ismael

Hi Michael, I was able to do what I wanted using reify instead of
reifyInstances, and also I used applyT instead of substT.
Thanks
2012/4/13 Ismael Figueroa Palet
Thanks for your reply, in particular the reference to subsT! I will work more on this next monday, and report my progress
Cheers!
2012/4/13 Michael Sloan
Hello!
It seems like you would want to use "reifyInstances" in order to get all of the instances associated with a class. Then, you can match up the variables in each instance with the variables in the class declaration, and create a mapping from the class variables to the instance parameters. Then, you can apply these mappings with substT:
http://hackage.haskell.org/packages/archive/haskell-src-meta/0.5.1.2/doc/htm...
The result would also need to have the context of the instance, perhaps reduced to just the constraints that mention the type variables used in the selected function.
-Michael Sloan
On Fri, Apr 13, 2012 at 11:37 AM, Ismael Figueroa Palet
wrote: Hi all, I think this is the right place for the following questions and I thank beforehand for your answers :-)
I'm experimenting with typeclasses and TH, and I want to define a 'macro' that works more or less like this:
Given the name of a typeclass and a function, return the expressions corresponding to the type-annotated instances, for instance
$(foo Show show)
should translate to:
["(show :: Int -> String)", "(show :: Bool -> String)", ....]
for all instances currently in scope.
I'm currently playing with the isInstance function (I'm running GHC 7.4.1) and can get a list of instances, and check if a given type is part of a typeclass or not. But I don't know how to create the expression corresponding to "instantiated function", as above.
Thanks! -- Ismael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ismael
-- Ismael
participants (2)
-
Ismael Figueroa Palet
-
Michael Sloan