Rational numbers

Uses Integers. Requires special terms (rationalNumbers) that cannot be implemented in Leibniz itself. Rational number literals are written with a division slash, which may not be surrounded by spaces, e.g. 1/3 or -2/5.

The sort describes the rational numbers. The integers are a subsort: .

There is a subsort ℚ.nz for the non-zero rational numbers, with ℤ.nzℚ.nz.

Another subsort, ℚ.nn, describes the non-negative rational numbers.

The positive rational numbers, ℚ.p, are both non-zero (ℚ.pℚ.nz) and non-negative (ℚ.pℚ.nn).

The natural numbers are always non-negative (ℚ.nn), and the non-zero natural numbers are positive (ℕ.nzℚ.p).

Arithmetic

Addition

The sum of two rational numbers is a rational number:
+ :

Important special cases are:
ℚ.p + ℚ.p : ℚ.p
ℚ.nn + ℚ.nn : ℚ.nn

Addition is commutative:
x:, y:, x + y = y + x
and associative:
x:, y:, z:, (x + y) + z = x + (y + z)

Rules:

Eliminate zeros:
(x:) + (zero:zero)x
(zero:zero) + (x:)x

Evaluation for number terms:
(x:) + (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x + y"

Subtraction

The difference between two rational numbers is a rational number:
- :

The negative of a rational number is a rational number:
-() :

Rules:

Eliminate zeros:
(x:) - (zero:zero)x
(zero:zero) - (x:) ⇒ -(x)

Evaluation for number terms:
(x:) - (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x - y"
-(x:) ⇒ Pharo:"x ensureNumber. x negated"

Multiplication

The product of two rational numbers is a rational number:
× :

Important special cases are:
ℚ.p × ℚ.p : ℚ.p
ℚ.nn × ℚ.nn : ℚ.nn
ℚ.nz × ℚ.nz : ℚ.nz

Multiplication is commutative:
x:, y:, x × y = y × x
and associative:
x:, y:, z:, (x × y) × z = x × (y × z)

Addition is distributive over multiplication:
x:, y:, z:, x × (y + z) = (x × y) + (x × z)

Rules:

Eliminate zeros:
(x:) × (zero:zero) ⇒ 0
(zero:zero) × (x:) ⇒ 0

Eliminate ones:
(one:one) × (x:)x
(x:) × (one:one)x

Evaluate for number terms:
(x:) × (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x * y"

Division

The quotient of a rational number and a non-zero rational number is a rational number:
÷ ℚ.nz :

Important special cases are:
ℚ.nz ÷ ℚ.nz : ℚ.nz
ℚ.nn ÷ ℚ.p : ℚ.nn
ℚ.p ÷ ℚ.p : ℚ.p

Rules:

Elimination of zeros and ones:
(z:zero) ÷ (x:ℚ.nz) ⇒ 0
(x:) ÷ (one:one)x

Evaluate for number terms:
(x:) ÷ (y:ℚ.nz) ⇒ Pharo:"x ensureNumber. y ensureNumber. x / y"

Note that division by zero is excluded by the definition of the division operator. 1 ÷ 0 : [ℚ] has no sort, only a kind, which is an error indicator. There is no rewrite rule either that would transform this term into a valid one.

Exponentiation

Exponentiation is defined for non-zero rational number to any integer power:
ℚ.nzℤ.nz : ℚ.nz
ℚ.nzzero : ℕ.nz

An important special case is:
ℚ.pℤ.nz : ℚ.p

Rules:

Eliminate zeros:
(x:ℚ.nz)z:zero ⇒ 1
(z:zero)x:ℕ.nz ⇒ 0

Eliminate ones:
(x:ℚ.nz)one:onex

Evaluate for number terms:
(x:)y:ℕ.nz ⇒ Pharo:"x raisedTo: y"
(x:ℚ.nz)y:ℤ.nz ⇒ Pharo:"x raisedTo: y"

Comparison

< : 𝔹
> : 𝔹
: 𝔹
: 𝔹

Evaluation rules for number terms:
(x:) < (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x < y"
(x:) > (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x > y"
(x:) (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x <= y"
(x:) (y:) ⇒ Pharo:"x ensureNumber. y ensureNumber. x >= y"