Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why prefer currying to tuple arguments in OCaml?

"Introduction to Caml" says

Note, in Caml it is better to use Curried function definitions for multiple-argument functions, not tuples.

when comparing 'a -> 'b -> 'c calling conventions to 'a * 'b -> 'c.

When working with SML/NJ I got used to using tuple types for both input and output : ('a * 'b) -> ('c * 'd) so using tuples to express multiple inputs seems symmetric with the way I express multiple outputs.

Why is currying recommended for OCaml function declarations over tuple arguments? Is it just the greater flexibility that comes with allowing currying/partial evaluation, or is there some other benefit that derives from implementation details of the OCaml compiler?

like image 321
Mike Samuel Avatar asked May 19 '12 16:05

Mike Samuel


People also ask

What is currying in OCaml?

In OCaml a function can return another function. as a result; this is what currying is doing.

Is currying supported in scheme?

It is possible to generate curried functions in Scheme. The function curry2 generates a curried version of a function, which accepts two parameters. The curried version takes one parameter at a time. Similarly, curry3 generates a curried version of a function that takes three parameters.

What is currying FP?

In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.

What does Curry do in Haskell?

Currying is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.


2 Answers

Yes, it is mainly the notational convenience and the flexibility to do partial application. Curried functions are idiomatic in OCaml, and the compiler is likely to optimise them somewhat better than tupled functions (whereas SML compilers typically optimise for tuples).

The pros of tupling are the argument/result symmetry you mention (which is especially useful when composing functions) and perhaps the notational familiarity (at least for people coming from the non-functional world).

like image 145
Andreas Rossberg Avatar answered Oct 01 '22 18:10

Andreas Rossberg


Some comment about the optimisation in OCaml.

In OCaml, i noticed that tuples are always allocated when passing them as argument. Even if allocating in the primary heap is fast in ocaml, it is of course longer than doing nothing. Thus each time you pass a tuple as argument, there is some time spent to the allocation and filling of the tuple.

I expected that the ocaml compiler would be optimizing cases where it isn't needed to build the tuple. For instance, when you inline the called function, you may only use the tuple components and not the tuple itself. Thus the tuple could just be ignored. Unfortunately in this case OCaml doesn't remove the useless tuple and still perform the allocation. For this reason, it may not be a good idea to use tuples in critical sections of code.

like image 29
Valentin Perrelle Avatar answered Oct 01 '22 18:10

Valentin Perrelle