\fxerror{If more space left, show definitions and explain}
\fxerror{Reorganise in modules properly. For now, don't worry too much.}
\begin{comment}
\begin{code}
-- | TODO: Proper haddock docs
{-# LANGUAGE Arrows #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE TupleSections #-}
module LiveCoding.Cell where

-- base
import Control.Arrow
import Control.Category
import Control.Concurrent (threadDelay)
import Control.Monad
import Control.Monad.Fix
import Data.Data
import Prelude hiding ((.), id)

-- transformers
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader

-- essence-of-live-coding
import LiveCoding.LiveProgram

\end{code}
\end{comment}
\fxerror{
Is it clear that we do this FRP approach because of modularity,
both in the program definitions and also in the state types?
Maybe don't show the definitions of the primitives, but show the state types,
and the custom migrations implemented so that FRP reloads correctly.
Ideally, show the custom migrations as examples how users can add their own migrations.
The main connective could be that Cells build up their state automatically in a way that the migration works well.
(Test)
}
\fxerror{An important point along those lines would also be that the state type becomes a tree,
branching at \mintinline{haskell}{>>>} and \mintinline{haskell}{***} and \mintinline{haskell}{+++},
so individual subtrees are preserved well
}
In ordinary functional programming, the smallest building blocks are functions.
It stands to reason that in live coding, they should also be some flavour of functions,
in fact, \mintinline{haskell}{Arrow}s \cite{Arrows}.
We will see that it is possible to define bigger live programs from reusable components.
Crucially, the library user is disburdened from separating state and step function.
The state type is built up behind the scenes,
in a manner compatible with the automatic state migration.

\subsection{Cells}
\label{sec:cells}

In our definition of live programs as pairs of state and state steppers,
we can generalise the step functions to an additional input and output type.
\begin{comment}
\begin{spec}
mStep :: a -> s -> m (b, s)
\end{spec}
By now, the reader may have rightfully become weary of the ubiquitous \mintinline{haskell}{IO} monad;
and promoting it to an arbitrary monad will turn out shortly to be a very useful generalisation.
\fxerror{This has now been introduced earlier, in the WAI example, as Reader.}

We collect these insights in a definition,
\end{comment}
Live programs are thus generalised to effectful \emph{Mealy machines} \cite{Mealy}.
Let us call them cells, the building blocks of everything live:
\begin{comment}
\begin{code}
{- | The basic building block of a live program.

You can build cells directly, by using constructors,
or through the 'Functor', 'Applicative', or 'Arrow' type classes.

The 'Cell' constructor is the main way build a cell,
but for efficiency purposes there is an additional constructor.
-}
\end{code}
\end{comment}
\begin{code}
data Cell m a b = forall s . Data s => Cell
  { ()
cellState :: s
  , ()
cellStep  :: s -> a -> m (b, s)
  }
\end{code}
\begin{comment}
\begin{code}
  -- ^ A cell consists of an internal state,
  --   and an effectful state transition function.
  | ArrM { Cell m a b -> a -> m b
runArrM :: a -> m b }
  -- ^ Effectively a cell with trivial state.
  --   Added to improve performance and keep state types simpler.
\end{code}
\end{comment}
\begin{comment}
\begin{code}
-- | Converts every 'Cell' to the 'Cell' constructor.
--   Semantically, it is the identity function.
toCell :: Functor m => Cell m a b -> Cell m a b
toCell :: Cell m a b -> Cell m a b
toCell cell :: Cell m a b
cell@Cell {} = Cell m a b
cell
toCell ArrM { a -> m b
runArrM :: a -> m b
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
  { cellState :: ()
cellState = ()
  , cellStep :: () -> a -> m (b, ())
cellStep  = (a -> m (b, ())) -> () -> a -> m (b, ())
forall a b. a -> b -> a
const ((a -> m (b, ())) -> () -> a -> m (b, ()))
-> (a -> m (b, ())) -> () -> a -> m (b, ())
forall a b. (a -> b) -> a -> b
$ (b -> (b, ())) -> m b -> m (b, ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, ()) (m b -> m (b, ())) -> (a -> m b) -> a -> m (b, ())
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> m b
runArrM
  }
\end{code}
\end{comment}
Such a cell may progress by one step,
consuming an \mintinline{haskell}{a} as input,
and producing, by means of an effect in some monad \mintinline{haskell}{m},
not only the updated cell,
but also an output datum \mintinline{haskell}{b}:

\begin{comment}
\begin{code}
-- | Execute a cell for one step.
\end{code}
\end{comment}
\begin{code}
step
  :: Monad m
  => Cell m a b
  -> a -> m (b, Cell m a b)
step :: Cell m a b -> a -> m (b, Cell m a b)
step Cell { s
s -> a -> m (b, s)
cellStep :: s -> a -> m (b, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } a
a = do
  (b
b, s
cellState') <- s -> a -> m (b, s)
cellStep s
cellState a
a
  (b, Cell m a b) -> m (b, Cell m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { cellState :: s
cellState = s
cellState', s -> a -> m (b, s)
cellStep :: s -> a -> m (b, s)
cellStep :: s -> a -> m (b, s)
.. })
\end{code}
\begin{comment}
\begin{code}
step cell :: Cell m a b
cell@ArrM { a -> m b
runArrM :: a -> m b
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } a
a = ( , Cell m a b
cell) (b -> (b, Cell m a b)) -> m b -> m (b, Cell m a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
runArrM a
a
\end{code}
\end{comment}

\begin{comment}
\begin{code}
-- | Execute a cell for several steps.
--   The number of steps is determined by the length of the list of inputs.
steps
  :: Monad m
  => Cell m a b
  -> [a]
  -> m ([b], Cell m a b)
steps :: Cell m a b -> [a] -> m ([b], Cell m a b)
steps Cell m a b
cell [] = ([b], Cell m a b) -> m ([b], Cell m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Cell m a b
cell)
steps Cell m a b
cell (a
a : [a]
as) = do
  (b
b, Cell m a b
cell') <- Cell m a b -> a -> m (b, Cell m a b)
forall (m :: * -> *) a b.
Monad m =>
Cell m a b -> a -> m (b, Cell m a b)
step Cell m a b
cell a
a
  ([b]
bs, Cell m a b
cell'') <- Cell m a b -> [a] -> m ([b], Cell m a b)
forall (m :: * -> *) a b.
Monad m =>
Cell m a b -> [a] -> m ([b], Cell m a b)
steps Cell m a b
cell' [a]
as
  ([b], Cell m a b) -> m ([b], Cell m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b]
bs, Cell m a b
cell'')
\end{code}
\end{comment}

As a simple example, consider the following \mintinline{haskell}{Cell} which adds all input and returns the delayed sum each step:
\begin{comment}
\begin{code}
-- | Add all inputs and return the delayed sum.
\end{code}
\end{comment}
\begin{code}
sumC :: (Monad m, Num a, Data a) => Cell m a a
sumC :: Cell m a a
sumC = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { a
a -> a -> m (a, a)
forall (m :: * -> *) b. (Monad m, Num b) => b -> b -> m (b, b)
cellStep :: forall (m :: * -> *) b. (Monad m, Num b) => b -> b -> m (b, b)
cellState :: a
cellStep :: a -> a -> m (a, a)
cellState :: a
.. }
  where
    cellState :: a
cellState = a
0
    cellStep :: b -> b -> m (b, b)
cellStep b
accum b
a = (b, b) -> m (b, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
accum, b
accum b -> b -> b
forall a. Num a => a -> a -> a
+ b
a)
\end{code}

We recover live programs as the special case of trivial input and output:
\begin{comment}
\begin{code}
-- | Convert a cell with no inputs and outputs to a live program.
--   Semantically, this is an isomorphism.
\end{code}
\end{comment}
\begin{code}
liveCell
  :: Monad m
  => Cell        m () ()
  -> LiveProgram m
liveCell :: Cell m () () -> LiveProgram m
liveCell Cell { s
s -> () -> m ((), s)
cellStep :: s -> () -> m ((), s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = LiveProgram :: forall (m :: * -> *) s. Data s => s -> (s -> m s) -> LiveProgram m
LiveProgram
  { liveState :: s
liveState = s
cellState
  , liveStep :: s -> m s
liveStep  = \s
state -> do
      (()
_, s
state') <- s -> () -> m ((), s)
cellStep s
state ()
      s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
state'
  }
\end{code}
\begin{comment}
\begin{code}
liveCell ArrM { () -> m ()
runArrM :: () -> m ()
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = LiveProgram :: forall (m :: * -> *) s. Data s => s -> (s -> m s) -> LiveProgram m
LiveProgram
  { liveState :: ()
liveState = ()
  , liveStep :: () -> m ()
liveStep  = () -> m ()
runArrM
  }
\end{code}
\end{comment}
\begin{comment}
\begin{code}
-- | The inverse to 'liveCell'.
toLiveCell
  :: Functor     m
  => LiveProgram m
  -> Cell        m () ()
toLiveCell :: LiveProgram m -> Cell m () ()
toLiveCell LiveProgram { s
s -> m s
liveStep :: s -> m s
liveState :: s
liveStep :: ()
liveState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
  { cellState :: s
cellState = s
liveState
  , cellStep :: s -> () -> m ((), s)
cellStep  = \s
s () -> ((), ) (s -> ((), s)) -> m s -> m ((), s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m s
liveStep s
s
  }
\end{code}
\end{comment}

\subsection{FRP for Automata-Based Programming}
Our cells are known in the literature as ``Effectful Mealy Machines'', ``transducers'' and ``resumptions''
\cite{MILNER1975157}, \cite{pirog2014coinductive}, \cite[Section 7]{hasuo_jacobs_2011}, \cite[Section 5.4]{AbramskyHaghverdiScott}.
They are known for their relevance to stream functions \cite{CaspiPouzet},
suggesting that they offer a wide variety of applications in FRP.
The essential parts of the API,
which is heavily inspired by the FRP library \texttt{dunai}
\cite{Dunai},
are shown here.
%\mintinline{haskell}{Cell}s can be composed in three directions:
%Sequentially and parallely in the data flow sense,
%and sequentially in the control flow sense.
We will address the data flow aspects in this section,
investigating control flow later in Section \ref{sec:control flow}.

\begin{comment}
\begin{code}
hoistCell :: (forall x. m1 x -> m2 x) -> Cell m1 a b -> Cell m2 a b
hoistCell forall x. m1 x -> m2 x
morph Cell { s
s -> a -> m1 (b, s)
cellStep :: s -> a -> m1 (b, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
  { cellStep :: s -> a -> m2 (b, s)
cellStep = \s
s a
a -> m1 (b, s) -> m2 (b, s)
forall x. m1 x -> m2 x
morph (m1 (b, s) -> m2 (b, s)) -> m1 (b, s) -> m2 (b, s)
forall a b. (a -> b) -> a -> b
$ s -> a -> m1 (b, s)
cellStep s
s a
a
  , s
cellState :: s
cellState :: s
..
  }
hoistCell forall x. m1 x -> m2 x
morph ArrM { a -> m1 b
runArrM :: a -> m1 b
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = ArrM :: forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM
  { runArrM :: a -> m2 b
runArrM = m1 b -> m2 b
forall x. m1 x -> m2 x
morph (m1 b -> m2 b) -> (a -> m1 b) -> a -> m2 b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> m1 b
runArrM
  }
\end{code}
\end{comment}

\paragraph{Composition}
By being an instance of the type class \mintinline{haskell}{Category},
% for any monad \mintinline{haskell}{m},
cells implement sequential composition:
\begin{spec}
(>>>) :: Monad m
  => Cell  m a b
  -> Cell  m   b c
  -> Cell  m a   c
\end{spec}

\begin{comment}
\begin{code}
data Composition state1 state2 = Composition
  { Composition state1 state2 -> state1
state1 :: state1
  , Composition state1 state2 -> state2
state2 :: state2
  }
  deriving Typeable (Composition state1 state2)
DataType
Constr
Typeable (Composition state1 state2)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> Composition state1 state2
    -> c (Composition state1 state2))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Composition state1 state2))
-> (Composition state1 state2 -> Constr)
-> (Composition state1 state2 -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c (Composition state1 state2)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Composition state1 state2)))
-> ((forall b. Data b => b -> b)
    -> Composition state1 state2 -> Composition state1 state2)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> Composition state1 state2
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> Composition state1 state2
    -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> Composition state1 state2 -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u) -> Composition state1 state2 -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> Composition state1 state2 -> m (Composition state1 state2))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Composition state1 state2 -> m (Composition state1 state2))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Composition state1 state2 -> m (Composition state1 state2))
-> Data (Composition state1 state2)
Composition state1 state2 -> DataType
Composition state1 state2 -> Constr
(forall b. Data b => b -> b)
-> Composition state1 state2 -> Composition state1 state2
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Composition state1 state2
-> c (Composition state1 state2)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Composition state1 state2)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Composition state1 state2))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u) -> Composition state1 state2 -> u
forall u.
(forall d. Data d => d -> u) -> Composition state1 state2 -> [u]
forall state1 state2.
(Data state1, Data state2) =>
Typeable (Composition state1 state2)
forall state1 state2.
(Data state1, Data state2) =>
Composition state1 state2 -> DataType
forall state1 state2.
(Data state1, Data state2) =>
Composition state1 state2 -> Constr
forall state1 state2.
(Data state1, Data state2) =>
(forall b. Data b => b -> b)
-> Composition state1 state2 -> Composition state1 state2
forall state1 state2 u.
(Data state1, Data state2) =>
Int
-> (forall d. Data d => d -> u) -> Composition state1 state2 -> u
forall state1 state2 u.
(Data state1, Data state2) =>
(forall d. Data d => d -> u) -> Composition state1 state2 -> [u]
forall state1 state2 r r'.
(Data state1, Data state2) =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
forall state1 state2 r r'.
(Data state1, Data state2) =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
forall state1 state2 (m :: * -> *).
(Data state1, Data state2, Monad m) =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
forall state1 state2 (m :: * -> *).
(Data state1, Data state2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
forall state1 state2 (c :: * -> *).
(Data state1, Data state2) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Composition state1 state2)
forall state1 state2 (c :: * -> *).
(Data state1, Data state2) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Composition state1 state2
-> c (Composition state1 state2)
forall state1 state2 (t :: * -> *) (c :: * -> *).
(Data state1, Data state2, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (Composition state1 state2))
forall state1 state2 (t :: * -> * -> *) (c :: * -> *).
(Data state1, Data state2, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Composition state1 state2))
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Composition state1 state2)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Composition state1 state2
-> c (Composition state1 state2)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (Composition state1 state2))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Composition state1 state2))
$cComposition :: Constr
$tComposition :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
$cgmapMo :: forall state1 state2 (m :: * -> *).
(Data state1, Data state2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
gmapMp :: (forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
$cgmapMp :: forall state1 state2 (m :: * -> *).
(Data state1, Data state2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
gmapM :: (forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
$cgmapM :: forall state1 state2 (m :: * -> *).
(Data state1, Data state2, Monad m) =>
(forall d. Data d => d -> m d)
-> Composition state1 state2 -> m (Composition state1 state2)
gmapQi :: Int
-> (forall d. Data d => d -> u) -> Composition state1 state2 -> u
$cgmapQi :: forall state1 state2 u.
(Data state1, Data state2) =>
Int
-> (forall d. Data d => d -> u) -> Composition state1 state2 -> u
gmapQ :: (forall d. Data d => d -> u) -> Composition state1 state2 -> [u]
$cgmapQ :: forall state1 state2 u.
(Data state1, Data state2) =>
(forall d. Data d => d -> u) -> Composition state1 state2 -> [u]
gmapQr :: (r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
$cgmapQr :: forall state1 state2 r r'.
(Data state1, Data state2) =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
gmapQl :: (r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
$cgmapQl :: forall state1 state2 r r'.
(Data state1, Data state2) =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Composition state1 state2
-> r
gmapT :: (forall b. Data b => b -> b)
-> Composition state1 state2 -> Composition state1 state2
$cgmapT :: forall state1 state2.
(Data state1, Data state2) =>
(forall b. Data b => b -> b)
-> Composition state1 state2 -> Composition state1 state2
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Composition state1 state2))
$cdataCast2 :: forall state1 state2 (t :: * -> * -> *) (c :: * -> *).
(Data state1, Data state2, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Composition state1 state2))
dataCast1 :: (forall d. Data d => c (t d))
-> Maybe (c (Composition state1 state2))
$cdataCast1 :: forall state1 state2 (t :: * -> *) (c :: * -> *).
(Data state1, Data state2, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (Composition state1 state2))
dataTypeOf :: Composition state1 state2 -> DataType
$cdataTypeOf :: forall state1 state2.
(Data state1, Data state2) =>
Composition state1 state2 -> DataType
toConstr :: Composition state1 state2 -> Constr
$ctoConstr :: forall state1 state2.
(Data state1, Data state2) =>
Composition state1 state2 -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Composition state1 state2)
$cgunfold :: forall state1 state2 (c :: * -> *).
(Data state1, Data state2) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Composition state1 state2)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Composition state1 state2
-> c (Composition state1 state2)
$cgfoldl :: forall state1 state2 (c :: * -> *).
(Data state1, Data state2) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Composition state1 state2
-> c (Composition state1 state2)
$cp1Data :: forall state1 state2.
(Data state1, Data state2) =>
Typeable (Composition state1 state2)
Data

instance Monad m => Category (Cell m) where
  id :: Cell m a a
id = (a -> m a) -> Cell m a a
forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

  ArrM b -> m c
f . :: Cell m b c -> Cell m a b -> Cell m a c
. ArrM a -> m b
g = (a -> m c) -> Cell m a c
forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM ((a -> m c) -> Cell m a c) -> (a -> m c) -> Cell m a c
forall a b. (a -> b) -> a -> b
$ b -> m c
f (b -> m c) -> (a -> m b) -> a -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m b
g
  Cell { s
s -> b -> m (c, s)
cellStep :: s -> b -> m (c, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } . ArrM { a -> m b
runArrM :: a -> m b
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> a -> m (c, s)
cellStep = \s
state -> s -> b -> m (c, s)
cellStep s
state (b -> m (c, s)) -> (a -> m b) -> a -> m (c, s)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m b
runArrM
    , s
cellState :: s
cellState :: s
..
    }
  ArrM { b -> m c
runArrM :: b -> m c
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } . Cell { s
s -> a -> m (b, s)
cellStep :: s -> a -> m (b, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> a -> m (c, s)
cellStep = \s
state -> (Kleisli m (b, s) (c, s) -> (b, s) -> m (c, s)
forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (Kleisli m (b, s) (c, s) -> (b, s) -> m (c, s))
-> Kleisli m (b, s) (c, s) -> (b, s) -> m (c, s)
forall a b. (a -> b) -> a -> b
$ Kleisli m b c -> Kleisli m (b, s) (c, s)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (Kleisli m b c -> Kleisli m (b, s) (c, s))
-> Kleisli m b c -> Kleisli m (b, s) (c, s)
forall a b. (a -> b) -> a -> b
$ (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli b -> m c
runArrM) ((b, s) -> m (c, s)) -> (a -> m (b, s)) -> a -> m (c, s)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< s -> a -> m (b, s)
cellStep s
state
    , s
cellState :: s
cellState :: s
..
    }
  Cell s
state2 s -> b -> m (c, s)
step2 . Cell s
state1 s -> a -> m (b, s)
step1 = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { Composition s s
Composition s s -> a -> m (c, Composition s s)
cellStep :: Composition s s -> a -> m (c, Composition s s)
cellState :: Composition s s
cellStep :: Composition s s -> a -> m (c, Composition s s)
cellState :: Composition s s
.. }
    where
      cellState :: Composition s s
cellState = s -> s -> Composition s s
forall state1 state2. state1 -> state2 -> Composition state1 state2
Composition s
state1 s
state2
      cellStep :: Composition s s -> a -> m (c, Composition s s)
cellStep (Composition s
state1 s
state2) a
a = do
        (!b
b, s
state1') <- s -> a -> m (b, s)
step1 s
state1 a
a
        (!c
c, s
state2') <- s -> b -> m (c, s)
step2 s
state2 b
b
        (c, Composition s s) -> m (c, Composition s s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c
c, s -> s -> Composition s s
forall state1 state2. state1 -> state2 -> Composition state1 state2
Composition s
state1' s
state2')
-- {-# RULES
-- "arrM/>>>" forall (f :: forall a b m . Monad m => a -> m b) g . arrM f >>> arrM g = arrM (f >=> g)
-- #-} -- Don't really need rules here because GHC will inline all that anyways
\end{code}
\end{comment}
For two cells \mintinline{haskell}{cell1} and \mintinline{haskell}{cell2}
with state types \mintinline{haskell}{state1} and \mintinline{haskell}{state2},
the composite \mintinline{haskell}{cell1 >>> cell2} holds a pair of both states:
\fxwarning{Syntax highlighting is not very good here}
\begin{spec}
data Composition state1 state2 = Composition
  { state1 :: state1
  , state2 :: state2
  } deriving Data
\end{spec}
The step function executes the steps of both cells after each other.
They only touch their individual state variable,
the state stays encapsulated.
The custom data\-type is isomorphic to an ordinary Haskell tuple \mintinline{haskell}{(state1, state2)}.
Yet it is beneficial to introduce it,
since it allows us to extend the migration function easily such that it correctly handles the common case where we live change a cell \mintinline[style=bw]{haskell}{cellMiddle} to a composition,
such as \mintinline[style=bw]{haskell}{cellLeft >>> cellMiddle},
or to \mintinline[style=bw]{haskell}{cellMiddle >>> cellRight}.

\paragraph{The Sensor-SF-Actuator-Pattern}
Composing \mintinline{haskell}{Cell}s sequentially allows us to form live programs out of \emph{sensors}, pure signal functions and \emph{actuators}:
\begin{code}
type Sensor   a   = Cell   IO         () a
type SF       a b = forall m . Cell m    a b
type Actuator   b = Cell   IO              b ()
\end{code}
\begin{code}
buildProg :: Sensor a -> SF a b -> Actuator b
  -> LiveProgram IO
buildProg :: Sensor a -> SF a b -> Actuator b -> LiveProgram IO
buildProg Sensor a
sensor SF a b
sf Actuator b
actuator = Cell IO () () -> LiveProgram IO
forall (m :: * -> *). Monad m => Cell m () () -> LiveProgram m
liveCell
  (Cell IO () () -> LiveProgram IO)
-> Cell IO () () -> LiveProgram IO
forall a b. (a -> b) -> a -> b
$ Sensor a
sensor Sensor a -> Cell IO a () -> Cell IO () ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Cell IO a b
SF a b
sf Cell IO a b -> Actuator b -> Cell IO a ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Actuator b
actuator
\end{code}
This (optional) division of the reactive program into three such parts is inspired by Yampa \cite{Yampa},
and was formulated in this way in \cite[Section 7.1.2]{Dunai}.
We conveniently build a whole live program from smaller components.
It is never necessary to specify a big state type manually,
it will be composed from basic building blocks like \mintinline{haskell}{Composition}.

\paragraph{Arrowized FRP}
\mintinline{haskell}{Cell}s are an instance of the \mintinline{haskell}{Arrow} type class,
which allows us to lift pure functions to \mintinline{haskell}{Cell}s:
\begin{spec}
arr
  :: Monad m => (a -> b)
  -> Cell  m     a    b
\end{spec}
\fxwarning{Would be nice to have the space to explain *** as well!}
Together with the \mintinline{haskell}{ArrowChoice} and \mintinline{haskell}{ArrowLoop} classes
(discussed in the appendix),
cells can be used in \emph{arrow notation} \cite{ArrowNotation} with \mintinline{haskell}{case}-expressions,
\mintinline{haskell}{if then else} constructs and recursion.
The next subsection gives some examples.

An essential aspect of an FRP framework is some notion of \emph{time}.
\fxwarning{Citation?}
As this approach essentially uses the \texttt{dunai} API,
a detailed treatment of time domains and clocks as in \texttt{rhine} \cite{Rhine} could be readily applied here,
but this will be deferred to future work.
For simplicity and explicitness,
assume that we will execute all \mintinline{haskell}{Cell}s at a certain fixed step rate,
say, twenty five steps per second.
Then Euler integration can be defined:
\begin{code}
stepRate :: Num a => a
stepRate :: a
stepRate = a
25
\end{code}
\begin{code}
integrate
  :: (Data a, Fractional a, Monad m)
  => Cell m a a
integrate :: Cell m a a
integrate = (a -> a) -> Cell m a a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
forall a. Num a => a
stepRate) Cell m a a -> Cell m a a -> Cell m a a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Cell m a a
forall (m :: * -> *) a. (Monad m, Num a, Data a) => Cell m a a
sumC
\end{code}
The time since activation of a cell is then famously \cite[Section 2.4]{Yampa} defined as:
\begin{code}
localTime
  :: (Data a, Fractional a, Monad m)
  => Cell m b a
localTime :: Cell m b a
localTime = (b -> a) -> Cell m b a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> b -> a
forall a b. a -> b -> a
const a
1) Cell m b a -> Cell m a a -> Cell m b a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Cell m a a
forall a (m :: * -> *).
(Data a, Fractional a, Monad m) =>
Cell m a a
integrate
\end{code}

\fxwarning{I cut a more detailed discussion about ArrowChoice and ArrowLoop here. Put in the appendix?}

\paragraph{Monads and Their Morphisms}
Beyond standard arrows, a \mintinline{haskell}{Cell} can encode effects in a monad,
so it is not surprising that Kleisli arrows can be lifted:
\begin{spec}
arrM
  :: Monad m => (a -> m b)
  -> Cell  m     a      b
\end{spec}
\begin{comment}
Mere monadic actions become a special case thereof:
\begin{spec}
constM
  :: Monad m
  ->       m   b
  -> Cell  m a b
\end{spec}
\end{comment}

In case our \mintinline{haskell}{Cell} is in another monad than \mintinline{haskell}{IO},
one can define a function that transports a cell along a monad morphism:
\begin{comment}
\begin{code}
-- | Hoist a 'Cell' along a monad morphism.
\end{code}
\end{comment}
\begin{code}
hoistCell
  :: (forall x . m1 x   ->      m2 x)
  ->        Cell m1 a b -> Cell m2 a b
\end{code}
For example, we may eliminate a \mintinline{haskell}{ReaderT r} context by supplying the environment through the \mintinline{haskell}{runReaderT} monad morphism,
or lift into a monad transformer:
\begin{comment}
\begin{code}
-- | Lift a 'Cell' into a monad transformer.
\end{code}
\end{comment}
\begin{code}
liftCell
  :: (Monad  m, MonadTrans t)
  => Cell    m  a b
  -> Cell (t m) a b
liftCell :: Cell m a b -> Cell (t m) a b
liftCell = (forall x. m x -> t m x) -> Cell m a b -> Cell (t m) a b
forall (m1 :: * -> *) (m2 :: * -> *) a b.
(forall x. m1 x -> m2 x) -> Cell m1 a b -> Cell m2 a b
hoistCell forall x. m x -> t m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
\end{code}
As described in \cite[Section 4]{Dunai},
we can successively handle effects
(such as global state, read-only variables, logging, exceptions, and others)
until we arrive at \mintinline{haskell}{IO}.
Then we can execute the live program in the same way as before.

\begin{comment}
\begin{code}
data Parallel stateP1 stateP2 = Parallel
  { Parallel stateP1 stateP2 -> stateP1
stateP1 :: stateP1
  , Parallel stateP1 stateP2 -> stateP2
stateP2 :: stateP2
  }
  deriving Typeable (Parallel stateP1 stateP2)
DataType
Constr
Typeable (Parallel stateP1 stateP2)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> Parallel stateP1 stateP2
    -> c (Parallel stateP1 stateP2))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2))
-> (Parallel stateP1 stateP2 -> Constr)
-> (Parallel stateP1 stateP2 -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c (Parallel stateP1 stateP2)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Parallel stateP1 stateP2)))
-> ((forall b. Data b => b -> b)
    -> Parallel stateP1 stateP2 -> Parallel stateP1 stateP2)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> Parallel stateP1 stateP2
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> Parallel stateP1 stateP2
    -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2))
-> Data (Parallel stateP1 stateP2)
Parallel stateP1 stateP2 -> DataType
Parallel stateP1 stateP2 -> Constr
(forall b. Data b => b -> b)
-> Parallel stateP1 stateP2 -> Parallel stateP1 stateP2
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Parallel stateP1 stateP2
-> c (Parallel stateP1 stateP2)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Parallel stateP1 stateP2))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> u
forall u.
(forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> [u]
forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Typeable (Parallel stateP1 stateP2)
forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Parallel stateP1 stateP2 -> DataType
forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Parallel stateP1 stateP2 -> Constr
forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
(forall b. Data b => b -> b)
-> Parallel stateP1 stateP2 -> Parallel stateP1 stateP2
forall stateP1 stateP2 u.
(Data stateP1, Data stateP2) =>
Int
-> (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> u
forall stateP1 stateP2 u.
(Data stateP1, Data stateP2) =>
(forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> [u]
forall stateP1 stateP2 r r'.
(Data stateP1, Data stateP2) =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
forall stateP1 stateP2 r r'.
(Data stateP1, Data stateP2) =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
forall stateP1 stateP2 (m :: * -> *).
(Data stateP1, Data stateP2, Monad m) =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
forall stateP1 stateP2 (m :: * -> *).
(Data stateP1, Data stateP2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
forall stateP1 stateP2 (c :: * -> *).
(Data stateP1, Data stateP2) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2)
forall stateP1 stateP2 (c :: * -> *).
(Data stateP1, Data stateP2) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Parallel stateP1 stateP2
-> c (Parallel stateP1 stateP2)
forall stateP1 stateP2 (t :: * -> *) (c :: * -> *).
(Data stateP1, Data stateP2, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (Parallel stateP1 stateP2))
forall stateP1 stateP2 (t :: * -> * -> *) (c :: * -> *).
(Data stateP1, Data stateP2, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Parallel stateP1 stateP2))
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Parallel stateP1 stateP2
-> c (Parallel stateP1 stateP2)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (Parallel stateP1 stateP2))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Parallel stateP1 stateP2))
$cParallel :: Constr
$tParallel :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
$cgmapMo :: forall stateP1 stateP2 (m :: * -> *).
(Data stateP1, Data stateP2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
gmapMp :: (forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
$cgmapMp :: forall stateP1 stateP2 (m :: * -> *).
(Data stateP1, Data stateP2, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
gmapM :: (forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
$cgmapM :: forall stateP1 stateP2 (m :: * -> *).
(Data stateP1, Data stateP2, Monad m) =>
(forall d. Data d => d -> m d)
-> Parallel stateP1 stateP2 -> m (Parallel stateP1 stateP2)
gmapQi :: Int
-> (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> u
$cgmapQi :: forall stateP1 stateP2 u.
(Data stateP1, Data stateP2) =>
Int
-> (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> u
gmapQ :: (forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> [u]
$cgmapQ :: forall stateP1 stateP2 u.
(Data stateP1, Data stateP2) =>
(forall d. Data d => d -> u) -> Parallel stateP1 stateP2 -> [u]
gmapQr :: (r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
$cgmapQr :: forall stateP1 stateP2 r r'.
(Data stateP1, Data stateP2) =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
gmapQl :: (r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
$cgmapQl :: forall stateP1 stateP2 r r'.
(Data stateP1, Data stateP2) =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> Parallel stateP1 stateP2
-> r
gmapT :: (forall b. Data b => b -> b)
-> Parallel stateP1 stateP2 -> Parallel stateP1 stateP2
$cgmapT :: forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
(forall b. Data b => b -> b)
-> Parallel stateP1 stateP2 -> Parallel stateP1 stateP2
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Parallel stateP1 stateP2))
$cdataCast2 :: forall stateP1 stateP2 (t :: * -> * -> *) (c :: * -> *).
(Data stateP1, Data stateP2, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Parallel stateP1 stateP2))
dataCast1 :: (forall d. Data d => c (t d))
-> Maybe (c (Parallel stateP1 stateP2))
$cdataCast1 :: forall stateP1 stateP2 (t :: * -> *) (c :: * -> *).
(Data stateP1, Data stateP2, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (Parallel stateP1 stateP2))
dataTypeOf :: Parallel stateP1 stateP2 -> DataType
$cdataTypeOf :: forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Parallel stateP1 stateP2 -> DataType
toConstr :: Parallel stateP1 stateP2 -> Constr
$ctoConstr :: forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Parallel stateP1 stateP2 -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2)
$cgunfold :: forall stateP1 stateP2 (c :: * -> *).
(Data stateP1, Data stateP2) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Parallel stateP1 stateP2)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Parallel stateP1 stateP2
-> c (Parallel stateP1 stateP2)
$cgfoldl :: forall stateP1 stateP2 (c :: * -> *).
(Data stateP1, Data stateP2) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Parallel stateP1 stateP2
-> c (Parallel stateP1 stateP2)
$cp1Data :: forall stateP1 stateP2.
(Data stateP1, Data stateP2) =>
Typeable (Parallel stateP1 stateP2)
Data

instance Monad m => Arrow (Cell m) where
  arr :: (b -> c) -> Cell m b c
arr = (b -> m c) -> Cell m b c
forall a (m :: * -> *) b. (a -> m b) -> Cell m a b
arrM ((b -> m c) -> Cell m b c)
-> ((b -> c) -> b -> m c) -> (b -> c) -> Cell m b c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> (b -> c) -> b -> m c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
.)

  -- For efficiency because Arrow desugaring favours 'first'
  first :: Cell m b c -> Cell m (b, d) (c, d)
first ArrM { b -> m c
runArrM :: b -> m c
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = ArrM :: forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM { runArrM :: (b, d) -> m (c, d)
runArrM = \(b
a, d
c) -> ( , d
c) (c -> (c, d)) -> m c -> m (c, d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> m c
runArrM b
a }
  first Cell { s
s -> b -> m (c, s)
cellStep :: s -> b -> m (c, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> (b, d) -> m ((c, d), s)
cellStep = \s
s (b
a, d
c) -> (c -> (c, d)) -> (c, s) -> ((c, d), s)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first ((, d
c) (c -> (c, d)) -> c -> (c, d)
forall a b. (a -> b) -> a -> b
$!) ((c, s) -> ((c, d), s)) -> m (c, s) -> m ((c, d), s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> b -> m (c, s)
cellStep s
s b
a
    , s
cellState :: s
cellState :: s
..
    }

  ArrM b -> m c
f *** :: Cell m b c -> Cell m b' c' -> Cell m (b, b') (c, c')
*** ArrM b' -> m c'
g = ((b, b') -> m (c, c')) -> Cell m (b, b') (c, c')
forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM (((b, b') -> m (c, c')) -> Cell m (b, b') (c, c'))
-> ((b, b') -> m (c, c')) -> Cell m (b, b') (c, c')
forall a b. (a -> b) -> a -> b
$ Kleisli m (b, b') (c, c') -> (b, b') -> m (c, c')
forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (Kleisli m (b, b') (c, c') -> (b, b') -> m (c, c'))
-> Kleisli m (b, b') (c, c') -> (b, b') -> m (c, c')
forall a b. (a -> b) -> a -> b
$ (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli b -> m c
f Kleisli m b c -> Kleisli m b' c' -> Kleisli m (b, b') (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** (b' -> m c') -> Kleisli m b' c'
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli b' -> m c'
g
  ArrM { b -> m c
runArrM :: b -> m c
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } *** Cell { s
s -> b' -> m (c', s)
cellStep :: s -> b' -> m (c', s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> (b, b') -> m ((c, c'), s)
cellStep = \s
state (b
a, b'
c) -> do
      !c
b <- b -> m c
runArrM b
a
      (!c'
d, s
state') <- s -> b' -> m (c', s)
cellStep s
state b'
c
      ((c, c'), s) -> m ((c, c'), s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
b, c'
d), s
state')
    , s
cellState :: s
cellState :: s
..
    }
  Cell { s
s -> b -> m (c, s)
cellStep :: s -> b -> m (c, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } *** ArrM { b' -> m c'
runArrM :: b' -> m c'
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> (b, b') -> m ((c, c'), s)
cellStep = \s
state (b
a, b'
c) -> do
      (!c
b, s
state') <- s -> b -> m (c, s)
cellStep s
state b
a
      !c'
d <- b' -> m c'
runArrM b'
c
      ((c, c'), s) -> m ((c, c'), s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
b, c'
d), s
state')
    , s
cellState :: s
cellState :: s
..
    }
  Cell s
stateP1 s -> b -> m (c, s)
step1 *** Cell s
stateP2 s -> b' -> m (c', s)
step2 = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { Parallel s s
Parallel s s -> (b, b') -> m ((c, c'), Parallel s s)
cellStep :: Parallel s s -> (b, b') -> m ((c, c'), Parallel s s)
cellState :: Parallel s s
cellStep :: Parallel s s -> (b, b') -> m ((c, c'), Parallel s s)
cellState :: Parallel s s
.. }
    where
      cellState :: Parallel s s
cellState = Parallel :: forall stateP1 stateP2.
stateP1 -> stateP2 -> Parallel stateP1 stateP2
Parallel { s
s
stateP2 :: s
stateP1 :: s
stateP2 :: s
stateP1 :: s
.. }
      cellStep :: Parallel s s -> (b, b') -> m ((c, c'), Parallel s s)
cellStep (Parallel { s
s
stateP2 :: s
stateP1 :: s
stateP2 :: forall stateP1 stateP2. Parallel stateP1 stateP2 -> stateP2
stateP1 :: forall stateP1 stateP2. Parallel stateP1 stateP2 -> stateP1
.. }) (b
a, b'
c) = do
        (!c
b, s
stateP1') <- s -> b -> m (c, s)
step1 s
stateP1 b
a
        (!c'
d, s
stateP2') <- s -> b' -> m (c', s)
step2 s
stateP2 b'
c
        ((c, c'), Parallel s s) -> m ((c, c'), Parallel s s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
b, c'
d), s -> s -> Parallel s s
forall stateP1 stateP2.
stateP1 -> stateP2 -> Parallel stateP1 stateP2
Parallel s
stateP1' s
stateP2')

arrM :: (a -> m b) -> Cell m a b
arrM :: (a -> m b) -> Cell m a b
arrM = (a -> m b) -> Cell m a b
forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM

constM :: m b -> Cell m a b
constM :: m b -> Cell m a b
constM = (a -> m b) -> Cell m a b
forall a (m :: * -> *) b. (a -> m b) -> Cell m a b
arrM ((a -> m b) -> Cell m a b)
-> (m b -> a -> m b) -> m b -> Cell m a b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m b -> a -> m b
forall a b. a -> b -> a
const

constC :: Monad m => b -> Cell m a b
constC :: b -> Cell m a b
constC = m b -> Cell m a b
forall (m :: * -> *) b a. m b -> Cell m a b
constM (m b -> Cell m a b) -> (b -> m b) -> b -> Cell m a b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return
\end{code}
\end{comment}

\begin{comment}
\begin{code}
instance MonadFix m => ArrowLoop (Cell m) where
  loop :: Cell m (b, d) (c, d) -> Cell m b c
loop ArrM { (b, d) -> m (c, d)
runArrM :: (b, d) -> m (c, d)
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = ArrM :: forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM
    { runArrM :: b -> m c
runArrM = \b
a -> do
        rec (c
b, d
c) <- (\d
c' -> (b, d) -> m (c, d)
runArrM (b
a, d
c')) d
c
        c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return c
b
    }
  loop (Cell s
state s -> (b, d) -> m ((c, d), s)
step) = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { s
s -> b -> m (c, s)
cellStep :: s -> b -> m (c, s)
cellState :: s
cellStep :: s -> b -> m (c, s)
cellState :: s
.. }
    where
      cellState :: s
cellState = s
state
      cellStep :: s -> b -> m (c, s)
cellStep s
state b
a = do
        rec ((c
b, d
c), s
state') <- (\d
c' -> s -> (b, d) -> m ((c, d), s)
step s
state (b
a, d
c')) d
c
        (c, s) -> m (c, s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c
b, s
state')

{-
instance ArrowLoop (Cell Identity) where
  loop (Cell state step) = Cell { .. }
    where
      cellState = state
      changedStep state (a, c) = runIdentity $ step state (a, c)
      cellStep state a = let ((b, c), state') = (\c' -> changedStep state (a, c')) c
        in return (b, state')
-}
\end{code}
\end{comment}

\subsection{A Sine Generator}
Making use of the \mintinline{haskell}{Arrows} syntax extension\footnote{%
Arrow notation -- or \mintinline{haskell}{proc .. do} notation --
is similar to monadic \mintinline{haskell}{do} notation,
except that not only is there a dedicated binder \mintinline{haskell}{<-} for output values,
but also an application operator \mintinline{haskell}{-<} for \emph{input} values.
The notation is desugared into the arrow operators,
such as \mintinline{haskell}{arr} and the composition \mintinline{haskell}{>>>}.},
we can implement a harmonic oscillator that will produce a sine wave with amplitude 10 and given period length:
\fxwarning{Comment on rec and ArrowFix}
\fxerror{I want to add a delay for numerical stability}
\begin{code}
sine
  :: MonadFix m
  => Double -> Cell m () Double
sine :: Double -> Cell m () Double
sine Double
t = proc () -> do
  rec
    let acc :: Double
acc = - (Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
forall a. Floating a => a
pi Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
t) Double -> Integer -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
pos Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
10)
    Double
vel <- Cell m Double Double
forall a (m :: * -> *).
(Data a, Fractional a, Monad m) =>
Cell m a a
integrate -< Double
acc
    Double
pos <- Cell m Double Double
forall a (m :: * -> *).
(Data a, Fractional a, Monad m) =>
Cell m a a
integrate -< Double
vel
  Cell m Double Double
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< Double
pos
\end{code}
By the laws of physics, velocity is the integral of accelleration,
and position is the integral of velocity.
In a harmonic oscillator, the acceleration is in the negative direction of the position,
multiplied by a spring factor depending on the period length,
which can be given as an argument.
The integration arrow encapsulates the current position and velocity of the oscillator as internal state, and returns the position.

The sine generator could in principle be used in an audio or video application.
For simplicity, we choose to visualise the signal on the console instead,
with our favourite Haskell operator varying its horizontal position:
\begin{code}
asciiArt :: Double -> String
asciiArt :: Double -> String
asciiArt Double
n = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round Double
n) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
">>="
\end{code}
\begin{code}
printEverySecond :: Cell IO String ()
printEverySecond :: Cell IO String ()
printEverySecond = proc String
string -> do
  Integer
count <- Cell IO Integer Integer
forall (m :: * -> *) a. (Monad m, Num a, Data a) => Cell m a a
sumC -< Integer
1 :: Integer
  if Integer
count Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
forall a. Num a => a
stepRate Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
    then (String -> IO ()) -> Cell IO String ()
forall a (m :: * -> *) b. (a -> m b) -> Cell m a b
arrM String -> IO ()
putStrLn -< String
string
    else Cell IO () ()
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA       -< ()
\end{code}
Our first live program
written in FRP is assembled using the pattern of sensor, signal function and actuator:
\begin{code}
printSine :: Double -> LiveProgram IO
printSine :: Double -> LiveProgram IO
printSine Double
t = Cell IO () () -> LiveProgram IO
forall (m :: * -> *). Monad m => Cell m () () -> LiveProgram m
liveCell
  (Cell IO () () -> LiveProgram IO)
-> Cell IO () () -> LiveProgram IO
forall a b. (a -> b) -> a -> b
$   Double -> Cell IO () Double
forall (m :: * -> *). MonadFix m => Double -> Cell m () Double
sine Double
t
  Cell IO () Double -> Cell IO Double () -> Cell IO () ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Double -> String) -> Cell IO Double String
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Double -> String
asciiArt
  Cell IO Double String -> Cell IO String () -> Cell IO Double ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Cell IO String ()
printEverySecond
\end{code}
\fxwarning{Maybe mention that we could use this in gloss, audio or whatever?}

What if we would run it,
and change the period in mid-execution?
%This is exactly what the framework was designed for.
\fxerror{Show Demo.hs as soon as I've explained the runtime in the previous section}
We execute the program such that after a certain time,
the live environment inserts \mintinline{haskell}{printSine} with a different period.
\fxerror{Actually, now that we have those fancy GHCi commands,
We can insert them instead of manually printing stuff.
Increases the immersion.
But it's actually cheating.
}
Let us execute it:\footnote{%
From now on, the GHCi commands will be suppressed.
}
\verbatiminput{../demos/DemoSine.txt}
It is clearly visible how the period of the oscillator changed,
\fxwarning{Only if this doesn't break. Maybe make figures?}
while its position (or, in terms of signal processing, its phase)
did not jump.
If we use the oscillator in an audio application,
we can retune it without hearing a glitch;
if we use it in a video application,
the widget will smoothly change its oscillating velocity without a jolt.

\section{Control Flow}
\label{sec:control flow}
\fxerror{Show only stuff where I can show most of the implementation. Reimplement, in a separate file, the API for the newtype, show its code and explain it.}
Although we now have the tools to build big signal pathways from single cells,
we have no way yet to let the incoming data decide which of several offered pathways to take for the rest of the execution.
While we can (due to \mintinline{haskell}{ArrowChoice}) temporarily branch between two cells using \mintinline{haskell}{if then else},
the branching is reevaluated (and the previous choice forgotten) every step.
We are lacking permanent \emph{control flow}.

The primeval arrowized FRP framework Yampa \cite{Yampa} caters for this requirement by means of switching from a signal function to another if an event occurs.
Such mechanisms are well studied, e.g. in \cite{WinogradHudak2014settable}.
\fxwarning{Possibly I've mentioned both earlier}
Dunai \cite[Section 5.3]{Dunai}, taking the monadic aspect seriously,
\fxwarning{Dunai, Yampa -> \texttt{Dunai} etc.?}
rediscovers switching as effect handling in the \mintinline{haskell}{Either} monad.
Although the state of a \mintinline{haskell}{Cell} is strongly restricted by the \mintinline{haskell}{Data} type class,
we can reimplement this powerful approach to control flow with few alterations,
and make typical control flow patterns such as exception handling and looping amenable to live coding without further effort.

\begin{comment}
\begin{code}
-- FIXME Why the hell is my left definition wrong or leads to the wrong instance?
data Choice stateL stateR = Choice
  { Choice stateL stateR -> stateL
choiceLeft  :: stateL
  , Choice stateL stateR -> stateR
choiceRight :: stateR
  }
  deriving Typeable (Choice stateL stateR)
DataType
Constr
Typeable (Choice stateL stateR)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> Choice stateL stateR
    -> c (Choice stateL stateR))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR))
-> (Choice stateL stateR -> Constr)
-> (Choice stateL stateR -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Choice stateL stateR)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Choice stateL stateR)))
-> ((forall b. Data b => b -> b)
    -> Choice stateL stateR -> Choice stateL stateR)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> Choice stateL stateR -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Choice stateL stateR -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> Choice stateL stateR -> m (Choice stateL stateR))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Choice stateL stateR -> m (Choice stateL stateR))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> Choice stateL stateR -> m (Choice stateL stateR))
-> Data (Choice stateL stateR)
Choice stateL stateR -> DataType
Choice stateL stateR -> Constr
(forall b. Data b => b -> b)
-> Choice stateL stateR -> Choice stateL stateR
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Choice stateL stateR
-> c (Choice stateL stateR)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Choice stateL stateR))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> Choice stateL stateR -> u
forall u.
(forall d. Data d => d -> u) -> Choice stateL stateR -> [u]
forall stateL stateR.
(Data stateL, Data stateR) =>
Typeable (Choice stateL stateR)
forall stateL stateR.
(Data stateL, Data stateR) =>
Choice stateL stateR -> DataType
forall stateL stateR.
(Data stateL, Data stateR) =>
Choice stateL stateR -> Constr
forall stateL stateR.
(Data stateL, Data stateR) =>
(forall b. Data b => b -> b)
-> Choice stateL stateR -> Choice stateL stateR
forall stateL stateR u.
(Data stateL, Data stateR) =>
Int -> (forall d. Data d => d -> u) -> Choice stateL stateR -> u
forall stateL stateR u.
(Data stateL, Data stateR) =>
(forall d. Data d => d -> u) -> Choice stateL stateR -> [u]
forall stateL stateR r r'.
(Data stateL, Data stateR) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
forall stateL stateR r r'.
(Data stateL, Data stateR) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
forall stateL stateR (m :: * -> *).
(Data stateL, Data stateR, Monad m) =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
forall stateL stateR (m :: * -> *).
(Data stateL, Data stateR, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
forall stateL stateR (c :: * -> *).
(Data stateL, Data stateR) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR)
forall stateL stateR (c :: * -> *).
(Data stateL, Data stateR) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Choice stateL stateR
-> c (Choice stateL stateR)
forall stateL stateR (t :: * -> *) (c :: * -> *).
(Data stateL, Data stateR, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Choice stateL stateR))
forall stateL stateR (t :: * -> * -> *) (c :: * -> *).
(Data stateL, Data stateR, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Choice stateL stateR))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Choice stateL stateR
-> c (Choice stateL stateR)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Choice stateL stateR))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Choice stateL stateR))
$cChoice :: Constr
$tChoice :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
$cgmapMo :: forall stateL stateR (m :: * -> *).
(Data stateL, Data stateR, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
gmapMp :: (forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
$cgmapMp :: forall stateL stateR (m :: * -> *).
(Data stateL, Data stateR, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
gmapM :: (forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
$cgmapM :: forall stateL stateR (m :: * -> *).
(Data stateL, Data stateR, Monad m) =>
(forall d. Data d => d -> m d)
-> Choice stateL stateR -> m (Choice stateL stateR)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Choice stateL stateR -> u
$cgmapQi :: forall stateL stateR u.
(Data stateL, Data stateR) =>
Int -> (forall d. Data d => d -> u) -> Choice stateL stateR -> u
gmapQ :: (forall d. Data d => d -> u) -> Choice stateL stateR -> [u]
$cgmapQ :: forall stateL stateR u.
(Data stateL, Data stateR) =>
(forall d. Data d => d -> u) -> Choice stateL stateR -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
$cgmapQr :: forall stateL stateR r r'.
(Data stateL, Data stateR) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
$cgmapQl :: forall stateL stateR r r'.
(Data stateL, Data stateR) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Choice stateL stateR -> r
gmapT :: (forall b. Data b => b -> b)
-> Choice stateL stateR -> Choice stateL stateR
$cgmapT :: forall stateL stateR.
(Data stateL, Data stateR) =>
(forall b. Data b => b -> b)
-> Choice stateL stateR -> Choice stateL stateR
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Choice stateL stateR))
$cdataCast2 :: forall stateL stateR (t :: * -> * -> *) (c :: * -> *).
(Data stateL, Data stateR, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Choice stateL stateR))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Choice stateL stateR))
$cdataCast1 :: forall stateL stateR (t :: * -> *) (c :: * -> *).
(Data stateL, Data stateR, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Choice stateL stateR))
dataTypeOf :: Choice stateL stateR -> DataType
$cdataTypeOf :: forall stateL stateR.
(Data stateL, Data stateR) =>
Choice stateL stateR -> DataType
toConstr :: Choice stateL stateR -> Constr
$ctoConstr :: forall stateL stateR.
(Data stateL, Data stateR) =>
Choice stateL stateR -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR)
$cgunfold :: forall stateL stateR (c :: * -> *).
(Data stateL, Data stateR) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Choice stateL stateR)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Choice stateL stateR
-> c (Choice stateL stateR)
$cgfoldl :: forall stateL stateR (c :: * -> *).
(Data stateL, Data stateR) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> Choice stateL stateR
-> c (Choice stateL stateR)
$cp1Data :: forall stateL stateR.
(Data stateL, Data stateR) =>
Typeable (Choice stateL stateR)
Data
instance Monad m => ArrowChoice (Cell m) where
{-
  left (Cell state step) = Cell { cellState = state, .. }
    where
      cellStep cellState (Left a) = do
        (b, cellState') <- step state a
        return (Left b, cellState')
      cellStep cellState (Right b) = return (Right b, cellState)
      -}
  ArrM b -> m c
f +++ :: Cell m b c -> Cell m b' c' -> Cell m (Either b b') (Either c c')
+++ ArrM b' -> m c'
g = (Either b b' -> m (Either c c'))
-> Cell m (Either b b') (Either c c')
forall (m :: * -> *) a b. (a -> m b) -> Cell m a b
ArrM ((Either b b' -> m (Either c c'))
 -> Cell m (Either b b') (Either c c'))
-> (Either b b' -> m (Either c c'))
-> Cell m (Either b b') (Either c c')
forall a b. (a -> b) -> a -> b
$ Kleisli m (Either b b') (Either c c')
-> Either b b' -> m (Either c c')
forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (Kleisli m (Either b b') (Either c c')
 -> Either b b' -> m (Either c c'))
-> Kleisli m (Either b b') (Either c c')
-> Either b b'
-> m (Either c c')
forall a b. (a -> b) -> a -> b
$ (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli b -> m c
f Kleisli m b c
-> Kleisli m b' c' -> Kleisli m (Either b b') (Either c c')
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ (b' -> m c') -> Kleisli m b' c'
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli b' -> m c'
g
  ArrM { b -> m c
runArrM :: b -> m c
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } +++ Cell { s
s -> b' -> m (c', s)
cellStep :: s -> b' -> m (c', s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> Either b b' -> m (Either c c', s)
cellStep = \s
state -> \case
        Left b
a -> do
          !c
b <- b -> m c
runArrM b
a
          (Either c c', s) -> m (Either c c', s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c c'
forall a b. a -> Either a b
Left c
b, s
state)
        Right b'
c -> do
          (!c'
d, s
state') <- s -> b' -> m (c', s)
cellStep s
state b'
c
          (Either c c', s) -> m (Either c c', s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c' -> Either c c'
forall a b. b -> Either a b
Right c'
d, s
state')
    , s
cellState :: s
cellState :: s
..
    }
  Cell { s
s -> b -> m (c, s)
cellStep :: s -> b -> m (c, s)
cellState :: s
cellStep :: ()
cellState :: ()
.. } +++ ArrM { b' -> m c'
runArrM :: b' -> m c'
runArrM :: forall (m :: * -> *) a b. Cell m a b -> a -> m b
.. } = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell
    { cellStep :: s -> Either b b' -> m (Either c c', s)
cellStep = \s
state -> \case
        Left b
a -> do
          (!c
b, s
state') <- s -> b -> m (c, s)
cellStep s
state b
a
          (Either c c', s) -> m (Either c c', s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c c'
forall a b. a -> Either a b
Left c
b, s
state')
        Right b'
c -> do
          !c'
d <- b' -> m c'
runArrM b'
c
          (Either c c', s) -> m (Either c c', s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c' -> Either c c'
forall a b. b -> Either a b
Right c'
d, s
state)
    , s
cellState :: s
cellState :: s
..
    }
  (Cell s
stateL s -> b -> m (c, s)
stepL) +++ (Cell s
stateR s -> b' -> m (c', s)
stepR) = Cell :: forall (m :: * -> *) a b s.
Data s =>
s -> (s -> a -> m (b, s)) -> Cell m a b
Cell { Choice s s
Choice s s -> Either b b' -> m (Either c c', Choice s s)
cellStep :: Choice s s -> Either b b' -> m (Either c c', Choice s s)
cellState :: Choice s s
cellStep :: Choice s s -> Either b b' -> m (Either c c', Choice s s)
cellState :: Choice s s
.. }
    where
      cellState :: Choice s s
cellState = s -> s -> Choice s s
forall stateL stateR. stateL -> stateR -> Choice stateL stateR
Choice s
stateL s
stateR
      cellStep :: Choice s s -> Either b b' -> m (Either c c', Choice s s)
cellStep (Choice s
stateL s
stateR) (Left b
a) = do
        (!c
b, s
stateL') <- s -> b -> m (c, s)
stepL s
stateL b
a
        (Either c c', Choice s s) -> m (Either c c', Choice s s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> Either c c'
forall a b. a -> Either a b
Left c
b, (s -> s -> Choice s s
forall stateL stateR. stateL -> stateR -> Choice stateL stateR
Choice s
stateL' s
stateR))
      cellStep (Choice s
stateL s
stateR) (Right b'
c) = do
        (!c'
d, s
stateR') <- s -> b' -> m (c', s)
stepR s
stateR b'
c
        (Either c c', Choice s s) -> m (Either c c', Choice s s)
forall (m :: * -> *) a. Monad m => a -> m a
return (c' -> Either c c'
forall a b. b -> Either a b
Right c'
d, (s -> s -> Choice s s
forall stateL stateR. stateL -> stateR -> Choice stateL stateR
Choice s
stateL s
stateR'))
\end{code}
\end{comment}