File: Ord.hs

package info (click to toggle)
bali-phy 4.0~beta16%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,192 kB
  • sloc: cpp: 119,288; xml: 13,482; haskell: 9,722; python: 2,930; yacc: 1,329; perl: 1,169; lex: 904; sh: 343; makefile: 26
file content (74 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Ord (module Data.Eq,
                 Ordering (..),
                 Ord,
                (<),
                (<=),
                (>),
                (>=),
                compare,
                min,
                max)
where

import Data.Eq

data Ordering = EQ | LT | GT

instance Eq Ordering where
    EQ == EQ = True
    LT == LT = True
    GT == GT = True
    _  == _  = False

infix 4 <, <=, >, >=

class Eq a => Ord a where
    compare :: a -> a -> Ordering
    (<), (>), (>=), (<=) :: a -> a -> Bool
    min, max :: a -> a -> a

    min x y = if (x <= y) then x else y
    max x y = if (x >= y) then x else y

    compare x y | x <  y    = LT
                | x == y    = EQ
                | otherwise = GT

    x <  y = not (x >= y)
    x >  y = not (x <= y)
    x >= y = x > y || x == y
    x <= y = x < y || x == y

foreign import bpcall "Prelude:" lessthan_char :: Char -> Char -> Bool
foreign import bpcall "Prelude:" lessthan_int :: Int -> Int -> Bool
foreign import bpcall "Prelude:" lessthan_integer :: Integer -> Integer -> Bool
foreign import bpcall "Prelude:" lessthan_double :: Double -> Double -> Bool

instance Ord Char where
    (<) = lessthan_char

instance Ord Int where
    (<) = lessthan_int 

instance Ord Integer where
    (<) = lessthan_integer

instance Ord Double where
    (<) = lessthan_double

instance Ord a => Ord [a] where
    compare []     []      = EQ
    compare []     (_:_)   = LT
    compare (_:_)  []      = GT
    compare (x:xs) (y:ys)  = case compare x y of LT -> LT
                                                 GT -> GT
                                                 EQ -> compare xs ys
    x < y = compare x y == LT
    x > y = compare x y == GT

instance (Ord a, Ord b) => Ord (a,b) where
    compare (x1,y1) (x2,y2) = let c1 = compare x1 x2
                              in case c1 of
                                   EQ -> compare y1 y2
                                   _  -> c1