The hello world program looks like this:
:- module hello.
:- import_module io.
:- pred main(io::di, io::uo) is det.
io.write_string("Hello world from Mercury!\n",!IO).
module hellois a module declaration,
interfacedeclares elements exported by this module and
implementationcontains the code for this module.
A couple of days ago I started reading the Ralph Becket's Mercury tutorial which is a very nice tutorial for learning the language. Also there's useful documentation available in the website including a Prolog to Mercury Transition Guide .
The language allows the creation of functions, for example here's the mandatory factorial function:
:- func factorial(int) = int.
factorial(N) = (if N = 0 then 1 else N*factorial(N-1)).
But also the language allows the creation of predicates that result in multiple solutions as in Prolog. For example see the following predicate that generates the numbers from 0 to N:
:- pred numbers(int::in, int::out) is nondet.
N > 0,
R = N
predsection declares the
numberspredicate. It says that the first argument is of type
intand is for input and the second argument is also type
intand is for output. Finally the
is nondetsays that this predicate may result in zero or more solutions.
The first rule says that when asking for numbers from 0, return 0. The second rule says that if asking for a number greater than 0 then return all the numbers from 0 to N-1 and then return N.
Using this predicate we can implement the following example from a previous post:
We need to write a program to find a 4-digit number such that ABCD*4 = DCBA where A,B,C,D are the digits of that number.
(Complete problem is described here)
A Mercury predicate that solves this problem looks like this:
:- pred solve_problem(list(int)::out) is nondet.
A > 0,
(1000*A + 100*B + 10*C + D)*4 = (1000*D + 100*C + 10*B + A),
R = [A,B,C,D].
As with Prolog the backtracking mechanism will find the values of A,B,C and D that satisfy the constraints.
In order to use the result of this predicate the
solutionspredicate is used. This predicate returns a list with all the results from the specified predicate.
This program writes:
[[2, 1, 7, 8]]
In future post I'll try to explore more features of this interesting language.