\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#266: Numbers are Commands}
\date{April 11, 2006}
\author{Kristoffer Lawson, Michal Malecki, Wolf-Dieter Busch, Paul Nash}
\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 \#266}&\textbf{Numbers are Commands}\\\hline
Author&

Kristoffer Lawson \tipmail{setok}{fishpool.com}
\par
Michal Malecki \tipmail{ethouris}{gmail.com}
\par
Wolf-Dieter Busch \tipmail{wolf-dieter.busch}{gmx.de}
\par
Paul Nash \tipmail{webscool}{ihug.co.nz}
\\
Created&Tuesday, \(\text{11}^{\text{th}}\) April 2006\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.8 $\\
Keywords&Tcl, unknown, expression\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP describes a change to Tcl's command dispatch which would allow every number to act as a command.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Maths in Tcl are continuously a stumbling block. The \textbf{expr} command is ugly and cumbersome. It adds a new C-like syntax within Tcl and easily makes lines less readable, especially with the most common use of simple arithmetic. In addition it is confusing to the programmer to have to remember there is a completely separate set of commands, or functions, which are only available to the \textbf{expr} command.

This proposal offers a clean solution which could eventually totally replace \textbf{expr}, yet offers few or no backward compatibility problems and fits in with the Tcl philosophy of ``every piece of executable code is a command''.

This addition is easy to build as a separate extension using the mechanisms provided by Tcl. However, this will not be widely used unless it is stamped as part of the language and part of its unique philosophy. In addition, implementations in the core can gain from many forms of optimisation that would not be possible with a normal extension.

There is an existing proposal for making operands into commands. That proposal is still a valid one, but the description in this TIP leads to syntax which will be more familiar to most programmers. It does not have operator precedence, which is a difference from normal mathematics, but one that should be easier to grasp than the reversed notation that operator commands offer. In fact, there are many claims which state operator precedence is a mistake anyway.

\section{Specification}

Every number that can be handled by Tcl is a command.

The format for number commands is:

\begin{quote}
\textit{number} \textit{operation} ?\textit{arg1 arg2 ...}?
\end{quote}

For example:
\begingroup\small\begin{verbatim}
 8 * 5 + 2
\end{verbatim}
\endgroup

The first argument must be an operator, and can be one of any specified for the \textbf{expr} command (+, -, *, /, ...)

Subsequent arguments can be either numbers or operators.

The command evaluates its arguments from left-to-right (no precedence) with the result of the left used as the operand for the next operation. \textit{e.g.} to get 242 a simple addition can be made:
\begingroup\small\begin{verbatim}
 200 + 42
\end{verbatim}
\endgroup

If multiple numbers are given without operators between them, the last operator is used. \textit{e.g.} for the result ``42'' the following can be used:
\begingroup\small\begin{verbatim}
 20 + 10 10 2
\end{verbatim}
\endgroup

To get ``23'', the following can be used:
\begingroup\small\begin{verbatim}
 20 + 10 10 2 / 2 + 2
\end{verbatim}
\endgroup

Normal Tcl brackets can be used for grouping purposes. \textit{e.g.} the result ``31337'' can be reached with the following:
\begingroup\small\begin{verbatim}
 62674 / [1 + 1]
\end{verbatim}
\endgroup

Number commands act just like any other commands in Tcl. They can be nested with brackets, aliased, replaced and even removed. The only difference is that they do not appear in the list returned by \textbf{info commands}. However, any replacements made on them do.

\subsection{Compatibility}

Because of the ability to replace number-commands, there are no real backwards compatibility issues. There are very few, if any, extensions that make numbers into commands and any that do will still work with the only exception being software that would mix extensions that would depend on the functionality in this TIP and extensions that create their own commands which are only numbers. In any case, the author is not aware of any extensions that create commands in that manner.

The only other area where incompatibilities would arise would be if someone is using an \textbf{unknown} mechanism which creates similar functionality to the one presented here. These extensions do exist, but are mostly presented as brain-twisters instead of for serious use.

Additionally, Tcl does not and cannot guarantee that new commands will not appear in the global namespace. Indeed, every new release does this.

For these reasons, this proposal can be implemented with a minor version bump.

\section{Implementation Discussion}

The first natural step is to add a detection phase before the normal unknown mechanism. In fact, a prototype implementation can easily and trivially be built with Tcl's \textbf{unknown} command. The detection should work by checking if the unknown command is a number and if so, it will run the number command functionality. Otherwise, the normal Tcl chain of events is followed.

If the command is replaced, this number mechanism is never reached, as a normal command exists to handle the situation.

If the command is removed, a special flag should be set to specify that the number in question cannot be used as a command.

At a later date, further integration with the bytecode engine can be made. The details of such optimizations are outside the scope of this TIP.

Some may argue that it is not good to disable defining commands with numbers by users. For example, someone would like to ``overload'' 0 and 1 to use [!file exists \$f] \&\& \{ puts ``File not found''; exit \}. This also does not look like a good solution for 'expr' command; this will be still not the same as expressions in other languages (required spaces between operators etc.)

\section{Alternative Approaches}

Instead of allowing multiple number arguments to use the same operator, a requirement could be made that every other argument must be an operator and every other one an operand. This might make the specification somewhat clearer, but would mean a mathematical operation on a list would not be as easy.

\section{Comments}

This is a change to the ``eleven rules'', so please do not do it. It takes the possibility that I can do it myself by changing the procedure unknown.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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