News for hackers. Stuff that matters.
Re:Rust will likely not support tail call optimization by beagle3 on Wednesday April 10, 2013 @06:52PM (#5528761)
Please, for the sake of $DIETY, stop calling it "Tail Call Optimization". It is not an optimization, and the wrong term causes lots of useless discussions and misunderstanding. Call it "Tail Call Elimination", because that's what it is.

With finite memory (that is, in the real world), a program that relies on tail recursion will blow up if TCE is not implemented even though it can run forever with TCE. Thus, it changes actual program results (rather than just runtime speed or amount of memory required), and cannot be considered just an "optimization", since it is functionally required.

And to future-language designers: Please, for the sake of $DIETY2, make the syntax for guaranteed-eliminated-tail-call different than a regular call: It's really a bad idea that a function such as:

    def f(x):
       ...
       return 0+f(x-1)
cannot in general be TCEd (adding 0 to a floating point imporper denormal will make it proper), whereas

     def f(x):
        ...
        return f(x-1)
can. I suggest "chain y" instead of "return y" so that the compiler can verify that a TCE can indeed happen.
Re:Rust will likely not support tail call optimization by millstone on Wednesday April 10, 2013 @07:36PM (#5528960)
cannot be considered just an "optimization", since it is functionally required

Here is a C program:

    #include <stdio.h>
    int main(void) { while (1) malloc(64); }
With clang, when compiled with -O0, this consumes all the memory on my system. When compiled with -O2, it runs forever in constant memory, because clang has optimized out the call to malloc. If I replace the call with, say, 'malloc((size_t)(-1) >> 1)', it even changes the output of the program.

Here is another example:

    int factorial(long long x) { return x * factorial(x-1); }
With gcc, when compiled with -O2, the factorial function can correctly compute the factorial of a very large number. When compiled with -O0, it cannot.

I would call both of these optimizations, even though someone may rely on either for correctness. So I don't think tail calls are unique in this respect.

make the syntax for guaranteed-eliminated-tail-call different than a regular call

Agreed.

Re:Rust will likely not support tail call optimization by bcoates on Wednesday April 10, 2013 @09:51PM (#5529440)
Both of those programs are playing with C's infamously broad undefined behavior rules, the first for a non-terminating loop and the second for using too much stack (when called with a sufficiently large number). -O2 being nice to you is a coincidence, not something you should rely on.

In a language which prefers diagnosing programmer error over punting both should be errors.

Re:Rust will likely not support tail call optimization by baddox on Wednesday April 10, 2013 @08:15PM (#5529100)
In the finite world beagle3 talks about, don't all optimizations potentially qualify as "functionally required"? If the finite world you're operating in can only run a program that requires at most N steps ("steps" being deliberately vague "computing resources," like CPU cycles or memory usage), then any optimization that makes an (N+1)-step program complete in N steps qualifies as a "functional requirement."
Re:Rust will likely not support tail call optimization by kragen on Wednesday April 10, 2013 @08:10PM (#5529084)
The reason people are so interested in TCO/TCE is not just that someone might rely on them for correctness, which as you point out is true of any optimization, but rather that they are so predictable that you can omit looping constructs from your core language if you have them.
Re:Rust will likely not support tail call optimization by beagle3 on Friday April 12, 2013 @07:30AM (#5537832)
> even though someone may rely on either for correctness.

That someone is playing with fire. It's the kind of optimization that might be removed in a hotfix because it turns out to break something else.

It's even worse than relying on things like byte order -- and if you rely on them, you are definitely outside the realm of "C language", and into the realm of "Specifically LLVM v.37x patchlevel 2, on an SSE4 architecture"

Re:Rust will likely not support tail call optimization by gngeal on Wednesday April 10, 2013 @07:16PM (#5528847)
"Please, for the sake of $DIETY"

If you worship food, that is. But you're right, people calling proper tail calls "an optimization" should be shot. Those who survive the subsequent generation scavenging should be shot again. :-)

Re:Rust will likely not support tail call optimization by ericbb on Wednesday April 10, 2013 @07:49PM (#5529026)
Or follow Alef (http://www.cs.bell-labs.com/who/rsc/thread/ug.pdf) and use "become". The Alef become statement is a kind of generalized tail-call form because you can use it to drop the current stack frame from anywhere, not just in tail-position.
Re:Rust will likely not support tail call optimization by kibwen on Wednesday April 10, 2013 @07:34PM (#5528946)
As a historical note, this was also the original plan for Rust, where the presence of the `be` keyword rather than the `return` keyword would indicate tail-calling.
Re:Rust will likely not support tail call optimization by kragen on Wednesday April 10, 2013 @08:52PM (#5529222)
I suggest "chain y" instead of "return y" so that the compiler can verify that a TCE can indeed happen.

I think the rust syntax for this was `be y` instead of `return y`.

Re:Rust will likely not support tail call optimization by srean on Wednesday April 10, 2013 @10:14PM (#5529547)
If I remember correctly the new version of TCL will have special syntax to enforce TCE. Unfortunately I did not book mark the relevant online discussion.