Copyright | (C) 2012-2015 Nicolas Frisby, (C) 2015 Ryan Scott |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Ryan Scott |
Portability | Portable |
Safe Haskell | None |
Language | Haskell98 |
Data.Functor.Invariant
Contents
Description
Haskell98 invariant functors (also known as exponential functors).
For more information, see Edward Kmett's article "Rotten Bananas":
- class Invariant f where
- invmap :: (a -> b) -> (b -> a) -> f a -> f b
- invmapFunctor :: Functor f => (a -> b) -> (b -> a) -> f a -> f b
- genericInvmap :: (Generic1 f, Invariant (Rep1 f)) => (a -> b) -> (b -> a) -> f a -> f b
- newtype WrappedFunctor f a = WrapFunctor {
- unwrapFunctor :: f a
- invmapContravariant :: Contravariant f => (a -> b) -> (b -> a) -> f a -> f b
- newtype WrappedContravariant f a = WrapContravariant {
- unwrapContravariant :: f a
- class Invariant2 f where
- invmap2 :: (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> f a b -> f c d
- invmap2Bifunctor :: Bifunctor f => (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> f a b -> f c d
- newtype WrappedBifunctor p a b :: (k -> k1 -> *) -> k -> k1 -> * = WrapBifunctor {
- unwrapBifunctor :: p a b
- invmap2Profunctor :: Profunctor f => (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> f a b -> f c d
- newtype WrappedProfunctor p a b = WrapProfunctor {
- unwrapProfunctor :: p a b
Invariant
class Invariant f where Source
Any * -> *
type parametric in the argument permits an instance of
Invariant
.
Instances should satisfy the following laws:
invmap id id = id invmap f2 f2' . invmap f1 f1' = invmap (f2 . f1) (f1' . f2')
Minimal complete definition
Nothing
Instances
invmapFunctor :: Functor f => (a -> b) -> (b -> a) -> f a -> f b Source
GHC.Generics
With GHC 7.2 or later, Invariant
instances can be defined easily using GHC
generics like so:
{-# LANGUAGE DeriveGeneric, FlexibleContexts #-}
import Data.Functor.Invariant
import GHC.Generics
data T f a = T (f a) deriving Generic1
instance Invariant f => Invariant
(T f)
Be aware that generic Invariant
instances cannot be derived for data types
that have function arguments in which the last type parameter appears in a
position other than the result type (e.g., data Fun a = Fun (a -> a)
). For
these, you can derive them using the Data.Functor.Invariant.TH module.
genericInvmap :: (Generic1 f, Invariant (Rep1 f)) => (a -> b) -> (b -> a) -> f a -> f b Source
A generic implementation of invmap
.
newtype WrappedFunctor f a Source
Constructors
WrapFunctor | |
Fields
|
Instances
Monad m => Monad (WrappedFunctor * m) Source | |
Functor f => Functor (WrappedFunctor * f) Source | |
Applicative f => Applicative (WrappedFunctor * f) Source | |
Foldable f => Foldable (WrappedFunctor * f) Source | |
Traversable f => Traversable (WrappedFunctor * f) Source | |
Alternative f => Alternative (WrappedFunctor * f) Source | |
MonadPlus m => MonadPlus (WrappedFunctor * m) Source | |
Functor f => Invariant (WrappedFunctor * f) Source | |
Eq (f a) => Eq (WrappedFunctor k f a) Source | |
Ord (f a) => Ord (WrappedFunctor k f a) Source | |
Read (f a) => Read (WrappedFunctor k f a) Source | |
Show (f a) => Show (WrappedFunctor k f a) Source |
invmapContravariant :: Contravariant f => (a -> b) -> (b -> a) -> f a -> f b Source
Every Contravariant
functor is also an Invariant
functor.
newtype WrappedContravariant f a Source
Wrap a Contravariant
functor to be used as a member of Invariant
.
Constructors
WrapContravariant | |
Fields
|
Instances
Divisible f => Divisible (WrappedContravariant * f) Source | |
Decidable f => Decidable (WrappedContravariant * f) Source | |
Contravariant f => Contravariant (WrappedContravariant * f) Source | |
Contravariant f => Invariant (WrappedContravariant * f) Source | |
Eq (f a) => Eq (WrappedContravariant k f a) Source | |
Ord (f a) => Ord (WrappedContravariant k f a) Source | |
Read (f a) => Read (WrappedContravariant k f a) Source | |
Show (f a) => Show (WrappedContravariant k f a) Source |
Invariant2
class Invariant2 f where Source
Any * -> * -> *
type parametric in both arguments permits an instance of
Invariant2
.
Instances should satisfy the following laws:
invmap2 id id id id = id invmap2 f2 f2' g2 g2' . invmap2 f1 f1' g1 g1' = invmap2 (f2 . f1) (f1' . f2') (g2 . g1) (g1' . g2')
Instances
Invariant2 (->) Source | |
Invariant2 Either Source | |
Invariant2 (,) Source | |
Invariant2 Const Source | from Control.Applicative |
Invariant2 Op Source | from the |
Invariant2 Arg Source | from Data.Semigroup |
Invariant2 Constant Source | from the |
Invariant2 (K1 i) Source | from GHC.Generics |
Invariant2 ((,,) a) Source | |
Arrow arr => Invariant2 (WrappedArrow arr) Source | from Control.Applicative |
Invariant2 p => Invariant2 (Codensity p) Source | from the |
Invariant2 p => Invariant2 (CofreeMapping p) Source | from the |
Invariant2 (FreeMapping p) Source | from the |
Invariant2 p => Invariant2 (CofreeTraversing p) Source | from the |
Invariant2 (FreeTraversing p) Source | from the |
Invariant2 p => Invariant2 (TambaraSum p) Source | from the |
Invariant2 (PastroSum p) Source | from the |
Invariant2 (CotambaraSum p) Source | from the |
Invariant2 (CopastroSum p) Source | from the |
Invariant2 p => Invariant2 (Closure p) Source | from the |
Invariant2 (Environment p) Source | from the |
Invariant2 p => Invariant2 (Tambara p) Source | from the |
Invariant2 (Pastro p) Source | from the |
Invariant2 (Cotambara p) Source | from the |
Invariant f => Invariant2 (Star f) Source | from the |
Invariant f => Invariant2 (Costar f) Source | from the |
Arrow arr => Invariant2 (WrappedArrow arr) Source | from the |
Invariant2 (Forget r) Source | from the |
Invariant2 (Tagged *) Source | from the |
Invariant2 ((,,,) a b) Source | |
(Invariant2 p, Invariant2 q) => Invariant2 (Ran p q) Source | from the |
(Invariant2 p, Invariant2 q) => Invariant2 (Procompose p q) Source | from the |
(Invariant2 p, Invariant2 q) => Invariant2 (Rift p q) Source | from the |
(Invariant f, Invariant2 p) => Invariant2 (Cayley f p) Source | from the |
Invariant2 ((,,,,) a b c) Source | |
Invariant f => Invariant2 (Clown * * f) Source | from the |
Invariant2 p => Invariant2 (Flip * * p) Source | from the |
Invariant g => Invariant2 (Joker * * g) Source | from the |
Bifunctor p => Invariant2 (WrappedBifunctor * * p) Source | from the |
Profunctor p => Invariant2 (WrappedProfunctor * * p) Source | |
(Invariant2 f, Invariant2 g) => Invariant2 (Product * * f g) Source | from the |
(Invariant2 p, Invariant2 q) => Invariant2 (Sum * * p q) Source | from the |
(Invariant f, Invariant2 p) => Invariant2 (Tannen * * * f p) Source | from the |
(Invariant2 p, Invariant f, Invariant g) => Invariant2 (Biff * * * * p f g) Source | from the |
invmap2Bifunctor :: Bifunctor f => (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> f a b -> f c d Source
Every Bifunctor
is also an Invariant2
functor.
newtype WrappedBifunctor p a b :: (k -> k1 -> *) -> k -> k1 -> *
Constructors
WrapBifunctor | |
Fields
|
Instances
Bifunctor p => Bifunctor (WrappedBifunctor * * p) | |
Bitraversable p => Bitraversable (WrappedBifunctor * * p) | |
Bifoldable p => Bifoldable (WrappedBifunctor * * p) | |
Biapplicative p => Biapplicative (WrappedBifunctor * * p) | |
Bifunctor p => Invariant2 (WrappedBifunctor * * p) Source | from the |
Bifunctor p => Functor (WrappedBifunctor * * p a) | |
Bifoldable p => Foldable (WrappedBifunctor * * p a) | |
Bitraversable p => Traversable (WrappedBifunctor * * p a) | |
Generic1 (WrappedBifunctor k * p a) | |
Bifunctor p => Invariant (WrappedBifunctor * * p a) Source | from the |
Eq (p a b) => Eq (WrappedBifunctor k k1 p a b) | |
Ord (p a b) => Ord (WrappedBifunctor k k1 p a b) | |
Read (p a b) => Read (WrappedBifunctor k k1 p a b) | |
Show (p a b) => Show (WrappedBifunctor k k1 p a b) | |
Generic (WrappedBifunctor k k1 p a b) | |
type Rep1 (WrappedBifunctor k k1 p a) = D1 D1WrappedBifunctor (C1 C1_0WrappedBifunctor (S1 S1_0_0WrappedBifunctor (Rec1 (p a)))) | |
type Rep (WrappedBifunctor k k1 p a b) = D1 D1WrappedBifunctor (C1 C1_0WrappedBifunctor (S1 S1_0_0WrappedBifunctor (Rec0 (p a b)))) |
invmap2Profunctor :: Profunctor f => (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> f a b -> f c d Source
Every Profunctor
is also an Invariant2
functor.
newtype WrappedProfunctor p a b Source
Wrap a Profunctor
to be used as a member of Invariant2
.
Constructors
WrapProfunctor | |
Fields
|
Instances
ProfunctorFunctor (WrappedProfunctor * *) Source | |
ProfunctorMonad (WrappedProfunctor * *) Source | |
ProfunctorComonad (WrappedProfunctor * *) Source | |
Mapping p => Mapping (WrappedProfunctor * * p) Source | |
Traversing p => Traversing (WrappedProfunctor * * p) Source | |
Choice p => Choice (WrappedProfunctor * * p) Source | |
Cochoice p => Cochoice (WrappedProfunctor * * p) Source | |
Closed p => Closed (WrappedProfunctor * * p) Source | |
Strong p => Strong (WrappedProfunctor * * p) Source | |
Costrong p => Costrong (WrappedProfunctor * * p) Source | |
Profunctor p => Profunctor (WrappedProfunctor * * p) Source | |
Profunctor p => Invariant2 (WrappedProfunctor * * p) Source | |
Profunctor p => Invariant (WrappedProfunctor * * p a) Source | |
Eq (p a b) => Eq (WrappedProfunctor k k p a b) Source | |
Ord (p a b) => Ord (WrappedProfunctor k k p a b) Source | |
Read (p a b) => Read (WrappedProfunctor k k p a b) Source | |
Show (p a b) => Show (WrappedProfunctor k k p a b) Source |