
--- Hal Daume III
The one is use is a unboxed and you mean I should import the Hugslibrary IOExts, just like in my code , right?
if you're using arrays of complex values like your data type, i seriously doubt you're using unboxed arrays. you will have to write special instance methods for them. and yes, you should just be able to use it as is.
I really don't have any idea about how to create an instance of that IOArray. The only thing I know about this instance is: instance IOArray bounds myDataType where, but that's nog enough. Can you (again) help me? __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

Liek I said, you should be able to use IOArrays just as you want: moussor:Trials/ cat UseArr.hs import Data.Array.IO data Foo = Foo Int Int deriving (Show) foo n = do (arr :: IOArray Int Foo) <- newArray (1,n) (Foo 0 0) mapM_ (\i -> writeArray arr i (Foo i (n-i))) [1..n] readArray arr (n-1) main = do v <- foo 1000 print v moussor:Trials/ ghc -fglasgow-exts UseArr.hs -o useArr moussor:Trials/ ./useArr Foo 999 1 However, if you want to use unboxed arrays, you need to do something like: instance MArray IOUArray Foo IO where unsafeRead (arr :: IOUArray i Foo) i = do (arr2 :: IOUArray i Int) <- castIOUArray arr v1 <- unsafeRead arr2 (i*2) v2 <- unsafeRead arr2 (i*2+1) return (Foo v1 v2) I leave the newArray_ and unsafeWrite functions as exercises :). basically in newArray you want to allocate twice as much space as an int would take (using caseIOUArray) and unsafeWrite is trivial given this unsafeRead. that said, you should probably just use IOArrays until you notice that they're too slow, then figure out how to do this trickery. -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume On Mon, 5 May 2003, Ron de Bruijn wrote:
--- Hal Daume III
wrote: The one is use is a unboxed and you mean I should import the Hugslibrary IOExts, just like in my code , right?
if you're using arrays of complex values like your data type, i seriously doubt you're using unboxed arrays. you will have to write special instance methods for them. and yes, you should just be able to use it as is.
I really don't have any idea about how to create an instance of that IOArray. The only thing I know about this instance is: instance IOArray bounds myDataType where, but that's nog enough.
Can you (again) help me?
__________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

Quoted: However, if you want to use unboxed arrays, you need to do something like: instance MArray IOUArray Foo IO where unsafeRead (arr :: IOUArray i Foo) i = do (arr2 :: IOUArray i Int) <- castIOUArray arr v1 <- unsafeRead arr2 (i*2) v2 <- unsafeRead arr2 (i*2+1) return (Foo v1 v2) End quoting I understand you just create one large array with the elements v1 and v2 next to eachother (I 'd never thought it would be that easy :) ). There is only a small problem: there is no such function as unsafeRead or unsaferead, at at least not in GHC (I saw unsafeThaw, but that also didn't work). P.S. I just turned on the fglasgow-exts, but I would preferabily do it without any extensions, how can I rewrite the same code, without the extensions? Greets Ron __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

You need -fglasgow-exts to get the type signatures. you can get around it with: instance MArray IOUArray Foo IO where unsafeRead arr = do arr2 <- myCast ... where myCast :: IOUArray i Foo -> IO (IOUArray i Int) myCast = unsafeCastIOUArray
There is only a small problem: there is no such function as unsafeRead or unsaferead, at at least not in GHC (I saw unsafeThaw, but that also didn't work).
oh come on, do you really believe that? Prelude Data.Array.MArray> :info MArray -- MArray is a class class (HasBounds a, Monad m) => MArray a :: (* -> * -> *) e m :: (* -> *) where { newArray :: forall i. (Ix i) => (i, i) -> e -> m (a i e) {- has default method -}; newArray_ :: forall i. (Ix i) => (i, i) -> m (a i e) {- has default method -}; Data.Array.Base.unsafeRead :: forall i. (Ix i) => a i e -> Int -> m e; Data.Array.Base.unsafeWrite :: forall i. (Ix i) => a i e -> Int -> e -> m (); } perhaps importing Data.Arary.Base might help :) - hal

--- Hal Daume III
You need -fglasgow-exts to get the type signatures. you can get around it with:
instance MArray IOUArray Foo IO where unsafeRead arr = do arr2 <- myCast ... where myCast :: IOUArray i Foo -> IO (IOUArray i Int) myCast = unsafeCastIOUArray
There is only a small problem: there is no such function as unsafeRead or unsaferead, at at least not in GHC (I saw unsafeThaw, but that also didn't work).
oh come on, do you really believe that?
Prelude Data.Array.MArray> :info MArray -- MArray is a class class (HasBounds a, Monad m) => MArray a :: (* -> * -> *) e m :: (*
-> *) where { newArray :: forall i. (Ix i) => (i, i) -> e -> m (a i e) {- has default method -}; newArray_ :: forall i. (Ix i) => (i, i) -> m (a i e) {- has default method -}; Data.Array.Base.unsafeRead :: forall i. (Ix i) => a i e -> Int -> m e; Data.Array.Base.unsafeWrite :: forall i. (Ix i) => a i e -> Int -> e -> m (); }
perhaps importing Data.Arary.Base might help :)
- hal
It's true when you type that in, it seems there is such a function, but I can't find it anywhere in the documentation (or is there some reason for this). It probably has to do with the forall keyword, that I just looked up in the GHC usermanual. What I think the forall keyword essentially does is defining within a dataconstructor what types of values there can be in certain places, dependant of each other (so it's like a kind of multi-container), but that doesn't explain why unsafeRead is nowhere to be found in the documentation and unsafeRead is a function. P.S. I think you meant Data.Base.Array(minor detail). Greets Ron __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com
participants (2)
-
Hal Daume III
-
Ron de Bruijn