{-# 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 = forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (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
(.) = 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
b, Circuit m a a
cir1) <- 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) <- 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)
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 = forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \b
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (b -> c
f b
a, 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \(b
b, d
d) ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, d
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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \(d
d, b
b) ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return ((d
d, c
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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \(b
b, b'
b') ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir1) <- 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) <- 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')
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit m b c
cir1 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \b
b ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (c
c, Circuit m b c
cir1) <- 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) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit m b c
cir1 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \b
b ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do rec ((c
c, d
d), Circuit m (b, d) (c, d)
cir) <- 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))
forall (m :: * -> *) a. Monad m => a -> m a
return (c
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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either b d
ebd ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event 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) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left c
c, 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 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right d
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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either d b
edb ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event 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) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right c
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 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left d
d, 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either b b'
ebb' ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event 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) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left c
c, Circuit m b c
cir1 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) <- 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')
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right c'
c', Circuit m b c
x 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) =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either b c
ebc ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event 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) <- 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)
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit m b d
cir1 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) <- 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')
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit m b d
x 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a -> Event m (b, Circuit m a b)
cir
forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal Signal m a
sa forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do a -> Event m (b, Circuit m a b)
cir <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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') <- 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)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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'
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) = forall (m :: * -> *) a b.
(Stream m a -> Stream m b) -> Processor m a b
Processor forall a b. (a -> b) -> a -> b
$ \Stream m a
sa ->
forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons forall a b. (a -> b) -> a -> b
$
do (a
a, Stream m a
xs) <- forall (m :: * -> *) a. Stream m a -> Process m (a, Stream m a)
runStream Stream m a
sa
(b
b, Circuit m a b
cir') <- 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 = forall (m :: * -> *) a b.
Processor m a b -> Stream m a -> Stream m b
runProcessor (forall (m :: * -> *) a b.
MonadDES m =>
Circuit m a b -> Processor m a b
circuitProcessor Circuit m a b
cir')
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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do b
b <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (a -> Event m b
f a
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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (acc
acc', b
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)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
let t :: Double
t = 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 -> forall a. Maybe a
Nothing
Just Double
t0 -> forall a. a -> Maybe a
Just (Double
t forall a. Num a => a -> a -> a
- Double
t0) }
in forall (m :: * -> *) a. Monad m => a -> m a
return (Arrival a
b, Maybe Double -> Circuit m a (Arrival a)
loop forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Double
t)
in forall {m :: * -> *} {a}.
Monad m =>
Maybe Double -> Circuit m a (Arrival a)
loop 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a0, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *). Point m -> Double
pointTime Point m
p, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (Maybe b
b, Circuit m a (Maybe b)
whether') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (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 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, 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)
Just b
b ->
do (c
c, Circuit m b c
process') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m b c
process b
b)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just c
c, 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')
(<?<) :: 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)
(<?<) = forall a b c. (a -> b -> c) -> b -> a -> c
flip 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 = forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m Bool) -> Circuit m a b -> Circuit m a (Maybe b)
filterCircuitM (forall (m :: * -> *) a. Monad m => a -> m a
return 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Bool
x <- 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') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a b
cir a
a)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just b
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 forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Double
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
init, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Double
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
dt :: Double
dt = Double
t forall a. Num a => a -> a -> a
- Double
t0
v :: Double
v = Double
v0 forall a. Num a => a -> a -> a
+ Double
a0 forall a. Num a => a -> a -> a
* Double
dt
Double
v seq :: forall a b. a -> b -> b
`seq` 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
init, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
case Either Double Double
a0 of
Left Double
v ->
Double
v seq :: forall a b. a -> b -> b
`seq` 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 forall a. Num a => a -> a -> a
- Double
t0
v :: Double
v = Double
v0 forall a. Num a => a -> a -> a
+ Double
a0 forall a. Num a => a -> a -> a
* Double
dt
Double
v seq :: forall a b. a -> b -> b
`seq` 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \t
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let v :: t
v = t
v0 forall a. Num a => a -> a -> a
+ t
a0
t
v seq :: forall a b. a -> b -> b
`seq` 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either a a
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, 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 =
forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> a -> b
$ \Either t t
a ->
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
case Either t t
a0 of
Left t
v ->
t
v seq :: forall a b. a -> b -> b
`seq` 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 forall a. Num a => a -> a -> a
+ t
a0
t
v seq :: forall a b. a -> b -> b
`seq` 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 = 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 =
forall (m :: * -> *) a. (Run m -> m a) -> Simulation m a
Simulation forall a b. (a -> b) -> a -> b
$ \Run m
r ->
do Ref m (Circuit m a b)
ref <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Circuit m a b
cir
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) e.
MonadMemo m =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (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 =
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do a
a <- forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m a
m
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a.
EventQueueing m =>
Event m a -> Dynamics m a
runEvent forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Circuit m a a
cir <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a a
cir a
a
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef Ref m (Circuit m a a)
ref Circuit m a a
cir'
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 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
iterateCircuitInPoints_ (Point m
p : [Point m]
ps) Circuit m a a
cir a
a =
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (a
a', Circuit m a a
cir') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m a a
cir a
a
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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 = 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 =
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (t
a', Circuit m t t
cir') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Circuit m a b -> a -> Event m (b, Circuit m a b)
runCircuit Circuit m t t
cir t
a
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
Task m a
task <- forall (m :: * -> *) a.
MonadDES m =>
Process m a -> Event m (Task m a)
runTask forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal SignalSource m a
source
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
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
iterateCircuitInPointsMaybe (Point m
p : [Point m]
ps) Circuit m a (Maybe a)
cir a
a =
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (Maybe a
a', Circuit m a (Maybe a)
cir') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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 -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
a' ->
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 = 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 = 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 =
forall (m :: * -> *).
EventQueueing m =>
Double -> EventPriority -> Event m () -> Event m ()
enqueueEventWithPriority (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) (forall (m :: * -> *). Point m -> EventPriority
pointPriority Point m
p) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p' ->
do (Either a a
ba', Circuit m a (Either a a)
cir') <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ 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
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
Task m (Either b a)
task <- forall (m :: * -> *) a.
MonadDES m =>
Process m a -> Event m (Task m a)
runTask forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal SignalSource m (Either b a)
source
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 (forall a b. b -> Either a b
Right a
a) SignalSource m (Either b a)
source
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall (m :: * -> *). Point m -> [Point m]
integPointsStartingFrom Point m
p
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let ps :: [Point m]
ps = forall a b. (a -> b) -> [a] -> [b]
map (\Double
t -> forall (m :: * -> *). Run m -> Double -> EventPriority -> Point m
pointAt (forall (m :: * -> *). Point m -> Run m
pointRun Point m
p) Double
t EventPriority
0) [Double]
ts
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
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 = forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit forall a b. (a -> b) -> 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 -> 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 ->
forall (m :: * -> *) a.
MonadDES m =>
String -> Event m a -> Event m a
traceEvent String
message forall a b. (a -> b) -> 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 -> forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit 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 ->
forall (m :: * -> *) a.
MonadDES m =>
String -> Event m a -> Event m a
traceEvent String
message forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a b.
(a -> Event m (b, Circuit m a b)) -> Circuit m a b
Circuit 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')