Mercury
Logic and functional programming language
Mercury is a statically typed logic and functional programming language designed for building large, reliable software systems, developed with a strong emphasis on compile-time verification of program properties such as type correctness,…
Definition
Mercury is a statically typed logic and functional programming language designed for building large, reliable software systems, developed with a strong emphasis on compile-time verification of program properties such as type correctness, data flow modes, and determinism. It extends the declarative style of logic programming languages like Prolog with strict static typing and mode analysis that catch many classes of errors before a program ever runs, targeting the reliability gap that untyped logic languages leave open.
Overview
Mercury was developed at the University of Melbourne with the explicit goal of bringing the declarative expressiveness of logic programming, as seen in Prolog, into a setting suitable for building large, dependable software systems, something Prolog's dynamically typed and loosely verified nature made difficult to guarantee at scale. Its designers observed that Prolog programs, while elegant for expressing relations and search, offered few static guarantees about correctness, so many errors surfaced only at runtime. Mercury's core design response was to layer strict static typing, mode declarations, and determinism analysis on top of a logic programming foundation. Mechanically, every Mercury predicate carries declared type information much like a strongly typed functional language, along with mode declarations specifying how each argument's instantiation state changes during a call, whether an argument is fully known going in, fully unknown coming out, or some other pattern. The compiler also requires determinism declarations, stating whether a predicate produces exactly one solution, zero or one, multiple solutions, or fails, and it verifies these declarations against the actual code, catching mismatches at compile time rather than allowing them to surface as unexpected behavior at runtime. This combination lets the Mercury compiler generate substantially more optimized code than a typical Prolog implementation, since much of the runtime search machinery that dynamically typed logic languages need can be eliminated when modes and determinism are known statically. Within functional-logic languages, Mercury is often paired conceptually with Curry, which also merges logic and functional programming, but the two differ sharply in philosophy: Curry leans on lazy evaluation and non-determinism as its core mechanism, while Mercury insists on strict evaluation and exhaustive static verification of modes and determinism before code is considered correct. Compared to Prolog, Mercury trades some of Prolog's dynamic flexibility for compile-time guarantees and significantly better runtime performance in well-typed, well-moded programs. In practice, Mercury has been used in projects requiring both the expressiveness of logic programming and stronger reliability guarantees than Prolog provides, including some compiler and program-analysis tooling, and it has served as a research vehicle for studying how static verification techniques can be layered onto declarative logic programming without sacrificing too much of its expressive power. Its own compiler is notably self-hosted, written in Mercury itself, which has served as a substantial real-world test of the language's suitability for large software. The language's principal trade-off is the learning and authoring overhead its strict verification requirements impose: writing correct mode and determinism declarations demands a deeper understanding of a predicate's behavior than dynamically typed logic programming requires, and this additional rigor, combined with a niche academic community, has kept Mercury's adoption limited outside of research and a small number of specialized applications valuing its reliability guarantees.
Key Features
- Strict static typing layered onto a logic programming foundation
- Mode declarations specifying argument instantiation states
- Compile-time determinism analysis verified against actual code
- Substantially optimized code generation compared to Prolog
- Self-hosted compiler written in Mercury itself
- Combines logic programming expressiveness with reliability guarantees
- Strict evaluation contrasted with lazy functional-logic alternatives
- Rooted in academic research from the University of Melbourne