0

Below is a Prolog problem in my textbook that is a variation of the Zebra problem. Instead of the original, it asks you to find the location of the pizza out of 4 people who each ordered a unique food and drink. I've seen solutions to similar problems with lists and libraries but I don't think that's the intended approach in the textbook.

Consider the following problem: Donna, Danny, David, and Doreen were seated at a table in a restaurant. The men sat across from each other, as did the women. They each ordered a different main course with a different beverage. in addition, – Doreen sat beside the person who ordered steak. – The chicken came with a Coke. – The person with the lasagna sat across from the person with milk. – David never drinks coffee – Donna only drinks water – Danny could not afford to order steak.

Who ordered the pizza? Write a prolog program that solves this problem by displaying who ordered each of the main courses and each of the beverages. Hint: Begin by writing clauses defining predicates beside(x,y), which holds if person x is sitting beside person y, and across(x,y), which holds if person x is sitting across from person y.

My thought process in my current solution was to define 4 positions and then designate which positions would be across and beside each other. However, when I input the query solution(pizza), it returns false. My best guess would be that this method of defining across and beside is not logically correct. Here is the code that I currently have:

person(donna). person(danny). person(david). person(doreen). position(1). position(2). position(3). position(4). % 1 [] 3 % 2 [] 4 % ^Seating arrangement with table uniq_people(A,B,C,D) :- person(A), person(B), person(C), person(D), A\=B, A\=C, A\=D, B\=C, B\=D, C\=D. across(1,3). across(2,4). across(X,Y) :- across(Y,X). beside(1,2). beside(3,4). beside(X,Y) :- beside(Y,X). solution(pizza) :- uniq_people(donna,danny,david,doreen), uniq_people(steak,chicken,lasagna,pizza), uniq_people(coffee,water,coke,milk), uniq_people(1,2,3,4), across(danny,david), across(donna,doreen), beside(doreen,steak), chicken = coke, across(lasagna,milk), donna = water, david \= coffee, danny \= steak. 
2
  • You should have stopped at the point where you wrote that steak has to be a person and thought "this doesn't seem right". Also at the point where you wrote the atom chicken has to unify with the atom coke. Or where you wrote that 1, 3 are across from each other and then asked if danny and david are across from each other and noticed that atom danny is neither 1 nor 3 so can't be across from anything. And at the point where you wrote the rules for across(...) and tested across(danny, david) just to make sure it worked and saw it go into an infinite loop and lock up. Commented Oct 10, 2023 at 3:54
  • 1
    How is the atom pizza going to be the solution, and if you instead made it a variable, how does it get any answer assigned to it? Why are you giving the unique people instead of asking for them? Why aren't you using the position facts? Why are you ignoring the "Hint" about people sitting across from each other and going for lasagna sitting across from milk? Go to some much smaller code and play with it until you understand variables from atoms, Prolog's search, defining recursive rules which don't loop forever, and then build up bit-by-bit from there. Commented Oct 10, 2023 at 4:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.