Breakpoints in OCaml Argument Pass

Today I was looking at the source code of the Jane Street Core_kernel module and I came across the compose function:

 (* The typical use case for these functions is to pass in functional arguments and get functions as a result. For this reason, we tell the compiler where to insert breakpoints in the argument-passing scheme. *) let compose fg = (); fun x -> f (gx) 

I would define the compose function as:

 let compose fgx = f (gx) 

The reason they give a definition to compose is how they did it, because compose is a function that takes the functions f and g as arguments and returns the function fun x -> f (gx) , which they defined compose , as they did this to tell the compiler to insert a breakpoint after f and g , but before x in the argument passing scheme. "

I have two questions:

  • Why do we need breakpoints in the argument passing scheme?
  • What difference does it make if we define compose usual way?

Based on Haskell, this agreement does not make any sense to me.

+8
haskell ocaml ocaml-core
source share
2 answers

This is a performance hack to avoid the cost of a partial application in the expected use case mentioned in the comment.

OCaml compiles curry functions in a fixed structure using closure to partially apply it where necessary. This means that the challenges of this reality are effective - there is no closure construct, just a function call.

compose for fun x -> f (gx) will be building a closure, but it will be more efficient than a partial application. Closures created by a partial application go through the caml_curryN shell, which exists to ensure that effects occur at the right time (if this closure is partially applied).

The fixed arity that the compiler chooses is based on simple parsing — essentially, how many arguments are taken in a string without any intermediate. Programmers Jane St. used this to select the desirability they desire by entering () "between the arguments.

In short, let compose fgx = f (gx) is a less desirable definition because it will make the general argument with two arguments compose fg be a more expensive partial application.

Semantically, of course, there is no difference.

+3
source share

It is worth noting that compiling a partial application has improved in OCaml, and this performance hacker is no longer needed.

+2
source share

All Articles