-- GENERATED by C->Haskell Compiler, version 0.16.3 Crystal Seed, 24 Jan 2009 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "./Nlopt/Raw.chs" #-}{-# LANGUAGE TypeFamilies,DeriveDataTypeable, RankNTypes, ConstraintKinds, FlexibleContexts #-}
module Nlopt.Raw where

import Ipopt.AnyRF

import Foreign.C.Types
import Foreign.Ptr
import Foreign.Marshal
import Foreign.Storable
import Data.Int
import C2HS

import qualified Data.Vector.Storable.Mutable as VM
import qualified Data.Vector as V
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Generic as VG

import Control.Exception
import Data.Typeable
import Numeric.AD (grad, jacobian, hessian)
import Control.Monad


-- * converting haskell functions

{- $conventions

NLOpt has three different types for functions 'FunPtrFunc' 'FunPtrMFunc' and 'FunPtrPrecond'.

[@AD@] means derivatives are calculated, and the haskell function does no IO.

[@G@] mean you are providing the derivatives

[@M@] means m functions are calculated at a time

-}

-- | an exact hessian calculated with AD. See 'toPrecondG'
-- XXX BFGS approx could also be done...
toPrecondAD :: (forall a. AnyRFCxt a => V.Vector a -> a) -> Precond
toPrecondAD f n xs vs r _usrData =
    toPrecondG (\x v -> return (hessian f x `mXv` v)) n xs vs r _usrData

-- | see <http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Preconditioning_with_approximate_Hessians>
-- only applies to 'NLOPT_LD_CCSAQ'
toPrecondG :: (VG.Vector vec Double, v ~ vec Double)
    => (v -> v -> IO v) -- ^ given @x,v@ calculate  @H(x)v@ where H the hessian
    -> Precond
toPrecondG f n xs vs r _usrData = do
    xs' <- ptrToV n xs
    vs' <- ptrToV n vs
    copyInto n r =<< f xs' vs'

toFuncM :: VG.Vector v Double => (v Double -> IO (v Double) ) -> MFunc
toFuncM f m r n xs _g _usrData = do
    copyInto m r =<< f =<< ptrToV n xs

-- | @n@ and @m@ type variables indicate the vector size as
-- number of inputs and number of outputs respectively
toFuncMG :: (VG.Vector n Double, VG.Vector n (m Double), n ~ m)
    => (n Double -> IO (m Double) ) -- @f@
    -> (n Double -> IO (n (m Double))) -- @grad f@
    -> MFunc
toFuncMG f g m r n xs g' _usrData = do
    xs' <- ptrToV n xs
    copyInto m r =<< f xs'
    -- probably should do this better...
    copyInto (n*m) g' . VG.concat . VG.toList =<< g xs'

