by evmar · 7y ago · 0 comments
interface Functor<A> {
map<B>(f: (a: A) => B): Functor<B>;
}
But the TypeScript definition loses something important: the point of a Functor is that you get back the same data type -- there's one `f` in the Haskell defn both in the argument and return type, while this TS definition can give you back any random data type. E.g. the implementation of Array.map could give you back an Either.I don't mean this comment to be a random nitpick of the post. Trying to think these things through is hard, and trying to use things you know to help understand the new idea is not unreasonable. But in particular with PL each thing has so much associated baggage (here in TS, subtyping) that reasoning "by metaphor" often means you end up missing the critical point.
[1] https://github.com/gcanti/fp-ts [2] https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-ki...
interface Functor<A> {
map<B>(f: (a: A) => A): Functor<A>;
}
You map over an option, you get an option. You map over an either, you get an either. etc etc.So you end up with
interface Functor<A> {
map<B>(f: (a: A) => A): Self<B>
}
where `Self` needs to recognize that the type being defined to implement this interface has a "slot". This tends to make things tough.I'm a very low level functional programmer. I'm big on immutability, big on not using loops and instead using map/flatMap/filter/fold, I tend to roll my own either and option implementations when they don't exist because it's the tidiest way of handling errors I've come across, etc etc. But when it comes to stuff like functors I don't get what it's buying me. What interesting stuff can I do if I know that both an option and a list can be mapped over?
I really need to look more deeply into it at some stage. I might be missing out on some powerful tools. Or it might be a bunch of stuff that's theoretically interesting but practically useless.
This gets handle by the Cats library in Scala (https://typelevel.org/cats/typeclasses/functor.html) by defining the type class the functor is being defined for as an abstract type on the functor itself.
It's very important for the type of `fmap f x` to be identical to `x` except in that its "inner" type has been modified. Without that, these kinds of interfaces lose all of their value.
The interface only requires you to implement the mapping between morphisms, but a mathematical functor also includes mappings between the objects from one category to the next.
Additionally fmap is actually mapping
(a -> b) to (f a -> f b)
but the way it's typically used in haskell breaks this intuition. Most people see it as ((a -> b), f a) to (f b)
as a result of how it's used in FP. Technically the usages are isomorphic but using it this way blocks your intuition from fully understanding the true nature of a functor.The following would be more a more accurate type class for a functor:
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
tmap :: a -> f a
See here:https://en.wikipedia.org/wiki/Functor
Note that there are TWO axioms for functor, I added the second axiom to complete the definition.
I think the reason why tmap doesn't exist is because it's trivial? Not sure maybe some expert can pitch in as I'm certainly not an expert in haskell.
The typeclass you created would be a functor along with a natural transformation from the identity functor to f. I don't think these always exist, but they do for applicative functors (pure/return).
The object mapping is reflected at the type level. "f a" is the object that "f" sends "a" to.
In Haskell, functors are all endofunctors. In math, functors can be between categories, and in such a case a -> f a might not make any sense because there are no morphisms between categories.
Using some quasi-Agda syntax, here would be a full definition of a functor between two categories. Maybe you could overload "->" so that "X -> Y" could mean "Mor X Y" for the set of morphisms between X and Y in C or D (depending on context). Curly brackets mean optional parameters, which Haskell approximates via typeclasses.
record Functor where
field
C, D : Cat
F : Ob C -> Ob D
fmap : {X Y : Ob C} -> Mor{C} X Y -> Mor{D} (F X) (F Y)
fmapId : {X : Ob C} -> fmap id == id
fmapComp : {X Y Z : Ob C} -> {f : Mor X Y} -> {g : Mor Y Z}
-> fmap g . fmap f == fmap (g . f)In CAT the category of small categories objects are categories and morphisms are functors. Haskell types can be thought of as a small categories so I don't think this statement is correct? I'm no expert but can you clarify, the definition on wikipedia says that functors are morphisms between categories in CAT.
>There might not be a corresponding morphism between "a" and "f a".
Hmm. if haskell types make up a small category that means 'a' and 'f a' are sets. I can't imagine a case where two sets cannot have a mapping between them. Can you give a specific example?
>The typeclass you created would be a functor along with a natural transformation from the identity functor to f.
I'm confused about this. Isn't the natural transformation from identity to another category (at least for small categories) equivalent to a functor? In my head they look identical.
https://en.wikipedia.org/wiki/Natural_transformation
If you look at the commutative diagram on this page and replace F(X) and G(X) with identity X and G the natural transformation diagram looks just like a diagram for a functor.
btw I'm not an expert in haskell (let alone agda) or category theory so forgive me if the stuff I talk about is totally off.
It is true that functors are morphisms between categories, but that's a morphism in CAT.
> I'm confused about this.
That's how these things go :-) Anyway, natural transformations are between two functors C -> D. This is an example of a 2-morphism in a 2-category. It's hard to come up with something to say other than they're just different, but think about this: a natural transformation is a consistent choice of morphisms F X -> G X (in D) for each object X (in C), but a functor needs to know where the morphisms of C go, too.
(An intuition is a natural transformations is kind of like a continuous path transforming one functor into another. This might just make things confusing, though.)
This is irrelevant. The question isn't, does there exist a function, it's does the functor determine one. (Given any sets A and B there is always a function from A to B unless B is empty and A is nonempty. But that's not very helpful, is it?)
> I'm confused about this. Isn't the natural transformation from identity to another category (at least for small categories) equivalent to a functor?
This doesn't mean anything. A natural transformation is from one functor F:C->D to another functor G:C->D. Not from a functor to... a category? Huh?
Also smallness is irrelevant to all of this.
This is incorrect in a bunch of ways. First, Haskell types aren't truly just sets. As a simple, practical example you might have
module Container (makeInt, Container, getValue) where
data Container a
= Container { getValue :: a }
deriving Functor
makeInt :: Int -> Container
makeInt = Container
And now we've got (externally) a type which is equivalent to the identity functor in structure, but has a weird interface which disallows tmap.More powerfully, we're only actually interested in discussing an interface in and of itself. A type may instantiate many interfaces of varying levels of power, but each interface needs to be well-defined and well-behaved on its own. Then their compositions need to be "glued together" properly.
So even if all Haskell Functors were actually TMapFunctors, it's still important to note that the interface TMapFunctor is a sub interface to Functor which allows more operations and has more laws.
An even stronger example of this is the fact that due to the way Haskell arrows work, all Haskell Functors are actually "strong" functors in the sense that we cannot even truly specify a non-string functor.
Strength is a way that fmap and products (or, really, category tensors) interact.
strength :: Functor f => (a, f b) -> f (a, b)
strength (a, fb) = fmap (\b -> (a, b)) fb
This seems completely obviously possible, but it's only because we can crack open products in building general anonymous arrows. That's not supported in every category and a generic functor should not be expected to satisfy it. Const q a = Const q
fmap _ (Const q) = Const q
is a perfectly viable Functor on a. Good luck converting a a into one of those.The answer is it does. In this context, the types are the objects. So if F is your functor, then F itself -- which does not have a type, but has kind * -> * -- is the mapping on the objects.
I think I get it. Thanks.
The notation "a -> f a" means a function that sends elements of 'a' to elements of 'f a', where 'a' is an object of the category Hask. (Functors don't look into the objects at the element level.)