{-# LANGUAGE RecursiveDo, Arrows #-}
module Simulation.Aivika.Trans.Circuit
(
Circuit(..),
iterateCircuitInIntegTimes,
iterateCircuitInIntegTimes_,
iterateCircuitInIntegTimesMaybe,
iterateCircuitInIntegTimesEither,
iterateCircuitInTimes,
iterateCircuitInTimes_,
iterateCircuitInTimesMaybe,
iterateCircuitInTimesEither,
arrCircuit,
accumCircuit,
arrivalCircuit,
delayCircuit,
timeCircuit,
(<?<),
(>?>),
filterCircuit,
filterCircuitM,
neverCircuit,
circuitSignaling,
circuitProcessor,
integCircuit,
integCircuitEither,
sumCircuit,
sumCircuitEither,
circuitTransform,
traceCircuit) where
import qualified Control.Category as C
import Control.Arrow
import Control.Monad.Fix
import Simulation.Aivika.Trans.Ref.Base
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.SD
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Dynamics
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Dynamics.Memo
import Simulation.Aivika.Trans.Transform
import Simulation.Aivika.Trans.SystemDynamics
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Stream
import Simulation.Aivika.Trans.Process
import Simulation.Aivika.Trans.Processor
import Simulation.Aivika.Trans.Task
import Simulation.Aivika.Arrival (Arrival(..))
newtype Circuit m a b =
Circuit { forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit :: a -> Event m (b, Circuit m a b)
}
instance MonadDES m => C.Category (Circuit m) where
{-# INLINABLE id #-}
id :: forall a. Circuit m a a
id = (a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (a, Circuit m a a)) -> Circuit m a a)
-> (a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall a b. (a -> b) -> a -> b
$ \a
a -> (a, Circuit m a a) -> Event m (a, Circuit m a a)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, Circuit m a a
forall a. Circuit m a a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
C.id)
{-# INLINABLE (.) #-}
. :: forall b c a. Circuit m b c -> Circuit m a b -> Circuit m a c
(.) = Circuit m b c -> Circuit m a b -> Circuit m a c
forall {m :: * -> *} {a} {b} {a}.
Monad m =>
Circuit m a b -> Circuit m a a -> Circuit m a b
dot
where
(Circuit a -> Event m (b, Circuit m a b)
g) dot :: Circuit m a b -> Circuit m a a -> Circuit m a b
`dot` (Circuit a -> Event m (a, Circuit m a a)
f) =
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b))
-> (Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
b, Circuit m a a
cir1) <- Point m -> Event m (a, Circuit m a a) -> m (a, Circuit m a a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m (a, Circuit m a a)
f a
a)
(b
c, Circuit m a b
cir2) <- Point m -> Event m (b, Circuit m a b) -> m (b, Circuit m a b)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m (b, Circuit m a b)
g a
b)
(b, Circuit m a b) -> m (b, Circuit m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
c, Circuit m a b
cir2 Circuit m a b -> Circuit m a a -> Circuit m a b
`dot` Circuit m a a
cir1)
instance MonadDES m => Arrow (Circuit m) where
{-# INLINABLE arr #-}
arr :: forall b c. (b -> c) -> Circuit m b c
arr b -> c
f = (b -> Event m (c, Circuit m b c)) -> Circuit m b c
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((b -> Event m (c, Circuit m b c)) -> Circuit m b c)
-> (b -> Event m (c, Circuit m b c)) -> Circuit m b c
forall a b. (a -> b) -> a -> b
$ \b
a -> (c, Circuit m b c) -> Event m (c, Circuit m b c)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> c
f b
a, (b -> c) -> Circuit m b c
forall b c. (b -> c) -> Circuit m b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr b -> c
f)
{-# INLINABLE first #-}
first :: forall b c d. Circuit m b c -> Circuit m (b, d) (c, d)
first (Circuit b -> Event m (c, Circuit m b c)
f) =
((b, d) -> Event m ((c, d), Circuit m (b, d) (c, d)))
-> Circuit m (b, d) (c, d)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit (((b, d) -> Event m ((c, d), Circuit m (b, d) (c, d)))
-> Circuit m (b, d) (c, d))
-> ((b, d) -> Event m ((c, d), Circuit m (b, d) (c, d)))
-> Circuit m (b, d) (c, d)
forall a b. (a -> b) -> a -> b
$ \(b
b, d
d) ->
(Point m -> m ((c, d), Circuit m (b, d) (c, d)))
-> Event m ((c, d), Circuit m (b, d) (c, d))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ((c, d), Circuit m (b, d) (c, d)))
-> Event m ((c, d), Circuit m (b, d) (c, d)))
-> (Point m -> m ((c, d), Circuit m (b, d) (c, d)))
-> Event m ((c, d), Circuit m (b, d) (c, d))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
((c, d), Circuit m (b, d) (c, d))
-> m ((c, d), Circuit m (b, d) (c, d))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, d
d), Circuit m b c -> Circuit m (b, d) (c, d)
forall b c d. Circuit m b c -> Circuit m (b, d) (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Circuit m b c
cir)
{-# INLINABLE second #-}
second :: forall b c d. Circuit m b c -> Circuit m (d, b) (d, c)
second (Circuit b -> Event m (c, Circuit m b c)
f) =
((d, b) -> Event m ((d, c), Circuit m (d, b) (d, c)))
-> Circuit m (d, b) (d, c)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit (((d, b) -> Event m ((d, c), Circuit m (d, b) (d, c)))
-> Circuit m (d, b) (d, c))
-> ((d, b) -> Event m ((d, c), Circuit m (d, b) (d, c)))
-> Circuit m (d, b) (d, c)
forall a b. (a -> b) -> a -> b
$ \(d
d, b
b) ->
(Point m -> m ((d, c), Circuit m (d, b) (d, c)))
-> Event m ((d, c), Circuit m (d, b) (d, c))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ((d, c), Circuit m (d, b) (d, c)))
-> Event m ((d, c), Circuit m (d, b) (d, c)))
-> (Point m -> m ((d, c), Circuit m (d, b) (d, c)))
-> Event m ((d, c), Circuit m (d, b) (d, c))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
((d, c), Circuit m (d, b) (d, c))
-> m ((d, c), Circuit m (d, b) (d, c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((d
d, c
c), Circuit m b c -> Circuit m (d, b) (d, c)
forall b c d. Circuit m b c -> Circuit m (d, b) (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second Circuit m b c
cir)
{-# INLINABLE (***) #-}
(Circuit b -> Event m (c, Circuit m b c)
f) *** :: forall b c b' c'.
Circuit m b c -> Circuit m b' c' -> Circuit m (b, b') (c, c')
*** (Circuit b' -> Event m (c', Circuit m b' c')
g) =
((b, b') -> Event m ((c, c'), Circuit m (b, b') (c, c')))
-> Circuit m (b, b') (c, c')
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit (((b, b') -> Event m ((c, c'), Circuit m (b, b') (c, c')))
-> Circuit m (b, b') (c, c'))
-> ((b, b') -> Event m ((c, c'), Circuit m (b, b') (c, c')))
-> Circuit m (b, b') (c, c')
forall a b. (a -> b) -> a -> b
$ \(b
b, b'
b') ->
(Point m -> m ((c, c'), Circuit m (b, b') (c, c')))
-> Event m ((c, c'), Circuit m (b, b') (c, c'))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ((c, c'), Circuit m (b, b') (c, c')))
-> Event m ((c, c'), Circuit m (b, b') (c, c')))
-> (Point m -> m ((c, c'), Circuit m (b, b') (c, c')))
-> Event m ((c, c'), Circuit m (b, b') (c, c'))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir1) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
(c'
c', Circuit m b' c'
cir2) <- Point m -> Event m (c', Circuit m b' c') -> m (c', Circuit m b' c')
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b' -> Event m (c', Circuit m b' c')
g b'
b')
((c, c'), Circuit m (b, b') (c, c'))
-> m ((c, c'), Circuit m (b, b') (c, c'))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit m b c
cir1 Circuit m b c -> Circuit m b' c' -> Circuit m (b, b') (c, c')
forall b c b' c'.
Circuit m b c -> Circuit m b' c' -> Circuit m (b, b') (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Circuit m b' c'
cir2)
{-# INLINABLE (&&&) #-}
(Circuit b -> Event m (c, Circuit m b c)
f) &&& :: forall b c c'.
Circuit m b c -> Circuit m b c' -> Circuit m b (c, c')
&&& (Circuit b -> Event m (c', Circuit m b c')
g) =
(b -> Event m ((c, c'), Circuit m b (c, c')))
-> Circuit m b (c, c')
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((b -> Event m ((c, c'), Circuit m b (c, c')))
-> Circuit m b (c, c'))
-> (b -> Event m ((c, c'), Circuit m b (c, c')))
-> Circuit m b (c, c')
forall a b. (a -> b) -> a -> b
$ \b
b ->
(Point m -> m ((c, c'), Circuit m b (c, c')))
-> Event m ((c, c'), Circuit m b (c, c'))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ((c, c'), Circuit m b (c, c')))
-> Event m ((c, c'), Circuit m b (c, c')))
-> (Point m -> m ((c, c'), Circuit m b (c, c')))
-> Event m ((c, c'), Circuit m b (c, c'))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir1) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
(c'
c', Circuit m b c'
cir2) <- Point m -> Event m (c', Circuit m b c') -> m (c', Circuit m b c')
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c', Circuit m b c')
g b
b)
((c, c'), Circuit m b (c, c')) -> m ((c, c'), Circuit m b (c, c'))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit m b c
cir1 Circuit m b c -> Circuit m b c' -> Circuit m b (c, c')
forall b c c'.
Circuit m b c -> Circuit m b c' -> Circuit m b (c, c')
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Circuit m b c'
cir2)
instance (MonadDES m, MonadFix m) => ArrowLoop (Circuit m) where
{-# INLINABLE loop #-}
loop :: forall b d c. Circuit m (b, d) (c, d) -> Circuit m b c
loop (Circuit (b, d) -> Event m ((c, d), Circuit m (b, d) (c, d))
f) =
(b -> Event m (c, Circuit m b c)) -> Circuit m b c
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((b -> Event m (c, Circuit m b c)) -> Circuit m b c)
-> (b -> Event m (c, Circuit m b c)) -> Circuit m b c
forall a b. (a -> b) -> a -> b
$ \b
b ->
(Point m -> m (c, Circuit m b c)) -> Event m (c, Circuit m b c)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (c, Circuit m b c)) -> Event m (c, Circuit m b c))
-> (Point m -> m (c, Circuit m b c)) -> Event m (c, Circuit m b c)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do rec ((c
c, d
d), Circuit m (b, d) (c, d)
cir) <- Point m
-> Event m ((c, d), Circuit m (b, d) (c, d))
-> m ((c, d), Circuit m (b, d) (c, d))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p ((b, d) -> Event m ((c, d), Circuit m (b, d) (c, d))
f (b
b, d
d))
(c, Circuit m b c) -> m (c, Circuit m b c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c
c, Circuit m (b, d) (c, d) -> Circuit m b c
forall b d c. Circuit m (b, d) (c, d) -> Circuit m b c
forall (a :: * -> * -> *) b d c.
ArrowLoop a =>
a (b, d) (c, d) -> a b c
loop Circuit m (b, d) (c, d)
cir)
instance MonadDES m => ArrowChoice (Circuit m) where
{-# INLINABLE left #-}
left :: forall b c d. Circuit m b c -> Circuit m (Either b d) (Either c d)
left x :: Circuit m b c
x@(Circuit b -> Event m (c, Circuit m b c)
f) =
(Either b d
-> Event m (Either c d, Circuit m (Either b d) (Either c d)))
-> Circuit m (Either b d) (Either c d)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either b d
-> Event m (Either c d, Circuit m (Either b d) (Either c d)))
-> Circuit m (Either b d) (Either c d))
-> (Either b d
-> Event m (Either c d, Circuit m (Either b d) (Either c d)))
-> Circuit m (Either b d) (Either c d)
forall a b. (a -> b) -> a -> b
$ \Either b d
ebd ->
(Point m -> m (Either c d, Circuit m (Either b d) (Either c d)))
-> Event m (Either c d, Circuit m (Either b d) (Either c d))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Either c d, Circuit m (Either b d) (Either c d)))
-> Event m (Either c d, Circuit m (Either b d) (Either c d)))
-> (Point m -> m (Either c d, Circuit m (Either b d) (Either c d)))
-> Event m (Either c d, Circuit m (Either b d) (Either c d))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either b d
ebd of
Left b
b ->
do (c
c, Circuit m b c
cir) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
(Either c d, Circuit m (Either b d) (Either c d))
-> m (Either c d, Circuit m (Either b d) (Either c d))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c d
forall a b. a -> Either a b
Left c
c, Circuit m b c -> Circuit m (Either b d) (Either c d)
forall b c d. Circuit m b c -> Circuit m (Either b d) (Either c d)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left Circuit m b c
cir)
Right d
d ->
(Either c d, Circuit m (Either b d) (Either c d))
-> m (Either c d, Circuit m (Either b d) (Either c d))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> Either c d
forall a b. b -> Either a b
Right d
d, Circuit m b c -> Circuit m (Either b d) (Either c d)
forall b c d. Circuit m b c -> Circuit m (Either b d) (Either c d)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left Circuit m b c
x)
{-# INLINABLE right #-}
right :: forall b c d. Circuit m b c -> Circuit m (Either d b) (Either d c)
right x :: Circuit m b c
x@(Circuit b -> Event m (c, Circuit m b c)
f) =
(Either d b
-> Event m (Either d c, Circuit m (Either d b) (Either d c)))
-> Circuit m (Either d b) (Either d c)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either d b
-> Event m (Either d c, Circuit m (Either d b) (Either d c)))
-> Circuit m (Either d b) (Either d c))
-> (Either d b
-> Event m (Either d c, Circuit m (Either d b) (Either d c)))
-> Circuit m (Either d b) (Either d c)
forall a b. (a -> b) -> a -> b
$ \Either d b
edb ->
(Point m -> m (Either d c, Circuit m (Either d b) (Either d c)))
-> Event m (Either d c, Circuit m (Either d b) (Either d c))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Either d c, Circuit m (Either d b) (Either d c)))
-> Event m (Either d c, Circuit m (Either d b) (Either d c)))
-> (Point m -> m (Either d c, Circuit m (Either d b) (Either d c)))
-> Event m (Either d c, Circuit m (Either d b) (Either d c))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either d b
edb of
Right b
b ->
do (c
c, Circuit m b c
cir) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
(Either d c, Circuit m (Either d b) (Either d c))
-> m (Either d c, Circuit m (Either d b) (Either d c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either d c
forall a b. b -> Either a b
Right c
c, Circuit m b c -> Circuit m (Either d b) (Either d c)
forall b c d. Circuit m b c -> Circuit m (Either d b) (Either d c)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right Circuit m b c
cir)
Left d
d ->
(Either d c, Circuit m (Either d b) (Either d c))
-> m (Either d c, Circuit m (Either d b) (Either d c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> Either d c
forall a b. a -> Either a b
Left d
d, Circuit m b c -> Circuit m (Either d b) (Either d c)
forall b c d. Circuit m b c -> Circuit m (Either d b) (Either d c)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right Circuit m b c
x)
{-# INLINABLE (+++) #-}
x :: Circuit m b c
x@(Circuit b -> Event m (c, Circuit m b c)
f) +++ :: forall b c b' c'.
Circuit m b c
-> Circuit m b' c' -> Circuit m (Either b b') (Either c c')
+++ y :: Circuit m b' c'
y@(Circuit b' -> Event m (c', Circuit m b' c')
g) =
(Either b b'
-> Event m (Either c c', Circuit m (Either b b') (Either c c')))
-> Circuit m (Either b b') (Either c c')
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either b b'
-> Event m (Either c c', Circuit m (Either b b') (Either c c')))
-> Circuit m (Either b b') (Either c c'))
-> (Either b b'
-> Event m (Either c c', Circuit m (Either b b') (Either c c')))
-> Circuit m (Either b b') (Either c c')
forall a b. (a -> b) -> a -> b
$ \Either b b'
ebb' ->
(Point m -> m (Either c c', Circuit m (Either b b') (Either c c')))
-> Event m (Either c c', Circuit m (Either b b') (Either c c'))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m
-> m (Either c c', Circuit m (Either b b') (Either c c')))
-> Event m (Either c c', Circuit m (Either b b') (Either c c')))
-> (Point m
-> m (Either c c', Circuit m (Either b b') (Either c c')))
-> Event m (Either c c', Circuit m (Either b b') (Either c c'))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either b b'
ebb' of
Left b
b ->
do (c
c, Circuit m b c
cir1) <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (c, Circuit m b c)
f b
b)
(Either c c', Circuit m (Either b b') (Either c c'))
-> m (Either c c', Circuit m (Either b b') (Either c c'))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c c'
forall a b. a -> Either a b
Left c
c, Circuit m b c
cir1 Circuit m b c
-> Circuit m b' c' -> Circuit m (Either b b') (Either c c')
forall b c b' c'.
Circuit m b c
-> Circuit m b' c' -> Circuit m (Either b b') (Either c c')
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ Circuit m b' c'
y)
Right b'
b' ->
do (c'
c', Circuit m b' c'
cir2) <- Point m -> Event m (c', Circuit m b' c') -> m (c', Circuit m b' c')
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b' -> Event m (c', Circuit m b' c')
g b'
b')
(Either c c', Circuit m (Either b b') (Either c c'))
-> m (Either c c', Circuit m (Either b b') (Either c c'))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c' -> Either c c'
forall a b. b -> Either a b
Right c'
c', Circuit m b c
x Circuit m b c
-> Circuit m b' c' -> Circuit m (Either b b') (Either c c')
forall b c b' c'.
Circuit m b c
-> Circuit m b' c' -> Circuit m (Either b b') (Either c c')
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ Circuit m b' c'
cir2)
{-# INLINABLE (|||) #-}
x :: Circuit m b d
x@(Circuit b -> Event m (d, Circuit m b d)
f) ||| :: forall b d c.
Circuit m b d -> Circuit m c d -> Circuit m (Either b c) d
||| y :: Circuit m c d
y@(Circuit c -> Event m (d, Circuit m c d)
g) =
(Either b c -> Event m (d, Circuit m (Either b c) d))
-> Circuit m (Either b c) d
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either b c -> Event m (d, Circuit m (Either b c) d))
-> Circuit m (Either b c) d)
-> (Either b c -> Event m (d, Circuit m (Either b c) d))
-> Circuit m (Either b c) d
forall a b. (a -> b) -> a -> b
$ \Either b c
ebc ->
(Point m -> m (d, Circuit m (Either b c) d))
-> Event m (d, Circuit m (Either b c) d)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (d, Circuit m (Either b c) d))
-> Event m (d, Circuit m (Either b c) d))
-> (Point m -> m (d, Circuit m (Either b c) d))
-> Event m (d, Circuit m (Either b c) d)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either b c
ebc of
Left b
b ->
do (d
d, Circuit m b d
cir1) <- Point m -> Event m (d, Circuit m b d) -> m (d, Circuit m b d)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m (d, Circuit m b d)
f b
b)
(d, Circuit m (Either b c) d) -> m (d, Circuit m (Either b c) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit m b d
cir1 Circuit m b d -> Circuit m c d -> Circuit m (Either b c) d
forall b d c.
Circuit m b d -> Circuit m c d -> Circuit m (Either b c) d
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| Circuit m c d
y)
Right c
b' ->
do (d
d, Circuit m c d
cir2) <- Point m -> Event m (d, Circuit m c d) -> m (d, Circuit m c d)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (c -> Event m (d, Circuit m c d)
g c
b')
(d, Circuit m (Either b c) d) -> m (d, Circuit m (Either b c) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit m b d
x Circuit m b d -> Circuit m c d -> Circuit m (Either b c) d
forall b d c.
Circuit m b d -> Circuit m c d -> Circuit m (Either b c) d
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| Circuit m c d
cir2)
circuitSignaling :: MonadDES m => Circuit m a b -> Signal m a -> Signal m b
{-# INLINABLE circuitSignaling #-}
circuitSignaling :: forall (m :: * -> *) a b.
MonadDES m =>
Circuit m a b -> Signal m a -> Signal m b
circuitSignaling (Circuit a -> Event m (b, Circuit m a b)
cir) Signal m a
sa =
Signal { handleSignal :: (b -> Event m ()) -> Event m (DisposableEvent m)
handleSignal = \b -> Event m ()
f ->
do Ref m (a -> Event m (b, Circuit m a b))
r <- Simulation m (Ref m (a -> Event m (b, Circuit m a b)))
-> Event m (Ref m (a -> Event m (b, Circuit m a b)))
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (a -> Event m (b, Circuit m a b)))
-> Event m (Ref m (a -> Event m (b, Circuit m a b))))
-> Simulation m (Ref m (a -> Event m (b, Circuit m a b)))
-> Event m (Ref m (a -> Event m (b, Circuit m a b)))
forall a b. (a -> b) -> a -> b
$ (a -> Event m (b, Circuit m a b))
-> Simulation m (Ref m (a -> Event m (b, Circuit m a b)))
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a -> Event m (b, Circuit m a b)
cir
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal Signal m a
sa ((a -> Event m ()) -> Event m (DisposableEvent m))
-> (a -> Event m ()) -> Event m (DisposableEvent m)
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do a -> Event m (b, Circuit m a b)
cir <- Point m
-> Event m (a -> Event m (b, Circuit m a b))
-> m (a -> Event m (b, Circuit m a b))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (a -> Event m (b, Circuit m a b))
-> m (a -> Event m (b, Circuit m a b)))
-> Event m (a -> Event m (b, Circuit m a b))
-> m (a -> Event m (b, Circuit m a b))
forall a b. (a -> b) -> a -> b
$ Ref m (a -> Event m (b, Circuit m a b))
-> Event m (a -> Event m (b, Circuit m a b))
forall a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m (a -> Event m (b, Circuit m a b))
r
(b
b, Circuit a -> Event m (b, Circuit m a b)
cir') <- Point m -> Event m (b, Circuit m a b) -> m (b, Circuit m a b)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m (b, Circuit m a b)
cir a
a)
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (a -> Event m (b, Circuit m a b))
-> (a -> Event m (b, Circuit m a b)) -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef Ref m (a -> Event m (b, Circuit m a b))
r a -> Event m (b, Circuit m a b)
cir'
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (b -> Event m ()
f b
b) }
circuitProcessor :: MonadDES m => Circuit m a b -> Processor m a b
{-# INLINABLE circuitProcessor #-}
circuitProcessor :: forall (m :: * -> *) a b.
MonadDES m =>
Circuit m a b -> Processor m a b
circuitProcessor (Circuit a -> Event m (b, Circuit m a b)
cir) = (Stream m a -> Stream m b) -> Processor m a b
forall (m :: * -> *) a b.
(Stream m a -> Stream m b) -> Processor m a b
Processor ((Stream m a -> Stream m b) -> Processor m a b)
-> (Stream m a -> Stream m b) -> Processor m a b
forall a b. (a -> b) -> a -> b
$ \Stream m a
sa ->
Process m (b, Stream m b) -> Stream m b
forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons (Process m (b, Stream m b) -> Stream m b)
-> Process m (b, Stream m b) -> Stream m b
forall a b. (a -> b) -> a -> b
$
do (a
a, Stream m a
xs) <- Stream m a -> Process m (a, Stream m a)
forall (m :: * -> *) a. Stream m a -> Process m (a, Stream m a)
runStream Stream m a
sa
(b
b, Circuit m a b
cir') <- Event m (b, Circuit m a b) -> Process m (b, Circuit m a b)
forall a. Event m a -> Process m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (a -> Event m (b, Circuit m a b)
cir a
a)
let f :: Stream m a -> Stream m b
f = Processor m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Processor m a b -> Stream m a -> Stream m b
runProcessor (Circuit m a b -> Processor m a b
forall (m :: * -> *) a b.
MonadDES m =>
Circuit m a b -> Processor m a b
circuitProcessor Circuit m a b
cir')
(b, Stream m b) -> Process m (b, Stream m b)
forall a. a -> Process m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Stream m a -> Stream m b
f Stream m a
xs)
arrCircuit :: MonadDES m => (a -> Event m b) -> Circuit m a b
{-# INLINABLE arrCircuit #-}
arrCircuit :: forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Circuit m a b
arrCircuit a -> Event m b
f =
let x :: Circuit m a b
x =
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b))
-> (Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do b
b <- Point m -> Event m b -> m b
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m b
f a
a)
(b, Circuit m a b) -> m (b, Circuit m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Circuit m a b
x)
in Circuit m a b
x
accumCircuit :: MonadDES m => (acc -> a -> Event m (acc, b)) -> acc -> Circuit m a b
{-# INLINABLE accumCircuit #-}
accumCircuit :: forall (m :: * -> *) acc a b.
MonadDES m =>
(acc -> a -> Event m (acc, b)) -> acc -> Circuit m a b
accumCircuit acc -> a -> Event m (acc, b)
f acc
acc =
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b))
-> (Point m -> m (b, Circuit m a b)) -> Event m (b, Circuit m a b)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (acc
acc', b
b) <- Point m -> Event m (acc, b) -> m (acc, b)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (acc -> a -> Event m (acc, b)
f acc
acc a
a)
(b, Circuit m a b) -> m (b, Circuit m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (acc -> a -> Event m (acc, b)) -> acc -> Circuit m a b
forall (m :: * -> *) acc a b.
MonadDES m =>
(acc -> a -> Event m (acc, b)) -> acc -> Circuit m a b
accumCircuit acc -> a -> Event m (acc, b)
f acc
acc')
arrivalCircuit :: MonadDES m => Circuit m a (Arrival a)
{-# INLINABLE arrivalCircuit #-}
arrivalCircuit :: forall (m :: * -> *) a. MonadDES m => Circuit m a (Arrival a)
arrivalCircuit =
let loop :: Maybe Double -> Circuit m a (Arrival a)
loop Maybe Double
t0 =
(a -> Event m (Arrival a, Circuit m a (Arrival a)))
-> Circuit m a (Arrival a)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (Arrival a, Circuit m a (Arrival a)))
-> Circuit m a (Arrival a))
-> (a -> Event m (Arrival a, Circuit m a (Arrival a)))
-> Circuit m a (Arrival a)
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (Arrival a, Circuit m a (Arrival a)))
-> Event m (Arrival a, Circuit m a (Arrival a))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Arrival a, Circuit m a (Arrival a)))
-> Event m (Arrival a, Circuit m a (Arrival a)))
-> (Point m -> m (Arrival a, Circuit m a (Arrival a)))
-> Event m (Arrival a, Circuit m a (Arrival a))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
b :: Arrival a
b = Arrival { arrivalValue :: a
arrivalValue = a
a,
arrivalTime :: Double
arrivalTime = Double
t,
arrivalDelay :: Maybe Double
arrivalDelay =
case Maybe Double
t0 of
Maybe Double
Nothing -> Maybe Double
forall a. Maybe a
Nothing
Just Double
t0 -> Double -> Maybe Double
forall a. a -> Maybe a
Just (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0) }
in (Arrival a, Circuit m a (Arrival a))
-> m (Arrival a, Circuit m a (Arrival a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Arrival a
b, Maybe Double -> Circuit m a (Arrival a)
loop (Maybe Double -> Circuit m a (Arrival a))
-> Maybe Double -> Circuit m a (Arrival a)
forall a b. (a -> b) -> a -> b
$ Double -> Maybe Double
forall a. a -> Maybe a
Just Double
t)
in Maybe Double -> Circuit m a (Arrival a)
forall {m :: * -> *} {a}.
Monad m =>
Maybe Double -> Circuit m a (Arrival a)
loop Maybe Double
forall a. Maybe a
Nothing
delayCircuit :: MonadDES m => a -> Circuit m a a
{-# INLINABLE delayCircuit #-}
delayCircuit :: forall (m :: * -> *) a. MonadDES m => a -> Circuit m a a
delayCircuit a
a0 =
(a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (a, Circuit m a a)) -> Circuit m a a)
-> (a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall a b. (a -> b) -> a -> b
$ \a
a ->
(a, Circuit m a a) -> Event m (a, Circuit m a a)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a0, a -> Circuit m a a
forall (m :: * -> *) a. MonadDES m => a -> Circuit m a a
delayCircuit a
a)
timeCircuit :: MonadDES m => Circuit m a Double
{-# INLINABLE timeCircuit #-}
timeCircuit :: forall (m :: * -> *) a. MonadDES m => Circuit m a Double
timeCircuit =
(a -> Event m (Double, Circuit m a Double)) -> Circuit m a Double
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (Double, Circuit m a Double)) -> Circuit m a Double)
-> (a -> Event m (Double, Circuit m a Double))
-> Circuit m a Double
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (Double, Circuit m a Double))
-> Event m (Double, Circuit m a Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Double, Circuit m a Double))
-> Event m (Double, Circuit m a Double))
-> (Point m -> m (Double, Circuit m a Double))
-> Event m (Double, Circuit m a Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
(Double, Circuit m a Double) -> m (Double, Circuit m a Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p, Circuit m a Double
forall (m :: * -> *) a. MonadDES m => Circuit m a Double
timeCircuit)
(>?>) :: MonadDES m
=> Circuit m a (Maybe b)
-> Circuit m b c
-> Circuit m a (Maybe c)
{-# INLINABLE (>?>) #-}
Circuit m a (Maybe b)
whether >?> :: forall (m :: * -> *) a b c.
MonadDES m =>
Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
>?> Circuit m b c
process =
(a -> Event m (Maybe c, Circuit m a (Maybe c)))
-> Circuit m a (Maybe c)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (Maybe c, Circuit m a (Maybe c)))
-> Circuit m a (Maybe c))
-> (a -> Event m (Maybe c, Circuit m a (Maybe c)))
-> Circuit m a (Maybe c)
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (Maybe c, Circuit m a (Maybe c)))
-> Event m (Maybe c, Circuit m a (Maybe c))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Maybe c, Circuit m a (Maybe c)))
-> Event m (Maybe c, Circuit m a (Maybe c)))
-> (Point m -> m (Maybe c, Circuit m a (Maybe c)))
-> Event m (Maybe c, Circuit m a (Maybe c))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (Maybe b
b, Circuit m a (Maybe b)
whether') <- Point m
-> Event m (Maybe b, Circuit m a (Maybe b))
-> m (Maybe b, Circuit m a (Maybe b))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Circuit m a (Maybe b)
-> a -> Event m (Maybe b, Circuit m a (Maybe b))
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a (Maybe b)
whether a
a)
case Maybe b
b of
Maybe b
Nothing ->
(Maybe c, Circuit m a (Maybe c))
-> m (Maybe c, Circuit m a (Maybe c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe c
forall a. Maybe a
Nothing, Circuit m a (Maybe b)
whether' Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
forall (m :: * -> *) a b c.
MonadDES m =>
Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
>?> Circuit m b c
process)
Just b
b ->
do (c
c, Circuit m b c
process') <- Point m -> Event m (c, Circuit m b c) -> m (c, Circuit m b c)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Circuit m b c -> b -> Event m (c, Circuit m b c)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m b c
process b
b)
(Maybe c, Circuit m a (Maybe c))
-> m (Maybe c, Circuit m a (Maybe c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Maybe c
forall a. a -> Maybe a
Just c
c, Circuit m a (Maybe b)
whether' Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
forall (m :: * -> *) a b c.
MonadDES m =>
Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
>?> Circuit m b c
process')
(<?<) :: MonadDES m
=> Circuit m b c
-> Circuit m a (Maybe b)
-> Circuit m a (Maybe c)
{-# INLINABLE (<?<) #-}
<?< :: forall (m :: * -> *) b c a.
MonadDES m =>
Circuit m b c -> Circuit m a (Maybe b) -> Circuit m a (Maybe c)
(<?<) = (Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c))
-> Circuit m b c -> Circuit m a (Maybe b) -> Circuit m a (Maybe c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
forall (m :: * -> *) a b c.
MonadDES m =>
Circuit m a (Maybe b) -> Circuit m b c -> Circuit m a (Maybe c)
(>?>)
filterCircuit :: MonadDES m => (a -> Bool) -> Circuit m a b -> Circuit m a (Maybe b)
{-# INLINABLE filterCircuit #-}
filterCircuit :: forall (m :: * -> *) a b.
MonadDES m =>
(a -> Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuit a -> Bool
pred = (a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuitM (Bool -> Event m Bool
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Event m Bool) -> (a -> Bool) -> a -> Event m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
pred)
filterCircuitM :: MonadDES m => (a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
{-# INLINABLE filterCircuitM #-}
filterCircuitM :: forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuitM a -> Event m Bool
pred Circuit m a b
cir =
(a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b))
-> (a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b)
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (Maybe b, Circuit m a (Maybe b)))
-> Event m (Maybe b, Circuit m a (Maybe b))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Maybe b, Circuit m a (Maybe b)))
-> Event m (Maybe b, Circuit m a (Maybe b)))
-> (Point m -> m (Maybe b, Circuit m a (Maybe b)))
-> Event m (Maybe b, Circuit m a (Maybe b))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Bool
x <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m Bool
pred a
a)
if Bool
x
then do (b
b, Circuit m a b
cir') <- Point m -> Event m (b, Circuit m a b) -> m (b, Circuit m a b)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Circuit m a b -> a -> Event m (b, Circuit m a b)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a b
cir a
a)
(Maybe b, Circuit m a (Maybe b))
-> m (Maybe b, Circuit m a (Maybe b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe b
forall a. a -> Maybe a
Just b
b, (a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuitM a -> Event m Bool
pred Circuit m a b
cir')
else (Maybe b, Circuit m a (Maybe b))
-> m (Maybe b, Circuit m a (Maybe b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b
forall a. Maybe a
Nothing, (a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuitM a -> Event m Bool
pred Circuit m a b
cir)
neverCircuit :: MonadDES m => Circuit m a (Maybe b)
{-# INLINABLE neverCircuit #-}
neverCircuit :: forall (m :: * -> *) a b. MonadDES m => Circuit m a (Maybe b)
neverCircuit =
(a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b)
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b))
-> (a -> Event m (Maybe b, Circuit m a (Maybe b)))
-> Circuit m a (Maybe b)
forall a b. (a -> b) -> a -> b
$ \a
a -> (Maybe b, Circuit m a (Maybe b))
-> Event m (Maybe b, Circuit m a (Maybe b))
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b
forall a. Maybe a
Nothing, Circuit m a (Maybe b)
forall (m :: * -> *) a b. MonadDES m => Circuit m a (Maybe b)
neverCircuit)
integCircuit :: MonadDES m
=> Double
-> Circuit m Double Double
{-# INLINABLE integCircuit #-}
integCircuit :: forall (m :: * -> *).
MonadDES m =>
Double -> Circuit m Double Double
integCircuit Double
init = Circuit m Double Double
start
where
start :: Circuit m Double Double
start =
(Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double)
-> (Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double
forall a b. (a -> b) -> a -> b
$ \Double
a ->
(Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double))
-> (Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
(Double, Circuit m Double Double)
-> m (Double, Circuit m Double Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
init, Double -> Double -> Double -> Circuit m Double Double
forall {m :: * -> *}.
Monad m =>
Double -> Double -> Double -> Circuit m Double Double
next Double
t Double
init Double
a)
next :: Double -> Double -> Double -> Circuit m Double Double
next Double
t0 Double
v0 Double
a0 =
(Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double)
-> (Double -> Event m (Double, Circuit m Double Double))
-> Circuit m Double Double
forall a b. (a -> b) -> a -> b
$ \Double
a ->
(Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double))
-> (Point m -> m (Double, Circuit m Double Double))
-> Event m (Double, Circuit m Double Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
dt :: Double
dt = Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
v :: Double
v = Double
v0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
a0 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dt
Double
v Double
-> m (Double, Circuit m Double Double)
-> m (Double, Circuit m Double Double)
forall a b. a -> b -> b
`seq` (Double, Circuit m Double Double)
-> m (Double, Circuit m Double Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
v, Double -> Double -> Double -> Circuit m Double Double
next Double
t Double
v Double
a)
integCircuitEither :: MonadDES m
=> Double
-> Circuit m (Either Double Double) Double
{-# INLINABLE integCircuitEither #-}
integCircuitEither :: forall (m :: * -> *).
MonadDES m =>
Double -> Circuit m (Either Double Double) Double
integCircuitEither Double
init = Circuit m (Either Double Double) Double
start
where
start :: Circuit m (Either Double Double) Double
start =
(Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double)
-> (Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double
forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
(Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double))
-> (Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
(Double, Circuit m (Either Double Double) Double)
-> m (Double, Circuit m (Either Double Double) Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
init, Double
-> Double
-> Either Double Double
-> Circuit m (Either Double Double) Double
forall {m :: * -> *}.
Monad m =>
Double
-> Double
-> Either Double Double
-> Circuit m (Either Double Double) Double
next Double
t Double
init Either Double Double
a)
next :: Double
-> Double
-> Either Double Double
-> Circuit m (Either Double Double) Double
next Double
t0 Double
v0 Either Double Double
a0 =
(Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double)
-> (Either Double Double
-> Event m (Double, Circuit m (Either Double Double) Double))
-> Circuit m (Either Double Double) Double
forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
(Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double))
-> (Point m -> m (Double, Circuit m (Either Double Double) Double))
-> Event m (Double, Circuit m (Either Double Double) Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
case Either Double Double
a0 of
Left Double
v ->
Double
v Double
-> m (Double, Circuit m (Either Double Double) Double)
-> m (Double, Circuit m (Either Double Double) Double)
forall a b. a -> b -> b
`seq` (Double, Circuit m (Either Double Double) Double)
-> m (Double, Circuit m (Either Double Double) Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
v, Double
-> Double
-> Either Double Double
-> Circuit m (Either Double Double) Double
next Double
t Double
v Either Double Double
a)
Right Double
a0 -> do
let dt :: Double
dt = Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
v :: Double
v = Double
v0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
a0 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dt
Double
v Double
-> m (Double, Circuit m (Either Double Double) Double)
-> m (Double, Circuit m (Either Double Double) Double)
forall a b. a -> b -> b
`seq` (Double, Circuit m (Either Double Double) Double)
-> m (Double, Circuit m (Either Double Double) Double)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
v, Double
-> Double
-> Either Double Double
-> Circuit m (Either Double Double) Double
next Double
t Double
v Either Double Double
a)
sumCircuit :: (MonadDES m, Num a)
=> a
-> Circuit m a a
{-# INLINABLE sumCircuit #-}
sumCircuit :: forall (m :: * -> *) a. (MonadDES m, Num a) => a -> Circuit m a a
sumCircuit a
init = Circuit m a a
start
where
start :: Circuit m a a
start =
(a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (a, Circuit m a a)) -> Circuit m a a)
-> (a -> Event m (a, Circuit m a a)) -> Circuit m a a
forall a b. (a -> b) -> a -> b
$ \a
a ->
(Point m -> m (a, Circuit m a a)) -> Event m (a, Circuit m a a)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (a, Circuit m a a)) -> Event m (a, Circuit m a a))
-> (Point m -> m (a, Circuit m a a)) -> Event m (a, Circuit m a a)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
(a, Circuit m a a) -> m (a, Circuit m a a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, a -> a -> Circuit m a a
forall {m :: * -> *} {t}.
(Monad m, Num t) =>
t -> t -> Circuit m t t
next a
init a
a)
next :: t -> t -> Circuit m t t
next t
v0 t
a0 =
(t -> Event m (t, Circuit m t t)) -> Circuit m t t
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((t -> Event m (t, Circuit m t t)) -> Circuit m t t)
-> (t -> Event m (t, Circuit m t t)) -> Circuit m t t
forall a b. (a -> b) -> a -> b
$ \t
a ->
(Point m -> m (t, Circuit m t t)) -> Event m (t, Circuit m t t)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (t, Circuit m t t)) -> Event m (t, Circuit m t t))
-> (Point m -> m (t, Circuit m t t)) -> Event m (t, Circuit m t t)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let v :: t
v = t
v0 t -> t -> t
forall a. Num a => a -> a -> a
+ t
a0
t
v t -> m (t, Circuit m t t) -> m (t, Circuit m t t)
forall a b. a -> b -> b
`seq` (t, Circuit m t t) -> m (t, Circuit m t t)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t
v, t -> t -> Circuit m t t
next t
v t
a)
sumCircuitEither :: (MonadDES m, Num a)
=> a
-> Circuit m (Either a a) a
{-# INLINABLE sumCircuitEither #-}
sumCircuitEither :: forall (m :: * -> *) a.
(MonadDES m, Num a) =>
a -> Circuit m (Either a a) a
sumCircuitEither a
init = Circuit m (Either a a) a
start
where
start :: Circuit m (Either a a) a
start =
(Either a a -> Event m (a, Circuit m (Either a a) a))
-> Circuit m (Either a a) a
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either a a -> Event m (a, Circuit m (Either a a) a))
-> Circuit m (Either a a) a)
-> (Either a a -> Event m (a, Circuit m (Either a a) a))
-> Circuit m (Either a a) a
forall a b. (a -> b) -> a -> b
$ \Either a a
a ->
(Point m -> m (a, Circuit m (Either a a) a))
-> Event m (a, Circuit m (Either a a) a)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (a, Circuit m (Either a a) a))
-> Event m (a, Circuit m (Either a a) a))
-> (Point m -> m (a, Circuit m (Either a a) a))
-> Event m (a, Circuit m (Either a a) a)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
(a, Circuit m (Either a a) a) -> m (a, Circuit m (Either a a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, a -> Either a a -> Circuit m (Either a a) a
forall {m :: * -> *} {t}.
(Monad m, Num t) =>
t -> Either t t -> Circuit m (Either t t) t
next a
init Either a a
a)
next :: t -> Either t t -> Circuit m (Either t t) t
next t
v0 Either t t
a0 =
(Either t t -> Event m (t, Circuit m (Either t t) t))
-> Circuit m (Either t t) t
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((Either t t -> Event m (t, Circuit m (Either t t) t))
-> Circuit m (Either t t) t)
-> (Either t t -> Event m (t, Circuit m (Either t t) t))
-> Circuit m (Either t t) t
forall a b. (a -> b) -> a -> b
$ \Either t t
a ->
(Point m -> m (t, Circuit m (Either t t) t))
-> Event m (t, Circuit m (Either t t) t)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (t, Circuit m (Either t t) t))
-> Event m (t, Circuit m (Either t t) t))
-> (Point m -> m (t, Circuit m (Either t t) t))
-> Event m (t, Circuit m (Either t t) t)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either t t
a0 of
Left t
v ->
t
v t
-> m (t, Circuit m (Either t t) t)
-> m (t, Circuit m (Either t t) t)
forall a b. a -> b -> b
`seq` (t, Circuit m (Either t t) t) -> m (t, Circuit m (Either t t) t)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t
v, t -> Either t t -> Circuit m (Either t t) t
next t
v Either t t
a)
Right t
a0 -> do
let v :: t
v = t
v0 t -> t -> t
forall a. Num a => a -> a -> a
+ t
a0
t
v t
-> m (t, Circuit m (Either t t) t)
-> m (t, Circuit m (Either t t) t)
forall a b. a -> b -> b
`seq` (t, Circuit m (Either t t) t) -> m (t, Circuit m (Either t t) t)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t
v, t -> Either t t -> Circuit m (Either t t) t
next t
v Either t t
a)
circuitTransform :: (MonadSD m, MonadDES m) => Circuit m a b -> Transform m a b
{-# INLINABLE circuitTransform #-}
circuitTransform :: forall (m :: * -> *) a b.
(MonadSD m, MonadDES m) =>
Circuit m a b -> Transform m a b
circuitTransform Circuit m a b
cir = (Dynamics m a -> Simulation m (Dynamics m b)) -> Transform m a b
forall (m :: * -> *) a b.
(Dynamics m a -> Simulation m (Dynamics m b)) -> Transform m a b
Transform Dynamics m a -> Simulation m (Dynamics m b)
start
where
start :: Dynamics m a -> Simulation m (Dynamics m b)
start Dynamics m a
m =
(Run m -> m (Dynamics m b)) -> Simulation m (Dynamics m b)
forall (m :: * -> *) a. (Run m -> m a) -> Simulation m a
Simulation ((Run m -> m (Dynamics m b)) -> Simulation m (Dynamics m b))
-> (Run m -> m (Dynamics m b)) -> Simulation m (Dynamics m b)
forall a b. (a -> b) -> a -> b
$ \Run m
r ->
do Ref m (Circuit m a b)
ref <- Run m
-> Simulation m (Ref m (Circuit m a b))
-> m (Ref m (Circuit m a b))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (Circuit m a b)) -> m (Ref m (Circuit m a b)))
-> Simulation m (Ref m (Circuit m a b))
-> m (Ref m (Circuit m a b))
forall a b. (a -> b) -> a -> b
$ Circuit m a b -> Simulation m (Ref m (Circuit m a b))
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Circuit m a b
cir
Run m -> Simulation m (Dynamics m b) -> m (Dynamics m b)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Dynamics m b) -> m (Dynamics m b))
-> Simulation m (Dynamics m b) -> m (Dynamics m b)
forall a b. (a -> b) -> a -> b
$
Dynamics m b -> Simulation m (Dynamics m b)
forall e. Dynamics m e -> Simulation m (Dynamics m e)
forall (m :: * -> *) e.
MonadMemo m =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Ref m (Circuit m a b) -> Dynamics m a -> Dynamics m b
forall {m :: * -> *} {a} {a}.
(EventQueueing m, MonadRef m) =>
Ref m (Circuit m a a) -> Dynamics m a -> Dynamics m a
next Ref m (Circuit m a b)
ref Dynamics m a
m)
next :: Ref m (Circuit m a a) -> Dynamics m a -> Dynamics m a
next Ref m (Circuit m a a)
ref Dynamics m a
m =
(Point m -> m a) -> Dynamics m a
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m a) -> Dynamics m a)
-> (Point m -> m a) -> Dynamics m a
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do a
a <- Point m -> Dynamics m a -> m a
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m a
m
Point m -> Dynamics m a -> m a
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p (Dynamics m a -> m a) -> Dynamics m a -> m a
forall a b. (a -> b) -> a -> b
$
Event m a -> Dynamics m a
forall a. Event m a -> Dynamics m a
forall (m :: * -> *) a.
EventQueueing m =>
Event m a -> Dynamics m a
runEvent (Event m a -> Dynamics m a) -> Event m a -> Dynamics m a
forall a b. (a -> b) -> a -> b
$
(Point m -> m a) -> Event m a
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m a) -> Event m a) -> (Point m -> m a) -> Event m a
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Circuit m a a
cir <- Point m -> Event m (Circuit m a a) -> m (Circuit m a a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Circuit m a a) -> m (Circuit m a a))
-> Event m (Circuit m a a) -> m (Circuit m a a)
forall a b. (a -> b) -> a -> b
$ Ref m (Circuit m a a) -> Event m (Circuit m a a)
forall a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m (Circuit m a a)
ref
(a
b, Circuit m a a
cir') <- Point m -> Event m (a, Circuit m a a) -> m (a, Circuit m a a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (a, Circuit m a a) -> m (a, Circuit m a a))
-> Event m (a, Circuit m a a) -> m (a, Circuit m a a)
forall a b. (a -> b) -> a -> b
$ Circuit m a a -> a -> Event m (a, Circuit m a a)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a a
cir a
a
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Circuit m a a) -> Circuit m a a -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef Ref m (Circuit m a a)
ref Circuit m a a
cir'
a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
b
iterateCircuitInPoints_ :: MonadDES m => [Point m] -> Circuit m a a -> a -> Event m ()
{-# INLINABLE iterateCircuitInPoints_ #-}
iterateCircuitInPoints_ :: forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m ()
iterateCircuitInPoints_ [] Circuit m a a
cir a
a = () -> Event m ()
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
iterateCircuitInPoints_ (Point m
p : [Point m]
ps) Circuit m a a
cir a
a =
Double -> EventPriority -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (Point m -> EventPriority
forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (a
a', Circuit m a a
cir') <- Point m -> Event m (a, Circuit m a a) -> m (a, Circuit m a a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (a, Circuit m a a) -> m (a, Circuit m a a))
-> Event m (a, Circuit m a a) -> m (a, Circuit m a a)
forall a b. (a -> b) -> a -> b
$ Circuit m a a -> a -> Event m (a, Circuit m a a)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a a
cir a
a
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Point m] -> Circuit m a a -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m ()
iterateCircuitInPoints_ [Point m]
ps Circuit m a a
cir' a
a'
iterateCircuitInPoints :: MonadDES m => [Point m] -> Circuit m a a -> a -> Event m (Task m a)
{-# INLINABLE iterateCircuitInPoints #-}
iterateCircuitInPoints :: forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m (Task m a)
iterateCircuitInPoints [Point m]
ps Circuit m a a
cir a
a =
do let loop :: [Point m] -> Circuit m t t -> t -> SignalSource m t -> Event m ()
loop [] Circuit m t t
cir t
a SignalSource m t
source = SignalSource m t -> t -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal SignalSource m t
source t
a
loop (Point m
p : [Point m]
ps) Circuit m t t
cir t
a SignalSource m t
source =
Double -> EventPriority -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (Point m -> EventPriority
forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (t
a', Circuit m t t
cir') <- Point m -> Event m (t, Circuit m t t) -> m (t, Circuit m t t)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (t, Circuit m t t) -> m (t, Circuit m t t))
-> Event m (t, Circuit m t t) -> m (t, Circuit m t t)
forall a b. (a -> b) -> a -> b
$ Circuit m t t -> t -> Event m (t, Circuit m t t)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m t t
cir t
a
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Point m] -> Circuit m t t -> t -> SignalSource m t -> Event m ()
loop [Point m]
ps Circuit m t t
cir' t
a' SignalSource m t
source
SignalSource m a
source <- Simulation m (SignalSource m a) -> Event m (SignalSource m a)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation Simulation m (SignalSource m a)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
Task m a
task <- Process m a -> Event m (Task m a)
forall (m :: * -> *) a.
MonadDES m =>
Process m a -> Event m (Task m a)
runTask (Process m a -> Event m (Task m a))
-> Process m a -> Event m (Task m a)
forall a b. (a -> b) -> a -> b
$ Signal m a -> Process m a
forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait (Signal m a -> Process m a) -> Signal m a -> Process m a
forall a b. (a -> b) -> a -> b
$ SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal SignalSource m a
source
[Point m] -> Circuit m a a -> a -> SignalSource m a -> Event m ()
forall {m :: * -> *} {t}.
(EventQueueing m, Monad m) =>
[Point m] -> Circuit m t t -> t -> SignalSource m t -> Event m ()
loop [Point m]
ps Circuit m a a
cir a
a SignalSource m a
source
Task m a -> Event m (Task m a)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return Task m a
task
iterateCircuitInIntegTimes_ :: MonadDES m => Circuit m a a -> a -> Event m ()
{-# INLINABLE iterateCircuitInIntegTimes_ #-}
iterateCircuitInIntegTimes_ :: forall (m :: * -> *) a.
MonadDES m =>
Circuit m a a -> a -> Event m ()
iterateCircuitInIntegTimes_ Circuit m a a
cir a
a =
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = Point m -> [Point m]
forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a a -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m ()
iterateCircuitInPoints_ [Point m]
ps Circuit m a a
cir a
a
iterateCircuitInTimes_ :: MonadDES m => [Double] -> Circuit m a a -> a -> Event m ()
{-# INLINABLE iterateCircuitInTimes_ #-}
iterateCircuitInTimes_ :: forall (m :: * -> *) a.
MonadDES m =>
[Double] -> Circuit m a a -> a -> Event m ()
iterateCircuitInTimes_ [Double]
ts Circuit m a a
cir a
a =
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = (Double -> Point m) -> [Double] -> [Point m]
forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> Run m -> Double -> EventPriority -> Point m
forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a a -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m ()
iterateCircuitInPoints_ [Point m]
ps Circuit m a a
cir a
a
iterateCircuitInIntegTimes :: MonadDES m => Circuit m a a -> a -> Event m (Task m a)
{-# INLINABLE iterateCircuitInIntegTimes #-}
iterateCircuitInIntegTimes :: forall (m :: * -> *) a.
MonadDES m =>
Circuit m a a -> a -> Event m (Task m a)
iterateCircuitInIntegTimes Circuit m a a
cir a
a =
(Point m -> m (Task m a)) -> Event m (Task m a)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Task m a)) -> Event m (Task m a))
-> (Point m -> m (Task m a)) -> Event m (Task m a)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = Point m -> [Point m]
forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
Point m -> Event m (Task m a) -> m (Task m a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Task m a) -> m (Task m a))
-> Event m (Task m a) -> m (Task m a)
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a a -> a -> Event m (Task m a)
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m (Task m a)
iterateCircuitInPoints [Point m]
ps Circuit m a a
cir a
a
iterateCircuitInTimes :: MonadDES m => [Double] -> Circuit m a a -> a -> Event m (Task m a)
{-# INLINABLE iterateCircuitInTimes #-}
iterateCircuitInTimes :: forall (m :: * -> *) a.
MonadDES m =>
[Double] -> Circuit m a a -> a -> Event m (Task m a)
iterateCircuitInTimes [Double]
ts Circuit m a a
cir a
a =
(Point m -> m (Task m a)) -> Event m (Task m a)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Task m a)) -> Event m (Task m a))
-> (Point m -> m (Task m a)) -> Event m (Task m a)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = (Double -> Point m) -> [Double] -> [Point m]
forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> Run m -> Double -> EventPriority -> Point m
forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
Point m -> Event m (Task m a) -> m (Task m a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Task m a) -> m (Task m a))
-> Event m (Task m a) -> m (Task m a)
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a a -> a -> Event m (Task m a)
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a a -> a -> Event m (Task m a)
iterateCircuitInPoints [Point m]
ps Circuit m a a
cir a
a
iterateCircuitInPointsMaybe :: MonadDES m => [Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
{-# INLINABLE iterateCircuitInPointsMaybe #-}
iterateCircuitInPointsMaybe :: forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInPointsMaybe [] Circuit m a (Maybe a)
cir a
a = () -> Event m ()
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
iterateCircuitInPointsMaybe (Point m
p : [Point m]
ps) Circuit m a (Maybe a)
cir a
a =
Double -> EventPriority -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (Point m -> EventPriority
forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (Maybe a
a', Circuit m a (Maybe a)
cir') <- Point m
-> Event m (Maybe a, Circuit m a (Maybe a))
-> m (Maybe a, Circuit m a (Maybe a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe a, Circuit m a (Maybe a))
-> m (Maybe a, Circuit m a (Maybe a)))
-> Event m (Maybe a, Circuit m a (Maybe a))
-> m (Maybe a, Circuit m a (Maybe a))
forall a b. (a -> b) -> a -> b
$ Circuit m a (Maybe a)
-> a -> Event m (Maybe a, Circuit m a (Maybe a))
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a (Maybe a)
cir a
a
case Maybe a
a' of
Maybe a
Nothing -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
a' ->
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInPointsMaybe [Point m]
ps Circuit m a (Maybe a)
cir' a
a'
iterateCircuitInIntegTimesMaybe :: MonadDES m => Circuit m a (Maybe a) -> a -> Event m ()
{-# INLINABLE iterateCircuitInIntegTimesMaybe #-}
iterateCircuitInIntegTimesMaybe :: forall (m :: * -> *) a.
MonadDES m =>
Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInIntegTimesMaybe Circuit m a (Maybe a)
cir a
a =
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = Point m -> [Point m]
forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInPointsMaybe [Point m]
ps Circuit m a (Maybe a)
cir a
a
iterateCircuitInTimesMaybe :: MonadDES m => [Double] -> Circuit m a (Maybe a) -> a -> Event m ()
{-# INLINABLE iterateCircuitInTimesMaybe #-}
iterateCircuitInTimesMaybe :: forall (m :: * -> *) a.
MonadDES m =>
[Double] -> Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInTimesMaybe [Double]
ts Circuit m a (Maybe a)
cir a
a =
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = (Double -> Point m) -> [Double] -> [Point m]
forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> Run m -> Double -> EventPriority -> Point m
forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
[Point m] -> Circuit m a (Maybe a) -> a -> Event m ()
iterateCircuitInPointsMaybe [Point m]
ps Circuit m a (Maybe a)
cir a
a
iterateCircuitInPointsEither :: MonadDES m => [Point m] -> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
{-# INLINABLE iterateCircuitInPointsEither #-}
iterateCircuitInPointsEither :: forall (m :: * -> *) a b.
MonadDES m =>
[Point m]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
iterateCircuitInPointsEither [Point m]
ps Circuit m a (Either b a)
cir a
a =
do let loop :: [Point m]
-> Circuit m a (Either a a)
-> Either a a
-> SignalSource m (Either a a)
-> Event m ()
loop [] Circuit m a (Either a a)
cir Either a a
ba SignalSource m (Either a a)
source = SignalSource m (Either a a) -> Either a a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal SignalSource m (Either a a)
source Either a a
ba
loop [Point m]
ps Circuit m a (Either a a)
cir ba :: Either a a
ba@(Left a
b) SignalSource m (Either a a)
source = SignalSource m (Either a a) -> Either a a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal SignalSource m (Either a a)
source Either a a
ba
loop (Point m
p : [Point m]
ps) Circuit m a (Either a a)
cir (Right a
a) SignalSource m (Either a a)
source =
Double -> EventPriority -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (Point m -> EventPriority
forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
(Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (Either a a
ba', Circuit m a (Either a a)
cir') <- Point m
-> Event m (Either a a, Circuit m a (Either a a))
-> m (Either a a, Circuit m a (Either a a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Either a a, Circuit m a (Either a a))
-> m (Either a a, Circuit m a (Either a a)))
-> Event m (Either a a, Circuit m a (Either a a))
-> m (Either a a, Circuit m a (Either a a))
forall a b. (a -> b) -> a -> b
$ Circuit m a (Either a a)
-> a -> Event m (Either a a, Circuit m a (Either a a))
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a (Either a a)
cir a
a
Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Point m]
-> Circuit m a (Either a a)
-> Either a a
-> SignalSource m (Either a a)
-> Event m ()
loop [Point m]
ps Circuit m a (Either a a)
cir' Either a a
ba' SignalSource m (Either a a)
source
SignalSource m (Either b a)
source <- Simulation m (SignalSource m (Either b a))
-> Event m (SignalSource m (Either b a))
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation Simulation m (SignalSource m (Either b a))
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
Task m (Either b a)
task <- Process m (Either b a) -> Event m (Task m (Either b a))
forall (m :: * -> *) a.
MonadDES m =>
Process m a -> Event m (Task m a)
runTask (Process m (Either b a) -> Event m (Task m (Either b a)))
-> Process m (Either b a) -> Event m (Task m (Either b a))
forall a b. (a -> b) -> a -> b
$ Signal m (Either b a) -> Process m (Either b a)
forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait (Signal m (Either b a) -> Process m (Either b a))
-> Signal m (Either b a) -> Process m (Either b a)
forall a b. (a -> b) -> a -> b
$ SignalSource m (Either b a) -> Signal m (Either b a)
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal SignalSource m (Either b a)
source
[Point m]
-> Circuit m a (Either b a)
-> Either b a
-> SignalSource m (Either b a)
-> Event m ()
forall {m :: * -> *} {a} {a}.
(EventQueueing m, Monad m) =>
[Point m]
-> Circuit m a (Either a a)
-> Either a a
-> SignalSource m (Either a a)
-> Event m ()
loop [Point m]
ps Circuit m a (Either b a)
cir (a -> Either b a
forall a b. b -> Either a b
Right a
a) SignalSource m (Either b a)
source
Task m (Either b a) -> Event m (Task m (Either b a))
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return Task m (Either b a)
task
iterateCircuitInIntegTimesEither :: MonadDES m => Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
{-# INLINABLE iterateCircuitInIntegTimesEither #-}
iterateCircuitInIntegTimesEither :: forall (m :: * -> *) a b.
MonadDES m =>
Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
iterateCircuitInIntegTimesEither Circuit m a (Either b a)
cir a
a =
(Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a)))
-> (Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = Point m -> [Point m]
forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
Point m -> Event m (Task m (Either b a)) -> m (Task m (Either b a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Task m (Either b a)) -> m (Task m (Either b a)))
-> Event m (Task m (Either b a)) -> m (Task m (Either b a))
forall a b. (a -> b) -> a -> b
$
[Point m]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
forall (m :: * -> *) a b.
MonadDES m =>
[Point m]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
iterateCircuitInPointsEither [Point m]
ps Circuit m a (Either b a)
cir a
a
iterateCircuitInTimesEither :: MonadDES m => [Double] -> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
{-# INLINABLE iterateCircuitInTimesEither #-}
iterateCircuitInTimesEither :: forall (m :: * -> *) a b.
MonadDES m =>
[Double]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
iterateCircuitInTimesEither [Double]
ts Circuit m a (Either b a)
cir a
a =
(Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a))
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a)))
-> (Point m -> m (Task m (Either b a)))
-> Event m (Task m (Either b a))
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = (Double -> Point m) -> [Double] -> [Point m]
forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> Run m -> Double -> EventPriority -> Point m
forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
Point m -> Event m (Task m (Either b a)) -> m (Task m (Either b a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Task m (Either b a)) -> m (Task m (Either b a)))
-> Event m (Task m (Either b a)) -> m (Task m (Either b a))
forall a b. (a -> b) -> a -> b
$
[Point m]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
forall (m :: * -> *) a b.
MonadDES m =>
[Point m]
-> Circuit m a (Either b a) -> a -> Event m (Task m (Either b a))
iterateCircuitInPointsEither [Point m]
ps Circuit m a (Either b a)
cir a
a
traceCircuit :: MonadDES m
=> Maybe String
-> Maybe String
-> Circuit m a b
-> Circuit m a b
{-# INLINABLE traceCircuit #-}
traceCircuit :: forall (m :: * -> *) a b.
MonadDES m =>
Maybe String -> Maybe String -> Circuit m a b -> Circuit m a b
traceCircuit Maybe String
request Maybe String
response Circuit m a b
cir = (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ Circuit m a b -> a -> Event m (b, Circuit m a b)
forall {m :: * -> *} {a} {b}.
MonadDES m =>
Circuit m a b -> a -> Event m (b, Circuit m a b)
loop Circuit m a b
cir where
loop :: Circuit m a b -> a -> Event m (b, Circuit m a b)
loop Circuit m a b
cir a
a =
do (b
b, Circuit m a b
cir') <-
case Maybe String
request of
Maybe String
Nothing -> Circuit m a b -> a -> Event m (b, Circuit m a b)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a b
cir a
a
Just String
message ->
String -> Event m (b, Circuit m a b) -> Event m (b, Circuit m a b)
forall (m :: * -> *) a.
MonadDES m =>
String -> Event m a -> Event m a
traceEvent String
message (Event m (b, Circuit m a b) -> Event m (b, Circuit m a b))
-> Event m (b, Circuit m a b) -> Event m (b, Circuit m a b)
forall a b. (a -> b) -> a -> b
$
Circuit m a b -> a -> Event m (b, Circuit m a b)
forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a b
cir a
a
case Maybe String
response of
Maybe String
Nothing -> (b, Circuit m a b) -> Event m (b, Circuit m a b)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ Circuit m a b -> a -> Event m (b, Circuit m a b)
loop Circuit m a b
cir')
Just String
message ->
String -> Event m (b, Circuit m a b) -> Event m (b, Circuit m a b)
forall (m :: * -> *) a.
MonadDES m =>
String -> Event m a -> Event m a
traceEvent String
message (Event m (b, Circuit m a b) -> Event m (b, Circuit m a b))
-> Event m (b, Circuit m a b) -> Event m (b, Circuit m a b)
forall a b. (a -> b) -> a -> b
$
(b, Circuit m a b) -> Event m (b, Circuit m a b)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit ((a -> Event m (b, Circuit m a b)) -> Circuit m a b)
-> (a -> Event m (b, Circuit m a b)) -> Circuit m a b
forall a b. (a -> b) -> a -> b
$ Circuit m a b -> a -> Event m (b, Circuit m a b)
loop Circuit m a b
cir')