I think I may have asked this on Haskell-Cafe at some point, but damned if I can find the answer now… So I’m asking it again here, so hopefully in future I can find the answer!
Haskell is fantastic at dealing with parametric polymorphism. But the trouble is that not everything is parametric. As a trivial example, suppose we want to fetch the first element of data out of a container. For a parametric type, that’s trivial:
class HasFirst c where first :: c x -> Maybe x instance HasFirst [] where first [] = Nothing first (x:_) = Just x
Now try and write an instance for ByteString. You can’t. Its type doesn’t mention the element type. You also cannot write an instance for Set, because it requires an Ord constraint – but the class head doesn’t mention the element type, so you cannot constrain it.
Associated types provide a neat way to completely fix these problems:
class HasFirst c where type Element c :: * first :: c -> Maybe (Element c) instance HasFirst [x] where type Element [x] = x first [] = Nothing first (x:_) = Just x instance HasFirst ByteString where type Element ByteString = Word8 first b = b ! 0 instance Ord x => HasFirst (Set x) where type Element (Set x) = x first s = findMin s
We now have a new problem, however. Consider trying to “fix” Functor so that it works for all container types:
class Functor f where type Element f :: * fmap :: (Functor f2) => (Element f -> Element f2) -> f -> f2
This doesn’t work at all. It says that if we have a function from the element type of f to the element type of f2, then we can turn an f into an f2. So far so good. However, there is apparently no way to demand that f and f2 are the same sort of container!
Under the existing Functor definition, we have
fmap :: (x -> y) -> [x] -> [y] fmap :: (x -> y) -> Seq x -> Seq y fmap :: (x -> y) -> IO x -> IO y
But we do not have fmap :: (x -> y) -> IO x -> [y]. That is quite impossible. But the class definition above allows it.
Does anybody know how to explain to the type system what I actually meant?
Edit
The above works by defining a way to compute an element type from a container type. What happens if you try to do it the other way around? Define a function to compute a container type from an element type? Does that work out any easier?
Well, the problem is that it’s not clear what the revised
Functoris supposed to mean. For instance, considerByteString. AByteStringcan only be mapped by replacing eachWord8element with an element of the same type. ButFunctoris for parametric mappable structures. There are really two conflicting notions of mapping here:So, in this case, you can’t explain to the type system what you meant, because it doesn’t make much sense. You can, however, change what you mean 🙂
Rigid mapping is easy to express with type families:
As far as parametric mapping, there are multiple ways to do it. The simplest way would be to retain the current
Functoras-is. Together, these classes covers structures likeByteString,[],Seq, and so on. However, they all fall down onSetandMap, because of theOrdconstraint on values. Happily, the constraint kinds extension coming in GHC 7.4 lets us fix this problem:Here, we’re saying that every instance should have an associated typeclass constraint. For instance, the Set instance will have
Element Set a = Ord a, to denote thatSets can only be constructed if anOrdinstance is available for the type. Anything that can appear on the left hand of=>is accepted. We can define our previous instances exactly as they were, but we can also doSetandMap:However, it’s pretty annoying to have to use two separate interfaces for rigid mapping and restricted parametric mapping. In fact, isn’t the latter a generalisation of the former? Consider the difference between
Set, which can only contain instances ofOrd, andByteString, which can only containWord8s. Surely we can express that as just another constraint?We apply the same trick done to
HasFirst(i.e. give instances for the whole structure and use a type family to expose the element type), and introduce a new associated constraint family:The idea here is that
Result f a rexpresses the constraints it needs on the value type (likeOrd a), and also constrains the resulting container type however it needs to; presumably, to ensure it has the type of a same-sort-of-container ofas. For instance,Result [a] b rwill presumably require thatris[b], andResult ByteString b rwill require thatbisWord8, andrisByteString.Type families already give us what we need to express “is” here: a type equality constraint. We can say
(a ~ b) => ...to require thataandbare the same type. We can, of course, use this in constraint family definitions. So, we have everything we need; on to the instances:Perfect! We can define instances for any type of container we want, rigid, parametric or parametric-but-restricted, and the types work out perfectly.
Disclaimer: I haven’t tried GHC 7.4 yet, so I don’t know if any of this actually compiles or works, but I think the basic ideas are sound.