Some time ago, I suggested a finer structure on the Ord class,
but at the time there was a concern that there were already too
many standard classes.  Here is the sort of thing I had in mind:


class  Preord a  where
    (<=), (>=)          :: a -> a -> Bool
    max, min            :: a -> a -> a

    (>=)                =  flip (<=)
    max x y | x >= y    =  x
            | y >= x    =  y
            |otherwise  =  error "max{PreludeCore}: no ordering relation"
    min x y | x <= y    =  x
            | y <= x    =  y
            |otherwise  =  error "min{PreludeCore}: no ordering relation"



class (Preord a, Eq a) => PartOrd a  where
    (<), (>)            :: a -> a -> Bool

    x <  y              =  x <= y && x /= y
    (>)                 =  flip (<)



Data Cmp                =  LT | EQ | GT

class  (PartOrd a) => Ord a  where
    cmp                 :: a -> a -> Cmp

    cmp x y | x == y    =  EQ
            | x <= y    =  LT
            | otherwise =  GT
    max x y | x >= y    =  x
            | otherwise =  y
    min x y | x <= y    =  x
            | otherwise =  y
            

(The above assumes another often discussed but not yet adopted feature:
overriding default methods in subclasses.)

I've also suggested we try something along the lines of the following:


class  Monoid a  where
    (+)                 :: a -> a -> a

class  (Monoid a) => Group a  where
    negate              :: a -> a
    (-)                 :: a -> a -> a

    x - y               =  x + negate y

class (Group a) => Ring a  where
    (*)                 :: a -> a -> a

class (Ring a) => Field a  where
    (/)                 :: a -> a -> a


(The numeric classes would be subclasses of these.)  Incidentally,
notice that we would want

        instance Monoid [a]  where
                (+)     =  (++)

Allowing the very useful

        double :: (Monoid a) => a -> a
        double x  =  x + x

which gives us both  double 2 ---> 4  and  double "Hi Mom! " --->
"Hi Mom! Hi Mom! ".    ;-)

Anyway, I see that these ideas have come up again.  Is there consensus
now as to whether the additional complexity in the standard classes
would be worthwhile?

--Joe

Joseph H. Fasel                         email:  [EMAIL PROTECTED]
Computer Research and Applications      phone:  +1 505 667 7158
University of California                fax:    +1 505 665 5220
Los Alamos National Laboratory          postal: C-3 MS B265
                                                Los Alamos, NM  87545

Reply via email to