The numeric c types are (effectively) integral, too.

Jeff Shaw shawjef3 at gmail.com
Wed Mar 27 16:17:05 CET 2013


This query is regarding Foreign.C.Types 
<http://hackage.haskell.org/packages/archive/base/4.6.0.1/doc/html/Foreign-C-Types.html>.

Now that the constructors for CClock, CTime, CUSeconds, and CSUSeconds 
are exposed, it's become clear that these are Ints or Words. Technically 
on some architectures they could be floating point, but I would bet that 
on all architectures that Haskell is ever used on, none of these are 
floating point types. Adding Bounded, Enum, Integral, and Bits instances 
to the "numeric" types would break nothing and ease situations such as this:

roundTo :: CTime -> CTime -> CTime
roundTo a b = (a `div` b) * b

This works right now, but requires a trivial Integral class be written 
for every program that wants to do it.

instance Integral CTime where
   quot (CTime a) (CTime b) = CTime (a `quot` b)
   rem (CTime a) (CTime b) = CTime (a `rem` b)
   div (CTime a) (CTime b) = CTime (a `div` b)
   mod (CTime a) (CTime b) = CTime (a `mod` b)
   quotRem (CTime n) (CTime d) = (\(d,m) -> (CTime d, CTime m)) (quotRem 
n d)
   divMod (CTime n) (CTime d) = (\(d,m) -> (CTime d, CTime m)) (divMod n d)
   toInteger (CTime t) = toInteger t

I think this and the other trivial instances should just go right into 
Foreign.C.Types for all the "numeric" types in Foreign.C.Types. Any 
thoughts to the contrary?

Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20130327/b902af5d/attachment.htm>


More information about the Libraries mailing list