Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'debug': 0.04; 'calls.': 0.07; 'later)': 0.09; 'loop.': 0.09; 'preferable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'worse': 0.09; 'anyway': 0.11; 'jan': 0.11; 'def': 0.13; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'something.': 0.16; 'tracebacks': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'version.': 0.18; '>>>': 0.20; '2015': 0.20; 'fraction': 0.22; 'precise': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; '14,': 0.27; 'function': 0.28; 'fine': 0.28; 'record': 0.29; 'does,': 0.29; 'reduced': 0.29; 'tail': 0.29; 'program,': 0.29; 'subject:/': 0.30; 'call.': 0.30; 'compared': 0.30; 'probably': 0.31; 'doubt': 0.33; 'traceback': 0.33; 'tue,': 0.34; '???': 0.35; 'but': 0.36; 'lines': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'no,': 0.38; 'skip:z 10': 0.38; 'loss': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'behavior': 0.61; 'per': 0.62; 'lose': 0.63; 'records': 0.70; 'jul': 0.72; 'iterative': 0.84; 'loose': 0.84; 'pardon': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Date: Tue, 14 Jul 2015 20:41:06 -0400 References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> <87bnff1eks.fsf@jester.gateway.sonic.net> <87d1zunctp.fsf@elektro.pacujo.net> <87k2u2eu67.fsf@elektro.pacujo.net> <55A51662.4090007@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <55A51662.4090007@rece.vub.ac.be> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436920888 news.xs4all.nl 2821 [2001:888:2000:d::a6]:44092 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93840 On 7/14/2015 10:02 AM, Antoon Pardon wrote: > On 07/14/2015 03:43 PM, Chris Angelico wrote: >> On Tue, Jul 14, 2015 at 11:38 PM, Marko Rauhamaa wrote: >>> No, tail call optimization doesn't change the behavior of the program, >>> for the worse anyway. >> It does, because you lose traceback records. That's pretty significant >> when you come to try to debug something. > > I doubt it would be really significant. Not compared to writing it iteratively. > When you write it iteratively, you don't get to keep a traceback record per time > you go throught the loop. So the traceback records you loose in tale call elimination > would be the traceback records you wouldn't have anyway in the iterative version. To repeat: loosing tracebacks is probably fine when the function has a single *recursive* tail call. This are precise the cases when it is simple and perhaps preferable to use a loop. But *recursive* tail calls are only a small fraction of all tail calls. So most of the time, the loss *would* be significant. Consider In moda: def f(a): return modb.g(a-3) In modb: def g(b): return modc.h(b*(b-1)) In modc: def h(c): return 1.0/c from moda import f ... (500 lines later) f(3) and your traceback has been reduced to In modc: line 1 return 1.0/c ZeroDivisionError: ... ??? -- Terry Jan Reedy