reactive-banana-0.8.0.0: Library for functional reactive programming (FRP).

Safe HaskellNone

Reactive.Banana.Combinators

Contents

Synopsis

Synopsis

Combinators for building event graphs.

Introduction

At its core, Functional Reactive Programming (FRP) is about two data types Event and Behavior and the various ways to combine them.

data Event t a Source

Event t a represents a stream of events as they occur in time. Semantically, you can think of Event t a as an infinite list of values that are tagged with their corresponding time of occurence,

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

Instances

data Behavior t a Source

Behavior t a represents a value that varies in time. Think of it as

 type Behavior t a = Time -> a

As you can see, both types seem to have a superfluous parameter t. The library uses it to rule out certain gross inefficiencies, in particular in connection with dynamic event switching. For basic stuff, you can completely ignore it, except of course for the fact that it will annoy you in your type signatures.

While the type synonyms mentioned above are the way you should think about Behavior and Event, they are a bit vague for formal manipulation. To remedy this, the library provides a very simple but authoritative model implementation. See Reactive.Banana.Model for more.

interpret :: (forall t. Event t a -> Event t b) -> [[a]] -> IO [[b]]Source

Interpret an event processing function. Useful for testing.

Core Combinators

never :: Event t aSource

Event that never occurs. Think of it as never = [].

union :: Event t a -> Event t a -> Event t aSource

Merge two event streams of the same type. In case of simultaneous occurrences, the left argument comes first. Think of it as

 union ((timex,x):xs) ((timey,y):ys)
    | timex <= timey = (timex,x) : union xs ((timey,y):ys)
    | timex >  timey = (timey,y) : union ((timex,x):xs) ys

unions :: [Event t a] -> Event t aSource

Merge several event streams of the same type.

 unions = foldr union never

filterE :: (a -> Bool) -> Event t a -> Event t aSource

Allow all events that fulfill the predicate, discard the rest. Think of it as

 filterE p es = [(time,a) | (time,a) <- es, p a]

collect :: Event t a -> Event t [a]Source

Collect simultaneous event occurences. The result will never contain an empty list. Example:

 collect [(time1, e1), (time1, e2)] = [(time1, [e1,e2])]

spill :: Event t [a] -> Event t aSource

Emit simultaneous event occurrences. The first element in the list will be emitted first, and so on.

Up to strictness, we have

 spill . collect = id

accumE :: a -> Event t (a -> a) -> Event t aSource

The accumE function accumulates a stream of events. Example:

 accumE "x" [(time1,(++"y")),(time2,(++"z"))]
    = [(time1,"xy"),(time2,"xyz")]

Note that the output events are simultaneous with the input events, there is no "delay" like in the case of accumB.

apply :: Behavior t (a -> b) -> Event t a -> Event t bSource

Apply a time-varying function to a stream of events. Think of it as

 apply bf ex = [(time, bf time x) | (time, x) <- ex]

This function is generally used in its infix variant <@>.

stepper :: a -> Event t a -> Behavior t aSource

Construct a time-varying function from an initial value and a stream of new values. Think of it as

 stepper x0 ex = \time -> last (x0 : [x | (timex,x) <- ex, timex < time])

Note that the smaller-than-sign in the comparision timex < time means that the value of the behavior changes "slightly after" the event occurrences. This allows for recursive definitions.

Also note that in the case of simultaneous occurrences, only the last one is kept.

Further combinators that Haddock can't document properly.

 instance Applicative (Behavior t)

Behavior is an applicative functor. In particular, we have the following functions.

 pure :: a -> Behavior t a

The constant time-varying value. Think of it as pure x = \time -> x.

 (<*>) :: Behavior t (a -> b) -> Behavior t a -> Behavior t b

Combine behaviors in applicative style. Think of it as bf <*> bx = \time -> bf time $ bx time.

Derived Combinators

Infix operators

(<@>) :: Behavior t (a -> b) -> Event t a -> Event t bSource

Infix synonym for the apply combinator. Similar to <*>.

 infixl 4 <@>

(<@) :: Behavior t b -> Event t a -> Event t bSource

Tag all event occurrences with a time-varying value. Similar to <*.

 infixl 4 <@

Filtering

filterJust :: Event t (Maybe a) -> Event t aSource

Allow all event occurrences that are Just values, discard the rest. Variant of filterE.

filterApply :: Behavior t (a -> Bool) -> Event t a -> Event t aSource

Allow all events that fulfill the time-varying predicate, discard the rest. Generalization of filterE.

whenE :: Behavior t Bool -> Event t a -> Event t aSource

Allow events only when the behavior is True. Variant of filterApply.

split :: Event t (Either a b) -> (Event t a, Event t b)Source

Split event occurrences according to a tag. The Left values go into the left component while the Right values go into the right component of the result.

Accumulation

Note: All accumulation functions are strict in the accumulated value!

Note: The order of arguments is acc -> (x,acc) which is also the convention used by unfoldr and State.

accumB :: a -> Event t (a -> a) -> Behavior t aSource

The accumB function is similar to a strict left fold, foldl'. It starts with an initial value and combines it with incoming events. For example, think

 accumB "x" [(time1,(++"y")),(time2,(++"z"))]
    = stepper "x" [(time1,"xy"),(time2,"xyz")]

Note that the value of the behavior changes "slightly after" the events occur. This allows for recursive definitions.

mapAccum :: acc -> Event t (acc -> (x, acc)) -> (Event t x, Behavior t acc)Source

Efficient combination of accumE and accumB.

Simultaneous event occurrences

calm :: Event t a -> Event t aSource

Keep only the last occurrence when simultaneous occurrences happen.

unionWith :: (a -> a -> a) -> Event t a -> Event t a -> Event t aSource

Combine simultaneous event occurrences into a single occurrence.

 unionWith f e1 e2 = fmap (foldr1 f) <$> collect (e1 `union` e2)