KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In my free time I'm learning Haskell, so this is a beginner question. In my readings I came across an example illustrating how Either a is made an instance of Functor : instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x Now, I'm trying to understand why the implementation maps in the case of a Right value constructor, but doesn't in the case of a Left ? Here is my understanding: First let me rewrite the above instance as instance Functor (Either a) where fmap g (Right x) = Right (g x) fmap g (Left x) = Left x Now: I know that fmap :: (c -> d) -> f c -> f d if we substitute f with Either a we get fmap :: (c -> d) -> Either a c -> Either a d the type of Right (g x) is Either a (g x) , and the type of g x is d , so we have that the type of Right (g x) is Either a d , which is what we expect from fmap (see 2. above) now, if we look at Left (g x) we can use the same reasoning to say that its type is Either (g x) b , that is Either d b , which is not what we expect from fmap (see 2. above): the d should be the second parameter, not the first! So we can't map over Left . Is my reasoning correct?
Tags (comma-separated)
Save Edits
Cancel