
Hi. I am using Hugs 98 I have that piece of code: class PlusTimes a where plus :: a -> a -> a instance PlusTimes Int where plus x y = x + y when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3 What am I doing wrong? Thanks. -- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

You did not specify what type of number it is -- in Haskell numeric constants like "1" are actually typed as forall a. Num a -- that is, can be any type of Num. Try: plus (2::Int) 3 -Ross On Nov 9, 2009, at 1:44 PM, Paul Tokarev wrote:
Hi.
I am using Hugs 98 I have that piece of code:
class PlusTimes a where plus :: a -> a -> a
instance PlusTimes Int where plus x y = x + y
when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3
What am I doing wrong? Thanks.
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com .
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks, that works. But how sould I change my class definition, so that it works without (2::Int), but just with 2? Ross Mellgren wrote:
You did not specify what type of number it is -- in Haskell numeric constants like "1" are actually typed as forall a. Num a -- that is, can be any type of Num.
Try: plus (2::Int) 3
-Ross
On Nov 9, 2009, at 1:44 PM, Paul Tokarev wrote:
Hi.
I am using Hugs 98 I have that piece of code:
class PlusTimes a where plus :: a -> a -> a
instance PlusTimes Int where plus x y = x + y
when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3
What am I doing wrong? Thanks.
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com .
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26274624.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

It's not your class definition that is the problem -- your class definition is good. The problem is that when you interact with it using the REPL, you don't specify any particular type you want so it's ambiguous. Usually this is not a problem in actual programs that you compile because there's enough context for the type inferencer to figure out what type 'a' should be. You can give it enough context in the REPL either by annotating the value or by using some other typed thing, e.g. let a = 1:: Int If you really want it to only work on Int, then you shouldn't use a typeclass, instead just write the function directly -- plus :: Int -> Int -> Int plus = (+) -Ross On Nov 9, 2009, at 5:09 PM, Paul Tokarev wrote:
Thanks, that works. But how sould I change my class definition, so that it works without (2::Int), but just with 2?
Ross Mellgren wrote:
You did not specify what type of number it is -- in Haskell numeric constants like "1" are actually typed as forall a. Num a -- that is, can be any type of Num.
Try: plus (2::Int) 3
-Ross
On Nov 9, 2009, at 1:44 PM, Paul Tokarev wrote:
Hi.
I am using Hugs 98 I have that piece of code:
class PlusTimes a where plus :: a -> a -> a
instance PlusTimes Int where plus x y = x + y
when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3
What am I doing wrong? Thanks.
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com .
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26274624.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com .
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

(+) is a name that is already taken by the Num typeclass, you're trying to overload it with a different class. It's equivalent to doing: foo :: Int foo = 1 foo :: String foo = "abc" this would cause an (obvious) namespace collision. If you want to redefine (+), you'll have to import a qualified prelude, by using the NoImplicitPrelude pragma and `import qualified Prelude`. A better alternative is to use a different name, `+`, `++` are both taken (for normal addition and list concatenation, resp.). /Joe On Nov 9, 2009, at 1:44 PM, Paul Tokarev wrote:
Hi.
I am using Hugs 98 I have that piece of code:
class PlusTimes a where plus :: a -> a -> a
instance PlusTimes Int where plus x y = x + y
when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3
What am I doing wrong? Thanks.
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Oh, crap, I'm sorry, I completely misread your post... Disregard my previous message. /Joe On Nov 9, 2009, at 1:44 PM, Paul Tokarev wrote:
Hi.
I am using Hugs 98 I have that piece of code:
class PlusTimes a where plus :: a -> a -> a
instance PlusTimes Int where plus x y = x + y
when I run : "plus 2 3" I get this error: ERROR - Unresolved overloading *** Type : (Num a, PlusTimes a) => a *** Expression : plus 2 3
What am I doing wrong? Thanks.
-- View this message in context: http://old.nabble.com/classes-question-tp26271257p26271257.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Joe Fredette
-
Paul Tokarev
-
Ross Mellgren