\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#10000: Dummy Proposal for Testing Editing Interfaces}
\date{December 03, 2000}
\author{Don Porter, Andreas Kupries, Richard Suchenwirth, Kevin B KENNY, Jeff Hobbs, Vince Darley, Fabrice Pardo, Joe Mistachkin, Donal K. Fellows, Mark Janssen, Reinhard Max, Andreas Leitgeb, Andreas Kupries, Francois Vogel, KEVIN KENNY}
\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 \#10000}&\textbf{Dummy Proposal for Testing Editing Interfaces}\\\hline
Author&

Don Porter \tipmail{dgp}{users.sourceforge.net}
\par
Andreas Kupries \tipmail{a.kupries}{westend.com}
\par
Richard Suchenwirth \tipmail{richard.suchenwirth}{kst.siemens.de}
\par
Kevin B KENNY \tipmail{kennykb}{acm.org}
\par
Jeff Hobbs \tipmail{hobbs}{users.sourceforge.net}
\par
Vince Darley \tipmail{vincentdarley}{users.sourceforge.net}
\par
Fabrice Pardo \tipmail{Fabrice.Pardo}{l2m.cnrs.fr}
\par
Joe Mistachkin \tipmail{joe}{mistachkin.com}
\par
Donal K. Fellows \tipmail{donal.k.fellows}{manchester.ac.uk}
\par
Mark Janssen \tipmail{mpc.janssen}{gmail.com}
\par
Reinhard Max \tipmail{max}{tclers.tk}
\par
Andreas Leitgeb \tipmail{avl}{logic.at}
\par
Andreas Kupries \tipmail{akupries}{shaw.ca}
\par
Francois Vogel \tipmail{fvogelnew1}{free.fr}
\par
KEVIN KENNY \tipmail{kevin.b.kenny}{gmail.com}
\\
Created&Sunday, \(\text{3}^{\text{rd}}\) December 2000\\
Type&Informative\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.216 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to complete the separation between string and numeric comparison operations in [expr] and related commands ([for], [if], [while], etc.). It introduces new comparison operators \textbf{ge}, \textbf{gt}, \textbf{le}, and \textbf{lt}, (along with the corresponding commands in the \textbf{::tcl::mathop} namespace), and to restrict the six operators \textbf{==}, \textbf{\tiprangle{}=}, \textbf{\tiprangle{}}, \textbf{\tiplangle{}=}, \textbf{\tiplangle{}} and \textbf{!=} to comparisons of numeric values.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Tcl throughout its history has had comparison operators that freely compare numeric and string values. These operators behave as expected if both their arguments are numeric: they compare values on the real number line. Hence, 15 \tiplangle{} 0x10 \tiplangle{} 0b10001. Similarly, if presented with non-numeric strings, they compare the strings in lexicographic order, as a programmer might expect: ``bambam'' \tiplangle{} ``barney'' \tiplangle{} ``betty'' \tiplangle{} ``fred''.

Trouble arises, however, when numeric and non-numeric strings are compared. The rule for comparison is that mixed-type comparisons like this are treated as string comparisons. The result is that \textbf{\tiplangle{}} does not induce an order. There are inconsistent comparison results, rendering \textbf{\tiplangle{}} and friends worthless for sorting. 0x10 \tiplangle{} 0y \tiplangle{} 1 \tiplangle{} 0x10.

The problems with this inconsistency prompted changes in May of 2000, introducing \textbf{eq} and \textbf{ne} operators that always perform string comparison. For whatever reason, the four inequality operations never followed. This leads to pitfalls for the unwary. It's fairly well entrenched in the Tcl folklore that comparisons other than \textbf{eq} and \textbf{ne} should be reserved for numeric arguments only, and experienced Tcl programmers know to write:
\begingroup\small\begin{verbatim}
 if {[string compare $x $y] < 0} { ... }
\end{verbatim}
\endgroup

in place of 
\begingroup\small\begin{verbatim}
 if {$x < $y} { ... }
\end{verbatim}
\endgroup

\section{Proposal}

Two things are proposed.
\begin{enumerate}
\item{}
[8.x] Four new bareword operators, \textbf{ge}, \textbf{gt}, \textbf{le} and \textbf{lt} shall be added to the expression parser and to the \textbf{::tcl::mathop} command set. They will have precedence identical to the existing operators \textbf{\tiprangle{}=}, \textbf{\tiprangle{}}, \textbf{\tiplangle{}=} and \textbf{\tiplangle{}}. They will accept string values, and return 0 or 1 according to lexicographic string comparison of their operators. This change is entirely backward compatible (it uses syntax that would previously have been erroneous), and should go in as soon as possible --- no later than the next point release, but ideally even in a patchlevel --- so that programmers can begin conversion as soon as possible. Use of the \textbf{==}, \textbf{\tiprangle{}=}, \textbf{\tiprangle{}}, \textbf{\tiplangle{}=}, \textbf{\tiplangle{}}, and \textbf{!=} for comparing non-numeric values shall immediately be deprecated.
\setcounter{enumi}{1}
\item{}
[9.0] Passing of non-numeric values to the \textbf{==}, \textbf{\tiprangle{}=}, \textbf{\tiprangle{}}, \textbf{\tiplangle{}=}, \textbf{\tiplangle{}}, and \textbf{!=} operators (or to their \textbf{tcl::mathop} equivalents) shall be forbidden and result in an error being thrown.
\end{enumerate}

\section{Discussion}

