0
votes

I am having trouble understanding how to start this program in Prolog.

Goal: Write a Prolog program that lists facts on who is a parent of who, and who is female and who is male. Based on these facts, it also codes rules on what defines mother, and what defines father. However, the functions mother and father cannot be used in a fact.

Examples:

?- mother(X, tommy)
X = jane
?- father(X, tommy)
X = john
?- mother(X, tammy)
X = beth
?- father(X, tammy)
X = mike

Any help is appreciated. How would I go about doing this?

1

1 Answers

0
votes

This is a very common exercise in prolog and just by having a quick google there are countless similar solutions to what you're looking for! Let's pop the information we have been given by the example into a set of facts.

parent(jane, tommy).
parent(beth, tammy).
parent(john, tommy).
parent(mike, tammy).

female(jane).
female(beth).

male(john).
male(mike).

the functions mother and father cannot be used in a fact.

So we have to find a way around that. So what do we know, if you're a mother you must be female and if you're a father you must be male.

mother(X, Y):-
    parent(X, Y),
    female(X).

father(X, Y):-
    parent(X, Y),
    male(X).

So when you write father(X, tammy). it will return X = mike. I would recommend having a dig through this github as the approach is very similiar.