{-# LANGUAGE Safe #-}
module SDP.Estimate
(
module Data.Functor.Classes,
Estimate (..), (<=.>), (<.), (>.), (<=.), (>=.), (==.), (/=.)
)
where
import Data.Functor.Classes
import SDP.Comparing
default ()
infixl 4 <==>, .<., .>., .<=., .>=., .==., ./=.
infixl 4 <.=>, .<, .>, .<=, .>=, .==, ./=
infixl 4 <=.>, <., >., <=., >=., ==., /=.
class Estimate e
where
{-# MINIMAL (<.=>), (<==>) #-}
(<.=>) :: e -> Int -> Ordering
(<==>) :: Compare e
(.==), (./=), (.<=), (.>=), (.<), (.>) :: e -> Int -> Bool
(.<.), (.>.), (.<=.), (.>=.), (.==.), (./=.) :: e -> e -> Bool
e
e .< Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
LT -> Bool
True; Ordering
_ -> Bool
False}
e
e .> Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
GT -> Bool
True; Ordering
_ -> Bool
False}
e
e .<= Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
GT -> Bool
False; Ordering
_ -> Bool
True}
e
e .>= Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
LT -> Bool
False; Ordering
_ -> Bool
True}
e
e .== Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
EQ -> Bool
True; Ordering
_ -> Bool
False}
e
e ./= Int
i = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
EQ -> Bool
False; Ordering
_ -> Bool
True}
e
e1 .<. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
LT -> Bool
True; Ordering
_ -> Bool
False}
e
e1 .>. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
GT -> Bool
True; Ordering
_ -> Bool
False}
e
e1 .<=. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
GT -> Bool
False; Ordering
_ -> Bool
True}
e
e1 .>=. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
LT -> Bool
False; Ordering
_ -> Bool
True}
e
e1 .==. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
EQ -> Bool
True; Ordering
_ -> Bool
False}
e
e1 ./=. e
e2 = case e
e1 Compare e
forall e. Estimate e => Compare e
<==> e
e2 of {Ordering
EQ -> Bool
False; Ordering
_ -> Bool
True}
(<=.>) :: (Estimate e) => Int -> e -> Ordering
Int
i <=.> :: Int -> e -> Ordering
<=.> e
e = case e
e e -> Int -> Ordering
forall e. Estimate e => e -> Int -> Ordering
<.=> Int
i of {Ordering
LT -> Ordering
GT; Ordering
EQ -> Ordering
EQ; Ordering
GT -> Ordering
LT}
(==.), (/=.), (<=.), (>=.), (<.), (>.) :: (Estimate e) => Int -> e -> Bool
==. :: Int -> e -> Bool
(==.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(.==)
/=. :: Int -> e -> Bool
(/=.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(./=)
<=. :: Int -> e -> Bool
(<=.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(.>=)
>=. :: Int -> e -> Bool
(>=.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(.<=)
<. :: Int -> e -> Bool
(<.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(.>)
>. :: Int -> e -> Bool
(>.) = (e -> Int -> Bool) -> Int -> e -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Int -> Bool
forall e. Estimate e => e -> Int -> Bool
(.<)
instance Estimate [a]
where
[] <==> :: Compare [a]
<==> [] = Ordering
EQ
[] <==> [a]
_ = Ordering
LT
[a]
_ <==> [] = Ordering
GT
[a]
xs <==> [a]
ys = [a] -> [a]
forall a. [a] -> [a]
tail [a]
xs Compare [a]
forall e. Estimate e => Compare e
<==> [a] -> [a]
forall a. [a] -> [a]
tail [a]
ys
[] <.=> :: [a] -> Int -> Ordering
<.=> Int
n = Int
0 Compare Int
forall o. Ord o => Compare o
<=> Int
n
[a]
es <.=> Int
n =
let go :: [a] -> t -> Ordering
go [a]
xs t
c | t
c t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
0 = Ordering
GT | [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = t
0 Compare t
forall o. Ord o => Compare o
<=> t
c | Bool
True = [a] -> [a]
forall a. [a] -> [a]
tail [a]
xs [a] -> t -> Ordering
`go` (t
c t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
in if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then Ordering
LT else [a] -> Int -> Ordering
forall t a. (Num t, Ord t) => [a] -> t -> Ordering
go [a]
es Int
n