5

I have to create family relations in Prolog for an assignment and I stumbled upon this problem.

man(john).
woman(lisa).
married(john,lisa).

?- married(john,X).
X = lisa.

?- married(X,john).
false.

How to make this predicate work in two ways? If john is married to lisa, then lisa is married to john.

For facts I can only use gender, parent, and married:

man(john).
woman(lisa).
parent(john,steve).
parent(lisa,steve).
married(john,lisa).

That requirement makes this solution unusable for me. I can't just add relation wife(lisa,john). because I have to define wife, husband etc. by myself, like.

wife(X,Y) :- woman(X),married(X,Y).
tas
  • 8,100
  • 3
  • 14
  • 22
Sava
  • 51
  • 2
  • I don't understand why you say the answer linked is unusable. Although it doesn't hand you the full answer to your question without doing any work, the principle that is needed is used there. – lurker Nov 16 '17 at 12:12

2 Answers2

4

Instead of trying to make married/2 bidirectional, you could define your predicate wife/2 accordingly. If the first argument of married/2 is always the husband and the second always the wife, just flip the arguments of the second goal:

wife(X,Y) :-
   woman(X),
   married(Y,X).      % <- changed to Y,X instead of X,Y

With your given facts this yields the desired result:

?- wife(X,Y).
X = lisa,
Y = john.

If your facts also include pairs the other way around, e.g.:

woman(ann).
man(anton).
married(ann, anton). 

you can include a second rule (namely your original rule) for wife/2 to deal with such pairs as well:

wife(X,Y) :-
   woman(X),
   married(Y,X).
wife(X,Y) :-          % <- new rule
   woman(X),          
   married(X,Y). 

With the additional rule and facts the above query now yields:

?- wife(X,Y).
X = lisa,
Y = john ;
X = ann,
Y = anton.
tas
  • 8,100
  • 3
  • 14
  • 22
0

If you can't add helper relation (it sounds like homework exercise), try to use predicate @< which operates on the "Standard Order of Terms":

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

Note, however, that this solution brings also some negative aspects (please read this answer).

trivelt
  • 1,913
  • 3
  • 22
  • 44
  • 1
    Voting down this answer without any comment is not helpful at all. Please let us know what is wrong... – trivelt Nov 17 '17 at 06:24
  • Actually it helped a lot, those negative aspects that you mentioned don't matter in my case beacause i don't need that extra information about the order of partners: married(john, lisa) is the same as married(lisa, john) for my needs. And I learned about the "Standard Order of Terms" :) – Sava Nov 18 '17 at 02:42