On Tue, Sep 28, 2010 at 8:54 AM, Felipe Lessa <felipe.le...@gmail.com> wrote:
> On Tue, Sep 28, 2010 at 10:41 AM, Peter Robinson <thaldy...@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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to