Reputation: 1466
I have the following piece of code that seems to consistently hang when running with after compiling with GHC (although no build failures with -Werror
).
import Data.Aeson
import Data.Scientific
import qualified Data.HashMap.Strict as S
myObj = Object $
S.fromList [("bla", Number $ pc * 100.0)]
where pc = 10 / 9
And when trying to access myObj
the program will hang. After some digging it seems like haskell has a tough time with the number conversion (although no warnings or errors with the above snippet). If I change the 9
above to a 10
, it doesn't hang. But I'm curious, why does the above hang?
Upvotes: 1
Views: 82
Reputation: 14329
The conversion of 10 % 9
(a Rational) to Scientific is what does not terminate.
10 / 9 :: Scientific
From the documentation of Data.Scientific:
WARNING: Although Scientific is an instance of Fractional, the methods are only partially defined! Specifically recip and / will diverge (i.e. loop and consume all space) when their outputs have an infinite decimal expansion. fromRational will diverge when the input Rational has an infinite decimal expansion. Consider using fromRationalRepetend for these rationals which will detect the repetition and indicate where it starts.
Therefore, try this instead:
let Right (x, _) = fromRationalRepetend Nothing (10 / 9) in x
You will have to decide what measures are appropriate. I decided here to ignore the possibility of Left
.
Upvotes: 5