From cd937c2be2900b8f13506d9ae7c731ad43d74e05 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 11 Sep 2007 17:34:24 +0000 Subject: allow setting off GSL default error handler --- lib/Data/Packed/Internal/Common.hs | 35 +++++------------------------------ lib/Data/Packed/Internal/Vector.hs | 27 ++++++++++++++++++++++----- lib/Data/Packed/Internal/aux.h | 2 ++ 3 files changed, 29 insertions(+), 35 deletions(-) (limited to 'lib/Data') diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs index 1212968..5548285 100644 --- a/lib/Data/Packed/Internal/Common.hs +++ b/lib/Data/Packed/Internal/Common.hs @@ -1,4 +1,4 @@ -{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} +{-# OPTIONS_GHC -fglasgow-exts #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Packed.Internal.Common @@ -23,13 +23,6 @@ import Data.List(transpose,intersperse) import Data.Typeable import Data.Maybe(fromJust) -debug x = trace (show x) x - -data Vector t = V { dim :: Int - , fptr :: ForeignPtr t - , ptr :: Ptr t - } -- deriving Typeable - ---------------------------------------------------------------------- instance (Storable a, RealFloat a) => Storable (Complex a) where -- alignment x = alignment (realPart x) -- @@ -40,6 +33,8 @@ instance (Storable a, RealFloat a) => Storable (Complex a) where -- poke p (a :+ b) = pokeArray (castPtr p) [a,b] -- ---------------------------------------------------------------------- +debug x = trace (show x) x + on :: (a -> a -> b) -> (t -> a) -> t -> t -> b on f g = \x y -> f (g x) (g y) @@ -55,13 +50,12 @@ common f = commonval . map f where commonval [a] = Just a commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing -xor :: Bool -> Bool -> Bool -xor a b = a && not b || b && not a - (//) :: x -> (x -> y) -> y infixl 0 // (//) = flip ($) +-- our codes should start from 1024 + errorCode :: Int -> String errorCode 1000 = "bad size" errorCode 1001 = "bad function code" @@ -71,25 +65,6 @@ errorCode 1004 = "singular" errorCode 1005 = "didn't converge" errorCode n = "code "++show n -check :: String -> [Vector a] -> IO Int -> IO () -check msg ls f = do - err <- f - when (err/=0) (error (msg++": "++errorCode err)) - mapM_ (touchForeignPtr . fptr) ls - return () - ---class (Storable a, Typeable a) => Field a ---instance (Storable a, Typeable a) => Field a - ---isReal :: (Data.Typeable.Typeable a) => (t -> a) -> t -> Bool ---isReal w x = typeOf (undefined :: Double) == typeOf (w x) - ---isComp :: (Data.Typeable.Typeable a) => (t -> a) -> t -> Bool ---isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x) - ---scast :: forall a . forall b . (Typeable a, Typeable b) => a -> b ---scast = fromJust . cast - {- | conversion of Haskell functions into function pointers that can be used in the C side -} foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double)) diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index f2646a4..0d9dc70 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -21,6 +21,28 @@ import Complex import Control.Monad(when) import Data.List(transpose) import Debug.Trace(trace) +import Foreign.C.String(peekCString) +import Foreign.C.Types + + +data Vector t = V { dim :: Int + , fptr :: ForeignPtr t + , ptr :: Ptr t + } + +check :: String -> [Vector a] -> IO Int -> IO () +check msg ls f = do + err <- f + when (err/=0) $ if err > 999 -- FIXME, it should be 1024 + then (error (msg++": "++errorCode err)) + else do + ps <- gsl_strerror err + s <- peekCString ps + error (msg++": "++s) + mapM_ (touchForeignPtr . fptr) ls + return () + +foreign import ccall "aux.h gsl_strerror" gsl_strerror :: Int -> IO (Ptr CChar) type Vc t s = Int -> Ptr t -> s -- not yet admitted by my haddock version @@ -30,8 +52,6 @@ type Vc t s = Int -> Ptr t -> s vec :: Vector t -> (Vc t s) -> s vec v f = f (dim v) (ptr v) ---baseOf v = (v `at` 0) - createVector :: Storable a => Int -> IO (Vector a) createVector n = do when (n <= 0) $ error ("trying to createVector of dim "++show n) @@ -86,8 +106,6 @@ infixl 9 @> (@>) = at - - -- | creates a new Vector by joining a list of Vectors join :: Storable t => [Vector t] -> Vector t join [] = error "joining zero vectors" @@ -111,7 +129,6 @@ asReal v = V { dim = 2*dim v, fptr = castForeignPtr (fptr v), ptr = castPtr (pt asComplex :: Vector Double -> Vector (Complex Double) asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } - ---------------------------------------------------------------- liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b diff --git a/lib/Data/Packed/Internal/aux.h b/lib/Data/Packed/Internal/aux.h index d055d35..83111e5 100644 --- a/lib/Data/Packed/Internal/aux.h +++ b/lib/Data/Packed/Internal/aux.h @@ -24,3 +24,5 @@ int submatrixR(int r1, int r2, int c1, int c2, KRMAT(x),RMAT(r)); int diagR(KRVEC(d),RMAT(r)); int diagC(KCVEC(d),CMAT(r)); + +const char * gsl_strerror (const int gsl_errno); -- cgit v1.2.3