I'm trying to understand whether the Haskell and C++ communities mean different things by the word "functor", or if there's some underlying concept that unifies the two meanings.
My understanding is that:
For Haskell, a functor is a structure/container that can be mapped over, i.e. a function may be applied to the values held within the structure/container without changing the (uh!) structure of the structure/container.
For C++, a functor is simply a class supporting
operator()
; what one might refer to as a callable in Python.
Are those definitions correct? If so, these seem opposite (but related) definitions to me; one is something a function is applied to, and the other is the function being applied.
3 Answers 3
The two meanings are unrelated.
The Haskell community (and really the Functional Programming community in general, and even the general programming community beyond FP) uses the term Functor in the sense of Category Theory. That's the same branch of mathematics where also the concepts of Monads, Duality, Arrows, and many other come from.
The C++ community is one of only a few programming communities that does not use this meaning. There, as you correctly identified, it simply means an object that has an operator ()
, and as such is comparable to a callable in Python, an object with an apply
method in Scala, or an object which responds to a call
message in Ruby.
Another example is Standard ML, where a Functor is a concept in its Module System representing the concept of Parametric Modules: SML's module system consists of Structures (which other module systems would call "module implementations"), Signatures (which is just what it sounds like: the public module interface specification), and Functors (kind of like module-level functions from structures to structures, similar to how a type constructor ("generic" if you speak Java or C#) is kind of like a type-level function from types-to-types). So, Functors are how SML does Parametric Modules which is something that not many module systems have.
They are sorta related to the Category Theoretical notion.
The term also exists in Prolog, but I don't know much about Prolog, so I will just leave you with this:
In Prolog, the word functor is used to refer to the atom at the start of a structure, along with its arity, that is, the number of arguments it takes.
[...]
The term functor is used in a different sense in mathematics and in functional programming, and a different way again in philosophy.
[Source: http://www.cse.unsw.edu.au/~billw/dictionaries/prolog/functor.html]
-
Other languages using "functor" with meanings that are probably not derived from category theory include Prolog and Standard ML, although you could make the case that the category theory definition makes sense for SML.Levi Pearson– Levi Pearson2021年01月28日 09:01:37 +00:00Commented Jan 28, 2021 at 9:01
-
I don't know about Prolog. I know about SML, so it's a bit embarrassing that I forgot about that. From my understanding, it is somewhat like a parametric module, correct?Jörg W Mittag– Jörg W Mittag2021年01月28日 09:02:58 +00:00Commented Jan 28, 2021 at 9:02
-
I added a paragraph each about SML and Prolog. Thanks for pointing that out, much appreciated!Jörg W Mittag– Jörg W Mittag2021年01月28日 09:17:09 +00:00Commented Jan 28, 2021 at 9:17
-
4Functor in Prolog refers to the "type" of an object; the nature of the particular grouping of objects.
date(A, B)
has functordate/2
whiledate(A, B, C)
has functordate/3
(as commonly expressed).IS4– IS42021年01月29日 11:24:14 +00:00Commented Jan 29, 2021 at 11:24
The word "functor" has been around for a very long time, and modern usage stems from analytic philosophy, especially topics related to logic and language. The first usage I found was due to Rudolph Carnap.
It has been used as a jargon term for different specific things in a lot of fields since then, but it's generally used to refer to something "function-like" that doesn't meet the definition of a mathematical function.
While the Haskell usage definitely comes straight from category theory, this is a relatively more recent usage and I believe C++ and other programming language usages borrow from other sources.
I wrote a blog on this with citations for my research on the question a few years ago, you can find it here if you'd like more historical details.
-
7Great article that you have in the link! It would be very helpful if you would in the answer give the key elements and dates that show that Carnap’s concept predates category theory ! (not everybody will read the link and realise how informed this answer is!)Christophe– Christophe2021年01月28日 20:15:47 +00:00Commented Jan 28, 2021 at 20:15
mostly yes, in Haskell a functor also needs to obey the functor laws
fmap id = id
and
fmap (g . f) = fmap g . fmap f
a C++ functor is essentially just a pattern for making a first class function, as any normal Haskell function would be, out of an object
Explore related questions
See similar questions with these tags.
Functor
do feel more like fungends than functors to me.