Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog programm returns yes instead of value

Tags:

prolog

I got the following event: item(C,X,G,P), (where C is a number for the product,X it's name,G it's price,P it's cost).
When i use the command item(n3001,_,_,P) directly on the prolog console i get as answer
G = 1.25 X = 100 but when i write the equation p3(C)-: item(C,_,_,P). then i consult the text i get as answer yes.
My question clarified is how come the one time i get the value of P which i want and the other time i get whether it's true or false?

like image 248
Kostas Thanasis Avatar asked May 03 '17 13:05

Kostas Thanasis


1 Answers

There are no return values in Prolog and p3/1 does not constitute a function but a relation. Your definition

p3(C) :-
   item(C,_,_,P).

reads: If item(C,_,_,P) succeeds then p3(C) succeeds as well. For the sake of argument, let's assume that your code includes the following fact:

item(n3001,100,1.25,1).

If you query

?- p3(n3001).

Prolog unifies C in the head of your rule with n3001 and then tries your goal item(C,_,_,P) which succeeds. Hence the rule succeeds and Prolog tells you:

   ?- p3(n3001).
yes

If you want to know the price corresponding to n3001 you have to to define a rule where P appears in the head of the rule as well, e.g.:

p3(C,P) :-
   item(C,_,_,P).

If you query that you'll get to see the value of P corresponding to n3001:

   ?- p3(n3001,P).
P = 1

If you query item/4 directly P appears in the arguments and therefore you get to see a substitution for it that satisfies your query:

   ?- item(n3001,_,_,P).
P = 1
like image 98
tas Avatar answered Oct 15 '22 12:10

tas