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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090421/e448181f/attachment.htm |
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 > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > A non-text attachment was scrubbed... Name: jfredett.vcf Type: text/x-vcard Size: 296 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20090421/be3b36aa/jfredett.vcf |
In reply to this post by Amitava Shee
On Tue, 21 Apr 2009 09:27:57 -0400
Amitava Shee <[hidden email]> 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> > When you type an expression into GHCi, it needs to be able to assign it a monomorphic type (a type without variables) in order to know which "show" to use on it to print it. From the types of bar (Foo a => a -> a) and 10 (Num a => a), GHCi can only deduce that bar 10 should have type ((Foo a, Num a) => a). It's also not smart enough to notice that there's only one type that's an instance of both Foo and Bar, and use it. Your declarations are perfectly fine, but there's not enough information at the call site of bar to pin it down to a particular type. It can be solved simply by adding a type signature there (bar 10 :: Int). Note also that, since numeric code is common and uses a lot of type classes, Haskell has ad-hoc defaulting rules for it: When there's an ambiguity on the type of a variable and at least one of its class constraints is numeric (e.g. Num or Integral), the compiler will try to use a default type. Unfortunately the default Num type is Integer which isn't an instance of Foo, so the defaulting rules don't apply in your case. |
In reply to this post by jfredett
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 <[hidden email]> 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 >> [hidden email] >> http://www.haskell.org/mailman/listinfo/beginners >> >> > An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090421/29e865ab/attachment.htm |
Right, I was incorrect, you should use the solution that Quentin gave-
to recap it, The basic problem is that GHCi is trying to infer a single type -- called "monomorphic" -- for your more general type. the type of `bar` is `Foo a => a -> a`, when ghci looks at a literal number (like 10) it interprets it as a "default" type of Integer (not int). Since Integer does not instance Foo, then GHCi yells at you. Quentin explained it far better than I, I refer you to his email. /Joe Amitava Shee wrote: > 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 <[hidden email] > <mailto:[hidden email]>> 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 > [hidden email] <mailto:[hidden email]> > http://www.haskell.org/mailman/listinfo/beginners > > > A non-text attachment was scrubbed... Name: jfredett.vcf Type: text/x-vcard Size: 296 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20090421/b856729c/jfredett-0001.vcf |
Free forum by Nabble | Edit this page |