Comments (1)
Let's start to specify the semantics that ought to be tested here. Much of this is taken/adapted from Conal Elliott's paper on Push-Pull FRP. We assume a testable notion of equality denoted as ==
. Furthermore, trait bounds like Send + Sync + Clone + 'static
are implied as required and omitted for better readability.
Streams
Monoid
For reference: Wikipedia: Monoid
The streams of type Stream<T>
should form a monoid under Stream::merge
with Stream::never()
being the identity. Thus, for arbitrary a, b, c: Stream<T>
the following properties hold:
- left identity:
Stream::never().merge(&a) == a
, - right identity:
a.merge(&Stream::never()) == a
, - associativity:
a.merge(&b).merge(&c) == a.merge(&b.merge(&c))
.
While this certainly holds when all events are fired in separate transactions, there could be an issue with events from the same transaction.
Functor
For reference: Wikipedia: Functor (type theory)
Streams form a functor under Stream::map
, i.e.
- the identity function is preserved:
a.map(|x| x) == a
, - it respects function composition:
a.map(f).map(g) == a.map(|x| g(f(x)))
.
Here, f: Fn(T) -> U
and g: Fn(U) -> V
if a: T
.
Monad
For reference: Wikipedia: Monad (functional programming)
Streams in principle form a monad under a bind
or flat_map
operation defined as:
fn bind<T, U, F: Fn(T) -> Stream<U>>(a: &Stream<T>, f: F) -> Stream<U> {
a.map(f).switch()
}
(Question: Push-Pull FRP delays past event occurrences until the generation of the inner event. Is this necessary for the algebraic laws to hold? And how does this compare to the non-lazy transactional environment Carboxyl's working in?)
The neutral element is less trivial to construct in Carboxyl. In Push-Pull FRP it is modelled as a stream that fires exactly once in the infinite past. We cannot do this. However, maybe it is possible to mimick it somehow like this:
fn return<T>(value: T) -> Stream<T> {
let sink = Sink::new();
let stream = sink.stream();
sink.send(value); // Too early?
stream
}
(We can't call it return
because that's a keyword in Rust, but I wanted to use Haskell terms as a reference.)
The problem here is obviously, that the value will be sent before any consumers of the stream can listen to it. Somehow streams must be constructed in such a way, that return(x).hold(y)
always samples as x
. This may require some tweaks to the internals. (E.g. an event could hypothetically store its last occurence as an optional value.)
Nonetheless, for adequate implementations of return
and bind
the monad laws state that
return
acts as a neutral element:return(x).bind(f) == f(x)
,a.bind(return) == a
,
a.bind(f).bind(g) == a.bind(|x| f(x).bind(g)
.
The monad implies an applicative functor but this is not a particularly useful notion here (see Elliott paper).
Signals
Functor
Signals form a functor under unary lifts. So for a: Signal<T>
, f: Fn(T) -> U
, g: Fn(U) -> V
lift!(|x| x, &a) == a
,lift!(|x| g(f(x)), &a) == lift!(g, &lift!(f, &a))
.
Applicative functor
For reference: Haskell docs on Applicative
To make signals an applicative we need a pure
function, which in Carboxyl's API is the same as Signal::new
, creating a signal with a constant value. Further we need a combining operator like this:
fn cmb<A, B, F: Fn(A) -> B>(f: &Signal<F>, a: &Signal<A>) -> Signal<B> {
lift!(|f, a| f(a), f, a)
}
Note: A (small) technical problem here is that most functions in Rust are not Clone
. This could be alleviated by wrapping it in an Arc
. However, in the long-term this should be addressed by an upstream RFC, as this work-around introduces a performance penalty. For simplicity, we'll assume that closures are Clone
here.
Now, the following laws must hold:
- identity:
cmb(&pure(|x| x), &a) == a
- composition:
cmb(&cmb(&cmb(&pure(comp), &f), &g), &h) == cmb(&f, &cmb(&g, &h))
wheref
,g
,h
are signals of composable functions andcomp
denotes function composition. - homomorphism:
cmb(&pure(f), &pure(x)) == pure(f(x))
- interchange:
cmb(&f, &pure(x)) == cmb(&pure(|g| g(x)), &f)
Monad
Elliott mentions that signals also form a monad, but does not elaborate this. Presumably this works with similarly to streams using switch
to construct the bind
operation. Constructing return
should be easier as it is simply Signal::new
. This monad may imply the applicative above.
from carboxyl.
Related Issues (20)
- Relicense to MPL 2.0 HOT 8
- Excessive cloning in Stream::fold HOT 8
- status of the project and functional programing in rust ? HOT 12
- Interop with reactive in other languages? HOT 3
- Comparison to Sodium? HOT 1
- Merge streams with function HOT 5
- Sink::feed methods should take IntoIterator
- Use Result<(), EquivError> as return type for stream_eq
- Return intermediate values from `Signal::cyclic`
- Create stream from an IntoIterator type
- Deal with dynamic switching more explicitly HOT 1
- Does not compile on current rust git HOT 2
- Can't compile crate with rust 1.2 HOT 3
- Marble support HOT 3
- Cyclic SignalMut definition HOT 5
- Test semantics of SignalMut HOT 1
- Change argument sequence of scan closure HOT 1
- Add Signal::map
- Remove SignalMut
- Rename `scan` to `fold`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carboxyl.