[Haskell-beginners] Fwd: Type constraints question
drg at david.gordon.name
Fri Dec 18 18:55:34 EST 2009
Apologies for the spam, I had some difficulty getting my email address set
up right on the list and wanted to make sure this finally got through (or
perhaps there are like 5 copies on the list now... sorry.)
---------- Forwarded message ----------
Ok, now I will try asking the right question (previous try wasn't actually
an example of the problem I'm having)
Here's the code:
data Test = Test
data Test2 = Test2
class MyClass a where
getChild :: MyClass b => a -> b
instance MyClass Test where
getChild a = Test2
instance MyClass Test2 where
getChild a = Test
On HUGS I get:
ERROR line 9 - Inferred type is not general enough
*** Expression : getChild
*** Expected type : (MyClass Test, MyClass a) => Test -> a
*** Inferred type : (MyClass Test, MyClass Test2) => Test -> Test2
So, what's the problem with always returning a particular instance of
MyClass? I just want to constrain it to be an instance of MyClass, nothing
2009/12/18 David Gordon <david.robert.gordon at googlemail.com>
> Hi Folks,
> Total newbie here. I don't know if I am having a syntactic problem or a
> This code:
> data Test = Test
> data Test2 = Test2
> class MyClass a where
> getChild :: (MyClass b) => a -> b
> instance MyClass Test where
> getChild = Test2
> instance MyClass Test2 where
> getChild = Test
> results in:
> [1 of 1] Compiling Main ( test.hs, interpreted )
> Couldn't match expected type `Test -> b'
> against inferred type `Test2'
> In the expression: Test2
> In the definition of `getChild': getChild = Test2
> In the instance declaration for `MyClass Test'
> Couldn't match expected type `Test2 -> b'
> against inferred type `Test'
> In the expression: Test
> In the definition of `getChild': getChild = Test
> In the instance declaration for `MyClass Test2'
> Failed, modules loaded: none.
> Is this a reasonable thing to try and do in Haskell? If not, I have a lot
more questions... ;)
> many thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners