<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu May 17 04:18:17 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='327'>
<header><title>Proper Tailcalls</title><author address="mailto:msofer@users.sf.net">Miguel Sofer</author><author address="mailto:escargo@skypoint.com">David S. Cargo</author><status type='project' state='final' tclversion="8.6" vote='after'>$Revision: 1.8 $</status><history></history><created day='20' month='sep' year='2008' /><keyword>tailcall NRE</keyword></header>
<abstract>This TIP recommends adding proper tailcalls to Tcl.</abstract>
<body><section title="Proposal">
<para>We propose to add a new command:</para>
<quote><emph style="bold">tailcall</emph> <emph style="italic">cmd</emph> ?<emph style="italic">arg ...</emph>?</quote>
<para>The command can only be invoked in a procedure or lambda body.</para>
<para>The effect of this command is very similar to:</para>
<quote><emph style="bold">return [uplevel 1 [list [namespace which</emph> <emph style="italic">cmd</emph> <emph style="bold">]</emph> ?<emph style="italic">arg ...</emph>?<emph style="bold">]]</emph></quote>
<para>with the sole exception that the invocation of <emph style="italic">cmd</emph> happens <emph style="italic">after</emph> the currently executing body returns and is not visible in Tcl&apos;s call stack.</para>
</section>
<section title="Rationale">
<para>The new Non-Recursive Engine (NRE) implemented in Tcl 8.6 allows support for a number of interesting features that have previously been difficult or impossible to implement efficiently in Tcl. One such feature is support for <emph style="italic">proper tailcalls</emph>, an important feature for functional-style programming. The new command allows unbounded recursion and enables programming in <emph style="italic">continuation passing style</emph>.</para>
</section>
<section title="Effect on Tcl&apos;s Call Stack">
<para><emph style="bold">tailcall</emph> is implemented as a new command, as opposed to an optimization that would be done automatically by the bytecode compiler, due to the effect it has on Tcl&apos;s call stack.</para>
<para>Consider the following example:</para>
<verbatim><vline encoding='base64'>IHByb2Mgc2hvd1N0YWNrIHt9IHs=</vline><vline encoding='base64'>ICAgICBzZXQgZGVwdGggW2luZm8gZnJhbWVd</vline><vline encoding='base64'>ICAgICBzZXQgcmVzIHt9</vline><vline encoding='base64'>ICAgICBmb3Ige3NldCBpIDF9IHskaSA8PSAkZGVwdGh9IHtpbmNyIGl9IHs=</vline><vline encoding='base64'>IAlsYXBwZW5kIHJlcyBbaW5mbyBmcmFtZSAkaV0=</vline><vline encoding='base64'>ICAgICB9</vline><vline encoding='base64'>ICAgICByZXR1cm4gJHJlcw==</vline><vline encoding='base64'>IH0=</vline><vline encoding='base64'>IA==</vline><vline encoding='base64'>IHByb2Mgb25lIGNtZCB7am9pbiBbJGNtZF0gXG59</vline><vline encoding='base64'>IHByb2MgdHdvIHt9IHt1cGxldmVsIDEgc2hvd1N0YWNrfQ==</vline><vline encoding='base64'>IHByb2MgdGhyZWUge30ge3RhaWxjYWxsIHNob3dTdGFja30=</vline></verbatim>
<para>When run at the interactive prompt, we obtain</para>
<verbatim><vline encoding='base64'>ICUgb25lIHR3bw==</vline><vline encoding='base64'>IHR5cGUgZXZhbCBsaW5lIDEgY21kIHtvbmUgdHdvfSBsZXZlbCAy</vline><vline encoding='base64'>IHR5cGUgcHJvYyBsaW5lIC0xIGNtZCB7JGNtZH0gcHJvYyA6Om9uZSBsZXZlbCAx</vline><vline encoding='base64'>IHR5cGUgcHJvYyBsaW5lIDEgY21kIHt1cGxldmVsIDEgc2hvd1N0YWNrfSBwcm9jIDo6dHdv</vline><vline encoding='base64'>IHR5cGUgZXZhbCBsaW5lIDEgY21kIHNob3dTdGFjayBwcm9jIDo6dHdv</vline><vline encoding='base64'>IHR5cGUgcHJvYyBsaW5lIDUgY21kIHtpbmZvIGZyYW1lICRpfSBwcm9jIDo6c2hvd1N0YWNrIGxldmVsIDA=</vline><vline encoding='base64'>ICUgb25lIHRocmVl</vline><vline encoding='base64'>IHR5cGUgZXZhbCBsaW5lIDEgY21kIHtvbmUgdGhyZWV9IGxldmVsIDI=</vline><vline encoding='base64'>IHR5cGUgcHJvYyBsaW5lIC0xIGNtZCB7JGNtZH0gcHJvYyA6Om9uZSBsZXZlbCAx</vline><vline encoding='base64'>IHR5cGUgcHJvYyBsaW5lIDUgY21kIHtpbmZvIGZyYW1lICRpfSBwcm9jIDo6c2hvd1N0YWNrIGxldmVsIDA=</vline><vline encoding='base64'>ICUg</vline></verbatim>
<para>Remark how <emph style="bold">tailcall</emph> completely removed the proc <emph style="italic">three</emph> from Tcl&apos;s call stack. This effect is also apparent on error traces.</para>
</section>
<section title="Implementation">
<para>An experimental implementation of tailcalls is available in Tcl 8.6a2 in CVS on sourceforge, in the ::tcl::unsupported namespace.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

