[Haskell-cafe] CDouble type coercion

SevenThunders mattcbro at earthlink.net
Sun May 14 03:30:43 EDT 2006


I am new to Haskell and found myself in a bind concerning the use of 
the C types, CDouble in particular.  I extract a CDouble via it's pointer
from a StorableArray.  Since the array must interface with C the elements of
the array must be CDouble.  Now I'd like to use Text.Printf to format print
statements of elements of the array, but Text.Printf requires Doubles as
inputs and so far I have not found an obvious way to coerce CDoubles into
Doubles.

Additionally,  withStorableArray returns a monad containing a CDouble
instead of Double as peek I suppose does.  Because I cant coerce my CDouble
into a Double,  printf chokes.  Thus the following code fails to compile on
GHC

import Foreign
import Foreign.C
import Foreign.C.Types (CInt, CDouble )
import Data.Array.Storable
import Text.Printf


foreign import ccall "matrix_c.h sumarr" sumarr :: Ptr CDouble -> IO
(CDouble)

main = do
    arr <- newListArray (1 , 3) [3,2,1]:: IO (StorableArray Int CDouble)
    -- extract the pointer to arr
    withStorableArray arr sumarr >>= (\x -> printf "15.7f\n" x)


The error message is,
test2.hs:13:44:
    No instance for (PrintfArg CDouble)
      arising from use of `printf' at test2.hs:13:44-49
    Probable fix: add an instance declaration for (PrintfArg CDouble)
    In a lambda abstraction: \ x -> printf "15.7f\n" x
    In the second argument of `(>>=)', namely `(\ x -> printf "15.7f\n" x)'
    In the result of a 'do' expression:
        (withStorableArray arr sumarr) >>= (\ x -> printf "15.7f\n" x)



--
View this message in context: http://www.nabble.com/CDouble-type-coercion-t1615450.html#a4378492
Sent from the Haskell - Haskell-Cafe forum at Nabble.com.



More information about the Haskell-Cafe mailing list