{-# language AllowAmbiguousTypes #-}
{-# language DataKinds #-}
{-# language DefaultSignatures #-}
{-# language DisambiguateRecordFields #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
{-# language UndecidableInstances #-}
{-# language UndecidableSuperClasses #-}
{-# language ViewPatterns #-}

module Rel8.Table.Ord
  ( OrdTable( ordTable ), (<:), (<=:), (>:), (>=:), least, greatest
  )
where

-- base
import Data.Functor.Const ( Const( Const ), getConst )
import Data.Kind ( Constraint, Type )
import GHC.Generics ( Rep )
import Prelude hiding ( seq )

-- rel8
import Rel8.Expr ( Expr, Col( E ) )
import Rel8.Expr.Bool ( (||.), (&&.), false, true )
import Rel8.Expr.Eq ( (==.) )
import Rel8.Expr.Ord ( (<.), (>.) )
import Rel8.FCF ( Eval, Exp )
import Rel8.Generic.Record ( Record )
import Rel8.Generic.Table
  ( GGTable, GGColumns, ggtable
  , GAlgebra
  )
import Rel8.Kind.Algebra ( KnownAlgebra )
import Rel8.Schema.Dict ( Dict( Dict ) )
import Rel8.Schema.HTable
  ( HTable, HConstrainTable
  , htabulateA, hfield, hdicts
  )
import Rel8.Schema.HTable.Identity ( HIdentity( HType ) )
import Rel8.Schema.Null (Sql)
import Rel8.Schema.Spec.ConstrainDBType ( ConstrainDBType, nullifier )
import Rel8.Table ( Columns, toColumns, TColumns )
import Rel8.Table.Bool ( bool )
import Rel8.Table.Eq ( EqTable )
import Rel8.Type.Eq ( DBEq )
import Rel8.Type.Ord ( DBOrd )


-- | The class of 'Table's that can be ordered. Ordering on tables is defined
-- by their lexicographic ordering of all columns, so this class means "all
-- columns in a 'Table' have an instance of 'DBOrd'".
type OrdTable :: Type -> Constraint
class EqTable a => OrdTable a where
  ordTable :: Columns a (Dict (ConstrainDBType DBOrd))

  default ordTable ::
    ( KnownAlgebra (GAlgebra (Rep (Record a)))
    , Eval (GGTable (GAlgebra (Rep (Record a))) TOrdTable TColumns (Dict (ConstrainDBType DBOrd)) (Rep (Record a)))
    , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
    )
    => Columns a (Dict (ConstrainDBType DBOrd))
  ordTable =
    (forall a (proxy :: * -> *).
 Eval (TOrdTable a) =>
 proxy a -> Eval (TColumns a) (Dict (ConstrainDBType DBOrd)))
-> (forall a (labels :: Labels) (necessity :: Necessity).
    SSpec ('Spec labels necessity a)
    -> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
    -> Dict
         (ConstrainDBType DBOrd) ('Spec labels necessity (Nullify a)))
-> Eval
     (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
     (Dict (ConstrainDBType DBOrd))
forall (algebra :: Algebra) (_Table :: * -> Exp Constraint)
       (_Columns :: * -> Exp HTable) (rep :: * -> *)
       (context :: HContext).
(KnownAlgebra algebra,
 Eval (GGTable algebra _Table _Columns context rep)) =>
(forall a (proxy :: * -> *).
 Eval (_Table a) =>
 proxy a -> Eval (_Columns a) context)
-> (forall a (labels :: Labels) (necessity :: Necessity).
    SSpec ('Spec labels necessity a)
    -> context ('Spec labels necessity a)
    -> context ('Spec labels necessity (Nullify a)))
-> Eval (GGColumns algebra _Columns rep) context
ggtable
      @(GAlgebra (Rep (Record a)))
      @TOrdTable
      @TColumns
      @(Rep (Record a))
      forall a (proxy :: * -> *).
Eval (TOrdTable a) =>
proxy a -> Eval (TColumns a) (Dict (ConstrainDBType DBOrd))
forall x (proxy :: * -> *).
OrdTable x =>
proxy x -> Columns x (Dict (ConstrainDBType DBOrd))
table
      forall (labels :: Labels) (necessity :: Necessity) a
       (c :: * -> Constraint).
SSpec ('Spec labels necessity a)
-> Dict (ConstrainDBType c) ('Spec labels necessity a)
-> Dict (ConstrainDBType c) ('Spec labels necessity (Nullify a))
forall a (labels :: Labels) (necessity :: Necessity).
SSpec ('Spec labels necessity a)
-> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
-> Dict
     (ConstrainDBType DBOrd) ('Spec labels necessity (Nullify a))
nullifier
    where
      table :: proxy x -> Columns x (Dict (ConstrainDBType DBOrd))
table (proxy x
_ :: proxy x) = OrdTable x => Columns x (Dict (ConstrainDBType DBOrd))
forall a. OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
ordTable @x


data TOrdTable :: Type -> Exp Constraint
type instance Eval (TOrdTable a) = OrdTable a


instance
  ( HTable t
  , f ~ Col Expr
  , HConstrainTable t (ConstrainDBType DBEq)
  , HConstrainTable t (ConstrainDBType DBOrd)
  )
  => OrdTable (t f)
 where
  ordTable :: Columns (t f) (Dict (ConstrainDBType DBOrd))
ordTable = HConstrainTable (Columns (t f)) (ConstrainDBType DBOrd) =>
Columns (t f) (Dict (ConstrainDBType DBOrd))
forall (t :: HTable) (c :: Spec -> Constraint).
(HTable t, HConstrainTable t c) =>
t (Dict c)
hdicts @(Columns (t f)) @(ConstrainDBType DBOrd)


instance Sql DBOrd a => OrdTable (Expr a) where
  ordTable :: Columns (Expr a) (Dict (ConstrainDBType DBOrd))
ordTable = Dict (ConstrainDBType DBOrd) ('Spec '[] 'Required a)
-> HType a (Dict (ConstrainDBType DBOrd))
forall (context :: HContext) a.
context ('Spec '[] 'Required a) -> HType a context
HType Dict (ConstrainDBType DBOrd) ('Spec '[] 'Required a)
forall a (c :: a -> Constraint) (a :: a). c a => Dict c a
Dict


instance (OrdTable a, OrdTable b) => OrdTable (a, b)


instance (OrdTable a, OrdTable b, OrdTable c) => OrdTable (a, b, c)


instance (OrdTable a, OrdTable b, OrdTable c, OrdTable d) => OrdTable (a, b, c, d)


instance (OrdTable a, OrdTable b, OrdTable c, OrdTable d, OrdTable e) =>
  OrdTable (a, b, c, d, e)


instance
  ( OrdTable a, OrdTable b, OrdTable c, OrdTable d, OrdTable e, OrdTable f
  )
  => OrdTable (a, b, c, d, e, f)


instance
  ( OrdTable a, OrdTable b, OrdTable c, OrdTable d, OrdTable e, OrdTable f
  , OrdTable g
  )
  => OrdTable (a, b, c, d, e, f, g)


-- | Test if one 'Table' sorts before another. Corresponds to comparing all
-- columns with '<'.
(<:) :: forall a. OrdTable a => a -> a -> Expr Bool
(a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
as) <: :: a -> a -> Expr Bool
<: (a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
bs) =
  ((Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool)
-> Expr Bool -> [(Expr Bool, Expr Bool)] -> Expr Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr @[] (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go Expr Bool
false ([(Expr Bool, Expr Bool)] -> Expr Bool)
-> [(Expr Bool, Expr Bool)] -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a k (b :: k). Const a b -> a
getConst (Const [(Expr Bool, Expr Bool)] (Columns a Any)
 -> [(Expr Bool, Expr Bool)])
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
 HField (Columns a) spec
 -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (spec :: Spec). HField t spec -> m (context spec))
-> m (t context)
htabulateA ((forall (spec :: Spec).
  HField (Columns a) spec
  -> Const [(Expr Bool, Expr Bool)] (Any spec))
 -> Const [(Expr Bool, Expr Bool)] (Columns a Any))
-> (forall (spec :: Spec).
    HField (Columns a) spec
    -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall a b. (a -> b) -> a -> b
$ \HField (Columns a) spec
field ->
    case (Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
as HField (Columns a) spec
field, Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
bs HField (Columns a) spec
field) of
      (E a, E b) -> case Columns a (Dict (ConstrainDBType DBOrd))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
forall a. OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
ordTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
        Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
Dict -> [(Expr Bool, Expr Bool)]
-> Const [(Expr Bool, Expr Bool)] (Any spec)
forall k a (b :: k). a -> Const a b
Const [(Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
<. Expr a
Expr a
b, Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. Expr a
Expr a
b)]
  where
    go :: (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go (Expr Bool
lt, Expr Bool
eq) Expr Bool
a = Expr Bool
lt Expr Bool -> Expr Bool -> Expr Bool
||. (Expr Bool
eq Expr Bool -> Expr Bool -> Expr Bool
&&. Expr Bool
a)
infix 4 <:


-- | Test if one 'Table' sorts before, or is equal to, another. Corresponds to
-- comparing all columns with '<='.
(<=:) :: forall a. OrdTable a => a -> a -> Expr Bool
(a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
as) <=: :: a -> a -> Expr Bool
<=: (a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
bs) =
  ((Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool)
-> Expr Bool -> [(Expr Bool, Expr Bool)] -> Expr Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr @[] (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go Expr Bool
true ([(Expr Bool, Expr Bool)] -> Expr Bool)
-> [(Expr Bool, Expr Bool)] -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a k (b :: k). Const a b -> a
getConst (Const [(Expr Bool, Expr Bool)] (Columns a Any)
 -> [(Expr Bool, Expr Bool)])
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
 HField (Columns a) spec
 -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (spec :: Spec). HField t spec -> m (context spec))
-> m (t context)
htabulateA ((forall (spec :: Spec).
  HField (Columns a) spec
  -> Const [(Expr Bool, Expr Bool)] (Any spec))
 -> Const [(Expr Bool, Expr Bool)] (Columns a Any))
-> (forall (spec :: Spec).
    HField (Columns a) spec
    -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall a b. (a -> b) -> a -> b
$ \HField (Columns a) spec
field ->
    case (Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
as HField (Columns a) spec
field, Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
bs HField (Columns a) spec
field) of
      (E a, E b) -> case Columns a (Dict (ConstrainDBType DBOrd))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
forall a. OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
ordTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
        Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
Dict -> [(Expr Bool, Expr Bool)]
-> Const [(Expr Bool, Expr Bool)] (Any spec)
forall k a (b :: k). a -> Const a b
Const [(Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
<. Expr a
Expr a
b, Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. Expr a
Expr a
b)]
  where
    go :: (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go (Expr Bool
lt, Expr Bool
eq) Expr Bool
a = Expr Bool
lt Expr Bool -> Expr Bool -> Expr Bool
||. (Expr Bool
eq Expr Bool -> Expr Bool -> Expr Bool
&&. Expr Bool
a)
infix 4 <=:


-- | Test if one 'Table' sorts after another. Corresponds to comparing all
-- columns with '>'.
(>:) :: forall a. OrdTable a => a -> a -> Expr Bool
(a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
as) >: :: a -> a -> Expr Bool
>: (a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
bs) =
  ((Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool)
-> Expr Bool -> [(Expr Bool, Expr Bool)] -> Expr Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr @[] (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go Expr Bool
false ([(Expr Bool, Expr Bool)] -> Expr Bool)
-> [(Expr Bool, Expr Bool)] -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a k (b :: k). Const a b -> a
getConst (Const [(Expr Bool, Expr Bool)] (Columns a Any)
 -> [(Expr Bool, Expr Bool)])
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
 HField (Columns a) spec
 -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (spec :: Spec). HField t spec -> m (context spec))
-> m (t context)
htabulateA ((forall (spec :: Spec).
  HField (Columns a) spec
  -> Const [(Expr Bool, Expr Bool)] (Any spec))
 -> Const [(Expr Bool, Expr Bool)] (Columns a Any))
-> (forall (spec :: Spec).
    HField (Columns a) spec
    -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall a b. (a -> b) -> a -> b
$ \HField (Columns a) spec
field ->
    case (Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
as HField (Columns a) spec
field, Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
bs HField (Columns a) spec
field) of
      (E a, E b) -> case Columns a (Dict (ConstrainDBType DBOrd))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
forall a. OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
ordTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
        Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
Dict -> [(Expr Bool, Expr Bool)]
-> Const [(Expr Bool, Expr Bool)] (Any spec)
forall k a (b :: k). a -> Const a b
Const [(Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
>. Expr a
Expr a
b, Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. Expr a
Expr a
b)]
  where
    go :: (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go (Expr Bool
gt, Expr Bool
eq) Expr Bool
a = Expr Bool
gt Expr Bool -> Expr Bool -> Expr Bool
||. (Expr Bool
eq Expr Bool -> Expr Bool -> Expr Bool
&&. Expr Bool
a)
infix 4 >:


-- | Test if one 'Table' sorts after another. Corresponds to comparing all
-- columns with '>='.
(>=:) :: forall a. OrdTable a => a -> a -> Expr Bool
(a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
as) >=: :: a -> a -> Expr Bool
>=: (a -> Columns a (Col Expr)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns -> Columns a (Col Expr)
bs) =
  ((Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool)
-> Expr Bool -> [(Expr Bool, Expr Bool)] -> Expr Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr @[] (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go Expr Bool
true ([(Expr Bool, Expr Bool)] -> Expr Bool)
-> [(Expr Bool, Expr Bool)] -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a k (b :: k). Const a b -> a
getConst (Const [(Expr Bool, Expr Bool)] (Columns a Any)
 -> [(Expr Bool, Expr Bool)])
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
-> [(Expr Bool, Expr Bool)]
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
 HField (Columns a) spec
 -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (spec :: Spec). HField t spec -> m (context spec))
-> m (t context)
htabulateA ((forall (spec :: Spec).
  HField (Columns a) spec
  -> Const [(Expr Bool, Expr Bool)] (Any spec))
 -> Const [(Expr Bool, Expr Bool)] (Columns a Any))
-> (forall (spec :: Spec).
    HField (Columns a) spec
    -> Const [(Expr Bool, Expr Bool)] (Any spec))
-> Const [(Expr Bool, Expr Bool)] (Columns a Any)
forall a b. (a -> b) -> a -> b
$ \HField (Columns a) spec
field ->
    case (Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
as HField (Columns a) spec
field, Columns a (Col Expr) -> HField (Columns a) spec -> Col Expr spec
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield Columns a (Col Expr)
bs HField (Columns a) spec
field) of
      (E a, E b) -> case Columns a (Dict (ConstrainDBType DBOrd))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
forall a. OrdTable a => Columns a (Dict (ConstrainDBType DBOrd))
ordTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
        Dict (ConstrainDBType DBOrd) ('Spec labels necessity a)
Dict -> [(Expr Bool, Expr Bool)]
-> Const [(Expr Bool, Expr Bool)] (Any spec)
forall k a (b :: k). a -> Const a b
Const [(Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
>. Expr a
Expr a
b, Expr a
a Expr a -> Expr a -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. Expr a
Expr a
b)]
  where
    go :: (Expr Bool, Expr Bool) -> Expr Bool -> Expr Bool
go (Expr Bool
gt, Expr Bool
eq) Expr Bool
a = Expr Bool
gt Expr Bool -> Expr Bool -> Expr Bool
||. (Expr Bool
eq Expr Bool -> Expr Bool -> Expr Bool
&&. Expr Bool
a)
infix 4 >=:


-- | Given two 'Table's, return the table that sorts before the other.
least :: OrdTable a => a -> a -> a
least :: a -> a -> a
least a
a a
b = a -> a -> Expr Bool -> a
forall a. Table Expr a => a -> a -> Expr Bool -> a
bool a
a a
b (a
a a -> a -> Expr Bool
forall a. OrdTable a => a -> a -> Expr Bool
<: a
b)


-- | Given two 'Table's, return the table that sorts after the other.
greatest :: OrdTable a => a -> a -> a
greatest :: a -> a -> a
greatest a
a a
b = a -> a -> Expr Bool -> a
forall a. Table Expr a => a -> a -> Expr Bool -> a
bool a
a a
b (a
a a -> a -> Expr Bool
forall a. OrdTable a => a -> a -> Expr Bool
>: a
b)