Skip to content

`toIntegralSized :: Integer -> Maybe Int` has suboptimal Core

integer_to_int :: Integer -> Maybe Int
integer_to_int = toIntegralSized

ghc-9.2.1-alpha1 -O2 -fforce-recomp -ddump-simpl -dsuppress-uniques -dsuppress-all produces the following Core:

-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
integer_to_int1 = 9223372036854775807

-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
integer_to_int2 = -9223372036854775808

-- RHS size: {terms: 22, types: 7, coercions: 0, joins: 0/0}
integer_to_int
  = \ x ->
      case integerLe# integer_to_int2 x of {
        __DEFAULT -> Nothing;
        1# ->
          case integerLe# x integer_to_int1 of {
            __DEFAULT -> Nothing;
            1# ->
              Just (case integerToInt# x of wild2 { __DEFAULT -> I# wild2 })
          }
      }

It would be nice if we could exploit the documented invariant that any Int-sized Integer will be represented with the IS constructor. The resulting Core should look roughly like this:

integer_to_int
  = \ x ->
      case x of {
        __DEFAULT -> Nothing;
        IS y -> Just (I# y)
      }

Could this be accomplished by adding a RULE?

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information