1. clearing "return info" in a function

I hope someone understands this. I'm deep inside a function (normally returns a value), and the situation is I want to call another function JUST AS IF I MYSELF HAD NOT BEEN CALLED AS A FUNCTION, which normally returns a value (and has return info on the stack or somewhere), so this "pending return stuff" is cleared permanently and the program won't build to an eventual crash (stack overflow or something that would happen from the "return" never happening). I hesitate to ask because I can imagine everyone saying "OH for Pete's sake! Just do it some other way that doesn't have that problem!" Well, maybe I could, but a little thinking tells me that it would be VERY COMPLEX as far as i know, to do so. It would be SO EASY to do what I want (and useful, I think enough so it could be a feature, being inside a function and being able to cancel the "function action" of always returning a value to whatever called it). At least I figured it'd be worth posting a question about, as it might be easy to do something like this from what little I know.

new topic     » topic index » view message » categorize

2. Re: clearing "return info" in a function

What you want is called tail-call optimization or elimination, and is not supported in Euphoria.

Edit: see below.

new topic     » goto parent     » topic index » view message » categorize

3. Re: clearing "return info" in a function

PeteE said...

What you want is called tail-call optimization or elimination, and is not supported in Euphoria.

I wonder if it would work with the translator, assuming you returned integers and had a C compiler that supported it.

new topic     » goto parent     » topic index » view message » categorize

4. Re: clearing "return info" in a function

PeteE said...

What you want is called tail-call optimization or elimination, and is not supported in Euphoria.

I expected this type of answer. Thanx for responding. I feel Euphoria SHOULD support it because Euphoria is so useful otherwise.

new topic     » goto parent     » topic index » view message » categorize

5. Re: clearing "return info" in a function

I have to eat my words, since I didn't try this beforehand:

function foo(integer x) 
        if x = 0 then 
                return 0 
        end if 
        return foo(x-1)   
end function 
 
? foo(1000000000) 

You'd expect this to blow the stack, but it runs fine in the interpreter. And the translated code shows goto instead of recursive call! Seems Euphoria IS smart enough to detect and optimize tail calls - great job guys!

int _1foo(int _x_142) 
{ 
    int _9 = NOVALUE; 
    int _0, _1, _2; 
     
L1:  
    if (!IS_ATOM_INT(_x_142)) { 
        _1 = (long)(DBL_PTR(_x_142)->dbl); 
        DeRefDS(_x_142); 
        _x_142 = _1; 
    } 
 
    /**         if x = 0 then*/ 
    if (_x_142 != 0) 
    goto L2; // [7] 18 
 
    /**                 return 0*/ 
    return 0; 
L2:  
 
    /**         return foo(x-1)*/ 
    _9 = _x_142 - 1; 
    if ((long)((unsigned long)_9 +(unsigned long) HIGH_BITS) >= 0){ 
        _9 = NewDouble((double)_9); 
    } 
    _0 = _x_142; 
    _x_142 = _9; 
    DeRef(_0); 
    DeRef(_9); 
    _9 = NOVALUE; 
    goto L1; // [24] 1 
    ; 
} 
new topic     » goto parent     » topic index » view message » categorize

6. Re: clearing "return info" in a function

PeteE said...

You'd expect this to blow the stack, but it runs fine in the interpreter. And the translated code shows goto instead of recursive call! Seems Euphoria IS smart enough to detect and optimize tail calls - great job guys!

Yes, we have a special op code when we detect this, and act accordingly. It looks like that optimization never made it into the release notes, but this was included in 4.0.0.

Matt

new topic     » goto parent     » topic index » view message » categorize

7. Re: clearing "return info" in a function

mattlewis said...
PeteE said...

You'd expect this to blow the stack, but it runs fine in the interpreter. And the translated code shows goto instead of recursive call! Seems Euphoria IS smart enough to detect and optimize tail calls - great job guys!

Yes, we have a special op code when we detect this, and act accordingly. It looks like that optimization never made it into the release notes, but this was included in 4.0.0.

Matt

I do remember it was noted somewhere, since I tested this feature as soon I learned about it.
I just searched 'tail recursion' phrase and found it here http://openeuphoria.org/wiki/view/ChangesFor4_0b2.wc

new topic     » goto parent     » topic index » view message » categorize

8. Re: clearing "return info" in a function

Insolor said...
mattlewis said...
PeteE said...

You'd expect this to blow the stack, but it runs fine in the interpreter. And the translated code shows goto instead of recursive call! Seems Euphoria IS smart enough to detect and optimize tail calls - great job guys!

Yes, we have a special op code when we detect this, and act accordingly. It looks like that optimization never made it into the release notes, but this was included in 4.0.0.

Matt

I do remember it was noted somewhere, since I tested this feature as soon I learned about it.
I just searched 'tail recursion' phrase and found it here http://openeuphoria.org/wiki/view/ChangesFor4_0b2.wc

Aha. I was looking in the release notes. Thanks for the pointer.

Matt

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu