如何按名称和类型搜索OCaml函数

8
在Haskell中,查找函数信息有两种主要方法。
  1. Sites like Hoogle and Stackage. These sites provide two main types of searching:

    1. Searching for the name of a function. For example, here is a search on Hoogle for a function called catMaybes.

      This search returns the type of the catMaybes function, as well as the package and module it is defined in.

      The major use-case for this type of search is when you see a function used somewhere and you want to know its type and what package it is defined in.

    2. Searching for the type of a function. For example, here is a search on Hoogle for a function of type [Maybe a] -> [a].

      This search returns multiple functions that have a similar type, the first of which is catMaybes. It also returns the package and module catMaybes is defined in.

      The major use-case for this type of search occurs when you are writing code. You know the type of the function you need, and you're wondering if it is already defined somewhere. For example, you have a list of Maybes, and you want to return a list with all the Nothings removed. You know the function is going to have the type [Maybe a] -> [a].

  2. Directly from ghci. In ghci, it is easy to get information about a function with the :info command, as long as the function is already in your environment.

    For example, here is a ghci session showing how to get info about the catMaybes function. Note how you must import the Data.Maybes module first:

    > import Data.Maybe
    > :info catMaybes
    catMaybes :: [Maybe a] -> [a]   -- Defined in ‘Data.Maybe’
    >
    

    :info shows both the type of the catMaybes and where it is defined.


在OCaml中,可以使用哪些网站/工具按名称或类型搜索函数?
例如,我正在阅读Real World OCaml。我遇到了一些代码,使用|>函数。我想知道是否有一个函数<|以相反的方式组合。然而,我不知道任何搜索名为<|的函数的方法。此外,我不知道如何找出|>定义在哪里。

根据上面的链接代码,我猜想|>要么在Pervasives中,要么在Jane Street的Core中的某个地方,但是有一个工具能给出确切的位置就好了。


1
如果我没记错的话,merlin 可以向你展示正在使用的函数从哪些地方被定义(包括内置函数)。然而,我不知道有没有搜索功能。 - Bergi
3个回答

9

awesome-ocaml有一个关于开发工具的部分,应该会很有帮助。

  • ocamloscope (github) 类似于 OCaml 的 Hoogle。按名称搜索效果很好。按类型搜索效果较差。
  • 对于本地名称搜索,ocp-browser 提供了一个方便的 TUI。
  • 在您的编辑器中,merlin 和 ocp-index 可以进行查找定义和查找文档。
  • 这里有一个正在进行的 odig 的公共实例(链接)包含大量(但不是全部)的包。您也可以像另一个答案中所述那样,在本地使用odig

P.S. 您要查找的函数是 @@ ,它位于标准库中。


ocamlscope页面应该正常工作吗?我得到一个空白页面,上面写着“正在维护中”。 - illabout
目前似乎出现了问题,但是确实应该可以正常运行。 - Drup
1
ocamloscope仍然不可用。 - dardub
ocamloscope已经停止运行多年了。虽然有https://camlspotter.github.io/ocamloscope.html,但它似乎并没有真正发挥作用 :,( - Shon

5

ocp-index软件包提供了一个基本的API函数搜索工具,例如:

$ ocp-index locate '|>'
/home/ivg/.opam/devel/build/ocaml/stdlib/pervasives.ml:39:0

ocp-browser是这个实用程序的一个美丽接口。

它们都与Emacs(和其他流行的文本编辑器)集成。说到文本编辑器和IDE,Merlin 是一个必备功能,否则我无法想象使用OCaml编码。它能够直接跳转到定义,提取文档和增量类型检查。

谈到基于Web的搜索,曾经有一个argot文档生成器,其中包含一个API搜索引擎,支持类型搜索、全文搜索和正则表达式。该项目在某种程度上已被放弃,并且不再适用于最新的OCaml。

我们进行了forked,更新到最新的OCaml,修复了一些错误,并增强了统一过程以获得更好的类型搜索。结果可以在此处找到。

主要特点之一是按类型清单搜索,忽略诸如函数中的参数排序、字段名称、记录名称和元组之间的差异(例如,string * int{name:string; age:int}相同)以及别名等不相关的内容。例如,在我们的项目中有相当多的别名,例如 type bil = stmt list = Stmt.t list = Stmt.t Core_kernel.Std.list = ...。您可以在搜索时选择任何名称(使用类型清单),因为该算法将正确统一所有别名。


3

odig 可能会很有用。安装后,您可以按包和函数进行浏览。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接