Version 1 of infix

Updated 2006-10-15 17:07:49

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 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.

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

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. 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.


[ Category Package | Category Mathematics ]