Re: Enum and bounded instances for (,) and Either

Viktor Dukhovni <[email protected]>
Newsgroups gmane.comp.lang.haskell.libraries
Message-ID <[email protected]>
> On May 12, 2021, at 4:35 PM, chessai <[email protected]> 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.
_______________________________________________
Libraries mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.