:- use_module(library(lists)). plan(State,Goal,Plan) :- plan(State,Goal, [], Plan). plan(State, Goal, Sol , Plan) :- subset(Goal, State), reverse(Sol, Plan). plan(State, Goal, Current, Plan) :- action(A, Pre, Del, Add), subset(Pre, State), maplist(dif(A), Current), subtract(State, Del, State1), append(Add, State1, NewState), plan(NewState, Goal, [A | Current], Plan). % action(action, preconditions, delete list, add list) action(drive(X,Y), [at(X)], [at(X)], [at(Y), vis(Y)]) :- rel(X,Y). con(perth, adelaide). con(darwin, adelaide). con(adelaide, sidney). con(sidney, brisbane). % symmetric closure rel(X,Y) :- con(X,Y). rel(Y,X) :- con(X,Y). solve(P) :- plan([at(sidney), vis(sidney)], [at(sidney), vis(sidney), vis(adelaide), vis(brisbane), vis(perth), vis(darwin)], P).