This is not necessarily the current version of this TIP.
| TIP: | 232 |
| Title: | Creating New Math Functions for the 'expr' Command |
| Version: | $Revision: 1.2 $ |
| Author: | Arjen Markus <arjen dot markus at wldelft dot nl> |
| State: | Draft |
| Type: | Project |
| Tcl-Version: | 8.5 |
| Vote: | Pending |
| Created: | Friday, 26 November 2004 |
| Keywords: | math, expr, Tcl |
This TIP proposes to expose the functionality of the Tcl_CreateMathFunc() function to the script level, so that programmers can define new math functions using Tcl procedures. As this functionality can be implemented in such a way that a number of facilities specific to the [expr] command become obsolete, the Tcl core will actually become a bit simpler, rather than more complicated.
It has long been possible to define new (mathematical) functions for the expr command via the C API - the function Tcl_CreateMathFunc() is meant for this. However, it is not possible to define new math functions at the script level, so that you could use a Tcl procedure instead of a C function.
There exist one or two extensions that allow this, most notably "funcproc" by D. Fellows - <http://wiki.tcl.tk/541>), but as the Tcl core does not allow it, it remains a somewhat hidden feature. The current TIP aims at making the functionality of such extensions standard, not by introducing a new command (as in the first version of this TIP) but by using a different approach to the mathetical functions known to the [expr] command.
With "scripted" math functions you can simplify such complex and error-prone expressions as:
set max [expr {$max>($a+1)? $max : ($a+1)}]
or
set f [expr { $x*[J0 $x] + [J1 [expr {2.0*$x}]] }]
to the more regular:
set max [expr {max($max,$a+1)}]
and
set f [expr { $x*J0($x) + J1(2.0*$x) }]
(assuming the additional functions max, J0 and J1 have been defined of course).
To quote Donal Fellows:
"If you are interested in going the other way - writing functions for expr in Tcl without the use of square brackets (useful for when you want to expose expressions to users without having to tell them to learn all about Tcl, no matter how much that would benefit them in the long run. This sort of situation also requires the use of safe interpreters) - then you will probably find http://www.man.ac.uk/~zzcgudf/tcl/#scripts/funcproc very interesting indeed."
In the first version of this TIP, the functionality hinted at above was to be implemented by a single new command:
exprfunc funcname procname
where:
funcname is the name of the function as it is known to the expr command
procname is the Tcl procedure that implements the command
This led to considerable discussion, with arguments both pro and con and several alternatives like a macro facility for [expr]. One particular problem with the first version is that while Tcl_CreateMathFunc() allows you to define a new function, there is no way to remove a function or to rename it.
A suggestion made by Kevin Kenny leads a more appropriate solution, reformulated:
Treat mathematical functions as ordinary Tcl commands/procedures that reside in a specific namespace, ::tcl::mathFunc
If we implement mathematical functions that way, then:
All the usual facilities for manipulating Tcl commands and procedures become available automatically.
There is no need anymore for a lot of special facilities, like the Tcl_Value type and the existing wrappers can be turned into regular commands.
There is no restriction anymore that functions should return single numbers as their result, opening the way to functions for manipulating vectors of numbers or intervals or ...
In the evaluation of expressions, calling a function or calling a command will become the same thing, thus simplifying the implementation.
The existing function Tcl_CreateMathFunc() will become obsolete and could be removed in Tcl 9.0.
A reference implementation does not yet exist, but it should be possible to introduce this new way of dealing with mathematical functions piecemeal.
This document is placed in the public domain.
This is not necessarily the current version of this TIP.