{-# language DataKinds #-}
{-# language DisambiguateRecordFields #-}
{-# language NamedFieldPuns #-}
{-# language TypeFamilies #-}

module Rel8.Schema.Context.Identity
  ( fromHEitherTable, toHEitherTable
  , fromHListTable, toHListTable
  , fromHMaybeTable, toHMaybeTable
  , fromHNonEmptyTable, toHNonEmptyTable
  , fromHTheseTable, toHTheseTable
  )
where

-- base
import Data.Functor ( ($>) )
import Data.Functor.Identity ( Identity )
import Data.List.NonEmpty ( NonEmpty )
import Prelude hiding ( null )

-- rel8
import Rel8.Schema.Context ( Col( Result ) )
import Rel8.Schema.HTable ( HTable )
import Rel8.Schema.HTable.Either ( HEitherTable(..) )
import Rel8.Schema.HTable.Identity ( HIdentity(..) )
import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
import Rel8.Schema.HTable.List ( HListTable )
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 Rel8.Schema.Null ( Nullify, Nullity( Null, NotNull ) )
import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ),  MaybeTag( IsJust ) )

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


toHEitherTable :: (HTable t, HTable u)
  => Either (t (Col Identity)) (u (Col Identity))
  -> HEitherTable t u (Col Identity)
toHEitherTable :: forall (t :: HTable) (u :: HTable).
(HTable t, HTable u) =>
Either (t (Col Identity)) (u (Col Identity))
-> HEitherTable t u (Col Identity)
toHEitherTable = (t (Col Identity) -> HEitherTable t u (Col Identity))
-> (u (Col Identity) -> HEitherTable t u (Col Identity))
-> Either (t (Col Identity)) (u (Col Identity))
-> HEitherTable t u (Col Identity)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either t (Col Identity) -> HEitherTable t u (Col Identity)
forall {left :: HTable} {right :: HTable}.
(HTable left, HTable right) =>
left (Col Identity) -> HEitherTable left right (Col Identity)
hleft u (Col Identity) -> HEitherTable t u (Col Identity)
forall {left :: HTable} {right :: HTable}.
(HTable left, HTable right) =>
right (Col Identity) -> HEitherTable left right (Col Identity)
hright
  where
    hleft :: left (Col Identity) -> HEitherTable left right (Col Identity)
hleft left (Col Identity)
table = HEitherTable :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HIdentity ('Spec '["isRight"] 'Required EitherTag) context
-> HNullify left context
-> HNullify right context
-> HEitherTable left right context
HEitherTable
      { htag :: HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
htag = Col Identity ('Spec '["isRight"] 'Required EitherTag)
-> HIdentity
     ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (EitherTag -> Col Identity ('Spec '["isRight"] 'Required EitherTag)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result EitherTag
IsLeft)
      , hleft :: HNullify left (Col Identity)
hleft = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a)))
-> left (Col Identity) -> HNullify left (Col Identity)
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 Identity ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (Nullify a))
nullifier left (Col Identity)
table
      , hright :: HNullify right (Col Identity)
hright = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Identity ('Spec labels necessity (Maybe a)))
-> HNullify right (Col Identity)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity (Maybe a)))
-> HNullify t context
hnulls forall (labels :: Labels) (necessity :: Necessity) a.
Col Identity ('Spec labels necessity (Maybe a))
null
      }
    hright :: right (Col Identity) -> HEitherTable left right (Col Identity)
hright right (Col Identity)
table = HEitherTable :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HIdentity ('Spec '["isRight"] 'Required EitherTag) context
-> HNullify left context
-> HNullify right context
-> HEitherTable left right context
HEitherTable
      { htag :: HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
htag = Col Identity ('Spec '["isRight"] 'Required EitherTag)
-> HIdentity
     ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (EitherTag -> Col Identity ('Spec '["isRight"] 'Required EitherTag)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result EitherTag
IsRight)
      , hleft :: HNullify left (Col Identity)
hleft = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Identity ('Spec labels necessity (Maybe a)))
-> HNullify left (Col Identity)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity (Maybe a)))
-> HNullify t context
hnulls forall (labels :: Labels) (necessity :: Necessity) a.
Col Identity ('Spec labels necessity (Maybe a))
null
      , hright :: HNullify right (Col Identity)
hright = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a)))
-> right (Col Identity) -> HNullify right (Col Identity)
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 Identity ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (Nullify a))
nullifier right (Col Identity)
table
      }
{-# INLINABLE toHEitherTable #-}


fromHEitherTable :: (HTable t, HTable u)
  => HEitherTable t u (Col Identity)
  -> Either (t (Col Identity)) (u (Col Identity))
fromHEitherTable :: forall (t :: HTable) (u :: HTable).
(HTable t, HTable u) =>
HEitherTable t u (Col Identity)
-> Either (t (Col Identity)) (u (Col Identity))
fromHEitherTable HEitherTable {HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
htag :: HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
htag :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HEitherTable left right context
-> HIdentity ('Spec '["isRight"] 'Required EitherTag) context
htag, HNullify t (Col Identity)
hleft :: HNullify t (Col Identity)
hleft :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HEitherTable left right context -> HNullify left context
hleft, HNullify u (Col Identity)
hright :: HNullify u (Col Identity)
hright :: forall (left :: HTable) (right :: HTable) (context :: HContext).
HEitherTable left right context -> HNullify right context
hright} = case HIdentity ('Spec '["isRight"] 'Required EitherTag) (Col Identity)
htag of
  HIdentity (Result a
tag) -> case a
tag of
    a
EitherTag
IsLeft -> Either (t (Col Identity)) (u (Col Identity))
-> (t (Col Identity)
    -> Either (t (Col Identity)) (u (Col Identity)))
-> Maybe (t (Col Identity))
-> Either (t (Col Identity)) (u (Col Identity))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either (t (Col Identity)) (u (Col Identity))
forall {a}. a
err t (Col Identity) -> Either (t (Col Identity)) (u (Col Identity))
forall a b. a -> Either a b
Left (Maybe (t (Col Identity))
 -> Either (t (Col Identity)) (u (Col Identity)))
-> Maybe (t (Col Identity))
-> Either (t (Col Identity)) (u (Col Identity))
forall a b. (a -> b) -> a -> b
$ (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a))
 -> Maybe (Col Identity ('Spec labels necessity a)))
-> HNullify t (Col Identity) -> Maybe (t (Col Identity))
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 Identity ('Spec labels necessity (Nullify a))
-> Maybe (Col Identity ('Spec labels necessity a))
unnullifier HNullify t (Col Identity)
hleft
    a
EitherTag
IsRight -> Either (t (Col Identity)) (u (Col Identity))
-> (u (Col Identity)
    -> Either (t (Col Identity)) (u (Col Identity)))
-> Maybe (u (Col Identity))
-> Either (t (Col Identity)) (u (Col Identity))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either (t (Col Identity)) (u (Col Identity))
forall {a}. a
err u (Col Identity) -> Either (t (Col Identity)) (u (Col Identity))
forall a b. b -> Either a b
Right (Maybe (u (Col Identity))
 -> Either (t (Col Identity)) (u (Col Identity)))
-> Maybe (u (Col Identity))
-> Either (t (Col Identity)) (u (Col Identity))
forall a b. (a -> b) -> a -> b
$ (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a))
 -> Maybe (Col Identity ('Spec labels necessity a)))
-> HNullify u (Col Identity) -> Maybe (u (Col Identity))
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 Identity ('Spec labels necessity (Nullify a))
-> Maybe (Col Identity ('Spec labels necessity a))
unnullifier HNullify u (Col Identity)
hright
  where
    err :: a
err = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"fromHEitherTable: mismatch between tag and data"
{-# INLINABLE fromHEitherTable #-}


toHListTable :: HTable t => [t (Col Identity)] -> HListTable t (Col Identity)
toHListTable :: forall (t :: HTable).
HTable t =>
[t (Col Identity)] -> HListTable t (Col Identity)
toHListTable = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> [Col Identity ('Spec labels necessity a)]
 -> Col Identity ('Spec labels necessity [a]))
-> [t (Col Identity)] -> HVectorize [] t (Col Identity)
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 necessity (list a)))
-> f (t context) -> HVectorize list t context'
hvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> [Col Identity ('Spec labels necessity a)]
-> Col Identity ('Spec labels necessity [a])
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> f (Col Identity ('Spec labels necessity a))
-> Col Identity ('Spec labels necessity (f a))
vectorizer
{-# INLINABLE toHListTable #-}


fromHListTable :: HTable t => HListTable t (Col Identity) -> [t (Col Identity)]
fromHListTable :: forall (t :: HTable).
HTable t =>
HListTable t (Col Identity) -> [t (Col Identity)]
fromHListTable = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity [a])
 -> [Col Identity ('Spec labels necessity a)])
-> HVectorize [] t (Col Identity) -> [t (Col Identity)]
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Repeat f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (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 Identity ('Spec labels necessity [a])
-> [Col Identity ('Spec labels necessity a)]
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (f a))
-> f (Col Identity ('Spec labels necessity a))
unvectorizer
{-# INLINABLE fromHListTable #-}


toHMaybeTable :: HTable t => Maybe (t (Col Identity)) -> HMaybeTable t (Col Identity)
toHMaybeTable :: forall (t :: HTable).
HTable t =>
Maybe (t (Col Identity)) -> HMaybeTable t (Col Identity)
toHMaybeTable = HMaybeTable t (Col Identity)
-> (t (Col Identity) -> HMaybeTable t (Col Identity))
-> Maybe (t (Col Identity))
-> HMaybeTable t (Col Identity)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe HMaybeTable t (Col Identity)
hnothing t (Col Identity) -> HMaybeTable t (Col Identity)
forall {table :: HTable}.
HTable table =>
table (Col Identity) -> HMaybeTable table (Col Identity)
hjust
  where
    hnothing :: HMaybeTable t (Col Identity)
hnothing = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HNullify table context -> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag = Col Identity ('Spec '["isJust"] 'Required (Maybe MaybeTag))
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (Maybe MaybeTag
-> Col Identity ('Spec '["isJust"] 'Required (Maybe MaybeTag))
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result Maybe MaybeTag
forall a. Maybe a
Nothing)
      , hjust :: HNullify t (Col Identity)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 Col Identity ('Spec labels necessity (Maybe a)))
-> HNullify t (Col Identity)
forall (t :: HTable) (context :: HContext).
HTable t =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 context ('Spec labels necessity (Maybe a)))
-> HNullify t context
hnulls forall (labels :: Labels) (necessity :: Necessity) a.
Col Identity ('Spec labels necessity (Maybe a))
null
      }
    hjust :: table (Col Identity) -> HMaybeTable table (Col Identity)
hjust table (Col Identity)
table = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HNullify table context -> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag = Col Identity ('Spec '["isJust"] 'Required (Maybe MaybeTag))
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (Maybe MaybeTag
-> Col Identity ('Spec '["isJust"] 'Required (Maybe MaybeTag))
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result (MaybeTag -> Maybe MaybeTag
forall a. a -> Maybe a
Just MaybeTag
IsJust))
      , hjust :: HNullify table (Col Identity)
hjust = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a)))
-> table (Col Identity) -> HNullify table (Col Identity)
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 Identity ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (Nullify a))
nullifier table (Col Identity)
table
      }
{-# INLINABLE toHMaybeTable #-}


fromHMaybeTable :: HTable t => HMaybeTable t (Col Identity) -> Maybe (t (Col Identity))
fromHMaybeTable :: forall (t :: HTable).
HTable t =>
HMaybeTable t (Col Identity) -> Maybe (t (Col Identity))
fromHMaybeTable HMaybeTable {HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context
-> HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
htag, HNullify t (Col Identity)
hjust :: HNullify t (Col Identity)
hjust :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context -> HNullify table context
hjust} = case HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag of
  HIdentity (Result a
tag) -> a
Maybe MaybeTag
tag Maybe MaybeTag -> t (Col Identity) -> Maybe (t (Col Identity))
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$>
    case (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (Nullify a))
 -> Maybe (Col Identity ('Spec labels necessity a)))
-> HNullify t (Col Identity) -> Maybe (t (Col Identity))
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 Identity ('Spec labels necessity (Nullify a))
-> Maybe (Col Identity ('Spec labels necessity a))
unnullifier HNullify t (Col Identity)
hjust of
      Maybe (t (Col Identity))
Nothing -> [Char] -> t (Col Identity)
forall a. HasCallStack => [Char] -> a
error [Char]
"fromHMaybeTable: mismatch between tag and data"
      Just t (Col Identity)
just -> t (Col Identity)
just
{-# INLINABLE fromHMaybeTable #-}


toHNonEmptyTable :: HTable t => NonEmpty (t (Col Identity)) -> HNonEmptyTable t (Col Identity)
toHNonEmptyTable :: forall (t :: HTable).
HTable t =>
NonEmpty (t (Col Identity)) -> HNonEmptyTable t (Col Identity)
toHNonEmptyTable = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> NonEmpty (Col Identity ('Spec labels necessity a))
 -> Col Identity ('Spec labels necessity (NonEmpty a)))
-> NonEmpty (t (Col Identity))
-> HVectorize NonEmpty t (Col Identity)
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 necessity (list a)))
-> f (t context) -> HVectorize list t context'
hvectorize forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> NonEmpty (Col Identity ('Spec labels necessity a))
-> Col Identity ('Spec labels necessity (NonEmpty a))
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> f (Col Identity ('Spec labels necessity a))
-> Col Identity ('Spec labels necessity (f a))
vectorizer
{-# INLINABLE toHNonEmptyTable #-}


fromHNonEmptyTable :: HTable t => HNonEmptyTable t (Col Identity) -> NonEmpty (t (Col Identity))
fromHNonEmptyTable :: forall (t :: HTable).
HTable t =>
HNonEmptyTable t (Col Identity) -> NonEmpty (t (Col Identity))
fromHNonEmptyTable = (forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> Col Identity ('Spec labels necessity (NonEmpty a))
 -> NonEmpty (Col Identity ('Spec labels necessity a)))
-> HVectorize NonEmpty t (Col Identity)
-> NonEmpty (t (Col Identity))
forall (t :: HTable) (f :: * -> *) (list :: * -> *)
       (context :: HContext) (context' :: HContext).
(HTable t, Repeat f, Vector list) =>
(forall (labels :: Labels) (necessity :: Necessity) a.
 SSpec ('Spec labels necessity a)
 -> context ('Spec labels necessity (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 Identity ('Spec labels necessity (NonEmpty a))
-> NonEmpty (Col Identity ('Spec labels necessity a))
forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (f a))
-> f (Col Identity ('Spec labels necessity a))
unvectorizer
{-# INLINABLE fromHNonEmptyTable #-}


toHTheseTable :: (HTable t, HTable u)
  => These (t (Col Identity)) (u (Col Identity))
  -> HTheseTable t u (Col Identity)
toHTheseTable :: forall (t :: HTable) (u :: HTable).
(HTable t, HTable u) =>
These (t (Col Identity)) (u (Col Identity))
-> HTheseTable t u (Col Identity)
toHTheseTable These (t (Col Identity)) (u (Col Identity))
tables = HTheseTable :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HIdentity ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) context
-> HNullify here context
-> HIdentity
     ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) context
-> HNullify there context
-> HTheseTable here there context
HTheseTable
  { hhereTag :: HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
hhereTag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
-> HIdentity
     ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Identity)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
hhereTag
  , HNullify t (Col Identity)
hhere :: HNullify t (Col Identity)
hhere :: HNullify t (Col Identity)
hhere
  , hthereTag :: HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
hthereTag = HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
-> HIdentity
     ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Identity)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
hthereTag
  , HNullify u (Col Identity)
hthere :: HNullify u (Col Identity)
hthere :: HNullify u (Col Identity)
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 Identity)
hhereTag
      , hjust :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context -> HNullify table context
hjust = HNullify t (Col Identity)
hhere
      } = Maybe (t (Col Identity)) -> HMaybeTable t (Col Identity)
forall (t :: HTable).
HTable t =>
Maybe (t (Col Identity)) -> HMaybeTable t (Col Identity)
toHMaybeTable (These (t (Col Identity)) (u (Col Identity))
-> Maybe (t (Col Identity))
forall a b. These a b -> Maybe a
justHere These (t (Col Identity)) (u (Col Identity))
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 Identity)
hthereTag
      , hjust :: forall (table :: HTable) (context :: HContext).
HMaybeTable table context -> HNullify table context
hjust = HNullify u (Col Identity)
hthere
      } = Maybe (u (Col Identity)) -> HMaybeTable u (Col Identity)
forall (t :: HTable).
HTable t =>
Maybe (t (Col Identity)) -> HMaybeTable t (Col Identity)
toHMaybeTable (These (t (Col Identity)) (u (Col Identity))
-> Maybe (u (Col Identity))
forall a b. These a b -> Maybe b
justThere These (t (Col Identity)) (u (Col Identity))
tables)
{-# INLINABLE toHTheseTable #-}


fromHTheseTable :: (HTable t, HTable u)
  => HTheseTable t u (Col Identity)
  -> These (t (Col Identity)) (u (Col Identity))
fromHTheseTable :: forall (t :: HTable) (u :: HTable).
(HTable t, HTable u) =>
HTheseTable t u (Col Identity)
-> These (t (Col Identity)) (u (Col Identity))
fromHTheseTable HTheseTable {HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
hhereTag :: HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
hhereTag :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HTheseTable here there context
-> HIdentity
     ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) context
hhereTag, HNullify t (Col Identity)
hhere :: HNullify t (Col Identity)
hhere :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HTheseTable here there context -> HNullify here context
hhere, HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
hthereTag :: HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
hthereTag :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HTheseTable here there context
-> HIdentity
     ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) context
hthereTag, HNullify u (Col Identity)
hthere :: HNullify u (Col Identity)
hthere :: forall (here :: HTable) (there :: HTable) (context :: HContext).
HTheseTable here there context -> HNullify there context
hthere} =
  case (HMaybeTable t (Col Identity) -> Maybe (t (Col Identity))
forall (t :: HTable).
HTable t =>
HMaybeTable t (Col Identity) -> Maybe (t (Col Identity))
fromHMaybeTable HMaybeTable t (Col Identity)
mhere, HMaybeTable u (Col Identity) -> Maybe (u (Col Identity))
forall (t :: HTable).
HTable t =>
HMaybeTable t (Col Identity) -> Maybe (t (Col Identity))
fromHMaybeTable HMaybeTable u (Col Identity)
mthere) of
    (Just t (Col Identity)
a, Maybe (u (Col Identity))
Nothing) -> t (Col Identity) -> These (t (Col Identity)) (u (Col Identity))
forall a b. a -> These a b
This t (Col Identity)
a
    (Maybe (t (Col Identity))
Nothing, Just u (Col Identity)
b) -> u (Col Identity) -> These (t (Col Identity)) (u (Col Identity))
forall a b. b -> These a b
That u (Col Identity)
b
    (Just t (Col Identity)
a, Just u (Col Identity)
b) -> t (Col Identity)
-> u (Col Identity) -> These (t (Col Identity)) (u (Col Identity))
forall a b. a -> b -> These a b
These t (Col Identity)
a u (Col Identity)
b
    (Maybe (t (Col Identity)), Maybe (u (Col Identity)))
_ -> [Char] -> These (t (Col Identity)) (u (Col Identity))
forall a. HasCallStack => [Char] -> a
error [Char]
"fromHTheseTable: mismatch between tags and data"
  where
    mhere :: HMaybeTable t (Col Identity)
mhere = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HNullify table context -> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag = HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Identity)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel HIdentity
  ('Spec '["hasHere"] 'Required (Maybe MaybeTag)) (Col Identity)
hhereTag
      , hjust :: HNullify t (Col Identity)
hjust = HNullify t (Col Identity)
hhere
      }
    mthere :: HMaybeTable u (Col Identity)
mthere = HMaybeTable :: forall (table :: HTable) (context :: HContext).
HIdentity ('Spec '["isJust"] 'Required (Maybe MaybeTag)) context
-> HNullify table context -> HMaybeTable table context
HMaybeTable
      { htag :: HIdentity
  ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
htag = HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
-> HIdentity
     ('Spec '["isJust"] 'Required (Maybe MaybeTag)) (Col Identity)
forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Identity)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel HIdentity
  ('Spec '["hasThere"] 'Required (Maybe MaybeTag)) (Col Identity)
hthereTag
      , hjust :: HNullify u (Col Identity)
hjust = HNullify u (Col Identity)
hthere
      }
{-# INLINABLE fromHTheseTable #-}


null :: Col Identity ('Spec labels necessity (Maybe a))
null :: forall (labels :: Labels) (necessity :: Necessity) a.
Col Identity ('Spec labels necessity (Maybe a))
null = Maybe a -> Col Identity ('Spec labels necessity (Maybe a))
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result Maybe a
forall a. Maybe a
Nothing


nullifier :: ()
  => SSpec ('Spec labels necessity a)
  -> Col Identity ('Spec labels necessity a)
  -> Col Identity ('Spec labels necessity (Nullify a))
nullifier :: forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (Nullify a))
nullifier SSpec {Nullity a
nullity :: forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a) -> Nullity a
nullity :: Nullity a
nullity} (Result a
a) = Maybe (Unnullify' (IsMaybe a) a)
-> Col
     Identity
     ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result (Maybe (Unnullify' (IsMaybe a) a)
 -> Col
      Identity
      ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a))))
-> Maybe (Unnullify' (IsMaybe a) a)
-> Col
     Identity
     ('Spec labels necessity (Maybe (Unnullify' (IsMaybe a) a)))
forall a b. (a -> b) -> a -> b
$ case Nullity a
nullity of
  Nullity a
Null -> a
Maybe (Unnullify' (IsMaybe a) a)
a
  Nullity a
NotNull -> a -> Maybe a
forall a. a -> Maybe a
Just a
a


unnullifier :: ()
  => SSpec ('Spec labels necessity a)
  -> Col Identity ('Spec labels necessity (Nullify a))
  -> Maybe (Col Identity ('Spec labels necessity a))
unnullifier :: forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (Nullify a))
-> Maybe (Col Identity ('Spec labels necessity a))
unnullifier SSpec {Nullity a
nullity :: Nullity a
nullity :: forall (labels :: Labels) (necessity :: Necessity) a.
SSpec ('Spec labels necessity a) -> Nullity a
nullity} (Result a
a) =
  case Nullity a
nullity of
    Nullity a
Null -> Col Identity ('Spec labels necessity a)
-> Maybe (Col Identity ('Spec labels necessity a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Col Identity ('Spec labels necessity a)
 -> Maybe (Col Identity ('Spec labels necessity a)))
-> Col Identity ('Spec labels necessity a)
-> Maybe (Col Identity ('Spec labels necessity a))
forall a b. (a -> b) -> a -> b
$ a -> Col Identity ('Spec labels necessity a)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result a
a
    Nullity a
NotNull -> a -> Col Identity ('Spec labels necessity a)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result (a -> Col Identity ('Spec labels necessity a))
-> Maybe a -> Maybe (Col Identity ('Spec labels necessity a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a
Maybe a
a


vectorizer :: Functor f
  => SSpec ('Spec labels necessity a)
  -> f (Col Identity ('Spec labels necessity a))
  -> Col Identity ('Spec labels necessity (f a))
vectorizer :: forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> f (Col Identity ('Spec labels necessity a))
-> Col Identity ('Spec labels necessity (f a))
vectorizer SSpec ('Spec labels necessity a)
_ = f a -> Col Identity ('Spec labels necessity (f a))
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result (f a -> Col Identity ('Spec labels necessity (f a)))
-> (f (Col Identity ('Spec labels necessity a)) -> f a)
-> f (Col Identity ('Spec labels necessity a))
-> Col Identity ('Spec labels necessity (f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Col Identity ('Spec labels necessity a) -> a)
-> f (Col Identity ('Spec labels necessity a)) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Result a
a) -> a
a
a)


unvectorizer :: Functor f
  => SSpec ('Spec labels necessity a)
  -> Col Identity ('Spec labels necessity (f a))
  -> f (Col Identity ('Spec labels necessity a))
unvectorizer :: forall (f :: * -> *) (labels :: Labels) (necessity :: Necessity) a.
Functor f =>
SSpec ('Spec labels necessity a)
-> Col Identity ('Spec labels necessity (f a))
-> f (Col Identity ('Spec labels necessity a))
unvectorizer SSpec ('Spec labels necessity a)
_ (Result a
results) = a -> Col Identity ('Spec labels necessity a)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result (a -> Col Identity ('Spec labels necessity a))
-> f a -> f (Col Identity ('Spec labels necessity a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a
f a
results


relabel :: ()
  => HIdentity ('Spec labels necessity a) (Col Identity)
  -> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel :: forall (labels :: Labels) (necessity :: Necessity) a
       (relabels :: Labels).
HIdentity ('Spec labels necessity a) (Col Identity)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
relabel (HIdentity (Result a
a)) = Col Identity ('Spec relabels necessity a)
-> HIdentity ('Spec relabels necessity a) (Col Identity)
forall (spec :: Spec) (context :: HContext).
context spec -> HIdentity spec context
HIdentity (a -> Col Identity ('Spec relabels necessity a)
forall labels (necessity :: Labels) (a :: Necessity).
labels -> Col Identity ('Spec necessity a labels)
Result a
a)