Personal tools


From HaskellWiki

Revision as of 16:00, 1 December 2011 by Ertes (Talk | contribs)

Jump to: navigation, search

Netwire is a library for functional reactive programming, which uses the concept of arrows for modelling an embedded domain-specific language. This language lets you express reactive systems, which means systems that change over time. It shares the basic concept with Yampa and its fork Animas, but it is itself not a fork and has many additional features.

This wiki page corresponds to Netwire version 3 and is currently a work in progress.


1 Features

Here is a list of some of the features of Netwire:

  • arrow interface (or optionally an applicative interface),
  • signal inhibition (ArrowZero / Alternative),
  • signal selection (ArrowPlus / Alternative),
  • self-adjusting wires (ArrowChoice),
  • rich set of event wires,
  • signal analysis wires (average, peak, etc.),
  • effectful wires.

2 Basics

The Netwire library is based around a data type called
. You need to import the
module to work with wires:
import Control.Wire
data Wire e (>~) a b
For some arrows
and all monoids
the type
Wire e (>~)
is an arrow. Only certain arrows are allowed for
, because
is actually a data family. These arrows are called base arrows in Netwire.
comp :: Wire e (>~) a b
Values of type
Wire e (>~) a b
are time-varying functions, which resemble the following type:
a >~ Either e b
So it's a function that takes a value of type
and either produces a value of type
or produces no value, but instead inhibits with a value of type
. The act of running a wire is called stepping and the process is called an instant. You can step a wire through one of the stepping functions, which we will cover later. When you step a wire, it will return a new version of itself along with its result. You are supposed to call the new version the next time you step.

2.1 The inhibition monoid

argument to
is called the inhibition monoid. For simple applications you can just use
here, but you may want to actually assign exception values to inhibition. We will cover that later. For now just use

2.2 Base arrows

argument to
is called the base arrow. In most cases you will use a
arrow here, and this is currently the only type of arrow supported, though more will be added in the future. For simple applications you can just use the
monad, and it is useful to define a type alias for your custom wire type:
type MyWire = Wire () (Kleisli IO)

3 Running wires

For running a wire you can use the stepping functions available in the
module. There is no need to import that module. It is automatically imported with
. For Kleisli-based wires you will want to use the
stepWireM ::
    Monad m
    => Wire e (Kleisli m) a b
    -> a
    -> m (Either e b, Wire e (Kleisli m) a b)
In our case we have
m = IO
, so our type signature is simply:
stepWireM :: MyWire a b -> a -> IO (Either () b, MyWire a b)
This function takes a wire and an input value. It passes the input value to the wire and returns its result value of type
Either () b
. Along with the result it also returns a new wire. Normally you would call
in a loop, which performs instant after instant. This is the basic structure:
system :: MyWire Int String
system = {- ... -}
main :: IO ()
main = loop system
    loop :: MyWire Int String -> IO ()
    loop w' = do
        (mx, w) <- stepWireM w' 15
        {- ... do something with mx ... -}
        loop w  -- loop with the new wire.