Search code examples
javascripthaskellfunctorcategory-theory

Is there any general functor (not limited to endofunctor) usage in programming?


Is there any general functor (not limited to endofunctor) usage in programming?

I understand the reason endofunctor employed is to make the structure simple like monoid or monad.

I also understand ultimately, all the value is settled down to a category of a programming language (such as Hask), but what I'm talking about here is endofunctor between the same category of Strings, Numbers, Booleans, or Functions.

Related questions:

Are all Haskell functors endofunctors?

Differences between functors and endofunctors


Solution

  • First, yes.

    For example, we all know that a monoid can be defined to be a single-object category with

    • arrows to be elements
    • the single object has no meaning
    • composition to be operator ((<>) in Haskell)
    • id arrow to be the identity (mempty in Haskell).

    And a homomorphism between two monoids becomes a functor between two categories in this sense.

    Now, say, type A and B are both monoids; A functor between them is just a homomorphic function f :: A -> B that maps each A to B, preserving the composition.

    But, wait, f :: A -> B is not even a Functor (note that I use the monospaced typeface here)!

    No, it is not a Functor in Haskell, but it still is a functor in mathematical sense.

    So, to emphasize, I state it again: "Non-endo" functors ARE used in programming, and probably even more often than endofunctors.

    The point here is that category theory is a highly abstract theory - It provides notions for abstracting concrete objects. We can define these notions to mean different things in different contexts.

    And Hask (or Set, or subcategories of Set) is just one of these infinite definitions, which makes

    • arrows to be functions
    • objects to be types (or, sets)
    • composition to be function composition (.)
    • id arrow to be the id function.

    Compare this "categorical universe" definition with the "categorical monoid" definition above - congrats, you've known two different takes on categories now!

    To conclude, remember that category theory itself is just some abstractions. Abstractions themselves have no meaning and no use at all. We connect them to real things, and only in this way they can bring us convenience. Understand abstract concepts through concrete examples, but NEVER simplify these concepts themselves to anything concrete (Like, never simplify functors to merely the functors within a certain "categorical universe" (e.g. Hask, Set, etc)!).

    P.S. If you ask "Is there a functor that sends Hask to another category in Haskell?" then the answer can be yes or no. For example, you can define a category Hask * Hask to contain any two types' cartesian product, and a functor data Diag a = Diag a a, fmap f x = Diag (f x) (f x) that sends each type A to its square A * A. However, Hask * Hask is still a subcategory of Hask, so we may say this is an endofunctor too.