Mercury is a
functional logic programming
Functional logic programming is the combination, in a single programming language, of the paradigms of functional programming and logic programming.Antoy, Sergio, and Michael Hanus.Functional logic programming" Commun. ACM 53.4 (2010): 74–85. Thi ...
language made for real-world uses. The first version was developed at the
University of Melbourne
The University of Melbourne (colloquially known as Melbourne University) is a public university, public research university located in Melbourne, Australia. Founded in 1853, it is Australia's second oldest university and the oldest in the state ...
, 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
Logic programming is a programming, database and knowledge representation paradigm based on formal logic. A logic program is a set of sentences in logical form, representing knowledge about some problem domain. Computation is performed by applyin ...
language. It is related to both
Prolog
Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics.
Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
and
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
.
[The Mercury Project - Motivation](_blank)
/ref> It features a strong, static, polymorphic type system
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
, and a strong mode and determinism system.
The official implementation, the Melbourne Mercury Compiler, is available for most Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
and Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
platforms, including Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
, macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
, and for Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
.
Overview
Mercury is based on the logic programming language Prolog
Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics.
Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
. 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
Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
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
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
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.][The Mercury Project - Benchmarks](_blank)
/ref> 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
Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics.
Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
, since it lacks ''extra-logical'' Prolog statements such as !
(cut) and imperative input/output
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
(I/O). This enables advanced static program analysis
In computer science, static program analysis (also known as static analysis or static simulation) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs duri ...
and program optimization
In computer science, program optimization, code optimization, or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be op ...
, including compile-time garbage collection
Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclable ...
, 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
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
) 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.ODASE
/ref>
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
The GNU Compiler Collection (GCC) is a collection of compilers from the GNU Project that support various programming languages, Computer architecture, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes ...
(GCC), the original Mercury back-end
* High-level C
* Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
* C#
Past
* Assembly language
In computing, assembly language (alternatively assembler language or symbolic machine code), often referred to simply as assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence bet ...
via the GCC back-end
* Aditi, a deductive database system also developed at the University of Melbourne
The University of Melbourne (colloquially known as Melbourne University) is a public university, public research university located in Melbourne, Australia. Founded in 1853, it is Australia's second oldest university and the oldest in the state ...
. Mercury-0.12.2 is the last version to support Aditi.
* Common Intermediate Language
Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. ...
(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:
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
Hello World may refer to:
* "Hello, World!" program, a computer program that outputs or displays the message "Hello, World!"
Music
* "Hello World!" (composition), song by the Iamus computer
* "Hello World" (Tremeloes song), 1969
* "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 mathematics, the Fibonacci sequence is a Integer sequence, sequence in which each element is the sum of the two elements that precede it. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted . Many w ...
(in the most obvious way):[Adapted fro]
Ralph Becket's Mercury tutorial
/ref>
:- 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", (fib(10)) !IO).
!IO
is a "state variable", which is syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
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
Emacs (), originally named EMACS (an acronym for "Editor Macros"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
* A plugin is available for the Eclipse IDE
Eclipse is an integrated development environment (IDE) used in computer programming. It contains a base workspace and an extensible plug-in system for customizing the environment. It had been the most popular IDE for Java development until 20 ...
* A plugin is available for the NetBeans IDE
See also
* Curry
Curry is a dish with a sauce or gravy seasoned with spices, mainly derived from the interchange of Indian cuisine with European taste in food, starting with the Portuguese, followed by the Dutch and British, and then thoroughly internatio ...
, another functional logic language
* Alice
Alice may refer to:
* Alice (name), most often a feminine given name, but also used as a surname
Literature
* Alice (''Alice's Adventures in Wonderland''), a character in books by Lewis Carroll
* ''Alice'' series, children's and teen books by ...
, a dialect language of Standard ML
Standard ML (SML) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Modular programming, modular, Functional programming, functional programming language with compile-time type checking and t ...
* Logtalk
Logtalk is an object-oriented logic programming language that extends and leverages the Prolog language with a feature set suitable for programming in the large.Paulo Moura (2003). Logtalk: Design of an Object-Oriented Logic Programming Language. ...
, language, an object-oriented extension of Prolog which compiles down to Prolog
* Oz/Mozart, a multiparadigm language
* Visual Prolog
Visual Prolog, previously known as PDC Prolog and Turbo Prolog, is a strongly typed object-oriented extension of Prolog. It was marketed by Borland as Turbo Prolog (version 1.0 in 1986 and version 2.0 in 1988). It is now developed and marketed by ...
, language, a strongly typed object-oriented extension of Prolog, with a new syntax
References
External links
*
{{DEFAULTSORT:Mercury (programming language)
Cross-platform free software
Functional logic programming languages
.NET programming languages
Programming languages created in 1995
Statically typed programming languages