I have a class as follows:
class Token a where
symbol :: a -> String
I also want all instances of Token to have a function convert which returns a parametrised type. The conversion alone works fine:
class Token a b where
convert :: a -> b
data Egal = One | Two
instance Token Egal Int where
convert One = 111
convert Two = 222
main = print $ show (convert One :: Int)
But when I try to use both symbol and convert I get errors about ambiguity. This is my code:
class Token a b where
convert :: a -> b
symbol :: a -> String
data Egal = One | Two
instance Token Egal Int where
convert One = 111
convert Two = 222
symbol One = "one"
symbol Two = "two"
main = print $ show (convert One :: Int)
What am I doing wrong?
EDIT: Reading my own question I started wondering: Should these be two distinct classes and my data Egal show instanciate both?
As you have defined things here, you can have instances with the same
abut conflictingbs. Like this:Now, should
symbol 'x'be"x"or"xxxx"? Both are possible depending which of the above instances gets chosen; it is ambiguous which instance should be used forsymbol, and therefore which answer you should get. There are various ways to fix this. One is to simply allow the ambiguity, and give yourself the ability to specify which instance to use at call sites. You can turn on theAllowAmbiguousTypesandTypeApplicationsextensions; then:But in many cases, you really want the compiler to check that you haven't made multiple instances with conflicting
as. Then you can use either theFunctionalDependenciesextension:or the
TypeFamiliesextension:They have more or less the same effect in most cases: conflicting instances are flagged, and there is no ambiguity left.