Why does `changes` return `Event t (Future a)`

邮差的信 提交于 2020-01-14 07:33:25

问题


The changes function has type Frameworks t => Behavior t a -> Moment t (Event t (Future a)). Future is abstract and there is only one function that consumes it (reactimate').

However, I can easily write the following function:

changes' :: Frameworks t => Behavior t a -> Moment t (Event t a)
changes' b = fmap (fmap const b <@>) (changes b)

to get a normal (non-Future) event.

Is something wrong with that function? If not, why does the original changes function have a more restrictive type?


回答1:


The function changes returns different values than the function changes' that you describe. The key point is the following:

Consider a Behavior defined by stepper (or accumB), which happens to change at time t0. What value does the Behavior have at this moment in time? The answer is that the Behavior takes on the new value for all times that are strictly larger than the time of change, t > t0, and that it still has its old value at time t0. In other words, the changes' function returns an event whose values are the old values of the Behavior at the time of change. In contrast, the changes function returns the new ("future") values. For various reasons that have to do with recursion, the new values are wrapped in a Future type, so that they cannot be accessed until the reactimate' phase.

EDIT: Tobias has drawn a picture for illustration:



来源:https://stackoverflow.com/questions/26980819/why-does-changes-return-event-t-future-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!