module QuantLib.Internal
  (
    Error(..)

  , Day -- reexport for simplicity
  , minDate
  , maxDate

  -- marshalling helpers
  , prePtr
  , preErrorCheck
  , errorCheck

  , fromMaybeBool
  , toMaybeBool
  , peekDynString
  , peekEnum
  , peekDouble
  , preEnum
  , withComplex
  , preComplex
  , peekComplex
  , preNum
  , preArray
  , preArrayWith
  , preIntArray
  , preDoubleArray
  , preCStringArray
  , prePtrArray
  , withEnumArray
  , withIntArray
  , withBoolArray
  , withDoubleArray
  , withRealVector
  , withRealVectorRaw
  , withNonEmptyDoubleArray
  , withDoubleArrayRaw
  , withBoolArrayRaw
  , withDayPtr
  , withStringArray
  , peekCStringArray
  , fromEnumQuantity
  , toEnumQuantity
  , fromEnumDouble
  , toEnumDouble
  , fromEnumC
  , toEnumC

  , withDay
  , toDay
  , withMaybeDay
  , fromMaybeInt
  , toMaybeDay
  , peekDayArray
  , peekBoolArray
  , withDayArray
  , peekDoubleArray
  , peekRealVector
  , borrowRealVector
  , RealVector
  , NonEmptyVector
  , singletonNonEmptyVector
  , consNonEmptyVector
  , nonEmptyVector
  , nonEmptyVectorToVector
  , withNonEmptyRealVector

  , toSerial
  , fromSerial
  , qlSavedSettings
  , qlFreeSavedSettings
  , fromMaybeDouble
  , fromMaybeEnum
  , fromMaybeEnumQuantity
  , peekIntArray
  , peekIntArray'
  , peekUIntArray
  , peekWord
  , peekStructArray
  , peekPtrArray
  , Matrix(..)
  , RealMatrix(..)
  , matrixRows
  , matrixColumns
  , matrixData
  , realMatrixRows
  , realMatrixColumns
  , realMatrixData
  , boxedRealMatrix
  , realMatrixFromVector
  , objectMatrix
  , qlNullInteger
  , qlFreeAdditionalResults

  , uncurryNested
  , zipWith6
  )
where

import Data.Complex(Complex((:+)))
import Foreign.C.Types(CUInt(..), CInt(..), CDouble(..))
import Foreign.C.String(CString, peekCString, withCString)
import Foreign.Ptr(Ptr, nullPtr, castPtr)
import Foreign.ForeignPtr(FinalizerPtr, newForeignPtr, newForeignPtr_, castForeignPtr)
import Foreign.Marshal.Array(peekArray, withArray)
import Foreign.Marshal.Utils(with, toBool, fromBool, withMany)
import Foreign.Storable(peek, poke, peekElemOff, pokeElemOff, Storable)
import Foreign.Marshal.Alloc(alloca)

import Control.Exception(Exception, SomeException, throwIO, mask, mask_, finally, onException, catch)
import Foreign.StablePtr(StablePtr, castStablePtrToPtr, deRefStablePtr, freeStablePtr)
import Control.Monad(when, (>=>))
import Data.Time.Calendar(Day(ModifiedJulianDay), toModifiedJulianDay, fromGregorian)
import Data.List.NonEmpty(NonEmpty, toList)

import Data.Vector.Storable(Vector, unsafeFromForeignPtr0)
import qualified Data.Vector.Storable as V

data Error = CPlusPlusException String
             -- |Call name, minimum required version, and linked QuantLib version.
           | UnsupportedQuantLibVersion String String String
           | DateConversion Day
           | EnumConversion String
             -- |A Haskell callback returned an array of the wrong length: expected, then returned.
           | CallbackResultLength Int Int
           deriving (Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Error -> ShowS
showsPrec :: Int -> Error -> ShowS
$cshow :: Error -> String
show :: Error -> String
$cshowList :: [Error] -> ShowS
showList :: [Error] -> ShowS
Show, Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
/= :: Error -> Error -> Bool
Eq)

instance Exception Error

foreign import ccall safe "ql.h qlErrorMessage" qlErrorMessage :: Ptr () -> IO CString
foreign import ccall safe "ql.h qlErrorCall" qlErrorCall :: Ptr () -> IO CString
foreign import ccall safe "ql.h qlErrorRequiredVersion" qlErrorRequiredVersion :: Ptr () -> IO CString
foreign import ccall safe "ql.h qlErrorLinkedVersion" qlErrorLinkedVersion :: Ptr () -> IO CString
foreign import ccall safe "ql.h qlTakeErrorException" qlTakeErrorException :: Ptr () -> IO (StablePtr SomeException)
foreign import ccall safe "ql.h qlFreeError" qlFreeError :: Ptr () -> IO ()

errorCheck :: Ptr (Ptr a) -> IO ()
errorCheck :: forall a. Ptr (Ptr a) -> IO ()
errorCheck Ptr (Ptr a)
p = IO () -> IO ()
forall a. IO a -> IO a
mask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
  errorPtr <- Ptr (Ptr a) -> IO (Ptr a)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr a)
p
  when (errorPtr /= nullPtr) $ do
    poke p nullPtr
    let owned = Ptr a -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr a
errorPtr
    flip finally (qlFreeError owned) $ do
      saved <- qlTakeErrorException owned
      if castStablePtrToPtr saved == nullPtr
        then do
          call <- qlErrorCall owned >>= peekCString
          if null call
            then qlErrorMessage owned >>= peekCString >>= throwIO . CPlusPlusException
            else do
              required <- qlErrorRequiredVersion owned >>= peekCString
              linked <- qlErrorLinkedVersion owned >>= peekCString
              throwIO $ UnsupportedQuantLibVersion call required linked
        else (deRefStablePtr saved `finally` freeStablePtr saved) >>= throwIO

-- Keep the native error, including an unconsumed callback exception, scoped to adoption.
preErrorCheck :: (Ptr (Ptr a) -> IO b) -> IO b
preErrorCheck :: forall a b. (Ptr (Ptr a) -> IO b) -> IO b
preErrorCheck Ptr (Ptr a) -> IO b
f = IO b -> IO b
forall a. IO a -> IO a
mask_ (IO b -> IO b) -> IO b -> IO b
forall a b. (a -> b) -> a -> b
$ Ptr a -> (Ptr (Ptr a) -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Ptr a
forall a. Ptr a
nullPtr ((Ptr (Ptr a) -> IO b) -> IO b) -> (Ptr (Ptr a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr a)
p ->
  (do
    result <- Ptr (Ptr a) -> IO b
f Ptr (Ptr a)
p IO b -> (SomeException -> IO b) -> IO b
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \SomeException
ex -> Ptr (Ptr a) -> IO ()
forall a. Ptr (Ptr a) -> IO ()
errorCheck Ptr (Ptr a)
p IO () -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SomeException -> IO b
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (SomeException
ex :: SomeException)
    errorCheck p
    pure result) IO b -> IO () -> IO b
forall a b. IO a -> IO b -> IO a
`finally` (do
      pending <- Ptr (Ptr a) -> IO (Ptr a)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr a)
p
      when (pending /= nullPtr) (qlFreeError (castPtr pending)))

fromMaybeBool :: Maybe Bool -> CInt
fromMaybeBool :: Maybe Bool -> CInt
fromMaybeBool = CInt -> (Bool -> CInt) -> Maybe Bool -> CInt
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (-CInt
1) Bool -> CInt
forall a. Num a => Bool -> a
fromBool

foreign import ccall safe "ql.h qlNullInteger" qlNullInteger :: CInt
foreign import ccall safe "ql.h qlNullReal" qlNullReal :: CDouble

fromMaybeInt :: (Integral a, Integral b) => Maybe a -> b
fromMaybeInt :: forall a b. (Integral a, Integral b) => Maybe a -> b
fromMaybeInt = b -> (a -> b) -> Maybe a -> b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CInt -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
qlNullInteger) a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral

fromMaybeDouble :: Maybe Double -> CDouble
fromMaybeDouble :: Maybe Double -> CDouble
fromMaybeDouble = CDouble -> (Double -> CDouble) -> Maybe Double -> CDouble
forall b a. b -> (a -> b) -> Maybe a -> b
maybe CDouble
qlNullReal Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac

-- |Marshals a Maybe of a plain by-value C++ enum whose lowest member maps to 0
-- (true of every by-value enum bound so far) as a C int, using -1 as the
-- ext::nullopt sentinel -- mirrors fromMaybeBool's convention.
fromMaybeEnum :: Enum a => Maybe a -> CInt
fromMaybeEnum :: forall a. Enum a => Maybe a -> CInt
fromMaybeEnum = CInt -> (a -> CInt) -> Maybe a -> CInt
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (-CInt
1) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (a -> Int) -> a -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum)

-- |'Nothing' emits the enum's -1 sentinel (same convention as 'fromMaybeEnum'), paired with a
-- placeholder quantity of 0 -- for enums like 'TimeUnit' that start at 0 and can't self-sentinel.
fromMaybeEnumQuantity :: Enum a => Maybe (Word, a) -> (CInt, CInt)
fromMaybeEnumQuantity :: forall a. Enum a => Maybe (Word, a) -> (CInt, CInt)
fromMaybeEnumQuantity = (CInt, CInt)
-> ((Word, a) -> (CInt, CInt)) -> Maybe (Word, a) -> (CInt, CInt)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CInt
0, -CInt
1) (Word, a) -> (CInt, CInt)
forall a b c.
(Enum a, Integral b, Integral c) =>
(b, a) -> (CInt, c)
fromEnumQuantity

toMaybeBool :: CInt -> Maybe Bool
toMaybeBool :: CInt -> Maybe Bool
toMaybeBool CInt
x = if CInt
x CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== -CInt
1 then Maybe Bool
forall a. Maybe a
Nothing else Bool -> Maybe Bool
forall a. a -> Maybe a
Just (Bool -> Maybe Bool) -> Bool -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool CInt
x

peekDynString :: CString -> IO String
peekDynString :: CString -> IO String
peekDynString CString
x
  | CString
x CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
== CString
forall a. Ptr a
nullPtr = String -> IO String
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
"" -- The following errorCheck reports a failed C++ call.
  | Bool
otherwise = CString -> IO String
peekCString CString
x IO String -> IO () -> IO String
forall a b. IO a -> IO b -> IO a
`finally` CString -> IO ()
qlFreeString CString
x

peekEnum :: (Enum a) => Ptr CInt -> IO a
peekEnum :: forall a. Enum a => Ptr CInt -> IO a
peekEnum Ptr CInt
x = Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> (CInt -> Int) -> CInt -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> a) -> IO CInt -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
x

peekDouble :: Ptr CDouble -> IO Double
peekDouble :: Ptr CDouble -> IO Double
peekDouble Ptr CDouble
x = CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (CDouble -> Double) -> IO CDouble -> IO Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CDouble -> IO CDouble
forall a. Storable a => Ptr a -> IO a
peek Ptr CDouble
x

peekWord :: Ptr CUInt -> IO Word
peekWord :: Ptr CUInt -> IO Word
peekWord Ptr CUInt
x = CUInt -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Word) -> IO CUInt -> IO Word
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
x

-- initialize pointer to a enum with a valid value before passing it to the function
preEnum :: (Storable a, Bounded a) => (Ptr a -> IO b) -> IO b
preEnum :: forall a b. (Storable a, Bounded a) => (Ptr a -> IO b) -> IO b
preEnum = a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with a
forall a. Bounded a => a
minBound

preNum :: (Storable a, Num a) => (Ptr a -> IO b) -> IO b
preNum :: forall a b. (Storable a, Num a) => (Ptr a -> IO b) -> IO b
preNum = a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with a
0

foreign import ccall safe "ql.h qlFreeString" qlFreeString :: CString -> IO ()
foreign import ccall safe "ql.h qlFreeInts" qlFreeInts :: Ptr CInt -> IO ()
foreign import ccall safe "ql.h qlFreeUInts" qlFreeUInts :: Ptr CUInt -> IO ()
foreign import ccall safe "ql.h qlFreeDoubles" qlFreeDoubles :: Ptr CDouble -> IO ()
foreign import ccall safe "ql.h &qlFreeDoubles" qlFreeDoublesFin :: FinalizerPtr CDouble
foreign import ccall safe "ql.h qlFreePointerArray" qlFreePointerArray :: Ptr (Ptr ()) -> IO ()
foreign import ccall safe "ql.h qlFreeAdditionalResults" qlFreeAdditionalResults :: CUInt -> Ptr () -> IO ()
foreign import ccall safe "ql.h qlFreeStringArray" qlFreeStringArray :: CUInt -> Ptr CString -> IO ()
foreign import ccall safe "ql.h qlSavedSettings" qlSavedSettings :: IO (Ptr ())
foreign import ccall safe "ql.h qlFreeSavedSettings" qlFreeSavedSettings :: Ptr () -> IO ()

withLArray :: (Storable b) => (a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray :: forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray a -> b
c [a]
x (CUInt, Ptr b) -> IO c
f = [b] -> (Ptr b -> IO c) -> IO c
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray ((a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
c [a]
x) (\Ptr b
px -> (CUInt, Ptr b) -> IO c
f (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUInt) -> Int -> CUInt
forall a b. (a -> b) -> a -> b
$ [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
x, Ptr b
px))

withEnumArray :: (Enum a) => [a] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withEnumArray :: forall a b. Enum a => [a] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withEnumArray = (a -> CInt) -> [a] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (a -> Int) -> a -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum)

withIntArray :: (Integral a, Num n, Storable n) => [a] -> ((CUInt, Ptr n) -> IO b) -> IO b
withIntArray :: forall a n b.
(Integral a, Num n, Storable n) =>
[a] -> ((CUInt, Ptr n) -> IO b) -> IO b
withIntArray = (a -> n) -> [a] -> ((CUInt, Ptr n) -> IO b) -> IO b
forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray a -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral

withBoolArray :: [Bool] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withBoolArray :: forall b. [Bool] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withBoolArray = (Bool -> CInt) -> [Bool] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray Bool -> CInt
forall a. Num a => Bool -> a
fromBool

withDoubleArray :: [Double] -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withDoubleArray :: forall b. [Double] -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withDoubleArray = (Double -> CDouble)
-> [Double] -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac

-- |Contiguous numeric data for APIs whose natural size is hundreds or thousands
-- of values.  The public element type stays 'Double'; C's @double@ representation
-- is used only at the FFI boundary.
type RealVector = Vector Double

-- |A storable vector known not to be empty.  The constructor is deliberately
-- hidden; build one with 'singletonNonEmptyVector', 'consNonEmptyVector', or
-- 'nonEmptyVector'.
newtype NonEmptyVector a = NonEmptyVector (Vector a)

singletonNonEmptyVector :: Storable a => a -> NonEmptyVector a
singletonNonEmptyVector :: forall a. Storable a => a -> NonEmptyVector a
singletonNonEmptyVector = Vector a -> NonEmptyVector a
forall a. Vector a -> NonEmptyVector a
NonEmptyVector (Vector a -> NonEmptyVector a)
-> (a -> Vector a) -> a -> NonEmptyVector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Vector a
forall a. Storable a => a -> Vector a
V.singleton

consNonEmptyVector :: Storable a => a -> Vector a -> NonEmptyVector a
consNonEmptyVector :: forall a. Storable a => a -> Vector a -> NonEmptyVector a
consNonEmptyVector a
x = Vector a -> NonEmptyVector a
forall a. Vector a -> NonEmptyVector a
NonEmptyVector (Vector a -> NonEmptyVector a)
-> (Vector a -> Vector a) -> Vector a -> NonEmptyVector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Vector a -> Vector a
forall a. Storable a => a -> Vector a -> Vector a
V.cons a
x

nonEmptyVector :: Storable a => Vector a -> Maybe (NonEmptyVector a)
nonEmptyVector :: forall a. Storable a => Vector a -> Maybe (NonEmptyVector a)
nonEmptyVector Vector a
x
  | Vector a -> Bool
forall a. Storable a => Vector a -> Bool
V.null Vector a
x = Maybe (NonEmptyVector a)
forall a. Maybe a
Nothing
  | Bool
otherwise = NonEmptyVector a -> Maybe (NonEmptyVector a)
forall a. a -> Maybe a
Just (Vector a -> NonEmptyVector a
forall a. Vector a -> NonEmptyVector a
NonEmptyVector Vector a
x)

nonEmptyVectorToVector :: NonEmptyVector a -> Vector a
nonEmptyVectorToVector :: forall a. NonEmptyVector a -> Vector a
nonEmptyVectorToVector (NonEmptyVector Vector a
x) = Vector a
x

withNonEmptyRealVector :: NonEmptyVector Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withNonEmptyRealVector :: forall b.
NonEmptyVector Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withNonEmptyRealVector (NonEmptyVector Vector Double
x) = Vector Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
forall b. Vector Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withRealVector Vector Double
x

-- |Borrows the vector's storage for one FFI call.  QuantLib copies every
-- input vector it receives, so the vector need not outlive the continuation.
withRealVector :: RealVector -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withRealVector :: forall b. Vector Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withRealVector Vector Double
x (CUInt, Ptr CDouble) -> IO b
f = Vector Double -> (Ptr Double -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
V.unsafeWith Vector Double
x ((Ptr Double -> IO b) -> IO b) -> (Ptr Double -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr Double
p -> (CUInt, Ptr CDouble) -> IO b
f (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector Double -> Int
forall a. Storable a => Vector a -> Int
V.length Vector Double
x), Ptr Double -> Ptr CDouble
forall a b. Ptr a -> Ptr b
castPtr Ptr Double
p)

-- |Like 'withRealVector', for an FFI argument whose length is carried separately.
withRealVectorRaw :: RealVector -> (Ptr CDouble -> IO b) -> IO b
withRealVectorRaw :: forall b. Vector Double -> (Ptr CDouble -> IO b) -> IO b
withRealVectorRaw Vector Double
x Ptr CDouble -> IO b
f = Vector Double -> (Ptr Double -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
V.unsafeWith Vector Double
x ((Ptr Double -> IO b) -> IO b) -> (Ptr Double -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ Ptr CDouble -> IO b
f (Ptr CDouble -> IO b)
-> (Ptr Double -> Ptr CDouble) -> Ptr Double -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Double -> Ptr CDouble
forall a b. Ptr a -> Ptr b
castPtr

withNonEmptyDoubleArray :: NonEmpty Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withNonEmptyDoubleArray :: forall b. NonEmpty Double -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
withNonEmptyDoubleArray NonEmpty Double
x = (Double -> CDouble)
-> [Double] -> ((CUInt, Ptr CDouble) -> IO b) -> IO b
forall b a c.
Storable b =>
(a -> b) -> [a] -> ((CUInt, Ptr b) -> IO c) -> IO c
withLArray Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac (NonEmpty Double -> [Double]
forall a. NonEmpty a -> [a]
toList NonEmpty Double
x)

withDoubleArrayRaw :: [Double] -> (Ptr CDouble -> IO b) -> IO b
withDoubleArrayRaw :: forall b. [Double] -> (Ptr CDouble -> IO b) -> IO b
withDoubleArrayRaw [Double]
x = [CDouble] -> (Ptr CDouble -> IO b) -> IO b
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray ((Double -> CDouble) -> [Double] -> [CDouble]
forall a b. (a -> b) -> [a] -> [b]
map Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac [Double]
x)

withBoolArrayRaw :: [Bool] -> (Ptr CInt -> IO b) -> IO b
withBoolArrayRaw :: forall b. [Bool] -> (Ptr CInt -> IO b) -> IO b
withBoolArrayRaw [Bool]
x = [CInt] -> (Ptr CInt -> IO b) -> IO b
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray ((Bool -> CInt) -> [Bool] -> [CInt]
forall a b. (a -> b) -> [a] -> [b]
map Bool -> CInt
forall a. Num a => Bool -> a
fromBool [Bool]
x)

withDayArray :: [Day] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withDayArray :: forall b. [Day] -> ((CUInt, Ptr CInt) -> IO b) -> IO b
withDayArray [Day]
x (CUInt, Ptr CInt) -> IO b
f = (Day -> IO CInt) -> [Day] -> IO [CInt]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Day -> IO CInt
toSerial [Day]
x IO [CInt] -> ([CInt] -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([CInt] -> (Ptr CInt -> IO b) -> IO b
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
`withArray` (\Ptr CInt
px -> (CUInt, Ptr CInt) -> IO b
f (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUInt) -> Int -> CUInt
forall a b. (a -> b) -> a -> b
$ [Day] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Day]
x, Ptr CInt
px)))

withDayPtr :: [Day] -> (Ptr CInt -> IO a) -> IO a
withDayPtr :: forall a. [Day] -> (Ptr CInt -> IO a) -> IO a
withDayPtr [Day]
x Ptr CInt -> IO a
f = (Day -> IO CInt) -> [Day] -> IO [CInt]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Day -> IO CInt
toSerial [Day]
x IO [CInt] -> ([CInt] -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([CInt] -> (Ptr CInt -> IO a) -> IO a
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
`withArray` Ptr CInt -> IO a
f)

-- |An array of plain C strings, for a function taking a @std::vector<std::string>@-shaped
-- argument as a flat @(count, char**)@ pair (the input-side counterpart of 'peekCStringArray'
-- below) -- first needed for @SecondaryCosts@' string keys (@QuantLib.Instrument.Energy@).
withStringArray :: [String] -> ((CUInt, Ptr CString) -> IO b) -> IO b
withStringArray :: forall b. [String] -> ((CUInt, Ptr CString) -> IO b) -> IO b
withStringArray [String]
xs (CUInt, Ptr CString) -> IO b
f = (String -> (CString -> IO b) -> IO b)
-> [String] -> ([CString] -> IO b) -> IO b
forall a b res.
(a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res
withMany String -> (CString -> IO b) -> IO b
forall a. String -> (CString -> IO a) -> IO a
withCString [String]
xs (\[CString]
ps -> [CString] -> (Ptr CString -> IO b) -> IO b
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [CString]
ps (\Ptr CString
p -> (CUInt, Ptr CString) -> IO b
f (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
xs), Ptr CString
p)))

prePtr :: (Storable a) => (Ptr a -> IO b) -> IO b
prePtr :: forall a b. Storable a => (Ptr a -> IO b) -> IO b
prePtr = (Ptr a -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca

preArray :: ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArray :: forall a b. ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArray (Ptr CUInt, Ptr (Ptr a)) -> IO b
f = CUInt -> (Ptr CUInt -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with CUInt
0 ((Ptr CUInt -> IO b) -> IO b) -> (Ptr CUInt -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$
  \Ptr CUInt
x -> Ptr a -> (Ptr (Ptr a) -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Ptr a
forall a. Ptr a
nullPtr ((Ptr (Ptr a) -> IO b) -> IO b) -> (Ptr (Ptr a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$
    \Ptr (Ptr a)
y -> (Ptr CUInt, Ptr (Ptr a)) -> IO b
f (Ptr CUInt
x, Ptr (Ptr a)
y)

-- Protect every output from the FFI return, including arrays not yet converted by c2hs.
preArrayWith :: (CUInt -> Ptr a -> IO ()) -> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith :: forall a b.
(CUInt -> Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith CUInt -> Ptr a -> IO ()
freeFn (Ptr CUInt, Ptr (Ptr a)) -> IO b
f = IO b -> IO b
forall a. IO a -> IO a
mask_ (IO b -> IO b) -> IO b -> IO b
forall a b. (a -> b) -> a -> b
$ ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
forall a b. ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArray (((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b)
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \slots :: (Ptr CUInt, Ptr (Ptr a))
slots@(Ptr CUInt
pl, Ptr (Ptr a)
pp) ->
  (Ptr CUInt, Ptr (Ptr a)) -> IO b
f (Ptr CUInt, Ptr (Ptr a))
slots IO b -> IO () -> IO b
forall a b. IO a -> IO b -> IO a
`finally` (do n <- Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
pl; p <- peek pp; when (p /= nullPtr) (freeFn n p))

preIntArray :: ((Ptr CUInt, Ptr (Ptr CInt)) -> IO b) -> IO b
preIntArray :: forall b. ((Ptr CUInt, Ptr (Ptr CInt)) -> IO b) -> IO b
preIntArray = (CUInt -> Ptr CInt -> IO ())
-> ((Ptr CUInt, Ptr (Ptr CInt)) -> IO b) -> IO b
forall a b.
(CUInt -> Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith ((Ptr CInt -> IO ()) -> CUInt -> Ptr CInt -> IO ()
forall a b. a -> b -> a
const Ptr CInt -> IO ()
qlFreeInts)

preDoubleArray :: ((Ptr CUInt, Ptr (Ptr CDouble)) -> IO b) -> IO b
preDoubleArray :: forall b. ((Ptr CUInt, Ptr (Ptr CDouble)) -> IO b) -> IO b
preDoubleArray = (CUInt -> Ptr CDouble -> IO ())
-> ((Ptr CUInt, Ptr (Ptr CDouble)) -> IO b) -> IO b
forall a b.
(CUInt -> Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith ((Ptr CDouble -> IO ()) -> CUInt -> Ptr CDouble -> IO ()
forall a b. a -> b -> a
const Ptr CDouble -> IO ()
qlFreeDoubles)

preCStringArray :: ((Ptr CUInt, Ptr (Ptr CString)) -> IO b) -> IO b
preCStringArray :: forall b. ((Ptr CUInt, Ptr (Ptr CString)) -> IO b) -> IO b
preCStringArray = (CUInt -> Ptr CString -> IO ())
-> ((Ptr CUInt, Ptr (Ptr CString)) -> IO b) -> IO b
forall a b.
(CUInt -> Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith CUInt -> Ptr CString -> IO ()
qlFreeStringArray

freePtrArray :: (Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
freePtrArray :: forall a. (Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
freePtrArray Ptr a -> IO ()
freeOne CUInt
n Ptr (Ptr a)
p = do
  (Int -> IO ()) -> [Int] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Ptr (Ptr a) -> Int -> IO (Ptr a)
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr (Ptr a)
p (Int -> IO (Ptr a)) -> (Ptr a -> IO ()) -> Int -> IO ()
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Ptr a
x -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Ptr a
x Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
/= Ptr a
forall a. Ptr a
nullPtr) (Ptr a -> IO ()
freeOne Ptr a
x)) [Int
0 .. CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
  Ptr (Ptr ()) -> IO ()
qlFreePointerArray (Ptr (Ptr a) -> Ptr (Ptr ())
forall a b. Ptr a -> Ptr b
castPtr Ptr (Ptr a)
p)

prePtrArray :: (Ptr a -> IO ()) -> ((Ptr CUInt, Ptr (Ptr (Ptr a))) -> IO b) -> IO b
prePtrArray :: forall a b.
(Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr (Ptr a))) -> IO b) -> IO b
prePtrArray = (CUInt -> Ptr (Ptr a) -> IO ())
-> ((Ptr CUInt, Ptr (Ptr (Ptr a))) -> IO b) -> IO b
forall a b.
(CUInt -> Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr a)) -> IO b) -> IO b
preArrayWith ((CUInt -> Ptr (Ptr a) -> IO ())
 -> ((Ptr CUInt, Ptr (Ptr (Ptr a))) -> IO b) -> IO b)
-> ((Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ())
-> (Ptr a -> IO ())
-> ((Ptr CUInt, Ptr (Ptr (Ptr a))) -> IO b)
-> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
forall a. (Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
freePtrArray

-- Clear the slot while masked before taking responsibility for releasing its allocation.
consumeArray :: (CUInt -> Ptr a -> IO ()) -> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray :: forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray CUInt -> Ptr a -> IO ()
freeFn Ptr CUInt
pl Ptr (Ptr a)
pp CUInt -> Ptr a -> IO b
convert = ((forall a. IO a -> IO a) -> IO b) -> IO b
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO b) -> IO b)
-> ((forall a. IO a -> IO a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
  n <- Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
pl
  p <- peek pp
  poke pp nullPtr
  restore (convert n p) `finally` freeFn n p

peekIntArray' :: (CInt -> b) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [b]
peekIntArray' :: forall b. (CInt -> b) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [b]
peekIntArray' CInt -> b
f Ptr CUInt
pl Ptr (Ptr CInt)
pp = (CUInt -> Ptr CInt -> IO ())
-> Ptr CUInt
-> Ptr (Ptr CInt)
-> (CUInt -> Ptr CInt -> IO [b])
-> IO [b]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray ((Ptr CInt -> IO ()) -> CUInt -> Ptr CInt -> IO ()
forall a b. a -> b -> a
const Ptr CInt -> IO ()
qlFreeInts) Ptr CUInt
pl Ptr (Ptr CInt)
pp ((CUInt -> Ptr CInt -> IO [b]) -> IO [b])
-> (CUInt -> Ptr CInt -> IO [b]) -> IO [b]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr CInt
p ->
  (CInt -> b) -> [CInt] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map CInt -> b
f ([CInt] -> [b]) -> IO [CInt] -> IO [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Ptr CInt -> IO [CInt]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n) Ptr CInt
p

peekUIntArray :: Ptr CUInt -> Ptr (Ptr CUInt) -> IO [Word]
peekUIntArray :: Ptr CUInt -> Ptr (Ptr CUInt) -> IO [Word]
peekUIntArray Ptr CUInt
pl Ptr (Ptr CUInt)
pp = (CUInt -> Ptr CUInt -> IO ())
-> Ptr CUInt
-> Ptr (Ptr CUInt)
-> (CUInt -> Ptr CUInt -> IO [Word])
-> IO [Word]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray ((Ptr CUInt -> IO ()) -> CUInt -> Ptr CUInt -> IO ()
forall a b. a -> b -> a
const Ptr CUInt -> IO ()
qlFreeUInts) Ptr CUInt
pl Ptr (Ptr CUInt)
pp ((CUInt -> Ptr CUInt -> IO [Word]) -> IO [Word])
-> (CUInt -> Ptr CUInt -> IO [Word]) -> IO [Word]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr CUInt
p ->
  (CUInt -> Word) -> [CUInt] -> [Word]
forall a b. (a -> b) -> [a] -> [b]
map CUInt -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([CUInt] -> [Word]) -> IO [CUInt] -> IO [Word]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Ptr CUInt -> IO [CUInt]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n) Ptr CUInt
p

peekIntArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Int]
peekIntArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Int]
peekIntArray = (CInt -> Int) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [Int]
forall b. (CInt -> b) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [b]
peekIntArray' CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

peekCStringArray :: Ptr CUInt -> Ptr (Ptr CString) -> IO [String]
peekCStringArray :: Ptr CUInt -> Ptr (Ptr CString) -> IO [String]
peekCStringArray Ptr CUInt
pl Ptr (Ptr CString)
pp = (CUInt -> Ptr CString -> IO ())
-> Ptr CUInt
-> Ptr (Ptr CString)
-> (CUInt -> Ptr CString -> IO [String])
-> IO [String]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray CUInt -> Ptr CString -> IO ()
qlFreeStringArray Ptr CUInt
pl Ptr (Ptr CString)
pp ((CUInt -> Ptr CString -> IO [String]) -> IO [String])
-> (CUInt -> Ptr CString -> IO [String]) -> IO [String]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr CString
p ->
  Int -> Ptr CString -> IO [CString]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n) Ptr CString
p IO [CString] -> ([CString] -> IO [String]) -> IO [String]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (CString -> IO String) -> [CString] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM CString -> IO String
peekCString

peekBoolArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Bool]
peekBoolArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Bool]
peekBoolArray = (CInt -> Bool) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [Bool]
forall b. (CInt -> b) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [b]
peekIntArray' CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool

peekDayArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Day]
peekDayArray :: Ptr CUInt -> Ptr (Ptr CInt) -> IO [Day]
peekDayArray = (CInt -> Day) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [Day]
forall b. (CInt -> b) -> Ptr CUInt -> Ptr (Ptr CInt) -> IO [b]
peekIntArray' CInt -> Day
fromSerial

peekDoubleArray :: Ptr CUInt -> Ptr (Ptr CDouble) -> IO [Double]
peekDoubleArray :: Ptr CUInt -> Ptr (Ptr CDouble) -> IO [Double]
peekDoubleArray Ptr CUInt
pl Ptr (Ptr CDouble)
pp = (CUInt -> Ptr CDouble -> IO ())
-> Ptr CUInt
-> Ptr (Ptr CDouble)
-> (CUInt -> Ptr CDouble -> IO [Double])
-> IO [Double]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray ((Ptr CDouble -> IO ()) -> CUInt -> Ptr CDouble -> IO ()
forall a b. a -> b -> a
const Ptr CDouble -> IO ()
qlFreeDoubles) Ptr CUInt
pl Ptr (Ptr CDouble)
pp ((CUInt -> Ptr CDouble -> IO [Double]) -> IO [Double])
-> (CUInt -> Ptr CDouble -> IO [Double]) -> IO [Double]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr CDouble
p ->
  (CDouble -> Double) -> [CDouble] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac ([CDouble] -> [Double]) -> IO [CDouble] -> IO [Double]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Ptr CDouble -> IO [CDouble]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n) Ptr CDouble
p

-- |Takes ownership of a C++-allocated @double[]@ result without copying it.
peekRealVector :: Ptr CUInt -> Ptr (Ptr CDouble) -> IO RealVector
peekRealVector :: Ptr CUInt -> Ptr (Ptr CDouble) -> IO (Vector Double)
peekRealVector Ptr CUInt
pl Ptr (Ptr CDouble)
pp = IO (Vector Double) -> IO (Vector Double)
forall a. IO a -> IO a
mask_ (IO (Vector Double) -> IO (Vector Double))
-> IO (Vector Double) -> IO (Vector Double)
forall a b. (a -> b) -> a -> b
$ do
  n <- CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Int) -> IO CUInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
pl
  p <- peek pp
  poke pp nullPtr
  fp <- newForeignPtr qlFreeDoublesFin p `onException` qlFreeDoubles p
  pure (unsafeFromForeignPtr0 (castForeignPtr fp) n)

-- |A non-owning vector view valid only during the C++ callback.
borrowRealVector :: Ptr CDouble -> CUInt -> IO RealVector
borrowRealVector :: Ptr CDouble -> CUInt -> IO (Vector Double)
borrowRealVector Ptr CDouble
p CUInt
n = do
  fp <- ForeignPtr CDouble -> ForeignPtr Double
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr (ForeignPtr CDouble -> ForeignPtr Double)
-> IO (ForeignPtr CDouble) -> IO (ForeignPtr Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CDouble -> IO (ForeignPtr CDouble)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr CDouble
p
  pure (unsafeFromForeignPtr0 fp (fromIntegral n))

-- |Convert before freeing: struct fields may borrow buffers owned by the aggregate.
peekStructArray :: Storable a => (a -> IO b) -> (CUInt -> Ptr a -> IO ()) -> Ptr CUInt -> Ptr (Ptr a) -> IO [b]
peekStructArray :: forall a b.
Storable a =>
(a -> IO b)
-> (CUInt -> Ptr a -> IO ()) -> Ptr CUInt -> Ptr (Ptr a) -> IO [b]
peekStructArray a -> IO b
convert CUInt -> Ptr a -> IO ()
freeFn Ptr CUInt
pl Ptr (Ptr a)
pp = (CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO [b]) -> IO [b]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray CUInt -> Ptr a -> IO ()
freeFn Ptr CUInt
pl Ptr (Ptr a)
pp ((CUInt -> Ptr a -> IO [b]) -> IO [b])
-> (CUInt -> Ptr a -> IO [b]) -> IO [b]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr a
p ->
  Int -> Ptr a -> IO [a]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n) Ptr a
p IO [a] -> ([a] -> IO [b]) -> IO [b]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a -> IO b) -> [a] -> IO [b]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM a -> IO b
convert

-- |The converter owns an element only on success; cleared slots mark completed handoffs.
peekPtrArray :: (Ptr a -> IO ()) -> (Ptr a -> IO b) -> Ptr CUInt -> Ptr (Ptr (Ptr a)) -> IO [b]
peekPtrArray :: forall a b.
(Ptr a -> IO ())
-> (Ptr a -> IO b) -> Ptr CUInt -> Ptr (Ptr (Ptr a)) -> IO [b]
peekPtrArray Ptr a -> IO ()
freeOne Ptr a -> IO b
peekOne Ptr CUInt
pl Ptr (Ptr (Ptr a))
pp = IO [b] -> IO [b]
forall a. IO a -> IO a
mask_ (IO [b] -> IO [b]) -> IO [b] -> IO [b]
forall a b. (a -> b) -> a -> b
$
  (CUInt -> Ptr (Ptr a) -> IO ())
-> Ptr CUInt
-> Ptr (Ptr (Ptr a))
-> (CUInt -> Ptr (Ptr a) -> IO [b])
-> IO [b]
forall a b.
(CUInt -> Ptr a -> IO ())
-> Ptr CUInt -> Ptr (Ptr a) -> (CUInt -> Ptr a -> IO b) -> IO b
consumeArray ((Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
forall a. (Ptr a -> IO ()) -> CUInt -> Ptr (Ptr a) -> IO ()
freePtrArray Ptr a -> IO ()
freeOne) Ptr CUInt
pl Ptr (Ptr (Ptr a))
pp ((CUInt -> Ptr (Ptr a) -> IO [b]) -> IO [b])
-> (CUInt -> Ptr (Ptr a) -> IO [b]) -> IO [b]
forall a b. (a -> b) -> a -> b
$ \CUInt
n Ptr (Ptr a)
p ->
    (Int -> IO b) -> [Int] -> IO [b]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\Int
i -> do
      raw <- Ptr (Ptr a) -> Int -> IO (Ptr a)
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr (Ptr a)
p Int
i
      x <- peekOne raw
      pokeElemOff p i nullPtr
      pure x) [Int
0 .. CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]

fromEnumQuantity :: (Enum a, Integral b, Integral c) => (b, a) -> (CInt, c)
fromEnumQuantity :: forall a b c.
(Enum a, Integral b, Integral c) =>
(b, a) -> (CInt, c)
fromEnumQuantity (b
x, a
u) = (b -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
x, Int -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> c) -> Int -> c
forall a b. (a -> b) -> a -> b
$ a -> Int
forall a. Enum a => a -> Int
fromEnum a
u)

toEnumQuantity :: (Enum a, Integral b, Integral c) => (CInt, c) -> (b, a)
toEnumQuantity :: forall a b c.
(Enum a, Integral b, Integral c) =>
(CInt, c) -> (b, a)
toEnumQuantity (CInt
x, c
u) = (CInt -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
x, Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> Int -> a
forall a b. (a -> b) -> a -> b
$ c -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral c
u)

fromEnumDouble :: (Enum a, Integral c) => (Double, a) -> (CDouble, c)
fromEnumDouble :: forall a c. (Enum a, Integral c) => (Double, a) -> (CDouble, c)
fromEnumDouble (Double
x, a
u) = (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
x, Int -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> c) -> Int -> c
forall a b. (a -> b) -> a -> b
$ a -> Int
forall a. Enum a => a -> Int
fromEnum a
u)

toEnumDouble :: (Enum a, Integral c) => (CDouble, c) -> (Double, a)
toEnumDouble :: forall a c. (Enum a, Integral c) => (CDouble, c) -> (Double, a)
toEnumDouble (CDouble
x, c
u) = (CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac CDouble
x, Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> Int -> a
forall a b. (a -> b) -> a -> b
$ c -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral c
u)

foreign import ccall safe "ql.h qlMinYear" qlMinYear :: CInt
foreign import ccall safe "ql.h qlMinMonth" qlMinMonth :: CInt
foreign import ccall safe "ql.h qlMinDay" qlMinDay :: CInt
foreign import ccall safe "ql.h qlMinDateSerialNumber" qlMinDateSerialNumber :: CInt
foreign import ccall safe "ql.h qlMaxDateSerialNumber" qlMaxDateSerialNumber :: CInt

-- |Julian day of the QuantLib zero date
qlStart :: CInt
qlStart :: CInt
qlStart = CInt
minDateJulianDays CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
- CInt
qlMinDateSerialNumber
  where minDateJulianDays :: CInt
minDateJulianDays = Day -> CInt
toModifiedJulianDay' (Day -> CInt) -> Day -> CInt
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Int -> Day
fromGregorian (CInt -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
qlMinYear) (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
qlMinMonth) (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
qlMinDay)

toModifiedJulianDay' :: Day -> CInt
toModifiedJulianDay' :: Day -> CInt
toModifiedJulianDay' = Integer -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> CInt) -> (Day -> Integer) -> Day -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Integer
toModifiedJulianDay

fromSerial :: CInt -> Day
fromSerial :: CInt -> Day
fromSerial CInt
x = Integer -> Day
ModifiedJulianDay (Integer -> Day) -> Integer -> Day
forall a b. (a -> b) -> a -> b
$ CInt -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt
x CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
+ CInt
qlStart)

dayIsValid :: Day -> Bool
dayIsValid :: Day -> Bool
dayIsValid Day
x = CInt
s CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
>= CInt
qlMinDateSerialNumber Bool -> Bool -> Bool
&& CInt
s CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
<= CInt
qlMaxDateSerialNumber
  where s :: CInt
s = Day -> CInt
toModifiedJulianDay' Day
x CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
- CInt
qlStart

toSerial :: Day -> IO CInt
toSerial :: Day -> IO CInt
toSerial Day
x | Day -> Bool
dayIsValid Day
x = CInt -> IO CInt
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (CInt -> IO CInt) -> CInt -> IO CInt
forall a b. (a -> b) -> a -> b
$ Day -> CInt
toModifiedJulianDay' Day
x CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
- CInt
qlStart
           | Bool
otherwise = Error -> IO CInt
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (Error -> IO CInt) -> Error -> IO CInt
forall a b. (a -> b) -> a -> b
$ Day -> Error
DateConversion Day
x

withDay :: Day -> (CInt -> IO a) -> IO a
withDay :: forall a. Day -> (CInt -> IO a) -> IO a
withDay Day
x CInt -> IO a
f = Day -> IO CInt
toSerial Day
x IO CInt -> (CInt -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO a
f

toDay :: CInt -> Day
toDay :: CInt -> Day
toDay = CInt -> Day
fromSerial

withMaybeDay :: Maybe Day -> (CInt -> IO a) -> IO a
withMaybeDay :: forall a. Maybe Day -> (CInt -> IO a) -> IO a
withMaybeDay Maybe Day
x CInt -> IO a
f = IO a -> (Day -> IO a) -> Maybe Day -> IO a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CInt -> IO a
f CInt
0) (Day -> (CInt -> IO a) -> IO a
forall a. Day -> (CInt -> IO a) -> IO a
`withDay` CInt -> IO a
f) Maybe Day
x

-- |Unlike the -1 sentinel used by 'fromMaybeBool'/'fromMaybeEnum'/'toMaybeBool', the
-- absent-date sentinel is 0: QuantLib serial 0 is not a representable date (serials
-- start at 'qlMinDateSerialNumber'), so it is free to mean "no date". This matches
-- 'withMaybeDay', which passes 0 in the other direction.
toMaybeDay :: CInt -> Maybe Day
toMaybeDay :: CInt -> Maybe Day
toMaybeDay CInt
0 = Maybe Day
forall a. Maybe a
Nothing
toMaybeDay CInt
x = Day -> Maybe Day
forall a. a -> Maybe a
Just (Day -> Maybe Day) -> Day -> Maybe Day
forall a b. (a -> b) -> a -> b
$ CInt -> Day
fromSerial CInt
x

-- |earliest allowed date in QuantLib
minDate :: Day
minDate :: Day
minDate = CInt -> Day
fromSerial CInt
qlMinDateSerialNumber

-- |latest date allowed in QuantLib
maxDate :: Day
maxDate :: Day
maxDate = CInt -> Day
fromSerial CInt
qlMaxDateSerialNumber

-- Public accessors must not be record fields: exported fields permit unchecked record updates.
data Matrix a = Matrix Word Word [a]
  deriving (Matrix a -> Matrix a -> Bool
(Matrix a -> Matrix a -> Bool)
-> (Matrix a -> Matrix a -> Bool) -> Eq (Matrix a)
forall a. Eq a => Matrix a -> Matrix a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Matrix a -> Matrix a -> Bool
== :: Matrix a -> Matrix a -> Bool
$c/= :: forall a. Eq a => Matrix a -> Matrix a -> Bool
/= :: Matrix a -> Matrix a -> Bool
Eq)

instance Show a => Show (Matrix a) where
  showsPrec :: Int -> Matrix a -> ShowS
showsPrec Int
p (Matrix Word
r Word
c [a]
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
"Matrix {matrixRows = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> ShowS
forall a. Show a => a -> ShowS
shows Word
r ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
", matrixColumns = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> ShowS
forall a. Show a => a -> ShowS
shows Word
c
      ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
", matrixData = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => a -> ShowS
shows [a]
d ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'}'

-- |Number of rows.
matrixRows :: Matrix a -> Word
matrixRows :: forall a. Matrix a -> Word
matrixRows (Matrix Word
r Word
_ [a]
_) = Word
r

-- |Number of columns.
matrixColumns :: Matrix a -> Word
matrixColumns :: forall a. Matrix a -> Word
matrixColumns (Matrix Word
_ Word
c [a]
_) = Word
c

-- |Elements in row-major order.
matrixData :: Matrix a -> [a]
matrixData :: forall a. Matrix a -> [a]
matrixData (Matrix Word
_ Word
_ [a]
d) = [a]
d

-- |Row-major numeric matrix backed by contiguous storage.  Use this for
-- large dense grids such as regression and volatility-surface data; small
-- process/correlation matrices remain boxed 'Matrix' values; object
-- matrices retain 'Matrix' because their elements need continuation-based FFI
-- marshalling rather than a raw contiguous pointer.
--
-- This representation interoperates directly with @hmatrix@ without making
-- @hasquant@ depend on it: @Numeric.LinearAlgebra.reshape cols
-- realMatrixData@ makes a row-major hmatrix matrix view with no element copy.
-- The reverse conversion through @flatten@ is zero-copy only for a contiguous
-- row-major hmatrix matrix; BLAS-produced or sliced matrices can require a
-- reorder/copy.
data RealMatrix = RealMatrix Word Word RealVector
  deriving (RealMatrix -> RealMatrix -> Bool
(RealMatrix -> RealMatrix -> Bool)
-> (RealMatrix -> RealMatrix -> Bool) -> Eq RealMatrix
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RealMatrix -> RealMatrix -> Bool
== :: RealMatrix -> RealMatrix -> Bool
$c/= :: RealMatrix -> RealMatrix -> Bool
/= :: RealMatrix -> RealMatrix -> Bool
Eq)

instance Show RealMatrix where
  showsPrec :: Int -> RealMatrix -> ShowS
showsPrec Int
p (RealMatrix Word
r Word
c Vector 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
"RealMatrix {realMatrixRows = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> ShowS
forall a. Show a => a -> ShowS
shows Word
r ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
", realMatrixColumns = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> ShowS
forall a. Show a => a -> ShowS
shows Word
c
      ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
", realMatrixData = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Double -> ShowS
forall a. Show a => a -> ShowS
shows Vector Double
d ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'}'

-- |Number of rows.
realMatrixRows :: RealMatrix -> Word
realMatrixRows :: RealMatrix -> Word
realMatrixRows (RealMatrix Word
r Word
_ Vector Double
_) = Word
r

-- |Number of columns.
realMatrixColumns :: RealMatrix -> Word
realMatrixColumns :: RealMatrix -> Word
realMatrixColumns (RealMatrix Word
_ Word
c Vector Double
_) = Word
c

-- |Contiguous elements in row-major order.
realMatrixData :: RealMatrix -> RealVector
realMatrixData :: RealMatrix -> Vector Double
realMatrixData (RealMatrix Word
_ Word
_ Vector Double
d) = Vector Double
d

-- |Construct a list-backed numeric matrix for small-dimensional APIs such as process
-- correlation and diffusion matrices. Returns a boxed 'Matrix' 'Double', /not/ a
-- 'RealMatrix' -- despite the shared @RealMatrix@ spelling this is not the pair of
-- 'realMatrixFromVector', which is the constructor for the contiguous 'RealMatrix' type.
-- Reach for this one when the dimensions are small and fixed (a correlation or diffusion
-- matrix), and for 'realMatrixFromVector' for large dense numerical grids such as
-- volatility surfaces and multi-asset LSM data.
boxedRealMatrix :: Word -> Word -> [Double] -> Either String (Matrix Double)
boxedRealMatrix :: Word -> Word -> [Double] -> Either String (Matrix Double)
boxedRealMatrix = Word -> Word -> [Double] -> Either String (Matrix Double)
forall a. Word -> Word -> [a] -> Either String (Matrix a)
objectMatrix

-- |Construct a row-major numeric matrix backed by contiguous storage. This is the
-- constructor for 'RealMatrix'; for a small correlation\/diffusion matrix use
-- 'boxedRealMatrix', which yields a boxed 'Matrix' 'Double' instead.
realMatrixFromVector :: Word -> Word -> RealVector -> Either String RealMatrix
realMatrixFromVector :: Word -> Word -> Vector Double -> Either String RealMatrix
realMatrixFromVector Word
rows Word
cols Vector Double
d
  | Word -> Word -> Int -> Bool
matrixDimensionsMatch Word
rows Word
cols (Vector Double -> Int
forall a. Storable a => Vector a -> Int
V.length Vector Double
d) = RealMatrix -> Either String RealMatrix
forall a b. b -> Either a b
Right (RealMatrix -> Either String RealMatrix)
-> RealMatrix -> Either String RealMatrix
forall a b. (a -> b) -> a -> b
$ Word -> Word -> Vector Double -> RealMatrix
RealMatrix Word
rows Word
cols Vector Double
d
  | Bool
otherwise = String -> Either String RealMatrix
forall a b. a -> Either a b
Left (String -> Either String RealMatrix)
-> String -> Either String RealMatrix
forall a b. (a -> b) -> a -> b
$ String
"Data length " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Vector Double -> Int
forall a. Storable a => Vector a -> Int
V.length Vector Double
d)
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" does not match dimensions " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word -> String
forall a. Show a => a -> String
show Word
rows String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word -> String
forall a. Show a => a -> String
show Word
cols

objectMatrix :: Word -> Word -> [a] -> Either String (Matrix a)
objectMatrix :: forall a. Word -> Word -> [a] -> Either String (Matrix a)
objectMatrix Word
rows Word
cols [a]
d
  | Word -> Word -> Int -> Bool
matrixDimensionsMatch Word
rows Word
cols ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
d) = Matrix a -> Either String (Matrix a)
forall a b. b -> Either a b
Right (Matrix a -> Either String (Matrix a))
-> Matrix a -> Either String (Matrix a)
forall a b. (a -> b) -> a -> b
$ Word -> Word -> [a] -> Matrix a
forall a. Word -> Word -> [a] -> Matrix a
Matrix Word
rows Word
cols [a]
d
  | Bool
otherwise = String -> Either String (Matrix a)
forall a b. a -> Either a b
Left (String -> Either String (Matrix a))
-> String -> Either String (Matrix a)
forall a b. (a -> b) -> a -> b
$ String
"Data length " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
d)
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" does not match dimensions " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word -> String
forall a. Show a => a -> String
show Word
rows String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word -> String
forall a. Show a => a -> String
show Word
cols

-- |Checks a matrix shape without overflowing the public 'Word' dimensions.
matrixDimensionsMatch :: Word -> Word -> Int -> Bool
matrixDimensionsMatch :: Word -> Word -> Int -> Bool
matrixDimensionsMatch Word
rows Word
cols Int
dataLength =
  Word -> Integer
forall a. Integral a => a -> Integer
toInteger Word
rows Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word -> Integer
forall a. Integral a => a -> Integer
toInteger Word
cols Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
dataLength

-- just a generic implementation to help when it's difficult to have Enum declaration due to complex module deps
fromEnumC :: (Enum a, Integral b) => a -> b
fromEnumC :: forall a b. (Enum a, Integral b) => a -> b
fromEnumC = Int -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> b) -> (a -> Int) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum

