[Haskell-cafe] Better names for
Olaf Klinke
olf at aatal-apotheke.de
Wed Apr 22 13:17:45 UTC 2020
> I've heard several times that some Haskell type classes and methods
> are
> ill-named.
>
> For instance, a functor can be both covariant and contravariant,
> while
> Haskell `Functor` typeclass i just covariant. Would `Cofunctor` be a
> better name for it? And what about the `fmap` method? Would it be
> better
> to call it `comap`? How about `Bifunctor`, `Profunctor` and others?
Hi Marc,
part of the answer is: For historical reasons and to preserve backwards
compatibility.
The term "Functor", however, comes from category theory. MacLane [1]
defines the term "Functor" to refer to covariant functors. The notion
of "contravariant" is introduced via the concept of "opposite category"
[2] which has the same objects but direction of arrows reversed. Thus a
contravariant functor F: C -> D is just an ordinary functor C -> D^op.
Category theory has no name for the action of a functor on arrows,
Haskell had to make up a name, which ended up to be "fmap". Sometimes
the notation F(f) is used in category theory where in Haskell we write
fmap f. If I could re-define Haskell from scratch, I'd probably name
the Functor type class function 'map' and omit the map function on
lists, since it is the same as the Functor instance.
As for your suggestion of 'comap': The prefix co- in category theory is
mostly used in conjunction with the opposite construction [3], whence a
category theorist would believe 'comap' to mean the opposite of 'map'.
Regards,
Olaf
[1] Saunders MacLane: Categories for the Working Mathematician. ISBN 7-
5062-6008-5
[2] See Op in Data.Functor.Contravariant
[3] See categorical concepts like limit vs. colimit, cone vs. cocone.
More information about the Haskell-Cafe
mailing list