streaming icon indicating copy to clipboard operation
streaming copied to clipboard

Generalize copy, perhaps

Open treeowl opened this issue 8 years ago • 7 comments

I don't actually know if this "makes sense", but it works for Of and (,), and it kind of smells right.

copy :: (Monad m, Comonad f) => Stream f m r -> Stream f (Stream f m) r
copy = Effect . return . loop where
  loop str = case str of
    Return r -> Return r
    Effect m -> Effect (fmap loop (lift m))
    Step f -> Effect (Step (extend (Return . Step) (fmap loop f)))

Comonad is actually overkill; we only really need Extend from Data.Functor.Extend in semigroupoids. Unfortunately, that's not a superclass.

treeowl avatar Sep 08 '17 02:09 treeowl

I was talking with Cale Gibbard, who said it seemed to go with an extract-like generalization of effects, which is very definitely possible as well, forming some sort of higher-level comonad or something:

effects :: (Monad m, Comonad f) => Stream f m r -> m r

treeowl avatar Sep 08 '17 04:09 treeowl

If you only need extend you could also ask that to be passed in as a parameter. Not sure if that permits other "interesting" extends

ocharles avatar Sep 08 '17 08:09 ocharles

@ocharles, that's a good idea for avoiding the dependency. It's kind of an ugly parameter, and would require a separate function (rather than replacing copy), but that seems fairly tolerable.

treeowl avatar Sep 08 '17 18:09 treeowl

Well, I suggest it more from wondering if as you only want Extend you really do just want to supply that function. Plus, maybe it means something interesting (like traverse being useful when given as a parameter as it's a traversal)

ocharles avatar Sep 08 '17 18:09 ocharles

@ocharles, yes, that's a good point too, and the answer is ... yes! We can get

copygen :: (Monad m, Functor f)
        => (forall a b. (f a -> b) -> f a -> g b)
        -> Stream f m r -> Stream f (Stream g m) r

treeowl avatar Sep 08 '17 18:09 treeowl

Actually, we can even get

copygen :: (Monad m, Functor g)
        => (forall a b. (f a -> b) -> g a -> h b)
        -> Stream g m r -> Stream f (Stream h m) r

treeowl avatar Sep 08 '17 19:09 treeowl

This is no longer the official repo for streaming. Please continue discussion of this issue here.

andrewthad avatar Sep 13 '17 13:09 andrewthad