Version 0 of ensemble with parameters

Updated 2008-10-18 23:56:08 by lars_h

A feature which is new in Tcl 8.6a3 is that namespace ensembles can have -parameters. This page explains this feature and shows some applications.

Explanation

What is a parameter?

In mathematics (and the like, e.g. physics), one sometimes distinguishes between two types of arguments for functions: "normal" arguments, and parameters. The parameters may be separated from the normal arguments by a semicolon (e.g. f(x;q)) or bar (e.g. F(q,r | x)), but the most common is probably that the parameter appears as a subscript of the main function symbol, such as the α on Bessel's J function in [L1 ]. When implemented in tcllib, this α is the first of two arguments of the math::special::Jn command, but it is usually only the second argument x that is considered a normal argument of "the Bessel J function" — α is merely a parameter.

The difference between parameters and other arguments is ultimately in the eye of the beholder, but some common distinguishing characteristics are:

  • Parameters don't tend to change as much as normal arguments.
  • Parameters can be of a different type than ordinary variables (e.g. if one is doing calculus, then integer arguments may be put aside as parameters, since it doesn't make sense to differentiate with respect to them).
  • The special case of the function that one gets by assigning fixed values to the parameters is still useful to reason about — perhaps even more useful than the general function.

In the case of math::special::Jn, all of these can be used to label the first argument as a parameter and the second as a normal argument.

Parameters in Tcl

In some languages, syntax enforces an explicit distinction between parameters and normal arguments. In for example [L2 ] (PDF document), it says:

It is important to define the mathematically natural concept, rather than the computationally natural one. This may sound obvious, but we will give an illustration: the Bessel functions. Consider the Bessel function (J_ν(z) for simplicity: the same points are true for other functions). This could be defined, as it is in all numerical libraries, as a function (ℂ×ℂ)→ℂ, but it makes more sense to define it as ℂ→(ℂ→ℂ), so that we can say that J(ν) satisfies Bessel's equation, rather than having to say that λx.J(ν,x) satisfies Bessel's equation.

Tcl is not one of these languages, since our best counterpart of "function" in the above sense is the command prefix. A practical definition of "parameter" in Tcl is argument included in the command prefix, since additional arguments provided when calling the command prefix are merely appended to the list of words that are found in it, and passed along to the base command without notice of where they came from; this (or things very much like it) is sometimes called currying [L3 ] (see also Custom curry). As of Tcl 8.5, you can use list to curry any command (or command prefix), and the only cost of doing so is that you have to remember to use {*} when calling the result of this currying, since it is a command prefix rather than a command.

There was however in Tcl 8.5 one class of commands which wasn't possible to bundle with parameters in a command prefix (or at least not very useful to do so), namely namespace ensembles. The reason for this was that the subcommand name of an ensemble was always the second word in the command; it you regard it as a parameter, then you're always calling the same subcommand, so you might just as well let your command prefix be what this command–subcommand combination is mapped to by the ensemble. This shouldn't be taken as saying a command–subcommand combination is never a useful command prefix (they sometimes are), rather the point is that there was no way to bundle a data parameter for an ensemble into a command prefix without also hardwiring the subcommand used.

Ensemble parameters

The solution TIP#314 [L4 ] provides to this problem is simply to add another option -parameters for namespace ensembles, which controls which word in the command is considered the subcommand name which the ensemble should map to something else.