Page 21, Exercise 1
ADT NaturalNumber is
objects:
an ordered subrange of the integers starting at zero and ending at the
maximum integer (INT_MAX) on the computer
functions:
for
all x, y ∈ NaturalNumber; TRUE, FALSE ∈ Boolean
and where +, −, <, and == are the usual integer operations
| NaturalNumber Zero( ) | ::= 0 |
| Boolean IsZero(x) | ::= if (x) return FALSE
return TRUE |
| Boolean Equal(x, y) | ::= if (x == y) return TRUE return FALSE |
| NaturalNumber Successor(x) | ::= if (x == INT_MAX) return x return x + 1 |
| NaturalNumber Add(x, y) | ::= if ((x + y) < INT_MAX) return x + y if ((x + y) == INT_MAX) return x + y return INT_MAX |
| NaturalNumber Subtract(x, y) | ::= if (x < y) return 0
return x − y |
| NaturalNumber Predecessor(x) | ::= if (x < 0) return ERROR if (x == 0) return 0 return x - 1 |
| Boolean IsGreater(x, y) | := if ((x-y)) < 0) return ERROR if ((x-y)) == 0) return FALSE return TRUE |
| NaturalNumber mult(x, y) | ::= if (x < 0) return ERROR |
| NaturalNumber div(x, y) | ::= if (x < 0) return ERROR |
end NaturalNumber