on 01b569f help sign in
rel8/src/Rel8/Schema/HTable/Nullify.hs
102 lines 2.8 kB history raw
1{-# language ConstraintKinds #-}
2{-# language DataKinds #-}
3{-# language DeriveAnyClass #-}
4{-# language DerivingStrategies #-}
5{-# language DeriveGeneric #-}
6{-# language FlexibleInstances #-}
7{-# language GADTs #-}
8{-# language LambdaCase #-}
9{-# language MultiParamTypeClasses #-}
10{-# language NamedFieldPuns #-}
11{-# language QuantifiedConstraints #-}
12{-# language RankNTypes #-}
13{-# language ScopedTypeVariables #-}
14{-# language StandaloneKindSignatures #-}
15{-# language TypeFamilies #-}
16{-# language UndecidableInstances #-}
18module Rel8.Schema.HTable.Nullify
19 ( HNullify( HNullify )
20 , Nullify
21 , hnulls, hnullify, hunnullify
22 )
23where
25-- base
26import Prelude hiding ( null )
28-- rel8
29import Rel8.Schema.HTable ( HTable, hfield, htabulate, htabulateA, hspecs )
30import qualified Rel8.Schema.Kind as K
31import Rel8.Schema.Null ( Nullity( Null, NotNull ) )
32import qualified Rel8.Schema.Null as Type ( Nullify )
33import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
35-- semigroupoids
36import Data.Functor.Apply ( Apply )
37import Rel8.Schema.HTable.MapTable
38import Rel8.FCF
39import GHC.Generics (Generic)
42type HNullify :: K.HTable -> K.HTable
43newtype HNullify table context = HNullify (HMapTable Nullify table context)
44 deriving stock Generic
45 deriving anyclass HTable
49-- | Transform a 'Spec' by allowing it to be @null@.
50data Nullify :: Spec -> Exp Spec
53type instance Eval (Nullify ('Spec labels a)) =
54 'Spec labels (Type.Nullify a)
57instance MapSpec Nullify where
58 mapInfo = \case
59 SSpec{labels, info, nullity} -> SSpec
60 { labels
61 , info
62 , nullity = case nullity of
63 Null -> Null
64 NotNull -> Null
65 }
68hnulls :: HTable t
69 => (forall labels a. ()
70 => SSpec ('Spec labels a)
71 -> context ('Spec labels (Type.Nullify a)))
72 -> HNullify t context
73hnulls null = HNullify $ htabulate $ \(HMapTableField field) -> case hfield hspecs field of
74 spec@SSpec {} -> null spec
75{-# INLINABLE hnulls #-}
78hnullify :: HTable t
79 => (forall labels a. ()
80 => SSpec ('Spec labels a)
81 -> context ('Spec labels a)
82 -> context ('Spec labels (Type.Nullify a)))
83 -> t context
84 -> HNullify t context
85hnullify nullifier a = HNullify $ htabulate $ \(HMapTableField field) ->
86 case hfield hspecs field of
87 spec@SSpec {} -> nullifier spec (hfield a field)
88{-# INLINABLE hnullify #-}
91hunnullify :: (HTable t, Apply m)
92 => (forall labels a. ()
93 => SSpec ('Spec labels a)
94 -> context ('Spec labels (Type.Nullify a))
95 -> m (context ('Spec labels a)))
96 -> HNullify t context
97 -> m (t context)
98hunnullify unnullifier (HNullify as) =
99 htabulateA $ \field -> case hfield hspecs field of
100 spec@SSpec {} -> case hfield as (HMapTableField field) of
101 a -> unnullifier spec a
102{-# INLINABLE hunnullify #-}