[Haskell-cafe] Relaxing atomicity of STM transactions

Antoine Latter aslatter at gmail.com
Tue Sep 28 10:01:16 EDT 2010


On Tue, Sep 28, 2010 at 8:54 AM, Felipe Lessa <felipe.lessa at gmail.com> wrote:
> On Tue, Sep 28, 2010 at 10:41 AM, Peter Robinson <thaldyron at gmail.com> wrote:
>> readTVarIO :: TVar a -> IO a
>
> One needs to know if it is ok to wrap this IO action into an STM
> action.  For example,
>
>> data I a = I a
>>
>> looselyReadTVar :: TVar a -> STM a
>> looselyReadTVar tvar =
>>   let v = unsafePerformIO (I <$> readTVarIO tvar)
>>   in case v of I x -> return x
>
> The 'case' is needed because otherwise the TVar would be read
> only when its value was requested, and we want to keep the
> ordering.  The 'I' datatype is used to avoid evaluating the
> user's value (which could even be 'undefined').
>
> Note that this function can be used on any monad, but I don't
> think that is a good idea =).
>
> Cheers!
>

Isn't there an 'unsafeIOToSTM' function somewhere? Something like:

> unsafeIOToSTM (IO k) = STM k

Then you might not need the case statement.

Antoine


More information about the Haskell-Cafe mailing list