[Haskell-cafe] Question, re: using Automaton

Ertugrul Soeylemez es at ertes.de
Sun Oct 16 16:41:07 CEST 2011

Captain Freako <capn.freako at gmail.com> wrote:

> Encapsulating an automaton by running it on a stream of inputs,
> obtaining a stream of outputs.
> Typical usage in arrow notation:
> 	proc p -> do
> 		...*		ys <- (|runAutomaton (\x -> ...)|) xs*
> Here xs refers to the input stream and x to individual elements of
> that stream. ys is bound to the output stream.  Could someone replace
> the ellipses w/ an expression that would compile and make sense?
> (I'm really struggling, trying to understand this example.)

Your expression should in fact compile and run out of the box.  I
haven't tried your example, but I use the (| |) syntax form frequently.
See the GHC manual [1] to see how it is translated.


    proc x -> do
        y <- trans subArrow -< (f x, g x)
        someComp3 -< f y

    subArrow = proc (x1, x2) -> do
        someComp1 -< x1
        y <- someComp2 -< x2
        returnA -< h y

The (| |) notation can be used to write this more concisely:

    proc x ->
        y <- (| trans (do
            someComp1 -< f x
            y <- someComp2 -< g x
            returnA -< h y |)
        someComp3 -< f y

It basically allows you to construct an arrow computation (subArrow) to
be passed to an arrow function (trans) using the outer computation's
bound variables without having to construct tuples to pass them around.
Under the hood this is translated to passing tuples.

Note:  I'm assuming GHC.

[1] <http://www.haskell.org/ghc/docs/latest/html/users_guide/


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Haskell-Cafe mailing list