Possibly Pythonic Tail Call Optimization (TCO/TRE)

Chris Angelico rosuav at gmail.com
Tue Jul 14 14:55:43 EDT 2015


On Wed, Jul 15, 2015 at 4:37 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Steven D'Aprano <steve at pearwood.info>:
>
>> Tail call behaviour is not undefined in Python. There is a strong
>> convention (followed by all implementations I know of) to follow the
>> reference implementation's (CPython) behaviour, which is not to
>> optimize tail calls. But even if an implementation chooses to not
>> follow that, it's not *undefined behaviour*. It's just
>> implementation-dependent behaviour.
>
> But in Scheme, tail call elimination is mandatory and thus can be relied
> on. Unless that promise is made, you can't write code that depends on

I think I'm beginning to understand. Tail call elimination is
necessary to cope with a system of programming that has deliberately
excluded certain other options (eg mutables, in pure functional
languages), but is not as important in a more fully-featured language.
Constant folding would be the same way; it's not absolutely critical,
and if Python doesn't support it for some particular expression then
it's not going to kill you, but in SPL [1] it would be a vital
efficiency improvement. Scheme needs TCE because of the way you have
to write code for Scheme to be even capable of executing it; Python
doesn't, because you can write your code to not need TCE.

[1] https://en.wikipedia.org/wiki/Shakespeare_(programming_language)#Constants_and_Assignment_of_Values

ChrisA



More information about the Python-list mailing list