Why does multiplication only short circuit on one side

后端 未结 3 522
独厮守ぢ
独厮守ぢ 2021-02-03 17:39

I was messing around with fix and after messing around with it I came across some weird behavior, namely that 0 * undefined is *** Exception: Pre

3条回答
  •  有刺的猬
    2021-02-03 18:05

    Your reasoning is correct. There is an unamb package providing tools for the sort of parallel computation you refer to. In fact, it offers Data.Unamb.pmult, which tries, in parallel, to check whether each operand is 1 or 0, and if so immediately produces a result. This parallel approach is likely to be much slower in most cases for simple arithmetic!

    The short-circuiting of (*) occurs only in GHC version 7.10. It came about as a result of changes to the implementation of the Integer type in that GHC version. That extra laziness was generally seen as a performance bug (as it interferes with strictness analysis and can even lead to space leaks in theory), so it will be removed in GHC 8.0.

提交回复
热议问题