I want to write a function which operates on double and any other type of number supporting multiplication and addition, yielding double as a result.
The following, of course, doesn’t compile, since type of (*) is t -> t -> t, so mixing different types is not allowed:
f :: (Num a) => Double -> a -> a -> Double
f x a b = a*x + b
What I want is the ability to write something like this:
f :: ...
f x a b = ... -- equivalent to a*x + b
f 1.0 (2 :: Int) (3 :: Int) -- returns 5.0
f 1.0 (2 :: Word32) (3 :: Word32) -- returns 5.0
f 1.0 (2 :: Float) (3 :: Float) -- returns 5.0
What should I do to make it work? Or maybe I’m fundamentally wrong and shouldn’t be doing this? It is very strange but I didn’t find anything on the internet about this.
In RWH.chapter6 there is a nice paragraph about converting numbers between some numeric types (Table 6.4).
Seems workable.