4 ms·
>For example, if you have an array of integers, apply a function to the contents with map, and you end up with an array of integers again, then you have an endo
by mlevental 8y ago
>For example, if you have an array of integers, apply a function to the contents with map, and you end up with an array of integers again, then you have an endofunctor
sorry i'm confused. been learning haskell in parallel with cat theory. isn't the only category in haskell Hask? whose objects are types? in that sense aren't all of the `Functor`s in haskell endofunctors?
- mikekchar 8y agoA category is just a grouping of mappings that have something in common. When we say "in the category of endofunctors" what we are saying is "consider the grouping of mappings where in the source set and the target set are the same". The "source set" is the set of possible values for the input of the fmap function -- in other words the type of the input parameter. The "target set" is the set of possible values for the output of the fmap function -- in other words the type of the return value. So if you have a container that contains a certain type of data and you can call fmap and it returns the same container with the same type of data, then it can be an endofunctor. Remember that we are talking about the "category of endofunctors", so it's basically saying "We are talking about operations where you start with a certain type and you return values of the same type". In that way, any monoid is a monad when you return the same type as you started with. You can't use it as a monad in a different context. In Haskell the compiler forbids you from doing it, but it's easy to get it wrong in a langues without static type checking (to much hilarity). Long story short, using a functor with bind is an endofunctor. Using a functor with map is an endofunctor if your transforming function doesn't change its type, but it's not guaranteed.
- dwohnitmok 8y agoThere are many many categories in Haskell. All you need for a category is to identify _something_ as the objects, _something_ as the arrows, and make sure your identifications result in something that matches the category laws. As a simple example, every monoid is a category with a single object (hence the mono- prefix). Hence every monoid in Haskell is a category. Now it is true that all `Functor`s in the sense of the Haskell typeclass are endofunctors in category theory, because a higher-kinded type can be thought of as a mapping between types in Haskell (e.g. `Maybe` maps `Int` to the new type `Maybe Int`) and `fmap` can be thought of as a mapping of functions between types to their remapped types (we take `a -> b` and replace it with `Maybe a -> Maybe b`), which results in a functor from Hask to Hask. However, it is not true that the only functors (in the category theory sense) are instances of the `Functor` typeclass.
- omaranto 8y agoYou are mostly right. Haskell Functors, when thought of as mathematical functors do go from Hask to Hask and thus are endofunctors. But you also say that "Hask is the only category". That needs some qualification, since the type class Control.Category is also in common use and models mathematical categories. Notice that the standard Functor class doesn't use Control.Category to specify the domain and target, so what you said about Haskell Functors modelling functors Hask -> Hask is correct.