Thursday, October 4, 2012

Mercury

 

Short history of Mercury

        Mercury is developed at the University Of Melbourne Computer Science department under the supervision of Zoltan Somogyi. The first version was developed by Fergus Henderson, Thomas Conway and Zoltan Somogyi and was released on April 8, 1995.



What is Mercury?

  • based on the logic programming language Prolog
  • functional logic programming language geared towards real-world applications
  •  purely declarative logic language and related to both Prolog and Haskell.
  • features a strong, static, polymorphic type system, as well as a strong mode and determinism system.

       The language is designed with software engineering principles in mind. Unlike the original implementations of Prolog, it has a separate compilation phase, rather than being directly interpreted, which allows a much wider range of errors to be caught before running a program. It features a sophisticated, strict type and mode system, which its authors claim makes it much easier to write robust software. Mercury's module system enables division into self-contained units, a problem for past logic programming languages.
       Due to the use of information obtained at compile time (such as type and mode information), programs written in Mercury typically perform significantly faster than equivalent programs written in Prolog. The authors claim that Mercury is the fastest logic language in the world, by a wide margin.

Back-ends:

Production level
  • Low-level C for GCC
  • High-level C

Beta quality
  • Java bytecode for the JVM
  • C#
  • Erlang

Examples:

Hello World program
% Hello World in Mercury
%
% compile with:
%   mmake hello.depend
%   mmake hello

:- module hello.
:- interface.
:- import_module io.

% di=desctructive input, uo=unique output
:- pred main(io__state::di, io__state::uo) is det.

:- implementation.

% "-->" tells that we are using DCG (definite clause grammar), which frees us
% from threading some extra arguments through the code.
main --> io__write_string("Hello World\n").
(Prints "Hello World" onto the screen)

Factorial
:- module factorial.

:- interface.

:- pred factorial(int::in, int::out) is det.

:- implementation.

:- import_module int.

factorial(N, R) :-
        ( if N =< 1 then
                        R = 1
        else
                        factorial(N - 1, R0),
                        R = N * R0
         ).
(Gets the factorial of a number)

Length of a List
%
% Calculates the length of a list
%

:- import_module list, int.

:- pred length(list(T)::in, int::out) is det.

length(L, N) :-
        (
                % if L is empty
                L = [],
                N = 0
        ;
                % else 
                L = [_Hd | Tl],
                length(Tl, N0),
                N = N0 + 1
        ).
(Determines the length of the list)


Comments:


To the best of my knowledge, Mercury is fairly true to the Prolog model, though it does allow you to specify the unification with a bit more control (and it allows functional programming to boot).
It sounds a bit iffy to say that it's designed with "good" software engineering principles. This sounds like it's claiming that compiled, static-typed languages are always better than interpreted, dynamic-typed languages.
-Qeny, April 2009 


Conclusion:

I think, Mercury and Prolog are different since they are designed with different aims. But if you want a purely declarative language,then choose Mercury.
-Kristelle Geguiera

References:




CMSC 124 T-8L

  • Geguiera, Kristelle
  • Arnante, Jose Mari Hanz
  • Gramata, Bryan Jay




No comments:

Post a Comment