Data.Poset
Description
Partially ordered data types. The standard Ord
class is for
total orders and therefore not suitable for floating point. However, we can
still define meaningful max
and sort
functions for these types.
We define our own Ord
class which is intended as a replacement for
Ord
. However, in order to take advantage of existing libraries
which use Ord
, we make every instance of Ord
an instance of
Ord
. This is done using the OverlappingInstances and
UndecidableInstances extensions -- it remains to be seen if problems occur
as a result of this.
Documentation
class Eq a => Poset a whereSource
Class for partially ordered data types. Instances should satisfy the following laws for all values a, b and c:
-
a <= a
. -
a <= b
andb <= a
impliesa == b
. -
a <= b
andb <= c
impliesa <= c
.
But note that the floating point instances don't satisfy the first rule.
class Poset a => Sortable a whereSource
Class for partially ordered data types where sorting makes sense. This includes all totally ordered sets and floating point types. Instances should satisfy the following laws:
- The set of elements for which
isOrdered
returns true is totally ordered. - The max (or min) of an insignificant element and a significant element is the significant one.
- The result of sorting a list should contain only significant elements.
-
max a b
=max b a
-
min a b
=min b a
The idea comes from floating point types, where non-comparable elements
(NaNs) are the exception rather than the rule. For these types, we can
define max
, min
and sortBy
to ignore insignificant elements. Thus, a
sort of floating point values will discard all NaNs and order the remaining
elements.
Minimal complete definition: isOrdered