GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main page
  • Contents
  • Current events
  • Random article
  • About Wikipedia
  • Contact us
  • Help
  • Learn to edit
  • Community portal
  • Recent changes
  • Upload file
  • Special pages
  • Search
  • Donate

This view

  • Original page
  • New address

GeezerVison is showing en.wikipedia.org — Mercury (programming language) - Wikipedia

(Redirected from Mercury programming language)
For Mercury Autocode, see Autocode.
Not to be confused with Mercurial or Mercury (RemObjects BASIC programming language).
Mercury
ParadigmLogic, functional, object-oriented
FamilyProlog, Haskell
Designed byZoltan Somogyi
DeveloperUniversity of Melbourne
First appearedApril 8, 1995 (1995-04-08)
Stable release
22.01.9[1] Edit this on Wikidata / 30 August 2026 (30 August 2026)
Typing disciplineStrong, static, polymorphic
ScopeLexical
Implementation languageMercury
PlatformIA-32, x86-64, ARM, SPARC64, Java, CLI
OSCross-platform: Unix, Linux, macOS, Solaris, FreeBSD, OpenBSD, Windows, Android
LicenseGPL compiler,
LGPL standard library
Filename extensions.m
Websitewww.mercurylang.org
Major implementations
Melbourne Mercury Compiler
Influenced by
Prolog, Hope, Haskell

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.

Mercury is a purely declarative logic programming language. It is related to both Prolog and Haskell.[2] It features a strong, static, polymorphic type system, and a strong mode and determinism system.

The official implementation, the Melbourne Mercury Compiler, is available for most Unix and Unix-like platforms, including Linux, macOS, and for Windows.

Overview

Mercury is based on the logic programming language Prolog. It has the same syntax and the same basic concepts such as the selective linear definite clause resolution (SLD) 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 in features and run-time efficiency.

The language is designed using software engineering principles. Unlike the original implementations of Prolog, it has a separate compilation phase, rather than being directly interpreted. This allows a much wider range of errors to be detected before running a program. It features a strict static type and mode system[2] and a module system.

By using information obtained at compile time (such as type and mode), programs written in Mercury typically perform significantly faster than equivalent programs written in Prolog.[3][4] Its authors claim that Mercury is the fastest logic language in the world, by a wide margin.[2]

Mercury is a purely declarative language, unlike Prolog, since it lacks extra-logical Prolog statements such as ! (cut) and imperative input/output (I/O). This enables advanced static program analysis and program optimization, including compile-time garbage collection,[5] but it can make certain programming constructs (such as a switch over a number of options, with a default) harder to express. While Mercury does allow impure functionality, it serves mainly as a way to call 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 and the Prince XML formatter. The software company ODASE has also been using Mercury to develop its Ontology-Centric software development platform, ODASE.[6]

Back-ends

Mercury has several back-ends, which enable compiling Mercury code into several languages, including:

Production level

  • Low-level C for GNU Compiler Collection (GCC), the original Mercury back-end
  • High-level C
  • Java
  • C#

Past

  • Assembly language via the GCC back-end
  • Aditi, a deductive database system also developed at the University of Melbourne. Mercury-0.12.2 is the last version to support Aditi.
  • Common Intermediate Language (CIL) for the .NET Framework
  • Erlang

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-endForeign language(s)
C (both levels) C
Java Java
Erlang Erlang
IL Common Intermediate Language (CIL) 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

Hello World:

 :- 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):[7]

 :- 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).

!IO is a "state variable", which is syntactic sugar for a pair of variables which are assigned concrete names at compilation; for example, the above is desugared to something like:

 main(IO0, IO) :-
        io.write_string("fib(10) = ", IO0, IO1),
        io.write_int(fib(10), IO1, IO2),
        io.nl(IO2, IO).

Release schedule

The stable release naming scheme was 0.1 up to 0.13 for the first thirteen stable releases. In February 2010 the Mercury project decided to name each stable release by using the year and month of the release. For example 10.04 is for a release made in April 2010.

There is often also a periodic snapshot of the development system release of the day (ROTD)

IDE and editor support

  • Developers provide support for Vim
  • Flycheck library for Emacs
  • A plugin is available for the Eclipse IDE
  • A plugin is available for the NetBeans IDE

See also

  • Curry, another functional logic language
  • Alice, a dialect language of Standard ML
  • Logtalk, language, an object-oriented extension of Prolog which compiles down to Prolog
  • Oz/Mozart, a multiparadigm language
  • Visual Prolog, language, a strongly typed object-oriented extension of Prolog, with a new syntax
  • λProlog, language, a higher order logic programming language also featuring static types and modes

References

  1. ↑ "Release 22.01.9". 30 August 2026. Retrieved 30 August 2026.
  2. 1 2 3 The Mercury Project - Motivation
  3. ↑ The Mercury Project - Benchmarks
  4. ↑ Somogyi, Zoltan; Henderson, Fergus; Conway, Thomas (October–December 1996). "The execution algorithm of Mercury: an efficient purely declarative logic programming language". Journal of Logic Programming. 29 (1–3). Mercurylang.org: 17–64. doi:10.1016/S0743-1066(96)00068-4. Retrieved 2008-08-30.
  5. ↑ Mazur, Nancy (May 2004). Compile-time garbage collection for the declarative language Mercury (PDF) (Thesis). Katholieke Universiteit Leuven.
  6. ↑ ODASE
  7. ↑ Adapted from Ralph Becket's Mercury tutorial

External links

  • Official website
Retrieved from "https://en.wikipedia.org/w/index.php?title=Mercury_(programming_language)&oldid=1381341206"
Categories:
  • Cross-platform free software
  • Functional logic programming languages
  • .NET programming languages
  • Programming languages created in 1995
  • Statically typed programming languages
Hidden categories:
  • Articles with short description
  • Short description matches Wikidata
  • All articles with unsourced statements
  • Articles with unsourced statements from February 2024
  • All accuracy disputes
  • Articles with disputed statements from February 2009
  • Articles with unsourced statements from January 2014
  • Official website different in Wikidata and Wikipedia
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.