You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Often, the stock-derived Ord instance of a data type has no semantic meaning (i.e. it is just used to put them into Sets/Maps). In particular, one usually does not spend much thought on the order of constructors, even though it influences the derived Ord instance.
Hence, when the Ord instance does carry important semantic meaning (see IntersectMBO/ouroboros-network#3856 (comment) for an example), it would be nice to have a way to be very explicit about the ordering (that goes futher than a warning in the comments); in order to nudge someone modifying the data type in the future towards thinking about the implications for the Ord instance.
Ideally, this abstraction
statically guarantees lawful instances,
and has performance equal to handwritten code.
One possible approach might look like this: Instead of stock-deriving Ord/Enum/Bounded for a type Foo, the user implements
For lawfulness, it additionally requires a test that fromEnum_ is injective.
Note that this construction could also be split into two independent but composable parts (deriving fromEnum from toEnum or vice versa, and deriving Ord via fromEnum).
Other options include using a symmetric ToEnum-based approach or ordinary TH.
The text was updated successfully, but these errors were encountered:
Often, the
stock
-derivedOrd
instance of a data type has no semantic meaning (i.e. it is just used to put them intoSet
s/Map
s). In particular, one usually does not spend much thought on the order of constructors, even though it influences the derivedOrd
instance.Hence, when the
Ord
instance does carry important semantic meaning (see IntersectMBO/ouroboros-network#3856 (comment) for an example), it would be nice to have a way to be very explicit about the ordering (that goes futher than a warning in the comments); in order to nudge someone modifying the data type in the future towards thinking about the implications for theOrd
instance.Ideally, this abstraction
One possible approach might look like this: Instead of
stock
-derivingOrd
/Enum
/Bounded
for a typeFoo
, the user implementsfor it, and uses the newtype
OrdFromEnum
(probably using
GHC.Generic
s and anArray
fortoEnum
for implementation) to allow users to write code such asFor lawfulness, it additionally requires a test that
fromEnum_
is injective.Note that this construction could also be split into two independent but composable parts (deriving
fromEnum
fromtoEnum
or vice versa, and derivingOrd
viafromEnum
).Other options include using a symmetric
ToEnum
-based approach or ordinary TH.The text was updated successfully, but these errors were encountered: