{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies          #-}

module HaskellWorks.Data.Bits.BitWise
    ( -- * Bit map
      BitWise(..)
    , Bit(..)
    , Shift(..)
    , TestBit(..)
    ) where

import Data.Int
import Data.Word
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Bits.BitLength
import HaskellWorks.Data.Naive
import HaskellWorks.Data.Positioning
import Prelude                          as P

import qualified Data.Bit             as Bit
import qualified Data.Bit.ThreadSafe  as BitTS
import qualified Data.Bits            as B
import qualified Data.Vector          as DV
import qualified Data.Vector.Storable as DVS
import qualified Data.Vector.Unboxed  as DVU

-- We pervasively use precedence to avoid excessive parentheses, and we use
-- the same precedence conventions of the C programming language: arithmetic
-- operators come first, ordered in the standard way, followed by shifts,
-- followed by logical operators; ⊕ sits between | and &.
infixl 9 .?.
infixl 8 .<., .>.
infixl 7 .&.        -- Bitwise AND.  eg. ∧
infixl 6 .^.        -- Bitwise XOR.  eg. ⊕
infixl 5 .|.        -- Bitwise OR.   eg. ∨

-- | Class of values that have shift operations
class Shift a where
  -- | Shift left by the specified count
  (.<.) :: a -> Count -> a
  -- | Shift right by the specified count
  (.>.) :: a -> Count -> a

  -- | Class of values that have bit test operations
class TestBit a where
  -- | Test whether the bit ad the given offset is set
  (.?.) :: a -> Position -> Bool

-- | Class of values that have bit wise logical operations
class BitWise a where
  -- | Bit-wise AND
  (.&.) :: a -> a -> a
  -- | Bit-wise OR
  (.|.) :: a -> a -> a
  -- | Bit-wise XOR
  (.^.) :: a -> a -> a
  -- | Bit-wise complement
  comp  :: a -> a
  -- | Bit-wise value of the given type with all bits set to zero
  all0s :: a
  -- | Bit-wise value of the given type with all bits set to one
  all1s :: a

class Bit a where
  bit :: Position -> a

--------------------------------------------------------------------------------
-- Instances

instance TestBit Bool where
  .?. :: Bool -> Position -> Bool
(.?.) Bool
w Position
0 = Bool
w
  (.?.) Bool
_ Position
_ = [Char] -> Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"Invalid bit index"
  {-# INLINE (.?.) #-}

instance TestBit [Bool] where
  .?. :: [Bool] -> Position -> Bool
(.?.) [Bool]
v Position
p = [Bool]
v [Bool] -> Int -> Bool
forall a. [a] -> Int -> a
!! Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
p
  {-# INLINE (.?.) #-}

instance TestBit Int where
  .?. :: Int -> Position -> Bool
(.?.) Int
w Position
n = Int -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit Int
w (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit Word8 where
  .?. :: Word8 -> Position -> Bool
(.?.) Word8
w Position
n = Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit Word8
w (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit Word16 where
  .?. :: Word16 -> Position -> Bool
(.?.) Word16
w Position
n = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit Word16
w (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit Word32 where
  .?. :: Word32 -> Position -> Bool
(.?.) Word32
w Position
n = Word32 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit Word32
w (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit Word64 where
  .?. :: Word64 -> Position -> Bool
(.?.) Word64
w Position
n = Word64 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit Word64
w (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (Naive Word8) where
  .?. :: Naive Word8 -> Position -> Bool
(.?.) Naive Word8
w Position
n = Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit (Naive Word8 -> Word8
forall a. Naive a -> a
naive Naive Word8
w) (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (Naive Word16) where
  .?. :: Naive Word16 -> Position -> Bool
(.?.) Naive Word16
w Position
n = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit (Naive Word16 -> Word16
forall a. Naive a -> a
naive Naive Word16
w) (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (Naive Word32) where
  .?. :: Naive Word32 -> Position -> Bool
(.?.) Naive Word32
w Position
n = Word32 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit (Naive Word32 -> Word32
forall a. Naive a -> a
naive Naive Word32
w) (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (Naive Word64) where
  .?. :: Naive Word64 -> Position -> Bool
(.?.) Naive Word64
w Position
n = Word64 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
B.testBit (Naive Word64 -> Word64
forall a. Naive a -> a
naive Naive Word64
w) (Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (DV.Vector Word8) where
  .?. :: Vector Word8 -> Position -> Bool
(.?.) Vector Word8
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word8 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word8
v in (Vector Word8
v Vector Word8 -> Position -> Elem (Vector Word8)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word8 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DV.Vector Word16) where
  .?. :: Vector Word16 -> Position -> Bool
(.?.) Vector Word16
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word16 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word16
v in (Vector Word16
v Vector Word16 -> Position -> Elem (Vector Word16)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word16 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DV.Vector Word32) where
  .?. :: Vector Word32 -> Position -> Bool
(.?.) Vector Word32
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word32 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word32
v in (Vector Word32
v Vector Word32 -> Position -> Elem (Vector Word32)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word32 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DV.Vector Word64) where
  .?. :: Vector Word64 -> Position -> Bool
(.?.) Vector Word64
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word64 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word64
v in (Vector Word64
v Vector Word64 -> Position -> Elem (Vector Word64)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word64 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DVS.Vector Word8) where
  .?. :: Vector Word8 -> Position -> Bool
(.?.) Vector Word8
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word8 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word8
v in (Vector Word8
v Vector Word8 -> Position -> Elem (Vector Word8)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word8 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DVS.Vector Word16) where
  .?. :: Vector Word16 -> Position -> Bool
(.?.) Vector Word16
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word16 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word16
v in (Vector Word16
v Vector Word16 -> Position -> Elem (Vector Word16)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word16 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DVS.Vector Word32) where
  .?. :: Vector Word32 -> Position -> Bool
(.?.) Vector Word32
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word32 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word32
v in (Vector Word32
v Vector Word32 -> Position -> Elem (Vector Word32)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word32 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DVS.Vector Word64) where
  .?. :: Vector Word64 -> Position -> Bool
(.?.) Vector Word64
v Position
n = let (Position
q, Position
r) = Position
n Position -> Position -> (Position, Position)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Vector Word64 -> Position
forall v. (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd Vector Word64
v in (Vector Word64
v Vector Word64 -> Position -> Elem (Vector Word64)
forall v. AtIndex v => v -> Position -> Elem v
!!! Position
q) Word64 -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Position
r
  {-# INLINE (.?.) #-}

instance TestBit (DVU.Vector Bit.Bit) where
  .?. :: Vector Bit -> Position -> Bool
(.?.) Vector Bit
v Position
n = Bit -> Bool
Bit.unBit (Vector Bit
v Vector Bit -> Int -> Bit
forall a. Unbox a => Vector a -> Int -> a
DVU.! Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance TestBit (DVU.Vector BitTS.Bit) where
  .?. :: Vector Bit -> Position -> Bool
(.?.) Vector Bit
v Position
n = Bit -> Bool
BitTS.unBit (Vector Bit
v Vector Bit -> Int -> Bit
forall a. Unbox a => Vector a -> Int -> a
DVU.! Position -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n)
  {-# INLINE (.?.) #-}

instance BitWise Int where
  .&. :: Int -> Int -> Int
(.&.) = Int -> Int -> Int
forall a. Bits a => a -> a -> a
(B..&.)
  {-# INLINE (.&.) #-}

  .|. :: Int -> Int -> Int
(.|.) = Int -> Int -> Int
forall a. Bits a => a -> a -> a
(B..|.)
  {-# INLINE (.|.) #-}

  .^. :: Int -> Int -> Int
(.^.) = Int -> Int -> Int
forall a. Bits a => a -> a -> a
B.xor
  {-# INLINE (.^.) #-}

  comp :: Int -> Int
comp  = Int -> Int
forall a. Bits a => a -> a
B.complement
  {-# INLINE comp #-}

  all0s :: Int
all0s = Int
0
  {-# INLINE all0s #-}

  all1s :: Int
all1s = -Int
1
  {-# INLINE all1s #-}

instance BitWise Word8 where
  .&. :: Word8 -> Word8 -> Word8
(.&.) = Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
(B..&.)
  {-# INLINE (.&.) #-}

  .|. :: Word8 -> Word8 -> Word8
(.|.) = Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
(B..|.)
  {-# INLINE (.|.) #-}

  .^. :: Word8 -> Word8 -> Word8
(.^.) = Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
B.xor
  {-# INLINE (.^.) #-}

  comp :: Word8 -> Word8
comp  = Word8 -> Word8
forall a. Bits a => a -> a
B.complement
  {-# INLINE comp #-}

  all0s :: Word8
all0s = Word8
0
  {-# INLINE all0s #-}

  all1s :: Word8
all1s = Word8
0xff
  {-# INLINE all1s #-}

instance BitWise Word16 where
  .&. :: Word16 -> Word16 -> Word16
(.&.) = Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
(B..&.)
  {-# INLINE (.&.) #-}

  .|. :: Word16 -> Word16 -> Word16
(.|.) = Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
(B..|.)
  {-# INLINE (.|.) #-}

  .^. :: Word16 -> Word16 -> Word16
(.^.) = Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
B.xor
  {-# INLINE (.^.) #-}

  comp :: Word16 -> Word16
comp  = Word16 -> Word16
forall a. Bits a => a -> a
B.complement
  {-# INLINE comp #-}

  all0s :: Word16
all0s = Word16
0
  {-# INLINE all0s #-}

  all1s :: Word16
all1s = Word16
0xffff
  {-# INLINE all1s #-}

instance BitWise Word32 where
  .&. :: Word32 -> Word32 -> Word32
(.&.) = Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
(B..&.)
  {-# INLINE (.&.) #-}

  .|. :: Word32 -> Word32 -> Word32
(.|.) = Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
(B..|.)
  {-# INLINE (.|.) #-}

  .^. :: Word32 -> Word32 -> Word32
(.^.) = Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
B.xor
  {-# INLINE (.^.) #-}

  comp :: Word32 -> Word32
comp  = Word32 -> Word32
forall a. Bits a => a -> a
B.complement
  {-# INLINE comp #-}

  all0s :: Word32
all0s = Word32
0
  {-# INLINE all0s #-}

  all1s :: Word32
all1s = Word32
0xffffffff
  {-# INLINE all1s #-}

instance BitWise Word64 where
  .&. :: Word64 -> Word64 -> Word64
(.&.) = Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
(B..&.)
  {-# INLINE (.&.) #-}

  .|. :: Word64 -> Word64 -> Word64
(.|.) = Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
(B..|.)
  {-# INLINE (.|.) #-}

  .^. :: Word64 -> Word64 -> Word64
(.^.) = Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
B.xor
  {-# INLINE (.^.) #-}

  comp :: Word64 -> Word64
comp  = Word64 -> Word64
forall a. Bits a => a -> a
B.complement
  {-# INLINE comp #-}

  all0s :: Word64
all0s = Word64
0
  {-# INLINE all0s #-}

  all1s :: Word64
all1s = Word64
0xffffffffffffffff
  {-# INLINE all1s #-}

instance Shift Int  where
  .<. :: Int -> Word64 -> Int
(.<.) Int
w Word64
n = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
B.shiftL Int
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Int -> Word64 -> Int
(.>.) Int
w Word64
n = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
B.shiftR Int
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Int8  where
  .<. :: Int8 -> Word64 -> Int8
(.<.) Int8
w Word64
n = Int8 -> Int -> Int8
forall a. Bits a => a -> Int -> a
B.shiftL Int8
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Int8 -> Word64 -> Int8
(.>.) Int8
w Word64
n = Int8 -> Int -> Int8
forall a. Bits a => a -> Int -> a
B.shiftR Int8
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Int16  where
  .<. :: Int16 -> Word64 -> Int16
(.<.) Int16
w Word64
n = Int16 -> Int -> Int16
forall a. Bits a => a -> Int -> a
B.shiftL Int16
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Int16 -> Word64 -> Int16
(.>.) Int16
w Word64
n = Int16 -> Int -> Int16
forall a. Bits a => a -> Int -> a
B.shiftR Int16
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Int32  where
  .<. :: Int32 -> Word64 -> Int32
(.<.) Int32
w Word64
n = Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
B.shiftL Int32
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Int32 -> Word64 -> Int32
(.>.) Int32
w Word64
n = Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
B.shiftR Int32
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Int64  where
  .<. :: Position -> Word64 -> Position
(.<.) Position
w Word64
n = Position -> Int -> Position
forall a. Bits a => a -> Int -> a
B.shiftL Position
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Position -> Word64 -> Position
(.>.) Position
w Word64
n = Position -> Int -> Position
forall a. Bits a => a -> Int -> a
B.shiftR Position
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Word8  where
  .<. :: Word8 -> Word64 -> Word8
(.<.) Word8
w Word64
n = Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
B.shiftL Word8
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Word8 -> Word64 -> Word8
(.>.) Word8
w Word64
n = Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
B.shiftR Word8
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Word16 where
  .<. :: Word16 -> Word64 -> Word16
(.<.) Word16
w Word64
n = Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
B.shiftL Word16
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Word16 -> Word64 -> Word16
(.>.) Word16
w Word64
n = Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
B.shiftR Word16
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Word32 where
  .<. :: Word32 -> Word64 -> Word32
(.<.) Word32
w Word64
n = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
B.shiftL Word32
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Word32 -> Word64 -> Word32
(.>.) Word32
w Word64
n = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
B.shiftR Word32
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

instance Shift Word64 where
  .<. :: Word64 -> Word64 -> Word64
(.<.) Word64
w Word64
n = Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
B.shiftL Word64
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.<.) #-}

  .>. :: Word64 -> Word64 -> Word64
(.>.) Word64
w Word64
n = Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
B.shiftR Word64
w (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
  {-# INLINE (.>.) #-}

-----

instance Bit Bool where
  bit :: Position -> Bool
bit Position
1 = Bool
True
  bit Position
_ = Bool
False
  {-# INLINE bit #-}

instance Bit Int where
  bit :: Position -> Int
bit Position
n = Int
1 Int -> Word64 -> Int
forall a. Shift a => a -> Word64 -> a
.<. Position -> Word64
forall a. ToCount a => a -> Word64
toCount Position
n
  {-# INLINE bit #-}

instance Bit Word8 where
  bit :: Position -> Word8
bit Position
n = Word8
1 Word8 -> Word64 -> Word8
forall a. Shift a => a -> Word64 -> a
.<. Position -> Word64
forall a. ToCount a => a -> Word64
toCount Position
n
  {-# INLINE bit #-}

instance Bit Word16 where
  bit :: Position -> Word16
bit Position
n = Word16
1 Word16 -> Word64 -> Word16
forall a. Shift a => a -> Word64 -> a
.<. Position -> Word64
forall a. ToCount a => a -> Word64
toCount Position
n
  {-# INLINE bit #-}

instance Bit Word32 where
  bit :: Position -> Word32
bit Position
n = Word32
1 Word32 -> Word64 -> Word32
forall a. Shift a => a -> Word64 -> a
.<. Position -> Word64
forall a. ToCount a => a -> Word64
toCount Position
n
  {-# INLINE bit #-}

instance Bit Word64 where
  bit :: Position -> Word64
bit Position
n = Word64
1 Word64 -> Word64 -> Word64
forall a. Shift a => a -> Word64 -> a
.<. Position -> Word64
forall a. ToCount a => a -> Word64
toCount Position
n
  {-# INLINE bit #-}

instance Bit (Naive Word8) where
  bit :: Position -> Naive Word8
bit Position
n = Word8 -> Naive Word8
forall a. a -> Naive a
Naive (Position -> Word8
forall a. Bit a => Position -> a
bit Position
n)
  {-# INLINE bit #-}

instance Bit (Naive Word16) where
  bit :: Position -> Naive Word16
bit Position
n = Word16 -> Naive Word16
forall a. a -> Naive a
Naive (Position -> Word16
forall a. Bit a => Position -> a
bit Position
n)
  {-# INLINE bit #-}

instance Bit (Naive Word32) where
  bit :: Position -> Naive Word32
bit Position
n = Word32 -> Naive Word32
forall a. a -> Naive a
Naive (Position -> Word32
forall a. Bit a => Position -> a
bit Position
n)
  {-# INLINE bit #-}

instance Bit (Naive Word64) where
  bit :: Position -> Naive Word64
bit Position
n = Word64 -> Naive Word64
forall a. a -> Naive a
Naive (Position -> Word64
forall a. Bit a => Position -> a
bit Position
n)
  {-# INLINE bit #-}