\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#461: Separate Numeric and String Comparison Operators}
\date{January 24, 2017}
\author{Kevin B Kenny, Kevin B Kenny, 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 \#461}&\textbf{Separate Numeric and String Comparison Operators}\\\hline
Author&

Kevin B Kenny \tipmail{kennyb}{acm.org}
\par
Kevin B Kenny \tipmail{kevin.b.kenny}{gmail.com}
\par
Kevin Kenny \tipmail{kennykb}{acm.org}
\\
Created&Tuesday, \(\text{24}^{\text{th}}\) January 2017\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.6 $\\
Keywords&Tcl, expression\\
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 encourages programmers 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}

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.

The six string compare operators shall be declared to function so that their results are the same as the results of [string compare]:
\begingroup\small\begin{verbatim}
    {$a lt $b}  <=> {[string compare $a $b] <  0}
    {$a le $b}  <=> {[string compare $a $b] <= 0}
    {$a eq $b}  <=> {[string compare $a $b] == 0}
    {$a ne $b}  <=> {[string compare $a $b] != 0}
    {$a gt $b}  <=> {[string compare $a $b] >  0}
    {$a ge $b}  <=> {[string compare $a $b] >= 0}
\end{verbatim}
\endgroup

It is also intended that any future changes to [string compare] (for example, a hypothetical change to make it follow Unicode collation semantics) will have the corresponding effect on these six operators.

Unlike what was specified in an earlier version of this TIP, no changes are to be made to the semantics of the comparison operators \textbf{==}, \textbf{\tiprangle{}=}, \textbf{\tiprangle{}}, \textbf{\tiplangle{}=}, \textbf{\tiplangle{}}, and \textbf{!=}.

\section{Discussion}

\subsection{Forcing typed comparisons in Tcl}

Programmers who wish to insure string semantics should restrict their comparisons to the \textbf{lt}, \textbf{le}, \textbf{eq}, \textbf{ne}, \textbf{gt} and \textbf{ge} operators.

Use of the \textbf{\tiplangle{}}, \textbf{\tiplangle{}=}, \textbf{==}, \textbf{!=}, \textbf{\tiprangle{}} and \textbf{\tiprangle{}=} operators with operands that might be non-numeric shall be regarded as poor programming style. Unless operands are constant, unary \textbf{+} should be used to force them to be numeric. Thus,
\begingroup\small\begin{verbatim}
 if {$x < $y} { ... }
\end{verbatim}
\endgroup

should be relaced 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.

\subsection{Rejected alternatives}

Earlier, the radical suggestion of \textit{requiring} the \textbf{\tiplangle{}}, \textbf{\tiplangle{}=}, \textbf{==}, \textbf{!=}, \textbf{\tiprangle{}} and \textbf{\tiprangle{}=} operators to have numeric arguments had been read into this TIP. It appears that there is far too much outstanding code that is written like:

 if \{\$x == ``somestring''\} \{ ... \}

to have the more radical option be viable.

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{}
\textit{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.}

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.
\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

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 \textbf{eq}, \textbf{ne}, \textbf{in} and \textbf{ni} operators. These are unlikely to go away. Adding \textbf{lt}, \textbf{le}, \textbf{gt} and \textbf{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.
\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.}

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.
\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].}

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 the expression
\begingroup\small\begin{verbatim}
   {[string compare $x $y] < 0}
\end{verbatim}
\endgroup
\end{enumerate}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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