[Haskell-beginners] Netwire fromRational
Nathan Hüsken
nathan.huesken at posteo.de
Wed Oct 3 23:13:38 CEST 2012
On 10/03/2012 10:18 PM, Chaddaï Fouché wrote:
> 1.0 _is_ a Rational in the first place, it's a number literal that is
> not integral, so it is translated to a fraction (Rational) by the
> parser then converted at runtime (only once so no worry about
> efficiency) to the proper type in the Fractional type class by
> fromRational, like Double, Float, Ratio, ... Here it's a WireP ()
> Double.
>
> This is how this code works :
>
>> let
>> x :: (Fractional a) => a
>> x = 1.23
>> y = (3.5 :: Double) + x
>> z = (2.7 :: Float) + x
>
That is good to know. I was unaware that 1.0 is parsed as a rational.
Thanks for the explanation.
> Note : Does GHC optimize the case were the final type is monomorphic
> and standard (Double, Float) if -O2 is given ? Sure one fromRational
> by literal shouldn't hurt much but...
I could argue that it also increased the size of the compiled
executable, but I am guessing it is of little magnitude.
More information about the Beginners
mailing list