Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; '(even': 0.05; 'cpython': 0.05; 'alain': 0.09; 'explanation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:while': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'assume': 0.14; 'assumptions': 0.16; 'buggy': 0.16; 'compiler.': 0.16; 'iteration': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:recursion': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'developers': 0.25; 'compiled': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'that.': 0.31; '(usually': 0.31; 'context,': 0.31; 'relies': 0.31; 'writes:': 0.31; 'core': 0.34; 'something': 0.35; 'done': 0.36; 'useful': 0.36; 'possible': 0.36; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'analyze': 0.60; 'is.': 0.60; 'received:173': 0.61; 'here': 0.66; 'secret': 0.74; 'conservative': 0.84; 'infamous': 0.84; 'manual,': 0.84; 'received:fios.verizon.net': 0.84; 'procedural': 0.91; 'steps.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Tail recursion to while iteration in 2 easy steps Date: Wed, 02 Oct 2013 18:17:06 -0400 References: <87had0axxy.fsf@dpt-info.u-strasbg.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: <87had0axxy.fsf@dpt-info.u-strasbg.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380752253 news.xs4all.nl 15867 [2001:888:2000:d::a6]:46384 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55384 On 10/2/2013 4:17 AM, Alain Ketterlin wrote: > Terry Reedy writes: > >> Part of the reason that Python does not do tail call optimization is >> that turning tail recursion into while iteration is almost trivial, >> once you know the secret of the two easy steps. Here it is. >> >> Assume that you have already done the work of turning a body recursive >> ('not tail recursive') form like >> >> def fact(n): return 1 if n <= 1 else n * fact(n-1) As I said in response to randomxxx, even this 0th step (recursion to recursion transformation) requires assumptions or carefully reasoning about the properties of the operations. >> into a tail recursion like > [...] > > How do know that either "<=" or "*" didn't rebind the name "fact" to > something else? I think that's the main reason why python cannot apply > any procedural optimization (even things like inlining are impossible, > or possible only under very conservative assumption, that make it > worthless). > > -- Alain. > > P/S: don't take me wrong; your explanation is excellent (and very useful > to python programmers). What I say is that it relies on assumptions that > do not apply to python. Program transformations (usually intended to be optimizations), whether automatic or manual, are infamous for being buggy in not always being correct because of hidden assumptions that are not always true. CPython core developers have be very conservative about what tranformations they put into the compiler. (1,2,3) can always be compiled as a constant, and so it is. [1,2,3] might or might not be a constant, depending on the context, and no attempt is made to analyze that. -- Terry Jan Reedy