{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}

module Data.Maybe.Unpacked.Numeric.Word
  ( 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 (Word#)
import GHC.Word (Word (W#))

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

instance Eq Maybe where
  Maybe
ma == :: Maybe -> Maybe -> Bool
== Maybe
mb =
    Bool -> (Word -> Bool) -> Maybe -> Bool
forall a. a -> (Word -> a) -> Maybe -> a
maybe
      (Maybe -> Bool
isNothing Maybe
mb)
      (\Word
a -> Bool -> (Word -> Bool) -> Maybe -> Bool
forall a. a -> (Word -> a) -> Maybe -> a
maybe Bool
False (\Word
b -> Word
a Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
b) Maybe
mb)
      Maybe
ma

instance Ord Maybe where
  compare :: Maybe -> Maybe -> Ordering
compare (Maybe (# (# #) | Word# #)
ma) (Maybe (# (# #) | Word# #)
mb) = case (# (# #) | Word# #)
ma of
    (# (# #) | #) -> case (# (# #) | Word# #)
mb of
      (# (# #) | #) -> Ordering
EQ
      (# | Word#
_ #) -> Ordering
LT
    (# | Word#
a #) -> case (# (# #) | Word# #)
mb of
      (# (# #) | #) -> Ordering
GT
      (# | Word#
b #) -> Word -> Word -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Word# -> Word
W# Word#
a) (Word# -> Word
W# Word#
b)

instance Show Maybe where
  showsPrec :: Int -> Maybe -> ShowS
showsPrec Int
p (Maybe (# (# #) | Word# #)
m) = case (# (# #) | Word# #)
m of
    (# (# #) | #) -> String -> ShowS
showString String
"nothing"
    (# | Word#
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 -> Word -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Word# -> Word
W# Word#
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
      Word
a <- ReadPrec Word -> ReadPrec Word
forall a. ReadPrec a -> ReadPrec a
step ReadPrec Word
forall a. Read a => ReadPrec a
readPrec
      Maybe -> ReadPrec Maybe
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return (Word -> Maybe
just Word
a)

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

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

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

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

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

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

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

just :: Word -> Maybe
just :: Word -> Maybe
just (W# Word#
w) = (# (# #) | Word# #) -> Maybe
Maybe (# | Word#
w #)

fromMaybe :: Word -> Maybe -> Word
fromMaybe :: Word -> Maybe -> Word
fromMaybe Word
a (Maybe (# (# #) | Word# #)
m) = case (# (# #) | Word# #)
m of
  (# (# #) | #) -> Word
a
  (# | Word#
w #) -> Word# -> Word
W# Word#
w

maybe :: a -> (Word -> a) -> Maybe -> a
maybe :: forall a. a -> (Word -> a) -> Maybe -> a
maybe a
a Word -> a
f (Maybe (# (# #) | Word# #)
m) = case (# (# #) | Word# #)
m of
  (# (# #) | #) -> a
a
  (# | Word#
w #) -> Word -> a
f (Word# -> Word
W# Word#
w)

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

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