A representation of natural numbers
Nowadays, when human beings write numerals, they usually use decimal notation (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, and so on) but as you probably know, there are many other notations. Here is a way of writing numerals, which is sometimes used in mathematical logic. It makes use of just four symbols: 0, succ, and the left and right brackets. This style of numeral is defined by the following inductive definition:
- 0 is a numeral.
- If X is a numeral, then so is succ(X).
Define a predicate numeral/1
which test whether its
argument is a number in the 'succ-notation' just described. The
predicate should, for example, behave as follows:
?- numeral(0). yes ?- numeral(4). no ?- numeral(succ(succ(succ))). no ?- numeral(succ(succ(0))). yesHint
Define a predicate greater_than/2
that takes two
numbers in the 'succ-notation' and as arguments and decides
whether the first one is greater than the second one. For
example:
?- greater_than(succ(succ(succ(0))),0). yes ?- greater_than(succ(succ(succ(0))),succ(0)). yes ?- greater_than(succ(succ(0)),succ(succ(succ(0)))). noHint
Define a predicate add/3
that adds two
numbers in the 'succ-notation'. That is add(X,Y,Z)
should be true if Z
is the sum of X
and Y
. For example:
?- add(succ(succ(succ(0))),succ(0),R). R = succ(succ(succ(succ(0)))) yes ?- add(0,succ(succ(0)),R). R = succ(succ(0)) yesHint