Hi,
I would like to create multi-dimensional haskell mutable arrays.
The program below fails to link when compiled with ghc-2.10 or
ghc-3.00 (-fglasgow-exts -fvia-C -cpp -recomp) on a debian 1.3 linux
box. Upon commenting out the line containing "error", it links, but
fails at run-time with:
Segmentation fault caught, address = 0
IOT trap/Abort
Program follows:
module Main ( main ) where
import ST
import IOBase
import Array
import ArrBase
type TwoD s = MutableArray s Int (MutableArray s Int Int)
setup :: ST s (TwoD s)
setup = let isz = 10
imax = isz - 1
osz = 2
omax = osz - 1 in
do
-- gives : undefined reference to `IOBase_error_closure'
-- x <- newArray (0, omax) (error "uninitialised")
dmy <- newArray (0, imax) 0
x <- newArray (0, omax) dmy
as <- (accumulate . replicate osz) (newArray (0, imax) 6)
mapM_ (\(i,v) -> writeArray x i v) (zip [0..omax] as)
return x
main :: IO ()
main = do
a <- stToIO setup
return ()
I have messed around with this program quite a bit, but could not find
any specific cause of the bug, nor any work-around. Removing the
mapM_ line stops the seg-v, but replacing it by different but
equivalent code brings the seg-v back. There seems to be something
wrong with the implementation of MutableArray when one MutableArray is
an element of another.
I first tried a naive version of this program that created one
inner array and stored it in all the slots of the outer array - this
worked as expected - modifying the contents of one copy of the inner
array effected the others as well. But the strange thing is that the
naive program worked! It seems ghc just doesn't like having several
distinct inner arrays...
BTW I also had a version that used the 'array' function to build a
monolithic outer array, followed by thawArray. I replaced this
with mapM_ above because thawArray was my first suspect for the seg-v.
Tim
--
----------------------------------------------------------------------
T.R.BARBOUR Email : [EMAIL PROTECTED]
----------------------------------------------------------------------
Department of Computer Science
The University of Melbourne
Parkville, Victoria 3052
Australia
----------------------------------------------------------------------