Consider this code, using the Functor
and Foldable
typeclasses:
{-# LANGUAGE DeriveFunctor, DeriveFoldable #-}
data Foo a = Foo (Maybe a) [a] deriving(Show, Functor, Foldable)
fmap (+1) (Foo (Just 1) [2,3,4]) -- result: Foo (Just 2) [3,4,5]
sum (Foo (Just 1) [2,3,4]) -- result: 10
Is there some analogous set of typeclasses that can operate on higher-kinded type parameters? For example:
data Bar a = Bar (a Int) (a Bool)
somethingLikeFmap :: forall t f g. SomethingLikeFunctor t => (forall a. f a -> g a) -> t f -> t g
somethingLikeAll :: forall t f. SomethingLikeFoldable t => (forall a. f a -> Bool) -> t f -> Bool
somethingLikeFmap listToMaybe (Bar [1,2,3] [False, True]) -- desired result: Bar (Just 1) (Just False)
somethingLikeAll null (Bar [1,2,3] [False, True]) -- desired result: False
somethingLikeAll null (Bar [] []) -- desired result: True
I suspect you are looking for
rank2classes
. For example:EDIT: Or possibly
mmorph
, though I suspectrank2classes
may prove more useful to you.