Created
January 19, 2016 12:07
-
-
Save Philonous/29a9976ae72f90ebb191 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ImpredicativeTypes #-} | |
{-# LANGUAGE RankNTypes #-} | |
module Experiment where | |
import Control.Monad.Trans | |
import Data.IORef | |
import Reactive.Banana | |
import Reactive.Banana.Frameworks | |
data Network = Network { eventNetwork :: EventNetwork | |
, sync :: MomentIO () -> IO () | |
} | |
newNet :: IO Network | |
newNet = do | |
(ah, call) <- newAddHandler | |
network <- compile (do | |
globalExecuteEV <- fromAddHandler ah | |
_ <- execute globalExecuteEV | |
return () ) | |
actuate network | |
return $ Network { eventNetwork = network | |
, sync = call | |
} | |
run :: Network -> MomentIO a -> IO a | |
run Network{sync = call} f = do | |
ref <- newIORef (error "Network hasn't written result to ref") | |
call $ do | |
res <- f | |
liftIO $ writeIORef ref res | |
readIORef ref | |
main :: IO () | |
main = do | |
net <- newNet | |
(bhv1, set1) <- run net $ newBehavior (0 :: Integer) | |
(bhv2, set2) <- run net $ newBehavior (0 :: Integer) | |
set1 3 | |
set2 7 | |
let sumB = (liftA2 (+) bhv1 bhv2) | |
print =<< run net (valueB sumB) | |
set1 5 | |
print =<< run net (valueB sumB) | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment