{-# language DataKinds #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
{-# language UndecidableInstances #-}

module Rel8.Table.Null
  ( NullTable(..)
  , nullableTable, nullTable, nullifyTable, unsafeUnnullifyTable
  , isNullTable, isNonNullTable
  , nameNullTable
  , toMaybeTable, toNullTable
  )
where

-- base
import Data.Kind ( Type )
import Prelude hiding ( null, undefined )

-- comonad
import Control.Comonad ( extract )

-- rel8
import Rel8.Expr ( Expr )
import Rel8.Expr.Bool ( not_ )
import Rel8.Kind.Context ( Reifiable )
import qualified Rel8.Schema.Kind as K
import Rel8.Schema.Name ( Name )
import Rel8.Table
  ( Table, Columns, Context, fromColumns, toColumns
  , FromExprs, fromResult, toResult
  , Transpose
  )
import Rel8.Table.Alternative
  ( AltTable, (<|>:)
  , AlternativeTable, emptyTable
  )
import Rel8.Table.Bool ( bool )
import Rel8.Table.Eq ( EqTable, eqTable )
import Rel8.Table.Maybe ( MaybeTable, justTable, maybeTable, nothingTable )
import Rel8.Table.Nullify ( Nullify, isNull )
import Rel8.Table.Ord ( OrdTable, ordTable )
import Rel8.Table.Projection ( Projectable, project )
import Rel8.Table.Serialize ( ToExprs )
import Rel8.Table.Undefined ( undefined )


-- | @NullTable t@ is the table @t@, but where all the columns in @t@ have the
-- possibility of being 'Rel8.null'. This is very similar to
-- 'Rel8.MaybeTable', except that it does not use an extra tag field, so it
-- cannot distinguish between @Nothing@ and @Just Nothing@ if nested. In other
-- words, if all of the columns of the @t@ passed to @NullTable@ are already
-- nullable, then @NullTable@ has no effect.
type NullTable :: K.Context -> Type -> Type
newtype NullTable context a = NullTable (Nullify context a)


instance Projectable (NullTable context) where
  project :: forall a b.
Projecting a b =>
Projection a b -> NullTable context a -> NullTable context b
project Projection a b
f (NullTable Nullify context a
a) = Nullify context b -> NullTable context b
forall (context :: * -> *) a.
Nullify context a -> NullTable context a
NullTable (Projection a b -> Nullify context a -> Nullify context b
forall a b.
Projecting a b =>
Projection a b -> Nullify context a -> Nullify context b
forall (f :: * -> *) a b.
(Projectable f, Projecting a b) =>
Projection a b -> f a -> f b
project Projection a b
f Nullify context a
a)


instance context ~ Expr => AltTable (NullTable context) where
  NullTable context a
ma <|>: :: forall a.
Table Expr a =>
NullTable context a -> NullTable context a -> NullTable context a
<|>: NullTable context a
mb = NullTable context a
-> NullTable context a -> Expr Bool -> NullTable context a
forall a. Table Expr a => a -> a -> Expr Bool -> a
bool NullTable context a
ma NullTable context a
mb (NullTable Expr a -> Expr Bool
forall a. Table Expr a => NullTable Expr a -> Expr Bool
isNullTable NullTable context a
NullTable Expr a
ma)


instance context ~ Expr => AlternativeTable (NullTable context) where
  emptyTable :: forall a. Table Expr a => NullTable context a
emptyTable = NullTable context a
NullTable Expr a
forall a. Table Expr a => NullTable Expr a
nullTable


instance (Table context a, Reifiable context, context ~ context') =>
  Table context' (NullTable context a)
 where
  type Columns (NullTable context a) = Columns (Nullify context a)
  type Context (NullTable context a) = Context (Nullify context a)
  type FromExprs (NullTable context a) = FromExprs (Nullify context a)
  type Transpose to (NullTable context a) = NullTable to (Transpose to a)

  toColumns :: NullTable context a -> Columns (NullTable context a) context'
toColumns (NullTable Nullify context a
a) = Nullify context a -> Columns (Nullify context a) context'
forall (context :: * -> *) a.
Table context a =>
a -> Columns a context
toColumns Nullify context a
a
  fromColumns :: Columns (NullTable context a) context' -> NullTable context a
fromColumns = Nullify context a -> NullTable context a
forall (context :: * -> *) a.
Nullify context a -> NullTable context a
NullTable (Nullify context a -> NullTable context a)
-> (HNullify (Columns a) context' -> Nullify context a)
-> HNullify (Columns a) context'
-> NullTable context a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HNullify (Columns a) context' -> Nullify context a
Columns (Nullify context a) context' -> Nullify context a
forall (context :: * -> *) a.
Table context a =>
Columns a context -> a
fromColumns

  toResult :: FromExprs (NullTable context a)
-> Columns (NullTable context a) Result
toResult = forall (context :: * -> *) a.
Table context a =>
FromExprs a -> Columns a Result
toResult @_ @(Nullify context a)
  fromResult :: Columns (NullTable context a) Result
-> FromExprs (NullTable context a)
fromResult = forall (context :: * -> *) a.
Table context a =>
Columns a Result -> FromExprs a
fromResult @_ @(Nullify context a)


instance (EqTable a, context ~ Expr) => EqTable (NullTable context a) where
  eqTable :: Columns (NullTable context a) (Dict (Sql DBEq))
eqTable = forall a. EqTable a => Columns a (Dict (Sql DBEq))
eqTable @(Nullify context a)


instance (OrdTable a, context ~ Expr) => OrdTable (NullTable context a) where
  ordTable :: Columns (NullTable context a) (Dict (Sql DBOrd))
ordTable = forall a. OrdTable a => Columns a (Dict (Sql DBOrd))
ordTable @(Nullify context a)


instance (ToExprs exprs a, context ~ Expr) =>
  ToExprs (NullTable context exprs) (Maybe a)


-- | Check if any of the non-nullable fields of @a@ are 'Rel8.null' under the
-- 'NullTable'. Returns 'Rel8.false' if @a@ has no non-nullable fields.
isNullTable :: Table Expr a => NullTable Expr a -> Expr Bool
isNullTable :: forall a. Table Expr a => NullTable Expr a -> Expr Bool
isNullTable (NullTable Nullify Expr a
a) = Nullify Expr a -> Expr Bool
forall a. Table Expr a => Nullify Expr a -> Expr Bool
isNull Nullify Expr a
a


-- | The inverse of 'isNullTable'.
isNonNullTable :: Table Expr a => NullTable Expr a -> Expr Bool
isNonNullTable :: forall a. Table Expr a => NullTable Expr a -> Expr Bool
isNonNullTable = Expr Bool -> Expr Bool
not_ (Expr Bool -> Expr Bool)
-> (NullTable Expr a -> Expr Bool) -> NullTable Expr a -> Expr Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NullTable Expr a -> Expr Bool
forall a. Table Expr a => NullTable Expr a -> Expr Bool
isNullTable


-- | Like 'Rel8.nullable'.
nullableTable :: (Table Expr a, Table Expr b)
  => b -> (a -> b) -> NullTable Expr a -> b
nullableTable :: forall a b.
(Table Expr a, Table Expr b) =>
b -> (a -> b) -> NullTable Expr a -> b
nullableTable b
b a -> b
f ma :: NullTable Expr a
ma@(NullTable Nullify Expr a
a) = b -> b -> Expr Bool -> b
forall a. Table Expr a => a -> a -> Expr Bool -> a
bool (a -> b
f (Nullify Expr a -> a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr a
a)) b
b (NullTable Expr a -> Expr Bool
forall a. Table Expr a => NullTable Expr a -> Expr Bool
isNullTable NullTable Expr a
ma)


-- | The null table. Like 'Rel8.null'.
nullTable :: Table Expr a => NullTable Expr a
nullTable :: forall a. Table Expr a => NullTable Expr a
nullTable = Nullify Expr a -> NullTable Expr a
forall (context :: * -> *) a.
Nullify context a -> NullTable context a
NullTable (a -> Nullify Expr a
forall a. a -> Nullify Expr a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Table Expr a => a
undefined)


-- | Lift any table into 'NullTable'. Like 'Rel8.nullify'.
nullifyTable :: a -> NullTable Expr a
nullifyTable :: forall a. a -> NullTable Expr a
nullifyTable = Nullify Expr a -> NullTable Expr a
forall (context :: * -> *) a.
Nullify context a -> NullTable context a
NullTable (Nullify Expr a -> NullTable Expr a)
-> (a -> Nullify Expr a) -> a -> NullTable Expr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Nullify Expr a
forall a. a -> Nullify Expr a
forall (f :: * -> *) a. Applicative f => a -> f a
pure


unsafeUnnullifyTable :: NullTable Expr a -> a
unsafeUnnullifyTable :: forall a. NullTable Expr a -> a
unsafeUnnullifyTable (NullTable Nullify Expr a
a) = Nullify Expr a -> a
forall a. Nullify Expr a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract Nullify Expr a
a


-- | Construct a 'NullTable' in the 'Name' context. This can be useful if you
-- have a 'NullTable' that you are storing in a table and need to construct a
-- 'TableSchema'.
nameNullTable :: a -> NullTable Name a
nameNullTable :: forall a. a -> NullTable Name a
nameNullTable = Nullify Name a -> NullTable Name a
forall (context :: * -> *) a.
Nullify context a -> NullTable context a
NullTable (Nullify Name a -> NullTable Name a)
-> (a -> Nullify Name a) -> a -> NullTable Name a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Nullify Name a
forall a. a -> Nullify Name a
forall (f :: * -> *) a. Applicative f => a -> f a
pure


-- | Convert a 'NullTable' to a 'MaybeTable'.
toMaybeTable :: Table Expr a => NullTable Expr a -> MaybeTable Expr a
toMaybeTable :: forall a. Table Expr a => NullTable Expr a -> MaybeTable Expr a
toMaybeTable = MaybeTable Expr a
-> (a -> MaybeTable Expr a)
-> NullTable Expr a
-> MaybeTable Expr a
forall a b.
(Table Expr a, Table Expr b) =>
b -> (a -> b) -> NullTable Expr a -> b
nullableTable MaybeTable Expr a
forall a. Table Expr a => MaybeTable Expr a
nothingTable a -> MaybeTable Expr a
forall a. a -> MaybeTable Expr a
justTable


-- | Convert a 'MaybeTable' to a 'NullTable'. Note that if the underlying @a@
-- has no non-nullable fields, this is a lossy conversion.
toNullTable :: Table Expr a => MaybeTable Expr a -> NullTable Expr a
toNullTable :: forall a. Table Expr a => MaybeTable Expr a -> NullTable Expr a
toNullTable = NullTable Expr a
-> (a -> NullTable Expr a) -> MaybeTable Expr a -> NullTable Expr a
forall b a. Table Expr b => b -> (a -> b) -> MaybeTable Expr a -> b
maybeTable NullTable Expr a
forall a. Table Expr a => NullTable Expr a
nullTable a -> NullTable Expr a
forall a. a -> NullTable Expr a
nullifyTable