generic-data-1.1.0.2: Deriving instances with GHC.Generics and related utilities

Safe Haskell | Safe-Inferred
Language | Haskell2010

Generic.Data.Internal.Traversable

Contents

- Library
  - Foldable
  - Traversable

Description

Generic implementation of `Foldable` and `Traversable`.

There is already a naive implementation using the generic ``Rep``'s own instances of `Foldable` and `Traversable`. However, deriving then generates a lot of code that may not be simplified away by GHC, that results in unnecessary run-time overhead.

In contrast, this implementation guarantees that the generated code is identical to stock-derived instances of `Foldable` and `Traversable`, which have the following syntactic properties:

- constructors with zero fields use `pure` once;
- constructors with one field use `fmap` once;
- constructors with n >= 2 fields use `liftA2` once and `(`<*>`)` n-2 times.

The heavy lifting is actually done by the ap-normalize library.

Synopsis

- gfoldMap :: (Generic1 f, GFoldable (Rep1 f), Monoid m) => (a -> m) -> f a -> m
- gtraverse :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => (a -> m b) -> f a -> m (f b)
- gsequenceA :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => f (m a) -> m (f a)
- class GFoldable_ t => GFoldable (t :: Type -> Type)
- class GTraversable_ t => GTraversable (t :: Type -> Type)
- class (GFoldMap t, Foldable t) => GFoldable_ (t :: Type -> Type)
- class (GTraverse Kleisli t, GTraverse Equal t) => GTraversable_ (t :: Type -> Type)
- data Maybe' m
  - = Nothing'
  - | Just' m
- type EndoM m = Endo (Maybe' m)
- liftEndoM :: Monoid m => m -> EndoM m
- lowerEndoM :: Monoid m => EndoM m -> m
- lowerMaybe :: Monoid m => Maybe' m -> m
- class GFoldMap (t :: Type -> Type) where
  - gfoldMap_ :: Monoid m => (a -> m) -> t a -> EndoM m
- data Equal (f :: Type -> Type) a b where
  - Refl :: forall (f :: Type -> Type) b. Equal f (f b) b
- newtype Kleisli (f :: Type -> Type) a b = Kleisli (a -> f b)
- class GTraverse (arr :: (Type -> Type) -> Type -> Type -> Type) (t :: Type -> Type) where
  - gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => arr f a b -> t a -> Aps f (t b)

# Library

gfoldMap :: (Generic1 f, GFoldable (Rep1 f), Monoid m) => (a -> m) -> f a -> m Source #

Generic `foldMap`.

    instance Foldable MyTypeF where
      foldMap = gfoldMap

gtraverse :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => (a -> m b) -> f a -> m (f b) Source #

Generic `traverse`.

    instance Traversable MyTypeF where
      traverse = gtraverse

gsequenceA :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => f (m a) -> m (f a) Source #

Generic `sequenceA`.

    instance Traversable MyTypeF where
      sequenceA = gsequenceA

See also `gtraverse`.

class GFoldable_ t => GFoldable (t :: Type -> Type) Source #

Class of generic representations for which `Foldable` can be derived.

#### Instances

Instances details

GFoldable_ t => GFoldable t Source # |
Instance details Defined in Generic.Data.Internal.Traversable

class GTraversable_ t => GTraversable (t :: Type -> Type) Source #

Class of generic representations for which `Traversable` can be derived.

#### Instances

Instances details

GTraversable_ t => GTraversable t Source # |
Instance details Defined in Generic.Data.Internal.Traversable

class (GFoldMap t, Foldable t) => GFoldable_ (t :: Type -> Type) Source #

Internal definition of `GFoldable`.

#### Instances

Instances details

(GFoldMap t, Foldable t) => GFoldable_ t Source # |
Instance details Defined in Generic.Data.Internal.Traversable

class (GTraverse Kleisli t, GTraverse Equal t) => GTraversable_ (t :: Type -> Type) Source #

Internal definition of `GTraversable`.

#### Instances

Instances details

(GTraverse Kleisli t, GTraverse Equal t) => GTraversable_ t Source # |
Instance details Defined in Generic.Data.Internal.Traversable

## Foldable

data Maybe' m Source #

Isomorphic to `Maybe m`, but we need to micromanage the use of Monoid vs Semigroup to match exactly the output of stock deriving, for inspection testing.

Constructors

Nothing' |
Just' m |

type EndoM m = Endo (Maybe' m) Source #

liftEndoM :: Monoid m => m -> EndoM m Source #

lowerEndoM :: Monoid m => EndoM m -> m Source #

lowerMaybe :: Monoid m => Maybe' m -> m Source #

class GFoldMap (t :: Type -> Type) where Source #

Methods

gfoldMap_ :: Monoid m => (a -> m) -> t a -> EndoM m Source #

#### Instances

Instances details

GFoldMap Par1 Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> Par1 a -> EndoM m Source #
GFoldMap (U1 :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> U1 a -> EndoM m Source #
GFoldMap (V1 :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> V1 a -> EndoM m Source #
Foldable t => GFoldMap (Rec1 t) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> Rec1 t a -> EndoM m Source #
(GFoldMap f, GFoldMap g) => GFoldMap (f :*: g) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> (f :*: g) a -> EndoM m Source #
(GFoldMap f, GFoldMap g) => GFoldMap (f :+: g) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> (f :+: g) a -> EndoM m Source #
GFoldMap (K1 i a :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a0 -> m) -> K1 i a a0 -> EndoM m Source #
(Foldable t, Foldable f) => GFoldMap (t :.: f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> (t :.: f) a -> EndoM m Source #
GFoldMap f => GFoldMap (M1 i c f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gfoldMap_ :: Monoid m => (a -> m) -> M1 i c f a -> EndoM m Source #

## Traversable

data Equal (f :: Type -> Type) a b where Source #

Constructors

Refl :: forall (f :: Type -> Type) b. Equal f (f b) b |

#### Instances

Instances details

GTraverse Equal Par1 Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Equal f a b -> Par1 a -> Aps f (Par1 b) Source #
Traversable t => GTraverse Equal (Rec1 t) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Equal f a b -> Rec1 t a -> Aps f (Rec1 t b) Source #
(Traversable t, Traversable f) => GTraverse Equal (t :.: f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => Equal f0 a b -> (t :.: f) a -> Aps f0 ((t :.: f) b) Source #

newtype Kleisli (f :: Type -> Type) a b Source #

Constructors

Kleisli (a -> f b) |

#### Instances

Instances details

GTraverse Kleisli Par1 Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Kleisli f a b -> Par1 a -> Aps f (Par1 b) Source #
Traversable t => GTraverse Kleisli (Rec1 t) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Kleisli f a b -> Rec1 t a -> Aps f (Rec1 t b) Source #
(Traversable t, Traversable f) => GTraverse Kleisli (t :.: f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => Kleisli f0 a b -> (t :.: f) a -> Aps f0 ((t :.: f) b) Source #

class GTraverse (arr :: (Type -> Type) -> Type -> Type -> Type) (t :: Type -> Type) where Source #

Methods

gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => arr f a b -> t a -> Aps f (t b) Source #

#### Instances

Instances details

GTraverse Equal Par1 Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Equal f a b -> Par1 a -> Aps f (Par1 b) Source #
GTraverse Kleisli Par1 Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Kleisli f a b -> Par1 a -> Aps f (Par1 b) Source #
GTraverse arr (U1 :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => arr f a b -> U1 a -> Aps f (U1 b) Source #
GTraverse arr (V1 :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => arr f a b -> V1 a -> Aps f (V1 b) Source #
Traversable t => GTraverse Equal (Rec1 t) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Equal f a b -> Rec1 t a -> Aps f (Rec1 t b) Source #
Traversable t => GTraverse Kleisli (Rec1 t) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a b. Applicative f => Kleisli f a b -> Rec1 t a -> Aps f (Rec1 t b) Source #
(GTraverse arr f, GTraverse arr g) => GTraverse arr (f :*: g) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => arr f0 a b -> (f :*: g) a -> Aps f0 ((f :*: g) b) Source #
(GTraverse arr f, GTraverse arr g) => GTraverse arr (f :+: g) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => arr f0 a b -> (f :+: g) a -> Aps f0 ((f :+: g) b) Source #
GTraverse arr (K1 i a :: Type -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f :: Type -> Type) a0 b. Applicative f => arr f a0 b -> K1 i a a0 -> Aps f (K1 i a b) Source #
(Traversable t, Traversable f) => GTraverse Equal (t :.: f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => Equal f0 a b -> (t :.: f) a -> Aps f0 ((t :.: f) b) Source #
(Traversable t, Traversable f) => GTraverse Kleisli (t :.: f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => Kleisli f0 a b -> (t :.: f) a -> Aps f0 ((t :.: f) b) Source #
GTraverse arr f => GTraverse arr (M1 i c f) Source # |
Instance details Defined in Generic.Data.Internal.Traversable Methods gtraverse_ :: forall (f0 :: Type -> Type) a b. Applicative f0 => arr f0 a b -> M1 i c f a -> Aps f0 (M1 i c f b) Source #