Results 1 to 3 of 3

Thread: Haskell: Data and Class problem

  1. #1
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Haskell: Data and Class problem

    I am fairly new to Haskell and have been experimenting with data and class declarations. I have been trying to do this:

    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
                               }
    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:

    Code:
    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)

    Code:
    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?

    Please help!

    deathsandwich
    Last edited by deathsandwich; 08-20-2010 at 08:39 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    deathsandwich (08-20-2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •