I am working on a modification to Control.Foldl that provides Category and Arrow instances. My definition is as follows:
data FoldlCat ab = forall x c. FoldlCat (x -> a -> c) x (c -> x) (c -> b)
where the first argument denotes the function "step" ( savedState -> newInput -> intermediateValue ), the second - for the initial savedState , the third - the save function ( intermediateValue -> savedState ), and the last - the function "extract" ( intermediateValue -> newOutput ).
So, something like map (*10) . scanl (+) 0 map (*10) . scanl (+) 0 can be expressed as FoldlCat (+) 0 id (*10) .
The main purpose of the save function is to facilitate the definition of id and arr as follows:
id = FoldlCat (\_ x -> x) () (const ()) id
Now I'm trying unsuccessfully to come up with an instance of ArrowLoop . I don’t see the reasons why this is impossible, but it’s not convenient for me with concepts like fix . My best attempt is still typical, but loops forever.
instance ArrowLoop FoldlCat where loop (FoldlCat sbad) = FoldlCat step b (a . snd) fst where step x = loop' d (sx) loop' fgx = let ~(v, ~(c,d)) = let ~v = g (x,d) in (v, fv) in (c, v)
I would appreciate it if someone could share their approach in defining such an instance (extra recognition if it works with a strict tuple in the battery inside (.) ) Or explain why this is not possible or advice on the best structure for FoldlCat.
{-
EDIT . Although I'm not sure how and why it works, fixing the strictness of step in (.) (I.e. removing bang in let !a' = step1 ay ) seems to do this example work.
source share