Integer Type, Coercions, and Notation #
This file defines the Int type as well as
- coercions, conversions, and compatibility with numeric literals,
- basic arithmetic operations add/sub/mul/pow,
- a few
Nat-related operations such asnegOfNatandsubNatNat, - relations
</≤/≥/>, theNonNegproperty andmin/max, - decidability of equality, relations and
NonNeg.
Division and modulus operations are defined in Init.Data.Int.DivMod.Basic.
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 (
0to∞). - negSucc : Nat → Int
The negation of the successor of a natural number is an integer (
-1to-∞).
Instances For
Equations
- instNatCastInt = { natCast := fun (n : Nat) => Int.ofNat n }
Equations
- Int.instInhabited = { default := Int.ofNat 0 }
Coercions #
Negation of a natural number.
Equations
- Int.negOfNat 0 = 0
- Int.negOfNat m.succ = Int.negSucc m
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) | k.succ => Int.negSucc k
Instances For
Addition of two integers.
#eval (7 : Int) + (6 : Int) -- 13
#eval (6 : Int) + (-6 : Int) -- 0
Implemented by efficient native code.
Equations
- (Int.ofNat m_2).add (Int.ofNat n_2) = Int.ofNat (m_2 + n_2)
- (Int.ofNat m_2).add (Int.negSucc n_2) = Int.subNatNat m_2 n_2.succ
- (Int.negSucc m_2).add (Int.ofNat n_2) = Int.subNatNat n_2 m_2.succ
- (Int.negSucc m_2).add (Int.negSucc n_2) = Int.negSucc (m_2 + n_2).succ
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
- (Int.ofNat m_2).mul (Int.ofNat n_2) = Int.ofNat (m_2 * n_2)
- (Int.ofNat m_2).mul (Int.negSucc n_2) = Int.negOfNat (m_2 * n_2.succ)
- (Int.negSucc m_2).mul (Int.ofNat n_2) = Int.negOfNat (m_2.succ * n_2)
- (Int.negSucc m_2).mul (Int.negSucc n_2) = Int.ofNat (m_2.succ * n_2.succ)
Instances For
Decides equality between two Ints.
#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
- (Int.ofNat m_1).decEq (Int.ofNat n_1) = match decEq m_1 n_1 with | isTrue h => isTrue ⋯ | isFalse h => isFalse ⋯
- (Int.ofNat m_1).decEq (Int.negSucc n_1) = isFalse ⋯
- (Int.negSucc m_1).decEq (Int.ofNat n_1) = isFalse ⋯
- (Int.negSucc m_1).decEq (Int.negSucc n_1) = match decEq m_1 n_1 with | isTrue h => isTrue ⋯ | isFalse h => isFalse ⋯
Instances For
sign #
Conversion #
divisibility #
Powers #
Equations
- instIntCastInt = { intCast := fun (n : Int) => n }
Equations
- instCoeTailIntOfIntCast = { coe := Int.cast }
Equations
- instCoeHTCTIntOfIntCast = { coe := Int.cast }