Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is OCaml's (+) not polymorphic?

I am an OCaml newbie. I like OCaml's speed but I don't fully understand its design. For example, I would like the + operator to be polymorphic to support integer, float and so on.

Why do we need +.?

like image 896
z_axis Avatar asked Nov 05 '11 00:11

z_axis


People also ask

Does OCaml allow polymorphic functions?

In OCaml, higher-order and polymorphic functions are powerful tools for code reuse. Higher-order functions are those functions that take other functions as arguments or return functions as results, while polymorphic functions are functions that act over values with many different types.

What is polymorphism in Haskell?

Parametric polymorphism In Haskell, this means any type in which a type variable, denoted by a name in a type beginning with a lowercase letter, appears without constraints (i.e. does not appear to the left of a =>). In Java and some similar languages, generics (roughly speaking) fill this role.

What is :: In OCaml?

Note that :: is technically a type constructor which is why you can use it in both patterns and expressions.


2 Answers

I would like the '+' operator to be polymorphic to support integer, float and so on. Why do we need '+.'?

Excellent question. There are many subtle trade-offs involved here.

The advantages of not overloading operators (as in OCaml) are:

  • Type inference is simpler and more predictable.
  • Code is more composable: moving code from one place to another cannot affect its meaning.
  • Predictable performance: you always know exactly which function is being invoked.

The disadvantages are:

  • Number of different operators quickly gets out of control: + for int, +. for float, +/ for arbitrary-precision rationals, +| for vectors, +|| for matrices and the complex numbers, low-dimensional vectors and matrices, homogeneous coordinates etc.

Some alternatives are:

  • Closed ad-hoc polymorphism and equality types as in Standard ML: slightly more complicated type inference, code no longer composable but a good solution for a fixed number of pre-defined types.
  • Open ad-hoc polymorphism and equality types as in F#: same as SML but can be applied to user-defined types as well. Expressive, fast and bug free but overloading is limited to certain operators and run-time resolution is prohibited.
  • Type classes as in Haskell: expressive but complicated and unpredictably slow due to potential run-time resolution.
  • Implicit promotion as in C/C++: simple but causes bugs like 2.3/0 and 1/12*3.7.
  • Dynamic typing as in Lisp's numerical tower: unpredictably slow.
like image 50
J D Avatar answered Oct 07 '22 10:10

J D


OCaml does not support polymorphic operators (numeric or otherwise) other than comparison operators. The + versus +. thing removes a lot of subtle bugs which can crop up in converting different sizes of integers, floats, and other numeric types back and forth. It also means that the compiler always knows exactly which numeric type is in use, thus making it easier to recognize when the programmer has made incorrect assumptions about a number always having an integer value. Requiring explicit casting between numeric types may seem awkward, but in the long run, it probably saves you more time tracking down weird bugs than you have to spend to write that extra period to be explicit.

Aside from the . versions of the numeric operators, I do not think that the OCaml syntax is particularly strange. It is very much in line with previous ML languages with appropriate and reasonable syntax extensions for its added features. If it initially seems odd to you, that probably simply indicates that you have been, thus far, only been programming in languages with closely related syntax. As you learn new languages, you will see that there are many different ways to have language syntax with different benefits and detriments, but a lot of it is just arbitrary conventions which someone decided on.

like image 36
Keith Irwin Avatar answered Oct 07 '22 11:10

Keith Irwin