Prolog - symmetric predicates

I need to model the family tree in the prologue. And I have a problem with character predicates. Data:

parent(x,y).
male(x).
female(y).
age(x, number).

Rules:

blood_relationgives me a headache. this is what i did:

blood_relation(X,Y):-ancestor(X,Y).
blood_relation(X,Y):-uncle(X,Y);brother(X,Y);sister(X,Y);(mother(Z,Y),sister(X,Z));(father(Z,Y),sister(X,Z));(father(Z,Y),brother(X,Z)).
blood_relation(X,Y):-uncle(X,Z),blood_relation(Z,Y).

and I get that I consider satisfactory results (I have double prints - I can fix it), the problem is that I want this ratio to be symmetrical. This is not right now.

blood_relation(johns_father, joh):yes 
blood_relation(john,johns_father): no

So, is there a way to fix this. And I need a request: all pairs that are not in blood_relation ..

Update:

What relationship is the first statement that should satisfy? blood_relation (X, Y): - blood_relation (X, Y).

sorry .. this is a bad copy / paste..it

blood_relation(X,Y):-ancestor(X,Y).

Now fixed above.

there are other rules here:

father(X,Y):-parent(X,Y),male(X).  
mother(X,Y):-parent(X,Y),female(X).  
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X).  
sister(X,Y):-parent(Z,X),parent(Z,Y),female(X).  
grandFather(X,Y):-parent(Z,Y),parent(X,Z),male(X).  
grandMother(X,Y):-parent(Z,Y),parent(X,Z),female(X).  
uncle(X,Y):-mother(Z,Y),brother(X,Z).  
ancestor(X,Y):-ancestor(X,Y).  
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).

- . . , , , . .

, blood_relation ? not_blood_relation - . . . , .

. . .

query.. not(blood_relation(X,Y)) , , . :

age(X,Y), Y>18,  
not(parent(X,Z)),write(X),nl,fail.

+3
3

, ?

, , . , [a1, [[a2], [b2, [[e3], [f3]]], [c2]]], <tree >= [root, [<tree1 > , <tree2 > ...]]:

%Y is immediate child of X?
child(X,Y,[X|S]) :- member([Y|_],S).

%pick one tree in S and check
child(X,Y,[X|S]) :- member([Z|SS],S),child(Z,Y,[Z|SS]).

%X and Y end up with same root?
sib(X,Y,[R|T]) :- child(R,X,[R|T]), child(R,Y,[R|T]).

, , , , ...

+1

. , , ..

, (, , , , ).

friends(1,2).
friends(5,2).
friends(7,4).

, "friends(A,B) :- friends(B,A)." , , , , . "@</2", , ( ) " ". , , . , !

, "friend/2" .

friends(A,B) :- A @< B, friends(B,A).

, , . , ( ). , , , , .

:

friended(1,2).
friended(5,2).
friended(7,4).

friends(A,B) :- friended(A,B).
friends(A,B) :- friended(B,A).

, ( , , - ).

-

, , , , , .

potential_enemies(A,B) :- user(A), user(B), \+ friends(A,B).
+8

, ?

blood_relation(X,Y):-blood_relation(X,Y).

, "" . "", , , , , .

/2, , -, ? : -).

, , :

not_blood_relation(X, Y) :- blood_relation(X, Y), !, fail.
not_blood_relation(X, Y).

, !

0

Source: https://habr.com/ru/post/1707164/


All Articles