Re:Functional programming in C++ (2012)
by bikeshaving on Monday December 28, 2020 @10:40PM
(#25566477)
If you’re interested in a language which does claim to optimize pure functions, take a look at Koka (https://koka-lang.github.io/koka/doc/index.html), a research language from Microsoft. It doesn’t implement any control flow besides conditionals and pattern matching; everything else is implemented with higher-order functions, and a cool syntax which allows trailing callback parameters to look like a block of code makes it look almost imperative. Additionally, they have a complex effect tracking system which tracks not only if a function is pure, but also things like if it might be non-terminating or throw errors. Somehow this is used to transform functional code into code which is about as fast as the imperative equivalent.
Ultimately, I think reasoning about function effects in such a fine-grained way probably won’t catch on, but it’s a great example of what you can do when you design compiler up. However, the original article, and most in production functional systems, are about attempting to carve out a functional system from imperative parts. The result is like you said, we’re executing unoptimized code.
The most important part of that explanation is can. The idea behind certain properties of FP, this being one of the most important ones, is that you are able to describe the results you want (somewhat declarative, moreso than imperative languages, less so than logic programming) and let the "compiler" or the "runtime" determine how that's delivered, without knowing or caring how it's executed. The problem with that is that the "compiler" or the "runtime" has to actually do the optimization, and if it doesn't, you're... executing unoptimized code!
Taking your React example, if it had appropriate heuristics, it could noop a ton of work and devs wouldn't have to even think about optimizing really simple good hygiene patterns. But it doesn't even look at your source code, it has no idea what weird stateful complex mess you're calling into, and it just assumes the worst.