[Haskell-cafe] fishing for ST mutable Vector examples

Yves Parès limestrael at gmail.com
Wed May 18 01:15:51 CEST 2011


Hello all,

By the way, is there any reason to prefer package 'vector' over package
'array'? Do they just provide different interfaces to similar
functionnalities or are there real performance stakes?
I personnaly prefer Data.Array, since:
- It gives the possibility to index with something else than integers
(thanks to Ix class)
- It provides Foldable/Traversable instances (better abstraction, so)


2011/4/22 Daniel Fischer <daniel.is.fischer at googlemail.com>

> On Friday 22 April 2011 20:14:38, Stephen Tetley wrote:
> > Hi Brad
> >
> > I think all you can do with an ST array is covered by the MArray class
> > and its derived operations - note the class is exported opaquely from
> > Data.Array.MArray - it has these two members that aren't exported so
> > aren't documented:
> >
> >
> > unsafeRead  :: Ix i => a i e -> Int -> m e
> > unsafeWrite :: Ix i => a i e -> Int -> e -> m ()
>
> Those are available from Data.Array.Base. I use them a lot, because there's
> no point in letting the runtime check array bounds after I just did to
> determine whether the loop is finished.
>
> >
> > To actually read and write you have to use the safe derived operations
> > which wrap the unsafe versions:
> >
> > readArray :: (MArray a e m, Ix i) => a i e -> i -> m e
> > writeArray :: (MArray a e m, Ix i) => a i e -> i -> e -> m ()
> >
> > For practical purposes I've found STArray's a bit of a white elephant
>
> I on the other hand use 'STUArray's very much. When you fill an array with
> an algorithm which works best with mutation (a sieve for example) and
> afterwards use it only for querying, runSTUArray (or runSTArray) is a great
> friend. ST guarantees that no other thread can mess with your array while
> you build it, when you're done it's immutable. IO doesn't give you these
> guarantees, you have to ascertain yourself that no other thread can mutate
> your array.
>
> It's the same for 'Vector's, ST's phantom type parameter isolates you from
> the outside world, with IOVectors, you have to do the protection yourself.
> I think vector doesn't provide an analogue to runST(U)Array, but if you
> need it, you can write
>
> runST (do
>   vec <- stuff
>   frz <- unsafeFreeze vec
>   return frz
>
> yourself.
>
> > - I always use IOArray instead, as I've either needed to initially
> > read an array from file or write one to file at the end.
>
> On the other hand, if you're doing IO, an IOArray is a fairly natural
> choice.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110518/9c700b25/attachment.htm>


More information about the Haskell-Cafe mailing list