This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] Why are arithmetic functions not polymorph?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Brian Hurt Subject: Re: [Caml-list] Why are arithmetic functions not polymorph?
```On Fri, 23 May 2003, hermanns wrote:

> Hello,
>
> I'm new to OCaml, so I hope my question is not to stupid.
>
> Can anybody explain to me why the arithmetic functions ('+', '-', ...)
> are not polymorph
> (in the sense that they have floating point equivalents '+.', '-.',
> ...).
> I don't understand this, because comparison funtions ('<', '>', ...)
> are polymorph.
> So, where is the problem with arithmetic functions?
>

Type inference is the short answer.  Consider the function:

let foo a b = a + b

Without operator overloading, the type this function has is clear- + only
operates on integers, so it's type is int->int->int.  Were I to write:

let foo a b = a +. b

now the type is clearly float->float->float.

The comparison operators call a generic- and comparitively expensive-
comparison function.  If you know that you are only going to be comparing
(for example) ints, it's oftentimes faster to explicitly state the types
involved, like:

let foo (a: int) (b: int) = a < b

This allows the compiler to replace the call to the expensive comparison
function with a cheap inline integer comparison.

Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```