Hacker News new | past | comments | ask | show | jobs | submit login

    It turns out that every generic type t has a corresponding map function map : ('a -> 'b) -> 'a t -> 'b t.
So how about x : 'w -> Bool?



If there is a function `map : ('a -> 'b) -> 'a t -> 'b t`, and there exists a function `x : 'w -> bool`, then `map x : `'w t -> bool t`. Is this what you were asking?


Think `data Foo a = Foo (a -> Bool)`, pardon my Haskell. A function `map :: (a -> b) -> Foo a -> Foo b` is impossible, however `contramap :: (a -> b) -> Foo b -> Foo a` is fine (just pre-compose the given function with the stored function).

Even worse with `data Bar a = Bar (a -> a)`.


Thanks. I know what contravariant functors are, but I haven't used them, so I didn't realize that was what the parent commenter was asking about.

You're right, my claim that every type 'a t has a corresponding (covariant) functor is incorrect, and I should either take that out or mention contravariant functors.


It’s even worse than that. Consider

    data Foo a = Foo (a -> a)
This admits neither Functor nor Contravariant. Sadly all you can say is “If you can map, it’s a functor.”

I always end up finding out more about any subject I actually publish a post about when people read it...


The other person also mentioned this. It's called invariance. There's also a fourth variance: https://www.benjamin.pizza/posts/2019-01-11-the-fourth-type-...


Thank you for pointing out contravariant functors; I have fixed that sentence in my post and credited you.


Oh that’s much more extensive. Like the link to Julie’s work.


I believe that you can say "As long as `a` only appears as the last parameter in a higher kinded type, or not as the parameter of any higher kinded type, then you can define a functor over it."




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: