How to read ocaml code with funny operators

I am reading a ocaml book which used two operators in a piece of example source code “>>=” and “>|=” I don’t know what they do.

My broader question is that what to do when you encounter such funny operators? I usually google but in the case of “>|=” even google could not find what does it do.

So what’s the strategy?

1 Like

These are monadic operators; >>= corresponds to bind, >|= corresponds to map (in whatever library/module is in scope for the snippets you were reading). Note that >>| is usually the map operator in Jane Street and affiliated libraries.

1 Like

~~ yes I was able to google that much. I was more interested in knowing what is the strategy and quick way of figuring operators out. Is there a way I can say “explain operator” in UTOP rather than googling? googling creates context switches and I am wasting time in the browser rather than reading my code.~~

I figure out things right after I type the question lol

here is what I was looking for

utop # (>>=);;
- : 'a t -> ('a -> 'b t) -> 'b t = <fun>
utop # (>|=);;
- : 'a t -> ('a -> 'b) -> 'b t = <fun>

So yes they are flatMap and map operations on a effect “t”.

Well, “operators” in OCaml are just functions that follow one of a couple of naming conventions to allow them to be used in an infix position; usually, they’re aliases for another function that has a “regular” name. As with all functions, the canonical way to learn about them would be to consult the docs for the module in question.

It’s interesting that you find that helpful; I suppose you have some background in e.g. Haskell?


More helpful would be if utop had a show directive that dumped the body of a function rather than just its signature, though perhaps that would require a bit too much lisp-esque reflection machinery.

1 Like

There is https://www.craigfe.io/operator-lookup/ that is nice resource for operator lookups for OCaml. The webpage covers the most common operators, and provides a short description and some examples on how to use them.

12 Likes

Sherlodoc can also help, e.g. searching for (>>=).

5 Likes

Thanks bnguyenvanyen, yes this is a great resource.

1 Like

Depends on the book but they should explain what the operators are before they use them. For example in Real World OCaml a couple of operators are introduced here: Concurrent Programming with Async - Real World OCaml

Writing out Deferred.bind explicitly can be rather verbose, and so Async includes an infix operator for it: >>= … Calling bind and return together is a fairly common pattern, and as such there is a standard shortcut for it called Deferred.map, which has the following signature:…and comes with its own infix equivalent, >>|.

If the book doesn’t provide an introductory explanation of the operators before using them, I would definitely give them feedback about that.

1 Like

Another way to discover these things, assuming you actually have the code compiling with its dependencies somewhere, is via Merlin. For example, vim has a function MerlinLocateIntf that takes you to a function (or other identifier’s) interface, which lets you see its type, documentation, and context in a larger module. And it has MerlinDocument to just see the documentation.

1 Like

I like what Purescript did with operators: you’re only allowed to have an operator if there’s a corresponding, tightly-coupled, named definition available. So whenever you look up an operator via whatever tool you use, you always get its named function counterpart as well.

While I imagine this is something we can’t enforce at language level for OCaml, we can make it a documentation discipline. For every module, one can do something like the following:

val (>>=) : ... (** see {!bind} *)
val bind : ... (** ... *)
2 Likes