Does Prolog use Eager Evaluation?

前端 未结 4 840
离开以前
离开以前 2021-01-12 04:42

Because Prolog uses chronological backtracking(from the Prolog Wikipedia page) even after an answer is found(in this example where there can only be one solution), would thi

4条回答
  •  借酒劲吻你
    2021-01-12 05:12

    As it stands, the question is not correct in what it states. Chronological backtracking does not mean that Prolog will necessarily backtrack "in an example where there can be only one solution".

    Consider this:

    foo(a, 1).
    foo(b, 2).
    foo(c, 3).
    
    ?- foo(b, X).
    X = 2.
    
    ?- foo(X, 2).
    X = b.
    

    So this is an example that does have only one solution and Prolog recognizes that, and does not attempt to backtrack. There are cases in which you can implement a solution to a problem in a way that Prolog will not recognize that there is only one logical solution, but this is due to the implementation and is not inherent to Prolog's execution model.

    You should read up on Prolog's execution model. From the Wikipedia article which you seem to cite, "Operationally, Prolog's execution strategy can be thought of as a generalization of function calls in other languages, one difference being that multiple clause heads can match a given call. In that case, [emphasis mine] the system creates a choice-point, unifies the goal with the clause head of the first alternative, and continues with the goals of that first alternative." Read Sterling and Shapiro's "The Art of Prolog" for a far more complete discussion of the subject.

提交回复
热议问题