agent fedora
agent fedora

Reputation: 25

Prolog (swi for sharing) Predicate to solve a puzzle

I'm not sure how to even start to write a prolog predicate for a riddle. It gives statements of fact, but nothing more.

Upvotes: 0

Views: 75

Answers (1)

Enigmativity
Enigmativity

Reputation: 117027

To start with, what are some known facts?

fact(north,green,honest).
fact(north,red,lie).
fact(south,green,lie).
fact(south,red,honest).

The question "I'm red or I'm from the South" is possibly a little ambiguous. Is it a boolean algebra OR or an XOR? Should this be "I'm red or I'm from the South, but not both" or should it be "I'm red or I'm from the South, or both"?

Let's tackle "I'm red or I'm from the South, but not both".

Now we can write these two rules pretty easily:

bogg(R,C) :- fact(R,C,honest), R \= south, C = red.
bogg(R,C) :- fact(R,C,honest), R = south, C \= red.

The lie position on this would then be:

bogg(R,C) :- fact(R,C,lie), R = south, C = red.
bogg(R,C) :- fact(R,C,lie), R \= south, C \= red.

If we run that, we get:

?- bogg(R,C).
false.

Clearly, if there is an answer to this problem the statement isn't "I'm red or I'm from the South, but not both".

So let's try "I'm red or I'm from the South, or both":

bogg(R,C) :- fact(R,C,honest), R = south, C = red, !.
bogg(R,C) :- fact(R,C,honest), R = south.
bogg(R,C) :- fact(R,C,honest), C = red.
bogg(R,C) :- fact(R,C,lie), R \= south, C \= red.

Now when I run it I get:

?- bogg(R,C).
R = south,
C = red.

Upvotes: 2

Related Questions