Shuheng Zheng
Shuheng Zheng

Reputation: 297

Is a C++ functor a functor in the sense of category theory?

A C++ functor is a class that supports overloads the () operator. Is this a functor in the sense of category theory? What are the objects and morphisms?

Upvotes: 4

Views: 1263

Answers (3)

Chris Beck
Chris Beck

Reputation: 16204

No.

IMO the least confusing way to refer to C++ "functors" is "function object".

In Java I guess an object with a call operator is called a "callable", but in C++ that has a slightly broader meaning, including any function pointer or anything else that can be used with the "call" syntax.

It's just one of these technical terms from math that got appropriated by computer scientists to mean something that basically has nothing to do with its original meaning. Take for instance "network topology", a term that people use to describe the layout of a network. This really has nothing to do with the mathematical meaning of "topology", which is concerned with properties of spaces that are preserved under arbitrary continuous transformations. I.e. "a topologist is a mathematician who cannot tell their coffee cup from their donut". Usually IMO it would make more sense to talk about the "network geometry" of a computer network, for most purposes that people talk about a computer network, since they care about things like distances between nodes, density, bottlenecks, etc. Most of which are not topological properties. But that's just a pet peeve of mine. :)

Upvotes: 3

Michael
Michael

Reputation: 5897

No, as Hurkyl said, despite of the name clash inferred by "objects" in objects-oriented programming, OOP functors are not related to category theory functors. A better match with OOP would be to rename OOP class as category, and conversions between types as functors. Something like this:

category Shape;
category Rectangle : forgetful functor Shape;
category Polygon : forgetful functor Shape
{
    functor Polygon( Rectangle & );
};

in place of

class Shape;
class Rectangle : public Shape;
class Polygon : public Shape
{
    implicit Polygon( Rectangle & );
};

Alas, the people who developed OOP are not the same as the ones who developed category theory, thus different lingo.

Upvotes: 4

user1084944
user1084944

Reputation:

No; as far as I am aware, the etymology is that "functor" as referring to a function object is a thing that does something (thus the use of a naming pattern like ----or), where that something is simply a function, and other names like 'functionor' or 'functionator' sound awkward.

Upvotes: 1

Related Questions