Convert from Arrow Notation

I'm still trying to find parallels between the notation of arrows and the semantics of the Arrow style classes defined in Haskell. In particular, this question seems like a very canonical example of a small counter written with an arrow:

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 to Haskell2010 without arrow designation?

+6
source share
2 answers
 {- | +---------+ >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 part of rec implemented using loop . The inner part, which converts reset to output using next (and creates a new value for next ), is just a clean function with two inputs and two outputs.

+11
source

Concurrency in functional code will be to use the op state. in crease

 import Data.List counter :: (Int, Int) -> Bool -> (Int, Int) counter (_, previous_next) reset = let output = if reset then 0 else previous_next next = output +1 in (output, next) runCounter :: [Bool] -> (Int, Int) runCounter = foldl' counter (0,1) main = do let resets = [True, False, True, False, False] result = fst $ runCounter resets print result 
+3
source

Source: https://habr.com/ru/post/973685/


All Articles