Pure (programming language)

Last updated
Pure
Pure lang logo.png
Pure with texmacs.png
Using Pure with TeXmacs
Paradigm Functional, declarative, term rewriting
Designed by Albert Gräf
Developer Albert Gräf
First appeared2008;16 years ago (2008)
Stable release
0.68 / 11 April 2018;6 years ago (2018-04-11)
Typing discipline strong, dynamic
OS Cross-platform: FreeBSD, Linux, macOS, Windows
License GNU Lesser General Public License
Website agraef.github.io/pure-lang
Influenced by
Q, Haskell, Lisp, Alice, MATLAB

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 (multiple-precision numbers), and compiling to native code through the LLVM. Pure is free and open-source software distributed (mostly) under the GNU Lesser General Public License version 3 or later.

Contents

Overview

Pure comes with an interpreter and debugger, provides automatic memory management, has powerful functional and symbolic programming abilities, and interfaces to libraries in C (e.g., for numerics, low-level protocols, and other such tasks). 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 (rather than off-side rule indents) 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 (such as local functions with lexical scoping, efficient vector and matrix support, and the built-in C interface) 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 uses, 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 (for Open Sound Control (OSC)).

Examples

The Fibonacci numbers (naive version):

fib0=0;fib1=1;fibn=fib(n-2)+fib(n-1)ifn>1;

Better (tail-recursive and linear-time) version:

fibn=fibs(0,1)nwithfibs(a,b)n=ifn<=0thenaelsefibs(b,a+b)(n-1);end;

Compute the first 20 Fibonacci numbers:

mapfib(1..20);

An algorithm for the n queens problem which employs a list comprehension to organize the backtracking search:

queensn=searchn1[]withsearchnip=[reversep]ifi>n;=cat[searchn(i+1)((i,j):p)|j=1..n;safe(i,j)p];safe(i,j)p=~any(check(i,j))p;check(i1,j1)(i2,j2)=i1==i2||j1==j2||i1+j1==i2+j2||i1-j1==i2-j2;end;

While Pure uses eager evaluation by default, it also supports lazy data structures such as streams (lazy lists). For instance, David Turner's algorithm [1] for computing the stream of prime numbers by trial division can be expressed in Pure:

primes=sieve(2..inf)withsieve(p:qs)=p:sieve[q|q=qs;qmodp]&;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 (using call by need evaluation) when the corresponding part of the list is accessed, e.g.:

primes!!(0..99);// yields the first 100 primes

Pure has efficient support for vectors and matrices (similar to that of MATLAB and GNU Octave), including vector and matrix comprehensions. E.g., a Gaussian elimination algorithm with partial pivoting can be implemented in Pure thus:

gauss_eliminationx::matrix=p,xwhenn,m=dimx;p,_,x=foldlstep(0..n-1,0,x)(0..m-1)end;step(p,i,x)j=ifmax_x==0thenp,i,xelse// updated row permutation and index:transpimax_ip,i+1,{//thetoprowsofthematrixremainunchanged:x!!(0..i-1,0..m-1);// the pivot row, divided by the pivot element:{x!(i,l)/x!(i,j)|l=0..m-1};// subtract suitable multiples of the pivot row:{{x!(k,l)-x!(k,j)*x!(i,l)/x!(i,j)|k=i+1..n-1;l=0..m-1}}whenn,m=dimx;max_i,max_x=pivoti(colxj);x=ifmax_x>0thenswapximax_ielsex;endwithpivotix=foldlmax(0,0)[j,abs(x!j)|j=i..#x-1];max(i,x)(j,y)=ifx<ythenj,yelsei,x;end;/* Swap rows i and j of the matrix x. */swapxij=x!!(transpij(0..n-1),0..m-1)whenn,m=dimxend;/* Apply a transposition to a permutation. */transpijp=[p!trk|k=0..#p-1]withtrk=ifk==ithenjelseifk==jthenielsekend;/* Example: */letx=dmatrix{2,1,-1,8;-3,-1,2,-11;-2,1,2,-3};x;gauss_eliminationx;

As a language based on term rewriting, Pure fully supports symbolic computation with expressions. Here is an example showing the use of local rewriting rules to expand and factor simple arithmetic expressions:

expand=reducewith(a+b)*c=a*c+b*c;a*(b+c)=a*b+a*c;end;factor=reducewitha*c+b*c=(a+b)*c;a*b+a*c=a*(b+c);end;expand((a+b)*2);// yields a*2+b*2factor(a*2+b*2);// yields (a+b)*2

Calling C functions from Pure is very easy. E.g., for a "Hello, World!" program, the following imports the puts function from the C library and uses it to print the string "Hello, world!" on the terminal:

externintputs(char*);hello=puts"Hello, world!";hello;

See also

Related Research Articles

<span class="mw-page-title-main">Erlang (programming language)</span> Programming language

Erlang is a general-purpose, concurrent, functional high-level programming language, and a garbage-collected runtime system. The term Erlang is used interchangeably with Erlang/OTP, or Open Telecom Platform (OTP), which consists of the Erlang runtime system, several ready-to-use components (OTP) mainly written in Erlang, and a set of design principles for Erlang programs.

Mercury is a functional logic programming language made for real-world uses. The first version was developed at the University of Melbourne, Computer Science department, by Fergus Henderson, Thomas Conway, and Zoltan Somogyi, under Somogyi's supervision, and released on April 8, 1995.

OCaml is a general-purpose, high-level, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.

<span class="mw-page-title-main">Sieve of Eratosthenes</span> Ancient algorithm for generating prime numbers

In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.

<span class="mw-page-title-main">Dynamic programming</span> Problem optimization method

Dynamic programming is both a mathematical optimization method and an algorithmic paradigm. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.

<span class="mw-page-title-main">Block matrix</span> Matrix defined using smaller matrices called blocks

In mathematics, a block matrix or a partitioned matrix is a matrix that is interpreted as having been broken into sections called blocks or submatrices.

In category theory, a branch of mathematics, a pushout is the colimit of a diagram consisting of two morphisms f : ZX and g : ZY with a common domain. The pushout consists of an object P along with two morphisms XP and YP that complete a commutative square with the two given morphisms f and g. In fact, the defining universal property of the pushout essentially says that the pushout is the "most general" way to complete this commutative square. Common notations for the pushout are and .

The general linear model or general multivariate regression model is a compact way of simultaneously writing several multiple linear regression models. In that sense it is not a separate statistical linear model. The various multiple linear regression models may be compactly written as

Cilk, Cilk++, Cilk Plus and OpenCilk are general-purpose programming languages designed for multithreaded parallel computing. They are based on the C and C++ programming languages, which they extend with constructs to express parallel loops and the fork–join idiom.

<span class="mw-page-title-main">Interval arithmetic</span> Method for bounding the errors of numerical computations

Interval arithmetic is a mathematical technique used to mitigate rounding and measurement errors in mathematical computation by computing function bounds. Numerical methods involving interval arithmetic can guarantee relatively reliable and mathematically correct results. Instead of representing a value as a single number, interval arithmetic or interval mathematics represents each value as a range of possibilities.

<span class="mw-page-title-main">Guillotine cutting</span> Process of producing small rectangular items of fixed dimensions

Guillotine cutting is the process of producing small rectangular items of fixed dimensions from a given large rectangular sheet, using only guillotine-cuts. A guillotine-cut is a straight bisecting line going from one edge of an existing rectangle to the opposite edge, similarly to a paper guillotine.

In statistics, a fixed effects model is a statistical model in which the model parameters are fixed or non-random quantities. This is in contrast to random effects models and mixed models in which all or some of the model parameters are random variables. In many applications including econometrics and biostatistics a fixed effects model refers to a regression model in which the group means are fixed (non-random) as opposed to a random effects model in which the group means are a random sample from a population. Generally, data can be grouped according to several observed factors. The group means could be modeled as fixed or random effects for each grouping. In a fixed effects model each group mean is a group-specific fixed quantity.

In mathematics, a moment matrix is a special symmetric square matrix whose rows and columns are indexed by monomials. The entries of the matrix depend on the product of the indexing monomials only

In mathematics, auxiliary functions are an important construction in transcendental number theory. They are functions that appear in most proofs in this area of mathematics and that have specific, desirable properties, such as taking the value zero for many arguments, or having a zero of high order at some point.

Job-shop scheduling, the job-shop problem (JSP) or job-shop scheduling problem (JSSP) is an optimization problem in computer science and operations research. It is a variant of optimal job scheduling. In a general job scheduling problem, we are given n jobs J1J2, ..., Jn of varying processing times, which need to be scheduled on m machines with varying processing power, while trying to minimize the makespan – the total length of the schedule. In the specific variant known as job-shop scheduling, each job consists of a set of operationsO1O2, ..., On which need to be processed in a specific order. Each operation has a specific machine that it needs to be processed on and only one operation in a job can be processed at a given time. A common relaxation is the flexible job shop, where each operation can be processed on any machine of a given set.

The nullity theorem is a mathematical theorem about the inverse of a partitioned matrix, which states that the nullity of a block in a matrix equals the nullity of the complementary block in its inverse matrix. Here, the nullity is the dimension of the kernel. The theorem was proven in an abstract setting by Gustafson (1984), and for matrices by.

<span class="mw-page-title-main">David Shmoys</span> American mathematician

David Bernard Shmoys is a Professor in the School of Operations Research and Information Engineering and the Department of Computer Science at Cornell University. He obtained his Ph.D. from the University of California, Berkeley in 1984. His major focus has been in the design and analysis of algorithms for discrete optimization problems.

This article describes the features in the programming language Haskell.

In statistics and in machine learning, a linear predictor function is a linear function of a set of coefficients and explanatory variables, whose value is used to predict the outcome of a dependent variable. This sort of function usually comes in linear regression, where the coefficients are called regression coefficients. However, they also occur in various types of linear classifiers, as well as in various other models, such as principal component analysis and factor analysis. In many of these models, the coefficients are referred to as "weights".

References

Notes

  1. Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975.