在序言中有一个 "out of global stack"

Having a "out of global stack" in prolog

大家好,我有一个关于 Prolog 的简单问题。

%on(Block,Object).
% clear(Object).
block(b1).
block(b2).
block(b3).

place(p1).
place(p2).
place(p3).
place(p4).
state1([clear(p2),clear(p4),clear(b2),clear(b3),on(b1,p1),on(b2,p3),on(b3,b1)]).
% visual state1
% b3
% b1    b2
% =  =  =  =
% 1  2  3  4     <----Positions
% can(Action,Condition).
% adds(Action,AddRelationship).
% deletes(Action,DeleteRelationship).
% move(Block,From,To).
can( move( Block, From, To), [ clear( Block), clear( To), on( Block, From)]) :-
  block( Block),      % Block to be moved
  object( To),           % "To" is a block or a place
  To \== Block,          % Block cannot bå moved to itself
  object( From),         % "From" is a block or a place
  From \== To,           % Move to new position
  Block \== From.        % Block not moved from itself
adds(move(X,From,To),[on(X,To),clear(From)]).
deletes(move(X,From,To),[on(X,From),clear(To)]).
object(X):-
      place(X)
      ;   
      block(X).
% plan(State,Goals,Plan,FinalState).
plan(State,Goals,[],State):-
    satisfied(State,Goals).
plan(State,Goals,Plan,FinalState) :-
    append(PrePlan,[Action|PostPlan],Plan),
    select(State,Goals,Goal),
    achieves(Action,Goal),
    can(Action,Condition),
    plan(State,Condition,PrePlan,MidState1),
    apply(MidState1,Action,MidState2),
    plan(MidState2,Goals,PostPlan,FinalState).
% satisfied(State,[]).
satisfied(State,[Goal|Goals]):-
    member(Goal,State),
    satisfied(State,Goals).
select(State,Goals,Goal):-
    member(Goal,Goals),
    not(member(Goal,State)).
achieves(Action,Goal):-
    adds(Action,Goals),
    member(Goal,Goals).
apply(State,Action,NewState):-
    deletes(Action,DelList),
    delete_all(State,DelList,State1),!,
    adds(action,AddList),
    append(AddList,State1,NewState).
delete_all([],_,[]).
delete_all([X|L1],L2,Diff):-
    member(X,L2),!,
    delete_all(L1,L2,Diff).
delete_all([X|L1],L2,[X|Diff]):-
    delete_all(L1,L2,Diff).

在编译器中 运行 之后它说它没有问题但是当我尝试执行命令时 plan(state1,on(b1,b2),Plan,FinalState). 它只是说出全局堆栈。谁能帮我解决这个问题

你只需要看这个:

plan(State,Goals,[],State):- false,
    satisfied(State,Goals).
plan(State,Goals,Plan,FinalState) :-
    append(PrePlan,[Action|PostPlan],Plan), false,
    select(State,Goals,Goal),
    achieves(Action,Goal),
    can(Action,Condition),
    plan(State,Condition,PrePlan,MidState1),
    apply(MidState1,Action,MidState2),
    plan(MidState2,Goals,PostPlan,FinalState).

?- plan(state1,on(b1,b2),Plan,FinalState).

由于该程序已经循环,因此相同的程序将循环并删除额外的 false 目标。你需要先解决这个问题。有关更多信息,请参阅