Log in

View Full Version : Haskell: Data and Class problem



deathsandwich
08-19-2010, 08:00 PM
I am fairly new to Haskell and have been experimenting with data and class declarations. I have been trying to do this:


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
}

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.

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:


getMove :: (Elements b) => String -> b
getMove x = read x :: (Elements a) => a

This will not work either, as the function "read" complains of ambiguity in the letter a. I also tried this (amongst other attempts)


getMove :: (Elements b) => String -> b
getMove "Burn" = Burn
getMove "Ember" = Ember
getMove "Bubble" = Bubble
getMove "WaterGun" = WaterGun
getMove _ = error "Unknown move!"

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?

Also, I have noticed that 3 :: (Num a) => a will work but Burn :: (Elements a) => a causes an ambiguity error. Why is this the case? :confused:

Please help!

deathsandwich

djr33
08-20-2010, 05:20 AM
Haskell is not one of the more common languages here: you may find better help at a Haskell-centered website.
Of course you are welcome to wait to see if someone can help, but it may not happen. There's always a chance that a user here knows Haskell in addition to the other languages they use often or that someone else may find this and answer it (such as through a google search), but I can't say it's likely.

However, if you now or later have questions about any of the languages that are more popular here, we'd be happy to help.



Based on programming in general, it looks to me like the "ambiguity" is that the structure can be parsed two ways. Imagine that the # sign is an operation and I'll use x, y and z as statements:
x # y # z

Now, this can be processed in two ways:
x # (y # z)
Or
(x # y) # z

This may be the problem with your statement. I don't know if that works in Haskell, but that's my guess about an "ambiguity" error. Unfortunately, I wouldn't know where to start to fix it, unless of course you could add parentheses like in my example above. In many cases that won't work, though.

deathsandwich
08-20-2010, 03:40 PM
Thanks for your reply and trying to help!

I've posted the same problem to a more Haskell-centered community like you suggested, so I should find a solution soon.