In: Computer Science
Getting an error:
ERROR: is/2: Arguments are not sufficiently instantiated
/*
*Go predicate
* Call as follows:
* ?- go(state(3,3,w),state(0,0,e)).
*/
go(Start,Goal) :-
empty_stack(Empty_visited_stack),
stack(Start,Empty_visited_stack,Visited_stack),
path(Start,Goal,Visited_stack).
/*
* path predicates
*/
path(Goal,Goal,Visited_stack) :-
write('Solution path is:' ), nl,
write(Visited_stack).
path(State,Goal,Visited_stack) :-
move(State,Next_state),
not(member_stack(Next_state,Visited_stack)),
stack(Next_state,Visited_stack,New_visited_stack),
path(Next_state,Goal,New_visited_stack).
safe(M1,C1) :-
(M1 =< C1; C1 = 0),
M2 is 3-M1, C2 is 3-C1,
(M2 =< C2; C2 = 0).
/*
* move predicates
*/
%move one monster from west to east
move(state(M1, H1, w), state(M2, H1,e)) :-
M1 > 0,
M2 is M1-1,
safe(M2, H1).
%move two monster from west to east
move(state(M1, H1, w), state(M2,H1,e)) :-
M1 > 1,
M2 is M1-2,
safe(M2, H1).
%move one human and one monster from west to east
move(state(M1,H1,w),state(M2, H2,e)) :-
M1 > 0,
H1 > 0,
M2 is M1-1,
H2 is H1-1,
safe(M2, H2).
%move one human from west to east
move(state(M1,H1,w), state(M1,H2,e)) :-
H1 > 0,
H2 is H2-1,
safe(M1,H2).
%move two humans from west to east
move(state(M1, H1, w), state(M1, H2, e)) :-
H1 > 1,
H2 is H1-2,
safe(M1,H2).
%move one monster from east to west
move(state(M1, H1, e), state(M2, H1,w)) :-
M1 < 3,
M2 is M1 + 1,
safe(M2, H1).
%move two monsters from east to west
move(state(M1, H1, e), state(M2,H1,w)) :-
M1 < 2,
M2 is M1 + 2,
safe(M2,H1).
%move one monster and one human from east to west
move(state(M1, H1, e),state(M2, H2, w)) :-
M1 < 3,
H1 < 3,
M2 is M1 +1,
H2 is H1 +1,
safe(M2, H2).
%move one human from east to west
move(state(M1, H1, e), state(M1,H2,w)) :-
H1 < 3,
H2 is H1+1,
safe(M1,H2).
%move two humans from east to west
move(state(M1,H1,e), state(M1, H2,w)):-
H1 < 2,
H2 is H2 + 2,
safe(M1,H2).
/*
* stack abstract data type
*/
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
empty_stack([]).
member_stack(E, S) :- member(E, S).
stack(E, S, [E|S]).
/*
* definition of writelist
*/
writelist([]) :- nl.
writelist([H|T]):-
write(H),
tab(1), /* "tab(n)" skips n spaces. */
writelist(T).
It's the missionaries and cannibals problem in prolog, M for
Missionary, H for Cannibals
Any help is highly appreciated!
* ?- go(state(3,3,w),state(0,0,e)).
*/
go(Start,Goal) :-
empty_stack(Empty_visited_stack),
stack(Start,Empty_visited_stack,Visited_stack),
path(Start,Goal,Visited_stack).
/*
* path predicates
*/
path(Goal,Goal,Visited_stack) :-
write('Solution path is:' ), nl,
write(Visited_stack).
path(State,Goal,Visited_stack) :-
move(State,Next_state),
not(member_stack(Next_state,Visited_stack)),
stack(Next_state,Visited_stack,New_visited_stack),
path(Next_state,Goal,New_visited_stack).
safe(M1,C1) :-
(M1 =< C1; C1 = 0),
M2 is 3-M1, C2 is 3-C1,
(M2 = C2; C2 = 0).
/*
* move predicates
*/
%move one monster from west to east
move(state(M1, H1, w), state(M2, H1,e)) :-
M1 >= 0,
M2 is M1-1,
safe(M2, H1).
%move two monster from west to east
move(state(M1, H1, w), state(M2,H1,e)) :-
M1 > 1,
M2 is M1-2,
safe(M2, H1).
%move one human and one monster from west to east
move(state(M1,H1,w),state(M2, H2,e)) :-
M1 > 0,
H1 > 0,
M2 is M1-1,
H2 is H1-1,
safe(M2, H2).
%move one human from west to east
move(state(M1,H1,w), state(M1,H2,e)) :-
H1 > 0,
H2 is H2-1,
safe(M1,H2).
%move two humans from west to east
move(state(M1, H1, w), state(M1, H2, e)) :-
H1 > 1,
H2 is H1-2,
safe(M1,H2).
%move one monster from east to west
move(state(M1, H1, e), state(M2, H1,w)) :-
M1 < 3,
M2 is M1 + 1,
safe(M2, H1).
%move two monsters from east to west
move(state(M1, H1, e), state(M2,H1,w)) :-
M1 < 2,
M2 is M1 - 2,
safe(M2,H1).
%move one monster and one human from east to west
move(state(M1, H1, e),state(M2, H2, w)) :-
M1 < 3,
H1 < 3,
M2 is M1 +1,
H2 is H1 +1,
safe(M2, H2).
%move one human from east to west
move(state(M1, H1, e), state(M1,H2,w)) :-
H1 < 3,
H2 is H1+1,
safe(M1,H2).
%move two humans from east to west
move(state(M1,H1,e), state(M1, H2,w)):-
H1 < 2,
H2 is H2 - 2,
safe(M1,H2).
/*
* stack abstract data type
*/
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
empty_stack([]).
member_stack(E, S) :- member(E, S).
stack(E, S, [E|S]).
/*
* definition of writelist
*/
writelist([]) :- nl.
writelist([H|T]):-
write(H),
tab(1), /* "tab(n)" skips n spaces. */
writelist(T).