F1, F2 ... inside elm core, what are they?

Hi everyone

I was just looking inside Elm/core and found a lot of F1, F2, functions. It seemed that I couldn’t find their origin where they’re defined. I’d appreciate any explanations about these functions and their philosophy for being there.

A quick aside: How do you usually find the definition of a function inside a pretty big code base? (e.g. by just looking at the import, include, … sections or you use grep/regex or some other fancy ways)

These are functions used internally (so, only in the JS, they are not defined in elm) to support partial function application.

Elm functions are compiled into javascript functions. But elm supports partial application. So we can say List.map String.toUpper and that is a value (a function that needs one other argument). In contrast, JS functions always need to either be not applied, or are evaluated when at least one argument is given. So elm functions cannot directly be translated to JS functions, because maybe the elm function is partially applied.

So elm introduces wrappers. F2 is a two-argument function, F3 is a 3-argument function, etc.
These functions return a wrapper around a JS function, that stores the arity (number of arguments that it needs to be fully applied). Then A2, A3 etc functions are used to partially apply arguments.
E.g. A3 is called with a wrapped function, and three arguments. When the wrapped function needs 3 more arguments to be fully applied, then those arguments are applied to the wrapped function, and the function is evaluated.

Otherwise, the function is re-wrapped, and the wrapper additionally stores the 3 (now partially applied) additional arguments.

7 Likes

It is worth saying that F2, … and A2, … are implementation details of the current version of the elm compiler. A different compiler could compile elm to javascript without using these functions.

3 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.