\subsection{Forcing numeric comparisons in today's Tcl}

Programmers who wish to prepare for the change, once the four new operators are in place, can adapt places in their code where they wish to force numeric comparisons by replacing expressions of the form:
\begingroup\small\begin{verbatim}
 if {$x < $y} { ... }
\end{verbatim}
\endgroup

with
\begingroup\small\begin{verbatim}
 if {+$x < +$y} { ... }
\end{verbatim}
\endgroup

The second comparison will have the effect of forcing both operands to be numeric, and the existing comparison code will then provide the correct semantics.

\subsection{Deprecation and compatibility}

It may be possible to introduce some sort of per-interpreter or per-namespace option to control the behaviour of numeric comparisons when evaluated in the given interpreter or namespace. The author of this TIP has not investigated how such an option might be implemented, and encourages those who propose it to do so. Until and unless a concrete implementation plan emerges from their investigation, the plan is to leave a ``backward compatibility'' setting out of scope.

In addition, at least one reader of this TIP has requested a setting whereby a warning can be delivered that functionality is deprecated. Since in the past, we have not been able to identify and standardize a mechanism whereby such warnings could be delivered, this functionality is also considered to be out of scope.

\subsection{Rejected alternatives}

One possible alternative to excluding non-numeric arguments from the comparison operators is to change their semantics so that all non-numeric strings are greater than all numbers. This change would at least yield a consistent ordering. The ordering that it yields would, however, be somewhat surprising, and not terribly useful. (It would at least be compatible with today's scheme for numeric comparisons.)

\subsection{Objections (and rebuttals)}

In out-of-band discussions, several objections were raised. This section attempts to address them.
\begin{enumerate}
\item{}
Tcl's expression parser has a hard limit of 64 different binary operators. This proposal consumes four of them, leaving only 28. There is a concern that this is a less-than-effective use of a limited resource.
\end{enumerate}

 The limit is self-imposed, in an effort to make the nodes of an expression parse tree fit in exactly 16 bytes (or four int's). It is far from obvious that this pretty size is actually useful. Few expressions are more than a few dozen parse nodes, and typical expressions are not parsed multiple times. It appears that neither the speed of the parse nor the size of the tree will be critical issues in most applications. In any case, we still have nearly half the operators left.
\begin{enumerate}
\setcounter{enumi}{1}
\item{}
\textit{There is some concern that using barewords for operators was a bad idea in the first place.} The fact that
\begingroup\small\begin{verbatim}
               expr {"foo"}
\end{verbatim}
\endgroup
\end{enumerate}

 and
\begingroup\small\begin{verbatim}
               set x foo; expr {$x}
\end{verbatim}
\endgroup

 both work, while
\begingroup\small\begin{verbatim}
               expr {foo}
\end{verbatim}
\endgroup

 is an invalid bareword is arguably surprising.

 Nevertheless, we have committed to the approach with the 'eq', 'ne', 'in' and 'ni' operators. These are unlikely to go away. Adding 'lt', 'le', 'gt' and 'ge' will make this problem no better nor worse.

 Moreover, the language of [expr] is not the same as Tcl. It does not strip comments, parse into words, and apply Tcl's precise substitution rules --- and it would be surprising if it did! There are other 'little languages' throughout Tcl --- regular expressions, glob patterns, assembly code, and so on. [expr] is one among many.
\begin{enumerate}
\setcounter{enumi}{2}
\item{}
\textit{There is concern that [expr], which was originally intended almost exclusively for numeric calculations, is being abused with string arguments and possibly string results.}
\end{enumerate}

 The author of this TIP contends that we introduced string values to [expr] a long time ago, certainly by the time that the \textbf{eq}, \textbf{ne}, \textbf{in} and \textbf{ni} operations were introduced. It is true that the use of numeric conversions in [expr] is incoherent, as seen in:
\begingroup\small\begin{verbatim}
               % proc tcl::mathfunc::cat {args} { join $args {} }
               % expr {cat(0x1,0x2,"a")}
               0x10x2a
               % expr {cat(0x1)}
               1
\end{verbatim}
\endgroup

 (Bug [e7c21ed678] is another manifestation of this general problem.) Once again, adding additional string operations that behave, with respect to data types, exactly the same as ones that are already there will neither fix nor exacerbate the general problem.
\begin{enumerate}
\setcounter{enumi}{3}
\item{}
\textit{Because [expr] has no interpreted form, the operations must have bytecode representations. The space of available bytecodes is under even more pressure than the space of available operators, and must not be squandered on operations that are duplicative of already-available functionality such as [string compare]}
\end{enumerate}

 The obvious rebuttal is that [string compare] is already bytecoded. There are no new operations required, merely a compiler that is smart enough to emit a short codeburst rather than a single bytecode. As an example, the code for the expression
\begingroup\small\begin{verbatim}
               {$x lt $y}
\end{verbatim}
\endgroup

 could be:
\begingroup\small\begin{verbatim}
    (0) loadScalar1 %v0        # var "x"
    (2) loadScalar1 %v1        # var "y"
    (4) strcmp 
    (5) push1 0        # "0"
    (7) lt 
\end{verbatim}
\endgroup

 For the other string operators, only the last bytecode in the burst would change. No new bytecode operations are needed. In fact, this codeburst is identical code to that generated for
\begingroup\small\begin{verbatim}
               {[string compare $x $y] < 0}
\end{verbatim}
\endgroup

\section{Colophon}

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