cat/natural
NaturalTransformation
type {minimal implementation transform
}.
Composition
function.
Types
A natural transformation of two functors
F and G is a collection of functions such that for every type a we have a function (component
) that goes from F a to G a.
This implementation in gleam is a bit more restrictive in that each component
is an instantiation of the generic
function transform for the type a.
Examples
A natural transformation from Option
to List
will contain a transform
function that, for any type a
, takes an Option(a)
and returns a List(a)
.
transform: fn(Option(a)) -> List(a)
pub opaque type NaturalTransformation(f, g, fa, ga)
Functions
pub fn ffmap(
alpha: NaturalTransformation(Functor(a, b, c, d, e), f, g, h),
) -> fn(fn(b) -> c) -> fn(d) -> e
Getter for the fmap
of f
of NaturalTransformation
.
pub fn gfmap(
alpha: NaturalTransformation(a, Functor(b, c, d, e, f), g, h),
) -> fn(fn(c) -> d) -> fn(e) -> f
Getter for the fmap
of g
of NaturalTransformation
.
pub fn horizontal_composition(
alpha: NaturalTransformation(
Functor(a, b, c, d, e),
Functor(f, b, c, g, h),
d,
g,
),
beta: NaturalTransformation(
Functor(i, j, k, l, m),
Functor(n, j, k, o, p),
l,
o,
),
) -> NaturalTransformation(
Functor(Pair(a, i), b, c, q, r),
Functor(Pair(f, n), b, c, s, t),
q,
t,
)
Deprecated: Imposible to implement
Horizontal composition
∘ of two natural transformations
.
alpha_a :: F a -> F' a
beta_a :: G a -> G' a
// These transformation compose:
beta ∘ alpha :: G ∘ F -> G' ∘ F'
(beta ∘ alpha)_a = G' (alpha_a) ∘ beta_Fa = beta_F'a ∘ G (alpha_a)
Due to gleam’s type system, we cannot implement this generic function
pub fn new(
f: fn() -> Functor(a, b, c, d, e),
g: fn() -> Functor(f, g, h, i, j),
transform: fn(d) -> k,
) -> NaturalTransformation(
Functor(a, b, c, d, e),
Functor(f, g, h, i, j),
d,
k,
)
Smart constructor
for the NaturalTransformation
type.
Takes two functor instance generation (so that the types stay generic) and a transform function.
pub fn transform(
alpha: NaturalTransformation(a, b, c, d),
) -> fn(c) -> d
Getter for the transform
field of NaturalTransformation
.
pub fn vertical_composition(
alpha: NaturalTransformation(a, b, c, d),
beta: NaturalTransformation(b, e, d, f),
) -> NaturalTransformation(a, e, c, f)
Vertical composition
⋅ of two natural transformations
.
alpha_a :: F a -> G a
beta_a :: G a -> H a
// These morphisms compose:
beta_a ∘ alpha_a :: F a -> H a
// So the natural transformations also compose:
(beta ⋅ alpha)_a = beta_a ∘ alpha_a
Examples
let maybe_const =
vertical_composition(
option_list_transformation(),
list_length_transformation()
)
None
|> transform(maybe_const)
// -> Const(0)
Some("abc")
|> transform(maybe_const)
// -> Const(1)