1
other_bank(e,w).
other_bank(w,e).
% farmer,wolf,goat,cabbage
move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :- other_bank(X,Y).
move([X,Wolf,X,Cabbage],goat,[Y,wolf,Y,Cabbage]) :- other_bank(X,Y).
move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :- other_bank(X,Y).
move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]):-other_bank(X,Y).

safety_check(X,X,_).
safety_check(X,_,X).
safe_status([Man,Wolf,Goat,Cabbage]):-
    safety_check(Man,Goat,Wolf),
    safety_check(Man,Goat,Cabbage).
solution([e,e,e,e],[]).
solution(Config,[Move|OtherMoves]):-
    move(Config,Move,NextConfig),
    safe_status(NextConfig),
    solution(NextConfig,OtherMoves).

% length(X,7),solution([w,w,w,w],X).

Compilation error while loading the .pl file in GNU Prolog

Farmer-wolf-goat-cabbage problem

false
  • 10,264
  • 13
  • 101
  • 209
  • I have corrected the code still is giving me a compilation that failed error. – siddhesh kadam Feb 03 '21 at 06:18
  • 1
    Do you get a warning of `singleton Wolf` on the second `move/3` line? It's a hint that this line still have a problem. – hakank Feb 03 '21 at 06:22
  • C:/Users/SIDDHESH/OneDrive/Desktop/prolog_data/data.pl for byte code... C:/Users/SIDDHESH/OneDrive/Desktop/prolog_data/data.pl:5:29: syntax error: , or ) expected 1 error(s) compilation failed – siddhesh kadam Feb 03 '21 at 06:31
  • It's strange. When I compile with `gplc` I got a singleton warning on `Wolf`. – hakank Feb 03 '21 at 06:37

1 Answers1

1

You are almost there! When spelt with lower case wolf is a constant (Upper case Wolf is a variable).

other_bank(e,w).
other_bank(w,e).
% farmer,wolf,goat,cabbage
move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :- other_bank(X,Y).
move([X,Wolf,X,Cabbage],goat,[Y,Wolf,Y,Cabbage]) :- other_bank(X,Y).
move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :- other_bank(X,Y).
move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]):-other_bank(X,Y).

safety_check(X,X,_).
safety_check(X,_,X).
safe_status([Man,Wolf,Goat,Cabbage]):-
    safety_check(Man,Goat,Wolf),
    safety_check(Man,Goat,Cabbage).
    
solution([e,e,e,e],[]).
solution(Config,[Move|OtherMoves]):-
    move(Config,Move,NextConfig),
    safe_status(NextConfig),
    solution(NextConfig,OtherMoves).

?- length(X,7),solution([w,w,w,w],X).
X = [goat,nothing,wolf,goat,cabbage,nothing,goat]
MWB
  • 11,740
  • 6
  • 46
  • 91
peter.cyc
  • 1,763
  • 1
  • 12
  • 19