[Haskell-cafe] fishing for ST mutable Vector examples

Don Stewart dons00 at gmail.com
Wed May 18 01:30:12 CEST 2011


Yes, the differences between

 * vector
 * array
 * repa

were discussed this week on Stack Overflow:

 http://stackoverflow.com/questions/6006304/what-haskell-representation-is-recommended-for-2d-unboxed-pixel-arrays-with-mill

The reason to prefer vectors of arrays are:

 * flexible interface
 * generic interface
 * growable
 * fusible operations.

They do not support multi-dimension indexing though. For that, there
is repa, which has a rich interface, supports fusion and slicing, and
is automagically parallel.   However, repa arrays are not mutable. So
if you need multidimensional mutable arrays, either the regular array
package, or hmatrix.

On Tue, May 17, 2011 at 4:15 PM, Yves Parès <limestrael at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



More information about the Haskell-Cafe mailing list