473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Why no tailcall-optimization?

Why doesn't Python optimize tailcalls? Are there plans for it?

I know GvR dislikes some of the functional additions like reduce and
Python is supposedly about "one preferrable way of doing things" but
not being able to use recursion properly is just a big pain in the
a**.

Sep 23 '08 #1
6 1289
On Sep 22, 8:13*pm, process <circularf...@gmail.comwrote:
Why doesn't Python optimize tailcalls? Are there plans for it?

I know GvR dislikes some of the functional additions like reduce and
Python is supposedly about "one preferrable way of doing things" but
not being able to use recursion properly is just a big pain in the
a**.
I didn't think this through completely-- is it incompatible with
closures and local function definitions?

def f( m ):
def g( n ):
return m+ n
stuff( )
return g( 0 )

In this case, the stack, growing up:

g
f
main

is not equivalent to:

g
main

in the last step, due to the local definition of 'm' in 'f'.
Sep 23 '08 #2
On Sep 22, 9:13 pm, process <circularf...@gmail.comwrote:
Why doesn't Python optimize tailcalls? Are there plans for it?

I know GvR dislikes some of the functional additions like reduce and
Python is supposedly about "one preferrable way of doing things" but
not being able to use recursion properly is just a big pain in the
a**.
There are some attempts, see for example
http://code.activestate.com/recipes/496691/
Sep 23 '08 #3
process wrote:
Why doesn't Python optimize tailcalls? Are there plans for it?
I started to write an article on this but it disappeared....
So short answer:
1. Unless down very carefully, in a way that would slow things down, it
would change the semantics of Python.
2. It is usually trivial to convert to a while loop, which amounts to
in-frame recursion. If using tail-recursion requires a nested inner
function to avoid repeating one-time initialization or exposing the
cumulation variable, writing the loop is easier since no nested function
is needed.
3. In Python, for loops are usually better for processing iterables,
which covers most uses of induction (recursion/iteration).

Terry Jan Reedy

Sep 23 '08 #4
On Sep 22, 9:13*pm, process <circularf...@gmail.comwrote:
Why doesn't Python optimize tailcalls? Are there plans for it?
The main technical difficulty is that the compiler has to know whether
the function returns a tail call or not at compile time.

But because Python is fully dynamic with regard to type, the compiler
never(**) knows anything about any object other than "it's an
object". The compiler doesn't even know if the object is callable.

Probably it would be possible to achieve this optimization without
involving the compiler, but it'd be at cost of great complexity and
probably would negatively affect ordinary function calls (which are
already slow enough).

(BTW, if you're just talking about converting simple tail-recursive
functions, and not about general tail-call optimization, then I'd
suggest a third-party package would be better for that, since it would
be a pretty complex thing that would benefit only a tiny fraction of
users.)
Carl Banks
(**) Actually the compiler can do some compile-time constant
expression folding, but that's about it. Otherwise it's object
agnostic.

Sep 23 '08 #5
On Sep 23, 3:13*am, process <circularf...@gmail.comwrote:
Why doesn't Python optimize tailcalls? Are there plans for it?
Because Python is a dynamic language. While a function is executing,
its name may be bound to another object. It may happen as a side
effect of what the function is doing, or even from another thread. The
compiler has no way of knowing that.

Recursion can always be expressed as iteration, possibly with a Python
list as stack.

Sep 23 '08 #6
On Sep 22, 7:13*pm, process <circularf...@gmail.comwrote:
Why doesn't Python optimize tailcalls? Are there plans for it?

I know GvR dislikes some of the functional additions like reduce and
Python is supposedly about "one preferrable way of doing things" but
not being able to use recursion properly is just a big pain in the
a**.
Eliminating tail calls affects the semantics of your program (by
changing memory complexity). As an optimization, it's a side-effect
of the implementation, which is a particularly nasty kind of side-
effect.

Even if guaranteed it's fairly subtle. "return x()" would be
optimized while "return x() + 1" would not. Or maybe you're calling a
function that uses a wrapper and the wrapper isn't tweaked right to be
optimized.

Recursion in general can be very useful, but not tail-recursion. The
only time it's not trivial to convert tail-recursion into a loop is
when it involves multiple functions, but even that can be done if you
know how.
Sep 23 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.