{-# language AllowAmbiguousTypes #-}
{-# language BlockArguments #-}
{-# 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.Eq
( EqTable( eqTable ), (==:), (/=:)
)
where
import Data.Foldable ( foldl' )
import Data.Functor.Const ( Const( Const ), getConst )
import Data.Kind ( Constraint, Type )
import Data.List.NonEmpty ( NonEmpty( (:|) ) )
import GHC.Generics ( Rep )
import Prelude
import Rel8.Expr ( Expr, Col( E ) )
import Rel8.Expr.Bool ( (||.), (&&.) )
import Rel8.Expr.Eq ( (==.), (/=.) )
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 ( Table, Columns, toColumns, TColumns )
import Rel8.Type.Eq ( DBEq )
type EqTable :: Type -> Constraint
class Table Expr a => EqTable a where
eqTable :: Columns a (Dict (ConstrainDBType DBEq))
default eqTable ::
( KnownAlgebra (GAlgebra (Rep (Record a)))
, Eval (GGTable (GAlgebra (Rep (Record a))) TEqTable TColumns (Dict (ConstrainDBType DBEq)) (Rep (Record a)))
, Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
)
=> Columns a (Dict (ConstrainDBType DBEq))
eqTable =
(forall a (proxy :: * -> *).
Eval (TEqTable a) =>
proxy a -> Eval (TColumns a) (Dict (ConstrainDBType DBEq)))
-> (forall a (labels :: Labels) (necessity :: Necessity).
SSpec ('Spec labels necessity a)
-> Dict (ConstrainDBType DBEq) ('Spec labels necessity a)
-> Dict
(ConstrainDBType DBEq) ('Spec labels necessity (Nullify a)))
-> Eval
(GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
(Dict (ConstrainDBType DBEq))
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)))
@TEqTable
@TColumns
@(Rep (Record a))
forall a (proxy :: * -> *).
Eval (TEqTable a) =>
proxy a -> Eval (TColumns a) (Dict (ConstrainDBType DBEq))
forall x (proxy :: * -> *).
EqTable x =>
proxy x -> Columns x (Dict (ConstrainDBType DBEq))
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 DBEq) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBEq) ('Spec labels necessity (Nullify a))
nullifier
where
table :: proxy x -> Columns x (Dict (ConstrainDBType DBEq))
table (proxy x
_ :: proxy x) = EqTable x => Columns x (Dict (ConstrainDBType DBEq))
forall a. EqTable a => Columns a (Dict (ConstrainDBType DBEq))
eqTable @x
data TEqTable :: Type -> Exp Constraint
type instance Eval (TEqTable a) = EqTable a
instance
( HTable t
, f ~ Col Expr
, HConstrainTable t (ConstrainDBType DBEq)
)
=> EqTable (t f)
where
eqTable :: Columns (t f) (Dict (ConstrainDBType DBEq))
eqTable = HConstrainTable (Columns (t f)) (ConstrainDBType DBEq) =>
Columns (t f) (Dict (ConstrainDBType DBEq))
forall (t :: HTable) (c :: Spec -> Constraint).
(HTable t, HConstrainTable t c) =>
t (Dict c)
hdicts @(Columns (t f)) @(ConstrainDBType DBEq)
instance Sql DBEq a => EqTable (Expr a) where
eqTable :: Columns (Expr a) (Dict (ConstrainDBType DBEq))
eqTable = Dict (ConstrainDBType DBEq) ('Spec '[] 'Required a)
-> HType a (Dict (ConstrainDBType DBEq))
forall (context :: HContext) a.
context ('Spec '[] 'Required a) -> HType a context
HType Dict (ConstrainDBType DBEq) ('Spec '[] 'Required a)
forall a (c :: a -> Constraint) (a :: a). c a => Dict c a
Dict
instance (EqTable a, EqTable b) => EqTable (a, b)
instance (EqTable a, EqTable b, EqTable c) => EqTable (a, b, c)
instance (EqTable a, EqTable b, EqTable c, EqTable d) => EqTable (a, b, c, d)
instance (EqTable a, EqTable b, EqTable c, EqTable d, EqTable e) =>
EqTable (a, b, c, d, e)
instance (EqTable a, EqTable b, EqTable c, EqTable d, EqTable e, EqTable f) =>
EqTable (a, b, c, d, e, f)
instance
( EqTable a, EqTable b, EqTable c, EqTable d, EqTable e, EqTable f
, EqTable g
)
=> EqTable (a, b, c, d, e, f, g)
(==:) :: forall a. EqTable 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)
-> NonEmpty (Expr Bool) -> Expr Bool
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1' Expr Bool -> Expr Bool -> Expr Bool
(&&.) (NonEmpty (Expr Bool) -> Expr Bool)
-> NonEmpty (Expr Bool) -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool)
forall a k (b :: k). Const a b -> a
getConst (Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool))
-> Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool)
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
HField (Columns a) spec -> Const (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (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 (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (Expr Bool)) (Columns a Any))
-> (forall (spec :: Spec).
HField (Columns a) spec -> Const (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (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 DBEq))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBEq) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (EqTable a => Columns a (Dict (ConstrainDBType DBEq))
forall a. EqTable a => Columns a (Dict (ConstrainDBType DBEq))
eqTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
Dict (ConstrainDBType DBEq) ('Spec labels necessity a)
Dict -> NonEmpty (Expr Bool) -> Const (NonEmpty (Expr Bool)) (Any spec)
forall k a (b :: k). a -> Const a b
Const (Expr Bool -> NonEmpty (Expr Bool)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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))
infix 4 ==:
(/=:) :: forall a. EqTable 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)
-> NonEmpty (Expr Bool) -> Expr Bool
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1' Expr Bool -> Expr Bool -> Expr Bool
(||.) (NonEmpty (Expr Bool) -> Expr Bool)
-> NonEmpty (Expr Bool) -> Expr Bool
forall a b. (a -> b) -> a -> b
$ Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool)
forall a k (b :: k). Const a b -> a
getConst (Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool))
-> Const (NonEmpty (Expr Bool)) (Columns a Any)
-> NonEmpty (Expr Bool)
forall a b. (a -> b) -> a -> b
$ (forall (spec :: Spec).
HField (Columns a) spec -> Const (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (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 (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (Expr Bool)) (Columns a Any))
-> (forall (spec :: Spec).
HField (Columns a) spec -> Const (NonEmpty (Expr Bool)) (Any spec))
-> Const (NonEmpty (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 DBEq))
-> HField (Columns a) ('Spec labels necessity a)
-> Dict (ConstrainDBType DBEq) ('Spec labels necessity a)
forall (t :: HTable) (context :: HContext) (spec :: Spec).
HTable t =>
t context -> HField t spec -> context spec
hfield (EqTable a => Columns a (Dict (ConstrainDBType DBEq))
forall a. EqTable a => Columns a (Dict (ConstrainDBType DBEq))
eqTable @a) HField (Columns a) spec
HField (Columns a) ('Spec labels necessity a)
field of
Dict (ConstrainDBType DBEq) ('Spec labels necessity a)
Dict -> NonEmpty (Expr Bool) -> Const (NonEmpty (Expr Bool)) (Any spec)
forall k a (b :: k). a -> Const a b
Const (Expr Bool -> NonEmpty (Expr Bool)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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))
infix 4 /=:
foldl1' :: (a -> a -> a) -> NonEmpty a -> a
foldl1' :: (a -> a -> a) -> NonEmpty a -> a
foldl1' a -> a -> a
f (a
a :| [a]
as) = (a -> a -> a) -> a -> [a] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' a -> a -> a
f a
a [a]
as