{-# LANGUAGE RecursiveDo, Arrows #-}

-- |
-- Module     : Simulation.Aivika.Circuit
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- It represents a circuit synchronized with the event queue.
-- Also it allows creating the recursive links with help of
-- the proc-notation.
--
-- The implementation is based on the <http://en.wikibooks.org/wiki/Haskell/Arrow_tutorial Arrow Tutorial>.
--
module Simulation.Aivika.Circuit
       (-- * The Circuit Arrow
        Circuit(..),
        iterateCircuitInIntegTimes,
        iterateCircuitInIntegTimes_,
        iterateCircuitInIntegTimesMaybe,
        iterateCircuitInIntegTimesEither,
        iterateCircuitInTimes,
        iterateCircuitInTimes_,
        iterateCircuitInTimesMaybe,
        iterateCircuitInTimesEither,
        -- * Circuit Primitives
        arrCircuit,
        accumCircuit,
        -- * The Arrival Circuit
        arrivalCircuit,
        -- * Delaying the Circuit
        delayCircuit,
        -- * The Time Circuit
        timeCircuit,
        -- * Conditional Computation
        (<?<),
        (>?>),
        filterCircuit,
        filterCircuitM,
        neverCircuit,
        -- * Converting to Signals and Processors
        circuitSignaling,
        circuitProcessor,
        -- * Integrals and Difference Equations
        integCircuit,
        integCircuitEither,
        sumCircuit,
        sumCircuitEither,
        -- * The Circuit Transform
        circuitTransform,
        -- * Debugging
        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

-- | Represents a circuit synchronized with the event queue.
-- Besides, it allows creating the recursive links with help of
-- the proc-notation.
--
newtype Circuit a b =
  Circuit { forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit :: a -> Event (b, Circuit a b)
            -- ^ Run the circuit.
          }

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)

-- | Get a signal transform by the specified circuit.
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) }

-- | Transform the circuit to a processor.
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)

-- | Create a simple circuit by the specified handling function
-- that runs the computation for each input value to get an output.
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

-- | Accumulator that outputs a value determined by the supplied function.
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') 

-- | A circuit that adds the information about the time points at which 
-- the values were received.
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

-- | Delay the input by one step using the specified initial value.
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)

-- | A circuit that returns the current modeling time.
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)

-- | Like '>>>' but processes only the represented events.
(>?>) :: Circuit a (Maybe b)
         -- ^ whether there is an event
         -> Circuit b c
         -- ^ process the event if it presents
         -> Circuit a (Maybe c)
         -- ^ the resulting circuit that processes only the represented events
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')

-- | Like '<<<' but processes only the represented events.
(<?<) :: Circuit b c
         -- ^ process the event if it presents
         -> Circuit a (Maybe b)
         -- ^ whether there is an event
         -> Circuit a (Maybe c)
         -- ^ the resulting circuit that processes only the represented events
<?< :: 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)
(>?>)

-- | Filter the circuit, calculating only those parts of the circuit that satisfy
-- the specified predicate.
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)

-- | Filter the circuit within the 'Event' computation, calculating only those parts
-- of the circuit that satisfy the specified predicate.
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)

-- | The source of events that never occur.
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)

-- | An approximation of the integral using Euler's method.
--
-- This function can be rather inaccurate as it depends on
-- the time points at wich the 'Circuit' computation is actuated.
-- Also Euler's method per se is not most accurate, although simple
-- enough for implementation.
--
-- Consider using the 'integ' function whenever possible.
-- That function can integrate with help of the Runge-Kutta method by
-- the specified integration time points that are passed in the simulation
-- specs to every 'Simulation', when running the model.
--
-- At the same time, the 'integCircuit' function has no mutable state
-- unlike the former. The latter consumes less memory but at the cost
-- of inaccuracy and relatively more slow simulation, had we requested
-- the integral in the same time points.
--
-- Regarding the recursive equations, the both functions allow defining them
-- but whithin different computations (either with help of the recursive
-- do-notation or the proc-notation).
integCircuit :: Double
                -- ^ the initial value
                -> Circuit Double Double
                -- ^ map the derivative to an integral
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)

-- | Like 'integCircuit' but allows either setting a new 'Left' integral value,
-- or using the 'Right' derivative when integrating by Euler's method.
integCircuitEither :: Double
                      -- ^ the initial value
                      -> Circuit (Either Double Double) Double
                      -- ^ map either a new 'Left' value or
                      -- the 'Right' derivative to an integral
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)

-- | A sum of differences starting from the specified initial value.
--
-- Consider using the more accurate 'diffsum' function whener possible as
-- it is calculated in every integration time point specified by specs
-- passed in to every 'Simulation', when running the model.
--
-- At the same time, the 'sumCircuit' function has no mutable state and
-- it consumes less memory than the former.
--
-- Regarding the recursive equations, the both functions allow defining them
-- but whithin different computations (either with help of the recursive
-- do-notation or the proc-notation).
sumCircuit :: Num a =>
              a
              -- ^ the initial value
              -> Circuit a a
              -- ^ map the difference to a sum
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)

-- | Like 'sumCircuit' but allows either setting a new 'Left' value for the sum, or updating it
-- by specifying the 'Right' difference.
sumCircuitEither :: Num a =>
                    a
                    -- ^ the initial value
                    -> Circuit (Either a a) a
                    -- ^ map either a new 'Left' value or
                    -- the 'Right' difference to a sum
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)

-- | Approximate the circuit as a transform of time varying function,
-- calculating the values in the integration time points and then
-- interpolating in all other time points. The resulting transform
-- computation is synchronized with the event queue.         
--
-- This procedure consumes memory as the underlying memoization allocates
-- an array to store the calculated values.
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

-- | Iterate the circuit in the specified time points.
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'

-- | Iterate the circuit in the specified time points returning a task
-- which completes after the final output of the circuit is received.
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

-- | Iterate the circuit in the integration time points.
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

-- | Iterate the circuit in the specified time points.
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 

-- | Iterate the circuit in the integration time points returning a task
-- which completes after the final output of the circuit is received.
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

-- | Iterate the circuit in the specified time points returning a task
-- which completes after the final output of the circuit is received.
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 

-- | Iterate the circuit in the specified time points, interrupting the iteration
-- immediately if 'Nothing' is returned within the 'Circuit' computation.
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'

-- | Iterate the circuit in the integration time points, interrupting the iteration
-- immediately if 'Nothing' is returned within the 'Circuit' computation.
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

-- | Iterate the circuit in the specified time points, interrupting the iteration
-- immediately if 'Nothing' is returned within the 'Circuit' computation.
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

-- | Iterate the circuit in the specified time points returning a task
-- that computes the final output of the circuit either after all points
-- are exhausted, or after the 'Left' result of type @b@ is received,
-- which interrupts the computation immediately.
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

-- | Iterate the circuit in the integration time points returning a task
-- that computes the final output of the circuit either after all points
-- are exhausted, or after the 'Left' result of type @b@ is received,
-- which interrupts the computation immediately.
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

-- | Iterate the circuit in the specified time points returning a task
-- that computes the final output of the circuit either after all points
-- are exhausted, or after the 'Left' result of type @b@ is received,
-- which interrupts the computation immediately.
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

-- | Show the debug messages with the current simulation time.
traceCircuit :: Maybe String
                -- ^ the request message
                -> Maybe String
                -- ^ the response message
                -> Circuit a b
                -- ^ a circuit
                -> 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')