Skip to main content
 首页 » 编程设计

haskell之如何在 Haskell/GHCi 中查看函数的定义

2024年06月03日37davidwang456

我正在使用 Haskell 2010.1.0.0.1 和 GHC 6。在 GHCi 提示符下键入 :t ,然后输入函数名称,即可显示该函数的类型。有没有办法也查看函数定义?

请您参考如下方法:

目前还没有。

最接近您想要的命令是 :info

:info name ...

Displays information about the given name(s). For example, if name is a class, then the class methods and their types will be printed; if name is a type constructor, then its definition will be printed; if name is a function, then its type will be printed. If name has been loaded from a source file, then GHCi will also display the location of its definition in the source.

For types and classes, GHCi also summarises instances that mention them. To avoid showing irrelevant information, an instance is shown only if (a) its head mentions name, and (b) all the other things mentioned in the instance are in scope (either qualified or otherwise) as a result of a :load or :module commands.

像这样:

Prelude> :info ($) 
($) :: (a -> b) -> a -> b   -- Defined in GHC.Base 
infixr 0 $ 

不过,您可以在 Hackage 上查看 haddock 工具生成的标识符的来源。

  1. Look up the module on Hackage
  2. Click on the source link

请注意,“?src” lambdabot 中 #haskell IRC channel 上的有效命令,并且可以执行您所期望的操作。

> ?src ($) 
> f $ x = f x