haskell - typeclass for repetitive actions until fixed point -
i noticed common pattern of executing action until stops having effects, when 1 knows signifies fixed point (ie, there can no future effects). there typeclass this?
is covered monadfix? looking @ code, seems be, scared off wiki page "it tempting see “recursion” , guess means performing actions recursively or repeatedly. no."
it seems me fixed points dual of identities. is, identity disappears when combined non-identity (0 (+), 1 (*), [] append, etc). whereas fixed point causes non-fixed point disappear under 'relax' operation below. there way formalize duality, , useful so? ie, there relationship between monadplus and/or monoid , monadrelax?
lastly, notice relax unfold/anamorphism. better express such?
{-# language multiparamtypeclasses, functionaldependencies #-} import control.monad.loops (iterateuntilm) -- cabal install monad-loops -- states relax fixed point under step class monad m => monadrelax m s | s -> m isfixed :: s -> bool step :: s -> m s -- (not always): step s = return s iff isfixed s relax :: monadrelax m s => s -> m s relax = iterateuntilm isfixed step
what asking for, plain fix
:
cd :: (monad m) => int -> int -> m int cd = fix (\f c -> if == 0 return c else f (c+i) (i-1))
this repeat computation, until i
becomes 0. (i added c
have meaningful computation; assume s=(int,int)
1 of them being rolling sum , other counter)
> cd 0 4 :: [int] [10]
this same as:
relax = fix (\f s -> if isfix s return s else f (step s))
i believe, definition of iterateuntilm
.
Comments
Post a Comment