<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi--<div><br></div><div>More silly typeclass questions. I'm not sure the right way to ask it, so I'll start with a failed code snippet:</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">data Foo a = Foo a <br><br>class TwoPi a where<br> div2pi :: (Floating b) => a -> b<br><br>instance (Floating a) => TwoPi (Foo a) where<br> div2pi (Foo a) = a / (2*pi)<br><br>instance TwoPi Float where<br> div2pi a = a / (2*pi)<br></font><br></div><div><br></div><div>This code is obviously meaningless, but I'm trying to figure out how you can create instances of a typeclass for data types of different kinds.</div><div><br></div><div>I have a similar piece of code that works:</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">data Foo a = Foo a <br><br><br>class Testable a where<br> isPos :: a -> Bool<br><br>instance (Ord b, Num b) => Testable (Foo b) where<br> isPos (Foo b) = b > 0<br><br>instance Testable Float where<br> isPos a = a > 0<br></font><br></div><div><br></div><div>One obvious difference is that the type of isPos is a -> Bool, with a defined type as the return. I'd rather not commit to a specific Floating type up front (I'd prefer sometimes Float sometimes Double, depending on the 'a' in Foo a, but trying to declare it as Float doesn't help me. This fails:</div><div><br></div><font class="Apple-style-span" face="'Courier New'">data Foo a = Foo a <br><br>class TwoPi a where<br> div2pi :: a -> Float<br><br>instance (Floating b) => TwoPi (Foo b) where<br> div2pi (Foo b) = b / (2*pi)<br><br>instance TwoPi Float where<br> div2pi a = a / (2*pi)<br></font><br><div><br></div><div>The errors I'm getting are various permutations of:</div><div><br></div><font class="Apple-style-span" face="'Courier New'"> Couldn't match expected type `Float' against inferred type `b'<br> `b' is a rigid type variable bound by<br> the instance declaration at gcbTestBad.hs:8:19<br> In the expression: b / (2 * pi)<br> In the definition of `div2pi': div2pi (Foo b) = b / (2 * pi)<br> In the instance declaration for `TwoPi (Foo b)'<br></font><br><div>What is the difference between these last two cases ("a -> Bool" and "a -> Float"), and is there anyway to make "a -> b" work? </div><div><br></div><div>Thanks--</div><div> Greg</div></body></html>