module FRP.NetWire.Wire
(
Wire(..),
WireState(..),
DTime,
Event,
Time,
cleanupWireState,
initWireState,
mkGen,
toGen
)
where
import Control.Applicative
import Control.Arrow
import Control.Category
import Control.Concurrent.STM
import Prelude hiding ((.), id)
import System.Random.Mersenne
type DTime = Double
type Event = Maybe
type Time = Double
data Wire a b where
WArr :: (a -> b) -> Wire a b
WConst :: b -> Wire a b
WGen :: (WireState -> a -> IO (Maybe b, Wire a b)) -> Wire a b
WId :: Wire a a
instance Alternative (Wire a) where
empty = zeroArrow
(<|>) = (<+>)
instance Applicative (Wire a) where
pure = WConst
wf' <*> wx' =
WGen $ \ws x' -> do
(mf, wf) <- toGen wf' ws x'
(mx, wx) <- toGen wx' ws x'
return (mf <*> mx, wf <*> wx)
instance Arrow Wire where
arr = WArr
first (WGen f) =
WGen $ \ws (x', y) -> do
(mx, w) <- f ws x'
return (fmap (,y) mx, first w)
first (WArr f) = WArr (first f)
first (WConst c) = WArr (first (const c))
first WId = WId
second (WGen f) =
WGen $ \ws (x, y') -> do
(my, w) <- f ws y'
return (fmap (x,) my, second w)
second (WArr f) = WArr (second f)
second (WConst c) = WArr (second (const c))
second WId = WId
wf *** WId = first wf
WId *** wg = second wg
wf' *** wg' =
WGen $ \ws (x', y') -> do
(mx, wf) <- toGen wf' ws x'
(my, wg) <- toGen wg' ws y'
return (liftA2 (,) mx my, wf *** wg)
wf' &&& wg' =
WGen $ \ws x' -> do
(mx1, wf) <- toGen wf' ws x'
(mx2, wg) <- toGen wg' ws x'
return (liftA2 (,) mx1 mx2, wf &&& wg)
instance ArrowChoice Wire where
left w' = wl
where
wl =
WGen $ \ws mx' ->
case mx' of
Left x' -> do
(mx, w) <- toGen w' ws x'
return (fmap Left mx, left w)
Right x -> return (Just (Right x), wl)
right w' = wl
where
wl =
WGen $ \ws mx' ->
case mx' of
Right x' -> do
(mx, w) <- toGen w' ws x'
return (fmap Right mx, right w)
Left x -> return (Just (Left x), wl)
wf' +++ wg' =
WGen $ \ws mx' ->
case mx' of
Left x' -> do
(mx, wf) <- toGen wf' ws x'
return (fmap Left mx, wf +++ wg')
Right x' -> do
(mx, wg) <- toGen wg' ws x'
return (fmap Right mx, wf' +++ wg)
wf' ||| wg' =
WGen $ \ws mx' ->
case mx' of
Left x' -> do
(mx, wf) <- toGen wf' ws x'
return (mx, wf ||| wg')
Right x' -> do
(mx, wg) <- toGen wg' ws x'
return (mx, wf' ||| wg)
instance ArrowPlus Wire where
WGen f <+> wg =
WGen $ \ws x' -> do
(mx, w1) <- f ws x'
case mx of
Just _ -> return (mx, w1 <+> wg)
Nothing -> do
(mx2, w2) <- toGen wg ws x'
return (mx2, w1 <+> w2)
wf <+> WGen _ = WGen (toGen wf)
wa@(WArr _) <+> _ = wa
wc@(WConst _) <+> _ = wc
WId <+> _ = WId
instance ArrowZero Wire where
zeroArrow = mkGen $ \_ _ -> return (Nothing, zeroArrow)
instance Category Wire where
id = WId
wf@(WGen f) . WGen g =
WGen $ \ws x'' -> do
(mx', w1) <- g ws x''
case mx' of
Nothing -> return (Nothing, wf . w1)
Just x' -> do
(mx, w2) <- f ws x'
return (mx, w2 . w1)
wf@(WArr f) . WGen g =
WGen $ \ws x' -> do
(mx, w) <- g ws x'
return (fmap f mx, wf . w)
wc@(WConst c) . WGen g =
WGen $ \ws x' -> do
(mx, w) <- g ws x'
return (fmap (const c) mx, wc . w)
WGen f . wg@(WArr g) =
WGen $ \ws x' -> do
(mx, w) <- f ws (g x')
return (mx, w . wg)
WGen f . wc@(WConst c) =
WGen $ \ws _ -> do
(mx, w) <- f ws c
return (mx, w . wc)
WArr f . WArr g = WArr (f . g)
WArr f . WConst c = WArr (const (f c))
WConst c . WArr _ = WConst c
WConst c . WConst _ = WConst c
WId . w2 = w2
w1 . WId = w1
instance Functor (Wire a) where
fmap f (WGen w') =
WGen $ \ws x' -> do
(x, w) <- w' ws x'
return (fmap f x, fmap f w)
fmap f (WArr g) = WArr (f . g)
fmap f (WConst c) = WConst (f c)
fmap f WId = WArr f
data WireState =
WireState {
wsDTime :: Double,
wsRndGen :: MTGen,
wsReqVar :: TVar Int
}
cleanupWireState :: WireState -> IO ()
cleanupWireState _ = return ()
initWireState :: IO WireState
initWireState =
WireState
<$> pure 0
<*> getStdGen
<*> newTVarIO 0
mkGen :: (WireState -> a -> IO (Maybe b, Wire a b)) -> Wire a b
mkGen = WGen
toGen :: Wire a b -> WireState -> a -> IO (Maybe b, Wire a b)
toGen (WGen f) ws x = f ws x
toGen wf@(WArr f) _ x = return (Just (f x), wf)
toGen wc@(WConst c) _ _ = return (Just c, wc)
toGen wi@WId _ x = return (Just x, wi)