\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#87: Allow Tcl Access to the Recursion Limit}
\date{February 19, 2002}
\author{Stephen Trier, Richard Suchenwirth}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#87}&\textbf{Allow Tcl Access to the Recursion Limit}\\\hline
Author&

Stephen Trier \tipmail{sct}{po.cwru.edu}
\par
Richard Suchenwirth \tipmail{richard.suchenwirth-bauersachs}{siemens.com}
\\
Created&Tuesday, \(\text{19}^{\text{th}}\) February 2002\\
Type&Project\\
Tcl Version&\(8.4\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.11 $\\
Keywords&Tcl\_SetRecusionLimit, recursion limit\\
Post History&\\
Discussions To&news:comp.lang.tcl\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

An extension to the [interp] command, [interp recursionlimit], will permit Tcl scripts to control their own recursion limits. Until now, this limit has been changeable from a C API, but not from within Tcl.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

As of Tcl 8.4a3, Tcl scripts must live with the default recursion depth of 1000 nested calls to the \textit{Tcl\_Eval} family of functions or resort to C code to change the limit. Nevertheless, Tcl programmers may find it useful to reduce the limit when debugging or to increase it for scripts that include deeply recursive functions. The changes proposed in this TIP will make this possible in pure Tcl code.

\section{Specification}
\begin{description}
\item[generic/tclInterp.c]
Add subcommands to [interp] and to the slave interpreter object command with the following syntax:

interp recursionlimit \textit{path} \textit{?newlimit?}

\textit{slave} recursionlimit \textit{?newlimit?}

The parameter \textit{newlimit} must be a positive integer. When it is present, the limit is changed to \textit{newlimit} and the command returns the new recursion limit. If the \textit{newlimit} parameter is absent, the command returns the current recursion limit.

No maximum value is enforced. It is the programmer's responsibility to ensure the recursion limit will not overflow the process stack.

A safe interpreter is not allowed to change the recursion limit for itself nor for any other interpreter. Attempting to do so will generate an error. Safe interpreters are allowed to query recursion limits.

If an interpreter changes its own recursion limit to a value lower than the current Tcl\_Eval nesting level, the limit will be changed, then an error message appropriate to this particular situation will be issued by the recursionlimit command. (Error text: ``falling back due to new recursion limit'')

If an interpreter changes a sub-interpreter's recursion limit to less than the sub-interpreter's current Tcl\_Eval nesting level, no immediate error is issued. The sub-interpreter will throw a ``too many nested calls to Tcl\_Eval (infinite loop?)'' error if its nesting is still deeper than its recursion limit when next a command is executed in its context.
\item[generic/tclTest.c]
Remove the now-unnecessary testsetrecursionlimit command.
\item[doc/interp.n]
Add documentation for the new subcommands, including a warning about stack overflow, much like the warning in the documentation for \textit{Tcl\_SetRecursionLimit()}.
\item[test/interp.test]
Add tests for the new subcommands.
\end{description}

\section{Comments Received}

Discussion of this TIP took place in the following threads:

\url{http://groups.google.com/groups?hl=en&threadm=3C6D0A88.5DC9D8B4\%40utdt.edu}

\url{http://groups.google.com/groups?hl=en&threadm=3C73E98A.8ED9DDE6\%40cisco.com}

\url{http://www.geocrawler.com/mail/thread.php3?subject=\%5BTCLCORE\%5D+TIP+\%2387\%3A+Allow+Tcl+Access+to+the+Recursion+Limit&list=7375}

Using a command or variable \textit{::tcl::recursionLimit} to manipulate the limit was initially considered, but Miguel Sofer suggested making the function a subcommand of [interp] because the recursion limit is logically an attribute of each interpreter. Miguel also pointed out that implementing \textit{TclpCheckStackSpace()} for Unix would mitigate the dangers of setting the recursion limit too high.

comp.lang.tcl saw some discussion of whether it would be appropriate to have a way to completely remove the recursion limit. The consensus was to not add such a feature.

The initial version of this TIP did not provide for a diagnostic error message for the case where the nesting is already deeper than the new recursion level. Ken Fitch, Don Porter, Miguel Sofer, and Donal Fellows discussed whether this was important. This version of the TIP uses Donal Fellows's suggestion of changing the recursion limit as requested, but providing a meaningful error message if the nesting is too deep for the new limit.

Donal Fellows suggested that slave interpreters should inherit their recursion limit from their parent. As it turns out, this behavior was already present but was not documented. The reference implementation documents it.

\section{Reference Implementation}

An implementation of this TIP, with tests and documentation, is patch number 522849 on SourceForge.

\section{Copyright}

This document is in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
