Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 277
Shuheng Zheng Avatar asked Feb 07 '23 13:02

Shuheng Zheng


2 Answers

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.

like image 122
Michael Avatar answered Feb 09 '23 02:02

Michael


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. :)

like image 35
Chris Beck Avatar answered Feb 09 '23 04:02

Chris Beck