나는 프롤로그를 가르치려고하고있다. 아래, 나는 무차별 그래프에서 노드 사이의 모든 경로를 반환해야한다고 생각하는 코드를 작성했습니다 ... 그러나 그렇지 않습니다. 왜이이 특정 코드가 작동하지 않는지 이해하려고합니다. (이 질문은 비슷한 프롤로그 길 찾기 게시물과 구분됩니다). 나는 이것을 SWI-Prolog에서 운영하고있다. 모든 단서?프롤로그의 길 찾기
% Define a directed graph (nodes may or may not be "room"s; edges are encoded by "leads_to" predicates).
room(kitchen).
room(living_room).
room(den).
room(stairs).
room(hall).
room(bathroom).
room(bedroom1).
room(bedroom2).
room(bedroom3).
room(studio).
leads_to(kitchen, living_room).
leads_to(living_room, stairs).
leads_to(living_room, den).
leads_to(stairs, hall).
leads_to(hall, bedroom1).
leads_to(hall, bedroom2).
leads_to(hall, bedroom3).
leads_to(hall, studio).
leads_to(living_room, outside). % Note "outside" is the only node that is not a "room"
leads_to(kitchen, outside).
% Define the indirection of the graph. This is what we'll work with.
neighbor(A,B) :- leads_to(A, B).
neighbor(A,B) :- leads_to(B, A).
IFF의 A -> B -> C -> D가
path(A, D, [B, C])
참이어야 후, 루프 - 프리 경로이다. 즉, 세 번째 인수는 중간 노드를 포함합니다. 그러나
% Base Rule (R0)
path(X,Y,[]) :- neighbor(X,Y).
% Inductive Rule (R1)
path(X,Y,[Z|P]) :- not(X == Y), neighbor(X,Z), not(member(Z, P)), path(Z,Y,P).
,
?- path(bedroom1, stairs, P).
은 false입니다. 왜? 우리는
?- neighbor(bedroom1, hall).
true.
?- not(member(hall, [])).
true.
?- path(hall, stairs, []).
true .
이후
X = bedroom1
Y = stairs
Z = hall
P = []
과 R1에 일치하지할까요? 사실
, 내가
?- path(A, B, P).
을 평가하는 경우가 난 단지 길이-1 솔루션을 얻을.