# unwanted Fractional constraint Classic List Threaded 3 messages Open this post in threaded view
|

## unwanted Fractional constraint

 For rational functions that take on integer values at integer arguments, for example n*(n+1)/2, is there a way to doctor the corresponding Haskell definition     f n = n*(n+1)/2 so that the type signature becomes     f :: Num a => a -> a rather than     f :: Fractional a => a -> a Doug McIlroy _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
 In reply to this post by M Douglas McIlroy [Attempting to resend]   Hi - I think your logic is:   I can define these two:   fFrac :: Fractional a => a -> a fFrac n = n * (n+1) / 2   fInt :: Integral a => a -> a fInt n = n * (n+1) `div` 2   so that, e.g.   fFrac (5.0 :: Float) == 15.0 :: Float   fInt (5 :: Integer) == 15 :: Integer   And all number types are either Integral or Fractional, so surely I should be able to define a single function of type:   f :: Num a => a -> a   This would seem reasonable, but I think there’s a problem with the last assumption. It is indeed possible for other types to be instances of Num, but not of Integral or Fractional.   For example, I could define:   instance Num Bool where   fromInteger 0 = False   fromInteger _ = True   (+) = (&&)   (*) = (||)   abs = id   signum _ = True   negate = not   Now this would probably be pretty dumb (and probably doesn’t comply with expectations), but is possible. (And also pretty dumb to define it without also define an instance Integral Bool where ..., but still possible).   So I don’t think   f :: Num a => a -> a   could be possible, since Num by itself (& the dumb Bool instance) has no way to do the division. (At least that I can think of, but would be very interested to hear if there is).   Regards, David. From: Beginners <[hidden email]> on behalf of M Douglas McIlroy <[hidden email]> Sent: Friday, December 11, 2020 1:03:03 PM To: [hidden email] <[hidden email]> Subject: [Haskell-beginners] unwanted Fractional constraint   For rational functions that take on integer values at integer arguments, for example n*(n+1)/2, is there a way to doctor the corresponding Haskell definition     f n = n*(n+1)/2 so that the type signature becomes     f :: Num a => a -> a rather than     f :: Fractional a => a -> a Doug McIlroy _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners