{-# language CPP #-}

{-----------------------------------------------------------------------------
    reactive-banana
------------------------------------------------------------------------------}
module Reactive.Banana.Types (
    -- | Primitive types.
    Event(..), Behavior(..),
    Moment(..), MomentIO(..), MonadMoment(..),
    Future(..),
    ) where

import Control.Applicative
import Control.Monad.IO.Class
import Control.Monad.Fix
import Data.String (IsString(..))
import Control.Monad.Trans.Accum (AccumT)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Identity (IdentityT)
import Control.Monad.Trans.Maybe (MaybeT)
import qualified Control.Monad.Trans.RWS.Lazy as Lazy (RWST)
import qualified Control.Monad.Trans.RWS.Strict as Strict (RWST)
import Control.Monad.Trans.Reader (ReaderT)
import qualified Control.Monad.Trans.State.Lazy as Lazy (StateT)
import qualified Control.Monad.Trans.State.Strict as Strict (StateT)
import qualified Control.Monad.Trans.Writer.Lazy as Lazy (WriterT)
import qualified Control.Monad.Trans.Writer.Strict as Strict (WriterT)

#if MIN_VERSION_transformers(0,5,6)
import qualified Control.Monad.Trans.RWS.CPS as CPS (RWST)
import qualified Control.Monad.Trans.Writer.CPS as CPS (WriterT)
#endif

import qualified Reactive.Banana.Prim.High.Combinators as Prim

{-----------------------------------------------------------------------------
    Types
------------------------------------------------------------------------------}

{-| @Event a@ represents a stream of events as they occur in time.
Semantically, you can think of @Event a@ as an infinite list of values
that are tagged with their corresponding time of occurrence,

> type Event a = [(Time,a)]

Each pair is called an /event occurrence/.
Note that within a single event stream,
no two event occurrences may happen at the same time.

<<doc/frp-event.png>>
-}
newtype Event a = E { forall a. Event a -> Event a
unE :: Prim.Event a }
-- Invariant: The empty list `[]` never occurs as event value.

-- | The function 'fmap' applies a function @f@ to every value.
-- Semantically,
--
-- > fmap :: (a -> b) -> Event a -> Event b
-- > fmap f e = [(time, f a) | (time, a) <- e]
instance Functor Event where
    fmap :: forall a b. (a -> b) -> Event a -> Event b
fmap a -> b
f = forall a. Event a -> Event a
E forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> Event a -> Event b
Prim.mapE a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE

-- | The combinator '<>' merges two event streams of the same type.
-- In case of simultaneous occurrences,
-- the events are combined with the underlying 'Semigroup' operation.
-- Semantically,
--
-- > (<>) :: Event a -> Event a -> Event a
-- > (<>) ex ey = unionWith (<>) ex ey
instance Semigroup a => Semigroup (Event a) where
    Event a
x <> :: Event a -> Event a -> Event a
<> Event a
y = forall a. Event a -> Event a
E forall a b. (a -> b) -> a -> b
$ forall a c b.
(a -> c)
-> (b -> c) -> (a -> b -> c) -> Event a -> Event b -> Event c
Prim.mergeWith forall a. a -> a
id forall a. a -> a
id forall a. Semigroup a => a -> a -> a
(<>) (forall a. Event a -> Event a
unE Event a
x) (forall a. Event a -> Event a
unE Event a
y)

-- | The combinator 'mempty' represents an event that never occurs.
-- It is a synonym,
--
-- > mempty :: Event a
-- > mempty = never
instance Semigroup a => Monoid (Event a) where
    mempty :: Event a
mempty  = forall a. Event a -> Event a
E forall a. Event a
Prim.never
    mappend :: Event a -> Event a -> Event a
mappend = forall a. Semigroup a => a -> a -> a
(<>)


{-| @Behavior a@ represents a value that varies in time.
Semantically, you can think of it as a function

> type Behavior a = Time -> a

<<doc/frp-behavior.png>>
-}
newtype Behavior a = B { forall a. Behavior a -> Behavior a
unB :: Prim.Behavior a }

-- | The function 'pure' returns a value that is constant in time. Semantically,
--
-- > pure     :: a -> Behavior a
-- > pure x    = \time -> x
--
-- The combinator '<*>' applies a time-varying function to a time-varying value.
--
-- > (<*>)    :: Behavior (a -> b) -> Behavior a -> Behavior b
-- > fx <*> bx = \time -> fx time $ bx time
instance Applicative Behavior where
    pure :: forall a. a -> Behavior a
pure a
x    = forall a. Behavior a -> Behavior a
B forall a b. (a -> b) -> a -> b
$ forall a. a -> Behavior a
Prim.pureB a
x
    Behavior (a -> b)
bf <*> :: forall a b. Behavior (a -> b) -> Behavior a -> Behavior b
<*> Behavior a
bx = forall a. Behavior a -> Behavior a
B forall a b. (a -> b) -> a -> b
$ forall a b. Behavior (a -> b) -> Behavior a -> Behavior b
Prim.applyB (forall a. Behavior a -> Behavior a
unB Behavior (a -> b)
bf) (forall a. Behavior a -> Behavior a
unB Behavior a
bx)

-- | The function 'fmap' applies a function @f@ at every point in time.
-- Semantically,
--
-- > fmap :: (a -> b) -> Behavior a -> Behavior b
-- > fmap f b = \time -> f (b time)
instance Functor Behavior where
    fmap :: forall a b. (a -> b) -> Behavior a -> Behavior b
fmap = forall (f :: * -> *) a b. Applicative f => (a -> b) -> f a -> f b
liftA

instance Semigroup a => Semigroup (Behavior a) where
  <> :: Behavior a -> Behavior a -> Behavior a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)

instance (Semigroup a, Monoid a) => Monoid (Behavior a) where
  mempty :: Behavior a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
  mappend :: Behavior a -> Behavior a -> Behavior a
mappend = forall a. Semigroup a => a -> a -> a
(<>)

instance Num a => Num (Behavior a) where
    + :: Behavior a -> Behavior a -> Behavior a
(+) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(+)
    (-) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
    * :: Behavior a -> Behavior a -> Behavior a
(*) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(*)
    abs :: Behavior a -> Behavior a
abs = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
abs
    signum :: Behavior a -> Behavior a
signum = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
signum
    fromInteger :: Integer -> Behavior a
fromInteger = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
    negate :: Behavior a -> Behavior a
negate = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
negate

instance Fractional a => Fractional (Behavior a) where
    / :: Behavior a -> Behavior a -> Behavior a
(/) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Fractional a => a -> a -> a
(/)
    fromRational :: Rational -> Behavior a
fromRational = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Fractional a => Rational -> a
fromRational
    recip :: Behavior a -> Behavior a
recip = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Fractional a => a -> a
recip

instance Floating a => Floating (Behavior a) where
    ** :: Behavior a -> Behavior a -> Behavior a
(**) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Floating a => a -> a -> a
(**)
    acos :: Behavior a -> Behavior a
acos = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
acos
    acosh :: Behavior a -> Behavior a
acosh = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
acosh
    asin :: Behavior a -> Behavior a
asin = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
asin
    asinh :: Behavior a -> Behavior a
asinh = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
asinh
    atan :: Behavior a -> Behavior a
atan = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
atan
    atanh :: Behavior a -> Behavior a
atanh = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
atanh
    cos :: Behavior a -> Behavior a
cos = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
cos
    cosh :: Behavior a -> Behavior a
cosh = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
cosh
    exp :: Behavior a -> Behavior a
exp = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
exp
    log :: Behavior a -> Behavior a
log = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
log
    logBase :: Behavior a -> Behavior a -> Behavior a
logBase = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Floating a => a -> a -> a
logBase
    pi :: Behavior a
pi = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Floating a => a
pi
    sin :: Behavior a -> Behavior a
sin = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
sin
    sinh :: Behavior a -> Behavior a
sinh = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
sinh
    sqrt :: Behavior a -> Behavior a
sqrt = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Floating a => a -> a
sqrt

instance IsString a => IsString (Behavior a) where
    fromString :: String -> Behavior a
fromString = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString

-- | The 'Future' monad is just a helper type for the 'changes' function.
--
-- A value of type @Future a@ is only available in the context
-- of a 'reactimate' but not during event processing.
newtype Future a = F { forall a. Future a -> Future a
unF :: Prim.Future a }

-- boilerplate class instances
instance Functor Future where fmap :: forall a b. (a -> b) -> Future a -> Future b
fmap a -> b
f = forall a. Future a -> Future a
F forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Future a -> Future a
unF

instance Monad Future where
    Future a
m >>= :: forall a b. Future a -> (a -> Future b) -> Future b
>>= a -> Future b
g = forall a. Future a -> Future a
F forall a b. (a -> b) -> a -> b
$ forall a. Future a -> Future a
unF Future a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Future a -> Future a
unF forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Future b
g

instance Applicative Future where
    pure :: forall a. a -> Future a
pure    = forall a. Future a -> Future a
F forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
    Future (a -> b)
