Difference between revisions of "Netwire"

From HaskellWiki
Jump to navigation Jump to search
(Initial revision)
 
m (→‎Scope: Expression.)
(51 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Netwire is a library for [[Functional Reactive Programming|functional reactive programming]], which uses the concept of [[Arrow|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.
+
Netwire is a [[Functional Reactive Programming|functional reactive programming]] library that provides both an applicative and an arrow interface. It allows you to express time-varying values with a rich event system.
   
  +
* [http://hackage.haskell.org/package/netwire Project page]
  +
* [http://hub.darcs.net/ertes/netwire Source repository]
   
 
== Features ==
 
== Features ==
   
Here is a list of some of the features of ''netwire'':
+
Here is a list of some of the features of Netwire:
   
* arrowized interface,
+
* applicative interface (or optionally an arrow interface),
  +
* signal intervals,
* applicative interface,
 
  +
* dynamic switching,
* signal inhibition (<code>ArrowZero</code> / <code>Alternative</code>),
 
 
* rich set of predefined functionality,
* choice and combination (<code>ArrowPlus</code> / <code>Alternative</code>),
 
 
* signal analysis (average, interpolation, peak, etc.),
* self-adjusting wires (<code>ArrowChoice</code>),
 
  +
* effectful reactive systems.
* rich set of event wires,
 
* signal analysis wires (average, peak, etc.),
 
* impure wires.
 
   
== Quickstart ==
+
== Scope ==
   
  +
Netwire's FRP framework is intended to be used for continuous applications. It replaces the traditional big main loop with its global state and event callbacks/branching by a completely declarative model. The following types of applications can benefit from using Netwire:
This is a quickstart introduction to Netwire for Haskell programmers familiar with arrowized functional reactive programming (AFRP), for example Yampa or Animas. It should quickly give you an idea of how the library works and how it differs from the two mentioned.
 
   
  +
* artificial intelligence and bots,
=== The wire ===
 
  +
* canvas-based graphics and animations,
  +
* continuous signal synthesis (audio waves, etc.),
  +
* games and game servers,
  +
* scene-based user interfaces (like OpenGL and vty),
  +
* simulations.
   
  +
If you can sensibly break your application down into ''frames'', then Netwire is for you. For other kinds of reactive applications like widget-based UIs you may want to look into [[reactive-banana]] instead.
Netwire calls its signal transformation functions ''wires''. You can think of a wire as a device with an input line and an output line. The difference between a function and a wire is that a wire can change itself throughout its lifetime. This is the basic idea of arrowized FRP. It gives you time-dependent values.
 
   
  +
== Get started ==
A wire is parameterized over its input and output types:
 
   
  +
The documentation is contained within the package itself, but you can also read it online:
<haskell>
 
data Wire a b
 
</haskell>
 
   
  +
* [http://hub.darcs.net/ertes/netwire/browse/README.md Tutorial]
  +
* [http://hackage.haskell.org/package/netwire Project page with API docs]
   
=== Differences from Yampa ===
+
== Model and correctness ==
   
  +
Netwire's underlying abstraction is more powerful than the [http://conal.net/papers/icfp97/ original formulation of time-varying values]. It allows you to implement general component systems with local state.
If you are not familiar with Yampa or Animas, you can safely skip this section.
 
   
  +
Starting with Netwire 5 there are two module trees <hask>Control.Wire</hask> and <hask>FRP.Netwire</hask>. The latter exports a subset of Netwire that closely follows the original model and provides a set of predefined FRP wires.
The main difference between Yampa and Netwire is that the underlying arrow is impure. While you can choose not to use the impure wires inside of <code>FRP.NetWire.IO</code>, it is a design choice for this library to explicitly allow impure computations. One theoretical implication is that you need to differentiate between pure stateless, pure stateful and impure signal transformations.
 
   
  +
Signal intervals are an extension of the original model and an integral part of Netwire: A behavior is a ''partial'' function of time with the limitation that both the defined and undefined intervals must have a non-zero length. This extension makes it much easier to express switching and multicasting systems.
A concept not found in Yampa is signal inhibition. A wire can choose not to return anything. This way you can temporarily block entire subnetworks. This is most useful with the combination operator <code><+></code>. Example:
 
   
  +
== History ==
<haskell>
 
w = w1 <+> w2
 
</haskell>
 
   
  +
This project started in 2011 as a replacement for Yampa to provide both a nicer interface and better integration into existing frameworks. Its original purpose was to power game servers and intelligent network bots. That's the origin of the name ''Netwire''.
The <code>w</code> wire runs its signal through the wire <code>w1</code>, and if it inhibits, it passes the signal to <code>w2</code>.
 
   
  +
However, before its first release ''signal intervals'' were added (originally under the term ''signal inhibition''). Netwire became a completely new abstraction, so it lost its connection to Yampa.
Another concept not found in Yampa is choice. Through the <code>ArrowChoice</code> instance wires allow you to choose one of a set of subwires for its signal without needing a switch. Essentially you can write <code>if</code> and <code>case</code> constructs inside of arrow notation.
 
   
  +
[[Category:FRP]]
Because of their impurity wires do not have an <code>ArrowLoop</code> instance. It is possible to write one, but it will diverge most of the time, rendering it useless.
 
 
 
=== Using a wire ===
 
 
To run a wire you will need to use the <code>withWire</code> and <code>stepWire</code> functions. The <code>withWire</code> initializes a wire and gives you a <code>Session</code> value. As metioned earlier in general a wire is a function, which can mutate itself over time. The session value captures the current state of the wire.
 
 
<haskell>
 
initWire :: Wire a b -> (Session a b -> IO c) -> IO c
 
stepWire :: a -> Session a b -> IO (Maybe b)
 
</haskell>
 
 
The <code>stepWire</code> function passes the given input value through the wire. If you use <code>stepWire</code>, then the wire will mutate in real time. If you need a different rate of time, you can use <code>stepWireDelta</code> or <code>stepWireTime</code> instead.
 
 
The stepping functions return a <code>Maybe b</code>. If the wire inhibits, then the result is <code>Nothing</code>, otherwise it will be <code>Just</code> the output.
 
 
 
=== Writing a wire ===
 
 
Pure stateless wires are easy to explain, so let's start with them. A pure stateless wire is essentially just a function of input and time. The simplest wire is the ''identity'' wire. It disregards time and just returns its input verbatim:
 
 
<haskell>
 
identity :: Wire a a
 
</haskell>
 

Revision as of 13:24, 14 November 2013

Netwire is a functional reactive programming library that provides both an applicative and an arrow interface. It allows you to express time-varying values with a rich event system.

Features

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

  • applicative interface (or optionally an arrow interface),
  • signal intervals,
  • dynamic switching,
  • rich set of predefined functionality,
  • signal analysis (average, interpolation, peak, etc.),
  • effectful reactive systems.

Scope

Netwire's FRP framework is intended to be used for continuous applications. It replaces the traditional big main loop with its global state and event callbacks/branching by a completely declarative model. The following types of applications can benefit from using Netwire:

  • artificial intelligence and bots,
  • canvas-based graphics and animations,
  • continuous signal synthesis (audio waves, etc.),
  • games and game servers,
  • scene-based user interfaces (like OpenGL and vty),
  • simulations.

If you can sensibly break your application down into frames, then Netwire is for you. For other kinds of reactive applications like widget-based UIs you may want to look into reactive-banana instead.

Get started

The documentation is contained within the package itself, but you can also read it online:

Model and correctness

Netwire's underlying abstraction is more powerful than the original formulation of time-varying values. It allows you to implement general component systems with local state.

Starting with Netwire 5 there are two module trees Control.Wire and FRP.Netwire. The latter exports a subset of Netwire that closely follows the original model and provides a set of predefined FRP wires.

Signal intervals are an extension of the original model and an integral part of Netwire: A behavior is a partial function of time with the limitation that both the defined and undefined intervals must have a non-zero length. This extension makes it much easier to express switching and multicasting systems.

History

This project started in 2011 as a replacement for Yampa to provide both a nicer interface and better integration into existing frameworks. Its original purpose was to power game servers and intelligent network bots. That's the origin of the name Netwire.

However, before its first release signal intervals were added (originally under the term signal inhibition). Netwire became a completely new abstraction, so it lost its connection to Yampa.