Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn TVar after initialisation
    primarykey
    data
    text
    <p>Is it possible to return the newly created TVar in a do block? I tried to implement this using this code:</p> <pre><code>type Buffer a = TVar [[(a,a)]] newBuffer :: STM (Buffer a) newBuffer = newTVar [[]] launchGhosts :: [[(String,String)]] -&gt; Buffer String launchGhosts unblocked = do buff &lt;- atomically newBuffer atomically $ put buff unblocked return buff computeBlock :: Buffer String -&gt; IO() computeBlock buff = do i &lt;- atomically $ get buff putStrLn $ show i put :: Buffer a -&gt; [[(a,a)]] -&gt; STM () put buff x = do writeTVar buff x get :: Buffer a -&gt; STM [[(a,a)]] get buff = do x &lt;- readTVar buff return x </code></pre> <p>And this should allow me to initialize the shared memory and use it at another place in the program. The main reason that I want to separate the memory initialization is to call the concurrent functions multiple times, without initialising the memory again and again.</p> <p>The type checker trows these two errors:</p> <pre><code>pacman.hs:65:29: No instance for (Monad TVar) arising from a do statement Possible fix: add an instance declaration for (Monad TVar) In a stmt of a 'do' block: buff &lt;- atomically newBuffer In the expression: do { buff &lt;- atomically newBuffer; atomically $ put buff unblocked; computeBlock buff; return buff } In an equation for `launchGhosts': launchGhosts unblocked = do { buff &lt;- atomically newBuffer; atomically $ put buff unblocked; computeBlock buff; .... } pacman.hs:65:37: Couldn't match expected type `TVar t0' with actual type `IO a0' In the return type of a call of `atomically' In a stmt of a 'do' block: buff &lt;- atomically newBuffer In the expression: do { buff &lt;- atomically newBuffer; atomically $ put buff unblocked; computeBlock buff; return buff } </code></pre> <p>Does anybody know what the problem is, or maybe another way to implement the idea behind this code?</p> <p>update: </p> <pre><code>launchGhosts :: [[(String,String)]] -&gt; IO(Buffer String) launchGhosts unblocked = do buff &lt;- atomically newBuffer atomically $ put buff unblocked return buff computeBlock :: IO(Buffer String) -&gt; IO() computeBlock buff = do i &lt;- atomically $ get buff putStrLn $ show i </code></pre> <p>update:</p> <pre><code>pacman.hs:71:46: Couldn't match expected type `Buffer a0' with actual type `IO (Buffer String)' In the first argument of `get', namely `buff' In the second argument of `($)', namely `get buff' In a stmt of a 'do' block: i &lt;- atomically $ get buff </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload