On this page

OCaml Custom Operators and Precedence Rules

Published on August 8, 2026

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Almost any short sequence of symbol characters can become a genuine operator in OCaml, usable infix between two values or prefix before one, exactly like the built-in + or ^. What makes this workable rather than chaotic is that an operator's precedence and associativity are never chosen by whoever defines it — they're determined mechanically, by the very first character of the operator's symbol, according to a fixed set of rules built into the language.

Operators Are Just Functions With Unusual Names​

An operator is defined exactly like any other function, just with a symbolic name wrapped in parentheses instead of an ordinary identifier:


let crumb a b = a ^ " > " ^ b

let ( &> ) = crumb

let ( @> ) = crumb

crumb joins two path segments with a separator; &> and @> are both just that same function under a different, symbolic name. Defining an operator this way, in two steps — an ordinary named function first, then a symbolic alias for it — is generally the clearer approach: it gives the operator a plain-English name for documentation and error messages, and makes clear at a glance that the operator is nothing more than shorthand for an already-understood function, not some entirely new piece of syntax.

Referring to an existing operator as a plain function works the same way in reverse, by wrapping its symbol in parentheses:


# (+);;

- : int -> int -> int = <fun>

# ( * );;

- : int -> int -> int = <fun>

Multiplication needs the extra spacing around * specifically because (* is also how a comment begins in OCaml — without the spaces, (*) would be parsed as the start of an (unterminated) comment rather than a reference to the multiplication function.

Which Characters Are Actually Allowed​

Not every symbol qualifies as a valid operator name. An infix operator's first character has to come from a fixed set — $ & * + - / = > @ ^ | % < # — and, aside from #, every character after the first is optional and drawn from a similarly fixed set of symbol characters. A prefix operator instead has to begin with ?, ~, or !, generally followed by at least one more symbol character when starting with ? or ~. This is what makes &>, @>, and !! all valid operator names, while something built from ordinary letters or unlisted punctuation simply isn't accepted as operator syntax at all.

Precedence and Associativity Are Decided by the First Character​

Two operators can share an identical implementation and still behave completely differently once chained together, purely because of how their first characters are classified. Chaining &> three times in a row:


# "a" &> "b" &> "c";;

- : string = "a > b > c"

produces a flat-looking left-to-right result, while chaining @> the exact same way:


# "a" @> "b" @> "c";;

- : string = "a > b > c > "

produces a subtly different structure underneath, because &> and @> associate in opposite directions even though both were defined identically as aliases for crumb. &>, starting with &, is left-associative, so "a" &> "b" &> "c" is grouped as ("a" &> "b") &> "c". @>, starting with @, is right-associative, so "a" @> "b" @> "c" is grouped as "a" @> ("b" @> "c") instead — and since crumb isn't associative in the mathematical sense (grouping genuinely changes the result once more than two segments are involved), the two versions can diverge once three or more values are chained together, purely as a consequence of which character the operator happened to start with.

The complete rule groups operators into five precedence bands, ordered from lowest to highest precedence, determined entirely by an operator's first character:

An operator starting with one of the higher-precedence characters binds more tightly than one starting from a lower band, exactly the same way * already binds more tightly than + in ordinary arithmetic — this five-band system is really just that same familiar idea, generalized to cover every symbol sequence a custom operator could be built from, rather than a handful of hardcoded built-in cases.

Prefix Operators​

A prefix operator sits before a single argument rather than between two, and is a natural fit for a short, frequently used unary transformation:


let ( ~< ) = List.rev

# ~< [1; 2; 3];;

- : int list = [3; 2; 1]

Prefix operators always bind more tightly than any infix operator, which is exactly the behavior expected from unary operators generally — ~< [1; 2; 3] @ [4] applies ~< to the list before the @ ever runs, without needing extra parentheses to force that order.

Binding Operators Fit This Same System​

A special category of operator, written let* ... in ..., let+ ... in ..., and similar, lets a custom binding-like syntax be defined for a specific type — most commonly used to chain together a sequence of functions that might each fail, using a type's own bind function as the implementation underneath. Defining ( let* ) = Option.bind (or the equivalent for Result) makes let* available as ordinary syntax, resembling a normal let binding while actually threading values through bind at each step. This is a distinct category from the ordinary infix and prefix operators covered above — its precedence and parsing rules are fixed by its own binding-operator syntax, rather than by the first-character rules that apply to +, @>, or ~<.

Practical Guidance Before Defining One​

A few habits keep custom operators from becoming a maintenance liability rather than a readability win. Checking whether a symbol is already in use — by wrapping it in parentheses in the toplevel and seeing whether it resolves to an existing function rather than an "unbound value" error — avoids accidentally shadowing something already meaningful elsewhere. Keeping a custom operator's scope narrow, local to the module or even the single function that needs it, limits how far a reader has to search to find out what an unfamiliar symbol means. And restraint matters more than cleverness here: a codebase with a small number of well-chosen, well-documented operators stays approachable, while one with many terse, overlapping symbols quickly becomes harder to read than the ordinary named functions it was meant to shorten.

Key Takeaways​

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Labeled & Optional Parameters

Next

Polymorphic Variants