Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See which Typeclasses the Type is an instance of in ghci?

Tags:

haskell

Is it possible to see which typeclasses the type implements? Something like:

>:typeclasses Int [Num, etc...] 
like image 527
Andriy Drozdyuk Avatar asked Feb 17 '12 21:02

Andriy Drozdyuk


People also ask

How do I check my type class in Haskell?

Haskell will check all types at compile time, including membership of typeclasses. So to check if a literal is of a type that supports Eq, you simply need to use it with (==) or (/=) and try to compile it.

What is an instance of a Haskell type class?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.

Is Eq a subclass of Ord?

We say that Eq is a superclass of Ord (conversely, Ord is a subclass of Eq), and any type which is an instance of Ord must also be an instance of Eq.

What is a type class dictionary?

Type classes define interfaces that in Haskell's terms are called dictionaries. For instance, from the Ord class definition the compiler will create a dictionary that stores all the class methods.


1 Answers

Use the :info command.

Prelude> :info Int data Int = GHC.Types.I# GHC.Prim.Int#   -- Defined in GHC.Types instance Bounded Int -- Defined in GHC.Enum instance Enum Int -- Defined in GHC.Enum instance Eq Int -- Defined in GHC.Base instance Integral Int -- Defined in GHC.Real instance Num Int -- Defined in GHC.Num instance Ord Int -- Defined in GHC.Base instance Read Int -- Defined in GHC.Read instance Real Int -- Defined in GHC.Real instance Show Int -- Defined in GHC.Show 

Naturally this list depends on the modules that are currently imported.

Prelude> :info (->) data (->) a b   -- Defined in GHC.Prim Prelude> :m +Control.Monad.Instances Prelude Control.Monad.Instances> :info (->) data (->) a b   -- Defined in GHC.Prim instance Monad ((->) r) -- Defined in Control.Monad.Instances instance Functor ((->) r) -- Defined in Control.Monad.Instances 
like image 82
Lily Ballard Avatar answered Oct 01 '22 18:10

Lily Ballard