Information Technology Reference
In-Depth Information
Figure 9.5.
Generating three-coins plans
?- plan([M1,M2,M3]).
M1 = flip_left
M2 = flip_left
M3 = flip_right
;
M1 = flip_left
M2 = flip_right
M3 = flip_left
middle, or the right coins. The first clause says, If I start in any state of the world
[ x , y , z ] and flip the leftmost coin, I will end up in a state [ x , y , z ] that is the same
as before except that x will be the opposite of x (where opposite is defined by the
last two clauses). The other two flips are analogous.
Now load both the general planner (figure 9.3) and the three-coins program
(figure 9.4) into Prolog. Figure 9.5 shows a query to the plan predicate asking for
a plan with precisely three moves. It also shows the first two solutions found by the
general planner.
9.2.3 Atoms as terms in Prolog
This section takes a small digression to consider a feature of Prolog that has not been
studied yet but that will simplify matters considerably.
So far, Prolog terms have been identified as variables, numbers, lists, or constants.
But, in fact, Prolog terms are variables, numbers, lists, or atoms . A constant as a term
is merely a special case of an atom where the predicate has no arguments and no
parentheses. (This aspect of Prolog was not discussed earlier, but at this stage the
distinction is no longer necessary.) So queries can be the following:
?- X=p, Y=q(a).
% X is a constant, but Y is an atom.
X = p, Y = q(a)
?- Y=p(a), Z=[a,Y,b].
% Atoms can appear in lists.
Y = p(a), Z = [a, p(a), b]
?- Y=p(W,W), p(a,_)=Y, Z=q(Y)
% Atoms can be nested.
Y = p(a,a),
W = a,
Z = q(p(a,a))
This feature of Prolog is used for moves. To previous moves like flip_middle one can
now add moves represented by atoms like go(loc2) .
 
Search WWH ::




Custom Search