I'm currently trying to learn a little Prolog. As an exercise I'm trying to solve the following riddle:
These rules are given:
*Every person that has neither a car nor a plane, has a bike.
*Every person that doesn't have a plane but has a bike, has a car
*Every person that doesn't have a plane but has a car, has a truck
*Every person that doesn't have a truck but has a boat, doesn't have a plane
*Every person that doesn't have a boat but has a plane, doesn't have a car
Now there's four people:
*Person1 doesn't have a car but has a boat
*Person2 doesn't have a boat but has a plane
*Person3 doesn't have a plane but has a bike
*Person4 doesn't have a bike but has a car
Which person doesn't have a truck?
What I've come up with so far is this:
doesnthave(car,pa).
has(boat,pa).
doesnthave(boat,pb).
has(plane,pb).
doesnthave(plane,pc).
has(bike,pc).
doesnthave(bike,pd).
has(car,pd).
has(bike,X) :- doesnthave(car,X),doesnthave(plane,X).
has(car,X) :- doesnthave(plane,X),has(bike,X).
has(truck,X) :- doesnthave(plane,X),has(car,X).
doesnthave(plane,X) :- doesnthave(truck,X),has(boat,X).
doesnthave(car,X) :- doesnthave(boat,X),has(plane,X).
Now this doesn't seem to be enough. Or is this not the way of solving puzzles like this in prolog?
Edit: It seems the first two statements are contradictory. Together they yield: Every person that has neither a car nor a plane, has a car. I'm not sure if there is a sensible solution to this.
not sure about this solution, but a simpler representation of the knowledge surely helps:
now we can query what's the ownerships (that note it's a one to many relation)
Note I placed the
P
erson before theT
ransport, then we can inspect visually the result...Seems that the solution is a triple...