Mercury (programming language) 19726 214415031 2008-05-23T13:23:23Z Jonmmorgan 745036 Added citations for the performance of Mercury against Prolog. {{Infobox programming language | name = Mercury | logo = [[Image:Mercury logo.jpg]] | paradigm = [[logic programming|Logic]], [[Functional programming|functional]] | year = [[1995]] | designer = [[Zoltan Somogyi|Zoltán Somogyi]] | developer = [[University of Melbourne]] | latest_release_version = 0.13.1 | latest_release_date = 1 December 2006 | latest_test_version = Release of the day (daily) | latest_test_date = | typing = [[Strongly-typed programming language|Strong]], [[Static typing|static]], [[Type polymorphism|polymorphic]] | implementations = Melbourne Mercury Compiler | dialects = | influenced_by = [[Prolog]], [[Haskell (programming language)|Haskell]] | influenced = | operating_system = [[Cross-platform]] | license = [[GPL]] | website = http://www.cs.mu.oz.au/research/mercury/ }} {{Otheruses4|the functional logic programming language|other software with the same name|Mercury}} '''Mercury''' is a [[Functional programming|functional]] [[logic programming|logic]] [[programming language]] geared towards real-world applications. It is developed at the [[University Of Melbourne]] Computer Science department under the supervision of [[Zoltan Somogyi|Zoltán Somogyi]]. The first version was developed by Fergus Henderson, Thomas Conway and Zoltán Somogyi and was released on April 8, 1995. 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.) 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 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 ([http://www.mercury.csse.unimelb.edu.au/information/benchmarks.html], [http://www.mercury.csse.unimelb.edu.au/information/papers.html#jlp]). Mercury is available for most [[Unix]] platforms, including [[Mac OS X]], and [[Microsoft Windows]] (in Windows, it requires one of the [[Cygwin]] or [[MinGW]] toolsets, and can be compiled either with gcc or [[Visual C++|Microsoft Visual C++]]). Notable programs written in Mercury include the Mercury compiler itself and the [[Prince XML]] formatter. == Back-ends == Mercury has several back-ends, which means it is possible to compile Mercury code into the following languages and code-styles: '''Production level''': *Low-level [[C (programming language)|C]] for [[GNU Compiler Collection|GCC]] (the original Mercury back-end) *High-level C '''Alpha or beta quality''' (may not work well, or even be completely broken): *[[Common Intermediate Language|IL]] for [[.NET Framework|Microsoft's .NET]] *[[Java (programming language)|Java]] bytecode for [[Sun Microsystems|Sun]]'s [[Java virtual machine|JVM]] *[[Assembly language|Assembler]] via the GCC back-end *[[Erlang (programming language)|Erlang]] '''Past back-ends''': * 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. 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) |- | C (both levels) | [[C (programming language)|C]] |- | IL | [[Common Intermediate Language|IL]] or [[C Sharp (programming language)|C#]] |- | Java | [[Java (programming language)|Java]] |} 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]]: <pre> :- module hello. :- interface. :- import_module io. :- pred main(io::di, io::uo) is det. :- implementation. main(!IO) :- io.write_string("Hello, World!\n", !IO). </pre> Calculating the 10th [[Fibonacci number]]: <pre> :- 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). </pre> (adapted from Ralph Becket's [http://www.cs.mu.oz.au/research/mercury/tutorial/book/book.pdf Mercury tutorial]). ==See also== *[[Alice (programming language)| Alice]] programming language *[[Oz (programming language)| Oz/Mozart]] programming language and compiler *[[Visual Prolog]] programming language ==External links== * [http://www.mercury.csse.unimelb.edu.au/ Official Mercury Homepage] * [http://en.literateprograms.org/Category:Programming_language:Mercury Literate Programs (examples) in Mercury] [[Category:Logic programming languages]] [[Category:Functional logic programming languages]] [[Category:.NET programming languages]] [[ca:Mercury]] [[de:Mercury (Programmiersprache)]] [[it:Mercury (linguaggio)]] [[ru:Mercury (язык программирования)]]