{-# 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 = (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (a, Circuit a a)) -> Circuit a a)
-> (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> b) -> a -> b
$ \a
a -> (a, Circuit a a) -> Event (a, Circuit a a)
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, Circuit a a
forall a. Circuit 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
(.) = 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) =
        (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
        (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b))
-> (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
        do (a
b, Circuit a a
cir1) <- Point -> Event (a, Circuit a a) -> IO (a, Circuit a a)
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) <- Point -> Event (b, Circuit a b) -> IO (b, Circuit a b)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (a -> Event (b, Circuit a b)
g a
b)
           (b, Circuit a b) -> IO (b, Circuit a b)
forall a. a -> IO a
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 = (b -> Event (c, Circuit b c)) -> Circuit b c
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((b -> Event (c, Circuit b c)) -> Circuit b c)
-> (b -> Event (c, Circuit b c)) -> Circuit b c
forall a b. (a -> b) -> a -> b
$ \b
a -> (c, Circuit b c) -> Event (c, Circuit b c)
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> c
f b
a, (b -> c) -> Circuit b c
forall b c. (b -> c) -> Circuit b c
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) =
    ((b, d) -> Event ((c, d), Circuit (b, d) (c, d)))
-> Circuit (b, d) (c, d)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit (((b, d) -> Event ((c, d), Circuit (b, d) (c, d)))
 -> Circuit (b, d) (c, d))
-> ((b, d) -> Event ((c, d), Circuit (b, d) (c, d)))
-> Circuit (b, d) (c, d)
forall a b. (a -> b) -> a -> b
$ \(b
b, d
d) ->
    (Point -> IO ((c, d), Circuit (b, d) (c, d)))
-> Event ((c, d), Circuit (b, d) (c, d))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ((c, d), Circuit (b, d) (c, d)))
 -> Event ((c, d), Circuit (b, d) (c, d)))
-> (Point -> IO ((c, d), Circuit (b, d) (c, d)))
-> Event ((c, d), Circuit (b, d) (c, d))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
    do (c
c, Circuit b c
cir) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c, Circuit b c)
f b
b)
       ((c, d), Circuit (b, d) (c, d))
-> IO ((c, d), Circuit (b, d) (c, d))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, d
d), Circuit b c -> Circuit (b, d) (c, d)
forall b c d. Circuit b c -> Circuit (b, d) (c, 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) =
    ((d, b) -> Event ((d, c), Circuit (d, b) (d, c)))
-> Circuit (d, b) (d, c)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit (((d, b) -> Event ((d, c), Circuit (d, b) (d, c)))
 -> Circuit (d, b) (d, c))
-> ((d, b) -> Event ((d, c), Circuit (d, b) (d, c)))
-> Circuit (d, b) (d, c)
forall a b. (a -> b) -> a -> b
$ \(d
d, b
b) ->
    (Point -> IO ((d, c), Circuit (d, b) (d, c)))
-> Event ((d, c), Circuit (d, b) (d, c))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ((d, c), Circuit (d, b) (d, c)))
 -> Event ((d, c), Circuit (d, b) (d, c)))
-> (Point -> IO ((d, c), Circuit (d, b) (d, c)))
-> Event ((d, c), Circuit (d, b) (d, c))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
    do (c
c, Circuit b c
cir) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c, Circuit b c)
f b
b)
       ((d, c), Circuit (d, b) (d, c))
-> IO ((d, c), Circuit (d, b) (d, c))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((d
d, c
c), Circuit b c -> Circuit (d, b) (d, c)
forall b c d. Circuit b c -> Circuit (d, b) (d, 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) =
    ((b, b') -> Event ((c, c'), Circuit (b, b') (c, c')))
-> Circuit (b, b') (c, c')
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit (((b, b') -> Event ((c, c'), Circuit (b, b') (c, c')))
 -> Circuit (b, b') (c, c'))
