module Data.BAByNF.Util.Binary ( Digit (..) , Seq (..) , val , toNum , fromVal , toChar ) where data Digit = B0 | B1 deriving (Digit -> Digit -> Bool (Digit -> Digit -> Bool) -> (Digit -> Digit -> Bool) -> Eq Digit forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: Digit -> Digit -> Bool == :: Digit -> Digit -> Bool $c/= :: Digit -> Digit -> Bool /= :: Digit -> Digit -> Bool Eq, Eq Digit Eq Digit => (Digit -> Digit -> Ordering) -> (Digit -> Digit -> Bool) -> (Digit -> Digit -> Bool) -> (Digit -> Digit -> Bool) -> (Digit -> Digit -> Bool) -> (Digit -> Digit -> Digit) -> (Digit -> Digit -> Digit) -> Ord Digit Digit -> Digit -> Bool Digit -> Digit -> Ordering Digit -> Digit -> Digit forall a. Eq a => (a -> a -> Ordering) -> (a -> a -> Bool) -> (a -> a -> Bool) -> (a -> a -> Bool) -> (a -> a -> Bool) -> (a -> a -> a) -> (a -> a -> a) -> Ord a $ccompare :: Digit -> Digit -> Ordering compare :: Digit -> Digit -> Ordering $c< :: Digit -> Digit -> Bool < :: Digit -> Digit -> Bool $c<= :: Digit -> Digit -> Bool <= :: Digit -> Digit -> Bool $c> :: Digit -> Digit -> Bool > :: Digit -> Digit -> Bool $c>= :: Digit -> Digit -> Bool >= :: Digit -> Digit -> Bool $cmax :: Digit -> Digit -> Digit max :: Digit -> Digit -> Digit $cmin :: Digit -> Digit -> Digit min :: Digit -> Digit -> Digit Ord) newtype Seq = Seq [Digit] deriving Seq -> Seq -> Bool (Seq -> Seq -> Bool) -> (Seq -> Seq -> Bool) -> Eq Seq forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: Seq -> Seq -> Bool == :: Seq -> Seq -> Bool $c/= :: Seq -> Seq -> Bool /= :: Seq -> Seq -> Bool Eq val :: Integral a => Digit -> a val :: forall a. Integral a => Digit -> a val Digit B0 = a 0 val Digit B1 = a 1 toNum :: Integral a => Seq -> a toNum :: forall a. Integral a => Seq -> a toNum (Seq [Digit] digits) = [Digit] -> a -> a forall {t}. Integral t => [Digit] -> t -> t toNum' [Digit] digits a 0 where toNum' :: [Digit] -> t -> t toNum' [] t acc = t acc toNum' (Digit d : [Digit] ds) t acc = let newAcc :: t newAcc = (t acc t -> t -> t forall a. Num a => a -> a -> a * t 2) t -> t -> t forall a. Num a => a -> a -> a + Digit -> t forall a. Integral a => Digit -> a val Digit d in [Digit] -> t -> t toNum' [Digit] ds t newAcc fromVal :: Integral a => a -> Maybe Digit fromVal :: forall a. Integral a => a -> Maybe Digit fromVal a 0 = Digit -> Maybe Digit forall a. a -> Maybe a Just Digit B0 fromVal a 1 = Digit -> Maybe Digit forall a. a -> Maybe a Just Digit B1 fromVal a _ = Maybe Digit forall a. Maybe a Nothing instance Show Seq where show :: Seq -> String show (Seq [Digit] x) = (Digit -> Char) -> [Digit] -> String forall a b. (a -> b) -> [a] -> [b] map Digit -> Char toChar [Digit] x toChar :: Digit -> Char toChar :: Digit -> Char toChar Digit B0 = Char '0' toChar Digit B1 = Char '1'