2012-02-17 45 views
20

是否有可能看到该类型实现的类型类型?喜欢的东西:查看哪个类型是ghci中的一个实例?

>:typeclasses Int 
[Num, etc...] 
+0

此外(以下),您可以查看源使用低级哈斯克尔模式。有关文档见github.com/haskell/haskell-mode/wiki和chrisdone.com/posts/haskell-mode-docs – artella 2014-06-01 07:04:47

回答

25

使用:info命令。

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 

当然这个列表取决于当前导入的模块。

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 
+1

哇,我刚刚注意到,我们发布中*彼此的二分之一*我们的答案:21:32: 48Z vs 21:32:47Z。 – 2012-02-18 00:14:55

+0

@TikhonJelvis:哈,很好。 – 2012-02-18 00:15:37

14

尝试:info:i与类型。

这将让你无论是类型类和类型的声明,以及告诉你它的定义(如果你不记得是什么构造它具有非常有用)。

对于自己定义的类型,你甚至可以得到一个链接到它是在Emacs的定义。这使得浏览源代码非常方便。

请注意:i是非常多用途的:您可以在值两种类型上使用它。所以:i True:i Bool都可以工作!

*Main> :i Bool 
data Bool = False | True -- Defined in GHC.Bool 
instance [overlap ok] Truthy Bool 
    -- Defined at /home/tikhon/Documents/blarg2.hs:40:10-20 
instance Bounded Bool -- Defined in GHC.Enum 
instance Enum Bool -- Defined in GHC.Enum 
instance Eq Bool -- Defined in GHC.Classes 
instance Ord Bool -- Defined in GHC.Classes 
instance Read Bool -- Defined in GHC.Read 
instance Show Bool -- Defined in GHC.Show 
instance Ix Bool -- Defined in GHC.Arr 

*Main> :i True 
data Bool = ... | True -- Defined in GHC.Bool 

它也用于检查符的优先级非常有用:

*Main> :i + 
class (Eq a, Show a) => Num a where 
    (+) :: a -> a -> a 
    ... 
     -- Defined in GHC.Num 
infixl 6 + 
相关问题