Kalpana Kalpana (Editor)

Mercury (programming language)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Designed by
  
Zoltan Somogyi

Paradigm
  
Logic, functional, object-oriented

Developer
  
University of Melbourne

First appeared
  
April 8, 1995; 21 years ago (1995-04-08)

Stable release
  
14.01.1 / September 8, 2014; 2 years ago (2014-09-08)

Typing discipline
  
Strong, static, polymorphic

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.

Contents

Mercury is a purely declarative logic programming language. It is related to both Prolog and Haskell. 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 (32bits only).

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 in features and run-time efficiency.

The language is designed using software engineering principles. Unlike the original implementations of Prolog, it has a separate compiling 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 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. Its authors claim that Mercury is the fastest logic language in the world, by a wide margin.

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, but 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, this serves mainly as a way to call foreign language code. All impure code must be marked explicitly.) 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. Mission Critical IT, 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 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#
  • Erlang
  • 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
  • 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:

    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:

    Calculating the 10th Fibonacci number (in the most obvious way):

    Release schedule

    Releases are named according to the year and month of release. The current stable release is 14.01.1 (September 2014). Prior releases were numbered 0.12, 0.13, etc., and the time between stable releases can be as long as 3 years.

    There is often also a snapshot release of the day (ROTD) consisting of the latest features and bug fixes added to the last stable release.

    References

    Mercury (programming language) Wikipedia