The primary issue causing the failure is a typo:
step((Man1, Goose, Fox, Beans1), "Beans", (Man2, Goose, Fox, Beans1)) :-
Should be:
step((Man1, Goose, Fox, Beans1), 'Beans', (Man2, Goose, Fox, Beans2)) :-
This will generate correct solutions. There are a few other clean up points:
- You are using double quotes on strings, which in Prolog, is actually a sequence of ASCII codes (prolog List). If you want a string displayed as just a string, you would use Prolog atoms which are enclosed in single quotes. So, for example,
'Man'
instead of"Man"
. - The goal of
valid(Tmp)
in yourreachable/2
clause is superfluous since the prior goal ofstep
ensures thatTmp
will be valid according to thevalid
rule. - In this specific application, the first of two
valid
goals in eachstep/3
clause isn't necessary since your first argument comes from a previously validated step. (It depends upon how "general" you wantstep/3
to behave).
Beyond that, the solve/1
still produces many duplicate results, but they are all correct and all the correct solutions are discovered.