dunai-0.8.1: Generalised reactive framework supporting classic, arrowized and monadic FRP.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.MonadicStreamFunction.Core

Description

Monadic Stream Functions are synchronized stream functions with side effects.

MSFs are defined by a function unMSF :: MSF m a b -> a -> m (b, MSF m a b) that executes one step of a simulation, and produces an output in a monadic context, and a continuation to be used for future steps.

MSFs are a generalisation of the implementation mechanism used by Yampa, Wormholes and other FRP and reactive implementations.

When combined with different monads, they produce interesting effects. For example, when combined with the Maybe monad, they become transformations that may stop producing outputs (and continuations). The Either monad gives rise to MSFs that end with a result (akin to Tasks in Yampa, and Monadic FRP).

Flattening, that is, going from some structure MSF (t m) a b to MSF m a b for a specific transformer t often gives rise to known FRP constructs. For instance, flattening with EitherT gives rise to switching, and flattening with ListT gives rise to parallelism with broadcasting.

MSFs can be used to implement many FRP variants, including Arrowized FRP, Classic FRP, and plain reactive programming. Arrowized and applicative syntax are both supported.

For a very detailed introduction to MSFs, see: http://dl.acm.org/citation.cfm?id=2976010 (mirror: http://www.cs.nott.ac.uk/~psxip1/#FRPRefactored).

Synopsis

Types

data MSF m a b Source #

Stepwise, side-effectful MSFs without implicit knowledge of time.

MSFs should be applied to streams or executed indefinitely or until they terminate. See reactimate and reactimateB for details. In general, calling the value constructor MSF or the function unMSF is discouraged.

Instances

Instances details
Monad m => Arrow (MSF m) Source #

Arrow instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Core

Methods

arr :: (b -> c) -> MSF m b c #

first :: MSF m b c -> MSF m (b, d) (c, d) #

second :: MSF m b c -> MSF m (d, b) (d, c) #

(***) :: MSF m b c -> MSF m b' c' -> MSF m (b, b') (c, c') #

(&&&) :: MSF m b c -> MSF m b c' -> MSF m b (c, c') #

(Monad m, MonadPlus m) => ArrowZero (MSF m) Source #

Instance of ArrowZero for Monadic Stream Functions (MSF). The monad must be an instance of MonadPlus.

Instance details

Defined in Data.MonadicStreamFunction.Instances.ArrowPlus

Methods

zeroArrow :: MSF m b c #

(Monad m, MonadPlus m) => ArrowPlus (MSF m) Source #

Instance of ArrowPlus for Monadic Stream Functions (MSF). The monad must be an instance of MonadPlus.

Instance details

Defined in Data.MonadicStreamFunction.Instances.ArrowPlus

Methods

(<+>) :: MSF m b c -> MSF m b c -> MSF m b c #

Monad m => ArrowChoice (MSF m) Source #

ArrowChoice instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.ArrowChoice

Methods

left :: MSF m b c -> MSF m (Either b d) (Either c d) #

right :: MSF m b c -> MSF m (Either d b) (Either d c) #

(+++) :: MSF m b c -> MSF m b' c' -> MSF m (Either b b') (Either c c') #

(|||) :: MSF m b d -> MSF m c d -> MSF m (Either b c) d #

MonadFix m => ArrowLoop (MSF m) Source #

ArrowLoop instance for MSFs. The monad must be an instance of MonadFix.

Instance details

Defined in Data.MonadicStreamFunction.Instances.ArrowLoop

Methods

loop :: MSF m (b, d) (c, d) -> MSF m b c #

Monad m => Category (MSF m :: Type -> Type -> Type) Source #

Instance definition for Category. Defines id and ..

Instance details

Defined in Data.MonadicStreamFunction.InternalCore

Methods

id :: forall (a :: k). MSF m a a #

(.) :: forall (b :: k) (c :: k) (a :: k). MSF m b c -> MSF m a b -> MSF m a c #

Monad m => Functor (MSF m a) Source #

Functor instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Core

Methods

fmap :: (a0 -> b) -> MSF m a a0 -> MSF m a b #

(<$) :: a0 -> MSF m a b -> MSF m a a0 #

(Functor m, Monad m) => Applicative (MSF m a) Source #

Applicative instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Core

Methods

pure :: a0 -> MSF m a a0 #

(<*>) :: MSF m a (a0 -> b) -> MSF m a a0 -> MSF m a b #

liftA2 :: (a0 -> b -> c) -> MSF m a a0 -> MSF m a b -> MSF m a c #

(*>) :: MSF m a a0 -> MSF m a b -> MSF m a b #

(<*) :: MSF m a a0 -> MSF m a b -> MSF m a a0 #

(Functor m, Monad m, MonadPlus m) => Alternative (MSF m a) Source # 
Instance details

Defined in Data.MonadicStreamFunction.Instances.ArrowPlus

Methods

empty :: MSF m a a0 #

(<|>) :: MSF m a a0 -> MSF m a a0 -> MSF m a a0 #

some :: MSF m a a0 -> MSF m a [a0] #

many :: MSF m a a0 -> MSF m a [a0] #

(Monad m, Floating b) => Floating (MSF m a b) Source #

Floating instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.Num

Methods

pi :: MSF m a b #

exp :: MSF m a b -> MSF m a b #

log :: MSF m a b -> MSF m a b #

sqrt :: MSF m a b -> MSF m a b #

(**) :: MSF m a b -> MSF m a b -> MSF m a b #

logBase :: MSF m a b -> MSF m a b -> MSF m a b #

sin :: MSF m a b -> MSF m a b #

cos :: MSF m a b -> MSF m a b #

tan :: MSF m a b -> MSF m a b #

asin :: MSF m a b -> MSF m a b #

acos :: MSF m a b -> MSF m a b #

atan :: MSF m a b -> MSF m a b #

sinh :: MSF m a b -> MSF m a b #

cosh :: MSF m a b -> MSF m a b #

tanh :: MSF m a b -> MSF m a b #

asinh :: MSF m a b -> MSF m a b #

acosh :: MSF m a b -> MSF m a b #

atanh :: MSF m a b -> MSF m a b #

log1p :: MSF m a b -> MSF m a b #

expm1 :: MSF m a b -> MSF m a b #

log1pexp :: MSF m a b -> MSF m a b #

log1mexp :: MSF m a b -> MSF m a b #

(Monad m, Fractional b) => Fractional (MSF m a b) Source #

Fractional instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.Num

Methods

(/) :: MSF m a b -> MSF m a b -> MSF m a b #

recip :: MSF m a b -> MSF m a b #

fromRational :: Rational -> MSF m a b #

(Monad m, Num b) => Num (MSF m a b) Source #

Num instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.Num

Methods

(+) :: MSF m a b -> MSF m a b -> MSF m a b #

(-) :: MSF m a b -> MSF m a b -> MSF m a b #

(*) :: MSF m a b -> MSF m a b -> MSF m a b #

negate :: MSF m a b -> MSF m a b #

abs :: MSF m a b -> MSF m a b #

signum :: MSF m a b -> MSF m a b #

fromInteger :: Integer -> MSF m a b #

(Monad m, VectorSpace v s) => VectorSpace (MSF m a v) s Source #

Vector-space instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.VectorSpace

Methods

zeroVector :: MSF m a v #

(*^) :: s -> MSF m a v -> MSF m a v #

(^/) :: MSF m a v -> s -> MSF m a v #

(^+^) :: MSF m a v -> MSF m a v -> MSF m a v #

(^-^) :: MSF m a v -> MSF m a v -> MSF m a v #

negateVector :: MSF m a v -> MSF m a v #

dot :: MSF m a v -> MSF m a v -> s #

norm :: MSF m a v -> s #

normalize :: MSF m a v -> MSF m a v #

Lifting and Monadic transformations

Lifting point-wise computations

constM :: Monad m => m b -> MSF m a b Source #

Lifts a monadic computation into a Stream.

arrM :: Monad m => (a -> m b) -> MSF m a b Source #

Apply a monadic transformation to every element of the input stream.

Generalisation of arr from Arrow to monadic functions.

liftBaseM :: (Monad m2, MonadBase m1 m2) => (a -> m1 b) -> MSF m2 a b Source #

Monadic lifting from one monad into another

Trans-monadic MSF combinators

MonadBase

liftBaseS :: (Monad m2, MonadBase m1 m2) => MSF m1 a b -> MSF m2 a b Source #

Lift innermost monadic actions in monad stack (generalisation of liftIO).

(^>>>) :: MonadBase m1 m2 => MSF m1 a b -> MSF m2 b c -> MSF m2 a c Source #

Lift the first MSF into the monad of the second.

(>>>^) :: MonadBase m1 m2 => MSF m2 a b -> MSF m1 b c -> MSF m2 a c Source #

Lift the second MSF into the monad of the first.

MonadTrans

liftTransS :: (MonadTrans t, Monad m, Monad (t m)) => MSF m a b -> MSF (t m) a b Source #

Lift inner monadic actions in monad stacks.

Generic Monadic Transformations

morphS :: (Monad m2, Monad m1) => (forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b Source #

Apply trans-monadic actions (in an arbitrary way).

This is just a convenience function when you have a function to move across monads, because the signature of morphGS is a bit complex.

morphGS Source #

Arguments

:: Monad m2 
=> (forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c))

The natural transformation. mi, ai and bi for i = 1, 2 can be chosen freely, but c must be universally quantified

-> MSF m1 a1 b1 
-> MSF m2 a2 b2 

Generic lifting of a morphism to the level of MSFs.

Natural transformation to the level of MSFs.

Mathematical background: The type a -> m (b, c) is a functor in c, and MSF m a b is its greatest fixpoint, i.e. it is isomorphic to the type a -> m (b, MSF m a b), by definition. The types m, a and b are parameters of the functor. Taking a fixpoint is functorial itself, meaning that a morphism (a natural transformation) of two such functors gives a morphism (an ordinary function) of their fixpoints.

This is in a sense the most general "abstract" lifting function, i.e. the most general one that only changes input, output and side effect types, and doesn't influence control flow. Other handling functions like exception handling or ListT broadcasting necessarily change control flow.

Depending on the past

feedback :: Monad m => c -> MSF m (a, c) (b, c) -> MSF m a b Source #

Well-formed looped connection of an output component as a future input.

Simulation

reactimate :: Monad m => MSF m () () -> m () Source #

Run an MSF indefinitely passing a unit-carrying input stream.

embed :: Monad m => MSF m a b -> [a] -> m [b] Source #

Apply a monadic stream function to a list.

Because the result is in a monad, it may be necessary to traverse the whole list to evaluate the value in the results to WHNF. For example, if the monad is the maybe monad, this may not produce anything if the MSF produces Nothing at any point, so the output stream cannot consumed progressively.

To explore the output progressively, use arrM and (>>>)', together with some action that consumes/actuates on the output.

This is called runSF in Liu, Cheng, Hudak, "Causal Commutative Arrows and Their Optimization"

Orphan instances

Monad m => Arrow (MSF m) Source #

Arrow instance for MSFs.

Instance details

Methods

arr :: (b -> c) -> MSF m b c #

first :: MSF m b c -> MSF m (b, d) (c, d) #

second :: MSF m b c -> MSF m (d, b) (d, c) #

(***) :: MSF m b c -> MSF m b' c' -> MSF m (b, b') (c, c') #

(&&&) :: MSF m b c -> MSF m b c' -> MSF m b (c, c') #

Monad m => Functor (MSF m a) Source #

Functor instance for MSFs.

Instance details

Methods

fmap :: (a0 -> b) -> MSF m a a0 -> MSF m a b #

(<$) :: a0 -> MSF m a b -> MSF m a a0 #

(Functor m, Monad m) => Applicative (MSF m a) Source #

Applicative instance for MSFs.

Instance details

Methods

pure :: a0 -> MSF m a a0 #

(<*>) :: MSF m a (a0 -> b) -> MSF m a a0 -> MSF m a b #

liftA2 :: (a0 -> b -> c) -> MSF m a a0 -> MSF m a b -> MSF m a c #

(*>) :: MSF m a a0 -> MSF m a b -> MSF m a b #

(<*) :: MSF m a a0 -> MSF m a b -> MSF m a a0 #