查看Type是哪个类型是ghci的实例?

And*_*yuk 25 haskell

是否可以查看该类型实现的类型类?就像是:

>:typeclasses Int
[Num, etc...]
Run Code Online (Sandbox Code Playgroud)

Lil*_*ard 31

使用该: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
Run Code Online (Sandbox Code Playgroud)

当然,此列表取决于当前导入的模块.

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
Run Code Online (Sandbox Code Playgroud)


Tik*_*vis 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   
Run Code Online (Sandbox Code Playgroud)

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

*Main> :i +
class (Eq a, Show a) => Num a where
  (+) :: a -> a -> a
  ...
      -- Defined in GHC.Num
infixl 6 +
Run Code Online (Sandbox Code Playgroud)