Pure (programming language): Difference between revisions

Content deleted Content added
m <syntaxhighlight lang="q"> for pygments 2.12 upgrade
WikiCleanerBot (talk | contribs)
m v2.05b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation)
 
(24 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Functional programming language}}
{{multiple issues|
{{redirect|Pure (language)|the linguistic notions|Linguistic purism|and|Adamic language}}
{{more footnotes|date=September 2016}}
{{notability|date=September 2016}}
{{primary sources|date=September 2016}}
}}
{{Infobox programming language
|name = Pure
|logo = Pure lang logo.png
|screenshot = Pure with texmacs.png
|screenshot caption = Using Pure with [[TeXmacs]]
|paradigm = [[Functional programming|Functional]], [[Declarative programming|declarative]], [[term rewriting]]
|designer = Albert Gräf
Line 13 ⟶ 12:
|latest release version = 0.68
|latest release date = {{Start date and age|2018|04|11|df=yes}}
|typing = [[Strong and weak typing|Strongstrong]], [[Type system#DYNAMIC|dynamic]]
|implementations =
|dialects =
|operating system = [[Cross-platform software|Cross-platform]]: [[FreeBSD]], [[Linux]], [[macOS]], [[Microsoft Windows|Windows]]
|license = [[GNU Lesser General Public License]]
|influenced by = Q, [[HaskellQ (programming language from Kx Systems)|Q]], [[Haskell]], [[Lisp (programming language)|Lisp]], [[Alice (programming language)|Alice]], [[MATLAB]]
|influenced =
|website = {{URL|https://agraef.github.io/pure-lang/}}
}}
 
'''Pure''', successor to the equational language '''Q''', is a dynamically typed, [[Functional programming|functional]] [[programming language]] based on [[term rewriting]]. It has facilities for user-defined [[operatorOperator (computer programming)|operator]] [[Syntax (programming languages)|syntax]], [[Macro (computer science)|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.
 
== Overview ==
Pure comes with an [[Interpreter (computing)|interpreter]] and [[debugger]], provides [[Garbage collection (computer science)|automatic memory management]], has powerful functional and symbolic programming abilities, and interfaces to [[Library (computing)|libraries]] in [[C (programming language)|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 (programming language)|Miranda]] and [[Haskell (programming language)|Haskell]], but it is a [[free-format language]] and thus uses explicit delimiters[[delimiter]]s (rather than [[off-side rule]] indents) to denote program structure.
 
The Pure language is a successor of the equational programming language Q,<ref> Q-Equational-Programming-Language https://q-lang.sourceforge.net/</ref> 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 [[Just-in-time compilation|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 applicationsuses, such as [[artificial intelligence]], symbolic computation,<ref>{{Cite web |title=REDUCE Related Projects |url=https://reduce-algebra.sourceforge.io/projects.php |access-date=2025-01-19 |website=REDUCE Computer Algebra System}}</ref> and real-time multimedia processing <ref>FAUST https://faust.grame.fr/.</ref>
 
Pure [[Plug-in (computing)|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 (programming language)|FAUST]], [[SuperCollider]], and liblo (for [[Open Sound Control]] (OSC)).
Line 80:
</syntaxhighlight>
 
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:
 
Namespaces, types and interfaces belong to the standard repertoire:
 
<syntaxhighlight lang="q">
nonfix nil;
gauss_elimination x::matrix = p,x
type bintree nil | bintree (bin x left right);
when n,m = dim x; p,_,x = foldl step (0..n-1,0,x) (0..m-1) end;
 
outfix « »;
step (p,i,x) j
namespace foo (« »);
= if max_x==0 then p,i,x else
infixr (::^) ^;
// updated row permutation and index:
x^y = 2*x+y;
transp i max_i p, i+1,
namespace;
{// the top rows of the matrix remain unchanged:
 
x!!(0..i-1,0..m-1);
interface stack with
// the pivot row, divided by the pivot element:
push s::stack x;
{x!(i,l)/x!(i,j) | l=0..m-1};
pop s::stack;
// subtract suitable multiples of the pivot row:
top s::stack;
{{x!(k,l)-x!(k,j)*x!(i,l)/x!(i,j) | k=i+1..n-1; l=0..m-1}}
when
n,m = dim x; max_i, max_x = pivot i (col x j);
x = if max_x>0 then swap x i max_i else x;
end with
pivot i x = foldl max (0,0) [j,abs (x!j)|j=i..#x-1];
max (i,x) (j,y) = if x<y then j,y else i,x;
end;
 
type stack [];
/* Swap rows i and j of the matrix x. */
 
push xs@[] x | push xs@(_:_) x = x:xs;
swap x i j = x!!(transp i j (0..n-1),0..m-1) when n,m = dim x end;
pop (x:xs) = xs;
top (x:xs) = x;
</syntaxhighlight>
 
/* Apply a transposition to a permutation. */
 
transp i j p = [p!tr k | k=0..#p-1]
with tr k = if k==i then j else if k==j then i else k end;
 
/* Example: */
 
let x = dmatrix {2,1,-1,8; -3,-1,2,-11; -2,1,2,-3};
x; gauss_elimination x;
</syntaxhighlight>
 
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 [[polynomial expansion|expand]] and [[factorization|factor]] simple arithmetic expressions:
Line 136 ⟶ 125:
</syntaxhighlight>
 
Calling [[C (programming language)|C]] functions from Pure is very easy. E.g., for a [["Hello, World!" program]], the following imports the <code>puts</code> function from the [[C library]] and uses it to print the string <code>"Hello, world!"</code> on the terminal:
 
<syntaxhighlight lang="c">
Line 143 ⟶ 132:
hello;
</syntaxhighlight>
 
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==
Line 152 ⟶ 143:
== References ==
* Albert Gräf. "Signal Processing in the Pure Programming Language". ''Linux Audio Conference 2009''.
* Michael Riepe. [http://www.heise.de/ix/artikel/Rein-ins-Vergnuegen-856225.html "Pure – eine einfache funktionale Sprache"] {{Webarchive|url=https://web.archive.org/web/20110319165239/http://www.heise.de/ix/artikel/Rein-ins-Vergnuegen-856225.html |date=2011-03-19 }}. ''Heise''.
* [https://web.archive.org/web/20130808212022/http://blueparen.com/node/6 "Interview With Albert Gräf"]. blueparen.
* Mark Boady, [https://www.cs.drexel.edu/~mwb33/posters/teach_pres/mwb33_teaching_presentation.pdf Introduction to Term Rewrite Systems and their Applications]
 
== Notes ==
 
{{Reflist}}
 
==External links==
* {{Official website|https://agraef.github.io/pure-lang/}}
* [https://github.com/agraef/pure-lang Pure at Github]
* [http://puredocs.bitbucket.org/ Pure language and library documentation]
* [httphttps://purelangagraef.bitbucketgithub.org/quickrefio/pure-quickrefdocs/pure.pdfhtml The Pure quickManual reference(html)]
* [httphttps://codeagraef.googlegithub.com/pio/pure-langdocs/wiki/PurePrimer0purelib.html The Pure PrimerLibrary Manual (html)]
* [https://agraef.github.io/pure-docs/ Documentation Overview (html)]
* [https://agraef.github.io/pure-docs/pure-reduce.html Computer Algebra with Pure: A Reduce Interface]
* [https://agraef.github.io/pure-lang/quickref/pure-quickref.pdf Pure quick reference]
* [https://github.com/agraef/pure-lang/wiki/UsingPure Using the Pure Interpreter]
* [https://github.com/agraef/pure-lang/wiki/TeXmacs Using Pure with TeXmacs]
* [https://github.com/agraef/pure-lang/wiki/pure-texmacs.en.pdf The Pure TeXmacs Plugin]
 
{{Programming languages}}