2012-02-17 72 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 + 
相關問題