Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full example for netwire?

Tags:

haskell

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.

like image 301
Cubic Avatar asked Feb 09 '13 11:02

Cubic


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.

like image 82
ertes Avatar answered Oct 20 '22 13:10

ertes


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

https://github.com/ocharles/netwire-classics/

like image 6
Nelson Avatar answered Oct 20 '22 15:10

Nelson