Functor/applicative composition: $(\text{Compose}\;F\;G)\;\alpha = F\,(G\;\alpha)$.
This witnesses the classical result that the composition of two functors is a functor, and the composition of two applicatives is an applicative.
Use cases:
- Combining effects:
Compose IO Option αrepresents an IO action that may fail - Nested mapping:
map fonCompose F Gmaps through both layers
- getCompose : F (G α)
Unwrap the composed value: $F\,(G\;\alpha)$.
Instances For
Functor instance for Compose F G: maps through both layers.
$$\text{fmap}\;f\;(\text{Compose}\;x) = \text{Compose}\;(\text{fmap}\;(\text{fmap}\;f)\;x)$$
Equations
- Data.Functor.Compose.instFunctor = { map := fun {α β : Type ?u.34} (f : α → β) (c : Data.Functor.Compose F G α) => { getCompose := (fun (x : G α) => f <$> x) <$> c.getCompose } }
Identity law for composed functors: $\text{fmap}\;\text{id} = \text{id}$.
If $F$ and $G$ are both lawful functors, their composition is also lawful.
Composition law for composed functors: $\text{fmap}\;(f \circ g) = \text{fmap}\;f \circ \text{fmap}\;g$.
Follows from the composition laws of $F$ and $G$ individually.
Pure instance for Compose F G: wraps a value in both layers.
$$\text{pure}\;a = \text{Compose}\;(\text{pure}\;(\text{pure}\;a))$$
Seq instance for Compose F G: applies through both layers using
the applicative structure of $F$ and $G$.
$$\text{Compose}\;f \mathbin{<*>} \text{Compose}\;x = \text{Compose}\;((\mathbin{<*>}) \mathbin{<\$>} f \mathbin{<*>} x)$$
Equations
- One or more equations did not get rendered due to their size.
Applicative instance for Compose F G: the composition of two applicatives
is an applicative.
Equations
- One or more equations did not get rendered due to their size.