-> ((b, b') -> Event ((c, c'), Circuit (b, b') (c, c')))
-> Circuit (b, b') (c, c')
forall a b. (a -> b) -> a -> b
$ \(b
b, b'
b') ->
    (Point -> IO ((c, c'), Circuit (b, b') (c, c')))
-> Event ((c, c'), Circuit (b, b') (c, c'))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ((c, c'), Circuit (b, b') (c, c')))
 -> Event ((c, c'), Circuit (b, b') (c, c')))
-> (Point -> IO ((c, c'), Circuit (b, b') (c, c')))
-> Event ((c, c'), Circuit (b, b') (c, c'))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
    do (c
c, Circuit b c
cir1) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
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) <- Point -> Event (c', Circuit b' c') -> IO (c', Circuit b' c')
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b' -> Event (c', Circuit b' c')
g b'
b')
       ((c, c'), Circuit (b, b') (c, c'))
-> IO ((c, c'), Circuit (b, b') (c, c'))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit b c
cir1 Circuit b c -> Circuit b' c' -> Circuit (b, b') (c, c')
forall b c b' c'.
Circuit b c -> Circuit b' c' -> Circuit (b, b') (c, c')
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) =
    (b -> Event ((c, c'), Circuit b (c, c'))) -> Circuit b (c, c')
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((b -> Event ((c, c'), Circuit b (c, c'))) -> Circuit b (c, c'))
-> (b -> Event ((c, c'), Circuit b (c, c'))) -> Circuit b (c, c')
forall a b. (a -> b) -> a -> b
$ \b
b ->
    (Point -> IO ((c, c'), Circuit b (c, c')))
-> Event ((c, c'), Circuit b (c, c'))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ((c, c'), Circuit b (c, c')))
 -> Event ((c, c'), Circuit b (c, c')))
-> (Point -> IO ((c, c'), Circuit b (c, c')))
-> Event ((c, c'), Circuit b (c, c'))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
    do (c
c, Circuit b c
cir1) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
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) <- Point -> Event (c', Circuit b c') -> IO (c', Circuit b c')
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c', Circuit b c')
g b
b)
       ((c, c'), Circuit b (c, c')) -> IO ((c, c'), Circuit b (c, c'))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
c, c'
c'), Circuit b c
cir1 Circuit b c -> Circuit b c' -> Circuit b (c, c')
forall b c c'. Circuit b c -> Circuit b c' -> Circuit b (c, c')
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) =
    (b -> Event (c, Circuit b c)) -> Circuit b c
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((b -> Event (c, Circuit b c)) -> Circuit b c)
-> (b -> Event (c, Circuit b c)) -> Circuit b c
forall a b. (a -> b) -> a -> b
$ \b
b ->
    (Point -> IO (c, Circuit b c)) -> Event (c, Circuit b c)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (c, Circuit b c)) -> Event (c, Circuit b c))
-> (Point -> IO (c, Circuit b c)) -> Event (c, Circuit b c)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
    do rec ((c
c, d
d), Circuit (b, d) (c, d)
cir) <- Point
-> Event ((c, d), Circuit (b, d) (c, d))
-> IO ((c, d), Circuit (b, d) (c, d))
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))
       (c, Circuit b c) -> IO (c, Circuit b c)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c
c, Circuit (b, d) (c, d) -> Circuit b c
forall b d c. Circuit (b, d) (c, d) -> Circuit b 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) =
    (Either b d
 -> Event (Either c d, Circuit (Either b d) (Either c d)))
-> Circuit (Either b d) (Either c d)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either b d
  -> Event (Either c d, Circuit (Either b d) (Either c d)))
 -> Circuit (Either b d) (Either c d))
-> (Either b d
    -> Event (Either c d, Circuit (Either b d) (Either c d)))
-> Circuit (Either b d) (Either c d)
forall a b. (a -> b) -> a -> b
$ \Either b d
ebd ->
    (Point -> IO (Either c d, Circuit (Either b d) (Either c d)))
-> Event (Either c d, Circuit (Either b d) (Either c d))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Either c d, Circuit (Either b d) (Either c d)))
 -> Event (Either c d, Circuit (Either b d) (Either c d)))
-> (Point -> IO (Either c d, Circuit (Either b d) (Either c d)))
-> Event (Either c d, Circuit (Either b d) (Either c d))
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) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c, Circuit b c)
f b
b)
           (Either c d, Circuit (Either b d) (Either c d))
-> IO (Either c d, Circuit (Either b d) (Either c d))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c d
forall a b. a -> Either a b
Left c
c, Circuit b c -> Circuit (Either b d) (Either c d)
forall b c d. Circuit b c -> Circuit (Either b d) (Either c d)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left Circuit b c
cir)
      Right d
d ->
        (Either c d, Circuit (Either b d) (Either c d))
-> IO (Either c d, Circuit (Either b d) (Either c d))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> Either c d
forall a b. b -> Either a b
Right d
d, Circuit b c -> Circuit (Either b d) (Either c d)
forall b c d. Circuit b c -> Circuit (Either b d) (Either c d)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left Circuit 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) =
    (Either d b
 -> Event (Either d c, Circuit (Either d b) (Either d c)))
-> Circuit (Either d b) (Either d c)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either d b
  -> Event (Either d c, Circuit (Either d b) (Either d c)))
 -> Circuit (Either d b) (Either d c))
-> (Either d b
    -> Event (Either d c, Circuit (Either d b) (Either d c)))
-> Circuit (Either d b) (Either d c)
forall a b. (a -> b) -> a -> b
$ \Either d b
edb ->
    (Point -> IO (Either d c, Circuit (Either d b) (Either d c)))
-> Event (Either d c, Circuit (Either d b) (Either d c))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Either d c, Circuit (Either d b) (Either d c)))
 -> Event (Either d c, Circuit (Either d b) (Either d c)))
-> (Point -> IO (Either d c, Circuit (Either d b) (Either d c)))
-> Event (Either d c, Circuit (Either d b) (Either d c))
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) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c, Circuit b c)
f b
b)
           (Either d c, Circuit (Either d b) (Either d c))
-> IO (Either d c, Circuit (Either d b) (Either d c))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either d c
forall a b. b -> Either a b
Right c
c, Circuit b c -> Circuit (Either d b) (Either d c)
forall b c d. Circuit b c -> Circuit (Either d b) (Either d c)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right Circuit b c
cir)
      Left d
d ->
        (Either d c, Circuit (Either d b) (Either d c))
-> IO (Either d c, Circuit (Either d b) (Either d c))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> Either d c
forall a b. a -> Either a b
Left d
d, Circuit b c -> Circuit (Either d b) (Either d c)
forall b c d. Circuit b c -> Circuit (Either d b) (Either d c)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right Circuit 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) =
    (Either b b'
 -> Event (Either c c', Circuit (Either b b') (Either c c')))
-> Circuit (Either b b') (Either c c')
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either b b'
  -> Event (Either c c', Circuit (Either b b') (Either c c')))
 -> Circuit (Either b b') (Either c c'))
-> (Either b b'
    -> Event (Either c c', Circuit (Either b b') (Either c c')))
-> Circuit (Either b b') (Either c c')
forall a b. (a -> b) -> a -> b
$ \Either b b'
ebb' ->
    (Point -> IO (Either c c', Circuit (Either b b') (Either c c')))
-> Event (Either c c', Circuit (Either b b') (Either c c'))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Either c c', Circuit (Either b b') (Either c c')))
 -> Event (Either c c', Circuit (Either b b') (Either c c')))
-> (Point -> IO (Either c c', Circuit (Either b b') (Either c c')))
-> Event (Either c c', Circuit (Either b b') (Either c c'))
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) <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (c, Circuit b c)
f b
b)
           (Either c c', Circuit (Either b b') (Either c c'))
-> IO (Either c c', Circuit (Either b b') (Either c c'))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c c'
forall a b. a -> Either a b
Left c
c, Circuit b c
cir1 Circuit b c -> Circuit b' c' -> Circuit (Either b b') (Either c c')
forall b c b' c'.
Circuit b c -> Circuit b' c' -> Circuit (Either b b') (Either c c')
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ Circuit b' c'
y)
      Right b'
b' ->
        do (c'
c', Circuit b' c'
cir2) <- Point -> Event (c', Circuit b' c') -> IO (c', Circuit b' c')
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b' -> Event (c', Circuit b' c')
g b'
b')
           (Either c c', Circuit (Either b b') (Either c c'))
-> IO (Either c c', Circuit (Either b b') (Either c c'))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c' -> Either c c'
forall a b. b -> Either a b
Right c'
c', Circuit b c
x Circuit b c -> Circuit b' c' -> Circuit (Either b b') (Either c c')
forall b c b' c'.
Circuit b c -> Circuit b' c' -> Circuit (Either b b') (Either c c')
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ Circuit 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) =
    (Either b c -> Event (d, Circuit (Either b c) d))
-> Circuit (Either b c) d
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either b c -> Event (d, Circuit (Either b c) d))
 -> Circuit (Either b c) d)
-> (Either b c -> Event (d, Circuit (Either b c) d))
-> Circuit (Either b c) d
forall a b. (a -> b) -> a -> b
$ \Either b c
ebc ->
    (Point -> IO (d, Circuit (Either b c) d))
-> Event (d, Circuit (Either b c) d)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (d, Circuit (Either b c) d))
 -> Event (d, Circuit (Either b c) d))
-> (Point -> IO (d, Circuit (Either b c) d))
-> Event (d, Circuit (Either b c) d)
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) <- Point -> Event (d, Circuit b d) -> IO (d, Circuit b d)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (b -> Event (d, Circuit b d)
f b
b)
           (d, Circuit (Either b c) d) -> IO (d, Circuit (Either b c) d)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit b d
cir1 Circuit b d -> Circuit c d -> Circuit (Either b c) d
forall b d c. Circuit b d -> Circuit c d -> Circuit (Either b c) d
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) <- Point -> Event (d, Circuit c d) -> IO (d, Circuit c d)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (c -> Event (d, Circuit c d)
g c
b')
           (d, Circuit (Either b c) d) -> IO (d, Circuit (Either b c) d)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d, Circuit b d
x Circuit b d -> Circuit c d -> Circuit (Either b c) d
forall b d c. Circuit b d -> Circuit c d -> Circuit (Either b c) d
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 ->
            (Point -> IO DisposableEvent) -> Event DisposableEvent
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO DisposableEvent) -> Event DisposableEvent)
-> (Point -> IO DisposableEvent) -> Event DisposableEvent
forall a b. (a -> b) -> a -> b
$ \Point
p ->
            do IORef (a -> Event (b, Circuit a b))
r <- (a -> Event (b, Circuit a b))
-> IO (IORef (a -> Event (b, Circuit a b)))
forall a. a -> IO (IORef a)
newIORef a -> Event (b, Circuit a b)
cir
               Point -> Event DisposableEvent -> IO DisposableEvent
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event DisposableEvent -> IO DisposableEvent)
-> Event DisposableEvent -> IO DisposableEvent
forall a b. (a -> b) -> a -> b
$
                 Signal a -> (a -> Event ()) -> Event DisposableEvent
forall a. Signal a -> (a -> Event ()) -> Event DisposableEvent
handleSignal Signal a
sa ((a -> Event ()) -> Event DisposableEvent)
-> (a -> Event ()) -> Event DisposableEvent
forall a b. (a -> b) -> a -> b
$ \a
a ->
                 (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
                 do a -> Event (b, Circuit a b)
cir <- IORef (a -> Event (b, Circuit a b))
-> IO (a -> Event (b, Circuit a b))
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') <- Point -> Event (b, Circuit a b) -> IO (b, Circuit a b)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (a -> Event (b, Circuit a b)
cir a
a)
                    IORef (a -> Event (b, Circuit a b))
-> (a -> Event (b, Circuit a b)) -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (a -> Event (b, Circuit a b))
r a -> Event (b, Circuit a b)
cir'
                    Point -> Event () -> IO ()
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) = (Stream a -> Stream b) -> Processor a b
forall a b. (Stream a -> Stream b) -> Processor a b
Processor ((Stream a -> Stream b) -> Processor a b)
-> (Stream a -> Stream b) -> Processor a b
forall a b. (a -> b) -> a -> b
$ \Stream a
sa ->
  Process (b, Stream b) -> Stream b