-- output-direction counterpart to 'fromEnumC', for a {#fun#} returning a bare enum value
-- (not through a pointer out-param, see 'peekEnum' for that case) across the same module-dep boundary
toEnumC :: (Enum a, Integral b) => b -> a
toEnumC :: forall a b. (Enum a, Integral b) => b -> a
toEnumC = Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> (b -> Int) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

uncurryNested :: (a -> b -> c -> d) -> (a, (b, c)) -> d
uncurryNested :: forall a b c d. (a -> b -> c -> d) -> (a, (b, c)) -> d
uncurryNested a -> b -> c -> d
f (a
x, (b
y, c
z)) = a -> b -> c -> d
f a
x b
y c
z

-- |'Prelude' only goes up to 'zipWith3'; this fills the gap for unpacking a C-side
-- structure-of-parallel-arrays result into one Haskell record/tuple per element.
zipWith6 :: (a -> b -> c -> d -> e -> f -> g) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]
zipWith6 :: forall a b c d e f g.
(a -> b -> c -> d -> e -> f -> g)
-> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]
zipWith6 a -> b -> c -> d -> e -> f -> g
f (a
a:[a]
as) (b
b:[b]
bs) (c
c:[c]
cs) (d
d:[d]
ds) (e
e:[e]
es) (f
g:[f]
gs) = a -> b -> c -> d -> e -> f -> g
f a
a b
b c
c d
d e
e f
g g -> [g] -> [g]
forall a. a -> [a] -> [a]
: (a -> b -> c -> d -> e -> f -> g)
-> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]
forall a b c d e f g.
(a -> b -> c -> d -> e -> f -> g)
-> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]
zipWith6 a -> b -> c -> d -> e -> f -> g
f [a]
as [b]
bs [c]
cs [d]
ds [e]
es [f]
gs
zipWith6 a -> b -> c -> d -> e -> f -> g
_ [a]
_ [b]
_ [c]
_ [d]
_ [e]
_ [f]
_ = []

