Based on your comment
It's only a knowledge base of restaurants. So there aren't any other rules.
we'll take it from there and start with the given rules.
restaurant(spaghetti, italian, 20).
restaurant('naan bread', indian, 30).
Next is the query you are trying
likes(adam, spaghetti).
Which is valid, but as we note in the comments some facts are missing.
The simplest fact to make the query correct would be
likes(adam, spaghetti).
but you have other queries like
likes(adam, restaurant, italian).
and facts like
restaurant('naan bread', indian, 30).
which suggest that you are aware that there are relationships between the four entities, adam
, itialian
and spaghetti
, 20
(price).
There are endless ways to make relationships but for this example we will keep it more on the simple side.
person(adam).
person(mary).
food_nationality(spaghetti, italian).
food_nationality(hamburger, americian).
food_nationality('naan bread', indian).
food_price(spaghetti, 20).
food_price(hamburger, 30).
food_price('naan bread', 30).
likes(adam,italian).
likes(mary,american).
Now that we have some facts if you wanted to know what it cost for adam to eat a food he likes we start with the facts and see what we can conclude.
We see the fact
person(adam).
but that only tells us adam is a person and doesn't lead to any more information for our question.
We also see the fact
likes(adam,italian).
which tells us that adam likes italian but doesn't give us a specific food.
We also see
food_nationality(spaghetti, italian).
So we know adam likes italian and italian has spagehetti but we still need a price.
We also see
food_price(spaghetti, 20).
So we know adam likes italian and italian has spagehetti and spaghetti cost 20. So the answer is that for adam to eat something he likes it will cost 20.
As a Prolog predicate it would be
cost_to_eat(Person,Price) :-
likes(Person,Nationality),
food_nationality(Food, Nationality),
food_price(Food, Price).
and running that for adam
?- cost_to_eat(adam,Price).
Price = 20.
It also works for mary
?- cost_to_eat(mary,Price).
Price = 30.
and it also works if you give just a price
?- cost_to_eat(Person,20).
Person = adam ;
false.
and it also works it you ask
?- cost_to_eat(Person,Cost).
Person = adam,
Cost = 20 ;
Person = mary,
Cost = 30.
restaurant
andlikes
, a bit similar to how aJOIN
works in SQL. – Willem Van Onsemlikes(X, Dish) :- likes(X, restaurant, Cuisine), restaurant(Dish, Cuisine, _).
Prolog isn't a mind reader, it can't tell that someone likes a dish just because they like the cuisine without you informing it. – Daniel Lyons