Skip to content

mikesol/purescript-behaviors

 
 

Repository files navigation

purescript-behaviors

Introduction

Push-pull FRP is concerned with events and behaviors. Events are values which occur discretely over time, and behaviors act like continuous functions of time.

Why bother with behaviors at all, when the machines we work with deal with events such as interrupts at the most basic level?

Well, we can work with continuous functions of time in a variety of ways, including by differentiation and integration. Also, working with a conceptually infinitely-dense representation means that we can defer the choice of sampling interval until we are ready to render our results.

This library takes a slightly novel approach by constructing behaviors from events.

newtype ABehavior event a = ABehavior (forall b. event (a -> b) -> event b)

type Behavior = ABehavior Event

Here, a Behavior is constructed directly from its sampling function. The various functions which work with this representation can delay the choice of sampling interval for as long as possible, but ultimately this Behavior is equivalent to working with events directly, albeit with alternative, function-like instances.

This representation has the correct type class instances, and supports operations such as integration, differentiation and even recursion, which means we can use it to solve interactive differential equations. For example, here is an exponential function computed as the solution of a differential equation:

exp = fixB 1.0 \b -> integral' 1.0 seconds ((-2.0 * _) <$> b)

Its differential equation is 𝑑𝑥/𝑑𝑡 = −2𝑥 , which is rearranged so the right hand side gives an expression whose fixed point solution is the exponential function desired.

𝑥 = ∫−2𝑥 𝑑𝑡

(In the purescript formulation, the values 1.0 are respectively the initial value for the Behavior being integrated over, and the initial approximation to the integral)

See the example project for a more interesting, interactive example.

See also a conversational demo. An accompanying video discussion breaks down a component of the example project.

About

A simple push-pull FRP implementation

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • PureScript 86.2%
  • Dhall 13.8%