Add Ord Laws to next Haskell Report
Herbert Valerio Riedel
hvriedel at gmail.com
Wed Feb 6 20:52:44 UTC 2019
On Wed, Feb 6, 2019 at 9:43 PM chessai . <chessai1996 at gmail.com> wrote:
> Per GHC.Classes (haddock-viewable from Data.Ord)
>
> "The Haskell Report defines no laws for Ord. However, <= is
> customarily expected to implement a non-strict partial order and have
> the following properties:"
>
> I propose that in the next report that the expected typeclass laws for
> Ord be added. They're generally agreed upon/understood.
>
Can you spell out the concrete change to the report wording you're
suggesting? For reference, the current wording used in the 2010 Haskell
Report is quoted below. While at it, you might also want to take into
account the `Eq` class definition in the report.
6.3.2 The Ord Class
class (Eq a) => Ord a where
compare :: a -> a -> Ordering
(<), (<=), (>=), (>) :: a -> a -> Bool
max, min :: a -> a -> a
compare x y | x == y = EQ
| x <= y = LT
| otherwise = GT
x <= y = compare x y /= GT
x < y = compare x y == LT
x >= y = compare x y /= LT
x > y = compare x y == GT
-- Note that (min x y, max x y) = (x,y) or (y,x)
max x y | x <= y = y
| otherwise = x
min x y | x <= y = x
| otherwise = y
The Ord class is used for totally ordered datatypes. All basic datatypes
except for functions, IO, and IOError, are instances of this class.
Instances of Ord can be derived for any user-defined datatype whose
constituent types are in Ord. The declared order of the constructors in the
data declaration determines the ordering in derived Ord instances. The
Ordering datatype allows a single comparison to determine the precise
ordering of two objects.
The default declarations allow a user to create an Ord instance either with
a type-specific compare function or with type-specific == and <= functions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190206/442426f2/attachment.html>
More information about the Libraries
mailing list