Additional properties of binary recursion on Nat
#
This file documents additional properties of binary recursion,
which allows us to more easily work with operations which do depend
on the number of leading zeros in the binary representation of n
.
For example, we can more easily work with Nat.bits
and Nat.size
.
See also: Nat.bitwise
, Nat.pow
(for various lemmas about size
and shiftLeft
/shiftRight
),
and Nat.digits
.
boddDiv2 n
returns a 2-tuple of type (Bool, Nat)
where the Bool
value indicates whether
n
is odd or not and the Nat
value returns ⌊n/2⌋
Equations
- Nat.boddDiv2 0 = (false, 0)
- Nat.boddDiv2 (Nat.succ n) = match Nat.boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, Nat.succ m)
Instances For
shiftLeft' b m n
performs a left shift of m
n
times
and adds the bit b
as the least significant bit each time.
Returns the corresponding natural number
Equations
- Nat.shiftLeft' b m 0 = m
- Nat.shiftLeft' b m (Nat.succ n) = Nat.bit b (Nat.shiftLeft' b m n)
Instances For
Std4 takes the unprimed name for Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n
.
A recursion principle for bit
representations of natural numbers.
For a predicate C : Nat → Sort*
, if instances can be
constructed for natural numbers of the form bit b n
,
they can be constructed for all natural numbers.
Equations
- Nat.binaryRec z f n = if n0 : n = 0 then Eq.mpr ⋯ z else let n' := Nat.div2 n; let_fun _x := ⋯; Eq.mpr ⋯ (f (Nat.bodd n) n' (Nat.binaryRec z f n'))
Instances For
bitwise ops
boddDiv2_eq
and bodd
#
The same as binaryRec_eq
,
but that one unfortunately requires f
to be the identity when appending false
to 0
.
Here, we allow you to explicitly say that that case is not happening,
i.e. supplying n = 0 → b = true
.
The same as binaryRec
, but the induction step can assume that if n=0
,
the bit being appended is true
Equations
- Nat.binaryRec' z f = Nat.binaryRec z fun (b : Bool) (n : ℕ) (ih : C n) => if h : n = 0 → b = true then f b n h ih else Eq.mpr ⋯ (id z)