<div dir="ltr"><div><div>There is nothing about recursion in documentation.<br><br></div>Your module:<br>-module(tc).<br>-export([a/0]).<br>a() -> b().<br>b() -> c().<br>c() -> z().<br>z() -> self().<br><br>Compiles to (memory dump):<br>00007F07875A2DA8: i_func_info_IaaI 0 tc a 0.<br>00007F07875A2DD0: i_call_only_f tc:b/0.<br><br>00007F07875A2DE0: i_func_info_IaaI 0 tc b 0.<br>00007F07875A2E08: i_call_only_f tc:c/0.<br><br>00007F07875A2E18: i_func_info_IaaI 0 tc c 0.<br>00007F07875A2E40: i_call_only_f tc:z/0.<br><br>00007F07875A2E50: i_func_info_IaaI 0 tc z 0.<br>00007F07875A2E78: self_r x(0).<br>00007F07875A2E80: return.<br><br></div>Note the call_only functions. These are the tail calls.<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-10-17 14:28 GMT+02:00 Salikhov Dinislam <span dir="ltr"><<a href="mailto:Dinislam.Salikhov@kaspersky.com" target="_blank">Dinislam.Salikhov@kaspersky.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
The confusing thing is that the doc says about tail <b>recursive</b>
call.<br>
For example, if I have a call chain:<br>
a() -><br>
% some code<br>
b().<br>
b() -><br>
% some code<br>
c().<br>
% ...<br>
y() -><br>
%some code<br>
z().<br>
Recursion is <b>not</b> involved here. And I'd like to know if
erlang requires (and guarantees) that all tail callees in the chain
above use the stack of the caller.<br>
AFAIU, compiler is free to not apply the optimization if it is not
stated in the specification (and it is pure luck that the compiler
does).<span class="HOEnZb"><font color="#888888"><br>
<br>
Salikhov Dinislam</font></span><div><div class="h5"><br>
<br>
<div class="m_-5841299804546824751moz-cite-prefix">On 10/17/2016 03:00 PM, Dmytro
Lytovchenko wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div>In the doc page you linked:<br>
> If the last expression of a function body is a function
call, a <strong>tail recursive</strong> call is done.<br>
<br>
</div>
<div>Compiler will replace call opcode with a tail call
(call_last, call_ext_last, apply_last). You can check it with
"erl -S test.erl" to see assembly, and in erl console:
"l(modulename)." to load the module then
"erts_debug:df(modulename)." to create disassembly from BEAM
VM memory (it will be a bit different from the erl -S output).<br>
<br>
</div>
<div>See that your calls are replaced with one of: call_last,
call_ext_last, apply_last.</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2016-10-17 10:58 GMT+02:00 Salikhov
Dinislam <span dir="ltr"><<a href="mailto:Dinislam.Salikhov@kaspersky.com" target="_blank">Dinislam.Salikhov@kaspersky.<wbr>com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div class="m_-5841299804546824751m_-6524208612865981557moz-text-flowed" style="font-family:-moz-fixed;font-size:12px" lang="x-unicode">Hello. <br>
<br>
Erlang guarantees tail recursion optimization and states
it in the documentation: <br>
<a class="m_-5841299804546824751m_-6524208612865981557moz-txt-link-freetext" href="http://erlang.org/doc/reference_manual/functions.html#id78464" target="_blank">http://erlang.org/doc/referenc<wbr>e_manual/functions.html#<wbr>id78464</a>
<br>
<br>
Does erlang guarantee that tail call optimization is
done in a generic case, without recursion? <br>
Say, we have a function calling a function from another
module as its final statement: <br>
alpha() -> <br>
xxx:beta(). <br>
Is it guaranteed that xxx:beta() will use the stack of
alpha() regardless whether recursion is involved. <br>
I mean whether the language guarantees it rather than
virtual machine may provide such optimization. <br>
<br>
Thanks in advance, <br>
Salikhov Dinislam <br>
</div>
</div>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
</div></div></div>
</blockquote></div><br></div>