{-# language DataKinds #-} {-# language FlexibleInstances #-} {-# language NamedFieldPuns #-} {-# language ScopedTypeVariables #-} {-# language StandaloneKindSignatures #-} {-# language TypeApplications #-} {-# language TypeFamilies #-} {-# language UndecidableInstances #-} {-# language UndecidableSuperClasses #-} {-# language ViewPatterns #-} module Rel8.Table.Ord ( OrdTable, (<:), (<=:), (>:), (>=:), least, greatest ) where -- base import Data.Functor.Const ( Const( Const ), getConst ) import Data.Kind ( Constraint, Type ) import Prelude hiding ( seq ) -- rel8 import Rel8.Expr ( Expr, Col(..) ) import Rel8.Expr.Bool ( (||.), (&&.), false, true ) import Rel8.Expr.Eq ( (==.) ) import Rel8.Expr.Ord ( (<.), (>.) ) import Rel8.Opaque ( Opaque ) import Rel8.Schema.Dict ( Dict( Dict ) ) import Rel8.Schema.HTable ( HConstrainTable, htabulateA, hfield, hdicts ) import Rel8.Schema.Spec.ConstrainDBType ( ConstrainDBType ) import Rel8.Table ( Columns, toColumns ) import Rel8.Table.Bool ( bool ) import Rel8.Table.Eq ( EqTable ) 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 , HConstrainTable (Columns a) (ConstrainDBType DBOrd) ) => OrdTable a instance ( EqTable a , HConstrainTable (Columns a) (ConstrainDBType DBOrd) ) => OrdTable a instance {-# OVERLAPPING #-} OrdTable Opaque -- | Test if one 'Table' sorts before another. Corresponds to comparing all -- columns with '<'. (<:) :: forall a. OrdTable a => a -> a -> Expr Bool (toColumns -> as) <: (toColumns -> bs) = foldr @[] go false $ getConst $ htabulateA $ \field -> case (hfield as field, hfield bs field) of (DB a, DB b) -> case hfield dicts field of Dict -> Const [(a <. b, a ==. b)] where dicts = hdicts @(Columns a) @(ConstrainDBType DBOrd) go (lt, eq) a = lt ||. (eq &&. 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 (toColumns -> as) <=: (toColumns -> bs) = foldr @[] go true $ getConst $ htabulateA $ \field -> case (hfield as field, hfield bs field) of (DB a, DB b) -> case hfield dicts field of Dict -> Const [(a <. b, a ==. b)] where dicts = hdicts @(Columns a) @(ConstrainDBType DBOrd) go (lt, eq) a = lt ||. (eq &&. a) infix 4 <=: -- | Test if one 'Table' sorts after another. Corresponds to comparing all -- columns with '>'. (>:) :: forall a. OrdTable a => a -> a -> Expr Bool (toColumns -> as) >: (toColumns -> bs) = foldr @[] go false $ getConst $ htabulateA $ \field -> case (hfield as field, hfield bs field) of (DB a, DB b) -> case hfield dicts field of Dict -> Const [(a >. b, a ==. b)] where dicts = hdicts @(Columns a) @(ConstrainDBType DBOrd) go (gt, eq) a = gt ||. (eq &&. a) infix 4 >: -- | Test if one 'Table' sorts after another. Corresponds to comparing all -- columns with '>='. (>=:) :: forall a. OrdTable a => a -> a -> Expr Bool (toColumns -> as) >=: (toColumns -> bs) = foldr @[] go true $ getConst $ htabulateA $ \field -> case (hfield as field, hfield bs field) of (DB a, DB b) -> case hfield dicts field of Dict -> Const [(a >. b, a ==. b)] where dicts = hdicts @(Columns a) @(ConstrainDBType DBOrd) go (gt, eq) a = gt ||. (eq &&. a) infix 4 >=: -- | Given two 'Table's, return the table that sorts before the other. least :: OrdTable a => a -> a -> a least a b = bool a b (a <: b) -- | Given two 'Table's, return the table that sorts after the other. greatest :: OrdTable a => a -> a -> a greatest a b = bool a b (a >: b)