\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#196: Tcl Commands as Values}
\date{May 11, 2004}
\author{Robert Suetterlin}
\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 \#196}&\textbf{Tcl Commands as Values}\\\hline
Author&

Robert Suetterlin \tipmail{robert}{mpe.mpg.de}
\\
Created&Tuesday, \(\text{11}^{\text{th}}\) May 2004\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Withdrawn\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes making command procedures first-class values in Tcl. It also changes the command binding scheme to recognize these values before performing old-style name lookup indirection.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

A Tcl script is a string containing one or more commands separated by semicolons or newlines. The evaluation of the script breaks these commands into words. Currently the string value of the first such word is used to lookup a command procedure (effectively a function pointer) and some clientData (per-command instance state). These will then be used by the virtual machine.

For example, in the case of pure Tcl procedures created with \textbf{proc} command, this is \textit{TclProcInterpProc} with an associated \textit{procPtr} that basically references the procedure body (as a string) and its formal argument list.

Because of the name lookup indirection, Tcl does not support anonymous commands. (There is quite some interest in using such with Tcl, please compare \cite{tip187} and \cite{tip194}. Several other languages (even C) support anonymous commands. They seem to be quite useful, just compare their use in Tcl's C code.)

This can be changed by allowing for a new kind of word (i.e. a new \textbf{Tcl\_Obj} \textit{commandObject}) that can be immediately interpreted as a command. (\cite{tip187} proposes the use of special Tcl lists, but this is too limiting for the scope of this document.) The evaluation checks if the first word is a \textit{commandObject}, if not it does the old style name lookup (with \textbf{unknown} fallback).

A \textit{commandObject} should store information equivalent to what is provided with the \textbf{Tcl\_CreateCommand()} API and stored in the global name lookup table. Thus it can reference any command procedure and any clientData, allowing the creation of arbitrary anonymous commands in C extensions for example.

Having such \textit{commandObject}s available will allow for \textbf{proc} to return these instead of an empty string and to skip registration of a command when its name is provided as the empty string.

\section{Examples}

This will allow to reproduce all of the features of \cite{tip187}. Compare this example with the one in \cite{tip187}:
\begingroup\small\begin{verbatim}
proc filter {list proc} {
    set res {}
    foreach e $list {
        if {![$proc $e]} {
            lappend res $e
        }
    }
}

set a [list 1 10 100 4 5]
set b [filter $a [proc x {expr $x<10}]]
\end{verbatim}
\endgroup

This sets the variable b to the list \{\textit{10 100}\}.

In addition this TIP still allows:
\begingroup\small\begin{verbatim}
proc {list lambda x {body}} {var} {puts $var} 
\end{verbatim}
\endgroup

if we really want to.

And we can use \textit{commandObject}s that have not been created by \textbf{proc} in the first place --- think OOP for example.

These \textit{commandObject}s could also be used as data structure for the command namespace, which would then become a dictionary, if I recall correctly. Allowing for registering an anonymous command with a name (via \textbf{rename}) by putting the name and the commandObject into the dictionary.

\section{Specification}

This document proposes at least the following changes to the Tcl core:
\begin{enumerate}
\item{}
Include a new \textit{Tcl\_Obj} \textbf{commandObj} that contains information equivalent to the arguments \textit{proc}, \textit{clientData} and \textit{deleteProc} to \textbf{Tcl\_CreateObjCommand()}. (Maybe interp is necessary, too. But I don't understand the byte compiler and execute stuff good enough to judge.)
\setcounter{enumi}{1}
\item{}
Change \textbf{Tcl\_GetCommandFromObj()} to check if \textit{objPtr} references a \textit{commandObj} first, otherwise check if it finds a named command.
\setcounter{enumi}{2}
\item{}
Have the \textbf{proc} command return a \textit{commandObj} that is equivalent to the \textbf{Tcl\_Create(Obj)Command()} calls in \textbf{Tcl\_ProcObjCmd()}. i.e. the \textit{proc} is equal to \textbf{Tcl(Obj)InterpProc()} and the \textit{clientData} is the \textit{procPtr}.
\end{enumerate}

\section{Copyright}

This document is in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#2}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#187]{tip187}
Salvatore Sanfilippo,
Miguel Sofer,
Paul Nash,
\emph{Procedures as Values},
on-line at \url{http://purl.org/tcl/tip/187.html}
\bibitem[TIP \#194]{tip194}
Miguel Sofer,
Joe Mistachkin,
\emph{Procedures as Values via '''apply'''},
on-line at \url{http://purl.org/tcl/tip/194.html}
\end{thebibliography}
\end{document}
