Mercury (programming language): Difference between revisions

Content deleted Content added
4zimuth (talk | contribs)
No edit summary
m Template:Infobox programming language parameters: cut fixed size, fills, fix, carriage returns restore. Cut needless whitespace character spaces to standardize, aid work via small screens.
 
(213 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|Functional logic programming language}}
{{Infobox Software
{{For|Mercury Autocode|Autocode}}
{{Distinguish|Mercurial|Mercury (RemObjects BASIC programming language)}}
{{Infobox programming language
| name = Mercury
| logo = [[Image:Mercury_logoMercury (programming language) logo.jpg]]
| paradigm = [[Logic programming|Logic]], [[Functional programming|functional]], [[Object-oriented programming|object-oriented]]{{citation needed|date=February 2024}}
| screenshot =
| family = [[Prolog]], [[Haskell]]
| caption =
| developerdesigner = [[Zoltan Somogyi|Zoltán Somogyi]]
| developer = [[University of Melbourne]]
| latest_release_version = 0.13.1
| released = {{Start date and age|1995|04|08}}
| latest_release_date = 1 December 2006
| latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}}
| latest_preview_date = Release of the day (daily)
| latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}}
| operating_system = [[Cross-platform]]
| typing = [[Strong and weak typing|strong]], [[Type system#Static type checking|static]], [[Polymorphism (computer science)|polymorphic]]
| platform =
| scope = [[Scope (computer science)|lexical]]
| genre = [[Programming language]]
| programming language = Mercury
| license = [[GPL]]
| platform = [[IA-32]], [[x86-64]], [[ARM architecture family|ARM]], [[SPARC]]64, [[Java (software platform)|Java]], [[Common Language Infrastructure|CLI]]
| website = http://www.cs.mu.oz.au/research/mercury/
| operating system = [[Cross-platform software|Cross-platform]]: [[Unix]], [[Linux]], [[macOS]], [[Oracle Solaris|Solaris]], [[FreeBSD]], [[OpenBSD]], [[Microsoft Windows|Windows]], [[Android (operating system)|Android]]
| license = [[GNU General Public License|GPL]] compiler,<br/>[[GNU Lesser General Public License|LGPL]] standard library
| file ext = .m
| file format =
| website = {{URL|www.mercurylang.org}}
| implementations = Melbourne Mercury Compiler
| dialects =
| influenced by = [[Prolog]], [[Hope (programming language)|Hope]], [[Haskell]]
| influenced =
}}
'''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.
 
'''Mercury''' is a purely [[FunctionalDeclarative programming|functionaldeclarative]] [[logic programming|logic]] [[programming language]] geared towards real-world applications. It is developedrelated atto theboth [[University Of MelbourneProlog]] Computer Science department under the supervision ofand [[Zoltan Somogyi|Zoltán SomogyiHaskell]].<ref name="motivation">[http://www.mercurylang.org/about/motivation.html The firstMercury versionProject was- developedMotivation]</ref> byIt Fergusfeatures a Hendersonstrong, Thomasstatic, Conwaypolymorphic and[[type Zoltán Somogyisystem]], and wasa releasedstrong onmode Apriland 8th,determinism 1995system.
 
The official implementation, the Melbourne Mercury Compiler, is available for most [[Unix]] and [[Unix-like]] platforms, including [[Linux]], [[macOS]], and for [[Microsoft Windows|Windows]].
Mercury has several features intended for better [[software engineering]]. It is [[compiler|compiled]] rather than interpreted, as is traditional for logic programming languages. It features a sophisticated, strict [[Data type|type]] and mode system. Its authors claim these features combined with logic programming's abstract nature speeds writing of reliable programs. Mercury's module system enables division into self-contained units, a problem for past logic programming languages. (But note that several [[Prolog]] implementations now also support modules.)
 
==Overview==
Mercury is a more [[Declarative programming|declarative]] language than [[Prolog]], since it lacks "extra-logical" Prolog statements such as "cut" (which prevents [[backtracking]]) and [[imperative programming|imperative]] [[I/O]]. This enables better program [[Optimization (computer science)|optimization]], but makes coding sequential algorithms harder.
Mercury is based on the logic programming language [[Prolog]]. It has the same syntax and the same basic concepts such as the [[SLD resolution|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 [[compiler|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 [[Type system#Static type checking|static type]] and mode system<ref name=motivation/> and a module system.
Mercury has a strong emphasis on purity. While it allows impure functionality, this must be explicitly marked, and at the top level all functionality must either be pure or the programmer must promise that it is pure. Often impure operations can be made pure by threading the I/O state through them, as is done in the Mercury standard library. Due to the optimizations enabled by the purity of the language, programs written in Mercury typically perform significantly faster than equivalent programs written in Prolog.
 
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.<ref name="benchmarks">[http://www.mercurylang.org/about/benchmarks.html The Mercury Project - Benchmarks]</ref><ref name="jlp">{{cite journal
Mercury is available for most [[Unix]] platforms, including [[Mac OS X]], and [[Microsoft Windows]] (on the latter platform, it requires one of the [[Cygwin]] or [[MinGW]] toolsets, and can be compiled either with gcc or [[Visual C++|Microsoft Visual C++]]).
|last= Somogyi
|first= Zoltan
|last2= Henderson |first2= Fergus |last3= Conway |first3= Thomas
|title= The execution algorithm of Mercury: an efficient purely declarative logic programming language
|journal= Journal of Logic Programming
|volume= 29
|issue= 1–3
|pages= 17–64
|publisher= Mercurylang.org
|date= October–December 1996
|url= http://www.mercurylang.org/documentation/papers.html#jlp
|doi= 10.1016/S0743-1066(96)00068-4
|access-date = 2008-08-30|citeseerx=10.1.1.46.9861}}</ref> Its authors claim that Mercury is the fastest logic language in the world, by a wide margin.<ref name="motivation"/>
 
Mercury is a purely [[Declarative programming|declarative]] language, unlike [[Prolog]], since it lacks ''extra-logical'' Prolog statements such as <code>!</code> (cut) and [[imperative programming|imperative]] [[input/output]] (I/O). This enables advanced [[static program analysis]] and [[program optimization]], including compile-time [[Garbage collection (computer science)|garbage collection]],<ref>{{cite thesis
Notable programs written in Mercury include the Mercury compiler itself and the [[Prince XML]] formatter.
|url=https://mercurylang.org/documentation/papers/CW2004_03_mazur.pdf
|title=Compile-time garbage collection for the declarative language Mercury
|first=Nancy |last=Mazur |date=May 2004 |publisher=Katholieke Universiteit Leuven}}</ref> but it can make certain programming constructs (such as a switch over a number of options, with a default{{Dubious|Examples_of_difficulties_introduced_by_declarativeness.3F|date=February 2009}}) 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 type system|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.<ref>[https://www.odase.io/ ODASE]</ref>
== Back-ends ==
Mercury has several back-ends, which means it is possible to compile Mercury code into the following languages and code-styles:
 
==Back-ends==
'''Production level''':
Mercury has several back-ends, which enable compiling Mercury code into several languages, including:
*Low-level [[C (programming language)|C]] for [[GNU Compiler Collection|GCC]] (the original Mercury back-end)
*High-level C
 
===Production level===
'''Alpha or beta quality''' (may not work well, or even be completely broken):
* Low-level [[C (programming language)|C]] for [[GNU Compiler Collection]] (GCC), the original Mercury back-end
*[[Common Intermediate Language|IL]] for [[.NET Framework|Microsoft's .NET]]
* High-level C
*[[Java (programming language)|Java]] bytecode for [[Sun Microsystems|Sun]]'s [[Java virtual machine|JVM]]
* [[AssemblyJava (programming language)|AssemblerJava]] via the GCC back-end
* [[ErlangC Sharp (programming language)|ErlangC#]]
 
===Past===
'''Past back-ends''':
* [[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 of Mercury that will support Aditi.
* Aditi, a deductive database system also developed at the [[University of Melbourne]]. Mercury-0.12.2 is the last version to support Aditi.{{citation needed|date=January 2014}}
* [[Common Intermediate Language]] (CIL) for the [[.NET Framework]]
* [[Erlang (programming language)|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:
This makes Mercury a useful high-level language for targeting multiple platforms, or for linking with code written using multiple back-ends.
 
Mercury also has a strong 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:
{| class="wikitable"
! Back-end !! Foreign language(s)
Line 51 ⟶ 80:
| C (both levels)
| [[C (programming language)|C]]
|-
| IL
| [[Common Intermediate Language|IL]], [[C Sharp (programming language)|C#]], or [[Managed Extensions for C++|Managed C++]]
|-
| Java
| [[Java (programming language)|Java]]
|-
| Erlang
| [[Erlang (programming language)|Erlang]]
|-
| IL
| [[Common Intermediate Language]] (CIL) or [[C Sharp (programming language)|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. As both C backends are the only back-ends considered production quality, this means that you will not lose a great deal of portability using foreign-language C code.
 
== Examples ==
[[Hello world program|Hello World]]:
<syntaxhighlight lang="prolog">
<pre>
:- module hello.
:- interface.
Line 74 ⟶ 106:
main(!IO) :-
io.write_string("Hello, World!\n", !IO).
</syntaxhighlight>
</pre>
 
Calculating the 10th [[Fibonacci number]] (in the most obvious way):<ref name="tutorial">Adapted from [http://www.mercurylang.org/documentation/papers/book.pdf Ralph Becket's Mercury tutorial]</ref>
 
<syntaxhighlight lang="prolog">
Calculating the 10th [[Fibonacci number|Fibonacci number]]:
<pre>
:- module fib.
:- interface.
Line 87 ⟶ 118:
:- import_module int.
 
:- func fib(int) = int.
fib(N) = (if N =< 2 then 1 else fib(N - 1) + fib(N - 2)).
 
main(!IO) :-
Line 94 ⟶ 125:
io.write_int(fib(10), !IO),
io.nl(!IO).
% Could instead use io.format("fib(10) = %d\n", [i(fib(10))], !IO).
</pre>
</syntaxhighlight>
<code>!IO</code> 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:
<syntaxhighlight lang="prolog">
main(IO0, IO) :-
io.write_string("fib(10) = ", IO0, IO1),
io.write_int(fib(10), IO1, IO2),
io.nl(IO2, IO).
</syntaxhighlight>
 
==Release schedule==
(adapted from Ralph Becket's [http://www.cs.mu.oz.au/research/mercury/tutorial/book/book.pdf Mercury tutorial]).
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 (text editor)|Vim]]
* Flycheck library for [[Emacs]]
* A plugin is available for the [[Eclipse (software)|Eclipse IDE]]
* A plugin is available for the [[NetBeans|NetBeans IDE]]
 
==See also==
{{Portal|Free and open-source software}}
*[[Oz (programming language) | Oz/Mozart]] programming language and compiler
* [[AliceCurry (programming language) | AliceCurry]], programminganother functional logic language
* [[Alice (programming language)|Alice]], a dialect language of [[Standard ML]]
*[[Visual Prolog]] programming language
* [[Logtalk]], language, an object-oriented extension of Prolog which compiles down to Prolog
* [[Oz (programming language)|Oz/Mozart]], a multiparadigm language
* [[Visual Prolog]], language, a strongly typed object-oriented extension of Prolog, with a new syntax
 
==References==
{{Reflist}}
 
==External links==
* {{Official website|www.mercurylang.org}}
* [http://www.mercury.csse.unimelb.edu.au/ Official Mercury Homepage]
* [http://en.literateprograms.org/Category:Programming_language:Mercury Literate Programs (examples) in Mercury]
 
[[Category{{DEFAULTSORT:LogicMercury (programming languages]]language)}}
[[Category:ProgrammingCross-platform languagesfree software]]
[[Category:Functional logic programming languages]]
[[Category:.NET programming languages]]
[[Category:Programming languages created in 1995]]
 
[[Category:Statically typed programming languages]]
[[ca:Mercury]]
[[de:Mercury (Programmiersprache)]]
[[it:Mercury (linguaggio)]]
[[ru:Mercury (язык программирования)]]