[net.lang.prolog] How should I do this?

mob@mit-amt.MIT.EDU (Mario O. Bourgoin) (08/04/86)

Hello,
	I  am trying   to write  a solution  program    for the Farmer
problem. Simply stated:

	A farmer wants to move himself, a silver fox, a fat  goose and
	some jucy grain across a river. Unfortunately, his  boat is so
	tiny he  can only  take one of his things  across on any trip.
	Worse   yet, an unattended  fox will   eat  a  goose, and   an
	unattended goose  will eat the grain,  so the  farmer must not
	leave the fox alone with the goose or the goose alone with the
	grain.  What is he to do?

So far,  I  have stated  the  problem in the   way outlined below. Can
someone   help we  to  get this  working? It figures   out   the first
alternative of moving the goose across but then starts looping on what
to do for the next move.

same(yes,1,1).
same(yes,2,2).
same(no,1,2).
same(no,2,1).

legal(state(F,X,G,A)) :-
	(same(no,X,G);same(yes,F,X)),
	(same(no,G,A);same(yes,F,G)).

linked(state(F1,X1,G1,A1),state(F2,X2,G2,A2)) :-
	same(no,F1,F2),
	(same(yes,X1,X2);(same(yes,F1,X1),same(yes,G1,G2),same(yes,A1,A2))),
	(same(yes,G1,A2);(same(yes,F1,G1),same(yes,X1,X2),same(yes,A1,A2))),
	(same(yes,A1,A2);(same(yes,F1,A1),same(yes,G1,G2),same(yes,X1,X2))).

path(State1,State2) :-
	legal(State1),legal(State2),
	(linked(State1,State2);(path(State1,State3),path(State3,State2))).

windes@uicsg.UUCP (08/07/86)

Well, I have a couple of suggestions:

	- First, I don't think your 'linked' procedure is doing what it
should.  Seems to me that the query 'linked(state(1,1,1,1),X).' should
produce four distinct alternatives.
	- Second, you need to rewrite your 'path' procedure because
A) it is left-recursive, and B) you need to keep a record of states already
visited, and exclude them from the possible next states.

	hope this helps.

Ed Windes * UofI ...ihnp4!uiucdcs!uicsl!uicsg!windes  - until 8/14/86
          * AT&T ...ihnp4!ihopa!edw		       - after 8/14/86

(HELP, I'm being help in a closet and forced to learn prolog!)