OCaml: why is the comparison operator agnostic of type, but arithmetic not?

I am wondering why the <operator supports int, string, bool or float, whereas + supports only int.

Can OCaml recognize basic arithmetic? What makes the comparison operator different from arithmetic? Is it the same for other FP languages?

+8
functional-programming ocaml
source share
2 answers

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:

 # let plus ab = a + b;; val plus : int -> int -> int = <fun> 

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 #

 # let plus ab = a + b val plus : a:int -> b:int -> int 

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:

 # let inline plus ab = a + b val inline plus : a: ^a -> b: ^b -> ^c when ( ^a or ^b) : (static member ( + ) : ^a * ^b -> ^c) 

This version will work for both int and float (and any other type (-combination) that has a static member (+) )

Haskell

 # let plus ab = a + b plus :: Num a => a -> a -> a 

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 .

+6
source share

There is currently no concept of “slightly polymorphic” in OCaml (technical name is “ad-hoc polymorphism”). You cannot say, "I accept integers and floats, but not the rest."

One can say, however, “I accept everything” as a comparison operator (the technical name is “parametric polymorphism”). However, note that they are a little deceiving: you cannot compare functions, even if the type system cannot catch it.

See this answer for more details.

+7
source share

All Articles