Est. 1995 Advanced

Mercury

A purely declarative logic/functional programming language built for real-world software, pairing Prolog-style logic with a strong static type, mode, and determinism system.

Created by Zoltan Somogyi, Fergus Henderson, and Thomas Conway (University of Melbourne)

Paradigm Declarative: logic and functional
Typing Static, strong, polymorphic; with mode and determinism systems
First Appeared 1995
Latest Version 22.01.8 (September 2023), with ongoing release-of-the-day development builds

Mercury is a purely declarative programming language that combines the logic-programming heritage of Prolog with the discipline of a strong, static, functional-style type system reminiscent of Haskell. Created at the University of Melbourne and first released in 1995, Mercury was designed to answer a pointed question: could the elegance of declarative programming be made fast, predictable, and robust enough for building large, real-world software? Its distinctive type, mode, and determinism systems are the language’s answer — a set of compile-time guarantees that catch a wide class of errors before a program ever runs.

History & Origins

Mercury grew out of frustration with the limits of conventional logic programming. Design work began around 1993 in the Department of Computer Science at the University of Melbourne, led by Zoltan Somogyi together with Fergus Henderson and Thomas Conway. The team admired Prolog’s declarative core but was dissatisfied with its extra-logical features — the cut, imperative side effects, and the absence of static checking — which made large Prolog programs hard to reason about and to optimize.

The first version of Mercury was released on 8 April 1995, with version 0.3, the first public beta, following on 18 July 1995. That early release already established the language’s signature combination of strong typing with parametric polymorphism, a mode system describing the flow of information through predicates, a determinism system classifying how many solutions a goal can produce, and a module system that separates interfaces from implementations. The first release widely regarded as stable, version 0.9, arrived in December 1999, adding existential types, refinements to type classes, and a substantially expanded standard library.

Design Philosophy

Mercury’s central idea is that a declarative program should say what is true, not how to compute it — but that a practical language must still give the compiler enough information to generate efficient code and to reject buggy programs. To achieve this, Mercury layers three interlocking static analyses on top of ordinary logic programming:

  • Types describe the kind of data a predicate or function works with, checked statically and supporting parametric polymorphism and type classes.
  • Modes describe the direction of data flow: which arguments are inputs and which are outputs in a given use of a predicate. A single predicate can have several modes.
  • Determinism describes how many solutions a goal can produce — for example, exactly one (det), at most one (semidet), zero or more (nondet), or one or more (multi).

Because these properties are checked at compile time, the Mercury compiler can reject programs that, in Prolog, would simply fail or loop at runtime. Equally important, the same information lets the compiler generate efficient native code; the language’s authors report that Mercury programs typically run substantially faster than equivalent programs in traditional interpreted Prolog, though such comparisons depend heavily on the particular programs and systems being measured. Mercury also takes purity seriously: it has no cut and no uncontrolled side effects. Input and output are handled declaratively by threading a unique “state of the world” value through the program, so that even I/O fits within the pure logical model.

Key Features

A unified logic/functional language

Mercury is both a logic language and a functional language. Predicates and functions coexist, and the language draws explicitly on Prolog for its relational style and on Haskell for type classes, higher-order programming, and purity. This makes it possible to write code in whichever style best fits the problem.

Strong static guarantees

The type, mode, and determinism systems together form Mercury’s defining feature. They turn many runtime failures into compile-time errors and document a predicate’s behavior precisely in its declaration. A programmer reading a Mercury predicate’s signature learns not only its argument types but also which arguments are inputs and outputs and how many answers to expect.

Declarative input/output

Rather than treating I/O as an imperative side effect, Mercury models it by passing a unique value representing the state of the external world. The mode and uniqueness systems ensure this state is threaded correctly, preserving the language’s declarative semantics even for programs that interact with files, networks, or the terminal.

Multiple compilation backends

Mercury is a compiled language with several backends. Over its history these have included a low-level C backend (historically using GCC), a high-level C backend, and managed backends targeting Java and C#. Earlier or experimental targets included native assembly, the Aditi deductive-database system, the .NET Common Intermediate Language, and an Erlang backend. The C, Java, and C# targets became the production-grade options.

Module system and standard library

Mercury programs are organized into modules with explicit interface and implementation sections, and the language ships with a substantial standard library covering data structures, I/O, exceptions, and more. This emphasis on modularity reflects the project’s goal of supporting programming in the large.

Evolution

For its first fifteen years Mercury used sequential 0.x version numbers, progressing from the 1995 betas through the stable 0.9 release and beyond. In February 2010 the project switched to a year-month numbering scheme, so that, for example, release 10.04 corresponds to April 2010. The most recent stable series is 22.01, first released on 31 March 2022; subsequent point releases continued through 22.01.8 in September 2023. Between formal releases, the project publishes frequent release-of-the-day development snapshots, and maintenance has continued into the mid-2020s, including improvements to compiler diagnostics such as colorized output.

Current Relevance

Mercury occupies a particular niche: it is a mature, decades-old research-grade language that has nonetheless proven itself in commercial production. Its most visible flagship is Prince (from YesLogic), a respected HTML/CSS-to-PDF formatter written entirely in Mercury — a standing demonstration that a purely declarative language can underpin a substantial commercial product. The language remains actively maintained by a small, dedicated community centered on the original Melbourne project, now developed openly on GitHub, and it continues to attract interest from programmers exploring the frontier where logic and functional programming meet.

Why It Matters

Mercury matters as a sustained, serious attempt to make declarative programming engineering-ready. Where Prolog showed the expressive power of logic programming, Mercury asked what it would take to ship and maintain large declarative systems — and answered with static types, modes, determinism analysis, and a pure I/O model. Many of these ideas, and the compiler techniques developed to support them, have informed wider research into logic and functional language implementation. For anyone interested in how much a compiler can verify about a program before it runs, Mercury remains one of the most thorough and instructive experiments in the field.

Timeline

1993
Design work on Mercury begins at the University of Melbourne's Department of Computer Science, led by Zoltan Somogyi with Fergus Henderson and Thomas Conway
1995
First version of Mercury released on 8 April 1995; version 0.3, the first public beta, follows on 18 July 1995
1999
Version 0.9, regarded as the first stable release, ships in December, adding features such as existential types, type-class refinements, and an expanded standard library
2003
YesLogic releases Prince 2.0, its HTML/CSS-to-PDF formatter written in Mercury, demonstrating the language in commercial production software
2010
The project adopts a year-month version-numbering scheme (for example, 10.04 for the April 2010 release), replacing the earlier 0.x sequence
2022
Mercury 22.01 released on 31 March, beginning the 22.01.x maintenance series
2023
Mercury 22.01.8, the latest stable point release of the 22.01 series, is published in September; development continues through regular release-of-the-day snapshots

Notable Uses & Legacy

Prince (YesLogic)

Prince, a widely used commercial converter from HTML and CSS to PDF, is written in Mercury. It is one of the most prominent real-world applications of the language and is frequently cited as evidence of Mercury's suitability for large, reliable production software.

The Mercury compiler

The Mercury compiler is itself written in Mercury, making the system self-hosting and serving as the largest continuously maintained Mercury codebase.

ODASE ontology-centric platform

The ODASE (Ontology-Driven Architecture for Software Engineering) platform has used Mercury to build business-rules and reasoning systems on top of formal ontologies.

Programming-languages research

Mercury has been a vehicle for academic research into static analysis, declarative debugging, deterministic execution, and compiler optimization, with ideas from the project influencing later work on logic and functional language implementation.

Language Influence

Influenced By

Running Today

Run examples using the official Docker image:

docker pull
Last updated: