r/coding • • Apr 12 '21

How Tail Call Optimization Really Works - Forget About Recursive Functions

https://eklitzke.org/how-tail-call-optimization-works
33 Upvotes

5 comments sorted by

0

u/Paddy3118 Apr 13 '21

"Forget about those recursive functions that can be tail-call optimised in a language that supports the optimisation after ensuring the recursion is able to be so-optimised."

2

u/[deleted] Apr 13 '21

There's probably a debate about the merits of an abstraction which requires TCO in order NOT TO CRASH, where TCO is entirely implicit and hidden properly of the recursive call. I.e. you need to know when it works (tail) and how it works (turns it into a loop with mutable iteration vars) in order to use it effectively.

And this is why pure functional languages rub me the wrong way. They have so many excellent ideas, which other languages have stolen (like lamdba, streams filter/map/reduce and so on). But they have these flawed abstraction aspects they refuse to let go of.

The first functional language to go fully mainstream will be one that deals with iteration and mutability in a honest, explicit way. Not like this.

1

u/aseigo Apr 15 '21

TCO is entirely implicit and hidden properly of the recursive call.

The last thing I want to have to do is think about how the compiler may or may not work. I also do not want to have to guess at my data input size, stack size, etc. in order to make these assumptions.

(... and just as it is true that TCO is very useful as it is true that TCO is sometimes unnecessary even when TCO is possible .. and it's usually the runtime env that has the information necessary to make this call.)

One of the reasons why declarative programming is rather nice, and works so well that imperative languages are picking up as much of it as they can (as you noted), is that you don't tell the system how to do thing, but what to do and it figures it out.

You're essentially arguing against what makes the style of programming useful. So useful that it's fundamentally changed how imperative languages are used.

that deals with iteration and mutability in a honest, explicit way.

"honest" is a loaded word that doesn't mean anything in this context. Explicit is meaningful, though.

There are FP languages which allow rebinding of variables (so they are still immutable, but you can re-use variables, which ends up looking quite similar sans many of the foot-guns), and when you wish to assert that the variable should be matched and not rebound they have 'pin' operators (or similar) to note that the command-case rebind is not what should happen.

Similarly for iteration: you rarely write recursive functions to iterate in most FP languages (there are exceptions to that in practice, but they are exceptions and usually with good reasons). Instead, one typically uses a higher-level construct like comprehensions and/or enumerables.

0

u/Mysquff Apr 13 '21

TCO is entirely implicit and hidden properly of the recursive call

Scala has a nice feature that addresses this problem:

classtailrec extends Annotation with StaticAnnotation

A method annotation which verifies that the method will be compiled with tail call optimization.

If it is present, the compiler will issue an error if the method cannot be optimized into a loop.

I wish other (not only functional) languages had features like this.

0

u/[deleted] Apr 13 '21

Yeah explicit is the way.

I this this is one reason that blocked TCO from JavaScript as well. They wanna add it, but with explicit syntax.