Search code examples
ocamltooling

how to search for OCaml functions by name and type


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.


Solution

  • 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.