{-# LANGUAGE Safe #-}

{- |
    Module      :  SDP.Estimate
    Copyright   :  (c) Andrey Mulik 2019
    License     :  BSD-style
    Maintainer  :  work.a.mulik@gmail.com
    Portability :  portable
    
    "SDP.Estimate" provides 'Estimate' class, type synonyms and some common
    comparators. This module is exported by "SDP.SafePrelude".
-}
module SDP.Estimate
(
  -- * Exports
  module Data.Functor.Classes,
  
  -- * Estimate
  Estimate (..), (<=.>), (<.), (>.), (<=.), (>=.), (==.), (/=.)
)
where

import Data.Functor.Classes

import SDP.Comparing

default ()

infixl 4 <==>, .<., .>., .<=., .>=., .==., ./=.

infixl 4 <.=>, .<, .>, .<=, .>=, .==, ./=
infixl 4 <=.>, <., >., <=., >=., ==., /=.

--------------------------------------------------------------------------------

{- |
  'Estimate' class provides the lazy comparsion structures by length.
  
  For some types (e.g., lists), this allows you to speed up the comparison or
  make it finite. For others (e.g., arrays), it may be convenient abbreviation.
-}
class Estimate e
  where
    {-# MINIMAL (<.=>), (<==>) #-}
    
    -- | Left-side structure length with number comparison.
    (<.=>) :: e -> Int -> Ordering
    
    -- | Two structures by length comparison.
    (<==>) :: Compare e
    
    -- | Left-side structure length with number comparison.
    (.==), (./=), (.<=), (.>=), (.<), (.>) :: e -> Int -> Bool
    
    -- | Two structures comparison by length.
    (.<.), (.>.), (.<=.), (.>=.), (.==.), (./=.) :: 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}

-- | Right-side number with structure length comparison.
(<=.>) :: (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}

-- | Right-side number with structure length comparison.
(==.), (/=.), (<=.), (>=.), (<.), (>.) :: (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