Suggestion: Add monadic variants of various ...morphism functions. #3
Comments
this would be great, as well as examples of their use. |
Or at implementation in arbitrary categories (of which the Kelsi category in one). |
I can't get it to work. There are at least two ways: (the below might not typecheck)
("Category.Foldable" and "Category.Functor" don't exist)
that's what the (. return) did in cataM. will they be easy to work with? On Wed, May 11, 2016 at 2:56 AM, Christopher King notifications@github.com
(this message was composed with dictation: charitably interpret typos)Sam |
You can implement cataM :: ... => (Base t a -> m a) -> t -> m a
cata :: ... => (Base t b -> b ) -> t -> b
cata' :: ... => (Base t (m a) -> m a) -> t -> m a
cata' = cata
cataM f = (>>= f) . cata (traverse (>>= f)) λ Prelude Data.Functor.Foldable > let cataM f = (>>= f) . cata (traverse (>>= f))
λ Prelude Data.Functor.Foldable > :t cataM
cataM
:: (Monad m, Traversable (Base t),
Data.Functor.Foldable.Foldable t) =>
(Base t b -> m b) -> t -> m b IIRC, when I needed The problem is e.g. > flip runState (0 :: Int) $ cata (\case { Nil -> return (); Cons _ x -> x >> modify' succ }) "foobar"
((),6) |
Added |
FWIW: https://hackage.haskell.org/package/unification-fd-0.10.0.1/docs/Data-Functor-Fixedpoint.html defines |
@phadej Could you elaborate on 'manual definition with non-monadic version' a bit? I understand why giving users control over sequencing is a good thing, but I'm having a bit of trouble thinking of how you'd write something like this for, say, an anamorphism with monadic effects (such as randomness via |
This is fundamentally different than running the anamorphism and trying to sequence some state through all the parts of a functor. As for cataM :: (Traversable (Base t), Monad m, Recursive t) => (Base t c -> m c) -> t -> m c
cataM = cata . (sequence >=>) it can be defined using
I also just checked and found it interesting that when you go to do the opposite construction using anaW :: (Distributive (Base t), Comonad f, Corecursive t) => (f a -> Base t a) -> f a -> t
anaW = ana . (=>= distribute) A distributive functor has a fixed shape. I had some hope when I first thought about it because |
- `fix` is at https://github.com/phadej/fix. I'll give Samuel and Ryan commit access to it, if this patch is accepted. - There are two bits to this: - `Fix`, `Mu` and `Nu` are not essential to the use of `recursion-schemes`, in fact: nothing broke. (Not that we have too many examples). - **If** someone wants to add Fix to `base`, we'd be closer to that. `fix` is essentially "done" package. - There's https://hackage.haskell.org/package/data-fix package, and the *right* way forward would be to use it. I'm in contact with the author to make `data-fix`. There are differences: - has a `(~>)` alias for hylo - `unfix` vs. `unFix` - `data-fix` uses `Eq (f (Fix f))` like instances - provides cataM, anaM, hyloM. We (with edward saying final words) decided not to add them. #3 The list in increasing amount of how hard the topics are to settle on :)
- `fix` is at https://github.com/phadej/fix. I'll give Samuel and Ryan commit access to it, if this patch is accepted. - There are two bits to this: - `Fix`, `Mu` and `Nu` are not essential to the use of `recursion-schemes`, in fact: nothing broke. (Not that we have too many examples). - **If** someone wants to add Fix to `base`, we'd be closer to that. `fix` is essentially "done" package. - There's https://hackage.haskell.org/package/data-fix package, and the *right* way forward would be to use it. I'm in contact with the author to make `data-fix`. There are differences: - has a `(~>)` alias for hylo - `unfix` vs. `unFix` - `data-fix` uses `Eq (f (Fix f))` like instances - provides cataM, anaM, hyloM. We (with edward saying final words) decided not to add them. #3 The list in increasing amount of how hard the topics are to settle on :)
I don't know if it's a good idea and if it isn't actually contained in some other generalized functions you already have, but I feel such monadic functions could be often useful:
If you like the idea, I can provide them.
The text was updated successfully, but these errors were encountered: