{-# LANGUAGE BangPatterns #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedSums #-} {-# LANGUAGE UnboxedTuples #-} module Data.Maybe.Unpacked.Numeric.Double ( 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 (Double#,Double(D#)) import GHC.Read (Read(readPrec)) import Text.Read (parens, Lexeme(Ident), lexP, (+++)) import Text.ParserCombinators.ReadPrec (prec, step) import qualified Prelude as P data Maybe = Maybe (# (# #) | Double# #) instance Eq Maybe where Maybe ma == :: Maybe -> Maybe -> Bool == Maybe mb = Bool -> (Double -> Bool) -> Maybe -> Bool forall a. a -> (Double -> a) -> Maybe -> a maybe (Maybe -> Bool isNothing Maybe mb) (\Double a -> Bool -> (Double -> Bool) -> Maybe -> Bool forall a. a -> (Double -> a) -> Maybe -> a maybe Bool False (\Double b -> Double a Double -> Double -> Bool forall a. Eq a => a -> a -> Bool == Double b) Maybe mb) Maybe ma instance Ord Maybe where compare :: Maybe -> Maybe -> Ordering compare Maybe ma Maybe mb = Ordering -> (Double -> Ordering) -> Maybe -> Ordering forall a. a -> (Double -> a) -> Maybe -> a maybe Ordering LT (\Double a -> Ordering -> (Double -> Ordering) -> Maybe -> Ordering forall a. a -> (Double -> a) -> Maybe -> a maybe Ordering GT (Double -> Double -> Ordering forall a. Ord a => a -> a -> Ordering compare Double a) Maybe mb) Maybe ma instance Show Maybe where showsPrec :: Int -> Maybe -> ShowS showsPrec Int p (Maybe (# (# #) | Double# #) m) = case (# (# #) | Double# #) m of (# (# #) | #) -> String -> ShowS showString String "nothing" (# | Double# d #) -> 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 -> Double -> ShowS forall a. Show a => Int -> a -> ShowS showsPrec Int 11 (Double# -> Double D# Double# d) 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 (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 Double a <- ReadPrec Double -> ReadPrec Double forall a. ReadPrec a -> ReadPrec a step ReadPrec Double forall a. Read a => ReadPrec a readPrec Maybe -> ReadPrec Maybe forall (m :: * -> *) a. Monad m => a -> m a return (Double -> Maybe just Double a) listToMaybe :: [Double] -> Maybe listToMaybe :: [Double] -> Maybe listToMaybe [] = Maybe nothing listToMaybe (Double x:[Double] _) = Double -> Maybe just Double x maybeToList :: Maybe -> [Double] maybeToList :: Maybe -> [Double] maybeToList = [Double] -> (Double -> [Double]) -> Maybe -> [Double] forall a. a -> (Double -> a) -> Maybe -> a maybe [] (Double -> [Double] -> [Double] forall a. a -> [a] -> [a] : []) catMaybes :: [Maybe] -> [Double] catMaybes :: [Maybe] -> [Double] catMaybes = (Maybe -> Maybe) -> [Maybe] -> [Double] forall a. (a -> Maybe) -> [a] -> [Double] mapMaybe Maybe -> Maybe forall a. a -> a id mapMaybe :: (a -> Maybe) -> [a] -> [Double] mapMaybe :: (a -> Maybe) -> [a] -> [Double] mapMaybe a -> Maybe _ [] = [] mapMaybe a -> Maybe f (a a : [a] as) = let ws :: [Double] ws = (a -> Maybe) -> [a] -> [Double] forall a. (a -> Maybe) -> [a] -> [Double] mapMaybe a -> Maybe f [a] as in [Double] -> (Double -> [Double]) -> Maybe -> [Double] forall a. a -> (Double -> a) -> Maybe -> a maybe [Double] ws (Double -> [Double] -> [Double] forall a. a -> [a] -> [a] : [Double] 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 :: (Double -> r -> r) -> (a -> Maybe) -> a -> r -> r mapMaybeFB :: (Double -> r -> r) -> (a -> Maybe) -> a -> r -> r mapMaybeFB Double -> r -> r cons a -> Maybe f a x r next = r -> (Double -> r) -> Maybe -> r forall a. a -> (Double -> a) -> Maybe -> a maybe r next ((Double -> r -> r) -> r -> Double -> r forall a b c. (a -> b -> c) -> b -> a -> c flip Double -> r -> r cons r next) (a -> Maybe f a x) isNothing :: Maybe -> Bool isNothing :: Maybe -> Bool isNothing = Bool -> (Double -> Bool) -> Maybe -> Bool forall a. a -> (Double -> a) -> Maybe -> a maybe Bool True (Bool -> Double -> Bool forall a b. a -> b -> a const Bool False) isJust :: Maybe -> Bool isJust :: Maybe -> Bool isJust = Bool -> (Double -> Bool) -> Maybe -> Bool forall a. a -> (Double -> a) -> Maybe -> a maybe Bool False (Bool -> Double -> Bool forall a b. a -> b -> a const Bool True) nothing :: Maybe nothing :: Maybe nothing = (# (# #) | Double# #) -> Maybe Maybe (# (# #) | #) just :: Double -> Maybe just :: Double -> Maybe just (D# Double# d) = (# (# #) | Double# #) -> Maybe Maybe (# | Double# d #) fromMaybe :: Double -> Maybe -> Double fromMaybe :: Double -> Maybe -> Double fromMaybe Double a (Maybe (# (# #) | Double# #) m) = case (# (# #) | Double# #) m of (# (# #) | #) -> Double a (# | Double# d #) -> Double# -> Double D# Double# d maybe :: a -> (Double -> a) -> Maybe -> a maybe :: a -> (Double -> a) -> Maybe -> a maybe a a Double -> a f (Maybe (# (# #) | Double# #) m) = case (# (# #) | Double# #) m of (# (# #) | #) -> a a (# | Double# d #) -> Double -> a f (Double# -> Double D# Double# d) toBaseMaybe :: Maybe -> P.Maybe Double toBaseMaybe :: Maybe -> Maybe Double toBaseMaybe = Maybe Double -> (Double -> Maybe Double) -> Maybe -> Maybe Double forall a. a -> (Double -> a) -> Maybe -> a maybe Maybe Double forall a. Maybe a P.Nothing Double -> Maybe Double forall a. a -> Maybe a P.Just fromBaseMaybe :: P.Maybe Double -> Maybe fromBaseMaybe :: Maybe Double -> Maybe fromBaseMaybe = Maybe -> (Double -> Maybe) -> Maybe Double -> Maybe forall b a. b -> (a -> b) -> Maybe a -> b P.maybe Maybe nothing Double -> Maybe just