HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebarbies-2.1.1.0Haskell2010

Data.Functor.Transformer

Functors on indexed-types.

  • 2 types
  • 6 classes
  • 18 values
  • Packagebarbies-2.1.1.0
  • Exports26
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTransformer.hs

Functor

1 declaration
classclass FunctorT (t :: (k -> Type) -> k' -> Type) where
#

Functor from indexed-types to indexed-types. Instances of FunctorT should satisfy the following laws:

tmap id = id
tmap f . tmap g = tmap (f . g)

There is a default tmap implementation for Generic types, so instances can derived automatically.

Methods

  • tmap :: (forall (a :: k). f a -> g a) -> t f x -> t g x
Instances18FunctorT, …
  • FunctorT LiftDefined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT MaybeTDefined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT BackwardsDefined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT IdentityTDefined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT ReverseDefined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (AccumT w)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (ExceptT e)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (ReaderT r)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (StateT s)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (StateT s)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (WriterT w)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (WriterT w)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (Product f)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (Sum f)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (RWST r w s)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • FunctorT (RWST r w s)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • Functor f => FunctorT (Compose f)Defined in barbies-2.1.1.0 · Barbies.Internal.FunctorT
  • (forall (f :: k'). FunctorB (b f)) => FunctorT (Flip b)Defined in barbies-2.1.1.0 · Barbies.Bi

Traversable

1 declaration
classclass FunctorT t => TraversableT (t :: (k -> Type) -> k' -> Type) where
#

Indexed-functors that can be traversed from left to right. Instances should satisfy the following laws:

 t . ttraverse f   = ttraverse (t . f)  -- naturality
ttraverse Data.Functor.Identity = Data.Functor.Identity           -- identity
ttraverse (Compose . fmap g . f) = Compose . fmap (ttraverse g) . ttraverse f -- composition

There is a default ttraverse implementation for Generic types, so instances can derived automatically.

Methods

Instances12TraversableT, …

Utility functions

valuetfor
  1. :: (TraversableT t, Applicative e)
  2. => t f x
  3. -> forall (a :: k). f a -> e (g a)
  4. -> e (t g x)
#

ttraverse with the arguments flipped. Useful when the traversing function is a large lambda:

tfor someTransformer $ fa -> ...
valuettraverse_
  1. :: (TraversableT t, Applicative e)
  2. => forall (a :: k). f a -> e c
  3. -> t f x
  4. -> e ()
#

Map each element to an action, evaluate these actions from left to right, and ignore the results.

valuetfoldMap
  1. :: (TraversableT t, Monoid m)
  2. => forall (a :: k). f a -> m
  3. -> t f x
  4. -> m
#

Map each element to a monoid, and combine the results.

Distributive

5 declarations
classclass FunctorT t => DistributiveT (t :: (Type -> Type) -> i -> Type) where
#

A FunctorT where the effects can be distributed to the fields: tdistribute turns an effectful way of building a transformer-type into a pure transformer-type with effectful ways of computing the values of its fields.

This class is the categorical dual of TraversableT, with tdistribute the dual of tsequence and tcotraverse the dual of ttraverse. As such, instances need to satisfy these laws:

tdistribute . h = tmap (Compose . h . getCompose) . tdistribute    -- naturality
tdistribute . Identity = tmap (Compose . Identity)                 -- identity
tdistribute . Compose = fmap (Compose . Compose . fmap getCompose . getCompose) . tdistribute . fmap distribute -- composition

By specializing f to ((->) a) and g to Identity, we can define a function that decomposes a function on distributive transformers into a collection of simpler functions:

tdecompose :: DistributiveT b => (a -> b Identity) -> b ((->) a)
tdecompose = tmap (fmap runIdentity . getCompose) . tdistribute

Lawful instances of the class can then be characterized as those that satisfy:

trecompose . tdecompose = id
tdecompose . trecompose = id

This means intuitively that instances need to have a fixed shape (i.e. no sum-types can be involved). Typically, this means record types, as long as they don't contain fields where the functor argument is not applied.

There is a default implementation of tdistribute based on Generic. Intuitively, it works on product types where the shape of a pure value is uniquely defined and every field is covered by the argument f.

Methods

Instances15DistributiveT, …
valuetdecompose :: DistributiveT t => (a -> t Identity x) -> t ((->) a) x
#

Decompose a function returning a distributive transformer, into a collection of simpler functions.

Applicative

1 declaration
classclass FunctorT t => ApplicativeT (t :: (k -> Type) -> k' -> Type) where
#

A FunctorT with application, providing operations to:

  • embed an "empty" value (tpure)

  • align and combine values (tprod)

It should satisfy the following laws:

Naturality of tprod
tmap ((Pair a b) -> Pair (f a) (g b)) (u `tprod' v) = tmap f u `tprod' tmap g v
Left and right identity
tmap ((Pair _ b) -> b) (tpure e `tprod' v) = v
tmap ((Pair a _) -> a) (u `tprod' tpure e) = u
Associativity
tmap ((Pair a (Pair b c)) -> Pair (Pair a b) c) (u `tprod' (v `tprod' w)) = (u `tprod' v) `tprod' w

It is to FunctorT in the same way is Applicative relates to Functor. For a presentation of Applicative as a monoidal functor, see Section 7 of Applicative Programming with Effects.

There is a default implementation of tprod and tpure based on Generic. Intuitively, it works on types where the value of tpure is uniquely defined. This corresponds rougly to record types (in the presence of sums, there would be several candidates for tpure), where every field is either a Monoid or covered by the argument f.

Methods

Instances5ApplicativeT

Utility functions

valuetzipWith4
  1. :: ApplicativeT t
  2. => forall (a :: k). f a -> g a -> h a -> i a -> j a
  3. -> t f x
  4. -> t g x
  5. -> t h x
  6. -> t i x
  7. -> t j x
#

An equivalent of zipWith4.

Monad

1 declaration
classclass FunctorT t => MonadT (t :: (k' -> Type) -> k' -> Type) where
#

Some endo-functors on indexed-types are monads. Common examples would be "functor-transformers", like Compose or ReaderT. In that sense, MonadT is similar to MonadTrans but with additional structure (see also mmorph's MMonad class).

Notice though that while lift assumes a Monad instance of the value to be lifted, tlift has no such constraint. This means we cannot have instances for most "monad transformers", since lifting typically involves an fmap.

MonadT also corresponds to the indexed-monad of Kleisli arrows of outrageous fortune.

Instances of this class should to satisfy the monad laws. They laws can stated either in terms of (tlift, tjoin) or (tlift, tembed). In the former:

tmap h . tlift = tlift . h
tmap h . tjoin = tjoin . tmap (tmap h)
tjoin . tlift  = id
tjoin . 'tmap tlift' = id
tjoin . tjoin = tjoin . tmap tjoin

In the latter:

tembed f . tlift = f
tembed tlift = id
tembed f . tembed g = tembed (tembed f . g)

Methods

  • tlift :: f a -> t f a

    Lift a functor to a transformed functor.

  • tjoin :: t (t f) a -> t f a

    The conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.

  • tembed :: MonadT t => (forall (x :: k'). f x -> t g x) -> t f a -> t g a

    Analogous to (=<<).

Instances8MonadT, …

Constraints and instance dictionaries

2 declarations
classclass FunctorT t => ConstraintsT (t :: (kl -> Type) -> kr -> Type) where
#

Instances of this class provide means to talk about constraints, both at compile-time, using AllT, and at run-time, in the form of Dict, via taddDicts.

A manual definition would look like this:

data T f a = A (f Int) (f String) | B (f Bool) (f Int)

instance ConstraintsT T where
  type AllT c T = (c Int, c String, c Bool)

  taddDicts t = case t of
    A x y -> A (Pair Dict x) (Pair Dict y)
    B z w -> B (Pair Dict z) (Pair Dict w)

Now, when we given a T f, if we need to use the Show instance of their fields, we can use:

taddDicts :: AllT Show t => t f -> t (Dict Show `Product' f)

There is a default implementation of ConstraintsT for Generic types, so in practice one will simply do:

derive instance Generic (T f a)
instance ConstraintsT T

Associated types

  • type family AllT (c :: k -> Constraint) (t :: (kl -> Type) -> kr -> Type) :: Constraint

    AllT c t should contain a constraint c a for each a occurring under an f in t f.

    For requiring constraints of the form c (f a), use AllTF.

Methods

typetype AllTF (c :: k -> Constraint) (f :: k1 -> k) (t :: (kl -> Type) -> kr -> Type) = AllT (ClassF c f) t
#

Similar to AllT but will put the functor argument f between the constraint c and the type a.

Utility functions

valuetmapC
  1. :: (AllT c t, ConstraintsT t)
  2. => forall (a :: k). c a => f a -> g a
  3. -> t f x
  4. -> t g x
#

Like tmap but a constraint is allowed to be required on each element of t.

Support for generic derivations

1 declaration
newtypenewtype Rec p a (x :: k)
#

Constructors

Instances45GTraversable, GApplicative, GFunctor, GDistributive, GBare, GConstraints, …