on d21213d help sign in
rel8/src/Rel8/Table/These.hs
320 lines 8.8 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 RecordWildCards #-}
9{-# language ScopedTypeVariables #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TupleSections #-}
12{-# language TypeApplications #-}
13{-# language TypeFamilies #-}
14{-# language UndecidableInstances #-}
16{-# options_ghc -fno-warn-orphans #-}
18module Rel8.Table.These
19 ( TheseTable(..)
20 , theseTable, thisTable, thatTable, thoseTable
21 , isThisTable, isThatTable, isThoseTable
22 , hasHereTable, hasThereTable
23 , justHereTable, justThereTable
24 , nameTheseTable
25 )
26where
28-- base
29import Control.Applicative ( liftA2 )
30import Data.Bifunctor ( Bifunctor, bimap )
31import Data.Functor.Identity ( runIdentity )
32import Data.Kind ( Type )
33import Prelude hiding ( undefined )
35-- rel8
36import Rel8.Expr ( Expr )
37import Rel8.Expr.Bool ( (&&.), not_ )
38import Rel8.Expr.Null ( isNonNull )
39import Rel8.Schema.Context.Label
40 ( Labelable
41 , HLabelable, hlabeler, hunlabeler
42 )
43import Rel8.Schema.Context.Nullify
44 ( Nullifiable, ConstrainTag
45 , HNullifiable, HConstrainTag
46 , hencodeTag, hdecodeTag
47 , hnullifier, hunnullifier
48 )
49import Rel8.Schema.HTable ( HTable )
50import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
51import Rel8.Schema.HTable.Identity ( HIdentity(..) )
52import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
53import Rel8.Schema.HTable.These ( HTheseTable(..) )
54import Rel8.Schema.Name ( Name )
55import Rel8.Table
56 ( Table, Columns, Context, fromColumns, toColumns
57 , reify, unreify
58 )
59import Rel8.Table.Eq ( EqTable, eqTable )
60import Rel8.Table.Maybe
61 ( MaybeTable(..)
62 , maybeTable, justTable, nothingTable
63 , isJustTable
64 , nameMaybeTable
65 )
66import Rel8.Table.Ord ( OrdTable, ordTable )
67import Rel8.Table.Recontextualize ( Recontextualize )
68import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
69import Rel8.Table.Tag ( Tag(..) )
70import Rel8.Table.Undefined ( undefined )
71import Rel8.Type.Tag ( MaybeTag )
73-- semigroupoids
74import Data.Functor.Apply ( Apply, (<.>) )
75import Data.Functor.Bind ( Bind, (>>-) )
77-- these
78import Data.These ( These )
81-- | @TheseTable a b@ is a Rel8 table that contains either the table @a@, the
82-- table @b@, or both tables @a@ and @b@. You can construct @TheseTable@s using
83-- 'thisTable', 'thatTable' and 'thoseTable'. @TheseTable@s can be
84-- eliminated/pattern matched using 'theseTable'.
85--
86-- @TheseTable@ is operationally the same as Haskell's 'These' type, but
87-- adapted to work with Rel8.
88type TheseTable :: Type -> Type -> Type
89data TheseTable a b = TheseTable
90 { here :: MaybeTable a
91 , there :: MaybeTable b
92 }
93 deriving stock Functor
96instance Bifunctor TheseTable where
97 bimap f g (TheseTable a b) = TheseTable (fmap f a) (fmap g b)
100instance (Table Expr a, Semigroup a) => Apply (TheseTable a) where
101 fs <.> as = TheseTable
102 { here = here fs <> here as
103 , there = there fs <.> there as
104 }
107instance (Table Expr a, Semigroup a) => Applicative (TheseTable a)
108 where
109 pure = thatTable
110 (<*>) = (<.>)
113instance (Table Expr a, Semigroup a) => Bind (TheseTable a) where
114 TheseTable here1 ma >>- f = case ma >>- f' of
115 mtb -> TheseTable
116 { here = maybeTable here1 ((here1 <>) . fst) mtb
117 , there = snd <$> mtb
118 }
119 where
120 f' a = case f a of
121 TheseTable here2 mb -> (here2,) <$> mb
124instance (Table Expr a, Semigroup a) => Monad (TheseTable a) where
125 (>>=) = (>>-)
128instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
129 Semigroup (TheseTable a b)
130 where
131 a <> b = TheseTable
132 { here = here a <> here b
133 , there = there a <> there b
134 }
137instance
138 ( Table context a, Table context b
139 , Labelable context, Nullifiable context, ConstrainTag context MaybeTag
140 ) => Table context (TheseTable a b)
141 where
142 type Columns (TheseTable a b) = HTheseTable (Columns a) (Columns b)
143 type Context (TheseTable a b) = Context a
145 toColumns = toColumns2 toColumns toColumns
146 fromColumns = fromColumns2 fromColumns fromColumns
147 reify = liftA2 bimap reify reify
148 unreify = liftA2 bimap unreify unreify
151instance
152 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
153 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
154 , Recontextualize from to a1 b1
155 , Recontextualize from to a2 b2
156 ) =>
157 Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
160instance (EqTable a, EqTable b) => EqTable (TheseTable a b) where
161 eqTable = toColumns2 id id (thoseTable (eqTable @a) (eqTable @b))
164instance (OrdTable a, OrdTable b) => OrdTable (TheseTable a b) where
165 ordTable = toColumns2 id id (thoseTable (ordTable @a) (ordTable @b))
168type instance FromExprs (TheseTable a b) = These (FromExprs a) (FromExprs b)
171instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ TheseTable exprs1 exprs2) =>
172 ToExprs x (These a b)
173 where
174 fromResult =
175 bimap (fromResult @exprs1) (fromResult @exprs2) .
176 fromColumns
177 toResult =
178 toColumns .
179 bimap (toResult @exprs1) (toResult @exprs2)
182toHereTag :: Tag "isJust" a -> Tag "hasHere" a
183toHereTag Tag {..} = Tag {..}
186toThereTag :: Tag "isJust" a -> Tag "hasThere" a
187toThereTag Tag {..} = Tag {..}
190-- | Test if a 'TheseTable' was constructed with 'thisTable'.
191isThisTable :: TheseTable a b -> Expr Bool
192isThisTable a = hasHereTable a &&. not_ (hasThereTable a)
195-- | Test if a 'TheseTable' was constructed with 'thatTable'.
196isThatTable :: TheseTable a b -> Expr Bool
197isThatTable a = not_ (hasHereTable a) &&. hasThereTable a
200-- | Test if a 'TheseTable' was constructed with 'thoseTable'.
201isThoseTable :: TheseTable a b -> Expr Bool
202isThoseTable a = hasHereTable a &&. hasThereTable a
205-- | Test if the @a@ table of @TheseTable a b@ is present.
206hasHereTable :: TheseTable a b -> Expr Bool
207hasHereTable TheseTable {here} = isJustTable here
210-- | Test if the @b@ table of @TheseTable a b@ is present.
211hasThereTable :: TheseTable a b -> Expr Bool
212hasThereTable TheseTable {there} = isJustTable there
215-- | Attempt to project out the @a@ table of a @TheseTable a b@.
216justHereTable :: TheseTable a b -> MaybeTable a
217justHereTable = here
220-- | Attempt to project out the @b@ table of a @TheseTable a b@.
221justThereTable :: TheseTable a b -> MaybeTable b
222justThereTable = there
225-- | Construct a @TheseTable@. Corresponds to 'This'.
226thisTable :: Table Expr b => a -> TheseTable a b
227thisTable a = TheseTable (justTable a) nothingTable
230-- | Construct a @TheseTable@. Corresponds to 'That'.
231thatTable :: Table Expr a => b -> TheseTable a b
232thatTable b = TheseTable nothingTable (justTable b)
235-- | Construct a @TheseTable@. Corresponds to 'These'.
236thoseTable :: a -> b -> TheseTable a b
237thoseTable a b = TheseTable (justTable a) (justTable b)
240-- | Pattern match on a 'TheseTable'.
241theseTable :: Table Expr c
242 => (a -> c) -> (b -> c) -> (a -> b -> c) -> TheseTable a b -> c
243theseTable f g h TheseTable {here, there} =
244 maybeTable
245 (maybeTable undefined f here)
246 (\b -> maybeTable (g b) (`h` b) here)
247 there
250nameTheseTable :: ()
251 => Name (Maybe MaybeTag)
252 -> Name (Maybe MaybeTag)
253 -> a
254 -> b
255 -> TheseTable a b
256nameTheseTable here there a b =
257 TheseTable
258 { here = nameMaybeTable here a
259 , there = nameMaybeTable there b
260 }
263toColumns2 ::
264 ( HTable t
265 , HTable u
266 , HConstrainTag context MaybeTag
267 , HLabelable context
268 , HNullifiable context
269 )
270 => (a -> t context)
271 -> (b -> u context)
272 -> TheseTable a b
273 -> HTheseTable t u context
274toColumns2 f g TheseTable {here, there} = HTheseTable
275 { hhereTag = HIdentity $ hencodeTag (toHereTag (tag here))
276 , hhere =
277 hlabel hlabeler $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
278 , hthereTag = HIdentity $ hencodeTag (toThereTag (tag there))
279 , hthere =
280 hlabel hlabeler $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
281 }
284fromColumns2 ::
285 ( HTable t
286 , HTable u
287 , HConstrainTag context MaybeTag
288 , HLabelable context
289 , HNullifiable context
290 )
291 => (t context -> a)
292 -> (u context -> b)
293 -> HTheseTable t u context
294 -> TheseTable a b
295fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
296 { here =
297 let
298 tag = hdecodeTag $ unHIdentity hhereTag
299 in
300 MaybeTable
301 { tag
302 , just = f $
303 runIdentity $
304 hunnullify (\a -> pure . hunnullifier a) $
305 hunlabel hunlabeler
306 hhere
307 }
308 , there =
309 let
310 tag = hdecodeTag $ unHIdentity hthereTag
311 in
312 MaybeTable
313 { tag
314 , just = g $
315 runIdentity $
316 hunnullify (\a -> pure . hunnullifier a) $
317 hunlabel hunlabeler
318 hthere
319 }
320 }