-- vim: set ff=unix ts=8 sts=2 sw=2 et:

-- Scalar components keep the FFI independent of C++ complex layout.
withComplex :: Complex Double -> (CDouble, CDouble)
withComplex :: Complex Double -> (CDouble, CDouble)
withComplex (Double
r :+ Double
i) = (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
r, Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
i)
preComplex :: ((Ptr CDouble, Ptr CDouble) -> IO a) -> IO a
preComplex :: forall a. ((Ptr CDouble, Ptr CDouble) -> IO a) -> IO a
preComplex (Ptr CDouble, Ptr CDouble) -> IO a
f = (Ptr CDouble -> IO a) -> IO a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CDouble -> IO a) -> IO a) -> (Ptr CDouble -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr CDouble
r -> (Ptr CDouble -> IO a) -> IO a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CDouble -> IO a) -> IO a) -> (Ptr CDouble -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr CDouble
i -> (Ptr CDouble, Ptr CDouble) -> IO a
f (Ptr CDouble
r, Ptr CDouble
i)
peekComplex :: Ptr CDouble -> Ptr CDouble -> IO (Complex Double)
peekComplex :: Ptr CDouble -> Ptr CDouble -> IO (Complex Double)
peekComplex Ptr CDouble
r Ptr CDouble
i = Double -> Double -> Complex Double
forall a. a -> a -> Complex a
(:+) (Double -> Double -> Complex Double)
-> (CDouble -> Double) -> CDouble -> Double -> Complex Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (CDouble -> Double -> Complex Double)
-> IO CDouble -> IO (Double -> Complex Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CDouble -> IO CDouble
forall a. Storable a => Ptr a -> IO a
peek Ptr CDouble
r IO (Double -> Complex Double) -> IO Double -> IO (Complex Double)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (CDouble -> Double) -> IO CDouble -> IO Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CDouble -> IO CDouble
forall a. Storable a => Ptr a -> IO a
peek Ptr CDouble
i)