{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedSums #-}
{-# LANGUAGE UnboxedTuples #-}

module Data.Maybe.Unpacked.Numeric.Word128
  ( Maybe(..)
  , just
  , nothing

  , maybe

  , isJust
  , isNothing
  , fromMaybe
  , listToMaybe
  , maybeToList
  , catMaybes
  , mapMaybe

  , toBaseMaybe
  , fromBaseMaybe
  ) where   

import Prelude hiding (Maybe,maybe)

import Data.WideWord (Word128(..))
import GHC.Base (build)
import GHC.Exts (Word#)
import GHC.Word (Word64(W64#))

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 (# (# #) | (# Word#, Word# #) #)

instance Eq Maybe where
  Maybe
ma == :: Maybe -> Maybe -> Bool
== Maybe
mb =
    forall a. a -> (Word128 -> a) -> Maybe -> a
maybe (Maybe -> Bool
isNothing Maybe
mb)
          (\Word128
a -> forall a. a -> (Word128 -> a) -> Maybe -> a
maybe Bool
False (\Word128
b -> Word128
a forall a. Eq a => a -> a -> Bool
== Word128
b) Maybe
mb) Maybe
ma
    
instance Ord Maybe where
  compare :: Maybe -> Maybe -> Ordering
compare Maybe
ma Maybe
mb = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe Ordering
LT (\Word128
a -> forall a. a -> (Word128 -> a) -> Maybe -> a
maybe Ordering
GT (forall a. Ord a => a -> a -> Ordering
compare Word128
a) Maybe
mb) Maybe
ma  

instance Show Maybe where
  showsPrec :: Int -> Maybe -> ShowS
showsPrec Int
p (Maybe (# (# #) | (# Word#, Word# #) #)
m) = case (# (# #) | (# Word#, Word# #) #)
m of
    (# (# #) | #) -> String -> ShowS
showString String
"nothing"
    (# | (# Word#
wa, Word#
wb #) #) -> Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10)
      forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"just "
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Word64 -> Word64 -> Word128
Word128 (Word# -> Word64
W64# Word#
wa) (Word# -> Word64
W64# Word#
wb))

instance Read Maybe where
  readPrec :: ReadPrec Maybe
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ ReadPrec Maybe
nothingP forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
+++ ReadPrec Maybe
justP
    where
      nothingP :: ReadPrec Maybe
nothingP = forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
        Ident String
"nothing" <- ReadPrec Lexeme
lexP
        forall (m :: * -> *) a. Monad m => a -> m a
return Maybe
nothing
      justP :: ReadPrec Maybe
justP = forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
        Ident String
"just" <- ReadPrec Lexeme
lexP
        Word128
a <- forall a. ReadPrec a -> ReadPrec a
step forall a. Read a => ReadPrec a
readPrec
        forall (m :: * -> *) a. Monad m => a -> m a
return (Word128 -> Maybe
just Word128
a)

listToMaybe :: [Word128] -> Maybe
listToMaybe :: [Word128] -> Maybe
listToMaybe [] = Maybe
nothing
listToMaybe (Word128
x:[Word128]
_) = Word128 -> Maybe
just Word128
x

maybeToList :: Maybe -> [Word128]
maybeToList :: Maybe -> [Word128]
maybeToList = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe [] (forall a. a -> [a] -> [a]
: [])

catMaybes :: [Maybe] -> [Word128]
catMaybes :: [Maybe] -> [Word128]
catMaybes = forall a. (a -> Maybe) -> [a] -> [Word128]
mapMaybe forall a. a -> a
id

mapMaybe :: (a -> Maybe) -> [a] -> [Word128]
mapMaybe :: forall a. (a -> Maybe) -> [a] -> [Word128]
mapMaybe a -> Maybe
_ [] = []
mapMaybe a -> Maybe
f (a
a : [a]
as) =
  let ws :: [Word128]
ws = forall a. (a -> Maybe) -> [a] -> [Word128]
mapMaybe a -> Maybe
f [a]
as
  in forall a. a -> (Word128 -> a) -> Maybe -> a
maybe [Word128]
ws (forall a. a -> [a] -> [a]
: [Word128]
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 :: (Word128 -> r -> r) -> (a -> Maybe) -> a -> r -> r
mapMaybeFB :: forall r a. (Word128 -> r -> r) -> (a -> Maybe) -> a -> r -> r
mapMaybeFB Word128 -> r -> r
cons a -> Maybe
f a
x r
next = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe r
next (forall a b c. (a -> b -> c) -> b -> a -> c
flip Word128 -> r -> r
cons r
next) (a -> Maybe
f a
x)

isNothing :: Maybe -> Bool
isNothing :: Maybe -> Bool
isNothing = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe Bool
True (forall a b. a -> b -> a
const Bool
False)

isJust :: Maybe -> Bool
isJust :: Maybe -> Bool
isJust = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe Bool
False (forall a b. a -> b -> a
const Bool
True)

nothing :: Maybe
nothing :: Maybe
nothing = (# (# #) | (# Word#, Word# #) #) -> Maybe
Maybe (# (# #) | #)

just :: Word128 -> Maybe
just :: Word128 -> Maybe
just (Word128 (W64# Word#
wa) (W64# Word#
wb)) = (# (# #) | (# Word#, Word# #) #) -> Maybe
Maybe (# | (# Word#
wa, Word#
wb #) #)

fromMaybe :: Word128 -> Maybe -> Word128
fromMaybe :: Word128 -> Maybe -> Word128
fromMaybe Word128
a (Maybe (# (# #) | (# Word#, Word# #) #)
m) = case (# (# #) | (# Word#, Word# #) #)
m of
  (# (# #) | #) -> Word128
a
  (# | (# Word#
wa, Word#
wb #) #) -> Word64 -> Word64 -> Word128
Word128 (Word# -> Word64
W64# Word#
wa) (Word# -> Word64
W64# Word#
wb)

maybe :: a -> (Word128 -> a) -> Maybe -> a
maybe :: forall a. a -> (Word128 -> a) -> Maybe -> a
maybe a
a Word128 -> a
f (Maybe (# (# #) | (# Word#, Word# #) #)
m) = case (# (# #) | (# Word#, Word# #) #)
m of
  (# (# #) | #) -> a
a
  (# | (# Word#
wa, Word#
wb #) #) -> Word128 -> a
f (Word64 -> Word64 -> Word128
Word128 (Word# -> Word64
W64# Word#
wa) (Word# -> Word64
W64# Word#
wb))

toBaseMaybe :: Maybe -> P.Maybe Word128
toBaseMaybe :: Maybe -> Maybe Word128
toBaseMaybe = forall a. a -> (Word128 -> a) -> Maybe -> a
maybe forall a. Maybe a
P.Nothing forall a. a -> Maybe a
P.Just

fromBaseMaybe :: P.Maybe Word128 -> Maybe
fromBaseMaybe :: Maybe Word128 -> Maybe
fromBaseMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
P.maybe Maybe
nothing Word128 -> Maybe
just