How are comparison and arithmetic operators implemented in OCaml standard library?

The comparison and arithmetic operations are implemented with compiler primitives in the standard library.
For instance, the = operator is defined as

external ( = ) : 'a -> 'a -> bool = "%equal"

where %prim is the name of a compiler primitive. If you want to reimplement your own standard library, you should bind those compiler primitives rather than use the FFI to reach the underlying C implementations. Not only it would be more efficient (since the compiler optimizes compiler primitives and not some arbitrary C bindings), it would be less brittle than relying on an implementation of the stdlib. Also, the lazy semantics of && and || can only be achieved with compiler primitives and not a C binding.

2 Likes