在 ghci 中查看特定类型的 Typeclass 定义 [英] Seeing Typeclass definition in ghci for a specific type

查看:23
本文介绍了在 ghci 中查看特定类型的 Typeclass 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 ghci 中查看特定类型的 Typeclass 定义?

Is there a way to see Typeclass definition in ghci for a specific type?

例如,Maybe 是这样定义的:

For example, Maybe is defined like this:

instance Functor Maybe where  
     fmap f (Just x) = Just (f x)  
     fmap f Nothing = Nothing  

我可以在 ghci 中看到这个吗?

Can I see this in ghci ?

当我在 ghci 中使用 :info 时,我得到了这个:

When, I use :info in ghci, I get this:

Prelude> :i Maybe
data Maybe a = Nothing | Just a     -- Defined in `Data.Maybe'
instance Eq a => Eq (Maybe a) -- Defined in `Data.Maybe'
instance Monad Maybe -- Defined in `Data.Maybe'
instance Functor Maybe -- Defined in `Data.Maybe'
instance Ord a => Ord (Maybe a) -- Defined in `Data.Maybe'
instance Read a => Read (Maybe a) -- Defined in `GHC.Read'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'

在上面的输出中,我想看看它是如何在 Data.Maybe 中定义为 Functor 的实例的.无论如何要在 ghci 中看到它?

In the above output, I want to see how it is defined in Data.Maybe as an instance for Functor. Anyway to see that in ghci ?

推荐答案

不,这是不可能的——不仅是对于实例,而是对于任何事情.GHC 只注册一个包的编译版本,所以源代码一般不会提供给 ghci.

No, it's not possible – not just for instances but for anything. GHC only registers the compiled version of a package, so the source code generally won't be available to ghci.

可能,大多数情况下你会使用 Hackage 的东西;在这种情况下,通过抓取模块,找到类或数据声明,然后点击 来源.

Probably, most often you'll be using stuff from Hackage; in that case it's very simple to find the source code of such instances by hoogling the module, locating the class or data declaration, and clicking on source.

当您无法访问互联网或其他任何原因而无法上网时,您首先需要了解该模块包含在哪个中.最简单的方法:

When you don't have internet access or whatever else reason you can't hoogle online, you first need to find out in what package the module is included. The easiest way to do that:

$ ghc-pkg find-module Data.Maybe
/usr/local/haskell/lib/ghc-7.6.2/package.conf.d
base-4.6.0.1
haskell2010-1.1.1.0
~/.ghc/x86_64-linux-7.6.2/package.conf.d

$ ghc-pkg find-module Data.Maybe
/usr/local/haskell/lib/ghc-7.6.2/package.conf.d
   base-4.6.0.1
   haskell2010-1.1.1.0
~/.ghc/x86_64-linux-7.6.2/package.conf.d

然后,正如我所说,GHC 不知道这些包的源代码在哪里——事实上,它甚至可能在您的系统上不可用!但是如果你已经用 cabal install 安装了这个包(或依赖它的包),它会在那里,默认在 ~/.cabal/packages/hackage.haskell.org/PᴀᴄᴋᴀɢᴇNᴀᴍᴇ(作为压缩档案,但这不是一个大障碍).在包项目文件夹中,您可以通过代表模块层次结构的目录结构简单地定位模块.

Then, as I said, GHC doesn't know where the source code to these packages is located – in fact it might not even be available on your system! But if you've installed the package (or one that depends on it) with cabal install, it will be there, by default in ~/.cabal/packages/hackage.haskell.org/PᴀᴄᴋᴀɢᴇNᴀᴍᴇ (as a compressed archive, but that's not a big hurdle). Within the package project folder, you can simply locate the module via the directory structure, which represents the module hierarchy.

其他软件包,例如您的 Data.Maybe 示例(软件包 haskell2010),可能与您安装的 GHC 一起提供,例如Haskell 平台.在这种情况下,我认为最简单的方法是在那里搜索 Haddock 文档文件.就我而言,

Other packages, like your example of Data.Maybe (package haskell2010), may have come right with your installation of GHC, e.g. the Haskell platform. In that case, I believe the easiest thing is to search there for the Haddock documentation file. In my case,

$ find/usr/local/haskell -name 'Data-Maybe.html' |头-n1 |xargs 火狐

$ find /usr/local/haskell -name 'Data-Maybe.html' | head -n1 | xargs firefox

这将打开 hoogle 将您链接到的内容(但在您的本地高清上),您还可以在其中以用户友好的方式浏览源代码.

That'll open up the equivalent to what hoogle links you to (but on your local HD), where you can also browse the source code in a user-friendly way.

这篇关于在 ghci 中查看特定类型的 Typeclass 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