/* CMPT 417 / COGS 300 Example Copyright Fred Popowich, January 2010 Assume we have three animals (dog,cat,mouse), a boat for up to two animals, and a river. They want to cross from the left bank to the right bank. It the cat is left unattended on in one location with either just the dog or the mouse, we have a disaster. Not a problem if the two are alone and the boat is there, though. Let's use a sorted list to represent the individuals at a specific location. The clock ticks whenever a boat leaves or arrives. */ /* A couple of constraints that spell disaster */ disaster([cat,dog]). disaster([cat,mouse]). /* Represent the state as a fact at(Time, Left, Boat, Right) Time is a list of clock ticks Left,Right is a list of animals (alphabetical order) */ at([], [boat,cat,dog,mouse], [], []). /* select an animal to board the boat on the left bank */ at([tick|Clock], Left, [boat,Animal], Right):- at(Clock, [boat|L0], [], Right), select(Animal,L0,Left), \+ disaster(Left). /* select two animals to board the boat on the left bank */ at([tick|Clock], Left, Boat, Right):- at(Clock, [boat|L0], [], Right), select(Animal1,L0,L1), select(Animal2,L1,Left), Animal1 @< Animal2, Boat = [boat,Animal1,Animal2], \+ disaster([Animal1,Animal2]). /* select an animal to board the boat on the right bank */ at([tick|Clock], Left, [boat,Animal], Right):- at(Clock, Left, [], [boat|R0]), select(Animal,R0,Right), \+ disaster(Right). /* select two animals to board the boat on the right bank */ at([tick|Clock], Left, Boat, Right):- at(Clock, Left, [], [boat|R0]), select(Animal1,R0,R1), select(Animal2,R1,Right), Animal1 @< Animal2, Boat = [boat,Animal1,Animal2], \+ disaster([Animal1,Animal2]). /* force everyone to get off the boat on the Right side Only appropriate when there is someone on the Boat. */ at([tick|Clock], Left, [], Right) :- at(Clock, Left, Boat, R0), Boat \== [], append(R0,Boat,R1), sort(R1,Right). /* force everyone to get off the boat on the Left side */ at([tick|Clock], Left, [], Right) :- at(Clock, L0, Boat, Right), Boat \== [], append(L0,Boat,L1), sort(L1,Left). /* is there anything to force the boat to actually cross the river during a clock tick? */