Haskell挂在数字转换上

Haskell hanging on number conversion

在使用 GHC 编译后 运行 时,我有以下代码似乎一直挂起(尽管使用 -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   

并且在尝试访问 myObj 时程序将挂起。经过一些挖掘后,似乎 haskell 在数字转换方面遇到了困难(尽管上述代码段没有警告或错误)。如果我将上面的 9 更改为 10,它不会挂起。但是我很好奇,上面为什么挂了?

10 % 9(理性)到科学的转换不会终止。

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.

因此,试试这个:

let Right (x, _) = fromRationalRepetend Nothing (10 / 9) in x

您必须决定采取何种措施是合适的。我在这里决定忽略 Left.

的可能性