Binary representation of integers using inductive types #
Note: Unlike in Coq, where this representation is preferred because of
the reliance on kernel reduction, in Lean this representation is discouraged
in favor of the "Peano" natural numbers Nat
, and the purpose of this
collection of theorems is to show the equivalence of the different approaches.
Equations
bit b n
appends the bit b
to the end of n
, where bit tt x = x1
and bit ff x = x0
.
Equations
- PosNum.bit b = bif b then PosNum.bit1 else PosNum.bit0
Instances For
ofNatSucc n
is the PosNum
corresponding to n + 1
.
Equations
- PosNum.ofNatSucc 0 = 1
- PosNum.ofNatSucc n.succ = (PosNum.ofNatSucc n).succ
Instances For
@[instance 100]
Equations
- PosNum.instOfNatHAddNatOfNat = { ofNat := PosNum.ofNat (n + 1) }
Ordering of PosNum
s.
Equations
- PosNum.one.cmp PosNum.one = Ordering.eq
- x✝.cmp PosNum.one = Ordering.gt
- PosNum.one.cmp x = Ordering.lt
- a.bit0.cmp b.bit0 = a.cmp b
- a.bit0.cmp b.bit1 = Ordering.casesOn (a.cmp b) Ordering.lt Ordering.lt Ordering.gt
- a.bit1.cmp b.bit0 = Ordering.casesOn (a.cmp b) Ordering.lt Ordering.gt Ordering.gt
- a.bit1.cmp b.bit1 = a.cmp b
Instances For
Equations
- PosNum.instLT = { lt := fun (a b : PosNum) => a.cmp b = Ordering.lt }
Equations
- PosNum.instLE = { le := fun (a b : PosNum) => ¬b < a }
Equations
- x✝¹.decidableLT x✝ = id inferInstance
Equations
- x✝¹.decidableLE x✝ = id inferInstance
Equations
- instReprPosNum = { reprPrec := fun (n : PosNum) (x : ℕ) => repr ↑n }
Equations
- instReprNum = { reprPrec := fun (n : Num) (x : ℕ) => repr ↑n }
Equations
- Num.instLT = { lt := fun (a b : Num) => a.cmp b = Ordering.lt }
Equations
- x✝¹.decidableLT x✝ = id inferInstance
Equations
- x✝¹.decidableLE x✝ = id inferInstance
Equations
- Num.ofNat' n = Nat.binaryRec 0 (fun (b : Bool) (x : ℕ) => bif b then Num.bit1 else Num.bit0) n
Instances For
Equations
- ZNum.ofInt' (Int.ofNat n) = (Num.ofNat' n).toZNum
- ZNum.ofInt' (Int.negSucc n) = (Num.ofNat' (n + 1)).toZNumNeg
Instances For
Converts a ZNum
to a PosNum
, mapping all out of range values to 1
.
Equations
- PosNum.ofZNum (ZNum.pos p) = p
- PosNum.ofZNum x✝ = 1
Instances For
Converts a ZNum
to an Option Num
, where ofZNum p = 0
if p < 0
.
Equations
- Num.ofZNum (ZNum.pos p) = Num.pos p
- Num.ofZNum x✝ = 0
Instances For
Equations
- ZNum.instLT = { lt := fun (a b : ZNum) => a.cmp b = Ordering.lt }
Equations
- x✝¹.decidableLT x✝ = id inferInstance
Equations
- x✝¹.decidableLE x✝ = id inferInstance
Auxiliary definition for Num.gcd
.
Equations
- Num.gcdAux 0 x✝¹ x✝ = x✝
- Num.gcdAux n.succ Num.zero x✝ = x✝
- Num.gcdAux n.succ x✝¹ x✝ = Num.gcdAux n (x✝ % x✝¹) x✝¹
Instances For
Equations
- instReprZNum = { reprPrec := fun (n : ZNum) (x : ℕ) => repr ↑n }