{-# language DataKinds #-}
{-# language FlexibleInstances #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}

module Rel8.Schema.Context.Label
  ( Labelable( labeler, unlabeler )
  , HLabelable( hlabeler, hunlabeler )
  )
where

-- base
import Data.Kind ( Constraint )
import Prelude hiding ( null )

-- rel8
import Rel8.Schema.Context ( Interpretation )
import Rel8.Schema.Dict ( Dict( Dict ) )
import Rel8.Schema.Kind ( Context, HContext )
import Rel8.Schema.Spec ( Spec( Spec ) )
import Rel8.Schema.Result ( Col( R ), Result )
import Rel8.Schema.Spec.ConstrainDBType ( ConstrainDBType )


type Labelable :: Context -> Constraint
class Interpretation context => Labelable context where
  labeler :: ()
    => Col context ('Spec labels necessity a)
    -> Col context ('Spec (label ': labels) necessity a)

  unlabeler :: ()
    => Col context ('Spec (label ': labels) necessity a)
    -> Col context ('Spec labels necessity a)


instance Labelable Result where
  labeler :: Col Result ('Spec labels necessity a)
-> Col Result ('Spec (label : labels) necessity a)
labeler (R a) = a -> Col Result ('Spec (label : labels) necessity a)
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R a
a
  unlabeler :: Col Result ('Spec (label : labels) necessity a)
-> Col Result ('Spec labels necessity a)
unlabeler (R a) = a -> Col Result ('Spec labels necessity a)
forall a (labels :: Labels) (necessity :: Necessity).
a -> Col Result ('Spec labels necessity a)
R a
a


type HLabelable :: HContext -> Constraint
class HLabelable context where
  hlabeler :: ()
    => context ('Spec labels necessity a)
    -> context ('Spec (label ': labels) necessity a)

  hunlabeler :: ()
    => context ('Spec (label ': labels) necessity a)
    -> context ('Spec labels necessity a)


instance Labelable context => HLabelable (Col context) where
  hlabeler :: Col context ('Spec labels necessity a)
-> Col context ('Spec (label : labels) necessity a)
hlabeler = Col context ('Spec labels necessity a)
-> Col context ('Spec (label : 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
  hunlabeler :: Col context ('Spec (label : labels) necessity a)
-> Col context ('Spec labels necessity a)
hunlabeler = Col context ('Spec (label : labels) necessity a)
-> Col context ('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


instance HLabelable (Dict (ConstrainDBType constraint)) where
  hlabeler :: Dict (ConstrainDBType constraint) ('Spec labels necessity a)
-> Dict
     (ConstrainDBType constraint) ('Spec (label : labels) necessity a)
hlabeler Dict (ConstrainDBType constraint) ('Spec labels necessity a)
Dict = Dict
  (ConstrainDBType constraint) ('Spec (label : labels) necessity a)
forall a (c :: a -> Constraint) (a :: a). c a => Dict c a
Dict
  hunlabeler :: Dict
  (ConstrainDBType constraint) ('Spec (label : labels) necessity a)
-> Dict (ConstrainDBType constraint) ('Spec labels necessity a)
hunlabeler Dict
  (ConstrainDBType constraint) ('Spec (label : labels) necessity a)
Dict = Dict (ConstrainDBType constraint) ('Spec labels necessity a)
forall a (c :: a -> Constraint) (a :: a). c a => Dict c a
Dict