[Haskell-cafe] Re: replicateM over vectors

Roman Leshchinskiy rl at cse.unsw.edu.au
Sun Apr 4 09:47:40 EDT 2010


On 04/04/2010, at 05:33, Chad Scherrer wrote:

> Roman Leshchinskiy <rl <at> cse.unsw.edu.au> writes:
> 
>> Ah. I missed that. Then your best bet is probably
>> 
>> replicate n action = munstream v $ Fusion.Stream.Monadic.generateM n (const
> action)
>>                                 $ new n
>> 
>> It's uglier that it should be but vector simply doesn't define the right
> combinators for this at the moment.
> 
> I'm having trouble getting this to typecheck. I'll reread your "Recycle Your
> Arrays" paper; maybe then it will make more sense.

Ugh. I shouldn't write emails while frantically scrambling to make a conference deadline. What I meant is this:

replicate n action = do { v <- new n; v' <- munstream v (generate M n (const action)) }

Sorry for the confusion.

>> There are two things one would have to do. First, add a function to
> Generic.New which initialises a New from a
>> Monadic.Stream and fusion rules for it. That's easy. The hard part is to
> generalise New to work with
>> arbitrary monads: at the moment it is defined as:
>> 
>> data New a = New (forall mv s. MVector mv a => ST s (mv s a))
>> 
>> This is because its basic reason for existence is to be passed to Vector.new
> which then does a runST to
>> produce an immutable vector. It is perhaps possible to make New more general
> but it's quite tricky. I'll
>> think about it after the ICFP deadline 
> 
> But the "m" I'm interested in happens to be ST. Sounds like it's still easy in
> principle, but not immediate. Is that right?

Not really. The big step is getting from the type above to a fixed s so that you can use it in a particular ST computation. It's not just a question of making the types work, either. I also have to convince myself that it is actually safe to do so (in particular, that the rewrite rules in the library can't break things). Getting from there to IO is very easy.

Roman




More information about the Haskell-Cafe mailing list