why does this not display the right answer - prolog

I have these facts and rules :
male(roelof).
male(mans).
male(ronald).
male(jan).
female(chantal).
female(marie).
female(gerda).
female(dagmar).
female(denise).
female(kimberly).
parent(mans,gerda).
parent(mans,roelof).
parent(marie,gerda).
parent(marie,roelof).
parent(dagmar,denise).
parent(dagmar,kimberly).
parent(ronald,denise).
parent(ronald,kimberly).
parent(chantal,tamara).
parent(roelof,tamara).
parent(jan,chantal).
parent(jan,dagmar).
father_child(Father, Child) :-
parent(Father, Child),
male(Father).
mother_child(Mother, Child) :-
parent(Mother, Child),
female(Mother).
child_father_mother(Child, Father, Mother) :-
father_child(Father, Child),
mother-child(Mother, Child).
same_father(Child, Sibling) :-
father_child(Father, Child),
father_child(Father, Sibling).
same_mother(Child, Sibling) :-
mother_child(Mother, Child),
mother_child(Mother, Sibling).
siblings(Child, Sibling) :-
same_father(Child, Sibling),
Child \= Sibling.
siblings(Child, Sibling) :-
same_mother(Child, Sibling),
Child \= Sibling.
display_siblings(X,Y) :-
setof(X-Y, (siblings(X,Y), \+X=Y), Sibs),
member((X-Y,Y), Sibs),
\+ (Y#<X, member((Y,X), Sibs)).
But when I do display_siblings I expected to see roelof-gerda.
But the output is only x=roelof
What did I do wrong.
Im a beginner at Prolog and try to understand how this works.
Roelof
Edit 1: Could this be a solution: How can I prevent duplicates in prolog
Edit 2: I will do but I still do not understand why 'setof(X-Y, (siblings(X,Y), X #< Y), Sibs),write(Sibs).' is wrong. It works in both cases display_'siblings(gerda,X)' and 'display_siblings(X,Y)'

Most of your problem lies in your display_siblings predicate:
display_siblings(X,Y) :-
setof(X-Y, (siblings(X,Y), \+X=Y), Sibs),
member((X-Y,Y), Sibs),
\+ (Y#<X, member((Y,X), Sibs)).
In the second line, the \+X=Y is superfluous since siblings already enforces that they are different. So that can be:
setof(X-Y, siblings(X,Y), Sibs),
The next line is querying whether (X-Y,Y) is a member of Sibs. However, Sibs consists of X-Y terms which will never match an (X-Y,Y) term. So this call to member will always fail. It should probably be:
member(X-Y, Sibs),
And then the last line is overly complex if all you want to do is eliminate symmetrical duplicates. You can simply use X #< Y and for efficiency, make this part of the setof check:
display_siblings(Sibs) :-
setof(X-Y, (siblings(X,Y), X #< Y), Sibs).
This will yield:
?- display_siblings(Sibs).
Sibs = [chantal-dagmar, denise-kimberly, gerda-roelof].
If you want to select all of the siblings for a given person and still avoid the symmetry redundancies, you may need to process out those redundancies after the setof:
display_siblings(X, Sibs) :-
setof(A-B, ((X = A ; X = B), siblings(A,B)), SibPairs),
pack(SibPairs, Sibs).
pack([X-Y|T], Sibs) :-
pack(T, SibList),
( member(Y-X, SibList)
-> SibList = Sibs
; Sibs = [X-Y|SibList]
).
pack([], []).
?- display_siblings(gerda, Sibs).
Sibs = [roelof-gerda].

Related

How to link constant with variable by assert?

I want to add in the DB a constant and a linked variable:
?- assertz(my(x, A))
So that in the future I can define A and get the only one entry. Sth like that:
?- assertz(my(x, A)), ..., A = 2.
?- my(A, B).
A = x,
B = 2.
Can this be done?
As I noted in the comments your idea of a link like a pointer is not the way to approach solving your problem.
A common solution is to walk the tree and construct a new tree as you walk the tree by replacing the leaf of the tree with a new leaf that contains the value from the input tree along with the associated value, what you are thinking should be linked.
Since you are somewhat new to Prolog I will do this with two examples. The first will just walk a tree and only return true when successfully walked. It can be used to understand how to walk a tree and run with gtrace to single step the code to understand it.
The second example will expand on the tree walk and add the type (link as you think) to the leaf item. The the old leaf for something simple like an atom a, will become a new leaf in the tree like (a,atom).
Also this was quickly written as a demonstration only. I am sure it will have problems if pressed into doing anything more than the single example.
:- module(example,
[
example/1
]).
example(walk) :-
Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])),
walk(Term).
example(infer_type) :-
Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])),
infer_type(Term,Is),
write(Is).
walk([]) :- !.
walk([T]) :- var(T), !.
walk(L) :- is_list(L), !, L = [H|T], walk(H), walk(T).
walk(T) :- compound(T), !, T =.. [_|Args], !, walk(Args).
walk(T) :- integer(T), !.
walk(T) :- var(T), !.
walk(T) :- atomic(T), !.
walk(T) :- T =.. [Arg|Args], !, walk(Arg), walk(Args).
infer_type([],[]) :- !.
infer_type([T],[(T,var)]) :- var(T), !.
infer_type(L,S) :- is_list(L), !, L = [H|T], infer_type(H,I), infer_type(T,Is), S = [I|Is].
infer_type(T,S) :- compound(T), !, T =.. [F|Args], !, infer_type(Args,Is), S =.. [F|Is].
infer_type(T,(T,integer)) :- integer(T), !.
infer_type(T,(T,var)) :- var(T), !.
infer_type(T,(T,atom)) :- atomic(T), !.
infer_type(T,S) :- T =.. [Arg|Args], !, infer_type(Arg,I), infer_type(Args,Is), S =.. [I|Is].
Example run
Note: I know there are warnings; it is a demo not production code.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.3)
?- working_directory(_,'C:/Users/Groot').
true.
?- [example].
Warning: c:/users/Groot/example.pl:20:
Warning: Singleton variables: [T]
Warning: c:/users/Groot/example.pl:24:
Warning: Singleton variables: [T]
true.
?- example(walk).
true.
?- example(infer_type).
term_size(a((1,integer),(Hello,atom),(Atom,atom),(1,integer)+(2,integer),[(a,atom),(($),atom),(_25642,var)]))
true.
As an exercise I did not identify the string as a string, the change should be easy.

