TIP #363: VECTOR MATH IN THE TCL CORE ======================================= Version: $Revision: 1.1 $ Author: Karl C. Hansen State: Draft Type: Project Tcl-Version: 9.0 Vote: Pending Created: Tuesday, 02 March 2010 URL: https://tip.tcl-lang.org363.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== The "expand" operator - *{*}* - was adopted in Tcl 8.5 and is very useful for simplifying code involving lists. It is proposed to add this operator to the list of actions performed during processing of double-quote ("), joining the *$*, *[*, and *\*, and to modify the behavior of *$* in variable substitution. The proposed behavior /will/ break existing substitution, but a trivial quoted-string substitution restores original behavior with the new approach. The proposed approach will enable eventual incorporation of vector-math to the Tcl engine without changing any of the existing syntax. RATIONALE =========== It is desired to enhance the current math engine in TCL to handle vector math, without having to create new syntax or add cumbersome new function calls. Given the assignments *set ScalarA 1*, *set ScalarB 2*, *set ListA {1 2 3}*, and *set ListB {4 5 6}*, evaluating *$ScalarA + $ScalarB*, *$ListA + $ListB*, and *$ListA + $ScalarB* would all be trivially understandable, with the first behaving as currently implemented, the second returning a list containing the element-by-element sums of the two lists, and the third returning a list containing the elements of ListA incremented by the common ScalarB. With the proposed enhancement (see below) the expression parser would receive a brace-delimited set of values wherever the lists appear, and a single value wherever the scalars appear. With the proposed enhancement, and a vector-math core enhancement, given the assignments above, the following would behave identically: expr $ListA + {4 5 6} expr $ListA + $ListB expr {1 2 3} + $ListB expr {1 2 3} + {4 5 6} With the proposed enhancement, incorporating vector math into the TCL core is vastly simplified, reducing to enhancing the current math handlers to perform different actions based on whether they received lists or scalars or both, and checking that the vectors have the appropriate element counts for the operation specified. PROPOSAL ========== 1. Add expansion -- *{*}* -- to set of actions performed during double-quote processing. 1. Treat expand operator as a single character 'tri-graph'. 2. Escape the operator -- *\{*}* -- to turn it into "regular text" causing it to behave as it currently does during double-quote processing. Note that this is identical with the current behavior of escape, i.e., *\{* currently gives *{*. With the new implementation it turns the expand operator into normal text inside of a quoted string. 2. Modify *$* substitution so that /lists/ are enclosed in braces during substitution, i.e. after *set A {1 2}* execution of *puts "$A"* yields *{1 2}* instead of *1 2* as it currently does. Note that this behavior only manifests inside of quoted strings, as the assignment *set B $A* still gives the same results as the original implementation, i.e. assigning a list to *B*. 1. Prefix *$* with expand *{*}* to restore current behavior, i.e., with the same assignment above, execution of *puts "{*}$A"* yields *1 2* instead of *{*}1 2* as it currently does. Note that this provides a fairly simple way to fix scripts broken by implementing this proposal. If exising scripts contain quoted lists, simply (a) replace all double-quoted occurrances of *{*}* with *\{*}*, and (b) replace all double-quoted occurrances of *$* with *{*}$*. COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows