Pure (programming language)
Pure, successor to the equational language Q, is a dynamically typed, functional programming language based on term rewriting. It has facilities for user-defined operator syntax, macros, arbitrary-precision arithmetic, and compiling to native code through the LLVM. Pure is free and open-source software distributed under the GNU Lesser General Public License version 3 or later.
Pure comes with an interpreter and debugger, provides automatic memory management, has powerful functional and symbolic programming abilities, and interfaces to libraries in C. At the same time, Pure is a small language designed from scratch; its interpreter is not large, and the library modules are written in Pure. The syntax of Pure resembles that of Miranda and Haskell, but it is a free-format language and thus uses explicit delimiters to denote program structure.
The Pure language is a successor of the equational programming language Q, previously created by the same author, Albert Gräf at the University of Mainz, Germany. Relative to Q, it offers some important new features and programs run much faster as they are compiled just-in-time to native code on the fly. Pure is mostly aimed at mathematical applications and scientific computing currently, but its interactive interpreter environment, the C interface and the growing set of addon modules make it suitable for a variety of other applications, such as artificial intelligence, symbolic computation, and real-time multimedia processing.
Pure plug-ins are available for the Gnumeric spreadsheet and Miller Puckette's Pure Data graphical multimedia software, which make it possible to extend these programs with functions written in the Pure language. Interfaces are also provided as library modules to GNU Octave, OpenCV, OpenGL, the GNU Scientific Library, FAUST, SuperCollider, and liblo.
Examples
The Fibonacci numbers :
fib 0 = 0;
fib 1 = 1;
fib n = fib + fib if n>1;
Better version:
fib n = fibs n with
fibs n = if n<=0 then a else fibs ;
end;
Compute the first 20 Fibonacci numbers:
map fib ;
An algorithm for the n queens problem which employs a list comprehension to organize the backtracking search:
queens n = search n 1 with
search n i p = if i>n;
= cat ;
safe p = ~any p;
check
= i1i2 || j1j2 || i1+j1i2+j2 || i1-j1i2-j2;
end;
While Pure uses eager evaluation by default, it also supports lazy data structures such as streams. For instance, David Turner's algorithm for computing the stream of prime numbers by trial division can be expressed in Pure:
primes = sieve with
sieve = p : sieve &;
end;
Use of the
&
operator turns the tail of the sieve into a thunk to delay its computation. The thunk is evaluated implicitly and then memoized when the corresponding part of the list is accessed, e.g.:
primes!!; // yields the first 100 primes
Pure has efficient support for vectors and matrices, including vector and matrix comprehensions. E.g., a Gaussian elimination algorithm with partial pivoting can be implemented in Pure thus:
gauss_elimination x::matrix = p,x
when n,m = dim x; p,_,x = foldl step end;
step j
= if max_x0 then p,i,x else
// updated row permutation and index:
transp i max_i p, i+1,