[Haskell-beginners] typeclass error
Amitava Shee
amitava.shee at gmail.com
Tue Apr 21 11:21:11 EDT 2009
Thank you.
I get the following error after making the suggested changes
-- foo.hs
{-# LANGUAGE FlexibleInstances #-}
module Foo where
class Foo a where
bar :: a -> a
--
-- instance Foo Int where
-- bar i = i + 10
--
-- instance Foo [Char] where
-- bar m = "foo" ++ m
--
instance Num a => Foo a where
bar x = x + 10
-----
foo.hs:14:0:
Constraint is no smaller than the instance head
in the constraint: Num a
(Use -XUndecidableInstances to permit this)
In the instance declaration for `Foo a'
Failed, modules loaded: none.
Prelude>
...Amitava
On Tue, Apr 21, 2009 at 9:42 AM, Joe Fredette <jfredett at gmail.com> wrote:
> If I'm not mistaken, changing:
>
> bar i = i + 10
>
> to
>
> bar i = i + 10::Int
>
> ought to fix it.
>
> The issue is that the compiler is being to general wrt the type of "10",
> it's inferring that it's just of type (Num a => a) -- the type of all
> numbers (sortof.)
>
> Alternately, you could implement this as:
>
> instance Num a => Foo a where
> bar x = x + 10
>
> which similarly ought to fix it. Do you understand why?
>
> Amitava Shee wrote:
>
>> I have the following code
>>
>> -- foo.hs
>> module Foo where
>>
>> class Foo a where
>> bar :: a -> a
>> instance Foo Int where
>> bar i = i + 10
>> ------------------
>>
>> It fails to compile withe following error
>> *Foo> bar 10
>>
>> <interactive>:1:4:
>> Ambiguous type variable `t' in the constraints:
>> `Num t' arising from the literal `10' at <interactive>:1:4-5
>> `Foo t' arising from a use of `bar' at <interactive>:1:0-5
>> Probable fix: add a type signature that fixes these type variable(s)
>> *Foo>
>>
>> - Thanks
>> Amitava Shee
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090421/29e865ab/attachment.htm
More information about the Beginners
mailing list