Mercury (programming language)

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Mercury
Image:Mercury_logo.jpg
Developer Zoltán Somogyi
Latest release 0.13.1 / 1 December 2006
OS Cross-platform
Genre Programming language
License GPL
Website http://www.cs.mu.oz.au/research/mercury/

Mercury is a functional logic programming language geared towards real-world applications. It is developed at the University Of Melbourne Computer Science department under the supervision of Zoltán Somogyi. The first version was developed by Fergus Henderson, Thomas Conway and Zoltán Somogyi and was released on April 8th, 1995.

Mercury has several features intended for better software engineering. It is compiled rather than interpreted, as is traditional for logic programming languages. It features a sophisticated, strict 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 language than Prolog, since it lacks "extra-logical" Prolog statements such as "cut" (which prevents backtracking) and imperative I/O. This enables better program 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.

Mercury is available for most Unix platforms, 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 Microsoft Visual C++).

Notable programs written in Mercury include the Mercury compiler itself and the Prince XML formatter.

Contents

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 for GCC (the original Mercury back-end)
  • High-level C

Alpha or beta quality (may not work well, or even be completely broken):

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:

Back-end Foreign language(s)
C (both levels) C
IL IL, C# or Managed C++
Java 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.

Hello World in Mercury:

 :- module hello.
 :- interface.
 :- import_module io.
 :- pred main(io::di, io::uo) is det.

 :- implementation.
 main(!IO) :-
        io.write_string("Hello, World!\n", !IO).

(adapted from Ralph Becket's Mercury tutorial).

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.