What's the SLD tree for this query?

后端 未结 3 1139
野的像风
野的像风 2021-01-18 19:10

Let\'s consider the following Prolog program (from \"The Art of Prolog\"):

natural_number(0).
natural_number(s(X)) :- natural_number(X).

plus(X, 0, X) :- na         


        
3条回答
  •  执念已碎
    2021-01-18 19:40

    Many Prolog systems are not as smart as we expect them to be. This has several reasons, mostly because of a tradeoff choice of the implementer. What appears important to some might not be that important to others.

    As a consequence these leftover choicepoints may accumulate in time and prevent to free auxiliary data. For example, when you want to read in a long list of text. A list that is that long that it does not fit into memory at once, but still can be processed efficiently with library(pio).

    If you expect exactly one answer, you might use call_semidet/1 to make it determinate. See this answer for its definition and a use case.

    ?- plus(s(s(s(0))), s(0), Z).
    Z = s(s(s(s(0)))) ;
    false.
    
    ?- call_semidet(plus(s(s(s(0))), s(0), Z)).
    Z = s(s(s(s(0)))).
    

    But you can see it also from a more optimistic side: Modern toplevels (like the one in SWI) do show you when there are leftover choicepoints. So you can consider some countermeasures like call_semidet/1.

    Here are some related answers:

    • implementing last/2

    • how to insert green cuts into append/3

提交回复
热议问题