\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#283: Modify Ensemble Command Resolution Behaviour}
\date{October 01, 2006}
\author{Miguel Sofer, Neil Madden}
\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 \#283}&\textbf{Modify Ensemble Command Resolution Behaviour}\\\hline
Author&

Miguel Sofer \tipmail{msofer}{users.sourceforge.net}
\par
Neil Madden \tipmail{nem}{cs.nott.ac.uk}
\\
Created&Sunday, \(\text{1}^{\text{st}}\) October 2006\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.22 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes that ensembles resolve all commands in their namespace.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Ensembles as proposed in \cite{tip112} cannot interoperate with the \textbf{namespace path} and \textbf{namespace unknown} functionality: as they resolve all commands in the caller's scope, it is necessary to define them using fully qualified names to insure that they are found in the ensemble's namespace. But fully qualified names bypass the namespace's \textbf{path} and \textbf{unknown} redirectors.

As an example (and risking ``uh-oo'' reactions in the present context) I would like to remark that this feature enables lightweight OO systems based on ensembles to provide inheritance of methods via \textbf{namespace path} and \textbf{namespace unknown}.

\section{Proposed Change}

This TIP proposes to fix this by changing the man page description of the ensemble unknown handler from:

\begin{quote}
It is up to the unknown handler to supply all namespace qualifiers if the implementing subcommand is not in the namespace of the caller of the ensemble command.
\end{quote}

to:

\begin{quote}
The implementing subcommand is looked up in the ensemble's namespace using normal command location rules.
\end{quote}

The description of the \textit{-map} option should change from:

\begin{quote}
When non-empty, this option supplies a dictionary that provides a mapping from subcommand names to a list of prefix words to substitute in place of the ensemble command and subcommand words (in a manner similar to an alias created with \textbf{interp alias}; the words are not reparsed after substitution).
\end{quote}

to:

\begin{quote}
When non-empty, this option supplies a dictionary that provides a mapping from subcommand names to a list of prefix words to substitute in place of the ensemble command and subcommand words (in a manner similar to an alias created with \textbf{interp alias}; the words are not reparsed after substitution). The resulting command is invoked with the original arguments in the namespace of the ensemble, though no additional stack frames are pushed in the process.
\end{quote}

\subsection{Compatability}

All scripts that followed previous best-practice and placed fully qualified command names in the command map or returned them from the unknown handler will be unaffected by this change. Only ensembles whose behaviour was undefined previously will be influenced, and then strictly in a positive direction.

\section{Reference Implementation and Documentation}

[RFE 1577282] [\url{http://sourceforge.net/tracker/index.php?func=detail&aid=1577282&group_id=10894&atid=360894}] (which depends on committed [Patch 1577278]) provides an implementation, with tests and docs.

\section{Illustration}
\begin{itemize}
\item{}
Consider the following script (without FQ names) stored in /tmp/test:
\begingroup\small\begin{verbatim}
   namespace eval a {
       proc check args {return YES}
   }
   namespace eval b {
       namespace path ::a
       namespace ensemble create \
           -command ::b \
           -map {go check}
   }
   namespace eval c {
       namespace path ::a
       namespace ensemble create \
           -command ::c \
           -map {go check} \
           -unknown u
       proc u args {return check}
   }
   namespace eval d {
       namespace unknown u
       namespace ensemble create \
           -command ::d \
           -map {go check}
       proc u args {::a::check}
   }
   array set res {}
   foreach cmd {b c d} {
       foreach scmd {foo go} {
           catch {$cmd $scmd} msg
           set res($cmd|$scmd) $msg
       }
   }
   parray res
\end{verbatim}
\endgroup
\end{itemize}

Currently the output is
\begingroup\small\begin{verbatim}
   mig@ice:~$ tclsh /tmp/test
   res(b|foo) = unknown or ambiguous subcommand "foo": must be go
   res(b|go)  = invalid command name "::b::check"
   res(c|foo) = invalid command name "u"
   res(c|go)  = invalid command name "::c::check"
   res(d|foo) = unknown or ambiguous subcommand "foo": must be go
   res(d|go)  = invalid command name "::d::check"
\end{verbatim}
\endgroup

After this TIP, it should be
\begingroup\small\begin{verbatim}
   mig@ice:~$ tclsh /tmp/test
   res(b|foo) = unknown or ambiguous subcommand "foo": must be go
   res(b|go)  = YES
   res(c|foo) = YES
   res(c|go)  = YES
   res(d|foo) = unknown or ambiguous subcommand "foo": must be go
   res(d|go)  = YES
\end{verbatim}
\endgroup
\begin{itemize}
\item{}
One commenter asked what the difference would be in the following code:
\begingroup\small\begin{verbatim}
   namespace eval ens {
       proc foo {} {
           puts "Caller namespace is: [uplevel 1 namespace current]"
           puts "I am [namespace origin foo]"
           puts "I am called as: [lindex [info level 0] 0]"
       }
       namespace export ens
       namespace ensemble create -command ens -map {sub foo}
   }
   namespace eval caller {
       proc foo {} {
           puts "Caller namespace is: [uplevel 1 namespace current]"
           puts "I am [namespace origin foo]"
           puts "I am called as: [lindex [info level 0] 0]"
       }
       namespace import ::ens::ens
       ens sub
   }
\end{verbatim}
\endgroup
\end{itemize}

Today (*) and after the patch this returns:
\begingroup\small\begin{verbatim}
   Caller namespace is: ::caller
   I am ::ens::foo
   I am called as: ens
\end{verbatim}
\endgroup

(*) Today==2006-24-10, after making \textbf{info level} aware of ensemble rewrites; prior to that date, only the last line would have changed from \textit{::ens::foo} to \textit{foo}.

\section{Remarks}
\begin{itemize}
\item{}
See also [Bug 1436096]
\end{itemize}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#112]{tip112}
Donal K. Fellows,
\emph{Ensembles are Namespaces are Commands},
on-line at \url{http://purl.org/tcl/tip/112.html}
\end{thebibliography}
\end{document}
