fishing for ST mutable Vector examples

hi all i was wondering if anyone could post some minimal examples on using mutable Vectors in the ST monad. i've been digging around in the usual places but haven't been able to find anything to get me over the hump thanks in advance brad

In my tutorial on using vectors,
http://www.haskell.org/haskellwiki/Numeric_Haskell:_A_Vector_Tutorial
There's some examples:
http://www.haskell.org/haskellwiki/Numeric_Haskell:_A_Vector_Tutorial#Impure...
that work in IO, and should work equally well in ST (as vectors are
parameterized by either primitive monad).
On Fri, Apr 22, 2011 at 10:32 AM, brad clawsie
hi all
i was wondering if anyone could post some minimal examples on using mutable Vectors in the ST monad. i've been digging around in the usual places but haven't been able to find anything to get me over the hump
thanks in advance brad
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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 () 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 always use IOArray instead, as I've either needed to initially read an array from file or write one to file at the end. You can't do this with ST. Best wishes Stephen

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.

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, the differences between
* vector
* array
* repa
were discussed this week on Stack Overflow:
http://stackoverflow.com/questions/6006304/what-haskell-representation-is-re...
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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, May 17, 2011 at 8:30 PM, Don Stewart
* vector * array * repa
Don, do you think that repa is as "recommended" as vector for production applications? I'm asking so because it is my understanding that accelerate still isn't mature enough to be used in production code, but I'm told vector is =). Cheers, =D -- Felipe.

Well, repa doesn't have a GPU backend. So if you need GPU stuff, you
probably do need to look at accelerate.
I think Repa is in "beta" now, the API might change a little (e.g.
we're discussing making the stuff under the hood Storable class
friendly). It also only has a small number of users, while vector has
thousands.
So, it depends on how much you like new code. There are probably still
bugs in Repa, and API changes ahead, however, it is more stable than
accelerate, which is a whole different beast. If you need
multi-dimensional, *mutable* arrays, hmatrix or the array package are
very stable, just not as much fun.
-- Don
On Tue, May 17, 2011 at 5:31 PM, Felipe Almeida Lessa
On Tue, May 17, 2011 at 8:30 PM, Don Stewart
wrote: * vector * array * repa
Don, do you think that repa is as "recommended" as vector for production applications? I'm asking so because it is my understanding that accelerate still isn't mature enough to be used in production code, but I'm told vector is =).
Cheers, =D
-- Felipe.

brad clawsie
hi all
i was wondering if anyone could post some minimal examples on using mutable Vectors in the ST monad. i've been digging around in the usual places but haven't been able to find anything to get me over the hump
thanks in advance brad
I was just looking into the same thing. This link at Rosetta Code has a list shuffling function, which is my first experiment with mutable Vectors: http://rosettacode.org/wiki/Balanced_brackets#Haskell The list is converted to a mutable Vector, the mapM_ performs a series of element swaps, then the result is frozen and converted back to a list. - Globules

I also recently implemented a Fisher-Yates shuffle in mutable vectors.
I probably should have used Gen (from mwc-random) in ST, but I already
had a GenIO hanging around for other reasons.
------------------------------------------------------------------------------
import Control.Monad.ST (unsafeIOToST)
import Data.Vector (Vector)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import System.Random.MWC
shuffle :: GenIO -> Vector k -> Vector k
shuffle rng v = if V.null v then v else V.modify go v
where
!n = V.length v
go mv = f (n-1)
where
-- note: inclusive
pickOne b = unsafeIOToST $ uniformR (0,b) rng
swap = MV.unsafeSwap mv
f 0 = return ()
f !k = do
idx <- pickOne k
swap k idx
f (k-1)
------------------------------------------------------------------------------
G.
On Sun, Apr 24, 2011 at 5:21 AM, Globules
brad clawsie
writes: hi all
i was wondering if anyone could post some minimal examples on using mutable Vectors in the ST monad. i've been digging around in the usual places but haven't been able to find anything to get me over the hump
thanks in advance brad
I was just looking into the same thing. This link at Rosetta Code has a list shuffling function, which is my first experiment with mutable Vectors:
http://rosettacode.org/wiki/Balanced_brackets#Haskell
The list is converted to a mutable Vector, the mapM_ performs a series of element swaps, then the result is frozen and converted back to a list.
- Globules
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
--
Gregory Collins

There is vector-algorithms [1] which has lots of mutable code. Note that it uses 'PrimMonad m => m ...', which means that the code may work on ST or IO. Cheers, [1] http://hackage.haskell.org/package/vector-algorithms -- Felipe.
participants (8)
-
brad clawsie
-
Daniel Fischer
-
Don Stewart
-
Felipe Almeida Lessa
-
Globules
-
Gregory Collins
-
Stephen Tetley
-
Yves Parès