# Struggling to find the relation between a Maybe structure and the endofunctor

Monad is a monoid in the category of endofunctors. Endofunctor is a functor that maps to itself. What does it mean? Well it means that, no matter which element in the set is taken as an input, the output value must also belong to the same set. I understand that if the input of the function is an integer, then the output of the function also must be an integer.

BTW, which method/function/functor we are looking at? Is it the constructor that is considered to be the endofunctor? Or perhaps it is a bind operator? If it is the latter one, then how do we detect if the input and the output elements belong the same set, considering we have multiple input arguments and only single output one?

If we assume the constructor is the endofunctor then when we deal with the `Maybe` structure, we take in a type and return a different type. In this case, I've got some questions:

1. Since it is an endofunctor shouldn't the input and output types match? For instance, if we pass in an integer, shouldn't it be that the output must also be an integer? Or we only care that we take a type and return a type (and the fact that types are different doesn't bother us)? Or wrapping around a type into something that preserves the original one is considered "the same''? Basically what does (and what doesn't) the set (that endofunctor uses) contain?
2. Am I right to assume that `Maybe` structure is a functor (even an endofunctor) in the first place? I mean since a functor is a function, then `Maybe` cannot be one since it is not a function, but an object.

I understand that category is a thing that consists of two sets:

1. The set of objects
2. The morphisms/arrows/maps between the sets

I also understand that when we deal with monads, the set of objects in the category must be endofunctors and the morphisms then would be compositions (binary operations) between these functors (I omit other important properties here for simplicity).

Furthermore, I understand that the idea behind the prefix "endo" means that both the domain of the functor and the codomain of it must be elements of the same set. Hence - my question.

• Good question. Useless for 'practical' programmers, fun for curious-about-theory programmers. I am reading blog.rockthejvm.com/… to understand it. The crux seems to be that the Functor is a function from Type to Type (where Type is the set of types): it converts e.g. a `int` to `vector<int>`, or `bool` to `Maybe<bool>`. Jul 17, 2022 at 12:32

In this answer I will use C++ for "language that allows monads". Feel free to substitute whichever language you want in place.

we only care that we take a type and return a type

Exactly. We aren't talking about C++ functions, we are talking about mathematical functions who's domain and codomain are C++ types.

[`Maybe`] is not a function, but an object.

No, it is neither a C++ function nor a C++ object. `Maybe` is not an object, nor is it a class. It is a class template, of one type parameter.

Any class template is a mathematical function with a codomain of the set of C++ types.

Any template of one type parameter (and no value or template template parameters) is a function with a domain of the set of C++ types.

When you supply a type parameter, you get a class, e.g. `Maybe<int>`, which you can then use to declare objects, e.g. `Maybe<int> foo;`

• Thank you for your answer! Can you tell me please, is it the constructor that is an endofunctor? Jul 18, 2022 at 11:49
• @pro100tom what do you mean by "the constructor"? `Maybe<int>::Maybe(int)`? That's just an ordinary member function of a class that is the instantiation of a template. It is the template `Maybe`, not a class `Maybe<int>` that is a monad Jul 18, 2022 at 11:57
• Monad is an endofunctor; it means that it must be a function. Since `Maybe` is not a function, it cannot be a monad (by definition). However `Maybe` has a constructor, which is a function. So my question is, is the constructor of the `Maybe` that is a monad? Jul 18, 2022 at 12:19
• I'm not even sure a functor is a mathematical function - for one, it acts on both the objects and the morphisms of its target category, for another, functions act on sets and neither the collection of objects nor the collection of morphisms in a category need to be sets. (Cf the category Set whose objects are sets - Russell's paradox leads to problems if we want a "set of all sets".) Mathematicians call categories where the two are sets "small" and I'm not sure programming language type system categories qualify. Jul 18, 2022 at 17:16
• And you could view this as mathematical nitpicking, but I generally don't think "function" is the best way to think about what a functor is, especially given the potential for confusion with functions in the actual programming language. A method of transforming one category to another, which boils down to a type constructor that includes a map function in the constructed type in the endofunctors-of-a-type-system case, seems less likely to lead people astray. Jul 18, 2022 at 17:21