{-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} module Data.Maybe.Unpacked.Numeric.Word64 ( Maybe (..) , just , nothing , maybe , isJust , isNothing , fromMaybe , listToMaybe , maybeToList , catMaybes , mapMaybe , toBaseMaybe , fromBaseMaybe ) where import Prelude hiding (Maybe, maybe) import GHC.Base (build) import GHC.Exts (Word64#) import GHC.Word (Word64 (W64#)) import GHC.Read (Read (readPrec)) import Text.ParserCombinators.ReadPrec (prec, step) import Text.Read (Lexeme (Ident), lexP, parens, (+++)) import qualified Prelude as P data Maybe = Maybe (# (# #) | Word64# #) instance Eq Maybe where Maybe ma == :: Maybe -> Maybe -> Bool == Maybe mb = Bool -> (Word64 -> Bool) -> Maybe -> Bool forall a. a -> (Word64 -> a) -> Maybe -> a maybe (Maybe -> Bool isNothing Maybe mb) (\Word64 a -> Bool -> (Word64 -> Bool) -> Maybe -> Bool forall a. a -> (Word64 -> a) -> Maybe -> a maybe Bool False (\Word64 b -> Word64 a Word64 -> Word64 -> Bool forall a. Eq a => a -> a -> Bool == Word64 b) Maybe mb) Maybe ma instance Ord Maybe where compare :: Maybe -> Maybe -> Ordering compare (Maybe (# (# #) | Word64# #) ma) (Maybe (# (# #) | Word64# #) mb) = case (# (# #) | Word64# #) ma of (# (# #) | #) -> case (# (# #) | Word64# #) mb of (# (# #) | #) -> Ordering EQ (# | Word64# _ #) -> Ordering LT (# | Word64# a #) -> case (# (# #) | Word64# #) mb of (# (# #) | #) -> Ordering GT (# | Word64# b #) -> Word64 -> Word64 -> Ordering forall a. Ord a => a -> a -> Ordering compare (Word64# -> Word64 W64# Word64# a) (Word64# -> Word64 W64# Word64# b) instance Show Maybe where showsPrec :: Int -> Maybe -> ShowS showsPrec Int p (Maybe (# (# #) | Word64# #) m) = case (# (# #) | Word64# #) m of (# (# #) | #) -> String -> ShowS showString String "nothing" (# | Word64# w #) -> Bool -> ShowS -> ShowS showParen (Int p Int -> Int -> Bool forall a. Ord a => a -> a -> Bool > Int 10) (ShowS -> ShowS) -> ShowS -> ShowS forall a b. (a -> b) -> a -> b $ String -> ShowS showString String "just " ShowS -> ShowS -> ShowS forall b c a. (b -> c) -> (a -> b) -> a -> c . Int -> Word64 -> ShowS forall a. Show a => Int -> a -> ShowS showsPrec Int 11 (Word64# -> Word64 W64# Word64# w) instance Read Maybe where readPrec :: ReadPrec Maybe readPrec = ReadPrec Maybe -> ReadPrec Maybe forall a. ReadPrec a -> ReadPrec a parens (ReadPrec Maybe -> ReadPrec Maybe) -> ReadPrec Maybe -> ReadPrec Maybe forall a b. (a -> b) -> a -> b $ ReadPrec Maybe nothingP ReadPrec Maybe -> ReadPrec Maybe -> ReadPrec Maybe forall a. ReadPrec a -> ReadPrec a -> ReadPrec a +++ ReadPrec Maybe justP where nothingP :: ReadPrec Maybe nothingP = do Ident String "nothing" <- ReadPrec Lexeme lexP Maybe -> ReadPrec Maybe forall a. a -> ReadPrec a forall (m :: * -> *) a. Monad m => a -> m a return Maybe nothing justP :: ReadPrec Maybe justP = Int -> ReadPrec Maybe -> ReadPrec Maybe forall a. Int -> ReadPrec a -> ReadPrec a prec Int 10 (ReadPrec Maybe -> ReadPrec Maybe) -> ReadPrec Maybe -> ReadPrec Maybe forall a b. (a -> b) -> a -> b $ do Ident String "just" <- ReadPrec Lexeme lexP Word64 a <- ReadPrec Word64 -> ReadPrec Word64 forall a. ReadPrec a -> ReadPrec a step ReadPrec Word64 forall a. Read a => ReadPrec a readPrec Maybe -> ReadPrec Maybe forall a. a -> ReadPrec a forall (m :: * -> *) a. Monad m => a -> m a return (Word64 -> Maybe just Word64 a) listToMaybe :: [Word64] -> Maybe listToMaybe :: [Word64] -> Maybe listToMaybe [] = Maybe nothing listToMaybe (Word64 x : [Word64] _) = Word64 -> Maybe just Word64 x maybeToList :: Maybe -> [Word64] maybeToList :: Maybe -> [Word64] maybeToList = [Word64] -> (Word64 -> [Word64]) -> Maybe -> [Word64] forall a. a -> (Word64 -> a) -> Maybe -> a maybe [] (Word64 -> [Word64] -> [Word64] forall a. a -> [a] -> [a] : []) catMaybes :: [Maybe] -> [Word64] catMaybes :: [Maybe] -> [Word64] catMaybes = (Maybe -> Maybe) -> [Maybe] -> [Word64] forall a. (a -> Maybe) -> [a] -> [Word64] mapMaybe Maybe -> Maybe forall a. a -> a id mapMaybe :: (a -> Maybe) -> [a] -> [Word64] mapMaybe :: forall a. (a -> Maybe) -> [a] -> [Word64] mapMaybe a -> Maybe _ [] = [] mapMaybe a -> Maybe f (a a : [a] as) = let ws :: [Word64] ws = (a -> Maybe) -> [a] -> [Word64] forall a. (a -> Maybe) -> [a] -> [Word64] mapMaybe a -> Maybe f [a] as in [Word64] -> (Word64 -> [Word64]) -> Maybe -> [Word64] forall a. a -> (Word64 -> a) -> Maybe -> a maybe [Word64] ws (Word64 -> [Word64] -> [Word64] forall a. a -> [a] -> [a] : [Word64] ws) (a -> Maybe f a a) {-# NOINLINE [1] mapMaybe #-} {-# RULES "mapMaybe" [~1] forall f xs. mapMaybe f xs = build (\c n -> foldr (mapMaybeFB c f) n xs) "mapMaybeList" [1] forall f. foldr (mapMaybeFB (:) f) [] = mapMaybe f #-} {-# NOINLINE [0] mapMaybeFB #-} mapMaybeFB :: (Word64 -> r -> r) -> (a -> Maybe) -> a -> r -> r mapMaybeFB :: forall r a. (Word64 -> r -> r) -> (a -> Maybe) -> a -> r -> r mapMaybeFB Word64 -> r -> r cons a -> Maybe f a x r next = r -> (Word64 -> r) -> Maybe -> r forall a. a -> (Word64 -> a) -> Maybe -> a maybe r next ((Word64 -> r -> r) -> r -> Word64 -> r forall a b c. (a -> b -> c) -> b -> a -> c flip Word64 -> r -> r cons r next) (a -> Maybe f a x) isNothing :: Maybe -> Bool isNothing :: Maybe -> Bool isNothing = Bool -> (Word64 -> Bool) -> Maybe -> Bool forall a. a -> (Word64 -> a) -> Maybe -> a maybe Bool True (Bool -> Word64 -> Bool forall a b. a -> b -> a const Bool False) isJust :: Maybe -> Bool isJust :: Maybe -> Bool isJust = Bool -> (Word64 -> Bool) -> Maybe -> Bool forall a. a -> (Word64 -> a) -> Maybe -> a maybe Bool False (Bool -> Word64 -> Bool forall a b. a -> b -> a const Bool True) nothing :: Maybe nothing :: Maybe nothing = (# (# #) | Word64# #) -> Maybe Maybe (# (# #) | #) just :: Word64 -> Maybe just :: Word64 -> Maybe just (W64# Word64# w) = (# (# #) | Word64# #) -> Maybe Maybe (# | Word64# w #) fromMaybe :: Word64 -> Maybe -> Word64 fromMaybe :: Word64 -> Maybe -> Word64 fromMaybe Word64 a (Maybe (# (# #) | Word64# #) m) = case (# (# #) | Word64# #) m of (# (# #) | #) -> Word64 a (# | Word64# w #) -> Word64# -> Word64 W64# Word64# w maybe :: a -> (Word64 -> a) -> Maybe -> a maybe :: forall a. a -> (Word64 -> a) -> Maybe -> a maybe a a Word64 -> a f (Maybe (# (# #) | Word64# #) m) = case (# (# #) | Word64# #) m of (# (# #) | #) -> a a (# | Word64# w #) -> Word64 -> a f (Word64# -> Word64 W64# Word64# w) toBaseMaybe :: Maybe -> P.Maybe Word64 toBaseMaybe :: Maybe -> Maybe Word64 toBaseMaybe = Maybe Word64 -> (Word64 -> Maybe Word64) -> Maybe -> Maybe Word64 forall a. a -> (Word64 -> a) -> Maybe -> a maybe Maybe Word64 forall a. Maybe a P.Nothing Word64 -> Maybe Word64 forall a. a -> Maybe a P.Just fromBaseMaybe :: P.Maybe Word64 -> Maybe fromBaseMaybe :: Maybe Word64 -> Maybe fromBaseMaybe = Maybe -> (Word64 -> Maybe) -> Maybe Word64 -> Maybe forall b a. b -> (a -> b) -> Maybe a -> b P.maybe Maybe nothing Word64 -> Maybe just