Bitwise "or" for PosNum
.
Equations
- PosNum.one.lor q.bit0 = q.bit1
- PosNum.one.lor x = x
- p.bit0.lor PosNum.one = p.bit1
- x.lor PosNum.one = x
- p.bit0.lor q.bit0 = (p.lor q).bit0
- p.bit0.lor q.bit1 = (p.lor q).bit1
- p.bit1.lor q.bit0 = (p.lor q).bit1
- p.bit1.lor q.bit1 = (p.lor q).bit1
Instances For
Bitwise "and" for PosNum
.
Equations
- PosNum.one.land q.bit0 = 0
- PosNum.one.land x = 1
- p.bit0.land PosNum.one = 0
- x.land PosNum.one = 1
- p.bit0.land q.bit0 = (p.land q).bit0
- p.bit0.land q.bit1 = (p.land q).bit0
- p.bit1.land q.bit0 = (p.land q).bit0
- p.bit1.land q.bit1 = (p.land q).bit1
Instances For
Equations
- PosNum.instHAndNum = { hAnd := PosNum.land }
Bitwise fun a b ↦ a && !b
for PosNum
. For example, ldiff 5 9 = 4
:
101
1001
----
100
Equations
- PosNum.one.ldiff a.bit0 = 1
- PosNum.one.ldiff x = 0
- p.bit0.ldiff PosNum.one = Num.pos p.bit0
- p.bit1.ldiff PosNum.one = Num.pos p.bit0
- p.bit0.ldiff q.bit0 = (p.ldiff q).bit0
- p.bit0.ldiff q.bit1 = (p.ldiff q).bit0
- p.bit1.ldiff q.bit0 = (p.ldiff q).bit1
- p.bit1.ldiff q.bit1 = (p.ldiff q).bit0
Instances For
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
Instances For
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
Instances For
Equations
- PosNum.instHShiftLeftNat = { hShiftLeft := PosNum.shiftl }
Equations
- PosNum.instHShiftRightNatNum = { hShiftRight := PosNum.shiftr }
Equations
- Num.instHShiftLeftNat = { hShiftLeft := Num.shiftl }
Equations
- Num.instHShiftRightNat = { hShiftRight := Num.shiftr }
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
.
Instances For
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
Instances For
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))
Instances For
Equations
- (NzsNum.msb b).not = NzsNum.msb (decide ¬b = true)
- (NzsNum.bit a p).not = NzsNum.bit (decide ¬a = true) p.not
Instances For
Equations
- NzsNum.«term~_» = Lean.ParserDescr.node `NzsNum.term~_ 100 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "~") (Lean.ParserDescr.cat `term 100))
Instances For
The head
of a NzsNum
is the boolean value of its LSB.
Equations
- x.head = match x with | NzsNum.msb b => b | NzsNum.bit b a => b
Instances For
Equations
- SNum.«term~_» = Lean.ParserDescr.node `SNum.term~_ 100 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol "~") (Lean.ParserDescr.cat `term 100))
Instances For
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))
Instances For
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)
Instances For
A dependent induction principle for SNum
which avoids relying on NzsNum
.
Equations
- SNum.drec' z s x = match x with | SNum.zero b => z b | SNum.nz p => NzsNum.drec' z s p
Instances For
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
Instances For
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
.