Btw, a somewhat related approach I tuned heavily (but relies on 'Bits'
rather than 'Bounded')
http://hackage.haskell.org/package/int-cast-0.1.1.0/docs/Data-IntCast.html#v:intCastMaybe
I'm a worried about the implementation requiring all those RULEs defined
in order to get GHC to optimize it away. That looks like the wrong way
to approach this, as by that you get something of an O(n^2) complexity
when you need to add new integral types to the RULE set to account for
all the potential direct conversion-paths between each pair.
Moreover, you limit yourself to 'Bounded' types (which is why I resorted
`Bits` rather than 'Bounded' for 'intCastMaybe' after noticing that I
couldn't convert from (half)unbounded 'Integer' or 'Natural' to/from
bounded types).
So I'm on the fence about 'toBoundedIntegral', as for one I want such a
facility for conversion, but OTOH, I'm not happy with the limitations
'toBoundedIntegral' exhibits. I'd like to see a more general solution.
PS: While working on the `int-cast` package (as well as experimenting
with type-level int literal-indexed integers[1]), I couldn't help but
wonder if we benefit from some GHC-provided facility for reflecting
integral boundary information at the type-level. One of the things I was
wishing for was for Haskell to support arbitrary statically-typed
(contiguous) sub-ranges of 'Integer' where GHC could then pick the most
efficient underlying type.
[1]: https://github.com/hvr/fixed-width-integers/blob/master/Data/Int/Fixed.hs