[Haskell-cafe] Proposal: Subcomputations in arrow notation

Paterson, Ross R.Paterson at city.ac.uk
Thu Sep 22 00:40:22 CEST 2011


Ertugrul Soeylemez writes:
>  I find myself writing the following very often:
>
>     system :: Wire IO () String
>     system =
>         proc _ -> do
>             botAddPeriod <- succ ^<< noise -< ()
>             botAddSpeed <- noise1 -< ()
>             botAddStart <- noise1 -< ()
>             botMsg <- event addBot -< (botAddPeriod, botAddSpeed, botAddStart)
>
>             bots <- manager -< ((), maybe MgrNop id botMsg)
>             let botStr = concatMap (printf "%8.2") . M.elems $ bots :: String
>             identity -< printf "Bot positions: %s" botStr
>
>         where
>         addBot :: Wire IO (Double, Double, Double) (MgrMsg Int IO () Double)
>         addBot =
>             proc (addPeriod, addSpeed, addStart) -> do
>                 periodically -< addPeriod
>                 botId <- identifier -< ()
>                 identity -< MgrAdd botId (constant addSpeed >>> integral addStart)

If addPeriod is supposed to be the same as botAddPeriod, etc, this should be
equivalent:

    system :: Wire IO () String
    system =
        proc _ -> do
            addPeriod <- succ ^<< noise -< ()
            addSpeed <- noise1 -< () 
            addStart <- noise1 -< () 
            botMsg <- (|event (do
                periodically -< addPeriod
                botId <- identifier -< ()
                identity -< MgrAdd botId (constant addSpeed >>> integral addStart))|)

            bots <- manager -< ((), maybe MgrNop id botMsg)
            let botStr = concatMap (printf "%8.2") . M.elems $ bots :: String
            identity -< printf "Bot positions: %s" botStr

See the GHC Arrow notation documentation for more about the banana brackets,
which let you use user-defined control structures like event.


More information about the Haskell-Cafe mailing list