- 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-∞
).
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).
Instances For
Equations
- Int.instInhabitedInt = { default := Int.ofNat 0 }
Negation of a natural number.
Equations
- Int.negOfNat x = match x with | 0 => 0 | Nat.succ m => 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) | 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
- mk: ∀ (n : Nat), Int.NonNeg (Int.ofNat n)
Sole constructor, proving that
ofNat n
is positive.
A proof that an Int
is non-negative.
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
Integer division. This function uses the "T-rounding" (Truncation-rounding) convention, meaning that it rounds toward zero. Also note that division by zero is defined to equal zero.
The relation between integer division and modulo is found in the
Int.mod_add_div
theorem in std which states
that a % b + b * (a / b) = a
, unconditionally.
Examples:
#eval (7 : Int) / (0 : Int) -- 0
#eval (0 : Int) / (7 : Int) -- 0
#eval (12 : Int) / (6 : Int) -- 2
#eval (12 : Int) / (-6 : Int) -- -2
#eval (-12 : Int) / (6 : Int) -- -2
#eval (-12 : Int) / (-6 : Int) -- 2
#eval (12 : Int) / (7 : Int) -- 1
#eval (12 : Int) / (-7 : Int) -- -1
#eval (-12 : Int) / (7 : Int) -- -1
#eval (-12 : Int) / (-7 : Int) -- 1
Implemented by efficient native code.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Integer modulo. This function uses the
"T-rounding" (Truncation-rounding) convention
to pair with Int.div
, meaning that a % b + b * (a / b) = a
unconditionally (see Int.mod_add_div
). In
particular, a % 0 = a
.
Examples:
#eval (7 : Int) % (0 : Int) -- 7
#eval (0 : Int) % (7 : Int) -- 0
#eval (12 : Int) % (6 : Int) -- 0
#eval (12 : Int) % (-6 : Int) -- 0
#eval (-12 : Int) % (6 : Int) -- 0
#eval (-12 : Int) % (-6 : Int) -- 0
#eval (12 : Int) % (7 : Int) -- 5
#eval (12 : Int) % (-7 : Int) -- 5
#eval (-12 : Int) % (7 : Int) -- 2
#eval (-12 : Int) % (-7 : Int) -- 2
Implemented by efficient native code.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Int.instHPowIntNat = { hPow := Int.pow }