I'm getting a bit confused with too many low-level details. Some of my responses are educated guesses, so maybe there's implementations that manage to circumvent what I say. Please, correct me where I'm wrong.

On 19 February 2018 at 03:31, Ertugrul Söylemez <esz@posteo.de> wrote:
> I suspect I may not be understanding precisely what you mean. Perhaps
> you can describe this in more detail or with an example?

The easiest way to see the difference is by looking at some of the
combinators.  Notice that things like 'hold', 'scan'/'accum', and 'tag'
are real functions.  In a first-class FRP system these would have types
like the following:

    hold :: a -> Event a -> Moment (Behaviour a)
    scan :: a -> Event (a -> a) -> Moment (Event a)
    tag  :: Behaviour (a -> b) -> Event a -> Event b

The Moment monad is not inherent to the way the underlying state machine
is constructed, but acts merely as a provider for the notion of "now".
Since 'tag' doesn't need that notion, it's a completely pure function.

Well, in a way. Yes, it can be a pure function, and an event can somehow be a delayed computation of how/when it is actually produced, computed/consumed the moment you want to actually evaluate the network.

Saying that they are pure would be just fine if Behaviours did not depend on the outside world (that is, if they were "calculated" from pure haskell functions). But I don't think they are. Not always. Not if you want to depend on any external user input.

In Reflex (and I'm not trying to discuss the particularities of this implementation), yes, Behaviour and Event are types in a family, but the actual definitions in Spider I can seee are records of IORefs with bangs. Far from pure.
 
You can have that function in AFRP as well:

    fmap :: (a -> b) -> Event a -> Event b

However, unlike 'fmap', 'tag' makes sense in a pure context.  You can
pass an Event and a Behaviour to a different thread via an MVar, combine
them there, then send the result back, and it will still work in the
context of the greater application (no isolated state machines).
 
I don't see how you cannot do that with wires. For instance, you can send a Wire m () (Event b), and a Wire m () (a -> b), and compose them in a pure context. Then you can bring that back and use it.

  You
can hold an event in any concurrent thread, etc.

Can you use it without doing IO and executing the computation associated to calculating/polling the behaviour? If so, it must be because the FRP evaluation method has some inherent thread-safety (I you need IO + more for that). Wouldn't you be able to put that thread safety in your monad, and then use it with MSFs/Wires?


Another example is that if the underlying monad is nontrivial (say IO)
you can't easily split behaviours in a pure context in AFRP.

You can, but you need a monad such that: (,) <$> ma <*> ma == (\x -> (x,x)) <$> ma.

Is this called idempotent?

But to implement any form of Classic FRP or Reactive Programming on top of MSFs, you want that kind of monad.

  This
restriction does not exist in first-class FRP:

Well, it is not exposed to the user, but someone must have thought about it and solved it. Duplication of effects is inherent to having monadic computations associated to obtaining the values of behaviours. If you don't cache for a given timestamp, you duplicate effects.

The same mechanism they used could be applicable to your monadic AFRP variant.


    unzipB :: Behavior (a, b) -> (Behavior a, Behavior b)
    splitE :: Event (Either a b) -> (Event a, Event b)

In AFRP you always have to do it in the context of the underlying state
machine, i.e. MSF/SF/Wire, which means that AFRP forces you to manage
all data structures holding reactive values as part of it or, again,
have isolated state machines.  With first-class FRP there is nothing
wrong with keeping a data structure of behaviours in an MVar and have
two concurrent threads modify it:

    MVar (Map K (Behaviour String))
 
AFRP requires the following instead, and unless all changes are planned
within the state machine and communicated via 'A' changes actually build
up in terms of complexity (you can't just keep composing it with more
and more MSF actions for free):

    MVar (MSF IO A (Map K String))

I could be wrong, but I think most of your problems go away with the kind of monad I mentioned.


Let me make clear that you can express all of these things in AFRP.  In
fact it's easily more powerful than first-class FRP, because if the
system exposes it, you get full access to the expressivity of what is
basically a generic state machine.  Just to provide one example: there
is no first-class counterpart to 'manage' from wires, because that
combinator only really makes sense in the context of the underlying
state machine.  But all of this comes at the expense of giving up
first-class behaviours and events in the above sense.

If this still doesn't convince you, I strongly suggest that you give
reflex a try.  It has a very similar controller interface to AFRP
(stepWire/unMSF) in that it gives you control over the main loop, so it
shouldn't feel too alien.

As a final remark due to all these issues and more with AFRP most of my
research in the past few years went into getting rid of the A while
retaining most of its advantages.

I cannot say I like arrow notation, or inputs based on tuples. We need more work on this.

However, I decided to embrace the A and I am finding a lot of extensions and guarantees that are possible, or easier, thanks to that.
 
 [...]


Ivan