Re: how to make this fail driven loop succesfull ?

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 03/08/2014 10:43 PM, Bob Minors wrote:
> Roelof,
> 
> You can make your failure-drive loop finish as 'true' like this:
> 
> siblings(Father,Mother) :- is_father(X, Father), is_mother(X, Mother), write(X), nl, fail ; true.
> 
> Reading from left-to-right, when it has found all the possible values
> for X and failed to find any more with backtracking, it will try the
> right-hand branch of the ';' and finally succeed with the 'true'.

Over time, as proposed here before, I generally prefer recursion or
forall/2 and avoid side effects and lists of objects satisfying some
criterion as much as possible.  So, you just write

sibling(Father, Mother, X) :-
	is_father(X, Father),
	is_mother(X, Mother).

You can easily combine this with other nice logical relations.  If you want
all of them, you use findall/3, but as late as possible because you cannot
easily combine siblings(Father, Mother, Siblings) without using member,
set operations, etc, which all make your code less readable.  If you merely
want to write them, use
	
	forall(sibling(Father, Mother, X), writeln(X)).

These are a few lessons I learned while helping non-experienced programmers
using Prolog to explore their data.

	Cheers --- Jan

> 
> 
> On 08/03/2014 21:00, Roelof Wobben wrote:
>> Hello, 
>>
>>
>> I have these facts: 
>>
>>
>> male(roelof). 
>>
>> male(mans).
>>
>> male(jan).
>>
>>
>> female(chantal). 
>>
>> female(tamara). 
>>
>> female(marie).
>>
>> female(gerda). 
>>
>>
>> parent(roelof,tamara).
>>
>> parent(mans, roelof). 
>>
>> parent(jan, chantal). 
>>
>> parent(marie, roelof). 
>>
>> parent(chantal, tamara).
>>
>> parent(mans, gerda).
>>
>> parent(marie, gerda). 
>>
>>
>> is_mother(Child,Mother):- 
>>
>> parent(Mother, Child),
>>
>> female(Mother).
>>
>>
>>
>> is_father(Child, Father):- 
>>
>> parent(Father, Child),
>>
>> male(Father). 
>>
>>
>>
>> siblings(Father,Mother) :- 
>>
>> is_father(X, Father), 
>>
>> is_mother(X, Mother),
>>
>>  write(X),
>>
>>  nl, 
>>
>> fail.
>>
>>
>> When I do siblings(mans,marie) it produces:
>>
>>
>> roelof
>>
>> gerda
>>
>> no
>>
>>
>> How can I change this to make the output like this:
>>
>>
>> roelof
>>
>> gerda
>>
>> yes. 
>>
>>
>> I have tried to add siblings(_) but with no success 
>>
>>
>> Roelof 		 	   		  
>> _______________________________________________
>> SWI-Prolog mailing list
>> [email protected]
>> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>
> 
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.