Mercury (programming language)

Last updated
Mercury
Mercury (programming language) logo.jpg
Paradigm Logic, functional, object-oriented [ citation needed ]
Designed by Zoltan Somogyi
Developer University of Melbourne
First appearedApril 8, 1995;28 years ago (1995-04-08)
Stable release
22.01.8 [1]   OOjs UI icon edit-ltr-progressive.svg / 8 September 2023;5 months ago (8 September 2023)
Typing discipline Strong, static, polymorphic
Implementation languageMercury
Platform IA-32, x86-64, Arm, Sparc64, Java, CLI
OS Cross-platform: Unix, Linux, macOS, Solaris, FreeBSD, OpenBSD, Windows, Android
License GPL compiler,
LGPL standard library
Filename extensions .m
Website www.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.

Contents

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

Past

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:

:-modulehello.:-interface.:-import_moduleio.:-predmain(io::di,io::uo)isdet.:-implementation.main(!IO):-io.write_string("Hello, World!\n",!IO).

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

:-modulefib.:-interface.:-import_moduleio.:-predmain(io::di,io::uo)isdet.:-implementation.:-import_moduleint.:-funcfib(int)=int.fib(N)=(ifN=<2then1elsefib(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

See also

Related Research Articles

<span class="mw-page-title-main">Erlang (programming language)</span> Programming language

Erlang is a general-purpose, concurrent, functional high-level programming language, and a garbage-collected runtime system. The term Erlang is used interchangeably with Erlang/OTP, or Open Telecom Platform (OTP), which consists of the Erlang runtime system, several ready-to-use components (OTP) mainly written in Erlang, and a set of design principles for Erlang programs.

Prolog is a logic programming language that has its origins in artificial intelligence and computational linguistics.

Procedural programming is a programming paradigm, classified as imperative programming, that involves implementing the behavior of a computer program as procedures that call each other. The resulting program is a series of steps that forms a hierarchy of calls to its constituent procedures.

OCaml is a general-purpose, high-level, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.

In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every term. Usually the terms are various language constructs of a computer program, such as variables, expressions, functions, or modules. A type system dictates the operations that can be performed on a term. For variables, the type system determines the allowed values of that term. Type systems formalize and enforce the otherwise implicit categories the programmer uses for algebraic data types, data structures, or other components.

In computer science, declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

<span class="mw-page-title-main">F Sharp (programming language)</span> Microsoft programming language

F# is a general-purpose, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods. It is most often used as a cross-platform Common Language Infrastructure (CLI) language on .NET, but can also generate JavaScript and graphics processing unit (GPU) code.

<span class="mw-page-title-main">Modula-3</span>

Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2 known as Modula-2+. While it has been influential in research circles it has not been adopted widely in industry. It was designed by Luca Cardelli, James Donahue, Lucille Glassman, Mick Jordan, Bill Kalsow and Greg Nelson at the Digital Equipment Corporation (DEC) Systems Research Center (SRC) and the Olivetti Research Center (ORC) in the late 1980s.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

ECLiPSe is a software system for the development and deployment of constraint logic programming applications, e.g., in the areas of optimization, planning, scheduling, resource allocation, timetabling, transport, etc. It is also suited for teaching most aspects of combinatorial problem solving, e.g., problem modeling, constraint programming, mathematical programming, and search techniques. It contains constraint solver libraries, a high-level modeling and control language, interfaces to third-party solvers, an integrated development environment and interfaces for embedding into host environments.

Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under the MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.

In computer programming, a pure function is a function that has the following properties:

  1. the function return values are identical for identical arguments, and
  2. the function has no side effects.

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. It provides support for encapsulation and data hiding, separation of concerns and enhanced code reuse. Logtalk uses standard Prolog syntax with the addition of a few operators and directives.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

<span class="mw-page-title-main">Cython</span> Programming language

Cython is a superset of the programming language Python, which allows developers to write Python code that yields performance comparable to that of C.

Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative features. It has a simple C#-like syntax and a powerful metaprogramming system.

<span class="mw-page-title-main">Ciao (programming language)</span>

Ciao is a general-purpose programming language which supports logic, constraint, functional, higher-order, and object-oriented programming styles. Its main design objectives are high expressive power, extensibility, safety, reliability, and efficient execution.

<span class="mw-page-title-main">Elm (programming language)</span> Functional programming language

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. "Release 22.01.8". 8 September 2023. Retrieved 18 September 2023.
  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. Mercurylang.org. 29 (1–3): 17–64. CiteSeerX   10.1.1.46.9861 . 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