on 00a36f5 help sign in
rel8/src/Rel8/Table/Maybe.hs
204 lines 6.2 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.Label ( Labelable, labeler, unlabeler )
33import Rel8.Schema.Context.Nullify
34 ( Nullifiable
35 , encodeTag, decodeTag
36 , nullifier, unnullifier
37 )
38import Rel8.Schema.HTable.Identity ( HIdentity(..) )
39import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
40import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
41import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
42import Rel8.Schema.Nullability
43 ( Nullify
44 , Nullability( Nullable, NonNullable )
45 , Sql, nullabilization
46 )
47import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
48import Rel8.Table.Alternative
49 ( AltTable, (<|>:)
50 , AlternativeTable, emptyTable
51 )
52import Rel8.Table.Bool ( bool )
53import Rel8.Table.Lifted
54 ( Table1, Columns1, ConstrainContext1, fromColumns1, toColumns1
55 )
56import Rel8.Table.Recontextualize ( Recontextualize )
57import Rel8.Table.Undefined ( undefined )
58import Rel8.Type ( DBType )
59import Rel8.Type.Tag ( MaybeTag( IsJust ) )
61-- semigroupoids
62import Data.Functor.Apply ( Apply, (<.>) )
63import Data.Functor.Bind ( Bind, (>>-) )
66-- | @MaybeTable t@ is the table @t@, but as the result of an outer join. If
67-- the outer join fails to match any rows, this is essentialy @Nothing@, and if
68-- the outer join does match rows, this is like @Just@. Unfortunately, SQL
69-- makes it impossible to distinguish whether or not an outer join matched any
70-- rows based generally on the row contents - if you were to join a row
71-- entirely of nulls, you can't distinguish if you matched an all null row, or
72-- if the match failed. For this reason @MaybeTable@ contains an extra field -
73-- a "nullTag" - to track whether or not the outer join produced any rows.
74type MaybeTable :: Type -> Type
75data MaybeTable a = MaybeTable
76 { tag :: Expr (Maybe MaybeTag)
77 , just :: a
78 }
79 deriving stock (Show, Functor)
82instance Apply MaybeTable where
83 MaybeTable tag f <.> MaybeTable tag' a = MaybeTable (tag <> tag') (f a)
86-- | Has the same behavior as the @Applicative@ instance for @Maybe@. See also:
87-- 'traverseMaybeTable'.
88instance Applicative MaybeTable where
89 (<*>) = (<.>)
90 pure = justTable
93instance Bind MaybeTable where
94 MaybeTable tag a >>- f = case f a of
95 MaybeTable tag' b -> MaybeTable (tag <> tag') b
98-- | Has the same behavior as the @Monad@ instance for @Maybe@. See also:
99-- 'bindMaybeTable'.
100instance Monad MaybeTable where
101 (>>=) = (>>-)
104instance AltTable MaybeTable where
105 ma@(MaybeTable tag a) <|>: MaybeTable tag' b = MaybeTable
106 { tag = boolExpr tag tag' condition
107 , just = bool a b condition
108 }
109 where
110 condition = isNothingTable ma
113instance AlternativeTable MaybeTable where
114 emptyTable = nothingTable
117instance (Table Expr a, Semigroup a) => Semigroup (MaybeTable a) where
118 ma <> mb = maybeTable mb (\a -> maybeTable ma (justTable . (a <>)) mb) ma
121instance (Table Expr a, Semigroup a) => Monoid (MaybeTable a) where
122 mempty = nothingTable
125instance Table1 MaybeTable where
126 type Columns1 MaybeTable = HMaybeTable
127 type ConstrainContext1 MaybeTable = Nullifiable
129 toColumns1 f MaybeTable {tag, just} = HMaybeTable
130 { htag
131 , hjust = hnullify (nullifier (isNonNull tag)) $ f just
132 }
133 where
134 htag = HIdentity (encodeTag tag)
136 fromColumns1 f HMaybeTable {htag = HIdentity htag, hjust} = MaybeTable
137 { tag
138 , just = f $ runIdentity $
139 hunnullify (\a -> pure . unnullifier (isNonNull tag) a) hjust
140 }
141 where
142 tag = decodeTag htag
144 {-# INLINABLE fromColumns1 #-}
145 {-# INLINABLE toColumns1 #-}
148instance
149 ( Table context a
150 , Labelable context, Nullifiable context
151 ) => Table context (MaybeTable a)
152 where
153 type Columns (MaybeTable a) = HMaybeTable (HLabel "Just" (Columns a))
154 type Context (MaybeTable a) = Context a
156 toColumns = toColumns1 (hlabel labeler . toColumns)
157 fromColumns = fromColumns1 (fromColumns . hunlabel unlabeler)
160instance
161 ( Labelable from, Nullifiable from
162 , Labelable to, Nullifiable to
163 , Recontextualize from to a b
164 ) => Recontextualize from to (MaybeTable a) (MaybeTable b)
167-- | Check if a @MaybeTable@ is absent of any row.. Like 'isNothing'.
168isNothingTable :: MaybeTable a -> Expr Bool
169isNothingTable (MaybeTable tag _) = isNull tag
172-- | Check if a @MaybeTable@ contains a row. Like 'isJust'.
173isJustTable :: MaybeTable a -> Expr Bool
174isJustTable (MaybeTable tag _) = isNonNull tag
177-- | Perform case analysis on a 'MaybeTable'. Like 'maybe'.
178maybeTable :: Table Expr b => b -> (a -> b) -> MaybeTable a -> b
179maybeTable b f ma@(MaybeTable _ a) = bool (f a) b (isNothingTable ma)
182-- | The null table. Like 'Nothing'.
183nothingTable :: Table Expr a => MaybeTable a
184nothingTable = MaybeTable null undefined
187-- | Lift any table into 'MaybeTable'. Like 'Just'. Note you can also use
188-- 'pure'.
189justTable :: a -> MaybeTable a
190justTable = MaybeTable (nullify (litExpr IsJust))
193-- | Project a single expression out of a 'MaybeTable'. You can think of this
194-- operator like the '$' operator, but it also has the ability to return
195-- @null@.
196--
197-- >>> select c $ fmap (fst $?) (optional (values [lit (True, False)]))
198-- [Just True]
199($?) :: forall a b. Sql DBType b
200 => (a -> Expr b) -> MaybeTable a -> Expr (Nullify b)
201f $? ma@(MaybeTable _ a) = case nullabilization @b of
202 Nullable -> boolExpr (f a) null (isNothingTable ma)
203 NonNullable -> boolExpr (nullify (f a)) null (isNothingTable ma)
204infixl 4 $?