%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%					%
% Artificial Intelligence Project	%
% Fall 1999				%
%					%
% by Margit Lang			%
% 					%
%                                    	%
%                                    	%
% A General Purpose Solver           	%
%					%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




% A GPS-like problem solver

%   plan( State, Goals, Actions, FinalState)

plan( State, Goals, [], State)  :-
  satisfied( State, Goals).

plan( State, Goals, Plan, FinalState)  :-
  conc( PrePlan, [Action | PostPlan], Plan),
  select( State, Goals, Goal),      % Select an unsatisfied goal
  achieves( Action, Goal),
  can( Action, Condition),
  plan( State, Condition, PrePlan, MidState1),
  apply( MidState1, Action, MidState2),
  plan( MidState2, Goals, PostPlan, FinalState).    % Assemble whole plan

satisfied( State, []).

satisfied( State, [Goal | Goals])  :-
  member( Goal, State),
  satisfied( State, Goals).

select( State, Goals, Goal)  :-
  member( Goal, Goals),
  \+ member( Goal, State).

achieves( Action, Goal)  :-
  add( Action, Goals),
  member( Goal, Goals).

apply( State, Action, NewState)  :-
  del( Action, DelList),
  delete( State, DelList, State1), !,
  add( Action, AddList),
  conc( AddList, State1, NewState).

conc( [], L, L).

conc( [X | L1], L2, [X | L3])  :-
  conc( L1, L2, L3).

member(X,[]) :- !, fail.
member(X,[X|_]).
member(X,[_|L]) :- member(X,L).

delete([],_,[]).

delete( [X|L1],L2,Diff)  :-
  member(X,L2),  !,
  delete(L1,L2,Diff).

delete( [X|L1],L2, [X|Diff])  :-
  delete(L1,L2,Diff).

