The type of integers. It is defined as an inductive type based on the
natural number type Nat
featuring two constructors: "a natural
number is an integer", and "the negation of a successor of a natural
number is an integer". The former represents integers between 0
(inclusive) and ∞
, and the latter integers between -∞
and -1
(inclusive).
This type is special-cased by the compiler. The runtime has a special
representation for Int
which stores "small" signed numbers directly,
and larger numbers use an arbitrary precision "bignum" library
(usually GMP). A "small number" is an integer
that can be encoded with 63 bits (31 bits on 32-bits architectures).
- ofNat: Nat → Int
A natural number is an integer (
0
to∞
). - negSucc: Nat → Int
The negation of the successor of a natural number is an integer (
-1
to-∞
).
Instances For
Equations
- instNatCastInt = { natCast := fun (n : Nat) => Int.ofNat n }
Equations
- Int.instInhabitedInt = { default := Int.ofNat 0 }
Coercions #
Negation of a natural number.
Equations
- Int.negOfNat x = match x with | 0 => 0 | Nat.succ m => Int.negSucc m
Instances For
Negation of an integer.
Implemented by efficient native code.
Equations
- Int.neg n = match n with | Int.ofNat n => Int.negOfNat n | Int.negSucc n => ↑(Nat.succ n)
Instances For
Equations
- Int.instNegInt = { neg := Int.neg }
Subtraction of two natural numbers.
Equations
- Int.subNatNat m n = match n - m with | 0 => Int.ofNat (m - n) | Nat.succ k => Int.negSucc k
Instances For
Multiplication of two integers.
#eval (63 : Int) * (6 : Int) -- 378
#eval (6 : Int) * (-6 : Int) -- -36
#eval (7 : Int) * (0 : Int) -- 0
Implemented by efficient native code.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A proof that an Int
is non-negative.
- mk: ∀ (n : Nat), Int.NonNeg (Int.ofNat n)
Sole constructor, proving that
ofNat n
is positive.
Instances For
Decides equality between two Int
s.
#eval (7 : Int) = (3 : Int) + (4 : Int) -- true
#eval (6 : Int) = (3 : Int) * (2 : Int) -- true
#eval ¬ (6 : Int) = (3 : Int) -- true
Implemented by efficient native code.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Decides whether a ≤ b
.
#eval ¬ ( (7 : Int) ≤ (0 : Int) ) -- true
#eval (0 : Int) ≤ (0 : Int) -- true
#eval (7 : Int) ≤ (10 : Int) -- true
Implemented by efficient native code.
Equations
- Int.decLe a b = Int.decNonneg (b - a)
Decides whether a < b
.
#eval `¬ ( (7 : Int) < 0 )` -- true
#eval `¬ ( (0 : Int) < 0 )` -- true
#eval `(7 : Int) < 10` -- true
Implemented by efficient native code.
Equations
- Int.decLt a b = Int.decNonneg (b - (a + 1))
Absolute value (Nat
) of an integer.
#eval (7 : Int).natAbs -- 7
#eval (0 : Int).natAbs -- 0
#eval (-11 : Int).natAbs -- 11
Implemented by efficient native code.
Equations
- Int.natAbs m = match m with | Int.ofNat m => m | Int.negSucc m => Nat.succ m
Instances For
sign #
Conversion #
Equations
- Int.toNat' x = match x with | Int.ofNat n => some n | Int.negSucc a => none
Instances For
divisibility #
Divisibility of integers. a ∣ b
(typed as \|
) says that
there is some c
such that b = a * c
.
Powers #
Equations
- Int.instHPowIntNat = { hPow := Int.pow }
Equations
- instIntCastInt = { intCast := fun (n : Int) => n }