Comparison operators are special in OCaml. They can recursively compare records, variant types, lists, and other data structures.
They bypass the type system (I hope I have formulated it correctly) and have special support at runtime. I would recommend reading this post about OCaml polymorphic comparison.
Comparison operators < , > , <> , = have the same signature:
'a -> 'a -> bool
And they always return bool regardless of the type of input arguments. Thus, these operators can have polymorphic behavior and do not require additional support by the type system for type inference:
Function type less
let less ab = a < b
automatically deducted on
val less : 'a -> 'a -> bool = <fun>
At the same time, the return type of arithmetic operators depends on the type of arguments:
See, you cannot automatically infer the type of the expression a + b in the case of the polymorphic operator + . To support this, the type system should be expanded.
F #
Subtracted by default int . To be able to write the plus function for float, you need to add an explicit type annotation to at least one of the input arguments:
# let plus (a:float) b = a + b val plus : a:float -> b:float -> float
If you add inline , you can get even more:
This version will work for both int and float (and any other type (-combination) that has a static member (+) )
Haskell
The return types and arguments of the plus function are automatically inferred for any type of class of type Num . This means that plus is truly polymorphic and can be used with ints, float, and other numeric types.
OCaml
Thus, this may be another special case (for example, in F #) or a fully functional solution such as classes of type Haskell. I vote for the latter.
There is an OCaml plug that solves this problem by expanding its modular implications .