31

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.

6
  • 4
    The extent of the shared underlying meaning is that functor is a pseudo-Latin word made by combining funct- (as in function) with the agentive suffix -or. So it should mean "that which performs or executes something". Which is actually the exact meaning of the Latin word factor, but that word is too overloaded in English.
    – hobbs
    Commented Jan 29, 2021 at 0:46
  • That said, instances of Haskell Functor do feel more like fungends than functors to me.
    – hobbs
    Commented Jan 29, 2021 at 0:48
  • 3
    @hobbs - I can't even tell anymore if you're making stuff up. Commented Jan 29, 2021 at 16:04
  • Nuance: "callable" in Python refers to anything that is callable (including both regular functions and other callable objects) whereas "functor" in C++ refers to only the callable objects.
    – mtraceur
    Commented Jan 29, 2021 at 18:46
  • @mtraceur In Python functions are objects, in C++ they aren't
    – Caleth
    Commented Jan 31, 2021 at 11:51

3 Answers 3

33

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]

4
  • 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. Commented 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? Commented Jan 28, 2021 at 9:02
  • I added a paragraph each about SML and Prolog. Thanks for pointing that out, much appreciated! Commented Jan 28, 2021 at 9:17
  • 4
    Functor in Prolog refers to the "type" of an object; the nature of the particular grouping of objects. date(A, B) has functor date/2 while date(A, B, C) has functor date/3 (as commonly expressed).
    – IS4
    Commented Jan 29, 2021 at 11:24
20

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.

1
  • 7
    Great 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
    Commented Jan 28, 2021 at 20:15
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.