gelisam / frp-zoo

Comparing many FRP implementations by reimplementing the same toy app in each.
490 stars 31 forks source link

Reactive-banana is continuous #58

Closed walseb closed 5 years ago

walseb commented 5 years ago

Source: https://apfelmus.nfshost.com/blog/2015/10/29-frp-banana-1-0.html "... reactive-banana is a mature library for functional reactive programming (FRP) that supports first-class Events and Behaviors, continuous time ..."

gelisam commented 5 years ago

@apflemus must have a different definition of "continuous time" than the definition I use in this project (see the Readme: "an FRP library in the style of Conal Elliott, meaning that signals are functions from time to values. This built-in notion of time allows interpolation between values, and other time-based transformations)! reactive-banana does not have a time :: Behavior Double builtin; it does not provide any time-manipulation primitives such as taking the derivative of a Behaviour Double over time or interpolating between sampled values; its behavior is deterministic, not dependent on some sampling rate; and the network is actuated by discrete external events, not by an internal time ticker. Like most FRP libraries, reactive-banana chose not to include that part of Conal's original formulation of FRP; and in my book, that's a good thing :)

walseb commented 5 years ago

Thanks a lot for the explanation! So behaviours in reactive-banana are using discrete time? And can you explain why not using continuous time is a good idea? I recently listened to a talk and podcast where Conal Elliott said that continuous time is both simpler to work with and faster to run. But maybe the technology just isn't there yet? I want to make a game with a library like the one he describes but his own library apparantly has lots of bugs. Do you think reactive-banana is the best fit for this? Also his handle is @HeinrichApfelmus

gelisam commented 5 years ago

So behaviours in reactive-banana are using discrete time?

I don't think "discrete time" is a useful FRP category.

Behaviour a is modelled as a function Time -> a. It's not that some FRP systems use a discrete representation for time type Time = Int while others use a representation which is closer to Real numbers. Rather, some expose the time to the user, while others leave it implicit; and among those which do expose the time, some offer time-based signal manipulation functions. This last case is what I mean by "continuous time".

In reactive-banana, its internal time ticks forward each time an event occurs, but the number of events which have occurred so far is not a very useful metric, so reactive-banana does not expose its internal time to the user.

If we plot the events against the clock time (not the internal time) at which they occur, there is no restriction on the clock time at which those events may be placed, it's not as if each second was divided into e.g. 60 frames and that each event had to be placed into a frame.

However, behaviors do not look like a continuous curve (even though it is pedagogically useful to think of them that way), because its value only changes at discrete times, when an event occurs.

And can you explain why not using continuous time is a good idea?

In Conal's model, the physical position of the mouse peripheral changes continuously, and the behavior of our program is defined in terms of that function from a Real number to a 2D position. You can define another signal which is the derivative of that function, giving you the direction in which the mouse is moving at every point in time, override that with zero movement unless the user is drag-and-dropping an icon in order to obtain the direction in which that icon should move at every point in time, and then integrate that direction back into a position in order to obtain the position of the icon at every point in time.

That's a nice mathematical model, but then the actual behavior of your program is only an approximation of that ideal. The mouse driver only samples the mouse position at a fixed interval, the numerical derivation and integration have small errors which accumulate over time, and at the end of the day, the icon doesn't quite stick to your mouse cursor.

I recently listened to a talk and podcast where Conal Elliott said that continuous time is both simpler to work with and faster to run. But maybe the technology just isn't there yet?

Even though I don't agree with all of his ideas, I love listening to Conal explaining them, he has such a gift for explaining himself clearly. Do you have a link?

I want to make a game with a library like the one he describes but his own library apparantly has lots of bugs. Do you think reactive-banana is the best fit for this?

I made a game using an earlier version of reactive-banana, and it was an enjoyable experience, I do recommend it!

walseb commented 5 years ago

Thanks very much for this answer! It cleared up a lot.

Even though I don't agree with all of his ideas, I love listening to Conal explaining them, he has such a gift for explaining himself clearly. Do you have a link?

Yes, here is the podcast episode. The talk is really long so I haven't gotten through it yet but it seems very interesting.