Mixture of Integer and Float arithmetic without "fromIntegral"?

When defining expressions with arithmetic operations on a mixture of Integers and Floats (except literals), an explicit conversion by "fromIntegral" for the Integers is necessary, for example: ceiling (fromIntegral n / x) Is it possible to do the "fromInteger" conversion automatically? What's the best way to do it? (overloading of operators, etc.?) Christian

Christian Lescher writes:
When defining expressions with arithmetic operations on a mixture of Integers and Floats (except literals), an explicit conversion by "fromIntegral" for the Integers is necessary, for example: ceiling (fromIntegral n / x)
Is it possible to do the "fromInteger" conversion automatically? What's the best way to do it? (overloading of operators, etc.?)
Hi. One complication is that some Integer values exceed the greatest Double value. In such cases, it's not clear which implementation of (/) should be used. It would certainly be nice to have an automatic conversion if, for example, n were an Int and x were a Float, because those types are both subsumed by Double. Regards, Tom

One complication is that some Integer values exceed the greatest Double value. In such cases, it's not clear which implementation of (/) should be used.
It would certainly be nice to have an automatic conversion if, for example, n were an Int and x were a Float, because those types are both subsumed by Double.
Let's say we restrict it to the types Int and Double. Is an automatic conversion at least in this case possible? Christian

Christian Lescher writes:
[...]
Let's say we restrict it to the types Int and Double. Is an automatic conversion at least in this case possible?
You could try wheeling out Haskell's Least Used Keyword: module Defs where default(Double, Int) n = 3 -- This is defaulted to Double x = 12.3 test = ceiling (x / n) Other than that, I only know an answer that starts with "Design and implement a language extension for ...", sorry.

You could try wheeling out Haskell's Least Used Keyword:
module Defs where
default(Double, Int)
n = 3 -- This is defaulted to Double x = 12.3
test = ceiling (x / n)
Other than that, I only know an answer that starts with "Design and implement a language extension for ...", sorry.
Never mind - thank you for the hint concerning the "default" declaration; I didn't know about that keyword yet. When writing the module as follows, no "fromIntegral" is necessary: module Defs where n :: Num a => a n = 3 x :: Fractional a => a x = 12.3 test = ceiling (x / n) However, I would like to have it without writing the type signature. Who knows a way to do this? Christian

Wed, 06 Dec 2000 11:45:43 +0100, Christian Lescher
module Defs where
n :: Num a => a n = 3
x :: Fractional a => a x = 12.3
test = ceiling (x / n)
However, I would like to have it without writing the type signature.
There are people (including me) who think that the monomorphism restriction rule, which makes the above definitions without type signatures monomorphic and thus defaulted to concrete types, should be removed. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK

module Defs where
n = 3 x = 12.3
test = ceiling (x / n)
There are people (including me) who think that the monomorphism restriction rule, which makes the above definitions without type signatures monomorphic and thus defaulted to concrete types, should be removed.
If you want to experiment with an implementation that do not enforce the monomorphism restriction, try nhc98. It happily infers the types n :: Num a => a x :: Fractional a => a for the toplevel definitions shown. I don't think anyone has ever complained about this feature. Regards, Malcolm
participants (4)
-
Christian Lescher
-
Malcolm Wallace
-
qrczak@knm.org.pl
-
Tom Pledger