Bitwise "or" for PosNum
.
Bitwise "and" for PosNum
.
Equations
- PosNum.instHAndNum = { hAnd := PosNum.land }
Bitwise "xor" for PosNum
.
Equations
- PosNum.one.lxor PosNum.one = 0
- PosNum.one.lxor q.bit0 = Num.pos q.bit1
- PosNum.one.lxor q.bit1 = Num.pos q.bit0
- p.bit0.lxor PosNum.one = Num.pos p.bit1
- p.bit1.lxor PosNum.one = Num.pos p.bit0
- p.bit0.lxor q.bit0 = (p.lxor q).bit0
- p.bit0.lxor q.bit1 = (p.lxor q).bit1
- p.bit1.lxor q.bit0 = (p.lxor q).bit1
- p.bit1.lxor q.bit1 = (p.lxor q).bit0
Equations
- PosNum.instHXorNum = { hXor := PosNum.lxor }
a.testBit n
is true
iff the n
-th bit (starting from the LSB) in the binary representation
of a
is active. If the size of a
is less than n
, this evaluates to false
.
Equations
- PosNum.instHShiftLeftNat = { hShiftLeft := PosNum.shiftl }
Equations
- PosNum.instHShiftRightNatNum = { hShiftRight := PosNum.shiftr }
Equations
- Num.instHShiftLeftNat = { hShiftLeft := Num.shiftl }
Equations
- Num.instHShiftRightNat = { hShiftRight := Num.shiftr }
Equations
Alternative representation of integers using a sign bit at the end.
The convention on sign here is to have the argument to msb
denote
the sign of the MSB itself, with all higher bits set to the negation
of this sign. The result is interpreted in two's complement.
13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb true)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb false))))
As with Num
, a special case must be added for zero, which has no msb,
but by two's complement symmetry there is a second special case for -1.
Here the Bool
field indicates the sign of the number.
0 = ..0000000(base 2) = zero false -1 = ..1111111(base 2) = zero true
The SNum
representation uses a bit string, essentially a list of 0 (false
) and 1 (true
) bits,
and the negation of the MSB is sign-extended to all higher bits.
Add a bit at the end of a NzsNum
.
Equations
- NzsNum.«term_::_» = Lean.ParserDescr.trailingNode `NzsNum.«term_::_» 1022 0 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "::") (Lean.ParserDescr.cat `term 0))
Sign of a NzsNum
.
Equations
- (NzsNum.msb b).sign = !b
- (NzsNum.bit a p).sign = p.sign
Equations
- (NzsNum.msb b).not = NzsNum.msb (decide ¬b = true)
- (NzsNum.bit a p).not = NzsNum.bit (decide ¬a = true) p.not
Equations
- NzsNum.«term~_» = Lean.ParserDescr.node `NzsNum.«term~_» 100 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "~") (Lean.ParserDescr.cat `term 100))
Add an inactive bit at the end of a NzsNum
. This mimics PosNum.bit0
.
Equations
Add an active bit at the end of a NzsNum
. This mimics PosNum.bit1
.
Equations
The head
of a NzsNum
is the boolean value of its LSB.
Equations
- (NzsNum.msb b).head = b
- (NzsNum.bit a p).head = a
Equations
- SNum.«term~_» = Lean.ParserDescr.node `SNum.«term~_» 100 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "~") (Lean.ParserDescr.cat `term 100))
Add a bit at the end of a SNum
. This mimics NzsNum.bit
.
Equations
- SNum.«term_::_» = Lean.ParserDescr.trailingNode `SNum.«term_::_» 1022 0 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "::") (Lean.ParserDescr.cat `term 0))
A dependent induction principle for NzsNum
, with base cases
0 : SNum
and (-1) : SNum
.
Equations
- NzsNum.drec' z s (NzsNum.msb b) = ⋯.mpr (s b (SNum.zero (decide ¬b = true)) (z (decide ¬b = true)))
- NzsNum.drec' z s (NzsNum.bit a p) = s a (SNum.nz p) (NzsNum.drec' z s p)
A dependent induction principle for SNum
which avoids relying on NzsNum
.
Equations
- SNum.drec' z s (SNum.zero z_1) = z z_1
- SNum.drec' z s (SNum.nz p) = NzsNum.drec' z s p
SNum.testBit n a
is true
iff the n
-th bit (starting from the LSB) of a
is active.
If the size of a
is less than n
, this evaluates to false
.
Equations
- SNum.testBit 0 x✝ = x✝.head
- SNum.testBit n.succ x✝ = SNum.testBit n x✝.tail
SNum.czAdd a b n
is n + a - b
(where a
and b
should be read as either 0 or 1).
This is useful to implement the carry system in cAdd
.
Equations
- SNum.czAdd false false x✝ = x✝
- SNum.czAdd false true x✝ = x✝.pred
- SNum.czAdd true false x✝ = x✝.succ
- SNum.czAdd true true x✝ = x✝