Family tree in Prolog

When I try to see who is brother to who and same for sister it gives me the sons and daughter, I cannot find the mistake...
father(pedro-i,fernando-i).
father(pedro-i,beatriz(1347)).
father(pedro-i,joão(1349)).
father(pedro-i,dinis(1354)).
father(pedro-i,joão_grão_mestre_da_ordem_de_avis).
mother(constança(1320),luis).
mother(constança(1320),maria(1342)).
mother(constança(1320),fernando-i).
mother(inês_de_castro,beatriz(1347)).
Any other opinion I appreciate that
ancestor(X,Y) :- mother(X,Y).
ancestor(X,Y) :- father(X,Y).
if_then_else(X,Y,male) :- father(X,Y).
if_then_else(X,Y,female) :- mother(X,Y).
son(X,Y) :- father(X,Y).
sister(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,female).
brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male).
descendent (X,Y) :- filho(X,Y).
descendent (X,Y) :- filho(X,Z),descendent (Z,Y).
grandfather(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
grandmother(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
grandchildren(X,Y) :- ancestor(Z,X),ancestor(Y,Z).
uncle(X,Y) :- brother(X,Z),ancestor(Z,Y).
Your clause brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male). requires Y to have an ancestor, but X also needs to have an ancestor -- the same ancestor:
brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,if_then_else(X,Y,male).
You also need to eliminate the requirement at the end that X be the father of Y.
brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,male(X).
male needs to depend simply on the individual (you don't need to be a father to be a male.) male (fernando-i)., etc.

how to avoid duplicates here?

I have these predicates:
male(roelof).
male(mans).
male(ronald).
male(jan).
female(chantal).
female(marie).
female(gerda).
female(dagmar).
female(denise).
female(kimberly).
parent(mans,gerda).
parent(mans,roelof).
parent(marie,gerda).
parent(marie,roelof).
parent(dagmar,denise).
parent(dagmar,kimberly).
parent(ronald,denise).
parent(ronald,kimberly).
parent(chantal,tamara).
parent(roelof,tamara).
parent(jan,chantal).
parent(jan,dagmar).
father_child(Father, Child) :-
parent(Father, Child),
male(Father).
mother_child(Mother, Child) :-
parent(Mother, Child),
female(Mother).
child_father_mother(Child, Father, Mother) :-
father_child(Father, Child),
mother_child(Mother, Child).
same_father(Child, Sibling) :-
father_child(Father, Child),
father_child(Father, Sibling).
same_mother(Child, Sibling) :-
mother_child(Mother, Child),
mother_child(Mother, Sibling).
siblings(X,Y) :-
( same_father(X, Y),
X \= Y
; same_mother(X, Y),
\+ same_father(X, Y)
).
display_siblings(Person) :-
findall(Person-Y, siblings(Person,Y), Sibs),
display_the_siblings(Sibs).
display_the_siblings([]) :-
write('Er zijn geen zussen/broers bekend').
display_the_siblings([X-Y]) :-
write('The enigste broer of zuster is '),
write(Y).
display_the_siblings([XY0,XY1|XYs2]) :-
XYs0=[XY0,XY1|XYs2],
write('Alle zusterparen zijn : \n '),
forall(( member(X - Y,XYs0)
),( format('~w en ~w. \n',[X,Y])
)).
It works fine but if you do display_siblings(X) you will see duplicates like this
Person 1 - Person 2
Person 2 - Person 1
How can I change this so the duplicates will not show up.
I know I can change findall to setof but then I see no output at all.
Roelof
Edit 1: #< works but it also means that display(kimberly) fails because Kimberly is further on the alphabet then denise. Can I not use the same trick as this topic : How can I prevent duplicates in prolog.
the simplest way should be to break the symmetry, for instance
...
findall(Person-Y, (siblings(Person,Y), Person #< Y), Sibs),
...

How to find blood relatives only in prolog

Using recursion i need to find all blood relatives of any person in the family tree.
My attempt so far has failed.
Here is my code, with my attempt at the bottom
female(helen).
female(debbie).
female(louise).
female(yvonne).
female(belinda).
female(heather).
male(john).
male(andrew).
male(barry).
male(daniel).
male(charles).
parent(helen, debbie).
parent(helen, barry).
parent(helen, louise).
parent(john, debbie).
parent(john, barry).
parent(andrew, louise).
parent(debbie, yvonne).
parent(debbie, daniel).
parent(barry, charles).
parent(barry, belinda).
parent(louise, heather).
mother(X, Y) :-
female(X),
parent(X, Y).
father(X, Y) :-
male(X),
parent(X,Y).
child(X, Y) :-
parent(Y, X).
daughter(X, Y) :-
parent(Y, X),
female(X).
son(X, Y) :-
parent(Y,X),
male(X).
sister(X, Y) :-
female(X),
parent(Q,X),
parent(Q,Y).
brother(X, Y) :-
male(X),
parent(Q,X),
parent(Q,Y).
sibling(X, Y) :-
parent(Q,X),
parent(Q,Y),
X\=Y.
uncle(X, Y) :-
parent(P,Y),
brother(X,P).
aunt(X, Y) :-
parent(P,Y),
sister(X,P).
cousin(C, Cousin):-
parent(Parent,C),
sibling(Parent,AU),
child(Cousin,AU).
%Here is Relative
relative(An, Re):-
An\=Re,
parent(An, Re);
sibling(An, Re).
relative(An, Rela):-
parent(An, Child);
sibling(An, Rela),
relative(Child, Rela),
An\=Rela, C\=Rela.
Sort of works, but gets stuck in an infinite loop at the end.
Thanks.
not sure about 'relatives' (any person bound reachable in a parent/child relation ?), but your definition seems more complex than needed ( do you know what ; does ?).
I tried
relative(An, Re):-
parent(An, Re).
relative(An, Rela):-
parent(An, C),
relative(C, Rela).
that yields
16 ?- forall(relative(X,Y),writeln(X:Y)).
helen:debbie
helen:barry
helen:louise
john:debbie
john:barry
andrew:louise
debbie:yvonne
debbie:daniel
barry:charles
barry:belinda
louise:heather
helen:yvonne
helen:daniel
helen:charles
helen:belinda
helen:heather
john:yvonne
john:daniel
john:charles
john:belinda
andrew:heather
true.
edit I tried another relation, using a generalized parent/2, but still too permissive.
relative(Pers, Re):-
ancestor(Re, Pers) ; sibling(Pers, Re) ; cousin(Pers, Re) ; uncle(Re, Pers) ; aunt(Re, Pers).
ancestor(Anc, Pers) :- parent(Anc, Pers).
ancestor(Anc, Pers) :- parent(Anc, P), ancestor(P, Pers).
Maybe cousin/2 is too permissive also. Here is the graph
I guess that heather should have only luise,helen,andrew as relatives. It's this true ?
edit given latest comment, seems that the definition could be right. I get
24 ?- setln(X,relative(heather,X)).
andrew
barry
belinda
charles
daniel
debbie
helen
louise
yvonne
true.
that is everyone is related to heather apart john.
Here's one way that works, but it will sometimes produce duplicates. Using setof will give the unique collection. I avoided the miscellaneous relations and stuck with descendent or parent.
descendent(A, B) :-
parent(B, A).
descendent(A, B) :-
parent(C, A),
descendent(C, B).
relative(A, B) :-
descendent(B, A).
relative(A, B) :-
descendent(A, B).
relative(A, B) :-
descendent(A, C),
descendent(B, C),
A \= B.
setof(A, relative(heather, A), Relatives).
Relatives = [andrew,barry,belinda,charles,daniel,debbie,helen,louise,yvonne]
If you don't have setof, you can use the findall/3 and sort/2 ISO predicates:
findall(A, relative(heather, A), R), sort(R, Relatives).
Note that the solutions presented so far assume that all of the relatives have unique names. A general case of dealing with relatives with the same first name (and possibly the same last name) you would need to track and compare lineages for differences.

Getting list of solutions in Prolog

I am learning prolog and I am reading a book called Programming Prolog for Artificial Intelligence. As practice I want to learn how to extend one of the examples in this book. Can someone please help?
Say you have these facts:
parent(pam, bob). %pam is a parent of bob
parent(george, bob). %george is a parent of bob
How would I write a prolog predicate that would give me a list of bobs parents? For example:
list_parents(bob, L).
L = [pam, george] ;
L = [george, pam] ;
true.
An all-solutions predicate like findall/3 might do the trick:
list_parents(P, L) :-
findall(Parent, parent(Parent, P), L).
Simply put, findall/3 finds all bindings for Parent in the 'backtrack-able' goal parent(Parent, P), and puts all bindings of Parent into the list L. Note that this won't remove duplicates, but you can do a sort/2 to L before returning it to create a set. Executing this:
?- list_parents(bob, L).
L = [pam, george].
If you don't have findall/3 in your PROLOG implementation, you could do it manually like this:
list_parents(P, L) :-
list_parents(P, [], L).
list_parents(P, Acc, L) :-
parent(Parent, P),
\+ member(Parent, Acc), !,
list_parents(P, [Parent|Acc], L).
list_parents(_, L, L).
This version sends calls to list_parents/2 off to an accumulator-version, list_parents/3. The latter tries to collect Parent bindings also, as long as we haven't seen them before (hence the \+ member check), and returns the list where no new Parent bindings accumulated into the Acc list can be found. Executing this gives us the same result as the first option:
?- list_parents(bob, L).
L = [pam, george].
Try this:
parent(pam, bob). %pam is a parent of bob
parent(george, bob). %george is a parent of bob
list_parents(A, Es, [X|Xs]) :- parent(X, A), \+ member(X, Es), list_parents(A, [X|Es], Xs).
list_parents(A, Es, []).
That was an inefficient method, a better method will need a "solutions" higher-order predicate.
list_parents(X, Ys) :- solutions(parent, [X, W], 1, Ys)

Resources