Prolog programm returns yes instead of value

Kostas Thanasis

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?

tas

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prolog returns variable name instead of value

Prolog. After assinging variable a value, programm ouptut changes from `yes` to `no`

Can prolog answer undetermined instead of just yes or no?

prolog returns value twice

Prolog - Assign value instead of evaluating it

Returning a true value instead of integer value in prolog

Prolog print value as result instead of true

Curl returns true instead of value

Echo returns array instead of value

HTML Checkbox - save value as "Yes" / "No" instead of "true" / "false" in database (Javascript)

.value returns undefined instead of proper html value

Mocking returns Mock Object instead of return value

JsonResults returns Enum value instead of String representation

JSpinner returns default value instead of typed one

Indirect expansion returns variable name instead of value

ifelse returns NA instead of expected value

FFI returns a huge integer value instead of 0

Sql: Query returns column name instead of Value

In pytest returns magicMock instance instead of return value

Object function returns function instead of value

MySQL returns the name of the column instead of the value

Scala reduce and fold returns 0 instead of a value

React Native AsyncStorage returns promise instead of value

inline R returns 0 instead of correct value?

Stored procedure returns 0 instead of value

libreoffice calc if function returns true instead of value

Program returns a value of -1073741571 instead of going forever

Python recursive function returns None instead of value

Selenium returns empty text instead of the actual value