[Haskell-cafe] Desugaring ArrowLoop notation
esz at posteo.de
Sat Jan 28 14:07:48 UTC 2017
> mean5 :: Fractional a => Circuit a a
> mean5 = proc value -> do
> (lastTot, lastN) <- delay (0,0) -< (tot, n)
> let (tot, n) = (lastTot + value, lastN + 1)
> let mean = tot / n
> returnA -< mean
> So I ask: what's the sour version of `mean5`?
The translation is very similar to the one for 'mfix', except that you
need to account for the side channel in the input:
loop $ proc (value, ~(tot', n')) -> do
(lastTot, lastN) <- delay (0,0) -< (tot', n')
let (tot, n) = (lastTot + value, lastN + 1)
id -< (tot / n, (tot, n))
You need the values 'tot' and 'n' to be in scope as input to the 'delay'
action. To achieve that you literally just return them as the second
output component, and 'loop' will feed them back as the second input
component. That's how they get in scope for 'delay's input.
This is not the exact way it's desugared (which is probably a lot
uglier), but it's equivalent and should give you an intuition for how
I'm not entirely sure the lazy pattern is necessary or even possible.
If it's possible, it's probably also necessary.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 487 bytes
Desc: not available
More information about the Haskell-Cafe