Questions tagged [successor-arithmetics]

Successor arithmetics, in Prolog also known as s(X)-notation or s(X)-numbers, is an encoding of the natural numbers based on Peano Axioms. Zero is represented by 0, one is represented as the successor of zero s(0), two as the successor of one s(s(0)) etc.

Successor arithmetics, in Prolog also known as s(X)-notation or s(X)-numbers, is an encoding of the natural numbers based on Peano Axioms. Zero is represented by 0, one is represented as the successor of zero s(0), two as the successor of one s(s(0)) etc.

Using this representation, the natural numbers are defined :

natural_number(0).
natural_number(s(X)) :-
   natural_number(X).

And all natural numbers are enumerated :

?- natural_number(X).
X = 0 ;
X = s(0) ;
X = s(s(0)) ;
X = s(s(s(0))) ;
X = s(s(s(s(0)))) ;
X = s(s(s(s(s(0))))) ...

Also other representations are used, like n or z, and succ(X); in mathematics a small sigma σ, or a postfix single quote '. Originally, Giuseppe Peano used postfix plus + and started with 1.

107 questions
42
votes
2 answers

Prolog successor notation yields incomplete result and infinite loop

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) :- …
9
votes
1 answer

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

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,…
Langston
  • 1,083
  • 10
  • 26
8
votes
4 answers

Convert peano number s(N) to integer in Prolog

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…
shaungus
  • 129
  • 3
  • 7
7
votes
3 answers

What does the s() predicate do in Prolog?

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…
okin33
  • 144
  • 1
  • 9
7
votes
3 answers

how to stop prolog from examining impossible solutions infinitely?

suppose the following program: nat(0). nat(s(N)) :- nat(N). /* 0+b=b */ plus(0,B,B) :- nat(B). /* (a+1)+b = c iff a+(b+1)=c */ plus(s(A),B,C) :- plus(A,s(B),C). it works great for adding two numbers, but when i attempt a query of the following…
yurib
  • 8,043
  • 3
  • 30
  • 55
6
votes
2 answers

prolog - infinite rule

I have the next rules % Signature: natural_number(N)/1 % Purpose: N is a natural number. natural_number(0). natural_number(s(X)) :- natural_number(X). ackermann(0, N, s(N)). % rule 1 ackermann(s(M),0,Result):- ackermann(M,s(0),Result). % rule…
Tom
  • 673
  • 4
  • 12
  • 20
6
votes
4 answers

Better termination for s(X)-sum

(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…
false
  • 10,264
  • 13
  • 101
  • 209
6
votes
1 answer

Trying to write a tree-height predicate - do I need Peano-style natural numbers?

As a basic Prolog exercise, I set myself the task of writing a binary tree height predicate that would work forwards and "backwards" - that is, as well as determining the height of a known binary tree, it should be able to find all binary trees…
user180247
6
votes
1 answer

What are the optimal green cuts for successor arithmetics sum?

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…
Pietro Braione
  • 1,123
  • 8
  • 22
5
votes
2 answers

Prolog predicate - infinite loop

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) :- …
5
votes
1 answer

Instantiate type variable in Haskell

EDIT: Solved. I was unware that enabling a language extension in the source file did not enable the language extension in GHCi. The solution was to :set FlexibleContexts in GHCi. I recently discovered that type declarations in classes and instances…
emi
  • 5,380
  • 1
  • 27
  • 45
5
votes
2 answers

Reversible tree length relation

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…
Manux
  • 3,643
  • 4
  • 30
  • 42
5
votes
3 answers

How does prolog run through recursive queries using succ?

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) …
Andy
  • 10,553
  • 21
  • 75
  • 125
4
votes
3 answers

Sum of a list in prolog

I'm reading 'the art of prolog' book and I found an exercise that reads 'Define the relation sum(ListOfIntegers,Sum) which holds if Sum is the sum of the ListOfIntegers, without using any auxiliary predicate' .I came up with this…
kaiseroskilo
  • 1,719
  • 4
  • 21
  • 29
4
votes
1 answer

Division using Peano numbers

I am trying to make a program that divides two Peano numbers. Unfortunately, the cycle starts running after I look for other answers. Is there any way to avoid duplicates using my method? s(0). s(X):- X. plus(0,Y,Y). plus(s(X), Y,…
Ignas Ausiejus
  • 175
  • 1
  • 1
  • 9
1
2 3 4 5 6 7 8