successor-arithmetics

How does prolog run through recursive queries using succ?

断了今生、忘了曾经 提交于 2019-12-05 15:09:53
Can someone explain to me why this prolog query works the way it does. The definition is: add(0,Y,Y). add(succ(X),Y,succ(Z)):- add(X,Y,Z). Given this: ?- add(succ(succ(succ(0))), succ(succ(0)), R). Heres the trace of the query: Call: (6) add(succ(succ(succ(0))), succ(succ(0)), R) Call: (7) add(succ(succ(0)), succ(succ(0)), _G648) Call: (8) add(succ(0), succ(succ(0)), _G650) Call: (9) add(0, succ(succ(0)), _G652) Exit: (9) add(0, succ(succ(0)), succ(succ(0))) Exit: (8) add(succ(0), succ(succ(0)), succ(succ(succ(0)))) Exit: (7) add(succ(succ(0)), succ(succ(0)), succ(succ(succ(succ(0))))) Exit:

Dividing two integers with an alternative way

两盒软妹~` 提交于 2019-12-04 06:41:55
问题 Let's consider that n=s(s(...s(0)...)) (simply n= s^n(0)). How could write a program calculating the division of two integers? I mean s^(n//m) (thats the definition of the division) . Any ideas? For example, if we had the question: ?-divide(s(s(s(s(0)))),s(0),D). i have written the following code: nat(0). nat(s(X)) :- nat(X). divide(0,_,D) :- D is 0. divide(s(X),s(Y),D) :- divide(X,Y,D). 回答1: Your predicate divide/3 assumes wrongly that the following equation holds when x and y are numbers:

What does the s() predicate do in Prolog?

孤人 提交于 2019-12-04 04:40:14
I have been trying to learn Prolog, and am totally stumped on what the predicate s() does. I see it used often and there is so little resources on the internet about Prolog that I cannot find an answer. Ex. /* sum(Is,S) is true if S is the sum of the list of integers Is. */ sum([],0). sum([0|Is],S):-sum(Is,S). sum([s(I)|Is], s(Z) ):-sum([I|Is],Z). s/1 does not do anything in itself, and it's not really a predicate. They are just terms, a representation of the successor of their argument. So, s(0) is used to represent the successor of 0 (i.e. 1 ), s(s(0)) is used to represent the successor of s

Dividing two integers with an alternative way

独自空忆成欢 提交于 2019-12-02 11:40:14
Let's consider that n=s(s(...s(0)...)) (simply n= s^n(0)). How could write a program calculating the division of two integers? I mean s^(n//m) (thats the definition of the division) . Any ideas? For example, if we had the question: ?-divide(s(s(s(s(0)))),s(0),D). i have written the following code: nat(0). nat(s(X)) :- nat(X). divide(0,_,D) :- D is 0. divide(s(X),s(Y),D) :- divide(X,Y,D). Your predicate divide/3 assumes wrongly that the following equation holds when x and y are numbers: (x-1)/(y-1) = x/y A counter-example is: (16-1)/(4-1) = 5 is different from 16/4 = 4 It seems you are trying

Too much backtracking: why is there a “redo” here?

送分小仙女□ 提交于 2019-12-02 03:06:46
问题 I'm doing a very simple exercise in Prolog and there's something I don't understand in the trace. The program is a "greater than" ( > ) on integers represented as successors: greater_than(succ(_), 0). greater_than(succ(A), succ(B)) :- greater_than(A, B). My problem: I don't understand why the request greater_than(succ(succ(succ(0))),succ(0)) generates a redo in the following trace: [trace] ?- greater_than(succ(succ(succ(0))),succ(0)). Call: (6) greater_than(succ(succ(succ(0))), succ(0)) ?

Too much backtracking: why is there a “redo” here?

冷暖自知 提交于 2019-12-01 23:02:22
I'm doing a very simple exercise in Prolog and there's something I don't understand in the trace. The program is a "greater than" ( > ) on integers represented as successors: greater_than(succ(_), 0). greater_than(succ(A), succ(B)) :- greater_than(A, B). My problem: I don't understand why the request greater_than(succ(succ(succ(0))),succ(0)) generates a redo in the following trace: [trace] ?- greater_than(succ(succ(succ(0))),succ(0)). Call: (6) greater_than(succ(succ(succ(0))), succ(0)) ? creep Call: (7) greater_than(succ(succ(0)), 0) ? creep Exit: (7) greater_than(succ(succ(0)), 0) ? creep

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

Reversible tree length relation

不想你离开。 提交于 2019-12-01 05:24:57
I'm trying to write reversible relations in "pure" Prolog (no is , cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic relations (add,mult,equal,less,...) which we must use to create those relations. Right now I'm trying to understand how to create reversible functions by creating the relation tree(List,Tree) which is true if List is the list of the leaves of the binary tree Tree . To achieve such a thing I'm trying to create the tree_size(Tree,N) relation which is

Natural number in SWI-prolog & recursive procedure

偶尔善良 提交于 2019-12-01 00:29:18
I have the next procedure for natural number is SWI-prolog: natural_number(0). natural_number(s(X)) :- natural_number(X). Now I want to do a recursive call, that stop when we arrive to 0. My natural number is represented by - s(0)=0, s(s(0))=1, s(s(s(0)))=2, etc So I define: recommend(A, B, natural_number(0)) :- dosomeFINITEfunction (a,b). recommend(a,b,mynumber):- dosomeFINITEfunction(a,b), recommend (a,b, natural_number(mynumber)). and call with: 3,5,s(0). But it gives me the error: out of local stack . What is the problem? Thank you. natural_number(s(0), 0). natural_number(s(s(X)), N) :-