Reputation: 1015
married(man, woman).
married(B, A) :-
married(A, B)
If B
is married to A
, A
is married to B
.
I understand that writing it as I have creates a loop, but I don't know how to prevent this.
parent(Parent, Child) :-
parent(married(Parent, Spouse), Child).
I am also unable to do something like this.
If Parent
is the parent of Child
:-
the spouse of Parent
is the parent of Child
.
Upvotes: 1
Views: 152
Reputation: 476503
Make two separate predicates. First you define a predicate that lists facts:
married_fact(philip, elisabeth).
married_fact(william, kate).
married(X, Y) :-
married_fact(X, Y).
married(X, Y) :-
married_fact(Y, X).
I am also unable to do something like this:
If
Parent
is the parent ofChild
:- the spouse ofParent
is the parent ofChild
.
The syntax parent(married(Parent, Spouse), Child).
does not make much sense, since married(Parent, Spouse)
is here a functor. Even if Prolog would see it as a predicate, a predicate does not "return" anything. A predicate is either true or false.
You can define a parent_fact/2
predicate:
parent_fact(phillip, charles).
and then define a predicate:
parent(Parent, Child) :-
parent_fact(Parent, Child).
parent(Parent, Child) :-
married(Parent, Spouse),
parent_fact(Spouse, Child).
Upvotes: 2