<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body 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).<br>
<br>
Salikhov Dinislam<br>
<br>
<div class="moz-cite-prefix">On 10/17/2016 03:00 PM, Dmytro
Lytovchenko wrote:<br>
</div>
<blockquote
cite="mid:CAOUb9CGq9R1gGZX=Uas-UuEkcWz6bP7_4k16+YWRSSaRyqw5Dg@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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 moz-do-not-send="true"
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">
<div class="m_-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 moz-do-not-send="true"
class="m_-6524208612865981557moz-txt-link-freetext"
href="http://erlang.org/doc/reference_manual/functions.html#id78464"
target="_blank">http://erlang.org/doc/<wbr>reference_manual/functions.<wbr>html#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 moz-do-not-send="true"
href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a moz-do-not-send="true"
href="http://erlang.org/mailman/listinfo/erlang-questions"
rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
</body>
</html>