How to search for OCaml functions by name and type

Haskell has two main ways of finding function information.

  • Sites like Hoogle and Stackage . These sites provide two main types of search:

    • Search function name. For example, here is a Hoogle search for a function called catMaybes .

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

      The main use case for this type of search is when you see some function used somewhere, and you want to know its type and which package it is defined.

    • Search for function type. For example, here is a Hoogle search for a function like [Maybe a] -> [a] .

      This search returns several functions of the same type, the first of which is catMaybes . It also returns the package and the catMaybes module catMaybes defined in.

      The main use case for this type of search occurs when you write code. You know the type of function you need, and you are wondering if it is already defined somewhere. For example, you have a Maybe s list, and you want to return a list with all Nothing deleted. You know that the function will be of type [Maybe a] -> [a] .

  • Directly from ghci . In ghci it is easy to get information about a function using the command :info if the function is already in your environment.

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

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

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


In OCaml, what sites / tools can I use to search for features by name or type ?

For example, I read Real World OCaml . I met some code using the |> function. I wondered if the function <| for making the return trip. However, I do not know how to look for a function named <| . In addition, I do not know how to determine the method for determining |> .

Based on the linked code above , I think that |> should have been in Pervasives or somewhere in Jane Street Core, but it would be nice to have a tool that gave the exact location.

+6
source share
3 answers

awesome-ocaml has a section on development tools that should be useful.

  • ocamloscope ( github ) is a kind of Hoogle for OCaml. Searching by name works well. Search by type is less good.
  • Ocp-browser provides a convenient TUI for local searches by name.
  • In your editor, merlin and ocp-index can search and define and documentation.
  • There is a public copy of WIP oig here with a lot of (but not all) packages. You can use odig locally as indicated in another answer.

PS The function you are looking for is @@ and it is in the standard library.

+7
source

odig may be helpful. after installation, you can browse by package and by function.

+2
source

The ocp-index package provides a basic tool for finding API functions, such as

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

ocp-browser is a great interface for this utility.

All are integrated with Emacs (and other popular text editors). Speaking of text editors and IDEs, Merlin is a killer feature, without which I can no longer imagine OCaml coding. He is able to go directly to the definition, retrieve documentation and incremental type checking.

Speaking of web search, the argot document generator appeared, which has an API search engine, text search and regular expressions. The project is somewhat abandoned and does not work with the latest OCaml.

We forked it, updated it to the latest OCaml, fixed several bugs and improved the unification procedure to get a better search by type. The result can be found here .

One of the main features is the search manifest by type, which ignores such irrelevant things as the order of parameters in functions, field names, differences between record names and tuples (for example, string * int matches {name : string; age : int} ) and smoothing. For example, in our project there are many aliases, for example, type bil = stmt list = Stmt.t list = Stmt.t Core_kernel.Std.list = ... You can select any name during the search (using the type manifest), since the algorithm will correctly combine all the aliases.

+2
source

Source: https://habr.com/ru/post/1014648/


All Articles