Learn You a Haskell has an example about functors. I can read LYAH, and text, and figure out what is supposed to happen — but I don’t know enough to write something like this. I’m finding this problem often in Haskell.
instance Functor (Either a) where
fmap f (Right x) = Right (f x)
fmap f (Left x) = Left x
However, I’m confused.. Why doesn’t this comple
instance Functor (Either a) where
fmap f (Right x) = Right (x)
fmap f (Left x) = Left (f x)
If f isn’t being used in the top definition, then what else constrains x such that it can’t satisfy Left
Here’s the functor class:
Note that “f” by itself is a type constructor because it’s applied to a type variable in the fmap line. Here are some examples to make this clear:
Type constructors:
Types:
“Maybe a” is a type with one type constructor (the “Maybe”) and one type variable (the “a”). It’s not something concrete yet, but it is usable in type signatures for polymorphic functions.
“Either” is a type constructor that takes two type arguments, so even after you apply one (e.g.
Either Stringit’s still a type constructor because it can take another type argument.The point of this is: when you define a
Functorinstance, the type constructorfcannot change. This is because it’s represented by the same variable,f, as both the argument and result offmap. The only type that’s allowed to change is the type that’s applied to thefconstructor.When you write
instance Functor (Either c),Either cis filled in forfeverywhere in the declaration offmap. This gives fmap the following type for this instance:With the definition of
Either, the only useful way to get this type is by applying theRightvalue to the function. Remember that “Either” has two possible values with possibly different types. Here theLeftvalue has type ‘c’, so you can’t apply it to the function (which expects an ‘a’)[1], and the result also wouldn’t be correct because you’d be left withEither b a, which doesn’t match the class definition.After replacing “f” with “Either c” to get the above type signature for fmap with the “Either c” instance, writing the implementation is next. There are two cases to consider, the Left and the Right. The type signature tells us that the type of the Left side, “c”, can’t change. We also don’t have any way to change the value because we don’t know what type it actually is. All we can do is leave it alone:
For the Right side, the type signature says that we have to change from a value with type “a” to a value with type “b”. The first argument is a function to do exactly that, so we use the function with the input value to get the new output. Putting the two together gives the full definition
There’s a more general principle at work here which is why writing a Functor instance that adjusts the Left side is impossible, at least with the Prelude definitions. Copying some code from above:
Even though we have a type variable ‘c’ in the instance definition, we can’t use it in any of the class methods because it’s not mentioned in the class definition. So you can’t write
The result of leftMap, and thus fmap, is now
(Either d) a. The(Either c)has changed to an(Either d), but this isn’t allowed because there’s no way to express it in the Functor class. To express this, you’d need a class with two type variables, e.g.In this class, since both the left and right type variables are in scope, it’s possible to write methods that operate on either (or both) sides.
Although in practice people usually just write “biMap” for the BiFunctor class and use “id” for the other function if a left or right mapping is necessary.
[1] More accurately, the Left value has type ‘c’, the function expects an ‘a’, but the type checker can’t unify those types because the ‘c’ type isn’t in scope in the class definition.