Given the following class:
class ListIsomorphic l where
toList :: l a -> [a]
fromList :: [a] -> l a
How can I write a instance for vector types using Data.Vector.Generic? This doesn't work:
instance (V.Vector v a) => ListIsomorphic v where
toList = V.toList
fromList = V.fromList
Giving me:
test.hs:31:10:
Variable ‘a’ occurs more often than in the instance head
in the constraint: V.Vector v a
(Use UndecidableInstances to permit this)
In the instance declaration for ‘ListIsomorphic v’
Don't. Adding an instance for all
vto yourListableclass will become cumbersome to use due to overlapping instances.A
Vector v a => visn't isomorphic to a list because it is constrained by which items can be elements of the list. You'd need a class that captures this constraint, something likeInstead of adding
ConstrainedListinstances for all typesVector v a => vwhich would get us into overlapping instances territory, instead we'll define it only for the types we're interested in. The following will cover all the types with aVectorinstance in the vector package.Instances for other types
You can write a
ConstrainedListinstance for regular lists[]that requires only the empty constraint for its elements.Anywhere that uses
toListorfromListwill also require anElem l ainstance.When we know concrete types for the lists and elements these functions will be easy to use without messing around with constraints.
Here Be Dragons
Don't try what follows. If you are interested in the class of things that are isomorphic to lists without additional constraints, just make another class for it. This just demonstrates what you can do when you've designed yourself into a corner: summon a dragon.
You can also write functions that require a proof that there is no constraint on the elements of a
ConstrainedList. This is way off into the realms of theconstraintspackage and programming styles that aren't really supported by GHC, but there aren't enoughconstraintsexamples so I'll leave this one here.We could check that a
ConstrainedListhas no constraint by just checking thatElem l a ~ (), but that wouldn't work if its constraint was written in a different way.()isn't the same type asAny aeven though()impliesAny a. The constraints package captures relationships like this by reifying them to the type classesClassand:=>All of that work lets us easily reuse functions without providing all those dictionaries when a concrete list type is known.