# Pure (programming language)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Pure_(programming_language)
> Markdown URL: https://mediated.wiki/source/Pure_(programming_language).md
> Source: https://en.wikipedia.org/wiki/Pure_(programming_language)
> Source revision: 1339030140
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Functional programming language

"Pure (language)" redirects here. For the linguistic notions, see [Linguistic purism](/source/Linguistic_purism) and [Adamic language](/source/Adamic_language).

Pure Using Pure with TeXmacs Paradigm Functional, declarative, term rewriting Designed by Albert Gräf Developer Albert Gräf First appeared 2008; 18 years ago (2008) Stable release 0.68 / 11 April 2018; 8 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 (equational language), Haskell, Lisp, Alice, MATLAB

**Pure**, successor to the equational language **Q**, is a dynamically typed, [functional](/source/Functional_programming) [programming language](/source/Programming_language) based on [term rewriting](/source/Term_rewriting). It has facilities for user-defined [operator](/source/Operator_(computer_programming)) [syntax](/source/Syntax_(programming_languages)), [macros](/source/Macro_(computer_science)), [arbitrary-precision arithmetic](/source/Arbitrary-precision_arithmetic) (multiple-precision numbers), and compiling to native code through the [LLVM](/source/LLVM). Pure is [free and open-source software](/source/Free_and_open-source_software) distributed (mostly) under the [GNU Lesser General Public License](/source/GNU_Lesser_General_Public_License) version 3 or later.

## Overview

Pure comes with an [interpreter](/source/Interpreter_(computing)) and [debugger](/source/Debugger), provides [automatic memory management](/source/Garbage_collection_(computer_science)), has powerful functional and symbolic programming abilities, and interfaces to [libraries](/source/Library_(computing)) in [C](/source/C_(programming_language)) (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](/source/Miranda_(programming_language)) and [Haskell](/source/Haskell), but it is a [free-format language](/source/Free-format_language) and thus uses explicit [delimiters](/source/Delimiter) (rather than [off-side rule](/source/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](/source/University_of_Mainz), Germany. Relative to Q, it offers some important new features (such as local functions with [lexical scoping](/source/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](/source/Just-in-time_compilation) to native code on the fly. Pure is mostly aimed at mathematical applications and [scientific computing](/source/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](/source/Artificial_intelligence), symbolic computation,[2] and real-time multimedia processing [3]

Pure [plug-ins](/source/Plug-in_(computing)) are available for the [Gnumeric](/source/Gnumeric) spreadsheet and Miller Puckette's [Pure Data](/source/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](/source/GNU_Octave), [OpenCV](/source/OpenCV), [OpenGL](/source/OpenGL), the [GNU Scientific Library](/source/GNU_Scientific_Library), [FAUST](/source/FAUST_(programming_language)), [SuperCollider](/source/SuperCollider), and liblo (for [Open Sound Control](/source/Open_Sound_Control) (OSC)).

## Examples

The [Fibonacci numbers](/source/Fibonacci_numbers) (naive version):

fib 0 = 0;
fib 1 = 1;
fib n = fib (n-2) + fib (n-1) if n>1;

Better ([tail-recursive](/source/Tail-recursive) and [linear-time](/source/Linear-time)) version:

fib n = fibs (0,1) n with
  fibs (a,b) n = if n<=0 then a else fibs (b,a+b) (n-1);
end;

Compute the first 20 Fibonacci numbers:

map fib (1..20);

An [algorithm](/source/Algorithm) for the [n queens problem](/source/Eight_queens_puzzle) which employs a [list comprehension](/source/List_comprehension) to organize the backtracking search:

queens n = search n 1 [] with
  search n i p  = [reverse p] if i>n;
                = cat [search n (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](/source/Eager_evaluation) by default, it also supports [lazy](/source/Lazy_evaluation) data structures such as streams (lazy [lists](/source/List_(computing))). For instance, [David Turner](/source/David_Turner_(computer_scientist))'s algorithm[4] for computing the stream of [prime numbers](/source/Prime_numbers) by [trial division](/source/Trial_division) can be expressed in Pure:

primes = sieve (2..inf) with
  sieve (p:qs) = p : sieve [q | q = qs; q mod p] &;
end;

Use of the & operator turns the tail of the sieve into a [thunk](/source/Thunk) to delay its computation. The thunk is evaluated implicitly and then [memoized](/source/Memoization) (using [call by need](/source/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](/source/MATLAB) and [GNU Octave](/source/GNU_Octave)), including vector and matrix comprehensions.

Namespaces, types and interfaces belong to the standard repertoire:

nonfix nil;
type bintree nil | bintree (bin x left right);

outfix « »;
namespace foo (« »);
infixr (::^) ^;
x^y = 2*x+y;
namespace;

interface stack with
  push s::stack x;
  pop s::stack;
  top s::stack;
end;

type stack [];

push xs@[] x | push xs@(_:_) x = x:xs;
pop (x:xs) = xs;
top (x:xs) = x;

As a language based on [term rewriting](/source/Term_rewriting), Pure fully supports [symbolic computation](/source/Symbolic_computation) with expressions. Here is an example showing the use of local rewriting rules to [expand](/source/Polynomial_expansion) and [factor](/source/Factorization) simple arithmetic expressions:

expand = reduce with
  (a+b)*c = a*c+b*c;
  a*(b+c) = a*b+a*c;
end;

factor = reduce with
  a*c+b*c = (a+b)*c;
  a*b+a*c = a*(b+c);
end;

expand ((a+b)*2); // yields a*2+b*2
factor (a*2+b*2); // yields (a+b)*2

Calling [C](/source/C_(programming_language)) functions from Pure is very easy. E.g., for a ["Hello, World!" program](/source/%22Hello%2C_World!%22_program), the following imports the puts function from the [C library](/source/C_library) and uses it to print the string "Hello, world!" on the terminal:

extern int puts(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

- [Free and open-source software portal](https://en.wikipedia.org/wiki/Portal:Free_and_open-source_software)

- [Functional programming](/source/Functional_programming)

- [Functional languages](https://en.wikipedia.org/wiki/Category:Functional_languages)

- [Clean (programming language)](/source/Clean_(programming_language))

## References

- Albert Gräf. "Signal Processing in the Pure Programming Language". *Linux Audio Conference 2009*.

- Michael Riepe. ["Pure – eine einfache funktionale Sprache"](http://www.heise.de/ix/artikel/Rein-ins-Vergnuegen-856225.html) [Archived](https://web.archive.org/web/20110319165239/http://www.heise.de/ix/artikel/Rein-ins-Vergnuegen-856225.html) 2011-03-19 at the [Wayback Machine](/source/Wayback_Machine). *Heise*.

- ["Interview With Albert Gräf"](https://web.archive.org/web/20130808212022/http://blueparen.com/node/6). blueparen.

- Mark Boady, [Introduction to Term Rewrite Systems and their Applications](https://www.cs.drexel.edu/~mwb33/posters/teach_pres/mwb33_teaching_presentation.pdf)

## Notes

1. **[^](#cite_ref-1)** Q-Equational-Programming-Language [https://q-lang.sourceforge.net/](https://q-lang.sourceforge.net/)

1. **[^](#cite_ref-2)** ["REDUCE Related Projects"](https://reduce-algebra.sourceforge.io/projects.php). *REDUCE Computer Algebra System*. Retrieved 2025-01-19.

1. **[^](#cite_ref-3)** FAUST [https://faust.grame.fr/](https://faust.grame.fr/).

1. **[^](#cite_ref-4)** Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975.

## External links

- [Official website](https://agraef.github.io/pure-lang)

- [Pure at Github](https://github.com/agraef/pure-lang)

- [The Pure Manual (html)](https://agraef.github.io/pure-docs/pure.html)

- [The Pure Library Manual (html)](https://agraef.github.io/pure-docs/purelib.html)

- [Documentation Overview (html)](https://agraef.github.io/pure-docs/)

- [Computer Algebra with Pure: A Reduce Interface](https://agraef.github.io/pure-docs/pure-reduce.html)

- [Pure quick reference](https://agraef.github.io/pure-lang/quickref/pure-quickref.pdf)

- [Using the Pure Interpreter](https://github.com/agraef/pure-lang/wiki/UsingPure)

- [Using Pure with TeXmacs](https://github.com/agraef/pure-lang/wiki/TeXmacs)

- [The Pure TeXmacs Plugin](https://github.com/agraef/pure-lang/wiki/pure-texmacs.en.pdf)

v t e Programming languages Comparison Timeline History Ada ALGOL Simula APL Assembly BASIC Visual Basic classic .NET C C++ C# COBOL Erlang Elixir Forth Fortran Go Haskell Java JavaScript Julia Kotlin Lisp Lua MATLAB ML Caml OCaml Standard ML Pascal Object Pascal Perl Raku PHP Prolog Python R Ruby Rust SAS SQL Scratch Shell Smalltalk Swift more... Lists: Alphabetical Categorical Generational Non-English-based Category

---
Adapted from the Wikipedia article [Pure (programming language)](https://en.wikipedia.org/wiki/Pure_(programming_language)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Pure_(programming_language)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
