successor-arithmetics

Prolog predicate - infinite loop

喜你入骨 提交于 2019-11-28 13:47:18
I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? This happends because the of evaluation order of pow2. If you switch the order of pow2, you'll have the first example stuck in infinite loop.. So you can check first if Y is a

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

Prolog predicate - infinite loop

ε祈祈猫儿з 提交于 2019-11-27 07:54:31
问题 I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? 回答1: This happends because the of evaluation order of pow2. If you switch

Better termination for s(X)-sum

混江龙づ霸主 提交于 2019-11-27 07:48:14
问题 (Let me sneak that in within the wave of midterm questions.) A common definition for the sum of two natural numbers is nat_nat_sum/3 : nat_nat_sum(0, N, N). nat_nat_sum(s(M), N, s(O)) :- nat_nat_sum(M, N, O). Strictly speaking, this definition is too general, for we have now also success for ?- nat_nat_sum(A, B, unnatural_number). Similarly, we get the following answer substitution: ?- nat_nat_sum(0, A, B). A = B. We interpret this answer substitution as including all natural numbers and do

Prolog successor notation yields incomplete result and infinite loop

一曲冷凌霜 提交于 2019-11-26 11:51:47
I start to learn Prolog and first learnt about the successor notation. And this is where I find out about writing Peano axioms in Prolog. See page 12 of the PDF : sum(0, M, M). sum(s(N), M, s(K)) :- sum(N,M,K). prod(0,M,0). prod(s(N), M, P) :- prod(N,M,K), sum(K,M,P). I put the multiplication rules into Prolog. Then I do the query: ?- prod(X,Y,s(s(s(s(s(s(0))))))). Which means finding the factor of 6 basically. Here are the results. X = s(0), Y = s(s(s(s(s(s(0)))))) ? ; X = s(s(0)), Y = s(s(s(0))) ? ; X = s(s(s(0))), Y = s(s(0)) ? ; infinite loop This result has two problems: Not all results

Prolog successor notation yields incomplete result and infinite loop

放肆的年华 提交于 2019-11-26 02:37:46
问题 I start to learn Prolog and first learnt about the successor notation. And this is where I find out about writing Peano axioms in Prolog. See page 12 of the PDF: sum(0, M, M). sum(s(N), M, s(K)) :- sum(N,M,K). prod(0,M,0). prod(s(N), M, P) :- prod(N,M,K), sum(K,M,P). I put the multiplication rules into Prolog. Then I do the query: ?- prod(X,Y,s(s(s(s(s(s(0))))))). Which means finding the factor of 6 basically. Here are the results. X = s(0), Y = s(s(s(s(s(s(0)))))) ? ; X = s(s(0)), Y = s(s(s