0
我有以下PROLOG查詢,它是數據庫。如何在PROLOG中回溯
r(X,Y), s(Y,Z), not(r(Y,X)), not(s(Y,Y).
r(a,b). r(a,c). r(b,a). r(a,d).
s(b,c). s(b,d). s(c,c). s(d,e).
PROLOG在此示例中如何回溯?我想它會是這樣的:
1- Unifies X with 'a' and Y with 'b'
2- Unifies Y with 'b' and Z with 'c'
3- This goal means that there mustn't be any other clause in the database where
this happens: r(a,b) r(b,a).
My doubt lies here. Does prolog advance to the next goals or does it verify
the other r(X,Y) database clauses to check for a match and possibly invalidate
the solution?
I guess that what Prolog does is the following:
- Verifies the rest of the r(X,Y) clauses to check for a r(Y,X) match and if
there is one, then it backtracks to the 2nd step (s(Y,Z)).
This will obviously generate unnecessary tree branches which do not need to be
tested since the 1st goal is always the same.
4. Verifies if S(X,Y), X == Y. Backtracks to step 1 with new values (a & c) and so on.
我是否正確?如果有人能根據這個問題繪製一棵樹,我真的會感到厭煩,所以我可以完全理解它。
這對分析回溯,ty非常有用! – Khabz