{-# language AllowAmbiguousTypes #-}
{-# language DataKinds #-}
{-# language DefaultSignatures #-}
{-# language DisambiguateRecordFields #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language FunctionalDependencies #-}
{-# language LambdaCase #-}
{-# language NamedFieldPuns #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
{-# language UndecidableInstances #-}

module Rel8.Table
  ( Table (Columns, Context, Unreify, toColumns, fromColumns, reify, unreify)
  , Congruent
  , TTable, TColumns, TContext, TUnreify
  )
where

-- base
import Data.Functor ( ($>) )
import Data.Functor.Identity ( Identity( Identity ) )
import Data.Kind ( Constraint, Type )
import Data.List.NonEmpty ( NonEmpty )
import Data.Proxy ( Proxy( Proxy ) )
import Data.Type.Equality ( (:~:)( Refl ) )
import GHC.Generics ( Generic, Rep, from, to )
import Prelude hiding ( null )

-- rel8
import Rel8.FCF ( Eval, Exp )
import Rel8.Kind.Algebra ( KnownAlgebra )
import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
import Rel8.Generic.Table
  ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
  , GAlgebra
  )
import Rel8.Generic.Record ( Record(..) )
import Rel8.Generic.Reify ( ARep )
import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
import Rel8.Schema.HTable ( HTable )
import Rel8.Schema.HTable.Either ( HEitherTable(..) )
import Rel8.Schema.HTable.Identity ( HIdentity(..), HType )
import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
import Rel8.Schema.HTable.List ( HListTable )
import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
import Rel8.Schema.HTable.These ( HTheseTable(..) )
import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
import qualified Rel8.Schema.Kind as K
import Rel8.Schema.Null ( Sql )
import Rel8.Schema.Reify
  ( Reify, Col( Reify ), hreify, hunreify
  , UnwrapReify
  , notReify
  )
import Rel8.Schema.Result
  ( Col( R ), Result
  , relabel
  , null, nullifier, unnullifier
  , vectorizer, unvectorizer
  )
import Rel8.Schema.Spec ( KnownSpec )
import Rel8.Type ( DBType )
import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ),  MaybeTag( IsJust ) )

-- these
import Data.These ( These( This, That, These ) )
import Data.These.Combinators ( justHere, justThere )


-- | @Table@s are one of the foundational elements of Rel8, and describe data
-- types that have a finite number of columns. Each of these columns contains
-- data under a shared context, and contexts describe how to interpret the
-- metadata about a column to a particular Haskell type. In Rel8, we have
-- contexts for expressions (the 'Rel8.Expr' context), aggregations (the
-- 'Rel8.Aggregate' context), insert values (the 'Rel8.Insert' contex), among
-- others.
--
-- In typical usage of Rel8 you don't need to derive instances of 'Table'
-- yourself, as anything that's an instance of 'Rel8.Rel8able' is always a
-- 'Table'.
type Table :: K.Context -> Type -> Constraint
class (HTable (Columns a), context ~ Context a) => Table context a | a -> context where
  -- | The 'HTable' functor that describes the schema of this table.
  type Columns a :: K.HTable

  -- | The common context that all columns use as an interpretation.
  type Context a :: K.Context

  type Unreify a :: Type

  toColumns :: a -> Columns a (Col context)
  fromColumns :: Columns a (Col context) -> a

  reify :: context :~: Reify ctx -> Unreify a -> a
  unreify :: context :~: Reify ctx -> a -> Unreify a

  type Columns a = Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
  type Context a = Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
  type Unreify a = DefaultUnreify a

  default toColumns ::
    ( Generic (Record a)
    , KnownAlgebra (GAlgebra (Rep (Record a)))
    , Eval (GGTable (GAlgebra (Rep (Record a))) (TTable context) TColumns (Col context) (Rep (Record a)))
    , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
    , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
    )
    => a -> Columns a (Col context)
  toColumns =
    (forall (spec :: Spec).
 (GAlgebra (Rep (Record a)) ~ 'Sum) =>
 Col context spec -> Col Result spec)
-> (forall (spec :: Spec).
    (GAlgebra (Rep (Record a)) ~ 'Sum) =>
    Col Result spec -> Col context spec)
-> (forall a.
    Eval (TTable context a) =>
    a -> Eval (TColumns a) (Col context))
-> GRecord (Rep a) Any
-> Eval
     (GGColumns (GAlgebra (Rep (Record a))) TColumns (GRecord (Rep a)))
     (Col context)
forall (algebra :: Algebra) (_Table :: * -> Exp Constraint)
       (_Columns :: * -> Exp HTable) (rep :: * -> *) (context :: HContext)
       x.
(KnownAlgebra algebra,
 Eval (GGTable algebra _Table _Columns context rep)) =>
(forall (spec :: Spec).
 (algebra ~ 'Sum) =>
 context spec -> Col Result spec)
-> (forall (spec :: Spec).
    (algebra ~ 'Sum) =>
    Col Result spec -> context spec)
-> (forall a. Eval (_Table a) => a -> Eval (_Columns a) context)
-> rep x
-> Eval (GGColumns algebra _Columns rep) context
ggtoColumns
      @(GAlgebra (Rep (Record a)))
      @(TTable context)
      @TColumns
      forall a. a -> a
forall (spec :: Spec).
(GAlgebra (Rep (Record a)) ~ 'Sum) =>
Col context spec -> Col Result spec
id
      forall a. a -> a
forall (spec :: Spec).
(GAlgebra (Rep (Record a)) ~ 'Sum) =>
Col Result spec -> Col context spec
id
      forall a.
Eval (TTable context a) =>
a -> Eval (TColumns a) (Col context)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (GRecord (Rep a) Any -> Columns a (Col context))
-> (a -> GRecord (Rep a) Any) -> a -> Columns a (Col context)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Record a -> GRecord (Rep a) Any
forall a x. Generic a => a -> Rep a x
from (Record a -> GRecord (Rep a) Any)
-> (a -> Record a) -> a -> GRecord (Rep a) Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    a -> Record a
forall a. a -> Record a
Record

  default fromColumns ::
    ( Generic (Record a)
    , KnownAlgebra (GAlgebra (Rep (Record a)))
    , Eval (GGTable (GAlgebra (Rep (Record a))) (TTable context) TColumns (Col context) (Rep (Record a)))
    , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
    , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
    )
    => Columns a (Col context) -> a
  fromColumns =
    Record a -> a
forall a. Record a -> a
unrecord (Record a -> a)
-> (Columns a (Col context) -> Record a)
-> Columns a (Col context)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    GRecord (Rep a) Any -> Record a
forall a x. Generic a => Rep a x -> a
to (GRecord (Rep a) Any -> Record a)
-> (Columns a (Col context) -> GRecord (Rep a) Any)
-> Columns a (Col context)
-> Record a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    (forall (spec :: Spec).
 (GAlgebra (Rep (Record a)) ~ 'Sum) =>
 Col context spec -> Col Result spec)
-> (forall (spec :: Spec).
    (GAlgebra (Rep (Record a)) ~ 'Sum) =>
    Col Result spec -> Col context spec)
-> (forall a.
    Eval (TTable context a) =>
    Eval (TColumns a) (Col context) -> a)
-> Eval
     (GGColumns (GAlgebra (Rep (Record a))) TColumns (GRecord (Rep a)))
     (Col context)
-> GRecord (Rep a) Any
forall (algebra :: Algebra) (_Table :: * -> Exp Constraint)
       (_Columns :: * -> Exp HTable) (rep :: * -> *) (context :: HContext)
       x.
(KnownAlgebra algebra,
 Eval (GGTable algebra _Table _Columns context rep)) =>
(forall (spec :: Spec).
 (algebra ~ 'Sum) =>
 context spec -> Col Result spec)
-> (forall (spec :: Spec).
    (algebra ~ 'Sum) =>
    Col Result spec -> context spec)
-> (forall a. Eval (_Table a) => Eval (_Columns a) context -> a)
-> Eval (GGColumns algebra _Columns rep) context
-> rep x
ggfromColumns
      @(GAlgebra (Rep (Record a)))
      @(TTable context)
      @TColumns
      forall a. a -> a
forall (spec :: Spec).
(GAlgebra (Rep (Record a)) ~ 'Sum) =>
Col context spec -> Col Result spec
id
      forall a. a -> a
forall (spec :: Spec).
(GAlgebra (Rep (Record a)) ~ 'Sum) =>
Col Result spec -> Col context spec
id
      forall a.
Eval (TTable context a) =>
Eval (TColumns a) (Col context) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns

  default reify ::
    ( Generic (Record a)
    , Generic (Record (Unreify a))
    , GMappable (TTable context) (Rep (Record a))
    , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
    )
    => context :~: Reify ctx -> Unreify a -> a
  reify context :~: Reify ctx
Refl =
    Record a -> a
forall a. Record a -> a
unrecord (Record a -> a) -> (Unreify a -> Record a) -> Unreify a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    GRecord (Rep a) Any -> Record a
forall a x. Generic a => Rep a x -> a
to (GRecord (Rep a) Any -> Record a)
-> (Unreify a -> GRecord (Rep a) Any) -> Unreify a -> Record a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Proxy TUnreify
-> (forall a. Eval (TTable context a) => Eval (TUnreify a) -> a)
-> GMap TUnreify (GRecord (Rep a)) Any
-> GRecord (Rep a) Any
forall (constraint :: * -> Exp Constraint) (rep :: * -> *)
       (proxy :: (* -> * -> *) -> *) (f :: * -> * -> *) x.
GMappable constraint rep =>
proxy f
-> (forall a. Eval (constraint a) => Eval (f a) -> a)
-> GMap f rep x
-> rep x
gunmap @(TTable context) (Proxy TUnreify
forall k (t :: k). Proxy t
Proxy @TUnreify) ((Reify ctx :~: Reify ctx) -> Unreify a -> a
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> Unreify a -> a
reify Reify ctx :~: Reify ctx
forall k (a :: k). a :~: a
Refl) (GMap TUnreify (GRecord (Rep a)) Any -> GRecord (Rep a) Any)
-> (Unreify a -> GMap TUnreify (GRecord (Rep a)) Any)
-> Unreify a
-> GRecord (Rep a) Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Record (Unreify a) -> GMap TUnreify (GRecord (Rep a)) Any
forall a x. Generic a => a -> Rep a x
from (Record (Unreify a) -> GMap TUnreify (GRecord (Rep a)) Any)
-> (Unreify a -> Record (Unreify a))
-> Unreify a
-> GMap TUnreify (GRecord (Rep a)) Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Unreify a -> Record (Unreify a)
forall a. a -> Record a
Record

  default unreify ::
    ( Generic (Record a)
    , Generic (Record (Unreify a))
    , GMappable (TTable context) (Rep (Record a))
    , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
    )
    => context :~: Reify ctx -> a -> Unreify a
  unreify context :~: Reify ctx
Refl =
    Record (Unreify a) -> Unreify a
forall a. Record a -> a
unrecord (Record (Unreify a) -> Unreify a)
-> (a -> Record (Unreify a)) -> a -> Unreify a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    GRecord (Rep (Unreify a)) Any -> Record (Unreify a)
forall a x. Generic a => Rep a x -> a
to (GRecord (Rep (Unreify a)) Any -> Record (Unreify a))
-> (a -> GRecord (Rep (Unreify a)) Any) -> a -> Record (Unreify a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Proxy TUnreify
-> (forall a. Eval (TTable context a) => a -> Eval (TUnreify a))
-> GRecord (Rep a) Any
-> GMap TUnreify (GRecord (Rep a)) Any
forall (constraint :: * -> Exp Constraint) (rep :: * -> *)
       (proxy :: (* -> * -> *) -> *) (f :: * -> * -> *) x.
GMappable constraint rep =>
proxy f
-> (forall a. Eval (constraint a) => a -> Eval (f a))
-> rep x
-> GMap f rep x
gmap @(TTable context) (Proxy TUnreify
forall k (t :: k). Proxy t
Proxy @TUnreify) ((Reify ctx :~: Reify ctx) -> a -> Unreify a
forall (context :: Context) a (ctx :: Context).
Table context a =>
(context :~: Reify ctx) -> a -> Unreify a
unreify Reify ctx :~: Reify ctx
forall k (a :: k). a :~: a
Refl) (GRecord (Rep a) Any -> GRecord (Rep (Unreify a)) Any)
-> (a -> GRecord (Rep a) Any) -> a -> GRecord (Rep (Unreify a)) Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Record a -> GRecord (Rep a) Any
forall a x. Generic a => a -> Rep a x
from (Record a -> GRecord (Rep a) Any)
-> (a -> Record a) -> a -> GRecord (Rep a) Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    a -> Record a
forall a. a -> Record a
Record


data TTable :: K.Context -> Type -> Exp Constraint
type instance Eval (TTable context a) = Table context a


data TColumns :: Type -> Exp K.HTable
type instance Eval (TColumns a) = Columns a


data TContext :: Type -> Exp K.Context
type instance Eval (TContext a) = Context a


data TUnreify :: Type -> Exp Type
type instance Eval (TUnreify a) = Unreify a


type DefaultUnreify :: Type -> Type
type family DefaultUnreify a where
  DefaultUnreify (t a b c d e f g) =
    t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f) (Unreify g)
  DefaultUnreify (t a b c d e f) =
    t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f)
  DefaultUnreify (t a b c d e) =
    t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e)
  DefaultUnreify (t a b c d) =
    t (Unreify a) (Unreify b) (Unreify c) (Unreify d)
  DefaultUnreify (t a b c) = t (Unreify a) (Unreify b) (Unreify c)
  DefaultUnreify (t a b) = t (Unreify a) (Unreify b)
  DefaultUnreify (t a) = t (Unreify a)
  DefaultUnreify a = ARep (GMap TUnreify (Rep a))


-- | Any 'HTable' is also a 'Table'.
instance HTable t => Table context (t (Col context)) where
  type Columns (t (Col context)) = t
  type Context (t (Col context)) = context
  type Unreify (t (Col context)) = t (Col (UnwrapReify context))

  toColumns :: t (Col context) -> Columns (t (Col context)) (Col context)
toColumns = t (Col context) -> Columns (t (Col context)) (Col context)
forall a. a -> a
id
  fromColumns :: Columns (t (Col context)) (Col context) -> t (Col context)
fromColumns = Columns (t (Col context)) (Col context) -> t (Col context)
forall a. a -> a
id

  reify :: (context :~: Reify ctx)
-> Unreify (t (Col context)) -> t (Col context)
reify context :~: Reify ctx
Refl = Unreify (t (Col context)) -> t (Col context)
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col context) -> t (Col (Reify context))
hreify
  unreify :: (context :~: Reify ctx)
-> t (Col context) -> Unreify (t (Col context))
unreify context :~: Reify ctx
Refl = t (Col context) -> Unreify (t (Col context))
forall (t :: HTable) (context :: Context).
HTable t =>
t (Col (Reify context)) -> t (Col context)
hunreify


-- | Any context is trivially a table.
instance KnownSpec spec => Table context (Col context spec) where
  type Columns (Col context spec) = HIdentity spec
  type Context (Col context spec) = context
  type Unreify (Col context spec) = Col (UnwrapReify context) spec

  toColumns :: Col context spec -> Columns (Col context spec) (Col context)
toColumns = Col context spec -> Columns (Col context spec) (Col context)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity
  fromColumns :: Columns (Col context spec) (Col context) -> Col context spec
fromColumns = Columns (Col context spec) (Col context) -> Col context spec
forall (spec :: Spec) (context :: HContext).
HIdentity spec context -> context spec
unHIdentity

  reify :: (context :~: Reify ctx)
-> Unreify (Col context spec) -> Col context spec
reify context :~: Reify ctx
Refl = Unreify (Col context spec) -> Col context spec
forall (context :: Context) (spec :: Spec).
Col context spec -> Col (Reify context) spec
Reify
  unreify :: (context :~: Reify ctx)
-> Col context spec -> Unreify (Col context spec)
unreify context :~: Reify ctx
Refl (Reify a) = Col ctx spec
Unreify (Col context spec)
a


instance Sql DBType a => Table Result (Identity a) where
  type Columns (Identity a) = HType a
  type Context (Identity a) = Result

  toColumns :: Identity a -> Columns (Identity a) (Col Result)
toColumns (Identity a
a) = Col Result ('Spec '[] 'Required a) -> HType a (Col Result)
forall (context :: HContext) a.
context ('Spec '[] 'Required a) -> HType a context
HType (a -> Col Result ('Spec '[] 'Required a)
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R a
a)
  fromColumns :: Columns (Identity a) (Col Result) -> Identity a
fromColumns (HType (R a)) = a -> Identity a
forall a. a -> Identity a
Identity a
a

  reify :: (Result :~: Reify ctx) -> Unreify (Identity a) -> Identity a
reify = (Result :~: Reify ctx) -> Unreify (Identity a) -> Identity a
forall (context :: Context) (ctx :: Context) a.
NotReify context =>
(context :~: Reify ctx) -> a
notReify
  unreify :: (Result :~: Reify ctx) -> Identity a -> Unreify (Identity a)
unreify = (Result :~: Reify ctx) -> Identity a -> Unreify (Identity a)
forall (context :: Context) (ctx :: Context) a.
NotReify context =>
(context :~: Reify ctx) -> a
notReify


instance (Table Result a, Table Result b) => Table Result (Either a b) where
  type Columns (Either a b) = HEitherTable (Columns a) (Columns b)
  type Context (Either a b) = Result

  toColumns :: Either a b -> Columns (Either a b) (Col Result)
toColumns = \case
    Left a
table -> HEitherTable :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HIdentity ('Spec '["isRight"] 'Required EitherTag) context
-> HLabel "Left" (HNullify left) context
-> HLabel "Right" (HNullify right) context
-> HEitherTable left right context
HEitherTable
      { htag :: HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Result)
htag = Col Result ('Spec '["isRight"] 'Required EitherTag)
-> HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Result)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (EitherTag -> Col Result ('Spec '["isRight"] 'Required EitherTag)
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R EitherTag
IsLeft)
      , hleft :: HLabel "Left" (HNullify (Columns a)) (Col Result)
hleft = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Left" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Left" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Left" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> Columns a (Col Result) -> HNullify (Columns a) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> t context -> HNullify t context
hnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
nullifier (a -> Columns a (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns a
table))
      , hright :: HLabel "Right" (HNullify (Columns b)) (Col Result)
hright = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Right" : labels) necessity a))
-> HNullify (Columns b) (Col Result)
-> HLabel "Right" (HNullify (Columns b)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Right" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> HNullify (Columns b) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> HNullify t context
hnulls (Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
-> SSpec ('Spec labels necessity a)
-> Col
     Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall a b. a -> b -> a
const Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity (Maybe a))
null))
      }
    Right b
table -> HEitherTable :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HIdentity ('Spec '["isRight"] 'Required EitherTag) context
-> HLabel "Left" (HNullify left) context
-> HLabel "Right" (HNullify right) context
-> HEitherTable left right context
HEitherTable
      { htag :: HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Result)
htag = Col Result ('Spec '["isRight"] 'Required EitherTag)
-> HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Result)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (EitherTag -> Col Result ('Spec '["isRight"] 'Required EitherTag)
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R EitherTag
IsRight)
      , hleft :: HLabel "Left" (HNullify (Columns a)) (Col Result)
hleft = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Left" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Left" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Left" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> HNullify t context
hnulls (Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
-> SSpec ('Spec labels necessity a)
-> Col
     Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall a b. a -> b -> a
const Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity (Maybe a))
null))
      , hright :: HLabel "Right" (HNullify (Columns b)) (Col Result)
hright = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Right" : labels) necessity a))
-> HNullify (Columns b) (Col Result)
-> HLabel "Right" (HNullify (Columns b)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Right" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> Columns b (Col Result) -> HNullify (Columns b) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> t context -> HNullify t context
hnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
nullifier (b -> Columns b (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns b
table))
      }

  fromColumns :: Columns (Either a b) (Col Result) -> Either a b
fromColumns HEitherTable {htag, hleft, hright} = case HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Result)
htag of
    HIdentity (R tag) -> case a
tag of
      a
IsLeft -> Either a b
-> (Columns a (Col Result) -> Either a b)
-> Maybe (Columns a (Col Result))
-> Either a b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either a b
forall a. a
err (a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b)
-> (Columns a (Col Result) -> a)
-> Columns a (Col Result)
-> Either a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns) (Maybe (Columns a (Col Result)) -> Either a b)
-> Maybe (Columns a (Col Result)) -> Either a b
forall a b. (a -> b) -> a -> b
$ (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a))
 -> Maybe (Col Result ('Spec labels necessity a)))
-> HNullify (Columns a) (Col Result)
-> Maybe (Columns a (Col Result))
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a))
 -> m (context ('Spec labels necessity a)))
-> HNullify t context -> m (t context)
hunnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
-> Maybe (Col Result ('Spec labels necessity a))
unnullifier ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Left" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Left" (HNullify (Columns a)) (Col Result)
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Left" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler HLabel "Left" (HNullify (Columns a)) (Col Result)
hleft)
      a
IsRight -> Either a b
-> (Columns b (Col Result) -> Either a b)
-> Maybe (Columns b (Col Result))
-> Either a b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either a b
forall a. a
err (b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b)
-> (Columns b (Col Result) -> b)
-> Columns b (Col Result)
-> Either a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns b (Col Result) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns) (Maybe (Columns b (Col Result)) -> Either a b)
-> Maybe (Columns b (Col Result)) -> Either a b
forall a b. (a -> b) -> a -> b
$ (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a))
 -> Maybe (Col Result ('Spec labels necessity a)))
-> HNullify (Columns b) (Col Result)
-> Maybe (Columns b (Col Result))
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a))
 -> m (context ('Spec labels necessity a)))
-> HNullify t context -> m (t context)
hunnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
-> Maybe (Col Result ('Spec labels necessity a))
unnullifier ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Right" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Right" (HNullify (Columns b)) (Col Result)
-> HNullify (Columns b) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Right" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler HLabel "Right" (HNullify (Columns b)) (Col Result)
hright)
    where
      err :: a
err = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Either.fromColumns: mismatch between tag and data"


instance Table Result a => Table Result [a] where
  type Columns [a] = HListTable (Columns a)
  type Context [a] = Result

  toColumns :: [a] -> Columns [a] (Col Result)
toColumns = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> [Col Result ('Spec labels necessity a)]
 -> Col Result ('Spec labels 'Required [a]))
-> [Columns a (Col Result)]
-> HVectorize [] (Columns a) (Col Result)
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Unzip f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> f (context ('Spec labels necessity a))
 -> context' ('Spec labels 'Required (list a)))
-> f (t context) -> HVectorize list t context'
hvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> [Col Result ('Spec labels necessity a)]
-> Col Result ('Spec labels 'Required [a])
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> f (Col Result ('Spec labels necessity a))
-> Col Result ('Spec labels 'Required (f a))
vectorizer ([Columns a (Col Result)]
 -> HVectorize [] (Columns a) (Col Result))
-> ([a] -> [Columns a (Col Result)])
-> [a]
-> HVectorize [] (Columns a) (Col Result)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Columns a (Col Result)) -> [a] -> [Columns a (Col Result)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Columns a (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns
  fromColumns :: Columns [a] (Col Result) -> [a]
fromColumns = (Columns a (Col Result) -> a) -> [Columns a (Col Result)] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns ([Columns a (Col Result)] -> [a])
-> (HVectorize [] (Columns a) (Col Result)
    -> [Columns a (Col Result)])
-> HVectorize [] (Columns a) (Col Result)
-> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels 'Required [a])
 -> [Col Result ('Spec labels necessity a)])
-> HVectorize [] (Columns a) (Col Result)
-> [Columns a (Col Result)]
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Zip f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels 'Required (list a))
 -> f (context' ('Spec labels necessity a)))
-> HVectorize list t context -> f (t context')
hunvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels 'Required [a])
-> [Col Result ('Spec labels necessity a)]
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels 'Required (f a))
-> f (Col Result ('Spec labels necessity a))
unvectorizer


instance Table Result a => Table Result (Maybe a) where
  type Columns (Maybe a) = HMaybeTable (Columns a)
  type Context (Maybe a) = Result

  toColumns :: Maybe a -> Columns (Maybe a) (Col Result)
toColumns = \case
    Maybe a
Nothing -> HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HLabel "Just" (HNullify table) context
-> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
htag = Col Result ('Spec '["isJust"] 'Required (Maybe MaybeTag))
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (Maybe MaybeTag
-> Col Result ('Spec '["isJust"] 'Required (Maybe MaybeTag))
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R Maybe MaybeTag
forall a. Maybe a
Nothing)
      , hjust :: HLabel "Just" (HNullify (Columns a)) (Col Result)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Just" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Just" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Just" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> HNullify t context
hnulls (Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
-> SSpec ('Spec labels necessity a)
-> Col
     Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall a b. a -> b -> a
const Col
  Result ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity (Maybe a))
null))
      }
    Just a
table -> HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HLabel "Just" (HNullify table) context
-> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
htag = Col Result ('Spec '["isJust"] 'Required (Maybe MaybeTag))
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (Maybe MaybeTag
-> Col Result ('Spec '["isJust"] 'Required (Maybe MaybeTag))
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R (MaybeTag -> Maybe MaybeTag
forall a. a -> Maybe a
Just MaybeTag
IsJust))
      , hjust :: HLabel "Just" (HNullify (Columns a)) (Col Result)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Just" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Just" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Just" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a)))
-> Columns a (Col Result) -> HNullify (Columns a) (Col Result)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a)))
-> t context -> HNullify t context
hnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
nullifier (a -> Columns a (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns a
table))
      }

  fromColumns :: Columns (Maybe a) (Col Result) -> Maybe a
fromColumns HMaybeTable {htag, hjust} = case HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
htag of
    HIdentity (R tag) -> a
Maybe MaybeTag
tag Maybe MaybeTag -> a -> Maybe a
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$>
      case (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels necessity (Nullify a))
 -> Maybe (Col Result ('Spec labels necessity a)))
-> HNullify (Columns a) (Col Result)
-> Maybe (Columns a (Col Result))
forall (t :: HTable) (m :: * -> *) (context :: HContext).
(HTable t, Apply m) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (Nullify a))
 -> m (context ('Spec labels necessity a)))
-> HNullify t context -> m (t context)
hunnullify forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels necessity (Nullify a))
-> Maybe (Col Result ('Spec labels necessity a))
unnullifier ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Just" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Just" (HNullify (Columns a)) (Col Result)
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Just" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler HLabel "Just" (HNullify (Columns a)) (Col Result)
hjust) of
        Maybe (Columns a (Col Result))
Nothing -> [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Maybe.fromColumns: mismatch between tag and data"
        Just Columns a (Col Result)
just -> Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns Columns a (Col Result)
just


instance Table Result a => Table Result (NonEmpty a) where
  type Columns (NonEmpty a) = HNonEmptyTable (Columns a)
  type Context (NonEmpty a) = Result

  toColumns :: NonEmpty a -> Columns (NonEmpty a) (Col Result)
toColumns = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> NonEmpty (Col Result ('Spec labels necessity a))
 -> Col Result ('Spec labels 'Required (NonEmpty a)))
-> NonEmpty (Columns a (Col Result))
-> HVectorize NonEmpty (Columns a) (Col Result)
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Unzip f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> f (context ('Spec labels necessity a))
 -> context' ('Spec labels 'Required (list a)))
-> f (t context) -> HVectorize list t context'
hvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> NonEmpty (Col Result ('Spec labels necessity a))
-> Col Result ('Spec labels 'Required (NonEmpty a))
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> f (Col Result ('Spec labels necessity a))
-> Col Result ('Spec labels 'Required (f a))
vectorizer (NonEmpty (Columns a (Col Result))
 -> HVectorize NonEmpty (Columns a) (Col Result))
-> (NonEmpty a -> NonEmpty (Columns a (Col Result)))
-> NonEmpty a
-> HVectorize NonEmpty (Columns a) (Col Result)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Columns a (Col Result))
-> NonEmpty a -> NonEmpty (Columns a (Col Result))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Columns a (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns
  fromColumns :: Columns (NonEmpty a) (Col Result) -> NonEmpty a
fromColumns = (Columns a (Col Result) -> a)
-> NonEmpty (Columns a (Col Result)) -> NonEmpty a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns (NonEmpty (Columns a (Col Result)) -> NonEmpty a)
-> (HVectorize NonEmpty (Columns a) (Col Result)
    -> NonEmpty (Columns a (Col Result)))
-> HVectorize NonEmpty (Columns a) (Col Result)
-> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Result ('Spec labels 'Required (NonEmpty a))
 -> NonEmpty (Col Result ('Spec labels necessity a)))
-> HVectorize NonEmpty (Columns a) (Col Result)
-> NonEmpty (Columns a (Col Result))
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Zip f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels 'Required (list a))
 -> f (context' ('Spec labels necessity a)))
-> HVectorize list t context -> f (t context')
hunvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels 'Required (NonEmpty a))
-> NonEmpty (Col Result ('Spec labels necessity a))
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> Col Result ('Spec labels 'Required (f a))
-> f (Col Result ('Spec labels necessity a))
unvectorizer


instance (Table Result a, Table Result b) => Table Result (These a b) where
  type Columns (These a b) = HTheseTable (Columns a) (Columns b)
  type Context (These a b) = Result

  toColumns :: These a b -> Columns (These a b) (Col Result)
toColumns These a b
tables = HTheseTable :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HIdentity ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) context
-> HLabel "Here" (HNullify here) context
-> HIdentity
     ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) context
-> HLabel "There" (HNullify there) context
-> HTheseTable here there context
HTheseTable
    { hhereTag :: HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Result)
hhereTag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
-> HIdentity
     ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Result)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Result)
-> HIdentity ('Spec relabels necessity a) (Col Result)
relabel HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
hhereTag
    , hhere :: HLabel "Here" (HNullify (Columns a)) (Col Result)
hhere = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Here" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Here" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Here" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Just" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Just" (HNullify (Columns a)) (Col Result)
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Just" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler (HLabel "Just" (HNullify (Columns a)) (Col Result)
-> Columns
     (HLabel "Just" (HNullify (Columns a)) (Col Result)) (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns HLabel "Just" (HNullify (Columns a)) (Col Result)
hhere))
    , hthereTag :: HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Result)
hthereTag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
-> HIdentity
     ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Result)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Result)
-> HIdentity ('Spec relabels necessity a) (Col Result)
relabel HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
hthereTag
    , hthere :: HLabel "There" (HNullify (Columns b)) (Col Result)
hthere = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("There" : labels) necessity a))
-> HNullify (Columns b) (Col Result)
-> HLabel "There" (HNullify (Columns b)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("There" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Just" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Just" (HNullify (Columns b)) (Col Result)
-> HNullify (Columns b) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Just" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler (HLabel "Just" (HNullify (Columns b)) (Col Result)
-> Columns
     (HLabel "Just" (HNullify (Columns b)) (Col Result)) (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns HLabel "Just" (HNullify (Columns b)) (Col Result)
hthere))
    }
    where
      HMaybeTable
        { htag :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context
-> HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
htag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
hhereTag
        , hjust :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context -> HLabel "Just" (HNullify table) context
hjust = HLabel "Just" (HNullify (Columns a)) (Col Result)
hhere
        } = Maybe a -> Columns (Maybe a) (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (These a b -> Maybe a
forall a b. These a b -> Maybe a
justHere These a b
tables)
      HMaybeTable
        { htag :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context
-> HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
htag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
hthereTag
        , hjust :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context -> HLabel "Just" (HNullify table) context
hjust = HLabel "Just" (HNullify (Columns b)) (Col Result)
hthere
        } = Maybe b -> Columns (Maybe b) (Col Result)
forall (context :: Context) a.
Table context a =>
a -> Columns a (Col context)
toColumns (These a b -> Maybe b
forall a b. These a b -> Maybe b
justThere These a b
tables)

  fromColumns :: Columns (These a b) (Col Result) -> These a b
fromColumns HTheseTable {hhereTag, hhere, hthereTag, hthere} =
    case (Columns (Maybe (Columns a (Col Result))) (Col Result)
-> Maybe (Columns a (Col Result))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns HMaybeTable (Columns a) (Col Result)
Columns (Maybe (Columns a (Col Result))) (Col Result)
mhere, Columns (Maybe (Columns b (Col Result))) (Col Result)
-> Maybe (Columns b (Col Result))
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns HMaybeTable (Columns b) (Col Result)
Columns (Maybe (Columns b (Col Result))) (Col Result)
mthere) of
      (Just Columns a (Col Result)
a, Maybe (Columns b (Col Result))
Nothing) -> a -> These a b
forall a b. a -> These a b
This (Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns Columns a (Col Result)
a)
      (Maybe (Columns a (Col Result))
Nothing, Just Columns b (Col Result)
b) -> b -> These a b
forall a b. b -> These a b
That (Columns b (Col Result) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns Columns b (Col Result)
b)
      (Just Columns a (Col Result)
a, Just Columns b (Col Result)
b) -> a -> b -> These a b
forall a b. a -> b -> These a b
These (Columns a (Col Result) -> a
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns Columns a (Col Result)
a) (Columns b (Col Result) -> b
forall (context :: Context) a.
Table context a =>
Columns a (Col context) -> a
fromColumns Columns b (Col Result)
b)
      (Maybe (Columns a (Col Result)), Maybe (Columns b (Col Result)))
_ -> [Char] -> These a b
forall a. HasCallStack => [Char] -> a
error [Char]
"These.fromColumns: mismatch between tags and data"
    where
      mhere :: HMaybeTable (Columns a) (Col Result)
mhere = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HLabel "Just" (HNullify table) context
-> HMaybeTable table context
HMaybeTable
        { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
htag = HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Result)
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Result)
-> HIdentity ('Spec relabels necessity a) (Col Result)
relabel HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Result)
hhereTag
        , hjust :: HLabel "Just" (HNullify (Columns a)) (Col Result)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Just" : labels) necessity a))
-> HNullify (Columns a) (Col Result)
-> HLabel "Just" (HNullify (Columns a)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Just" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("Here" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "Here" (HNullify (Columns a)) (Col Result)
-> HNullify (Columns a) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("Here" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler HLabel "Here" (HNullify (Columns a)) (Col Result)
hhere)
        }
      mthere :: HMaybeTable (Columns b) (Col Result)
mthere = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HLabel "Just" (HNullify table) context
-> HMaybeTable table context
HMaybeTable
        { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
htag = HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Result)
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Result)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Result)
-> HIdentity ('Spec relabels necessity a) (Col Result)
relabel HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Result)
hthereTag
        , hjust :: HLabel "Just" (HNullify (Columns b)) (Col Result)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec labels necessity a)
 -> Col Result ('Spec ("Just" : labels) necessity a))
-> HNullify (Columns b) (Col Result)
-> HLabel "Just" (HNullify (Columns b)) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity a)
 -> context ('Spec (label : labels) necessity a))
-> t context -> HLabel label t context
hlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec labels necessity a)
-> Col Result ('Spec ("Just" : labels) necessity a)
forall (context :: Context) (labels :: Labels)
       (necessity :: Necessity) a (label :: Symbol).
Labelable context =>
Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
labeler ((forall (labels :: Labels) (necessity :: Necessity) a.
 Col Result ('Spec ("There" : labels) necessity a)
 -> Col Result ('Spec labels necessity a))
-> HLabel "There" (HNullify (Columns b)) (Col Result)
-> HNullify (Columns b) (Col Result)
forall (t :: HTable) (label :: Symbol) (context :: HContext).
(HTable t, KnownSymbol label) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec (label : labels) necessity a)
 -> context ('Spec labels necessity a))
-> HLabel label t context -> t context
hunlabel forall (labels :: Labels) (necessity :: Necessity) a.
Col Result ('Spec ("There" : labels) necessity a)
-> Col Result ('Spec labels necessity a)
forall (context :: Context) (label :: Symbol) (labels :: Labels)
       (necessity :: Necessity) a.
Labelable context =>
Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
unlabeler HLabel "There" (HNullify (Columns b)) (Col Result)
hthere)
        }


instance (Table context a, Table context b, Labelable context)
  => Table context (a, b)


instance
  ( Table context a, Table context b, Table context c
  , Labelable context
  )
  => Table context (a, b, c)


instance
  ( Table context a, Table context b, Table context c, Table context d
  , Labelable context
  )
  => Table context (a, b, c, d)


instance
  ( Table context a, Table context b, Table context c, Table context d
  , Table context e
  , Labelable context
  )
  => Table context (a, b, c, d, e)


instance
  ( Table context a, Table context b, Table context c, Table context d
  , Table context e, Table context f
  , Labelable context
  )
  => Table context (a, b, c, d, e, f)


instance
  ( Table context a, Table context b, Table context c, Table context d
  , Table context e, Table context f, Table context g
  , Labelable context
  )
  => Table context (a, b, c, d, e, f, g)


type Congruent :: Type -> Type -> Constraint
class Columns a ~ Columns b => Congruent a b
instance Columns a ~ Columns b => Congruent a b