What's the SLD tree for this query?

后端 未结 3 1140
野的像风
野的像风 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条回答
  •  梦毁少年i
    2021-01-18 19:44

    The problem is not directly related to SLD trees, since Prolog systems do not build SLD trees in a look-ahead manner as you describe it. But some optimizations found in certain Prolog systems essentially have this effect and change the blind brute force head matching. Namely indexing and choice point elimination.

    There is now a known limitation of SWI-Prolog. Although it does multi-argument indexing, it does not do choice point elimination for non-first argument indexes cascaded indexing. Means it only picks one argument, but then no further. There are some Prolog systems that do multi argument indexing and cascaded indexing. For example in Jekejeke Prolog we do not have No/false:

    Peano Cascade

    Bye

    P.S.: The newest version of Jekejeke Prolog even does not literally cascade, since it detects that the first argument index has no sensitivity. Therefore although it builds the index for the first argument due to the actual call pattern, it skips the first argument index and does not use it, and solely uses the second argument. Skipping gives a little speed. :-)

    The skipping is seen via the dump/1 command of the development environment version:

    ?- dump(plus/3).
    -------- plus/3 ---------
    length=2
    arg=0
      =length=2
    arg=1
      0=length=1
      s=length=1
    Yes
    

    So it has not decended into arg=0 and built an arg=1 index there, but instead built in parallel an arg=0 and an arg=1 index. We might still call this heuristic cascading since individual queries lead to multiple indexes, but they have not really the shape of a cascade.

提交回复
热议问题