[Haskell-cafe] ArrowLoop and streamprocessors

Mathijs Kwik bluescreen303 at gmail.com
Wed Mar 30 23:29:34 CEST 2011


Hi all,

I'm playing around a bit with arrows (more specifically, something
like a CPS style streamprocessor as described in "Generalising Monads
to Arrows" by John Hughes).
A part of my program takes inputs/signals from 2 sources.
The sources don't produce output at the same rate, and this part is
able to handle inputs independently.
So I figured I need Either A B ~> O for this part.

handleA :: A ~> O
handleB :: B ~> O

someBox :: Either A B ~> O
someBox = handleA ||| handleB

So far so good.

Now, further downstream (someBox >>> otherBox) there is.

otherBox :: O ~> Either C B

Let's say C are "normal" outputs, and B are control signals that need
to get wired back to someBox.
Control signals are rare, so maybe there's 1000 C outputs and only 1 B
output in a certain timeframe.
Also note that in this CPS style streamprocessing, there's no 1-on-1
relation between input and output, so on 1 input (O), otherBox might
produce 2 outputs (2 times C), or 4 outputs (3 times C and 1 time B).

To "wire back" B's to someBox, I'm pretty sure I need to use ArrowLoop.
But (loop :: a (b, d) (c, d) -> a b c) uses tuples, which means the
processing will only continue when both inputs are available.
So if I turn someBox into (A, B) ~> O and otherBox into O ~> (C, B),
the processing will instantly halt, waiting for a B to arrive after
the A comes in.

I know about the 'delay' trick that usually works for loops, where an
output value is just inserted before the actual outputs, but that
won't help in my case, because otherBox doesn't produce B's at the
same rate that someBox receives A's, so by inserting a dummy B,
someBox will only "run" once.
Also, there's no relation between the number of A inputs someBox
receives and the number of inputs for otherBox, so I also can't have
otherBox just insert "noop" signals after every "run".

So loop really doesn't seem to help here, but I couldn't find another
way either to feed outputs back into the system.
What I need is:
Either A B ~> Either C B -> A ~> C

Does such a thing exist?

Thanks,
Mathijs

PS: I remember reading about (unfortunately didn't get the examples
working) Reactive (FRP), which has the concept of Events and Behaviors
I think Yampa has something like it too. A Behavior has a value at all
times, which was updated by Events. That sounds a bit like it might
solve my problem, since I need something that contains "Latest Control
Signal was X" that updates when B's are produced, but that doesn't
have to be put "on the wire" as a stream of events (because I can't
determine how many events to put to match incoming A's. However, I
hope there is another solution because I found Reactive and Yampa
quite hard to grasp.



More information about the Haskell-Cafe mailing list