bin-0.1: Bin: binary natural numbers.

Data.Bin

Description

Binary natural numbers, Bin.

This module is designed to be imported qualified.

Synopsis

# Binary natural numbers

data Bin Source #

Binary natural numbers.

Numbers are represented in little-endian order, the representation is unique.

>>> mapM_ (putStrLn .  explicitShow) [0 .. 7]
BZ
BP BE
BP (B0 BE)
BP (B1 BE)
BP (B0 (B0 BE))
BP (B1 (B0 BE))
BP (B0 (B1 BE))
BP (B1 (B1 BE))


Constructors

 BZ zero BP BinP non-zero
Instances
 Source # >>> take 10 $iterate succ BZ [0,1,2,3,4,5,6,7,8,9] >>> take 10 [BZ ..] [0,1,2,3,4,5,6,7,8,9]  Instance detailsDefined in Data.Bin Methodssucc :: Bin -> Bin #pred :: Bin -> Bin #toEnum :: Int -> Bin #fromEnum :: Bin -> Int #enumFrom :: Bin -> [Bin] #enumFromThen :: Bin -> Bin -> [Bin] #enumFromTo :: Bin -> Bin -> [Bin] #enumFromThenTo :: Bin -> Bin -> Bin -> [Bin] # Source # Instance detailsDefined in Data.Bin Methods(==) :: Bin -> Bin -> Bool #(/=) :: Bin -> Bin -> Bool # Source # Instance detailsDefined in Data.Bin Methodsquot :: Bin -> Bin -> Bin #rem :: Bin -> Bin -> Bin #div :: Bin -> Bin -> Bin #mod :: Bin -> Bin -> Bin #quotRem :: Bin -> Bin -> (Bin, Bin) #divMod :: Bin -> Bin -> (Bin, Bin) # Source # Instance detailsDefined in Data.Bin Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Bin -> c Bin #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Bin #dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Bin) #dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Bin) #gmapT :: (forall b. Data b => b -> b) -> Bin -> Bin #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Bin -> r #gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Bin -> r #gmapQ :: (forall d. Data d => d -> u) -> Bin -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> Bin -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> Bin -> m Bin #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Bin -> m Bin #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Bin -> m Bin # Source # >>> 0 + 2 :: Bin 2 >>> 1 + 2 :: Bin 3 >>> 4 * 8 :: Bin 32 >>> 7 * 7 :: Bin 49  Instance detailsDefined in Data.Bin Methods(+) :: Bin -> Bin -> Bin #(-) :: Bin -> Bin -> Bin #(*) :: Bin -> Bin -> Bin #negate :: Bin -> Bin #abs :: Bin -> Bin #signum :: Bin -> Bin # Source # Instance detailsDefined in Data.Bin Methodscompare :: Bin -> Bin -> Ordering #(<) :: Bin -> Bin -> Bool #(<=) :: Bin -> Bin -> Bool #(>) :: Bin -> Bin -> Bool #(>=) :: Bin -> Bin -> Bool #max :: Bin -> Bin -> Bin #min :: Bin -> Bin -> Bin # Source # Instance detailsDefined in Data.Bin Methods Source # Bin is printed as Natural.To see explicit structure, use explicitShow or explicitShowsPrec Instance detailsDefined in Data.Bin MethodsshowsPrec :: Int -> Bin -> ShowS #show :: Bin -> String #showList :: [Bin] -> ShowS # Source # Instance detailsDefined in Data.Bin Methodsfunction :: (Bin -> b) -> Bin :-> b # Source # Instance detailsDefined in Data.Bin Methodsshrink :: Bin -> [Bin] # Source # Instance detailsDefined in Data.Bin Methodscoarbitrary :: Bin -> Gen b -> Gen b # Source # Instance detailsDefined in Data.Bin Methods(.&.) :: Bin -> Bin -> Bin #(.|.) :: Bin -> Bin -> Bin #xor :: Bin -> Bin -> Bin #shift :: Bin -> Int -> Bin #rotate :: Bin -> Int -> Bin #bit :: Int -> Bin #setBit :: Bin -> Int -> Bin #clearBit :: Bin -> Int -> Bin #complementBit :: Bin -> Int -> Bin #testBit :: Bin -> Int -> Bool #bitSize :: Bin -> Int #isSigned :: Bin -> Bool #shiftL :: Bin -> Int -> Bin #unsafeShiftL :: Bin -> Int -> Bin #shiftR :: Bin -> Int -> Bin #unsafeShiftR :: Bin -> Int -> Bin #rotateL :: Bin -> Int -> Bin #rotateR :: Bin -> Int -> Bin #popCount :: Bin -> Int # Source # Instance detailsDefined in Data.Bin Methodsrnf :: Bin -> () # Source # Instance detailsDefined in Data.Bin MethodshashWithSalt :: Int -> Bin -> Int #hash :: Bin -> Int # Source # Instance detailsDefined in Data.Type.Bin MethodstestEquality :: SBin a -> SBin b -> Maybe (a :~: b) # Convert Bin to Natural >>> toNatural 0 0  >>> toNatural 2 2  >>> toNatural$ BP $B0$ B1 \$ BE
6


Convert Natural to Nat

>>> fromNatural 4
4

>>> explicitShow (fromNatural 4)
"BP (B0 (B0 BE))"


Convert from Bin to Nat.

>>> toNat 5
5

>>> N.explicitShow (toNat 5)
"S (S (S (S (S Z))))"


Convert from Nat to Bin.

>>> fromNat 5
5

>>> explicitShow (fromNat 5)
"BP (B1 (B0 BE))"


Arguments

 :: a $$0$$ -> a $$1$$ -> (a -> a) $$2x$$ -> (a -> a) $$2x + 1$$ -> Bin -> a

Fold Bin.

# Positive natural numbers

data BinP Source #

Non-zero binary natural numbers.

We could have called this type Bin1, but that's used as type alias for promoted BP BE in Data.Type.Bin.

Constructors

 BE one B0 BinP mult2 B1 BinP mult2 plus 1
Instances
 Source # Instance detailsDefined in Data.BinP Methodssucc :: BinP -> BinP #pred :: BinP -> BinP #toEnum :: Int -> BinP #fromEnum :: BinP -> Int #enumFrom :: BinP -> [BinP] #enumFromThen :: BinP -> BinP -> [BinP] #enumFromTo :: BinP -> BinP -> [BinP] #enumFromThenTo :: BinP -> BinP -> BinP -> [BinP] # Source # Instance detailsDefined in Data.BinP Methods(==) :: BinP -> BinP -> Bool #(/=) :: BinP -> BinP -> Bool # Source # Instance detailsDefined in Data.BinP Methodsquot :: BinP -> BinP -> BinP #rem :: BinP -> BinP -> BinP #div :: BinP -> BinP -> BinP #mod :: BinP -> BinP -> BinP #quotRem :: BinP -> BinP -> (BinP, BinP) #divMod :: BinP -> BinP -> (BinP, BinP) # Source # Instance detailsDefined in Data.BinP Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> BinP -> c BinP #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c BinP #dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c BinP) #dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c BinP) #gmapT :: (forall b. Data b => b -> b) -> BinP -> BinP #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> BinP -> r #gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> BinP -> r #gmapQ :: (forall d. Data d => d -> u) -> BinP -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> BinP -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> BinP -> m BinP #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> BinP -> m BinP #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> BinP -> m BinP # Source # Instance detailsDefined in Data.BinP Methods(+) :: BinP -> BinP -> BinP #(-) :: BinP -> BinP -> BinP #(*) :: BinP -> BinP -> BinP #negate :: BinP -> BinP #abs :: BinP -> BinP #signum :: BinP -> BinP # Source # >>> sort [1 .. 9 :: BinP] [1,2,3,4,5,6,7,8,9]  Instance detailsDefined in Data.BinP Methodscompare :: BinP -> BinP -> Ordering #(<) :: BinP -> BinP -> Bool #(<=) :: BinP -> BinP -> Bool #(>) :: BinP -> BinP -> Bool #(>=) :: BinP -> BinP -> Bool #max :: BinP -> BinP -> BinP #min :: BinP -> BinP -> BinP # Source # Instance detailsDefined in Data.BinP Methods Source # Instance detailsDefined in Data.BinP MethodsshowsPrec :: Int -> BinP -> ShowS #show :: BinP -> String #showList :: [BinP] -> ShowS # Source # Instance detailsDefined in Data.BinP Methodsfunction :: (BinP -> b) -> BinP :-> b # Source # Instance detailsDefined in Data.BinP Methodsshrink :: BinP -> [BinP] # Source # Instance detailsDefined in Data.BinP Methodscoarbitrary :: BinP -> Gen b -> Gen b # Source # NOTE: .&., xor, shiftR and rotateR are __NOT_ implemented. They may make number zero. Instance detailsDefined in Data.BinP Methods(.&.) :: BinP -> BinP -> BinP #(.|.) :: BinP -> BinP -> BinP #xor :: BinP -> BinP -> BinP #shift :: BinP -> Int -> BinP #rotate :: BinP -> Int -> BinP #bit :: Int -> BinP #setBit :: BinP -> Int -> BinP #clearBit :: BinP -> Int -> BinP #testBit :: BinP -> Int -> Bool #bitSize :: BinP -> Int #shiftL :: BinP -> Int -> BinP #unsafeShiftL :: BinP -> Int -> BinP #shiftR :: BinP -> Int -> BinP #unsafeShiftR :: BinP -> Int -> BinP #rotateL :: BinP -> Int -> BinP #rotateR :: BinP -> Int -> BinP #popCount :: BinP -> Int # Source # Instance detailsDefined in Data.BinP Methodsrnf :: BinP -> () # Source # Instance detailsDefined in Data.BinP MethodshashWithSalt :: Int -> BinP -> Int #hash :: BinP -> Int # Source # Instance detailsDefined in Data.Type.BinP MethodstestEquality :: SBinP a -> SBinP b -> Maybe (a :~: b) #

# Showing

show displaying a structure of Bin.

>>> explicitShow 0
"BZ"

>>> explicitShow 2
"BP (B0 BE)"


showsPrec displaying a structure of Bin.

# Extras

This is a total function.

>>> map predP [1..10]
[0,1,2,3,4,5,6,7,8,9]