Magician Artemka
Magician Artemka

Reputation: 67

Problems with comparison in prolog's rules

I'm new in Prolog and I create a small base wit "car" statements.

car(ford, mondeo, 125600, 31000).
car(ford, mondeo, 111232, 35000).
car(renault, megane, 82000, 28000).
car(renault, laguna, 102000, 35000).
car(renault, laguna, 82000, 42000).
car(www, pasat, 82000, 42000).
car(renault, aaa, 82000, 428900).

How I can find all cars with >10000 third argument? I try write a rule in consol (!not in the file!) ?- car(Z,X,C > 10000,V). but I get only false (but how? Prolog must print all cars, because all cars are match).

Upvotes: 1

Views: 53

Answers (1)

fferri
fferri

Reputation: 18940

car(Z,X,C > 10000,V). will not work, because prolog will try to unify each rule head with car(Z,X,C > 10000,V)., and it will fail for every one of those.

More in detail:

car(ford, mondeo, 125600, 31000) will fail to unify with car(Z,X,C > 10000,V), because 125600 fails to unify with C > 10000.

Make sure to read and understand how unification works.

The correct strategy is to exploit backtracking to filter results: you probably have noticed that entering the query car(Z,X,C,V) will return you all the cars' data.

If you add a further goal to that, it will have to go back when the second goal fail, and try another choice.

Continue the reading also with proof search.

The second goal you should add to your query then is C>10000:

car(Z,X,C,V), C > 10000

Upvotes: 1

Related Questions