{-|
Module: Reflex.BehaviorWriter.Base
Description: Implementation of BehaviorWriter
-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
#ifdef USE_REFLEX_OPTIMIZER
{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
#endif
module Reflex.BehaviorWriter.Base
  ( BehaviorWriterT (..)
  , runBehaviorWriterT
  , withBehaviorWriterT
  ) where

import Control.Monad.Exception
import Control.Monad.Identity
import Control.Monad.IO.Class
import Control.Monad.Reader
import Control.Monad.Ref
import Control.Monad.State.Strict
import Data.Dependent.Map (DMap)
import qualified Data.Dependent.Map as DMap
import Data.Functor.Misc
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Some (Some)

import Reflex.Class
import Reflex.Adjustable.Class
import Reflex.BehaviorWriter.Class
import Reflex.Host.Class
import Reflex.PerformEvent.Class
import Reflex.PostBuild.Class
import Reflex.Query.Class
import Reflex.Requester.Class
import Reflex.TriggerEvent.Class

-- | A basic implementation of 'BehaviorWriter'.
newtype BehaviorWriterT t w m a = BehaviorWriterT { BehaviorWriterT t w m a -> StateT [Behavior t w] m a
unBehaviorWriterT :: StateT [Behavior t w] m a }
  deriving (a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
(a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
(forall a b.
 (a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b)
-> (forall a b.
    a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a)
-> Functor (BehaviorWriterT t w m)
forall a b. a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall a b.
(a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
forall t w (m :: * -> *) a b.
Functor m =>
a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a b.
Functor m =>
(a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
$c<$ :: forall t w (m :: * -> *) a b.
Functor m =>
a -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
fmap :: (a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
$cfmap :: forall t w (m :: * -> *) a b.
Functor m =>
(a -> b) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
Functor, Functor (BehaviorWriterT t w m)
a -> BehaviorWriterT t w m a
Functor (BehaviorWriterT t w m) =>
(forall a. a -> BehaviorWriterT t w m a)
-> (forall a b.
    BehaviorWriterT t w m (a -> b)
    -> BehaviorWriterT t w m a -> BehaviorWriterT t w m b)
-> (forall a b c.
    (a -> b -> c)
    -> BehaviorWriterT t w m a
    -> BehaviorWriterT t w m b
    -> BehaviorWriterT t w m c)
-> (forall a b.
    BehaviorWriterT t w m a
    -> BehaviorWriterT t w m b -> BehaviorWriterT t w m b)
-> (forall a b.
    BehaviorWriterT t w m a
    -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a)
-> Applicative (BehaviorWriterT t w m)
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
BehaviorWriterT t w m (a -> b)
-> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
(a -> b -> c)
-> BehaviorWriterT t w m a
-> BehaviorWriterT t w m b
-> BehaviorWriterT t w m c
forall a. a -> BehaviorWriterT t w m a
forall a b.
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall a b.
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
forall a b.
BehaviorWriterT t w m (a -> b)
-> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
forall a b c.
(a -> b -> c)
-> BehaviorWriterT t w m a
-> BehaviorWriterT t w m b
-> BehaviorWriterT t w m c
forall t w (m :: * -> *).
Monad m =>
Functor (BehaviorWriterT t w m)
forall t w (m :: * -> *) a. Monad m => a -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m (a -> b)
-> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
forall t w (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> BehaviorWriterT t w m a
-> BehaviorWriterT t w m b
-> BehaviorWriterT t w m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
$c<* :: forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
*> :: BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
$c*> :: forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
liftA2 :: (a -> b -> c)
-> BehaviorWriterT t w m a
-> BehaviorWriterT t w m b
-> BehaviorWriterT t w m c
$cliftA2 :: forall t w (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> BehaviorWriterT t w m a
-> BehaviorWriterT t w m b
-> BehaviorWriterT t w m c
<*> :: BehaviorWriterT t w m (a -> b)
-> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
$c<*> :: forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m (a -> b)
-> BehaviorWriterT t w m a -> BehaviorWriterT t w m b
pure :: a -> BehaviorWriterT t w m a
$cpure :: forall t w (m :: * -> *) a. Monad m => a -> BehaviorWriterT t w m a
$cp1Applicative :: forall t w (m :: * -> *).
Monad m =>
Functor (BehaviorWriterT t w m)
Applicative, Applicative (BehaviorWriterT t w m)
a -> BehaviorWriterT t w m a
Applicative (BehaviorWriterT t w m) =>
(forall a b.
 BehaviorWriterT t w m a
 -> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b)
-> (forall a b.
    BehaviorWriterT t w m a
    -> BehaviorWriterT t w m b -> BehaviorWriterT t w m b)
-> (forall a. a -> BehaviorWriterT t w m a)
-> Monad (BehaviorWriterT t w m)
BehaviorWriterT t w m a
-> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
forall a. a -> BehaviorWriterT t w m a
forall a b.
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
forall a b.
BehaviorWriterT t w m a
-> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b
forall t w (m :: * -> *).
Monad m =>
Applicative (BehaviorWriterT t w m)
forall t w (m :: * -> *) a. Monad m => a -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> BehaviorWriterT t w m a
$creturn :: forall t w (m :: * -> *) a. Monad m => a -> BehaviorWriterT t w m a
>> :: BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
$c>> :: forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m b
>>= :: BehaviorWriterT t w m a
-> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b
$c>>= :: forall t w (m :: * -> *) a b.
Monad m =>
BehaviorWriterT t w m a
-> (a -> BehaviorWriterT t w m b) -> BehaviorWriterT t w m b
$cp1Monad :: forall t w (m :: * -> *).
Monad m =>
Applicative (BehaviorWriterT t w m)
Monad, Monad (BehaviorWriterT t w m)
Monad (BehaviorWriterT t w m) =>
(forall a. IO a -> BehaviorWriterT t w m a)
-> MonadIO (BehaviorWriterT t w m)
IO a -> BehaviorWriterT t w m a
forall a. IO a -> BehaviorWriterT t w m a
forall t w (m :: * -> *).
MonadIO m =>
Monad (BehaviorWriterT t w m)
forall t w (m :: * -> *) a.
MonadIO m =>
IO a -> BehaviorWriterT t w m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> BehaviorWriterT t w m a
$cliftIO :: forall t w (m :: * -> *) a.
MonadIO m =>
IO a -> BehaviorWriterT t w m a
$cp1MonadIO :: forall t w (m :: * -> *).
MonadIO m =>
Monad (BehaviorWriterT t w m)
MonadIO, Monad (BehaviorWriterT t w m)
Monad (BehaviorWriterT t w m) =>
(forall a.
 (a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a)
-> MonadFix (BehaviorWriterT t w m)
(a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
forall a. (a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
forall t w (m :: * -> *).
MonadFix m =>
Monad (BehaviorWriterT t w m)
forall t w (m :: * -> *) a.
MonadFix m =>
(a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
$cmfix :: forall t w (m :: * -> *) a.
MonadFix m =>
(a -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
$cp1MonadFix :: forall t w (m :: * -> *).
MonadFix m =>
Monad (BehaviorWriterT t w m)
MonadFix, MonadIO (BehaviorWriterT t w m)
MonadException (BehaviorWriterT t w m)
(MonadIO (BehaviorWriterT t w m),
 MonadException (BehaviorWriterT t w m)) =>
(forall b.
 ((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
  -> BehaviorWriterT t w m b)
 -> BehaviorWriterT t w m b)
-> MonadAsyncException (BehaviorWriterT t w m)
((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
 -> BehaviorWriterT t w m b)
-> BehaviorWriterT t w m b
forall b.
((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
 -> BehaviorWriterT t w m b)
-> BehaviorWriterT t w m b
forall t w (m :: * -> *).
MonadAsyncException m =>
MonadIO (BehaviorWriterT t w m)
forall t w (m :: * -> *).
MonadAsyncException m =>
MonadException (BehaviorWriterT t w m)
forall t w (m :: * -> *) b.
MonadAsyncException m =>
((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
 -> BehaviorWriterT t w m b)
-> BehaviorWriterT t w m b
forall (m :: * -> *).
(MonadIO m, MonadException m) =>
(forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> MonadAsyncException m
mask :: ((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
 -> BehaviorWriterT t w m b)
-> BehaviorWriterT t w m b
$cmask :: forall t w (m :: * -> *) b.
MonadAsyncException m =>
((forall a. BehaviorWriterT t w m a -> BehaviorWriterT t w m a)
 -> BehaviorWriterT t w m b)
-> BehaviorWriterT t w m b
$cp2MonadAsyncException :: forall t w (m :: * -> *).
MonadAsyncException m =>
MonadException (BehaviorWriterT t w m)
$cp1MonadAsyncException :: forall t w (m :: * -> *).
MonadAsyncException m =>
MonadIO (BehaviorWriterT t w m)
MonadAsyncException, Monad (BehaviorWriterT t w m)
e -> BehaviorWriterT t w m a
Monad (BehaviorWriterT t w m) =>
(forall e a. Exception e => e -> BehaviorWriterT t w m a)
-> (forall e a.
    Exception e =>
    BehaviorWriterT t w m a
    -> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a)
-> (forall a b.
    BehaviorWriterT t w m a
    -> BehaviorWriterT t w m b -> BehaviorWriterT t w m a)
-> MonadException (BehaviorWriterT t w m)
BehaviorWriterT t w m a
-> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall e a. Exception e => e -> BehaviorWriterT t w m a
forall e a.
Exception e =>
BehaviorWriterT t w m a
-> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
forall a b.
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall t w (m :: * -> *).
MonadException m =>
Monad (BehaviorWriterT t w m)
forall t w (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> BehaviorWriterT t w m a
forall t w (m :: * -> *) e a.
(MonadException m, Exception e) =>
BehaviorWriterT t w m a
-> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a b.
MonadException m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
forall (m :: * -> *).
Monad m =>
(forall e a. Exception e => e -> m a)
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> (forall a b. m a -> m b -> m a)
-> MonadException m
finally :: BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
$cfinally :: forall t w (m :: * -> *) a b.
MonadException m =>
BehaviorWriterT t w m a
-> BehaviorWriterT t w m b -> BehaviorWriterT t w m a
catch :: BehaviorWriterT t w m a
-> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
$ccatch :: forall t w (m :: * -> *) e a.
(MonadException m, Exception e) =>
BehaviorWriterT t w m a
-> (e -> BehaviorWriterT t w m a) -> BehaviorWriterT t w m a
throw :: e -> BehaviorWriterT t w m a
$cthrow :: forall t w (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> BehaviorWriterT t w m a
$cp1MonadException :: forall t w (m :: * -> *).
MonadException m =>
Monad (BehaviorWriterT t w m)
MonadException) -- The list is kept in reverse order

-- | Run a 'BehaviorWriterT' action.  The behavior writer output will be provided
-- along with the result of the action.
runBehaviorWriterT :: (Monad m, Reflex t, Monoid w) => BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT :: BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT (BehaviorWriterT a :: StateT [Behavior t w] m a
a) = do
  (result :: a
result, ws :: [Behavior t w]
ws) <- StateT [Behavior t w] m a
-> [Behavior t w] -> m (a, [Behavior t w])
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT [Behavior t w] m a
a []
  (a, Behavior t w) -> m (a, Behavior t w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, [Behavior t w] -> Behavior t w
forall a. Monoid a => [a] -> a
mconcat ([Behavior t w] -> Behavior t w) -> [Behavior t w] -> Behavior t w
forall a b. (a -> b) -> a -> b
$ [Behavior t w] -> [Behavior t w]
forall a. [a] -> [a]
reverse [Behavior t w]
ws)

-- | Map a function over the output of a 'BehaviorWriterT'.
withBehaviorWriterT :: (Monoid w, Monoid w', Reflex t, MonadHold t m)
                   => (w -> w')
                   -> BehaviorWriterT t w m a
                   -> BehaviorWriterT t w' m a
withBehaviorWriterT :: (w -> w') -> BehaviorWriterT t w m a -> BehaviorWriterT t w' m a
withBehaviorWriterT f :: w -> w'
f dw :: BehaviorWriterT t w m a
dw = do
  (r :: a
r, d :: Behavior t w'
d) <- m (a, Behavior t w') -> BehaviorWriterT t w' m (a, Behavior t w')
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (a, Behavior t w') -> BehaviorWriterT t w' m (a, Behavior t w'))
-> m (a, Behavior t w')
-> BehaviorWriterT t w' m (a, Behavior t w')
forall a b. (a -> b) -> a -> b
$ do
    (r :: a
r, d :: Behavior t w
d) <- BehaviorWriterT t w m a -> m (a, Behavior t w)
forall (m :: * -> *) t w a.
(Monad m, Reflex t, Monoid w) =>
BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT BehaviorWriterT t w m a
dw
    let d' :: Behavior t w'
d' = (w -> w') -> Behavior t w -> Behavior t w'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap w -> w'
f Behavior t w
d
    (a, Behavior t w') -> m (a, Behavior t w')
forall (m :: * -> *) a. Monad m => a -> m a
return (a
r, Behavior t w'
d')
  Behavior t w' -> BehaviorWriterT t w' m ()
forall t w (m :: * -> *).
BehaviorWriter t w m =>
Behavior t w -> m ()
tellBehavior Behavior t w'
d
  a -> BehaviorWriterT t w' m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r

deriving instance MonadHold t m => MonadHold t (BehaviorWriterT t w m)
deriving instance MonadSample t m => MonadSample t (BehaviorWriterT t w m)

instance MonadTrans (BehaviorWriterT t w) where
  lift :: m a -> BehaviorWriterT t w m a
lift = StateT [Behavior t w] m a -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a.
StateT [Behavior t w] m a -> BehaviorWriterT t w m a
BehaviorWriterT (StateT [Behavior t w] m a -> BehaviorWriterT t w m a)
-> (m a -> StateT [Behavior t w] m a)
-> m a
-> BehaviorWriterT t w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> StateT [Behavior t w] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance MonadRef m => MonadRef (BehaviorWriterT t w m) where
  type Ref (BehaviorWriterT t w m) = Ref m
  newRef :: a -> BehaviorWriterT t w m (Ref (BehaviorWriterT t w m) a)
newRef = m (Ref m a) -> BehaviorWriterT t w m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> BehaviorWriterT t w m (Ref m a))
-> (a -> m (Ref m a)) -> a -> BehaviorWriterT t w m (Ref m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef
  readRef :: Ref (BehaviorWriterT t w m) a -> BehaviorWriterT t w m a
readRef = m a -> BehaviorWriterT t w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> BehaviorWriterT t w m a)
-> (Ref m a -> m a) -> Ref m a -> BehaviorWriterT t w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef
  writeRef :: Ref (BehaviorWriterT t w m) a -> a -> BehaviorWriterT t w m ()
writeRef r :: Ref (BehaviorWriterT t w m) a
r = m () -> BehaviorWriterT t w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> BehaviorWriterT t w m ())
-> (a -> m ()) -> a -> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref m a
Ref (BehaviorWriterT t w m) a
r

instance MonadAtomicRef m => MonadAtomicRef (BehaviorWriterT t w m) where
  atomicModifyRef :: Ref (BehaviorWriterT t w m) a
-> (a -> (a, b)) -> BehaviorWriterT t w m b
atomicModifyRef r :: Ref (BehaviorWriterT t w m) a
r = m b -> BehaviorWriterT t w m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> BehaviorWriterT t w m b)
-> ((a -> (a, b)) -> m b)
-> (a -> (a, b))
-> BehaviorWriterT t w m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef Ref m a
Ref (BehaviorWriterT t w m) a
r

instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (BehaviorWriterT t w m) where
  newEventWithTrigger :: (EventTrigger t a -> IO (IO ()))
-> BehaviorWriterT t w m (Event t a)
newEventWithTrigger = m (Event t a) -> BehaviorWriterT t w m (Event t a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> BehaviorWriterT t w m (Event t a))
-> ((EventTrigger t a -> IO (IO ())) -> m (Event t a))
-> (EventTrigger t a -> IO (IO ()))
-> BehaviorWriterT t w m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EventTrigger t a -> IO (IO ())) -> m (Event t a)
forall t (m :: * -> *) a.
MonadReflexCreateTrigger t m =>
(EventTrigger t a -> IO (IO ())) -> m (Event t a)
newEventWithTrigger
  newFanEventWithTrigger :: (forall a. k a -> EventTrigger t a -> IO (IO ()))
-> BehaviorWriterT t w m (EventSelector t k)
newFanEventWithTrigger f :: forall a. k a -> EventTrigger t a -> IO (IO ())
f = m (EventSelector t k) -> BehaviorWriterT t w m (EventSelector t k)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (EventSelector t k)
 -> BehaviorWriterT t w m (EventSelector t k))
-> m (EventSelector t k)
-> BehaviorWriterT t w m (EventSelector t k)
forall a b. (a -> b) -> a -> b
$ (forall a. k a -> EventTrigger t a -> IO (IO ()))
-> m (EventSelector t k)
forall t (m :: * -> *) (k :: * -> *).
(MonadReflexCreateTrigger t m, GCompare k) =>
(forall a. k a -> EventTrigger t a -> IO (IO ()))
-> m (EventSelector t k)
newFanEventWithTrigger forall a. k a -> EventTrigger t a -> IO (IO ())
f

instance (Monad m, Monoid w, Reflex t) => BehaviorWriter t w (BehaviorWriterT t w m) where
  tellBehavior :: Behavior t w -> BehaviorWriterT t w m ()
tellBehavior w :: Behavior t w
w = StateT [Behavior t w] m () -> BehaviorWriterT t w m ()
forall t w (m :: * -> *) a.
StateT [Behavior t w] m a -> BehaviorWriterT t w m a
BehaviorWriterT (StateT [Behavior t w] m () -> BehaviorWriterT t w m ())
-> StateT [Behavior t w] m () -> BehaviorWriterT t w m ()
forall a b. (a -> b) -> a -> b
$ ([Behavior t w] -> [Behavior t w]) -> StateT [Behavior t w] m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (Behavior t w
w Behavior t w -> [Behavior t w] -> [Behavior t w]
forall a. a -> [a] -> [a]
:)

instance MonadReader r m => MonadReader r (BehaviorWriterT t w m) where
  ask :: BehaviorWriterT t w m r
ask = m r -> BehaviorWriterT t w m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m r
forall r (m :: * -> *). MonadReader r m => m r
ask
  local :: (r -> r) -> BehaviorWriterT t w m a -> BehaviorWriterT t w m a
local f :: r -> r
f (BehaviorWriterT a :: StateT [Behavior t w] m a
a) = StateT [Behavior t w] m a -> BehaviorWriterT t w m a
forall t w (m :: * -> *) a.
StateT [Behavior t w] m a -> BehaviorWriterT t w m a
BehaviorWriterT (StateT [Behavior t w] m a -> BehaviorWriterT t w m a)
-> StateT [Behavior t w] m a -> BehaviorWriterT t w m a
forall a b. (a -> b) -> a -> b
$ (m (a, [Behavior t w]) -> m (a, [Behavior t w]))
-> StateT [Behavior t w] m a -> StateT [Behavior t w] m a
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
mapStateT ((r -> r) -> m (a, [Behavior t w]) -> m (a, [Behavior t w])
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f) StateT [Behavior t w] m a
a
  reader :: (r -> a) -> BehaviorWriterT t w m a
reader = m a -> BehaviorWriterT t w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> BehaviorWriterT t w m a)
-> ((r -> a) -> m a) -> (r -> a) -> BehaviorWriterT t w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader

instance PerformEvent t m => PerformEvent t (BehaviorWriterT t w m) where
  type Performable (BehaviorWriterT t w m) = Performable m
  performEvent_ :: Event t (Performable (BehaviorWriterT t w m) ())
-> BehaviorWriterT t w m ()
performEvent_ = m () -> BehaviorWriterT t w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> BehaviorWriterT t w m ())
-> (Event t (Performable m ()) -> m ())
-> Event t (Performable m ())
-> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t (Performable m ()) -> m ()
forall t (m :: * -> *).
PerformEvent t m =>
Event t (Performable m ()) -> m ()
performEvent_
  performEvent :: Event t (Performable (BehaviorWriterT t w m) a)
-> BehaviorWriterT t w m (Event t a)
performEvent = m (Event t a) -> BehaviorWriterT t w m (Event t a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> BehaviorWriterT t w m (Event t a))
-> (Event t (Performable m a) -> m (Event t a))
-> Event t (Performable m a)
-> BehaviorWriterT t w m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t (Performable m a) -> m (Event t a)
forall t (m :: * -> *) a.
PerformEvent t m =>
Event t (Performable m a) -> m (Event t a)
performEvent

instance TriggerEvent t m => TriggerEvent t (BehaviorWriterT t w m) where
  newTriggerEvent :: BehaviorWriterT t w m (Event t a, a -> IO ())
newTriggerEvent = m (Event t a, a -> IO ())
-> BehaviorWriterT t w m (Event t a, a -> IO ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t a, a -> IO ())
forall t (m :: * -> *) a.
TriggerEvent t m =>
m (Event t a, a -> IO ())
newTriggerEvent
  newTriggerEventWithOnComplete :: BehaviorWriterT t w m (Event t a, a -> IO () -> IO ())
newTriggerEventWithOnComplete = m (Event t a, a -> IO () -> IO ())
-> BehaviorWriterT t w m (Event t a, a -> IO () -> IO ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t a, a -> IO () -> IO ())
forall t (m :: * -> *) a.
TriggerEvent t m =>
m (Event t a, a -> IO () -> IO ())
newTriggerEventWithOnComplete
  newEventWithLazyTriggerWithOnComplete :: ((a -> IO () -> IO ()) -> IO (IO ()))
-> BehaviorWriterT t w m (Event t a)
newEventWithLazyTriggerWithOnComplete = m (Event t a) -> BehaviorWriterT t w m (Event t a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> BehaviorWriterT t w m (Event t a))
-> (((a -> IO () -> IO ()) -> IO (IO ())) -> m (Event t a))
-> ((a -> IO () -> IO ()) -> IO (IO ()))
-> BehaviorWriterT t w m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a -> IO () -> IO ()) -> IO (IO ())) -> m (Event t a)
forall t (m :: * -> *) a.
TriggerEvent t m =>
((a -> IO () -> IO ()) -> IO (IO ())) -> m (Event t a)
newEventWithLazyTriggerWithOnComplete

instance PostBuild t m => PostBuild t (BehaviorWriterT t w m) where
  getPostBuild :: BehaviorWriterT t w m (Event t ())
getPostBuild = m (Event t ()) -> BehaviorWriterT t w m (Event t ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall t (m :: * -> *). PostBuild t m => m (Event t ())
getPostBuild

instance MonadState s m => MonadState s (BehaviorWriterT t w m) where
  get :: BehaviorWriterT t w m s
get = m s -> BehaviorWriterT t w m s
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m s
forall s (m :: * -> *). MonadState s m => m s
get
  put :: s -> BehaviorWriterT t w m ()
put = m () -> BehaviorWriterT t w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> BehaviorWriterT t w m ())
-> (s -> m ()) -> s -> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put

instance Requester t m => Requester t (BehaviorWriterT t w m) where
  type Request (BehaviorWriterT t w m) = Request m
  type Response (BehaviorWriterT t w m) = Response m
  requesting :: Event t (Request (BehaviorWriterT t w m) a)
-> BehaviorWriterT
     t w m (Event t (Response (BehaviorWriterT t w m) a))
requesting = m (Event t (Response m a))
-> BehaviorWriterT t w m (Event t (Response m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t (Response m a))
 -> BehaviorWriterT t w m (Event t (Response m a)))
-> (Event t (Request m a) -> m (Event t (Response m a)))
-> Event t (Request m a)
-> BehaviorWriterT t w m (Event t (Response m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t (Request m a) -> m (Event t (Response m a))
forall t (m :: * -> *) a.
Requester t m =>
Event t (Request m a) -> m (Event t (Response m a))
requesting
  requesting_ :: Event t (Request (BehaviorWriterT t w m) a)
-> BehaviorWriterT t w m ()
requesting_ = m () -> BehaviorWriterT t w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> BehaviorWriterT t w m ())
-> (Event t (Request m a) -> m ())
-> Event t (Request m a)
-> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t (Request m a) -> m ()
forall t (m :: * -> *) a.
Requester t m =>
Event t (Request m a) -> m ()
requesting_

instance (MonadQuery t q m, Monad m) => MonadQuery t q (BehaviorWriterT t w m) where
  tellQueryIncremental :: Incremental t (AdditivePatch q) -> BehaviorWriterT t w m ()
tellQueryIncremental = m () -> BehaviorWriterT t w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> BehaviorWriterT t w m ())
-> (Incremental t (AdditivePatch q) -> m ())
-> Incremental t (AdditivePatch q)
-> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Incremental t (AdditivePatch q) -> m ()
forall t q (m :: * -> *).
MonadQuery t q m =>
Incremental t (AdditivePatch q) -> m ()
tellQueryIncremental
  askQueryResult :: BehaviorWriterT t w m (Dynamic t (QueryResult q))
askQueryResult = m (Dynamic t (QueryResult q))
-> BehaviorWriterT t w m (Dynamic t (QueryResult q))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Dynamic t (QueryResult q))
forall t q (m :: * -> *).
MonadQuery t q m =>
m (Dynamic t (QueryResult q))
askQueryResult
  queryIncremental :: Incremental t (AdditivePatch q)
-> BehaviorWriterT t w m (Dynamic t (QueryResult q))
queryIncremental = m (Dynamic t (QueryResult q))
-> BehaviorWriterT t w m (Dynamic t (QueryResult q))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t (QueryResult q))
 -> BehaviorWriterT t w m (Dynamic t (QueryResult q)))
-> (Incremental t (AdditivePatch q)
    -> m (Dynamic t (QueryResult q)))
-> Incremental t (AdditivePatch q)
-> BehaviorWriterT t w m (Dynamic t (QueryResult q))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Incremental t (AdditivePatch q) -> m (Dynamic t (QueryResult q))
forall t q (m :: * -> *).
MonadQuery t q m =>
Incremental t (AdditivePatch q) -> m (Dynamic t (QueryResult q))
queryIncremental

instance (Adjustable t m, Monoid w, MonadHold t m, Reflex t) => Adjustable t (BehaviorWriterT t w m) where
  runWithReplace :: BehaviorWriterT t w m a
-> Event t (BehaviorWriterT t w m b)
-> BehaviorWriterT t w m (a, Event t b)
runWithReplace a0 :: BehaviorWriterT t w m a
a0 a' :: Event t (BehaviorWriterT t w m b)
a' = do
    (result0 :: (a, Behavior t w)
result0, result' :: Event t (b, Behavior t w)
result') <- m ((a, Behavior t w), Event t (b, Behavior t w))
-> BehaviorWriterT
     t w m ((a, Behavior t w), Event t (b, Behavior t w))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ((a, Behavior t w), Event t (b, Behavior t w))
 -> BehaviorWriterT
      t w m ((a, Behavior t w), Event t (b, Behavior t w)))
-> m ((a, Behavior t w), Event t (b, Behavior t w))
-> BehaviorWriterT
     t w m ((a, Behavior t w), Event t (b, Behavior t w))
forall a b. (a -> b) -> a -> b
$ m (a, Behavior t w)
-> Event t (m (b, Behavior t w))
-> m ((a, Behavior t w), Event t (b, Behavior t w))
forall t (m :: * -> *) a b.
Adjustable t m =>
m a -> Event t (m b) -> m (a, Event t b)
runWithReplace (BehaviorWriterT t w m a -> m (a, Behavior t w)
forall (m :: * -> *) t w a.
(Monad m, Reflex t, Monoid w) =>
BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT BehaviorWriterT t w m a
a0) (Event t (m (b, Behavior t w))
 -> m ((a, Behavior t w), Event t (b, Behavior t w)))
-> Event t (m (b, Behavior t w))
-> m ((a, Behavior t w), Event t (b, Behavior t w))
forall a b. (a -> b) -> a -> b
$ BehaviorWriterT t w m b -> m (b, Behavior t w)
forall (m :: * -> *) t w a.
(Monad m, Reflex t, Monoid w) =>
BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT (BehaviorWriterT t w m b -> m (b, Behavior t w))
-> Event t (BehaviorWriterT t w m b)
-> Event t (m (b, Behavior t w))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t (BehaviorWriterT t w m b)
a'
    Behavior t w -> BehaviorWriterT t w m ()
forall t w (m :: * -> *).
BehaviorWriter t w m =>
Behavior t w -> m ()
tellBehavior (Behavior t w -> BehaviorWriterT t w m ())
-> (Behavior t (Behavior t w) -> Behavior t w)
-> Behavior t (Behavior t w)
-> BehaviorWriterT t w m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t (Behavior t w) -> Behavior t w
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Behavior t (Behavior t w) -> BehaviorWriterT t w m ())
-> BehaviorWriterT t w m (Behavior t (Behavior t w))
-> BehaviorWriterT t w m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Behavior t w
-> Event t (Behavior t w)
-> BehaviorWriterT t w m (Behavior t (Behavior t w))
forall k (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold ((a, Behavior t w) -> Behavior t w
forall a b. (a, b) -> b
snd (a, Behavior t w)
result0) ((b, Behavior t w) -> Behavior t w
forall a b. (a, b) -> b
snd ((b, Behavior t w) -> Behavior t w)
-> Event t (b, Behavior t w) -> Event t (Behavior t w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t (b, Behavior t w)
result')
    (a, Event t b) -> BehaviorWriterT t w m (a, Event t b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, Behavior t w) -> a
forall a b. (a, b) -> a
fst (a, Behavior t w)
result0, (b, Behavior t w) -> b
forall a b. (a, b) -> a
fst ((b, Behavior t w) -> b) -> Event t (b, Behavior t w) -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t (b, Behavior t w)
result')
  traverseIntMapWithKeyWithAdjust :: (Key -> v -> BehaviorWriterT t w m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> BehaviorWriterT t w m (IntMap v', Event t (PatchIntMap v'))
traverseIntMapWithKeyWithAdjust = ((Key -> v -> m (v', Behavior t w))
 -> IntMap v
 -> Event t (PatchIntMap v)
 -> m (IntMap (v', Behavior t w),
       Event t (PatchIntMap (v', Behavior t w))))
-> (Key -> v -> BehaviorWriterT t w m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> BehaviorWriterT t w m (IntMap v', Event t (PatchIntMap v'))
forall t w v' (p :: * -> *) (p' :: * -> *) v (m :: * -> *).
(PatchTarget (p' (Behavior t w)) ~ IntMap (Behavior t w),
 Patch (p' (Behavior t w)), Monoid w, Reflex t, MonadHold t m,
 Functor p, p ~ p') =>
((Key -> v -> m (v', Behavior t w))
 -> IntMap v
 -> Event t (p v)
 -> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w))))
-> (Key -> v -> BehaviorWriterT t w m v')
-> IntMap v
-> Event t (p v)
-> BehaviorWriterT t w m (IntMap v', Event t (p v'))
traverseIntMapWithKeyWithAdjustImpl (Key -> v -> m (v', Behavior t w))
-> IntMap v
-> Event t (PatchIntMap v)
-> m (IntMap (v', Behavior t w),
      Event t (PatchIntMap (v', Behavior t w)))
forall t (m :: * -> *) v v'.
Adjustable t m =>
(Key -> v -> m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> m (IntMap v', Event t (PatchIntMap v'))
traverseIntMapWithKeyWithAdjust
  traverseDMapWithKeyWithAdjustWithMove :: (forall a. k a -> v a -> BehaviorWriterT t w m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> BehaviorWriterT
     t w m (DMap k v', Event t (PatchDMapWithMove k v'))
traverseDMapWithKeyWithAdjustWithMove = ((forall a.
  k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
 -> DMap k v
 -> Event t (PatchDMapWithMove k v)
 -> m (DMap k (BehaviorWriterTLoweredResult t w v'),
       Event
         t (PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v'))))
-> ((forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
    -> PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v')
    -> PatchDMapWithMove k v')
-> ((forall a.
     BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
    -> PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v')
    -> PatchMapWithMove (Some k) (Behavior t w))
-> (forall a. k a -> v a -> BehaviorWriterT t w m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> BehaviorWriterT
     t w m (DMap k v', Event t (PatchDMapWithMove k v'))
forall t w (k :: * -> *) (v' :: * -> *)
       (p :: (* -> *) -> (* -> *) -> *) (p' :: * -> * -> *) (v :: * -> *)
       (m :: * -> *).
(PatchTarget (p' (Some k) (Behavior t w))
 ~ Map (Some k) (Behavior t w),
 Patch (p' (Some k) (Behavior t w)), Monoid w, Reflex t,
 MonadHold t m) =>
((forall a.
  k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
 -> DMap k v
 -> Event t (p k v)
 -> m (DMap k (BehaviorWriterTLoweredResult t w v'),
       Event t (p k (BehaviorWriterTLoweredResult t w v'))))
-> ((forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
    -> p k (BehaviorWriterTLoweredResult t w v') -> p k v')
-> ((forall a.
     BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
    -> p k (BehaviorWriterTLoweredResult t w v')
    -> p' (Some k) (Behavior t w))
-> (forall a. k a -> v a -> BehaviorWriterT t w m (v' a))
-> DMap k v
-> Event t (p k v)
-> BehaviorWriterT t w m (DMap k v', Event t (p k v'))
traverseDMapWithKeyWithAdjustImpl (forall a. k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> m (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event
        t (PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v')))
forall t (m :: * -> *) (k :: * -> *) (v :: * -> *) (v' :: * -> *).
(Adjustable t m, GCompare k) =>
(forall a. k a -> v a -> m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> m (DMap k v', Event t (PatchDMapWithMove k v'))
traverseDMapWithKeyWithAdjustWithMove (forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
-> PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v')
-> PatchDMapWithMove k v'
forall k1 (k2 :: k1 -> *) (v :: k1 -> *) (v' :: k1 -> *).
(forall (a :: k1). v a -> v' a)
-> PatchDMapWithMove k2 v -> PatchDMapWithMove k2 v'
mapPatchDMapWithMove (forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
-> PatchDMapWithMove k (BehaviorWriterTLoweredResult t w v')
-> PatchMapWithMove (Some k) (Behavior t w)
forall k1 (k2 :: k1 -> *) (v :: k1 -> *) v'.
(forall (a :: k1). v a -> v')
-> PatchDMapWithMove k2 v -> PatchMapWithMove (Some k2) v'
weakenPatchDMapWithMoveWith

traverseIntMapWithKeyWithAdjustImpl
  :: forall t w v' p p' v m.
     ( PatchTarget (p' (Behavior t w)) ~ IntMap (Behavior t w)
     , Patch (p' (Behavior t w))
     , Monoid w
     , Reflex t
     , MonadHold t m
     , Functor p
     , p ~ p'
     )
  => (   (IntMap.Key -> v -> m (v', Behavior t w))
      -> IntMap v
      -> Event t (p v)
      -> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
     )
  -> (IntMap.Key -> v -> BehaviorWriterT t w m v')
  -> IntMap v
  -> Event t (p v)
  -> BehaviorWriterT t w m (IntMap v', Event t (p v'))
traverseIntMapWithKeyWithAdjustImpl :: ((Key -> v -> m (v', Behavior t w))
 -> IntMap v
 -> Event t (p v)
 -> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w))))
-> (Key -> v -> BehaviorWriterT t w m v')
-> IntMap v
-> Event t (p v)
-> BehaviorWriterT t w m (IntMap v', Event t (p v'))
traverseIntMapWithKeyWithAdjustImpl base :: (Key -> v -> m (v', Behavior t w))
-> IntMap v
-> Event t (p v)
-> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
base f :: Key -> v -> BehaviorWriterT t w m v'
f (IntMap v
dm0 :: IntMap v) dm' :: Event t (p v)
dm' = do
  (result0 :: IntMap (v', Behavior t w)
result0, result' :: Event t (p (v', Behavior t w))
result') <- m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
-> BehaviorWriterT
     t w m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
 -> BehaviorWriterT
      t w m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w))))
-> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
-> BehaviorWriterT
     t w m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
forall a b. (a -> b) -> a -> b
$ (Key -> v -> m (v', Behavior t w))
-> IntMap v
-> Event t (p v)
-> m (IntMap (v', Behavior t w), Event t (p (v', Behavior t w)))
base (\k :: Key
k v :: v
v -> BehaviorWriterT t w m v' -> m (v', Behavior t w)
forall (m :: * -> *) t w a.
(Monad m, Reflex t, Monoid w) =>
BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT (BehaviorWriterT t w m v' -> m (v', Behavior t w))
-> BehaviorWriterT t w m v' -> m (v', Behavior t w)
forall a b. (a -> b) -> a -> b
$ Key -> v -> BehaviorWriterT t w m v'
f Key
k v
v) IntMap v
dm0 Event t (p v)
dm'
  let liftedResult0 :: IntMap v'
liftedResult0 = ((v', Behavior t w) -> v')
-> IntMap (v', Behavior t w) -> IntMap v'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v', Behavior t w) -> v'
forall a b. (a, b) -> a
fst IntMap (v', Behavior t w)
result0
      liftedResult' :: Event t (p v')
liftedResult' = (p (v', Behavior t w) -> p v')
-> Event t (p (v', Behavior t w)) -> Event t (p v')
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((v', Behavior t w) -> v') -> p (v', Behavior t w) -> p v'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v', Behavior t w) -> v'
forall a b. (a, b) -> a
fst) Event t (p (v', Behavior t w))
result'
      liftedWritten0 :: IntMap (Behavior t w)
      liftedWritten0 :: IntMap (Behavior t w)
liftedWritten0 = ((v', Behavior t w) -> Behavior t w)
-> IntMap (v', Behavior t w) -> IntMap (Behavior t w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v', Behavior t w) -> Behavior t w
forall a b. (a, b) -> b
snd IntMap (v', Behavior t w)
result0
      liftedWritten' :: Event t (p (Behavior t w))
liftedWritten' = (p (v', Behavior t w) -> p (Behavior t w))
-> Event t (p (v', Behavior t w)) -> Event t (p (Behavior t w))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((v', Behavior t w) -> Behavior t w)
-> p (v', Behavior t w) -> p (Behavior t w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v', Behavior t w) -> Behavior t w
forall a b. (a, b) -> b
snd) Event t (p (v', Behavior t w))
result'
  Incremental t (p (Behavior t w))
i <- PatchTarget (p (Behavior t w))
-> Event t (p (Behavior t w))
-> BehaviorWriterT t w m (Incremental t (p (Behavior t w)))
forall k (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental IntMap (Behavior t w)
PatchTarget (p (Behavior t w))
liftedWritten0 Event t (p (Behavior t w))
liftedWritten'
  Behavior t w -> BehaviorWriterT t w m ()
forall t w (m :: * -> *).
BehaviorWriter t w m =>
Behavior t w -> m ()
tellBehavior (Behavior t w -> BehaviorWriterT t w m ())
-> Behavior t w -> BehaviorWriterT t w m ()
forall a b. (a -> b) -> a -> b
$ PullM t w -> Behavior t w
forall k (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t w -> Behavior t w) -> PullM t w -> Behavior t w
forall a b. (a -> b) -> a -> b
$ do
    IntMap (Behavior t w)
m <- Behavior t (IntMap (Behavior t w))
-> PullM t (IntMap (Behavior t w))
forall k (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (IntMap (Behavior t w))
 -> PullM t (IntMap (Behavior t w)))
-> Behavior t (IntMap (Behavior t w))
-> PullM t (IntMap (Behavior t w))
forall a b. (a -> b) -> a -> b
$ Incremental t (p (Behavior t w))
-> Behavior t (PatchTarget (p (Behavior t w)))
forall k (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Behavior t (PatchTarget p)
currentIncremental Incremental t (p (Behavior t w))
i
    [w] -> w
forall a. Monoid a => [a] -> a
mconcat ([w] -> w) -> (IntMap w -> [w]) -> IntMap w -> w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap w -> [w]
forall a. IntMap a -> [a]
IntMap.elems (IntMap w -> w) -> PullM t (IntMap w) -> PullM t w
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Behavior t w -> PullM t w)
-> IntMap (Behavior t w) -> PullM t (IntMap w)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Behavior t w -> PullM t w
forall k (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample IntMap (Behavior t w)
m
  (IntMap v', Event t (p v'))
-> BehaviorWriterT t w m (IntMap v', Event t (p v'))
forall (m :: * -> *) a. Monad m => a -> m a
return (IntMap v'
liftedResult0, Event t (p v')
liftedResult')

newtype BehaviorWriterTLoweredResult t w v a = BehaviorWriterTLoweredResult (v a, Behavior t w)

traverseDMapWithKeyWithAdjustImpl
  :: forall t w k v' p p' v m.
     ( PatchTarget (p' (Some k) (Behavior t w)) ~ Map (Some k) (Behavior t w)
     , Patch (p' (Some k) (Behavior t w))
     , Monoid w
     , Reflex t
     , MonadHold t m
     )
  => (   (forall a. k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
      -> DMap k v
      -> Event t (p k v)
      -> m (DMap k (BehaviorWriterTLoweredResult t w v'), Event t (p k (BehaviorWriterTLoweredResult t w v')))
     )
  -> ((forall a. BehaviorWriterTLoweredResult t w v' a -> v' a) -> p k (BehaviorWriterTLoweredResult t w v') -> p k v')
  -> ((forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w) -> p k (BehaviorWriterTLoweredResult t w v') -> p' (Some k) (Behavior t w))
  -> (forall a. k a -> v a -> BehaviorWriterT t w m (v' a))
  -> DMap k v
  -> Event t (p k v)
  -> BehaviorWriterT t w m (DMap k v', Event t (p k v'))
traverseDMapWithKeyWithAdjustImpl :: ((forall a.
  k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
 -> DMap k v
 -> Event t (p k v)
 -> m (DMap k (BehaviorWriterTLoweredResult t w v'),
       Event t (p k (BehaviorWriterTLoweredResult t w v'))))
-> ((forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
    -> p k (BehaviorWriterTLoweredResult t w v') -> p k v')
-> ((forall a.
     BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
    -> p k (BehaviorWriterTLoweredResult t w v')
    -> p' (Some k) (Behavior t w))
-> (forall a. k a -> v a -> BehaviorWriterT t w m (v' a))
-> DMap k v
-> Event t (p k v)
-> BehaviorWriterT t w m (DMap k v', Event t (p k v'))
traverseDMapWithKeyWithAdjustImpl base :: (forall a. k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
-> DMap k v
-> Event t (p k v)
-> m (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event t (p k (BehaviorWriterTLoweredResult t w v')))
base mapPatch :: (forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
-> p k (BehaviorWriterTLoweredResult t w v') -> p k v'
mapPatch weakenPatchWith :: (forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
-> p k (BehaviorWriterTLoweredResult t w v')
-> p' (Some k) (Behavior t w)
weakenPatchWith f :: forall a. k a -> v a -> BehaviorWriterT t w m (v' a)
f (DMap k v
dm0 :: DMap k v) dm' :: Event t (p k v)
dm' = do
  (result0 :: DMap k (BehaviorWriterTLoweredResult t w v')
result0, result' :: Event t (p k (BehaviorWriterTLoweredResult t w v'))
result') <- m (DMap k (BehaviorWriterTLoweredResult t w v'),
   Event t (p k (BehaviorWriterTLoweredResult t w v')))
-> BehaviorWriterT
     t
     w
     m
     (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event t (p k (BehaviorWriterTLoweredResult t w v')))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (DMap k (BehaviorWriterTLoweredResult t w v'),
    Event t (p k (BehaviorWriterTLoweredResult t w v')))
 -> BehaviorWriterT
      t
      w
      m
      (DMap k (BehaviorWriterTLoweredResult t w v'),
       Event t (p k (BehaviorWriterTLoweredResult t w v'))))
-> m (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event t (p k (BehaviorWriterTLoweredResult t w v')))
-> BehaviorWriterT
     t
     w
     m
     (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event t (p k (BehaviorWriterTLoweredResult t w v')))
forall a b. (a -> b) -> a -> b
$ (forall a. k a -> v a -> m (BehaviorWriterTLoweredResult t w v' a))
-> DMap k v
-> Event t (p k v)
-> m (DMap k (BehaviorWriterTLoweredResult t w v'),
      Event t (p k (BehaviorWriterTLoweredResult t w v')))
base (\k :: k a
k v :: v a
v -> ((v' a, Behavior t w) -> BehaviorWriterTLoweredResult t w v' a)
-> m (v' a, Behavior t w)
-> m (BehaviorWriterTLoweredResult t w v' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v' a, Behavior t w) -> BehaviorWriterTLoweredResult t w v' a
forall t w (v :: * -> *) a.
(v a, Behavior t w) -> BehaviorWriterTLoweredResult t w v a
BehaviorWriterTLoweredResult (m (v' a, Behavior t w)
 -> m (BehaviorWriterTLoweredResult t w v' a))
-> m (v' a, Behavior t w)
-> m (BehaviorWriterTLoweredResult t w v' a)
forall a b. (a -> b) -> a -> b
$ BehaviorWriterT t w m (v' a) -> m (v' a, Behavior t w)
forall (m :: * -> *) t w a.
(Monad m, Reflex t, Monoid w) =>
BehaviorWriterT t w m a -> m (a, Behavior t w)
runBehaviorWriterT (BehaviorWriterT t w m (v' a) -> m (v' a, Behavior t w))
-> BehaviorWriterT t w m (v' a) -> m (v' a, Behavior t w)
forall a b. (a -> b) -> a -> b
$ k a -> v a -> BehaviorWriterT t w m (v' a)
forall a. k a -> v a -> BehaviorWriterT t w m (v' a)
f k a
k v a
v) DMap k v
dm0 Event t (p k v)
dm'
  let getValue :: BehaviorWriterTLoweredResult t w v a -> v a
getValue (BehaviorWriterTLoweredResult (v :: v a
v, _)) = v a
v
      getWritten :: BehaviorWriterTLoweredResult t w v a -> Behavior t w
getWritten (BehaviorWriterTLoweredResult (_, w :: Behavior t w
w)) = Behavior t w
w
      liftedResult0 :: DMap k v'
liftedResult0 = (forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
-> DMap k (BehaviorWriterTLoweredResult t w v') -> DMap k v'
forall k1 (f :: k1 -> *) (g :: k1 -> *) (k2 :: k1 -> *).
(forall (v :: k1). f v -> g v) -> DMap k2 f -> DMap k2 g
DMap.map forall a. BehaviorWriterTLoweredResult t w v' a -> v' a
forall t w (v :: * -> *) a.
BehaviorWriterTLoweredResult t w v a -> v a
getValue DMap k (BehaviorWriterTLoweredResult t w v')
result0
      liftedResult' :: Event t (p k v')
liftedResult' = Event t (p k (BehaviorWriterTLoweredResult t w v'))
-> (p k (BehaviorWriterTLoweredResult t w v') -> p k v')
-> Event t (p k v')
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t (p k (BehaviorWriterTLoweredResult t w v'))
result' ((p k (BehaviorWriterTLoweredResult t w v') -> p k v')
 -> Event t (p k v'))
-> (p k (BehaviorWriterTLoweredResult t w v') -> p k v')
-> Event t (p k v')
forall a b. (a -> b) -> a -> b
$ (forall a. BehaviorWriterTLoweredResult t w v' a -> v' a)
-> p k (BehaviorWriterTLoweredResult t w v') -> p k v'
mapPatch forall a. BehaviorWriterTLoweredResult t w v' a -> v' a
forall t w (v :: * -> *) a.
BehaviorWriterTLoweredResult t w v a -> v a
getValue
      liftedWritten0 :: Map (Some k) (Behavior t w)
      liftedWritten0 :: Map (Some k) (Behavior t w)
liftedWritten0 = (forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
-> DMap k (BehaviorWriterTLoweredResult t w v')
-> Map (Some k) (Behavior t w)
forall k1 (v :: k1 -> *) v' (k2 :: k1 -> *).
(forall (a :: k1). v a -> v') -> DMap k2 v -> Map (Some k2) v'
weakenDMapWith forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w
forall t w (v :: * -> *) a.
BehaviorWriterTLoweredResult t w v a -> Behavior t w
getWritten DMap k (BehaviorWriterTLoweredResult t w v')
result0
      liftedWritten' :: Event t (p' (Some k) (Behavior t w))
liftedWritten' = Event t (p k (BehaviorWriterTLoweredResult t w v'))
-> (p k (BehaviorWriterTLoweredResult t w v')
    -> p' (Some k) (Behavior t w))
-> Event t (p' (Some k) (Behavior t w))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t (p k (BehaviorWriterTLoweredResult t w v'))
result' ((p k (BehaviorWriterTLoweredResult t w v')
  -> p' (Some k) (Behavior t w))
 -> Event t (p' (Some k) (Behavior t w)))
-> (p k (BehaviorWriterTLoweredResult t w v')
    -> p' (Some k) (Behavior t w))
-> Event t (p' (Some k) (Behavior t w))
forall a b. (a -> b) -> a -> b
$ (forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w)
-> p k (BehaviorWriterTLoweredResult t w v')
-> p' (Some k) (Behavior t w)
weakenPatchWith forall a. BehaviorWriterTLoweredResult t w v' a -> Behavior t w
forall t w (v :: * -> *) a.
BehaviorWriterTLoweredResult t w v a -> Behavior t w
getWritten
  Incremental t (p' (Some k) (Behavior t w))
i <- PatchTarget (p' (Some k) (Behavior t w))
-> Event t (p' (Some k) (Behavior t w))
-> BehaviorWriterT
     t w m (Incremental t (p' (Some k) (Behavior t w)))
forall k (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental Map (Some k) (Behavior t w)
PatchTarget (p' (Some k) (Behavior t w))
liftedWritten0 Event t (p' (Some k) (Behavior t w))
liftedWritten'
  Behavior t w -> BehaviorWriterT t w m ()
forall t w (m :: * -> *).
BehaviorWriter t w m =>
Behavior t w -> m ()
tellBehavior (Behavior t w -> BehaviorWriterT t w m ())
-> Behavior t w -> BehaviorWriterT t w m ()
forall a b. (a -> b) -> a -> b
$ PullM t w -> Behavior t w
forall k (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t w -> Behavior t w) -> PullM t w -> Behavior t w
forall a b. (a -> b) -> a -> b
$ do
    Map (Some k) (Behavior t w)
m <- Behavior t (Map (Some k) (Behavior t w))
-> PullM t (Map (Some k) (Behavior t w))
forall k (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (Map (Some k) (Behavior t w))
 -> PullM t (Map (Some k) (Behavior t w)))
-> Behavior t (Map (Some k) (Behavior t w))
-> PullM t (Map (Some k) (Behavior t w))
forall a b. (a -> b) -> a -> b
$ Incremental t (p' (Some k) (Behavior t w))
-> Behavior t (PatchTarget (p' (Some k) (Behavior t w)))
forall k (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Behavior t (PatchTarget p)
currentIncremental Incremental t (p' (Some k) (Behavior t w))
i
    [w] -> w
forall a. Monoid a => [a] -> a
mconcat ([w] -> w) -> (Map (Some k) w -> [w]) -> Map (Some k) w -> w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map (Some k) w -> [w]
forall k a. Map k a -> [a]
Map.elems (Map (Some k) w -> w) -> PullM t (Map (Some k) w) -> PullM t w
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Behavior t w -> PullM t w)
-> Map (Some k) (Behavior t w) -> PullM t (Map (Some k) w)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Behavior t w -> PullM t w
forall k (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Map (Some k) (Behavior t w)
m
  (DMap k v', Event t (p k v'))
-> BehaviorWriterT t w m (DMap k v', Event t (p k v'))
forall (m :: * -> *) a. Monad m => a -> m a
return (DMap k v'
liftedResult0, Event t (p k v')
liftedResult')