tags:

views:

82

answers:

4
+1  Q: 

Prolog rule help

Dear good people,

I am new to Prolog and am experimenting around with some stuff, in particular i'm thinking about how to do a certain thing in prolog. I am aware of facts and rules, facts being something of the sort

specialCustomer(x).                     //person x is a specialcustomer

and rules:

totalSpend(x,500) :- specialCustomer(x).     //if x spends 500, he is a special customer

Would this fact and rule be valid in prolog? Is the rule wrong? How would i be able to query this through prolog? As in would a call of

totalSpend(bob,500).

be a valid call?

sorry if i am answering my own question, i just seem to be a bit...well confused!

+1  A: 

Everything you wrote is syntactically valid, but from your comments it doesn't seem like it does what you want it to.

specialCustomer(x).

Here you're saying specialCustomer(x) is true (and specialCustomer(anything_else) is false).

totalSpend(x,500) :- specialCustomer(x).

Here you're saying that totalSpend(x,500) is true iff specialCustomer(x) is true. Since you already defined special customer to be true, you could just as well have written

totalSpend(x,500).

Your comment makes it look as if you think that the part before the :- is the condition for the part after it, but it's the other way around.

totalSpend(bob,500).

Here you're asking whether totalSpend(bob, 500) is true, but since there is no rule for bob, it will be false.

Note that x and bob are symbols, not variables. So specialCustomer(x) will be true, but specialCustomer(bob) won't be.

sepp2k
Ohhh ok, I see. Forgot that the :- was the other way round in prolog! So am i right in thinking instead it will be something like:specialCustomer(bob) :- totalSpend(bob,500).is the way to probably do it? then the call totalSpend(bob,500). would mean specialCustomer(bob). is true?
KP65
@keval, yes, that's correct, except that you should use a variable (initial capital) instead of an ordinary atom (initial small).
Max Shawabkeh
I see, thank you. I was looking at it again, what about something of the sort: specialCustomer(X). totalSpend(500). Discount(X): - specialCustomer(X), totalSpend(Y).i think its right
KP65
Ok, i seem to be completely stuck, don't think I can get anything working. I don't seem to understand how I can write this in prolog, i could easily write it in java but thats not what i want. Any help?
KP65
A: 

What you probably want to express is

speccust(bob).
totalSpend(X,500) :- speccust(X).

such that bob is a special customer, and if somebody spent 500, then he is a special customer. In practice, you would save that to a file, say customer.pl, and for instance in swi-prolog load it by putting ['customer.pl']. Then, you can put queries to the database. In this case, you maybe want to know who is a special customer, then you would state:

totalSpend(Who, 500).

and receive Who = bob.

Bullet Tooth Tony
Thanks. It makes sense, I already have it in a file. So if i gave another name, e.g tom instead of bob how will it store in the database?
KP65
also how would you represent that totalspend should be greater >500 and not just 500 to be a speccust?
KP65
If you want it to apply to tom you need to add a fact speccust(tom).
rvirding
+1  A: 

Maybe you want x to be a variable? For that it has to be an upper case X.

starblue
+2  A: 

If you want to say that Bob, Jim and everyone who spends more than 500 are special customers, then define some people's spending, you would define it as follows:

specialCustomer(bob).
specialCustomer(jim).
specialCustomer(Who) :-
  totalSpend(Who, Amount),
  Amount >= 500.

totalSpend(mary, 400).
totalSpend(jack, 600).
totalSpend(pam, 500).

Then you would query it as follows:

?- specialCustomer(jim).
true.

?- specialCustomer(mary).
false.

?- specialCustomer(jack).
true.

?- specialCustomer(pam).
true.

?- specialCustomer(X).
X = bob ;
X = jim ;
X = jack ;
X = pam.
Max Shawabkeh
You took the code right out of my mouth. It seems to that much of the confusion is between adding something to the database and testing whether it is in the database.
rvirding