Is it possible to get the information of instances of a type?

Hi, If this was in ruby or other languages that support reflection, it won't be a question. But in Haskell, could I write a code to list the classes that a type instanced? TemplateHaskell as well. -- 竹密岂妨流水过 山高哪阻野云飞

Magicloud Magiclouds
If this was in ruby or other languages that support reflection, it won't be a question. But in Haskell, could I write a code to list the classes that a type instanced?
In regular Haskell, type information is completely lost after compilation, so you can't recover any of that. There is no run-time type information like in languages with OO inheritance. However, types can choose to provide type information through the Typeable type class (Data.Typeable). Generally you wouldn't want to use it, if you write Haskell properly (i.e. if you don't try to write Ruby in Haskell).
TemplateHaskell as well.
I'm not sure about that one. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On Wed, Oct 26, 2011 at 6:53 AM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
But in Haskell, could I write a code to list the classes that a type instanced? TemplateHaskell as well.
It's possible with TemplateHaskell. Look at classInstances and the ClassI data constructor. http://haskell.org/ghc/docs/7.0.4/html/libraries/template-haskell-2.5.0.0/La...

On Wed, Oct 26, 2011 at 09:15:41AM -0700, Nathan Howell wrote:
On Wed, Oct 26, 2011 at 6:53 AM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
But in Haskell, could I write a code to list the classes that a type instanced? TemplateHaskell as well.
It's possible with TemplateHaskell. Look at classInstances and the ClassI data constructor.
http://haskell.org/ghc/docs/7.0.4/html/libraries/template-haskell-2.5.0.0/La...
No, this lists all the instances of a class. OP asked for the classes of which a given type is an instace. Presumably it is possible, since Haddock does it! In the documentation generated for a type it lists classes of which the type is an instance. So you might want to look at how Haddock does it. I suspect the only way is through the GHC API. -Brent

No, this lists all the instances of a class. OP asked for the classes of which a given type is an instace.
Presumably it is possible, since Haddock does it! In the documentation generated for a type it lists classes of which the type is an instance. So you might want to look at how Haddock does it. I suspect the only way is through the GHC API.
ghci :info does it too.

On Thu, Oct 27, 2011 at 7:04 AM, Evan Laforge
No, this lists all the instances of a class. OP asked for the classes of which a given type is an instace.
Presumably it is possible, since Haddock does it! In the documentation generated for a type it lists classes of which the type is an instance. So you might want to look at how Haddock does it. I suspect the only way is through the GHC API.
ghci :info does it too.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Generally looked into GHC's source, seems like the :info (line 850 of compiler/main/InteractiveEval.hs) is internal function. At last, GHC parsed the code, it sure knew the type info. -- 竹密岂妨流水过 山高哪阻野云飞

Can't be done. Even if this particular module doesn't contain "instance Class Type", it's quite possible that the said instance would be defined in another module, about which this one knows nothing about.
On the other hand, what would you do with that information?
Отправлено с iPad
26.10.2011, в 17:53, Magicloud Magiclouds
Hi, If this was in ruby or other languages that support reflection, it won't be a question. But in Haskell, could I write a code to list the classes that a type instanced? TemplateHaskell as well. -- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Wed, Oct 26, 2011 at 09:10:23PM +0400, MigMit wrote:
Can't be done. Even if this particular module doesn't contain "instance Class Type", it's quite possible that the said instance would be defined in another module, about which this one knows nothing about.
That doesn't mean it can't be done, only that you would have to be explicit about which modules to look in. -Brent

On Thu, Oct 27, 2011 at 1:57 AM, Brent Yorgey
On Wed, Oct 26, 2011 at 09:10:23PM +0400, MigMit wrote:
Can't be done. Even if this particular module doesn't contain "instance Class Type", it's quite possible that the said instance would be defined in another module, about which this one knows nothing about.
That doesn't mean it can't be done, only that you would have to be explicit about which modules to look in.
-Brent
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
May I know more about this? Have not used TH on this subject. -- 竹密岂妨流水过 山高哪阻野云飞

For TH look also at
http://www.mail-archive.com/haskell-cafe@haskell.org/msg13057.html
2011/10/27 Magicloud Magiclouds
On Thu, Oct 27, 2011 at 1:57 AM, Brent Yorgey
wrote: On Wed, Oct 26, 2011 at 09:10:23PM +0400, MigMit wrote:
Can't be done. Even if this particular module doesn't contain "instance Class Type", it's quite possible that the said instance would be defined in another module, about which this one knows nothing about.
That doesn't mean it can't be done, only that you would have to be explicit about which modules to look in.
-Brent
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
May I know more about this? Have not used TH on this subject.
-- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Brent Yorgey
-
Dmitry Olshansky
-
Ertugrul Soeylemez
-
Evan Laforge
-
Magicloud Magiclouds
-
MigMit
-
Nathan Howell