3

I have a vector of Float elements created by the getVectorFloat function. In order to make some measurements, I need to use deepSeqArray. However, I don't manage to do it.

Here is my example:

import Data.Array.Repa as R
import Data.Array.Repa.Algorithms.Randomish

len :: Int
len = 3000000

main = do
    ws <- getVectorFloat len
    ws `deepSeqArray` return()

getVectorFloat :: Int -> Array DIM1 Float
getVectorFloat len = R.map (toFloat) (getVectorDouble len)

toFloat :: Double -> Float
toFloat a = realToFrac a

getVectorDouble :: Int -> Array DIM1 Double
getVectorDouble len = randomishDoubleArray (Z :. len) (-100) 100 1

And the error I get:

haskell.hs:9:9:
    Couldn't match expected type `Array sh0 a0'
                with actual type `Float'
    In the first argument of `deepSeqArray', namely `ws'
    In the expression: ws `deepSeqArray` return ()
    In the expression:
      do { ws <- getVectorFloat len;
             ws `deepSeqArray` return () }

I don't understand why Array sh0 a0 can't be matched to Array Dim1 Float.

Thank you for your help

Don Stewart
  • 137,316
  • 36
  • 365
  • 468

1 Answers1

1

The problemo comes, from the do which doesn't do what you think. When you do ws <- getVectorFloat len

ws is not set with the Array, but called for each value of it. ie the do notation for an array , is similar to a map. Consider the following example

prelude > do x <- [1,2,3]; return (x*10)
[10, 20, 30]

or more fun ;-)

Prelude> do <-[1,2,3];y<-[10,20,30]; return(x,y)                                                                                                                                         
[(1,10),(1,20),(1,30),(2,10),(2,20),(2,30),(3,10),(3,20),(3,30)]   

So I'm not sure your usage of do is appropriate.

mb14
  • 22,276
  • 7
  • 60
  • 102
  • 1
    Ok. So I have set `ws = getVectorFloat len` before the main. The main now contains `do ws \`deepSeqArray\` return()`. This compiles correctly. Thanks ! – Wilfried Kirschenmann Apr 11 '11 at 12:47