{-# LINE 1 "lib/CPython/Types/Complex.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.Complex
( Complex
, complexType
, toComplex
, fromComplex
) where
import qualified Data.Complex as C
import CPython.Internal
newtype Complex = Complex (ForeignPtr Complex)
instance Object Complex where
toObject (Complex x) = SomeObject x
fromForeignPtr = Complex
instance Concrete Complex where
concreteType _ = complexType
complexType :: (Type)
complexType =
unsafePerformIO $
let {res = complexType'_} in
peekStaticObject res >>= \res' ->
return (res')
{-# LINE 41 "lib/CPython/Types/Complex.chs" #-}
toComplex :: C.Complex Double -> IO Complex
toComplex x = raw >>= stealObject where
real = realToFrac $ C.realPart x
imag = realToFrac $ C.imagPart x
raw = pyComplexFromDoubles real imag
fromComplex :: Complex -> IO (C.Complex Double)
fromComplex py = withObject py $ \pyPtr -> do
real <- pyComplexRealAsDouble pyPtr
imag <- pyComplexImagAsDouble pyPtr
return $ realToFrac real C.:+ realToFrac imag
foreign import ccall unsafe "CPython/Types/Complex.chs.h hscpython_PyComplex_Type"
complexType'_ :: (Ptr ())
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_FromDoubles"
pyComplexFromDoubles :: (CDouble -> (CDouble -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_RealAsDouble"
pyComplexRealAsDouble :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_ImagAsDouble"
pyComplexImagAsDouble :: ((Ptr ()) -> (IO CDouble))