I'm trying to learn swi-prolog and I'm struggling with the following question.
The question goes: Vanessa, Mary and Penny study chemistry, biology and math, in countries Spain, Portugal, Venezuela. It is known, that Vanessa is not studying in Spain, while Mary is not in Portugal. The one who lives in Spain doesn't study math. The one in Portugal studies chemistry. Mary doesn't like biology, but likes Caracas. Define what each person studies and from where?
I tried many approaches where I tried to find where each person lives and then which subject is studied in each country, but when I try to combine those two things to get the end result, it is not definitive, and gives me ambiguous answer, meaning I get alternatives where one person lives in 2 different countries and might study 3 subjects. Any help or guidance is appreciated.
A possible solution could be the following:
At first, we add the countries, persons, and subjects to the knowledgebase:
Then we add invalid combinations:
Next we need a predicate to determine valid country-person-subject combinations:
And last a predicate which calculates three students and checks if their values are distinct:
This returns one result (without the
!
infindStudents/3
there are 6 results, but these are only permutations). You can also omit the!
and for example setP1
,P2
, andP3
to fix values, i.e.vanessa
,mary
,penny
.