<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Hello all!<br><br>I have been experimenting with data and class declarations and came across a problem. I have been trying to do this:<br>
<br>
<div style="margin: 5px 20px 20px;">
        <div class="smallfont" style="margin-bottom: 2px;">Code:</div>
        <pre class="alt2" dir="ltr" style="margin: 0px; padding: 6px; border: 1px inset; width: 750px; height: 194px; text-align: left; overflow: auto;">data Fire = Burn | Ember<br>data Water = Bubble | WaterGun<br><br>class Elements a<br><br>instance Elements Fire<br>instance Elements Water<br><br>data Elemental = Elemental { name :: String,<br>                             move :: (Elements a) =&gt; a<br>                           }</pre>
</div>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.<br>
<br>
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:<br>
<br>
<div style="margin: 5px 20px 20px;">
        <div class="smallfont" style="margin-bottom: 2px;">Code:</div>
        <pre class="alt2" dir="ltr" style="margin: 0px; padding: 6px; border: 1px inset; width: 750px; height: 50px; text-align: left; overflow: auto;">getMove :: (Elements b) =&gt; String -&gt; b<br>getMove x = read x :: (Elements a) =&gt; a</pre>
</div>This will not work either, as the function "read" complains of 
ambiguity in the letter a. I also tried this (amongst other attempts)<br>
<br>
<div style="margin: 5px 20px 20px;">
        <div class="smallfont" style="margin-bottom: 2px;">Code:</div>
        <pre class="alt2" dir="ltr" style="margin: 0px; padding: 6px; border: 1px inset; width: 750px; height: 114px; text-align: left; overflow: auto;">getMove :: (Elements b) =&gt; String -&gt; b<br>getMove "Burn" = Burn<br>getMove "Ember" = Ember<br>getMove "Bubble" = Bubble<br>getMove "WaterGun" = WaterGun<br>getMove _ = error "Unknown move!"</pre>
</div>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?<br>
<br>
Also, I have noticed that <code style="background-color: rgb(255, 255, 187);">3 :: (Num a) =&gt; a</code> will work but <code style="background-color: rgb(255, 255, 187);">Burn :: (Elements a) =&gt; a</code> causes an ambiguity error. Why is this the case? <img src="http://www.dynamicdrive.com/forums/images/smilies/confused.gif" alt="" title="Confused" class="inlineimg" border="0"><br>
<br>
Please help!<br><br>Mike<br>
<br>                                               </body>
</html>