Hello, I m having some difficulties with the following thing: I've made a class which looks somehow like this: class A a where inc::a->a .... now, when I want to make a instance of it with type Int and define the function inc like this: instance A Int where inc a = a + 1 I can't make a call to it when I enter the folowing inc 5 (When I type inc 5::Int it does the job perfectly, because I explicitly say 5 is an Integer, but how can I tell the program 5 is an Int?????ithout saying it explicitly in the prompt) What s being the problem here?? Am I overlooking something here? If you can help me... don't hesitate to contact me Thx, a haskell-lover _________________________________________________________________ Verzend en ontvang Hotmail via je mobieltje: http://mobile.msn.com
tor 2002-05-23 klockan 15.38 skrev Jos Mistiaen:
Hello,
I m having some difficulties with the following thing:
I've made a class which looks somehow like this: class A a where inc::a->a ....
now, when I want to make a instance of it with type Int and define the function inc like this:
instance A Int where inc a = a + 1
I can't make a call to it when I enter the folowing
inc 5
(When I type inc 5::Int it does the job perfectly, because I explicitly say 5 is an Integer, but how can I tell the program 5 is an Int?????ithout saying it explicitly in the prompt)
Because 5 does not have type Int, it has type Num a => a which means that 5 can have any numeric type. Since + works for any numeric type, you can make your instance more general (so that it works for any numeric type, including Int) instance Num a => A a where inc a = a + 1 Regards, Martin -- Martin Norbäck d95mback@dtek.chalmers.se Kapplandsgatan 40 +46 (0)708 26 33 60 S-414 78 GÖTEBORG http://www.dtek.chalmers.se/~d95mback/ SWEDEN OpenPGP ID: 3FA8580B
Hello,
I m having some difficulties with the following thing:
I've made a class which looks somehow like this: class A a where inc::a->a ....
now, when I want to make a instance of it with type Int and define the function inc like this:
instance A Int where inc a = a + 1
I can't make a call to it when I enter the folowing
inc 5
(When I type inc 5::Int it does the job perfectly, because I explicitly say 5 is an Integer, but how can I tell the program 5 is an Int?????ithout saying it explicitly in the prompt)
What s being the problem here?? Am I overlooking something here? If you can help me... don't hesitate to contact me
Thx,
a haskell-lover
This appears to be a question based on the exam of our declarative languages course. Please consult Bart Demoen, Remko Troncon or me with your questions. Cheers, Tom
Jos Mistiaen wrote:
(When I type inc 5::Int it does the job perfectly, because I explicitly say 5 is an Integer, but how can I tell the program 5 is an Int?????ithout saying it explicitly in the prompt)
You HAVE to tell the program that 5 is an Int, because there might be other instances of your class, e.g.: class A a where inc::a->a instance A Int where inc a = a + 1 instance A Float where inc a = a - 1 Then you get: Main> inc 5 :: Int 6 (18 reductions, 21 cells) but: Main> inc 5 :: Float 4.0 (18 reductions, 26 cells) So clearly the result depends on the type, which therefor cannot be omitted. Regards, Janis. -- Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de
participants (4)
-
Janis Voigtlaender -
Jos Mistiaen -
Martin Norbäck -
Tom Schrijvers