Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonoApply #159

Open
eschnett opened this issue Jan 24, 2018 · 1 comment
Open

MonoApply #159

eschnett opened this issue Jan 24, 2018 · 1 comment

Comments

@eschnett
Copy link

It seems that a type class

class MonoFunctor mono => MonoApply mono where
    liftF2 :: (Element mono -> Element mono -> Element mono) -> mono -> mono -> mono

would be convenient to have.

Since you can't store functions in a MonoFunctor, it seems that the obvious generalizations liftF3 etc. need to be defined manually, which is inconvenient.

An alternative and more generic definition could be

class MonoFunctor mono => MonoCotraversable mono where
    cotraverse :: (Foldable f, Functor f) => (f (Element mono) -> Element mono) -> f mono -> mono

I don't know whether the Functor constraint here is sufficient -- I've seen examples where others defined Cotraversable with a stronger Comonad constraint on f instead.

An example use case would be

data IntList = IntList [Int]
instance MonoFunctor IntList where
    type Element IntList = Int
    ofmap f (IntList xs) = IntList (map f xs)

instance MonoApply IntList where
    oliftF2 f (IntList xs) (IntList ys) = IntList (zipWith f xs ys)
max2 = oliftF2 max

instance MonoCotraversable IntList where
    ocotraverse f xss = IntList $ map f (sequenceA xss)
maxMany = ocotraverse maximum

Haskell has traverse f = sequenceA . fmap f. What I'm suggesting here is similar to cotraverse f = fmap f . sequenceA. Since MonoTraversable by construction can't offer sequenceA, it seems this should be provided explicitly.

@snoyberg
Copy link
Owner

snoyberg commented Jan 26, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants