{-# language DataKinds #-}
{-# language FlexibleContexts #-}
{-# language LambdaCase #-}
{-# language MultiParamTypeClasses #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeFamilies #-}
{-# language UndecidableInstances #-}

module Rel8.Column.Either
  ( HEither, AHEither(..)
  )
where

-- base
import Control.Applicative ( liftA2 )
import Data.Bifunctor ( Bifunctor, bimap )
import Data.Kind ( Type )
import Prelude

-- rel8
import Rel8.Aggregate ( Aggregate )
import Rel8.Expr ( Expr )
import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
import Rel8.Schema.Context ( Col )
import Rel8.Schema.HTable.Either ( HEitherTable )
import qualified Rel8.Schema.Kind as K
import Rel8.Schema.Name ( Name(..) )
import Rel8.Schema.Reify ( Reify, hreify, hunreify )
import Rel8.Schema.Result ( Result )
import Rel8.Table
  ( Table, Columns, Context, fromColumns, toColumns
  , Unreify, reify, unreify
  )
import Rel8.Table.Either ( EitherTable )
import Rel8.Table.Recontextualize ( Recontextualize )


-- | Nest an 'Either' value within a 'Rel8able'. @HEither f a b@ will produce a
-- 'EitherTable' @a b@ in the 'Expr' context, and a 'Either' @a b@ in the
-- 'Result' context.
type HEither :: K.Context -> Type -> Type -> Type
type family HEither context where
  HEither (Reify context) = AHEither context
  HEither Aggregate = EitherTable
  HEither Expr = EitherTable
  HEither Name = EitherTable
  HEither Result = Either


type AHEither :: K.Context -> Type -> Type -> Type
newtype AHEither context a b = AHEither (HEither context a b)


instance Reifiable context => Bifunctor (AHEither context) where
  bimap :: (a -> b)
-> (c -> d) -> AHEither context a c -> AHEither context b d
bimap = SContext context
-> (a -> b)
-> (c -> d)
-> AHEither context a c
-> AHEither context b d
forall (context :: Context) a c b d.
SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
sbimapEither SContext context
forall (context :: Context). Reifiable context => SContext context
contextSing


instance Reifiable context => Functor (AHEither context a) where
  fmap :: (a -> b) -> AHEither context a a -> AHEither context a b
fmap = (a -> a)
-> (a -> b) -> AHEither context a a -> AHEither context a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> a
forall a. a -> a
id


instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
  => Table (Reify context) (AHEither context a b)
 where
  type Context (AHEither context a b) = Reify context
  type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
  type Unreify (AHEither context a b) = HEither context (Unreify a) (Unreify b)

  fromColumns :: Columns (AHEither context a b) (Col (Reify context))
-> AHEither context a b
fromColumns = SContext context
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall (context :: Context) a b.
(Table (Reify context) a, Table (Reify context) b) =>
SContext context
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
sfromColumnsEither SContext context
forall (context :: Context). Reifiable context => SContext context
contextSing
  toColumns :: AHEither context a b
-> Columns (AHEither context a b) (Col (Reify context))
toColumns = SContext context
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (context :: Context) a b.
(Table (Reify context) a, Table (Reify context) b) =>
SContext context
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
stoColumnsEither SContext context
forall (context :: Context). Reifiable context => SContext context
contextSing
  reify :: (Reify context :~: Reify ctx)
-> Unreify (AHEither context a b) -> AHEither context a b
reify Reify context :~: Reify ctx
proof = ((Unreify a -> a)
 -> (Unreify b -> b)
 -> AHEither context (Unreify a) (Unreify b)
 -> AHEither context a b)
-> ((Reify context :~: Reify ctx) -> Unreify a -> a)
-> ((Reify context :~: Reify ctx) -> Unreify b -> b)
-> (Reify context :~: Reify ctx)
-> AHEither context (Unreify a) (Unreify b)
-> AHEither context a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Unreify a -> a)
-> (Unreify b -> b)
-> AHEither context (Unreify a) (Unreify b)
-> AHEither context a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Reify context :~: Reify ctx) -> Unreify a -> a
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> Unreify a -> a
reify (Reify context :~: Reify ctx) -> Unreify b -> b
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> Unreify a -> a
reify Reify context :~: Reify ctx
proof (AHEither context (Unreify a) (Unreify b) -> AHEither context a b)
-> (HEither context (Unreify a) (Unreify b)
    -> AHEither context (Unreify a) (Unreify b))
-> HEither context (Unreify a) (Unreify b)
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HEither context (Unreify a) (Unreify b)
-> AHEither context (Unreify a) (Unreify b)
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither
  unreify :: (Reify context :~: Reify ctx)
-> AHEither context a b -> Unreify (AHEither context a b)
unreify Reify context :~: Reify ctx
proof = (\(AHEither HEither context (Unreify a) (Unreify b)
a) -> HEither context (Unreify a) (Unreify b)
a) (AHEither context (Unreify a) (Unreify b)
 -> HEither context (Unreify a) (Unreify b))
-> (AHEither context a b
    -> AHEither context (Unreify a) (Unreify b))
-> AHEither context a b
-> HEither context (Unreify a) (Unreify b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a -> Unreify a)
 -> (b -> Unreify b)
 -> AHEither context a b
 -> AHEither context (Unreify a) (Unreify b))
-> ((Reify context :~: Reify ctx) -> a -> Unreify a)
-> ((Reify context :~: Reify ctx) -> b -> Unreify b)
-> (Reify context :~: Reify ctx)
-> AHEither context a b
-> AHEither context (Unreify a) (Unreify b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (a -> Unreify a)
-> (b -> Unreify b)
-> AHEither context a b
-> AHEither context (Unreify a) (Unreify b)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Reify context :~: Reify ctx) -> a -> Unreify a
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> a -> Unreify a
unreify (Reify context :~: Reify ctx) -> b -> Unreify b
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> a -> Unreify a
unreify Reify context :~: Reify ctx
proof


instance
  ( Reifiable context, Reifiable context'
  , Recontextualize (Reify context) (Reify context') a a'
  , Recontextualize (Reify context) (Reify context') b b'
  ) =>
  Recontextualize
    (Reify context)
    (Reify context')
    (AHEither context a b)
    (AHEither context' a' b')


sbimapEither :: ()
  => SContext context
  -> (a -> c)
  -> (b -> d)
  -> AHEither context a b
  -> AHEither context c d
sbimapEither :: SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
sbimapEither = \case
  SContext context
SAggregate -> \a -> c
f b -> d
g (AHEither HEither context a b
a) -> HEither context c d -> AHEither context c d
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither ((a -> c) -> (b -> d) -> EitherTable a b -> EitherTable c d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> c
f b -> d
g EitherTable a b
HEither context a b
a)
  SContext context
SExpr -> \a -> c
f b -> d
g (AHEither HEither context a b
a) -> HEither context c d -> AHEither context c d
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither ((a -> c) -> (b -> d) -> EitherTable a b -> EitherTable c d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> c
f b -> d
g EitherTable a b
HEither context a b
a)
  SContext context
SResult -> \a -> c
f b -> d
g (AHEither HEither context a b
a) -> HEither context c d -> AHEither context c d
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither ((a -> c) -> (b -> d) -> Either a b -> Either c d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> c
f b -> d
g Either a b
HEither context a b
a)
  SContext context
SName -> \a -> c
f b -> d
g (AHEither HEither context a b
a) -> HEither context c d -> AHEither context c d
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither ((a -> c) -> (b -> d) -> EitherTable a b -> EitherTable c d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> c
f b -> d
g EitherTable a b
HEither context a b
a)
  SReify SContext context
context -> \a -> c
f b -> d
g (AHEither HEither context a b
a) -> HEither context c d -> AHEither context c d
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
forall (context :: Context) a c b d.
SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
sbimapEither SContext context
context a -> c
f b -> d
g AHEither context a b
HEither context a b
a)


sfromColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
  => SContext context
  -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
  -> AHEither context a b
sfromColumnsEither :: SContext context
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
sfromColumnsEither = \case
  SContext context
SAggregate ->
    EitherTable a b -> AHEither context a b
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (EitherTable a b -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable a b)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (Columns a (Col Aggregate) -> a)
-> (Columns b (Col Aggregate) -> b)
-> EitherTable
     (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
-> EitherTable a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Aggregate)) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns a (Col (Reify Aggregate)) -> a)
-> (Columns a (Col Aggregate) -> Columns a (Col (Reify Aggregate)))
-> Columns a (Col Aggregate)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col Aggregate) -> Columns a (Col (Reify Aggregate))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Columns b (Col (Reify Aggregate)) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns b (Col (Reify Aggregate)) -> b)
-> (Columns b (Col Aggregate) -> Columns b (Col (Reify Aggregate)))
-> Columns b (Col Aggregate)
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col Aggregate) -> Columns b (Col (Reify Aggregate))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (EitherTable
   (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
 -> EitherTable a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable
         (Columns a (Col Aggregate)) (Columns b (Col Aggregate)))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col context)
-> EitherTable
     (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (HEitherTable (Columns a) (Columns b) (Col context)
 -> EitherTable
      (Columns a (Col Aggregate)) (Columns b (Col Aggregate)))
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable
     (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify
  SContext context
SExpr ->
    EitherTable a b -> AHEither context a b
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (EitherTable a b -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable a b)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (Columns a (Col Expr) -> a)
-> (Columns b (Col Expr) -> b)
-> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
-> EitherTable a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Expr)) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns a (Col (Reify Expr)) -> a)
-> (Columns a (Col Expr) -> Columns a (Col (Reify Expr)))
-> Columns a (Col Expr)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col Expr) -> Columns a (Col (Reify Expr))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Columns b (Col (Reify Expr)) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns b (Col (Reify Expr)) -> b)
-> (Columns b (Col Expr) -> Columns b (Col (Reify Expr)))
-> Columns b (Col Expr)
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col Expr) -> Columns b (Col (Reify Expr))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
 -> EitherTable a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr)))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col context)
-> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (HEitherTable (Columns a) (Columns b) (Col context)
 -> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr)))
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify
  SContext context
SResult ->
    Either a b -> AHEither context a b
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (Either a b -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> Either a b)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (Columns a (Col Result) -> a)
-> (Columns b (Col Result) -> b)
-> Either (Columns a (Col Result)) (Columns b (Col Result))
-> Either a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Result)) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns a (Col (Reify Result)) -> a)
-> (Columns a (Col Result) -> Columns a (Col (Reify Result)))
-> Columns a (Col Result)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col Result) -> Columns a (Col (Reify Result))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Columns b (Col (Reify Result)) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns b (Col (Reify Result)) -> b)
-> (Columns b (Col Result) -> Columns b (Col (Reify Result)))
-> Columns b (Col Result)
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col Result) -> Columns b (Col (Reify Result))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Either (Columns a (Col Result)) (Columns b (Col Result))
 -> Either a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> Either (Columns a (Col Result)) (Columns b (Col Result)))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> Either a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col context)
-> Either (Columns a (Col Result)) (Columns b (Col Result))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (HEitherTable (Columns a) (Columns b) (Col context)
 -> Either (Columns a (Col Result)) (Columns b (Col Result)))
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> Either (Columns a (Col Result)) (Columns b (Col Result))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify
  SContext context
SName ->
    EitherTable a b -> AHEither context a b
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (EitherTable a b -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable a b)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (Columns a (Col Name) -> a)
-> (Columns b (Col Name) -> b)
-> EitherTable (Columns a (Col Name)) (Columns b (Col Name))
-> EitherTable a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Name)) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns a (Col (Reify Name)) -> a)
-> (Columns a (Col Name) -> Columns a (Col (Reify Name)))
-> Columns a (Col Name)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col Name) -> Columns a (Col (Reify Name))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Columns b (Col (Reify Name)) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns b (Col (Reify Name)) -> b)
-> (Columns b (Col Name) -> Columns b (Col (Reify Name)))
-> Columns b (Col Name)
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col Name) -> Columns b (Col (Reify Name))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (EitherTable (Columns a (Col Name)) (Columns b (Col Name))
 -> EitherTable a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> EitherTable (Columns a (Col Name)) (Columns b (Col Name)))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col context)
-> EitherTable (Columns a (Col Name)) (Columns b (Col Name))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (HEitherTable (Columns a) (Columns b) (Col context)
 -> EitherTable (Columns a (Col Name)) (Columns b (Col Name)))
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> EitherTable (Columns a (Col Name)) (Columns b (Col Name))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify
  SReify SContext context
context ->
    AHEither context a b -> AHEither context a b
forall (context :: Context) a b.
HEither context a b -> AHEither context a b
AHEither (AHEither context a b -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> AHEither context a b)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    SContext context
-> (Columns a (Col (Reify context)) -> a)
-> (Columns b (Col (Reify context)) -> b)
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
-> AHEither context a b
forall (context :: Context) a c b d.
SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
sbimapEither SContext context
context (Columns a (Col (Reify (Reify context))) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns a (Col (Reify (Reify context))) -> a)
-> (Columns a (Col (Reify context))
    -> Columns a (Col (Reify (Reify context))))
-> Columns a (Col (Reify context))
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col (Reify context))
-> Columns a (Col (Reify (Reify context)))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (Columns b (Col (Reify (Reify context))) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (Columns b (Col (Reify (Reify context))) -> b)
-> (Columns b (Col (Reify context))
    -> Columns b (Col (Reify (Reify context))))
-> Columns b (Col (Reify context))
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col (Reify context))
-> Columns b (Col (Reify (Reify context)))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify) (AHEither
   context
   (Columns a (Col (Reify context)))
   (Columns b (Col (Reify context)))
 -> AHEither context a b)
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> AHEither
         context
         (Columns a (Col (Reify context)))
         (Columns b (Col (Reify context))))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    SContext context
-> HEitherTable
     (Columns (Columns a (Col (Reify context))))
     (Columns (Columns b (Col (Reify context))))
     (Col (Reify context))
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
forall (context :: Context) a b.
(Table (Reify context) a, Table (Reify context) b) =>
SContext context
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither context a b
sfromColumnsEither SContext context
context (HEitherTable (Columns a) (Columns b) (Col context)
 -> AHEither
      context
      (Columns a (Col (Reify context)))
      (Columns b (Col (Reify context))))
-> (HEitherTable (Columns a) (Columns b) (Col (Reify context))
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    HEitherTable (Columns a) (Columns b) (Col (Reify context))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify


stoColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
  => SContext context
  -> AHEither context a b
  -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
stoColumnsEither :: SContext context
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
stoColumnsEither = \case
  SContext context
SAggregate ->
    HEitherTable (Columns a) (Columns b) (Col context)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify (HEitherTable (Columns a) (Columns b) (Col context)
 -> HEitherTable (Columns a) (Columns b) (Col (Reify context)))
-> (AHEither context a b
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    EitherTable (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (EitherTable
   (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
 -> HEitherTable (Columns a) (Columns b) (Col context))
-> (AHEither context a b
    -> EitherTable
         (Columns a (Col Aggregate)) (Columns b (Col Aggregate)))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (a -> Columns a (Col Aggregate))
-> (b -> Columns b (Col Aggregate))
-> EitherTable a b
-> EitherTable
     (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Aggregate)) -> Columns a (Col Aggregate)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns a (Col (Reify Aggregate)) -> Columns a (Col Aggregate))
-> (a -> Columns a (Col (Reify Aggregate)))
-> a
-> Columns a (Col Aggregate)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Col (Reify Aggregate))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Columns b (Col (Reify Aggregate)) -> Columns b (Col Aggregate)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns b (Col (Reify Aggregate)) -> Columns b (Col Aggregate))
-> (b -> Columns b (Col (Reify Aggregate)))
-> b
-> Columns b (Col Aggregate)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Columns b (Col (Reify Aggregate))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (EitherTable a b
 -> EitherTable
      (Columns a (Col Aggregate)) (Columns b (Col Aggregate)))
-> (AHEither context a b -> EitherTable a b)
-> AHEither context a b
-> EitherTable
     (Columns a (Col Aggregate)) (Columns b (Col Aggregate))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (\(AHEither HEither context a b
a) -> EitherTable a b
HEither context a b
a)
  SContext context
SExpr ->
    HEitherTable (Columns a) (Columns b) (Col context)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify (HEitherTable (Columns a) (Columns b) (Col context)
 -> HEitherTable (Columns a) (Columns b) (Col (Reify context)))
-> (AHEither context a b
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
 -> HEitherTable (Columns a) (Columns b) (Col context))
-> (AHEither context a b
    -> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr)))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (a -> Columns a (Col Expr))
-> (b -> Columns b (Col Expr))
-> EitherTable a b
-> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Expr)) -> Columns a (Col Expr)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns a (Col (Reify Expr)) -> Columns a (Col Expr))
-> (a -> Columns a (Col (Reify Expr))) -> a -> Columns a (Col Expr)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Col (Reify Expr))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Columns b (Col (Reify Expr)) -> Columns b (Col Expr)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns b (Col (Reify Expr)) -> Columns b (Col Expr))
-> (b -> Columns b (Col (Reify Expr))) -> b -> Columns b (Col Expr)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Columns b (Col (Reify Expr))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (EitherTable a b
 -> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr)))
-> (AHEither context a b -> EitherTable a b)
-> AHEither context a b
-> EitherTable (Columns a (Col Expr)) (Columns b (Col Expr))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (\(AHEither HEither context a b
a) -> EitherTable a b
HEither context a b
a)
  SContext context
SResult ->
    HEitherTable (Columns a) (Columns b) (Col context)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify (HEitherTable (Columns a) (Columns b) (Col context)
 -> HEitherTable (Columns a) (Columns b) (Col (Reify context)))
-> (AHEither context a b
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Either (Columns a (Col Result)) (Columns b (Col Result))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (Either (Columns a (Col Result)) (Columns b (Col Result))
 -> HEitherTable (Columns a) (Columns b) (Col context))
-> (AHEither context a b
    -> Either (Columns a (Col Result)) (Columns b (Col Result)))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (a -> Columns a (Col Result))
-> (b -> Columns b (Col Result))
-> Either a b
-> Either (Columns a (Col Result)) (Columns b (Col Result))
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Result)) -> Columns a (Col Result)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns a (Col (Reify Result)) -> Columns a (Col Result))
-> (a -> Columns a (Col (Reify Result)))
-> a
-> Columns a (Col Result)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Col (Reify Result))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Columns b (Col (Reify Result)) -> Columns b (Col Result)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns b (Col (Reify Result)) -> Columns b (Col Result))
-> (b -> Columns b (Col (Reify Result)))
-> b
-> Columns b (Col Result)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Columns b (Col (Reify Result))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Either a b
 -> Either (Columns a (Col Result)) (Columns b (Col Result)))
-> (AHEither context a b -> Either a b)
-> AHEither context a b
-> Either (Columns a (Col Result)) (Columns b (Col Result))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (\(AHEither HEither context a b
a) -> Either a b
HEither context a b
a)
  SContext context
SName ->
    HEitherTable (Columns a) (Columns b) (Col context)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify (HEitherTable (Columns a) (Columns b) (Col context)
 -> HEitherTable (Columns a) (Columns b) (Col (Reify context)))
-> (AHEither context a b
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    EitherTable (Columns a (Col Name)) (Columns b (Col Name))
-> HEitherTable (Columns a) (Columns b) (Col context)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (EitherTable (Columns a (Col Name)) (Columns b (Col Name))
 -> HEitherTable (Columns a) (Columns b) (Col context))
-> (AHEither context a b
    -> EitherTable (Columns a (Col Name)) (Columns b (Col Name)))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (a -> Columns a (Col Name))
-> (b -> Columns b (Col Name))
-> EitherTable a b
-> EitherTable (Columns a (Col Name)) (Columns b (Col Name))
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Columns a (Col (Reify Name)) -> Columns a (Col Name)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns a (Col (Reify Name)) -> Columns a (Col Name))
-> (a -> Columns a (Col (Reify Name))) -> a -> Columns a (Col Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Col (Reify Name))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Columns b (Col (Reify Name)) -> Columns b (Col Name)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns b (Col (Reify Name)) -> Columns b (Col Name))
-> (b -> Columns b (Col (Reify Name))) -> b -> Columns b (Col Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Columns b (Col (Reify Name))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (EitherTable a b
 -> EitherTable (Columns a (Col Name)) (Columns b (Col Name)))
-> (AHEither context a b -> EitherTable a b)
-> AHEither context a b
-> EitherTable (Columns a (Col Name)) (Columns b (Col Name))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (\(AHEither HEither context a b
a) -> EitherTable a b
HEither context a b
a)
  SReify SContext context
context ->
    HEitherTable (Columns a) (Columns b) (Col context)
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify (HEitherTable (Columns a) (Columns b) (Col context)
 -> HEitherTable (Columns a) (Columns b) (Col (Reify context)))
-> (AHEither context a b
    -> HEitherTable (Columns a) (Columns b) (Col context))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    SContext context
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
-> HEitherTable
     (Columns (Columns a (Col (Reify context))))
     (Columns (Columns b (Col (Reify context))))
     (Col (Reify context))
forall (context :: Context) a b.
(Table (Reify context) a, Table (Reify context) b) =>
SContext context
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col (Reify context))
stoColumnsEither SContext context
context (AHEither
   context
   (Columns a (Col (Reify context)))
   (Columns b (Col (Reify context)))
 -> HEitherTable (Columns a) (Columns b) (Col context))
-> (AHEither context a b
    -> AHEither
         context
         (Columns a (Col (Reify context)))
         (Columns b (Col (Reify context))))
-> AHEither context a b
-> HEitherTable (Columns a) (Columns b) (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    SContext context
-> (a -> Columns a (Col (Reify context)))
-> (b -> Columns b (Col (Reify context)))
-> AHEither context a b
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
forall (context :: Context) a c b d.
SContext context
-> (a -> c)
-> (b -> d)
-> AHEither context a b
-> AHEither context c d
sbimapEither SContext context
context (Columns a (Col (Reify (Reify context)))
-> Columns a (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns a (Col (Reify (Reify context)))
 -> Columns a (Col (Reify context)))
-> (a -> Columns a (Col (Reify (Reify context))))
-> a
-> Columns a (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Columns a (Col (Reify (Reify context)))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (Columns b (Col (Reify (Reify context)))
-> Columns b (Col (Reify context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify (Columns b (Col (Reify (Reify context)))
 -> Columns b (Col (Reify context)))
-> (b -> Columns b (Col (Reify (Reify context))))
-> b
-> Columns b (Col (Reify context))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Columns b (Col (Reify (Reify context)))
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns) (AHEither context a b
 -> AHEither
      context
      (Columns a (Col (Reify context)))
      (Columns b (Col (Reify context))))
-> (AHEither context a b -> AHEither context a b)
-> AHEither context a b
-> AHEither
     context
     (Columns a (Col (Reify context)))
     (Columns b (Col (Reify context)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (\(AHEither HEither context a b
a) -> AHEither context a b
HEither context a b
a)