OSDev.orghttps://forum.osdev.org/ An incorrect information in the Wikihttps://forum.osdev.org/viewtopic.php?f=8&t=33791 Page 1 of 1

 Author: FlandreScarlet [ Sat Jul 27, 2019 10:29 am ] Post subject: An incorrect information in the Wiki Well, I'm not sure if this is the right place for this, but basically there is an incorrect information in the wiki. It's in the page "Tail Recursion and Tail Call Optimization" (https://wiki.osdev.org/Tail_Recursion_and_Tail_Call_Optimization)In this page, it's written that a tail-call is when a call occurs in the end of a function. This is incorrect, a tail-call is when, in assembly, a CALL occurs before RET.Code: unsigned long long Factorial(unsigned start) {    if (start > 1) {       return Factorial(start - 1) * start;    } // if    return 1; } // Factorial(start)Now, according to the page, there is no tail-call here (which is correct, but for a very different reason. I'll get back to this)Now the page also writes:Code:unsigned long long Factorial(unsigned start) {    if (start <= 1) {       return 1;    } // if    return start * Factorial(start - 1); } // Factorial(start)The page claims there is a tail-call here, which is incorrect, there is none. As I said a tail-call is when a CALL occurs before RET. I'll just use pseudo-assembly for simplicity (since CALL, RET and JMP are more or less the same in most architectures it doesn't matter much):Code:CMP start,1JMP.HI _endofblockMOV return_value, 1RET_endofblock:SUB tmp, start, 1PUSH tmpCALL FactorialMUL return_value, return_value, startRETAs you can see there is a MUL between CALL and RET. As a result, there is no tail-call. The same logic also applies to the above code, as that code will also result in a MUL between CALL and RET.Now an example of a tail-call would include:Code:function foo(data) {    a(data);    return b(data);}In assembly, this is:Code:PUSH dataCALL aPUSH data #assuming this is neededCALL bRETNow this can be tail-call optimized, by combining the CALL and RET to simply JMP.Code:PUSH dataCALL aPUSH data #assuming this is neededJMP bThe new code will behave in the exact same way, with a minor and desirable difference, it won't push IP/PC to the stack, which is a great thing if the said tail-call is also a recursive call (though it isn't in this case)Contrary to what the said page claims, a tail call doesn't need to be at the tail of the code:Code:function bar(data) {    if ( a(data) ) {        return b(data);    }    return c(data);}In assembly:Code:PUSH dataCALL aTEST return_valueJMP.FALSE _endofblockPUSH dataCALL bRET_endofblock:PUSH dataCALL cRETHere, both b and c are tail-calls even though only c is at the tail, because both result in a case where CALL and RET follow each other, and can be optimized to:Code:PUSH dataCALL aTEST return_valueJMP.FALSE _endofblockPUSH dataJMP b_endofblock:PUSH dataJMP cAnother example, where the call isn't at the tail, but nonetheless is a tail-call:Code:function foo(){       int myInteger = bar();       return myInteger;}In assembly:Code:CALL barMOV myInteger, return_valueMOV return_value, myIntegerRETWe can first optimize the unnecessary MOVs, getting rid of myInteger:Code:CALL barRETAnd then simply apply tail-call optimization:Code:JMP barOn the contrary, even if the statement is at the tail, it may not be a tail-call, and the code in the said page is a great example for that:Code:unsigned long long Factorial(unsigned start) {    if (start <= 1) {       return 1;    } // if    return start * Factorial(start - 1); } // Factorial(start)Code:CMP start,1JMP.HI _endofblockMOV return_value, 1RET_endofblock:SUB tmp, start, 1PUSH tmpCALL FactorialMUL return_value, return_value, startRETAs you can see, this code results in a MUL between CALL and RET, therefore there is no tail-call, even though the call is actually on the tail of the function.Now of course, this also depends on the exact architecture, but as I said, the way the instructions CALL, RET and JMP work are usually the same, so most of the above examples with pseudo-assembly would still work on most real architectures.If you, the person reading this, are a moderator or anyone else with the privilege to modify the wiki, I ask of you to correct this page, please. Thanks in advance.Source: https://en.wikipedia.org/wiki/Tail_call

 Author: XenOS [ Sat Jul 27, 2019 11:12 am ] Post subject: Re: An incorrect information in the Wiki Actually everybody can edit the wiki, so also you could do it if you want to. To get edit rights, you just need to go to the User Control Panel here in the forum, then to the tab User Groups, and join the wiki group.Of course, you're right - nicely spotted!

 Author: FlandreScarlet [ Sat Jul 27, 2019 11:18 am ] Post subject: Re: An incorrect information in the Wiki Oh, didn't know everyone could do it. Thanks.

 Author: Schol-R-LEA [ Sun Jul 28, 2019 2:44 pm ] Post subject: Re: An incorrect information in the Wiki I wasn't even aware that there was a page on this here. According to the history, it was posted in January of this year, and I am guessing no one except the OP (Johnburger) had noticed it until now.FlandreScarlet: You're right, the example as given is not a tail call at all. The OP seems to think that the position of the call on the line of source code is what is relevant, which is not at all the case - it has to do with it being the last action in the generated code before the function exits and returns. The real relevant factor is whether the activation record (or stack frame, or local environment - take your pick, those terms all amount to almost the same thing) on the call stack can be reused without losing any necessary information, and in this case, the answer is no.There is a related optimization which is referred to as a 'tail recursion modulo cons' which could be applied here, but it is significantly more complex to for the compiler writers to implement. While a discussion of it might be appropriate, as it is the example is entirely incorrect.The funny thing is, in Scheme textbooks (which I am guessing where Johnburger got this, as it seems like a garbled version of a common example), the reverse is usually given, replacing this linear recursion:Code:; yes, I am aware that factorial is only defined for positive integers, ; but I wanted to keep it simple(define (factorial-1 n)  (if (<= n 1)    1     (* n (factorial-1 (- n 1)))))with this 'linear iteration' (i.e., a recursion suitable for TCO, which in Scheme terms is considered iteration because it gets optimized into one):Code:; note that the 'named let' basically creates a special-purpose internal function;; it's that function 'fact' which is recursing in this case.(define (factorial-2 n)  (let fact ((product 1)               (counter 1))    (if (> counter n)        product        (fact (* product counter) (+ 1 counter))))) However, this approach to iteration isn't necessary, or even particularly applicable, in languages which have built-in iterative operators such as while() and for() (or even a standard iteration macro, such as Common Lisp's (loop)). In fact, even Scheme has one, (do), though it's rather odd:Code:(define (factorial-3 n)  (do ((counter 1 (+ 1 counter))       (product 1 (* product counter)))      ((> counter n) product)  ;; note that there is no loop body in this case,  ;; as the iteration clauses do all the heavy lifting  )) In any case, actual idiomatic Scheme would really be do this (she said, tongue firmly in cheek) with an accumulator applied to a stream (a lazy list). Assuming one got that far in learning the language, that is. Note that standardization of SRFI library names between implementations is... problematic.Code:#!r6rs; at this point I have probably lost everyone anyway, so a detailed explanation is probably fruitless.; I will mention that I've tested this in Guile, so at least one implementation can use it...(import (srfi :41))(define (factorial-4 n)  (if (<= n 0)      1      (let ((range (stream-range 1 (+ 1 n))))        (stream-fold         (lambda (x y)           (* x y))         (stream-car range) (stream-cdr range)))))Enough of that, I think.

 Author: FlandreScarlet [ Sun Jul 28, 2019 8:05 pm ] Post subject: Re: An incorrect information in the Wiki Interesting. Thank you for putting this information here. Although factorial isn't strictly defined only for positive integers. Usually, it is also defined for zero where:0! = 1There is also more generalized definition with the gamma function, which can also be applied to non-integers.So it depends on the exact definition for factorial you are using.Not really an important matter, but nerd instincts kicked in.

 Author: linguofreak [ Wed Aug 28, 2019 10:37 pm ] Post subject: Re: An incorrect information in the Wiki FlandreScarlet wrote:Interesting. Thank you for putting this information here. Although factorial isn't strictly defined only for positive integers. Usually, it is also defined for zero where:0! = 1There is also more generalized definition with the gamma function, which can also be applied to non-integers.So it depends on the exact definition for factorial you are using.Not really an important matter, but nerd instincts kicked in. The really interesting thing is that if you use the definition factorial=gamma(x+1), the only complex numbers for which factorial is not defined are real integers (specifically, the negative integers).So strictly speaking, factorial is defined for other numbers than the natural numbers over any domain that is a superset of the real integers, but over the real integers is defined only for the naturals.

 Page 1 of 1 All times are UTC - 6 hours Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/