Does SLD tree exist if the query is invalid in Prolog?

自闭症网瘾萝莉.ら 提交于 2020-01-25 10:14:02

问题


Considering the following Prolog program :

reverse_bits([1], [0]).
reverse_bits([0], [1]).
reverse_bits([H|T], [0|R]) :- H==1, reverse_bits(T, R).
reverse_bits([H|T], [1|R]) :- H==0, reverse_bits(T, R).

What's the SLD tree for the question above ?

reverse_bits(Input, [1, 0, 0]).?

It's not valid so does it even exist a SLD tree ?


回答1:


Here is a minimal example using the sldnfdraw package. First, generate a program and query using the syntax specified in the library documentation:

% estamos.pl

:- use_module(library(sldnfdraw)).
:- sldnf.

:- begin_program.

reverse_bits([1], [0]).
reverse_bits([0], [1]).
reverse_bits([H|T], [0|R]) :- H==1, reverse_bits(T, R).
reverse_bits([H|T], [1|R]) :- H==0, reverse_bits(T, R).

:- end_program.

:- begin_query.

reverse_bits(Input,[1,0,0]).

:- end_query.

Next, generate a .tex file for the relevant resolution tree:

?- draw_goal('estamos-tree.tex').

You can run this goal from the command line. Finally, include this file in a .tex document

% estamos-tree-draw.tex

\documentclass{article}
\usepackage{epic,eepic}
\usepackage{ecltree}
\begin{document}
\input{estamos-tree}
\end{document}

and compile with

$ latex estamos-tree-draw.tex
$ dvipdf estamos-tree-draw.dvi

There should be a .pdf file in your source folder containing the resolution tree.

Code improvement

For what it's worth I would suggest writing your program as:

reverse_bits([],[]).
reverse_bits([0|T],[1|R]) :- reverse_bits(T,R).
reverse_bits([1|T],[0|R]) :- reverse_bits(T,R).

for simplicity and to avoid using == in place of unification, as false noted in the comments.

Output when using = (unification)

Here we use = unification in the "guard part" testing H.

Output when using == (term equivalence)

When using term equivalence == we are done really quickly because there is failure at the first call:

Because the only clause that matches is reverse_bits([H|T], [1|R]) :- H==0, reverse_bits(T, R). and at that point, H is a fresh variable and in no way equivalent to 0.



来源:https://stackoverflow.com/questions/59685831/does-sld-tree-exist-if-the-query-is-invalid-in-prolog

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