[mercury-users] more Mercury homework: "Mercuryfy" some simple Prolog

Jean-Marc Vanel jeanmarc.vanel at gmail.com
Sat Sep 25 01:01:19 AEST 2010


2010/9/24 Peter Ross <pro at missioncriticalit.com>
>
> > But, then, what to put in the implementation, which would the equivalent
> of
> > an empty class in Java ?
> >
> The equivalent to the empty class is.
>
> :- type person ---> person.
>

If I undertand well, this would be a class, but with no possibility to
create instances.

Reading the mercury_trans_guide :
http://www.mercury.csse.unimelb.edu.au/information/doc-release/mercury_trans_guide/Syntax.html#Syntax

it begins with the sentence :

*Prolog and Mercury have very similar syntax. Although there are a few
differences, by and large if a program is accepted by a Prolog system, it
will be accepted by Mercury. *

It seems to me that this is over-optimistic.
A simple and typical Prolog program like :

brother( jacques, simone ).
parent( simone, jean_marc ).
uncle(Uncle,Person) :-
        brother(Uncle,Parent), parent(Parent,Person).

requires quite a large quantity of boilerplate code.

Here the translation I did:

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

:- implementation.
%%%% declarations %%%%
:- import_module string.
:- type person.
:- func create( string ) = person .
:- pred uncle( person/*Uncle*/::out, person::in ) is nondet .

:- pred brother( person, person) is nondet .
:- mode brother( in, out ) is nondet .
:- mode brother( out, in ) is nondet .

:- pred parent(person, person).
:- mode parent(in, out) is semidet. % not everyone is a parent
% :- mode parent(out, in) is det.  % everyone has a parent
:- mode parent(out, in) is semidet.

%%%% implementation %%%%
:- type person == string .
create( S ) = S .

%%%% facts %%%%
brother( create("jacques"), create("simone")).
parent( create("simone"), create("jean_marc") ).

%%%% rules %%%%
uncle(Uncle,Person) :-
        brother(Uncle,Parent), parent(Parent,Person).

main(!IO) :-
        uncle(Uncle, create("jean_marc") )
-> (
        io.write( Uncle, !IO),
        io.write_string( " is an uncle.\n", !IO)
) ;
        io.write_string( "Fail\n", !IO)
.

I recognize that most of this necessary for a language for developing large
and modular programs.
But this really points to the lack of documentation. The tutorial is
skeletal, and the manual is laconic.

Happily the community is here and answers !

But I really wonder if it is possible to learn the language without living
in Melbourne or Brussels, and meeting the Gurus often.
These are only simple things I tried until now . What time will it take me
to do non trivial stuff ?
Compare with a language like Scala, which is 6 years younger than Mercury.
Scala has many tutorials and books.
I know that Scala has no logic programming features, so Mercury still is
promising.

-- 
Jean-Marc Vanel
Consulting, services, training,
Rule-based programming, Semantic Web
http://jmvanel.free.fr/
EulerGUI, a turntable GUI for Semantic Web + rules, XML, UML, eCore, Java
bytecode
+33 (0)6 89 16 29 52 -- +33 (0)1 39 55 58 16
( we rarely listen to voice messages, please send a mail instead )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20100924/e599d430/attachment.html>


More information about the users mailing list