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

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

问题描述



例如, Maybe

是这样定义的:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b fmap f Nothing = Nothing

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



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

 前奏> :我也许
data也许a = Nothing |只是 - 在`Data.Maybe'中定义
实例Eq a => Eq(也许a) - 在`Data.Maybe'中定义
实例Monad Maybe - 定义于`Data.Maybe'
实例Functor Maybe - 定义于`Data.Maybe'
实例Ord a => Ord(也许a) - 在`Data.Maybe'中定义
实例读取a =>阅读(也许a) - 在`GHC.Read'中定义
实例Show a =>显示(也许a) - 在`GHC.Show'中定义

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

解决方案

不,不可能 - 不仅仅是为了实例,而是为了任何事情。 GHC只注册一个软件包的编译版本,所以源代码一般不会提供给ghci。



可能多数情况下,你会使用Hackage的东西;在这种情况下,通过搜索模块可以很容易地找到这些实例的源代码。 a>,找到类或数据声明,然后单击包。最简单的方法:


$ ghc-pkg find-module Data.Maybe

/ usr / local /haskell/lib/ghc-7.6.2/package.conf.d

ase-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ᴀᴍᴇ(作为一个压缩存档,但这不是一个很大的障碍)。在包项目文件夹中,您可以简单地通过代表模块层次结构的目录结构找到模块。



其他包,如 Data.Maybe (package haskell2010 ),可能在您安装GHC后会出现问题,例如Haskell平台。在这种情况下,我相信最简单的事情就是在那里搜索Haddock文档文件。在我的例子中,


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


这将打开相当于hoogle链接到您(但在本地HD上)的链接,您也可以以用户友好的方式浏览源代码。


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

For example, Maybe is defined like this:

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

Can I see this in ghci ?

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'

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 ?

解决方案

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.

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

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.

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' | head -n1 | xargs firefox

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天全站免登陆