\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#408: Allow Any Command for expr Functions}
\date{August 17, 2012}
\author{Brian Griffin}
\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 \#408}&\textbf{Allow Any Command for expr Functions}\\\hline
Author&

Brian Griffin \tipmail{brian_griffin}{mentor.com}
\\
Created&Friday, \(\text{17}^{\text{th}}\) August 2012\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.1 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

Proposed expansion of what constitutes a function in an \textbf{expr} expression.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale: Nested expr Calls}

The \textbf{expr} command in Tcl is the only way to perform math and comparison operations. Tcl's variable and command substitution rules allow for great flexibility, although the syntax does not lead to great readability, especially due to the ordering imposed on substitutions. For example, in the expression:
\begingroup\small\begin{verbatim}
 expr {[lindex $data $start] > 4}
\end{verbatim}
\endgroup

the command substitution ([...]) is performed before the expression can be parsed for evaluation, and, in order for the command substitution to be parsed, the variable substitution (\$) must be performed. The consequences of this ordering means that arguments to commands cannot themselves be expressions unless the argument is itself a command substitution involving \textbf{expr}. This means, for example, that to compute an index value, \textbf{expr} must be recursively called:
\begingroup\small\begin{verbatim}
 expr {[lindex $data [expr {$start + 2}]] > 4}
\end{verbatim}
\endgroup

However, when using a math function, the command substitution ordering does not apply since the function is part of the expression syntax:
\begingroup\small\begin{verbatim}
 expr {pow($x + 2, 2)}
\end{verbatim}
\endgroup

It seems reasonable and useful if any Tcl command can be called using function syntax:
\begingroup\small\begin{verbatim}
 expr {lindex($data, $start + 2) > 4}
\end{verbatim}
\endgroup

The common command substitution pattern has lead many developers to end up writing things like:
\begingroup\small\begin{verbatim}
 if {[expr {$a+$b}] > 7} ...
\end{verbatim}
\endgroup

In hindsight, such statements look ridiculous, but it works and it follows the same pattern as the \textbf{lindex} case above. If subexpressions could be evaluated directly by the enclosing \textbf{expr} operations, this would greatly simplify the overall expression and make it more readable and thereby more maintainable, and hopefully, less error prone.

The goal here is to reduce the instances of recursive \textbf{expr} calls in an expression. It seems overly complex and confusing to read, and to have to use command substitution syntax inside what is already an \textbf{expr} operation.

\section{Proposed Changes}

The proposal is to simply apply normal Tcl command resolution rules to \textbf{expr} functions. The one caveat is that \textbf{tcl::mathfunc} namespace is always searched first.

I see this as a continuation to \cite{tip232}.

\section{Rejected Alternatives}

This can already be accomplished today by creating alias functions in the \textbf{tcl::mathfunc} namespace, but this mechanism is awkward in that:
\begin{enumerate}
\item{}
It must be defined beforehand for the set of commands used or intended to be used in expressions,
\setcounter{enumi}{1}
\item{}
For OO and namespace code, some specialized duplication is also required, and
\setcounter{enumi}{2}
\item{}
It can lead to confusion when it's not clear why some commands work as functions and others don't.
\end{enumerate}

\section{Proof of Concept}

(Thanks and credit to Fr\'{e}d\'{e}ric Bonnet)

A quick \& dirty proof of concept with unknown handlers:
\begingroup\small\begin{verbatim}
proc mathproc {cmd args} {
    if {[namespace qualifiers $cmd] eq "tcl::mathfunc"} {
        return [uplevel [list [namespace tail $cmd]] $args]
    }
    uplevel [list ::unknown $cmd] $args
}
namespace unknown mathproc

set l [expr {list(1,2,3)}]
=> 1 2 3

expr {lindex($l,2)}
=> 3

expr {expr(join($l,"+"))}
=> 6
\end{verbatim}
\endgroup

\section{Reference Implementation}

None currently

\section{Copyright}

Copyright (c) 2012 by GriffinTk

This document is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License [\url{http://creativecommons.org/licenses/by-sa/3.0/deed.en_US}].

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#232]{tip232}
Arjen Markus,
Kevin Kenny,
\emph{Creating New Math Functions for the 'expr' Command},
on-line at \url{http://purl.org/tcl/tip/232.html}
\end{thebibliography}
\end{document}
