#show infix functions in utop


I’m new to OCaml. While exploring utop looking for modulo function, I tried to use #show mod;;, which produces syntax error. I found on SO that mod is defined as infix so I tried #show (mod);; and that works. I found this a bit confusing, is there any way to see in utop if the function is defined as infix?

OCaml doesn’t actually let you define infix functions as you want in general. Basically, you can only:

  1. Define a function with an alphanumeric identifier as in let ident123 a b = .... The function will be prefix.

  2. Define a function with ASCII symbols between brackets, as in let (++) a b = .... The function will be infix when you use it without the brackets as in a ++ b (though you can also do (++) a b).

If you try to define an infix function whose name is alphanumeric (let (ident) a b = ...), you get a syntax error.

At this point you might be wondering what about mod then? Well mod and the bitwise manipulation operators (lsl, lsr, land etc) are special-cased so that they are infix even though they’re alphanumeric. So the way you know if a function is infix, to answer your question, is just that you have to remember that mod and the bitwise operators (and nothing else I think) are infix.