Q - Equational Programming Language




About Q

Q is a functional programming language based on term rewriting. Thus, a Q program or "script" is simply a collection of equations which are used to evaluate expressions in a symbolic fashion. The equations establish algebraic identities and are interpreted as rewriting rules in order to reduce expressions to "normal forms". For instance, here is how you define a function sqr which squares its argument by multiplying it with itself:

sqr X          = X*X;

Note that, as in Prolog, capitalized identifiers are used to indicate the variables in an equation, which are bound to the actual values when an equation is applied. Equations may also include a condition part, as in the following definition of the factorial function:

fact N         = N*fact (N-1) if N>0;
               = 1 otherwise;

Functions on structured arguments are defined by "pattern matching". E.g., the product of a list (denoted in Prolog-like syntax) can be computed with these two equations:

prod []        = 1;
prod [X|Xs]    = X*prod Xs;

With this definition, the factorial can now also be defined as follows (the notation [1..N], as in Haskell, denotes an arithmetic sequence):

fact N         = prod [1..N];

As you can see, the definitions are really just like mathematical equations. The syntax is superficially similar to other modern functional languages like Miranda and Haskell, except that Q is "free-format", i.e., it does not use layout to indicate syntactical structure (thus the semicolon is used to terminate an equation).

Due to its term rewriting heritage, Q goes well beyond most other functional languages in that it also allows you to perform computations with symbolic expressions. For instance, with the definition of the sqr function from above, you will find that sqr (X+1) evaluates to (X+1)*(X+1). This might first look like an arcane feature, but it is actually quite useful, because you can try your definitions with symbolic inputs, too.

Functional Programming Power

Despite its conceptual simplicity, Q is a full-featured functional programming language with a modern syntax, curried function applications (which implies that functions are first-class citizens which can be parameters and results of function applications), dynamic typing (using a single-inheritance OO type system, akin to Smalltalk), exception handling, and POSIX multithreading. Scripts are executed in a bytecode interpreter which byte-compiles scripts in an eye blink and runs them about as fast as interpreted Lisp or Haskell. The interpreter provides a built-in symbolic debugger which allows you to trace the reductions performed during expression evaluation.

Just like other modern functional languages, Q supports a terse and abstract mathematical style, and it is not uncommon that the equivalent of dozens of lines of C is just a one- or two-liner in Q. Even more important than raw code size is the high level of abstraction which enables you to handle complex data structures with ease, and tackle complicated programming tasks which are almost hopeless endeavours with conventional programming languages.

That is not to say that you won't ever write a line of C again. C has its uses for interfacing to third-party software and for time-critical processing. Like most scripting languages, Q also has a C interface. The Q interpreter is both extensible (new primitives written in C/C++ can be loaded at runtime) and embeddable (Q can be called inside your C/C++ applications). The distribution already includes a number of modules for interfacing to various popular third-party libraries, which makes Q a powerful tool for scientific programming, computer music, multimedia, and other advanced applications. As of version 6.0, Q now also supports SWIG which makes it easy to interface to other libraries if needed.

Using Q

Using Q is supposed to be fairly simple: you throw together some equations, start the interpreter and then type in the expressions you wish to evaluate. All this can be done with a few keystrokes, using "Q mode" in GNU Emacs/XEmacs. The distribution also includes a syntax file to enjoy syntax highlighting for the Q language in the advanced KDE editor Kate, and the Windows version comes with a graphical application called "Qpad" for editing and running Q scripts. As of Q 7.9, there also is a new Qt-based application called "QCalc" available, which is useful both as a Q spreadsheet and as an alternative frontend to the Q interpreter; please refer to the QCalc manual for more information. Of course, you can also run the Q programming utilities from the command line if you prefer that, and Q scripts can be run as standalone programs, too.

Batteries Included

The Q distribution is divided into a number of different packages. The core package is designed to be fairly portable and includes the Q interpreter and related tools, a standard library (written mostly in Q itself) and a set of standard modules. The standard library implements a lot of useful Q types and functions, such as rational and complex numbers, additional list processing functions (including list comprehensions), "streams" (a "lazy" variant of lists), container data structures (sets, dictionaries, etc.), and a PostScript interface. Also included in the core is a POSIX system interface which provides, among other goodies, lowlevel I/O, process and thread management, sockets, filename globbing and regular expression matching. A number of other modules add the necessary facilities for doing portable graphics (GGI/ImageMagick), Tcl/Tk GUIs (including support for Peter G. Baum's Gnocl and a GUI builder which lets you create GNOME/GTK+ applications), scientific programming (Octave) and visualization (OpenDX), databases (ODBC) and web programming (Curl, XML/XSLT). A Q module for the Apache web server is also available, which enables you to run your Q scripts "on the web".

There is also a growing number of different add-on modules. These may not be as portable as the core, but usually run at least under the more popular operating systems. Currently available are a combinatorial graph library and editor, an interface to the Qt GUI toolkit, and several multimedia-related modules (OpenGL+AL, MIDI, sound and synthesis, video, etc.), more are under development. Also take a look at the examples for some other applications which have been written using Q.

Open Source

Last but not least, Q is open source software, which means that the sources of all programs and software modules included in the Q programming system are freely available. Q is also free software which is distributed under the GNU General Public License; see the COPYING file for details.

© 2007 by Albert Gräf