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

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

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

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Ertugrul Söylemez
Magicloud Magiclouds <[hidden email]> wrote:

>   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/



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Nathan Howell-2
In reply to this post by Magicloud Magiclouds
On Wed, Oct 26, 2011 at 6:53 AM, Magicloud Magiclouds <[hidden email]> 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.



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

MigMit
In reply to this post by Magicloud Magiclouds
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 <[hidden email]> написал(а):

> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Brent Yorgey-2
In reply to this post by Nathan Howell-2
On Wed, Oct 26, 2011 at 09:15:41AM -0700, Nathan Howell wrote:

> On Wed, Oct 26, 2011 at 6:53 AM, Magicloud Magiclouds <
> [hidden email]> 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/Language-Haskell-TH.html#v:classInstances

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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Brent Yorgey-2
In reply to this post by MigMit
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Evan Laforge
In reply to this post by Brent Yorgey-2
> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Magicloud Magiclouds
On Thu, Oct 27, 2011 at 7:04 AM, Evan Laforge <[hidden email]> wrote:

>> 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
> [hidden email]
> 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.

--
竹密岂妨流水过
山高哪阻野云飞

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Magicloud Magiclouds
In reply to this post by Brent Yorgey-2
On Thu, Oct 27, 2011 at 1:57 AM, Brent Yorgey <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Dmitry Olshansky
For TH look also at
http://www.mail-archive.com/haskell-cafe@.../msg13057.html


2011/10/27 Magicloud Magiclouds <[hidden email]>
On Thu, Oct 27, 2011 at 1:57 AM, Brent Yorgey <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe