Heiko Seeberger wrote a great blog post on category theory here:
https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/
In it, he defines a GenericFunctor like so:
trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] {
def fmap[A, B](f: A ->> B): F[A] ->>> F[B]
}
I did not have any luck finding documentation references to the ->> and ->>> symbols in documentation. Could someone please explain what they are doing?
The symbols themselves don't mean anything. They are arbitrary names Heiko picked:
> class Foo[A, B]
defined class Foo
> class Foo[M1[_], M2[_]]
defined class Foo
> class GenericFunctor[->>[_, _], ->>>[_, _], F[_]]
defined class GenericFunctor
They are parts of type parameters that they themselves are type constructors (higher-kinded types if you want to sound fancy).
Type applications can be written infix, so A ->> B
is same as ->>[A, B]
.
As per what's going on... Heiko says
Looking at the ingredients, we find all that we need: Types
A
andB
are mapped to typesF[A]
andF[B]
and mapsA ->> B
are mapped to mapsF[A] ->>> F[B]
.
Since we are talking category theory, we want to avoid the term function because that's implementation specific, but we want to describe something kind of like a function. Something-like-a-function in their lingo is an arrow. We need two of them since we don't want to assume the incoming and outgoing arrows to be the same. These two arrows are represented by ->>
and ->>>
. F[_]
is a container like List
and Option
. I think..
So fmap
(aka map
method in Scala) takes an arrow of values and returns another arrow of containers. Except unlike map
method, fmap
returns an arrow that takes a container.
A specific application of the GenericFunctor
using Function
for both arrows is Functor
. And specific application of Functor
that uses List
for the container is ListFunctor
.
object ListFunctor extends Functor[List] {
def fmap[A, B](f: A => B): List[A] => List[B] = as => as map f
}
So that's taking a function from A
to B
, and returning a function from List[A]
to List[B]
, calling map
internally.
A clue is that they are within square brackets in the trait definition: they are just arbitrary symbols that have been picked by the blog author, just as [T]
is often chosen for generic classes, traits, and methods. These here just happen to be higher-kinded types (i.e. parameters with parameters).
The arrow-like name was chosen because, as he says,
"A ->> B is just another way to write ->>[A, B], which nicely reflects the fact that we are talking about maps here."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With