-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Deriving generalized functors with GHC.Generics -- -- Derive fmap, and other fmap-like functions where the -- parameter of the functor could occur anywhere. -- -- See the README for details. @package generic-functor @version 0.2.0.0 module Generic.Functor.Internal.Implicit -- | Core of multimap multimapI :: forall arr x y. MultimapI arr x y => arr -> x -> y multitraverse :: forall f arr x y. Multitraverse f arr x y => arr -> x -> f y -- | This is kept internal because of the Fold wrapping. multifold_ :: forall m arr x y. Multifold_ m arr x y => arr -> Fold m x y multimapOf :: forall cat arr x y. MultimapOf cat arr x y => arr -> cat x y -- | Core of multimap. class MultimapOf (->) arr x y => MultimapI arr x y -- | Constraint for multifold_. class MultimapOf (Fold m) arr x y => Multifold_ m arr x y -- | Constraint for multitraverse. class Multitraverse_ f arr x y => Multitraverse f arr x y -- | Internal definition of Multitraverse type Multitraverse_ f arr x y = (MultimapOf (Kleisli f) (WrapKleisli f arr) x y, CoercibleKleisli f (WrapKleisli f arr) arr) type family WrapKleisli (f :: Type -> Type) (arr :: Type) -- | Auxiliary constraint for Multitraverse class Coercible warr arr => CoercibleKleisli (f :: Type -> Type) warr arr class Multimap_ cat (S2 arr) x y => MultimapOf cat arr x y -- | Fold m is like Kleisli (Const m), but it has a -- different FunctorOf instance, with Foldable instead of -- Traversable. newtype Fold m x y Fold :: (x -> m) -> Fold m x y [unFold] :: Fold m x y -> x -> m class CatLike cat catid :: CatLike cat => cat x x class FunctorOf cat t catmap :: FunctorOf cat t => cat a b -> cat (t a) (t b) class BifunctorOf cat t catbimap :: BifunctorOf cat t => cat a b -> cat c d -> cat (t a c) (t b d) -- | Internal implementation of MultimapOf. class Multimap_ cat arr x y multimap_ :: Multimap_ cat arr x y => arr -> cat x y -- | Heterogeneous lists of arrows are constructed as lists separated by -- (:+) and terminated by (). -- --
-- if -- f :: a -> a' -- g :: b -> b' -- -- then -- f :+ g :+ () :: (a -> a') :+ (b -> b') :+ () ---- -- Those lists are used by gmultimap and multimap. -- --
-- bimap_ :: (a -> a') -> (b -> b') -> (Maybe a, [Either b a]) -> (Maybe a', [Either b' a']) -- bimap_ f g = multimap (f :+ g :+ ()) --data a :+ b (:+) :: a -> b -> (:+) a b infixr 1 :+ infixr 1 :+ data Rule rule mode Rule :: rule -> mode -> Rule rule mode data AnyId AnyId :: AnyId data AnyFunctor AnyFunctor :: AnyFunctor data AnyBifunctor AnyBifunctor :: AnyBifunctor data NilArr NilArr :: NilArr data Incoherent Incoherent :: Incoherent type Default mode arr = arr :+ Rule AnyId mode :+ Rule AnyFunctor mode :+ Rule AnyBifunctor mode :+ NilArr defaultIncoherent :: arr -> Default Incoherent arr def :: mode -> arr -> Default mode arr -- | arr is the list of arrows provided by the user. It is -- constant. When testing whether any arrow matches, arr' is the -- remaining list of arrows to be tested. data S arr arr' S :: arr -> arr' -> S arr arr' type S2 arr = S arr arr s2 :: arr -> S2 arr instance Generic.Functor.Internal.Implicit.MultimapOf (->) arr x y => Generic.Functor.Internal.Implicit.MultimapI arr x y instance Generic.Functor.Internal.Implicit.MultimapOf (Generic.Functor.Internal.Implicit.Fold m) arr x y => Generic.Functor.Internal.Implicit.Multifold_ m arr x y instance Generic.Functor.Internal.Implicit.Multitraverse_ f arr x y => Generic.Functor.Internal.Implicit.Multitraverse f arr x y instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S2 arr) x y => Generic.Functor.Internal.Implicit.MultimapOf cat arr x y instance (Generic.Functor.Internal.Implicit.FunctorOf cat f, Generic.Functor.Internal.Implicit.MultimapOf cat arr x y) => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (Generic.Functor.Internal.Implicit.Rule Generic.Functor.Internal.Implicit.AnyFunctor Generic.Functor.Internal.Implicit.Incoherent Generic.Functor.Internal.Implicit.:+ arr')) (f x) (f y) instance (Generic.Functor.Internal.Implicit.BifunctorOf cat f, Generic.Functor.Internal.Implicit.MultimapOf cat arr x1 y1, Generic.Functor.Internal.Implicit.MultimapOf cat arr x2 y2) => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (Generic.Functor.Internal.Implicit.Rule Generic.Functor.Internal.Implicit.AnyBifunctor Generic.Functor.Internal.Implicit.Incoherent Generic.Functor.Internal.Implicit.:+ arr')) (f x1 x2) (f y1 y2) instance (Generic.Functor.Internal.Implicit.MultimapOf (->) arr y1 x1, Generic.Functor.Internal.Implicit.MultimapOf (->) arr x2 y2) => Generic.Functor.Internal.Implicit.Multimap_ (->) (Generic.Functor.Internal.Implicit.S arr (Generic.Functor.Internal.Implicit.Rule Generic.Functor.Internal.Implicit.AnyBifunctor Generic.Functor.Internal.Implicit.Incoherent Generic.Functor.Internal.Implicit.:+ arr')) (x1 -> x2) (y1 -> y2) instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr arr') x y => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (arr0 Generic.Functor.Internal.Implicit.:+ arr')) x y instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (arr0 Generic.Functor.Internal.Implicit.:+ (arr1 Generic.Functor.Internal.Implicit.:+ arr2))) x y => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr ((arr0 Generic.Functor.Internal.Implicit.:+ arr1) Generic.Functor.Internal.Implicit.:+ arr2)) x y instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr arr') x y => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (Generic.Functor.Internal.Implicit.NilArr Generic.Functor.Internal.Implicit.:+ arr')) x y instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr arr') x y => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (() Generic.Functor.Internal.Implicit.:+ arr')) x y instance Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (cat a b Generic.Functor.Internal.Implicit.:+ arr')) a b instance Generic.Functor.Internal.Implicit.CatLike cat => Generic.Functor.Internal.Implicit.Multimap_ cat (Generic.Functor.Internal.Implicit.S arr (Generic.Functor.Internal.Implicit.Rule Generic.Functor.Internal.Implicit.AnyId Generic.Functor.Internal.Implicit.Incoherent Generic.Functor.Internal.Implicit.:+ arr')) x x instance (d GHC.Types.~ Generic.Functor.Internal.Implicit.NilArr) => Generic.Functor.Internal.Implicit.CoercibleKleisli f d Generic.Functor.Internal.Implicit.NilArr instance (d GHC.Types.~ Generic.Functor.Internal.Implicit.Rule rule mode) => Generic.Functor.Internal.Implicit.CoercibleKleisli f d (Generic.Functor.Internal.Implicit.Rule rule mode) instance (Generic.Functor.Internal.Implicit.CoercibleKleisli f a b, Generic.Functor.Internal.Implicit.CoercibleKleisli f arr arr') => Generic.Functor.Internal.Implicit.CoercibleKleisli f (a Generic.Functor.Internal.Implicit.:+ arr) (b Generic.Functor.Internal.Implicit.:+ arr') instance (Data.Bifoldable.Bifoldable t, GHC.Base.Monoid m) => Generic.Functor.Internal.Implicit.BifunctorOf (Generic.Functor.Internal.Implicit.Fold m) t instance Data.Bifunctor.Bifunctor t => Generic.Functor.Internal.Implicit.BifunctorOf (->) t instance (GHC.Base.Applicative f, Data.Bitraversable.Bitraversable t) => Generic.Functor.Internal.Implicit.BifunctorOf (Control.Arrow.Kleisli f) t instance (Data.Foldable.Foldable t, GHC.Base.Monoid m) => Generic.Functor.Internal.Implicit.FunctorOf (Generic.Functor.Internal.Implicit.Fold m) t instance GHC.Base.Functor t => Generic.Functor.Internal.Implicit.FunctorOf (->) t instance (GHC.Base.Applicative f, Data.Traversable.Traversable t) => Generic.Functor.Internal.Implicit.FunctorOf (Control.Arrow.Kleisli f) t instance GHC.Base.Monoid m => Generic.Functor.Internal.Implicit.CatLike (Generic.Functor.Internal.Implicit.Fold m) instance Generic.Functor.Internal.Implicit.CatLike (->) instance GHC.Base.Applicative f => Generic.Functor.Internal.Implicit.CatLike (Control.Arrow.Kleisli f) instance (b2 GHC.Types.~ f c, a GHC.Types.~ Control.Arrow.Kleisli f b1 c) => Generic.Functor.Internal.Implicit.CoercibleKleisli f a (b1 -> b2) -- | This is an internal module. Look, don't touch. -- -- Generic.Functor is the public API. module Generic.Functor.Internal -- | Generic implementation of fmap. See also GenericFunctor -- for DerivingVia, using gfmap under the hood. -- --
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gfmap)
--
-- data Twice a = Twice (Either a a)
-- deriving Generic
--
-- instance Functor Twice where
-- fmap = gfmap
--
--
-- Unlike gsolomap, gfmap is safe to use in all contexts.
gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b
-- | Generalized generic functor.
--
-- gsolomap is a generalization of gfmap (generic
-- fmap), where the type parameter to be "mapped" does not have to
-- be the last one.
--
-- gsolomap is unsafe: misuse will break your programs.
-- Read the Usage section below for details.
--
--
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gsolomap)
--
-- data Result a r = Error a | Ok r -- Another name for Either
-- deriving Generic
--
-- mapError :: (a -> b) -> Result a r -> Result b r
-- mapError = gsolomap
--
-- mapOk :: (r -> s) -> Result a r -> Result a s
-- mapOk = gsolomap
--
-- mapBoth :: (a -> b) -> Result a a -> Result b b
-- mapBoth = gsolomap
--
--
-- -- map1 :: (a -> b) -> Either e (Maybe [IO a]) -> Either e (Maybe [IO b]) -- map1 = solomap -- -- equivalent to: fmap . fmap . fmap . fmap -- -- map2 :: (a -> b) -> (e -> Either [a] r) -> (e -> Either [b] r) -- map2 = solomap -- -- equivalent to: \f -> fmap (bimap (fmap f) id) --solomap :: forall a b x y. Solomap a b x y => (a -> b) -> x -> y -- | Generic n-ary functor. -- -- A generalization of gsolomap to map over multiple parameters -- simultaneously. gmultimap takes a list of functions separated -- by (:+) and terminated by (). -- -- gmultimap is unsafe: misuse will break your programs. -- The type of every function in the list must be some (a -> -- b) where a and b are distinct type variables. -- -- See the Usage section of gsolomap for details. -- --
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gmultimap)
--
-- data Three a b c = One a | Two b | Three c
-- deriving Generic
--
-- mapThree :: (a -> a') -> (b -> b') -> (c -> c') -> Three a b c -> Three a' b' c'
-- mapThree f g h = gmultimap (f :+ g :+ h :+ ())
--
gmultimap :: forall arr x y. (Generic x, Generic y, GMultimap arr x y) => arr -> x -> y
-- | Implicit n-ary functor.
--
-- A generalization of solomap to map over multiple parameters
-- simultaneously. multimap takes a list of functions separated by
-- (:+) and terminated by ().
--
-- multimap is unsafe: misuse will break your programs. The
-- type of every function in the list must be some (a -> b)
-- where a and b are distinct type variables.
--
-- See the Usage section of gsolomap for details.
--
-- -- type F a b c = Either a (b, c) -- -- map3 :: (a -> a') -> (b -> b') -> (c -> c') -> F a b c -> F a' b' c' -- map3 f g h = multimap (f :+ g :+ h :+ ()) -- -- equivalent to: \f g h -> bimap f (bimap g h) --multimap :: forall arr x y. Multimap arr x y => arr -> x -> y -- | Generic implementation of bimap from Bifunctor. See also -- GenericBifunctor. gbimap :: forall f a b c d. GBimap f => (a -> b) -> (c -> d) -> f a c -> f b d -- | Generic implementation of first from Bifunctor. See also -- GenericBifunctor. gfirst :: forall f a b c. GFirst f => (a -> b) -> f a c -> f b c -- | Generic implementation of second from Bifunctor. See -- also GenericBifunctor. gsecond :: forall f a c d. GSecond f => (c -> d) -> f a c -> f a d -- | Generic implementation of foldMap from Foldable. gfoldMap :: forall t m a. (GFoldMap m t, Monoid m) => (a -> m) -> t a -> m -- | Generic implementation of bifoldMap from Bifoldable. gbifoldMap :: forall t m a b. (GBifoldMap m t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m -- | Generic implementation of traverse from Traversable. gtraverse :: forall t f a b. (GTraverse f t, Applicative f) => (a -> f b) -> t a -> f (t b) -- | Generic implementation of bitraverse from -- Bitraversable. gbitraverse :: forall t f a b c d. (GBitraverse f t, Applicative f) => (a -> f b) -> (c -> f d) -> t a c -> f (t b d) -- | Explicitly require a constraint, to force the instantiation of a -- quantified constraint. with :: forall c r. (c => r) -> c => r -- | Generic Functor. Constraint for gfmap. class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f -- | Internal component of GFunctor. -- -- This is an example of the "quantified constraints trick" to encode -- forall a b. GMap1 a b (Rep (f a)) (Rep (f b)) which doesn't -- actually work as-is. class GMap1 (Default Incoherent (a -> b)) (Rep (f a)) (Rep (f b)) => GFunctorRep a b f -- | Constraint for gbimap. class (forall a c. Generic (f a c), forall a b c d. GBimapRep a b c d f) => GBimap f -- | Internal component of GBifunctor. class GMap1 (Default Incoherent ((a -> b) :+ (c -> d))) (Rep (f a c)) (Rep (f b d)) => GBimapRep a b c d f -- | Constraint for gfirst. class (forall a c. Generic (f a c), forall a b c. GFirstRep a b c f) => GFirst f -- | Internal component of GFirst. class GMap1 (Default Incoherent (a -> b)) (Rep (f a c)) (Rep (f b c)) => GFirstRep a b c f -- | Constraint for gsecond. class (forall a c. Generic (f a c), forall a c d. GFunctorRep c d (f a)) => GSecond f -- | Generic Bifunctor. class (GBimap f, GFirst f, GSecond f) => GBifunctor f -- | Constraint for gtraverse. class (forall a. Generic (t a), forall a b. GTraverseRep a b f t) => GTraverse f t class GTraverse1 f (Default Incoherent (a -> f b)) (Rep (t a)) (Rep (t b)) => GTraverseRep a b f t -- | Generic Traversable. class (forall f. Applicative f => GBitraverse f t) => GTraversable t -- | Constraint for gtraverse. class (forall a b. Generic (t a b), forall a b c d. GBitraverseRep a b c d f t) => GBitraverse f t class GTraverse1 f (Default Incoherent ((a -> f b) :+ (c -> f d))) (Rep (t a c)) (Rep (t b d)) => GBitraverseRep a b c d f t -- | Generic Bitraversable. class (forall f. Applicative f => GBitraverse f t) => GBitraversable t -- | Constraint for gfoldMap. class (forall a. Generic (t a), forall a b. GFoldMapRep a b m t) => GFoldMap m t class GFoldMap1 m (Default Incoherent (Fold m a b)) (Rep (t a)) (Rep (t b)) => GFoldMapRep a b m t -- | Generic Foldable. Constraint for GenericFunctor -- (deriving-via Foldable). class (forall m. Monoid m => GFoldMap m t) => GFoldable t -- | Constraint for gbifoldMap. class (forall a b. Generic (t a b), forall a b c d. GBifoldMapRep a b c d m t) => GBifoldMap m t class GFoldMap1 m (Default Incoherent (Fold m a b :+ Fold m c d)) (Rep (t a c)) (Rep (t b d)) => GBifoldMapRep a b c d m t -- | Generic Foldable. Constraint for GenericFunctor -- (deriving-via Foldable). class (forall m. Monoid m => GBifoldMap m t) => GBifoldable t -- | Constraint for gsolomap. class GMultimap (a -> b) x y => GSolomap a b x y -- | Constraint for solomap. class Multimap (a -> b) x y => Solomap a b x y -- | Constraint for gmultimap. class GMap1 (Default Incoherent arr) (Rep x) (Rep y) => GMultimap arr x y -- | Constraint for multimap. class MultimapI (Default Incoherent arr) x y => Multimap arr x y -- | newtype for DerivingVia of Functor and -- Foldable instances. -- -- Note: the GHC extensions DeriveFunctor, -- DeriveFoldable, and DeriveTraversable (which implies -- all three) already works out-of-the-box in most cases. There are -- exceptions, such as the following example. -- --
-- {-# LANGUAGE DeriveGeneric, DerivingVia #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (GenericFunctor(..))
--
-- data Twice a = Twice (Either a a)
-- deriving Generic
-- deriving (Functor, Foldable) via (GenericFunctor Twice)
--
newtype GenericFunctor f a
GenericFunctor :: f a -> GenericFunctor f a
-- | newtype for DerivingVia of Bifunctor and
-- Bifoldable instances.
--
-- Note: deriving Bifunctor for a generic type often requires
-- Functor instances for types mentioned in the fields.
--
--
-- {-# LANGUAGE DeriveGeneric, DerivingVia #-}
--
-- import Data.Bifoldable (Bifoldable)
-- import Data.Bifunctor (Bifunctor)
-- import GHC.Generics (Generic)
-- import Generic.Functor (GenericFunctor(..), GenericBifunctor(..))
--
-- data Tree a b = Node a (Tree a b) (Tree a b) | Leaf b
-- deriving Generic
-- deriving (Functor, Foldable) via (GenericFunctor (Tree a))
-- deriving (Bifunctor, Bifoldable) via (GenericBifunctor Tree)
--
-- data CofreeF f a b = a :< f b
-- deriving Generic
-- deriving (Bifunctor, Bifoldable) via (GenericBifunctor (CofreeF f))
--
newtype GenericBifunctor f a b
GenericBifunctor :: f a b -> GenericBifunctor f a b
coerce1 :: Coercible s t => (r -> s) -> r -> t
coerce2 :: Coercible t u => (r -> s -> t) -> r -> s -> u
coerce3 :: (Coercible w v, Coercible (f b d) (g b d)) => (r -> w -> f b d) -> r -> v -> g b d
coerceFoldMap :: Coercible t u => (am -> t -> m) -> am -> u -> m
coerceBifoldMap :: Coercible t u => (am -> bm -> t -> m) -> am -> bm -> u -> m
-- | We use the same class to implement all of fmap, foldMap,
-- traverse, instantiating m as Identity, 'Const
-- (EndoM mm)' and 'Aps n' respectively. Those three cases differ in
-- their instances for K1.
--
-- (the K stands for Kleisli, because the result is Kleisli
-- m (f ()) (g ())
class GMultimapK m arr f g
gmultimapK :: GMultimapK m arr f g => arr -> f () -> m (g ())
class GMultimapK Identity arr f g => GMap1 arr f g
gmapRep :: GMap1 arr f g => arr -> f () -> g ()
type EndoM m = Endo (Maybe m)
unEndoM :: Monoid m => EndoM m -> m
liftEndoM :: Monoid m => m -> EndoM m
foldToConst :: Monoid m => Fold m x y -> x -> Const (EndoM m) y
class GMultimapK (Const (EndoM m)) arr f g => GFoldMap1 m arr f g
-- | Danger! GFoldMap1 m arr f f MUST come from a quantified
-- constraint (see use in gfoldMap).
gfoldMapRep :: forall m arr f. (GFoldMap1 m arr f f, Monoid m) => arr -> f () -> m
class GMultimapK (Aps m) arr f g => GTraverse1 m arr f g
gtraverseRep :: GTraverse1 m arr f g => arr -> f () -> Aps m (g ())
instance (forall a. GHC.Generics.Generic (t a), forall a b. Generic.Functor.Internal.GTraverseRep a b f t) => Generic.Functor.Internal.GTraverse f t
instance Generic.Functor.Internal.GTraverse1 f (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent (a -> f b)) (GHC.Generics.Rep (t a)) (GHC.Generics.Rep (t b)) => Generic.Functor.Internal.GTraverseRep a b f t
instance (forall (f :: * -> *). GHC.Base.Applicative f => Generic.Functor.Internal.GBitraverse f t) => Generic.Functor.Internal.GTraversable t
instance (forall (f :: * -> *). GHC.Base.Applicative f => Generic.Functor.Internal.GBitraverse f t) => Generic.Functor.Internal.GBitraversable t
instance (forall a b. GHC.Generics.Generic (t a b), forall a b c d. Generic.Functor.Internal.GBitraverseRep a b c d f t) => Generic.Functor.Internal.GBitraverse f t
instance Generic.Functor.Internal.GTraverse1 f (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent ((a -> f b) Generic.Functor.Internal.Implicit.:+ (c -> f d))) (GHC.Generics.Rep (t a c)) (GHC.Generics.Rep (t b d)) => Generic.Functor.Internal.GBitraverseRep a b c d f t
instance Generic.Functor.Internal.GMultimapK (ApNormalize.Aps.Aps m) arr f g => Generic.Functor.Internal.GTraverse1 m arr f g
instance (forall m. GHC.Base.Monoid m => Generic.Functor.Internal.GFoldMap m t) => Generic.Functor.Internal.GFoldable t
instance Generic.Functor.Internal.GFoldable f => Data.Foldable.Foldable (Generic.Functor.Internal.GenericFunctor f)
instance (forall a. GHC.Generics.Generic (t a), forall a b. Generic.Functor.Internal.GFoldMapRep a b m t) => Generic.Functor.Internal.GFoldMap m t
instance Generic.Functor.Internal.GFoldMap1 m (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent (Generic.Functor.Internal.Implicit.Fold m a b)) (GHC.Generics.Rep (t a)) (GHC.Generics.Rep (t b)) => Generic.Functor.Internal.GFoldMapRep a b m t
instance (forall m. GHC.Base.Monoid m => Generic.Functor.Internal.GBifoldMap m t) => Generic.Functor.Internal.GBifoldable t
instance Generic.Functor.Internal.GBifoldable f => Data.Bifoldable.Bifoldable (Generic.Functor.Internal.GenericBifunctor f)
instance (forall a b. GHC.Generics.Generic (t a b), forall a b c d. Generic.Functor.Internal.GBifoldMapRep a b c d m t) => Generic.Functor.Internal.GBifoldMap m t
instance Generic.Functor.Internal.GFoldMap1 m (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent (Generic.Functor.Internal.Implicit.Fold m a b Generic.Functor.Internal.Implicit.:+ Generic.Functor.Internal.Implicit.Fold m c d)) (GHC.Generics.Rep (t a c)) (GHC.Generics.Rep (t b d)) => Generic.Functor.Internal.GBifoldMapRep a b c d m t
instance Generic.Functor.Internal.GMultimapK (Data.Functor.Const.Const (Generic.Functor.Internal.EndoM m)) arr f g => Generic.Functor.Internal.GFoldMap1 m arr f g
instance (Generic.Functor.Internal.Implicit.Multifold_ m arr x y, GHC.Base.Monoid m) => Generic.Functor.Internal.GMultimapK (Data.Functor.Const.Const (Generic.Functor.Internal.EndoM m)) arr (GHC.Generics.K1 i x) (GHC.Generics.K1 i' y)
instance Generic.Functor.Internal.GMultimapK (Data.Functor.Const.Const (Generic.Functor.Internal.EndoM m)) arr (GHC.Generics.K1 i x) (GHC.Generics.K1 i x)
instance (forall a. GHC.Generics.Generic (f a), forall a b. Generic.Functor.Internal.GFunctorRep a b f) => Generic.Functor.Internal.GFunctor f
instance Generic.Functor.Internal.GFunctor f => GHC.Base.Functor (Generic.Functor.Internal.GenericFunctor f)
instance (Generic.Functor.Internal.GBimap f, Generic.Functor.Internal.GFirst f, Generic.Functor.Internal.GSecond f) => Generic.Functor.Internal.GBifunctor f
instance Generic.Functor.Internal.GBifunctor f => Data.Bifunctor.Bifunctor (Generic.Functor.Internal.GenericBifunctor f)
instance (forall a c. GHC.Generics.Generic (f a c), forall a c d. Generic.Functor.Internal.GFunctorRep c d (f a)) => Generic.Functor.Internal.GSecond f
instance Generic.Functor.Internal.GMap1 (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent (a -> b)) (GHC.Generics.Rep (f a)) (GHC.Generics.Rep (f b)) => Generic.Functor.Internal.GFunctorRep a b f
instance (forall a c. GHC.Generics.Generic (f a c), forall a b c d. Generic.Functor.Internal.GBimapRep a b c d f) => Generic.Functor.Internal.GBimap f
instance Generic.Functor.Internal.GMap1 (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent ((a -> b) Generic.Functor.Internal.Implicit.:+ (c -> d))) (GHC.Generics.Rep (f a c)) (GHC.Generics.Rep (f b d)) => Generic.Functor.Internal.GBimapRep a b c d f
instance (forall a c. GHC.Generics.Generic (f a c), forall a b c. Generic.Functor.Internal.GFirstRep a b c f) => Generic.Functor.Internal.GFirst f
instance Generic.Functor.Internal.GMap1 (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent (a -> b)) (GHC.Generics.Rep (f a c)) (GHC.Generics.Rep (f b c)) => Generic.Functor.Internal.GFirstRep a b c f
instance Generic.Functor.Internal.GMultimap (a -> b) x y => Generic.Functor.Internal.GSolomap a b x y
instance Generic.Functor.Internal.GMap1 (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent arr) (GHC.Generics.Rep x) (GHC.Generics.Rep y) => Generic.Functor.Internal.GMultimap arr x y
instance Generic.Functor.Internal.GMultimapK Data.Functor.Identity.Identity arr f g => Generic.Functor.Internal.GMap1 arr f g
instance Generic.Functor.Internal.Implicit.MultimapI arr x y => Generic.Functor.Internal.GMultimapK Data.Functor.Identity.Identity arr (GHC.Generics.K1 i x) (GHC.Generics.K1 i' y)
instance Generic.Functor.Internal.Implicit.Multitraverse m arr x y => Generic.Functor.Internal.GMultimapK (ApNormalize.Aps.Aps m) arr (GHC.Generics.K1 i x) (GHC.Generics.K1 i' y)
instance (Generic.Functor.Internal.GMultimapK m arr f g, GHC.Base.Functor m) => Generic.Functor.Internal.GMultimapK m arr (GHC.Generics.M1 i c f) (GHC.Generics.M1 i' c' g)
instance (Generic.Functor.Internal.GMultimapK m arr f1 g1, Generic.Functor.Internal.GMultimapK m arr f2 g2, GHC.Base.Applicative m) => Generic.Functor.Internal.GMultimapK m arr (f1 GHC.Generics.:+: f2) (g1 GHC.Generics.:+: g2)
instance (Generic.Functor.Internal.GMultimapK m arr f1 g1, Generic.Functor.Internal.GMultimapK m arr f2 g2, GHC.Base.Applicative m) => Generic.Functor.Internal.GMultimapK m arr (f1 GHC.Generics.:*: f2) (g1 GHC.Generics.:*: g2)
instance GHC.Base.Applicative m => Generic.Functor.Internal.GMultimapK m arr GHC.Generics.U1 GHC.Generics.U1
instance Generic.Functor.Internal.GMultimapK m arr GHC.Generics.V1 GHC.Generics.V1
instance Generic.Functor.Internal.Multimap (a -> b) x y => Generic.Functor.Internal.Solomap a b x y
instance Generic.Functor.Internal.Implicit.MultimapI (Generic.Functor.Internal.Implicit.Default Generic.Functor.Internal.Implicit.Incoherent arr) x y => Generic.Functor.Internal.Multimap arr x y
-- | Generic and generalized functors.
module Generic.Functor
-- | Generalized generic functor.
--
-- gsolomap is a generalization of gfmap (generic
-- fmap), where the type parameter to be "mapped" does not have to
-- be the last one.
--
-- gsolomap is unsafe: misuse will break your programs.
-- Read the Usage section below for details.
--
--
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gsolomap)
--
-- data Result a r = Error a | Ok r -- Another name for Either
-- deriving Generic
--
-- mapError :: (a -> b) -> Result a r -> Result b r
-- mapError = gsolomap
--
-- mapOk :: (r -> s) -> Result a r -> Result a s
-- mapOk = gsolomap
--
-- mapBoth :: (a -> b) -> Result a a -> Result b b
-- mapBoth = gsolomap
--
--
-- -- map1 :: (a -> b) -> Either e (Maybe [IO a]) -> Either e (Maybe [IO b]) -- map1 = solomap -- -- equivalent to: fmap . fmap . fmap . fmap -- -- map2 :: (a -> b) -> (e -> Either [a] r) -> (e -> Either [b] r) -- map2 = solomap -- -- equivalent to: \f -> fmap (bimap (fmap f) id) --solomap :: forall a b x y. Solomap a b x y => (a -> b) -> x -> y -- | Generic n-ary functor. -- -- A generalization of gsolomap to map over multiple parameters -- simultaneously. gmultimap takes a list of functions separated -- by (:+) and terminated by (). -- -- gmultimap is unsafe: misuse will break your programs. -- The type of every function in the list must be some (a -> -- b) where a and b are distinct type variables. -- -- See the Usage section of gsolomap for details. -- --
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gmultimap)
--
-- data Three a b c = One a | Two b | Three c
-- deriving Generic
--
-- mapThree :: (a -> a') -> (b -> b') -> (c -> c') -> Three a b c -> Three a' b' c'
-- mapThree f g h = gmultimap (f :+ g :+ h :+ ())
--
gmultimap :: forall arr x y. (Generic x, Generic y, GMultimap arr x y) => arr -> x -> y
-- | Implicit n-ary functor.
--
-- A generalization of solomap to map over multiple parameters
-- simultaneously. multimap takes a list of functions separated by
-- (:+) and terminated by ().
--
-- multimap is unsafe: misuse will break your programs. The
-- type of every function in the list must be some (a -> b)
-- where a and b are distinct type variables.
--
-- See the Usage section of gsolomap for details.
--
-- -- type F a b c = Either a (b, c) -- -- map3 :: (a -> a') -> (b -> b') -> (c -> c') -> F a b c -> F a' b' c' -- map3 f g h = multimap (f :+ g :+ h :+ ()) -- -- equivalent to: \f g h -> bimap f (bimap g h) --multimap :: forall arr x y. Multimap arr x y => arr -> x -> y -- | Heterogeneous lists of arrows are constructed as lists separated by -- (:+) and terminated by (). -- --
-- if -- f :: a -> a' -- g :: b -> b' -- -- then -- f :+ g :+ () :: (a -> a') :+ (b -> b') :+ () ---- -- Those lists are used by gmultimap and multimap. -- --
-- bimap_ :: (a -> a') -> (b -> b') -> (Maybe a, [Either b a]) -> (Maybe a', [Either b' a']) -- bimap_ f g = multimap (f :+ g :+ ()) --data a :+ b (:+) :: a -> b -> (:+) a b infixr 1 :+ infixr 1 :+ -- | newtype for DerivingVia of Functor and -- Foldable instances. -- -- Note: the GHC extensions DeriveFunctor, -- DeriveFoldable, and DeriveTraversable (which implies -- all three) already works out-of-the-box in most cases. There are -- exceptions, such as the following example. -- --
-- {-# LANGUAGE DeriveGeneric, DerivingVia #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (GenericFunctor(..))
--
-- data Twice a = Twice (Either a a)
-- deriving Generic
-- deriving (Functor, Foldable) via (GenericFunctor Twice)
--
newtype GenericFunctor f a
GenericFunctor :: f a -> GenericFunctor f a
-- | newtype for DerivingVia of Bifunctor and
-- Bifoldable instances.
--
-- Note: deriving Bifunctor for a generic type often requires
-- Functor instances for types mentioned in the fields.
--
--
-- {-# LANGUAGE DeriveGeneric, DerivingVia #-}
--
-- import Data.Bifoldable (Bifoldable)
-- import Data.Bifunctor (Bifunctor)
-- import GHC.Generics (Generic)
-- import Generic.Functor (GenericFunctor(..), GenericBifunctor(..))
--
-- data Tree a b = Node a (Tree a b) (Tree a b) | Leaf b
-- deriving Generic
-- deriving (Functor, Foldable) via (GenericFunctor (Tree a))
-- deriving (Bifunctor, Bifoldable) via (GenericBifunctor Tree)
--
-- data CofreeF f a b = a :< f b
-- deriving Generic
-- deriving (Bifunctor, Bifoldable) via (GenericBifunctor (CofreeF f))
--
newtype GenericBifunctor f a b
GenericBifunctor :: f a b -> GenericBifunctor f a b
-- | Generic implementation of fmap. See also GenericFunctor
-- for DerivingVia, using gfmap under the hood.
--
--
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import GHC.Generics (Generic)
-- import Generic.Functor (gfmap)
--
-- data Twice a = Twice (Either a a)
-- deriving Generic
--
-- instance Functor Twice where
-- fmap = gfmap
--
--
-- Unlike gsolomap, gfmap is safe to use in all contexts.
gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b
-- | Generic implementation of foldMap from Foldable.
gfoldMap :: forall t m a. (GFoldMap m t, Monoid m) => (a -> m) -> t a -> m
-- | Generic implementation of traverse from Traversable.
gtraverse :: forall t f a b. (GTraverse f t, Applicative f) => (a -> f b) -> t a -> f (t b)
-- | Generic implementation of bimap from Bifunctor. See also
-- GenericBifunctor.
gbimap :: forall f a b c d. GBimap f => (a -> b) -> (c -> d) -> f a c -> f b d
-- | Generic implementation of first from Bifunctor. See also
-- GenericBifunctor.
gfirst :: forall f a b c. GFirst f => (a -> b) -> f a c -> f b c
-- | Generic implementation of second from Bifunctor. See
-- also GenericBifunctor.
gsecond :: forall f a c d. GSecond f => (c -> d) -> f a c -> f a d
-- | Generic implementation of bifoldMap from Bifoldable.
gbifoldMap :: forall t m a b. (GBifoldMap m t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m
-- | Generic implementation of bitraverse from
-- Bitraversable.
gbitraverse :: forall t f a b c d. (GBitraverse f t, Applicative f) => (a -> f b) -> (c -> f d) -> t a c -> f (t b d)
-- | Generic Functor. Constraint for gfmap.
class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f
-- | Generic Foldable. Constraint for GenericFunctor
-- (deriving-via Foldable).
class (forall m. Monoid m => GFoldMap m t) => GFoldable t
-- | Constraint for gfoldMap.
class (forall a. Generic (t a), forall a b. GFoldMapRep a b m t) => GFoldMap m t
-- | Generic Traversable.
class (forall f. Applicative f => GBitraverse f t) => GTraversable t
-- | Constraint for gtraverse.
class (forall a. Generic (t a), forall a b. GTraverseRep a b f t) => GTraverse f t
-- | Generic Bifunctor.
class (GBimap f, GFirst f, GSecond f) => GBifunctor f
-- | Constraint for gbimap.
class (forall a c. Generic (f a c), forall a b c d. GBimapRep a b c d f) => GBimap f
-- | Constraint for gfirst.
class (forall a c. Generic (f a c), forall a b c. GFirstRep a b c f) => GFirst f
-- | Constraint for gsecond.
class (forall a c. Generic (f a c), forall a c d. GFunctorRep c d (f a)) => GSecond f
-- | Generic Foldable. Constraint for GenericFunctor
-- (deriving-via Foldable).
class (forall m. Monoid m => GBifoldMap m t) => GBifoldable t
-- | Constraint for gbifoldMap.
class (forall a b. Generic (t a b), forall a b c d. GBifoldMapRep a b c d m t) => GBifoldMap m t
-- | Generic Bitraversable.
class (forall f. Applicative f => GBitraverse f t) => GBitraversable t
-- | Constraint for gtraverse.
class (forall a b. Generic (t a b), forall a b c d. GBitraverseRep a b c d f t) => GBitraverse f t
-- | Constraint for gsolomap.
class GMultimap (a -> b) x y => GSolomap a b x y
-- | Constraint for solomap.
class Multimap (a -> b) x y => Solomap a b x y
-- | Constraint for gmultimap.
class GMap1 (Default Incoherent arr) (Rep x) (Rep y) => GMultimap arr x y
-- | Constraint for multimap.
class MultimapI (Default Incoherent arr) x y => Multimap arr x y