In a poll based FRP system, any behavior has a meaningful join
- the sample of
join bb
is the sample of the b
obtained by sampling bb
In push based FRP, any behavior that is a step function composed with other step functions has a meaningful >>=
and join
. Pushing values through >>=
can be described in imperative terms:
- when the argument of the bind changes, evaluate the bind and
- change the current step function to the returned step function
- change the value to the value of the current step function
- when the value of the current step function changes, change the value
Providing a Monad
instance may be slightly undesirable because it is likely to be chosen by preference by library users, even if it is less efficient. For example, the code in this unrelated answer performs more work when a computation was built with >>=
than if it had been equivalently built with <*>
.
Conal Elliott described in declarative terms a join
for simultaneously pushing and polling values for behaviors built from step functions:
-- Reactive is a behavior that can only be a step function
data Reactive a = a `Stepper` Event a
newtype Event a = Ev (Future (Reactive a))
join :: Reactive (Reactive a) -> Reactive a
join ((a `Stepper` Ev ur) `Stepper` Ev urr ) =
((`switcher` Ev urr ) <$> ur) _+_ (join <$> urr )
switcher :: Reactive a -> Event (Reactive a) -> Reactive a
r `switcher` er = join (r `Stepper` er)
where Future
is the type for a value we haven't seen yet, _+_
is the first of the two Future
possibilities to occur, and <$>
is infix fmap
on Future
s. [1]
If we don't provide any other means of creating behaviors than
- the constant function (which is trivially a step function)
- a "stepper" that remembers the most recent value of an event
- application of various combinators of behaviors where the combinators themselves aren't time-varying
then every behavior is a step function and we can use this or a similar Monad
instance for behaviors.
Difficulties only arise when we want to have behaviors that are continuous or are a function of some time other than when an event occurred. Consider if we had the following
time :: Behavior t t
which is the behavior that tracks the current time. A Monad
instance for polling the system would still be the same, but we can no longer push changes through the system reliably. What happens when we make something as simple as time >>= \x -> if am x then return 0 else return 1
(where am t
is true for times in the morning)? Neither our definition of >>=
above nor Elliot's join
can admit the optimization of knowing when the time changes; it changes continuously. The best we could do to >>=
is something like:
- if we know that the argument to the bind is step valued then
- when the argument of the bind changes, evaluate the bind and
- change the current step function to the returned step function
- change the value to the value of the current step function
- when the value of the current step function changes, change the value
- otherwise
- return an abstract syntax tree for this
>>=
For the join
form, we would be reduced to doing something similar, and simply record the AST in the instance that the outer behavior in a join
isn't a step function.
Additionally, anything built using this as an input could change at noon and midnight, whether or not any other event was raised. It would taint everything from that point on with the inability to reliably push events.
From an implementation point of view, our best option would seem to be to continuously poll time
, and replace anywhere it was used with a stepper built from the polling events. This wouldn't update values between events, so now users of our library can't reliably poll values.
Our best choice for an implementation would be to keep an abstract syntax tree of what happened with arbitrary behaviors like these and provide no means to generate events from behaviors. Then behaviors can be polled, but no updates will ever be pushed for them. In that case, we might as well leave it out of the library, and let the user pass around ASTs (which they can get for Free
), and let the user evaluate the entire AST every time it's polled. We can't optimize it any more for the library user, since any value like this can change continuously.
There is one final option, but it involves introducing quite a bit of complexity. Introduce the notion of predictability for properties of continuously varying values and computations of continuously varying values. This would allow us to provide a Monad interface for a larger subset of time-varying behaviors, but not for all of them. This complexity is already desirable in other parts of programs, but I don't know of any libraries outside symbolic math which attempt to address this.
Monad (Behavior t)
instance? I don't see how a correctbehaviorNow
could be anything other than a correctjoin
. – Pasturagereactive-banana
doesn't have aMonad (Behavior t)
instance I assume the implementation somehow inhibits it. – Loganiaceousjoin
is not just forbidden, the type system explicitly forbidsSignal (Signal a)
(yesSignal
is the main reactive component). That means no ability to dynamically createSignal
s. To still be able to create GUIs with a dynamic amount of inputs, Elm decouplesInput
s and representation of an input in the GUI. You can declaratively specify a static amount ofInput
s and have a dynamic amount of GUI widgets that report to thoseInput
s. link – Septavalent