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