naturalToWord vs 'clampNaturalToWord'

Keith keith.wygant at
Wed Sep 16 02:19:07 UTC 2020

The short of it is,

clampNaturalToWord :: Natural → Word
clampNaturalToWord n = case n of
   NatS# w -> W# w
   _ -> maxBound

results in much better space and time use than naturalToWord, which always takes the least significant Word of the Natural.

Because naturalToWord takes the least significant Word, you always have to check for size before conversion. Even though you do check, GHC has to assume that, after conversion, the big Natural could have resulted in a small Word and will uselessly duplicate all branches for both cases.

Is there anything wrong with clamping by default in this case?


Sent from my phone with K-9 Mail.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list