3 ms·
i think i got what functors are in category theory ( aka morphisms between categories , IIUC ), but i can't understand what it is in haskell. There's a link mis
by bsaul 12y ago
i think i got what functors are in category theory ( aka morphisms between categories , IIUC ), but i can't understand what it is in haskell. There's a link missing in my head between category theory and programming language.
I also couldn't find a post or a video that would explain what monads are in cateogy theory.
- kqr 12y agoWhen the type is some sort of container type (Maybe, Either, List, Tuple and so on), the functor interface gives you a function that will change the contents of the container, while preserving the container structure. Example: fmap (*3) (Just 12) === Just 36 When the type is some sort of computation type (IO, Promise, STM, Parser, unwrapped Reader), the functor interface gives you a function that will take a function and compose it with the computation. In other words, the result of the computation will be passed through your specified function before it is finally returned. Example: fmap (+5) (getPromise age) -- promise age is filled with 43 and -- the fmap call will return 48
- dllthomas 12y agoFirst, all functors in Haskell are endofunctors in Hask. They map Haskell types to other Haskell types, and Haskell functions to other Haskell functions. A functor in Category Theory has two parts - a map of the objects and a map of the arrows. In Haskell, when you create a Functor instance, you are saying "this type constructor is the map for objects; here's how you map the arrows". It's typically not the best place to start, but since you asked - a monad, in category theory, is a functor plus two natural transformations. In Haskell, the functor is the type constructor plus fmap from the Functor instance. The natural transformations are return (eta, in the math) and join (mu). Haskell programmers have historically found it more useful to talk about bind than join, but in the presence of fmap you can implement either from the other, so there's not much theoretical difference: join m = bind m id bind m f = join (fmap f m)
- bsaul 12y agoThanks for that answer. I first thought monads were a completely different beast than functors, but considering the small number of fundamental objects category theory deals with, i had a hard time finding out what.