I want to define the following rule in prolog:
when X
is thirsty, X
should go to the bar.
after, I would ask prolog thirsty(X)
and prolog should give back go(bar)
I have tried it on the following way:
go(Y).
thirsty(X) :- go(bar).
But when I test it with
thirsty(bob).
The result will only be true
. Can someone help me what I have to change to get go(bar)
as the result?
Looking at the first part of this sentence, "when
X
is thirsty”, there’s clearly a property thirsty thatX
can have, sothirsty
sounds like a good predicate (thirsty(X)
meansX
is thirsty).Then there’s a few ways to look at the second part of the sentence, "
X
should go to the bar”, I’ll write some below in order of increasing complexity with the last being what I think you want:X
, in which case you can define the predicateshould_go_to_the_bar
as a predicate so thatshould_go_to_the_bar(X)
meansX
should go to the bar.Then your program can be:
X
andthe_bar
, so thatshould_go_to(X, the_bar)
means that X should go to the_bar.Then your program can be:
go_to(X, the_bar)
, so thatshould(go_to(the_bar))
means that it should be that go_to(X, the_bar).Then your program can be:
X
and an action they can do likego_to(X, the_bar)
, so thatshould(X, go_to(X, the_bar)
means that X should go_to(X, the_bar) i.e. X should make it true that “X goes to the bar".Then your program can be:
I think this last interpretation is closest to what you want.
go(Y).
as a rule doesn't really mean anything, other than go anywhere perhaps. When you assert rules in Prolog, you should be able to assign some semantic meaning to them. Your rule says, IfX
is thirsty, then thebar
is a place to go which of course always succeeds sincebar
is always a place to go, as is any place else you might want to go according to your rule,go(Y)
. Since you didn't useX
anywhere in yourthirsty(X)
predicate clause body, it's never used (so you probably saw a singleton variable warning about that).Defining a good rule in Prolog is first about stating your rule in a sensible way first. Perhaps a reasonable rule involving a person might be:
This might be stated as:
Or more generally,
Then you'd need to assert some facts about the person (or query the user for these facts):
With that fact asserted, the following query can result: