Built with doc-gen4, running Lean4. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑Ctrl+↓to navigate, Ctrl+🖱️to focus. On Mac, use Cmdinstead of Ctrl.
/-
Copyright (c) 2022 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Std.Classes.Dvd

open Nat

namespace Int

/--
`-[n+1]` is suggestive notation for `negSucc n`, which is the second constructor of
`Int` for making strictly negative numbers by mapping `n : Nat` to `-(n + 1)`.
-/
scoped notation "-[" 
n: ?m.2083
n
"+1]" =>
negSucc: NatInt
negSucc
n: ?m.2083
n
/- ## sign -/ /-- Returns the "sign" of the integer as another integer: `1` for positive numbers, `-1` for negative numbers, and `0` for `0`. -/ def
sign: IntInt
sign
:
Int: Type
Int
Int: Type
Int
|
succ: NatNat
succ
_ =>
1: ?m.7907
1
|
0: Int
0
=>
0: ?m.7925
0
| -[_+1] => -
1: ?m.7938
1
/-! ## toNat' -/ /-- * If `n : Nat`, then `int.toNat' n = some n` * If `n : Int` is negative, then `int.toNat' n = none`. -/ def
toNat': IntOption Nat
toNat'
:
Int: Type
Int
Option: Type ?u.8169 → Type ?u.8169
Option
Nat: Type
Nat
| (n : Nat) =>
some: {α : Type ?u.8266} → αOption α
some
n: Nat
n
| -[_+1] =>
none: {α : Type ?u.8278} → Option α
none
/-! ## Quotient and remainder There are three main conventions for integer division, referred here as the E, F, T rounding conventions. All three pairs satisfy the identity `x % y + (x / y) * y = x` unconditionally, and satisfy `x / 0 = 0` and `x % 0 = x`. -/ /-! ### E-rounding division This pair satisfies `0 ≤ mod x y < natAbs y` for `y ≠ 0`. -/ /-- Integer division. This version of `Int.div` uses the E-rounding convention (euclidean division), in which `Int.emod x y` satisfies `0 ≤ mod x y < natAbs y` for `y ≠ 0` and `Int.ediv` is the unique function satisfying `emod x y + (ediv x y) * y = x`. -/ def
ediv: IntIntInt
ediv
:
Int: Type
Int
Int: Type
Int
Int: Type
Int
|
ofNat: NatInt
ofNat
m: Nat
m
,
ofNat: NatInt
ofNat
n: Nat
n
=>
ofNat: NatInt
ofNat
(
m: Nat
m
/
n: Nat
n
) |
ofNat: NatInt
ofNat
m: Nat
m
, -[
n: Nat
n
+1] => -
ofNat: NatInt
ofNat
(
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
) | -[_+1],
0: Int
0
=>
0: ?m.8541
0
| -[
m: Nat
m
+1],
succ: NatNat
succ
n: Nat
n
=> -[
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
+1] | -[
m: Nat
m
+1], -[
n: Nat
n
+1] =>
ofNat: NatInt
ofNat
(
succ: NatNat
succ
(
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
)) /-- Integer modulus. This version of `Int.mod` uses the E-rounding convention (euclidean division), in which `Int.emod x y` satisfies `0 ≤ emod x y < natAbs y` for `y ≠ 0` and `Int.ediv` is the unique function satisfying `emod x y + (ediv x y) * y = x`. -/ def
emod: IntIntInt
emod
:
Int: Type
Int
Int: Type
Int
Int: Type
Int
|
ofNat: NatInt
ofNat
m: Nat
m
,
n: Int
n
=>
ofNat: NatInt
ofNat
(
m: Nat
m
%
natAbs: IntNat
natAbs
n: Int
n
) | -[
m: Nat
m
+1],
n: Int
n
=>
subNatNat: NatNatInt
subNatNat
(
natAbs: IntNat
natAbs
n: Int
n
) (
succ: NatNat
succ
(
m: Nat
m
%
natAbs: IntNat
natAbs
n: Int
n
)) /-! ### F-rounding division This pair satisfies `fdiv x y = floor (x / y)`. -/ /-- Integer division. This version of `Int.div` uses the F-rounding convention (flooring division), in which `Int.fdiv x y` satisfies `fdiv x y = floor (x / y)` and `Int.fmod` is the unique function satisfying `fmod x y + (fdiv x y) * y = x`. -/ def
fdiv: IntIntInt
fdiv
:
Int: Type
Int
Int: Type
Int
Int: Type
Int
|
0: Int
0
, _ =>
0: ?m.9539
0
|
ofNat: NatInt
ofNat
m: Nat
m
,
ofNat: NatInt
ofNat
n: Nat
n
=>
ofNat: NatInt
ofNat
(
m: Nat
m
/
n: Nat
n
) |
succ: NatNat
succ
m: Nat
m
, -[
n: Nat
n
+1] => -[
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
+1] | -[_+1],
0: Int
0
=>
0: ?m.9735
0
| -[
m: Nat
m
+1],
succ: NatNat
succ
n: Nat
n
=> -[
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
+1] | -[
m: Nat
m
+1], -[
n: Nat
n
+1] =>
ofNat: NatInt
ofNat
(
succ: NatNat
succ
m: Nat
m
/
succ: NatNat
succ
n: Nat
n
) /-- Integer modulus. This version of `Int.mod` uses the F-rounding convention (flooring division), in which `Int.fdiv x y` satisfies `fdiv x y = floor (x / y)` and `Int.fmod` is the unique function satisfying `fmod x y + (fdiv x y) * y = x`. -/ def
fmod: IntIntInt
fmod
:
Int: Type
Int
Int: Type
Int
Int: Type
Int
|
0: Int
0
, _ =>
0: ?m.10424
0
|
ofNat: NatInt
ofNat
m: Nat
m
,
ofNat: NatInt
ofNat
n: Nat
n
=>
ofNat: NatInt
ofNat
(
m: Nat
m
%
n: Nat
n
) |
succ: NatNat
succ
m: Nat
m
, -[
n: Nat
n
+1] =>
subNatNat: NatNatInt
subNatNat
(
m: Nat
m
%
succ: NatNat
succ
n: Nat
n
)
n: Nat
n
| -[
m: Nat
m
+1],
ofNat: NatInt
ofNat
n: Nat
n
=>
subNatNat: NatNatInt
subNatNat
n: Nat
n
(
succ: NatNat
succ
(
m: Nat
m
%
n: Nat
n
)) | -[
m: Nat
m
+1], -[
n: Nat
n
+1] => -
ofNat: NatInt
ofNat
(
succ: NatNat
succ
m: Nat
m
%
succ: NatNat
succ
n: Nat
n
) /-! ### T-rounding division This pair satisfies `div x y = round_to_zero (x / y)`. `Int.div` and `Int.mod` are defined in core lean. -/ /-- Core Lean provides instances using T-rounding division, i.e. `Int.div` and `Int.mod`. We override these here. -/
instance: Div Int
instance
:
Div: Type ?u.11189 → Type ?u.11189
Div
Int: Type
Int
:= ⟨
Int.ediv: IntIntInt
Int.ediv
instance: Mod Int
instance
:
Mod: Type ?u.11219 → Type ?u.11219
Mod
Int: Type
Int
:= ⟨
Int.emod: IntIntInt
Int.emod
⟩ /-! ## gcd -/ /-- Computes the greatest common divisor of two integers, as a `Nat`. -/ def
gcd: IntIntNat
gcd
(
m: Int
m
n: Int
n
:
Int: Type
Int
) :
Nat: Type
Nat
:=
m: Int
m
.
natAbs: IntNat
natAbs
.
gcd: NatNatNat
gcd
n: Int
n
.
natAbs: IntNat
natAbs
/-! ## divisibility -/ /-- Divisibility of integers. `a ∣ b` (typed as `\|`) says that there is some `c` such that `b = a * c`. -/
instance: Dvd Int
instance
:
Dvd: Type ?u.11282 → Type ?u.11282
Dvd
Int: Type
Int
:= ⟨fun
a: ?m.11290
a
b: ?m.11293
b
=> ∃
c: ?m.11298
c
,
b: ?m.11293
b
=
a: ?m.11290
a
*
c: ?m.11298
c