haskellfrparrow-abstractionnetwire

Convert from arrow notation


I'm still trying to get a hang of the parallels between arrow notation and the semantics of the Arrow typeclasses defined in Haskell. In particular, this question seems to have a very canonical example of a small counter written with arrow notation:

counter :: ArrowCircuit a => a Bool Int
counter = proc reset -> do
        rec     output <- returnA -< if reset then 0 else next
                next <- delay 0 -< output+1
        returnA -< output

Can someone show me how to convert this back into Haskell2010 without arrow notation?


Solution

  • {- |
                         +---------+
     >Bool>-------------->         |
                         |         >------------------>Int>
           +---------+   |  arr f  |
      /----> delay 0 >--->         >---------\
      |    +---------+   |         |         |
      |                  +---------+         |
      |                                      |
      \--------------------------------------/ 
    
     -}
    counter' :: ArrowCircuit a => a Bool Int
    counter' = loop $ second (delay 0) >>> arr f
      where
        f (reset, next) = let output = if reset then 0 else next
                              next' = output + 1
                           in (output, next')
    

    The recursive rec part is implemented using loop. The inner part that converts reset to output using next (and producing new next value) is just a pure function with two inputs and two outputs.