I've read the netwire quickstart, but I have problems imagining how the whole thing would look like in a 'real' application. As the tutorial only deals with pure wires, I'm especially interested in in games if there are any, but I wouldn't mind others. Reactive Banana examples would probably do as well. They should just illustrate how FRP is useful.
2 Answers
Netwire is used in some real-world applications, but I don't know where to find them. Likely they are currently behind closed doors. However, a few example applications have been blogged about on Reddit, so you may want to check out /r/haskell. Just search for "netwire" there. Unfortunately reactive-banana examples won't help, because the concepts of these two libraries especially for event handling are entirely different.
The overall structure of a Netwire application is this: First you define a reactive value, let's call it simulation
. In the simplest case it's a pure wire:
simulation :: WireP a [Particle]
Without detailing (yet) how to write that wire let me now explain what it is. The output type is [Particle]
, so it's a reactive list of particles. That means, this list can change over time. Notably the input type is fully polymorphic, so you know that this reactive value does not depend on other reactive values.
Now you want to get the actual value of the particle list at certain points in time. This is where your session and stepping functions come in. Most applications only need one of the session stepping functions like stepSessionP
in this case. You just call this function in a loop to get the current value of the wire at this instant. There is no need to call this function continuously.
You will notice that the stepping function doesn't give you a [Particle]
, but a Either LastException [Particle]
. This is because reactive values in Netwire can inhibit. This is the event concept. You know from the category laws that
w . id
is the same as just w
in about the same way x + 0
is the same as x
. The identity wire is neutral with respect to (.)
. However, now imagine
w . myId
where myId
acts like the identity wire, just resulting in whatever reactive value it depends on, but sometimes it doesn't result at all. Sometimes it ignores the value and just inhibits, in which case the composition itself inhibits. You can interpret myId
as an event wire and read the composition as "w
if myId
":
w . keyDown Space
Then you have your selection operator (<|>)
, which acts like an "or" for events:
w1 . ev1 <|> w2 . ev2 <|> w3
If ev1
inhibits, the rest is tried. Ideally the main wire never inhibits, so you could use stepSessionP_
instead, but it is composed of potentially inhibiting wires. You can also use your own inhibition monoid to have things like quit signals.

- 4,430
- 1
- 18
- 23
-
2That's an ok introduction to netwire, but it doesn't really help me understand how that would look like in an application. – Cubic Feb 09 '13 at 17:08
-
@Cubic: The tutorial covers that. It gives you a simple application you can experiment with. If you have any problems with the example, give a more detailed problem description here. – ertes Feb 09 '13 at 20:28
-
2I understand the example, but all it does is define a single pure wire and step it. While this is fun and all, it doesn't actually show how one can use wires in a real application. I realize that there's not _the_ application for FRP, but I'd like to see at least one. – Cubic Feb 09 '13 at 23:27
-
@Cubic: Well, ideally in a real application your wire is pure, too. However, "pure" doesn't mean "non-monadic". It's quite common to have a reader monad below your wire like `WireM ((->) AppState)` instead of `WireP`. For example in an SDL simulation you could have the current GUI events as your `AppState` and the wire could produce a picture encoded similarly to `Picture` in Gloss or `Image` in vty. The main loop would be responsible for constructing `AppState` and displaying the output image. – ertes Feb 10 '13 at 01:44
-
I have no idea what the m-Parameter that's usually a monad does though - except for fixing the type of stepWire. – Cubic Feb 10 '13 at 12:05
-
For those of you who stumble on this in the future, I thought this was a great example of a full application using netwire. He is also starting (as of today) to work on PACMAN as well.
http://www.reddit.com/r/haskell/comments/1kmes7/building_an_asteroids_clone_in_haskell_using/
http://ocharles.org.uk/blog/posts/2013-08-18-asteroids-in-netwire.html

- 511
- 4
- 5