[Haskell-cafe] Desugaring ArrowLoop notation

Ertugrul Söylemez esz at posteo.de
Sat Jan 28 14:07:48 UTC 2017

>     mean5 :: Fractional a => Circuit a a
>     mean5 = proc value -> do
>     rec
>         (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:

    mean5 =
        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
'loop' works.

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...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170128/75335109/attachment.sig>

More information about the Haskell-Cafe mailing list