prolog-cut

Finding query for which a prolog program gives incorrect result

↘锁芯ラ 提交于 2019-12-02 06:02:04
问题 This Prolog program defines the third argument to be the maximum value of the first two numeric arguments: max(X, Y, X) :- X >= Y, !. max(X, Y, Y). I think that this program works just fine. But I am told that it can give incorrect result. Can you tell when and why? 回答1: This is a textbook example. ?- max(5,1,1). true. Homework: Why is the program wrong? How do we make the program correct? EDIT max(X, Y, X) :- X >= Y, !. max(X, Y, Y). Our intention is to say: If X is greater than Y, then Max

Finding query for which a prolog program gives incorrect result

爱⌒轻易说出口 提交于 2019-12-02 02:43:24
This Prolog program defines the third argument to be the maximum value of the first two numeric arguments: max(X, Y, X) :- X >= Y, !. max(X, Y, Y). I think that this program works just fine. But I am told that it can give incorrect result. Can you tell when and why? This is a textbook example. ?- max(5,1,1). true. Homework: Why is the program wrong? How do we make the program correct? EDIT max(X, Y, X) :- X >= Y, !. max(X, Y, Y). Our intention is to say: If X is greater than Y, then Max is X. Otherwise , Max must be Y. Instead, what is say is: When the first and third arguments (X and Max) can

What's the SLD tree for this query?

我是研究僧i 提交于 2019-12-01 18:53:47
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) :- natural_number(X). plus(X, s(Y), s(Z)) :- plus(X, Y, Z). and the query: ?- plus(s(s(s(0))), s(0), Z). Both SICStus and SWI produce the expected Z = s(s(s(s(0)))) answer, but query the user for the next answer (a correct no / false answer). However, I cannot understand why there is an open branch in the SLD tree after the only goal is found. I tried debugging both under SICStus and under SWI, but I am not really able to interpret the result yet.

What's the SLD tree for this query?

99封情书 提交于 2019-12-01 18:29:12
问题 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) :- natural_number(X). plus(X, s(Y), s(Z)) :- plus(X, Y, Z). and the query: ?- plus(s(s(s(0))), s(0), Z). Both SICStus and SWI produce the expected Z = s(s(s(s(0)))) answer, but query the user for the next answer (a correct no / false answer). However, I cannot understand why there is an open branch in the SLD tree after the only goal is found. I

Why do we use '!' in prolog

匆匆过客 提交于 2019-11-29 18:25:24
This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :- p(X,Y,Z). What is the use of '!' and predicate p(,,) in the above code. OR Can anybody just add comments in every step of the above code so that i can able to understand . Thanks. mat Beginners tend to use !/0 because they are not aware of its negative consequences. This is because most Prolog textbooks that are popular among beginners are quite bad and

Implementing cut in tracing meta interpreter prolog

天大地大妈咪最大 提交于 2019-11-29 10:15:48
I have this tracing meta interpreter, altered from previous question Prolog unbind bound variable . I don't understand how to interpret cut. Thanks to user @false who told me that the cut is badly implemented, my question is, how should I implement cut in this meta-interpreter? %tracer mi_trace(Goal):- mi_trace(Goal, 0). mi_trace(V, _):- var(V), !, throw(error(instantiation_error, _)). mi_trace(true, _Depth):-!, true. mi_trace(fail, _Depth):-!, fail. mi_trace(A > B, _Depth):-!, A > B. mi_trace(A < B, _Depth):-!, A < B. mi_trace(A =< B, _Depth):-!, A =< B. mi_trace(A >= B, _Depth):-!, A >= B.

Why do we use '!' in prolog

爷,独闯天下 提交于 2019-11-28 14:16:53
问题 This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :- p(X,Y,Z). What is the use of '!' and predicate p(,,) in the above code. OR Can anybody just add comments in every step of the above code so that i can able to understand . Thanks. 回答1: Beginners tend to use !/0 because they are not aware of its negative

What are the optimal green cuts for successor arithmetics sum?

旧巷老猫 提交于 2019-11-28 07:42:31
问题 To grok green cuts in Prolog I am trying to add them to the standard definition of sum in successor arithmetics (see predicate plus in What's the SLD tree for this query?). The idea is to "clean up" the output as much as possible by eliminating all useless backtracks (i.e., no ... ; false ) while keeping identical behavior under all possible combinations of argument instantiations - all instantiated, one/two/three completely uninstantiated, and all variations including partially instantiated

Implementing cut in tracing meta interpreter prolog

一曲冷凌霜 提交于 2019-11-28 03:26:43
问题 I have this tracing meta interpreter, altered from previous question Prolog unbind bound variable. I don't understand how to interpret cut. Thanks to user @false who told me that the cut is badly implemented, my question is, how should I implement cut in this meta-interpreter? %tracer mi_trace(Goal):- mi_trace(Goal, 0). mi_trace(V, _):- var(V), !, throw(error(instantiation_error, _)). mi_trace(true, _Depth):-!, true. mi_trace(fail, _Depth):-!, fail. mi_trace(A > B, _Depth):-!, A > B. mi_trace

Prolog append with cut operator

随声附和 提交于 2019-11-27 08:56:25
What problem can occur when we use append with cut operator? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL). I have tried several different inputs, but it always succeeds. ?- append2([1,2],[5],L). L = [1, 2, 5]. ?- append2([1,2],[1,2],L). L = [1, 2, 1, 2]. ?- append2([],[1,2],L). L = [1, 2]. ?- append2([1,2],[],L). L = [1, 2]. There are two kinds of cuts ; green cuts and red cuts. Green cuts are inserted just to improve efficiency and don't change the semantics of the program. Red cuts, on the other hand, do. By definition, green cuts do not cause any problems. So, is there any