recursive update a “Behaviour” in Sodium yields 'thread blocked …'

孤街浪徒 提交于 2020-01-15 05:41:28

问题


i would update a Behaviour (Cell / Val) from it's current value.

but the following code throws a thread blocked indefinitely in an MVar operation exception.

i have expected it prints three times 'value of i: '. what did i missing? - thanks.

  {-# LANGUAGE RecursiveDo #-}
  module Main where

  import           FRP.Sodium

  main :: IO ()
  main = do
    (e, t) <- sync newEvent

    rec
      b <- sync $ hold 0 $ snapshot (\_ i -> i + 1) e b

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    sync $ t "ping"
    sync $ t "ping"
    sync $ t "ping"

    return ()

  • GHCi, version 7.8.3
  • sodium-0.11.0.3

回答1:


Your recursive let from RecursiveDo is in the IO monad. The Reactive monad also has a MonadFix instance. You can define b completely within Reactive and then use a sync around this to execute the entire definition as a transaction.

main = do
    (e, t) <- sync newEvent

    b <- sync $
        do
            rec
                b <- hold 0 $ snapshot (\_ i -> i + 1) e b
            return b

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    ...

The RecursiveDo notation isn't helping with an example this simple. The same thing can be written more easily in terms of mfix.

main = do
    (e, t) <- sync newEvent

    b <- sync . mfix $ hold 0 . snapshot (\_ i -> i + 1) e

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    ...

It's probably worth mentioning that creating a Behavior from an Event is usually done with accum.

b <- sync $ accum 0 (const (+1) <$> e)

In sodium this is a derived function and is internally defined in terms of hold, snapshot, and mfix.

accum :: Context r => a -> Event r (a -> a) -> Reactive r (Behavior r a)
accum z efa = do
    rec
        s <- hold z $ snapshot ($) efa s
    return s


来源:https://stackoverflow.com/questions/30000359/recursive-update-a-behaviour-in-sodium-yields-thread-blocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!