Need rewrite rules for fromIntegral
j6carey opened this issue · 1 comments
j6carey commented
It seems that newtypes such as Seconds
need rewrite rules in order for fromIntegral
to work efficiently. Perhaps something inspired by these general rules for Int64
from GHC.Int:
{-# RULES
"fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
"fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
#-}
Without such rules, we temporarily create an indefinite-precision Integer
:
module T where
import Data.Hourglass
import Data.Word
foo :: Word32 -> Seconds
foo = fromIntegral
$ ghc -O2 -ddump-simpl -dsuppress-all t.hs
[1 of 1] Compiling T ( t.hs, t.o )
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 12, types: 6, coercions: 2}
foo
foo =
\ ds_a1Ed ->
case ds_a1Ed of _ { W32# x#_a1Eg ->
case integerToInt (wordToInteger x#_a1Eg)
of wild1_a1TV { __DEFAULT ->
(I64# wild1_a1TV) `cast` ...
}
}
(Tested using hourglass-0.2.9
.)
vincenthz commented
Good idea. Any chance you can make that in a PR ?