473,772 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proper tail recursion

Is it feasable, and/or desirable to have Python optimize tail-recursive
calls, similar to Scheme and gcc -O2? i.e. effectively compile this:

def foo(n):
return foo(n-1)

into this:

def foo(n):
goto foo(n-1)

This would naturally only be enabled in optimized bytecode.

On an unrelated, Scheme-ish note, is there any way for a generator to
access itself? i.e. I want to be able to write this:

def gen():
yield get_current_con tinuation(),som eothergenerator ()

Thanks,
Chris

Jul 18 '05 #1
10 3075
Christopher T King <sq******@WPI.E DU> wrote in
news:Pi******** *************** *************** @ccc4.wpi.edu:
Is it feasable, and/or desirable to have Python optimize tail-recursive
calls, similar to Scheme and gcc -O2? i.e. effectively compile this:

def foo(n):
return foo(n-1)

into this:

def foo(n):
goto foo(n-1)

This would naturally only be enabled in optimized bytecode.

On an unrelated, Scheme-ish note, is there any way for a generator to
access itself? i.e. I want to be able to write this:

def gen():
yield get_current_con tinuation(),som eothergenerator ()

Thanks,
Chris


Just because a function looks recursive to you, doesn't mean it actually is
recursive when it gets called.

How do you propose to handle this?

def foo(n):
return foo(n-1)

def baz(n):
return 0

bar = foo
foo = baz
bar(3)

One solution might be to provide a keyword which refers to the currently
executing function: this would be useful not only for recursion but also to
access attributes on the function. These help with methods and generators
which as you noted find it hard to reference themselves.

However, I don't believe there is at present any easy way to get hold of
the current executing function or generator object.
Jul 18 '05 #2
Duncan Booth <me@privacy.net > wrote:
Christopher T King <sq******@WPI.E DU> wrote in
news:Pi******** *************** *************** @ccc4.wpi.edu:
Is it feasable, and/or desirable to have Python optimize tail-recursive
calls, similar to Scheme and gcc -O2? i.e. effectively compile this:

Just because a function looks recursive to you, doesn't mean it actually is
recursive when it gets called. How do you propose to handle this? def foo(n):
return foo(n-1) def baz(n):
return 0 bar = foo
foo = baz
bar(3)
Instead of doing tail recursion elimination, one could implement
general tail call optimization. It shouldn't be too difficult to
make the compiler recognize tail calls, i.e

return spam(params)

outside any try statement. It could then generate the byte code
TAIL_CALL instead of CALL_FUNCTION. TAIL_CALL would throw away
the current stack frame before calling spam().

This doesn't give you the performance improvement of not making a
function call, but at least it makes a tail recursive function
run in constant space.

Of course, some care needs to be taken to ensure that the current
stack frame isn't thrown away if the called function is a local
function accessing the namespace of the calling function. I.e,
in the case

def parrot(x):
def spam(n):
return n + y
y = x*x
return spam(5)

the call to 'return spam(5)' must make sure that 'y' is
accessible to spam(). That might need a runtime check in many
cases. (In the common case of there being no local functions at
all in the caller, it can be determined statically, of course.)
Hmm, there is an attribute 'func_closure' on function objects,
that at a quick glance seems to be None if it doesn't access the
surrounding namespace.

However, I don't believe there is at present any easy way to get hold of
the current executing function or generator object.


Raising and catching an exception, and digging through the
traceback object might give you enough information to do
that. But it's not very pretty, and I'm not sure that it
is portable between C-Python, Jython, PyPy, Psyco, and any
other Python compiler/interpreter.
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"The one who says it cannot be done should ! bellman @ lysator.liu.se
never interrupt the one who is doing it." ! Make Love -- Nicht Wahr!
Jul 18 '05 #3
On Tue, 6 Jul 2004, Thomas Bellman wrote:
Duncan Booth <me@privacy.net > wrote:
Just because a function looks recursive to you, doesn't mean it actually is
recursive when it gets called.
How do you propose to handle this?

def foo(n):
return foo(n-1)

def baz(n):
return 0

bar = foo
foo = baz
bar(3)


Instead of doing tail recursion elimination, one could implement
general tail call optimization. It shouldn't be too difficult to
make the compiler recognize tail calls, i.e

return spam(params)

outside any try statement. It could then generate the byte code
TAIL_CALL instead of CALL_FUNCTION. TAIL_CALL would throw away
the current stack frame before calling spam().

This doesn't give you the performance improvement of not making a
function call, but at least it makes a tail recursive function
run in constant space.


What he said. :) I don't mean to optimize recursive functions into loops,
but simply to replace normal calls at the end of functions with tail
calls.
Of course, some care needs to be taken to ensure that the current
stack frame isn't thrown away if the called function is a local
function accessing the namespace of the calling function.
[snip]


I'm just guessing here (my knowledge of the internals isn't too great),
but in the case of closures, isn't it okay to throw away the stack space,
since any variables needed are kept in the function's func_closure
attribute? To modify your example:

def parrot(x):
def spam(n):
return n + y
y = x*x
return spam

print parrot(3)(5)

This returns 28, as expected, and is (roughly) functionally no different
from a tail-callified version of your parrot() function.
However, I don't believe there is at present any easy way to get hold of
the current executing function or generator object.


Raising and catching an exception, and digging through the
traceback object might give you enough information to do
that. But it's not very pretty, and I'm not sure that it
is portable between C-Python, Jython, PyPy, Psyco, and any
other Python compiler/interpreter.


I don't need it that badly ;) -- it just would have made a neat
generaliztion in my code, rather than special-casing a yield of 'None' to
refer to the generator that yielded it.

Jul 18 '05 #4
Thomas Bellman <be*****@lysato r.liu.se> wrote in
news:cc******** **@news.island. liu.se:
Instead of doing tail recursion elimination, one could implement
general tail call optimization. It shouldn't be too difficult to
make the compiler recognize tail calls, i.e

return spam(params)

outside any try statement. It could then generate the byte code
TAIL_CALL instead of CALL_FUNCTION. TAIL_CALL would throw away
the current stack frame before calling spam().

That ought to be doable. The main disadvantages I can see are that stack
backtraces will be incomplete (which could be confusing), and unbounded
recursion won't be caught (which may or may not matter).
This doesn't give you the performance improvement of not making a
function call, but at least it makes a tail recursive function
run in constant space.

Of course, some care needs to be taken to ensure that the current
stack frame isn't thrown away if the called function is a local
function accessing the namespace of the calling function. I.e,
in the case

def parrot(x):
def spam(n):
return n + y
y = x*x
return spam(5)

the call to 'return spam(5)' must make sure that 'y' is
accessible to spam().


No it doesn't. References to 'y' in parrot are handled indirectly by
storing the value in a cell object. The stack frame only holds a reference
to the cell object. So long as the cell still has another reference the
value remains valid.
However, I don't believe there is at present any easy way to get hold
of the current executing function or generator object.


Raising and catching an exception, and digging through the
traceback object might give you enough information to do
that. But it's not very pretty, and I'm not sure that it
is portable between C-Python, Jython, PyPy, Psyco, and any
other Python compiler/interpreter.

I'm pretty sure that there is nothing useful in the traceback object. You
can find out which instruction was executing, but this doesn't help in
identifying the function since several functions could share the same code
(an active generator being an obvious example where this happens).
Jul 18 '05 #5
Poking around in ceval.c, I discovered the PREDICT macros, and plan to
make a preliminary implementation of tail calls using a similar test (use
tail code call for any CALL_FUNCTION followed by a RETURN_VALUE). But I
was just wondering, is there any reason why LOAD_CONST and friends don't
check for a following RETURN_VALUE to avoid the push/loop/pop cycle and
rather simply return the value?

Jul 18 '05 #6
On Wed, 7 Jul 2004, Christopher T King wrote:
Poking around in ceval.c, I discovered the PREDICT macros, and plan to
make a preliminary implementation of tail calls using a similar test (use


I take that back. The intertwinement of C functions and Python calls on
the stack makes this nearly impossible -- there's no easy way to pop both
the C and Python stacks before calling the next function (it could be
partially done popping only the Python stack, though). Methinks tail calls
will have to wait for Stackless.

Jul 18 '05 #7
Christopher T King wrote:
I take that back. The intertwinement of C functions and Python calls on
the stack makes this nearly impossible -- there's no easy way to pop both
the C and Python stacks before calling the next function (it could be
partially done popping only the Python stack, though). Methinks tail calls
will have to wait for Stackless.


Here I am, replying to myself again :)

I'm trying a new implementation, making good use of a loop around the
entirety of PyEval_EvalFram e(). Unfortunately, my changes cause the
interpreter to segfault after a couple of tail calls. A debugger shows
that 0xffffffff is getting stuck into a pool->freeblock in
PyObject_Free() , and then is subsequently dereferenced in
PyObject_Malloc () (causing the segfault).

Could this be caused by my code Py_DECREFing an object too many times,
but leaving a pointer to it somewhere? (My changes don't explicitly set
anything to 0xffffffff or -1.) Or am I just in way over my head? :P
Jul 18 '05 #8
[Chris King]
I'm trying a new implementation, making good use of a loop around the
entirety of PyEval_EvalFram e(). Unfortunately, my changes cause the
interpreter to segfault after a couple of tail calls. A debugger shows
that 0xffffffff is getting stuck into a pool->freeblock in
PyObject_Free() , and then is subsequently dereferenced in
PyObject_Malloc () (causing the segfault).

Could this be caused by my code Py_DECREFing an object too many times,
but leaving a pointer to it somewhere? (My changes don't explicitly set
anything to 0xffffffff or -1.) Or am I just in way over my head? :P


WHen fiddling with Python internals, build Python in debug mode. That
enables many checks that will save you weeks of debugging. For
example, if you decref an object too many times, the expansion of the
Py_DECREF macro in a debug build will catch that and complain the
instant the refcount goes negative. In a debug build pymalloc also
pads both ends of allocated regions with special byte patterns,
initializes newly allocated memory with another special byte pattern,
and overwrites newly freed memory with a third special byte pattern.
That's very effective at catching many kinds of problems too. Read
Misc/SpecialBuilds.t xt.

And yes, of course you're in way over your head. But that's how you
learn to swim, so enjoy it <wink>.
Jul 18 '05 #9
Tim Peters wrote:
WHen fiddling with Python internals, build Python in debug mode. That
enables many checks that will save you weeks of debugging.


Thanks! I found the bug -- I was just forgetting to reset the stack
after a call_function.

I have a preliminary implementation ready, the patch is against 2.4a1:
http://users.wpi.edu/~squirrel/temp/tailcall.diff.gz

This patch only works for simple functions (i.e. those that can be
handled by fast_function), but extension to other function types should
be trivial. I haven't fully tested this with regards to exception
handling and whatnot, so I want to stick with a simple case for now.

The patch works roughly as follows:
1. When a CALL_FUNCTION opcode is encountered, check if the following
opcode is RETURN_VALUE. If so, execute the tail call code.
2. The tail call code calls a modified version of call_function that
sets up and returns a frame object for the function to be called (if
possible), rather than recursively calling PyEval_EvalFram e. This frame
is stored in a temporary variable, and a RETURN_VALUE is simulated to
exit the loop.
3. After cleaning up the current frame, PyEval_EvalFram e loops back up
to the top, now using the temporarily stored frame as the current frame.

Of course, instead of a loop, gcc's tail-call optimization feature could
be used, but this would be non-portable to other compilers.

An example of the patch in action:

# Note default arguments aren't supported in the current patch
def fact2(n,v):
if n:
return fact2(n-1,v*n)
else:
return v

def fact(n):
return fact2(n,1)

Without the patch:
fact(10000) RuntimeError: maximum recursion depth exceeded

With the patch: fact(10000)

<really really huge number>

Any feedback would be greatly appreciated!
Jul 18 '05 #10

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

Similar topics

4
2115
by: Crutcher | last post by:
This is fun :) {Note: I take no responsibilty for anyone who uses this in production code} #!/usr/bin/env python2.4 # This program shows off a python decorator # which implements tail call optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack.
0
1895
by: Ray Wesley Kinserlow Jr. | last post by:
We have been studying tail recursion in my computer class. The prof told us that some compilers will turn a tail recursion into an iteration thus allowing many, many function calls to the recursion. I have proved to myself that the two flavors of VC++ I have will do this but I cannot get the GNU G++ compiler to do so. It is run in unix on a sun sparc and is version 3.3.2. It's threading is POSIX. Is there a switch which will compile a...
19
2286
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
11
3927
by: Josiah Manson | last post by:
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am attempting to make a list of polynomial functions such that poly(3) = 1, poly(3) = 3, poly(3) = 9, etc. Could someone point me in the right direction? Thanks. def make_polys(n): """Make a list of polynomial functions up to order n. """
35
4741
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
3
2050
by: not_a_commie | last post by:
A tail call is a way of removing the current method from the stack before recursing. See http://blogs.msdn.com/shrib/archive/2005/01/25/360370.aspx .. I would love to see it in C# 4.0 and wouldn't mind if we needed a special keyword to make it happen.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.