Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml type of the plus operator

Tags:

ocaml

Why is the type of a plus ( + ) considered to be int -> int -> int as opposed to (int * int) -> int? To me, the second makes sense because it "accepts" a 2-tuple (the addends) and returns a single int (their sum).

Thank you!

like image 490
Muhammad Khan Avatar asked Jul 03 '12 01:07

Muhammad Khan


People also ask

What is the operator in OCaml?

Comparisons (Relational Operators) OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality. Beware: <> is structural not-equals while != is physical not-equals.

What does :: mean in OCaml?

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ). It is however worth noting that the symbol can be used in two different ways and it is also interpreted in two different ways by the compiler.

Does OCaml use ==?

There are two equality operators in OCaml, = and == , with corresponding inequality operators <> and !=


1 Answers

You can make a language where (+) has the type (int * int) -> int. In fact, SML works exactly this way. It just affects the meaning of infix operators. However OCaml conventions strongly favor the use of curried functions (of the type a -> b -> c) rather than uncurried ones. One nice result is that you can partially apply them. For example ((+) 7) is a meaningful expression of type int -> int. I find this notation useful quite often.

like image 79
Jeffrey Scofield Avatar answered Nov 11 '22 20:11

Jeffrey Scofield