Pure (programming language)

Last updated
Pure
Pure with texmacs.png
Using Pure with TeXmacs
Paradigm Functional, declarative, term rewriting
Designed by Albert Gräf
Developer Albert Gräf
First appeared2008;17 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 [1] , 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 [2] , and real-time multimedia processing [3]

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 [4] 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.

Namespaces, types and interfaces belong to the standard repertoire:

nonfixnil;typebintreenil|bintree(binxleftright);outfix«»;namespacefoo(«»);infixr(::^)^;x^y=2*x+y;namespace;interfacestackwithpushs::stackx;pops::stack;tops::stack;end;typestack[];pushxs@[]x|pushxs@(_:_)x=x:xs;pop(x:xs)=xs;top(x:xs)=x;


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;

Instead of manually compiling source files to LLVM bitcode modules, one can also place the source code into a Pure script, enclosing it in %< ... %> (inline code, e.g. C, Fortran 77/90 and so on).

See also

Related Research Articles

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.

Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving and computational linguistics.

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

Lua is a lightweight, high-level, multi-paradigm programming language designed mainly for embedded use in applications. Lua is cross-platform software, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C application programming interface (API) to embed it into applications.

Standard ML (SML) is a general-purpose, high-level, modular, functional programming language with compile-time type checking and type inference. It is popular for writing compilers, for programming language research, and for developing theorem provers.

<span class="mw-page-title-main">Scilab</span> Open-source numerical computation software

Scilab is a free and open-source, cross-platform numerical computational package and a high-level, numerically oriented programming language. It can be used for signal processing, statistical analysis, image enhancement, fluid dynamics simulations, numerical optimization, and modeling, simulation of explicit and implicit dynamical systems and symbolic manipulations.

<span class="mw-page-title-main">D (programming language)</span> Multi-paradigm system programming language

D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language. As it has developed, it has drawn inspiration from other high-level programming languages. Notably, it has been influenced by Java, Python, Ruby, C#, and Eiffel.

In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: "either it will or will not be a match." The patterns generally have the form of either sequences or tree structures. Uses of pattern matching include outputting the locations of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence.

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 .

In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.

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.

The GNU Scientific Library is a software library for numerical computations in applied mathematics and science. The GSL is written in C; wrappers are available for other programming languages. The GSL is part of the GNU Project and is distributed under the GNU General Public License.

<span class="mw-page-title-main">Fredkin gate</span> Universal reversible logic gate, applied in quantum computing

The Fredkin gate is a computational circuit suitable for reversible computing, invented by Edward Fredkin. It is universal, which means that any logical or arithmetic operation can be constructed entirely of Fredkin gates. The Fredkin gate is a circuit or device with three inputs and three outputs that transmits the first bit unchanged and swaps the last two bits if, and only if, the first bit is 1.

<span class="mw-page-title-main">Macaulay2</span> Computer algebra system

Macaulay2 is a free computer algebra system created by Daniel Grayson and Michael Stillman for computation in commutative algebra and algebraic geometry.

<span class="mw-page-title-main">Cosmos (operating system)</span> Toolkit for building GUI and command-line based operating systems

C# Open Source Managed Operating System (Cosmos) is a toolkit for building GUI and command-line based operating systems, written mostly in the programming language C# and small amounts of a high-level assembly language named X#. Cosmos is a backronym, in that the acronym was chosen before the meaning. It is open-source software released under a BSD license.

<span class="mw-page-title-main">Cython</span> Programming language

Cython is a superset of the programming language Python, which allows developers to write Python code that yields performance comparable to that of C.

This article describes the features in the programming language Haskell.

Haskell is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell has pioneered several programming language features such as type classes, which enable type-safe operator overloading, and monadic input/output (IO). It is named after logician Haskell Curry. Haskell's main implementation is the Glasgow Haskell Compiler (GHC).

The Perl virtual machine is a stack-based process virtual machine implemented as an opcodes interpreter which runs previously compiled programs written in the Perl language. The opcodes interpreter is a part of the Perl interpreter, which also contains a compiler in one executable file, commonly /usr/bin/perl on various Unix-like systems or perl.exe on Microsoft Windows systems.

Racket has been under active development as a vehicle for programming language research since the mid-1990s, and has accumulated many features over the years. This article describes and demonstrates some of these features. Note that one of Racket's main design goals is to accommodate creating new programming languages, both domain-specific languages and completely new languages. Therefore, some of the following examples are in different languages, but they are all implemented in Racket. Please refer to the main article for more information.

<span class="mw-page-title-main">Cuneiform (programming language)</span> Open-source workflow language

Cuneiform is an open-source workflow language for large-scale scientific data analysis. It is a statically typed functional programming language promoting parallel computing. It features a versatile foreign function interface allowing users to integrate software from many external programming languages. At the organizational level Cuneiform provides facilities like conditional branching and general recursion making it Turing-complete. In this, Cuneiform is the attempt to close the gap between scientific workflow systems like Taverna, KNIME, or Galaxy and large-scale data analysis programming models like MapReduce or Pig Latin while offering the generality of a functional programming language.

References

Notes

  1. Q-Equational-Programming-Language https://q-lang.sourceforge.net/
  2. "REDUCE Related Projects". REDUCE Computer Algebra System. Retrieved 2025-01-19.
  3. FAUST https://faust.grame.fr/.
  4. Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975.