[Haskell] I try to thaw an array of unboxed arrays to the IO monad

Andreas Marth Andreas-Haskell at gmx.net
Mon Jan 3 18:52:36 EST 2005


Hallo everybody!

A few years ago I wrote a program wich contained (among much else) the
following:

>import IOExts (thawIOArray,freezeIOArray)                         -- had to
be replaced, because ghc doesn't find it anymore
>import Data.Array.Unboxed as UA (UArray,listArray)
>import Data.Array.IArray as IA (Array,listArray)

>main :: IO ()
>main = do let sL = [1,4,6,3,2,5]
>   dim = length sL
>   help :: [FlatVector]
>   help = [listFlatVector (1,s) [0|i<-[1..s]]|s<-sL]
>   tmp :: Vector FlatVector
>   tmp = listVector (1,dim) help
>  v <- thawIOArray tmp
>  -- lots of stuff
>  res <- freezeIOArray v
>  return ()

>type FlatVector  = UArray Int Double

>listFlatVector :: (Int,Int) -> [Double] -> FlatVector
>listFlatVector = UA.listArray

>type Vector a = Array Int a

>listVector :: (Int,Int) -> [a] -> Vector a
>listVector = IA.listArray


which worked.

Now GHC doesn't know IOExts anymore and even doesn't know "thawIOArray" and
"freezeIOArray". (At least I could not find it anymore.)

So I tried the following:

>import Data.Array.Base (thaw,freeze)
>import Data.Array.Unboxed as UA (UArray,listArray)
>import Data.Array.IArray as IA (Array,listArray)

>main :: IO ()
>main = do let sL = [1,4,6,3,2,5]
>   dim = length sL
>   help :: [FlatVector]
>   help = [listFlatVector (1,s) [0|i<-[1..s]]|s<-sL]
>   tmp :: Vector FlatVector
>   tmp = listVector (1,dim) help
>  v <- thaw tmp
>  -- lots of stuff
>--  res <- freeze v
>  return ()

>type FlatVector  = UArray Int Double

>listFlatVector :: (Int,Int) -> [Double] -> FlatVector
>listFlatVector = UA.listArray

>type Vector a = Array Int a

>listVector :: (Int,Int) -> [a] -> Vector a
>listVector = IA.listArray

Which yields the following error in Hugs:


ERROR "E:\Inf-Sys\NormalerCode\mini-thaw.hs":6 - Cannot justify constraints
in explicitly
typed binding
*** Expression    : main
*** Type          : IO ()
*** Given context : ()
*** Constraints   : MArray a (UArray Int Double) IO

And this one in GHC:

mini-thaw.hs:12:
    No instance for (Data.Array.Base.MArray b FlatVector IO)
      arising from use of `thaw' at mini-thaw.hs:12
    In a 'do' expression: v <- thaw tmp
    In the definition of `main':
        main = do
                 let sL = [1, 4, 6, 3, 2, 5]
                     dim = length sL
                     help :: [FlatVector]
                     help = [... | s <- ...]
                     tmp :: Vector FlatVector
                     tmp = listVector (1, dim) help
                 v <- thaw tmp
                 return ()

I'm a bit disturbed because I could not find a way to solve this. And to
make an Array of Arrays (the inner ones unboxed if possible*) mutable should
not be thatch a big problem.

Any solutions available? I would be glad for any hint.

Thanks,
Andreas


* At that time only GHC provided unboxed Arrays.



More information about the Haskell mailing list