[Haskell] TArray?

Simon Marlow simonmar at microsoft.com
Tue Dec 13 08:46:38 EST 2005


On 13 December 2005 13:11, Sebastian Sylvan wrote:

> On 12/13/05, Tomasz Zielonka <tomasz.zielonka at gmail.com> wrote:
>> On Tue, Dec 13, 2005 at 01:32:46PM +0100, Sebastian Sylvan wrote:
>>> Are there plans to include a mutable array for use in the STM monad,
>>> or a good reason for why this is not needed? Also, an unboxed
>>> version would be nice.
>> 
>> Why not use an (Array idx (TVar a)) ?
> 
> Ah.. Good thinking!
> This should work for in the STM monad for anything you would IOArray
> for in the IO monad,  if you write proper instances for MArray of
> course...
> 
> It's a bit inefficient, an extra indirection (compared to a primitive
> implementation) but that shouldn't matter too much for the general
> case. And the TVars themselves introduces some extra overhead, I would
> suppose (which could be per-array rather than per-element, without
> losing the property that only writes to the same element causes
> retries).
> It gets even trickier if you want an unboxed mutable array in the STM
> monad. 
> 
> But still, it's useful and I think a mutable array for the STM monad
> should be in the libraries. It could be implemented as an Array at
> first, and perhaps later switch to a more efficient representation.

In the past I have used arrays of TVars, as Thomasz suggested.  It would
indeed be better to have a primitive STM array, the only problem with
this is the extra complexity.  One simplifying assumption is that it
should consider changes at the level of the whole array, rather than
per-element (otherwise you'd use an array of TVars).

Cheers,
	Simon


More information about the Haskell mailing list