Data.Ratio and exceptions
Kazu Yamamoto ( 山本和彦 )
kazu at iij.ad.jp
Tue Jun 5 11:47:54 CEST 2012
Thank you for reply.
> It is intended that you do not divide by zero, also because it is so
> simple to check it before calling the function.
I'm not convinced. For instance, for division by zero of Int, it is
also easy to check it before calling the "div" function. But we have
> The best solution
> would certainly be a number type that excludes zero values, such that
> division by zero can be catched at compile time. But currently
> Haskell's type system may not be expressive enough to handle this
> without a lot of explicit type conversion.
I'm not suggesting such a drastic change. What I want to suggest is to
define a new data constructor for ArithException and to change % so
that it throws the new data constructor.
In test code, we want to check the exception of Data.Ratio with
hspec-shouldbe as follows:
evaluate (1 `div'` 0) `shouldThrow` anyArithException
More information about the Libraries