Mercury (programming language)
<templatestyles src="Module:Hatnote/styles.css"></templatestyles>
<templatestyles src="Module:Hatnote/styles.css"></templatestyles>
File:Mercury logo.jpg | |
Paradigm | Logic, functional, object-oriented |
---|---|
Designed by | Zoltan Somogyi |
Developer | University of Melbourne |
First appeared | 1995 |
Stable release | 14.01.1 / September 8, 2014 |
Typing discipline | Strong, static, polymorphic |
OS | Cross-platform (Unix, Mac OS X, Windows) |
License | GPL for compiler, LGPL for standard library |
Filename extensions | .m |
Website | www |
Major implementations | |
Melbourne Mercury Compiler | |
Influenced by | |
Prolog, Hope, Haskell |
Mercury is a functional logic programming language geared towards real-world applications. It was initially developed at the University of Melbourne Computer Science department under the supervision of Zoltan Somogyi. The first version was developed by Fergus Henderson, Thomas Conway and Zoltan Somogyi and was released on April 8, 1995.
Mercury is a purely declarative logic language. It is related to both Prolog and Haskell.[1] It features a strong, static, polymorphic type system, as well as a strong mode and determinism system.
The official implementation, the Melbourne Mercury Compiler, is available for most Unix platforms, including Mac OS X, as well as for Microsoft Windows.
Contents
Overview
Mercury is based on the logic programming language Prolog. It has the same syntax, and the same basic concepts such as the SLD resolution algorithm. It can be viewed as a pure subset of Prolog with strong types and modes. As such, it is often compared to its predecessor, both in terms of features, and run-time efficiency.
The language is designed with software engineering principles in mind. Unlike the original implementations of Prolog, it has a separate compilation phase, rather than being directly interpreted, which allows a much wider range of errors to be caught before running a program. It features a strict static type and mode system[1] and a module system.
Due to the use of information obtained at compile time (such as type and mode information), programs written in Mercury typically perform significantly faster than equivalent programs written in Prolog.[2][3] Its authors claim that Mercury is the fastest logic language in the world, by a wide margin.[1]
Mercury is a purely declarative language, unlike Prolog, since it lacks "extra-logical" Prolog statements such as "cut" and imperative I/O. This enables advanced static analysis and program optimization, including compile-time garbage collection,[4] but can make certain programming constructs (such as a switch over a number of options, with a default[dubious ]) harder to express. (Note that while Mercury does allow impure functionality, this serves primarily as a way of calling foreign language code. All impure code must be explicitly marked.) Operations which would typically be impure (such as input/output) are expressed using pure constructs in Mercury using linear types, by threading a dummy "world" value through all relevant code.
Notable programs written in Mercury include the Mercury compiler itself and the Prince XML formatter. Mission Critical IT [1], a software company, has also been using Mercury since 2000 to develop enterprise applications and its Ontology-Driven software development platform ODASE.
Back-ends
Mercury has several back-ends, which means it is possible to compile Mercury code into the following languages:
Production level:
Beta quality:
Alpha quality (may not work well, or even be completely broken):
- IL for Microsoft's .NET
- Assembly via the GCC back-end
Past back-ends:
- Aditi, a deductive database system also developed at the University of Melbourne. Mercury-0.12.2 is the last version of Mercury that supports Aditi.[citation needed]
Mercury also features a foreign language interface, allowing code in other languages (depending on the chosen back-end) to be linked with Mercury code. The following foreign languages are possible:
Back-end | Foreign language(s) |
---|---|
C (both levels) | C |
Java | Java |
Erlang | Erlang |
IL | IL or C# |
Other languages can then be interfaced to by calling them from these languages. However, this means that foreign language code may need to be written several times for the different backends, otherwise portability between backends will be lost.
The most commonly used back-end is the original low-level C back-end.
Examples
:- module hello.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
io.write_string("Hello, World!\n", !IO).
Calculating the 10th Fibonacci number (in the most obvious way):[5]
:- module fib.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- func fib(int) = int.
fib(N) = (if N =< 2 then 1 else fib(N - 1) + fib(N - 2)).
main(!IO) :-
io.write_string("fib(10) = ", !IO),
io.write_int(fib(10), !IO),
io.nl(!IO).
% Could instead use io.format("fib(10) = %d\n", [i(fib(10))], !IO).
Release schedule
The Mercury project has a 6 monthly release cycle. Releases are named according to the year and month of the release. The current stable release is 14.01.1 (September 2014). Previously releases were numbered 0.12, 0.13, etc. and the period between stable releases was very large (3 years).
There is also a snapshot release consisting of the latest features and bug fixes added to the last stable release.
See also
- Alice programming language, a dialect of Standard ML
- Logtalk programming language, an object-oriented extension of Prolog which compiles down to Prolog
- Oz/Mozart, a multiparadigm programming language
- Visual Prolog programming language, a strongly typed object-oriented extension of Prolog (with a new syntax)
References
- ↑ 1.0 1.1 1.2 The Mercury Project - Motivation
- ↑ The Mercury Project - Benchmarks
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Adapted from Ralph Becket's Mercury tutorial
External links
- Pages with syntax highlighting errors
- Pages with broken file links
- All accuracy disputes
- Articles with disputed statements from February 2009
- Articles with unsourced statements from January 2014
- Official website not in Wikidata
- Cross-platform free software
- Functional languages
- Functional logic programming languages
- Logic programming languages
- .NET programming languages
- 1995 in computer science