successor-arithmetics

`less/2` relation in Peano arithmetic

眉间皱痕 提交于 2021-02-16 14:48:07
问题 This less-than predicate in Peano arithmetic less(0, s(_)). less(s(X), s(Y)) :- less(X, Y). loops when ?- less(X, Y), X=s(0), Y=0. Is there a better way to write less/2 (using Horn clauses only)? 回答1: You can use when/2 . Making it not anymore an infinitely enumerating predicate and still keeping it 100% pure. The when/2 modifies the S (selection rule) in SLD-Resolution, an idea that can be traced back to Alain Colmerauer. less(X, Y) :- when((nonvar(X),nonvar(Y)), less2(X,Y)). less2(0, s(_)).

Reversible tree length relation

点点圈 提交于 2020-01-11 05:25:14
问题 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

What does the s() predicate do in Prolog?

耗尽温柔 提交于 2019-12-21 11:55:31
问题 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). 回答1: 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.

What does the s() predicate do in Prolog?

匆匆过客 提交于 2019-12-21 11:55:07
问题 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). 回答1: 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.

Convert peano number s(N) to integer in Prolog

邮差的信 提交于 2019-12-19 17:47:13
问题 I came across this natural number evaluation of logical numbers in a tutorial and it's been giving me some headache: natural_number(0). natural_number(s(N)) :- natural_number(N). The rule roughly states that: if N is 0 it's natural, if not we try to send the contents of s/1 back recursively to the rule until the content is 0 , then it's a natural number if not then it's not. So I tested the above logic implementation, thought to myself, well this works if I want to represent s(0) as 1 and s(s

Natural number in SWI-prolog & recursive procedure

99封情书 提交于 2019-12-19 04:33:34
问题 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

How can I rewrite “+ 1” (plus one) to “S” (succ) in Coq?

南笙酒味 提交于 2019-12-10 17:13:11
问题 I have the following Lemma with an incomplete proof: Lemma (s_is_plus_one : forall n:nat, S n = n + 1). Proof. intros. reflexivity. Qed. This proof fails with Unable to unify "n + 1" with "S n". It seems like eq_S would be the way to prove this, but I can't apply it (it doesn't recognize n + 1 as S n : Error: Unable to find an instance for the variable y. ). I've also tried ring , but it can't find a relation. When I use rewrite , it just reduces to the same final goal. How can I finish this

Explanation of Prolog recursive procedure

こ雲淡風輕ζ 提交于 2019-12-10 16:44:59
问题 I'd like someone to explain this procedure if possible (from the book 'learn prolog now'). It takes two numerals and adds them together. add(0,Y,Y). add(s(X),Y,s(Z)) :- add(X,Y,Z). In principle I understand, but I have a few issues. Lets say I issue the query ?- add(s(s(0)), s(0), R). Which results in: R = s(s(s(0))). Step 1 is the match with rule 2. Now X becomes s(0) and Y is still s(0). However Z (according to the book) becomes s(_G648), or s() with an uninstantiated variable inside it.

Division two naturals in prolog [duplicate]

无人久伴 提交于 2019-12-08 15:26:13
问题 This question already has answers here : Dividing two integers with an alternative way (3 answers) Closed 4 years ago . I have written the following code: nat(0). nat(s(X)) :- nat(X). divide(0,_,0). divide(X,Y,D) :- X@<Y, D is 0. divide(X,s(0),X). divide(_,0,undefined) :- !. Everything is right up to here. but what should i write, to calculate the division of two other naturals? for example divide(s(s(s(s(s(s(s(s(0)))))))),s(s(0)),D).??? 回答1: I think the easiest way would be to define

How does prolog run through recursive queries using succ?

邮差的信 提交于 2019-12-07 08:12:20
问题 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