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
- instDecidableEqPosNum.decEq PosNum.one PosNum.one = isTrue ⋯
- instDecidableEqPosNum.decEq PosNum.one a.bit1 = isFalse ⋯
- instDecidableEqPosNum.decEq PosNum.one a.bit0 = isFalse ⋯
- instDecidableEqPosNum.decEq a.bit1 PosNum.one = isFalse ⋯
- instDecidableEqPosNum.decEq a.bit1 b.bit1 = if h : a = b then h ▸ have inst := instDecidableEqPosNum.decEq a a; isTrue ⋯ else isFalse ⋯
- instDecidableEqPosNum.decEq a.bit1 a_1.bit0 = isFalse ⋯
- instDecidableEqPosNum.decEq a.bit0 PosNum.one = isFalse ⋯
- instDecidableEqPosNum.decEq a.bit0 a_1.bit1 = isFalse ⋯
- instDecidableEqPosNum.decEq a.bit0 b.bit0 = if h : a = b then h ▸ have inst := instDecidableEqPosNum.decEq a a; isTrue ⋯ else isFalse ⋯
Instances For
Equations
Instances For
Equations
Equations
- instDecidableEqZNum.decEq ZNum.zero ZNum.zero = isTrue ⋯
- instDecidableEqZNum.decEq ZNum.zero (ZNum.pos a) = isFalse ⋯
- instDecidableEqZNum.decEq ZNum.zero (ZNum.neg a) = isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.pos a) ZNum.zero = isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.pos a) (ZNum.pos b) = if h : a = b then h ▸ isTrue ⋯ else isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.pos a) (ZNum.neg a_1) = isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.neg a) ZNum.zero = isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.neg a) (ZNum.pos a_1) = isFalse ⋯
- instDecidableEqZNum.decEq (ZNum.neg a) (ZNum.neg b) = if h : a = b then h ▸ isTrue ⋯ else isFalse ⋯
Instances For
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 PosNums.
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
- Num.instLT = { lt := fun (a b : Num) => a.cmp b = Ordering.lt }
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 }
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✝¹