Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type family Rem (x :: Nat) (y :: Nat) :: Nat where ...
- type family RemImpl (x :: Nat) (y :: nat) (acc :: Nat) :: Nat where ...
- type RemPow2 x p = FromBits (TakeLastN p (ToBits x RemPow2Bits))
- type TakeLastN n xs = TakeLastNImplRev n xs '[]
- type family TakeLastNImplRev (n :: Nat) (xs :: [t]) (acc :: [t]) :: [t] where ...
- type family TakeLastNImplTakeNRev (n :: Nat) (rs :: [t]) (acc :: [t]) :: [t] where ...
- type RemPow2Bits = 32
- type Div x y = DivImpl (x - (x `Rem` y)) y 0
- type family DivImpl (x :: Nat) (y :: nat) (acc :: Nat) :: Nat where ...
- type family TestHighBit (x :: Nat) (n :: Nat) :: Bool where ...
- type ToBits x n = ToBits_ x n False
- type family ToBits_ (x :: Nat) (n :: Nat) (started :: Bool) :: [Bool] where ...
- type family ToBitsInner (highBitSet :: Bool) (x :: Nat) (n :: Nat) (started :: Bool) :: [Bool] where ...
- type FromBits bits = FromBits_ bits 0
- type family FromBits_ (bits :: [Bool]) (acc :: Nat) :: Nat where ...
- type family ShiftBitsR (bits :: [Bool]) (n :: Nat) :: [Bool] where ...
- type family GetMostSignificantBitIndex (highestBit :: Nat) (n :: Nat) :: Nat where ...
- type family ShiftR (xMaxBits :: Nat) (x :: Nat) (bits :: Nat) :: Nat where ...
Documentation
type family Rem (x :: Nat) (y :: Nat) :: Nat where ... Source #
Get the remainder of the integer division of x and y, such that forall x
y. exists k. (Rem x y) == x - y * k
The algorithm is: count down x
until zero, incrementing the accumulator at each step. Whenever the
accumulator is equal to y set it to zero.
If the accumulator has reached y reset it. It is important to do this BEFORE checking if x == y and then returning the accumulator, for the case where x = k * y with k > 0. For example:
6 Rem
3 = RemImpl 6 3 0
RemImpl 6 3 0 = RemImpl (6-1) 3 (0+1) -- RemImpl Clause 4
RemImpl 5 3 1 = RemImpl (5-1) 3 (1+1) -- RemImpl Clause 4
RemImpl 4 3 2 = RemImpl (4-1) 3 (2+1) -- RemImpl Clause 4
RemImpl 3 3 3 = RemImpl 3 3 0 -- RemImpl Clause 2 !!!
RemImpl 3 3 0 = 0 -- RemImpl Clause 3 !!!
type RemPow2 x p = FromBits (TakeLastN p (ToBits x RemPow2Bits)) Source #
Efficient Rem
operation for power of 2 values. Note that x must be
representable by RemPow2Bits
bits.
type TakeLastN n xs = TakeLastNImplRev n xs '[] Source #
type family TakeLastNImplRev (n :: Nat) (xs :: [t]) (acc :: [t]) :: [t] where ... Source #
TakeLastNImplRev n '[] acc = TakeLastNImplTakeNRev n acc '[] | |
TakeLastNImplRev n (x ': xs) acc = TakeLastNImplRev n xs (x ': acc) |
type family TakeLastNImplTakeNRev (n :: Nat) (rs :: [t]) (acc :: [t]) :: [t] where ... Source #
TakeLastNImplTakeNRev n '[] acc = acc | |
TakeLastNImplTakeNRev 0 rs acc = acc | |
TakeLastNImplTakeNRev n (r ': rs) acc = TakeLastNImplTakeNRev (n - 1) rs (r ': acc) |
type RemPow2Bits = 32 Source #
Maximum number of bits an argument x
of RemPow2
may occupy.
type Div x y = DivImpl (x - (x `Rem` y)) y 0 Source #
Integer division of x and y: Div x y ==> x / y
,
NOTE This only works for small numbers currently
Bit manipulation
type family TestHighBit (x :: Nat) (n :: Nat) :: Bool where ... Source #
TestHighBit x n = (2 ^ n) <=? x |
type family ToBits_ (x :: Nat) (n :: Nat) (started :: Bool) :: [Bool] where ... Source #
ToBits_ x 0 started = '[] | |
ToBits_ x n started = ToBitsInner (TestHighBit x (n - 1)) x (n - 1) started |
type family ToBitsInner (highBitSet :: Bool) (x :: Nat) (n :: Nat) (started :: Bool) :: [Bool] where ... Source #
type family ShiftBitsR (bits :: [Bool]) (n :: Nat) :: [Bool] where ... Source #
ShiftBitsR bits 0 = bits | |
ShiftBitsR '[] n = '[] | |
ShiftBitsR '[e] 1 = '[] | |
ShiftBitsR (e ': rest) 1 = e ': ShiftBitsR rest 1 | |
ShiftBitsR (e ': rest) n = ShiftBitsR (ShiftBitsR (e ': rest) 1) (n - 1) |
type family GetMostSignificantBitIndex (highestBit :: Nat) (n :: Nat) :: Nat where ... Source #
GetMostSignificantBitIndex 0 n = 1 | |
GetMostSignificantBitIndex highestBit n = If ((2 ^ (highestBit + 1)) <=? n) (TypeError (((Text "number to big: " :<>: ShowType n) :<>: Text " >= ") :<>: ShowType (2 ^ (highestBit + 1)))) (If ((2 ^ highestBit) <=? n) highestBit (GetMostSignificantBitIndex (highestBit - 1) n)) |