dunai-0.14.0: Generalised reactive framework supporting classic, arrowized and monadic FRP.
Copyright(c) Ivan Perez and Manuel Baerenz 2016
LicenseBSD3
Maintainerivan.perez@keera.co.uk
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.MonadicStreamFunction.InternalCore

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.

This modules defines only the minimal core. By default, you should import Data.MonadicStreamFunction.Core or Data.MonadicStreamFunction whenever possible, and define your functions without accessing the MSF constructor. Those modules, as well as other modules in dunai, also provide convenient instances. This module may be useful if you are extending dunai with functionality that cannot be (conveniently) expressed using the existing high-level API.

Synopsis

Definitions

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.

Constructors

MSF 

Fields

Instances

Instances details
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 => 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 => 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, 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, 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 #

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

Instance of Alternative for Monadic Stream Functions (MSF), implemented using the ArrowZero and ArrowPlus instances.

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] #

(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 #

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 #

(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, 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, 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, Eq s, Num s, VectorSpace v s, Floating (MSF m a s)) => VectorSpace (MSF m a v) (MSF m a s) Source #

Vector-space instance for MSFs.

Instance details

Defined in Data.MonadicStreamFunction.Instances.VectorSpace

Methods

zeroVector :: MSF m a v #

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

(^/) :: MSF m a v -> MSF m a 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 -> MSF m a s #

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

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

Monadic computations and MSFs

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.

Feedback loops

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.

Execution/simulation

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"

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

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