You can use a type checker to help you remove fromIntegral calls:
- Comment out your type signature for
header - Also comment out the definition of
main - Download the code in ghci
- Use
:t header to find out if GHC is suitable for a signature like header .
Doing this gives:
*Main> :t header header :: (Integral a1, Integral a2, RealFrac a) => a -> a2 -> a1 -> PutM ()
This suggests that we can remove fromIntegral by the rate and bps parameters, and indeed, this is the definition of the header typechecks:
header dur rate bps = do putLazyByteString $ BLC.pack ".snd" putWord32be 24 putWord32be $ floor $ bps * dur * rate putWord32be 3 putWord32be $ fromIntegral rate putWord32be 1
and type now:
*Main> :t header header :: (Integral a, RealFrac a) => a -> a -> a -> PutM ()
Note that we still have fromIntegral at rate , which we could eliminate using floor , for example:
putWord32be $ floor rate
which changes the header type to RealFrac a => a -> a -> a -> PutM () .
The main thing is to use a type checker to help you understand what the most common type signature can have a function.
Erikr source share