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

module Data.Maybe.Unpacked.Numeric.Complex.Double
  ( Complex (..)
  , toBaseComplex
  , fromBaseComplex
  , Maybe (..)
  , just
  , nothing
  , maybe
  , isJust
  , isNothing
  , fromMaybe
  , listToMaybe
  , maybeToList
  , catMaybes
  , mapMaybe
  , toBaseMaybe
  , fromBaseMaybe
  ) where

import Prelude hiding (Maybe, maybe)

import qualified Data.Complex as C
import GHC.Base (build)
import GHC.Exts (Double (D#), Double#, (==##))

import GHC.Read (Read (readPrec), expectP)
import Text.ParserCombinators.ReadPrec (prec, step)
import Text.Read (Lexeme (Ident), lexP, parens, (+++))
import qualified Prelude as P

data Complex = Complex Double# Double#

toBaseComplex :: Complex -> C.Complex Double
toBaseComplex :: Complex -> Complex Double
toBaseComplex (Complex Double#
d1# Double#
d2#) = (Double# -> Double
D# Double#
d1#) Double -> Double -> Complex Double
forall a. a -> a -> Complex a
C.:+ (Double# -> Double
D# Double#
d2#)

fromBaseComplex :: C.Complex Double -> Complex
fromBaseComplex :: Complex Double -> Complex
fromBaseComplex ((D# Double#
d1#) C.:+ (D# Double#
d2#)) = Double# -> Double# -> Complex
Complex Double#
d1# Double#
d2#

instance Eq Complex where
  Complex Double#
a Double#
b == :: Complex -> Complex -> Bool
== Complex Double#
c Double#
d =
    case Double#
a Double# -> Double# -> Int#
==## Double#
c of
      Int#
1# -> case Double#
b Double# -> Double# -> Int#
==## Double#
d of
        Int#
1# -> Bool
True
        Int#
_ -> Bool
False
      Int#
_ -> Bool
False

instance Show Complex where
  showsPrec :: Int -> Complex -> ShowS
showsPrec Int
p (Complex Double#
a Double#
b) =
    Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
      String -> ShowS
showString String
"Complex "
        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#
a)
        ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
        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#
b)

instance Read Complex where
  readPrec :: ReadPrec Complex
readPrec = ReadPrec Complex -> ReadPrec Complex
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec Complex -> ReadPrec Complex)
-> ReadPrec Complex -> ReadPrec Complex
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec Complex -> ReadPrec Complex
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec Complex -> ReadPrec Complex)
-> ReadPrec Complex -> ReadPrec Complex
forall a b. (a -> b) -> a -> b
$ do
    Lexeme -> ReadPrec ()
expectP (String -> Lexeme
Ident String
"Complex")
    (D# Double#
a) <- ReadPrec Double -> ReadPrec Double
forall a. ReadPrec a -> ReadPrec a
step ReadPrec Double
forall a. Read a => ReadPrec a
readPrec
    (D# Double#
b) <- ReadPrec Double -> ReadPrec Double
forall a. ReadPrec a -> ReadPrec a
step ReadPrec Double
forall a. Read a => ReadPrec a
readPrec
    Complex -> ReadPrec Complex
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double# -> Double# -> Complex
Complex Double#
a Double#
b)

data Maybe = Maybe (# (# #) | Complex #)

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

instance Show Maybe where
  showsPrec :: Int -> Maybe -> ShowS
showsPrec Int
p (Maybe (# (# #) | Complex #)
m) = case (# (# #) | Complex #)
m of
    (# (# #) | #) -> String -> ShowS
showString String
"nothing"
    (# | Complex
c #) ->
      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 -> Complex -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Complex
c

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
      Complex
a <- ReadPrec Complex -> ReadPrec Complex
forall a. ReadPrec a -> ReadPrec a
step ReadPrec Complex
forall a. Read a => ReadPrec a
readPrec
      Maybe -> ReadPrec Maybe
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return (Complex -> Maybe
just Complex
a)

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

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

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

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

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

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

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

just :: Complex -> Maybe
just :: Complex -> Maybe
just Complex
c = (# (# #) | Complex #) -> Maybe
Maybe (# | Complex
c #)

fromMaybe :: Complex -> Maybe -> Complex
fromMaybe :: Complex -> Maybe -> Complex
fromMaybe Complex
a (Maybe (# (# #) | Complex #)
m) = case (# (# #) | Complex #)
m of
  (# (# #) | #) -> Complex
a
  (# | Complex
c #) -> Complex
c

maybe :: a -> (Complex -> a) -> Maybe -> a
maybe :: forall a. a -> (Complex -> a) -> Maybe -> a
maybe a
a Complex -> a
f (Maybe (# (# #) | Complex #)
m) = case (# (# #) | Complex #)
m of
  (# (# #) | #) -> a
a
  (# | Complex
c #) -> Complex -> a
f Complex
c

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

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