{-# LANGUAGE CPP
, FlexibleInstances
, UndecidableInstances
#-}
#if __GLASGOW_HASKELL__ < 710
{-# LANGUAGE OverlappingInstances #-}
#endif
{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
module Data.Number.PartialOrd
(
PartialOrd(..)
, comparingPO
) where
import Prelude hiding (isNaN)
import Hugs.RealFloat (isNaN)
class PartialOrd a where
cmp :: a -> a -> Maybe Ordering
gt :: a -> a -> Maybe Bool
gt a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
GT -> forall a. a -> Maybe a
Just Bool
True
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
False
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
ge :: a -> a -> Maybe Bool
ge a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
LT -> forall a. a -> Maybe a
Just Bool
False
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
True
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
eq :: a -> a -> Maybe Bool
eq a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
EQ -> forall a. a -> Maybe a
Just Bool
True
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
False
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
ne :: a -> a -> Maybe Bool
ne a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
EQ -> forall a. a -> Maybe a
Just Bool
False
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
True
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
le :: a -> a -> Maybe Bool
le a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
GT -> forall a. a -> Maybe a
Just Bool
False
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
True
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
lt :: a -> a -> Maybe Bool
lt a
x a
y = case a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y of
Just Ordering
LT -> forall a. a -> Maybe a
Just Bool
True
Just Ordering
_ -> forall a. a -> Maybe a
Just Bool
False
Maybe Ordering
Nothing -> forall a. Maybe a
Nothing
maxPO :: a -> a -> Maybe a
maxPO a
x a
y = do Ordering
o <- a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y
case Ordering
o of
Ordering
GT -> forall a. a -> Maybe a
Just a
x
Ordering
EQ -> forall a. a -> Maybe a
Just a
x
Ordering
LT -> forall a. a -> Maybe a
Just a
y
minPO :: a -> a -> Maybe a
minPO a
x a
y = do Ordering
o <- a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a
y
case Ordering
o of
Ordering
GT -> forall a. a -> Maybe a
Just a
y
Ordering
EQ -> forall a. a -> Maybe a
Just a
x
Ordering
LT -> forall a. a -> Maybe a
Just a
x
infix 4 `gt`, `ge`, `eq`, `ne`, `le`, `lt`, `maxPO`, `minPO`
instance
#if __GLASGOW_HASKELL__ >= 710
{-# OVERLAPPABLE #-}
#endif
(Ord a) => PartialOrd a where
cmp :: a -> a -> Maybe Ordering
cmp a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> Ordering
`compare` a
y
gt :: a -> a -> Maybe Bool
gt a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> Bool
> a
y
ge :: a -> a -> Maybe Bool
ge a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> Bool
>= a
y
eq :: a -> a -> Maybe Bool
eq a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Eq a => a -> a -> Bool
== a
y
ne :: a -> a -> Maybe Bool
ne a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Eq a => a -> a -> Bool
/= a
y
le :: a -> a -> Maybe Bool
le a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> Bool
<= a
y
lt :: a -> a -> Maybe Bool
lt a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> Bool
< a
y
maxPO :: a -> a -> Maybe a
maxPO a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> a
`max` a
y
minPO :: a -> a -> Maybe a
minPO a
x a
y = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! a
x forall a. Ord a => a -> a -> a
`min` a
y
instance
#if __GLASGOW_HASKELL__ >= 710
{-# OVERLAPPING #-}
#endif
PartialOrd Float where
cmp :: Float -> Float -> Maybe Ordering
cmp Float
x Float
y | forall a. RealFloat a => a -> Bool
isNaN Float
x Bool -> Bool -> Bool
|| forall a. RealFloat a => a -> Bool
isNaN Float
y = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Float
x forall a. Ord a => a -> a -> Ordering
`compare` Float
y
instance
#if __GLASGOW_HASKELL__ >= 710
{-# OVERLAPPING #-}
#endif
PartialOrd Double where
cmp :: Double -> Double -> Maybe Ordering
cmp Double
x Double
y | forall a. RealFloat a => a -> Bool
isNaN Double
x Bool -> Bool -> Bool
|| forall a. RealFloat a => a -> Bool
isNaN Double
y = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Double
x forall a. Ord a => a -> a -> Ordering
`compare` Double
y
comparingPO :: (PartialOrd b) => (a -> b) -> a -> a -> Maybe Ordering
comparingPO :: forall b a. PartialOrd b => (a -> b) -> a -> a -> Maybe Ordering
comparingPO a -> b
p a
x a
y = a -> b
p a
x forall a. PartialOrd a => a -> a -> Maybe Ordering
`cmp` a -> b
p a
y