forall a. Process (a, Stream a) -> Stream a
Cons (Process (b, Stream b) -> Stream b)
-> Process (b, Stream b) -> Stream b
forall a b. (a -> b) -> a -> b
$
  do (a
a, Stream a
xs) <- Stream a -> Process (a, Stream a)
forall a. Stream a -> Process (a, Stream a)
runStream Stream a
sa
     (b
b, Circuit a b
cir') <- Event (b, Circuit a b) -> Process (b, Circuit a b)
forall a. Event a -> Process a
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 = Processor a b -> Stream a -> Stream b
forall a b. Processor a b -> Stream a -> Stream b
runProcessor (Circuit a b -> Processor a b
forall a b. Circuit a b -> Processor a b
circuitProcessor Circuit a b
cir')
     (b, Stream b) -> Process (b, Stream b)
forall a. a -> Process a
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 =
        (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
        (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b))
-> (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
        do b
b <- Point -> Event b -> IO b
forall a. Point -> Event a -> IO a
invokeEvent Point
p (a -> Event b
f a
a)
           (b, Circuit a b) -> IO (b, Circuit a b)
forall a. a -> IO 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 =
  (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ \a
a ->
  (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b))
-> (Point -> IO (b, Circuit a b)) -> Event (b, Circuit a b)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do (acc
acc', b
b) <- Point -> Event (acc, b) -> IO (acc, b)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (acc -> a -> Event (acc, b)
f acc
acc a
a)
     (b, Circuit a b) -> IO (b, Circuit a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (acc -> a -> Event (acc, b)) -> acc -> Circuit a 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 =
        (a -> Event (Arrival a, Circuit a (Arrival a)))
-> Circuit a (Arrival a)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (Arrival a, Circuit a (Arrival a)))
 -> Circuit a (Arrival a))
-> (a -> Event (Arrival a, Circuit a (Arrival a)))
-> Circuit a (Arrival a)
forall a b. (a -> b) -> a -> b
$ \a
a ->
        (Point -> IO (Arrival a, Circuit a (Arrival a)))
-> Event (Arrival a, Circuit a (Arrival a))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Arrival a, Circuit a (Arrival a)))
 -> Event (Arrival a, Circuit a (Arrival a)))
-> (Point -> IO (Arrival a, Circuit a (Arrival a)))
-> Event (Arrival a, Circuit a (Arrival a))
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 -> Maybe Double
forall a. Maybe a
Nothing
                              Just Double
t0 -> Double -> Maybe Double
forall a. a -> Maybe a
Just (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0) }
        in (Arrival a, Circuit a (Arrival a))
-> IO (Arrival a, Circuit a (Arrival a))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Arrival a
b, Maybe Double -> Circuit a (Arrival a)
loop (Maybe Double -> Circuit a (Arrival a))
-> Maybe Double -> Circuit a (Arrival a)
forall a b. (a -> b) -> a -> b
$ Double -> Maybe Double
forall a. a -> Maybe a
Just Double
t)
  in Maybe Double -> Circuit a (Arrival a)
forall {a}. Maybe Double -> Circuit a (Arrival a)
loop Maybe Double
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 =
  (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (a, Circuit a a)) -> Circuit a a)
-> (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> b) -> a -> b
$ \a
a ->
  (a, Circuit a a) -> Event (a, Circuit a a)
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a0, a -> Circuit a a
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 =
  (a -> Event (Double, Circuit a Double)) -> Circuit a Double
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (Double, Circuit a Double)) -> Circuit a Double)
-> (a -> Event (Double, Circuit a Double)) -> Circuit a Double
forall a b. (a -> b) -> a -> b
$ \a
a ->
  (Point -> IO (Double, Circuit a Double))
-> Event (Double, Circuit a Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Double, Circuit a Double))
 -> Event (Double, Circuit a Double))
-> (Point -> IO (Double, Circuit a Double))
-> Event (Double, Circuit a Double)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  (Double, Circuit a Double) -> IO (Double, Circuit a Double)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Point -> Double
pointTime Point
p, Circuit a Double
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 =
  (a -> Event (Maybe c, Circuit a (Maybe c))) -> Circuit a (Maybe c)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (Maybe c, Circuit a (Maybe c)))
 -> Circuit a (Maybe c))
-> (a -> Event (Maybe c, Circuit a (Maybe c)))
-> Circuit a (Maybe c)
forall a b. (a -> b) -> a -> b
$ \a
a ->
  (Point -> IO (Maybe c, Circuit a (Maybe c)))
-> Event (Maybe c, Circuit a (Maybe c))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Maybe c, Circuit a (Maybe c)))
 -> Event (Maybe c, Circuit a (Maybe c)))
-> (Point -> IO (Maybe c, Circuit a (Maybe c)))
-> Event (Maybe c, Circuit a (Maybe c))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do (Maybe b
b, Circuit a (Maybe b)
whether') <- Point
-> Event (Maybe b, Circuit a (Maybe b))
-> IO (Maybe b, Circuit a (Maybe b))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Circuit a (Maybe b) -> a -> Event (Maybe b, Circuit a (Maybe b))
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 ->
         (Maybe c, Circuit a (Maybe c)) -> IO (Maybe c, Circuit a (Maybe c))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe c
forall a. Maybe a
Nothing, Circuit a (Maybe b)
whether' Circuit a (Maybe b) -> Circuit b c -> Circuit a (Maybe c)
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') <- Point -> Event (c, Circuit b c) -> IO (c, Circuit b c)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Circuit b c -> b -> Event (c, Circuit b c)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit b c
process b
b)
            (Maybe c, Circuit a (Maybe c)) -> IO (Maybe c, Circuit a (Maybe c))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Maybe c
forall a. a -> Maybe a
Just c
c, Circuit a (Maybe b)
whether' Circuit a (Maybe b) -> Circuit b c -> Circuit a (Maybe c)
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)
(<?<) = (Circuit a (Maybe b) -> Circuit b c -> Circuit a (Maybe c))
-> Circuit b c -> Circuit a (Maybe b) -> Circuit a (Maybe c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Circuit a (Maybe b) -> Circuit b c -> Circuit a (Maybe c)
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 = (a -> Event Bool) -> Circuit a b -> Circuit a (Maybe b)
forall a b. (a -> Event Bool) -> Circuit a b -> Circuit a (Maybe b)
filterCircuitM (Bool -> Event Bool
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Event Bool) -> (a -> Bool) -> a -> Event Bool
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 =
  (a -> Event (Maybe b, Circuit a (Maybe b))) -> Circuit a (Maybe b)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (Maybe b, Circuit a (Maybe b)))
 -> Circuit a (Maybe b))
-> (a -> Event (Maybe b, Circuit a (Maybe b)))
-> Circuit a (Maybe b)
forall a b. (a -> b) -> a -> b
$ \a
a ->
  (Point -> IO (Maybe b, Circuit a (Maybe b)))
-> Event (Maybe b, Circuit a (Maybe b))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Maybe b, Circuit a (Maybe b)))
 -> Event (Maybe b, Circuit a (Maybe b)))
-> (Point -> IO (Maybe b, Circuit a (Maybe b)))
-> Event (Maybe b, Circuit a (Maybe b))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Bool
x <- Point -> Event Bool -> IO Bool
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') <- Point -> Event (b, Circuit a b) -> IO (b, Circuit a b)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Circuit a b -> a -> Event (b, Circuit a b)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit a b
cir a
a)
               (Maybe b, Circuit a (Maybe b)) -> IO (Maybe b, Circuit a (Maybe b))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe b
forall a. a -> Maybe a
Just b
b, (a -> Event Bool) -> Circuit a b -> Circuit a (Maybe b)
forall a b. (a -> Event Bool) -> Circuit a b -> Circuit a (Maybe b)
filterCircuitM a -> Event Bool
pred Circuit a b
cir')
       else (Maybe b, Circuit a (Maybe b)) -> IO (Maybe b, Circuit a (Maybe b))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b
forall a. Maybe a
Nothing, (a -> Event Bool) -> Circuit a b -> Circuit a (Maybe b)
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 =
  (a -> Event (Maybe b, Circuit a (Maybe b))) -> Circuit a (Maybe b)
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (Maybe b, Circuit a (Maybe b)))
 -> Circuit a (Maybe b))
-> (a -> Event (Maybe b, Circuit a (Maybe b)))
-> Circuit a (Maybe b)
forall a b. (a -> b) -> a -> b
$ \a
a -> (Maybe b, Circuit a (Maybe b))
-> Event (Maybe b, Circuit a (Maybe b))
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b
forall a. Maybe a
Nothing, Circuit a (Maybe b)
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 = 
      (Double -> Event (Double, Circuit Double Double))
-> Circuit Double Double
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Double -> Event (Double, Circuit Double Double))
 -> Circuit Double Double)
-> (Double -> Event (Double, Circuit Double Double))
-> Circuit Double Double
forall a b. (a -> b) -> a -> b
$ \Double
a ->
      (Point -> IO (Double, Circuit Double Double))
-> Event (Double, Circuit Double Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Double, Circuit Double Double))
 -> Event (Double, Circuit Double Double))
-> (Point -> IO (Double, Circuit Double Double))
-> Event (Double, Circuit Double Double)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      do let t :: Double
t = Point -> Double
pointTime Point
p
         (Double, Circuit Double Double)
-> IO (Double, Circuit Double Double)
forall a. a -> IO a
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 =
      (Double -> Event (Double, Circuit Double Double))
-> Circuit Double Double
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Double -> Event (Double, Circuit Double Double))
 -> Circuit Double Double)
-> (Double -> Event (Double, Circuit Double Double))
-> Circuit Double Double
forall a b. (a -> b) -> a -> b
$ \Double
a ->
      (Point -> IO (Double, Circuit Double Double))
-> Event (Double, Circuit Double Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Double, Circuit Double Double))
 -> Event (Double, Circuit Double Double))
-> (Point -> IO (Double, Circuit Double Double))
-> Event (Double, Circuit Double Double)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      do let t :: Double
t  = Point -> Double
pointTime Point
p
             dt :: Double
dt = Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
             v :: Double
v  = Double
v0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
a0 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dt
         Double
v Double
-> IO (Double, Circuit Double Double)
-> IO (Double, Circuit Double Double)
forall a b. a -> b -> b
`seq` (Double, Circuit Double Double)
-> IO (Double, Circuit Double Double)
forall a. a -> IO a
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 = 
      (Either Double Double
 -> Event (Double, Circuit (Either Double Double) Double))
-> Circuit (Either Double Double) Double
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either Double Double
  -> Event (Double, Circuit (Either Double Double) Double))
 -> Circuit (Either Double Double) Double)
-> (Either Double Double
    -> Event (Double, Circuit (Either Double Double) Double))
-> Circuit (Either Double Double) Double
forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
      (Point -> IO (Double, Circuit (Either Double Double) Double))
-> Event (Double, Circuit (Either Double Double) Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Double, Circuit (Either Double Double) Double))
 -> Event (Double, Circuit (Either Double Double) Double))
-> (Point -> IO (Double, Circuit (Either Double Double) Double))
-> Event (Double, Circuit (Either Double Double) Double)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      do let t :: Double
t = Point -> Double
pointTime Point
p
         (Double, Circuit (Either Double Double) Double)
-> IO (Double, Circuit (Either Double Double) Double)
forall a. a -> IO a
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 =
      (Either Double Double
 -> Event (Double, Circuit (Either Double Double) Double))
-> Circuit (Either Double Double) Double
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either Double Double
  -> Event (Double, Circuit (Either Double Double) Double))
 -> Circuit (Either Double Double) Double)
-> (Either Double Double
    -> Event (Double, Circuit (Either Double Double) Double))
-> Circuit (Either Double Double) Double
forall a b. (a -> b) -> a -> b
$ \Either Double Double
a ->
      (Point -> IO (Double, Circuit (Either Double Double) Double))
-> Event (Double, Circuit (Either Double Double) Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Double, Circuit (Either Double Double) Double))
 -> Event (Double, Circuit (Either Double Double) Double))
-> (Point -> IO (Double, Circuit (Either Double Double) Double))
-> Event (Double, Circuit (Either Double Double) Double)
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 Double
-> IO (Double, Circuit (Either Double Double) Double)
-> IO (Double, Circuit (Either Double Double) Double)
forall a b. a -> b -> b
`seq` (Double, Circuit (Either Double Double) Double)
-> IO (Double, Circuit (Either Double Double) Double)
forall a. a -> IO a
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 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
                 v :: Double
v  = Double
v0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
a0 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dt
             Double
v Double
-> IO (Double, Circuit (Either Double Double) Double)
-> IO (Double, Circuit (Either Double Double) Double)
forall a b. a -> b -> b
`seq` (Double, Circuit (Either Double Double) Double)
-> IO (Double, Circuit (Either Double Double) Double)
forall a. a -> IO a
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 = 
      (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (a, Circuit a a)) -> Circuit a a)
-> (a -> Event (a, Circuit a a)) -> Circuit a a
forall a b. (a -> b) -> a -> b
$ \a
a ->
      (Point -> IO (a, Circuit a a)) -> Event (a, Circuit a a)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (a, Circuit a a)) -> Event (a, Circuit a a))
-> (Point -> IO (a, Circuit a a)) -> Event (a, Circuit a a)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      (a, Circuit a a) -> IO (a, Circuit a a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, a -> a -> Circuit a a
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 =
      (t -> Event (t, Circuit t t)) -> Circuit t t
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((t -> Event (t, Circuit t t)) -> Circuit t t)
-> (t -> Event (t, Circuit t t)) -> Circuit t t
forall a b. (a -> b) -> a -> b
$ \t
a ->
      (Point -> IO (t, Circuit t t)) -> Event (t, Circuit t t)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (t, Circuit t t)) -> Event (t, Circuit t t))
-> (Point -> IO (t, Circuit t t)) -> Event (t, Circuit t t)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      do let v :: t
v = t
v0 t -> t -> t
forall a. Num a => a -> a -> a
+ t
a0
         t
v t -> IO (t, Circuit t t) -> IO (t, Circuit t t)
forall a b. a -> b -> b
`seq` (t, Circuit t t) -> IO (t, Circuit t t)
forall a. a -> IO a
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 = 
      (Either a a -> Event (a, Circuit (Either a a) a))
-> Circuit (Either a a) a
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either a a -> Event (a, Circuit (Either a a) a))
 -> Circuit (Either a a) a)
-> (Either a a -> Event (a, Circuit (Either a a) a))
-> Circuit (Either a a) a
forall a b. (a -> b) -> a -> b
$ \Either a a
a ->
      (Point -> IO (a, Circuit (Either a a) a))
-> Event (a, Circuit (Either a a) a)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (a, Circuit (Either a a) a))
 -> Event (a, Circuit (Either a a) a))
-> (Point -> IO (a, Circuit (Either a a) a))
-> Event (a, Circuit (Either a a) a)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      (a, Circuit (Either a a) a) -> IO (a, Circuit (Either a a) a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
init, a -> Either a a -> Circuit (Either a a) a
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 =
      (Either t t -> Event (t, Circuit (Either t t) t))
-> Circuit (Either t t) t
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((Either t t -> Event (t, Circuit (Either t t) t))
 -> Circuit (Either t t) t)
-> (Either t t -> Event (t, Circuit (Either t t) t))
-> Circuit (Either t t) t
forall a b. (a -> b) -> a -> b
$ \Either t t
a ->
      (Point -> IO (t, Circuit (Either t t) t))
-> Event (t, Circuit (Either t t) t)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (t, Circuit (Either t t) t))
 -> Event (t, Circuit (Either t t) t))
-> (Point -> IO (t, Circuit (Either t t) t))
-> Event (t, Circuit (Either t t) t)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      case Either t t
a0 of
        Left t
v ->
          t
v t
-> IO (t, Circuit (Either t t) t) -> IO (t, Circuit (Either t t) t)
forall a b. a -> b -> b
`seq` (t, Circuit (Either t t) t) -> IO (t, Circuit (Either t t) t)
forall a. a -> IO a
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 t -> t -> t
forall a. Num a => a -> a -> a
+ t
a0
          t
v t
-> IO (t, Circuit (Either t t) t) -> IO (t, Circuit (Either t t) t)
forall a b. a -> b -> b
`seq` (t, Circuit (Either t t) t) -> IO (t, Circuit (Either t t) t)
forall a. a -> IO a
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 = (Dynamics a -> Simulation (Dynamics b)) -> Transform a b
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 =
      (Run -> IO (Dynamics b)) -> Simulation (Dynamics b)
forall a. (Run -> IO a) -> Simulation a
Simulation ((Run -> IO (Dynamics b)) -> Simulation (Dynamics b))
-> (Run -> IO (Dynamics b)) -> Simulation (Dynamics b)
forall a b. (a -> b) -> a -> b
$ \Run
r ->
      do IORef (Circuit a b)
ref <- Circuit a b -> IO (IORef (Circuit a b))
forall a. a -> IO (IORef a)
newIORef Circuit a b
cir
         Run -> Simulation (Dynamics b) -> IO (Dynamics b)
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r (Simulation (Dynamics b) -> IO (Dynamics b))
-> Simulation (Dynamics b) -> IO (Dynamics b)
forall a b. (a -> b) -> a -> b
$
           Dynamics b -> Simulation (Dynamics b)
forall e. Dynamics e -> Simulation (Dynamics e)
memo0Dynamics (IORef (Circuit a b) -> Dynamics a -> Dynamics b
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 =
      (Point -> IO a) -> Dynamics a
forall a. (Point -> IO a) -> Dynamics a
Dynamics ((Point -> IO a) -> Dynamics a) -> (Point -> IO a) -> Dynamics a
forall a b. (a -> b) -> a -> b
$ \Point
p ->
      do a
a <- Point -> Dynamics a -> IO a
forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p Dynamics a
m
         Circuit a a
cir <- IORef (Circuit a a) -> IO (Circuit a a)
forall a. IORef a -> IO a
readIORef IORef (Circuit a a)
ref
         (a
b, Circuit a a
cir') <-
           Point -> Dynamics (a, Circuit a a) -> IO (a, Circuit a a)
forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p (Dynamics (a, Circuit a a) -> IO (a, Circuit a a))
-> Dynamics (a, Circuit a a) -> IO (a, Circuit a a)
forall a b. (a -> b) -> a -> b
$
           Event (a, Circuit a a) -> Dynamics (a, Circuit a a)
forall a. Event a -> Dynamics a
runEvent (Circuit a a -> a -> Event (a, Circuit a a)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit a a
cir a
a)
         IORef (Circuit a a) -> Circuit a a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Circuit a a)
ref Circuit a a
cir'
         a -> IO a
forall a. a -> IO a
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 = () -> Event ()
forall a. a -> Event 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) (Event () -> Event ()) -> Event () -> Event ()
forall a b. (a -> b) -> a -> b
$
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p' ->
  do (a
a', Circuit a a
cir') <- Point -> Event (a, Circuit a a) -> IO (a, Circuit a a)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (a, Circuit a a) -> IO (a, Circuit a a))
-> Event (a, Circuit a a) -> IO (a, Circuit a a)
forall a b. (a -> b) -> a -> b
$ Circuit a a -> a -> Event (a, Circuit a a)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit a a
cir a
a
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Point] -> Circuit a a -> a -> Event ()
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 = SignalSource t -> t -> Event ()
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) (Event () -> Event ()) -> Event () -> Event ()
forall a b. (a -> b) -> a -> b
$
           (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p' ->
           do (t
a', Circuit t t
cir') <- Point -> Event (t, Circuit t t) -> IO (t, Circuit t t)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (t, Circuit t t) -> IO (t, Circuit t t))
-> Event (t, Circuit t t) -> IO (t, Circuit t t)
forall a b. (a -> b) -> a -> b
$ Circuit t t -> t -> Event (t, Circuit t t)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit t t
cir t
a
              Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
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 <- Simulation (SignalSource a) -> Event (SignalSource a)
forall a. Simulation a -> Event a
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation Simulation (SignalSource a)
forall a. Simulation (SignalSource a)
newSignalSource
     Task a
task <- Process a -> Event (Task a)
forall a. Process a -> Event (Task a)
runTask (Process a -> Event (Task a)) -> Process a -> Event (Task a)
forall a b. (a -> b) -> a -> b
$ Signal a -> Process a
forall a. Signal a -> Process a
processAwait (Signal a -> Process a) -> Signal a -> Process a
forall a b. (a -> b) -> a -> b
$ SignalSource a -> Signal a
forall a. SignalSource a -> Signal a
publishSignal SignalSource a
source
     [Point] -> Circuit a a -> a -> SignalSource a -> Event ()
forall {t}.
[Point] -> Circuit t t -> t -> SignalSource t -> Event ()
loop [Point]
ps Circuit a a
cir a
a SignalSource a
source
     Task a -> Event (Task a)
forall a. a -> Event a
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 =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = Point -> [Point]
integPointsStartingFrom Point
p
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a a -> a -> Event ()
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 =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = (Double -> Point) -> [Double] -> [Point]
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
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a a -> a -> Event ()
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 =
  (Point -> IO (Task a)) -> Event (Task a)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Task a)) -> Event (Task a))
-> (Point -> IO (Task a)) -> Event (Task a)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = Point -> [Point]
integPointsStartingFrom Point
p
     Point -> Event (Task a) -> IO (Task a)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Task a) -> IO (Task a)) -> Event (Task a) -> IO (Task a)
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a a -> a -> Event (Task a)
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 =
  (Point -> IO (Task a)) -> Event (Task a)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Task a)) -> Event (Task a))
-> (Point -> IO (Task a)) -> Event (Task a)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = (Double -> Point) -> [Double] -> [Point]
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
     Point -> Event (Task a) -> IO (Task a)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Task a) -> IO (Task a)) -> Event (Task a) -> IO (Task a)
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a a -> a -> Event (Task a)
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 = () -> Event ()
forall a. a -> Event 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) (Event () -> Event ()) -> Event () -> Event ()
forall a b. (a -> b) -> a -> b
$
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p' ->
  do (Maybe a
a', Circuit a (Maybe a)
cir') <- Point
-> Event (Maybe a, Circuit a (Maybe a))
-> IO (Maybe a, Circuit a (Maybe a))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Maybe a, Circuit a (Maybe a))
 -> IO (Maybe a, Circuit a (Maybe a)))
-> Event (Maybe a, Circuit a (Maybe a))
-> IO (Maybe a, Circuit a (Maybe a))
forall a b. (a -> b) -> a -> b
$ Circuit a (Maybe a) -> a -> Event (Maybe a, Circuit a (Maybe a))
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 -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
       Just a
a' ->
         Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Point] -> Circuit a (Maybe a) -> a -> Event ()
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 =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = Point -> [Point]
integPointsStartingFrom Point
p
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a (Maybe a) -> a -> Event ()
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 =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = (Double -> Point) -> [Double] -> [Point]
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
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a (Maybe a) -> a -> Event ()
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 = SignalSource (Either a a) -> Either a a -> Event ()
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 = SignalSource (Either a a) -> Either a a -> Event ()
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) (Event () -> Event ()) -> Event () -> Event ()
forall a b. (a -> b) -> a -> b
$
           (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p' ->
           do (Either a a
ba', Circuit a (Either a a)
cir') <- Point
-> Event (Either a a, Circuit a (Either a a))
-> IO (Either a a, Circuit a (Either a a))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Either a a, Circuit a (Either a a))
 -> IO (Either a a, Circuit a (Either a a)))
-> Event (Either a a, Circuit a (Either a a))
-> IO (Either a a, Circuit a (Either a a))
forall a b. (a -> b) -> a -> b
$ Circuit a (Either a a)
-> a -> Event (Either a a, Circuit a (Either a a))
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit a (Either a a)
cir a
a
              Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
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 <- Simulation (SignalSource (Either b a))
-> Event (SignalSource (Either b a))
forall a. Simulation a -> Event a
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation Simulation (SignalSource (Either b a))
forall a. Simulation (SignalSource a)
newSignalSource
     Task (Either b a)
task <- Process (Either b a) -> Event (Task (Either b a))
forall a. Process a -> Event (Task a)
runTask (Process (Either b a) -> Event (Task (Either b a)))
-> Process (Either b a) -> Event (Task (Either b a))
forall a b. (a -> b) -> a -> b
$ Signal (Either b a) -> Process (Either b a)
forall a. Signal a -> Process a
processAwait (Signal (Either b a) -> Process (Either b a))
-> Signal (Either b a) -> Process (Either b a)
forall a b. (a -> b) -> a -> b
$ SignalSource (Either b a) -> Signal (Either b a)
forall a. SignalSource a -> Signal a
publishSignal SignalSource (Either b a)
source
     [Point]
-> Circuit a (Either b a)
-> Either b a
-> SignalSource (Either b a)
-> Event ()
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 (a -> Either b a
forall a b. b -> Either a b
Right a
a) SignalSource (Either b a)
source
     Task (Either b a) -> Event (Task (Either b a))
forall a. a -> Event a
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 =
  (Point -> IO (Task (Either b a))) -> Event (Task (Either b a))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Task (Either b a))) -> Event (Task (Either b a)))
-> (Point -> IO (Task (Either b a))) -> Event (Task (Either b a))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = Point -> [Point]
integPointsStartingFrom Point
p
     Point -> Event (Task (Either b a)) -> IO (Task (Either b a))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Task (Either b a)) -> IO (Task (Either b a)))
-> Event (Task (Either b a)) -> IO (Task (Either b a))
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a (Either b a) -> a -> Event (Task (Either b a))
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 =
  (Point -> IO (Task (Either b a))) -> Event (Task (Either b a))
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Task (Either b a))) -> Event (Task (Either b a)))
-> (Point -> IO (Task (Either b a))) -> Event (Task (Either b a))
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let ps :: [Point]
ps = (Double -> Point) -> [Double] -> [Point]
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
     Point -> Event (Task (Either b a)) -> IO (Task (Either b a))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Task (Either b a)) -> IO (Task (Either b a)))
-> Event (Task (Either b a)) -> IO (Task (Either b a))
forall a b. (a -> b) -> a -> b
$ 
       [Point] -> Circuit a (Either b a) -> a -> Event (Task (Either b a))
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 = (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ Circuit a b -> a -> Event (b, Circuit 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 -> Circuit a b -> a -> Event (b, Circuit a b)
forall a b. Circuit a b -> a -> Event (b, Circuit a b)
runCircuit Circuit a b
cir a
a
           Just String
message -> 
             String -> Event (b, Circuit a b) -> Event (b, Circuit a b)
forall a. String -> Event a -> Event a
traceEvent String
message (Event (b, Circuit a b) -> Event (b, Circuit a b))
-> Event (b, Circuit a b) -> Event (b, Circuit a b)
forall a b. (a -> b) -> a -> b
$
             Circuit a b -> a -> Event (b, Circuit 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 -> (b, Circuit a b) -> Event (b, Circuit a b)
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ Circuit a b -> a -> Event (b, Circuit a b)
loop Circuit a b
cir')
         Just String
message ->
           String -> Event (b, Circuit a b) -> Event (b, Circuit a b)
forall a. String -> Event a -> Event a
traceEvent String
message (Event (b, Circuit a b) -> Event (b, Circuit a b))
-> Event (b, Circuit a b) -> Event (b, Circuit a b)
forall a b. (a -> b) -> a -> b
$
           (b, Circuit a b) -> Event (b, Circuit a b)
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> Event (b, Circuit a b)) -> Circuit a b
Circuit ((a -> Event (b, Circuit a b)) -> Circuit a b)
-> (a -> Event (b, Circuit a b)) -> Circuit a b
forall a b. (a -> b) -> a -> b
$ Circuit a b -> a -> Event (b, Circuit a b)
loop Circuit a b
cir')