% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % CARTONS.PL % This is a set of production rules for a robot that % stacks cartons in a warehouse. The rules have been % translated into Prolog rules that can be used with % the forward-chaining inference engin in FCHAIN.PL. :- dynamic f/1, g/1, rule/1. :- multifile f/1, g/1, rule/1. rule(1) :- g(stack([X,Y|Rest])), f(supports(X,Y)), then, rg(stack([X,Y|Rest])), ag(stack([Y|Rest])). rule(2) :- g(stack([X,Y|Rest])), \+ f(supports(X,_)), \+ f(supports(Y,_)), f(supports(Z,Y)), then, rf(supports(Z,Y)), af(supports(X,Y)), rg(stack([X,Y|Rest])), ag(stack([Y|Rest])). rule(3) :- g(stack([X,Y|Rest])), f(supports(X,Z)), Y \== Z, \+ g(remove(Z)), then, ag(remove(Z)). rule(4) :- g(stack([_,X|Rest])), f(supports(X,Y)), \+ g(remove(Y)), then, ag(remove(Y)). rule(5) :- g(remove(X)), f(supports(X,Y)), \+ g(remove(Y)), then, ag(remove(Y)). rule(6) :- g(remove(X)), \+ f(supports(X,_)), f(supports(Y,X)), then, rf(supports(Y,X)), af(supports(floor,X)), rg(remove(X)). rule(7) :- g(stack([_])), then, rg(stack([_])). % initial facts and goals for the carton-stacking robot f(supports(floor,a)). f(supports(floor,b)). f(supports(b,c)). f(supports(c,d)). g(stack([a,b,c,d])).