How to interpret this Prolog goal with a cut, and improve efficiency

…衆ロ難τιáo~ 提交于 2019-12-11 05:15:17

问题


I have been reading through the answers and comments of my previous question and I have tried applying the given explanations on an example from Bratko (Prolog Programming for Artificial Intelligence, p. 130), but I am not sure I understand it completely. The example is described below:

I read the tree and the code as follows:

In the goal list C :- P, Q, R, !, S, T, U. Prolog will one by one try to instantiate the variables, as normal, to eventually get to true.. Let's say that a value is found for P and Q, and the first try on R fails, then Prolog can back track to the case where P and Q were found, and try another option for R if available. However, if R is found as well (leading to P, Q, R = true.), and ! succeeds as it always does, we throw away any choice points and there's nothing to back track to from that point on (not even C :- V.). What this means is that if no results can be found for S, the goal C :- P, Q, R, !, S, T, U. will fail immediately. But Prolog can still backtrack to A :- B, C, D. to find other values for B. If another match is found for B, C will be tried again anew. And so on.

Assuming that my interpretation is correct, if the goal C :- P, Q, R, !, S, T, U. succeeds or fails regardless of the value of B, how would you improve efficiency? My guess would be to re-write A :- B, C, D. as A :- B, !, C, D.

Is my interpretation correct? And what about my improvement in efficiency, given some a-priori information on C?


回答1:


Yes, your understanding is correct. To see it better, we can re-write the predicates as

a = (b & c & d)
c = (p & q & r) ~~>! (s & t & u) ; v

with & for &&:, and the rest of operators, from this answer (or if it isn't clear, see this as a pseudocode, with ~~>! passing no more than one solution through). When the cut is reached, c is committed, but a is still backtrackable.

If C in A :- B, C, D. succeeds or fails regardless of the value of B, you can also reorder the goals as

A :- C, B, D.

The cut in A :- B, !, C, D. is a red cut, it only lets B succeed once, but what if you're interested in its second result? Red cuts alter the meaning of a predicate.



来源:https://stackoverflow.com/questions/40994026/how-to-interpret-this-prolog-goal-with-a-cut-and-improve-efficiency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!