问题
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