f <*> :: forall a b. Future (a -> b) -> Future a -> Future b
<*> Future a
a = forall a. Future a -> Future a
F forall a b. (a -> b) -> a -> b
$ forall a. Future a -> Future a
unF Future (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Future a -> Future a
unF Future a
a


{-| The 'Moment' monad denotes a /pure/ computation that happens
at one particular moment in time. Semantically, it is a reader monad

> type Moment a = Time -> a

When run, the argument tells the time at which this computation happens.

Note that in this context, /time/ really means to /logical time/.
Of course, every calculation on a computer takes some
amount of wall-clock time to complete.
Instead, what is meant here is the time as it relates to
'Event's and 'Behavior's.
We use the fiction that every calculation within the 'Moment'
monad takes zero /logical time/ to perform.
-}
newtype Moment a = M { forall a. Moment a -> Moment a
unM :: Prim.Moment a }

{-| The 'MomentIO' monad is used to add inputs and outputs
to an event network.
-}
newtype MomentIO a = MIO { forall a. MomentIO a -> Moment a
unMIO :: Prim.Moment a }

instance MonadIO MomentIO where liftIO :: forall a. IO a -> MomentIO a
liftIO = forall a. Moment a -> MomentIO a
MIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

{-| An instance of the 'MonadMoment' class denotes a computation
that happens at one particular moment in time.
Unlike the 'Moment' monad, it need not be pure anymore.
-}
class MonadFix m => MonadMoment m where
    liftMoment :: Moment a -> m a

instance MonadMoment Moment   where liftMoment :: forall a. Moment a -> Moment a
liftMoment = forall a. a -> a
id
instance MonadMoment MomentIO where liftMoment :: forall a. Moment a -> MomentIO a
liftMoment = forall a. Moment a -> MomentIO a
MIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
unM
instance (MonadMoment m, Monoid w) => MonadMoment (AccumT w m) where liftMoment :: forall a. Moment a -> AccumT w m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (ExceptT e m) where liftMoment :: forall a. Moment a -> ExceptT e m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (IdentityT m) where liftMoment :: forall a. Moment a -> IdentityT m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (MaybeT m) where liftMoment :: forall a. Moment a -> MaybeT m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance (MonadMoment m, Monoid w) => MonadMoment (Lazy.RWST r w s m) where liftMoment :: forall a. Moment a -> RWST r w s m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance (MonadMoment m, Monoid w) => MonadMoment (Strict.RWST r w s m) where liftMoment :: forall a. Moment a -> RWST r w s m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (ReaderT r m) where liftMoment :: forall a. Moment a -> ReaderT r m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (Lazy.StateT s m) where liftMoment :: forall a. Moment a -> StateT s m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (Strict.StateT s m) where liftMoment :: forall a. Moment a -> StateT s m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance (MonadMoment m, Monoid w) => MonadMoment (Lazy.WriterT w m) where liftMoment :: forall a. Moment a -> WriterT w m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance (MonadMoment m, Monoid w) => MonadMoment (Strict.WriterT w m) where liftMoment :: forall a. Moment a -> WriterT w m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment

#if MIN_VERSION_transformers(0,5,6)
instance MonadMoment m => MonadMoment (CPS.RWST r w s m) where liftMoment :: forall a. Moment a -> RWST r w s m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
instance MonadMoment m => MonadMoment (CPS.WriterT w m) where liftMoment :: forall a. Moment a -> WriterT w m a
liftMoment = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment
#endif

-- boilerplate class instances
instance Functor Moment where fmap :: forall a b. (a -> b) -> Moment a -> Moment b
fmap a -> b
f = forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
unM
instance Monad Moment where
    Moment a
m >>= :: forall a b. Moment a -> (a -> Moment b) -> Moment b
>>= a -> Moment b
g = forall a. Moment a -> Moment a
M forall a b. (a -> b) -> a -> b
$ forall a. Moment a -> Moment a
unM Moment a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Moment a -> Moment a
unM forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Moment b
g
instance Applicative Moment where
    pure :: forall a. a -> Moment a
pure    = forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
    Moment (a -> b)
f <*> :: forall a b. Moment (a -> b) -> Moment a -> Moment b
<*> Moment a
a = forall a. Moment a -> Moment a
M forall a b. (a -> b) -> a -> b
$ forall a. Moment a -> Moment a
unM Moment (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Moment a -> Moment a
unM Moment a
a
instance MonadFix Moment where mfix :: forall a. (a -> Moment a) -> Moment a
mfix a -> Moment a
f = forall a. Moment a -> Moment a
M forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall a. Moment a -> Moment a
unM forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Moment a
f)

instance Semigroup a => Semigroup (Moment a) where
    <> :: Moment a -> Moment a -> Moment a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
instance Monoid a => Monoid (Moment a) where
    mempty :: Moment a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty


instance Functor MomentIO where fmap :: forall a b. (a -> b) -> MomentIO a -> MomentIO b
fmap a -> b
f = forall a. Moment a -> MomentIO a
MIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MomentIO a -> Moment a
unMIO
instance Monad MomentIO where
    MomentIO a
m >>= :: forall a b. MomentIO a -> (a -> MomentIO b) -> MomentIO b
>>= a -> MomentIO b
g = forall a. Moment a -> MomentIO a
MIO forall a b. (a -> b) -> a -> b
$ forall a. MomentIO a -> Moment a
unMIO MomentIO a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. MomentIO a -> Moment a
unMIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> MomentIO b
g
instance Applicative MomentIO where
    pure :: forall a. a -> MomentIO a
pure    = forall a. Moment a -> MomentIO a
MIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
    MomentIO (a -> b)
f <*> :: forall a b. MomentIO (a -> b) -> MomentIO a -> MomentIO b
<*> MomentIO a
a = forall a. Moment a -> MomentIO a
MIO forall a b. (a -> b) -> a -> b
$ forall a. MomentIO a -> Moment a
unMIO MomentIO (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. MomentIO a -> Moment a
unMIO MomentIO a
a
instance MonadFix MomentIO where mfix :: forall a. (a -> MomentIO a) -> MomentIO a
mfix a -> MomentIO a
f = forall a. Moment a -> MomentIO a
MIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall a. MomentIO a -> Moment a
unMIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> MomentIO a
f)

instance Semigroup a => Semigroup (MomentIO a) where
    <> :: MomentIO a -> MomentIO a -> MomentIO a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
instance Monoid a => Monoid (MomentIO a) where
    mempty :: MomentIO a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty