on 137ebed help sign in
rel8/src/Rel8/Table/Maybe.hs
254 lines 7.6 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveFunctor #-}
3{-# language DerivingStrategies #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language LambdaCase #-}
7{-# language MultiParamTypeClasses #-}
8{-# language NamedFieldPuns #-}
9{-# language ScopedTypeVariables #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TypeApplications #-}
12{-# language TypeFamilies #-}
13{-# language UndecidableInstances #-}
15module Rel8.Table.Maybe
16 ( MaybeTable(..)
17 , maybeTable, nothingTable, justTable
18 , isNothingTable, isJustTable
19 , ($?)
20 , nameMaybeTable
21 )
22where
24-- base
25import Data.Functor.Identity ( runIdentity )
26import Data.Kind ( Type )
27import Prelude hiding ( null, undefined )
29-- rel8
30import Rel8.Expr ( Expr )
31import Rel8.Expr.Bool ( boolExpr )
32import Rel8.Expr.Null ( isNull, isNonNull, null, nullify )
33import Rel8.Schema.Context.Label
34 ( Labelable, HLabelable, hlabeler, hunlabeler
35 )
36import Rel8.Schema.Context.Nullify
37 ( Nullifiable, ConstrainTag
38 , HNullifiable, HConstrainTag
39 , hencodeTag, hdecodeTag
40 , hnullifier, hunnullifier
41 )
42import Rel8.Schema.HTable ( HTable )
43import Rel8.Schema.HTable.Identity ( HIdentity(..) )
44import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
45import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
46import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
47import Rel8.Schema.Name ( Name )
48import Rel8.Schema.Null ( Nullify, Nullity( Null, NotNull ), Sql, nullable )
49import Rel8.Table
50 ( Table, Columns, Context, fromColumns, toColumns
51 , reify, unreify
52 )
53import Rel8.Table.Alternative
54 ( AltTable, (<|>:)
55 , AlternativeTable, emptyTable
56 )
57import Rel8.Table.Bool ( bool )
58import Rel8.Table.Eq ( EqTable, eqTable )
59import Rel8.Table.Ord ( OrdTable, ordTable )
60import Rel8.Table.Recontextualize ( Recontextualize )
61import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
62import Rel8.Table.Tag ( Tag(..), fromExpr, fromName )
63import Rel8.Table.Undefined ( undefined )
64import Rel8.Type ( DBType )
65import Rel8.Type.Tag ( MaybeTag )
67-- semigroupoids
68import Data.Functor.Apply ( Apply, (<.>) )
69import Data.Functor.Bind ( Bind, (>>-) )
72-- | @MaybeTable t@ is the table @t@, but as the result of an outer join. If
73-- the outer join fails to match any rows, this is essentialy @Nothing@, and if
74-- the outer join does match rows, this is like @Just@. Unfortunately, SQL
75-- makes it impossible to distinguish whether or not an outer join matched any
76-- rows based generally on the row contents - if you were to join a row
77-- entirely of nulls, you can't distinguish if you matched an all null row, or
78-- if the match failed. For this reason @MaybeTable@ contains an extra field -
79-- a "nullTag" - to track whether or not the outer join produced any rows.
80type MaybeTable :: Type -> Type
81data MaybeTable a = MaybeTable
82 { tag :: Tag "isJust" (Maybe MaybeTag)
83 , just :: a
84 }
85 deriving stock Functor
88instance Apply MaybeTable where
89 MaybeTable tag f <.> MaybeTable tag' a = MaybeTable (tag <> tag') (f a)
92-- | Has the same behavior as the @Applicative@ instance for @Maybe@. See also:
93-- 'Rel8.traverseMaybeTable'.
94instance Applicative MaybeTable where
95 (<*>) = (<.>)
96 pure = justTable
99instance Bind MaybeTable where
100 MaybeTable tag a >>- f = case f a of
101 MaybeTable tag' b -> MaybeTable (tag <> tag') b
104-- | Has the same behavior as the @Monad@ instance for @Maybe@.
105instance Monad MaybeTable where
106 (>>=) = (>>-)
109instance AltTable MaybeTable where
110 ma@(MaybeTable tag a) <|>: MaybeTable tag' b = MaybeTable
111 { tag = (tag <> tag')
112 { expr = boolExpr (expr tag) (expr tag') condition
113 }
114 , just = bool a b condition
115 }
116 where
117 condition = isNothingTable ma
120instance AlternativeTable MaybeTable where
121 emptyTable = nothingTable
124instance (Table Expr a, Semigroup a) => Semigroup (MaybeTable a) where
125 ma <> mb = maybeTable mb (\a -> maybeTable ma (justTable . (a <>)) mb) ma
128instance (Table Expr a, Semigroup a) => Monoid (MaybeTable a) where
129 mempty = nothingTable
132instance
133 ( Table context a
134 , Labelable context, Nullifiable context
135 , ConstrainTag context MaybeTag
136 ) => Table context (MaybeTable a)
137 where
138 type Columns (MaybeTable a) = HMaybeTable (Columns a)
139 type Context (MaybeTable a) = Context a
141 toColumns = toColumns1 toColumns
142 fromColumns = fromColumns1 fromColumns
143 reify = fmap fmap reify
144 unreify = fmap fmap unreify
147instance
148 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
149 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
150 , Recontextualize from to a b
151 )
152 => Recontextualize from to (MaybeTable a) (MaybeTable b)
155instance EqTable a => EqTable (MaybeTable a) where
156 eqTable = toColumns1 id (justTable (eqTable @a))
159instance OrdTable a => OrdTable (MaybeTable a) where
160 ordTable = toColumns1 id (justTable (ordTable @a))
163type instance FromExprs (MaybeTable a) = Maybe (FromExprs a)
166instance ToExprs exprs a => ToExprs (MaybeTable exprs) (Maybe a) where
167 fromResult = fmap (fromResult @exprs) . fromColumns
168 toResult = toColumns . fmap (toResult @exprs)
171-- | Check if a @MaybeTable@ is absent of any row. Like 'Data.Maybe.isNothing'.
172isNothingTable :: MaybeTable a -> Expr Bool
173isNothingTable (MaybeTable tag _) = isNull (expr tag)
176-- | Check if a @MaybeTable@ contains a row. Like 'Data.Maybe.isJust'.
177isJustTable :: MaybeTable a -> Expr Bool
178isJustTable (MaybeTable tag _) = isNonNull (expr tag)
181-- | Perform case analysis on a 'MaybeTable'. Like 'maybe'.
182maybeTable :: Table Expr b => b -> (a -> b) -> MaybeTable a -> b
183maybeTable b f ma@(MaybeTable _ a) = bool (f a) b (isNothingTable ma)
184{-# INLINABLE maybeTable #-}
187-- | The null table. Like 'Nothing'.
188nothingTable :: Table Expr a => MaybeTable a
189nothingTable = MaybeTable (fromExpr null) undefined
192-- | Lift any table into 'MaybeTable'. Like 'Just'. Note you can also use
193-- 'pure'.
194justTable :: a -> MaybeTable a
195justTable = MaybeTable (fromExpr mempty)
198-- | Project a single expression out of a 'MaybeTable'. You can think of this
199-- operator like the '$' operator, but it also has the ability to return
200-- @null@.
201($?) :: forall a b. Sql DBType b
202 => (a -> Expr b) -> MaybeTable a -> Expr (Nullify b)
203f $? ma@(MaybeTable _ a) = case nullable @b of
204 Null -> boolExpr (f a) null (isNothingTable ma)
205 NotNull -> boolExpr (nullify (f a)) null (isNothingTable ma)
206infixl 4 $?
209-- | Construct a 'MaybeTable' in the 'Name' context. This can be useful if you
210-- have a 'MaybeTable' that you are storing in a table and need to construct a
211-- 'TableSchema'.
212nameMaybeTable
213 :: Name (Maybe MaybeTag)
214 -- ^ The name of the column to track whether a row is a 'justTable' or
215 -- 'nothingTable'.
216 -> a
217 -- ^ Names of the columns in @a@.
218 -> MaybeTable a
219nameMaybeTable = MaybeTable . fromName
222toColumns1 ::
223 ( HTable t
224 , HConstrainTag context MaybeTag
225 , HLabelable context
226 , HNullifiable context
227 )
228 => (a -> t context)
229 -> MaybeTable a
230 -> HMaybeTable t context
231toColumns1 f MaybeTable {tag, just} = HMaybeTable
232 { htag
233 , hjust = hlabel hlabeler $ hnullify (hnullifier tag isNonNull) $ f just
234 }
235 where
236 htag = HIdentity (hencodeTag tag)
239fromColumns1 ::
240 ( HTable t
241 , HConstrainTag context MaybeTag
242 , HLabelable context
243 , HNullifiable context
244 )
245 => (t context -> a)
246 -> HMaybeTable t context
247 -> MaybeTable a
248fromColumns1 f HMaybeTable {htag = HIdentity htag, hjust} = MaybeTable
249 { tag
250 , just = f $ runIdentity $
251 hunnullify (\a -> pure . hunnullifier a) (hunlabel hunlabeler hjust)
252 }
253 where
254 tag = hdecodeTag htag