Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to search for OCaml functions by name and type

Tags:

ocaml

tooling

In Haskell, there are two main ways to look up information on functions.

  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.


In OCaml, what sites/tools can be used to search for functions by name or type?

For example, I'm reading through Real World OCaml. I came across some code using the |> function. I wondered if there was a function <| for composing the opposite way. However, I don't know of any way of searching for a function called <|. Also, I don't know of any way of figuring out where |> is defined.

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

like image 980
illabout Avatar asked Feb 02 '17 13:02

illabout


2 Answers

awesome-ocaml has a section on dev tools that should be helpful.

  • ocamloscope (github) is sort of an Hoogle for OCaml. Search by name works well. Search by type is less good.
  • For local search by name, ocp-browser provides a convenient TUI.
  • In your editor, merlin and ocp-index can do lookup-to-definition and lookup-documentation.
  • There is a WIP public instance of odig here with a lot (but not all) packages. You can use odig locally too, as stated in another answer.

P.S. The function you are looking for is @@, and it's in the standard library.

like image 59
Drup Avatar answered Oct 01 '22 10:10

Drup


The ocp-index package provides a basic facility for searching API functions, e.g.,

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

The ocp-browser is a beautiful interface to this utility.

They are all integrated with Emacs (and other popular text editors). Speaking of text editors and IDE, Merlin is a killer feature without which I can't imagine OCaml coding anymore. It is capable of jumping directly to the definition, extracting documentation and incremental typechecking.

Speaking of web-based search, there was an argot document generator that has an API search engine featuring type search, full-text search, and regular expressions. A project is somewhat abandoned and doesn't work with the latest OCaml.

We forked it, update to the latest OCaml, fixed a few bugs, and enhanced the unification procedure to get a better type search. The result can be found here.

One of the main features is a search by type manifest, that ignores such irrelevant things as parameter ordering in functions, field names, differences between record names and tuples (e.g., string * int is the same as {name : string; age : int}) and aliasing. For example, in our project there are quite a few aliases, e.g., type bil = stmt list = Stmt.t list = Stmt.t Core_kernel.Std.list = .... You can choose any name when you search (using type manifest), as the algorithm will correctly unify all aliases.

like image 20
ivg Avatar answered Oct 01 '22 11:10

ivg