on b983def help sign in
rel8/src/Rel8/Table/Maybe.hs
205 lines 6.3 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveFunctor #-}
3{-# language DerivingStrategies #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language MultiParamTypeClasses #-}
7{-# language NamedFieldPuns #-}
8{-# language ScopedTypeVariables #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeApplications #-}
11{-# language TypeFamilies #-}
12{-# language UndecidableInstances #-}
14module Rel8.Table.Maybe
15 ( MaybeTable(..)
16 , maybeTable, nothingTable, justTable
17 , isNothingTable, isJustTable
18 , ($?)
19 )
20where
22-- base
23import Data.Functor.Identity ( runIdentity )
24import Data.Kind ( Type )
25import Prelude hiding ( null, repeat, undefined, zipWith )
27-- rel8
28import Rel8.Expr ( Expr )
29import Rel8.Expr.Bool ( boolExpr )
30import Rel8.Expr.Null ( isNull, isNonNull, null, nullify )
31import Rel8.Expr.Serialize ( litExpr )
32import Rel8.Schema.Context ( DB )
33import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
34import Rel8.Schema.Context.Nullify
35 ( Nullifiable
36 , encodeTag, decodeTag
37 , nullifier, unnullifier
38 )
39import Rel8.Schema.HTable.Identity ( HIdentity(..) )
40import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
41import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
42import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
43import Rel8.Schema.Nullability
44 ( Nullify
45 , Nullability( Nullable, NonNullable )
46 , Sql, nullabilization
47 )
48import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
49import Rel8.Table.Alternative
50 ( AltTable, (<|>:)
51 , AlternativeTable, emptyTable
52 )
53import Rel8.Table.Bool ( bool )
54import Rel8.Table.Lifted
55 ( Table1, Columns1, ConstrainContext1, fromColumns1, toColumns1
56 )
57import Rel8.Table.Recontextualize ( Recontextualize )
58import Rel8.Table.Undefined ( undefined )
59import Rel8.Type ( DBType )
60import Rel8.Type.Tag ( MaybeTag( IsJust ) )
62-- semigroupoids
63import Data.Functor.Apply ( Apply, (<.>) )
64import Data.Functor.Bind ( Bind, (>>-) )
67-- | @MaybeTable t@ is the table @t@, but as the result of an outer join. If
68-- the outer join fails to match any rows, this is essentialy @Nothing@, and if
69-- the outer join does match rows, this is like @Just@. Unfortunately, SQL
70-- makes it impossible to distinguish whether or not an outer join matched any
71-- rows based generally on the row contents - if you were to join a row
72-- entirely of nulls, you can't distinguish if you matched an all null row, or
73-- if the match failed. For this reason @MaybeTable@ contains an extra field -
74-- a "nullTag" - to track whether or not the outer join produced any rows.
75type MaybeTable :: Type -> Type
76data MaybeTable a = MaybeTable
77 { tag :: Expr (Maybe MaybeTag)
78 , just :: a
79 }
80 deriving stock (Show, Functor)
83instance Apply MaybeTable where
84 MaybeTable tag f <.> MaybeTable tag' a = MaybeTable (tag <> tag') (f a)
87-- | Has the same behavior as the @Applicative@ instance for @Maybe@. See also:
88-- 'traverseMaybeTable'.
89instance Applicative MaybeTable where
90 (<*>) = (<.>)
91 pure = justTable
94instance Bind MaybeTable where
95 MaybeTable tag a >>- f = case f a of
96 MaybeTable tag' b -> MaybeTable (tag <> tag') b
99-- | Has the same behavior as the @Monad@ instance for @Maybe@. See also:
100-- 'bindMaybeTable'.
101instance Monad MaybeTable where
102 (>>=) = (>>-)
105instance AltTable MaybeTable where
106 ma@(MaybeTable tag a) <|>: MaybeTable tag' b = MaybeTable
107 { tag = boolExpr tag tag' condition
108 , just = bool a b condition
109 }
110 where
111 condition = isNothingTable ma
114instance AlternativeTable MaybeTable where
115 emptyTable = nothingTable
118instance (Table DB a, Semigroup a) => Semigroup (MaybeTable a) where
119 ma <> mb = maybeTable mb (\a -> maybeTable ma (justTable . (a <>)) mb) ma
122instance (Table DB a, Semigroup a) => Monoid (MaybeTable a) where
123 mempty = nothingTable
126instance Table1 MaybeTable where
127 type Columns1 MaybeTable = HMaybeTable
128 type ConstrainContext1 MaybeTable = Nullifiable
130 toColumns1 f MaybeTable {tag, just} = HMaybeTable
131 { htag
132 , hjust = hnullify (nullifier (isNonNull tag)) $ f just
133 }
134 where
135 htag = HIdentity (encodeTag tag)
137 fromColumns1 f HMaybeTable {htag = HIdentity htag, hjust} = MaybeTable
138 { tag
139 , just = f $ runIdentity $
140 hunnullify (\a -> pure . unnullifier (isNonNull tag) a) hjust
141 }
142 where
143 tag = decodeTag htag
145 {-# INLINABLE fromColumns1 #-}
146 {-# INLINABLE toColumns1 #-}
149instance
150 ( Table context a
151 , Labelable context, Nullifiable context
152 ) => Table context (MaybeTable a)
153 where
154 type Columns (MaybeTable a) = HMaybeTable (HLabel "Just" (Columns a))
155 type Context (MaybeTable a) = Context a
157 toColumns = toColumns1 (hlabel labeler . toColumns)
158 fromColumns = fromColumns1 (fromColumns . hunlabel unlabeler)
161instance
162 ( Labelable from, Nullifiable from
163 , Labelable to, Nullifiable to
164 , Recontextualize from to a b
165 ) => Recontextualize from to (MaybeTable a) (MaybeTable b)
168-- | Check if a @MaybeTable@ is absent of any row.. Like 'isNothing'.
169isNothingTable :: MaybeTable a -> Expr Bool
170isNothingTable (MaybeTable tag _) = isNull tag
173-- | Check if a @MaybeTable@ contains a row. Like 'isJust'.
174isJustTable :: MaybeTable a -> Expr Bool
175isJustTable (MaybeTable tag _) = isNonNull tag
178-- | Perform case analysis on a 'MaybeTable'. Like 'maybe'.
179maybeTable :: Table DB b => b -> (a -> b) -> MaybeTable a -> b
180maybeTable b f ma@(MaybeTable _ a) = bool (f a) b (isNothingTable ma)
183-- | The null table. Like 'Nothing'.
184nothingTable :: Table DB a => MaybeTable a
185nothingTable = MaybeTable null undefined
188-- | Lift any table into 'MaybeTable'. Like 'Just'. Note you can also use
189-- 'pure'.
190justTable :: a -> MaybeTable a
191justTable = MaybeTable (nullify (litExpr IsJust))
194-- | Project a single expression out of a 'MaybeTable'. You can think of this
195-- operator like the '$' operator, but it also has the ability to return
196-- @null@.
197--
198-- >>> select c $ fmap (fst $?) (optional (values [lit (True, False)]))
199-- [Just True]
200($?) :: forall a b. Sql DBType b
201 => (a -> Expr b) -> MaybeTable a -> Expr (Nullify b)
202f $? ma@(MaybeTable _ a) = case nullabilization @b of
203 Nullable -> boolExpr (f a) null (isNothingTable ma)
204 NonNullable -> boolExpr (nullify (f a)) null (isNothingTable ma)
205infixl 4 $?