简体   繁体   English

功能反应式编程的“信号”表示是否正确?

[英]Is the 'Signal' representation of Functional Reactive Programming correct?

I have been researching FRP and found a bunch of different implementations. 我一直在研究FRP,发现了一堆不同的实现。 One model I have seen is one I will refer to as the 'Signal' representation. 我见过的一个模型是我将其称为“信号”表示的模型。 This essential combines Events and Behaviours into one entity. 这一基本要素将事件和行为结合到一个实体中。

Firstly, a Signal is an object thats value is a Behaviour. 首先,Signal是一个值为行为的对象。 Secondly, a Signal has an Event 'stream' that can be seen and operated on as a standard data structure (you can use 'each', 'map' and 'filter' etc on the Signal to define how Events are reacted to). 其次,信号有一个事件'流',可以作为标准数据结构查看和操作(您可以在信号上使用'each','map'和'filter'等来定义事件如何反应)。 For example I can do this (where 'time' is a Signal representation of time): 例如,我可以这样做(其中'时间'是时间的信号表示):

time.each { t => print(t) } // every time there is a 'tick' the current time is printed
a = time * 5 //where 'a' would be a dynamic value always up to date with time

Is this representation of FRP correct or are there any problems? FRP的这种表示是正确的还是有问题? I quite like the way this works and also how simple it is to describe personally but I'm not sure its right. 我非常喜欢它的工作方式以及个人描述的简单方法,但我不确定它的正确性。

Unfortunately, coalescing "event" and "behavior" into a single entity "signal" doesn't work so well. 不幸的是,将“事件”和“行为”合并为单个实体“信号”并不能很好地工作。

Most signal-based FRP implementations that I know end up creating an additional "event"-like type along the lines of 我所知道的大多数基于信号的FRP实现最终会创建一个类似于“事件”的附加类型

type Event a = Signal (Maybe a)

So, the concept of events doesn't go away, and there is no real simplification. 因此,事件的概念不会消失,也没有真正的简化。 In fact, I would argue that the signal type is a semantic complification. 事实上,我认为信号类型是语义合并。 Signals are only popular because they are easier to implement. 信号很受欢迎,因为它们更容易实现。

The main argument against signals is that they cannot represent continuous time behaviors, because they have to cater to the discrete events. 反对信号的主要论据是它们不能代表连续的时间行为,因为它们必须迎合离散事件。 In Conal Elliott's original vision , behaviors were simple continuous functions of time 在Conal Elliott 最初的愿景中 ,行为是简单的连续时间函数

type Behavior a = Time -> a
-- = function that takes the current time as parameter and returns
--   the corresponding value of type  a

In contrast, signals always are always discretized and usually associated with a fixed time step. 相反,信号始终是离散的,并且通常与固定的时间步长相关联。 (It is possible to implement both events and behaviors on top of a variable time step signal, but it's not a good abstraction by itself.) Compare this to an event stream (可以在可变时间步长信号的基础上实现事件和行为,但它本身并不是一个好的抽象。)将此与事件流进行比较

type Event a = [(Time,a)]
-- list of pairs of the form (current time, corresponding event value)

where the individual events don't necessarily occur in regularly spaced time intervals. 其中个别事件不一定以规则间隔的时间间隔发生。

The argument for the distinction between behaviors and events is that their API is quite different. 行为和事件之间区别的论点是它们的API非常不同。 The main point is that they have different product types: 重点是他们有不同的产品类型:

(Behavior a , Behavior b) = Behavior (a,b)
(Event a    , Event b   ) = Event (a :+: b)

In words: a pair of behaviors is the same as a behavior of pairs, but a pair of events is the same as an event from either component/channel. 用语言来说:一对行为与对的行为相同,但是一对事件与来自任一组件/通道的事件相同。 Another point is that there are two operations 另一点是有两个操作

(<*>) :: Behavior (a -> b) -> Behavior a -> Behavior b
apply :: Behavior (a -> b) -> Event a    -> Event b

that have almost the same type, but quite different semantics. 它们具有几乎相同的类型,但语义却截然不同。 (The first updates the result when the first argument changes, while the second doesn't.) (第一个参数更改时,第一个更新结果,而第二个参数更改时,第一个更新结果。)

To summarize: signals can be used for implementing FRP and are valuable for experimenting with new implementation techniques, but behaviors and events are a better abstraction for people who just want to use FRP. 总结一下:信号可用于实现FRP,对于尝试新的实现技术很有价值,但对于只想使用FRP的人来说,行为和事件是更好的抽象。

(Full Disclosure: I have implemented an FRP library called reactive-banana in Haskell.) (完全披露:我在Haskell中实现了一个名为reactive-banana的FRP库。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM