3

Working through Simon Peyton Jones concurrency example, I have the following code:

import Control.Concurrent.STM
import Control.Concurrent.STM.TVar

deposit account amount = do
    bal <- readTVar account
    writeTVar account (bal+amount)

I am trying to test this in GHCi REPL

*Main> checking <- atomically $ newTVar 100
*Main> atomically $ deposit checking 10

How do I verify my checking balance is $110?

I have tried

*Main> checking
*Main> readTVar checking
*Main> balance <- readTVar checking
zhon
  • 1,610
  • 1
  • 22
  • 31

1 Answers1

6

atomically $ readTVar checking does what you want. The GHCi REPL automatically executes any IO action that you give it.

Venge
  • 2,417
  • 16
  • 21
  • 2
    For this simple case, there's also `readTVarIO`, which is equivalent to `atomically . readTVar` but more efficient. – John L Jan 08 '14 at 23:00