% Blocks world described in Sections 3.6 and 3.9 of

%  Esra Erdem, "Theory and applications of answer set programming," 
%  Ph.D. Thesis. Technical Report CS-TR-02-69, Department of Computer 
%  Sciences, University of Texas at Austin, 2002.

% effect of moving a block
on(B,L,T1) :- block(B), location(L), move(B,L,T), next(T,T1).

% a block can be moved only when it's clear 
:- location(L), block(B), block(B1), time(T), 
   move(B,L,T), on(B1,B,T).

% any two blocks cannot be on the same block at the same time
:- 2{on(B1,B,T):block(B1)}, time(T), block(B). 

% wherever a block is, it's not anywhere else
-on(B,L1,T) :- time(T), location(L1), location(L), block(B), 
on(B,L,T), not eq(L,L1).  
  
% every block is supported by the table
supported(B,T) :- block(B), time(T), on(B,table,T).
supported(B,T) :- block(B), block(B1), time(T), on(B,B1,T),
                  supported(B1,T), not eq(B,B1).
:- block(B), time(T), not supported(B,T).   

% no concurrency
:- 2{move(B,L,T):block(B):location(L)},time(T). 

% inertia
on(B,L,T1) :- location(L), block(B), on(B,L,T), 
not -on(B,L,T1), next(T,T1).

% initial values and actions are exogenous
on(B,L,0) | -on(B,L,0) :- block(B), location(L).

{move(B,L,T)} :- block(B), location(L), time(T), T < lasttime.

% auxiliary predicates
time(0..lasttime).
next(T,T+1) :- time(T), lt(T,lasttime). 

location(L) :- block(L).
location(table).

hide.
show move(B,L,T).
