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?
Token a b | a->b
(read up on functional dependencies). – Hileman