[m-users.] basic newbie-friendly example needed

Tomas By tomas at basun.net
Wed Sep 2 10:54:58 AEST 2015


Hello,

Here is that example with Mercury declarations added. Assuming you have 
the compiler installed, you can say:

$ mmc genealogy.m
$ ./genealogy
[john, david, steve, judy, marilyn, susan]

I don't think this demonstrates many of Mercury's features, but maybe it 
will take you one step forward.

/Tomas



----------------------------------------------------------------------
:- module genealogy.
:- interface.
:- use_module io.
:- pred main(io.state::di,io.state::uo) is det.
:- implementation.
:- import_module solutions.

main(!IO) :-
   solutions((pred(X::out) is nondet :-
     grandparent(giuseppe,X)),Xs),
   io.print(Xs,!IO), io.nl(!IO).

:- type person ---> john ; david ; steve ; george ; georgeA ; giuseppe ; 
robert
  ; fred ; moses ; jacques ; judy ; marilyn ; estelle ; evelyn ; margaret 
; mary
  ; albina ; ida ; theresa ; therese ; susan ; inger.

:- pred male(person).
:- mode male(in) is semidet.
:- mode male(out) is multi.
male(john).
male(david).
male(steve).
male(george).
male(georgeA).
male(giuseppe).
male(robert).
male(fred).
male(moses).
male(jacques).

:- pred female(person).
:- mode female(in) is semidet.
:- mode female(out) is multi.
female(judy).
female(marilyn).
female(estelle).
female(evelyn).
female(margaret).
female(mary).
female(albina).
female(ida).
female(theresa).
female(therese).
female(susan).

:- pred parents(person,person,person).
:- mode parents(in,in,out) is semidet.
:- mode parents(in,out,out) is semidet.
:- mode parents(out,in,out) is nondet.
:- mode parents(out,out,in) is nondet.
parents(john,george,estelle).
parents(david,george,estelle).
parents(steve,george,estelle).
parents(marilyn,george,estelle).
parents(judy,george,estelle).
parents(george,georgeA,ida).
parents(georgeA,moses,theresa).
parents(moses,jacques,therese).
parents(estelle,giuseppe,albina).
parents(evelyn,giuseppe,albina).
parents(margaret,giuseppe,albina).
parents(fred,giuseppe,albina).
parents(robert,giuseppe,albina).
parents(mary,giuseppe,albina).
parents(susan,robert,inger).

:- pred sister(person,person).
:- mode sister(in,out) is nondet.
:- mode sister(out,in) is nondet.
sister(S,X) :-
   parents(S,F,M), parents(X,F,M), female(S), S \= X.

:- pred parent(person,person).
:- mode parent(in,out) is nondet.
:- mode parent(out,in) is nondet.
parent(P,X) :- parents(X,P,_).
parent(P,X) :- parents(X,_,P).

:- pred aunt(person,person).
:- mode aunt(in,out) is nondet.
:- mode aunt(out,in) is nondet.
aunt(A,X) :- parent(P,X), sister(A,P).

:- pred grandparent(person,person).
:- mode grandparent(in,out) is nondet.
:- mode grandparent(out,in) is nondet.
grandparent(G,X) :- parent(G,P), parent(P,X).

:- pred grandmother(person,person).
:- mode grandmother(in,out) is nondet.
:- mode grandmother(out,in) is nondet.
grandmother(GM,X) :- grandparent(GM,X), female(GM).

:- end_module genealogy.



On 2015-09-01 17:31, Sandy Norton wrote:
> Hi Folks,
> 
> Unfortunately, I've been really struggling to find newbie-friendly
> resources to learn Mercury. The mercury book tutorial is a good start
> but it's incomplete and was last updated in 2010.
> 
> Would it be possible to have a basic example demonstrating mercury's
> specific features (types, modes, purity, IO) along the lines of the
> canonical geneology demo in prolog (e.g
> http://web.cs.ucla.edu/~rosen/161/notes/prolog3.html)?
> 
> Regards,
> 
> Sandy
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://www.mercurylang.org/lists/listinfo/users




More information about the users mailing list