% Situation Calculus Representation of Blocks World.
%
%   Normal Prolog execution suffices to find plans as long as the first
%   goal in query is sit/1.
%
%   Example:  Find a situation with c on b on a.
%
%      ?- sit(S), on(b,a,S), on(c,b,S), extract_plan(S,P).
%      S = [puton(c, b), puton(b, a), puton(a, table)],
%      P = [puton(a, table), puton(b, a), puton(c, b)] 

% initial state a is on b other blocks are on the table.
on(a,b,[]).
on(b,table,[]).
on(c,table,[]).
on(d,table,[]).         
on(e,table,[]).
on(f,table,[]).
on(g,table,[]).

% situation axioms defining "on" fluent
on(X,Y, [puton(X,Y)|_]).
on(X,Y, [puton(A,_)|S]) :-
	diff(X,A),
	on(X,Y,S).

clear(table, _).
clear(B1, S) :- block(B1), \+ on(_, B1, S).

% poss(Action, Situation) insures that the preconditions of the
%   Action hold in Situation.
poss(puton(A,table),S) :-
	block(A),
	clear(A,S),
	\+on(A,table,S).
poss(puton(A,B),S) :-
	block(A),
	block(B),
	A \= B,
	clear(A,S),
	clear(B,S).

% diff(X,Y) holds when X and Y are distinct objects.
diff(X,Y) :-
	object(X),
	object(Y),
	X \= Y.

sit([]).
sit([A|S]) :- sit(S), poss(A,S). 

object(table).
object(B) :- block(B).

block(a). block(b). block(c). block(d). block(e). block(f). block(g).

% extract_plan(Situation, Plan). Plan is the ordered list of actions that
%    produced Situation.
extract_plan(Sit,Plan) :- reverse(Sit, Plan).



