Help explain this code

start :- 
   action(state(left,left,left,left),
   state(right,right,right,right)).

action(Start,Goal) :-
   plan(Start,Goal,[Start],Path),
   nl,write('Solution:'),nl,
   write_path(Path).
   % write_path(Path), fail. % all solutions output

plan(Start,Goal,Visited,Path) :-
   go(Start,Next),
   safe(Next),
   \+ member(Next,Visited),    % not(member(...))
   plan(Next,Goal,[Next|Visited],Path).
   plan(Goal,Goal,Path,Path).

go(state(X,X,Z,K),state(Y,Y,Z,K)) :- across(X,Y). % farmer, wolf
go(state(X,W,X,K),state(Y,W,Y,K)) :- across(X,Y). % farmer, goat
go(state(X,W,Z,X),state(Y,W,Z,Y)) :- across(X,Y). % farmer, cabbage
go(state(X,W,Z,K),state(Y,W,Z,K)) :- across(X,Y). % farmer

across(left,right).
across(right,left).

safe(state(B,W,Z,K)) :-
   across(W,Z), across(Z,K).
safe(state(B,B,B,K)).
safe(state(B,W,B,B)).

Can anyone help me explain this code ?
This is PROLOG program for the farmer–wolf–goat–cabbage problem.
Thanks everyone.

thanks you , i will do it

start :- action(state(left,left,left,left),
state(right,right,right,right)).

action(Start,Goal):-
plan(Start,Goal,[Start],Path),
nl,write('Solution:'),nl,
write_path(Path).
% write_path(Path), fail. % all solutions output

plan(Start,Goal,Visited,Path):-
go(Start,Next),
safe(Next),
\+ member(Next,Visited), % not(member(...))
plan(Next,Goal,[Next|Visited],Path).
plan(Goal,Goal,Path,Path).

go(state(X,X,Z,K),state(Y,Y,Z,K)):-across(X,Y). % farmer, wolf
go(state(X,W,X,K),state(Y,W,Y,K)):-across(X,Y). % farmer, goat
go(state(X,W,Z,X),state(Y,W,Z,Y)):-across(X,Y). % farmer, cabbage
go(state(X,W,Z,K),state(Y,W,Z,K)):-across(X,Y). % farmer

across(left,right).
across(right,left).

safe(state(B,W,Z,K)):- across(W,Z), across(Z,K).
safe(state(B,B,B,K)).
safe(state(B,W,B,B)).

enter write(Path). then start. to run it

Since we don’t know how much Prolog you know there is no sense in us trying to explain every detail. Instead explain what you think it does then we can help you where you have mistakes. Pretend we are the duck.

In other words if you don’t understand unification then you should not be looking at code at this level, it could take a few chapters to explain it.

Hmm , no way to understand it now , right ?

I don’t know what you mean by that.

If you are asking if we can explain it the answer is yes, but I don’t want to spend hours writing a detailed answer for just this if I have to start off explaining unification.


Also the reason I requested where you found the code is that it most likely came from a book or links back to something for teaching and that would explain it.

My English is not good, so if I say something wrong, please forgive me.
Yes , i found it in Introduction to AI book and i read their explanation but i don’t understand .
Hmm , Can you help me list any knowledge to understand that code?

Which book? Please provide title and author.

Just post in your native language and I will use Google Translate.

Introduction to artificial intelligence - Wolfgang Ertel

Thanks.

So it is Fig. 5.4 on page 84.

I glanced at book and saw that it really is not a book for beginners or self learning. It is heavy on the math and logic and a bit light on the programming aspects. Perhaps you should work through the site Learn Prolog Now! and then come back to this.

Thank you very much

I tried to run the program according to the instructions in the book but it didn’t work.

Please read the chapter completely before asking questions.

On page 84 right below the figure with the code it states

The definition of the predicate write_path for the task of outputting the plan
found is missing here. It is suggested as an exercise for the reader (Exercise 5.2 on
page 88). For initial program tests the literal write_path(Path) can be
replaced with write(Path). For the query “?- start.” we get the answer

Solution:
Farmer and goat from left to right
Farmer from right to left
Farmer and wolf from left to right
Farmer and goat from right to left
Farmer and cabbage from left to right
Farmer from right to left
Farmer and goat from left to right
Yes

When I change the code as noted and run I get.

?- start.

Solution:
[state(right,right,right,right),state(left,right,left,right),state(right,right,left,right),state(left,right,left,left),state(right,right,right,left),state(left,left,right,left),state(right,left,right,left),state(left,left,left,left)]
true ;

Solution:
[state(right,right,right,right),state(left,right,left,right),state(right,right,left,right),state(left,right,left,left),state(right,right,right,left),state(left,left,right,left),state(right,left,right,left),state(left,left,left,left)]
true ;

Solution:
[state(right,right,right,right),state(left,right,left,right),state(right,right,left,right),state(left,left,left,right),state(right,left,right,right),state(left,left,right,left),state(right,left,right,left),state(left,left,left,left)]
true ;

Solution:
[state(right,right,right,right),state(left,right,left,right),state(right,right,left,right),state(left,left,left,right),state(right,left,right,right),state(left,left,right,left),state(right,left,right,left),state(left,left,left,left)]
true ;
false.
1 Like

While you can ask beginner Prolog questions here, answering such questions is not the primary goal of this site. A better place to seek help and more likely to receive a reply is at StackOverflow using the Prolog tag.

Thank you.