toFuncMAD :: (forall a. AnyRFCxt a => V.Vector a -> V.Vector a) -> MFunc
toFuncMAD f m r n xs g _usrData = do
    xs' <- ptrToV n xs
    copyInto m r (f xs')
    -- grad[i*n + j] should be satisfied...
    copyInto (n*m) g (V.concat (V.toList (jacobian f xs')))

toFunc :: (VG.Vector v Double) => (v Double -> IO Double) -> Func
toFunc f n xs _g _usrData = fmap CDouble $ f =<< ptrToV n xs

-- | where the gradient happens via AD
toFuncAD :: (forall a. AnyRFCxt a => V.Vector a -> a) -> Func
toFuncAD f n xs g _usrData = do
    xs' <- ptrToV n xs
    copyInto n g (grad f xs')
    return (CDouble (f xs'))

toFuncG :: (VG.Vector v Double) => (v Double -> IO Double) -- ^ @f@
    -> (v Double -> IO (v Double)) -- ^ @grad(f)@
    -> Func
toFuncG f g n xs g' _usrData = do
    xs' <- ptrToV n xs
    copyInto n g' =<< g xs'
    CDouble `fmap` f xs'

type family UnFunPtr a
type instance UnFunPtr (FunPtr a) = a

type Func = UnFunPtr FunPtrFunc
type MFunc = UnFunPtr FunPtrMFunc
type Precond = UnFunPtr FunPtrPrecond

type FunPtrFunc    = ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))))
{-# LINE 103 "./Nlopt/Raw.chs" #-}
type FunPtrMFunc   = ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))))
{-# LINE 104 "./Nlopt/Raw.chs" #-}
type FunPtrPrecond = ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))))
{-# LINE 105 "./Nlopt/Raw.chs" #-}

newtype NLOpt = NLOpt (ForeignPtr (NLOpt))
withNLOpt (NLOpt fptr) = withForeignPtr fptr
{-# LINE 107 "./Nlopt/Raw.chs" #-}
data NloptAlgorithm = NLOPT_GN_DIRECT
                    | NLOPT_GN_DIRECT_L
                    | NLOPT_GN_DIRECT_L_RAND
                    | NLOPT_GN_DIRECT_NOSCAL
                    | NLOPT_GN_DIRECT_L_NOSCAL
                    | NLOPT_GN_DIRECT_L_RAND_NOSCAL
                    | NLOPT_GN_ORIG_DIRECT
                    | NLOPT_GN_ORIG_DIRECT_L
                    | NLOPT_GD_STOGO
                    | NLOPT_GD_STOGO_RAND
                    | NLOPT_LD_LBFGS_NOCEDAL
                    | NLOPT_LD_LBFGS
                    | NLOPT_LN_PRAXIS
                    | NLOPT_LD_VAR1
                    | NLOPT_LD_VAR2
                    | NLOPT_LD_TNEWTON
                    | NLOPT_LD_TNEWTON_RESTART
                    | NLOPT_LD_TNEWTON_PRECOND
                    | NLOPT_LD_TNEWTON_PRECOND_RESTART
                    | NLOPT_GN_CRS2_LM
                    | NLOPT_GN_MLSL
                    | NLOPT_GD_MLSL
                    | NLOPT_GN_MLSL_LDS
                    | NLOPT_GD_MLSL_LDS
                    | NLOPT_LD_MMA
                    | NLOPT_LN_COBYLA
                    | NLOPT_LN_NEWUOA
                    | NLOPT_LN_NEWUOA_BOUND
                    | NLOPT_LN_NELDERMEAD
                    | NLOPT_LN_SBPLX
                    | NLOPT_LN_AUGLAG
                    | NLOPT_LD_AUGLAG
                    | NLOPT_LN_AUGLAG_EQ
                    | NLOPT_LD_AUGLAG_EQ
                    | NLOPT_LN_BOBYQA
                    | NLOPT_GN_ISRES
                    | NLOPT_AUGLAG
                    | NLOPT_AUGLAG_EQ
                    | NLOPT_G_MLSL
                    | NLOPT_G_MLSL_LDS
                    | NLOPT_LD_SLSQP
                    | NLOPT_LD_CCSAQ
                    | NLOPT_GN_ESCH
                    | NLOPT_NUM_ALGORITHMS
                    deriving (Show,Bounded,Ord,Eq)
instance Enum NloptAlgorithm where
  fromEnum NLOPT_GN_DIRECT = 0
  fromEnum NLOPT_GN_DIRECT_L = 1
  fromEnum NLOPT_GN_DIRECT_L_RAND = 2
  fromEnum NLOPT_GN_DIRECT_NOSCAL = 3
  fromEnum NLOPT_GN_DIRECT_L_NOSCAL = 4
  fromEnum NLOPT_GN_DIRECT_L_RAND_NOSCAL = 5
  fromEnum NLOPT_GN_ORIG_DIRECT = 6
  fromEnum NLOPT_GN_ORIG_DIRECT_L = 7
  fromEnum NLOPT_GD_STOGO = 8
  fromEnum NLOPT_GD_STOGO_RAND = 9
  fromEnum NLOPT_LD_LBFGS_NOCEDAL = 10
  fromEnum NLOPT_LD_LBFGS = 11
  fromEnum NLOPT_LN_PRAXIS = 12
  fromEnum NLOPT_LD_VAR1 = 13
  fromEnum NLOPT_LD_VAR2 = 14
  fromEnum NLOPT_LD_TNEWTON = 15
  fromEnum NLOPT_LD_TNEWTON_RESTART = 16
  fromEnum NLOPT_LD_TNEWTON_PRECOND = 17
  fromEnum NLOPT_LD_TNEWTON_PRECOND_RESTART = 18
  fromEnum NLOPT_GN_CRS2_LM = 19
  fromEnum NLOPT_GN_MLSL = 20
  fromEnum NLOPT_GD_MLSL = 21
  fromEnum NLOPT_GN_MLSL_LDS = 22
  fromEnum NLOPT_GD_MLSL_LDS = 23
  fromEnum NLOPT_LD_MMA = 24
  fromEnum NLOPT_LN_COBYLA = 25
  fromEnum NLOPT_LN_NEWUOA = 26
  fromEnum NLOPT_LN_NEWUOA_BOUND = 27
  fromEnum NLOPT_LN_NELDERMEAD = 28
  fromEnum NLOPT_LN_SBPLX = 29
  fromEnum NLOPT_LN_AUGLAG = 30
  fromEnum NLOPT_LD_AUGLAG = 31
  fromEnum NLOPT_LN_AUGLAG_EQ = 32
  fromEnum NLOPT_LD_AUGLAG_EQ = 33
  fromEnum NLOPT_LN_BOBYQA = 34
  fromEnum NLOPT_GN_ISRES = 35
  fromEnum NLOPT_AUGLAG = 36
  fromEnum NLOPT_AUGLAG_EQ = 37
  fromEnum NLOPT_G_MLSL = 38
  fromEnum NLOPT_G_MLSL_LDS = 39
  fromEnum NLOPT_LD_SLSQP = 40
  fromEnum NLOPT_LD_CCSAQ = 41
  fromEnum NLOPT_GN_ESCH = 42
  fromEnum NLOPT_NUM_ALGORITHMS = 43

  toEnum 0 = NLOPT_GN_DIRECT
  toEnum 1 = NLOPT_GN_DIRECT_L
  toEnum 2 = NLOPT_GN_DIRECT_L_RAND
  toEnum 3 = NLOPT_GN_DIRECT_NOSCAL
  toEnum 4 = NLOPT_GN_DIRECT_L_NOSCAL
  toEnum 5 = NLOPT_GN_DIRECT_L_RAND_NOSCAL
  toEnum 6 = NLOPT_GN_ORIG_DIRECT
  toEnum 7 = NLOPT_GN_ORIG_DIRECT_L
  toEnum 8 = NLOPT_GD_STOGO
  toEnum 9 = NLOPT_GD_STOGO_RAND
  toEnum 10 = NLOPT_LD_LBFGS_NOCEDAL
  toEnum 11 = NLOPT_LD_LBFGS
  toEnum 12 = NLOPT_LN_PRAXIS
  toEnum 13 = NLOPT_LD_VAR1
  toEnum 14 = NLOPT_LD_VAR2
  toEnum 15 = NLOPT_LD_TNEWTON
  toEnum 16 = NLOPT_LD_TNEWTON_RESTART
  toEnum 17 = NLOPT_LD_TNEWTON_PRECOND
  toEnum 18 = NLOPT_LD_TNEWTON_PRECOND_RESTART
  toEnum 19 = NLOPT_GN_CRS2_LM
  toEnum 20 = NLOPT_GN_MLSL
  toEnum 21 = NLOPT_GD_MLSL
  toEnum 22 = NLOPT_GN_MLSL_LDS
  toEnum 23 = NLOPT_GD_MLSL_LDS
  toEnum 24 = NLOPT_LD_MMA
  toEnum 25 = NLOPT_LN_COBYLA
  toEnum 26 = NLOPT_LN_NEWUOA
  toEnum 27 = NLOPT_LN_NEWUOA_BOUND
  toEnum 28 = NLOPT_LN_NELDERMEAD
  toEnum 29 = NLOPT_LN_SBPLX
  toEnum 30 = NLOPT_LN_AUGLAG
  toEnum 31 = NLOPT_LD_AUGLAG
  toEnum 32 = NLOPT_LN_AUGLAG_EQ
  toEnum 33 = NLOPT_LD_AUGLAG_EQ
  toEnum 34 = NLOPT_LN_BOBYQA
  toEnum 35 = NLOPT_GN_ISRES
  toEnum 36 = NLOPT_AUGLAG
  toEnum 37 = NLOPT_AUGLAG_EQ
  toEnum 38 = NLOPT_G_MLSL
  toEnum 39 = NLOPT_G_MLSL_LDS
  toEnum 40 = NLOPT_LD_SLSQP
  toEnum 41 = NLOPT_LD_CCSAQ
  toEnum 42 = NLOPT_GN_ESCH
  toEnum 43 = NLOPT_NUM_ALGORITHMS
  toEnum unmatched = error ("NloptAlgorithm.toEnum: Cannot match " ++ show unmatched)

{-# LINE 108 "./Nlopt/Raw.chs" #-}

-- | negative (above NLOPT_SUCCESS) values of these are thrown as exceptions. The positive ones are
-- return values.
data NloptResult = NLOPT_FAILURE
                 | NLOPT_INVALID_ARGS
                 | NLOPT_OUT_OF_MEMORY
                 | NLOPT_ROUNDOFF_LIMITED
                 | NLOPT_FORCED_STOP
                 | NLOPT_SUCCESS
                 | NLOPT_STOPVAL_REACHED
                 | NLOPT_FTOL_REACHED
                 | NLOPT_XTOL_REACHED
                 | NLOPT_MAXEVAL_REACHED
                 | NLOPT_MAXTIME_REACHED
                 deriving (Show,Typeable)
instance Enum NloptResult where
  fromEnum NLOPT_FAILURE = (-1)
  fromEnum NLOPT_INVALID_ARGS = (-2)
  fromEnum NLOPT_OUT_OF_MEMORY = (-3)
  fromEnum NLOPT_ROUNDOFF_LIMITED = (-4)
  fromEnum NLOPT_FORCED_STOP = (-5)
  fromEnum NLOPT_SUCCESS = 1
  fromEnum NLOPT_STOPVAL_REACHED = 2
  fromEnum NLOPT_FTOL_REACHED = 3
  fromEnum NLOPT_XTOL_REACHED = 4
  fromEnum NLOPT_MAXEVAL_REACHED = 5
  fromEnum NLOPT_MAXTIME_REACHED = 6

  toEnum (-1) = NLOPT_FAILURE
  toEnum (-2) = NLOPT_INVALID_ARGS
  toEnum (-3) = NLOPT_OUT_OF_MEMORY
  toEnum (-4) = NLOPT_ROUNDOFF_LIMITED
  toEnum (-5) = NLOPT_FORCED_STOP
  toEnum 1 = NLOPT_SUCCESS
  toEnum 2 = NLOPT_STOPVAL_REACHED
  toEnum 3 = NLOPT_FTOL_REACHED
  toEnum 4 = NLOPT_XTOL_REACHED
  toEnum 5 = NLOPT_MAXEVAL_REACHED
  toEnum 6 = NLOPT_MAXTIME_REACHED
  toEnum unmatched = error ("NloptResult.toEnum: Cannot match " ++ show unmatched)

{-# LINE 112 "./Nlopt/Raw.chs" #-}

instance Exception NloptResult

checkEC :: CInt -> IO NloptResult
checkEC n | n < 0 = throwIO e
          | otherwise = return e
    where e = fromCInt n :: NloptResult

nloptSrand :: Int -> IO ()
nloptSrand a1 =
  let {a1' = fromIntegral a1} in 
  nloptSrand'_ a1' >>= \res ->
  return ()
{-# LINE 121 "./Nlopt/Raw.chs" #-}
nloptSrandTime :: IO ()
nloptSrandTime =
  nloptSrandTime'_ >>= \res ->
  return ()
{-# LINE 122 "./Nlopt/Raw.chs" #-}
nloptVersion :: IO (Int, Int, Int)
nloptVersion =
  alloca $ \a1' -> 
  alloca $ \a2' -> 
  alloca $ \a3' -> 
  nloptVersion'_ a1' a2' a3' >>= \res ->
  peekInt  a1'>>= \a1'' -> 
  peekInt  a2'>>= \a2'' -> 
  peekInt  a3'>>= \a3'' -> 
  return (a1'', a2'', a3'')
{-# LINE 126 "./Nlopt/Raw.chs" #-}
nloptCreate :: NloptAlgorithm -> Int -> IO (NLOpt)
nloptCreate a1 a2 =
  let {a1' = toCInt a1} in 
  let {a2' = fromIntegral a2} in 
  nloptCreate'_ a1' a2' >>= \res ->
  ptrToNLOpt res >>= \res' ->
  return (res')
{-# LINE 127 "./Nlopt/Raw.chs" #-}
nloptCopy :: NLOpt -> IO (NLOpt)
nloptCopy a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptCopy'_ a1' >>= \res ->
  ptrToNLOpt res >>= \res' ->
  return (res')
{-# LINE 128 "./Nlopt/Raw.chs" #-}

-- | should not need to be called manually
nloptDestroy :: Ptr () -> IO ()
nloptDestroy a1 =
  let {a1' = id a1} in 
  nloptDestroy'_ a1' >>= \res ->
  return ()
{-# LINE 131 "./Nlopt/Raw.chs" #-}

nloptOptimize :: NLOpt -> Vec -> IO (NloptResult, Double)
nloptOptimize a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  alloca $ \a3' -> 
  nloptOptimize'_ a1' a2' a3' >>= \res ->
  peekDouble  a3'>>= \a3'' -> 
  checkEC res >>= \res' ->
  return (res', a3'')
{-# LINE 135 "./Nlopt/Raw.chs" #-}

nloptSetMinObjective :: NLOpt -> Func -> IO (NloptResult)
nloptSetMinObjective a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withNull $ \a3' -> 
  nloptSetMinObjective'_ a1' a2' a3' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 139 "./Nlopt/Raw.chs" #-}
nloptSetMaxObjective :: NLOpt -> Func -> IO (NloptResult)
nloptSetMaxObjective a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withNull $ \a3' -> 
  nloptSetMaxObjective'_ a1' a2' a3' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 142 "./Nlopt/Raw.chs" #-}

nloptSetPrecondMinObjective :: NLOpt -> Func -> Precond -> IO (NloptResult)
nloptSetPrecondMinObjective a1 a2 a3 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withPrecond a3 $ \a3' -> 
  withNull $ \a4' -> 
  nloptSetPrecondMinObjective'_ a1' a2' a3' a4' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 149 "./Nlopt/Raw.chs" #-}

nloptSetPrecondMaxObjective :: NLOpt -> Func -> Precond -> IO (NloptResult)
nloptSetPrecondMaxObjective a1 a2 a3 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withPrecond a3 $ \a3' -> 
  withNull $ \a4' -> 
  nloptSetPrecondMaxObjective'_ a1' a2' a3' a4' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 155 "./Nlopt/Raw.chs" #-}

nloptGetAlgorithm :: NLOpt -> IO (NloptAlgorithm)
nloptGetAlgorithm a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetAlgorithm'_ a1' >>= \res ->
  let {res' = fromCInt res} in
  return (res')
{-# LINE 157 "./Nlopt/Raw.chs" #-}
nloptGetDimension :: NLOpt -> IO (Int)
nloptGetDimension a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetDimension'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')
{-# LINE 158 "./Nlopt/Raw.chs" #-}

-- * constraints
nloptSetLowerBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptSetLowerBounds a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptSetLowerBounds'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 162 "./Nlopt/Raw.chs" #-}
nloptSetUpperBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptSetUpperBounds a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptSetUpperBounds'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 164 "./Nlopt/Raw.chs" #-}

nloptGetLowerBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptGetLowerBounds a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptGetLowerBounds'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 167 "./Nlopt/Raw.chs" #-}
nloptGetUpperBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptGetUpperBounds a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptGetUpperBounds'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 169 "./Nlopt/Raw.chs" #-}

nloptSetLowerBounds1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetLowerBounds1 a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetLowerBounds1'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 172 "./Nlopt/Raw.chs" #-}
nloptSetUpperBounds1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetUpperBounds1 a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetUpperBounds1'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 174 "./Nlopt/Raw.chs" #-}

nloptRemoveInequalityConstraints :: NLOpt -> IO (NloptResult)
nloptRemoveInequalityConstraints a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptRemoveInequalityConstraints'_ a1' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 177 "./Nlopt/Raw.chs" #-}

nloptAddInequalityConstraint :: NLOpt -> Func -> Double -> IO (NloptResult)
nloptAddInequalityConstraint a1 a2 a4 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withNull $ \a3' -> 
  let {a4' = realToFrac a4} in 
  nloptAddInequalityConstraint'_ a1' a2' a3' a4' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 183 "./Nlopt/Raw.chs" #-}

nloptAddPrecondInequalityConstraint :: NLOpt -> Func -> Precond -> Double -> IO (NloptResult)
nloptAddPrecondInequalityConstraint a1 a2 a3 a5 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withPrecond a3 $ \a3' -> 
  withNull $ \a4' -> 
  let {a5' = realToFrac a5} in 
  nloptAddPrecondInequalityConstraint'_ a1' a2' a3' a4' a5' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 190 "./Nlopt/Raw.chs" #-}

nloptAddInequalityMconstraint :: NLOpt -> Int -> MFunc -> Vec -> IO (NloptResult)
nloptAddInequalityMconstraint a1 a2 a3 a5 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  withMFunc a3 $ \a3' -> 
  withNull $ \a4' -> 
  vmUnsafeWith a5 $ \a5' -> 
  nloptAddInequalityMconstraint'_ a1' a2' a3' a4' a5' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 197 "./Nlopt/Raw.chs" #-}

nloptRemoveEqualityConstraints :: NLOpt -> IO (NloptResult)
nloptRemoveEqualityConstraints a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptRemoveEqualityConstraints'_ a1' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 200 "./Nlopt/Raw.chs" #-}

nloptAddEqualityConstraint :: NLOpt -> Func -> Double -> IO (NloptResult)
nloptAddEqualityConstraint a1 a2 a4 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withNull $ \a3' -> 
  let {a4' = realToFrac a4} in 
  nloptAddEqualityConstraint'_ a1' a2' a3' a4' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 206 "./Nlopt/Raw.chs" #-}

nloptAddPrecondEqualityConstraint :: NLOpt -> Func -> Precond -> Double -> IO (NloptResult)
nloptAddPrecondEqualityConstraint a1 a2 a3 a5 =
  withNLOpt_ a1 $ \a1' -> 
  withFunc a2 $ \a2' -> 
  withPrecond a3 $ \a3' -> 
  withNull $ \a4' -> 
  let {a5' = realToFrac a5} in 
  nloptAddPrecondEqualityConstraint'_ a1' a2' a3' a4' a5' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 213 "./Nlopt/Raw.chs" #-}

nloptAddEqualityMconstraint :: NLOpt -> Int -> MFunc -> Vec -> IO (NloptResult)
nloptAddEqualityMconstraint a1 a2 a3 a5 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  withMFunc a3 $ \a3' -> 
  withNull $ \a4' -> 
  vmUnsafeWith a5 $ \a5' -> 
  nloptAddEqualityMconstraint'_ a1' a2' a3' a4' a5' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 220 "./Nlopt/Raw.chs" #-}

-- * stopping criteria
nloptSetStopval :: NLOpt -> Double -> IO (NloptResult)
nloptSetStopval a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetStopval'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 224 "./Nlopt/Raw.chs" #-}

nloptGetStopval :: NLOpt -> IO (Double)
nloptGetStopval a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetStopval'_ a1' >>= \res ->
  let {res' = realToFrac res} in
  return (res')
{-# LINE 226 "./Nlopt/Raw.chs" #-}

nloptSetFtolRel :: NLOpt -> Double -> IO (NloptResult)
nloptSetFtolRel a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetFtolRel'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 229 "./Nlopt/Raw.chs" #-}

nloptGetFtolRel :: NLOpt -> IO (Double)
nloptGetFtolRel a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetFtolRel'_ a1' >>= \res ->
  let {res' = realToFrac res} in
  return (res')
{-# LINE 231 "./Nlopt/Raw.chs" #-}

nloptSetFtolAbs :: NLOpt -> Double -> IO (NloptResult)
nloptSetFtolAbs a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetFtolAbs'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 234 "./Nlopt/Raw.chs" #-}

nloptGetFtolAbs :: NLOpt -> IO (Double)
nloptGetFtolAbs a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetFtolAbs'_ a1' >>= \res ->
  let {res' = realToFrac res} in
  return (res')
{-# LINE 236 "./Nlopt/Raw.chs" #-}

nloptSetXtolRel :: NLOpt -> Double -> IO (NloptResult)
nloptSetXtolRel a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetXtolRel'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 239 "./Nlopt/Raw.chs" #-}

nloptGetXtolRel :: NLOpt -> IO (Double)
nloptGetXtolRel a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetXtolRel'_ a1' >>= \res ->
  let {res' = realToFrac res} in
  return (res')
{-# LINE 241 "./Nlopt/Raw.chs" #-}

nloptSetXtolAbs1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetXtolAbs1 a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetXtolAbs1'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 244 "./Nlopt/Raw.chs" #-}

nloptSetXtolAbs :: NLOpt -> Vec -> IO (NloptResult)
nloptSetXtolAbs a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptSetXtolAbs'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 247 "./Nlopt/Raw.chs" #-}

nloptGetXtolAbs :: NLOpt -> Vec -> IO (NloptResult)
nloptGetXtolAbs a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptGetXtolAbs'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 250 "./Nlopt/Raw.chs" #-}

nloptSetMaxeval :: NLOpt -> Int -> IO (NloptResult)
nloptSetMaxeval a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  nloptSetMaxeval'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 253 "./Nlopt/Raw.chs" #-}

nloptGetMaxeval :: NLOpt -> IO (Int)
nloptGetMaxeval a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetMaxeval'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')
{-# LINE 255 "./Nlopt/Raw.chs" #-}

nloptSetMaxtime :: NLOpt -> Double -> IO (NloptResult)
nloptSetMaxtime a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetMaxtime'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 258 "./Nlopt/Raw.chs" #-}

nloptGetMaxtime :: NLOpt -> IO (Double)
nloptGetMaxtime a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetMaxtime'_ a1' >>= \res ->
  let {res' = realToFrac res} in
  return (res')
{-# LINE 260 "./Nlopt/Raw.chs" #-}

nloptForceStop :: NLOpt -> IO (NloptResult)
nloptForceStop a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptForceStop'_ a1' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 262 "./Nlopt/Raw.chs" #-}
nloptSetForceStop :: NLOpt -> Int -> IO (NloptResult)
nloptSetForceStop a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  nloptSetForceStop'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 263 "./Nlopt/Raw.chs" #-}
nloptGetForceStop :: NLOpt -> IO (Int)
nloptGetForceStop a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetForceStop'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')
{-# LINE 264 "./Nlopt/Raw.chs" #-}


-- * more algorithm-specific parameters
nloptSetLocalOptimizer :: NLOpt -> NLOpt -> IO (NloptResult)
nloptSetLocalOptimizer a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  withNLOpt_ a2 $ \a2' -> 
  nloptSetLocalOptimizer'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 269 "./Nlopt/Raw.chs" #-}

nloptSetPopulation :: NLOpt -> Int -> IO (NloptResult)
nloptSetPopulation a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  nloptSetPopulation'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 272 "./Nlopt/Raw.chs" #-}

nloptGetPopulation :: NLOpt -> IO (Int)
nloptGetPopulation a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetPopulation'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')
{-# LINE 274 "./Nlopt/Raw.chs" #-}

nloptSetVectorStorage :: NLOpt -> Int -> IO (NloptResult)
nloptSetVectorStorage a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  nloptSetVectorStorage'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 277 "./Nlopt/Raw.chs" #-}

nloptGetVectorStorage :: NLOpt -> IO (Int)
nloptGetVectorStorage a1 =
  withNLOpt_ a1 $ \a1' -> 
  nloptGetVectorStorage'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')
{-# LINE 279 "./Nlopt/Raw.chs" #-}

nloptSetInitialStep :: NLOpt -> Vec -> IO (NloptResult)
nloptSetInitialStep a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  nloptSetInitialStep'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 282 "./Nlopt/Raw.chs" #-}

nloptGetInitialStep :: NLOpt -> Vec -> Vec -> IO (NloptResult)
nloptGetInitialStep a1 a2 a3 =
  withNLOpt_ a1 $ \a1' -> 
  vmUnsafeWith a2 $ \a2' -> 
  vmUnsafeWith a3 $ \a3' -> 
  nloptGetInitialStep'_ a1' a2' a3' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 286 "./Nlopt/Raw.chs" #-}

nloptSetInitialStep1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetInitialStep1 a1 a2 =
  withNLOpt_ a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  nloptSetInitialStep1'_ a1' a2' >>= \res ->
  checkEC res >>= \res' ->
  return (res')
{-# LINE 289 "./Nlopt/Raw.chs" #-}

-- * utils for ffi

-- $note
-- much is copied from Ipopt.Raw

withNLOpt_ x f = withNLOpt x (f . castPtr)

vmUnsafeWith :: VM.IOVector Double -> (Ptr CDouble -> IO b) -> IO b
vmUnsafeWith v f = VM.unsafeWith v (f . castPtr)

ptrToVS :: Integral n => n -> Ptr CDouble -> IO Vec
ptrToVS n p = do
    fp <- newForeignPtr_ (castPtr p)
    return (VM.unsafeFromForeignPtr0 fp (fromIntegral n))

ptrToV :: (VG.Vector v Double, Integral n) => n -> Ptr CDouble -> IO (v Double)
ptrToV n p = fmap V.convert $ VS.unsafeFreeze =<< ptrToVS n p

copyInto _ p _ | p == nullPtr = return ()
copyInto n dest src = do
    to <- ptrToVS n dest
    VM.copy to =<< VS.unsafeThaw (V.convert src)

type Vec = VM.IOVector Double

toCInt x = fromIntegral (fromEnum x)
fromCInt x = toEnum (fromIntegral x)
peekInt ptr = fmap fromIntegral (peek ptr)


ptrToNLOpt :: Ptr () -> IO NLOpt
ptrToNLOpt p = do
    fp <- newForeignPtr nloptDestroyFP p
    return (NLOpt (castForeignPtr fp))

foreign import ccall "wrapper" mkNloptFinalizer :: (Ptr () -> IO ()) -> IO (FunPtr (Ptr () -> IO ()))

foreign import ccall unsafe "& nlopt_destroy" nloptDestroyFP :: FunPtr (Ptr () -> IO ())

foreign import ccall "wrapper" mkFunc :: Func -> IO (FunPtr Func)
foreign import ccall "wrapper" mkPrecond :: Precond -> IO (FunPtr Precond)
foreign import ccall "wrapper" mkMFunc :: MFunc -> IO (FunPtr MFunc)

withFunc :: Func -> (FunPtr Func -> IO b) -> IO b
withFunc f g = do
    f' <- mkFunc f
    r <- g f'
    -- freeHaskellFunPtr f'
    return r

withPrecond :: Precond -> (FunPtr Precond -> IO b) -> IO b
withPrecond f g = do
    f' <- mkPrecond f
    r <- g f'
    -- freeHaskellFunPtr f'
    return r

withMFunc :: MFunc -> (FunPtr MFunc -> IO b) -> IO b
withMFunc f g = do
    f' <- mkMFunc f
    r <- g f'
    -- freeHaskellFunPtr f'
    return r

withNull f = f nullPtr

-- | c2hs generates CDouble peek a Double instead
peekDouble :: Ptr CDouble -> IO Double
peekDouble p = peek (castPtr p)


-- | naive matrix × vector
mXv :: Num a => V.Vector (V.Vector a) -> V.Vector a -> V.Vector a
mXv m v = V.map (V.sum . V.zipWith (*) v) m

-- * c2hs-generated

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_srand"
  nloptSrand'_ :: (CULong -> (IO ()))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_srand_time"
  nloptSrandTime'_ :: (IO ())

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_version"
  nloptVersion'_ :: ((Ptr CInt) -> ((Ptr CInt) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_create"
  nloptCreate'_ :: (CInt -> (CUInt -> (IO (Ptr ()))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_copy"
  nloptCopy'_ :: ((Ptr ()) -> (IO (Ptr ())))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_destroy"
  nloptDestroy'_ :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_optimize"
  nloptOptimize'_ :: ((Ptr ()) -> ((Ptr CDouble) -> ((Ptr CDouble) -> (IO CInt))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_min_objective"
  nloptSetMinObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_max_objective"
  nloptSetMaxObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_precond_min_objective"
  nloptSetPrecondMinObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (IO CInt)))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_precond_max_objective"
  nloptSetPrecondMaxObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (IO CInt)))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_algorithm"
  nloptGetAlgorithm'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_dimension"
  nloptGetDimension'_ :: ((Ptr ()) -> (IO CUInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_lower_bounds"
  nloptSetLowerBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_upper_bounds"
  nloptSetUpperBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_lower_bounds"
  nloptGetLowerBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_upper_bounds"
  nloptGetUpperBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_lower_bounds1"
  nloptSetLowerBounds1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_upper_bounds1"
  nloptSetUpperBounds1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_remove_inequality_constraints"
  nloptRemoveInequalityConstraints'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_inequality_constraint"
  nloptAddInequalityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (CDouble -> (IO CInt)))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_precond_inequality_constraint"
  nloptAddPrecondInequalityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (CDouble -> (IO CInt))))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_inequality_mconstraint"
  nloptAddInequalityMconstraint'_ :: ((Ptr ()) -> (CUInt -> ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))) -> ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt))))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_remove_equality_constraints"
  nloptRemoveEqualityConstraints'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_equality_constraint"
  nloptAddEqualityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (CDouble -> (IO CInt)))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_precond_equality_constraint"
  nloptAddPrecondEqualityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (CDouble -> (IO CInt))))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_equality_mconstraint"
  nloptAddEqualityMconstraint'_ :: ((Ptr ()) -> (CUInt -> ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))) -> ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt))))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_stopval"
  nloptSetStopval'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_stopval"
  nloptGetStopval'_ :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_ftol_rel"
  nloptSetFtolRel'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_ftol_rel"
  nloptGetFtolRel'_ :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_ftol_abs"
  nloptSetFtolAbs'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_ftol_abs"
  nloptGetFtolAbs'_ :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_rel"
  nloptSetXtolRel'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_xtol_rel"
  nloptGetXtolRel'_ :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_abs1"
  nloptSetXtolAbs1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_abs"
  nloptSetXtolAbs'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_xtol_abs"
  nloptGetXtolAbs'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_maxeval"
  nloptSetMaxeval'_ :: ((Ptr ()) -> (CInt -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_maxeval"
  nloptGetMaxeval'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_maxtime"
  nloptSetMaxtime'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_maxtime"
  nloptGetMaxtime'_ :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_force_stop"
  nloptForceStop'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_force_stop"
  nloptSetForceStop'_ :: ((Ptr ()) -> (CInt -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_force_stop"
  nloptGetForceStop'_ :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_local_optimizer"
  nloptSetLocalOptimizer'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_population"
  nloptSetPopulation'_ :: ((Ptr ()) -> (CUInt -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_population"
  nloptGetPopulation'_ :: ((Ptr ()) -> (IO CUInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_vector_storage"
  nloptSetVectorStorage'_ :: ((Ptr ()) -> (CUInt -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_vector_storage"
  nloptGetVectorStorage'_ :: ((Ptr ()) -> (IO CUInt))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_initial_step"
  nloptSetInitialStep'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_initial_step"
  nloptGetInitialStep'_ :: ((Ptr ()) -> ((Ptr CDouble) -> ((Ptr CDouble) -> (IO CInt))))

foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_initial_step1"
  nloptSetInitialStep1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))