I am sure others will give a better reply, but using RankNTypes you can type-check the first alternative you mentioned. Depending on what you are planning to do with the move field, it should be OK to use RankNTypes here, I guess.
HTH,
Ozgur
Hello all!
I have been experimenting with data and class declarations and came across a problem. I have been trying to do this:
So, the idea was that "move" in the data constructor "Elemental" would be able to take any value from either the type Fire or Water. However, the error message tells me this is an illegal polymorphic type.Code:data Fire = Burn | Ember
data Water = Bubble | WaterGun
class Elements a
instance Elements Fire
instance Elements Water
data Elemental = Elemental { name :: String,
move :: (Elements a) => a
}
Therefore, I tried creating a function that could read my value for me after "show" was applied to the move. Hence, the data declaration for Elemental could now assign "move" to a String. The function looked like this:
This will not work either, as the function "read" complains of ambiguity in the letter a. I also tried this (amongst other attempts)Code:getMove :: (Elements b) => String -> b
getMove x = read x :: (Elements a) => a
The above caused the function to infer the type Fire, and then complain about the type Water. So, how can I either create a function that can return multiple types like I am trying to above, or is there a way to adjust the data declaration for Elemental?Code:getMove :: (Elements b) => String -> b
getMove "Burn" = Burn
getMove "Ember" = Ember
getMove "Bubble" = Bubble
getMove "WaterGun" = WaterGun
getMove _ = error "Unknown move!"
Also, I have noticed that3 :: (Num a) => a
will work butBurn :: (Elements a) => a
causes an ambiguity error. Why is this the case?
Please help!
Mike
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners