An incorrect information in the Wiki
- Clover5411
- Member
 Member
- Posts: 25
- Joined: Sat Jul 27, 2019 9:41 am
An incorrect information in the Wiki
Post by Clover5411 »
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: Select all
 unsigned long long Factorial(unsigned start) {
 if (start > 1) {
 return Factorial(start - 1) * start;
 } // if
 return 1;
 } // Factorial(start)Now the page also writes:
Code: Select all
unsigned long long Factorial(unsigned start) {
 if (start <= 1) {
 return 1;
 } // if
 return start * Factorial(start - 1);
 } // Factorial(start)Code: Select all
CMP start,1
JMP.HI _endofblock
MOV return_value, 1
RET
_endofblock:
SUB tmp, start, 1
PUSH tmp
CALL Factorial
MUL return_value, return_value, start
RETNow an example of a tail-call would include:
Code: Select all
function foo(data) {
 a(data);
 return b(data);
}Code: Select all
PUSH data
CALL a
PUSH data #assuming this is needed
CALL b
RETCode: Select all
PUSH data
CALL a
PUSH data #assuming this is needed
JMP bContrary to what the said page claims, a tail call doesn't need to be at the tail of the code:
Code: Select all
function bar(data) {
 if ( a(data) ) {
 return b(data);
 }
 return c(data);
}Code: Select all
PUSH data
CALL a
TEST return_value
JMP.FALSE _endofblock
PUSH data
CALL b
RET
_endofblock:
PUSH data
CALL c
RETCode: Select all
PUSH data
CALL a
TEST return_value
JMP.FALSE _endofblock
PUSH data
JMP b
_endofblock:
PUSH data
JMP cCode: Select all
function foo()
{
 int myInteger = bar();
 return myInteger;
}Code: Select all
CALL bar
MOV myInteger, return_value
MOV return_value, myInteger
RETCode: Select all
CALL bar
RETCode: Select all
JMP barCode: Select all
unsigned long long Factorial(unsigned start) {
 if (start <= 1) {
 return 1;
 } // if
 return start * Factorial(start - 1);
 } // Factorial(start)Code: Select all
CMP start,1
JMP.HI _endofblock
MOV return_value, 1
RET
_endofblock:
SUB tmp, start, 1
PUSH tmp
CALL Factorial
MUL return_value, return_value, start
RETNow 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
- xenos
- Member
 Member
- Posts: 1130
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: An incorrect information in the Wiki
Of course, you're right - nicely spotted!
- Clover5411
- Member
 Member
- Posts: 25
- Joined: Sat Jul 27, 2019 9:41 am
Re: An incorrect information in the Wiki
Post by Clover5411 »
- Schol-R-LEA
- Member
 Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: An incorrect information in the Wiki
Post by Schol-R-LEA »
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: Select all
; 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)))))Code: Select all
; 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))))) 
Code: Select all
(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
 )) Code: Select all
#!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)))))Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
- Clover5411
- Member
 Member
- Posts: 25
- Joined: Sat Jul 27, 2019 9:41 am
Re: An incorrect information in the Wiki
Post by Clover5411 »
Although factorial isn't strictly defined only for positive integers.
Usually, it is also defined for zero where:
0! = 1
There 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. :mrgreen:
- linguofreak
- Member
 Member
- Posts: 510
- Joined: Wed Mar 09, 2011 3:55 am
Re: An incorrect information in the Wiki
Post by linguofreak »
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).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! = 1
There 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. :mrgreen:
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.
- Clover5411
- Member
 Member
- Posts: 25
- Joined: Sat Jul 27, 2019 9:41 am
Re: An incorrect information in the Wiki
Post by Clover5411 »
I suppose I should also ask, can I even delete stuff from the main page? I don't know how to do that, so I took what felt like the most reasonable action at the time; but in retrospect, it was a bit childish. (EDIT: Nevermind, it was surprisingly simple... Truth be told, temptation to delete that page and this topic is strong...)
- SeaLiteral
- Posts: 19
- Joined: Wed Sep 27, 2017 1:44 pm
Re: An incorrect information in the Wiki
Post by SeaLiteral »
I haven't had the confidence to change much on the wiki myself either. There's even this "if someone writes something and I make an edit to it, how will the person that initially wrote the text feel?" feeling that I probably shouldn't have that much of but I do. I feel like I know of several mistakes in the interrupts tutorial, but even though I see them as mistakes (or in one case, just doing a thing without properly explaining why you're doing it that way) I don't really feel like I have the confidence to change what someone else wrote.KineticManiac wrote:Okay, so this took me way longer than it should have. The reason being a mixture of laziness and fearing my own ineptitude.
Re: An incorrect information in the Wiki
Over the years, many people who have written a lot in the Wiki drop out of the hobby, partially or completely. If you hold whatever they wrote sacred, sooner or later the Wiki will wither and die.
Those who wrote into the Wiki before you might have been before you, but that doesn't make them Elders. A Wiki is a cooperation thing.
In the beginning, it was one static HTML page with a couple of good hints. It grew to what it is today only because we got people to contribute. Please, continue in that vein.
By posting to the Wiki, every author has agreed to have his / her work modified later on. The Wiki holds a history, and if your edit is considered faulty, there are discussion pages as well as the ability to revert changes, wholly or partially. You cannot "destroy" information in the Wiki.
So... edit. Please.
Re: An incorrect information in the Wiki
This is also a very good reason to use citations profusely, so that information can be cross-referenced and validated, and if any source is incorrect, the corrections can be escalated upstream in order to reduce misinformation.Solar wrote: By posting to the Wiki, every author has agreed to have his / her work modified later on. The Wiki holds a history, and if your edit is considered faulty, there are discussion pages as well as the ability to revert changes, wholly or partially. You cannot "destroy" information in the Wiki.
But yes, wikis should be ever evolving with new information, new knowledge, corrections, adaptations to more contemporary situations and so forth.