turion / rhine

Haskell Functional Reactive Programming framework with type-level clocks
http://hackage.haskell.org/package/rhine
123 stars 21 forks source link

Implement event clock #17

Closed turion closed 6 years ago

turion commented 6 years ago

Implement a clock in a special monad m (or monad transformer) that ticks upon a particular side effect in m (depending on the clock value). Other parts of the signal network that are also in m can then trigger events on this clock.

One crude implementation could go roughly like:

data MVarEventClock a = MVarEventClock (MVar a)

instance Clock IO MVarEventClock where
  startClock (MVarEventClock var) = arrM_ getCurrentTime &&& arrM_ (takeMVar var)

triggerEvent :: MVarEventClock a -> a -> IO ()
triggerEvent (MVarEventClock var) = putMVar var

Care needs to be taken that triggerEvent does not lead to deadlocks when called from a signal on the same clock. Possibly, Chan or STM.TChan will give a more robust implementation.

Is it possible to implement such a clock purely?

turion commented 6 years ago

Fixed in f22e8c99f04eaafb094499e4645738f771ce57d6, using Chans.