{-# language FlexibleContexts #-}
{-# language MonoLocalBinds #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
{-# language TupleSections #-}
{-# language UndecidableInstances #-}
module Rel8.Tabulate
(
Tabulation
, fromQuery
, toQuery
, liftQuery
, through
, lookup
, aggregate
, aggregate1
, distinct
, order
, materialize
, count
, optional
, many
, some
, exists
, present
, absent
, align
, alignWith
, leftAlign
, leftAlignWith
, rightAlign
, rightAlignWith
, zip
, zipWith
, similarity
, difference
)
where
import Control.Applicative ( (<|>), empty, liftA2 )
import Control.Monad ( liftM2 )
import Data.Bifunctor ( Bifunctor, bimap, first, second )
import Data.Foldable ( traverse_ )
import Data.Function ( on )
import Data.Functor.Contravariant ( Contravariant, (>$<), contramap )
import Data.Int ( Int64 )
import Data.Kind ( Type )
import Data.Maybe ( fromJust, fromMaybe )
import Prelude hiding ( lookup, zip, zipWith )
import Data.Bifunctor.Clown ( Clown( Clown ), runClown )
import Control.Comonad ( extract )
import qualified Opaleye.Order as Opaleye ( orderBy, distinctOnExplicit )
import Data.Profunctor ( dimap, lmap )
import Data.Profunctor.Product
( ProductProfunctor, (***!)
, SumProfunctor, (+++!)
)
import qualified Data.Profunctor.Product as PP
import Rel8.Aggregate (Aggregator' (Aggregator), Aggregator, toAggregator1)
import Rel8.Aggregate.Fold (Fallback (Fallback))
import Rel8.Expr ( Expr )
import Rel8.Expr.Aggregate (countStar)
import Rel8.Expr.Bool ( true )
import Rel8.Order ( Order( Order ) )
import Rel8.Query ( Query )
import qualified Rel8.Query.Aggregate as Q
import qualified Rel8.Query.Exists as Q ( exists, present, absent )
import Rel8.Query.Filter ( where_ )
import Rel8.Query.List ( catNonEmptyTable )
import qualified Rel8.Query.Materialize as Q
import qualified Rel8.Query.Maybe as Q ( optional )
import Rel8.Query.Opaleye ( mapOpaleye, unsafePeekQuery )
import Rel8.Query.Rebind ( rebind )
import Rel8.Query.These ( alignBy )
import Rel8.Table ( Table, fromColumns, toColumns )
import Rel8.Table.Aggregate (groupBy, listAgg, nonEmptyAgg)
import Rel8.Table.Alternative
( AltTable, (<|>:)
, AlternativeTable, emptyTable
)
import Rel8.Table.Eq (EqTable, (==:))
import Rel8.Table.List (ListTable)
import Rel8.Table.Maybe (MaybeTable (MaybeTable), fromMaybeTable)
import Rel8.Table.NonEmpty (NonEmptyTable)
import Rel8.Table.Opaleye ( unpackspec )
import Rel8.Table.Ord ( OrdTable )
import Rel8.Table.Order ( ascTable )
import Rel8.Table.Projection
( Biprojectable, biproject
, Projectable, project
, apply
)
import Rel8.Table.These ( TheseTable( TheseTable ), theseTable )
import Data.Functor.Apply ( Apply, liftF2 )
import Data.Functor.Bind ( Bind, (>>-) )
type Key :: Type -> Type
type Key = Maybe
cat :: Table Expr k => Key k -> Query k
cat :: forall k. Table Expr k => Key k -> Query k
cat = Query k -> (k -> Query k) -> Maybe k -> Query k
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Query k
forall a. Table Expr a => Query a
forall (f :: * -> *) a. (AlternativeTable f, Table Expr a) => f a
emptyTable k -> Query k
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
key :: (ProductProfunctor p, SumProfunctor p)
=> p a b -> p (Key a) (Key b)
key :: forall (p :: * -> * -> *) a b.
(ProductProfunctor p, SumProfunctor p) =>
p a b -> p (Key a) (Key b)
key p a b
a = (Key a -> Either () a)
-> (Either () b -> Key b)
-> p (Either () a) (Either () b)
-> p (Key a) (Key b)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap Key a -> Either () a
forall {b}. Maybe b -> Either () b
from Either () b -> Key b
forall {a} {a}. Either a a -> Maybe a
to (p () ()
forall (p :: * -> * -> *). ProductProfunctor p => p () ()
PP.empty p () () -> p a b -> p (Either () a) (Either () b)
forall a b a' b'. p a b -> p a' b' -> p (Either a a') (Either b b')
forall (p :: * -> * -> *) a b a' b'.
SumProfunctor p =>
p a b -> p a' b' -> p (Either a a') (Either b b')
+++! p a b
a)
where
from :: Maybe b -> Either () b
from = Either () b -> (b -> Either () b) -> Maybe b -> Either () b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () b
forall a b. a -> Either a b
Left ()) b -> Either () b
forall a b. b -> Either a b
Right
to :: Either a a -> Maybe a
to = (a -> Maybe a) -> (a -> Maybe a) -> Either a a -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe a -> a -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing) a -> Maybe a
forall a. a -> Maybe a
Just
keyed :: (ProductProfunctor p, SumProfunctor p)
=> p k l -> p a b -> p (Key k, a) (Key l, b)
keyed :: forall (p :: * -> * -> *) k l a b.
(ProductProfunctor p, SumProfunctor p) =>
p k l -> p a b -> p (Key k, a) (Key l, b)
keyed p k l
k p a b
a = p k l -> p (Key k) (Key l)
forall (p :: * -> * -> *) a b.
(ProductProfunctor p, SumProfunctor p) =>
p a b -> p (Key a) (Key b)
key p k l
k p (Key k) (Key l) -> p a b -> p (Key k, a) (Key l, b)
forall a b a' b'. p a b -> p a' b' -> p (a, a') (b, b')
forall (p :: * -> * -> *) a b a' b'.
ProductProfunctor p =>
p a b -> p a' b' -> p (a, a') (b, b')
***! p a b
a
type Predicate :: Type -> Type
newtype Predicate a = Predicate (Maybe (a -> Expr Bool))
instance Contravariant Predicate where
contramap :: forall a' a. (a' -> a) -> Predicate a -> Predicate a'
contramap a' -> a
f (Predicate Maybe (a -> Expr Bool)
a) = Maybe (a' -> Expr Bool) -> Predicate a'
forall a. Maybe (a -> Expr Bool) -> Predicate a
Predicate ((a' -> a) -> (a -> Expr Bool) -> a' -> Expr Bool
forall a b c. (a -> b) -> (b -> c) -> a -> c
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a' -> a
f ((a -> Expr Bool) -> a' -> Expr Bool)
-> Maybe (a -> Expr Bool) -> Maybe (a' -> Expr Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (a -> Expr Bool)
a)
instance Semigroup (Predicate k) where
Predicate Maybe (k -> Expr Bool)
ma <> :: Predicate k -> Predicate k -> Predicate k
<> Predicate Maybe (k -> Expr Bool)
mb = Maybe (k -> Expr Bool) -> Predicate k
forall a. Maybe (a -> Expr Bool) -> Predicate a
Predicate (Maybe (k -> Expr Bool) -> Predicate k)
-> Maybe (k -> Expr Bool) -> Predicate k
forall a b. (a -> b) -> a -> b
$ Maybe (k -> Expr Bool)
ma Maybe (k -> Expr Bool)
-> Maybe (k -> Expr Bool) -> Maybe (k -> Expr Bool)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (k -> Expr Bool)
mb
instance Monoid (Predicate k) where
mempty :: Predicate k
mempty = Maybe (k -> Expr Bool) -> Predicate k
forall a. Maybe (a -> Expr Bool) -> Predicate a
Predicate Maybe (k -> Expr Bool)
forall a. Maybe a
Nothing
match :: EqTable k => Key k -> Predicate k
match :: forall k. EqTable k => Key k -> Predicate k
match = Maybe (k -> Expr Bool) -> Predicate k
forall a. Maybe (a -> Expr Bool) -> Predicate a
Predicate (Maybe (k -> Expr Bool) -> Predicate k)
-> (Key k -> Maybe (k -> Expr Bool)) -> Key k -> Predicate k
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> k -> Expr Bool) -> Key k -> Maybe (k -> Expr Bool)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap k -> k -> Expr Bool
forall a. EqTable a => a -> a -> Expr Bool
(==:)
ensure :: Predicate k -> Key k -> Query ()
ensure :: forall k. Predicate k -> Key k -> Query ()
ensure (Predicate Maybe (k -> Expr Bool)
mp) = (k -> Query ()) -> Maybe k -> Query ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (\k
k -> ((k -> Expr Bool) -> Query ())
-> Maybe (k -> Expr Bool) -> Query ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (\k -> Expr Bool
p -> Expr Bool -> Query ()
where_ (k -> Expr Bool
p k
k)) Maybe (k -> Expr Bool)
mp)
type Tabulation :: Type -> Type -> Type
newtype Tabulation k a = Tabulation (Predicate k -> Query (Key k, a))
instance Biprojectable Tabulation where
biproject :: forall a b c d.
(Projecting a b, Projecting c d) =>
Projection a b
-> Projection c d -> Tabulation a c -> Tabulation b d
biproject Projection a b
f Projection c d
g =
(a -> b) -> (c -> d) -> Tabulation a c -> Tabulation b d
forall a b c d.
(a -> b) -> (c -> d) -> Tabulation a c -> Tabulation b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap
(Columns b (Context b) -> b
Columns (Transpose (Field a) b) (Context b) -> b
forall (context :: * -> *) a.
Table context a =>
Columns a context -> a
fromColumns (Columns (Transpose (Field a) b) (Context b) -> b)
-> (a -> Columns (Transpose (Field a) b) (Context b)) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Projection a b -> Columns a (Context b) -> Columns b (Context b)
forall a b (context :: * -> *).
Projecting a b =>
Projection a b -> Columns a context -> Columns b context
apply Projection a b
f (Columns a (Context b)
-> Columns (Transpose (Field a) b) (Context b))
-> (a -> Columns a (Context b))
-> a
-> Columns (Transpose (Field a) b) (Context b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Context b)
forall (context :: * -> *) a.
Table context a =>
a -> Columns a context
toColumns)
(Columns d (Context d) -> d
Columns (Transpose (Field c) d) (Context d) -> d
forall (context :: * -> *) a.
Table context a =>
Columns a context -> a
fromColumns (Columns (Transpose (Field c) d) (Context d) -> d)
-> (c -> Columns (Transpose (Field c) d) (Context d)) -> c -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Projection c d -> Columns c (Context d) -> Columns d (Context d)
forall a b (context :: * -> *).
Projecting a b =>
Projection a b -> Columns a context -> Columns b context
apply Projection c d
g (Columns c (Context d)
-> Columns (Transpose (Field c) d) (Context d))
-> (c -> Columns c (Context d))
-> c
-> Columns (Transpose (Field c) d) (Context d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Columns c (Context d)
forall (context :: * -> *) a.
Table context a =>
a -> Columns a context
toColumns)
instance Bifunctor Tabulation where
bimap :: forall a b c d.
(a -> b) -> (c -> d) -> Tabulation a c -> Tabulation b d
bimap a -> b
f c -> d
g (Tabulation Predicate a -> Query (Key a, c)
a) = (Predicate b -> Query (Key b, d)) -> Tabulation b d
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate b -> Query (Key b, d)) -> Tabulation b d)
-> (Predicate b -> Query (Key b, d)) -> Tabulation b d
forall a b. (a -> b) -> a -> b
$ \Predicate b
p ->
(Key a -> Key b) -> (c -> d) -> (Key a, c) -> (Key b, d)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((a -> b) -> Key a -> Key b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) c -> d
g ((Key a, c) -> (Key b, d)) -> Query (Key a, c) -> Query (Key b, d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Predicate a -> Query (Key a, c)
a (a -> b
f (a -> b) -> Predicate b -> Predicate a
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< Predicate b
p)
instance Functor (Tabulation k) where
fmap :: forall a b. (a -> b) -> Tabulation k a -> Tabulation k b
fmap = (a -> b) -> Tabulation k a -> Tabulation k b
forall b c a. (b -> c) -> Tabulation a b -> Tabulation a c
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second
instance Projectable (Tabulation k) where
project :: forall a b.
Projecting a b =>
Projection a b -> Tabulation k a -> Tabulation k b
project Projection a b
f = (a -> b) -> Tabulation k a -> Tabulation k b
forall a b. (a -> b) -> Tabulation k a -> Tabulation k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Columns b (Context b) -> b
Columns (Transpose (Field a) b) (Context b) -> b
forall (context :: * -> *) a.
Table context a =>
Columns a context -> a
fromColumns (Columns (Transpose (Field a) b) (Context b) -> b)
-> (a -> Columns (Transpose (Field a) b) (Context b)) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Projection a b -> Columns a (Context b) -> Columns b (Context b)
forall a b (context :: * -> *).
Projecting a b =>
Projection a b -> Columns a context -> Columns b context
apply Projection a b
f (Columns a (Context b)
-> Columns (Transpose (Field a) b) (Context b))
-> (a -> Columns a (Context b))
-> a
-> Columns (Transpose (Field a) b) (Context b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Context b)
forall (context :: * -> *) a.
Table context a =>
a -> Columns a context
toColumns)
instance EqTable k => Apply (Tabulation k) where
liftF2 :: forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
liftF2 = (a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
instance EqTable k => Applicative (Tabulation k) where
pure :: forall a. a -> Tabulation k a
pure = Query a -> Tabulation k a
forall a k. Query a -> Tabulation k a
liftQuery (Query a -> Tabulation k a)
-> (a -> Query a) -> a -> Tabulation k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Query a
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
liftA2 :: forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
liftA2 = (a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2
instance EqTable k => Bind (Tabulation k) where
Tabulation Predicate k -> Query (Key k, a)
as >>- :: forall a b.
Tabulation k a -> (a -> Tabulation k b) -> Tabulation k b
>>- a -> Tabulation k b
f = (Predicate k -> Query (Key k, b)) -> Tabulation k b
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, b)) -> Tabulation k b)
-> (Predicate k -> Query (Key k, b)) -> Tabulation k b
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> do
(Key k
k, a
a) <- Predicate k -> Query (Key k, a)
as Predicate k
p
case a -> Tabulation k b
f a
a of
Tabulation Predicate k -> Query (Key k, b)
bs -> do
let p' :: Predicate k
p' = Key k -> Predicate k
forall k. EqTable k => Key k -> Predicate k
match Key k
k
(Key k
k', b
b) <- Predicate k -> Query (Key k, b)
bs (Predicate k
p' Predicate k -> Predicate k -> Predicate k
forall a. Semigroup a => a -> a -> a
<> Predicate k
p)
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p' Key k
k'
(Key k, b) -> Query (Key k, b)
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
k' Key k -> Key k -> Key k
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Key k
k, b
b)
instance EqTable k => Monad (Tabulation k) where
>>= :: forall a b.
Tabulation k a -> (a -> Tabulation k b) -> Tabulation k b
(>>=) = Tabulation k a -> (a -> Tabulation k b) -> Tabulation k b
forall a b.
Tabulation k a -> (a -> Tabulation k b) -> Tabulation k b
forall (m :: * -> *) a b. Bind m => m a -> (a -> m b) -> m b
(>>-)
instance EqTable k => AltTable (Tabulation k) where
Tabulation k a
tas <|>: :: forall a.
Table Expr a =>
Tabulation k a -> Tabulation k a -> Tabulation k a
<|>: Tabulation k a
tbs = do
Either (Query a) (Query (k, a))
eas <- Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
forall k a.
Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
peek Tabulation k a
tas
Either (Query a) (Query (k, a))
ebs <- Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
forall k a.
Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
peek Tabulation k a
tbs
case (Either (Query a) (Query (k, a))
eas, Either (Query a) (Query (k, a))
ebs) of
(Left Query a
as, Left Query a
bs) -> Query a -> Tabulation k a
forall a k. Query a -> Tabulation k a
liftQuery (Query a -> Tabulation k a) -> Query a -> Tabulation k a
forall a b. (a -> b) -> a -> b
$ Query a
as Query a -> Query a -> Query a
forall a. Table Expr a => Query a -> Query a -> Query a
forall (f :: * -> *) a.
(AltTable f, Table Expr a) =>
f a -> f a -> f a
<|>: Query a
bs
(Right Query (k, a)
as, Right Query (k, a)
bs) -> Query (k, a) -> Tabulation k a
forall k a. Query (k, a) -> Tabulation k a
fromQuery (Query (k, a) -> Tabulation k a) -> Query (k, a) -> Tabulation k a
forall a b. (a -> b) -> a -> b
$ Query (k, a)
as Query (k, a) -> Query (k, a) -> Query (k, a)
forall a. Table Expr a => Query a -> Query a -> Query a
forall (f :: * -> *) a.
(AltTable f, Table Expr a) =>
f a -> f a -> f a
<|>: Query (k, a)
bs
(Either (Query a) (Query (k, a)), Either (Query a) (Query (k, a)))
_ -> NonEmptyTable Expr a -> Query a
forall a. Table Expr a => NonEmptyTable Expr a -> Query a
catNonEmptyTable (NonEmptyTable Expr a -> Query a)
-> Tabulation k (NonEmptyTable Expr a) -> Tabulation k a
forall a b k. (a -> Query b) -> Tabulation k a -> Tabulation k b
`through` (Tabulation k (NonEmptyTable Expr a)
-> Tabulation k (NonEmptyTable Expr a)
-> Tabulation k (NonEmptyTable Expr a)
forall a. Semigroup a => a -> a -> a
(<>) (Tabulation k (NonEmptyTable Expr a)
-> Tabulation k (NonEmptyTable Expr a)
-> Tabulation k (NonEmptyTable Expr a))
-> (Tabulation k a -> Tabulation k (NonEmptyTable Expr a))
-> Tabulation k a
-> Tabulation k a
-> Tabulation k (NonEmptyTable Expr a)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Tabulation k a -> Tabulation k (NonEmptyTable Expr a)
forall k a.
(EqTable k, Table Expr a) =>
Tabulation k a -> Tabulation k (NonEmptyTable Expr a)
some) Tabulation k a
tas Tabulation k a
tbs
instance EqTable k => AlternativeTable (Tabulation k) where
emptyTable :: forall a. Table Expr a => Tabulation k a
emptyTable = (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall a b. (a -> b) -> a -> b
$ Query (Key k, a) -> Predicate k -> Query (Key k, a)
forall a b. a -> b -> a
const (Query (Key k, a) -> Predicate k -> Query (Key k, a))
-> Query (Key k, a) -> Predicate k -> Query (Key k, a)
forall a b. (a -> b) -> a -> b
$ (a -> (Key k, a)) -> Query a -> Query (Key k, a)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty,) Query a
forall a. Table Expr a => Query a
forall (f :: * -> *) a. (AlternativeTable f, Table Expr a) => f a
emptyTable
instance (EqTable k, Table Expr a, Semigroup a) => Semigroup (Tabulation k a)
where
<> :: Tabulation k a -> Tabulation k a -> Tabulation k a
(<>) = (TheseTable Expr a a -> a)
-> Tabulation k a -> Tabulation k a -> Tabulation k a
forall k a b c.
EqTable k =>
(TheseTable Expr a b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
alignWith ((a -> a) -> (a -> a) -> (a -> a -> a) -> TheseTable Expr a a -> a
forall c a b.
Table Expr c =>
(a -> c) -> (b -> c) -> (a -> b -> c) -> TheseTable Expr a b -> c
theseTable a -> a
forall a. a -> a
id a -> a
forall a. a -> a
id a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>))
instance (EqTable k, Table Expr a, Semigroup a) => Monoid (Tabulation k a)
where
mempty :: Tabulation k a
mempty = Tabulation k a
forall a. Table Expr a => Tabulation k a
forall (f :: * -> *) a. (AlternativeTable f, Table Expr a) => f a
emptyTable
fromQuery :: Query (k, a) -> Tabulation k a
fromQuery :: forall k a. Query (k, a) -> Tabulation k a
fromQuery = (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Query (k, a) -> Predicate k -> Query (Key k, a))
-> Query (k, a)
-> Tabulation k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query (Key k, a) -> Predicate k -> Query (Key k, a)
forall a b. a -> b -> a
const (Query (Key k, a) -> Predicate k -> Query (Key k, a))
-> (Query (k, a) -> Query (Key k, a))
-> Query (k, a)
-> Predicate k
-> Query (Key k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> (Key k, a)) -> Query (k, a) -> Query (Key k, a)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((k -> Key k) -> (k, a) -> (Key k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first k -> Key k
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
toQuery :: Table Expr k => Tabulation k a -> Query (k, a)
toQuery :: forall k a. Table Expr k => Tabulation k a -> Query (k, a)
toQuery (Tabulation Predicate k -> Query (Key k, a)
f) = do
(Key k
mk, a
a) <- Predicate k -> Query (Key k, a)
f Predicate k
forall a. Monoid a => a
mempty
k
k <- Key k -> Query k
forall k. Table Expr k => Key k -> Query k
cat Key k
mk
(k, a) -> Query (k, a)
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
k, a
a)
liftQuery :: Query a -> Tabulation k a
liftQuery :: forall a k. Query a -> Tabulation k a
liftQuery = (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Query a -> Predicate k -> Query (Key k, a))
-> Query a
-> Tabulation k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query (Key k, a) -> Predicate k -> Query (Key k, a)
forall a b. a -> b -> a
const (Query (Key k, a) -> Predicate k -> Query (Key k, a))
-> (Query a -> Query (Key k, a))
-> Query a
-> Predicate k
-> Query (Key k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (Key k, a)) -> Query a -> Query (Key k, a)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty,)
through :: (a -> Query b) -> Tabulation k a -> Tabulation k b
through :: forall a b k. (a -> Query b) -> Tabulation k a -> Tabulation k b
through a -> Query b
f (Tabulation Predicate k -> Query (Key k, a)
as) = (Predicate k -> Query (Key k, b)) -> Tabulation k b
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, b)) -> Tabulation k b)
-> (Predicate k -> Query (Key k, b)) -> Tabulation k b
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> do
(Key k
k, a
a) <- Predicate k -> Query (Key k, a)
as Predicate k
p
b
b <- a -> Query b
f a
a
(Key k, b) -> Query (Key k, b)
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
k, b
b)
infixr 1 `through`
lookup :: EqTable k => k -> Tabulation k a -> Query a
lookup :: forall k a. EqTable k => k -> Tabulation k a -> Query a
lookup k
k (Tabulation Predicate k -> Query (Key k, a)
f) = do
(Key k
k', a
a) <- Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p Key k
k'
a -> Query a
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
where
p :: Predicate k
p = Key k -> Predicate k
forall k. EqTable k => Key k -> Predicate k
match (k -> Key k
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure k
k)
aggregate :: (EqTable k, Table Expr i, Table Expr a)
=> Aggregator i a -> Tabulation k i -> Tabulation k a
aggregate :: forall k i a.
(EqTable k, Table Expr i, Table Expr a) =>
Aggregator i a -> Tabulation k i -> Tabulation k a
aggregate aggregator :: Aggregator i a
aggregator@(Aggregator (Fallback a
fallback) Aggregator i a
_) =
(MaybeTable Expr a -> a)
-> Tabulation k (MaybeTable Expr a) -> Tabulation k a
forall a b. (a -> b) -> Tabulation k a -> Tabulation k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> MaybeTable Expr a -> a
forall a. Table Expr a => a -> MaybeTable Expr a -> a
fromMaybeTable a
fallback) (Tabulation k (MaybeTable Expr a) -> Tabulation k a)
-> (Tabulation k i -> Tabulation k (MaybeTable Expr a))
-> Tabulation k i
-> Tabulation k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tabulation k a -> Tabulation k (MaybeTable Expr a)
forall k a. Tabulation k a -> Tabulation k (MaybeTable Expr a)
optional (Tabulation k a -> Tabulation k (MaybeTable Expr a))
-> (Tabulation k i -> Tabulation k a)
-> Tabulation k i
-> Tabulation k (MaybeTable Expr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Aggregator i a -> Tabulation k i -> Tabulation k a
forall k i (fold :: Fold) a.
(EqTable k, Table Expr i) =>
Aggregator' fold i a -> Tabulation k i -> Tabulation k a
aggregate1 Aggregator i a
aggregator
aggregate1 :: (EqTable k, Table Expr i)
=> Aggregator' fold i a -> Tabulation k i -> Tabulation k a
aggregate1 :: forall k i (fold :: Fold) a.
(EqTable k, Table Expr i) =>
Aggregator' fold i a -> Tabulation k i -> Tabulation k a
aggregate1 Aggregator' fold i a
aggregator (Tabulation Predicate k -> Query (Key k, i)
f) =
(Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall a b. (a -> b) -> a -> b
$ Unpackspec (Key k, i) (Key k, i)
-> Aggregator' 'Semi (Key k, i) (Key k, a)
-> Query (Key k, i)
-> Query (Key k, a)
forall i (fold :: Fold) a.
Unpackspec i i -> Aggregator' fold i a -> Query i -> Query a
Q.aggregateU (Unpackspec k k
-> Unpackspec i i -> Unpackspec (Key k, i) (Key k, i)
forall (p :: * -> * -> *) k l a b.
(ProductProfunctor p, SumProfunctor p) =>
p k l -> p a b -> p (Key k, a) (Key l, b)
keyed Unpackspec k k
forall a. Table Expr a => Unpackspec a a
unpackspec Unpackspec i i
forall a. Table Expr a => Unpackspec a a
unpackspec) (Aggregator' 'Semi k k
-> Aggregator' 'Semi i a -> Aggregator' 'Semi (Key k, i) (Key k, a)
forall (p :: * -> * -> *) k l a b.
(ProductProfunctor p, SumProfunctor p) =>
p k l -> p a b -> p (Key k, a) (Key l, b)
keyed Aggregator' 'Semi k k
forall a. EqTable a => Aggregator1 a a
groupBy (Aggregator' fold i a -> Aggregator' 'Semi i a
forall (fold :: Fold) i a. Aggregator' fold i a -> Aggregator1 i a
toAggregator1 Aggregator' fold i a
aggregator)) (Query (Key k, i) -> Query (Key k, a))
-> (Predicate k -> Query (Key k, i))
-> Predicate k
-> Query (Key k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate k -> Query (Key k, i)
f
distinct :: EqTable k => Tabulation k a -> Tabulation k a
distinct :: forall k a. EqTable k => Tabulation k a -> Tabulation k a
distinct (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall a b. (a -> b) -> a -> b
$
(Select (Key k, a) -> Select (Key k, a))
-> Query (Key k, a) -> Query (Key k, a)
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (Unpackspec (Key k) (Key k)
-> ((Key k, a) -> Key k) -> Select (Key k, a) -> Select (Key k, a)
forall b a. Unpackspec b b -> (a -> b) -> Select a -> Select a
Opaleye.distinctOnExplicit (Unpackspec k k -> Unpackspec (Key k) (Key k)
forall (p :: * -> * -> *) a b.
(ProductProfunctor p, SumProfunctor p) =>
p a b -> p (Key a) (Key b)
key Unpackspec k k
forall a. Table Expr a => Unpackspec a a
unpackspec) (Key k, a) -> Key k
forall a b. (a, b) -> a
fst) (Query (Key k, a) -> Query (Key k, a))
-> (Predicate k -> Query (Key k, a))
-> Predicate k
-> Query (Key k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate k -> Query (Key k, a)
f
order :: OrdTable k => Order a -> Tabulation k a -> Tabulation k a
order :: forall k a.
OrdTable k =>
Order a -> Tabulation k a -> Tabulation k a
order Order a
ordering (Tabulation Predicate k -> Query (Key k, a)
f) =
(Predicate k -> Query (Key k, a)) -> Tabulation k a
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, a)) -> Tabulation k a)
-> (Predicate k -> Query (Key k, a)) -> Tabulation k a
forall a b. (a -> b) -> a -> b
$ (Select (Key k, a) -> Select (Key k, a))
-> Query (Key k, a) -> Query (Key k, a)
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (Order (Key k, a) -> Select (Key k, a) -> Select (Key k, a)
forall a. Order a -> Select a -> Select a
Opaleye.orderBy Order (Key k, a)
ordering') (Query (Key k, a) -> Query (Key k, a))
-> (Predicate k -> Query (Key k, a))
-> Predicate k
-> Query (Key k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate k -> Query (Key k, a)
f
where
Order Order (Key k, a)
ordering' = Clown Order (Key k, a) (Key Any, Any) -> Order (Key k, a)
forall {k1} {k2} (f :: k1 -> *) (a :: k1) (b :: k2).
Clown f a b -> f a
runClown (Clown Order k Any
-> Clown Order a Any -> Clown Order (Key k, a) (Key Any, Any)
forall (p :: * -> * -> *) k l a b.
(ProductProfunctor p, SumProfunctor p) =>
p k l -> p a b -> p (Key k, a) (Key l, b)
keyed (Order k -> Clown Order k Any
forall {k} {k1} (f :: k -> *) (a :: k) (b :: k1).
f a -> Clown f a b
Clown Order k
forall a. OrdTable a => Order a
ascTable) (Order a -> Clown Order a Any
forall {k} {k1} (f :: k -> *) (a :: k) (b :: k1).
f a -> Clown f a b
Clown Order a
ordering))
count :: EqTable k => Tabulation k a -> Tabulation k (Expr Int64)
count :: forall k a.
EqTable k =>
Tabulation k a -> Tabulation k (Expr Int64)
count = Aggregator (Expr Bool) (Expr Int64)
-> Tabulation k (Expr Bool) -> Tabulation k (Expr Int64)
forall k i a.
(EqTable k, Table Expr i, Table Expr a) =>
Aggregator i a -> Tabulation k i -> Tabulation k a
aggregate Aggregator (Expr Bool) (Expr Int64)
forall (fold :: Fold) i. Aggregator' fold i (Expr Int64)
countStar (Tabulation k (Expr Bool) -> Tabulation k (Expr Int64))
-> (Tabulation k a -> Tabulation k (Expr Bool))
-> Tabulation k a
-> Tabulation k (Expr Int64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Expr Bool
true Expr Bool -> Tabulation k a -> Tabulation k (Expr Bool)
forall a b. a -> Tabulation k b -> Tabulation k a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$)
optional :: Tabulation k a -> Tabulation k (MaybeTable Expr a)
optional :: forall k a. Tabulation k a -> Tabulation k (MaybeTable Expr a)
optional (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, MaybeTable Expr a))
-> Tabulation k (MaybeTable Expr a)
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, MaybeTable Expr a))
-> Tabulation k (MaybeTable Expr a))
-> (Predicate k -> Query (Key k, MaybeTable Expr a))
-> Tabulation k (MaybeTable Expr a)
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> case Predicate k
p of
Predicate Maybe (k -> Expr Bool)
Nothing -> (a -> MaybeTable Expr a)
-> (Key k, a) -> (Key k, MaybeTable Expr a)
forall a b. (a -> b) -> (Key k, a) -> (Key k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> MaybeTable Expr a
forall a. a -> MaybeTable Expr a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Key k, a) -> (Key k, MaybeTable Expr a))
-> Query (Key k, a) -> Query (Key k, MaybeTable Expr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k
_ -> (MaybeTable Expr (Key k, a) -> (Key k, MaybeTable Expr a))
-> Query (MaybeTable Expr (Key k, a))
-> Query (Key k, MaybeTable Expr a)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\MaybeTable Expr (Key k, a)
m -> (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty, (Key k, a) -> a
forall a b. (a, b) -> b
snd ((Key k, a) -> a)
-> MaybeTable Expr (Key k, a) -> MaybeTable Expr a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeTable Expr (Key k, a)
m)) (Query (MaybeTable Expr (Key k, a))
-> Query (Key k, MaybeTable Expr a))
-> Query (MaybeTable Expr (Key k, a))
-> Query (Key k, MaybeTable Expr a)
forall a b. (a -> b) -> a -> b
$ Query (Key k, a) -> Query (MaybeTable Expr (Key k, a))
forall a. Query a -> Query (MaybeTable Expr a)
Q.optional (Query (Key k, a) -> Query (MaybeTable Expr (Key k, a)))
-> Query (Key k, a) -> Query (MaybeTable Expr (Key k, a))
forall a b. (a -> b) -> a -> b
$ do
(Key k
k, a
a) <- Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p Key k
k
(Key k, a) -> Query (Key k, a)
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
k, a
a)
many :: (EqTable k, Table Expr a)
=> Tabulation k a -> Tabulation k (ListTable Expr a)
many :: forall k a.
(EqTable k, Table Expr a) =>
Tabulation k a -> Tabulation k (ListTable Expr a)
many = Aggregator a (ListTable Expr a)
-> Tabulation k a -> Tabulation k (ListTable Expr a)
forall k i a.
(EqTable k, Table Expr i, Table Expr a) =>
Aggregator i a -> Tabulation k i -> Tabulation k a
aggregate Aggregator a (ListTable Expr a)
forall a (fold :: Fold).
Table Expr a =>
Aggregator' fold a (ListTable Expr a)
listAgg
some :: (EqTable k, Table Expr a)
=> Tabulation k a -> Tabulation k (NonEmptyTable Expr a)
some :: forall k a.
(EqTable k, Table Expr a) =>
Tabulation k a -> Tabulation k (NonEmptyTable Expr a)
some = Aggregator' 'Semi a (NonEmptyTable Expr a)
-> Tabulation k a -> Tabulation k (NonEmptyTable Expr a)
forall k i (fold :: Fold) a.
(EqTable k, Table Expr i) =>
Aggregator' fold i a -> Tabulation k i -> Tabulation k a
aggregate1 Aggregator' 'Semi a (NonEmptyTable Expr a)
forall a. Table Expr a => Aggregator1 a (NonEmptyTable Expr a)
nonEmptyAgg
exists :: Tabulation k a -> Tabulation k (Expr Bool)
exists :: forall k a. Tabulation k a -> Tabulation k (Expr Bool)
exists (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, Expr Bool))
-> Tabulation k (Expr Bool)
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, Expr Bool))
-> Tabulation k (Expr Bool))
-> (Predicate k -> Query (Key k, Expr Bool))
-> Tabulation k (Expr Bool)
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> case Predicate k
p of
Predicate Maybe (k -> Expr Bool)
Nothing -> (Expr Bool
true Expr Bool -> (Key k, a) -> (Key k, Expr Bool)
forall a b. a -> (Key k, b) -> (Key k, a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$) ((Key k, a) -> (Key k, Expr Bool))
-> Query (Key k, a) -> Query (Key k, Expr Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k
_ -> (Expr Bool -> (Key k, Expr Bool))
-> Query (Expr Bool) -> Query (Key k, Expr Bool)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty,) (Query (Expr Bool) -> Query (Key k, Expr Bool))
-> Query (Expr Bool) -> Query (Key k, Expr Bool)
forall a b. (a -> b) -> a -> b
$ Query () -> Query (Expr Bool)
forall a. Query a -> Query (Expr Bool)
Q.exists (Query () -> Query (Expr Bool)) -> Query () -> Query (Expr Bool)
forall a b. (a -> b) -> a -> b
$ do
(Key k
k, a
_) <- Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p Key k
k
present :: Tabulation k a -> Tabulation k ()
present :: forall k a. Tabulation k a -> Tabulation k ()
present (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, ())) -> Tabulation k ()
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, ())) -> Tabulation k ())
-> (Predicate k -> Query (Key k, ())) -> Tabulation k ()
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> do
Query () -> Query ()
forall a. Query a -> Query ()
Q.present (Query () -> Query ()) -> Query () -> Query ()
forall a b. (a -> b) -> a -> b
$ do
(Key k
k, a
_) <- Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p Key k
k
(Key k, ()) -> Query (Key k, ())
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty, ())
absent :: Tabulation k a -> Tabulation k ()
absent :: forall k a. Tabulation k a -> Tabulation k ()
absent (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, ())) -> Tabulation k ()
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, ())) -> Tabulation k ())
-> (Predicate k -> Query (Key k, ())) -> Tabulation k ()
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> do
Query () -> Query ()
forall a. Query a -> Query ()
Q.absent (Query () -> Query ()) -> Query () -> Query ()
forall a b. (a -> b) -> a -> b
$ do
(Key k
k, a
_) <- Predicate k -> Query (Key k, a)
f Predicate k
p
Predicate k -> Key k -> Query ()
forall k. Predicate k -> Key k -> Query ()
ensure Predicate k
p Key k
k
(Key k, ()) -> Query (Key k, ())
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty, ())
align :: EqTable k
=> Tabulation k a -> Tabulation k b -> Tabulation k (TheseTable Expr a b)
align :: forall k a b.
EqTable k =>
Tabulation k a
-> Tabulation k b -> Tabulation k (TheseTable Expr a b)
align = (TheseTable Expr a b -> TheseTable Expr a b)
-> Tabulation k a
-> Tabulation k b
-> Tabulation k (TheseTable Expr a b)
forall k a b c.
EqTable k =>
(TheseTable Expr a b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
alignWith TheseTable Expr a b -> TheseTable Expr a b
forall a. a -> a
id
alignWith :: EqTable k
=> (TheseTable Expr a b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
alignWith :: forall k a b c.
EqTable k =>
(TheseTable Expr a b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
alignWith TheseTable Expr a b -> c
f (Tabulation Predicate k -> Query (Key k, a)
as) (Tabulation Predicate k -> Query (Key k, b)
bs) = (Predicate k -> Query (Key k, c)) -> Tabulation k c
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, c)) -> Tabulation k c)
-> (Predicate k -> Query (Key k, c)) -> Tabulation k c
forall a b. (a -> b) -> a -> b
$ \Predicate k
p -> do
TheseTable Expr (Key k, a) (Key k, b)
tkab <- (Query (Key k, a)
-> Query (Key k, b)
-> Query (TheseTable Expr (Key k, a) (Key k, b)))
-> (Predicate k -> Query (Key k, a))
-> (Predicate k -> Query (Key k, b))
-> Predicate k
-> Query (TheseTable Expr (Key k, a) (Key k, b))
forall a b c.
(a -> b -> c)
-> (Predicate k -> a) -> (Predicate k -> b) -> Predicate k -> c
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 (((Key k, a) -> (Key k, b) -> Expr Bool)
-> Query (Key k, a)
-> Query (Key k, b)
-> Query (TheseTable Expr (Key k, a) (Key k, b))
forall a b.
(a -> b -> Expr Bool)
-> Query a -> Query b -> Query (TheseTable Expr a b)
alignBy (Key k, a) -> (Key k, b) -> Expr Bool
forall {b} {b} {b}.
(Context b ~ Expr, Transpose Expr b ~ b, EqTable b) =>
(Maybe b, b) -> (Maybe b, b) -> Expr Bool
condition) Predicate k -> Query (Key k, a)
as Predicate k -> Query (Key k, b)
bs Predicate k
p
Key k
k <- (k -> Query k) -> Key k -> Query (Key k)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse (String -> k -> Query k
forall a. Table Expr a => String -> a -> Query a
rebind String
"key") (Key k -> Query (Key k)) -> Key k -> Query (Key k)
forall a b. (a -> b) -> a -> b
$ TheseTable Expr (Key k) (Key k) -> Key k
forall {a}.
(Context a ~ Expr, Transpose Expr a ~ a, Table Expr a) =>
TheseTable Expr (Maybe a) (Maybe a) -> Maybe a
recover (TheseTable Expr (Key k) (Key k) -> Key k)
-> TheseTable Expr (Key k) (Key k) -> Key k
forall a b. (a -> b) -> a -> b
$ ((Key k, a) -> Key k)
-> ((Key k, b) -> Key k)
-> TheseTable Expr (Key k, a) (Key k, b)
-> TheseTable Expr (Key k) (Key k)
forall a b c d.
(a -> b) -> (c -> d) -> TheseTable Expr a c -> TheseTable Expr b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Key k, a) -> Key k
forall a b. (a, b) -> a
fst (Key k, b) -> Key k
forall a b. (a, b) -> a
fst TheseTable Expr (Key k, a) (Key k, b)
tkab
let
tab :: TheseTable Expr a b
tab = ((Key k, a) -> a)
-> ((Key k, b) -> b)
-> TheseTable Expr (Key k, a) (Key k, b)
-> TheseTable Expr a b
forall a b c d.
(a -> b) -> (c -> d) -> TheseTable Expr a c -> TheseTable Expr b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Key k, a) -> a
forall a b. (a, b) -> b
snd (Key k, b) -> b
forall a b. (a, b) -> b
snd TheseTable Expr (Key k, a) (Key k, b)
tkab
(Key k, c) -> Query (Key k, c)
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Key k
k, TheseTable Expr a b -> c
f TheseTable Expr a b
tab)
where
condition :: (Maybe b, b) -> (Maybe b, b) -> Expr Bool
condition (Maybe b
k, b
_) (Maybe b
k', b
_) = Expr Bool -> Maybe (Expr Bool) -> Expr Bool
forall a. a -> Maybe a -> a
fromMaybe Expr Bool
true ((b -> b -> Expr Bool) -> Maybe b -> Maybe b -> Maybe (Expr Bool)
forall a b c. (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> Expr Bool
forall a. EqTable a => a -> a -> Expr Bool
(==:) Maybe b
k Maybe b
k')
recover :: TheseTable Expr (Maybe a) (Maybe a) -> Maybe a
recover (TheseTable mma :: MaybeTable Expr (Maybe a)
mma@(MaybeTable Expr (Maybe MaybeTag)
_ Nullify Expr (Maybe a)
ma) mmb :: MaybeTable Expr (Maybe a)
mmb@(MaybeTable Expr (Maybe MaybeTag)
_ Nullify Expr (Maybe a)
mb)) =
case Nullify Expr (Maybe a) -> Maybe a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr (Maybe a)
ma of
Maybe a
Nothing -> Nullify Expr (Maybe a) -> Maybe a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr (Maybe a)
mb
Just a
a -> case Nullify Expr (Maybe a) -> Maybe a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr (Maybe a)
mb of
Maybe a
Nothing -> Nullify Expr (Maybe a) -> Maybe a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr (Maybe a)
ma
Just a
b -> case a
a a -> MaybeTable Expr (Maybe a) -> MaybeTable Expr a
forall a b. a -> MaybeTable Expr b -> MaybeTable Expr a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MaybeTable Expr (Maybe a)
mma MaybeTable Expr a -> MaybeTable Expr a -> MaybeTable Expr a
forall a.
Table Expr a =>
MaybeTable Expr a -> MaybeTable Expr a -> MaybeTable Expr a
forall (f :: * -> *) a.
(AltTable f, Table Expr a) =>
f a -> f a -> f a
<|>: a
b a -> MaybeTable Expr (Maybe a) -> MaybeTable Expr a
forall a b. a -> MaybeTable Expr b -> MaybeTable Expr a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MaybeTable Expr (Maybe a)
mmb of
MaybeTable Expr (Maybe MaybeTag)
_ Nullify Expr a
c -> a -> Maybe a
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Nullify Expr a -> a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr a
c)
leftAlign :: EqTable k
=> Tabulation k a -> Tabulation k b -> Tabulation k (a, MaybeTable Expr b)
leftAlign :: forall k a b.
EqTable k =>
Tabulation k a
-> Tabulation k b -> Tabulation k (a, MaybeTable Expr b)
leftAlign = (a -> MaybeTable Expr b -> (a, MaybeTable Expr b))
-> Tabulation k a
-> Tabulation k b
-> Tabulation k (a, MaybeTable Expr b)
forall k a b c.
EqTable k =>
(a -> MaybeTable Expr b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
leftAlignWith (,)
leftAlignWith :: EqTable k
=> (a -> MaybeTable Expr b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
leftAlignWith :: forall k a b c.
EqTable k =>
(a -> MaybeTable Expr b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
leftAlignWith a -> MaybeTable Expr b -> c
f Tabulation k a
left Tabulation k b
right = (a -> MaybeTable Expr b -> c)
-> Tabulation k a
-> Tabulation k (MaybeTable Expr b)
-> Tabulation k c
forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> MaybeTable Expr b -> c
f Tabulation k a
left (Tabulation k b -> Tabulation k (MaybeTable Expr b)
forall k a. Tabulation k a -> Tabulation k (MaybeTable Expr a)
optional Tabulation k b
right)
rightAlign :: EqTable k
=> Tabulation k a -> Tabulation k b -> Tabulation k (MaybeTable Expr a, b)
rightAlign :: forall k a b.
EqTable k =>
Tabulation k a
-> Tabulation k b -> Tabulation k (MaybeTable Expr a, b)
rightAlign = (MaybeTable Expr a -> b -> (MaybeTable Expr a, b))
-> Tabulation k a
-> Tabulation k b
-> Tabulation k (MaybeTable Expr a, b)
forall k a b c.
EqTable k =>
(MaybeTable Expr a -> b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
rightAlignWith (,)
rightAlignWith :: EqTable k
=> (MaybeTable Expr a -> b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
rightAlignWith :: forall k a b c.
EqTable k =>
(MaybeTable Expr a -> b -> c)
-> Tabulation k a -> Tabulation k b -> Tabulation k c
rightAlignWith MaybeTable Expr a -> b -> c
f Tabulation k a
left Tabulation k b
right = (b -> MaybeTable Expr a -> c)
-> Tabulation k b
-> Tabulation k (MaybeTable Expr a)
-> Tabulation k c
forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((MaybeTable Expr a -> b -> c) -> b -> MaybeTable Expr a -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip MaybeTable Expr a -> b -> c
f) Tabulation k b
right (Tabulation k a -> Tabulation k (MaybeTable Expr a)
forall k a. Tabulation k a -> Tabulation k (MaybeTable Expr a)
optional Tabulation k a
left)
zip :: EqTable k
=> Tabulation k a -> Tabulation k b -> Tabulation k (a, b)
zip :: forall k a b.
EqTable k =>
Tabulation k a -> Tabulation k b -> Tabulation k (a, b)
zip = (a -> b -> (a, b))
-> Tabulation k a -> Tabulation k b -> Tabulation k (a, b)
forall k a b c.
EqTable k =>
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
zipWith (,)
zipWith :: EqTable k
=> (a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
zipWith :: forall k a b c.
EqTable k =>
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
zipWith = (a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall a b c.
(a -> b -> c) -> Tabulation k a -> Tabulation k b -> Tabulation k c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
similarity :: EqTable k => Tabulation k a -> Tabulation k b -> Tabulation k a
similarity :: forall k a b.
EqTable k =>
Tabulation k a -> Tabulation k b -> Tabulation k a
similarity Tabulation k a
a Tabulation k b
b = Tabulation k a
a Tabulation k a -> Tabulation k () -> Tabulation k a
forall a b. Tabulation k a -> Tabulation k b -> Tabulation k a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Tabulation k b -> Tabulation k ()
forall k a. Tabulation k a -> Tabulation k ()
present Tabulation k b
b
difference :: EqTable k => Tabulation k a -> Tabulation k b -> Tabulation k a
difference :: forall k a b.
EqTable k =>
Tabulation k a -> Tabulation k b -> Tabulation k a
difference Tabulation k a
a Tabulation k b
b = Tabulation k a
a Tabulation k a -> Tabulation k () -> Tabulation k a
forall a b. Tabulation k a -> Tabulation k b -> Tabulation k a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Tabulation k b -> Tabulation k ()
forall k a. Tabulation k a -> Tabulation k ()
absent Tabulation k b
b
materialize :: (Table Expr k, Table Expr a, Table Expr b)
=> Tabulation k a -> (Tabulation k a -> Query b) -> Query b
materialize :: forall k a b.
(Table Expr k, Table Expr a, Table Expr b) =>
Tabulation k a -> (Tabulation k a -> Query b) -> Query b
materialize Tabulation k a
tabulation Tabulation k a -> Query b
f = case Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
forall k a.
Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
peek Tabulation k a
tabulation of
Tabulation Predicate k -> Query (Key k, Either (Query a) (Query (k, a)))
query -> do
(Key k
_, Either (Query a) (Query (k, a))
equery) <- Predicate k -> Query (Key k, Either (Query a) (Query (k, a)))
query Predicate k
forall a. Monoid a => a
mempty
case Either (Query a) (Query (k, a))
equery of
Left Query a
as -> Query a -> (Query a -> Query b) -> Query b
forall a b.
(Table Expr a, Table Expr b) =>
Query a -> (Query a -> Query b) -> Query b
Q.materialize Query a
as (Tabulation k a -> Query b
f (Tabulation k a -> Query b)
-> (Query a -> Tabulation k a) -> Query a -> Query b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query a -> Tabulation k a
forall a k. Query a -> Tabulation k a
liftQuery)
Right Query (k, a)
kas -> Query (k, a) -> (Query (k, a) -> Query b) -> Query b
forall a b.
(Table Expr a, Table Expr b) =>
Query a -> (Query a -> Query b) -> Query b
Q.materialize Query (k, a)
kas (Tabulation k a -> Query b
f (Tabulation k a -> Query b)
-> (Query (k, a) -> Tabulation k a) -> Query (k, a) -> Query b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query (k, a) -> Tabulation k a
forall k a. Query (k, a) -> Tabulation k a
fromQuery)
peek :: Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
peek :: forall k a.
Tabulation k a -> Tabulation k (Either (Query a) (Query (k, a)))
peek (Tabulation Predicate k -> Query (Key k, a)
f) = (Predicate k -> Query (Key k, Either (Query a) (Query (k, a))))
-> Tabulation k (Either (Query a) (Query (k, a)))
forall k a. (Predicate k -> Query (Key k, a)) -> Tabulation k a
Tabulation ((Predicate k -> Query (Key k, Either (Query a) (Query (k, a))))
-> Tabulation k (Either (Query a) (Query (k, a))))
-> (Predicate k -> Query (Key k, Either (Query a) (Query (k, a))))
-> Tabulation k (Either (Query a) (Query (k, a)))
forall a b. (a -> b) -> a -> b
$ \Predicate k
p ->
(Key k, Either (Query a) (Query (k, a)))
-> Query (Key k, Either (Query a) (Query (k, a)))
forall a. a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Key k, Either (Query a) (Query (k, a)))
-> Query (Key k, Either (Query a) (Query (k, a))))
-> (Key k, Either (Query a) (Query (k, a)))
-> Query (Key k, Either (Query a) (Query (k, a)))
forall a b. (a -> b) -> a -> b
$ (Key k
forall a. Maybe a
forall (f :: * -> *) a. Alternative f => f a
empty,) (Either (Query a) (Query (k, a))
-> (Key k, Either (Query a) (Query (k, a))))
-> Either (Query a) (Query (k, a))
-> (Key k, Either (Query a) (Query (k, a)))
forall a b. (a -> b) -> a -> b
$ case Query (Key k, a) -> (Key k, a)
forall a. Query a -> a
unsafePeekQuery (Predicate k -> Query (Key k, a)
f Predicate k
p) of
(Key k
Nothing, a
_) -> Query a -> Either (Query a) (Query (k, a))
forall a b. a -> Either a b
Left (Query a -> Either (Query a) (Query (k, a)))
-> Query a -> Either (Query a) (Query (k, a))
forall a b. (a -> b) -> a -> b
$ ((Key k, a) -> a) -> Query (Key k, a) -> Query a
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key k, a) -> a
forall a b. (a, b) -> b
snd (Predicate k -> Query (Key k, a)
f Predicate k
p)
(Just k
_, a
_) -> Query (k, a) -> Either (Query a) (Query (k, a))
forall a b. b -> Either a b
Right (Query (k, a) -> Either (Query a) (Query (k, a)))
-> Query (k, a) -> Either (Query a) (Query (k, a))
forall a b. (a -> b) -> a -> b
$ ((Key k, a) -> (k, a)) -> Query (Key k, a) -> Query (k, a)
forall a b. (a -> b) -> Query a -> Query b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Key k -> k) -> (Key k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key k -> k
forall a. HasCallStack => Maybe a -> a
fromJust) (Predicate k -> Query (Key k, a)
f Predicate k
p)