Information Technology Reference
In-Depth Information
Figure 7.4.
Using the list version of the blocks world program
?- left(b1,b7).
Call: (7) left(b1, b7)
Call: (8) scene(_L205)
% Get the scene.
Exit: (8) scene([[b1, b2], [b3, b4, b5, b6], [b7]])
% Generate a first pair of stacks.
Call: (8) before(_L206, _L207, ...)
Exit: (8) before([b1, b2], [b3, b4, b5, b6],...)
Call: (8) member(b1, [b1, b2])
% Test if X is in the first.
Exit: (8) member(b1, [b1, b2])
% YES
Call: (8) member(b7, [b3, b4, b5, b6])
% Test if Y is in the second.
Fail: (8) member(b7, [b3, b4, b5, b6])
%
NO
Redo: (8) member(b1, [b1, b2])
Fail: (8) member(b1, [b1, b2])
% Generate another pair of stacks.
Redo: (8) before(_L206, _L207, ...)
Exit: (8) before([b1, b2], [b7], ...)
Call: (8) member(b1, [b1, b2])
% Test if X is in the first.
Exit: (8) member(b1, [b1, b2])
%
YES
Call: (8) member(b7, [b7])
% Test if Y is in the second.
Exit: (8) member(b7, [b7])
%
YES
Exit: (7) left(b1, b7)
% The second pair works.
Yes
7.3.1 The blocks world revisited
The predicates
member
and
append
can be used to define a much more succinct version
of the blocks-world program (see section 4.2).
Here is the idea. An entire blocks-world
scene
can be represented by a left-to-right
list of stacks, where a
stack
is represented by a top-to-bottom list of blocks. So the
scene depicted in figure 4.1 is represented by the following list:
[[b1,b2],[b3,b4,b5,b6],[b7]]
The predicates
above
and
left
can then be defined using the
before
predicate:
A block
X
is above a block
Y
in the scene if there is a stack in the scene where
X
appears before
Y
in the stack.
A block
X
is somewhere to the left of a block
Y
if there is a stack in the scene that
contains
X
that is before a stack in the scene that contains
Y
.
The full blocks-world program is shown in figure 7.3. A trace of the
left
predicate is
shown in figure 7.4.