1
votes

I'm relatively new to prolog, and I'll try and explain this the best I can. Say that I have a small knowledge base of restaurants, where it has the name, cuisine, and price.

restaurant(spaghetti, italian, 20).
restaurant('naan bread', indian, 30).
...

And I have some people that like certain restaurants, such as:

likes(adam, restaurant, italian).

Where Adam likes italian restaurants. The main issue I'm having is that if I do a query such as:

likes(adam, spaghetti).

or

likes(adam, _, spaghetti).

It only comes up with false, no matter what I put. I have done a lot of research, but can't seem to get it to work - since I'm newish at prolog I don't really understand it. I have looked at books like 'Programming in Prolog' by Clocksin and Mellish and various websites, but I can't seem to find an answer or one I understand.

2
You can construct a predicate that calls the two predicates restaurant and likes, a bit similar to how a JOIN works in SQL.Willem Van Onsem
Where did you get the question for this? Was it from a book or class? There is probably more facts in the database and you need to list them all here so we can help you.Guy Coder
You probably need to add a rule such as likes(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
@GuyCoder yeah, it's from a class, but it's only a knowledge base of restaurants. So there aren't any other rules.user10702472

2 Answers

0
votes

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.
0
votes

So let's just say you have the following facts, as suggested in your question, with no extra and no changes:

restaurant(spaghetti, italian, 20).
restaurant('naan bread', indian, 30).

likes(adam, restaurant, italian).

Now you'd like to ask the question "What dish does Adam like?"

We can add this simple rule:

likes(Person, Dish) :-
    likes(Person, restaurant, Type),
    restaurant(Dish, Type, _).

Now you can go ahead and ask the following:

?- likes(adam, Dish), write(Dish).

This spits out the answer spaghetti.