Version 5 of infix

Updated 2008-01-13 13:14:46 by sarnold

One of the styles for writing expressions, with operations in between the operands, as contrasted to postfix (operations follow operands, as in RPN) and prefix (operations come before operands, as in Tcl in general). Infix notation for mathematical expressions is by far the most common, and is in Tcl supported by expr.


As of 2006-10-14, infix is also the name of a Tcl package (and command defined by this package) that lets programmers write sequences of mathematical formulae in the familiar infix form. A short example:

  package require infix 0.2
  ::infix::core::setup base numconst expr::ops expr::fun
  proc ngon_corner {num_sides radius} {
     infix {
        n <- num_sides
        r <- radius
     } {
        alpha = acos(-1) / n ;  # acos(-1) = pi
        r*cos(alpha), r*sin(alpha)
     }
  }
  ngon_corner 6 10 ; # Returns "8.66025403784 5.0" - a two element list

A notable feature is that the little language implemented by this package is completely configurable (setting it up for expr-like operations is what the ::infix::core::setup command does), so you can define new operations, or define the usual ones to do something unusual. A setting that turns +, -, etc. into the operations of the math::bignum package is included with the infix package. Sarnold: math::bignum is deprecated in Tcl 8.5, isn't it? But I see a real interest for complex numbers and math::bigfloat extension, for instance, to be infix'd. Nice and interesting work you've done!

The code is available at

  http://abel.math.umu.se/~lars/tcl/infix.tar.gz

(requires tcllib, tcllib 1.8 is sufficient).

A paper on the package (which includes the entire source code, commented and explained) is available at

  http://abel.math.umu.se/~lars/tcl/infix.pdf

-- Lars H


Some kind of a user's manual (incomplete)

The user commands created by the package are

::infix::core::setup ?module ...?

Creates a command infix in the namespace it is called from, and loads the listed modules of settings for the little language of that infix command. See below for lists of defined modules and the syntax of the infix command.

::infix::core::opalias name type cmd ?arg ...?

Define a new operation name of type type that gets implemented by appending the operand(s) to the command prefix cmd ?arg ...?, as specified. Any previous meaning of the token name gets overwritten.

The possible types include:

binary priority
A binary, left-associative operation with priority as specified.
binary priority associativity
A binary operation with priority and associativity as specified. Possibilities for associativity include right-associative, non-associative, and n-ary.
prefix priority
A unary prefix operation with priority as specified.
postfix priority
A unary postfix operation with priority as specified.

The priorities should be Tcl numbers (non-integers are fine). Higher priority means tighter binding to the operands. In case of equal priority, the associativity setting is used to resolve which operation acts on which operands. The standard modules uses priorities in the range -2 (for ;) to 14 (for factorial), with + at 10, * at 11, and ** (right-associative) at 12.

::infix::core::funalias name numargs cmd ?arg ...?

Define a new function name with numargs arguments that gets implemented by appending the argument(s) to the command prefix cmd ?arg ...?, as specified. Any previous meaning of the token name gets overwritten. numargs may be any (in which case any number of arguments are accepted) or an integer.

The infix command

The infix command has the syntax

infix symlinks body

The body is where the actual expressions in the infix little language are written; the infix command returns the value of (the last statement in) the body. The symlinks argument links symbolic names appearing in the body to Tcl variables in the context from which infix was called.

The format of the symlinks is a list with a multiple of three elements. The first element in a triplet is the infix body symbolic name. The last element in a triplet is the external quantity to which the symbolic name is linked. The middle element of the triplet is an "arrow" that determines how the two are linked:

<-
Input-only value; the external quantity is the name of a variable whose value is copied to the symbol.
->
Output-only value; the external quantity is the name of a variable which is set to the final value of the symbol.
<->
Input&output value; combines <- and ->.
<=
Input-only constant; the external quantity becomes the value of the symbol. Useful for constants that don't fit into the infix body syntax.
<e
The external quantity is evaluated as a script, and the symbol is set to the result of that script.

To increment variable $a by $b one could do

  infix {
     a <-> a
     b <-  b
  } { a := a + b }

but also

  infix {
     a <- a
     b <- b
     c -> a
  } { c = a + b }

To Be Continued...

List of modules

Each module module is implemented by the package infix::module, so anyone can define new modules. The following are those that come with the infix package itself.

baseBasic definitions: parentheses for grouping, = for definition, := for assignment, semicolon as separator, and comma as list constructor.
expr::opsThe unary and binary operations of expr.
expr::funThe expr built-in functions.
expr::ternaryThe expr ternary ?: operation. (See ifthen for an alternative.)
softsemicolonA more forgiving statement seprator. The base semicolons may only appear between expressions, but this relaxes the syntax so that a semicolon is effectively ignored if there is no expression after it.
ifthenImplements expression choices of the form if condition then expression else expression fi (also allowing elseif clauses and omitting the else clause). Unlike ?:, this can have semicolons and the like in the expressions without a need to wrap them up in parentheses.
numconstMakes symbols that look like numeric constants be interpreted as such. (Without it, e.g. 0, 1, and 3.5 behave just like x and y.) Underscore is a substitute for minus sign in exponents: 3.2e_1 is 3.2*10**-1.
bignumOperations as for expr::ops, but implemented using the math::bignum commands, with their representation for values. Numeric constants are supported, as are the postfix operations ! (factorial) and !! (semifactorial), and functions sqrt, powm, fromstr, and tostr. Compatible with the ifthen module.
TeX::semiChanges the tokenizer, so that TeX-style control sequences such \alpha and \cdot count as tokens.
expr::delimSome expr-functions written as delimiters.
listbracketBrackets for list construction and indexing.

[ Category Package | Category Mathematics ]