Enum and bounded instances for (,) and Either

Viktor Dukhovni ietf-dane at dukhovni.org
Wed May 12 21:15:01 UTC 2021


> On May 12, 2021, at 4:35 PM, chessai <chessai1996 at gmail.com> wrote:
> 
> Note that it is probably just missing some `error` calls in the right place.

Well, fromEnum for such instances is necessarily fragile, because:

  * The cardinality of the of the product set will often exceed
    the range of Int values, as e.g. with (Int, a) for any
    `a` with two or more elements.

  * The pair instance assumes that Bounded Enums have a minBound of zero,
    this is not true for e.g. Int8.  The indices of the *inner*, less
    significant tuple terms have to be shifted to start at zero (possibly
    even using a larger integral result type than Int).

	innerToEnum x = toEnum (x :: t) - toEnum (minBound @t).

Getting this right takes care, and it is IMHO likely to cause more issues
that it would solve.  Users who really want this can employ suitable
newtype wrappers.

-- 
	Viktor.


More information about the Libraries mailing list